Black Lives Matter. Support the Equal Justice Initiative.

Source file src/syscall/exec_linux.go

Documentation: syscall

     1  // Copyright 2011 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
     8  
     9  import (
    10  	"runtime"
    11  	"unsafe"
    12  )
    13  
    14  // SysProcIDMap holds Container ID to Host ID mappings used for User Namespaces in Linux.
    15  // See user_namespaces(7).
    16  type SysProcIDMap struct {
    17  	ContainerID int // Container ID.
    18  	HostID      int // Host ID.
    19  	Size        int // Size.
    20  }
    21  
    22  type SysProcAttr struct {
    23  	Chroot     string      // Chroot.
    24  	Credential *Credential // Credential.
    25  	// Ptrace tells the child to call ptrace(PTRACE_TRACEME).
    26  	// Call runtime.LockOSThread before starting a process with this set,
    27  	// and don't call UnlockOSThread until done with PtraceSyscall calls.
    28  	Ptrace bool
    29  	Setsid bool // Create session.
    30  	// Setpgid sets the process group ID of the child to Pgid,
    31  	// or, if Pgid == 0, to the new child's process ID.
    32  	Setpgid bool
    33  	// Setctty sets the controlling terminal of the child to
    34  	// file descriptor Ctty. Ctty must be a descriptor number
    35  	// in the child process: an index into ProcAttr.Files.
    36  	// This is only meaningful if Setsid is true.
    37  	Setctty bool
    38  	Noctty  bool // Detach fd 0 from controlling terminal
    39  	Ctty    int  // Controlling TTY fd
    40  	// Foreground places the child process group in the foreground.
    41  	// This implies Setpgid. The Ctty field must be set to
    42  	// the descriptor of the controlling TTY.
    43  	// Unlike Setctty, in this case Ctty must be a descriptor
    44  	// number in the parent process.
    45  	Foreground   bool
    46  	Pgid         int            // Child's process group ID if Setpgid.
    47  	Pdeathsig    Signal         // Signal that the process will get when its parent dies (Linux only)
    48  	Cloneflags   uintptr        // Flags for clone calls (Linux only)
    49  	Unshareflags uintptr        // Flags for unshare calls (Linux only)
    50  	UidMappings  []SysProcIDMap // User ID mappings for user namespaces.
    51  	GidMappings  []SysProcIDMap // Group ID mappings for user namespaces.
    52  	// GidMappingsEnableSetgroups enabling setgroups syscall.
    53  	// If false, then setgroups syscall will be disabled for the child process.
    54  	// This parameter is no-op if GidMappings == nil. Otherwise for unprivileged
    55  	// users this should be set to false for mappings work.
    56  	GidMappingsEnableSetgroups bool
    57  	AmbientCaps                []uintptr // Ambient capabilities (Linux only)
    58  }
    59  
    60  var (
    61  	none  = [...]byte{'n', 'o', 'n', 'e', 0}
    62  	slash = [...]byte{'/', 0}
    63  )
    64  
    65  // Implemented in runtime package.
    66  func runtime_BeforeFork()
    67  func runtime_AfterFork()
    68  func runtime_AfterForkInChild()
    69  
    70  // Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
    71  // If a dup or exec fails, write the errno error to pipe.
    72  // (Pipe is close-on-exec so if exec succeeds, it will be closed.)
    73  // In the child, this function must not acquire any locks, because
    74  // they might have been locked at the time of the fork. This means
    75  // no rescheduling, no malloc calls, and no new stack segments.
    76  // For the same reason compiler does not race instrument it.
    77  // The calls to RawSyscall are okay because they are assembly
    78  // functions that do not grow the stack.
    79  //go:norace
    80  func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) {
    81  	// Set up and fork. This returns immediately in the parent or
    82  	// if there's an error.
    83  	r1, err1, p, locked := forkAndExecInChild1(argv0, argv, envv, chroot, dir, attr, sys, pipe)
    84  	if locked {
    85  		runtime_AfterFork()
    86  	}
    87  	if err1 != 0 {
    88  		return 0, err1
    89  	}
    90  
    91  	// parent; return PID
    92  	pid = int(r1)
    93  
    94  	if sys.UidMappings != nil || sys.GidMappings != nil {
    95  		Close(p[0])
    96  		var err2 Errno
    97  		// uid/gid mappings will be written after fork and unshare(2) for user
    98  		// namespaces.
    99  		if sys.Unshareflags&CLONE_NEWUSER == 0 {
   100  			if err := writeUidGidMappings(pid, sys); err != nil {
   101  				err2 = err.(Errno)
   102  			}
   103  		}
   104  		RawSyscall(SYS_WRITE, uintptr(p[1]), uintptr(unsafe.Pointer(&err2)), unsafe.Sizeof(err2))
   105  		Close(p[1])
   106  	}
   107  
   108  	return pid, 0
   109  }
   110  
   111  const _LINUX_CAPABILITY_VERSION_3 = 0x20080522
   112  
   113  type capHeader struct {
   114  	version uint32
   115  	pid     int32
   116  }
   117  
   118  type capData struct {
   119  	effective   uint32
   120  	permitted   uint32
   121  	inheritable uint32
   122  }
   123  type caps struct {
   124  	hdr  capHeader
   125  	data [2]capData
   126  }
   127  
   128  // See CAP_TO_INDEX in linux/capability.h:
   129  func capToIndex(cap uintptr) uintptr { return cap >> 5 }
   130  
   131  // See CAP_TO_MASK in linux/capability.h:
   132  func capToMask(cap uintptr) uint32 { return 1 << uint(cap&31) }
   133  
   134  // forkAndExecInChild1 implements the body of forkAndExecInChild up to
   135  // the parent's post-fork path. This is a separate function so we can
   136  // separate the child's and parent's stack frames if we're using
   137  // vfork.
   138  //
   139  // This is go:noinline because the point is to keep the stack frames
   140  // of this and forkAndExecInChild separate.
   141  //
   142  //go:noinline
   143  //go:norace
   144  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) {
   145  	// Defined in linux/prctl.h starting with Linux 4.3.
   146  	const (
   147  		PR_CAP_AMBIENT       = 0x2f
   148  		PR_CAP_AMBIENT_RAISE = 0x2
   149  	)
   150  
   151  	// vfork requires that the child not touch any of the parent's
   152  	// active stack frames. Hence, the child does all post-fork
   153  	// processing in this stack frame and never returns, while the
   154  	// parent returns immediately from this frame and does all
   155  	// post-fork processing in the outer frame.
   156  	// Declare all variables at top in case any
   157  	// declarations require heap allocation (e.g., err1).
   158  	var (
   159  		err2                      Errno
   160  		nextfd                    int
   161  		i                         int
   162  		caps                      caps
   163  		fd1                       uintptr
   164  		puid, psetgroups, pgid    []byte
   165  		uidmap, setgroups, gidmap []byte
   166  	)
   167  
   168  	if sys.UidMappings != nil {
   169  		puid = []byte("/proc/self/uid_map\000")
   170  		uidmap = formatIDMappings(sys.UidMappings)
   171  	}
   172  
   173  	if sys.GidMappings != nil {
   174  		psetgroups = []byte("/proc/self/setgroups\000")
   175  		pgid = []byte("/proc/self/gid_map\000")
   176  
   177  		if sys.GidMappingsEnableSetgroups {
   178  			setgroups = []byte("allow\000")
   179  		} else {
   180  			setgroups = []byte("deny\000")
   181  		}
   182  		gidmap = formatIDMappings(sys.GidMappings)
   183  	}
   184  
   185  	// Record parent PID so child can test if it has died.
   186  	ppid, _ := rawSyscallNoError(SYS_GETPID, 0, 0, 0)
   187  
   188  	// Guard against side effects of shuffling fds below.
   189  	// Make sure that nextfd is beyond any currently open files so
   190  	// that we can't run the risk of overwriting any of them.
   191  	fd := make([]int, len(attr.Files))
   192  	nextfd = len(attr.Files)
   193  	for i, ufd := range attr.Files {
   194  		if nextfd < int(ufd) {
   195  			nextfd = int(ufd)
   196  		}
   197  		fd[i] = int(ufd)
   198  	}
   199  	nextfd++
   200  
   201  	// Allocate another pipe for parent to child communication for
   202  	// synchronizing writing of User ID/Group ID mappings.
   203  	if sys.UidMappings != nil || sys.GidMappings != nil {
   204  		if err := forkExecPipe(p[:]); err != nil {
   205  			err1 = err.(Errno)
   206  			return
   207  		}
   208  	}
   209  
   210  	hasRawVforkSyscall := runtime.GOARCH == "amd64" || runtime.GOARCH == "ppc64" || runtime.GOARCH == "s390x" || runtime.GOARCH == "arm64"
   211  
   212  	// About to call fork.
   213  	// No more allocation or calls of non-assembly functions.
   214  	runtime_BeforeFork()
   215  	locked = true
   216  	switch {
   217  	case hasRawVforkSyscall && (sys.Cloneflags&CLONE_NEWUSER == 0 && sys.Unshareflags&CLONE_NEWUSER == 0):
   218  		r1, err1 = rawVforkSyscall(SYS_CLONE, uintptr(SIGCHLD|CLONE_VFORK|CLONE_VM)|sys.Cloneflags)
   219  	case runtime.GOARCH == "s390x":
   220  		r1, _, err1 = RawSyscall6(SYS_CLONE, 0, uintptr(SIGCHLD)|sys.Cloneflags, 0, 0, 0, 0)
   221  	default:
   222  		r1, _, err1 = RawSyscall6(SYS_CLONE, uintptr(SIGCHLD)|sys.Cloneflags, 0, 0, 0, 0, 0)
   223  	}
   224  	if err1 != 0 || r1 != 0 {
   225  		// If we're in the parent, we must return immediately
   226  		// so we're not in the same stack frame as the child.
   227  		// This can at most use the return PC, which the child
   228  		// will not modify, and the results of
   229  		// rawVforkSyscall, which must have been written after
   230  		// the child was replaced.
   231  		return
   232  	}
   233  
   234  	// Fork succeeded, now in child.
   235  
   236  	runtime_AfterForkInChild()
   237  
   238  	// Enable the "keep capabilities" flag to set ambient capabilities later.
   239  	if len(sys.AmbientCaps) > 0 {
   240  		_, _, err1 = RawSyscall6(SYS_PRCTL, PR_SET_KEEPCAPS, 1, 0, 0, 0, 0)
   241  		if err1 != 0 {
   242  			goto childerror
   243  		}
   244  	}
   245  
   246  	// Wait for User ID/Group ID mappings to be written.
   247  	if sys.UidMappings != nil || sys.GidMappings != nil {
   248  		if _, _, err1 = RawSyscall(SYS_CLOSE, uintptr(p[1]), 0, 0); err1 != 0 {
   249  			goto childerror
   250  		}
   251  		r1, _, err1 = RawSyscall(SYS_READ, uintptr(p[0]), uintptr(unsafe.Pointer(&err2)), unsafe.Sizeof(err2))
   252  		if err1 != 0 {
   253  			goto childerror
   254  		}
   255  		if r1 != unsafe.Sizeof(err2) {
   256  			err1 = EINVAL
   257  			goto childerror
   258  		}
   259  		if err2 != 0 {
   260  			err1 = err2
   261  			goto childerror
   262  		}
   263  	}
   264  
   265  	// Session ID
   266  	if sys.Setsid {
   267  		_, _, err1 = RawSyscall(SYS_SETSID, 0, 0, 0)
   268  		if err1 != 0 {
   269  			goto childerror
   270  		}
   271  	}
   272  
   273  	// Set process group
   274  	if sys.Setpgid || sys.Foreground {
   275  		// Place child in process group.
   276  		_, _, err1 = RawSyscall(SYS_SETPGID, 0, uintptr(sys.Pgid), 0)
   277  		if err1 != 0 {
   278  			goto childerror
   279  		}
   280  	}
   281  
   282  	if sys.Foreground {
   283  		pgrp := int32(sys.Pgid)
   284  		if pgrp == 0 {
   285  			r1, _ = rawSyscallNoError(SYS_GETPID, 0, 0, 0)
   286  
   287  			pgrp = int32(r1)
   288  		}
   289  
   290  		// Place process group in foreground.
   291  		_, _, err1 = RawSyscall(SYS_IOCTL, uintptr(sys.Ctty), uintptr(TIOCSPGRP), uintptr(unsafe.Pointer(&pgrp)))
   292  		if err1 != 0 {
   293  			goto childerror
   294  		}
   295  	}
   296  
   297  	// Unshare
   298  	if sys.Unshareflags != 0 {
   299  		_, _, err1 = RawSyscall(SYS_UNSHARE, sys.Unshareflags, 0, 0)
   300  		if err1 != 0 {
   301  			goto childerror
   302  		}
   303  
   304  		if sys.Unshareflags&CLONE_NEWUSER != 0 && sys.GidMappings != nil {
   305  			dirfd := int(_AT_FDCWD)
   306  			if fd1, _, err1 = RawSyscall6(SYS_OPENAT, uintptr(dirfd), uintptr(unsafe.Pointer(&psetgroups[0])), uintptr(O_WRONLY), 0, 0, 0); err1 != 0 {
   307  				goto childerror
   308  			}
   309  			r1, _, err1 = RawSyscall(SYS_WRITE, uintptr(fd1), uintptr(unsafe.Pointer(&setgroups[0])), uintptr(len(setgroups)))
   310  			if err1 != 0 {
   311  				goto childerror
   312  			}
   313  			if _, _, err1 = RawSyscall(SYS_CLOSE, uintptr(fd1), 0, 0); err1 != 0 {
   314  				goto childerror
   315  			}
   316  
   317  			if fd1, _, err1 = RawSyscall6(SYS_OPENAT, uintptr(dirfd), uintptr(unsafe.Pointer(&pgid[0])), uintptr(O_WRONLY), 0, 0, 0); err1 != 0 {
   318  				goto childerror
   319  			}
   320  			r1, _, err1 = RawSyscall(SYS_WRITE, uintptr(fd1), uintptr(unsafe.Pointer(&gidmap[0])), uintptr(len(gidmap)))
   321  			if err1 != 0 {
   322  				goto childerror
   323  			}
   324  			if _, _, err1 = RawSyscall(SYS_CLOSE, uintptr(fd1), 0, 0); err1 != 0 {
   325  				goto childerror
   326  			}
   327  		}
   328  
   329  		if sys.Unshareflags&CLONE_NEWUSER != 0 && sys.UidMappings != nil {
   330  			dirfd := int(_AT_FDCWD)
   331  			if fd1, _, err1 = RawSyscall6(SYS_OPENAT, uintptr(dirfd), uintptr(unsafe.Pointer(&puid[0])), uintptr(O_WRONLY), 0, 0, 0); err1 != 0 {
   332  				goto childerror
   333  			}
   334  			r1, _, err1 = RawSyscall(SYS_WRITE, uintptr(fd1), uintptr(unsafe.Pointer(&uidmap[0])), uintptr(len(uidmap)))
   335  			if err1 != 0 {
   336  				goto childerror
   337  			}
   338  			if _, _, err1 = RawSyscall(SYS_CLOSE, uintptr(fd1), 0, 0); err1 != 0 {
   339  				goto childerror
   340  			}
   341  		}
   342  
   343  		// The unshare system call in Linux doesn't unshare mount points
   344  		// mounted with --shared. Systemd mounts / with --shared. For a
   345  		// long discussion of the pros and cons of this see debian bug 739593.
   346  		// The Go model of unsharing is more like Plan 9, where you ask
   347  		// to unshare and the namespaces are unconditionally unshared.
   348  		// To make this model work we must further mark / as MS_PRIVATE.
   349  		// This is what the standard unshare command does.
   350  		if sys.Unshareflags&CLONE_NEWNS == CLONE_NEWNS {
   351  			_, _, err1 = RawSyscall6(SYS_MOUNT, uintptr(unsafe.Pointer(&none[0])), uintptr(unsafe.Pointer(&slash[0])), 0, MS_REC|MS_PRIVATE, 0, 0)
   352  			if err1 != 0 {
   353  				goto childerror
   354  			}
   355  		}
   356  	}
   357  
   358  	// Chroot
   359  	if chroot != nil {
   360  		_, _, err1 = RawSyscall(SYS_CHROOT, uintptr(unsafe.Pointer(chroot)), 0, 0)
   361  		if err1 != 0 {
   362  			goto childerror
   363  		}
   364  	}
   365  
   366  	// User and groups
   367  	if cred := sys.Credential; cred != nil {
   368  		ngroups := uintptr(len(cred.Groups))
   369  		groups := uintptr(0)
   370  		if ngroups > 0 {
   371  			groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
   372  		}
   373  		if !(sys.GidMappings != nil && !sys.GidMappingsEnableSetgroups && ngroups == 0) && !cred.NoSetGroups {
   374  			_, _, err1 = RawSyscall(_SYS_setgroups, ngroups, groups, 0)
   375  			if err1 != 0 {
   376  				goto childerror
   377  			}
   378  		}
   379  		_, _, err1 = RawSyscall(sys_SETGID, uintptr(cred.Gid), 0, 0)
   380  		if err1 != 0 {
   381  			goto childerror
   382  		}
   383  		_, _, err1 = RawSyscall(sys_SETUID, uintptr(cred.Uid), 0, 0)
   384  		if err1 != 0 {
   385  			goto childerror
   386  		}
   387  	}
   388  
   389  	if len(sys.AmbientCaps) != 0 {
   390  		// Ambient capabilities were added in the 4.3 kernel,
   391  		// so it is safe to always use _LINUX_CAPABILITY_VERSION_3.
   392  		caps.hdr.version = _LINUX_CAPABILITY_VERSION_3
   393  
   394  		if _, _, err1 := RawSyscall(SYS_CAPGET, uintptr(unsafe.Pointer(&caps.hdr)), uintptr(unsafe.Pointer(&caps.data[0])), 0); err1 != 0 {
   395  			goto childerror
   396  		}
   397  
   398  		for _, c := range sys.AmbientCaps {
   399  			// Add the c capability to the permitted and inheritable capability mask,
   400  			// otherwise we will not be able to add it to the ambient capability mask.
   401  			caps.data[capToIndex(c)].permitted |= capToMask(c)
   402  			caps.data[capToIndex(c)].inheritable |= capToMask(c)
   403  		}
   404  
   405  		if _, _, err1 := RawSyscall(SYS_CAPSET, uintptr(unsafe.Pointer(&caps.hdr)), uintptr(unsafe.Pointer(&caps.data[0])), 0); err1 != 0 {
   406  			goto childerror
   407  		}
   408  
   409  		for _, c := range sys.AmbientCaps {
   410  			_, _, err1 = RawSyscall6(SYS_PRCTL, PR_CAP_AMBIENT, uintptr(PR_CAP_AMBIENT_RAISE), c, 0, 0, 0)
   411  			if err1 != 0 {
   412  				goto childerror
   413  			}
   414  		}
   415  	}
   416  
   417  	// Chdir
   418  	if dir != nil {
   419  		_, _, err1 = RawSyscall(SYS_CHDIR, uintptr(unsafe.Pointer(dir)), 0, 0)
   420  		if err1 != 0 {
   421  			goto childerror
   422  		}
   423  	}
   424  
   425  	// Parent death signal
   426  	if sys.Pdeathsig != 0 {
   427  		_, _, err1 = RawSyscall6(SYS_PRCTL, PR_SET_PDEATHSIG, uintptr(sys.Pdeathsig), 0, 0, 0, 0)
   428  		if err1 != 0 {
   429  			goto childerror
   430  		}
   431  
   432  		// Signal self if parent is already dead. This might cause a
   433  		// duplicate signal in rare cases, but it won't matter when
   434  		// using SIGKILL.
   435  		r1, _ = rawSyscallNoError(SYS_GETPPID, 0, 0, 0)
   436  		if r1 != ppid {
   437  			pid, _ := rawSyscallNoError(SYS_GETPID, 0, 0, 0)
   438  			_, _, err1 := RawSyscall(SYS_KILL, pid, uintptr(sys.Pdeathsig), 0)
   439  			if err1 != 0 {
   440  				goto childerror
   441  			}
   442  		}
   443  	}
   444  
   445  	// Pass 1: look for fd[i] < i and move those up above len(fd)
   446  	// so that pass 2 won't stomp on an fd it needs later.
   447  	if pipe < nextfd {
   448  		_, _, err1 = RawSyscall(SYS_DUP3, uintptr(pipe), uintptr(nextfd), O_CLOEXEC)
   449  		if _SYS_dup != SYS_DUP3 && err1 == ENOSYS {
   450  			_, _, err1 = RawSyscall(_SYS_dup, uintptr(pipe), uintptr(nextfd), 0)
   451  			if err1 != 0 {
   452  				goto childerror
   453  			}
   454  			RawSyscall(fcntl64Syscall, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
   455  		} else if err1 != 0 {
   456  			goto childerror
   457  		}
   458  		pipe = nextfd
   459  		nextfd++
   460  	}
   461  	for i = 0; i < len(fd); i++ {
   462  		if fd[i] >= 0 && fd[i] < int(i) {
   463  			if nextfd == pipe { // don't stomp on pipe
   464  				nextfd++
   465  			}
   466  			_, _, err1 = RawSyscall(SYS_DUP3, uintptr(fd[i]), uintptr(nextfd), O_CLOEXEC)
   467  			if _SYS_dup != SYS_DUP3 && err1 == ENOSYS {
   468  				_, _, err1 = RawSyscall(_SYS_dup, uintptr(fd[i]), uintptr(nextfd), 0)
   469  				if err1 != 0 {
   470  					goto childerror
   471  				}
   472  				RawSyscall(fcntl64Syscall, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
   473  			} else if err1 != 0 {
   474  				goto childerror
   475  			}
   476  			fd[i] = nextfd
   477  			nextfd++
   478  		}
   479  	}
   480  
   481  	// Pass 2: dup fd[i] down onto i.
   482  	for i = 0; i < len(fd); i++ {
   483  		if fd[i] == -1 {
   484  			RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
   485  			continue
   486  		}
   487  		if fd[i] == int(i) {
   488  			// dup2(i, i) won't clear close-on-exec flag on Linux,
   489  			// probably not elsewhere either.
   490  			_, _, err1 = RawSyscall(fcntl64Syscall, uintptr(fd[i]), F_SETFD, 0)
   491  			if err1 != 0 {
   492  				goto childerror
   493  			}
   494  			continue
   495  		}
   496  		// The new fd is created NOT close-on-exec,
   497  		// which is exactly what we want.
   498  		_, _, err1 = RawSyscall(_SYS_dup, uintptr(fd[i]), uintptr(i), 0)
   499  		if err1 != 0 {
   500  			goto childerror
   501  		}
   502  	}
   503  
   504  	// By convention, we don't close-on-exec the fds we are
   505  	// started with, so if len(fd) < 3, close 0, 1, 2 as needed.
   506  	// Programs that know they inherit fds >= 3 will need
   507  	// to set them close-on-exec.
   508  	for i = len(fd); i < 3; i++ {
   509  		RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
   510  	}
   511  
   512  	// Detach fd 0 from tty
   513  	if sys.Noctty {
   514  		_, _, err1 = RawSyscall(SYS_IOCTL, 0, uintptr(TIOCNOTTY), 0)
   515  		if err1 != 0 {
   516  			goto childerror
   517  		}
   518  	}
   519  
   520  	// Set the controlling TTY to Ctty
   521  	if sys.Setctty {
   522  		_, _, err1 = RawSyscall(SYS_IOCTL, uintptr(sys.Ctty), uintptr(TIOCSCTTY), 1)
   523  		if err1 != 0 {
   524  			goto childerror
   525  		}
   526  	}
   527  
   528  	// Enable tracing if requested.
   529  	// Do this right before exec so that we don't unnecessarily trace the runtime
   530  	// setting up after the fork. See issue #21428.
   531  	if sys.Ptrace {
   532  		_, _, err1 = RawSyscall(SYS_PTRACE, uintptr(PTRACE_TRACEME), 0, 0)
   533  		if err1 != 0 {
   534  			goto childerror
   535  		}
   536  	}
   537  
   538  	// Time to exec.
   539  	_, _, err1 = RawSyscall(SYS_EXECVE,
   540  		uintptr(unsafe.Pointer(argv0)),
   541  		uintptr(unsafe.Pointer(&argv[0])),
   542  		uintptr(unsafe.Pointer(&envv[0])))
   543  
   544  childerror:
   545  	// send error code on pipe
   546  	RawSyscall(SYS_WRITE, uintptr(pipe), uintptr(unsafe.Pointer(&err1)), unsafe.Sizeof(err1))
   547  	for {
   548  		RawSyscall(SYS_EXIT, 253, 0, 0)
   549  	}
   550  }
   551  
   552  // Try to open a pipe with O_CLOEXEC set on both file descriptors.
   553  func forkExecPipe(p []int) (err error) {
   554  	err = Pipe2(p, O_CLOEXEC)
   555  	// pipe2 was added in 2.6.27 and our minimum requirement is 2.6.23, so it
   556  	// might not be implemented.
   557  	if err == ENOSYS {
   558  		if err = Pipe(p); err != nil {
   559  			return
   560  		}
   561  		if _, err = fcntl(p[0], F_SETFD, FD_CLOEXEC); err != nil {
   562  			return
   563  		}
   564  		_, err = fcntl(p[1], F_SETFD, FD_CLOEXEC)
   565  	}
   566  	return
   567  }
   568  
   569  func formatIDMappings(idMap []SysProcIDMap) []byte {
   570  	var data []byte
   571  	for _, im := range idMap {
   572  		data = append(data, []byte(itoa(im.ContainerID)+" "+itoa(im.HostID)+" "+itoa(im.Size)+"\n")...)
   573  	}
   574  	return data
   575  }
   576  
   577  // writeIDMappings writes the user namespace User ID or Group ID mappings to the specified path.
   578  func writeIDMappings(path string, idMap []SysProcIDMap) error {
   579  	fd, err := Open(path, O_RDWR, 0)
   580  	if err != nil {
   581  		return err
   582  	}
   583  
   584  	if _, err := Write(fd, formatIDMappings(idMap)); err != nil {
   585  		Close(fd)
   586  		return err
   587  	}
   588  
   589  	if err := Close(fd); err != nil {
   590  		return err
   591  	}
   592  
   593  	return nil
   594  }
   595  
   596  // writeSetgroups writes to /proc/PID/setgroups "deny" if enable is false
   597  // and "allow" if enable is true.
   598  // This is needed since kernel 3.19, because you can't write gid_map without
   599  // disabling setgroups() system call.
   600  func writeSetgroups(pid int, enable bool) error {
   601  	sgf := "/proc/" + itoa(pid) + "/setgroups"
   602  	fd, err := Open(sgf, O_RDWR, 0)
   603  	if err != nil {
   604  		return err
   605  	}
   606  
   607  	var data []byte
   608  	if enable {
   609  		data = []byte("allow")
   610  	} else {
   611  		data = []byte("deny")
   612  	}
   613  
   614  	if _, err := Write(fd, data); err != nil {
   615  		Close(fd)
   616  		return err
   617  	}
   618  
   619  	return Close(fd)
   620  }
   621  
   622  // writeUidGidMappings writes User ID and Group ID mappings for user namespaces
   623  // for a process and it is called from the parent process.
   624  func writeUidGidMappings(pid int, sys *SysProcAttr) error {
   625  	if sys.UidMappings != nil {
   626  		uidf := "/proc/" + itoa(pid) + "/uid_map"
   627  		if err := writeIDMappings(uidf, sys.UidMappings); err != nil {
   628  			return err
   629  		}
   630  	}
   631  
   632  	if sys.GidMappings != nil {
   633  		// If the kernel is too old to support /proc/PID/setgroups, writeSetGroups will return ENOENT; this is OK.
   634  		if err := writeSetgroups(pid, sys.GidMappingsEnableSetgroups); err != nil && err != ENOENT {
   635  			return err
   636  		}
   637  		gidf := "/proc/" + itoa(pid) + "/gid_map"
   638  		if err := writeIDMappings(gidf, sys.GidMappings); err != nil {
   639  			return err
   640  		}
   641  	}
   642  
   643  	return nil
   644  }
   645  

View as plain text