Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_rlimit.c revision 1.1.6.2
      1  1.1.6.2  christos /*	$NetBSD: netbsd32_rlimit.c,v 1.1.6.2 2019/06/10 22:07:02 christos Exp $	*/
      2  1.1.6.2  christos 
      3  1.1.6.2  christos /*
      4  1.1.6.2  christos  * Copyright (c) 1998, 2001, 2008, 2018 Matthew R. Green
      5  1.1.6.2  christos  * All rights reserved.
      6  1.1.6.2  christos  *
      7  1.1.6.2  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1.6.2  christos  * modification, are permitted provided that the following conditions
      9  1.1.6.2  christos  * are met:
     10  1.1.6.2  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1.6.2  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1.6.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1.6.2  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1.6.2  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1.6.2  christos  *
     16  1.1.6.2  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1.6.2  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1.6.2  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1.6.2  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1.6.2  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1.6.2  christos  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1.6.2  christos  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1.6.2  christos  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1.6.2  christos  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1.6.2  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1.6.2  christos  * SUCH DAMAGE.
     27  1.1.6.2  christos  *
     28  1.1.6.2  christos  * from: NetBSD: netbsd32_netbsd.c,v 1.218 2018/08/10 21:44:58 pgoyette Exp
     29  1.1.6.2  christos  */
     30  1.1.6.2  christos 
     31  1.1.6.2  christos /* rlimit netbsd32 related code */
     32  1.1.6.2  christos 
     33  1.1.6.2  christos #include <sys/cdefs.h>
     34  1.1.6.2  christos __KERNEL_RCSID(0, "$NetBSD: netbsd32_rlimit.c,v 1.1.6.2 2019/06/10 22:07:02 christos Exp $");
     35  1.1.6.2  christos 
     36  1.1.6.2  christos #include <sys/param.h>
     37  1.1.6.2  christos #include <sys/systm.h>
     38  1.1.6.2  christos #include <sys/resource.h>
     39  1.1.6.2  christos #include <sys/exec.h>
     40  1.1.6.2  christos 
     41  1.1.6.2  christos #include <compat/netbsd32/netbsd32.h>
     42  1.1.6.2  christos #include <compat/netbsd32/netbsd32_syscall.h>
     43  1.1.6.2  christos #include <compat/netbsd32/netbsd32_syscallargs.h>
     44  1.1.6.2  christos #include <compat/netbsd32/netbsd32_conv.h>
     45  1.1.6.2  christos 
     46  1.1.6.2  christos #define LIMITCHECK(a, b) ((a) != RLIM_INFINITY && (a) > (b))
     47  1.1.6.2  christos 
     48  1.1.6.2  christos static void
     49  1.1.6.2  christos fixlimit(int which, struct rlimit *alim)
     50  1.1.6.2  christos {
     51  1.1.6.2  christos 	switch (which) {
     52  1.1.6.2  christos 	case RLIMIT_DATA:
     53  1.1.6.2  christos 		if (LIMITCHECK(alim->rlim_cur, MAXDSIZ32))
     54  1.1.6.2  christos 			alim->rlim_cur = MAXDSIZ32;
     55  1.1.6.2  christos 		if (LIMITCHECK(alim->rlim_max, MAXDSIZ32))
     56  1.1.6.2  christos 			alim->rlim_max = MAXDSIZ32;
     57  1.1.6.2  christos 		return;
     58  1.1.6.2  christos 	case RLIMIT_STACK:
     59  1.1.6.2  christos 		if (LIMITCHECK(alim->rlim_cur, MAXSSIZ32))
     60  1.1.6.2  christos 			alim->rlim_cur = MAXSSIZ32;
     61  1.1.6.2  christos 		if (LIMITCHECK(alim->rlim_max, MAXSSIZ32))
     62  1.1.6.2  christos 			alim->rlim_max = MAXSSIZ32;
     63  1.1.6.2  christos 		return;
     64  1.1.6.2  christos 	default:
     65  1.1.6.2  christos 		return;
     66  1.1.6.2  christos 	}
     67  1.1.6.2  christos }
     68  1.1.6.2  christos 
     69  1.1.6.2  christos int
     70  1.1.6.2  christos netbsd32_getrlimit(struct lwp *l, const struct netbsd32_getrlimit_args *uap,
     71  1.1.6.2  christos     register_t *retval)
     72  1.1.6.2  christos {
     73  1.1.6.2  christos 	/* {
     74  1.1.6.2  christos 		syscallarg(int) which;
     75  1.1.6.2  christos 		syscallarg(netbsd32_rlimitp_t) rlp;
     76  1.1.6.2  christos 	} */
     77  1.1.6.2  christos 	int which = SCARG(uap, which);
     78  1.1.6.2  christos 	struct rlimit alim;
     79  1.1.6.2  christos 
     80  1.1.6.2  christos 	if ((u_int)which >= RLIM_NLIMITS)
     81  1.1.6.2  christos 		return EINVAL;
     82  1.1.6.2  christos 
     83  1.1.6.2  christos 	alim = l->l_proc->p_rlimit[which];
     84  1.1.6.2  christos 
     85  1.1.6.2  christos 	fixlimit(which, &alim);
     86  1.1.6.2  christos 
     87  1.1.6.2  christos 	return copyout(&alim, SCARG_P32(uap, rlp), sizeof(alim));
     88  1.1.6.2  christos }
     89  1.1.6.2  christos 
     90  1.1.6.2  christos int
     91  1.1.6.2  christos netbsd32_setrlimit(struct lwp *l, const struct netbsd32_setrlimit_args *uap,
     92  1.1.6.2  christos     register_t *retval)
     93  1.1.6.2  christos {
     94  1.1.6.2  christos 	/* {
     95  1.1.6.2  christos 		syscallarg(int) which;
     96  1.1.6.2  christos 		syscallarg(const netbsd32_rlimitp_t) rlp;
     97  1.1.6.2  christos 	} */
     98  1.1.6.2  christos 		int which = SCARG(uap, which);
     99  1.1.6.2  christos 	struct rlimit alim;
    100  1.1.6.2  christos 	int error;
    101  1.1.6.2  christos 
    102  1.1.6.2  christos 	if ((u_int)which >= RLIM_NLIMITS)
    103  1.1.6.2  christos 		return EINVAL;
    104  1.1.6.2  christos 
    105  1.1.6.2  christos 	error = copyin(SCARG_P32(uap, rlp), &alim, sizeof(struct rlimit));
    106  1.1.6.2  christos 	if (error)
    107  1.1.6.2  christos 		return (error);
    108  1.1.6.2  christos 
    109  1.1.6.2  christos 	fixlimit(which, &alim);
    110  1.1.6.2  christos 
    111  1.1.6.2  christos 	return dosetrlimit(l, l->l_proc, which, &alim);
    112  1.1.6.2  christos }
    113  1.1.6.2  christos 
    114  1.1.6.2  christos void
    115  1.1.6.2  christos netbsd32_adjust_limits(struct proc *p)
    116  1.1.6.2  christos {
    117  1.1.6.2  christos 	static const struct {
    118  1.1.6.2  christos 		int id;
    119  1.1.6.2  christos 		rlim_t lim;
    120  1.1.6.2  christos 	} lm[] = {
    121  1.1.6.2  christos 		{ RLIMIT_DATA,	MAXDSIZ32 },
    122  1.1.6.2  christos 		{ RLIMIT_STACK, MAXSSIZ32 },
    123  1.1.6.2  christos 	};
    124  1.1.6.2  christos 	size_t i;
    125  1.1.6.2  christos 	struct plimit *lim;
    126  1.1.6.2  christos 	struct rlimit *rlim;
    127  1.1.6.2  christos 
    128  1.1.6.2  christos 	/*
    129  1.1.6.2  christos 	 * We can only reduce the current limits, we cannot stop external
    130  1.1.6.2  christos 	 * processes from changing them (eg via sysctl) later on.
    131  1.1.6.2  christos 	 * So there is no point trying to lock out such changes here.
    132  1.1.6.2  christos 	 *
    133  1.1.6.2  christos 	 * If we assume that rlim_cur/max are accessed using atomic
    134  1.1.6.2  christos 	 * operations, we don't need to lock against any other updates
    135  1.1.6.2  christos 	 * that might happen if the plimit structure is shared writable
    136  1.1.6.2  christos 	 * between multiple processes.
    137  1.1.6.2  christos 	 */
    138  1.1.6.2  christos 
    139  1.1.6.2  christos 	/* Scan to determine is any limits are out of range */
    140  1.1.6.2  christos 	lim = p->p_limit;
    141  1.1.6.2  christos 	for (i = 0; ; i++) {
    142  1.1.6.2  christos 		if (i >= __arraycount(lm))
    143  1.1.6.2  christos 			/* All in range */
    144  1.1.6.2  christos 			return;
    145  1.1.6.2  christos 		rlim = lim->pl_rlimit + lm[i].id;
    146  1.1.6.2  christos 		if (LIMITCHECK(rlim->rlim_cur, lm[i].lim))
    147  1.1.6.2  christos 			break;
    148  1.1.6.2  christos 		if (LIMITCHECK(rlim->rlim_max, lm[i].lim))
    149  1.1.6.2  christos 			break;
    150  1.1.6.2  christos 	}
    151  1.1.6.2  christos 
    152  1.1.6.2  christos 	lim_privatise(p);
    153  1.1.6.2  christos 
    154  1.1.6.2  christos 	lim = p->p_limit;
    155  1.1.6.2  christos 	for (i = 0; i < __arraycount(lm); i++) {
    156  1.1.6.2  christos 		rlim = lim->pl_rlimit + lm[i].id;
    157  1.1.6.2  christos 		if (LIMITCHECK(rlim->rlim_cur, lm[i].lim))
    158  1.1.6.2  christos 			rlim->rlim_cur = lm[i].lim;
    159  1.1.6.2  christos 		if (LIMITCHECK(rlim->rlim_max, lm[i].lim))
    160  1.1.6.2  christos 			rlim->rlim_max = lm[i].lim;
    161  1.1.6.2  christos 	}
    162  1.1.6.2  christos }
    163