sys/kern/kern_plimit.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | /* * Copyright (c) 2006,2017,2018 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@backplane.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Copyright (c) 1982, 1986, 1991, 1993 * The Regents of the University of California. All rights reserved. * (c) UNIX System Laboratories, Inc. * All or some portions of this file are derived from material licensed * to the University of California by American Telephone and Telegraph * Co. or Unix System Laboratories, Inc. and are reproduced herein with * the permission of UNIX System Laboratories, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94 */ #include <sys/resource.h> #include <sys/spinlock.h> #include <sys/proc.h> #include <sys/caps.h> #include <sys/file.h> #include <sys/lockf.h> #include <sys/kern_syscall.h> #include <sys/malloc.h> #include <sys/sysmsg.h> #include <vm/vm_param.h> #include <vm/vm.h> #include <vm/vm_map.h> #include <machine/pmap.h> #include <sys/spinlock2.h> static MALLOC_DEFINE(M_PLIMIT, "plimit", "resource limits"); static void plimit_copy(struct plimit *olimit, struct plimit *nlimit); static __inline struct plimit * readplimits(struct proc *p) { thread_t td = curthread; struct plimit *limit; limit = td->td_limit; if (limit != p->p_limit) { spin_lock_shared(&p->p_spin); limit = p->p_limit; atomic_add_int(&limit->p_refcnt, 1); spin_unlock_shared(&p->p_spin); if (td->td_limit) plimit_free(td->td_limit); td->td_limit = limit; } return limit; } /* * Initialize proc0's plimit structure. All later plimit structures * are inherited through fork. */ void plimit_init0(struct plimit *limit) { int i; rlim_t lim; for (i = 0; i < RLIM_NLIMITS; ++i) { limit->pl_rlimit[i].rlim_cur = RLIM_INFINITY; limit->pl_rlimit[i].rlim_max = RLIM_INFINITY; } limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur = maxfiles; limit->pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; limit->pl_rlimit[RLIMIT_NPROC].rlim_cur = maxproc; limit->pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; lim = ptoa((rlim_t)vmstats.v_free_count); limit->pl_rlimit[RLIMIT_RSS].rlim_max = lim; limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim; limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3; limit->p_cpulimit = RLIM_INFINITY; limit->p_refcnt = 1; spin_init(&limit->p_spin, "plimitinit"); } /* * Return a plimit for use by a new forked process given the one * contained in the parent process. */ struct plimit * plimit_fork(struct proc *p1) { struct plimit *olimit = p1->p_limit; struct plimit *nlimit; uint32_t count; /* * Try to share the parent's plimit structure. If we cannot, make * a copy. * * NOTE: (count) value is field prior to increment. */ count = atomic_fetchadd_int(&olimit->p_refcnt, 1); cpu_ccfence(); if (count & PLIMITF_EXCLUSIVE) { if ((count & PLIMITF_MASK) == 1 && p1->p_nthreads == 1) { atomic_clear_int(&olimit->p_refcnt, PLIMITF_EXCLUSIVE); } else { nlimit = kmalloc(sizeof(*nlimit), M_PLIMIT, M_WAITOK); plimit_copy(olimit, nlimit); plimit_free(olimit); olimit = nlimit; } } return olimit; } /* * This routine is called when a new LWP is created for a process. We * must force exclusivity to ensure that p->p_limit remains stable. * * LWPs share the same process structure so this does not bump refcnt. */ void plimit_lwp_fork(struct proc *p) { struct plimit *olimit = p->p_limit; struct plimit *nlimit; uint32_t count; count = olimit->p_refcnt; cpu_ccfence(); if ((count & PLIMITF_EXCLUSIVE) == 0) { if (count != 1) { nlimit = kmalloc(sizeof(*nlimit), M_PLIMIT, M_WAITOK); plimit_copy(olimit, nlimit); p->p_limit = nlimit; plimit_free(olimit); olimit = nlimit; } atomic_set_int(&olimit->p_refcnt, PLIMITF_EXCLUSIVE); } } /* * This routine is called to fixup a process's p_limit structure prior * to it being modified. If index >= 0 the specified modification is also * made. * * This routine must make the limit structure exclusive. If we are threaded, * the structure will already be exclusive. A later fork will convert it * back to copy-on-write if possible. * * We can count on p->p_limit being stable since if we had created any * threads it will have already been made exclusive. */ void plimit_modify(struct proc *p, int index, struct rlimit *rlim) { struct plimit *olimit; struct plimit *nlimit; uint32_t count; /* * Make exclusive */ olimit = p->p_limit; count = olimit->p_refcnt; cpu_ccfence(); if ((count & PLIMITF_EXCLUSIVE) == 0) { if (count != 1) { nlimit = kmalloc(sizeof(*nlimit), M_PLIMIT, M_WAITOK); plimit_copy(olimit, nlimit); p->p_limit = nlimit; plimit_free(olimit); olimit = nlimit; } atomic_set_int(&olimit->p_refcnt, PLIMITF_EXCLUSIVE); } /* * Make modification */ if (index >= 0) { if (p->p_nthreads == 1) { p->p_limit->pl_rlimit[index] = *rlim; } else { spin_lock(&olimit->p_spin); p->p_limit->pl_rlimit[index].rlim_cur = rlim->rlim_cur; p->p_limit->pl_rlimit[index].rlim_max = rlim->rlim_max; spin_unlock(&olimit->p_spin); } } } /* * Destroy a process's plimit structure. */ void plimit_free(struct plimit *limit) { uint32_t count; count = atomic_fetchadd_int(&limit->p_refcnt, -1); if ((count & ~PLIMITF_EXCLUSIVE) == 1) { limit->p_refcnt = -999; kfree(limit, M_PLIMIT); } } /* * Modify a resource limit (from system call) */ int kern_setrlimit(u_int which, struct rlimit *limp) { struct proc *p = curproc; struct plimit *limit; struct rlimit *alimp; int error; if (which >= RLIM_NLIMITS) return (EINVAL); /* * We will be modifying a resource, make a copy if necessary. */ plimit_modify(p, -1, NULL); limit = p->p_limit; alimp = &limit->pl_rlimit[which]; /* * Preserve historical bugs by treating negative limits as unsigned. */ if (limp->rlim_cur < 0) limp->rlim_cur = RLIM_INFINITY; if (limp->rlim_max < 0) limp->rlim_max = RLIM_INFINITY; spin_lock(&limit->p_spin); if (limp->rlim_cur > alimp->rlim_max || limp->rlim_max > alimp->rlim_max) { spin_unlock(&limit->p_spin); error = caps_priv_check(p->p_ucred, SYSCAP_NOPROC_SETRLIMIT); if (error) return (error); } else { spin_unlock(&limit->p_spin); } if (limp->rlim_cur > limp->rlim_max) limp->rlim_cur = limp->rlim_max; switch (which) { case RLIMIT_CPU: spin_lock(&limit->p_spin); if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000) limit->p_cpulimit = RLIM_INFINITY; else limit->p_cpulimit = (rlim_t)1000000 * limp->rlim_cur; spin_unlock(&limit->p_spin); break; case RLIMIT_DATA: if (limp->rlim_cur > maxdsiz) limp->rlim_cur = maxdsiz; if (limp->rlim_max > maxdsiz) limp->rlim_max = maxdsiz; break; case RLIMIT_STACK: if (limp->rlim_cur > maxssiz) limp->rlim_cur = maxssiz; if (limp->rlim_max > maxssiz) limp->rlim_max = maxssiz; /* * Stack is allocated to the max at exec time with only * "rlim_cur" bytes accessible. If stack limit is going * up make more accessible, if going down make inaccessible. */ spin_lock(&limit->p_spin); if (limp->rlim_cur != alimp->rlim_cur) { vm_offset_t addr; vm_size_t size; vm_prot_t prot; if (limp->rlim_cur > alimp->rlim_cur) { prot = VM_PROT_ALL; size = limp->rlim_cur - alimp->rlim_cur; addr = USRSTACK - limp->rlim_cur; } else { prot = VM_PROT_NONE; size = alimp->rlim_cur - limp->rlim_cur; addr = USRSTACK - alimp->rlim_cur; } spin_unlock(&limit->p_spin); addr = trunc_page(addr); size = round_page(size); vm_map_protect(&p->p_vmspace->vm_map, addr, addr+size, prot, FALSE); } else { spin_unlock(&limit->p_spin); } break; case RLIMIT_NOFILE: if (limp->rlim_cur > maxfilesperproc) limp->rlim_cur = maxfilesperproc; if (limp->rlim_max > maxfilesperproc) limp->rlim_max = maxfilesperproc; break; case RLIMIT_NPROC: if (limp->rlim_cur > maxprocperuid) limp->rlim_cur = maxprocperuid; if (limp->rlim_max > maxprocperuid) limp->rlim_max = maxprocperuid; if (limp->rlim_cur < 1) limp->rlim_cur = 1; if (limp->rlim_max < 1) limp->rlim_max = 1; break; case RLIMIT_POSIXLOCKS: if (limp->rlim_cur > maxposixlocksperuid) limp->rlim_cur = maxposixlocksperuid; if (limp->rlim_max > maxposixlocksperuid) limp->rlim_max = maxposixlocksperuid; break; } spin_lock(&limit->p_spin); *alimp = *limp; spin_unlock(&limit->p_spin); return (0); } int sys_setrlimit(struct sysmsg *sysmsg, const struct __setrlimit_args *uap) { struct rlimit alim; int error; error = copyin(uap->rlp, &alim, sizeof(alim)); if (error) return (error); error = kern_setrlimit(uap->which, &alim); return (error); } /* * The rlimit indexed by which is returned in the second argument. */ int kern_getrlimit(u_int which, struct rlimit *limp) { struct proc *p = curproc; struct plimit *limit; /* * p is NULL when kern_getrlimit is called from a * kernel thread. In this case as the calling proc * isn't available we just skip the limit check. */ if (p == NULL) return 0; if (which >= RLIM_NLIMITS) return (EINVAL); limit = readplimits(p); *limp = limit->pl_rlimit[which]; return (0); } int sys_getrlimit(struct sysmsg *sysmsg, const struct __getrlimit_args *uap) { struct rlimit lim; int error; error = kern_getrlimit(uap->which, &lim); if (error == 0) error = copyout(&lim, uap->rlp, sizeof(*uap->rlp)); return error; } /* * Determine if the cpu limit has been reached and return an operations * code for the caller to perform. */ int plimit_testcpulimit(struct proc *p, u_int64_t ttime) { struct plimit *limit; struct rlimit *rlim; int mode; limit = readplimits(p); /* * Initial tests without the spinlock. This is the fast path. * Any 32/64 bit glitches will fall through and retest with * the spinlock. */ if (limit->p_cpulimit == RLIM_INFINITY) return(PLIMIT_TESTCPU_OK); if (ttime <= limit->p_cpulimit) return(PLIMIT_TESTCPU_OK); if (ttime > limit->p_cpulimit) { rlim = &limit->pl_rlimit[RLIMIT_CPU]; if (ttime / (rlim_t)1000000 >= rlim->rlim_max + 5) mode = PLIMIT_TESTCPU_KILL; else mode = PLIMIT_TESTCPU_XCPU; } else { mode = PLIMIT_TESTCPU_OK; } return(mode); } /* * Helper routine to copy olimit to nlimit and initialize nlimit for * use. nlimit's reference count will be set to 1 and its exclusive bit * will be cleared. */ static void plimit_copy(struct plimit *olimit, struct plimit *nlimit) { *nlimit = *olimit; spin_init(&nlimit->p_spin, "plimitcopy"); nlimit->p_refcnt = 1; } /* * This routine returns the value of a resource, downscaled based on * the processes fork depth and chroot depth (up to 50%). This mechanism * is designed to prevent run-aways from blowing up unrelated processes * running under the same UID. * * NOTE: Currently only applicable to RLIMIT_NPROC. We could also limit * file descriptors but we shouldn't have to as these are allocated * dynamically. */ u_int64_t plimit_getadjvalue(int i) { struct proc *p = curproc; struct plimit *limit; uint64_t v; uint32_t depth; limit = p->p_limit; v = limit->pl_rlimit[i].rlim_cur; if (i == RLIMIT_NPROC) { /* * 10% per chroot (around 1/3% per fork depth), with a * maximum of 50% downscaling of the resource limit. */ depth = p->p_depth; if (depth > 32 * 5) depth = 32 * 5; v -= v * depth / 320; } return v; } |