kern_resource.c revision 1.158 1 /* $NetBSD: kern_resource.c,v 1.158 2011/04/30 23:41:17 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.158 2011/04/30 23:41:17 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 limfree(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 limfree(struct plimit *lim)
703 {
704 struct plimit *sv_lim;
705
706 do {
707 if (atomic_dec_uint_nv(&lim->pl_refcnt) > 0)
708 return;
709 if (lim->pl_corename != defcorename)
710 free(lim->pl_corename, M_TEMP);
711 sv_lim = lim->pl_sv_limit;
712 mutex_destroy(&lim->pl_lock);
713 pool_cache_put(plimit_cache, lim);
714 } while ((lim = sv_lim) != NULL);
715 }
716
717 struct pstats *
718 pstatscopy(struct pstats *ps)
719 {
720
721 struct pstats *newps;
722
723 newps = pool_cache_get(pstats_cache, PR_WAITOK);
724
725 memset(&newps->pstat_startzero, 0,
726 (unsigned) ((char *)&newps->pstat_endzero -
727 (char *)&newps->pstat_startzero));
728 memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
729 ((char *)&newps->pstat_endcopy -
730 (char *)&newps->pstat_startcopy));
731
732 return (newps);
733
734 }
735
736 void
737 pstatsfree(struct pstats *ps)
738 {
739
740 pool_cache_put(pstats_cache, ps);
741 }
742
743 /*
744 * sysctl interface in five parts
745 */
746
747 /*
748 * sysctl_proc_findproc: a routine for sysctl proc subtree helpers that
749 * need to pick a valid process by PID.
750 *
751 * => Hold a reference on the process, on success.
752 */
753 static int
754 sysctl_proc_findproc(lwp_t *l, pid_t pid, proc_t **p2)
755 {
756 proc_t *p;
757 int error;
758
759 if (pid == PROC_CURPROC) {
760 p = l->l_proc;
761 } else {
762 mutex_enter(proc_lock);
763 p = proc_find(pid);
764 if (p == NULL) {
765 mutex_exit(proc_lock);
766 return ESRCH;
767 }
768 }
769 error = rw_tryenter(&p->p_reflock, RW_READER) ? 0 : EBUSY;
770 if (pid != PROC_CURPROC) {
771 mutex_exit(proc_lock);
772 }
773 *p2 = p;
774 return error;
775 }
776
777 /*
778 * sysctl_proc_corename: helper routine to get or set the core file name
779 * for a process specified by PID.
780 */
781 static int
782 sysctl_proc_corename(SYSCTLFN_ARGS)
783 {
784 struct proc *p;
785 struct plimit *lim;
786 char *cnbuf, *cname;
787 struct sysctlnode node;
788 size_t len;
789 int error;
790
791 /* First, validate the request. */
792 if (namelen != 0 || name[-1] != PROC_PID_CORENAME)
793 return EINVAL;
794
795 /* Find the process. Hold a reference (p_reflock), if found. */
796 error = sysctl_proc_findproc(l, (pid_t)name[-2], &p);
797 if (error)
798 return error;
799
800 /* XXX-elad */
801 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, p,
802 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
803 if (error) {
804 rw_exit(&p->p_reflock);
805 return error;
806 }
807
808 cnbuf = PNBUF_GET();
809
810 if (newp == NULL) {
811 /* Get case: copy the core name into the buffer. */
812 error = kauth_authorize_process(l->l_cred,
813 KAUTH_PROCESS_CORENAME, p,
814 KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_GET), NULL, NULL);
815 if (error) {
816 goto done;
817 }
818 lim = p->p_limit;
819 mutex_enter(&lim->pl_lock);
820 strlcpy(cnbuf, lim->pl_corename, MAXPATHLEN);
821 mutex_exit(&lim->pl_lock);
822 } else {
823 /* Set case: just use the temporary buffer. */
824 error = kauth_authorize_process(l->l_cred,
825 KAUTH_PROCESS_CORENAME, p,
826 KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_SET), cnbuf, NULL);
827 if (error) {
828 goto done;
829 }
830 }
831
832 node = *rnode;
833 node.sysctl_data = cnbuf;
834 error = sysctl_lookup(SYSCTLFN_CALL(&node));
835
836 /* Return if error, or if we are only retrieving the core name. */
837 if (error || newp == NULL) {
838 goto done;
839 }
840
841 /*
842 * Validate new core name. It must be either "core", "/core",
843 * or end in ".core".
844 */
845 len = strlen(cnbuf);
846 if ((len < 4 || strcmp(cnbuf + len - 4, "core") != 0) ||
847 (len > 4 && cnbuf[len - 5] != '/' && cnbuf[len - 5] != '.')) {
848 error = EINVAL;
849 goto done;
850 }
851
852 /* Allocate, copy and set the new core name for plimit structure. */
853 cname = malloc(++len, M_TEMP, M_WAITOK | M_CANFAIL);
854 if (cname == NULL) {
855 error = ENOMEM;
856 goto done;
857 }
858 memcpy(cname, cnbuf, len);
859
860 char *ocname;
861 lim_privatise(p, false);
862 lim = p->p_limit;
863 mutex_enter(&lim->pl_lock);
864 ocname = lim->pl_corename;
865 lim->pl_corename = cname;
866 mutex_exit(&lim->pl_lock);
867 if (ocname != defcorename)
868 free(ocname, M_TEMP);
869
870 done:
871 rw_exit(&p->p_reflock);
872 PNBUF_PUT(cnbuf);
873 return error;
874 }
875
876 /*
877 * sysctl helper routine for checking/setting a process's stop flags,
878 * one for fork and one for exec.
879 */
880 static int
881 sysctl_proc_stop(SYSCTLFN_ARGS)
882 {
883 struct proc *ptmp;
884 int i, f, error = 0;
885 struct sysctlnode node;
886
887 if (namelen != 0)
888 return (EINVAL);
889
890 /* Find the process. Hold a reference (p_reflock), if found. */
891 error = sysctl_proc_findproc(l, (pid_t)name[-2], &ptmp);
892 if (error)
893 return error;
894
895 /* XXX-elad */
896 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
897 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
898 if (error)
899 goto out;
900
901 switch (rnode->sysctl_num) {
902 case PROC_PID_STOPFORK:
903 f = PS_STOPFORK;
904 break;
905 case PROC_PID_STOPEXEC:
906 f = PS_STOPEXEC;
907 break;
908 case PROC_PID_STOPEXIT:
909 f = PS_STOPEXIT;
910 break;
911 default:
912 error = EINVAL;
913 goto out;
914 }
915
916 i = (ptmp->p_flag & f) ? 1 : 0;
917 node = *rnode;
918 node.sysctl_data = &i;
919 error = sysctl_lookup(SYSCTLFN_CALL(&node));
920 if (error || newp == NULL)
921 goto out;
922
923 mutex_enter(ptmp->p_lock);
924 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_STOPFLAG,
925 ptmp, KAUTH_ARG(f), NULL, NULL);
926 if (!error) {
927 if (i) {
928 ptmp->p_sflag |= f;
929 } else {
930 ptmp->p_sflag &= ~f;
931 }
932 }
933 mutex_exit(ptmp->p_lock);
934 out:
935 rw_exit(&ptmp->p_reflock);
936 return error;
937 }
938
939 /*
940 * sysctl helper routine for a process's rlimits as exposed by sysctl.
941 */
942 static int
943 sysctl_proc_plimit(SYSCTLFN_ARGS)
944 {
945 struct proc *ptmp;
946 u_int limitno;
947 int which, error = 0;
948 struct rlimit alim;
949 struct sysctlnode node;
950
951 if (namelen != 0)
952 return (EINVAL);
953
954 which = name[-1];
955 if (which != PROC_PID_LIMIT_TYPE_SOFT &&
956 which != PROC_PID_LIMIT_TYPE_HARD)
957 return (EINVAL);
958
959 limitno = name[-2] - 1;
960 if (limitno >= RLIM_NLIMITS)
961 return (EINVAL);
962
963 if (name[-3] != PROC_PID_LIMIT)
964 return (EINVAL);
965
966 /* Find the process. Hold a reference (p_reflock), if found. */
967 error = sysctl_proc_findproc(l, (pid_t)name[-4], &ptmp);
968 if (error)
969 return error;
970
971 /* XXX-elad */
972 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
973 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
974 if (error)
975 goto out;
976
977 /* Check if we can view limits. */
978 if (newp == NULL) {
979 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
980 ptmp, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_GET), &alim,
981 KAUTH_ARG(which));
982 if (error)
983 goto out;
984 }
985
986 node = *rnode;
987 memcpy(&alim, &ptmp->p_rlimit[limitno], sizeof(alim));
988 if (which == PROC_PID_LIMIT_TYPE_HARD)
989 node.sysctl_data = &alim.rlim_max;
990 else
991 node.sysctl_data = &alim.rlim_cur;
992
993 error = sysctl_lookup(SYSCTLFN_CALL(&node));
994 if (error || newp == NULL) {
995 goto out;
996 }
997 error = dosetrlimit(l, ptmp, limitno, &alim);
998 out:
999 rw_exit(&ptmp->p_reflock);
1000 return error;
1001 }
1002
1003 static struct sysctllog *proc_sysctllog;
1004
1005 /*
1006 * and finally, the actually glue that sticks it to the tree
1007 */
1008 static void
1009 sysctl_proc_setup()
1010 {
1011
1012 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1013 CTLFLAG_PERMANENT,
1014 CTLTYPE_NODE, "proc", NULL,
1015 NULL, 0, NULL, 0,
1016 CTL_PROC, CTL_EOL);
1017 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1018 CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER,
1019 CTLTYPE_NODE, "curproc",
1020 SYSCTL_DESCR("Per-process settings"),
1021 NULL, 0, NULL, 0,
1022 CTL_PROC, PROC_CURPROC, CTL_EOL);
1023
1024 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1025 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1026 CTLTYPE_STRING, "corename",
1027 SYSCTL_DESCR("Core file name"),
1028 sysctl_proc_corename, 0, NULL, MAXPATHLEN,
1029 CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL);
1030 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1031 CTLFLAG_PERMANENT,
1032 CTLTYPE_NODE, "rlimit",
1033 SYSCTL_DESCR("Process limits"),
1034 NULL, 0, NULL, 0,
1035 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL);
1036
1037 #define create_proc_plimit(s, n) do { \
1038 sysctl_createv(&proc_sysctllog, 0, NULL, NULL, \
1039 CTLFLAG_PERMANENT, \
1040 CTLTYPE_NODE, s, \
1041 SYSCTL_DESCR("Process " s " limits"), \
1042 NULL, 0, NULL, 0, \
1043 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
1044 CTL_EOL); \
1045 sysctl_createv(&proc_sysctllog, 0, NULL, NULL, \
1046 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
1047 CTLTYPE_QUAD, "soft", \
1048 SYSCTL_DESCR("Process soft " s " limit"), \
1049 sysctl_proc_plimit, 0, NULL, 0, \
1050 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
1051 PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL); \
1052 sysctl_createv(&proc_sysctllog, 0, NULL, NULL, \
1053 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
1054 CTLTYPE_QUAD, "hard", \
1055 SYSCTL_DESCR("Process hard " s " limit"), \
1056 sysctl_proc_plimit, 0, NULL, 0, \
1057 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
1058 PROC_PID_LIMIT_TYPE_HARD, CTL_EOL); \
1059 } while (0/*CONSTCOND*/)
1060
1061 create_proc_plimit("cputime", PROC_PID_LIMIT_CPU);
1062 create_proc_plimit("filesize", PROC_PID_LIMIT_FSIZE);
1063 create_proc_plimit("datasize", PROC_PID_LIMIT_DATA);
1064 create_proc_plimit("stacksize", PROC_PID_LIMIT_STACK);
1065 create_proc_plimit("coredumpsize", PROC_PID_LIMIT_CORE);
1066 create_proc_plimit("memoryuse", PROC_PID_LIMIT_RSS);
1067 create_proc_plimit("memorylocked", PROC_PID_LIMIT_MEMLOCK);
1068 create_proc_plimit("maxproc", PROC_PID_LIMIT_NPROC);
1069 create_proc_plimit("descriptors", PROC_PID_LIMIT_NOFILE);
1070 create_proc_plimit("sbsize", PROC_PID_LIMIT_SBSIZE);
1071 create_proc_plimit("vmemoryuse", PROC_PID_LIMIT_AS);
1072
1073 #undef create_proc_plimit
1074
1075 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1076 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1077 CTLTYPE_INT, "stopfork",
1078 SYSCTL_DESCR("Stop process at fork(2)"),
1079 sysctl_proc_stop, 0, NULL, 0,
1080 CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL);
1081 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1082 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1083 CTLTYPE_INT, "stopexec",
1084 SYSCTL_DESCR("Stop process at execve(2)"),
1085 sysctl_proc_stop, 0, NULL, 0,
1086 CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL);
1087 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1088 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1089 CTLTYPE_INT, "stopexit",
1090 SYSCTL_DESCR("Stop process before completing exit"),
1091 sysctl_proc_stop, 0, NULL, 0,
1092 CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL);
1093 }
1094