1*8269e767SBrooks Davis.\" Copyright (c) 1980, 1991, 1993 2*8269e767SBrooks Davis.\" The Regents of the University of California. All rights reserved. 3*8269e767SBrooks Davis.\" 4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 6*8269e767SBrooks Davis.\" are met: 7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 8*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 10*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 11*8269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors 13*8269e767SBrooks Davis.\" may be used to endorse or promote products derived from this software 14*8269e767SBrooks Davis.\" without specific prior written permission. 15*8269e767SBrooks Davis.\" 16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*8269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*8269e767SBrooks Davis.\" SUCH DAMAGE. 27*8269e767SBrooks Davis.\" 28*8269e767SBrooks Davis.Dd January 26, 2022 29*8269e767SBrooks Davis.Dt EXECVE 2 30*8269e767SBrooks Davis.Os 31*8269e767SBrooks Davis.Sh NAME 32*8269e767SBrooks Davis.Nm execve , 33*8269e767SBrooks Davis.Nm fexecve 34*8269e767SBrooks Davis.Nd execute a file 35*8269e767SBrooks Davis.Sh LIBRARY 36*8269e767SBrooks Davis.Lb libc 37*8269e767SBrooks Davis.Sh SYNOPSIS 38*8269e767SBrooks Davis.In unistd.h 39*8269e767SBrooks Davis.Ft int 40*8269e767SBrooks Davis.Fn execve "const char *path" "char *const argv[]" "char *const envp[]" 41*8269e767SBrooks Davis.Ft int 42*8269e767SBrooks Davis.Fn fexecve "int fd" "char *const argv[]" "char *const envp[]" 43*8269e767SBrooks Davis.Sh DESCRIPTION 44*8269e767SBrooks DavisThe 45*8269e767SBrooks Davis.Fn execve 46*8269e767SBrooks Davissystem call 47*8269e767SBrooks Davistransforms the calling process into a new process. 48*8269e767SBrooks DavisThe new process is constructed from an ordinary file, 49*8269e767SBrooks Daviswhose name is pointed to by 50*8269e767SBrooks Davis.Fa path , 51*8269e767SBrooks Daviscalled the 52*8269e767SBrooks Davis.Em new process file . 53*8269e767SBrooks DavisThe 54*8269e767SBrooks Davis.Fn fexecve 55*8269e767SBrooks Davissystem call is equivalent to 56*8269e767SBrooks Davis.Fn execve 57*8269e767SBrooks Davisexcept that the file to be executed is determined by the file 58*8269e767SBrooks Davisdescriptor 59*8269e767SBrooks Davis.Fa fd 60*8269e767SBrooks Davisinstead of a 61*8269e767SBrooks Davis.Fa path . 62*8269e767SBrooks DavisThis file is either an executable object file, 63*8269e767SBrooks Davisor a file of data for an interpreter. 64*8269e767SBrooks DavisAn executable object file consists of an identifying header, 65*8269e767SBrooks Davisfollowed by pages of data representing the initial program (text) 66*8269e767SBrooks Davisand initialized data pages. 67*8269e767SBrooks DavisAdditional pages may be specified 68*8269e767SBrooks Davisby the header to be initialized with zero data; see 69*8269e767SBrooks Davis.Xr elf 5 70*8269e767SBrooks Davisand 71*8269e767SBrooks Davis.Xr a.out 5 . 72*8269e767SBrooks Davis.Pp 73*8269e767SBrooks DavisAn interpreter file begins with a line of the form: 74*8269e767SBrooks Davis.Pp 75*8269e767SBrooks Davis.Bd -ragged -offset indent -compact 76*8269e767SBrooks Davis.Sy \&#! 77*8269e767SBrooks Davis.Em interpreter 78*8269e767SBrooks Davis.Bq Em arg 79*8269e767SBrooks Davis.Ed 80*8269e767SBrooks Davis.Pp 81*8269e767SBrooks DavisWhen an interpreter file is 82*8269e767SBrooks Davis.Sy execve Ap d , 83*8269e767SBrooks Davisthe system actually 84*8269e767SBrooks Davis.Sy execve Ap s 85*8269e767SBrooks Davisthe specified 86*8269e767SBrooks Davis.Em interpreter . 87*8269e767SBrooks DavisIf the optional 88*8269e767SBrooks Davis.Em arg 89*8269e767SBrooks Davisis specified, it becomes the first argument to the 90*8269e767SBrooks Davis.Em interpreter , 91*8269e767SBrooks Davisand the name of the originally 92*8269e767SBrooks Davis.Sy execve Ap d 93*8269e767SBrooks Davisfile becomes the second argument; 94*8269e767SBrooks Davisotherwise, the name of the originally 95*8269e767SBrooks Davis.Sy execve Ap d 96*8269e767SBrooks Davisfile becomes the first argument. 97*8269e767SBrooks DavisThe original arguments are shifted over to 98*8269e767SBrooks Davisbecome the subsequent arguments. 99*8269e767SBrooks DavisThe zeroth argument is set to the specified 100*8269e767SBrooks Davis.Em interpreter . 101*8269e767SBrooks Davis.Pp 102*8269e767SBrooks DavisThe argument 103*8269e767SBrooks Davis.Fa argv 104*8269e767SBrooks Davisis a pointer to a null-terminated array of 105*8269e767SBrooks Davischaracter pointers to null-terminated character strings. 106*8269e767SBrooks DavisThese strings construct the argument list to be made available to the new 107*8269e767SBrooks Davisprocess. 108*8269e767SBrooks DavisAt least one argument must be present in 109*8269e767SBrooks Davisthe array; by custom, the first element should be 110*8269e767SBrooks Davisthe name of the executed program (for example, the last component of 111*8269e767SBrooks Davis.Fa path ) . 112*8269e767SBrooks Davis.Pp 113*8269e767SBrooks DavisThe argument 114*8269e767SBrooks Davis.Fa envp 115*8269e767SBrooks Davisis also a pointer to a null-terminated array of 116*8269e767SBrooks Davischaracter pointers to null-terminated strings. 117*8269e767SBrooks DavisA pointer to this array is normally stored in the global variable 118*8269e767SBrooks Davis.Va environ . 119*8269e767SBrooks DavisThese strings pass information to the 120*8269e767SBrooks Davisnew process that is not directly an argument to the command (see 121*8269e767SBrooks Davis.Xr environ 7 ) . 122*8269e767SBrooks Davis.Pp 123*8269e767SBrooks DavisFile descriptors open in the calling process image remain open in 124*8269e767SBrooks Davisthe new process image, except for those for which the close-on-exec 125*8269e767SBrooks Davisflag is set (see 126*8269e767SBrooks Davis.Xr close 2 127*8269e767SBrooks Davisand 128*8269e767SBrooks Davis.Xr fcntl 2 ) . 129*8269e767SBrooks DavisDescriptors that remain open are unaffected by 130*8269e767SBrooks Davis.Fn execve . 131*8269e767SBrooks DavisIf any of the standard descriptors (0, 1, and/or 2) are closed at the 132*8269e767SBrooks Davistime 133*8269e767SBrooks Davis.Fn execve 134*8269e767SBrooks Davisis called, and the process will gain privilege as a result of set-id 135*8269e767SBrooks Davissemantics, those descriptors will be re-opened automatically. 136*8269e767SBrooks DavisNo programs, whether privileged or not, should assume that these descriptors 137*8269e767SBrooks Daviswill remain closed across a call to 138*8269e767SBrooks Davis.Fn execve . 139*8269e767SBrooks Davis.Pp 140*8269e767SBrooks DavisSignals set to be ignored in the calling process are set to be ignored in 141*8269e767SBrooks Davisthe 142*8269e767SBrooks Davisnew process. 143*8269e767SBrooks DavisSignals which are set to be caught in the calling process image 144*8269e767SBrooks Davisare set to default action in the new process image. 145*8269e767SBrooks DavisBlocked signals remain blocked regardless of changes to the signal action. 146*8269e767SBrooks DavisThe signal stack is reset to be undefined (see 147*8269e767SBrooks Davis.Xr sigaction 2 148*8269e767SBrooks Davisfor more information). 149*8269e767SBrooks Davis.Pp 150*8269e767SBrooks DavisIf the set-user-ID mode bit of the new process image file is set 151*8269e767SBrooks Davis(see 152*8269e767SBrooks Davis.Xr chmod 2 ) , 153*8269e767SBrooks Davisthe effective user ID of the new process image is set to the owner ID 154*8269e767SBrooks Davisof the new process image file. 155*8269e767SBrooks DavisIf the set-group-ID mode bit of the new process image file is set, 156*8269e767SBrooks Davisthe effective group ID of the new process image is set to the group ID 157*8269e767SBrooks Davisof the new process image file. 158*8269e767SBrooks Davis(The effective group ID is the first element of the group list.) 159*8269e767SBrooks DavisThe real user ID, real group ID and 160*8269e767SBrooks Davisother group IDs of the new process image remain the same as the calling 161*8269e767SBrooks Davisprocess image. 162*8269e767SBrooks DavisAfter any set-user-ID and set-group-ID processing, 163*8269e767SBrooks Davisthe effective user ID is recorded as the saved set-user-ID, 164*8269e767SBrooks Davisand the effective group ID is recorded as the saved set-group-ID. 165*8269e767SBrooks DavisThese values may be used in changing the effective IDs later (see 166*8269e767SBrooks Davis.Xr setuid 2 ) . 167*8269e767SBrooks Davis.Pp 168*8269e767SBrooks DavisThe set-ID bits are not honored if the respective file system has the 169*8269e767SBrooks Davis.Cm nosuid 170*8269e767SBrooks Davisoption enabled or if the new process file is an interpreter file. 171*8269e767SBrooks DavisSyscall 172*8269e767SBrooks Davistracing is disabled if effective IDs are changed. 173*8269e767SBrooks Davis.Pp 174*8269e767SBrooks DavisThe new process also inherits the following attributes from 175*8269e767SBrooks Davisthe calling process: 176*8269e767SBrooks Davis.Pp 177*8269e767SBrooks Davis.Bl -column parent_process_ID -offset indent -compact 178*8269e767SBrooks Davis.It process ID Ta see Xr getpid 2 179*8269e767SBrooks Davis.It parent process ID Ta see Xr getppid 2 180*8269e767SBrooks Davis.It process group ID Ta see Xr getpgrp 2 181*8269e767SBrooks Davis.It access groups Ta see Xr getgroups 2 182*8269e767SBrooks Davis.It working directory Ta see Xr chdir 2 183*8269e767SBrooks Davis.It root directory Ta see Xr chroot 2 184*8269e767SBrooks Davis.It control terminal Ta see Xr termios 4 185*8269e767SBrooks Davis.It resource usages Ta see Xr getrusage 2 186*8269e767SBrooks Davis.It interval timers Ta see Xr getitimer 2 187*8269e767SBrooks Davis.It resource limits Ta see Xr getrlimit 2 188*8269e767SBrooks Davis.It file mode mask Ta see Xr umask 2 189*8269e767SBrooks Davis.It signal mask Ta see Xr sigaction 2 , 190*8269e767SBrooks Davis.Xr sigprocmask 2 191*8269e767SBrooks Davis.El 192*8269e767SBrooks Davis.Pp 193*8269e767SBrooks DavisWhen a program is executed as a result of an 194*8269e767SBrooks Davis.Fn execve 195*8269e767SBrooks Davissystem call, it is entered as follows: 196*8269e767SBrooks Davis.Bd -literal -offset indent 197*8269e767SBrooks Davismain(argc, argv, envp) 198*8269e767SBrooks Davisint argc; 199*8269e767SBrooks Davischar **argv, **envp; 200*8269e767SBrooks Davis.Ed 201*8269e767SBrooks Davis.Pp 202*8269e767SBrooks Daviswhere 203*8269e767SBrooks Davis.Fa argc 204*8269e767SBrooks Davisis the number of elements in 205*8269e767SBrooks Davis.Fa argv 206*8269e767SBrooks Davis(the ``arg count'') 207*8269e767SBrooks Davisand 208*8269e767SBrooks Davis.Fa argv 209*8269e767SBrooks Davispoints to the array of character pointers 210*8269e767SBrooks Davisto the arguments themselves. 211*8269e767SBrooks Davis.Pp 212*8269e767SBrooks DavisThe 213*8269e767SBrooks Davis.Fn fexecve 214*8269e767SBrooks Davisignores the file offset of 215*8269e767SBrooks Davis.Fa fd . 216*8269e767SBrooks DavisSince execute permission is checked by 217*8269e767SBrooks Davis.Fn fexecve , 218*8269e767SBrooks Davisthe file descriptor 219*8269e767SBrooks Davis.Fa fd 220*8269e767SBrooks Davisneed not have been opened with the 221*8269e767SBrooks Davis.Dv O_EXEC 222*8269e767SBrooks Davisflag. 223*8269e767SBrooks DavisHowever, if the file to be executed denies read permission for the process 224*8269e767SBrooks Davispreparing to do the exec, the only way to provide the 225*8269e767SBrooks Davis.Fa fd 226*8269e767SBrooks Davisto 227*8269e767SBrooks Davis.Fn fexecve 228*8269e767SBrooks Davisis to use the 229*8269e767SBrooks Davis.Dv O_EXEC 230*8269e767SBrooks Davisflag when opening 231*8269e767SBrooks Davis.Fa fd . 232*8269e767SBrooks DavisNote that the file to be executed can not be open for writing. 233*8269e767SBrooks Davis.Sh RETURN VALUES 234*8269e767SBrooks DavisAs the 235*8269e767SBrooks Davis.Fn execve 236*8269e767SBrooks Davissystem call overlays the current process image 237*8269e767SBrooks Daviswith a new process image the successful call 238*8269e767SBrooks Davishas no process to return to. 239*8269e767SBrooks DavisIf 240*8269e767SBrooks Davis.Fn execve 241*8269e767SBrooks Davisdoes return to the calling process an error has occurred; the 242*8269e767SBrooks Davisreturn value will be -1 and the global variable 243*8269e767SBrooks Davis.Va errno 244*8269e767SBrooks Davisis set to indicate the error. 245*8269e767SBrooks Davis.Sh ERRORS 246*8269e767SBrooks DavisThe 247*8269e767SBrooks Davis.Fn execve 248*8269e767SBrooks Davissystem call 249*8269e767SBrooks Daviswill fail and return to the calling process if: 250*8269e767SBrooks Davis.Bl -tag -width Er 251*8269e767SBrooks Davis.It Bq Er ENOTDIR 252*8269e767SBrooks DavisA component of the path prefix is not a directory. 253*8269e767SBrooks Davis.It Bq Er ENAMETOOLONG 254*8269e767SBrooks DavisA component of a pathname exceeded 255 characters, 255*8269e767SBrooks Davisor an entire path name exceeded 1023 characters. 256*8269e767SBrooks Davis.It Bq Er ENOEXEC 257*8269e767SBrooks DavisWhen invoking an interpreted script, the length of the first line, 258*8269e767SBrooks Davisinclusive of the 259*8269e767SBrooks Davis.Sy \&#! 260*8269e767SBrooks Davisprefix and terminating newline, exceeds 261*8269e767SBrooks Davis.Dv MAXSHELLCMDLEN 262*8269e767SBrooks Davischaracters. 263*8269e767SBrooks Davis.It Bq Er ENOENT 264*8269e767SBrooks DavisThe new process file does not exist. 265*8269e767SBrooks Davis.It Bq Er ELOOP 266*8269e767SBrooks DavisToo many symbolic links were encountered in translating the pathname. 267*8269e767SBrooks Davis.It Bq Er EACCES 268*8269e767SBrooks DavisSearch permission is denied for a component of the path prefix. 269*8269e767SBrooks Davis.It Bq Er EACCES 270*8269e767SBrooks DavisThe new process file is not an ordinary file. 271*8269e767SBrooks Davis.It Bq Er EACCES 272*8269e767SBrooks DavisThe new process file mode denies execute permission. 273*8269e767SBrooks Davis.It Bq Er EINVAL 274*8269e767SBrooks Davis.Fa argv 275*8269e767SBrooks Davisdid not contain at least one element. 276*8269e767SBrooks Davis.It Bq Er ENOEXEC 277*8269e767SBrooks DavisThe new process file has the appropriate access 278*8269e767SBrooks Davispermission, but has an invalid magic number in its header. 279*8269e767SBrooks Davis.It Bq Er ETXTBSY 280*8269e767SBrooks DavisThe new process file is a pure procedure (shared text) 281*8269e767SBrooks Davisfile that is currently open for writing by some process. 282*8269e767SBrooks Davis.It Bq Er ENOMEM 283*8269e767SBrooks DavisThe new process requires more virtual memory than 284*8269e767SBrooks Davisis allowed by the imposed maximum 285*8269e767SBrooks Davis.Pq Xr getrlimit 2 . 286*8269e767SBrooks Davis.It Bq Er E2BIG 287*8269e767SBrooks DavisThe number of bytes in the new process' argument list 288*8269e767SBrooks Davisis larger than the system-imposed limit. 289*8269e767SBrooks DavisThis limit is specified by the 290*8269e767SBrooks Davis.Xr sysctl 3 291*8269e767SBrooks DavisMIB variable 292*8269e767SBrooks Davis.Dv KERN_ARGMAX . 293*8269e767SBrooks Davis.It Bq Er EFAULT 294*8269e767SBrooks DavisThe new process file is not as long as indicated by 295*8269e767SBrooks Davisthe size values in its header. 296*8269e767SBrooks Davis.It Bq Er EFAULT 297*8269e767SBrooks DavisThe 298*8269e767SBrooks Davis.Fa path , 299*8269e767SBrooks Davis.Fa argv , 300*8269e767SBrooks Davisor 301*8269e767SBrooks Davis.Fa envp 302*8269e767SBrooks Davisarguments 303*8269e767SBrooks Davispoint 304*8269e767SBrooks Davisto an illegal address. 305*8269e767SBrooks Davis.It Bq Er EIO 306*8269e767SBrooks DavisAn I/O error occurred while reading from the file system. 307*8269e767SBrooks Davis.It Bq Er EINTEGRITY 308*8269e767SBrooks DavisCorrupted data was detected while reading from the file system. 309*8269e767SBrooks Davis.El 310*8269e767SBrooks Davis.Pp 311*8269e767SBrooks DavisIn addition, the 312*8269e767SBrooks Davis.Fn fexecve 313*8269e767SBrooks Daviswill fail and return to the calling process if: 314*8269e767SBrooks Davis.Bl -tag -width Er 315*8269e767SBrooks Davis.It Bq Er EBADF 316*8269e767SBrooks DavisThe 317*8269e767SBrooks Davis.Fa fd 318*8269e767SBrooks Davisargument is not a valid file descriptor open for executing. 319*8269e767SBrooks Davis.El 320*8269e767SBrooks Davis.Sh SEE ALSO 321*8269e767SBrooks Davis.Xr ktrace 1 , 322*8269e767SBrooks Davis.Xr _exit 2 , 323*8269e767SBrooks Davis.Xr fork 2 , 324*8269e767SBrooks Davis.Xr open 2 , 325*8269e767SBrooks Davis.Xr execl 3 , 326*8269e767SBrooks Davis.Xr exit 3 , 327*8269e767SBrooks Davis.Xr sysctl 3 , 328*8269e767SBrooks Davis.Xr a.out 5 , 329*8269e767SBrooks Davis.Xr elf 5 , 330*8269e767SBrooks Davis.Xr fdescfs 5 , 331*8269e767SBrooks Davis.Xr environ 7 , 332*8269e767SBrooks Davis.Xr mount 8 333*8269e767SBrooks Davis.Sh STANDARDS 334*8269e767SBrooks DavisThe 335*8269e767SBrooks Davis.Fn execve 336*8269e767SBrooks Davissystem call conforms to 337*8269e767SBrooks Davis.St -p1003.1-2001 , 338*8269e767SBrooks Daviswith the exception of reopening descriptors 0, 1, and/or 2 in certain 339*8269e767SBrooks Daviscircumstances. 340*8269e767SBrooks DavisA future update of the Standard is expected to require this behavior, 341*8269e767SBrooks Davisand it may become the default for non-privileged processes as well. 342*8269e767SBrooks Davis.\" NB: update this caveat when TC1 is blessed. 343*8269e767SBrooks DavisThe support for executing interpreted programs is an extension. 344*8269e767SBrooks DavisThe 345*8269e767SBrooks Davis.Fn fexecve 346*8269e767SBrooks Davissystem call conforms to The Open Group Extended API Set 2 specification. 347*8269e767SBrooks Davis.Sh HISTORY 348*8269e767SBrooks DavisThe 349*8269e767SBrooks Davis.Fn execve 350*8269e767SBrooks Davissystem call appeared in 351*8269e767SBrooks Davis.At v7 . 352*8269e767SBrooks DavisThe 353*8269e767SBrooks Davis.Fn fexecve 354*8269e767SBrooks Davissystem call appeared in 355*8269e767SBrooks Davis.Fx 8.0 . 356*8269e767SBrooks Davis.Sh CAVEATS 357*8269e767SBrooks DavisIf a program is 358*8269e767SBrooks Davis.Em setuid 359*8269e767SBrooks Davisto a non-super-user, but is executed when 360*8269e767SBrooks Davisthe real 361*8269e767SBrooks Davis.Em uid 362*8269e767SBrooks Davisis ``root'', then the program has some of the powers 363*8269e767SBrooks Davisof a super-user as well. 364*8269e767SBrooks Davis.Pp 365*8269e767SBrooks DavisWhen executing an interpreted program through 366*8269e767SBrooks Davis.Fn fexecve , 367*8269e767SBrooks Daviskernel supplies 368*8269e767SBrooks Davis.Pa /dev/fd/n 369*8269e767SBrooks Davisas a second argument to the interpreter, 370*8269e767SBrooks Daviswhere 371*8269e767SBrooks Davis.Ar n 372*8269e767SBrooks Davisis the file descriptor passed in the 373*8269e767SBrooks Davis.Fa fd 374*8269e767SBrooks Davisargument to 375*8269e767SBrooks Davis.Fn fexecve . 376*8269e767SBrooks DavisFor this construction to work correctly, the 377*8269e767SBrooks Davis.Xr fdescfs 5 378*8269e767SBrooks Davisfilesystem shall be mounted on 379*8269e767SBrooks Davis.Pa /dev/fd . 380