xref: /dragonfly/sys/kern/kern_resource.c (revision 2d8a3be7)
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.15 2003/11/03 15:57:33 daver 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(p, up, sp, ip)
433 	struct proc *p;
434 	struct timeval *up;
435 	struct timeval *sp;
436 	struct timeval *ip;
437 {
438 	struct thread *td = p->p_thread;
439 	int s;
440 
441 	/*
442 	 * Calculate at the statclock level.  YYY if the thread is owned by
443 	 * another cpu we need to forward the request to the other cpu, or
444 	 * have a token to interlock the information.
445 	 */
446 	s = splstatclock();
447 	up->tv_sec = td->td_uticks / 1000000;
448 	up->tv_usec = td->td_uticks % 1000000;
449 	sp->tv_sec = td->td_sticks / 1000000;
450 	sp->tv_usec = td->td_sticks % 1000000;
451 	if (ip != NULL) {
452 		ip->tv_sec = td->td_iticks / 1000000;
453 		ip->tv_usec = td->td_iticks % 1000000;
454 	}
455 	splx(s);
456 }
457 
458 /* ARGSUSED */
459 int
460 getrusage(struct getrusage_args *uap)
461 {
462 	struct proc *p = curproc;
463 	struct rusage *rup;
464 
465 	switch (uap->who) {
466 
467 	case RUSAGE_SELF:
468 		rup = &p->p_stats->p_ru;
469 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
470 		break;
471 
472 	case RUSAGE_CHILDREN:
473 		rup = &p->p_stats->p_cru;
474 		break;
475 
476 	default:
477 		return (EINVAL);
478 	}
479 	return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
480 	    sizeof (struct rusage)));
481 }
482 
483 void
484 ruadd(ru, ru2)
485 	struct rusage *ru, *ru2;
486 {
487 	long *ip, *ip2;
488 	int i;
489 
490 	timevaladd(&ru->ru_utime, &ru2->ru_utime);
491 	timevaladd(&ru->ru_stime, &ru2->ru_stime);
492 	if (ru->ru_maxrss < ru2->ru_maxrss)
493 		ru->ru_maxrss = ru2->ru_maxrss;
494 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
495 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
496 		*ip++ += *ip2++;
497 }
498 
499 /*
500  * Make a copy of the plimit structure.
501  * We share these structures copy-on-write after fork,
502  * and copy when a limit is changed.
503  */
504 struct plimit *
505 limcopy(lim)
506 	struct plimit *lim;
507 {
508 	struct plimit *copy;
509 
510 	MALLOC(copy, struct plimit *, sizeof(struct plimit),
511 	    M_SUBPROC, M_WAITOK);
512 	bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit));
513 	copy->p_lflags = 0;
514 	copy->p_refcnt = 1;
515 	return (copy);
516 }
517 
518 /*
519  * Find the uidinfo structure for a uid.  This structure is used to
520  * track the total resource consumption (process count, socket buffer
521  * size, etc.) for the uid and impose limits.
522  */
523 void
524 uihashinit()
525 {
526 	uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
527 }
528 
529 static struct uidinfo *
530 uilookup(uid)
531 	uid_t uid;
532 {
533 	struct	uihashhead *uipp;
534 	struct	uidinfo *uip;
535 
536 	uipp = UIHASH(uid);
537 	LIST_FOREACH(uip, uipp, ui_hash)
538 		if (uip->ui_uid == uid)
539 			break;
540 
541 	return (uip);
542 }
543 
544 static struct uidinfo *
545 uicreate(uid)
546 	uid_t uid;
547 {
548 	struct	uidinfo *uip, *norace;
549 
550 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_NOWAIT);
551 	if (uip == NULL) {
552 		MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK);
553 		/*
554 		 * if we M_WAITOK we must look afterwards or risk
555 		 * redundant entries
556 		 */
557 		norace = uilookup(uid);
558 		if (norace != NULL) {
559 			FREE(uip, M_UIDINFO);
560 			return (norace);
561 		}
562 	}
563 	LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
564 	uip->ui_uid = uid;
565 	uip->ui_proccnt = 0;
566 	uip->ui_sbsize = 0;
567 	uip->ui_ref = 0;
568 	return (uip);
569 }
570 
571 struct uidinfo *
572 uifind(uid)
573 	uid_t uid;
574 {
575 	struct	uidinfo *uip;
576 
577 	uip = uilookup(uid);
578 	if (uip == NULL)
579 		uip = uicreate(uid);
580 	uip->ui_ref++;
581 	return (uip);
582 }
583 
584 int
585 uifree(uip)
586 	struct	uidinfo *uip;
587 {
588 
589 	if (--uip->ui_ref == 0) {
590 		if (uip->ui_sbsize != 0)
591 			/* XXX no %qd in kernel.  Truncate. */
592 			printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
593 			    uip->ui_uid, (long)uip->ui_sbsize);
594 		if (uip->ui_proccnt != 0)
595 			printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
596 			    uip->ui_uid, uip->ui_proccnt);
597 		LIST_REMOVE(uip, ui_hash);
598 		FREE(uip, M_UIDINFO);
599 		return (1);
600 	}
601 	return (0);
602 }
603 
604 /*
605  * Change the count associated with number of processes
606  * a given user is using.  When 'max' is 0, don't enforce a limit
607  */
608 int
609 chgproccnt(uip, diff, max)
610 	struct	uidinfo	*uip;
611 	int	diff;
612 	int	max;
613 {
614 	/* don't allow them to exceed max, but allow subtraction */
615 	if (diff > 0 && uip->ui_proccnt + diff > max && max != 0)
616 		return (0);
617 	uip->ui_proccnt += diff;
618 	if (uip->ui_proccnt < 0)
619 		printf("negative proccnt for uid = %d\n", uip->ui_uid);
620 	return (1);
621 }
622 
623 /*
624  * Change the total socket buffer size a user has used.
625  */
626 int
627 chgsbsize(uip, hiwat, to, max)
628 	struct	uidinfo	*uip;
629 	u_long *hiwat;
630 	u_long	to;
631 	rlim_t	max;
632 {
633 	rlim_t new;
634 	int s;
635 
636 	s = splnet();
637 	new = uip->ui_sbsize + to - *hiwat;
638 	/* don't allow them to exceed max, but allow subtraction */
639 	if (to > *hiwat && new > max) {
640 		splx(s);
641 		return (0);
642 	}
643 	uip->ui_sbsize = new;
644 	*hiwat = to;
645 	if (uip->ui_sbsize < 0)
646 		printf("negative sbsize for uid = %d\n", uip->ui_uid);
647 	splx(s);
648 	return (1);
649 }
650