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