2012-01-09 21:37:46 +09:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
// +build linux
|
|
|
|
|
|
|
|
|
|
package syscall
|
|
|
|
|
|
|
|
|
|
import (
|
2016-03-18 18:55:26 -04:00
|
|
|
"runtime"
|
2012-01-09 21:37:46 +09:00
|
|
|
"unsafe"
|
|
|
|
|
)
|
|
|
|
|
|
2014-10-02 11:37:06 -07:00
|
|
|
// SysProcIDMap holds Container ID to Host ID mappings used for User Namespaces in Linux.
|
|
|
|
|
// See user_namespaces(7).
|
|
|
|
|
type SysProcIDMap struct {
|
|
|
|
|
ContainerID int // Container ID.
|
|
|
|
|
HostID int // Host ID.
|
|
|
|
|
Size int // Size.
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-09 21:37:46 +09:00
|
|
|
type SysProcAttr struct {
|
2016-05-31 19:44:48 -07:00
|
|
|
Chroot string // Chroot.
|
|
|
|
|
Credential *Credential // Credential.
|
|
|
|
|
Ptrace bool // Enable tracing.
|
|
|
|
|
Setsid bool // Create session.
|
|
|
|
|
Setpgid bool // Set process group ID to Pgid, or, if Pgid == 0, to new pid.
|
|
|
|
|
Setctty bool // Set controlling terminal to fd Ctty (only meaningful if Setsid is set)
|
|
|
|
|
Noctty bool // Detach fd 0 from controlling terminal
|
|
|
|
|
Ctty int // Controlling TTY fd
|
|
|
|
|
Foreground bool // Place child's process group in foreground. (Implies Setpgid. Uses Ctty as fd of controlling TTY)
|
|
|
|
|
Pgid int // Child's process group ID if Setpgid.
|
|
|
|
|
Pdeathsig Signal // Signal that the process will get when its parent dies (Linux only)
|
|
|
|
|
Cloneflags uintptr // Flags for clone calls (Linux only)
|
|
|
|
|
Unshareflags uintptr // Flags for unshare calls (Linux only)
|
|
|
|
|
UidMappings []SysProcIDMap // User ID mappings for user namespaces.
|
|
|
|
|
GidMappings []SysProcIDMap // Group ID mappings for user namespaces.
|
2015-06-03 10:50:39 -07:00
|
|
|
// 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
|
2017-05-17 02:05:32 -07:00
|
|
|
AmbientCaps []uintptr // Ambient capabilities (Linux only)
|
2012-01-09 21:37:46 +09:00
|
|
|
}
|
|
|
|
|
|
os/exec: handle Unshareflags with CLONE_NEWNS
In some newer Linux distros, systemd forces
all mount namespaces to be shared, starting
at /. This disables the CLONE_NEWNS
flag in unshare(2) and clone(2).
While this problem is most commonly seen
on systems with systemd, it can happen anywhere,
due to how Linux namespaces now work.
Hence, to create a private mount namespace,
it is not sufficient to just set
CLONE_NEWS; you have to call mount(2) to change
the behavior of namespaces, i.e.
mount("none", "/", NULL, MS_REC|MS_PRIVATE, NULL)
This is tested and working and we can now correctly
start child process with private namespaces on Linux
distros that use systemd.
The new test works correctly on Ubuntu 16.04.2 LTS.
It fails if I comment out the new Mount, and
succeeds otherwise. In each case it correctly
cleans up after itself.
Fixes #19661
Change-Id: I52240b59628e3772b529d9bbef7166606b0c157d
Reviewed-on: https://go-review.googlesource.com/38471
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-03-22 14:40:55 -07:00
|
|
|
var (
|
|
|
|
|
none = [...]byte{'n', 'o', 'n', 'e', 0}
|
|
|
|
|
slash = [...]byte{'/', 0}
|
|
|
|
|
)
|
|
|
|
|
|
2013-08-13 13:01:30 +04:00
|
|
|
// Implemented in runtime package.
|
|
|
|
|
func runtime_BeforeFork()
|
|
|
|
|
func runtime_AfterFork()
|
2017-06-12 22:36:03 -07:00
|
|
|
func runtime_AfterForkInChild()
|
2013-08-13 13:01:30 +04:00
|
|
|
|
2012-01-09 21:37:46 +09:00
|
|
|
// Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
|
|
|
|
|
// If a dup or exec fails, write the errno error to pipe.
|
|
|
|
|
// (Pipe is close-on-exec so if exec succeeds, it will be closed.)
|
|
|
|
|
// In the child, this function must not acquire any locks, because
|
2016-03-01 23:21:55 +00:00
|
|
|
// they might have been locked at the time of the fork. This means
|
2012-01-09 21:37:46 +09:00
|
|
|
// no rescheduling, no malloc calls, and no new stack segments.
|
2013-07-16 15:35:03 +04:00
|
|
|
// For the same reason compiler does not race instrument it.
|
2012-01-09 21:37:46 +09:00
|
|
|
// The calls to RawSyscall are okay because they are assembly
|
|
|
|
|
// functions that do not grow the stack.
|
2015-10-19 17:01:37 -07:00
|
|
|
//go:norace
|
2012-01-09 21:37:46 +09:00
|
|
|
func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) {
|
2017-06-20 15:12:12 -04:00
|
|
|
// Set up and fork. This returns immediately in the parent or
|
|
|
|
|
// if there's an error.
|
|
|
|
|
r1, err1, p, locked := forkAndExecInChild1(argv0, argv, envv, chroot, dir, attr, sys, pipe)
|
|
|
|
|
if locked {
|
|
|
|
|
runtime_AfterFork()
|
|
|
|
|
}
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
return 0, err1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parent; return PID
|
|
|
|
|
pid = int(r1)
|
|
|
|
|
|
|
|
|
|
if sys.UidMappings != nil || sys.GidMappings != nil {
|
|
|
|
|
Close(p[0])
|
|
|
|
|
err := writeUidGidMappings(pid, sys)
|
|
|
|
|
var err2 Errno
|
|
|
|
|
if err != nil {
|
|
|
|
|
err2 = err.(Errno)
|
|
|
|
|
}
|
|
|
|
|
RawSyscall(SYS_WRITE, uintptr(p[1]), uintptr(unsafe.Pointer(&err2)), unsafe.Sizeof(err2))
|
|
|
|
|
Close(p[1])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pid, 0
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 15:59:02 -04:00
|
|
|
// forkAndExecInChild1 implements the body of forkAndExecInChild up to
|
|
|
|
|
// the parent's post-fork path. This is a separate function so we can
|
|
|
|
|
// separate the child's and parent's stack frames if we're using
|
|
|
|
|
// vfork.
|
|
|
|
|
//
|
|
|
|
|
// This is go:noinline because the point is to keep the stack frames
|
|
|
|
|
// of this and forkAndExecInChild separate.
|
|
|
|
|
//
|
|
|
|
|
//go:noinline
|
2017-06-20 15:12:12 -04:00
|
|
|
//go:norace
|
|
|
|
|
func forkAndExecInChild1(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (r1 uintptr, err1 Errno, p [2]int, locked bool) {
|
2017-05-17 02:05:32 -07:00
|
|
|
// Defined in linux/prctl.h starting with Linux 4.3.
|
|
|
|
|
const (
|
|
|
|
|
PR_CAP_AMBIENT = 0x2f
|
|
|
|
|
PR_CAP_AMBIENT_RAISE = 0x2
|
|
|
|
|
)
|
|
|
|
|
|
2017-06-20 15:12:12 -04:00
|
|
|
// vfork requires that the child not touch any of the parent's
|
|
|
|
|
// active stack frames. Hence, the child does all post-fork
|
|
|
|
|
// processing in this stack frame and never returns, while the
|
|
|
|
|
// parent returns immediately from this frame and does all
|
|
|
|
|
// post-fork processing in the outer frame.
|
2012-01-09 21:37:46 +09:00
|
|
|
// Declare all variables at top in case any
|
|
|
|
|
// declarations require heap allocation (e.g., err1).
|
|
|
|
|
var (
|
2014-10-02 11:37:06 -07:00
|
|
|
err2 Errno
|
2012-01-09 21:37:46 +09:00
|
|
|
nextfd int
|
|
|
|
|
i int
|
|
|
|
|
)
|
|
|
|
|
|
2014-12-11 10:25:32 -05:00
|
|
|
// Record parent PID so child can test if it has died.
|
2017-12-18 11:59:26 +01:00
|
|
|
ppid, _ := rawSyscallNoError(SYS_GETPID, 0, 0, 0)
|
2014-12-11 10:25:32 -05:00
|
|
|
|
2013-04-30 11:52:23 -07:00
|
|
|
// Guard against side effects of shuffling fds below.
|
|
|
|
|
// Make sure that nextfd is beyond any currently open files so
|
|
|
|
|
// that we can't run the risk of overwriting any of them.
|
2012-02-11 08:47:19 +11:00
|
|
|
fd := make([]int, len(attr.Files))
|
2013-04-30 11:52:23 -07:00
|
|
|
nextfd = len(attr.Files)
|
2012-02-11 08:47:19 +11:00
|
|
|
for i, ufd := range attr.Files {
|
2013-04-30 11:52:23 -07:00
|
|
|
if nextfd < int(ufd) {
|
|
|
|
|
nextfd = int(ufd)
|
|
|
|
|
}
|
2012-02-11 08:47:19 +11:00
|
|
|
fd[i] = int(ufd)
|
|
|
|
|
}
|
2013-04-30 11:52:23 -07:00
|
|
|
nextfd++
|
2012-01-09 21:37:46 +09:00
|
|
|
|
2014-10-02 11:37:06 -07:00
|
|
|
// Allocate another pipe for parent to child communication for
|
|
|
|
|
// synchronizing writing of User ID/Group ID mappings.
|
|
|
|
|
if sys.UidMappings != nil || sys.GidMappings != nil {
|
|
|
|
|
if err := forkExecPipe(p[:]); err != nil {
|
2017-06-20 15:12:12 -04:00
|
|
|
err1 = err.(Errno)
|
|
|
|
|
return
|
2014-10-02 11:37:06 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-09 21:37:46 +09:00
|
|
|
// About to call fork.
|
|
|
|
|
// No more allocation or calls of non-assembly functions.
|
2013-08-13 13:01:30 +04:00
|
|
|
runtime_BeforeFork()
|
2017-06-20 15:12:12 -04:00
|
|
|
locked = true
|
2017-02-25 18:37:17 +01:00
|
|
|
switch {
|
|
|
|
|
case runtime.GOARCH == "amd64" && sys.Cloneflags&CLONE_NEWUSER == 0:
|
|
|
|
|
r1, err1 = rawVforkSyscall(SYS_CLONE, uintptr(SIGCHLD|CLONE_VFORK|CLONE_VM)|sys.Cloneflags)
|
|
|
|
|
case runtime.GOARCH == "s390x":
|
2016-03-18 18:55:26 -04:00
|
|
|
r1, _, err1 = RawSyscall6(SYS_CLONE, 0, uintptr(SIGCHLD)|sys.Cloneflags, 0, 0, 0, 0)
|
2017-02-25 18:37:17 +01:00
|
|
|
default:
|
2016-03-18 18:55:26 -04:00
|
|
|
r1, _, err1 = RawSyscall6(SYS_CLONE, uintptr(SIGCHLD)|sys.Cloneflags, 0, 0, 0, 0, 0)
|
|
|
|
|
}
|
2017-06-20 15:12:12 -04:00
|
|
|
if err1 != 0 || r1 != 0 {
|
|
|
|
|
// If we're in the parent, we must return immediately
|
|
|
|
|
// so we're not in the same stack frame as the child.
|
|
|
|
|
// This can at most use the return PC, which the child
|
|
|
|
|
// will not modify, and the results of
|
|
|
|
|
// rawVforkSyscall, which must have been written after
|
|
|
|
|
// the child was replaced.
|
|
|
|
|
return
|
2012-01-09 21:37:46 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fork succeeded, now in child.
|
|
|
|
|
|
2017-06-12 22:36:03 -07:00
|
|
|
runtime_AfterForkInChild()
|
|
|
|
|
|
2017-05-17 02:05:32 -07:00
|
|
|
// Enable the "keep capabilities" flag to set ambient capabilities later.
|
|
|
|
|
if len(sys.AmbientCaps) > 0 {
|
|
|
|
|
_, _, err1 = RawSyscall6(SYS_PRCTL, PR_SET_KEEPCAPS, 1, 0, 0, 0, 0)
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-02 11:37:06 -07:00
|
|
|
// Wait for User ID/Group ID mappings to be written.
|
|
|
|
|
if sys.UidMappings != nil || sys.GidMappings != nil {
|
|
|
|
|
if _, _, err1 = RawSyscall(SYS_CLOSE, uintptr(p[1]), 0, 0); err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
r1, _, err1 = RawSyscall(SYS_READ, uintptr(p[0]), uintptr(unsafe.Pointer(&err2)), unsafe.Sizeof(err2))
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
if r1 != unsafe.Sizeof(err2) {
|
|
|
|
|
err1 = EINVAL
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
if err2 != 0 {
|
|
|
|
|
err1 = err2
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-09 21:37:46 +09:00
|
|
|
// Session ID
|
|
|
|
|
if sys.Setsid {
|
|
|
|
|
_, _, err1 = RawSyscall(SYS_SETSID, 0, 0, 0)
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set process group
|
2015-02-17 22:23:16 -05:00
|
|
|
if sys.Setpgid || sys.Foreground {
|
|
|
|
|
// Place child in process group.
|
|
|
|
|
_, _, err1 = RawSyscall(SYS_SETPGID, 0, uintptr(sys.Pgid), 0)
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if sys.Foreground {
|
2015-05-02 22:44:49 -04:00
|
|
|
pgrp := int32(sys.Pgid)
|
2015-02-17 22:23:16 -05:00
|
|
|
if pgrp == 0 {
|
2017-12-18 11:59:26 +01:00
|
|
|
r1, _ = rawSyscallNoError(SYS_GETPID, 0, 0, 0)
|
2015-02-17 22:23:16 -05:00
|
|
|
|
2015-05-02 22:44:49 -04:00
|
|
|
pgrp = int32(r1)
|
2015-02-17 22:23:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Place process group in foreground.
|
|
|
|
|
_, _, err1 = RawSyscall(SYS_IOCTL, uintptr(sys.Ctty), uintptr(TIOCSPGRP), uintptr(unsafe.Pointer(&pgrp)))
|
2012-01-09 21:37:46 +09:00
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 18:47:24 -07:00
|
|
|
// Unshare
|
2016-05-31 19:44:48 -07:00
|
|
|
if sys.Unshareflags != 0 {
|
|
|
|
|
_, _, err1 = RawSyscall(SYS_UNSHARE, sys.Unshareflags, 0, 0)
|
2016-05-18 18:47:24 -07:00
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
os/exec: handle Unshareflags with CLONE_NEWNS
In some newer Linux distros, systemd forces
all mount namespaces to be shared, starting
at /. This disables the CLONE_NEWNS
flag in unshare(2) and clone(2).
While this problem is most commonly seen
on systems with systemd, it can happen anywhere,
due to how Linux namespaces now work.
Hence, to create a private mount namespace,
it is not sufficient to just set
CLONE_NEWS; you have to call mount(2) to change
the behavior of namespaces, i.e.
mount("none", "/", NULL, MS_REC|MS_PRIVATE, NULL)
This is tested and working and we can now correctly
start child process with private namespaces on Linux
distros that use systemd.
The new test works correctly on Ubuntu 16.04.2 LTS.
It fails if I comment out the new Mount, and
succeeds otherwise. In each case it correctly
cleans up after itself.
Fixes #19661
Change-Id: I52240b59628e3772b529d9bbef7166606b0c157d
Reviewed-on: https://go-review.googlesource.com/38471
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-03-22 14:40:55 -07:00
|
|
|
// The unshare system call in Linux doesn't unshare mount points
|
|
|
|
|
// mounted with --shared. Systemd mounts / with --shared. For a
|
|
|
|
|
// long discussion of the pros and cons of this see debian bug 739593.
|
|
|
|
|
// The Go model of unsharing is more like Plan 9, where you ask
|
|
|
|
|
// to unshare and the namespaces are unconditionally unshared.
|
|
|
|
|
// To make this model work we must further mark / as MS_PRIVATE.
|
|
|
|
|
// This is what the standard unshare command does.
|
|
|
|
|
if sys.Unshareflags&CLONE_NEWNS == CLONE_NEWNS {
|
|
|
|
|
_, _, err1 = RawSyscall6(SYS_MOUNT, uintptr(unsafe.Pointer(&none[0])), uintptr(unsafe.Pointer(&slash[0])), 0, MS_REC|MS_PRIVATE, 0, 0)
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-18 18:47:24 -07:00
|
|
|
}
|
|
|
|
|
|
2017-04-24 16:09:24 -07:00
|
|
|
// Chroot
|
|
|
|
|
if chroot != nil {
|
|
|
|
|
_, _, err1 = RawSyscall(SYS_CHROOT, uintptr(unsafe.Pointer(chroot)), 0, 0)
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-09 21:37:46 +09:00
|
|
|
// User and groups
|
|
|
|
|
if cred := sys.Credential; cred != nil {
|
|
|
|
|
ngroups := uintptr(len(cred.Groups))
|
2016-05-27 15:02:31 -07:00
|
|
|
groups := uintptr(0)
|
2012-01-09 21:37:46 +09:00
|
|
|
if ngroups > 0 {
|
2016-05-27 15:02:31 -07:00
|
|
|
groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
|
|
|
|
|
}
|
2017-02-10 04:10:48 -02:00
|
|
|
if !(sys.GidMappings != nil && !sys.GidMappingsEnableSetgroups && ngroups == 0) && !cred.NoSetGroups {
|
2016-10-18 22:56:14 -04:00
|
|
|
_, _, err1 = RawSyscall(_SYS_setgroups, ngroups, groups, 0)
|
2015-08-26 20:45:28 -07:00
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
2012-01-09 21:37:46 +09:00
|
|
|
}
|
2016-11-09 20:35:46 +00:00
|
|
|
_, _, err1 = RawSyscall(sys_SETGID, uintptr(cred.Gid), 0, 0)
|
2012-01-09 21:37:46 +09:00
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
2016-11-09 20:35:46 +00:00
|
|
|
_, _, err1 = RawSyscall(sys_SETUID, uintptr(cred.Uid), 0, 0)
|
2012-01-09 21:37:46 +09:00
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-17 02:05:32 -07:00
|
|
|
for _, c := range sys.AmbientCaps {
|
|
|
|
|
_, _, err1 = RawSyscall6(SYS_PRCTL, PR_CAP_AMBIENT, uintptr(PR_CAP_AMBIENT_RAISE), c, 0, 0, 0)
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-09 21:37:46 +09:00
|
|
|
// Chdir
|
|
|
|
|
if dir != nil {
|
|
|
|
|
_, _, err1 = RawSyscall(SYS_CHDIR, uintptr(unsafe.Pointer(dir)), 0, 0)
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-25 12:53:34 -06:00
|
|
|
// Parent death signal
|
|
|
|
|
if sys.Pdeathsig != 0 {
|
|
|
|
|
_, _, err1 = RawSyscall6(SYS_PRCTL, PR_SET_PDEATHSIG, uintptr(sys.Pdeathsig), 0, 0, 0, 0)
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Signal self if parent is already dead. This might cause a
|
|
|
|
|
// duplicate signal in rare cases, but it won't matter when
|
|
|
|
|
// using SIGKILL.
|
2017-12-18 11:59:26 +01:00
|
|
|
r1, _ = rawSyscallNoError(SYS_GETPPID, 0, 0, 0)
|
2015-01-25 12:53:34 -06:00
|
|
|
if r1 != ppid {
|
2017-12-18 11:59:26 +01:00
|
|
|
pid, _ := rawSyscallNoError(SYS_GETPID, 0, 0, 0)
|
2015-01-25 12:53:34 -06:00
|
|
|
_, _, err1 := RawSyscall(SYS_KILL, pid, uintptr(sys.Pdeathsig), 0)
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-09 21:37:46 +09:00
|
|
|
// Pass 1: look for fd[i] < i and move those up above len(fd)
|
|
|
|
|
// so that pass 2 won't stomp on an fd it needs later.
|
|
|
|
|
if pipe < nextfd {
|
2015-03-09 07:03:00 +11:00
|
|
|
_, _, err1 = RawSyscall(_SYS_dup, uintptr(pipe), uintptr(nextfd), 0)
|
2012-01-09 21:37:46 +09:00
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
|
|
|
|
|
pipe = nextfd
|
|
|
|
|
nextfd++
|
|
|
|
|
}
|
|
|
|
|
for i = 0; i < len(fd); i++ {
|
|
|
|
|
if fd[i] >= 0 && fd[i] < int(i) {
|
2016-03-26 19:35:21 +00:00
|
|
|
if nextfd == pipe { // don't stomp on pipe
|
|
|
|
|
nextfd++
|
|
|
|
|
}
|
2015-03-09 07:03:00 +11:00
|
|
|
_, _, err1 = RawSyscall(_SYS_dup, uintptr(fd[i]), uintptr(nextfd), 0)
|
2012-01-09 21:37:46 +09:00
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
|
|
|
|
|
fd[i] = nextfd
|
|
|
|
|
nextfd++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pass 2: dup fd[i] down onto i.
|
|
|
|
|
for i = 0; i < len(fd); i++ {
|
|
|
|
|
if fd[i] == -1 {
|
|
|
|
|
RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if fd[i] == int(i) {
|
|
|
|
|
// dup2(i, i) won't clear close-on-exec flag on Linux,
|
|
|
|
|
// probably not elsewhere either.
|
|
|
|
|
_, _, err1 = RawSyscall(SYS_FCNTL, uintptr(fd[i]), F_SETFD, 0)
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// The new fd is created NOT close-on-exec,
|
|
|
|
|
// which is exactly what we want.
|
2015-03-09 07:03:00 +11:00
|
|
|
_, _, err1 = RawSyscall(_SYS_dup, uintptr(fd[i]), uintptr(i), 0)
|
2012-01-09 21:37:46 +09:00
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// By convention, we don't close-on-exec the fds we are
|
|
|
|
|
// started with, so if len(fd) < 3, close 0, 1, 2 as needed.
|
|
|
|
|
// Programs that know they inherit fds >= 3 will need
|
|
|
|
|
// to set them close-on-exec.
|
|
|
|
|
for i = len(fd); i < 3; i++ {
|
|
|
|
|
RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Detach fd 0 from tty
|
|
|
|
|
if sys.Noctty {
|
|
|
|
|
_, _, err1 = RawSyscall(SYS_IOCTL, 0, uintptr(TIOCNOTTY), 0)
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-30 17:36:18 -07:00
|
|
|
// Set the controlling TTY to Ctty
|
2015-02-17 22:23:16 -05:00
|
|
|
if sys.Setctty {
|
2017-05-22 10:33:31 -05:00
|
|
|
_, _, err1 = RawSyscall(SYS_IOCTL, uintptr(sys.Ctty), uintptr(TIOCSCTTY), 1)
|
2012-01-09 21:37:46 +09:00
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 12:13:23 -04:00
|
|
|
// Enable tracing if requested.
|
|
|
|
|
// Do this right before exec so that we don't unnecessarily trace the runtime
|
|
|
|
|
// setting up after the fork. See issue #21428.
|
|
|
|
|
if sys.Ptrace {
|
|
|
|
|
_, _, err1 = RawSyscall(SYS_PTRACE, uintptr(PTRACE_TRACEME), 0, 0)
|
|
|
|
|
if err1 != 0 {
|
|
|
|
|
goto childerror
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-09 21:37:46 +09:00
|
|
|
// Time to exec.
|
|
|
|
|
_, _, err1 = RawSyscall(SYS_EXECVE,
|
|
|
|
|
uintptr(unsafe.Pointer(argv0)),
|
|
|
|
|
uintptr(unsafe.Pointer(&argv[0])),
|
|
|
|
|
uintptr(unsafe.Pointer(&envv[0])))
|
|
|
|
|
|
|
|
|
|
childerror:
|
|
|
|
|
// send error code on pipe
|
|
|
|
|
RawSyscall(SYS_WRITE, uintptr(pipe), uintptr(unsafe.Pointer(&err1)), unsafe.Sizeof(err1))
|
|
|
|
|
for {
|
|
|
|
|
RawSyscall(SYS_EXIT, 253, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-01-10 17:04:55 -08:00
|
|
|
|
|
|
|
|
// Try to open a pipe with O_CLOEXEC set on both file descriptors.
|
|
|
|
|
func forkExecPipe(p []int) (err error) {
|
|
|
|
|
err = Pipe2(p, O_CLOEXEC)
|
|
|
|
|
// pipe2 was added in 2.6.27 and our minimum requirement is 2.6.23, so it
|
|
|
|
|
// might not be implemented.
|
|
|
|
|
if err == ENOSYS {
|
|
|
|
|
if err = Pipe(p); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if _, err = fcntl(p[0], F_SETFD, FD_CLOEXEC); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
_, err = fcntl(p[1], F_SETFD, FD_CLOEXEC)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2014-10-02 11:37:06 -07:00
|
|
|
|
|
|
|
|
// writeIDMappings writes the user namespace User ID or Group ID mappings to the specified path.
|
|
|
|
|
func writeIDMappings(path string, idMap []SysProcIDMap) error {
|
|
|
|
|
fd, err := Open(path, O_RDWR, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data := ""
|
|
|
|
|
for _, im := range idMap {
|
|
|
|
|
data = data + itoa(im.ContainerID) + " " + itoa(im.HostID) + " " + itoa(im.Size) + "\n"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bytes, err := ByteSliceFromString(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
Close(fd)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := Write(fd, bytes); err != nil {
|
|
|
|
|
Close(fd)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := Close(fd); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-03 10:50:39 -07:00
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-02 11:37:06 -07:00
|
|
|
// 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 {
|
|
|
|
|
if sys.UidMappings != nil {
|
|
|
|
|
uidf := "/proc/" + itoa(pid) + "/uid_map"
|
|
|
|
|
if err := writeIDMappings(uidf, sys.UidMappings); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if sys.GidMappings != nil {
|
2015-06-03 10:50:39 -07:00
|
|
|
// 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
|
|
|
|
|
}
|
2014-10-02 11:37:06 -07:00
|
|
|
gidf := "/proc/" + itoa(pid) + "/gid_map"
|
|
|
|
|
if err := writeIDMappings(gidf, sys.GidMappings); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|