kern_sharedpage.c revision 78371
1/* 2 * Copyright (c) 1993, David Greenman 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/kern/kern_exec.c 78371 2001-06-16 23:34:23Z peter $ 27 */ 28 29#include <sys/param.h> 30#include <sys/systm.h> 31#include <sys/lock.h> 32#include <sys/mutex.h> 33#include <sys/sysproto.h> 34#include <sys/signalvar.h> 35#include <sys/kernel.h> 36#include <sys/mount.h> 37#include <sys/filedesc.h> 38#include <sys/fcntl.h> 39#include <sys/acct.h> 40#include <sys/exec.h> 41#include <sys/imgact.h> 42#include <sys/imgact_elf.h> 43#include <sys/wait.h> 44#include <sys/proc.h> 45#include <sys/pioctl.h> 46#include <sys/malloc.h> 47#include <sys/namei.h> 48#include <sys/sysent.h> 49#include <sys/shm.h> 50#include <sys/sysctl.h> 51#include <sys/vnode.h> 52 53#include <vm/vm.h> 54#include <vm/vm_param.h> 55#include <vm/pmap.h> 56#include <vm/vm_page.h> 57#include <vm/vm_map.h> 58#include <vm/vm_kern.h> 59#include <vm/vm_extern.h> 60#include <vm/vm_object.h> 61#include <vm/vm_pager.h> 62 63#include <machine/reg.h> 64 65MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 66 67static register_t *exec_copyout_strings __P((struct image_params *)); 68 69/* XXX This should be vm_size_t. */ 70static u_long ps_strings = PS_STRINGS; 71SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, ""); 72 73/* XXX This should be vm_size_t. */ 74static u_long usrstack = USRSTACK; 75SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, ""); 76 77u_long ps_arg_cache_limit = PAGE_SIZE / 16; 78SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 79 &ps_arg_cache_limit, 0, ""); 80 81int ps_argsopen = 1; 82SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, ""); 83 84/* 85 * Each of the items is a pointer to a `const struct execsw', hence the 86 * double pointer here. 87 */ 88static const struct execsw **execsw; 89 90#ifndef _SYS_SYSPROTO_H_ 91struct execve_args { 92 char *fname; 93 char **argv; 94 char **envv; 95}; 96#endif 97 98/* 99 * execve() system call. 100 */ 101int 102execve(p, uap) 103 struct proc *p; 104 register struct execve_args *uap; 105{ 106 struct nameidata nd, *ndp; 107 struct ucred *newcred, *oldcred; 108 register_t *stack_base; 109 int error, len, i; 110 struct image_params image_params, *imgp; 111 struct vattr attr; 112 int (*img_first) __P((struct image_params *)); 113 114 imgp = &image_params; 115 116 /* 117 * Initialize part of the common data 118 */ 119 imgp->proc = p; 120 imgp->uap = uap; 121 imgp->attr = &attr; 122 imgp->argc = imgp->envc = 0; 123 imgp->argv0 = NULL; 124 imgp->entry_addr = 0; 125 imgp->vmspace_destroyed = 0; 126 imgp->interpreted = 0; 127 imgp->interpreter_name[0] = '\0'; 128 imgp->auxargs = NULL; 129 imgp->vp = NULL; 130 imgp->firstpage = NULL; 131 imgp->ps_strings = 0; 132 imgp->auxarg_size = 0; 133 134 /* 135 * Allocate temporary demand zeroed space for argument and 136 * environment strings 137 */ 138 imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE); 139 if (imgp->stringbase == NULL) { 140 error = ENOMEM; 141 goto exec_fail; 142 } 143 imgp->stringp = imgp->stringbase; 144 imgp->stringspace = ARG_MAX; 145 imgp->image_header = imgp->stringbase + ARG_MAX; 146 147 /* 148 * Translate the file name. namei() returns a vnode pointer 149 * in ni_vp amoung other things. 150 */ 151 ndp = &nd; 152 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 153 UIO_USERSPACE, uap->fname, p); 154 155interpret: 156 157 error = namei(ndp); 158 if (error) { 159 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 160 ARG_MAX + PAGE_SIZE); 161 goto exec_fail; 162 } 163 164 imgp->vp = ndp->ni_vp; 165 imgp->fname = uap->fname; 166 167 /* 168 * Check file permissions (also 'opens' file) 169 */ 170 error = exec_check_permissions(imgp); 171 if (error) { 172 VOP_UNLOCK(imgp->vp, 0, p); 173 goto exec_fail_dealloc; 174 } 175 176 error = exec_map_first_page(imgp); 177 VOP_UNLOCK(imgp->vp, 0, p); 178 if (error) 179 goto exec_fail_dealloc; 180 181 /* 182 * If the current process has a special image activator it 183 * wants to try first, call it. For example, emulating shell 184 * scripts differently. 185 */ 186 error = -1; 187 if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 188 error = img_first(imgp); 189 190 /* 191 * Loop through the list of image activators, calling each one. 192 * An activator returns -1 if there is no match, 0 on success, 193 * and an error otherwise. 194 */ 195 for (i = 0; error == -1 && execsw[i]; ++i) { 196 if (execsw[i]->ex_imgact == NULL || 197 execsw[i]->ex_imgact == img_first) { 198 continue; 199 } 200 error = (*execsw[i]->ex_imgact)(imgp); 201 } 202 203 if (error) { 204 if (error == -1) 205 error = ENOEXEC; 206 goto exec_fail_dealloc; 207 } 208 209 /* 210 * Special interpreter operation, cleanup and loop up to try to 211 * activate the interpreter. 212 */ 213 if (imgp->interpreted) { 214 exec_unmap_first_page(imgp); 215 /* free name buffer and old vnode */ 216 NDFREE(ndp, NDF_ONLY_PNBUF); 217 vrele(ndp->ni_vp); 218 /* set new name to that of the interpreter */ 219 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 220 UIO_SYSSPACE, imgp->interpreter_name, p); 221 goto interpret; 222 } 223 224 /* 225 * Copy out strings (args and env) and initialize stack base 226 */ 227 stack_base = exec_copyout_strings(imgp); 228 p->p_vmspace->vm_minsaddr = (char *)stack_base; 229 230 /* 231 * If custom stack fixup routine present for this process 232 * let it do the stack setup. 233 * Else stuff argument count as first item on stack 234 */ 235 if (p->p_sysent->sv_fixup) 236 (*p->p_sysent->sv_fixup)(&stack_base, imgp); 237 else 238 suword(--stack_base, imgp->argc); 239 240 /* 241 * For security and other reasons, the file descriptor table cannot 242 * be shared after an exec. 243 */ 244 if (p->p_fd->fd_refcnt > 1) { 245 struct filedesc *tmp; 246 247 tmp = fdcopy(p); 248 fdfree(p); 249 p->p_fd = tmp; 250 } 251 252 /* Stop profiling */ 253 stopprofclock(p); 254 255 /* close files on exec */ 256 fdcloseexec(p); 257 258 /* reset caught signals */ 259 execsigs(p); 260 261 /* name this process - nameiexec(p, ndp) */ 262 len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 263 bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 264 p->p_comm[len] = 0; 265 266 /* 267 * mark as execed, wakeup the process that vforked (if any) and tell 268 * it that it now has its own resources back 269 */ 270 PROC_LOCK(p); 271 p->p_flag |= P_EXEC; 272 if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 273 p->p_flag &= ~P_PPWAIT; 274 wakeup((caddr_t)p->p_pptr); 275 } 276 277 /* 278 * XXX: Note, the whole execve() is incredibly racey right now 279 * with regards to debugging and privilege/credential management. 280 * In particular, it's possible to race during exec() to attach 281 * debugging to a process that will gain privilege. 282 * 283 * This MUST be fixed prior to any release. 284 */ 285 286 /* 287 * Implement image setuid/setgid. 288 * 289 * Don't honor setuid/setgid if the filesystem prohibits it or if 290 * the process is being traced. 291 */ 292 oldcred = p->p_ucred; 293 newcred = NULL; 294 if ((((attr.va_mode & VSUID) && oldcred->cr_uid != attr.va_uid) || 295 ((attr.va_mode & VSGID) && oldcred->cr_gid != attr.va_gid)) && 296 (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 297 (p->p_flag & P_TRACED) == 0) { 298 PROC_UNLOCK(p); 299 /* 300 * Turn off syscall tracing for set-id programs, except for 301 * root. Record any set-id flags first to make sure that 302 * we do not regain any tracing during a possible block. 303 */ 304 setsugid(p); 305 if (p->p_tracep && suser_xxx(oldcred, NULL, PRISON_ROOT)) { 306 p->p_traceflag = 0; 307 vrele(p->p_tracep); 308 p->p_tracep = NULL; 309 } 310 /* 311 * Set the new credentials. 312 */ 313 newcred = crdup(oldcred); 314 if (attr.va_mode & VSUID) 315 change_euid(newcred, attr.va_uid); 316 if (attr.va_mode & VSGID) 317 change_egid(newcred, attr.va_gid); 318 setugidsafety(p); 319 } else { 320 if (oldcred->cr_uid == oldcred->cr_ruid && 321 oldcred->cr_gid == oldcred->cr_rgid) 322 p->p_flag &= ~P_SUGID; 323 PROC_UNLOCK(p); 324 } 325 326 /* 327 * Implement correct POSIX saved-id behavior. 328 * 329 * XXX: It's not clear that the existing behavior is 330 * POSIX-compliant. A number of sourses indicate that the saved 331 * uid/gid should only be updated if the new ruid is not equal to 332 * the old ruid, or the new euid is not equal to the old euid and 333 * the new euid is not equal to the old ruid. The FreeBSD code 334 * always updates the saved uid/gid. Also, this code uses the new 335 * (replaced) euid and egid as the source, which may or may not be 336 * the right ones to use. 337 */ 338 if (newcred == NULL) { 339 if (oldcred->cr_svuid != oldcred->cr_uid || 340 oldcred->cr_svgid != oldcred->cr_gid) { 341 newcred = crdup(oldcred); 342 change_svuid(newcred, newcred->cr_uid); 343 change_svgid(newcred, newcred->cr_gid); 344 } 345 } else { 346 change_svuid(newcred, newcred->cr_uid); 347 change_svgid(newcred, newcred->cr_gid); 348 } 349 350 if (newcred != NULL) { 351 PROC_LOCK(p); 352 p->p_ucred = newcred; 353 PROC_UNLOCK(p); 354 crfree(oldcred); 355 } 356 357 /* 358 * Store the vp for use in procfs 359 */ 360 if (p->p_textvp) /* release old reference */ 361 vrele(p->p_textvp); 362 VREF(ndp->ni_vp); 363 p->p_textvp = ndp->ni_vp; 364 365 /* 366 * notify others that we exec'd 367 */ 368 PROC_LOCK(p); 369 KNOTE(&p->p_klist, NOTE_EXEC); 370 371 /* 372 * If tracing the process, trap to debugger so breakpoints 373 * can be set before the program executes. 374 */ 375 _STOPEVENT(p, S_EXEC, 0); 376 377 if (p->p_flag & P_TRACED) 378 psignal(p, SIGTRAP); 379 380 /* clear "fork but no exec" flag, as we _are_ execing */ 381 p->p_acflag &= ~AFORK; 382 383 /* Set values passed into the program in registers. */ 384 setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base, 385 imgp->ps_strings); 386 387 /* Free any previous argument cache */ 388 if (p->p_args && --p->p_args->ar_ref == 0) 389 FREE(p->p_args, M_PARGS); 390 p->p_args = NULL; 391 392 /* Cache arguments if they fit inside our allowance */ 393 i = imgp->endargs - imgp->stringbase; 394 if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 395 PROC_UNLOCK(p); 396 MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i, 397 M_PARGS, M_WAITOK); 398 KASSERT(p->p_args != NULL, ("malloc of p_args failed")); 399 PROC_LOCK(p); 400 p->p_args->ar_ref = 1; 401 p->p_args->ar_length = i; 402 bcopy(imgp->stringbase, p->p_args->ar_args, i); 403 } 404 PROC_UNLOCK(p); 405 406exec_fail_dealloc: 407 408 /* 409 * free various allocated resources 410 */ 411 if (imgp->firstpage) 412 exec_unmap_first_page(imgp); 413 414 if (imgp->stringbase != NULL) 415 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 416 ARG_MAX + PAGE_SIZE); 417 418 if (imgp->vp) { 419 NDFREE(ndp, NDF_ONLY_PNBUF); 420 vrele(imgp->vp); 421 } 422 423 if (error == 0) 424 return (0); 425 426exec_fail: 427 if (imgp->vmspace_destroyed) { 428 /* sorry, no more process anymore. exit gracefully */ 429 exit1(p, W_EXITCODE(0, SIGABRT)); 430 /* NOT REACHED */ 431 return(0); 432 } else { 433 return(error); 434 } 435} 436 437int 438exec_map_first_page(imgp) 439 struct image_params *imgp; 440{ 441 int rv, i; 442 int initial_pagein; 443 vm_page_t ma[VM_INITIAL_PAGEIN]; 444 vm_object_t object; 445 446 447 if (imgp->firstpage) { 448 exec_unmap_first_page(imgp); 449 } 450 451 VOP_GETVOBJECT(imgp->vp, &object); 452 mtx_lock(&vm_mtx); 453 454 ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 455 456 if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 457 initial_pagein = VM_INITIAL_PAGEIN; 458 if (initial_pagein > object->size) 459 initial_pagein = object->size; 460 for (i = 1; i < initial_pagein; i++) { 461 if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 462 if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 463 break; 464 if (ma[i]->valid) 465 break; 466 vm_page_busy(ma[i]); 467 } else { 468 ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL); 469 if (ma[i] == NULL) 470 break; 471 } 472 } 473 initial_pagein = i; 474 475 rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 476 ma[0] = vm_page_lookup(object, 0); 477 478 if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) { 479 if (ma[0]) { 480 vm_page_protect(ma[0], VM_PROT_NONE); 481 vm_page_free(ma[0]); 482 } 483 mtx_unlock(&vm_mtx); 484 return EIO; 485 } 486 } 487 488 vm_page_wire(ma[0]); 489 vm_page_wakeup(ma[0]); 490 491 pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0])); 492 imgp->firstpage = ma[0]; 493 494 mtx_unlock(&vm_mtx); 495 return 0; 496} 497 498void 499exec_unmap_first_page(imgp) 500 struct image_params *imgp; 501{ 502 503 if (imgp->firstpage) { 504 mtx_lock(&vm_mtx); 505 pmap_kremove((vm_offset_t) imgp->image_header); 506 vm_page_unwire(imgp->firstpage, 1); 507 mtx_unlock(&vm_mtx); 508 imgp->firstpage = NULL; 509 } 510} 511 512/* 513 * Destroy old address space, and allocate a new stack 514 * The new stack is only SGROWSIZ large because it is grown 515 * automatically in trap.c. 516 */ 517int 518exec_new_vmspace(imgp) 519 struct image_params *imgp; 520{ 521 int error; 522 struct vmspace *vmspace = imgp->proc->p_vmspace; 523 caddr_t stack_addr = (caddr_t) (USRSTACK - MAXSSIZ); 524 vm_map_t map = &vmspace->vm_map; 525 526 mtx_assert(&vm_mtx, MA_OWNED); 527 imgp->vmspace_destroyed = 1; 528 529 /* 530 * Blow away entire process VM, if address space not shared, 531 * otherwise, create a new VM space so that other threads are 532 * not disrupted 533 */ 534 if (vmspace->vm_refcnt == 1) { 535 if (vmspace->vm_shm) 536 shmexit(imgp->proc); 537 pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS); 538 vm_map_remove(map, 0, VM_MAXUSER_ADDRESS); 539 } else { 540 vmspace_exec(imgp->proc); 541 vmspace = imgp->proc->p_vmspace; 542 map = &vmspace->vm_map; 543 } 544 545 /* Allocate a new stack */ 546 error = vm_map_stack (&vmspace->vm_map, (vm_offset_t)stack_addr, 547 (vm_size_t)MAXSSIZ, VM_PROT_ALL, VM_PROT_ALL, 0); 548 if (error) 549 return (error); 550 551#ifdef __ia64__ 552 { 553 /* 554 * Allocate backing store. We really need something 555 * similar to vm_map_stack which can allow the backing 556 * store to grow upwards. This will do for now. 557 */ 558 vm_offset_t bsaddr; 559 bsaddr = USRSTACK - 2*MAXSSIZ; 560 error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr, 561 4*PAGE_SIZE, 0, 562 VM_PROT_ALL, VM_PROT_ALL, 0); 563 imgp->proc->p_md.md_bspstore = bsaddr; 564 } 565#endif 566 567 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 568 * VM_STACK case, but they are still used to monitor the size of the 569 * process stack so we can check the stack rlimit. 570 */ 571 vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT; 572 vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ; 573 574 return(0); 575} 576 577/* 578 * Copy out argument and environment strings from the old process 579 * address space into the temporary string buffer. 580 */ 581int 582exec_extract_strings(imgp) 583 struct image_params *imgp; 584{ 585 char **argv, **envv; 586 char *argp, *envp; 587 int error; 588 size_t length; 589 590 /* 591 * extract arguments first 592 */ 593 594 argv = imgp->uap->argv; 595 596 if (argv) { 597 argp = (caddr_t) (intptr_t) fuword(argv); 598 if (argp == (caddr_t) -1) 599 return (EFAULT); 600 if (argp) 601 argv++; 602 if (imgp->argv0) 603 argp = imgp->argv0; 604 if (argp) { 605 do { 606 if (argp == (caddr_t) -1) 607 return (EFAULT); 608 if ((error = copyinstr(argp, imgp->stringp, 609 imgp->stringspace, &length))) { 610 if (error == ENAMETOOLONG) 611 return(E2BIG); 612 return (error); 613 } 614 imgp->stringspace -= length; 615 imgp->stringp += length; 616 imgp->argc++; 617 } while ((argp = (caddr_t) (intptr_t) fuword(argv++))); 618 } 619 } 620 621 imgp->endargs = imgp->stringp; 622 623 /* 624 * extract environment strings 625 */ 626 627 envv = imgp->uap->envv; 628 629 if (envv) { 630 while ((envp = (caddr_t) (intptr_t) fuword(envv++))) { 631 if (envp == (caddr_t) -1) 632 return (EFAULT); 633 if ((error = copyinstr(envp, imgp->stringp, 634 imgp->stringspace, &length))) { 635 if (error == ENAMETOOLONG) 636 return(E2BIG); 637 return (error); 638 } 639 imgp->stringspace -= length; 640 imgp->stringp += length; 641 imgp->envc++; 642 } 643 } 644 645 return (0); 646} 647 648/* 649 * Copy strings out to the new process address space, constructing 650 * new arg and env vector tables. Return a pointer to the base 651 * so that it can be used as the initial stack pointer. 652 */ 653register_t * 654exec_copyout_strings(imgp) 655 struct image_params *imgp; 656{ 657 int argc, envc; 658 char **vectp; 659 char *stringp, *destp; 660 register_t *stack_base; 661 struct ps_strings *arginfo; 662 int szsigcode; 663 664 /* 665 * Calculate string base and vector table pointers. 666 * Also deal with signal trampoline code for this exec type. 667 */ 668 arginfo = (struct ps_strings *)PS_STRINGS; 669 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 670 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 671 roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 672 673 /* 674 * install sigcode 675 */ 676 if (szsigcode) 677 copyout(imgp->proc->p_sysent->sv_sigcode, 678 ((caddr_t)arginfo - szsigcode), szsigcode); 679 680 /* 681 * If we have a valid auxargs ptr, prepare some room 682 * on the stack. 683 */ 684 if (imgp->auxargs) { 685 /* 686 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 687 * lower compatibility. 688 */ 689 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size 690 : (AT_COUNT * 2); 691 /* 692 * The '+ 2' is for the null pointers at the end of each of 693 * the arg and env vector sets,and imgp->auxarg_size is room 694 * for argument of Runtime loader. 695 */ 696 vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 + 697 imgp->auxarg_size) * sizeof(char *)); 698 699 } else 700 /* 701 * The '+ 2' is for the null pointers at the end of each of 702 * the arg and env vector sets 703 */ 704 vectp = (char **) 705 (destp - (imgp->argc + imgp->envc + 2) * sizeof(char *)); 706 707 /* 708 * vectp also becomes our initial stack base 709 */ 710 stack_base = (register_t *)vectp; 711 712 stringp = imgp->stringbase; 713 argc = imgp->argc; 714 envc = imgp->envc; 715 716 /* 717 * Copy out strings - arguments and environment. 718 */ 719 copyout(stringp, destp, ARG_MAX - imgp->stringspace); 720 721 /* 722 * Fill in "ps_strings" struct for ps, w, etc. 723 */ 724 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 725 suword(&arginfo->ps_nargvstr, argc); 726 727 /* 728 * Fill in argument portion of vector table. 729 */ 730 for (; argc > 0; --argc) { 731 suword(vectp++, (long)(intptr_t)destp); 732 while (*stringp++ != 0) 733 destp++; 734 destp++; 735 } 736 737 /* a null vector table pointer separates the argp's from the envp's */ 738 suword(vectp++, 0); 739 740 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 741 suword(&arginfo->ps_nenvstr, envc); 742 743 /* 744 * Fill in environment portion of vector table. 745 */ 746 for (; envc > 0; --envc) { 747 suword(vectp++, (long)(intptr_t)destp); 748 while (*stringp++ != 0) 749 destp++; 750 destp++; 751 } 752 753 /* end of vector table is a null pointer */ 754 suword(vectp, 0); 755 756 return (stack_base); 757} 758 759/* 760 * Check permissions of file to execute. 761 * Called with imgp->vp locked. 762 * Return 0 for success or error code on failure. 763 */ 764int 765exec_check_permissions(imgp) 766 struct image_params *imgp; 767{ 768 struct proc *p = imgp->proc; 769 struct vnode *vp = imgp->vp; 770 struct vattr *attr = imgp->attr; 771 int error; 772 773 /* Get file attributes */ 774 error = VOP_GETATTR(vp, attr, p->p_ucred, p); 775 if (error) 776 return (error); 777 778 /* 779 * 1) Check if file execution is disabled for the filesystem that this 780 * file resides on. 781 * 2) Insure that at least one execute bit is on - otherwise root 782 * will always succeed, and we don't want to happen unless the 783 * file really is executable. 784 * 3) Insure that the file is a regular file. 785 */ 786 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 787 ((attr->va_mode & 0111) == 0) || 788 (attr->va_type != VREG)) { 789 return (EACCES); 790 } 791 792 /* 793 * Zero length files can't be exec'd 794 */ 795 if (attr->va_size == 0) 796 return (ENOEXEC); 797 798 /* 799 * Check for execute permission to file based on current credentials. 800 */ 801 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 802 if (error) 803 return (error); 804 805 /* 806 * Check number of open-for-writes on the file and deny execution 807 * if there are any. 808 */ 809 if (vp->v_writecount) 810 return (ETXTBSY); 811 812 /* 813 * Call filesystem specific open routine (which does nothing in the 814 * general case). 815 */ 816 error = VOP_OPEN(vp, FREAD, p->p_ucred, p); 817 if (error) 818 return (error); 819 820 return (0); 821} 822 823/* 824 * Exec handler registration 825 */ 826int 827exec_register(execsw_arg) 828 const struct execsw *execsw_arg; 829{ 830 const struct execsw **es, **xs, **newexecsw; 831 int count = 2; /* New slot and trailing NULL */ 832 833 if (execsw) 834 for (es = execsw; *es; es++) 835 count++; 836 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 837 if (newexecsw == NULL) 838 return ENOMEM; 839 xs = newexecsw; 840 if (execsw) 841 for (es = execsw; *es; es++) 842 *xs++ = *es; 843 *xs++ = execsw_arg; 844 *xs = NULL; 845 if (execsw) 846 free(execsw, M_TEMP); 847 execsw = newexecsw; 848 return 0; 849} 850 851int 852exec_unregister(execsw_arg) 853 const struct execsw *execsw_arg; 854{ 855 const struct execsw **es, **xs, **newexecsw; 856 int count = 1; 857 858 if (execsw == NULL) 859 panic("unregister with no handlers left?\n"); 860 861 for (es = execsw; *es; es++) { 862 if (*es == execsw_arg) 863 break; 864 } 865 if (*es == NULL) 866 return ENOENT; 867 for (es = execsw; *es; es++) 868 if (*es != execsw_arg) 869 count++; 870 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 871 if (newexecsw == NULL) 872 return ENOMEM; 873 xs = newexecsw; 874 for (es = execsw; *es; es++) 875 if (*es != execsw_arg) 876 *xs++ = *es; 877 *xs = NULL; 878 if (execsw) 879 free(execsw, M_TEMP); 880 execsw = newexecsw; 881 return 0; 882} 883