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