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