Merge branch 'vendor/DHCPCD'
[dragonfly.git] / sys / kern / kern_plimit.c
1 /*
2  * Copyright (c) 2006,2017,2018 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
16  *    distribution.
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.
20  *
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
32  * SUCH DAMAGE.
33  */
34 /*
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.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
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.
54  *
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
65  * SUCH DAMAGE.
66  *
67  *      @(#)kern_resource.c     8.5 (Berkeley) 1/21/94
68  */
69 #include <sys/resource.h>
70 #include <sys/spinlock.h>
71 #include <sys/proc.h>
72 #include <sys/priv.h>
73 #include <sys/file.h>
74 #include <sys/lockf.h>
75 #include <sys/kern_syscall.h>
76 #include <sys/malloc.h>
77
78 #include <vm/vm_param.h>
79 #include <vm/vm.h>
80 #include <vm/vm_map.h>
81
82 #include <machine/pmap.h>
83
84 #include <sys/spinlock2.h>
85
86 static MALLOC_DEFINE(M_PLIMIT, "plimit", "resource limits");
87
88 static void plimit_copy(struct plimit *olimit, struct plimit *nlimit);
89
90 static __inline
91 struct plimit *
92 readplimits(struct proc *p)
93 {
94         thread_t td = curthread;
95         struct plimit *limit;
96
97         limit = td->td_limit;
98         if (limit != p->p_limit) {
99                 spin_lock_shared(&p->p_spin);
100                 limit = p->p_limit;
101                 atomic_add_int(&limit->p_refcnt, 1);
102                 spin_unlock_shared(&p->p_spin);
103                 if (td->td_limit)
104                         plimit_free(td->td_limit);
105                 td->td_limit = limit;
106         }
107         return limit;
108 }
109
110 /*
111  * Initialize proc0's plimit structure.  All later plimit structures
112  * are inherited through fork.
113  */
114 void
115 plimit_init0(struct plimit *limit)
116 {
117         int i;
118         rlim_t lim;
119
120         for (i = 0; i < RLIM_NLIMITS; ++i) {
121                 limit->pl_rlimit[i].rlim_cur = RLIM_INFINITY;
122                 limit->pl_rlimit[i].rlim_max = RLIM_INFINITY;
123         }
124         limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur = maxfiles;
125         limit->pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
126         limit->pl_rlimit[RLIMIT_NPROC].rlim_cur = maxproc;
127         limit->pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
128         lim = ptoa((rlim_t)vmstats.v_free_count);
129         limit->pl_rlimit[RLIMIT_RSS].rlim_max = lim;
130         limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
131         limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
132         limit->p_cpulimit = RLIM_INFINITY;
133         limit->p_refcnt = 1;
134         spin_init(&limit->p_spin, "plimitinit");
135 }
136
137 /*
138  * Return a plimit for use by a new forked process given the one
139  * contained in the parent process.
140  */
141 struct plimit *
142 plimit_fork(struct proc *p1)
143 {
144         struct plimit *olimit = p1->p_limit;
145         struct plimit *nlimit;
146         uint32_t count;
147
148         /*
149          * Try to share the parent's plimit structure.  If we cannot, make
150          * a copy.
151          *
152          * NOTE: (count) value is field prior to increment.
153          */
154         count = atomic_fetchadd_int(&olimit->p_refcnt, 1);
155         cpu_ccfence();
156         if (count & PLIMITF_EXCLUSIVE) {
157                 if ((count & PLIMITF_MASK) == 1 && p1->p_nthreads == 1) {
158                         atomic_clear_int(&olimit->p_refcnt, PLIMITF_EXCLUSIVE);
159                 } else {
160                         nlimit = kmalloc(sizeof(*nlimit), M_PLIMIT, M_WAITOK);
161                         plimit_copy(olimit, nlimit);
162                         plimit_free(olimit);
163                         olimit = nlimit;
164                 }
165         }
166         return olimit;
167 }
168
169 /*
170  * This routine is called when a new LWP is created for a process.  We
171  * must force exclusivity to ensure that p->p_limit remains stable.
172  *
173  * LWPs share the same process structure so this does not bump refcnt.
174  */
175 void
176 plimit_lwp_fork(struct proc *p)
177 {
178         struct plimit *olimit = p->p_limit;
179         struct plimit *nlimit;
180         uint32_t count;
181
182         count = olimit->p_refcnt;
183         cpu_ccfence();
184         if ((count & PLIMITF_EXCLUSIVE) == 0) {
185                 if (count != 1) {
186                         nlimit = kmalloc(sizeof(*nlimit), M_PLIMIT, M_WAITOK);
187                         plimit_copy(olimit, nlimit);
188                         p->p_limit = nlimit;
189                         plimit_free(olimit);
190                         olimit = nlimit;
191                 }
192                 atomic_set_int(&olimit->p_refcnt, PLIMITF_EXCLUSIVE);
193         }
194 }
195
196 /*
197  * This routine is called to fixup a process's p_limit structure prior
198  * to it being modified.  If index >= 0 the specified modification is also
199  * made.
200  *
201  * This routine must make the limit structure exclusive.  If we are threaded,
202  * the structure will already be exclusive.  A later fork will convert it
203  * back to copy-on-write if possible.
204  *
205  * We can count on p->p_limit being stable since if we had created any
206  * threads it will have already been made exclusive.
207  */
208 void
209 plimit_modify(struct proc *p, int index, struct rlimit *rlim)
210 {
211         struct plimit *olimit;
212         struct plimit *nlimit;
213         uint32_t count;
214
215         /*
216          * Make exclusive
217          */
218         olimit = p->p_limit;
219         count = olimit->p_refcnt;
220         cpu_ccfence();
221         if ((count & PLIMITF_EXCLUSIVE) == 0) {
222                 if (count != 1) {
223                         nlimit = kmalloc(sizeof(*nlimit), M_PLIMIT, M_WAITOK);
224                         plimit_copy(olimit, nlimit);
225                         p->p_limit = nlimit;
226                         plimit_free(olimit);
227                         olimit = nlimit;
228                 }
229                 atomic_set_int(&olimit->p_refcnt, PLIMITF_EXCLUSIVE);
230         }
231
232         /*
233          * Make modification
234          */
235         if (index >= 0) {
236                 if (p->p_nthreads == 1) {
237                         p->p_limit->pl_rlimit[index] = *rlim;
238                 } else {
239                         spin_lock(&olimit->p_spin);
240                         p->p_limit->pl_rlimit[index].rlim_cur = rlim->rlim_cur;
241                         p->p_limit->pl_rlimit[index].rlim_max = rlim->rlim_max;
242                         spin_unlock(&olimit->p_spin);
243                 }
244         }
245 }
246
247 /*
248  * Destroy a process's plimit structure.
249  */
250 void
251 plimit_free(struct plimit *limit)
252 {
253         uint32_t count;
254
255         count = atomic_fetchadd_int(&limit->p_refcnt, -1);
256
257         if ((count & ~PLIMITF_EXCLUSIVE) == 1) {
258                 limit->p_refcnt = -999;
259                 kfree(limit, M_PLIMIT);
260         }
261 }
262
263 /*
264  * Modify a resource limit (from system call)
265  */
266 int
267 kern_setrlimit(u_int which, struct rlimit *limp)
268 {
269         struct proc *p = curproc;
270         struct plimit *limit;
271         struct rlimit *alimp;
272         int error;
273
274         if (which >= RLIM_NLIMITS)
275                 return (EINVAL);
276
277         /*
278          * We will be modifying a resource, make a copy if necessary.
279          */
280         plimit_modify(p, -1, NULL);
281         limit = p->p_limit;
282         alimp = &limit->pl_rlimit[which];
283
284         /*
285          * Preserve historical bugs by treating negative limits as unsigned.
286          */
287         if (limp->rlim_cur < 0)
288                 limp->rlim_cur = RLIM_INFINITY;
289         if (limp->rlim_max < 0)
290                 limp->rlim_max = RLIM_INFINITY;
291
292         spin_lock(&limit->p_spin);
293         if (limp->rlim_cur > alimp->rlim_max ||
294             limp->rlim_max > alimp->rlim_max) {
295                 spin_unlock(&limit->p_spin);
296                 error = priv_check_cred(p->p_ucred, PRIV_PROC_SETRLIMIT, 0);
297                 if (error)
298                         return (error);
299         } else {
300                 spin_unlock(&limit->p_spin);
301         }
302         if (limp->rlim_cur > limp->rlim_max)
303                 limp->rlim_cur = limp->rlim_max;
304
305         switch (which) {
306         case RLIMIT_CPU:
307                 spin_lock(&limit->p_spin);
308                 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000)
309                         limit->p_cpulimit = RLIM_INFINITY;
310                 else
311                         limit->p_cpulimit = (rlim_t)1000000 * limp->rlim_cur;
312                 spin_unlock(&limit->p_spin);
313                 break;
314         case RLIMIT_DATA:
315                 if (limp->rlim_cur > maxdsiz)
316                         limp->rlim_cur = maxdsiz;
317                 if (limp->rlim_max > maxdsiz)
318                         limp->rlim_max = maxdsiz;
319                 break;
320
321         case RLIMIT_STACK:
322                 if (limp->rlim_cur > maxssiz)
323                         limp->rlim_cur = maxssiz;
324                 if (limp->rlim_max > maxssiz)
325                         limp->rlim_max = maxssiz;
326                 /*
327                  * Stack is allocated to the max at exec time with only
328                  * "rlim_cur" bytes accessible.  If stack limit is going
329                  * up make more accessible, if going down make inaccessible.
330                  */
331                 spin_lock(&limit->p_spin);
332                 if (limp->rlim_cur != alimp->rlim_cur) {
333                         vm_offset_t addr;
334                         vm_size_t size;
335                         vm_prot_t prot;
336
337                         if (limp->rlim_cur > alimp->rlim_cur) {
338                                 prot = VM_PROT_ALL;
339                                 size = limp->rlim_cur - alimp->rlim_cur;
340                                 addr = USRSTACK - limp->rlim_cur;
341                         } else {
342                                 prot = VM_PROT_NONE;
343                                 size = alimp->rlim_cur - limp->rlim_cur;
344                                 addr = USRSTACK - alimp->rlim_cur;
345                         }
346                         spin_unlock(&limit->p_spin);
347                         addr = trunc_page(addr);
348                         size = round_page(size);
349                         vm_map_protect(&p->p_vmspace->vm_map,
350                                        addr, addr+size, prot, FALSE);
351                 } else {
352                         spin_unlock(&limit->p_spin);
353                 }
354                 break;
355
356         case RLIMIT_NOFILE:
357                 if (limp->rlim_cur > maxfilesperproc)
358                         limp->rlim_cur = maxfilesperproc;
359                 if (limp->rlim_max > maxfilesperproc)
360                         limp->rlim_max = maxfilesperproc;
361                 break;
362
363         case RLIMIT_NPROC:
364                 if (limp->rlim_cur > maxprocperuid)
365                         limp->rlim_cur = maxprocperuid;
366                 if (limp->rlim_max > maxprocperuid)
367                         limp->rlim_max = maxprocperuid;
368                 if (limp->rlim_cur < 1)
369                         limp->rlim_cur = 1;
370                 if (limp->rlim_max < 1)
371                         limp->rlim_max = 1;
372                 break;
373         case RLIMIT_POSIXLOCKS:
374                 if (limp->rlim_cur > maxposixlocksperuid)
375                         limp->rlim_cur = maxposixlocksperuid;
376                 if (limp->rlim_max > maxposixlocksperuid)
377                         limp->rlim_max = maxposixlocksperuid;
378                 break;
379         }
380         spin_lock(&limit->p_spin);
381         *alimp = *limp;
382         spin_unlock(&limit->p_spin);
383         return (0);
384 }
385
386 /*
387  * The rlimit indexed by which is returned in the second argument.
388  */
389 int
390 kern_getrlimit(u_int which, struct rlimit *limp)
391 {
392         struct proc *p = curproc;
393         struct plimit *limit;
394
395         /*
396          * p is NULL when kern_getrlimit is called from a
397          * kernel thread. In this case as the calling proc
398          * isn't available we just skip the limit check.
399          */
400         if (p == NULL)
401                 return 0;
402
403         if (which >= RLIM_NLIMITS)
404                 return (EINVAL);
405
406         limit = readplimits(p);
407         *limp = limit->pl_rlimit[which];
408
409         return (0);
410 }
411
412 /*
413  * Determine if the cpu limit has been reached and return an operations
414  * code for the caller to perform.
415  */
416 int
417 plimit_testcpulimit(struct proc *p, u_int64_t ttime)
418 {
419         struct plimit *limit;
420         struct rlimit *rlim;
421         int mode;
422
423         limit = readplimits(p);
424
425         /*
426          * Initial tests without the spinlock.  This is the fast path.
427          * Any 32/64 bit glitches will fall through and retest with
428          * the spinlock.
429          */
430         if (limit->p_cpulimit == RLIM_INFINITY)
431                 return(PLIMIT_TESTCPU_OK);
432         if (ttime <= limit->p_cpulimit)
433                 return(PLIMIT_TESTCPU_OK);
434
435         if (ttime > limit->p_cpulimit) {
436                 rlim = &limit->pl_rlimit[RLIMIT_CPU];
437                 if (ttime / (rlim_t)1000000 >= rlim->rlim_max + 5)
438                         mode = PLIMIT_TESTCPU_KILL;
439                 else
440                         mode = PLIMIT_TESTCPU_XCPU;
441         } else {
442                 mode = PLIMIT_TESTCPU_OK;
443         }
444
445         return(mode);
446 }
447
448 /*
449  * Helper routine to copy olimit to nlimit and initialize nlimit for
450  * use.  nlimit's reference count will be set to 1 and its exclusive bit
451  * will be cleared.
452  */
453 static
454 void
455 plimit_copy(struct plimit *olimit, struct plimit *nlimit)
456 {
457         *nlimit = *olimit;
458
459         spin_init(&nlimit->p_spin, "plimitcopy");
460         nlimit->p_refcnt = 1;
461 }
462
463 /*
464  * This routine returns the value of a resource, downscaled based on
465  * the processes fork depth and chroot depth (up to 50%).  This mechanism
466  * is designed to prevent run-aways from blowing up unrelated processes
467  * running under the same UID.
468  *
469  * NOTE: Currently only applicable to RLIMIT_NPROC.  We could also limit
470  *       file descriptors but we shouldn't have to as these are allocated
471  *       dynamically.
472  */
473 u_int64_t
474 plimit_getadjvalue(int i)
475 {
476         struct proc *p = curproc;
477         struct plimit *limit;
478         uint64_t v;
479         uint32_t depth;
480
481         limit = p->p_limit;
482         v = limit->pl_rlimit[i].rlim_cur;
483         if (i == RLIMIT_NPROC) {
484                 /*
485                  * 10% per chroot (around 1/3% per fork depth), with a
486                  * maximum of 50% downscaling of the resource limit.
487                  */
488                 depth = p->p_depth;
489                 if (depth > 32 * 5)
490                         depth = 32 * 5;
491                 v -= v * depth / 320;
492         }
493         return v;
494 }