1*650cb29cSsimonb /* $NetBSD: netbsd32_rlimit.c,v 1.2 2021/01/19 03:20:13 simonb Exp $ */
280c61288Smrg
380c61288Smrg /*
480c61288Smrg * Copyright (c) 1998, 2001, 2008, 2018 Matthew R. Green
580c61288Smrg * All rights reserved.
680c61288Smrg *
780c61288Smrg * Redistribution and use in source and binary forms, with or without
880c61288Smrg * modification, are permitted provided that the following conditions
980c61288Smrg * are met:
1080c61288Smrg * 1. Redistributions of source code must retain the above copyright
1180c61288Smrg * notice, this list of conditions and the following disclaimer.
1280c61288Smrg * 2. Redistributions in binary form must reproduce the above copyright
1380c61288Smrg * notice, this list of conditions and the following disclaimer in the
1480c61288Smrg * documentation and/or other materials provided with the distribution.
1580c61288Smrg *
1680c61288Smrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1780c61288Smrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1880c61288Smrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1980c61288Smrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2080c61288Smrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2180c61288Smrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2280c61288Smrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2380c61288Smrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2480c61288Smrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2580c61288Smrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2680c61288Smrg * SUCH DAMAGE.
2780c61288Smrg *
2880c61288Smrg * from: NetBSD: netbsd32_netbsd.c,v 1.218 2018/08/10 21:44:58 pgoyette Exp
2980c61288Smrg */
3080c61288Smrg
3180c61288Smrg /* rlimit netbsd32 related code */
3280c61288Smrg
3380c61288Smrg #include <sys/cdefs.h>
34*650cb29cSsimonb __KERNEL_RCSID(0, "$NetBSD: netbsd32_rlimit.c,v 1.2 2021/01/19 03:20:13 simonb Exp $");
3580c61288Smrg
3680c61288Smrg #include <sys/param.h>
3780c61288Smrg #include <sys/systm.h>
3880c61288Smrg #include <sys/resource.h>
3980c61288Smrg #include <sys/exec.h>
4080c61288Smrg
4180c61288Smrg #include <compat/netbsd32/netbsd32.h>
4280c61288Smrg #include <compat/netbsd32/netbsd32_syscall.h>
4380c61288Smrg #include <compat/netbsd32/netbsd32_syscallargs.h>
4480c61288Smrg #include <compat/netbsd32/netbsd32_conv.h>
4580c61288Smrg
4680c61288Smrg #define LIMITCHECK(a, b) ((a) != RLIM_INFINITY && (a) > (b))
4780c61288Smrg
4880c61288Smrg static void
fixlimit(int which,struct rlimit * alim)4980c61288Smrg fixlimit(int which, struct rlimit *alim)
5080c61288Smrg {
5180c61288Smrg switch (which) {
5280c61288Smrg case RLIMIT_DATA:
5380c61288Smrg if (LIMITCHECK(alim->rlim_cur, MAXDSIZ32))
5480c61288Smrg alim->rlim_cur = MAXDSIZ32;
5580c61288Smrg if (LIMITCHECK(alim->rlim_max, MAXDSIZ32))
5680c61288Smrg alim->rlim_max = MAXDSIZ32;
5780c61288Smrg return;
5880c61288Smrg case RLIMIT_STACK:
5980c61288Smrg if (LIMITCHECK(alim->rlim_cur, MAXSSIZ32))
6080c61288Smrg alim->rlim_cur = MAXSSIZ32;
6180c61288Smrg if (LIMITCHECK(alim->rlim_max, MAXSSIZ32))
6280c61288Smrg alim->rlim_max = MAXSSIZ32;
6380c61288Smrg return;
6480c61288Smrg default:
6580c61288Smrg return;
6680c61288Smrg }
6780c61288Smrg }
6880c61288Smrg
6980c61288Smrg int
netbsd32_getrlimit(struct lwp * l,const struct netbsd32_getrlimit_args * uap,register_t * retval)7080c61288Smrg netbsd32_getrlimit(struct lwp *l, const struct netbsd32_getrlimit_args *uap,
7180c61288Smrg register_t *retval)
7280c61288Smrg {
7380c61288Smrg /* {
7480c61288Smrg syscallarg(int) which;
7580c61288Smrg syscallarg(netbsd32_rlimitp_t) rlp;
7680c61288Smrg } */
7780c61288Smrg int which = SCARG(uap, which);
7880c61288Smrg struct rlimit alim;
7980c61288Smrg
8080c61288Smrg if ((u_int)which >= RLIM_NLIMITS)
8180c61288Smrg return EINVAL;
8280c61288Smrg
8380c61288Smrg alim = l->l_proc->p_rlimit[which];
8480c61288Smrg
8580c61288Smrg fixlimit(which, &alim);
8680c61288Smrg
8780c61288Smrg return copyout(&alim, SCARG_P32(uap, rlp), sizeof(alim));
8880c61288Smrg }
8980c61288Smrg
9080c61288Smrg int
netbsd32_setrlimit(struct lwp * l,const struct netbsd32_setrlimit_args * uap,register_t * retval)9180c61288Smrg netbsd32_setrlimit(struct lwp *l, const struct netbsd32_setrlimit_args *uap,
9280c61288Smrg register_t *retval)
9380c61288Smrg {
9480c61288Smrg /* {
9580c61288Smrg syscallarg(int) which;
9680c61288Smrg syscallarg(const netbsd32_rlimitp_t) rlp;
9780c61288Smrg } */
9880c61288Smrg int which = SCARG(uap, which);
9980c61288Smrg struct rlimit alim;
10080c61288Smrg int error;
10180c61288Smrg
10280c61288Smrg if ((u_int)which >= RLIM_NLIMITS)
10380c61288Smrg return EINVAL;
10480c61288Smrg
10580c61288Smrg error = copyin(SCARG_P32(uap, rlp), &alim, sizeof(struct rlimit));
10680c61288Smrg if (error)
107*650cb29cSsimonb return error;
10880c61288Smrg
10980c61288Smrg fixlimit(which, &alim);
11080c61288Smrg
11180c61288Smrg return dosetrlimit(l, l->l_proc, which, &alim);
11280c61288Smrg }
11380c61288Smrg
11480c61288Smrg void
netbsd32_adjust_limits(struct proc * p)11580c61288Smrg netbsd32_adjust_limits(struct proc *p)
11680c61288Smrg {
11780c61288Smrg static const struct {
11880c61288Smrg int id;
11980c61288Smrg rlim_t lim;
12080c61288Smrg } lm[] = {
12180c61288Smrg { RLIMIT_DATA, MAXDSIZ32 },
12280c61288Smrg { RLIMIT_STACK, MAXSSIZ32 },
12380c61288Smrg };
12480c61288Smrg size_t i;
12580c61288Smrg struct plimit *lim;
12680c61288Smrg struct rlimit *rlim;
12780c61288Smrg
12880c61288Smrg /*
12980c61288Smrg * We can only reduce the current limits, we cannot stop external
13080c61288Smrg * processes from changing them (eg via sysctl) later on.
13180c61288Smrg * So there is no point trying to lock out such changes here.
13280c61288Smrg *
13380c61288Smrg * If we assume that rlim_cur/max are accessed using atomic
13480c61288Smrg * operations, we don't need to lock against any other updates
13580c61288Smrg * that might happen if the plimit structure is shared writable
13680c61288Smrg * between multiple processes.
13780c61288Smrg */
13880c61288Smrg
13980c61288Smrg /* Scan to determine is any limits are out of range */
14080c61288Smrg lim = p->p_limit;
14180c61288Smrg for (i = 0; ; i++) {
14280c61288Smrg if (i >= __arraycount(lm))
14380c61288Smrg /* All in range */
14480c61288Smrg return;
14580c61288Smrg rlim = lim->pl_rlimit + lm[i].id;
14680c61288Smrg if (LIMITCHECK(rlim->rlim_cur, lm[i].lim))
14780c61288Smrg break;
14880c61288Smrg if (LIMITCHECK(rlim->rlim_max, lm[i].lim))
14980c61288Smrg break;
15080c61288Smrg }
15180c61288Smrg
15280c61288Smrg lim_privatise(p);
15380c61288Smrg
15480c61288Smrg lim = p->p_limit;
15580c61288Smrg for (i = 0; i < __arraycount(lm); i++) {
15680c61288Smrg rlim = lim->pl_rlimit + lm[i].id;
15780c61288Smrg if (LIMITCHECK(rlim->rlim_cur, lm[i].lim))
15880c61288Smrg rlim->rlim_cur = lm[i].lim;
15980c61288Smrg if (LIMITCHECK(rlim->rlim_max, lm[i].lim))
16080c61288Smrg rlim->rlim_max = lm[i].lim;
16180c61288Smrg }
16280c61288Smrg }
163