]> Cypherpunks.ru repositories - gostls13.git/commitdiff
syscall: avoid writing to p when Pipe(p) fails
authorRuss Cox <rsc@golang.org>
Wed, 8 Dec 2021 23:06:41 +0000 (18:06 -0500)
committerFilippo Valsorda <filippo@golang.org>
Thu, 9 Dec 2021 13:36:38 +0000 (13:36 +0000)
Generally speaking Go functions make no guarantees
about what has happened to result parameters on error,
and Pipe is no exception: callers should avoid looking at
p if Pipe returns an error.

However, we had a bug in which ForkExec was using the
content of p after a failed Pipe, and others may too.
As a robustness fix, make Pipe avoid writing to p on failure.

Updates #50057

Change-Id: Ie8955025dbd20702fabadc9bbe1d1a5ac0f36305
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1291271
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/370577
Run-TryBot: Filippo Valsorda <filippo@golang.org>
Trust: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alex Rakoczy <alex@golang.org>
src/syscall/syscall_aix.go
src/syscall/syscall_darwin.go
src/syscall/syscall_dragonfly.go
src/syscall/syscall_freebsd.go
src/syscall/syscall_linux.go
src/syscall/syscall_netbsd.go
src/syscall/syscall_openbsd.go
src/syscall/syscall_plan9.go
src/syscall/syscall_solaris.go

index 0f5101999ff9687d8ed71e9339cb835ee9f3206f..739c55f1793f282af629e8b4dd4f9533327621a6 100644 (file)
@@ -66,8 +66,10 @@ func Pipe(p []int) (err error) {
        }
        var pp [2]_C_int
        err = pipe(&pp)
-       p[0] = int(pp[0])
-       p[1] = int(pp[1])
+       if err == nil {
+               p[0] = int(pp[0])
+               p[1] = int(pp[1])
+       }
        return
 }
 
index 5bb34e300c5f5f18330cc980479353a7aecaa842..902d6e77e11cb2bbf0e9512704a43ed8e77532f6 100644 (file)
@@ -80,8 +80,10 @@ func Pipe(p []int) (err error) {
        }
        var q [2]int32
        err = pipe(&q)
-       p[0] = int(q[0])
-       p[1] = int(q[1])
+       if err == nil {
+               p[0] = int(q[0])
+               p[1] = int(q[1])
+       }
        return
 }
 
index cc92c4a93edc9c50c70119b147ec020a0e23bf61..f3c0f545214c8e2bffd9d2a5b7f0181cb61604cc 100644 (file)
@@ -98,8 +98,11 @@ func Pipe(p []int) (err error) {
        if len(p) != 2 {
                return EINVAL
        }
-       p[0], p[1], err = pipe()
-       return
+       r, w, err := pipe()
+       if err == nil {
+               p[0], p[1] = r, w
+       }
+       return err
 }
 
 //sysnb        pipe2(p *[2]_C_int, flags int) (r int, w int, err error)
@@ -111,7 +114,10 @@ func Pipe2(p []int, flags int) (err error) {
        var pp [2]_C_int
        // pipe2 on dragonfly takes an fds array as an argument, but still
        // returns the file descriptors.
-       p[0], p[1], err = pipe2(&pp, flags)
+       r, w, err := pipe2(&pp, flags)
+       if err == nil {
+               p[0], p[1] = r, w
+       }
        return err
 }
 
index 6f44b25cb9a415e546c81f5007188596c4d65541..ecb9ec825aa0dd3e60874004f8f7e51b2db3dcfe 100644 (file)
@@ -105,8 +105,10 @@ func Pipe2(p []int, flags int) error {
        }
        var pp [2]_C_int
        err := pipe2(&pp, flags)
-       p[0] = int(pp[0])
-       p[1] = int(pp[1])
+       if err == nil {
+               p[0] = int(pp[0])
+               p[1] = int(pp[1])
+       }
        return err
 }
 
index c0022996412665fea08ad66939edd5ee5a008346..abcf1d5dfefc5756761c337c344f40d6306981db 100644 (file)
@@ -173,8 +173,10 @@ func Pipe2(p []int, flags int) error {
        }
        var pp [2]_C_int
        err := pipe2(&pp, flags)
-       p[0] = int(pp[0])
-       p[1] = int(pp[1])
+       if err == nil {
+               p[0] = int(pp[0])
+               p[1] = int(pp[1])
+       }
        return err
 }
 
index cebef10be885949b984ddd1462f70479cb3fa21c..0d562cc78e42b3bd4efe31a57e1e7583979cb99e 100644 (file)
@@ -114,8 +114,10 @@ func Pipe2(p []int, flags int) error {
        }
        var pp [2]_C_int
        err := pipe2(&pp, flags)
-       p[0] = int(pp[0])
-       p[1] = int(pp[1])
+       if err == nil {
+               p[0] = int(pp[0])
+               p[1] = int(pp[1])
+       }
        return err
 }
 
index 195cf8617ca70f6e02eb13afa6be7685a70995a2..fa939ec5c8849501ba5467bebb7baf209b30a13e 100644 (file)
@@ -72,8 +72,10 @@ func Pipe2(p []int, flags int) error {
        }
        var pp [2]_C_int
        err := pipe2(&pp, flags)
-       p[0] = int(pp[0])
-       p[1] = int(pp[1])
+       if err == nil {
+               p[0] = int(pp[0])
+               p[1] = int(pp[1])
+       }
        return err
 }
 
index d16cad45d851da0de4cb30a954f98f3c3c349dbe..6a8ab97dc6cc9238d5d8af1073005156c94343d0 100644 (file)
@@ -198,8 +198,10 @@ func Pipe(p []int) (err error) {
        }
        var pp [2]int32
        err = pipe(&pp)
-       p[0] = int(pp[0])
-       p[1] = int(pp[1])
+       if err == nil {
+               p[0] = int(pp[0])
+               p[1] = int(pp[1])
+       }
        return
 }
 
index 5f12f229c4c78622d078a3afc66c0b80fc53841e..f44a9e25ac97bde5923eb688f9a67a2c8d11618b 100644 (file)
@@ -57,7 +57,9 @@ func Pipe(p []int) (err error) {
        if e1 != 0 {
                err = Errno(e1)
        }
-       p[0], p[1] = int(r0), int(w0)
+       if err == nil {
+               p[0], p[1] = int(r0), int(w0)
+       }
        return
 }