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