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