diff options
Diffstat (limited to 'sys/kern')
-rw-r--r-- | sys/kern/exec_subr.c | 4 | ||||
-rw-r--r-- | sys/kern/init_main.c | 4 | ||||
-rw-r--r-- | sys/kern/kern_descrip.c | 8 | ||||
-rw-r--r-- | sys/kern/kern_exec.c | 4 | ||||
-rw-r--r-- | sys/kern/kern_exit.c | 13 | ||||
-rw-r--r-- | sys/kern/kern_fork.c | 11 | ||||
-rw-r--r-- | sys/kern/kern_resource.c | 190 | ||||
-rw-r--r-- | sys/kern/kern_sig.c | 5 | ||||
-rw-r--r-- | sys/kern/sys_generic.c | 4 | ||||
-rw-r--r-- | sys/kern/vfs_vnops.c | 4 |
10 files changed, 201 insertions, 46 deletions
diff --git a/sys/kern/exec_subr.c b/sys/kern/exec_subr.c index f2282a4a357..f32be4bddf4 100644 --- a/sys/kern/exec_subr.c +++ b/sys/kern/exec_subr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: exec_subr.c,v 1.55 2018/04/12 17:13:44 deraadt Exp $ */ +/* $OpenBSD: exec_subr.c,v 1.56 2019/06/21 09:39:48 visa Exp $ */ /* $NetBSD: exec_subr.c,v 1.9 1994/12/04 03:10:42 mycroft Exp $ */ /* @@ -351,7 +351,7 @@ exec_setup_stack(struct proc *p, struct exec_package *epp) epp->ep_maxsaddr = USRSTACK - MAXSSIZ - MAXSSIZ_GUARD; epp->ep_minsaddr = USRSTACK; #endif - epp->ep_ssize = round_page(p->p_rlimit[RLIMIT_STACK].rlim_cur); + epp->ep_ssize = round_page(lim_cur(RLIMIT_STACK)); if (stackgap_random != 0) { sgap = arc4random() & (stackgap_random - 1); diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index b4bacaae1b7..a6a2b6da51e 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: init_main.c,v 1.289 2019/06/20 14:55:22 anton Exp $ */ +/* $OpenBSD: init_main.c,v 1.290 2019/06/21 09:39:48 visa Exp $ */ /* $NetBSD: init_main.c,v 1.84.4.1 1996/06/02 09:08:06 mrg Exp $ */ /* @@ -373,7 +373,7 @@ main(void *framep) cpu_configure(); /* Configure virtual memory system, set vm rlimits. */ - uvm_init_limits(p); + uvm_init_limits(&limit0); /* Per CPU memory allocation */ percpu_init(); diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 7da5dadb4ee..45fae009691 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_descrip.c,v 1.184 2019/05/13 17:31:51 deraadt Exp $ */ +/* $OpenBSD: kern_descrip.c,v 1.185 2019/06/21 09:39:48 visa Exp $ */ /* $NetBSD: kern_descrip.c,v 1.42 1996/03/30 22:24:38 christos Exp $ */ /* @@ -353,7 +353,7 @@ restart: FRELE(fp, p); return (0); } - if ((u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur || + if ((u_int)new >= lim_cur(RLIMIT_NOFILE) || (u_int)new >= maxfiles) { FRELE(fp, p); return (EBADF); @@ -414,7 +414,7 @@ restart: case F_DUPFD: case F_DUPFD_CLOEXEC: newmin = (long)SCARG(uap, arg); - if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur || + if ((u_int)newmin >= lim_cur(RLIMIT_NOFILE) || (u_int)newmin >= maxfiles) { error = EINVAL; break; @@ -864,7 +864,7 @@ fdalloc(struct proc *p, int want, int *result) * expanding the ofile array. */ restart: - lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles); + lim = min((int)lim_cur(RLIMIT_NOFILE), maxfiles); last = min(fdp->fd_nfiles, lim); if ((i = want) < fdp->fd_freefile) i = fdp->fd_freefile; diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c index 3019366cb1c..301d847f1c8 100644 --- a/sys/kern/kern_exec.c +++ b/sys/kern/kern_exec.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_exec.c,v 1.205 2019/06/01 14:11:17 mpi Exp $ */ +/* $OpenBSD: kern_exec.c,v 1.206 2019/06/21 09:39:48 visa Exp $ */ /* $NetBSD: kern_exec.c,v 1.75 1996/02/09 18:59:28 christos Exp $ */ /*- @@ -201,7 +201,7 @@ check_exec(struct proc *p, struct exec_package *epp) /* check limits */ if ((epp->ep_tsize > MAXTSIZ) || - (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur)) + (epp->ep_dsize > lim_cur(RLIMIT_DATA))) error = ENOMEM; if (!error) diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c index 74ac47d02f8..ab2f0de086d 100644 --- a/sys/kern/kern_exit.c +++ b/sys/kern/kern_exit.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_exit.c,v 1.177 2019/06/13 21:19:28 mpi Exp $ */ +/* $OpenBSD: kern_exit.c,v 1.178 2019/06/21 09:39:48 visa Exp $ */ /* $NetBSD: kern_exit.c,v 1.39 1996/04/22 01:38:25 christos Exp $ */ /* @@ -328,6 +328,15 @@ exit1(struct proc *p, int rv, int flags) KASSERT(pr->ps_refcnt > 0); } + /* Release the thread's read reference of resource limit structure. */ + if (p->p_limit != NULL) { + struct plimit *limit; + + limit = p->p_limit; + p->p_limit = NULL; + lim_free(limit); + } + /* * Other substructures are freed from reaper and wait(). */ @@ -636,7 +645,7 @@ process_zap(struct process *pr) free(pr->ps_ptstat, M_SUBPROC, sizeof(*pr->ps_ptstat)); pool_put(&rusage_pool, pr->ps_ru); KASSERT(TAILQ_EMPTY(&pr->ps_threads)); - limfree(pr->ps_limit); + lim_free(pr->ps_limit); crfree(pr->ps_ucred); pool_put(&process_pool, pr); nprocesses--; diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index 190c749720e..95180d9de31 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_fork.c,v 1.212 2019/06/01 14:11:17 mpi Exp $ */ +/* $OpenBSD: kern_fork.c,v 1.213 2019/06/21 09:39:48 visa Exp $ */ /* $NetBSD: kern_fork.c,v 1.29 1996/02/09 18:59:34 christos Exp $ */ /* @@ -151,6 +151,7 @@ thread_new(struct proc *parent, vaddr_t uaddr) p = pool_get(&proc_pool, PR_WAITOK); p->p_stat = SIDL; /* protect against others */ p->p_flag = 0; + p->p_limit = NULL; /* * Make a proc table entry for the new process. @@ -210,6 +211,8 @@ process_initialize(struct process *pr, struct proc *p) LIST_INIT(&pr->ps_kqlist); LIST_INIT(&pr->ps_sigiolst); + mtx_init(&pr->ps_mtx, IPL_MPFLOOR); + timeout_set(&pr->ps_realit_to, realitexpire, pr); timeout_set(&pr->ps_rucheck_to, rucheck, pr); } @@ -237,12 +240,10 @@ process_new(struct proc *p, struct process *parent, int flags) process_initialize(pr, p); pr->ps_pid = allocpid(); + lim_fork(parent, pr); /* post-copy fixups */ pr->ps_pptr = parent; - pr->ps_limit->pl_refcnt++; - if (pr->ps_limit->pl_rlimit[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) - timeout_add_msec(&pr->ps_rucheck_to, RUCHECK_INTERVAL); /* bump references to the text vnode (for sysctl) */ pr->ps_textvp = parent->ps_textvp; @@ -373,7 +374,7 @@ fork1(struct proc *curp, int flags, void (*func)(void *), void *arg, * Don't allow a nonprivileged user to exceed their current limit. */ count = chgproccnt(uid, 1); - if (uid != 0 && count > curp->p_rlimit[RLIMIT_NPROC].rlim_cur) { + if (uid != 0 && count > lim_cur(RLIMIT_NPROC)) { (void)chgproccnt(uid, -1); nprocesses--; nthreads--; diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c index 3bc9425020a..050326ab6fd 100644 --- a/sys/kern/kern_resource.c +++ b/sys/kern/kern_resource.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_resource.c,v 1.64 2019/06/10 03:15:53 visa Exp $ */ +/* $OpenBSD: kern_resource.c,v 1.65 2019/06/21 09:39:48 visa Exp $ */ /* $NetBSD: kern_resource.c,v 1.38 1996/10/23 07:19:38 matthias Exp $ */ /*- @@ -53,9 +53,16 @@ #include <uvm/uvm_extern.h> +/* Resource usage check interval in msec */ +#define RUCHECK_INTERVAL 1000 + /* SIGXCPU interval in seconds of process runtime */ #define SIGXCPU_INTERVAL 5 +struct plimit *lim_copy(struct plimit *); +struct plimit *lim_write_begin(void); +void lim_write_commit(struct plimit *); + void tuagg_sub(struct tusage *, struct proc *); /* @@ -65,6 +72,13 @@ rlim_t maxdmap = MAXDSIZ; rlim_t maxsmap = MAXSSIZ; /* + * Serializes resource limit updates. + * This lock has to be held together with ps_mtx when updating + * the process' ps_limit. + */ +struct rwlock rlimit_lock = RWLOCK_INITIALIZER("rlimitlk"); + +/* * Resource controls and accounting. */ @@ -229,25 +243,27 @@ int dosetrlimit(struct proc *p, u_int which, struct rlimit *limp) { struct rlimit *alimp; + struct plimit *limit; rlim_t maxlim; int error; if (which >= RLIM_NLIMITS || limp->rlim_cur > limp->rlim_max) return (EINVAL); - alimp = &p->p_rlimit[which]; - if (limp->rlim_max > alimp->rlim_max) - if ((error = suser(p)) != 0) - return (error); - if (p->p_p->ps_limit->pl_refcnt > 1) { - struct plimit *l = p->p_p->ps_limit; + rw_enter_write(&rlimit_lock); - /* limcopy() can sleep, so copy before decrementing refcnt */ - p->p_p->ps_limit = limcopy(l); - limfree(l); - alimp = &p->p_rlimit[which]; + alimp = &p->p_p->ps_limit->pl_rlimit[which]; + if (limp->rlim_max > alimp->rlim_max) { + if ((error = suser(p)) != 0) { + rw_exit_write(&rlimit_lock); + return (error); + } } + /* Get exclusive write access to the limit structure. */ + limit = lim_write_begin(); + alimp = &limit->pl_rlimit[which]; + switch (which) { case RLIMIT_DATA: maxlim = maxdmap; @@ -316,6 +332,10 @@ dosetrlimit(struct proc *p, u_int which, struct rlimit *limp) } *alimp = *limp; + + lim_write_commit(limit); + rw_exit_write(&rlimit_lock); + return (0); } @@ -326,16 +346,19 @@ sys_getrlimit(struct proc *p, void *v, register_t *retval) syscallarg(int) which; syscallarg(struct rlimit *) rlp; } */ *uap = v; - struct rlimit *alimp; + struct plimit *limit; + struct rlimit alimp; int error; if (SCARG(uap, which) < 0 || SCARG(uap, which) >= RLIM_NLIMITS) return (EINVAL); - alimp = &p->p_rlimit[SCARG(uap, which)]; - error = copyout(alimp, SCARG(uap, rlp), sizeof(struct rlimit)); + limit = lim_read_enter(); + alimp = limit->pl_rlimit[SCARG(uap, which)]; + lim_read_leave(limit); + error = copyout(&alimp, SCARG(uap, rlp), sizeof(struct rlimit)); #ifdef KTRACE if (error == 0 && KTRPOINT(p, KTR_STRUCT)) - ktrrlimit(p, alimp); + ktrrlimit(p, &alimp); #endif return (error); } @@ -507,8 +530,8 @@ ruadd(struct rusage *ru, struct rusage *ru2) void rucheck(void *arg) { + struct rlimit rlim; struct process *pr = arg; - struct rlimit *rlim; time_t runtime; int s; @@ -518,9 +541,12 @@ rucheck(void *arg) runtime = pr->ps_tu.tu_runtime.tv_sec; SCHED_UNLOCK(s); - rlim = &pr->ps_limit->pl_rlimit[RLIMIT_CPU]; - if ((rlim_t)runtime >= rlim->rlim_cur) { - if ((rlim_t)runtime >= rlim->rlim_max) { + mtx_enter(&pr->ps_mtx); + rlim = pr->ps_limit->pl_rlimit[RLIMIT_CPU]; + mtx_leave(&pr->ps_mtx); + + if ((rlim_t)runtime >= rlim.rlim_cur) { + if ((rlim_t)runtime >= rlim.rlim_max) { prsignal(pr, SIGKILL); } else if (runtime >= pr->ps_nextxcpu) { prsignal(pr, SIGXCPU); @@ -562,7 +588,7 @@ lim_startup(struct plimit *limit0) * and copy when a limit is changed. */ struct plimit * -limcopy(struct plimit *lim) +lim_copy(struct plimit *lim) { struct plimit *newlim; @@ -574,9 +600,129 @@ limcopy(struct plimit *lim) } void -limfree(struct plimit *lim) +lim_free(struct plimit *lim) { - if (--lim->pl_refcnt > 0) + if (atomic_dec_int_nv(&lim->pl_refcnt) > 0) return; pool_put(&plimit_pool, lim); } + +void +lim_fork(struct process *parent, struct process *child) +{ + struct plimit *limit; + + mtx_enter(&parent->ps_mtx); + limit = parent->ps_limit; + atomic_inc_int(&limit->pl_refcnt); + mtx_leave(&parent->ps_mtx); + + child->ps_limit = limit; + + if (limit->pl_rlimit[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) + timeout_add_msec(&child->ps_rucheck_to, RUCHECK_INTERVAL); +} + +/* + * Return an exclusive write reference to the process' resource limit structure. + * The caller has to release the structure by calling lim_write_commit(). + * + * This invalidates any plimit read reference held by the calling thread. + */ +struct plimit * +lim_write_begin(void) +{ + struct plimit *limit; + struct proc *p = curproc; + + rw_assert_wrlock(&rlimit_lock); + + if (p->p_limit != NULL) + lim_free(p->p_limit); + p->p_limit = NULL; + + /* + * It is safe to access ps_limit here without holding ps_mtx + * because rlimit_lock excludes other writers. + */ + + limit = p->p_p->ps_limit; + if (P_HASSIBLING(p) || limit->pl_refcnt > 1) + limit = lim_copy(limit); + + return (limit); +} + +/* + * Finish exclusive write access to the plimit structure. + * This makes the structure visible to other threads in the process. + */ +void +lim_write_commit(struct plimit *limit) +{ + struct plimit *olimit; + struct proc *p = curproc; + + rw_assert_wrlock(&rlimit_lock); + + if (limit != p->p_p->ps_limit) { + mtx_enter(&p->p_p->ps_mtx); + olimit = p->p_p->ps_limit; + p->p_p->ps_limit = limit; + mtx_leave(&p->p_p->ps_mtx); + + lim_free(olimit); + } +} + +/* + * Begin read access to the process' resource limit structure. + * The access has to be finished by calling lim_read_leave(). + * + * Sections denoted by lim_read_enter() and lim_read_leave() cannot nest. + */ +struct plimit * +lim_read_enter(void) +{ + struct plimit *limit; + struct proc *p = curproc; + struct process *pr = p->p_p; + + /* + * This thread might not observe the latest value of ps_limit + * if another thread updated the limits very recently on another CPU. + * However, the anomaly should disappear quickly, especially if + * there is any synchronization activity between the threads (or + * the CPUs). + */ + + limit = p->p_limit; + if (limit != pr->ps_limit) { + mtx_enter(&pr->ps_mtx); + limit = pr->ps_limit; + atomic_inc_int(&limit->pl_refcnt); + mtx_leave(&pr->ps_mtx); + if (p->p_limit != NULL) + lim_free(p->p_limit); + p->p_limit = limit; + } + KASSERT(limit != NULL); + return (limit); +} + +/* + * Get the value of the resource limit in given process. + */ +rlim_t +lim_cur_proc(struct proc *p, int which) +{ + struct process *pr = p->p_p; + rlim_t val; + + KASSERT(which >= 0 && which < RLIM_NLIMITS); + + mtx_enter(&pr->ps_mtx); + val = pr->ps_limit->pl_rlimit[which].rlim_cur; + mtx_leave(&pr->ps_mtx); + return (val); +} diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c index 4e3efa9c736..5738cd71af1 100644 --- a/sys/kern/kern_sig.c +++ b/sys/kern/kern_sig.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_sig.c,v 1.230 2019/05/13 19:21:31 bluhm Exp $ */ +/* $OpenBSD: kern_sig.c,v 1.231 2019/06/21 09:39:48 visa Exp $ */ /* $NetBSD: kern_sig.c,v 1.54 1996/04/22 01:38:32 christos Exp $ */ /* @@ -1548,8 +1548,7 @@ coredump(struct proc *p) } /* Don't dump if will exceed file size limit. */ - if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >= - p->p_rlimit[RLIMIT_CORE].rlim_cur) + if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >= lim_cur(RLIMIT_CORE)) return (EFBIG); if (incrash && nosuidcoredump == 3) { diff --git a/sys/kern/sys_generic.c b/sys/kern/sys_generic.c index ccb8502ed21..5fb4cf04258 100644 --- a/sys/kern/sys_generic.c +++ b/sys/kern/sys_generic.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sys_generic.c,v 1.123 2019/01/21 23:41:26 cheloha Exp $ */ +/* $OpenBSD: sys_generic.c,v 1.124 2019/06/21 09:39:48 visa Exp $ */ /* $NetBSD: sys_generic.c,v 1.24 1996/03/29 00:25:32 cgd Exp $ */ /* @@ -935,7 +935,7 @@ doppoll(struct proc *p, struct pollfd *fds, u_int nfds, int timo, ncoll, i, s, error; /* Standards say no more than MAX_OPEN; this is possibly better. */ - if (nfds > min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles)) + if (nfds > min((int)lim_cur(RLIMIT_NOFILE), maxfiles)) return (EINVAL); /* optimize for the default case, of a small nfds value */ diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c index 2d5d00e5eff..c02f8f0cba1 100644 --- a/sys/kern/vfs_vnops.c +++ b/sys/kern/vfs_vnops.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vfs_vnops.c,v 1.97 2018/08/20 16:00:22 mpi Exp $ */ +/* $OpenBSD: vfs_vnops.c,v 1.98 2019/06/21 09:39:48 visa Exp $ */ /* $NetBSD: vfs_vnops.c,v 1.20 1996/02/04 02:18:41 christos Exp $ */ /* @@ -247,7 +247,7 @@ vn_fsizechk(struct vnode *vp, struct uio *uio, int ioflag, ssize_t *overrun) *overrun = 0; if (vp->v_type == VREG && p != NULL && !(ioflag & IO_NOLIMIT)) { - rlim_t limit = p->p_rlimit[RLIMIT_FSIZE].rlim_cur; + rlim_t limit = lim_cur_proc(p, RLIMIT_FSIZE); /* if already at or over the limit, send the signal and fail */ if (uio->uio_offset >= limit) { |