1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2006 nCircle Network Security, Inc. 5 * Copyright (c) 2009 Robert N. M. Watson 6 * Copyright (c) 2020 Mariusz Zaborski <oshogbo@FreeBSD.org> 7 * All rights reserved. 8 * 9 * This software was developed by Robert N. M. Watson for the TrustedBSD 10 * Project under contract to nCircle Network Security, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY, 25 * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/jail.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/mutex.h> 42 #include <sys/sx.h> 43 #include <sys/priv.h> 44 #include <sys/proc.h> 45 #include <sys/sdt.h> 46 #include <sys/sysctl.h> 47 #include <sys/systm.h> 48 49 #include <security/mac/mac_framework.h> 50 51 /* 52 * `suser_enabled' (which can be set by the security.bsd.suser_enabled 53 * sysctl) determines whether the system 'super-user' policy is in effect. If 54 * it is nonzero, an effective uid of 0 connotes special privilege, 55 * overriding many mandatory and discretionary protections. If it is zero, 56 * uid 0 is offered no special privilege in the kernel security policy. 57 * Setting it to zero may seriously impact the functionality of many existing 58 * userland programs, and should not be done without careful consideration of 59 * the consequences. 60 */ 61 62 static bool 63 suser_enabled(struct ucred *cred) 64 { 65 66 return (prison_allow(cred, PR_ALLOW_SUSER) ? true : false); 67 } 68 69 static void inline 70 prison_suser_set(struct prison *pr, int enabled) 71 { 72 73 if (enabled) { 74 pr->pr_allow |= PR_ALLOW_SUSER; 75 } else { 76 pr->pr_allow &= ~PR_ALLOW_SUSER; 77 } 78 } 79 80 static int 81 sysctl_kern_suser_enabled(SYSCTL_HANDLER_ARGS) 82 { 83 struct prison *pr, *cpr; 84 struct ucred *cred; 85 int descend, error, enabled; 86 87 cred = req->td->td_ucred; 88 enabled = suser_enabled(cred); 89 90 error = sysctl_handle_int(oidp, &enabled, 0, req); 91 if (error || !req->newptr) 92 return (error); 93 94 pr = cred->cr_prison; 95 sx_slock(&allprison_lock); 96 mtx_lock(&pr->pr_mtx); 97 98 prison_suser_set(pr, enabled); 99 if (!enabled) { 100 FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend) { 101 prison_suser_set(cpr, 0); 102 } 103 } 104 mtx_unlock(&pr->pr_mtx); 105 sx_sunlock(&allprison_lock); 106 return (0); 107 } 108 109 SYSCTL_PROC(_security_bsd, OID_AUTO, suser_enabled, CTLTYPE_INT | 110 CTLFLAG_RWTUN | CTLFLAG_PRISON | CTLFLAG_MPSAFE, 0, 0, 111 &sysctl_kern_suser_enabled, "I", "Processes with uid 0 have privilege"); 112 113 static int unprivileged_mlock = 1; 114 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_mlock, CTLFLAG_RWTUN, 115 &unprivileged_mlock, 0, "Allow non-root users to call mlock(2)"); 116 117 static int unprivileged_read_msgbuf = 1; 118 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf, 119 CTLFLAG_RW, &unprivileged_read_msgbuf, 0, 120 "Unprivileged processes may read the kernel message buffer"); 121 122 SDT_PROVIDER_DEFINE(priv); 123 SDT_PROBE_DEFINE1(priv, kernel, priv_check, priv__ok, "int"); 124 SDT_PROBE_DEFINE1(priv, kernel, priv_check, priv__err, "int"); 125 126 static __always_inline int 127 priv_check_cred_pre(struct ucred *cred, int priv) 128 { 129 int error; 130 131 #ifdef MAC 132 error = mac_priv_check(cred, priv); 133 #else 134 error = 0; 135 #endif 136 return (error); 137 } 138 139 static __always_inline int 140 priv_check_cred_post(struct ucred *cred, int priv, int error, bool handled) 141 { 142 143 if (__predict_true(handled)) 144 goto out; 145 /* 146 * Now check with MAC, if enabled, to see if a policy module grants 147 * privilege. 148 */ 149 #ifdef MAC 150 if (mac_priv_grant(cred, priv) == 0) { 151 error = 0; 152 goto out; 153 } 154 #endif 155 156 /* 157 * The default is deny, so if no policies have granted it, reject 158 * with a privilege error here. 159 */ 160 error = EPERM; 161 out: 162 if (SDT_PROBES_ENABLED()) { 163 if (error) 164 SDT_PROBE1(priv, kernel, priv_check, priv__err, priv); 165 else 166 SDT_PROBE1(priv, kernel, priv_check, priv__ok, priv); 167 } 168 return (error); 169 } 170 171 /* 172 * Check a credential for privilege. Lots of good reasons to deny privilege; 173 * only a few to grant it. 174 */ 175 int 176 priv_check_cred(struct ucred *cred, int priv) 177 { 178 int error; 179 180 KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege %d", 181 priv)); 182 183 switch (priv) { 184 case PRIV_VFS_LOOKUP: 185 return (priv_check_cred_vfs_lookup(cred)); 186 case PRIV_VFS_GENERATION: 187 return (priv_check_cred_vfs_generation(cred)); 188 } 189 190 /* 191 * We first evaluate policies that may deny the granting of 192 * privilege unilaterally. 193 */ 194 error = priv_check_cred_pre(cred, priv); 195 if (error) 196 goto out; 197 198 /* 199 * Jail policy will restrict certain privileges that may otherwise be 200 * be granted. 201 */ 202 error = prison_priv_check(cred, priv); 203 if (error) 204 goto out; 205 206 if (unprivileged_mlock) { 207 /* 208 * Allow unprivileged users to call mlock(2)/munlock(2) and 209 * mlockall(2)/munlockall(2). 210 */ 211 switch (priv) { 212 case PRIV_VM_MLOCK: 213 case PRIV_VM_MUNLOCK: 214 error = 0; 215 goto out; 216 } 217 } 218 219 if (unprivileged_read_msgbuf) { 220 /* 221 * Allow an unprivileged user to read the kernel message 222 * buffer. 223 */ 224 if (priv == PRIV_MSGBUF) { 225 error = 0; 226 goto out; 227 } 228 } 229 230 /* 231 * Having determined if privilege is restricted by various policies, 232 * now determine if privilege is granted. At this point, any policy 233 * may grant privilege. For now, we allow short-circuit boolean 234 * evaluation, so may not call all policies. Perhaps we should. 235 * 236 * Superuser policy grants privilege based on the effective (or in 237 * the case of specific privileges, real) uid being 0. We allow the 238 * superuser policy to be globally disabled, although this is 239 * currenty of limited utility. 240 */ 241 if (suser_enabled(cred)) { 242 switch (priv) { 243 case PRIV_MAXFILES: 244 case PRIV_MAXPROC: 245 case PRIV_PROC_LIMIT: 246 if (cred->cr_ruid == 0) { 247 error = 0; 248 goto out; 249 } 250 break; 251 case PRIV_VFS_READ_DIR: 252 /* 253 * Allow PRIV_VFS_READ_DIR for root if we're not in a 254 * jail, otherwise deny unless a MAC policy grants it. 255 */ 256 if (jailed(cred)) 257 break; 258 /* FALLTHROUGH */ 259 default: 260 if (cred->cr_uid == 0) { 261 error = 0; 262 goto out; 263 } 264 break; 265 } 266 } 267 268 /* 269 * Writes to kernel/physical memory are a typical root-only operation, 270 * but non-root users are expected to be able to read it (provided they 271 * have permission to access /dev/[k]mem). 272 */ 273 if (priv == PRIV_KMEM_READ) { 274 error = 0; 275 goto out; 276 } 277 278 /* 279 * Allow unprivileged process debugging on a per-jail basis. 280 * Do this here instead of prison_priv_check(), so it can also 281 * apply to prison0. 282 */ 283 if (priv == PRIV_DEBUG_UNPRIV) { 284 if (prison_allow(cred, PR_ALLOW_UNPRIV_DEBUG)) { 285 error = 0; 286 goto out; 287 } 288 } 289 290 return (priv_check_cred_post(cred, priv, error, false)); 291 out: 292 return (priv_check_cred_post(cred, priv, error, true)); 293 } 294 295 int 296 priv_check(struct thread *td, int priv) 297 { 298 299 KASSERT(td == curthread, ("priv_check: td != curthread")); 300 301 return (priv_check_cred(td->td_ucred, priv)); 302 } 303 304 static int __noinline 305 priv_check_cred_vfs_lookup_slow(struct ucred *cred) 306 { 307 int error; 308 309 error = priv_check_cred_pre(cred, PRIV_VFS_LOOKUP); 310 if (error) 311 goto out; 312 313 if (cred->cr_uid == 0 && suser_enabled(cred)) { 314 error = 0; 315 goto out; 316 } 317 318 return (priv_check_cred_post(cred, PRIV_VFS_LOOKUP, error, false)); 319 out: 320 return (priv_check_cred_post(cred, PRIV_VFS_LOOKUP, error, true)); 321 322 } 323 324 int 325 priv_check_cred_vfs_lookup(struct ucred *cred) 326 { 327 int error; 328 329 if (__predict_false(mac_priv_check_fp_flag || 330 mac_priv_grant_fp_flag || SDT_PROBES_ENABLED())) 331 return (priv_check_cred_vfs_lookup_slow(cred)); 332 333 error = EPERM; 334 if (cred->cr_uid == 0 && suser_enabled(cred)) 335 error = 0; 336 return (error); 337 } 338 339 int 340 priv_check_cred_vfs_lookup_nomac(struct ucred *cred) 341 { 342 int error; 343 344 if (__predict_false(mac_priv_check_fp_flag || 345 mac_priv_grant_fp_flag || SDT_PROBES_ENABLED())) 346 return (EAGAIN); 347 348 error = EPERM; 349 if (cred->cr_uid == 0 && suser_enabled(cred)) 350 error = 0; 351 return (error); 352 } 353 354 static int __noinline 355 priv_check_cred_vfs_generation_slow(struct ucred *cred) 356 { 357 int error; 358 359 error = priv_check_cred_pre(cred, PRIV_VFS_GENERATION); 360 if (error) 361 goto out; 362 363 if (jailed(cred)) { 364 error = EPERM; 365 goto out; 366 } 367 368 if (cred->cr_uid == 0 && suser_enabled(cred)) { 369 error = 0; 370 goto out; 371 } 372 373 return (priv_check_cred_post(cred, PRIV_VFS_GENERATION, error, false)); 374 out: 375 return (priv_check_cred_post(cred, PRIV_VFS_GENERATION, error, true)); 376 377 } 378 379 int 380 priv_check_cred_vfs_generation(struct ucred *cred) 381 { 382 int error; 383 384 if (__predict_false(mac_priv_check_fp_flag || 385 mac_priv_grant_fp_flag || SDT_PROBES_ENABLED())) 386 return (priv_check_cred_vfs_generation_slow(cred)); 387 388 error = EPERM; 389 if (!jailed(cred) && cred->cr_uid == 0 && suser_enabled(cred)) 390 error = 0; 391 return (error); 392 } 393