xref: /dragonfly/sys/kern/kern_resource.c (revision 4caa7869)
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.17 2003/11/05 23:26:20 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 
56 #include <vm/vm.h>
57 #include <vm/vm_param.h>
58 #include <sys/lock.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_map.h>
61 
62 static int donice (struct proc *chgp, int n);
63 
64 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
65 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
66 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
67 static u_long uihash;		/* size of hash table - 1 */
68 
69 static struct uidinfo	*uicreate (uid_t uid);
70 static struct uidinfo	*uilookup (uid_t uid);
71 
72 /*
73  * Resource controls and accounting.
74  */
75 
76 int
77 getpriority(struct getpriority_args *uap)
78 {
79 	struct proc *curp = curproc;
80 	struct proc *p;
81 	int low = PRIO_MAX + 1;
82 
83 	switch (uap->which) {
84 	case PRIO_PROCESS:
85 		if (uap->who == 0)
86 			p = curp;
87 		else
88 			p = pfind(uap->who);
89 		if (p == 0)
90 			break;
91 		if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
92 			break;
93 		low = p->p_nice;
94 		break;
95 
96 	case PRIO_PGRP:
97 	{
98 		struct pgrp *pg;
99 
100 		if (uap->who == 0)
101 			pg = curp->p_pgrp;
102 		else if ((pg = pgfind(uap->who)) == NULL)
103 			break;
104 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
105 			if ((PRISON_CHECK(curp->p_ucred, p->p_ucred) && p->p_nice < low))
106 				low = p->p_nice;
107 		}
108 		break;
109 	}
110 	case PRIO_USER:
111 		if (uap->who == 0)
112 			uap->who = curp->p_ucred->cr_uid;
113 		FOREACH_PROC_IN_SYSTEM(p)
114 			if (PRISON_CHECK(curp->p_ucred, p->p_ucred) &&
115 			    p->p_ucred->cr_uid == uap->who &&
116 			    p->p_nice < low)
117 				low = p->p_nice;
118 		break;
119 
120 	default:
121 		return (EINVAL);
122 	}
123 	if (low == PRIO_MAX + 1)
124 		return (ESRCH);
125 	uap->sysmsg_result = low;
126 	return (0);
127 }
128 
129 /* ARGSUSED */
130 int
131 setpriority(struct setpriority_args *uap)
132 {
133 	struct proc *curp = curproc;
134 	struct proc *p;
135 	int found = 0, error = 0;
136 
137 	switch (uap->which) {
138 
139 	case PRIO_PROCESS:
140 		if (uap->who == 0)
141 			p = curp;
142 		else
143 			p = pfind(uap->who);
144 		if (p == 0)
145 			break;
146 		if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
147 			break;
148 		error = donice(p, uap->prio);
149 		found++;
150 		break;
151 
152 	case PRIO_PGRP:
153 	{
154 		struct pgrp *pg;
155 
156 		if (uap->who == 0)
157 			pg = curp->p_pgrp;
158 		else if ((pg = pgfind(uap->who)) == NULL)
159 			break;
160 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
161 			if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
162 				error = donice(p, uap->prio);
163 				found++;
164 			}
165 		}
166 		break;
167 	}
168 	case PRIO_USER:
169 		if (uap->who == 0)
170 			uap->who = curp->p_ucred->cr_uid;
171 		FOREACH_PROC_IN_SYSTEM(p)
172 			if (p->p_ucred->cr_uid == uap->who &&
173 			    PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
174 				error = donice(p, uap->prio);
175 				found++;
176 			}
177 		break;
178 
179 	default:
180 		return (EINVAL);
181 	}
182 	if (found == 0)
183 		return (ESRCH);
184 	return (error);
185 }
186 
187 static int
188 donice(struct proc *chgp, int n)
189 {
190 	struct proc *curp = curproc;
191 	struct ucred *cr = curp->p_ucred;
192 
193 	if (cr->cr_uid && cr->cr_ruid &&
194 	    cr->cr_uid != chgp->p_ucred->cr_uid &&
195 	    cr->cr_ruid != chgp->p_ucred->cr_uid)
196 		return (EPERM);
197 	if (n > PRIO_MAX)
198 		n = PRIO_MAX;
199 	if (n < PRIO_MIN)
200 		n = PRIO_MIN;
201 	if (n < chgp->p_nice && suser_cred(cr, 0))
202 		return (EACCES);
203 	chgp->p_nice = n;
204 	(void)resetpriority(chgp);
205 	return (0);
206 }
207 
208 /*
209  * Set realtime priority
210  */
211 /* ARGSUSED */
212 int
213 rtprio(struct rtprio_args *uap)
214 {
215 	struct proc *curp = curproc;
216 	struct proc *p;
217 	struct ucred *cr = curp->p_ucred;
218 	struct rtprio rtp;
219 	int error;
220 
221 	error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
222 	if (error)
223 		return (error);
224 
225 	if (uap->pid == 0)
226 		p = curp;
227 	else
228 		p = pfind(uap->pid);
229 
230 	if (p == 0)
231 		return (ESRCH);
232 
233 	switch (uap->function) {
234 	case RTP_LOOKUP:
235 		return (copyout(&p->p_rtprio, uap->rtp, sizeof(struct rtprio)));
236 	case RTP_SET:
237 		if (cr->cr_uid && cr->cr_ruid &&
238 		    cr->cr_uid != p->p_ucred->cr_uid &&
239 		    cr->cr_ruid != p->p_ucred->cr_uid)
240 		        return (EPERM);
241 		/* disallow setting rtprio in most cases if not superuser */
242 		if (suser_cred(cr, 0)) {
243 			/* can't set someone else's */
244 			if (uap->pid)
245 				return (EPERM);
246 			/* can't set realtime priority */
247 /*
248  * Realtime priority has to be restricted for reasons which should be
249  * obvious. However, for idle priority, there is a potential for
250  * system deadlock if an idleprio process gains a lock on a resource
251  * that other processes need (and the idleprio process can't run
252  * due to a CPU-bound normal process). Fix me! XXX
253  */
254  			if (RTP_PRIO_IS_REALTIME(rtp.type))
255 				return (EPERM);
256 		}
257 		switch (rtp.type) {
258 #ifdef RTP_PRIO_FIFO
259 		case RTP_PRIO_FIFO:
260 #endif
261 		case RTP_PRIO_REALTIME:
262 		case RTP_PRIO_NORMAL:
263 		case RTP_PRIO_IDLE:
264 			if (rtp.prio > RTP_PRIO_MAX)
265 				return (EINVAL);
266 			p->p_rtprio = rtp;
267 			return (0);
268 		default:
269 			return (EINVAL);
270 		}
271 
272 	default:
273 		return (EINVAL);
274 	}
275 }
276 
277 int
278 setrlimit(struct __setrlimit_args *uap)
279 {
280 	struct rlimit alim;
281 	int error;
282 
283 	error = copyin(uap->rlp, &alim, sizeof(alim));
284 	if (error)
285 		return (error);
286 
287 	error = kern_setrlimit(uap->which, &alim);
288 
289 	return (error);
290 }
291 
292 int
293 kern_setrlimit(u_int which, struct rlimit *limp)
294 {
295 	struct proc *p = curproc;
296 	struct rlimit *alimp;
297 	int error;
298 
299 	if (which >= RLIM_NLIMITS)
300 		return (EINVAL);
301 	alimp = &p->p_rlimit[which];
302 
303 	/*
304 	 * Preserve historical bugs by treating negative limits as unsigned.
305 	 */
306 	if (limp->rlim_cur < 0)
307 		limp->rlim_cur = RLIM_INFINITY;
308 	if (limp->rlim_max < 0)
309 		limp->rlim_max = RLIM_INFINITY;
310 
311 	if (limp->rlim_cur > alimp->rlim_max ||
312 	    limp->rlim_max > alimp->rlim_max)
313 		if ((error = suser_cred(p->p_ucred, PRISON_ROOT)))
314 			return (error);
315 	if (limp->rlim_cur > limp->rlim_max)
316 		limp->rlim_cur = limp->rlim_max;
317 	if (p->p_limit->p_refcnt > 1 &&
318 	    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
319 		p->p_limit->p_refcnt--;
320 		p->p_limit = limcopy(p->p_limit);
321 		alimp = &p->p_rlimit[which];
322 	}
323 
324 	switch (which) {
325 
326 	case RLIMIT_CPU:
327 		if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000)
328 			p->p_limit->p_cpulimit = RLIM_INFINITY;
329 		else
330 			p->p_limit->p_cpulimit =
331 			    (rlim_t)1000000 * limp->rlim_cur;
332 		break;
333 	case RLIMIT_DATA:
334 		if (limp->rlim_cur > maxdsiz)
335 			limp->rlim_cur = maxdsiz;
336 		if (limp->rlim_max > maxdsiz)
337 			limp->rlim_max = maxdsiz;
338 		break;
339 
340 	case RLIMIT_STACK:
341 		if (limp->rlim_cur > maxssiz)
342 			limp->rlim_cur = maxssiz;
343 		if (limp->rlim_max > maxssiz)
344 			limp->rlim_max = maxssiz;
345 		/*
346 		 * Stack is allocated to the max at exec time with only
347 		 * "rlim_cur" bytes accessible.  If stack limit is going
348 		 * up make more accessible, if going down make inaccessible.
349 		 */
350 		if (limp->rlim_cur != alimp->rlim_cur) {
351 			vm_offset_t addr;
352 			vm_size_t size;
353 			vm_prot_t prot;
354 
355 			if (limp->rlim_cur > alimp->rlim_cur) {
356 				prot = VM_PROT_ALL;
357 				size = limp->rlim_cur - alimp->rlim_cur;
358 				addr = USRSTACK - limp->rlim_cur;
359 			} else {
360 				prot = VM_PROT_NONE;
361 				size = alimp->rlim_cur - limp->rlim_cur;
362 				addr = USRSTACK - alimp->rlim_cur;
363 			}
364 			addr = trunc_page(addr);
365 			size = round_page(size);
366 			(void) vm_map_protect(&p->p_vmspace->vm_map,
367 					      addr, addr+size, prot, FALSE);
368 		}
369 		break;
370 
371 	case RLIMIT_NOFILE:
372 		if (limp->rlim_cur > maxfilesperproc)
373 			limp->rlim_cur = maxfilesperproc;
374 		if (limp->rlim_max > maxfilesperproc)
375 			limp->rlim_max = maxfilesperproc;
376 		break;
377 
378 	case RLIMIT_NPROC:
379 		if (limp->rlim_cur > maxprocperuid)
380 			limp->rlim_cur = maxprocperuid;
381 		if (limp->rlim_max > maxprocperuid)
382 			limp->rlim_max = maxprocperuid;
383 		if (limp->rlim_cur < 1)
384 			limp->rlim_cur = 1;
385 		if (limp->rlim_max < 1)
386 			limp->rlim_max = 1;
387 		break;
388 	}
389 	*alimp = *limp;
390 	return (0);
391 }
392 
393 /*
394  * The rlimit indexed by which is returned in the second argument.
395  */
396 int
397 kern_getrlimit(u_int which, struct rlimit *limp)
398 {
399 	struct thread *td = curthread;
400 	struct proc *p = td->td_proc;
401 
402 	if (which >= RLIM_NLIMITS)
403 		return (EINVAL);
404 
405 	*limp = p->p_rlimit[which];
406 
407 	return (0);
408 }
409 
410 int
411 getrlimit(struct __getrlimit_args *uap)
412 {
413 	struct rlimit lim;
414 	int error;
415 
416 	error = kern_getrlimit(uap->which, &lim);
417 
418 	if (error == 0)
419 		error = copyout(&lim, uap->rlp, sizeof(*uap->rlp));
420 	return error;
421 }
422 
423 /*
424  * Transform the running time and tick information in proc p into user,
425  * system, and interrupt time usage.
426  *
427  * Since we are limited to statclock tick granularity this is a statisical
428  * calculation which will be correct over the long haul, but should not be
429  * expected to measure fine grained deltas.
430  */
431 void
432 calcru(struct proc *p, struct timeval *up, struct timeval *sp,
433 	struct timeval *ip)
434 {
435 	struct thread *td = p->p_thread;
436 	int s;
437 
438 	/*
439 	 * Calculate at the statclock level.  YYY if the thread is owned by
440 	 * another cpu we need to forward the request to the other cpu, or
441 	 * have a token to interlock the information.
442 	 */
443 	s = splstatclock();
444 	up->tv_sec = td->td_uticks / 1000000;
445 	up->tv_usec = td->td_uticks % 1000000;
446 	sp->tv_sec = td->td_sticks / 1000000;
447 	sp->tv_usec = td->td_sticks % 1000000;
448 	if (ip != NULL) {
449 		ip->tv_sec = td->td_iticks / 1000000;
450 		ip->tv_usec = td->td_iticks % 1000000;
451 	}
452 	splx(s);
453 }
454 
455 /* ARGSUSED */
456 int
457 getrusage(struct getrusage_args *uap)
458 {
459 	struct proc *p = curproc;
460 	struct rusage *rup;
461 
462 	switch (uap->who) {
463 
464 	case RUSAGE_SELF:
465 		rup = &p->p_stats->p_ru;
466 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
467 		break;
468 
469 	case RUSAGE_CHILDREN:
470 		rup = &p->p_stats->p_cru;
471 		break;
472 
473 	default:
474 		return (EINVAL);
475 	}
476 	return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
477 	    sizeof (struct rusage)));
478 }
479 
480 void
481 ruadd(struct rusage *ru, struct rusage *ru2)
482 {
483 	long *ip, *ip2;
484 	int i;
485 
486 	timevaladd(&ru->ru_utime, &ru2->ru_utime);
487 	timevaladd(&ru->ru_stime, &ru2->ru_stime);
488 	if (ru->ru_maxrss < ru2->ru_maxrss)
489 		ru->ru_maxrss = ru2->ru_maxrss;
490 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
491 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
492 		*ip++ += *ip2++;
493 }
494 
495 /*
496  * Make a copy of the plimit structure.
497  * We share these structures copy-on-write after fork,
498  * and copy when a limit is changed.
499  */
500 struct plimit *
501 limcopy(struct plimit *lim)
502 {
503 	struct plimit *copy;
504 
505 	MALLOC(copy, struct plimit *, sizeof(struct plimit),
506 	    M_SUBPROC, M_WAITOK);
507 	bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit));
508 	copy->p_lflags = 0;
509 	copy->p_refcnt = 1;
510 	return (copy);
511 }
512 
513 /*
514  * Find the uidinfo structure for a uid.  This structure is used to
515  * track the total resource consumption (process count, socket buffer
516  * size, etc.) for the uid and impose limits.
517  */
518 void
519 uihashinit(void)
520 {
521 	uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
522 }
523 
524 static struct uidinfo *
525 uilookup(uid_t uid)
526 {
527 	struct	uihashhead *uipp;
528 	struct	uidinfo *uip;
529 
530 	uipp = UIHASH(uid);
531 	LIST_FOREACH(uip, uipp, ui_hash) {
532 		if (uip->ui_uid == uid)
533 			break;
534 	}
535 	return (uip);
536 }
537 
538 static struct uidinfo *
539 uicreate(uid_t uid)
540 {
541 	struct	uidinfo *uip, *norace;
542 
543 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_NOWAIT);
544 	if (uip == NULL) {
545 		MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK);
546 		/*
547 		 * if we M_WAITOK we must look afterwards or risk
548 		 * redundant entries
549 		 */
550 		norace = uilookup(uid);
551 		if (norace != NULL) {
552 			FREE(uip, M_UIDINFO);
553 			return (norace);
554 		}
555 	}
556 	LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
557 	uip->ui_uid = uid;
558 	uip->ui_proccnt = 0;
559 	uip->ui_sbsize = 0;
560 	uip->ui_ref = 0;
561 	varsymset_init(&uip->ui_varsymset, NULL);
562 	return (uip);
563 }
564 
565 struct uidinfo *
566 uifind(uid_t uid)
567 {
568 	struct	uidinfo *uip;
569 
570 	uip = uilookup(uid);
571 	if (uip == NULL)
572 		uip = uicreate(uid);
573 	uip->ui_ref++;
574 	return (uip);
575 }
576 
577 static __inline void
578 uifree(struct uidinfo *uip)
579 {
580 	if (uip->ui_sbsize != 0)
581 		/* XXX no %qd in kernel.  Truncate. */
582 		printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
583 		    uip->ui_uid, (long)uip->ui_sbsize);
584 	if (uip->ui_proccnt != 0)
585 		printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
586 		    uip->ui_uid, uip->ui_proccnt);
587 	LIST_REMOVE(uip, ui_hash);
588 	varsymset_clean(&uip->ui_varsymset);
589 	FREE(uip, M_UIDINFO);
590 }
591 
592 void
593 uihold(struct uidinfo *uip)
594 {
595 	++uip->ui_ref;
596 	KKASSERT(uip->ui_ref > 0);
597 }
598 
599 void
600 uidrop(struct uidinfo *uip)
601 {
602 	KKASSERT(uip->ui_ref > 0);
603 	if (--uip->ui_ref == 0)
604 		uifree(uip);
605 }
606 
607 void
608 uireplace(struct uidinfo **puip, struct uidinfo *nuip)
609 {
610 	uidrop(*puip);
611 	*puip = nuip;
612 }
613 
614 /*
615  * Change the count associated with number of processes
616  * a given user is using.  When 'max' is 0, don't enforce a limit
617  */
618 int
619 chgproccnt(struct uidinfo *uip, int diff, int max)
620 {
621 	/* don't allow them to exceed max, but allow subtraction */
622 	if (diff > 0 && uip->ui_proccnt + diff > max && max != 0)
623 		return (0);
624 	uip->ui_proccnt += diff;
625 	if (uip->ui_proccnt < 0)
626 		printf("negative proccnt for uid = %d\n", uip->ui_uid);
627 	return (1);
628 }
629 
630 /*
631  * Change the total socket buffer size a user has used.
632  */
633 int
634 chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t max)
635 {
636 	rlim_t new;
637 	int s;
638 
639 	s = splnet();
640 	new = uip->ui_sbsize + to - *hiwat;
641 	/* don't allow them to exceed max, but allow subtraction */
642 	if (to > *hiwat && new > max) {
643 		splx(s);
644 		return (0);
645 	}
646 	uip->ui_sbsize = new;
647 	*hiwat = to;
648 	if (uip->ui_sbsize < 0)
649 		printf("negative sbsize for uid = %d\n", uip->ui_uid);
650 	splx(s);
651 	return (1);
652 }
653 
654