1 /*- 2 * Copyright (c) 1982, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94 39 * $FreeBSD: src/sys/kern/kern_resource.c,v 1.55.2.5 2001/11/03 01:41:08 ps Exp $ 40 * $DragonFly: src/sys/kern/kern_resource.c,v 1.25 2006/03/23 15:21:41 drhodus Exp $ 41 */ 42 43 #include "opt_compat.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/sysproto.h> 48 #include <sys/file.h> 49 #include <sys/kern_syscall.h> 50 #include <sys/kernel.h> 51 #include <sys/resourcevar.h> 52 #include <sys/malloc.h> 53 #include <sys/proc.h> 54 #include <sys/time.h> 55 #include <sys/lockf.h> 56 57 #include <vm/vm.h> 58 #include <vm/vm_param.h> 59 #include <sys/lock.h> 60 #include <vm/pmap.h> 61 #include <vm/vm_map.h> 62 63 #include <sys/thread2.h> 64 65 static int donice (struct proc *chgp, int n); 66 67 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures"); 68 #define UIHASH(uid) (&uihashtbl[(uid) & uihash]) 69 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl; 70 static u_long uihash; /* size of hash table - 1 */ 71 72 static struct uidinfo *uicreate (uid_t uid); 73 static struct uidinfo *uilookup (uid_t uid); 74 75 /* 76 * Resource controls and accounting. 77 */ 78 79 int 80 getpriority(struct getpriority_args *uap) 81 { 82 struct proc *curp = curproc; 83 struct proc *p; 84 int low = PRIO_MAX + 1; 85 86 switch (uap->which) { 87 case PRIO_PROCESS: 88 if (uap->who == 0) 89 p = curp; 90 else 91 p = pfind(uap->who); 92 if (p == 0) 93 break; 94 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred)) 95 break; 96 low = p->p_nice; 97 break; 98 99 case PRIO_PGRP: 100 { 101 struct pgrp *pg; 102 103 if (uap->who == 0) 104 pg = curp->p_pgrp; 105 else if ((pg = pgfind(uap->who)) == NULL) 106 break; 107 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 108 if ((PRISON_CHECK(curp->p_ucred, p->p_ucred) && p->p_nice < low)) 109 low = p->p_nice; 110 } 111 break; 112 } 113 case PRIO_USER: 114 if (uap->who == 0) 115 uap->who = curp->p_ucred->cr_uid; 116 FOREACH_PROC_IN_SYSTEM(p) 117 if (PRISON_CHECK(curp->p_ucred, p->p_ucred) && 118 p->p_ucred->cr_uid == uap->who && 119 p->p_nice < low) 120 low = p->p_nice; 121 break; 122 123 default: 124 return (EINVAL); 125 } 126 if (low == PRIO_MAX + 1) 127 return (ESRCH); 128 uap->sysmsg_result = low; 129 return (0); 130 } 131 132 /* ARGSUSED */ 133 int 134 setpriority(struct setpriority_args *uap) 135 { 136 struct proc *curp = curproc; 137 struct proc *p; 138 int found = 0, error = 0; 139 140 switch (uap->which) { 141 142 case PRIO_PROCESS: 143 if (uap->who == 0) 144 p = curp; 145 else 146 p = pfind(uap->who); 147 if (p == 0) 148 break; 149 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred)) 150 break; 151 error = donice(p, uap->prio); 152 found++; 153 break; 154 155 case PRIO_PGRP: 156 { 157 struct pgrp *pg; 158 159 if (uap->who == 0) 160 pg = curp->p_pgrp; 161 else if ((pg = pgfind(uap->who)) == NULL) 162 break; 163 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 164 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) { 165 error = donice(p, uap->prio); 166 found++; 167 } 168 } 169 break; 170 } 171 case PRIO_USER: 172 if (uap->who == 0) 173 uap->who = curp->p_ucred->cr_uid; 174 FOREACH_PROC_IN_SYSTEM(p) 175 if (p->p_ucred->cr_uid == uap->who && 176 PRISON_CHECK(curp->p_ucred, p->p_ucred)) { 177 error = donice(p, uap->prio); 178 found++; 179 } 180 break; 181 182 default: 183 return (EINVAL); 184 } 185 if (found == 0) 186 return (ESRCH); 187 return (error); 188 } 189 190 static int 191 donice(struct proc *chgp, int n) 192 { 193 struct proc *curp = curproc; 194 struct ucred *cr = curp->p_ucred; 195 196 if (cr->cr_uid && cr->cr_ruid && 197 cr->cr_uid != chgp->p_ucred->cr_uid && 198 cr->cr_ruid != chgp->p_ucred->cr_uid) 199 return (EPERM); 200 if (n > PRIO_MAX) 201 n = PRIO_MAX; 202 if (n < PRIO_MIN) 203 n = PRIO_MIN; 204 if (n < chgp->p_nice && suser_cred(cr, 0)) 205 return (EACCES); 206 chgp->p_nice = n; 207 chgp->p_usched->resetpriority(&chgp->p_lwp); 208 return (0); 209 } 210 211 /* 212 * Set realtime priority 213 */ 214 /* ARGSUSED */ 215 int 216 rtprio(struct rtprio_args *uap) 217 { 218 struct proc *curp = curproc; 219 struct proc *p; 220 struct ucred *cr = curp->p_ucred; 221 struct rtprio rtp; 222 int error; 223 224 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio)); 225 if (error) 226 return (error); 227 228 if (uap->pid == 0) 229 p = curp; 230 else 231 p = pfind(uap->pid); 232 233 if (p == 0) 234 return (ESRCH); 235 236 switch (uap->function) { 237 case RTP_LOOKUP: 238 return (copyout(&p->p_lwp.lwp_rtprio, uap->rtp, sizeof(struct rtprio))); 239 case RTP_SET: 240 if (cr->cr_uid && cr->cr_ruid && 241 cr->cr_uid != p->p_ucred->cr_uid && 242 cr->cr_ruid != p->p_ucred->cr_uid) 243 return (EPERM); 244 /* disallow setting rtprio in most cases if not superuser */ 245 if (suser_cred(cr, 0)) { 246 /* can't set someone else's */ 247 if (uap->pid) 248 return (EPERM); 249 /* can't set realtime priority */ 250 /* 251 * Realtime priority has to be restricted for reasons which should be 252 * obvious. However, for idle priority, there is a potential for 253 * system deadlock if an idleprio process gains a lock on a resource 254 * that other processes need (and the idleprio process can't run 255 * due to a CPU-bound normal process). Fix me! XXX 256 */ 257 if (RTP_PRIO_IS_REALTIME(rtp.type)) 258 return (EPERM); 259 } 260 switch (rtp.type) { 261 #ifdef RTP_PRIO_FIFO 262 case RTP_PRIO_FIFO: 263 #endif 264 case RTP_PRIO_REALTIME: 265 case RTP_PRIO_NORMAL: 266 case RTP_PRIO_IDLE: 267 if (rtp.prio > RTP_PRIO_MAX) 268 return (EINVAL); 269 p->p_lwp.lwp_rtprio = rtp; 270 return (0); 271 default: 272 return (EINVAL); 273 } 274 275 default: 276 return (EINVAL); 277 } 278 } 279 280 int 281 setrlimit(struct __setrlimit_args *uap) 282 { 283 struct rlimit alim; 284 int error; 285 286 error = copyin(uap->rlp, &alim, sizeof(alim)); 287 if (error) 288 return (error); 289 290 error = kern_setrlimit(uap->which, &alim); 291 292 return (error); 293 } 294 295 int 296 kern_setrlimit(u_int which, struct rlimit *limp) 297 { 298 struct proc *p = curproc; 299 struct rlimit *alimp; 300 int error; 301 302 if (which >= RLIM_NLIMITS) 303 return (EINVAL); 304 alimp = &p->p_rlimit[which]; 305 306 /* 307 * Preserve historical bugs by treating negative limits as unsigned. 308 */ 309 if (limp->rlim_cur < 0) 310 limp->rlim_cur = RLIM_INFINITY; 311 if (limp->rlim_max < 0) 312 limp->rlim_max = RLIM_INFINITY; 313 314 if (limp->rlim_cur > alimp->rlim_max || 315 limp->rlim_max > alimp->rlim_max) 316 if ((error = suser_cred(p->p_ucred, PRISON_ROOT))) 317 return (error); 318 if (limp->rlim_cur > limp->rlim_max) 319 limp->rlim_cur = limp->rlim_max; 320 if (p->p_limit->p_refcnt > 1 && 321 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) { 322 p->p_limit->p_refcnt--; 323 p->p_limit = limcopy(p->p_limit); 324 alimp = &p->p_rlimit[which]; 325 } 326 327 switch (which) { 328 329 case RLIMIT_CPU: 330 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000) 331 p->p_limit->p_cpulimit = RLIM_INFINITY; 332 else 333 p->p_limit->p_cpulimit = 334 (rlim_t)1000000 * limp->rlim_cur; 335 break; 336 case RLIMIT_DATA: 337 if (limp->rlim_cur > maxdsiz) 338 limp->rlim_cur = maxdsiz; 339 if (limp->rlim_max > maxdsiz) 340 limp->rlim_max = maxdsiz; 341 break; 342 343 case RLIMIT_STACK: 344 if (limp->rlim_cur > maxssiz) 345 limp->rlim_cur = maxssiz; 346 if (limp->rlim_max > maxssiz) 347 limp->rlim_max = maxssiz; 348 /* 349 * Stack is allocated to the max at exec time with only 350 * "rlim_cur" bytes accessible. If stack limit is going 351 * up make more accessible, if going down make inaccessible. 352 */ 353 if (limp->rlim_cur != alimp->rlim_cur) { 354 vm_offset_t addr; 355 vm_size_t size; 356 vm_prot_t prot; 357 358 if (limp->rlim_cur > alimp->rlim_cur) { 359 prot = VM_PROT_ALL; 360 size = limp->rlim_cur - alimp->rlim_cur; 361 addr = USRSTACK - limp->rlim_cur; 362 } else { 363 prot = VM_PROT_NONE; 364 size = alimp->rlim_cur - limp->rlim_cur; 365 addr = USRSTACK - alimp->rlim_cur; 366 } 367 addr = trunc_page(addr); 368 size = round_page(size); 369 (void) vm_map_protect(&p->p_vmspace->vm_map, 370 addr, addr+size, prot, FALSE); 371 } 372 break; 373 374 case RLIMIT_NOFILE: 375 if (limp->rlim_cur > maxfilesperproc) 376 limp->rlim_cur = maxfilesperproc; 377 if (limp->rlim_max > maxfilesperproc) 378 limp->rlim_max = maxfilesperproc; 379 break; 380 381 case RLIMIT_NPROC: 382 if (limp->rlim_cur > maxprocperuid) 383 limp->rlim_cur = maxprocperuid; 384 if (limp->rlim_max > maxprocperuid) 385 limp->rlim_max = maxprocperuid; 386 if (limp->rlim_cur < 1) 387 limp->rlim_cur = 1; 388 if (limp->rlim_max < 1) 389 limp->rlim_max = 1; 390 break; 391 case RLIMIT_POSIXLOCKS: 392 if (limp->rlim_cur > maxposixlocksperuid) 393 limp->rlim_cur = maxposixlocksperuid; 394 if (limp->rlim_max > maxposixlocksperuid) 395 limp->rlim_max = maxposixlocksperuid; 396 break; 397 } 398 *alimp = *limp; 399 return (0); 400 } 401 402 /* 403 * The rlimit indexed by which is returned in the second argument. 404 * 405 * MP SAFE 406 */ 407 int 408 kern_getrlimit(u_int which, struct rlimit *limp) 409 { 410 struct thread *td = curthread; 411 struct proc *p = td->td_proc; 412 413 if (which >= RLIM_NLIMITS) 414 return (EINVAL); 415 416 *limp = p->p_rlimit[which]; 417 418 return (0); 419 } 420 421 int 422 getrlimit(struct __getrlimit_args *uap) 423 { 424 struct rlimit lim; 425 int error; 426 427 error = kern_getrlimit(uap->which, &lim); 428 429 if (error == 0) 430 error = copyout(&lim, uap->rlp, sizeof(*uap->rlp)); 431 return error; 432 } 433 434 /* 435 * Transform the running time and tick information in proc p into user, 436 * system, and interrupt time usage. 437 * 438 * Since we are limited to statclock tick granularity this is a statisical 439 * calculation which will be correct over the long haul, but should not be 440 * expected to measure fine grained deltas. 441 */ 442 void 443 calcru(struct proc *p, struct timeval *up, struct timeval *sp, 444 struct timeval *ip) 445 { 446 struct thread *td = p->p_thread; 447 448 /* 449 * Calculate at the statclock level. YYY if the thread is owned by 450 * another cpu we need to forward the request to the other cpu, or 451 * have a token to interlock the information. 452 */ 453 crit_enter(); 454 up->tv_sec = td->td_uticks / 1000000; 455 up->tv_usec = td->td_uticks % 1000000; 456 sp->tv_sec = td->td_sticks / 1000000; 457 sp->tv_usec = td->td_sticks % 1000000; 458 if (ip != NULL) { 459 ip->tv_sec = td->td_iticks / 1000000; 460 ip->tv_usec = td->td_iticks % 1000000; 461 } 462 crit_exit(); 463 } 464 465 /* ARGSUSED */ 466 int 467 getrusage(struct getrusage_args *uap) 468 { 469 struct proc *p = curproc; 470 struct rusage *rup; 471 472 switch (uap->who) { 473 474 case RUSAGE_SELF: 475 rup = &p->p_stats->p_ru; 476 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL); 477 break; 478 479 case RUSAGE_CHILDREN: 480 rup = &p->p_stats->p_cru; 481 break; 482 483 default: 484 return (EINVAL); 485 } 486 return (copyout((caddr_t)rup, (caddr_t)uap->rusage, 487 sizeof (struct rusage))); 488 } 489 490 void 491 ruadd(struct rusage *ru, struct rusage *ru2) 492 { 493 long *ip, *ip2; 494 int i; 495 496 timevaladd(&ru->ru_utime, &ru2->ru_utime); 497 timevaladd(&ru->ru_stime, &ru2->ru_stime); 498 if (ru->ru_maxrss < ru2->ru_maxrss) 499 ru->ru_maxrss = ru2->ru_maxrss; 500 ip = &ru->ru_first; ip2 = &ru2->ru_first; 501 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) 502 *ip++ += *ip2++; 503 } 504 505 /* 506 * Make a copy of the plimit structure. 507 * We share these structures copy-on-write after fork, 508 * and copy when a limit is changed. 509 */ 510 struct plimit * 511 limcopy(struct plimit *lim) 512 { 513 struct plimit *copy; 514 515 MALLOC(copy, struct plimit *, sizeof(struct plimit), 516 M_SUBPROC, M_WAITOK); 517 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit)); 518 copy->p_lflags = 0; 519 copy->p_refcnt = 1; 520 return (copy); 521 } 522 523 /* 524 * Find the uidinfo structure for a uid. This structure is used to 525 * track the total resource consumption (process count, socket buffer 526 * size, etc.) for the uid and impose limits. 527 */ 528 void 529 uihashinit(void) 530 { 531 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash); 532 } 533 534 static struct uidinfo * 535 uilookup(uid_t uid) 536 { 537 struct uihashhead *uipp; 538 struct uidinfo *uip; 539 540 uipp = UIHASH(uid); 541 LIST_FOREACH(uip, uipp, ui_hash) { 542 if (uip->ui_uid == uid) 543 break; 544 } 545 return (uip); 546 } 547 548 static struct uidinfo * 549 uicreate(uid_t uid) 550 { 551 struct uidinfo *uip, *norace; 552 553 /* 554 * Allocate space and check for a race 555 */ 556 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK); 557 norace = uilookup(uid); 558 if (norace != NULL) { 559 FREE(uip, M_UIDINFO); 560 return (norace); 561 } 562 563 /* 564 * Initialize structure and enter it into the hash table 565 */ 566 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash); 567 uip->ui_uid = uid; 568 uip->ui_proccnt = 0; 569 uip->ui_sbsize = 0; 570 uip->ui_ref = 0; 571 uip->ui_posixlocks = 0; 572 varsymset_init(&uip->ui_varsymset, NULL); 573 return (uip); 574 } 575 576 struct uidinfo * 577 uifind(uid_t uid) 578 { 579 struct uidinfo *uip; 580 581 uip = uilookup(uid); 582 if (uip == NULL) 583 uip = uicreate(uid); 584 uip->ui_ref++; 585 return (uip); 586 } 587 588 static __inline void 589 uifree(struct uidinfo *uip) 590 { 591 if (uip->ui_sbsize != 0) 592 /* XXX no %qd in kernel. Truncate. */ 593 printf("freeing uidinfo: uid = %d, sbsize = %ld\n", 594 uip->ui_uid, (long)uip->ui_sbsize); 595 if (uip->ui_proccnt != 0) 596 printf("freeing uidinfo: uid = %d, proccnt = %ld\n", 597 uip->ui_uid, uip->ui_proccnt); 598 LIST_REMOVE(uip, ui_hash); 599 varsymset_clean(&uip->ui_varsymset); 600 FREE(uip, M_UIDINFO); 601 } 602 603 void 604 uihold(struct uidinfo *uip) 605 { 606 ++uip->ui_ref; 607 KKASSERT(uip->ui_ref > 0); 608 } 609 610 void 611 uidrop(struct uidinfo *uip) 612 { 613 KKASSERT(uip->ui_ref > 0); 614 if (--uip->ui_ref == 0) 615 uifree(uip); 616 } 617 618 void 619 uireplace(struct uidinfo **puip, struct uidinfo *nuip) 620 { 621 uidrop(*puip); 622 *puip = nuip; 623 } 624 625 /* 626 * Change the count associated with number of processes 627 * a given user is using. When 'max' is 0, don't enforce a limit 628 */ 629 int 630 chgproccnt(struct uidinfo *uip, int diff, int max) 631 { 632 /* don't allow them to exceed max, but allow subtraction */ 633 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) 634 return (0); 635 uip->ui_proccnt += diff; 636 if (uip->ui_proccnt < 0) 637 printf("negative proccnt for uid = %d\n", uip->ui_uid); 638 return (1); 639 } 640 641 /* 642 * Change the total socket buffer size a user has used. 643 */ 644 int 645 chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t max) 646 { 647 rlim_t new; 648 649 crit_enter(); 650 new = uip->ui_sbsize + to - *hiwat; 651 /* don't allow them to exceed max, but allow subtraction */ 652 if (to > *hiwat && new > max) { 653 crit_exit(); 654 return (0); 655 } 656 uip->ui_sbsize = new; 657 *hiwat = to; 658 if (uip->ui_sbsize < 0) 659 printf("negative sbsize for uid = %d\n", uip->ui_uid); 660 crit_exit(); 661 return (1); 662 } 663 664