netbsd32_execve.c revision 1.2.2.7 1 /* $NetBSD: netbsd32_execve.c,v 1.2.2.7 2002/06/20 03:43:08 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2001 Matthew R. Green
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. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_execve.c,v 1.2.2.7 2002/06/20 03:43:08 nathanw Exp $");
33
34 #if defined(_KERNEL_OPT)
35 #include "opt_ktrace.h"
36 #endif
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/stat.h>
43 #include <sys/wait.h>
44 #include <sys/ktrace.h>
45 #include <sys/vnode.h>
46 #include <sys/file.h>
47 #include <sys/filedesc.h>
48 #include <sys/namei.h>
49
50 #include <uvm/uvm_extern.h>
51
52 #include <sys/sa.h>
53 #include <sys/syscallargs.h>
54 #include <sys/proc.h>
55 #include <sys/acct.h>
56 #include <sys/exec.h>
57
58 #include <compat/netbsd32/netbsd32.h>
59 #include <compat/netbsd32/netbsd32_syscall.h>
60 #include <compat/netbsd32/netbsd32_syscallargs.h>
61
62 /* this is provided by kern/kern_exec.c */
63 extern int exec_maxhdrsz;
64 #if defined(LKM) || defined(_LKM)
65 extern struct lock exec_lock;
66 #endif
67
68 /*
69 * Need to completly reimplement this syscall due to argument copying.
70 */
71 /* ARGSUSED */
72 int
73 netbsd32_execve(p, v, retval)
74 struct proc *p;
75 void *v;
76 register_t *retval;
77 {
78 struct netbsd32_execve_args /* {
79 syscallarg(const netbsd32_charp) path;
80 syscallarg(netbsd32_charpp) argp;
81 syscallarg(netbsd32_charpp) envp;
82 } */ *uap = v;
83 struct sys_execve_args ua;
84 caddr_t sg;
85
86 NETBSD32TOP_UAP(path, const char);
87 NETBSD32TOP_UAP(argp, char *);
88 NETBSD32TOP_UAP(envp, char *);
89 sg = stackgap_init(p, 0);
90 CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
91
92 return netbsd32_execve2(p, &ua, retval);
93 }
94
95 int
96 netbsd32_execve2(p, uap, retval)
97 struct proc *p;
98 struct sys_execve_args *uap;
99 register_t *retval;
100 {
101 /* Function args */
102 int error, i;
103 struct exec_package pack;
104 struct nameidata nid;
105 struct vattr attr;
106 struct ucred *cred = p->p_ucred;
107 char *argp;
108 netbsd32_charp const *cpp;
109 char *dp;
110 netbsd32_charp sp;
111 long argc, envc;
112 size_t len;
113 char *stack;
114 struct ps_strings arginfo;
115 struct vmspace *vm;
116 char **tmpfap;
117 int szsigcode;
118 struct exec_vmcmd *base_vcp = NULL;
119
120 /*
121 * Init the namei data to point the file user's program name.
122 * This is done here rather than in check_exec(), so that it's
123 * possible to override this settings if any of makecmd/probe
124 * functions call check_exec() recursively - for example,
125 * see exec_script_makecmds().
126 */
127 NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
128
129 /*
130 * initialize the fields of the exec package.
131 */
132 pack.ep_name = SCARG(uap, path);
133 pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
134 pack.ep_hdrlen = exec_maxhdrsz;
135 pack.ep_hdrvalid = 0;
136 pack.ep_ndp = &nid;
137 pack.ep_emul_arg = NULL;
138 pack.ep_vmcmds.evs_cnt = 0;
139 pack.ep_vmcmds.evs_used = 0;
140 pack.ep_vap = &attr;
141 pack.ep_flags = 0;
142
143 #if defined(LKM) || defined(_LKM)
144 lockmgr(&exec_lock, LK_SHARED, NULL);
145 #endif
146
147 /* see if we can run it. */
148 if ((error = check_exec(p, &pack)) != 0)
149 goto freehdr;
150
151 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
152
153 /* allocate an argument buffer */
154 argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
155 #ifdef DIAGNOSTIC
156 if (argp == (vaddr_t) 0)
157 panic("netbsd32_execve: argp == NULL");
158 #endif
159 dp = argp;
160 argc = 0;
161
162 /* copy the fake args list, if there's one, freeing it as we go */
163 if (pack.ep_flags & EXEC_HASARGL) {
164 tmpfap = pack.ep_fa;
165 while (*tmpfap != NULL) {
166 char *cp;
167
168 cp = *tmpfap;
169 while (*cp)
170 *dp++ = *cp++;
171 dp++;
172
173 FREE(*tmpfap, M_EXEC);
174 tmpfap++; argc++;
175 }
176 FREE(pack.ep_fa, M_EXEC);
177 pack.ep_flags &= ~EXEC_HASARGL;
178 }
179
180 /* Now get argv & environment */
181 if (!(cpp = (netbsd32_charp *)SCARG(uap, argp))) {
182 error = EINVAL;
183 goto bad;
184 }
185
186 if (pack.ep_flags & EXEC_SKIPARG)
187 cpp++;
188
189 while (1) {
190 len = argp + ARG_MAX - dp;
191 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
192 goto bad;
193 if (!sp)
194 break;
195 if ((error = copyinstr((char *)(u_long)sp, dp,
196 len, &len)) != 0) {
197 if (error == ENAMETOOLONG)
198 error = E2BIG;
199 goto bad;
200 }
201 dp += len;
202 cpp++;
203 argc++;
204 }
205
206 envc = 0;
207 /* environment need not be there */
208 if ((cpp = (netbsd32_charp *)SCARG(uap, envp)) != NULL ) {
209 while (1) {
210 len = argp + ARG_MAX - dp;
211 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
212 goto bad;
213 if (!sp)
214 break;
215 if ((error = copyinstr((char *)(u_long)sp,
216 dp, len, &len)) != 0) {
217 if (error == ENAMETOOLONG)
218 error = E2BIG;
219 goto bad;
220 }
221 dp += len;
222 cpp++;
223 envc++;
224 }
225 }
226
227 dp = (char *) ALIGN(dp);
228
229 szsigcode = pack.ep_es->es_emul->e_esigcode -
230 pack.ep_es->es_emul->e_sigcode;
231
232 /* Now check if args & environ fit into new stack */
233 if (pack.ep_flags & EXEC_32)
234 len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
235 sizeof(int) + sizeof(int) + dp + STACKGAPLEN +
236 szsigcode + sizeof(struct ps_strings)) - argp;
237 else
238 len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
239 sizeof(char *) + sizeof(int) + dp + STACKGAPLEN +
240 szsigcode + sizeof(struct ps_strings)) - argp;
241
242 len = ALIGN(len); /* make the stack "safely" aligned */
243
244 if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
245 error = ENOMEM;
246 goto bad;
247 }
248
249 /* adjust "active stack depth" for process VSZ */
250 pack.ep_ssize = len; /* maybe should go elsewhere, but... */
251
252 /*
253 * Do whatever is necessary to prepare the address space
254 * for remapping. Note that this might replace the current
255 * vmspace with another!
256 */
257 uvmspace_exec(p, VM_MIN_ADDRESS, (vaddr_t)pack.ep_minsaddr);
258
259 /* Now map address space */
260 vm = p->p_vmspace;
261 vm->vm_taddr = (char *) pack.ep_taddr;
262 vm->vm_tsize = btoc(pack.ep_tsize);
263 vm->vm_daddr = (char *) pack.ep_daddr;
264 vm->vm_dsize = btoc(pack.ep_dsize);
265 vm->vm_ssize = btoc(pack.ep_ssize);
266 vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
267 vm->vm_minsaddr = (char *) pack.ep_minsaddr;
268
269 /* create the new process's VM space by running the vmcmds */
270 #ifdef DIAGNOSTIC
271 if (pack.ep_vmcmds.evs_used == 0)
272 panic("netbsd32_execve: no vmcmds");
273 #endif
274 for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
275 struct exec_vmcmd *vcp;
276
277 vcp = &pack.ep_vmcmds.evs_cmds[i];
278 if (vcp->ev_flags & VMCMD_RELATIVE) {
279 #ifdef DIAGNOSTIC
280 if (base_vcp == NULL)
281 panic("netbsd32_execve: relative vmcmd with no base");
282 if (vcp->ev_flags & VMCMD_BASE)
283 panic("netbsd32_execve: illegal base & relative vmcmd");
284 #endif
285 vcp->ev_addr += base_vcp->ev_addr;
286 }
287 error = (*vcp->ev_proc)(p, vcp);
288 #ifdef DEBUG
289 if (error) {
290 int j;
291
292 for (j = 0; j <= i; j++)
293 printf("vmcmd[%d] = %#lx/%#lx @ %#lx\n", j,
294 vcp[j-i].ev_addr, vcp[j-i].ev_len,
295 vcp[j-i].ev_offset);
296 }
297 #endif
298 if (vcp->ev_flags & VMCMD_BASE)
299 base_vcp = vcp;
300 }
301
302 /* free the vmspace-creation commands, and release their references */
303 kill_vmcmds(&pack.ep_vmcmds);
304
305 /* if an error happened, deallocate and punt */
306 if (error) {
307 #ifdef DEBUG
308 printf("netbsd32_execve: vmcmd %i failed: %d\n", i-1, error);
309 #endif
310 goto exec_abort;
311 }
312
313 /* remember information about the process */
314 arginfo.ps_nargvstr = argc;
315 arginfo.ps_nenvstr = envc;
316
317 stack = (char *) (vm->vm_minsaddr - len);
318 /* Now copy argc, args & environ to new stack */
319 error = (*pack.ep_es->es_copyargs)(&pack, &arginfo,
320 &stack, argp);
321 if (error) {
322 #ifdef DEBUG
323 printf("netbsd32_execve: copyargs failed\n");
324 #endif
325 goto exec_abort;
326 }
327 /* restore the stack back to its original point */
328 stack = (char *) (vm->vm_minsaddr - len);
329
330 /* fill process ps_strings info */
331 p->p_psstr = (struct ps_strings *)(vm->vm_minsaddr -
332 sizeof(struct ps_strings));
333 p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
334 p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
335 p->p_psenv = offsetof(struct ps_strings, ps_envstr);
336 p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
337
338 /* copy out the process's ps_strings structure */
339 if (copyout(&arginfo, (char *)p->p_psstr, sizeof(arginfo))) {
340 #ifdef DEBUG
341 printf("netbsd32_execve: ps_strings copyout failed\n");
342 #endif
343 goto exec_abort;
344 }
345
346 /* copy out the process's signal trapoline code */
347 if (szsigcode) {
348 if (copyout((char *)pack.ep_es->es_emul->e_sigcode,
349 p->p_sigctx.ps_sigcode = (char *)p->p_psstr - szsigcode,
350 szsigcode)) {
351 #ifdef DEBUG
352 printf("netbsd32_execve: sig trampoline copyout failed\n");
353 #endif
354 goto exec_abort;
355 }
356 #ifdef PMAP_NEED_PROCWR
357 /* This is code. Let the pmap do what is needed. */
358 pmap_procwr(p, (vaddr_t)p->p_sigacts->ps_sigcode, szsigcode);
359 #endif
360 }
361
362 stopprofclock(p); /* stop profiling */
363 fdcloseexec(p); /* handle close on exec */
364 execsigs(p); /* reset catched signals */
365 p->p_ctxlink = NULL; /* reset ucontext link */
366
367 /* set command name & other accounting info */
368 len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
369 memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
370 p->p_comm[len] = 0;
371 p->p_acflag &= ~AFORK;
372
373 /* record proc's vnode, for use by procfs and others */
374 if (p->p_textvp)
375 vrele(p->p_textvp);
376 VREF(pack.ep_vp);
377 p->p_textvp = pack.ep_vp;
378
379 p->p_flag |= P_EXEC;
380 if (p->p_flag & P_PPWAIT) {
381 p->p_flag &= ~P_PPWAIT;
382 wakeup((caddr_t) p->p_pptr);
383 }
384
385 /*
386 * deal with set[ug]id.
387 * MNT_NOSUID has already been used to disable s[ug]id.
388 */
389 if ((p->p_flag & P_TRACED) == 0 &&
390
391 (((attr.va_mode & S_ISUID) != 0 &&
392 p->p_ucred->cr_uid != attr.va_uid) ||
393
394 ((attr.va_mode & S_ISGID) != 0 &&
395 p->p_ucred->cr_gid != attr.va_gid))) {
396 /*
397 * Mark the process as SUGID before we do
398 * anything that might block.
399 */
400 p_sugid(p);
401
402 p->p_ucred = crcopy(cred);
403 #ifdef KTRACE
404 /*
405 * If process is being ktraced, turn off - unless
406 * root set it.
407 */
408 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
409 ktrderef(p);
410 #endif
411 if (attr.va_mode & S_ISUID)
412 p->p_ucred->cr_uid = attr.va_uid;
413 if (attr.va_mode & S_ISGID)
414 p->p_ucred->cr_gid = attr.va_gid;
415 } else
416 p->p_flag &= ~P_SUGID;
417 p->p_cred->p_svuid = p->p_ucred->cr_uid;
418 p->p_cred->p_svgid = p->p_ucred->cr_gid;
419
420 doexechooks(p);
421
422 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
423
424 PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
425 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
426 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
427 vput(pack.ep_vp);
428
429 /* setup new registers and do misc. setup. */
430 (*pack.ep_es->es_emul->e_setregs)(p, &pack, (u_long) stack);
431 if (pack.ep_es->es_setregs)
432 (*pack.ep_es->es_setregs)(p, &pack, (u_long) stack);
433
434 if (p->p_flag & P_TRACED)
435 psignal(p, SIGTRAP);
436
437 free(pack.ep_hdr, M_EXEC);
438
439 /*
440 * Call emulation specific exec hook. This can setup setup per-process
441 * p->p_emuldata or do any other per-process stuff an emulation needs.
442 *
443 * If we are executing process of different emulation than the
444 * original forked process, call e_proc_exit() of the old emulation
445 * first, then e_proc_exec() of new emulation. If the emulation is
446 * same, the exec hook code should deallocate any old emulation
447 * resources held previously by this process.
448 */
449 if (p->p_emul && p->p_emul->e_proc_exit
450 && p->p_emul != pack.ep_es->es_emul)
451 (*p->p_emul->e_proc_exit)(p);
452
453 /*
454 * Call exec hook. Emulation code may NOT store reference to anything
455 * from &pack.
456 */
457 if (pack.ep_es->es_emul->e_proc_exec)
458 (*pack.ep_es->es_emul->e_proc_exec)(p, &pack);
459
460 /* update p_emul, the old value is no longer needed */
461 p->p_emul = pack.ep_es->es_emul;
462
463 #ifdef KTRACE
464 if (KTRPOINT(p, KTR_EMUL))
465 ktremul(p);
466 #endif
467
468 #if defined(LKM) || defined(_LKM)
469 lockmgr(&exec_lock, LK_RELEASE, NULL);
470 #endif
471
472 return (EJUSTRETURN);
473
474 bad:
475 /* free the vmspace-creation commands, and release their references */
476 kill_vmcmds(&pack.ep_vmcmds);
477 /* kill any opened file descriptor, if necessary */
478 if (pack.ep_flags & EXEC_HASFD) {
479 pack.ep_flags &= ~EXEC_HASFD;
480 (void) fdrelease(p, pack.ep_fd);
481 }
482 /* close and put the exec'd file */
483 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
484 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
485 vput(pack.ep_vp);
486 PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
487 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
488
489 freehdr:
490 #if defined(LKM) || defined(_LKM)
491 lockmgr(&exec_lock, LK_RELEASE, NULL);
492 #endif
493
494 free(pack.ep_hdr, M_EXEC);
495 return error;
496
497 exec_abort:
498 #if defined(LKM) || defined(_LKM)
499 lockmgr(&exec_lock, LK_RELEASE, NULL);
500 #endif
501
502 /*
503 * the old process doesn't exist anymore. exit gracefully.
504 * get rid of the (new) address space we have created, if any, get rid
505 * of our namei data and vnode, and exit noting failure
506 */
507 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
508 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
509 if (pack.ep_emul_arg)
510 FREE(pack.ep_emul_arg, M_TEMP);
511 PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
512 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
513 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
514 vput(pack.ep_vp);
515 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
516 free(pack.ep_hdr, M_EXEC);
517 exit1(p, W_EXITCODE(error, SIGABRT));
518
519 /* NOTREACHED */
520 return 0;
521 }
522