Source file src/syscall/exec_bsd.go

     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  //go:build dragonfly || netbsd || (openbsd && mips64)
     6  
     7  package syscall
     8  
     9  import (
    10  	"runtime"
    11  	"unsafe"
    12  )
    13  
    14  type SysProcAttr struct {
    15  	Chroot     string      // Chroot.
    16  	Credential *Credential // Credential.
    17  	Ptrace     bool        // Enable tracing.
    18  	Setsid     bool        // Create session.
    19  	// Setpgid sets the process group ID of the child to Pgid,
    20  	// or, if Pgid == 0, to the new child's process ID.
    21  	Setpgid bool
    22  	// Setctty sets the controlling terminal of the child to
    23  	// file descriptor Ctty. Ctty must be a descriptor number
    24  	// in the child process: an index into ProcAttr.Files.
    25  	// This is only meaningful if Setsid is true.
    26  	Setctty bool
    27  	Noctty  bool // Detach fd 0 from controlling terminal
    28  	Ctty    int  // Controlling TTY fd
    29  	// Foreground places the child process group in the foreground.
    30  	// This implies Setpgid. The Ctty field must be set to
    31  	// the descriptor of the controlling TTY.
    32  	// Unlike Setctty, in this case Ctty must be a descriptor
    33  	// number in the parent process.
    34  	Foreground bool
    35  	Pgid       int // Child's process group ID if Setpgid.
    36  }
    37  
    38  // Implemented in runtime package.
    39  func runtime_BeforeFork()
    40  func runtime_AfterFork()
    41  func runtime_AfterForkInChild()
    42  
    43  // Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
    44  // If a dup or exec fails, write the errno error to pipe.
    45  // (Pipe is close-on-exec so if exec succeeds, it will be closed.)
    46  // In the child, this function must not acquire any locks, because
    47  // they might have been locked at the time of the fork. This means
    48  // no rescheduling, no malloc calls, and no new stack segments.
    49  // For the same reason compiler does not race instrument it.
    50  // The calls to RawSyscall are okay because they are assembly
    51  // functions that do not grow the stack.
    52  //
    53  //go:norace
    54  func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) {
    55  	// Declare all variables at top in case any
    56  	// declarations require heap allocation (e.g., err1).
    57  	var (
    58  		r1     uintptr
    59  		err1   Errno
    60  		nextfd int
    61  		i      int
    62  	)
    63  
    64  	// guard against side effects of shuffling fds below.
    65  	// Make sure that nextfd is beyond any currently open files so
    66  	// that we can't run the risk of overwriting any of them.
    67  	fd := make([]int, len(attr.Files))
    68  	nextfd = len(attr.Files)
    69  	for i, ufd := range attr.Files {
    70  		if nextfd < int(ufd) {
    71  			nextfd = int(ufd)
    72  		}
    73  		fd[i] = int(ufd)
    74  	}
    75  	nextfd++
    76  
    77  	// About to call fork.
    78  	// No more allocation or calls of non-assembly functions.
    79  	runtime_BeforeFork()
    80  	r1, _, err1 = RawSyscall(SYS_FORK, 0, 0, 0)
    81  	if err1 != 0 {
    82  		runtime_AfterFork()
    83  		return 0, err1
    84  	}
    85  
    86  	if r1 != 0 {
    87  		// parent; return PID
    88  		runtime_AfterFork()
    89  		return int(r1), 0
    90  	}
    91  
    92  	// Fork succeeded, now in child.
    93  
    94  	// Enable tracing if requested.
    95  	if sys.Ptrace {
    96  		_, _, err1 = RawSyscall(SYS_PTRACE, uintptr(PTRACE_TRACEME), 0, 0)
    97  		if err1 != 0 {
    98  			goto childerror
    99  		}
   100  	}
   101  
   102  	// Session ID
   103  	if sys.Setsid {
   104  		_, _, err1 = RawSyscall(SYS_SETSID, 0, 0, 0)
   105  		if err1 != 0 {
   106  			goto childerror
   107  		}
   108  	}
   109  
   110  	// Set process group
   111  	if sys.Setpgid || sys.Foreground {
   112  		// Place child in process group.
   113  		_, _, err1 = RawSyscall(SYS_SETPGID, 0, uintptr(sys.Pgid), 0)
   114  		if err1 != 0 {
   115  			goto childerror
   116  		}
   117  	}
   118  
   119  	if sys.Foreground {
   120  		// This should really be pid_t, however _C_int (aka int32) is
   121  		// generally equivalent.
   122  		pgrp := _C_int(sys.Pgid)
   123  		if pgrp == 0 {
   124  			r1, _, err1 = RawSyscall(SYS_GETPID, 0, 0, 0)
   125  			if err1 != 0 {
   126  				goto childerror
   127  			}
   128  
   129  			pgrp = _C_int(r1)
   130  		}
   131  
   132  		// Place process group in foreground.
   133  		_, _, err1 = RawSyscall(SYS_IOCTL, uintptr(sys.Ctty), uintptr(TIOCSPGRP), uintptr(unsafe.Pointer(&pgrp)))
   134  		if err1 != 0 {
   135  			goto childerror
   136  		}
   137  	}
   138  
   139  	// Restore the signal mask. We do this after TIOCSPGRP to avoid
   140  	// having the kernel send a SIGTTOU signal to the process group.
   141  	runtime_AfterForkInChild()
   142  
   143  	// Chroot
   144  	if chroot != nil {
   145  		_, _, err1 = RawSyscall(SYS_CHROOT, uintptr(unsafe.Pointer(chroot)), 0, 0)
   146  		if err1 != 0 {
   147  			goto childerror
   148  		}
   149  	}
   150  
   151  	// User and groups
   152  	if cred := sys.Credential; cred != nil {
   153  		ngroups := uintptr(len(cred.Groups))
   154  		groups := uintptr(0)
   155  		if ngroups > 0 {
   156  			groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
   157  		}
   158  		if !cred.NoSetGroups {
   159  			_, _, err1 = RawSyscall(SYS_SETGROUPS, ngroups, groups, 0)
   160  			if err1 != 0 {
   161  				goto childerror
   162  			}
   163  		}
   164  		_, _, err1 = RawSyscall(SYS_SETGID, uintptr(cred.Gid), 0, 0)
   165  		if err1 != 0 {
   166  			goto childerror
   167  		}
   168  		_, _, err1 = RawSyscall(SYS_SETUID, uintptr(cred.Uid), 0, 0)
   169  		if err1 != 0 {
   170  			goto childerror
   171  		}
   172  	}
   173  
   174  	// Chdir
   175  	if dir != nil {
   176  		_, _, err1 = RawSyscall(SYS_CHDIR, uintptr(unsafe.Pointer(dir)), 0, 0)
   177  		if err1 != 0 {
   178  			goto childerror
   179  		}
   180  	}
   181  
   182  	// Pass 1: look for fd[i] < i and move those up above len(fd)
   183  	// so that pass 2 won't stomp on an fd it needs later.
   184  	if pipe < nextfd {
   185  		if runtime.GOOS == "netbsd" || (runtime.GOOS == "openbsd" && runtime.GOARCH == "mips64") {
   186  			_, _, err1 = RawSyscall(_SYS_DUP3, uintptr(pipe), uintptr(nextfd), O_CLOEXEC)
   187  		} else if runtime.GOOS == "dragonfly" {
   188  			_, _, err1 = RawSyscall(SYS_FCNTL, uintptr(pipe), _F_DUP2FD_CLOEXEC, uintptr(nextfd))
   189  		} else {
   190  			_, _, err1 = RawSyscall(SYS_DUP2, uintptr(pipe), uintptr(nextfd), 0)
   191  			if err1 != 0 {
   192  				goto childerror
   193  			}
   194  			_, _, err1 = RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
   195  		}
   196  		if err1 != 0 {
   197  			goto childerror
   198  		}
   199  		pipe = nextfd
   200  		nextfd++
   201  	}
   202  	for i = 0; i < len(fd); i++ {
   203  		if fd[i] >= 0 && fd[i] < i {
   204  			if nextfd == pipe { // don't stomp on pipe
   205  				nextfd++
   206  			}
   207  			if runtime.GOOS == "netbsd" || (runtime.GOOS == "openbsd" && runtime.GOARCH == "mips64") {
   208  				_, _, err1 = RawSyscall(_SYS_DUP3, uintptr(fd[i]), uintptr(nextfd), O_CLOEXEC)
   209  			} else if runtime.GOOS == "dragonfly" {
   210  				_, _, err1 = RawSyscall(SYS_FCNTL, uintptr(fd[i]), _F_DUP2FD_CLOEXEC, uintptr(nextfd))
   211  			} else {
   212  				_, _, err1 = RawSyscall(SYS_DUP2, uintptr(fd[i]), uintptr(nextfd), 0)
   213  				if err1 != 0 {
   214  					goto childerror
   215  				}
   216  				_, _, err1 = RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
   217  			}
   218  			if err1 != 0 {
   219  				goto childerror
   220  			}
   221  			fd[i] = nextfd
   222  			nextfd++
   223  		}
   224  	}
   225  
   226  	// Pass 2: dup fd[i] down onto i.
   227  	for i = 0; i < len(fd); i++ {
   228  		if fd[i] == -1 {
   229  			RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
   230  			continue
   231  		}
   232  		if fd[i] == i {
   233  			// dup2(i, i) won't clear close-on-exec flag on Linux,
   234  			// probably not elsewhere either.
   235  			_, _, err1 = RawSyscall(SYS_FCNTL, uintptr(fd[i]), F_SETFD, 0)
   236  			if err1 != 0 {
   237  				goto childerror
   238  			}
   239  			continue
   240  		}
   241  		// The new fd is created NOT close-on-exec,
   242  		// which is exactly what we want.
   243  		_, _, err1 = RawSyscall(SYS_DUP2, uintptr(fd[i]), uintptr(i), 0)
   244  		if err1 != 0 {
   245  			goto childerror
   246  		}
   247  	}
   248  
   249  	// By convention, we don't close-on-exec the fds we are
   250  	// started with, so if len(fd) < 3, close 0, 1, 2 as needed.
   251  	// Programs that know they inherit fds >= 3 will need
   252  	// to set them close-on-exec.
   253  	for i = len(fd); i < 3; i++ {
   254  		RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
   255  	}
   256  
   257  	// Detach fd 0 from tty
   258  	if sys.Noctty {
   259  		_, _, err1 = RawSyscall(SYS_IOCTL, 0, uintptr(TIOCNOTTY), 0)
   260  		if err1 != 0 {
   261  			goto childerror
   262  		}
   263  	}
   264  
   265  	// Set the controlling TTY to Ctty
   266  	if sys.Setctty {
   267  		_, _, err1 = RawSyscall(SYS_IOCTL, uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0)
   268  		if err1 != 0 {
   269  			goto childerror
   270  		}
   271  	}
   272  
   273  	// Time to exec.
   274  	_, _, err1 = RawSyscall(SYS_EXECVE,
   275  		uintptr(unsafe.Pointer(argv0)),
   276  		uintptr(unsafe.Pointer(&argv[0])),
   277  		uintptr(unsafe.Pointer(&envv[0])))
   278  
   279  childerror:
   280  	// send error code on pipe
   281  	RawSyscall(SYS_WRITE, uintptr(pipe), uintptr(unsafe.Pointer(&err1)), unsafe.Sizeof(err1))
   282  	for {
   283  		RawSyscall(SYS_EXIT, 253, 0, 0)
   284  	}
   285  }
   286  

View as plain text