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