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