exec_subr.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* $OpenBSD: exec_subr.c,v 1.50 2015/03/14 03:38:50 jsg Exp $ */
  2. /* $NetBSD: exec_subr.c,v 1.9 1994/12/04 03:10:42 mycroft Exp $ */
  3. /*
  4. * Copyright (c) 1993, 1994 Christopher G. Demetriou
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. All advertising materials mentioning features or use of this software
  16. * must display the following acknowledgement:
  17. * This product includes software developed by Christopher G. Demetriou.
  18. * 4. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <sys/param.h>
  33. #include <sys/systm.h>
  34. #include <sys/proc.h>
  35. #include <sys/malloc.h>
  36. #include <sys/vnode.h>
  37. #include <sys/exec.h>
  38. #include <sys/mman.h>
  39. #include <sys/resourcevar.h>
  40. #include <uvm/uvm_extern.h>
  41. #ifdef DEBUG
  42. /*
  43. * new_vmcmd():
  44. * create a new vmcmd structure and fill in its fields based
  45. * on function call arguments. make sure objects ref'd by
  46. * the vmcmd are 'held'.
  47. *
  48. * If not debugging, this is a macro, so it's expanded inline.
  49. */
  50. void
  51. new_vmcmd(struct exec_vmcmd_set *evsp,
  52. int (*proc)(struct proc *, struct exec_vmcmd *), u_long len, u_long addr,
  53. struct vnode *vp, u_long offset, u_int prot, int flags)
  54. {
  55. struct exec_vmcmd *vcp;
  56. if (evsp->evs_used >= evsp->evs_cnt)
  57. vmcmdset_extend(evsp);
  58. vcp = &evsp->evs_cmds[evsp->evs_used++];
  59. vcp->ev_proc = proc;
  60. vcp->ev_len = len;
  61. vcp->ev_addr = addr;
  62. if ((vcp->ev_vp = vp) != NULL)
  63. vref(vp);
  64. vcp->ev_offset = offset;
  65. vcp->ev_prot = prot;
  66. vcp->ev_flags = flags;
  67. }
  68. #endif /* DEBUG */
  69. void
  70. vmcmdset_extend(struct exec_vmcmd_set *evsp)
  71. {
  72. struct exec_vmcmd *nvcp;
  73. u_int ocnt;
  74. #ifdef DIAGNOSTIC
  75. if (evsp->evs_used < evsp->evs_cnt)
  76. panic("vmcmdset_extend: not necessary");
  77. #endif
  78. ocnt = evsp->evs_cnt;
  79. KASSERT(ocnt > 0);
  80. /* figure out number of entries in new set */
  81. evsp->evs_cnt += ocnt;
  82. /* reallocate the command set */
  83. nvcp = mallocarray(evsp->evs_cnt, sizeof(*nvcp), M_EXEC,
  84. M_WAITOK);
  85. memcpy(nvcp, evsp->evs_cmds, ocnt * sizeof(*nvcp));
  86. if (evsp->evs_cmds != evsp->evs_start)
  87. free(evsp->evs_cmds, M_EXEC, ocnt * sizeof(*nvcp));
  88. evsp->evs_cmds = nvcp;
  89. }
  90. void
  91. kill_vmcmds(struct exec_vmcmd_set *evsp)
  92. {
  93. struct exec_vmcmd *vcp;
  94. int i;
  95. for (i = 0; i < evsp->evs_used; i++) {
  96. vcp = &evsp->evs_cmds[i];
  97. if (vcp->ev_vp != NULLVP)
  98. vrele(vcp->ev_vp);
  99. }
  100. /*
  101. * Free old vmcmds and reset the array.
  102. */
  103. evsp->evs_used = 0;
  104. if (evsp->evs_cmds != evsp->evs_start)
  105. free(evsp->evs_cmds, M_EXEC, 0);
  106. evsp->evs_cmds = evsp->evs_start;
  107. evsp->evs_cnt = EXEC_DEFAULT_VMCMD_SETSIZE;
  108. }
  109. int
  110. exec_process_vmcmds(struct proc *p, struct exec_package *epp)
  111. {
  112. struct exec_vmcmd *base_vc = NULL;
  113. int error = 0;
  114. int i;
  115. for (i = 0; i < epp->ep_vmcmds.evs_used && !error; i++) {
  116. struct exec_vmcmd *vcp;
  117. vcp = &epp->ep_vmcmds.evs_cmds[i];
  118. if (vcp->ev_flags & VMCMD_RELATIVE) {
  119. #ifdef DIAGNOSTIC
  120. if (base_vc == NULL)
  121. panic("exec_process_vmcmds: RELATIVE no base");
  122. #endif
  123. vcp->ev_addr += base_vc->ev_addr;
  124. }
  125. error = (*vcp->ev_proc)(p, vcp);
  126. if (vcp->ev_flags & VMCMD_BASE) {
  127. base_vc = vcp;
  128. }
  129. }
  130. kill_vmcmds(&epp->ep_vmcmds);
  131. return (error);
  132. }
  133. /*
  134. * vmcmd_map_pagedvn():
  135. * handle vmcmd which specifies that a vnode should be mmap'd.
  136. * appropriate for handling demand-paged text and data segments.
  137. */
  138. int
  139. vmcmd_map_pagedvn(struct proc *p, struct exec_vmcmd *cmd)
  140. {
  141. /*
  142. * note that if you're going to map part of a process as being
  143. * paged from a vnode, that vnode had damn well better be marked as
  144. * VTEXT. that's handled in the routine which sets up the vmcmd to
  145. * call this routine.
  146. */
  147. struct uvm_object *uobj;
  148. int error;
  149. /*
  150. * map the vnode in using uvm_map.
  151. */
  152. if (cmd->ev_len == 0)
  153. return (0);
  154. if (cmd->ev_offset & PAGE_MASK)
  155. return (EINVAL);
  156. if (cmd->ev_addr & PAGE_MASK)
  157. return (EINVAL);
  158. if (cmd->ev_len & PAGE_MASK)
  159. return (EINVAL);
  160. /*
  161. * first, attach to the object
  162. */
  163. uobj = uvn_attach(cmd->ev_vp, PROT_READ | PROT_EXEC);
  164. if (uobj == NULL)
  165. return (ENOMEM);
  166. /*
  167. * do the map
  168. */
  169. error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
  170. uobj, cmd->ev_offset, 0,
  171. UVM_MAPFLAG(cmd->ev_prot, PROT_MASK, MAP_INHERIT_COPY,
  172. MADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
  173. /*
  174. * check for error
  175. */
  176. if (error) {
  177. /*
  178. * error: detach from object
  179. */
  180. uobj->pgops->pgo_detach(uobj);
  181. }
  182. return (error);
  183. }
  184. /*
  185. * vmcmd_map_readvn():
  186. * handle vmcmd which specifies that a vnode should be read from.
  187. * appropriate for non-demand-paged text/data segments, i.e. impure
  188. * objects (a la OMAGIC and NMAGIC).
  189. */
  190. int
  191. vmcmd_map_readvn(struct proc *p, struct exec_vmcmd *cmd)
  192. {
  193. int error;
  194. vm_prot_t prot;
  195. if (cmd->ev_len == 0)
  196. return (0);
  197. prot = cmd->ev_prot;
  198. cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */
  199. error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
  200. round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
  201. UVM_MAPFLAG(prot | PROT_WRITE, PROT_MASK, MAP_INHERIT_COPY,
  202. MADV_NORMAL, UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
  203. if (error)
  204. return (error);
  205. error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
  206. cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT,
  207. p->p_ucred, NULL, p);
  208. if (error)
  209. return (error);
  210. if ((prot & PROT_WRITE) == 0) {
  211. /*
  212. * we had to map in the area at PROT_WRITE so that vn_rdwr()
  213. * could write to it. however, the caller seems to want
  214. * it mapped read-only, so now we are going to have to call
  215. * uvm_map_protect() to fix up the protection. ICK.
  216. */
  217. return (uvm_map_protect(&p->p_vmspace->vm_map,
  218. trunc_page(cmd->ev_addr),
  219. round_page(cmd->ev_addr + cmd->ev_len),
  220. prot, FALSE));
  221. }
  222. return (0);
  223. }
  224. /*
  225. * vmcmd_map_zero():
  226. * handle vmcmd which specifies a zero-filled address space region.
  227. */
  228. int
  229. vmcmd_map_zero(struct proc *p, struct exec_vmcmd *cmd)
  230. {
  231. if (cmd->ev_len == 0)
  232. return (0);
  233. cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */
  234. return (uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
  235. round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
  236. UVM_MAPFLAG(cmd->ev_prot, PROT_MASK, MAP_INHERIT_COPY,
  237. MADV_NORMAL, UVM_FLAG_FIXED|UVM_FLAG_COPYONW)));
  238. }
  239. /*
  240. * vmcmd_randomize():
  241. * handle vmcmd which specifies a randomized address space region.
  242. */
  243. int
  244. vmcmd_randomize(struct proc *p, struct exec_vmcmd *cmd)
  245. {
  246. char *buf;
  247. int error;
  248. size_t off = 0, len;
  249. if (cmd->ev_len == 0)
  250. return (0);
  251. if (cmd->ev_len > ELF_RANDOMIZE_LIMIT)
  252. return (EINVAL);
  253. buf = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
  254. len = cmd->ev_len;
  255. do {
  256. size_t sublen = MIN(len, PAGE_SIZE);
  257. arc4random_buf(buf, sublen);
  258. error = copyout(buf, (void *)cmd->ev_addr + off, sublen);
  259. if (error)
  260. break;
  261. off += sublen;
  262. len -= sublen;
  263. if (len)
  264. yield();
  265. } while (len);
  266. free(buf, M_TEMP, PAGE_SIZE);
  267. return (error);
  268. }
  269. /*
  270. * exec_setup_stack(): Set up the stack segment for an executable.
  271. *
  272. * Note that the ep_ssize parameter must be set to be the current stack
  273. * limit; this is adjusted in the body of execve() to yield the
  274. * appropriate stack segment usage once the argument length is
  275. * calculated.
  276. *
  277. * This function returns an int for uniformity with other (future) formats'
  278. * stack setup functions. They might have errors to return.
  279. */
  280. int
  281. exec_setup_stack(struct proc *p, struct exec_package *epp)
  282. {
  283. vaddr_t sgap;
  284. #ifdef MACHINE_STACK_GROWS_UP
  285. epp->ep_maxsaddr = USRSTACK;
  286. epp->ep_minsaddr = USRSTACK + MAXSSIZ;
  287. #else
  288. epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
  289. epp->ep_minsaddr = USRSTACK;
  290. #endif
  291. epp->ep_ssize = round_page(p->p_rlimit[RLIMIT_STACK].rlim_cur);
  292. if (stackgap_random != 0) {
  293. sgap = arc4random() & (stackgap_random - 1);
  294. sgap = trunc_page(sgap);
  295. #ifdef MACHINE_STACK_GROWS_UP
  296. epp->ep_maxsaddr += sgap;
  297. epp->ep_minsaddr += sgap;
  298. #else
  299. epp->ep_maxsaddr -= sgap;
  300. epp->ep_minsaddr -= sgap;
  301. #endif
  302. }
  303. /*
  304. * set up commands for stack. note that this takes *two*, one to
  305. * map the part of the stack which we can access, and one to map
  306. * the part which we can't.
  307. *
  308. * arguably, it could be made into one, but that would require the
  309. * addition of another mapping proc, which is unnecessary
  310. *
  311. * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
  312. * <stack> ep_minsaddr
  313. */
  314. #ifdef MACHINE_STACK_GROWS_UP
  315. NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
  316. ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
  317. epp->ep_maxsaddr + epp->ep_ssize, NULLVP, 0, PROT_NONE);
  318. NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
  319. epp->ep_maxsaddr, NULLVP, 0,
  320. PROT_READ | PROT_WRITE);
  321. #else
  322. NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
  323. ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
  324. epp->ep_maxsaddr, NULLVP, 0, PROT_NONE);
  325. NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
  326. (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
  327. PROT_READ | PROT_WRITE);
  328. #endif
  329. return (0);
  330. }