Skip to content

Commit f5c60ff

Browse files
LK4D4ianlancetaylor
authored andcommitted
syscall: add GidMappingsEnableSetgroups to Linux SysProcAttr
Linux 3.19 made a change in the handling of setgroups and the 'gid_map' file to address a security issue. The upshot of the 3.19 changes is that in order to update the 'gid_maps' file, use of the setgroups() system call in this user namespace must first be disabled by writing "deny" to one of the /proc/PID/setgroups files for this namespace. Also added tests for remapping uid_map and gid_map inside new user namespace. Fixes #10626 Change-Id: I4d2539acbab741a37092d277e10f31fc39a8feb7 Reviewed-on: https://go-review.googlesource.com/10670 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 368f0ee commit f5c60ff

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

src/syscall/exec_linux.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ type SysProcAttr struct {
3333
Cloneflags uintptr // Flags for clone calls (Linux only)
3434
UidMappings []SysProcIDMap // User ID mappings for user namespaces.
3535
GidMappings []SysProcIDMap // Group ID mappings for user namespaces.
36+
// GidMappingsEnableSetgroups enabling setgroups syscall.
37+
// If false, then setgroups syscall will be disabled for the child process.
38+
// This parameter is no-op if GidMappings == nil. Otherwise for unprivileged
39+
// users this should be set to false for mappings work.
40+
GidMappingsEnableSetgroups bool
3641
}
3742

3843
// Implemented in runtime package.
@@ -366,6 +371,32 @@ func writeIDMappings(path string, idMap []SysProcIDMap) error {
366371
return nil
367372
}
368373

374+
// writeSetgroups writes to /proc/PID/setgroups "deny" if enable is false
375+
// and "allow" if enable is true.
376+
// This is needed since kernel 3.19, because you can't write gid_map without
377+
// disabling setgroups() system call.
378+
func writeSetgroups(pid int, enable bool) error {
379+
sgf := "/proc/" + itoa(pid) + "/setgroups"
380+
fd, err := Open(sgf, O_RDWR, 0)
381+
if err != nil {
382+
return err
383+
}
384+
385+
var data []byte
386+
if enable {
387+
data = []byte("allow")
388+
} else {
389+
data = []byte("deny")
390+
}
391+
392+
if _, err := Write(fd, data); err != nil {
393+
Close(fd)
394+
return err
395+
}
396+
397+
return Close(fd)
398+
}
399+
369400
// writeUidGidMappings writes User ID and Group ID mappings for user namespaces
370401
// for a process and it is called from the parent process.
371402
func writeUidGidMappings(pid int, sys *SysProcAttr) error {
@@ -377,6 +408,10 @@ func writeUidGidMappings(pid int, sys *SysProcAttr) error {
377408
}
378409

379410
if sys.GidMappings != nil {
411+
// If the kernel is too old to support /proc/PID/setgroups, writeSetGroups will return ENOENT; this is OK.
412+
if err := writeSetgroups(pid, sys.GidMappingsEnableSetgroups); err != nil && err != ENOENT {
413+
return err
414+
}
380415
gidf := "/proc/" + itoa(pid) + "/gid_map"
381416
if err := writeIDMappings(gidf, sys.GidMappings); err != nil {
382417
return err

src/syscall/exec_linux_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2015 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build linux
6+
7+
package syscall_test
8+
9+
import (
10+
"os"
11+
"os/exec"
12+
"strings"
13+
"syscall"
14+
"testing"
15+
)
16+
17+
func whoamiCmd(t *testing.T, uid int, setgroups bool) *exec.Cmd {
18+
if _, err := os.Stat("/proc/self/ns/user"); err != nil {
19+
if os.IsNotExist(err) {
20+
t.Skip("kernel doesn't support user namespaces")
21+
}
22+
t.Fatalf("Failed to stat /proc/self/ns/user: %v", err)
23+
}
24+
cmd := exec.Command("whoami")
25+
cmd.SysProcAttr = &syscall.SysProcAttr{
26+
Cloneflags: syscall.CLONE_NEWUSER,
27+
UidMappings: []syscall.SysProcIDMap{
28+
{ContainerID: 0, HostID: uid, Size: 1},
29+
},
30+
GidMappings: []syscall.SysProcIDMap{
31+
{ContainerID: 0, HostID: uid, Size: 1},
32+
},
33+
GidMappingsEnableSetgroups: setgroups,
34+
}
35+
return cmd
36+
}
37+
38+
func testNEWUSERRemap(t *testing.T, uid int, setgroups bool) {
39+
cmd := whoamiCmd(t, uid, setgroups)
40+
out, err := cmd.CombinedOutput()
41+
if err != nil {
42+
t.Fatalf("Cmd failed with err %v, output: %s", err, out)
43+
}
44+
sout := strings.TrimSpace(string(out))
45+
want := "root"
46+
if sout != want {
47+
t.Fatalf("whoami = %q; want %q", out, want)
48+
}
49+
}
50+
51+
func TestCloneNEWUSERAndRemapRootDisableSetgroups(t *testing.T) {
52+
if os.Getuid() != 0 {
53+
t.Skip("skipping root only test")
54+
}
55+
testNEWUSERRemap(t, 0, false)
56+
}
57+
58+
func TestCloneNEWUSERAndRemapRootEnableSetgroups(t *testing.T) {
59+
if os.Getuid() != 0 {
60+
t.Skip("skipping root only test")
61+
}
62+
testNEWUSERRemap(t, 0, false)
63+
}
64+
65+
func TestCloneNEWUSERAndRemapNoRootDisableSetgroups(t *testing.T) {
66+
if os.Getuid() == 0 {
67+
t.Skip("skipping unprivileged user only test")
68+
}
69+
testNEWUSERRemap(t, os.Getuid(), false)
70+
}
71+
72+
func TestCloneNEWUSERAndRemapNoRootSetgroupsEnableSetgroups(t *testing.T) {
73+
if os.Getuid() == 0 {
74+
t.Skip("skipping unprivileged user only test")
75+
}
76+
cmd := whoamiCmd(t, os.Getuid(), true)
77+
err := cmd.Run()
78+
if err == nil {
79+
t.Skip("probably old kernel without security fix")
80+
}
81+
if !strings.Contains(err.Error(), "operation not permitted") {
82+
t.Fatalf("Unprivileged gid_map rewriting with GidMappingsEnableSetgroups must fail")
83+
}
84+
}

0 commit comments

Comments
 (0)