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.22 2005/06/25 20:03:28 dillon 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); 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_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_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 int 406 kern_getrlimit(u_int which, struct rlimit *limp) 407 { 408 struct thread *td = curthread; 409 struct proc *p = td->td_proc; 410 411 if (which >= RLIM_NLIMITS) 412 return (EINVAL); 413 414 *limp = p->p_rlimit[which]; 415 416 return (0); 417 } 418 419 int 420 getrlimit(struct __getrlimit_args *uap) 421 { 422 struct rlimit lim; 423 int error; 424 425 error = kern_getrlimit(uap->which, &lim); 426 427 if (error == 0) 428 error = copyout(&lim, uap->rlp, sizeof(*uap->rlp)); 429 return error; 430 } 431 432 /* 433 * Transform the running time and tick information in proc p into user, 434 * system, and interrupt time usage. 435 * 436 * Since we are limited to statclock tick granularity this is a statisical 437 * calculation which will be correct over the long haul, but should not be 438 * expected to measure fine grained deltas. 439 */ 440 void 441 calcru(struct proc *p, struct timeval *up, struct timeval *sp, 442 struct timeval *ip) 443 { 444 struct thread *td = p->p_thread; 445 446 /* 447 * Calculate at the statclock level. YYY if the thread is owned by 448 * another cpu we need to forward the request to the other cpu, or 449 * have a token to interlock the information. 450 */ 451 crit_enter(); 452 up->tv_sec = td->td_uticks / 1000000; 453 up->tv_usec = td->td_uticks % 1000000; 454 sp->tv_sec = td->td_sticks / 1000000; 455 sp->tv_usec = td->td_sticks % 1000000; 456 if (ip != NULL) { 457 ip->tv_sec = td->td_iticks / 1000000; 458 ip->tv_usec = td->td_iticks % 1000000; 459 } 460 crit_exit(); 461 } 462 463 /* ARGSUSED */ 464 int 465 getrusage(struct getrusage_args *uap) 466 { 467 struct proc *p = curproc; 468 struct rusage *rup; 469 470 switch (uap->who) { 471 472 case RUSAGE_SELF: 473 rup = &p->p_stats->p_ru; 474 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL); 475 break; 476 477 case RUSAGE_CHILDREN: 478 rup = &p->p_stats->p_cru; 479 break; 480 481 default: 482 return (EINVAL); 483 } 484 return (copyout((caddr_t)rup, (caddr_t)uap->rusage, 485 sizeof (struct rusage))); 486 } 487 488 void 489 ruadd(struct rusage *ru, struct rusage *ru2) 490 { 491 long *ip, *ip2; 492 int i; 493 494 timevaladd(&ru->ru_utime, &ru2->ru_utime); 495 timevaladd(&ru->ru_stime, &ru2->ru_stime); 496 if (ru->ru_maxrss < ru2->ru_maxrss) 497 ru->ru_maxrss = ru2->ru_maxrss; 498 ip = &ru->ru_first; ip2 = &ru2->ru_first; 499 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) 500 *ip++ += *ip2++; 501 } 502 503 /* 504 * Make a copy of the plimit structure. 505 * We share these structures copy-on-write after fork, 506 * and copy when a limit is changed. 507 */ 508 struct plimit * 509 limcopy(struct plimit *lim) 510 { 511 struct plimit *copy; 512 513 MALLOC(copy, struct plimit *, sizeof(struct plimit), 514 M_SUBPROC, M_WAITOK); 515 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit)); 516 copy->p_lflags = 0; 517 copy->p_refcnt = 1; 518 return (copy); 519 } 520 521 /* 522 * Find the uidinfo structure for a uid. This structure is used to 523 * track the total resource consumption (process count, socket buffer 524 * size, etc.) for the uid and impose limits. 525 */ 526 void 527 uihashinit(void) 528 { 529 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash); 530 } 531 532 static struct uidinfo * 533 uilookup(uid_t uid) 534 { 535 struct uihashhead *uipp; 536 struct uidinfo *uip; 537 538 uipp = UIHASH(uid); 539 LIST_FOREACH(uip, uipp, ui_hash) { 540 if (uip->ui_uid == uid) 541 break; 542 } 543 return (uip); 544 } 545 546 static struct uidinfo * 547 uicreate(uid_t uid) 548 { 549 struct uidinfo *uip, *norace; 550 551 /* 552 * Allocate space and check for a race 553 */ 554 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK); 555 norace = uilookup(uid); 556 if (norace != NULL) { 557 FREE(uip, M_UIDINFO); 558 return (norace); 559 } 560 561 /* 562 * Initialize structure and enter it into the hash table 563 */ 564 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash); 565 uip->ui_uid = uid; 566 uip->ui_proccnt = 0; 567 uip->ui_sbsize = 0; 568 uip->ui_ref = 0; 569 uip->ui_posixlocks = 0; 570 varsymset_init(&uip->ui_varsymset, NULL); 571 return (uip); 572 } 573 574 struct uidinfo * 575 uifind(uid_t uid) 576 { 577 struct uidinfo *uip; 578 579 uip = uilookup(uid); 580 if (uip == NULL) 581 uip = uicreate(uid); 582 uip->ui_ref++; 583 return (uip); 584 } 585 586 static __inline void 587 uifree(struct uidinfo *uip) 588 { 589 if (uip->ui_sbsize != 0) 590 /* XXX no %qd in kernel. Truncate. */ 591 printf("freeing uidinfo: uid = %d, sbsize = %ld\n", 592 uip->ui_uid, (long)uip->ui_sbsize); 593 if (uip->ui_proccnt != 0) 594 printf("freeing uidinfo: uid = %d, proccnt = %ld\n", 595 uip->ui_uid, uip->ui_proccnt); 596 LIST_REMOVE(uip, ui_hash); 597 varsymset_clean(&uip->ui_varsymset); 598 FREE(uip, M_UIDINFO); 599 } 600 601 void 602 uihold(struct uidinfo *uip) 603 { 604 ++uip->ui_ref; 605 KKASSERT(uip->ui_ref > 0); 606 } 607 608 void 609 uidrop(struct uidinfo *uip) 610 { 611 KKASSERT(uip->ui_ref > 0); 612 if (--uip->ui_ref == 0) 613 uifree(uip); 614 } 615 616 void 617 uireplace(struct uidinfo **puip, struct uidinfo *nuip) 618 { 619 uidrop(*puip); 620 *puip = nuip; 621 } 622 623 /* 624 * Change the count associated with number of processes 625 * a given user is using. When 'max' is 0, don't enforce a limit 626 */ 627 int 628 chgproccnt(struct uidinfo *uip, int diff, int max) 629 { 630 /* don't allow them to exceed max, but allow subtraction */ 631 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) 632 return (0); 633 uip->ui_proccnt += diff; 634 if (uip->ui_proccnt < 0) 635 printf("negative proccnt for uid = %d\n", uip->ui_uid); 636 return (1); 637 } 638 639 /* 640 * Change the total socket buffer size a user has used. 641 */ 642 int 643 chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t max) 644 { 645 rlim_t new; 646 647 crit_enter(); 648 new = uip->ui_sbsize + to - *hiwat; 649 /* don't allow them to exceed max, but allow subtraction */ 650 if (to > *hiwat && new > max) { 651 crit_exit(); 652 return (0); 653 } 654 uip->ui_sbsize = new; 655 *hiwat = to; 656 if (uip->ui_sbsize < 0) 657 printf("negative sbsize for uid = %d\n", uip->ui_uid); 658 crit_exit(); 659 return (1); 660 } 661 662