kern_exec.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /* $OpenBSD: kern_exec.c,v 1.163 2015/07/22 05:31:33 deraadt Exp $ */
  2. /* $NetBSD: kern_exec.c,v 1.75 1996/02/09 18:59:28 christos Exp $ */
  3. /*-
  4. * Copyright (C) 1993, 1994 Christopher G. Demetriou
  5. * Copyright (C) 1992 Wolfgang Solfrank.
  6. * Copyright (C) 1992 TooLs GmbH.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by TooLs GmbH.
  20. * 4. The name of TooLs GmbH may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
  24. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  29. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  31. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  32. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <sys/param.h>
  35. #include <sys/systm.h>
  36. #include <sys/filedesc.h>
  37. #include <sys/kernel.h>
  38. #include <sys/proc.h>
  39. #include <sys/mount.h>
  40. #include <sys/malloc.h>
  41. #include <sys/pool.h>
  42. #include <sys/namei.h>
  43. #include <sys/vnode.h>
  44. #include <sys/file.h>
  45. #include <sys/acct.h>
  46. #include <sys/exec.h>
  47. #include <sys/ktrace.h>
  48. #include <sys/resourcevar.h>
  49. #include <sys/wait.h>
  50. #include <sys/mman.h>
  51. #include <sys/signalvar.h>
  52. #include <sys/stat.h>
  53. #include <sys/conf.h>
  54. #ifdef SYSVSHM
  55. #include <sys/shm.h>
  56. #endif
  57. #include <sys/syscallargs.h>
  58. #include <uvm/uvm_extern.h>
  59. #ifdef __HAVE_MD_TCB
  60. # include <machine/tcb.h>
  61. #endif
  62. #include "systrace.h"
  63. #if NSYSTRACE > 0
  64. #include <dev/systrace.h>
  65. #endif
  66. const struct kmem_va_mode kv_exec = {
  67. .kv_wait = 1,
  68. .kv_map = &exec_map
  69. };
  70. /*
  71. * Map the shared signal code.
  72. */
  73. int exec_sigcode_map(struct process *, struct emul *);
  74. /*
  75. * If non-zero, stackgap_random specifies the upper limit of the random gap size
  76. * added to the fixed stack position. Must be n^2.
  77. */
  78. int stackgap_random = STACKGAP_RANDOM;
  79. /*
  80. * check exec:
  81. * given an "executable" described in the exec package's namei info,
  82. * see what we can do with it.
  83. *
  84. * ON ENTRY:
  85. * exec package with appropriate namei info
  86. * proc pointer of exec'ing proc
  87. * NO SELF-LOCKED VNODES
  88. *
  89. * ON EXIT:
  90. * error: nothing held, etc. exec header still allocated.
  91. * ok: filled exec package, one locked vnode.
  92. *
  93. * EXEC SWITCH ENTRY:
  94. * Locked vnode to check, exec package, proc.
  95. *
  96. * EXEC SWITCH EXIT:
  97. * ok: return 0, filled exec package, one locked vnode.
  98. * error: destructive:
  99. * everything deallocated except exec header.
  100. * non-destructive:
  101. * error code, locked vnode, exec header unmodified
  102. */
  103. int
  104. check_exec(struct proc *p, struct exec_package *epp)
  105. {
  106. int error, i;
  107. struct vnode *vp;
  108. struct nameidata *ndp;
  109. size_t resid;
  110. ndp = epp->ep_ndp;
  111. ndp->ni_cnd.cn_nameiop = LOOKUP;
  112. ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
  113. /* first get the vnode */
  114. if ((error = namei(ndp)) != 0)
  115. return (error);
  116. epp->ep_vp = vp = ndp->ni_vp;
  117. /* check for regular file */
  118. if (vp->v_type == VDIR) {
  119. error = EISDIR;
  120. goto bad1;
  121. }
  122. if (vp->v_type != VREG) {
  123. error = EACCES;
  124. goto bad1;
  125. }
  126. /* get attributes */
  127. if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
  128. goto bad1;
  129. /* Check mount point */
  130. if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
  131. error = EACCES;
  132. goto bad1;
  133. }
  134. if ((vp->v_mount->mnt_flag & MNT_NOSUID))
  135. epp->ep_vap->va_mode &= ~(VSUID | VSGID);
  136. /* check access. for root we have to see if any exec bit on */
  137. if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
  138. goto bad1;
  139. if ((epp->ep_vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
  140. error = EACCES;
  141. goto bad1;
  142. }
  143. /* try to open it */
  144. if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
  145. goto bad1;
  146. /* unlock vp, we need it unlocked from here */
  147. VOP_UNLOCK(vp, 0, p);
  148. /* now we have the file, get the exec header */
  149. error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
  150. UIO_SYSSPACE, 0, p->p_ucred, &resid, p);
  151. if (error)
  152. goto bad2;
  153. epp->ep_hdrvalid = epp->ep_hdrlen - resid;
  154. /*
  155. * set up the vmcmds for creation of the process
  156. * address space
  157. */
  158. error = ENOEXEC;
  159. for (i = 0; i < nexecs && error != 0; i++) {
  160. int newerror;
  161. if (execsw[i].es_check == NULL)
  162. continue;
  163. newerror = (*execsw[i].es_check)(p, epp);
  164. if (!newerror && !(epp->ep_emul->e_flags & EMUL_ENABLED))
  165. newerror = EPERM;
  166. /* make sure the first "interesting" error code is saved. */
  167. if (!newerror || error == ENOEXEC)
  168. error = newerror;
  169. if (epp->ep_flags & EXEC_DESTR && error != 0)
  170. return (error);
  171. }
  172. if (!error) {
  173. /* check that entry point is sane */
  174. if (epp->ep_entry > VM_MAXUSER_ADDRESS) {
  175. error = ENOEXEC;
  176. }
  177. /* check limits */
  178. if ((epp->ep_tsize > MAXTSIZ) ||
  179. (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
  180. error = ENOMEM;
  181. if (!error)
  182. return (0);
  183. }
  184. /*
  185. * free any vmspace-creation commands,
  186. * and release their references
  187. */
  188. kill_vmcmds(&epp->ep_vmcmds);
  189. bad2:
  190. /*
  191. * close the vnode, free the pathname buf, and punt.
  192. */
  193. vn_close(vp, FREAD, p->p_ucred, p);
  194. pool_put(&namei_pool, ndp->ni_cnd.cn_pnbuf);
  195. return (error);
  196. bad1:
  197. /*
  198. * free the namei pathname buffer, and put the vnode
  199. * (which we don't yet have open).
  200. */
  201. pool_put(&namei_pool, ndp->ni_cnd.cn_pnbuf);
  202. vput(vp);
  203. return (error);
  204. }
  205. /*
  206. * exec system call
  207. */
  208. /* ARGSUSED */
  209. int
  210. sys_execve(struct proc *p, void *v, register_t *retval)
  211. {
  212. struct sys_execve_args /* {
  213. syscallarg(const char *) path;
  214. syscallarg(char *const *) argp;
  215. syscallarg(char *const *) envp;
  216. } */ *uap = v;
  217. int error;
  218. struct exec_package pack;
  219. struct nameidata nid;
  220. struct vattr attr;
  221. struct ucred *cred = p->p_ucred;
  222. char *argp;
  223. char * const *cpp, *dp, *sp;
  224. struct process *pr = p->p_p;
  225. long argc, envc;
  226. size_t len, sgap;
  227. #ifdef MACHINE_STACK_GROWS_UP
  228. size_t slen;
  229. #endif
  230. char *stack;
  231. struct ps_strings arginfo;
  232. struct vmspace *vm = pr->ps_vmspace;
  233. char **tmpfap;
  234. extern struct emul emul_native;
  235. #if NSYSTRACE > 0
  236. int wassugid = ISSET(pr->ps_flags, PS_SUGID | PS_SUGIDEXEC);
  237. size_t pathbuflen;
  238. #endif
  239. char *pathbuf = NULL;
  240. struct vnode *otvp;
  241. /* get other threads to stop */
  242. if ((error = single_thread_set(p, SINGLE_UNWIND, 1)))
  243. return (error);
  244. /*
  245. * Cheap solution to complicated problems.
  246. * Mark this process as "leave me alone, I'm execing".
  247. */
  248. atomic_setbits_int(&pr->ps_flags, PS_INEXEC);
  249. #if NSYSTRACE > 0
  250. if (ISSET(p->p_flag, P_SYSTRACE)) {
  251. systrace_execve0(p);
  252. pathbuf = pool_get(&namei_pool, PR_WAITOK);
  253. error = copyinstr(SCARG(uap, path), pathbuf, MAXPATHLEN,
  254. &pathbuflen);
  255. if (error != 0)
  256. goto clrflag;
  257. }
  258. #endif
  259. if (pathbuf != NULL) {
  260. NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, p);
  261. } else {
  262. NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE,
  263. SCARG(uap, path), p);
  264. }
  265. /*
  266. * initialize the fields of the exec package.
  267. */
  268. if (pathbuf != NULL)
  269. pack.ep_name = pathbuf;
  270. else
  271. pack.ep_name = (char *)SCARG(uap, path);
  272. pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
  273. pack.ep_hdrlen = exec_maxhdrsz;
  274. pack.ep_hdrvalid = 0;
  275. pack.ep_ndp = &nid;
  276. pack.ep_interp = NULL;
  277. pack.ep_emul_arg = NULL;
  278. VMCMDSET_INIT(&pack.ep_vmcmds);
  279. pack.ep_vap = &attr;
  280. pack.ep_emul = &emul_native;
  281. pack.ep_flags = 0;
  282. /* see if we can run it. */
  283. if ((error = check_exec(p, &pack)) != 0) {
  284. goto freehdr;
  285. }
  286. /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
  287. /* allocate an argument buffer */
  288. argp = km_alloc(NCARGS, &kv_exec, &kp_pageable, &kd_waitok);
  289. #ifdef DIAGNOSTIC
  290. if (argp == NULL)
  291. panic("execve: argp == NULL");
  292. #endif
  293. dp = argp;
  294. argc = 0;
  295. /* copy the fake args list, if there's one, freeing it as we go */
  296. if (pack.ep_flags & EXEC_HASARGL) {
  297. tmpfap = pack.ep_fa;
  298. while (*tmpfap != NULL) {
  299. char *cp;
  300. cp = *tmpfap;
  301. while (*cp)
  302. *dp++ = *cp++;
  303. *dp++ = '\0';
  304. free(*tmpfap, M_EXEC, 0);
  305. tmpfap++; argc++;
  306. }
  307. free(pack.ep_fa, M_EXEC, 0);
  308. pack.ep_flags &= ~EXEC_HASARGL;
  309. }
  310. /* Now get argv & environment */
  311. if (!(cpp = SCARG(uap, argp))) {
  312. error = EFAULT;
  313. goto bad;
  314. }
  315. if (pack.ep_flags & EXEC_SKIPARG)
  316. cpp++;
  317. while (1) {
  318. len = argp + ARG_MAX - dp;
  319. if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
  320. goto bad;
  321. if (!sp)
  322. break;
  323. if ((error = copyinstr(sp, dp, len, &len)) != 0) {
  324. if (error == ENAMETOOLONG)
  325. error = E2BIG;
  326. goto bad;
  327. }
  328. dp += len;
  329. cpp++;
  330. argc++;
  331. }
  332. /* must have at least one argument */
  333. if (argc == 0) {
  334. error = EINVAL;
  335. goto bad;
  336. }
  337. envc = 0;
  338. /* environment does not need to be there */
  339. if ((cpp = SCARG(uap, envp)) != NULL ) {
  340. while (1) {
  341. len = argp + ARG_MAX - dp;
  342. if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
  343. goto bad;
  344. if (!sp)
  345. break;
  346. if ((error = copyinstr(sp, dp, len, &len)) != 0) {
  347. if (error == ENAMETOOLONG)
  348. error = E2BIG;
  349. goto bad;
  350. }
  351. dp += len;
  352. cpp++;
  353. envc++;
  354. }
  355. }
  356. dp = (char *)(((long)dp + _STACKALIGNBYTES) & ~_STACKALIGNBYTES);
  357. sgap = STACKGAPLEN;
  358. /*
  359. * If we have enabled random stackgap, the stack itself has already
  360. * been moved from a random location, but is still aligned to a page
  361. * boundary. Provide the lower bits of random placement now.
  362. */
  363. if (stackgap_random != 0) {
  364. sgap += arc4random() & PAGE_MASK;
  365. sgap = (sgap + _STACKALIGNBYTES) & ~_STACKALIGNBYTES;
  366. }
  367. /* Now check if args & environ fit into new stack */
  368. len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) +
  369. sizeof(long) + dp + sgap + sizeof(struct ps_strings)) - argp;
  370. len = (len + _STACKALIGNBYTES) &~ _STACKALIGNBYTES;
  371. if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
  372. error = ENOMEM;
  373. goto bad;
  374. }
  375. /* adjust "active stack depth" for process VSZ */
  376. pack.ep_ssize = len; /* maybe should go elsewhere, but... */
  377. /*
  378. * we're committed: any further errors will kill the process, so
  379. * kill the other threads now.
  380. */
  381. single_thread_set(p, SINGLE_EXIT, 0);
  382. /*
  383. * Prepare vmspace for remapping. Note that uvmspace_exec can replace
  384. * pr_vmspace!
  385. */
  386. uvmspace_exec(p, VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS);
  387. vm = pr->ps_vmspace;
  388. /* Now map address space */
  389. vm->vm_taddr = (char *)trunc_page(pack.ep_taddr);
  390. vm->vm_tsize = atop(round_page(pack.ep_taddr + pack.ep_tsize) -
  391. trunc_page(pack.ep_taddr));
  392. vm->vm_daddr = (char *)trunc_page(pack.ep_daddr);
  393. vm->vm_dsize = atop(round_page(pack.ep_daddr + pack.ep_dsize) -
  394. trunc_page(pack.ep_daddr));
  395. vm->vm_dused = 0;
  396. vm->vm_ssize = atop(round_page(pack.ep_ssize));
  397. vm->vm_maxsaddr = (char *)pack.ep_maxsaddr;
  398. vm->vm_minsaddr = (char *)pack.ep_minsaddr;
  399. /* create the new process's VM space by running the vmcmds */
  400. #ifdef DIAGNOSTIC
  401. if (pack.ep_vmcmds.evs_used == 0)
  402. panic("execve: no vmcmds");
  403. #endif
  404. error = exec_process_vmcmds(p, &pack);
  405. /* if an error happened, deallocate and punt */
  406. if (error)
  407. goto exec_abort;
  408. /* old "stackgap" is gone now */
  409. pr->ps_stackgap = 0;
  410. #ifdef MACHINE_STACK_GROWS_UP
  411. pr->ps_strings = (vaddr_t)vm->vm_maxsaddr + sgap;
  412. if (uvm_map_protect(&vm->vm_map, (vaddr_t)vm->vm_maxsaddr,
  413. trunc_page(pr->ps_strings), PROT_NONE, TRUE))
  414. goto exec_abort;
  415. #else
  416. pr->ps_strings = (vaddr_t)vm->vm_minsaddr - sizeof(arginfo) - sgap;
  417. if (uvm_map_protect(&vm->vm_map,
  418. round_page(pr->ps_strings + sizeof(arginfo)),
  419. (vaddr_t)vm->vm_minsaddr, PROT_NONE, TRUE))
  420. goto exec_abort;
  421. #endif
  422. /* remember information about the process */
  423. arginfo.ps_nargvstr = argc;
  424. arginfo.ps_nenvstr = envc;
  425. #ifdef MACHINE_STACK_GROWS_UP
  426. stack = (char *)vm->vm_maxsaddr + sizeof(arginfo) + sgap;
  427. slen = len - sizeof(arginfo) - sgap;
  428. #else
  429. stack = (char *)(vm->vm_minsaddr - len);
  430. #endif
  431. /* Now copy argc, args & environ to new stack */
  432. if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp))
  433. goto exec_abort;
  434. /* copy out the process's ps_strings structure */
  435. if (copyout(&arginfo, (char *)pr->ps_strings, sizeof(arginfo)))
  436. goto exec_abort;
  437. stopprofclock(pr); /* stop profiling */
  438. fdcloseexec(p); /* handle close on exec */
  439. execsigs(p); /* reset caught signals */
  440. TCB_SET(p, NULL); /* reset the TCB address */
  441. pr->ps_kbind_addr = 0; /* reset the kbind bits */
  442. pr->ps_kbind_cookie = 0;
  443. /* set command name & other accounting info */
  444. memset(p->p_comm, 0, sizeof(p->p_comm));
  445. len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
  446. memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
  447. pr->ps_acflag &= ~AFORK;
  448. /* record proc's vnode, for use by sysctl */
  449. otvp = pr->ps_textvp;
  450. vref(pack.ep_vp);
  451. pr->ps_textvp = pack.ep_vp;
  452. if (otvp)
  453. vrele(otvp);
  454. atomic_setbits_int(&pr->ps_flags, PS_EXEC);
  455. if (pr->ps_flags & PS_PPWAIT) {
  456. atomic_clearbits_int(&pr->ps_flags, PS_PPWAIT);
  457. atomic_clearbits_int(&pr->ps_pptr->ps_flags, PS_ISPWAIT);
  458. wakeup(pr->ps_pptr);
  459. }
  460. /*
  461. * If process does execve() while it has a mismatched real,
  462. * effective, or saved uid/gid, we set PS_SUGIDEXEC.
  463. */
  464. if (cred->cr_uid != cred->cr_ruid ||
  465. cred->cr_uid != cred->cr_svuid ||
  466. cred->cr_gid != cred->cr_rgid ||
  467. cred->cr_gid != cred->cr_svgid)
  468. atomic_setbits_int(&pr->ps_flags, PS_SUGIDEXEC);
  469. else
  470. atomic_clearbits_int(&pr->ps_flags, PS_SUGIDEXEC);
  471. /*
  472. * deal with set[ug]id.
  473. * MNT_NOEXEC has already been used to disable s[ug]id.
  474. */
  475. if ((attr.va_mode & (VSUID | VSGID)) && proc_cansugid(p)) {
  476. int i;
  477. atomic_setbits_int(&pr->ps_flags, PS_SUGID|PS_SUGIDEXEC);
  478. #ifdef KTRACE
  479. /*
  480. * If process is being ktraced, turn off - unless
  481. * root set it.
  482. */
  483. if (pr->ps_tracevp && !(pr->ps_traceflag & KTRFAC_ROOT))
  484. ktrcleartrace(pr);
  485. #endif
  486. p->p_ucred = cred = crcopy(cred);
  487. if (attr.va_mode & VSUID)
  488. cred->cr_uid = attr.va_uid;
  489. if (attr.va_mode & VSGID)
  490. cred->cr_gid = attr.va_gid;
  491. /*
  492. * For set[ug]id processes, a few caveats apply to
  493. * stdin, stdout, and stderr.
  494. */
  495. error = 0;
  496. fdplock(p->p_fd);
  497. for (i = 0; i < 3; i++) {
  498. struct file *fp = NULL;
  499. /*
  500. * NOTE - This will never return NULL because of
  501. * immature fds. The file descriptor table is not
  502. * shared because we're suid.
  503. */
  504. fp = fd_getfile(p->p_fd, i);
  505. /*
  506. * Ensure that stdin, stdout, and stderr are already
  507. * allocated. We do not want userland to accidentally
  508. * allocate descriptors in this range which has implied
  509. * meaning to libc.
  510. */
  511. if (fp == NULL) {
  512. short flags = FREAD | (i == 0 ? 0 : FWRITE);
  513. struct vnode *vp;
  514. int indx;
  515. if ((error = falloc(p, &fp, &indx)) != 0)
  516. break;
  517. #ifdef DIAGNOSTIC
  518. if (indx != i)
  519. panic("sys_execve: falloc indx != i");
  520. #endif
  521. if ((error = cdevvp(getnulldev(), &vp)) != 0) {
  522. fdremove(p->p_fd, indx);
  523. closef(fp, p);
  524. break;
  525. }
  526. if ((error = VOP_OPEN(vp, flags, cred, p)) != 0) {
  527. fdremove(p->p_fd, indx);
  528. closef(fp, p);
  529. vrele(vp);
  530. break;
  531. }
  532. if (flags & FWRITE)
  533. vp->v_writecount++;
  534. fp->f_flag = flags;
  535. fp->f_type = DTYPE_VNODE;
  536. fp->f_ops = &vnops;
  537. fp->f_data = (caddr_t)vp;
  538. FILE_SET_MATURE(fp, p);
  539. }
  540. }
  541. fdpunlock(p->p_fd);
  542. if (error)
  543. goto exec_abort;
  544. } else
  545. atomic_clearbits_int(&pr->ps_flags, PS_SUGID);
  546. /*
  547. * Reset the saved ugids and update the process's copy of the
  548. * creds if the creds have been changed
  549. */
  550. if (cred->cr_uid != cred->cr_svuid ||
  551. cred->cr_gid != cred->cr_svgid) {
  552. /* make sure we have unshared ucreds */
  553. p->p_ucred = cred = crcopy(cred);
  554. cred->cr_svuid = cred->cr_uid;
  555. cred->cr_svgid = cred->cr_gid;
  556. }
  557. if (pr->ps_ucred != cred) {
  558. struct ucred *ocred;
  559. ocred = pr->ps_ucred;
  560. crhold(cred);
  561. pr->ps_ucred = cred;
  562. crfree(ocred);
  563. }
  564. if (pr->ps_flags & PS_SUGIDEXEC) {
  565. int i, s = splclock();
  566. timeout_del(&pr->ps_realit_to);
  567. for (i = 0; i < nitems(pr->ps_timer); i++) {
  568. timerclear(&pr->ps_timer[i].it_interval);
  569. timerclear(&pr->ps_timer[i].it_value);
  570. }
  571. splx(s);
  572. }
  573. /* reset CPU time usage for the thread, but not the process */
  574. timespecclear(&p->p_tu.tu_runtime);
  575. p->p_tu.tu_uticks = p->p_tu.tu_sticks = p->p_tu.tu_iticks = 0;
  576. km_free(argp, NCARGS, &kv_exec, &kp_pageable);
  577. pool_put(&namei_pool, nid.ni_cnd.cn_pnbuf);
  578. vn_close(pack.ep_vp, FREAD, cred, p);
  579. /*
  580. * notify others that we exec'd
  581. */
  582. KNOTE(&pr->ps_klist, NOTE_EXEC);
  583. /* setup new registers and do misc. setup. */
  584. if (pack.ep_emul->e_fixup != NULL) {
  585. if ((*pack.ep_emul->e_fixup)(p, &pack) != 0)
  586. goto free_pack_abort;
  587. }
  588. #ifdef MACHINE_STACK_GROWS_UP
  589. (*pack.ep_emul->e_setregs)(p, &pack, (u_long)stack + slen, retval);
  590. #else
  591. (*pack.ep_emul->e_setregs)(p, &pack, (u_long)stack, retval);
  592. #endif
  593. /* map the process's signal trampoline code */
  594. if (exec_sigcode_map(pr, pack.ep_emul))
  595. goto free_pack_abort;
  596. #ifdef __HAVE_EXEC_MD_MAP
  597. /* perform md specific mappings that process might need */
  598. if (exec_md_map(p, &pack))
  599. goto free_pack_abort;
  600. #endif
  601. if (pr->ps_flags & PS_TRACED)
  602. psignal(p, SIGTRAP);
  603. free(pack.ep_hdr, M_EXEC, 0);
  604. /*
  605. * Call emulation specific exec hook. This can setup per-process
  606. * p->p_emuldata or do any other per-process stuff an emulation needs.
  607. *
  608. * If we are executing process of different emulation than the
  609. * original forked process, call e_proc_exit() of the old emulation
  610. * first, then e_proc_exec() of new emulation. If the emulation is
  611. * same, the exec hook code should deallocate any old emulation
  612. * resources held previously by this process.
  613. */
  614. if (pr->ps_emul && pr->ps_emul->e_proc_exit &&
  615. pr->ps_emul != pack.ep_emul)
  616. (*pr->ps_emul->e_proc_exit)(p);
  617. p->p_descfd = 255;
  618. if ((pack.ep_flags & EXEC_HASFD) && pack.ep_fd < 255)
  619. p->p_descfd = pack.ep_fd;
  620. /*
  621. * Call exec hook. Emulation code may NOT store reference to anything
  622. * from &pack.
  623. */
  624. if (pack.ep_emul->e_proc_exec)
  625. (*pack.ep_emul->e_proc_exec)(p, &pack);
  626. /* update ps_emul, the old value is no longer needed */
  627. pr->ps_emul = pack.ep_emul;
  628. #ifdef KTRACE
  629. if (KTRPOINT(p, KTR_EMUL))
  630. ktremul(p);
  631. #endif
  632. atomic_clearbits_int(&pr->ps_flags, PS_INEXEC);
  633. single_thread_clear(p, P_SUSPSIG);
  634. #if NSYSTRACE > 0
  635. if (ISSET(p->p_flag, P_SYSTRACE) &&
  636. wassugid && !ISSET(pr->ps_flags, PS_SUGID | PS_SUGIDEXEC))
  637. systrace_execve1(pathbuf, p);
  638. #endif
  639. if (pathbuf != NULL)
  640. pool_put(&namei_pool, pathbuf);
  641. return (0);
  642. bad:
  643. /* free the vmspace-creation commands, and release their references */
  644. kill_vmcmds(&pack.ep_vmcmds);
  645. /* kill any opened file descriptor, if necessary */
  646. if (pack.ep_flags & EXEC_HASFD) {
  647. pack.ep_flags &= ~EXEC_HASFD;
  648. fdplock(p->p_fd);
  649. (void) fdrelease(p, pack.ep_fd);
  650. fdpunlock(p->p_fd);
  651. }
  652. if (pack.ep_interp != NULL)
  653. pool_put(&namei_pool, pack.ep_interp);
  654. if (pack.ep_emul_arg != NULL)
  655. free(pack.ep_emul_arg, M_TEMP, 0);
  656. /* close and put the exec'd file */
  657. vn_close(pack.ep_vp, FREAD, cred, p);
  658. pool_put(&namei_pool, nid.ni_cnd.cn_pnbuf);
  659. km_free(argp, NCARGS, &kv_exec, &kp_pageable);
  660. freehdr:
  661. free(pack.ep_hdr, M_EXEC, 0);
  662. #if NSYSTRACE > 0
  663. clrflag:
  664. #endif
  665. atomic_clearbits_int(&pr->ps_flags, PS_INEXEC);
  666. single_thread_clear(p, P_SUSPSIG);
  667. if (pathbuf != NULL)
  668. pool_put(&namei_pool, pathbuf);
  669. return (error);
  670. exec_abort:
  671. /*
  672. * the old process doesn't exist anymore. exit gracefully.
  673. * get rid of the (new) address space we have created, if any, get rid
  674. * of our namei data and vnode, and exit noting failure
  675. */
  676. uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
  677. VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
  678. if (pack.ep_interp != NULL)
  679. pool_put(&namei_pool, pack.ep_interp);
  680. if (pack.ep_emul_arg != NULL)
  681. free(pack.ep_emul_arg, M_TEMP, 0);
  682. pool_put(&namei_pool, nid.ni_cnd.cn_pnbuf);
  683. vn_close(pack.ep_vp, FREAD, cred, p);
  684. km_free(argp, NCARGS, &kv_exec, &kp_pageable);
  685. free_pack_abort:
  686. free(pack.ep_hdr, M_EXEC, 0);
  687. if (pathbuf != NULL)
  688. pool_put(&namei_pool, pathbuf);
  689. exit1(p, W_EXITCODE(0, SIGABRT), EXIT_NORMAL);
  690. /* NOTREACHED */
  691. atomic_clearbits_int(&pr->ps_flags, PS_INEXEC);
  692. return (0);
  693. }
  694. void *
  695. copyargs(struct exec_package *pack, struct ps_strings *arginfo, void *stack,
  696. void *argp)
  697. {
  698. char **cpp = stack;
  699. char *dp, *sp;
  700. size_t len;
  701. void *nullp = NULL;
  702. long argc = arginfo->ps_nargvstr;
  703. int envc = arginfo->ps_nenvstr;
  704. if (copyout(&argc, cpp++, sizeof(argc)))
  705. return (NULL);
  706. dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
  707. sp = argp;
  708. /* XXX don't copy them out, remap them! */
  709. arginfo->ps_argvstr = cpp; /* remember location of argv for later */
  710. for (; --argc >= 0; sp += len, dp += len)
  711. if (copyout(&dp, cpp++, sizeof(dp)) ||
  712. copyoutstr(sp, dp, ARG_MAX, &len))
  713. return (NULL);
  714. if (copyout(&nullp, cpp++, sizeof(nullp)))
  715. return (NULL);
  716. arginfo->ps_envstr = cpp; /* remember location of envp for later */
  717. for (; --envc >= 0; sp += len, dp += len)
  718. if (copyout(&dp, cpp++, sizeof(dp)) ||
  719. copyoutstr(sp, dp, ARG_MAX, &len))
  720. return (NULL);
  721. if (copyout(&nullp, cpp++, sizeof(nullp)))
  722. return (NULL);
  723. return (cpp);
  724. }
  725. int
  726. exec_sigcode_map(struct process *pr, struct emul *e)
  727. {
  728. vsize_t sz;
  729. sz = (vaddr_t)e->e_esigcode - (vaddr_t)e->e_sigcode;
  730. /*
  731. * If we don't have a sigobject for this emulation, create one.
  732. *
  733. * sigobject is an anonymous memory object (just like SYSV shared
  734. * memory) that we keep a permanent reference to and that we map
  735. * in all processes that need this sigcode. The creation is simple,
  736. * we create an object, add a permanent reference to it, map it in
  737. * kernel space, copy out the sigcode to it and unmap it.
  738. * Then we map it with PROT_READ|PROT_EXEC into the process just
  739. * the way sys_mmap would map it.
  740. */
  741. if (e->e_sigobject == NULL) {
  742. vaddr_t va;
  743. int r;
  744. e->e_sigobject = uao_create(sz, 0);
  745. uao_reference(e->e_sigobject); /* permanent reference */
  746. if ((r = uvm_map(kernel_map, &va, round_page(sz), e->e_sigobject,
  747. 0, 0, UVM_MAPFLAG(PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
  748. MAP_INHERIT_SHARE, MADV_RANDOM, 0)))) {
  749. uao_detach(e->e_sigobject);
  750. return (ENOMEM);
  751. }
  752. memcpy((void *)va, e->e_sigcode, sz);
  753. uvm_unmap(kernel_map, va, va + round_page(sz));
  754. }
  755. pr->ps_sigcode = 0; /* no hint */
  756. uao_reference(e->e_sigobject);
  757. if (uvm_map(&pr->ps_vmspace->vm_map, &pr->ps_sigcode, round_page(sz),
  758. e->e_sigobject, 0, 0, UVM_MAPFLAG(PROT_READ | PROT_EXEC,
  759. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_INHERIT_COPY,
  760. MADV_RANDOM, UVM_FLAG_COPYONW))) {
  761. uao_detach(e->e_sigobject);
  762. return (ENOMEM);
  763. }
  764. return (0);
  765. }