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