Просмотр исходного кода

unix: solicit EPERM via wrong PID in creds test.

In a Linux user namespace that doesn't have UID 0 mapped, WriteMsgUnix
will return an EINVAL as the uid-valid-in-uns check comes first in the kernel.

Even if in a user and PID namespace, using the wrong PID in Ucred will
always give EPERM.

Change-Id: Ia7452bbf2911c3b9a2aa5d7df1572e8b0790ff38
GitHub-Last-Rev: 341d3f0cba0511411caffd2ac5a6c8dfb7a022ef
GitHub-Pull-Request: golang/sys#7
Reviewed-on: https://go-review.googlesource.com/103857
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Chris Koch 7 лет назад
Родитель
Сommit
f67933eaf9
1 измененных файлов с 15 добавлено и 18 удалено
  1. 15 18
      unix/creds_test.go

+ 15 - 18
unix/creds_test.go

@@ -72,27 +72,24 @@ func TestSCMCredentials(t *testing.T) {
 		defer cli.Close()
 
 		var ucred unix.Ucred
-		if os.Getuid() != 0 {
-			ucred.Pid = int32(os.Getpid())
-			ucred.Uid = 0
-			ucred.Gid = 0
-			oob := unix.UnixCredentials(&ucred)
-			_, _, err := cli.(*net.UnixConn).WriteMsgUnix(nil, oob, nil)
-			if op, ok := err.(*net.OpError); ok {
-				err = op.Err
-			}
-			if sys, ok := err.(*os.SyscallError); ok {
-				err = sys.Err
-			}
-			if err != syscall.EPERM {
-				t.Fatalf("WriteMsgUnix failed with %v, want EPERM", err)
-			}
-		}
-
-		ucred.Pid = int32(os.Getpid())
+		ucred.Pid = int32(os.Getpid() - 1)
 		ucred.Uid = uint32(os.Getuid())
 		ucred.Gid = uint32(os.Getgid())
 		oob := unix.UnixCredentials(&ucred)
+		_, _, err = cli.(*net.UnixConn).WriteMsgUnix(nil, oob, nil)
+		if op, ok := err.(*net.OpError); ok {
+			err = op.Err
+		}
+		if sys, ok := err.(*os.SyscallError); ok {
+			err = sys.Err
+		}
+		if err != syscall.EPERM {
+			t.Fatalf("WriteMsgUnix failed with %v, want EPERM", err)
+		}
+
+		// Fix the PID.
+		ucred.Pid = int32(os.Getpid())
+		oob = unix.UnixCredentials(&ucred)
 
 		// On SOCK_STREAM, this is internally going to send a dummy byte
 		n, oobn, err := cli.(*net.UnixConn).WriteMsgUnix(nil, oob, nil)