Skip to content

Commit 8261c88

Browse files
LK4D4ianlancetaylor
authored andcommitted
syscall: don't call Setgroups if Credential.Groups is empty
Setgroups with zero-length groups is no-op for changing groups and supposed to be used only for determining curent groups length. Also because we deny setgroups by default if use GidMappings we have unnecessary error from that no-op syscall. Change-Id: I8f74fbca9190a3dcbbef1d886c518e01fa05eb62 Reviewed-on: https://go-review.googlesource.com/13938 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent b55c4a0 commit 8261c88

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/syscall/exec_linux.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,12 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
191191
// User and groups
192192
if cred := sys.Credential; cred != nil {
193193
ngroups := uintptr(len(cred.Groups))
194-
var groups unsafe.Pointer
195194
if ngroups > 0 {
196-
groups = unsafe.Pointer(&cred.Groups[0])
197-
}
198-
_, _, err1 = RawSyscall(SYS_SETGROUPS, ngroups, uintptr(groups), 0)
199-
if err1 != 0 {
200-
goto childerror
195+
groups := unsafe.Pointer(&cred.Groups[0])
196+
_, _, err1 = RawSyscall(SYS_SETGROUPS, ngroups, uintptr(groups), 0)
197+
if err1 != 0 {
198+
goto childerror
199+
}
201200
}
202201
_, _, err1 = RawSyscall(SYS_SETGID, uintptr(cred.Gid), 0, 0)
203202
if err1 != 0 {

src/syscall/exec_linux_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,11 @@ func TestCloneNEWUSERAndRemapNoRootSetgroupsEnableSetgroups(t *testing.T) {
109109
t.Fatalf("Unprivileged gid_map rewriting with GidMappingsEnableSetgroups must fail")
110110
}
111111
}
112+
113+
func TestEmptyCredGroupsDisableSetgroups(t *testing.T) {
114+
cmd := whoamiCmd(t, os.Getuid(), os.Getgid(), false)
115+
cmd.SysProcAttr.Credential = &syscall.Credential{}
116+
if err := cmd.Run(); err != nil {
117+
t.Fatal(err)
118+
}
119+
}

0 commit comments

Comments
 (0)