diff options
| author | Alexander Morozov <lk4d4math@gmail.com> | 2015-06-03 10:50:39 -0700 |
|---|---|---|
| committer | Ian Lance Taylor <iant@golang.org> | 2015-06-12 23:38:59 +0000 |
| commit | f5c60ff2da4851f9056120a423ce6b48624fb97e (patch) | |
| tree | 1ad6d39cccc3faae5e9e87df8e8847efbb3f4866 /src/syscall/exec_linux.go | |
| parent | 368f0ee6c4dc60b314dffbb63f5eab0ad62185d2 (diff) | |
| download | go-f5c60ff2da4851f9056120a423ce6b48624fb97e.tar.xz | |
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 <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/syscall/exec_linux.go')
| -rw-r--r-- | src/syscall/exec_linux.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/syscall/exec_linux.go b/src/syscall/exec_linux.go index 3aa30c7364..9bac042124 100644 --- a/src/syscall/exec_linux.go +++ b/src/syscall/exec_linux.go @@ -33,6 +33,11 @@ type SysProcAttr struct { Cloneflags uintptr // Flags for clone calls (Linux only) UidMappings []SysProcIDMap // User ID mappings for user namespaces. GidMappings []SysProcIDMap // Group ID mappings for user namespaces. + // GidMappingsEnableSetgroups enabling setgroups syscall. + // If false, then setgroups syscall will be disabled for the child process. + // This parameter is no-op if GidMappings == nil. Otherwise for unprivileged + // users this should be set to false for mappings work. + GidMappingsEnableSetgroups bool } // Implemented in runtime package. @@ -366,6 +371,32 @@ func writeIDMappings(path string, idMap []SysProcIDMap) error { return nil } +// writeSetgroups writes to /proc/PID/setgroups "deny" if enable is false +// and "allow" if enable is true. +// This is needed since kernel 3.19, because you can't write gid_map without +// disabling setgroups() system call. +func writeSetgroups(pid int, enable bool) error { + sgf := "/proc/" + itoa(pid) + "/setgroups" + fd, err := Open(sgf, O_RDWR, 0) + if err != nil { + return err + } + + var data []byte + if enable { + data = []byte("allow") + } else { + data = []byte("deny") + } + + if _, err := Write(fd, data); err != nil { + Close(fd) + return err + } + + return Close(fd) +} + // writeUidGidMappings writes User ID and Group ID mappings for user namespaces // for a process and it is called from the parent process. func writeUidGidMappings(pid int, sys *SysProcAttr) error { @@ -377,6 +408,10 @@ func writeUidGidMappings(pid int, sys *SysProcAttr) error { } if sys.GidMappings != nil { + // If the kernel is too old to support /proc/PID/setgroups, writeSetGroups will return ENOENT; this is OK. + if err := writeSetgroups(pid, sys.GidMappingsEnableSetgroups); err != nil && err != ENOENT { + return err + } gidf := "/proc/" + itoa(pid) + "/gid_map" if err := writeIDMappings(gidf, sys.GidMappings); err != nil { return err |
