1 /* $NetBSD: kern_resource.c,v 1.147 2008/10/11 13:40:57 pooka Exp $ */ 2 3 /*- 4 * Copyright (c) 1982, 1986, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)kern_resource.c 8.8 (Berkeley) 2/14/95 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.147 2008/10/11 13:40:57 pooka Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/file.h> 46 #include <sys/resourcevar.h> 47 #include <sys/malloc.h> 48 #include <sys/kmem.h> 49 #include <sys/namei.h> 50 #include <sys/pool.h> 51 #include <sys/proc.h> 52 #include <sys/sysctl.h> 53 #include <sys/timevar.h> 54 #include <sys/kauth.h> 55 #include <sys/atomic.h> 56 #include <sys/mount.h> 57 #include <sys/syscallargs.h> 58 #include <sys/atomic.h> 59 60 #include <uvm/uvm_extern.h> 61 62 /* 63 * Maximum process data and stack limits. 64 * They are variables so they are patchable. 65 */ 66 rlim_t maxdmap = MAXDSIZ; 67 rlim_t maxsmap = MAXSSIZ; 68 69 static pool_cache_t plimit_cache; 70 static pool_cache_t pstats_cache; 71 72 void 73 resource_init(void) 74 { 75 76 plimit_cache = pool_cache_init(sizeof(struct plimit), 0, 0, 0, 77 "plimitpl", NULL, IPL_NONE, NULL, NULL, NULL); 78 pstats_cache = pool_cache_init(sizeof(struct pstats), 0, 0, 0, 79 "pstatspl", NULL, IPL_NONE, NULL, NULL, NULL); 80 } 81 82 /* 83 * Resource controls and accounting. 84 */ 85 86 int 87 sys_getpriority(struct lwp *l, const struct sys_getpriority_args *uap, 88 register_t *retval) 89 { 90 /* { 91 syscallarg(int) which; 92 syscallarg(id_t) who; 93 } */ 94 struct proc *curp = l->l_proc, *p; 95 int low = NZERO + PRIO_MAX + 1; 96 int who = SCARG(uap, who); 97 98 mutex_enter(proc_lock); 99 switch (SCARG(uap, which)) { 100 case PRIO_PROCESS: 101 if (who == 0) 102 p = curp; 103 else 104 p = p_find(who, PFIND_LOCKED); 105 if (p != NULL) 106 low = p->p_nice; 107 break; 108 109 case PRIO_PGRP: { 110 struct pgrp *pg; 111 112 if (who == 0) 113 pg = curp->p_pgrp; 114 else if ((pg = pg_find(who, PFIND_LOCKED)) == NULL) 115 break; 116 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 117 if (p->p_nice < low) 118 low = p->p_nice; 119 } 120 break; 121 } 122 123 case PRIO_USER: 124 if (who == 0) 125 who = (int)kauth_cred_geteuid(l->l_cred); 126 PROCLIST_FOREACH(p, &allproc) { 127 if ((p->p_flag & PK_MARKER) != 0) 128 continue; 129 mutex_enter(p->p_lock); 130 if (kauth_cred_geteuid(p->p_cred) == 131 (uid_t)who && p->p_nice < low) 132 low = p->p_nice; 133 mutex_exit(p->p_lock); 134 } 135 break; 136 137 default: 138 mutex_exit(proc_lock); 139 return (EINVAL); 140 } 141 mutex_exit(proc_lock); 142 143 if (low == NZERO + PRIO_MAX + 1) 144 return (ESRCH); 145 *retval = low - NZERO; 146 return (0); 147 } 148 149 /* ARGSUSED */ 150 int 151 sys_setpriority(struct lwp *l, const struct sys_setpriority_args *uap, 152 register_t *retval) 153 { 154 /* { 155 syscallarg(int) which; 156 syscallarg(id_t) who; 157 syscallarg(int) prio; 158 } */ 159 struct proc *curp = l->l_proc, *p; 160 int found = 0, error = 0; 161 int who = SCARG(uap, who); 162 163 mutex_enter(proc_lock); 164 switch (SCARG(uap, which)) { 165 case PRIO_PROCESS: 166 if (who == 0) 167 p = curp; 168 else 169 p = p_find(who, PFIND_LOCKED); 170 if (p != 0) { 171 mutex_enter(p->p_lock); 172 error = donice(l, p, SCARG(uap, prio)); 173 mutex_exit(p->p_lock); 174 found++; 175 } 176 break; 177 178 case PRIO_PGRP: { 179 struct pgrp *pg; 180 181 if (who == 0) 182 pg = curp->p_pgrp; 183 else if ((pg = pg_find(who, PFIND_LOCKED)) == NULL) 184 break; 185 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 186 mutex_enter(p->p_lock); 187 error = donice(l, p, SCARG(uap, prio)); 188 mutex_exit(p->p_lock); 189 found++; 190 } 191 break; 192 } 193 194 case PRIO_USER: 195 if (who == 0) 196 who = (int)kauth_cred_geteuid(l->l_cred); 197 PROCLIST_FOREACH(p, &allproc) { 198 if ((p->p_flag & PK_MARKER) != 0) 199 continue; 200 mutex_enter(p->p_lock); 201 if (kauth_cred_geteuid(p->p_cred) == 202 (uid_t)SCARG(uap, who)) { 203 error = donice(l, p, SCARG(uap, prio)); 204 found++; 205 } 206 mutex_exit(p->p_lock); 207 } 208 break; 209 210 default: 211 mutex_exit(proc_lock); 212 return EINVAL; 213 } 214 mutex_exit(proc_lock); 215 if (found == 0) 216 return (ESRCH); 217 return (error); 218 } 219 220 /* 221 * Renice a process. 222 * 223 * Call with the target process' credentials locked. 224 */ 225 int 226 donice(struct lwp *l, struct proc *chgp, int n) 227 { 228 kauth_cred_t cred = l->l_cred; 229 230 KASSERT(mutex_owned(chgp->p_lock)); 231 232 if (n > PRIO_MAX) 233 n = PRIO_MAX; 234 if (n < PRIO_MIN) 235 n = PRIO_MIN; 236 n += NZERO; 237 if (kauth_authorize_process(cred, KAUTH_PROCESS_NICE, chgp, 238 KAUTH_ARG(n), NULL, NULL)) 239 return (EACCES); 240 sched_nice(chgp, n); 241 return (0); 242 } 243 244 /* ARGSUSED */ 245 int 246 sys_setrlimit(struct lwp *l, const struct sys_setrlimit_args *uap, 247 register_t *retval) 248 { 249 /* { 250 syscallarg(int) which; 251 syscallarg(const struct rlimit *) rlp; 252 } */ 253 int which = SCARG(uap, which); 254 struct rlimit alim; 255 int error; 256 257 error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit)); 258 if (error) 259 return (error); 260 return (dosetrlimit(l, l->l_proc, which, &alim)); 261 } 262 263 int 264 dosetrlimit(struct lwp *l, struct proc *p, int which, struct rlimit *limp) 265 { 266 struct rlimit *alimp; 267 int error; 268 269 if ((u_int)which >= RLIM_NLIMITS) 270 return (EINVAL); 271 272 if (limp->rlim_cur < 0 || limp->rlim_max < 0) 273 return (EINVAL); 274 275 if (limp->rlim_cur > limp->rlim_max) { 276 /* 277 * This is programming error. According to SUSv2, we should 278 * return error in this case. 279 */ 280 return (EINVAL); 281 } 282 283 alimp = &p->p_rlimit[which]; 284 /* if we don't change the value, no need to limcopy() */ 285 if (limp->rlim_cur == alimp->rlim_cur && 286 limp->rlim_max == alimp->rlim_max) 287 return 0; 288 289 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT, 290 p, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_SET), limp, KAUTH_ARG(which)); 291 if (error) 292 return (error); 293 294 lim_privatise(p, false); 295 /* p->p_limit is now unchangeable */ 296 alimp = &p->p_rlimit[which]; 297 298 switch (which) { 299 300 case RLIMIT_DATA: 301 if (limp->rlim_cur > maxdmap) 302 limp->rlim_cur = maxdmap; 303 if (limp->rlim_max > maxdmap) 304 limp->rlim_max = maxdmap; 305 break; 306 307 case RLIMIT_STACK: 308 if (limp->rlim_cur > maxsmap) 309 limp->rlim_cur = maxsmap; 310 if (limp->rlim_max > maxsmap) 311 limp->rlim_max = maxsmap; 312 313 /* 314 * Return EINVAL if the new stack size limit is lower than 315 * current usage. Otherwise, the process would get SIGSEGV the 316 * moment it would try to access anything on it's current stack. 317 * This conforms to SUSv2. 318 */ 319 if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE 320 || limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE) { 321 return (EINVAL); 322 } 323 324 /* 325 * Stack is allocated to the max at exec time with 326 * only "rlim_cur" bytes accessible (In other words, 327 * allocates stack dividing two contiguous regions at 328 * "rlim_cur" bytes boundary). 329 * 330 * Since allocation is done in terms of page, roundup 331 * "rlim_cur" (otherwise, contiguous regions 332 * overlap). If stack limit is going up make more 333 * accessible, if going down make inaccessible. 334 */ 335 limp->rlim_cur = round_page(limp->rlim_cur); 336 if (limp->rlim_cur != alimp->rlim_cur) { 337 vaddr_t addr; 338 vsize_t size; 339 vm_prot_t prot; 340 341 if (limp->rlim_cur > alimp->rlim_cur) { 342 prot = VM_PROT_READ | VM_PROT_WRITE; 343 size = limp->rlim_cur - alimp->rlim_cur; 344 addr = (vaddr_t)p->p_vmspace->vm_minsaddr - 345 limp->rlim_cur; 346 } else { 347 prot = VM_PROT_NONE; 348 size = alimp->rlim_cur - limp->rlim_cur; 349 addr = (vaddr_t)p->p_vmspace->vm_minsaddr - 350 alimp->rlim_cur; 351 } 352 (void) uvm_map_protect(&p->p_vmspace->vm_map, 353 addr, addr+size, prot, false); 354 } 355 break; 356 357 case RLIMIT_NOFILE: 358 if (limp->rlim_cur > maxfiles) 359 limp->rlim_cur = maxfiles; 360 if (limp->rlim_max > maxfiles) 361 limp->rlim_max = maxfiles; 362 break; 363 364 case RLIMIT_NPROC: 365 if (limp->rlim_cur > maxproc) 366 limp->rlim_cur = maxproc; 367 if (limp->rlim_max > maxproc) 368 limp->rlim_max = maxproc; 369 break; 370 } 371 372 mutex_enter(&p->p_limit->pl_lock); 373 *alimp = *limp; 374 mutex_exit(&p->p_limit->pl_lock); 375 return (0); 376 } 377 378 /* ARGSUSED */ 379 int 380 sys_getrlimit(struct lwp *l, const struct sys_getrlimit_args *uap, 381 register_t *retval) 382 { 383 /* { 384 syscallarg(int) which; 385 syscallarg(struct rlimit *) rlp; 386 } */ 387 struct proc *p = l->l_proc; 388 int which = SCARG(uap, which); 389 struct rlimit rl; 390 391 if ((u_int)which >= RLIM_NLIMITS) 392 return (EINVAL); 393 394 mutex_enter(p->p_lock); 395 memcpy(&rl, &p->p_rlimit[which], sizeof(rl)); 396 mutex_exit(p->p_lock); 397 398 return copyout(&rl, SCARG(uap, rlp), sizeof(rl)); 399 } 400 401 /* 402 * Transform the running time and tick information in proc p into user, 403 * system, and interrupt time usage. 404 * 405 * Should be called with p->p_lock held unless called from exit1(). 406 */ 407 void 408 calcru(struct proc *p, struct timeval *up, struct timeval *sp, 409 struct timeval *ip, struct timeval *rp) 410 { 411 uint64_t u, st, ut, it, tot; 412 struct lwp *l; 413 struct bintime tm; 414 struct timeval tv; 415 416 mutex_spin_enter(&p->p_stmutex); 417 st = p->p_sticks; 418 ut = p->p_uticks; 419 it = p->p_iticks; 420 mutex_spin_exit(&p->p_stmutex); 421 422 tm = p->p_rtime; 423 424 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 425 lwp_lock(l); 426 bintime_add(&tm, &l->l_rtime); 427 if ((l->l_pflag & LP_RUNNING) != 0) { 428 struct bintime diff; 429 /* 430 * Adjust for the current time slice. This is 431 * actually fairly important since the error 432 * here is on the order of a time quantum, 433 * which is much greater than the sampling 434 * error. 435 */ 436 binuptime(&diff); 437 bintime_sub(&diff, &l->l_stime); 438 bintime_add(&tm, &diff); 439 } 440 lwp_unlock(l); 441 } 442 443 tot = st + ut + it; 444 bintime2timeval(&tm, &tv); 445 u = (uint64_t)tv.tv_sec * 1000000ul + tv.tv_usec; 446 447 if (tot == 0) { 448 /* No ticks, so can't use to share time out, split 50-50 */ 449 st = ut = u / 2; 450 } else { 451 st = (u * st) / tot; 452 ut = (u * ut) / tot; 453 } 454 if (sp != NULL) { 455 sp->tv_sec = st / 1000000; 456 sp->tv_usec = st % 1000000; 457 } 458 if (up != NULL) { 459 up->tv_sec = ut / 1000000; 460 up->tv_usec = ut % 1000000; 461 } 462 if (ip != NULL) { 463 if (it != 0) 464 it = (u * it) / tot; 465 ip->tv_sec = it / 1000000; 466 ip->tv_usec = it % 1000000; 467 } 468 if (rp != NULL) { 469 *rp = tv; 470 } 471 } 472 473 /* ARGSUSED */ 474 int 475 sys_getrusage(struct lwp *l, const struct sys_getrusage_args *uap, 476 register_t *retval) 477 { 478 /* { 479 syscallarg(int) who; 480 syscallarg(struct rusage *) rusage; 481 } */ 482 struct rusage ru; 483 struct proc *p = l->l_proc; 484 485 switch (SCARG(uap, who)) { 486 case RUSAGE_SELF: 487 mutex_enter(p->p_lock); 488 memcpy(&ru, &p->p_stats->p_ru, sizeof(ru)); 489 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL); 490 rulwps(p, &ru); 491 mutex_exit(p->p_lock); 492 break; 493 494 case RUSAGE_CHILDREN: 495 mutex_enter(p->p_lock); 496 memcpy(&ru, &p->p_stats->p_cru, sizeof(ru)); 497 mutex_exit(p->p_lock); 498 break; 499 500 default: 501 return EINVAL; 502 } 503 504 return copyout(&ru, SCARG(uap, rusage), sizeof(ru)); 505 } 506 507 void 508 ruadd(struct rusage *ru, struct rusage *ru2) 509 { 510 long *ip, *ip2; 511 int i; 512 513 timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime); 514 timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime); 515 if (ru->ru_maxrss < ru2->ru_maxrss) 516 ru->ru_maxrss = ru2->ru_maxrss; 517 ip = &ru->ru_first; ip2 = &ru2->ru_first; 518 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) 519 *ip++ += *ip2++; 520 } 521 522 void 523 rulwps(proc_t *p, struct rusage *ru) 524 { 525 lwp_t *l; 526 527 KASSERT(mutex_owned(p->p_lock)); 528 529 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 530 ruadd(ru, &l->l_ru); 531 ru->ru_nvcsw += (l->l_ncsw - l->l_nivcsw); 532 ru->ru_nivcsw += l->l_nivcsw; 533 } 534 } 535 536 /* 537 * Make a copy of the plimit structure. 538 * We share these structures copy-on-write after fork, 539 * and copy when a limit is changed. 540 * 541 * Unfortunately (due to PL_SHAREMOD) it is possibly for the structure 542 * we are copying to change beneath our feet! 543 */ 544 struct plimit * 545 lim_copy(struct plimit *lim) 546 { 547 struct plimit *newlim; 548 char *corename; 549 size_t alen, len; 550 551 newlim = pool_cache_get(plimit_cache, PR_WAITOK); 552 mutex_init(&newlim->pl_lock, MUTEX_DEFAULT, IPL_NONE); 553 newlim->pl_flags = 0; 554 newlim->pl_refcnt = 1; 555 newlim->pl_sv_limit = NULL; 556 557 mutex_enter(&lim->pl_lock); 558 memcpy(newlim->pl_rlimit, lim->pl_rlimit, 559 sizeof(struct rlimit) * RLIM_NLIMITS); 560 561 alen = 0; 562 corename = NULL; 563 for (;;) { 564 if (lim->pl_corename == defcorename) { 565 newlim->pl_corename = defcorename; 566 break; 567 } 568 len = strlen(lim->pl_corename) + 1; 569 if (len <= alen) { 570 newlim->pl_corename = corename; 571 memcpy(corename, lim->pl_corename, len); 572 corename = NULL; 573 break; 574 } 575 mutex_exit(&lim->pl_lock); 576 if (corename != NULL) 577 free(corename, M_TEMP); 578 alen = len; 579 corename = malloc(alen, M_TEMP, M_WAITOK); 580 mutex_enter(&lim->pl_lock); 581 } 582 mutex_exit(&lim->pl_lock); 583 if (corename != NULL) 584 free(corename, M_TEMP); 585 return newlim; 586 } 587 588 void 589 lim_addref(struct plimit *lim) 590 { 591 atomic_inc_uint(&lim->pl_refcnt); 592 } 593 594 /* 595 * Give a process it's own private plimit structure. 596 * This will only be shared (in fork) if modifications are to be shared. 597 */ 598 void 599 lim_privatise(struct proc *p, bool set_shared) 600 { 601 struct plimit *lim, *newlim; 602 603 lim = p->p_limit; 604 if (lim->pl_flags & PL_WRITEABLE) { 605 if (set_shared) 606 lim->pl_flags |= PL_SHAREMOD; 607 return; 608 } 609 610 if (set_shared && lim->pl_flags & PL_SHAREMOD) 611 return; 612 613 newlim = lim_copy(lim); 614 615 mutex_enter(p->p_lock); 616 if (p->p_limit->pl_flags & PL_WRITEABLE) { 617 /* Someone crept in while we were busy */ 618 mutex_exit(p->p_lock); 619 limfree(newlim); 620 if (set_shared) 621 p->p_limit->pl_flags |= PL_SHAREMOD; 622 return; 623 } 624 625 /* 626 * Since most accesses to p->p_limit aren't locked, we must not 627 * delete the old limit structure yet. 628 */ 629 newlim->pl_sv_limit = p->p_limit; 630 newlim->pl_flags |= PL_WRITEABLE; 631 if (set_shared) 632 newlim->pl_flags |= PL_SHAREMOD; 633 p->p_limit = newlim; 634 mutex_exit(p->p_lock); 635 } 636 637 void 638 limfree(struct plimit *lim) 639 { 640 struct plimit *sv_lim; 641 642 do { 643 if (atomic_dec_uint_nv(&lim->pl_refcnt) > 0) 644 return; 645 if (lim->pl_corename != defcorename) 646 free(lim->pl_corename, M_TEMP); 647 sv_lim = lim->pl_sv_limit; 648 mutex_destroy(&lim->pl_lock); 649 pool_cache_put(plimit_cache, lim); 650 } while ((lim = sv_lim) != NULL); 651 } 652 653 struct pstats * 654 pstatscopy(struct pstats *ps) 655 { 656 657 struct pstats *newps; 658 659 newps = pool_cache_get(pstats_cache, PR_WAITOK); 660 661 memset(&newps->pstat_startzero, 0, 662 (unsigned) ((char *)&newps->pstat_endzero - 663 (char *)&newps->pstat_startzero)); 664 memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy, 665 ((char *)&newps->pstat_endcopy - 666 (char *)&newps->pstat_startcopy)); 667 668 return (newps); 669 670 } 671 672 void 673 pstatsfree(struct pstats *ps) 674 { 675 676 pool_cache_put(pstats_cache, ps); 677 } 678 679 /* 680 * sysctl interface in five parts 681 */ 682 683 /* 684 * a routine for sysctl proc subtree helpers that need to pick a valid 685 * process by pid. 686 */ 687 static int 688 sysctl_proc_findproc(struct lwp *l, struct proc **p2, pid_t pid) 689 { 690 struct proc *ptmp; 691 int error = 0; 692 693 if (pid == PROC_CURPROC) 694 ptmp = l->l_proc; 695 else if ((ptmp = pfind(pid)) == NULL) 696 error = ESRCH; 697 698 *p2 = ptmp; 699 return (error); 700 } 701 702 /* 703 * sysctl helper routine for setting a process's specific corefile 704 * name. picks the process based on the given pid and checks the 705 * correctness of the new value. 706 */ 707 static int 708 sysctl_proc_corename(SYSCTLFN_ARGS) 709 { 710 struct proc *ptmp; 711 struct plimit *lim; 712 int error = 0, len; 713 char *cname; 714 char *ocore; 715 char *tmp; 716 struct sysctlnode node; 717 718 /* 719 * is this all correct? 720 */ 721 if (namelen != 0) 722 return (EINVAL); 723 if (name[-1] != PROC_PID_CORENAME) 724 return (EINVAL); 725 726 /* 727 * whom are we tweaking? 728 */ 729 error = sysctl_proc_findproc(l, &ptmp, (pid_t)name[-2]); 730 if (error) 731 return (error); 732 733 /* XXX-elad */ 734 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp, 735 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL); 736 if (error) 737 return (error); 738 739 if (newp == NULL) { 740 error = kauth_authorize_process(l->l_cred, 741 KAUTH_PROCESS_CORENAME, ptmp, 742 KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_GET), NULL, NULL); 743 if (error) 744 return (error); 745 } 746 747 /* 748 * let them modify a temporary copy of the core name 749 */ 750 cname = PNBUF_GET(); 751 lim = ptmp->p_limit; 752 mutex_enter(&lim->pl_lock); 753 strlcpy(cname, lim->pl_corename, MAXPATHLEN); 754 mutex_exit(&lim->pl_lock); 755 756 node = *rnode; 757 node.sysctl_data = cname; 758 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 759 760 /* 761 * if that failed, or they have nothing new to say, or we've 762 * heard it before... 763 */ 764 if (error || newp == NULL) 765 goto done; 766 lim = ptmp->p_limit; 767 mutex_enter(&lim->pl_lock); 768 error = strcmp(cname, lim->pl_corename); 769 mutex_exit(&lim->pl_lock); 770 if (error == 0) 771 /* Unchanged */ 772 goto done; 773 774 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CORENAME, 775 ptmp, KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_SET), cname, NULL); 776 if (error) 777 return (error); 778 779 /* 780 * no error yet and cname now has the new core name in it. 781 * let's see if it looks acceptable. it must be either "core" 782 * or end in ".core" or "/core". 783 */ 784 len = strlen(cname); 785 if (len < 4) { 786 error = EINVAL; 787 } else if (strcmp(cname + len - 4, "core") != 0) { 788 error = EINVAL; 789 } else if (len > 4 && cname[len - 5] != '/' && cname[len - 5] != '.') { 790 error = EINVAL; 791 } 792 if (error != 0) { 793 goto done; 794 } 795 796 /* 797 * hmm...looks good. now...where do we put it? 798 */ 799 tmp = malloc(len + 1, M_TEMP, M_WAITOK|M_CANFAIL); 800 if (tmp == NULL) { 801 error = ENOMEM; 802 goto done; 803 } 804 memcpy(tmp, cname, len + 1); 805 806 lim_privatise(ptmp, false); 807 lim = ptmp->p_limit; 808 mutex_enter(&lim->pl_lock); 809 ocore = lim->pl_corename; 810 lim->pl_corename = tmp; 811 mutex_exit(&lim->pl_lock); 812 if (ocore != defcorename) 813 free(ocore, M_TEMP); 814 815 done: 816 PNBUF_PUT(cname); 817 return error; 818 } 819 820 /* 821 * sysctl helper routine for checking/setting a process's stop flags, 822 * one for fork and one for exec. 823 */ 824 static int 825 sysctl_proc_stop(SYSCTLFN_ARGS) 826 { 827 struct proc *ptmp; 828 int i, f, error = 0; 829 struct sysctlnode node; 830 831 if (namelen != 0) 832 return (EINVAL); 833 834 error = sysctl_proc_findproc(l, &ptmp, (pid_t)name[-2]); 835 if (error) 836 return (error); 837 838 /* XXX-elad */ 839 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp, 840 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL); 841 if (error) 842 return (error); 843 844 switch (rnode->sysctl_num) { 845 case PROC_PID_STOPFORK: 846 f = PS_STOPFORK; 847 break; 848 case PROC_PID_STOPEXEC: 849 f = PS_STOPEXEC; 850 break; 851 case PROC_PID_STOPEXIT: 852 f = PS_STOPEXIT; 853 break; 854 default: 855 return (EINVAL); 856 } 857 858 i = (ptmp->p_flag & f) ? 1 : 0; 859 node = *rnode; 860 node.sysctl_data = &i; 861 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 862 if (error || newp == NULL) 863 return (error); 864 865 mutex_enter(ptmp->p_lock); 866 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_STOPFLAG, 867 ptmp, KAUTH_ARG(f), NULL, NULL); 868 if (!error) { 869 if (i) { 870 ptmp->p_sflag |= f; 871 } else { 872 ptmp->p_sflag &= ~f; 873 } 874 } 875 mutex_exit(ptmp->p_lock); 876 877 return error; 878 } 879 880 /* 881 * sysctl helper routine for a process's rlimits as exposed by sysctl. 882 */ 883 static int 884 sysctl_proc_plimit(SYSCTLFN_ARGS) 885 { 886 struct proc *ptmp; 887 u_int limitno; 888 int which, error = 0; 889 struct rlimit alim; 890 struct sysctlnode node; 891 892 if (namelen != 0) 893 return (EINVAL); 894 895 which = name[-1]; 896 if (which != PROC_PID_LIMIT_TYPE_SOFT && 897 which != PROC_PID_LIMIT_TYPE_HARD) 898 return (EINVAL); 899 900 limitno = name[-2] - 1; 901 if (limitno >= RLIM_NLIMITS) 902 return (EINVAL); 903 904 if (name[-3] != PROC_PID_LIMIT) 905 return (EINVAL); 906 907 error = sysctl_proc_findproc(l, &ptmp, (pid_t)name[-4]); 908 if (error) 909 return (error); 910 911 /* XXX-elad */ 912 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp, 913 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL); 914 if (error) 915 return (error); 916 917 /* Check if we can view limits. */ 918 if (newp == NULL) { 919 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT, 920 ptmp, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_GET), &alim, 921 KAUTH_ARG(which)); 922 if (error) 923 return (error); 924 } 925 926 node = *rnode; 927 memcpy(&alim, &ptmp->p_rlimit[limitno], sizeof(alim)); 928 if (which == PROC_PID_LIMIT_TYPE_HARD) 929 node.sysctl_data = &alim.rlim_max; 930 else 931 node.sysctl_data = &alim.rlim_cur; 932 933 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 934 if (error || newp == NULL) 935 return (error); 936 937 return (dosetrlimit(l, ptmp, limitno, &alim)); 938 } 939 940 /* 941 * and finally, the actually glue that sticks it to the tree 942 */ 943 SYSCTL_SETUP(sysctl_proc_setup, "sysctl proc subtree setup") 944 { 945 946 sysctl_createv(clog, 0, NULL, NULL, 947 CTLFLAG_PERMANENT, 948 CTLTYPE_NODE, "proc", NULL, 949 NULL, 0, NULL, 0, 950 CTL_PROC, CTL_EOL); 951 sysctl_createv(clog, 0, NULL, NULL, 952 CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER, 953 CTLTYPE_NODE, "curproc", 954 SYSCTL_DESCR("Per-process settings"), 955 NULL, 0, NULL, 0, 956 CTL_PROC, PROC_CURPROC, CTL_EOL); 957 958 sysctl_createv(clog, 0, NULL, NULL, 959 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, 960 CTLTYPE_STRING, "corename", 961 SYSCTL_DESCR("Core file name"), 962 sysctl_proc_corename, 0, NULL, MAXPATHLEN, 963 CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL); 964 sysctl_createv(clog, 0, NULL, NULL, 965 CTLFLAG_PERMANENT, 966 CTLTYPE_NODE, "rlimit", 967 SYSCTL_DESCR("Process limits"), 968 NULL, 0, NULL, 0, 969 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL); 970 971 #define create_proc_plimit(s, n) do { \ 972 sysctl_createv(clog, 0, NULL, NULL, \ 973 CTLFLAG_PERMANENT, \ 974 CTLTYPE_NODE, s, \ 975 SYSCTL_DESCR("Process " s " limits"), \ 976 NULL, 0, NULL, 0, \ 977 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \ 978 CTL_EOL); \ 979 sysctl_createv(clog, 0, NULL, NULL, \ 980 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \ 981 CTLTYPE_QUAD, "soft", \ 982 SYSCTL_DESCR("Process soft " s " limit"), \ 983 sysctl_proc_plimit, 0, NULL, 0, \ 984 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \ 985 PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL); \ 986 sysctl_createv(clog, 0, NULL, NULL, \ 987 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \ 988 CTLTYPE_QUAD, "hard", \ 989 SYSCTL_DESCR("Process hard " s " limit"), \ 990 sysctl_proc_plimit, 0, NULL, 0, \ 991 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \ 992 PROC_PID_LIMIT_TYPE_HARD, CTL_EOL); \ 993 } while (0/*CONSTCOND*/) 994 995 create_proc_plimit("cputime", PROC_PID_LIMIT_CPU); 996 create_proc_plimit("filesize", PROC_PID_LIMIT_FSIZE); 997 create_proc_plimit("datasize", PROC_PID_LIMIT_DATA); 998 create_proc_plimit("stacksize", PROC_PID_LIMIT_STACK); 999 create_proc_plimit("coredumpsize", PROC_PID_LIMIT_CORE); 1000 create_proc_plimit("memoryuse", PROC_PID_LIMIT_RSS); 1001 create_proc_plimit("memorylocked", PROC_PID_LIMIT_MEMLOCK); 1002 create_proc_plimit("maxproc", PROC_PID_LIMIT_NPROC); 1003 create_proc_plimit("descriptors", PROC_PID_LIMIT_NOFILE); 1004 create_proc_plimit("sbsize", PROC_PID_LIMIT_SBSIZE); 1005 1006 #undef create_proc_plimit 1007 1008 sysctl_createv(clog, 0, NULL, NULL, 1009 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, 1010 CTLTYPE_INT, "stopfork", 1011 SYSCTL_DESCR("Stop process at fork(2)"), 1012 sysctl_proc_stop, 0, NULL, 0, 1013 CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL); 1014 sysctl_createv(clog, 0, NULL, NULL, 1015 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, 1016 CTLTYPE_INT, "stopexec", 1017 SYSCTL_DESCR("Stop process at execve(2)"), 1018 sysctl_proc_stop, 0, NULL, 0, 1019 CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL); 1020 sysctl_createv(clog, 0, NULL, NULL, 1021 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, 1022 CTLTYPE_INT, "stopexit", 1023 SYSCTL_DESCR("Stop process before completing exit"), 1024 sysctl_proc_stop, 0, NULL, 0, 1025 CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL); 1026 } 1027