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