2 * Copyright (c) 2006,2017,2018 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 * (c) UNIX System Laboratories, Inc.
38 * All or some portions of this file are derived from material licensed
39 * to the University of California by American Telephone and Telegraph
40 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41 * the permission of UNIX System Laboratories, Inc.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
69 #include <sys/resource.h>
70 #include <sys/spinlock.h>
74 #include <sys/lockf.h>
75 #include <sys/kern_syscall.h>
76 #include <sys/malloc.h>
77 #include <sys/sysmsg.h>
79 #include <vm/vm_param.h>
81 #include <vm/vm_map.h>
83 #include <machine/pmap.h>
85 #include <sys/spinlock2.h>
87 static MALLOC_DEFINE(M_PLIMIT, "plimit", "resource limits");
89 static void plimit_copy(struct plimit *olimit, struct plimit *nlimit);
93 readplimits(struct proc *p)
95 thread_t td = curthread;
99 if (limit != p->p_limit) {
100 spin_lock_shared(&p->p_spin);
102 atomic_add_int(&limit->p_refcnt, 1);
103 spin_unlock_shared(&p->p_spin);
105 plimit_free(td->td_limit);
106 td->td_limit = limit;
112 * Initialize proc0's plimit structure. All later plimit structures
113 * are inherited through fork.
116 plimit_init0(struct plimit *limit)
121 for (i = 0; i < RLIM_NLIMITS; ++i) {
122 limit->pl_rlimit[i].rlim_cur = RLIM_INFINITY;
123 limit->pl_rlimit[i].rlim_max = RLIM_INFINITY;
125 limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur = maxfiles;
126 limit->pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
127 limit->pl_rlimit[RLIMIT_NPROC].rlim_cur = maxproc;
128 limit->pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
129 lim = ptoa((rlim_t)vmstats.v_free_count);
130 limit->pl_rlimit[RLIMIT_RSS].rlim_max = lim;
131 limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
132 limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
133 limit->p_cpulimit = RLIM_INFINITY;
135 spin_init(&limit->p_spin, "plimitinit");
139 * Return a plimit for use by a new forked process given the one
140 * contained in the parent process.
143 plimit_fork(struct proc *p1)
145 struct plimit *olimit = p1->p_limit;
146 struct plimit *nlimit;
150 * Try to share the parent's plimit structure. If we cannot, make
153 * NOTE: (count) value is field prior to increment.
155 count = atomic_fetchadd_int(&olimit->p_refcnt, 1);
157 if (count & PLIMITF_EXCLUSIVE) {
158 if ((count & PLIMITF_MASK) == 1 && p1->p_nthreads == 1) {
159 atomic_clear_int(&olimit->p_refcnt, PLIMITF_EXCLUSIVE);
161 nlimit = kmalloc(sizeof(*nlimit), M_PLIMIT, M_WAITOK);
162 plimit_copy(olimit, nlimit);
171 * This routine is called when a new LWP is created for a process. We
172 * must force exclusivity to ensure that p->p_limit remains stable.
174 * LWPs share the same process structure so this does not bump refcnt.
177 plimit_lwp_fork(struct proc *p)
179 struct plimit *olimit = p->p_limit;
180 struct plimit *nlimit;
183 count = olimit->p_refcnt;
185 if ((count & PLIMITF_EXCLUSIVE) == 0) {
187 nlimit = kmalloc(sizeof(*nlimit), M_PLIMIT, M_WAITOK);
188 plimit_copy(olimit, nlimit);
193 atomic_set_int(&olimit->p_refcnt, PLIMITF_EXCLUSIVE);
198 * This routine is called to fixup a process's p_limit structure prior
199 * to it being modified. If index >= 0 the specified modification is also
202 * This routine must make the limit structure exclusive. If we are threaded,
203 * the structure will already be exclusive. A later fork will convert it
204 * back to copy-on-write if possible.
206 * We can count on p->p_limit being stable since if we had created any
207 * threads it will have already been made exclusive.
210 plimit_modify(struct proc *p, int index, struct rlimit *rlim)
212 struct plimit *olimit;
213 struct plimit *nlimit;
220 count = olimit->p_refcnt;
222 if ((count & PLIMITF_EXCLUSIVE) == 0) {
224 nlimit = kmalloc(sizeof(*nlimit), M_PLIMIT, M_WAITOK);
225 plimit_copy(olimit, nlimit);
230 atomic_set_int(&olimit->p_refcnt, PLIMITF_EXCLUSIVE);
237 if (p->p_nthreads == 1) {
238 p->p_limit->pl_rlimit[index] = *rlim;
240 spin_lock(&olimit->p_spin);
241 p->p_limit->pl_rlimit[index].rlim_cur = rlim->rlim_cur;
242 p->p_limit->pl_rlimit[index].rlim_max = rlim->rlim_max;
243 spin_unlock(&olimit->p_spin);
249 * Destroy a process's plimit structure.
252 plimit_free(struct plimit *limit)
256 count = atomic_fetchadd_int(&limit->p_refcnt, -1);
258 if ((count & ~PLIMITF_EXCLUSIVE) == 1) {
259 limit->p_refcnt = -999;
260 kfree(limit, M_PLIMIT);
265 * Modify a resource limit (from system call)
268 kern_setrlimit(u_int which, struct rlimit *limp)
270 struct proc *p = curproc;
271 struct plimit *limit;
272 struct rlimit *alimp;
275 if (which >= RLIM_NLIMITS)
279 * We will be modifying a resource, make a copy if necessary.
281 plimit_modify(p, -1, NULL);
283 alimp = &limit->pl_rlimit[which];
286 * Preserve historical bugs by treating negative limits as unsigned.
288 if (limp->rlim_cur < 0)
289 limp->rlim_cur = RLIM_INFINITY;
290 if (limp->rlim_max < 0)
291 limp->rlim_max = RLIM_INFINITY;
293 spin_lock(&limit->p_spin);
294 if (limp->rlim_cur > alimp->rlim_max ||
295 limp->rlim_max > alimp->rlim_max) {
296 spin_unlock(&limit->p_spin);
297 error = priv_check_cred(p->p_ucred, PRIV_PROC_SETRLIMIT, 0);
301 spin_unlock(&limit->p_spin);
303 if (limp->rlim_cur > limp->rlim_max)
304 limp->rlim_cur = limp->rlim_max;
308 spin_lock(&limit->p_spin);
309 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000)
310 limit->p_cpulimit = RLIM_INFINITY;
312 limit->p_cpulimit = (rlim_t)1000000 * limp->rlim_cur;
313 spin_unlock(&limit->p_spin);
316 if (limp->rlim_cur > maxdsiz)
317 limp->rlim_cur = maxdsiz;
318 if (limp->rlim_max > maxdsiz)
319 limp->rlim_max = maxdsiz;
323 if (limp->rlim_cur > maxssiz)
324 limp->rlim_cur = maxssiz;
325 if (limp->rlim_max > maxssiz)
326 limp->rlim_max = maxssiz;
328 * Stack is allocated to the max at exec time with only
329 * "rlim_cur" bytes accessible. If stack limit is going
330 * up make more accessible, if going down make inaccessible.
332 spin_lock(&limit->p_spin);
333 if (limp->rlim_cur != alimp->rlim_cur) {
338 if (limp->rlim_cur > alimp->rlim_cur) {
340 size = limp->rlim_cur - alimp->rlim_cur;
341 addr = USRSTACK - limp->rlim_cur;
344 size = alimp->rlim_cur - limp->rlim_cur;
345 addr = USRSTACK - alimp->rlim_cur;
347 spin_unlock(&limit->p_spin);
348 addr = trunc_page(addr);
349 size = round_page(size);
350 vm_map_protect(&p->p_vmspace->vm_map,
351 addr, addr+size, prot, FALSE);
353 spin_unlock(&limit->p_spin);
358 if (limp->rlim_cur > maxfilesperproc)
359 limp->rlim_cur = maxfilesperproc;
360 if (limp->rlim_max > maxfilesperproc)
361 limp->rlim_max = maxfilesperproc;
365 if (limp->rlim_cur > maxprocperuid)
366 limp->rlim_cur = maxprocperuid;
367 if (limp->rlim_max > maxprocperuid)
368 limp->rlim_max = maxprocperuid;
369 if (limp->rlim_cur < 1)
371 if (limp->rlim_max < 1)
374 case RLIMIT_POSIXLOCKS:
375 if (limp->rlim_cur > maxposixlocksperuid)
376 limp->rlim_cur = maxposixlocksperuid;
377 if (limp->rlim_max > maxposixlocksperuid)
378 limp->rlim_max = maxposixlocksperuid;
381 spin_lock(&limit->p_spin);
383 spin_unlock(&limit->p_spin);
388 sys_setrlimit(struct sysmsg *sysmsg, const struct __setrlimit_args *uap)
393 error = copyin(uap->rlp, &alim, sizeof(alim));
397 error = kern_setrlimit(uap->which, &alim);
403 * The rlimit indexed by which is returned in the second argument.
406 kern_getrlimit(u_int which, struct rlimit *limp)
408 struct proc *p = curproc;
409 struct plimit *limit;
412 * p is NULL when kern_getrlimit is called from a
413 * kernel thread. In this case as the calling proc
414 * isn't available we just skip the limit check.
419 if (which >= RLIM_NLIMITS)
422 limit = readplimits(p);
423 *limp = limit->pl_rlimit[which];
429 sys_getrlimit(struct sysmsg *sysmsg, const struct __getrlimit_args *uap)
434 error = kern_getrlimit(uap->which, &lim);
437 error = copyout(&lim, uap->rlp, sizeof(*uap->rlp));
442 * Determine if the cpu limit has been reached and return an operations
443 * code for the caller to perform.
446 plimit_testcpulimit(struct proc *p, u_int64_t ttime)
448 struct plimit *limit;
452 limit = readplimits(p);
455 * Initial tests without the spinlock. This is the fast path.
456 * Any 32/64 bit glitches will fall through and retest with
459 if (limit->p_cpulimit == RLIM_INFINITY)
460 return(PLIMIT_TESTCPU_OK);
461 if (ttime <= limit->p_cpulimit)
462 return(PLIMIT_TESTCPU_OK);
464 if (ttime > limit->p_cpulimit) {
465 rlim = &limit->pl_rlimit[RLIMIT_CPU];
466 if (ttime / (rlim_t)1000000 >= rlim->rlim_max + 5)
467 mode = PLIMIT_TESTCPU_KILL;
469 mode = PLIMIT_TESTCPU_XCPU;
471 mode = PLIMIT_TESTCPU_OK;
478 * Helper routine to copy olimit to nlimit and initialize nlimit for
479 * use. nlimit's reference count will be set to 1 and its exclusive bit
484 plimit_copy(struct plimit *olimit, struct plimit *nlimit)
488 spin_init(&nlimit->p_spin, "plimitcopy");
489 nlimit->p_refcnt = 1;
493 * This routine returns the value of a resource, downscaled based on
494 * the processes fork depth and chroot depth (up to 50%). This mechanism
495 * is designed to prevent run-aways from blowing up unrelated processes
496 * running under the same UID.
498 * NOTE: Currently only applicable to RLIMIT_NPROC. We could also limit
499 * file descriptors but we shouldn't have to as these are allocated
503 plimit_getadjvalue(int i)
505 struct proc *p = curproc;
506 struct plimit *limit;
511 v = limit->pl_rlimit[i].rlim_cur;
512 if (i == RLIMIT_NPROC) {
514 * 10% per chroot (around 1/3% per fork depth), with a
515 * maximum of 50% downscaling of the resource limit.
520 v -= v * depth / 320;