sys/kern/kern_caps.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | /* * Copyright (c) 2023 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@backplane.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include <sys/param.h> #include <sys/acct.h> #include <sys/caps.h> #include <sys/systm.h> #include <sys/sysmsg.h> #include <sys/kernel.h> #include <sys/lock.h> #include <sys/proc.h> #include <sys/malloc.h> #include <sys/pioctl.h> #include <sys/resourcevar.h> #include <sys/jail.h> #include <sys/lockf.h> #include <sys/spinlock.h> #include <sys/sysctl.h> #include <sys/spinlock2.h> __read_mostly static int caps_available = 1; SYSCTL_INT(_kern, OID_AUTO, caps_available, CTLFLAG_RW, &caps_available, 0 , ""); /* * Quick check for cap restriction in cred (no bounds checks), * return cap flags. */ static __inline int caps_check_cred(struct ucred *cred, int cap) { __syscapelm_t elm; cap &= ~__SYSCAP_XFLAGS; elm = cred->cr_caps.caps[__SYSCAP_INDEX(cap)]; return ((int)(elm >> __SYSCAP_SHIFT(cap)) & __SYSCAP_ALL); } /* * int syscap_get(int cap, void *data, size_t bytes); */ int sys_syscap_get(struct sysmsg *sysmsg, const struct syscap_get_args *uap) { struct ucred *cred; int cap = uap->cap & ~__SYSCAP_XFLAGS; int res; int error; if (cap < 0) return EINVAL; if (cap >= __SYSCAP_COUNT) return EOPNOTSUPP; if (uap->bytes && uap->bytes < sizeof(syscap_base_t)) return EINVAL; error = 0; /* * Get capability restriction from parent pid */ if (uap->cap & __SYSCAP_INPARENT) { struct proc *pp; pp = pfind(curproc->p_ppid); if (pp == NULL) return EINVAL; lwkt_gettoken_shared(&pp->p_token); /* protect cred */ cred = pp->p_ucred; crhold(cred); lwkt_reltoken(&pp->p_token); PRELE(pp); /* from pfind */ } else { cred = curthread->td_ucred; } /* * No resource data by default */ if (uap->data && uap->bytes) { syscap_base_t base; base.res = SYSCAP_RESOURCE_EOF; base.len = sizeof(base); error = copyout(&base, uap->data, sizeof(base)); } /* * Get resource bits */ if (error == 0) { res = (int)(cred->cr_caps.caps[__SYSCAP_INDEX(cap)] >> __SYSCAP_SHIFT(cap)); res &= __SYSCAP_BITS_MASK; sysmsg->sysmsg_result = res; } if (uap->cap & __SYSCAP_INPARENT) crfree(cred); return error; } /* * int syscap_set(int cap, int flags, const void *data, size_t bytes) */ int sys_syscap_set(struct sysmsg *sysmsg, const struct syscap_set_args *uap) { struct ucred *cred; struct proc *pp; int cap = uap->cap & ~__SYSCAP_XFLAGS; int res; int error; int flags = uap->flags; __syscapelm_t anymask; if (cap < 0 || cap >= __SYSCAP_COUNT) return EINVAL; if (flags & ~__SYSCAP_BITS_MASK) return EINVAL; if (uap->data || uap->bytes) return EINVAL; error = 0; /* * Get capability restriction from parent pid. We can only * mess with the parent if it is running under the same userid * and prison. */ if (uap->cap & __SYSCAP_INPARENT) { pp = pfind(curproc->p_ppid); if (pp == NULL) return EINVAL; if (pp->p_ucred->cr_uid != curproc->p_ucred->cr_uid || pp->p_ucred->cr_prison != curproc->p_ucred->cr_prison) { PRELE(pp); /* from pfind */ return EINVAL; } } else { pp = curproc; } lwkt_gettoken(&pp->p_token); /* protect p_ucred */ cred = pp->p_ucred; /* * Calculate normalized value for requested capability and check * against the stored value. If they do not match, wire-or to * add the bits and set appropriate SYSCAP_ANY bits indicating * deviation from the root syscaps. */ res = (int)(cred->cr_caps.caps[__SYSCAP_INDEX(cap)] >> __SYSCAP_SHIFT(cap)); res &= __SYSCAP_BITS_MASK; /* * Handle resource data, if any */ /* * Set resource bits */ if (error == 0) { if (res != (res | flags)) { cred = cratom_proc(pp); anymask = (__syscapelm_t)flags << __SYSCAP_SHIFT(SYSCAP_ANY); atomic_set_64(&cred->cr_caps.caps[0], anymask); atomic_set_64(&cred->cr_caps.caps[ __SYSCAP_INDEX(cap)], ((__syscapelm_t)uap->flags << __SYSCAP_SHIFT(cap))); } sysmsg->sysmsg_result = res | uap->flags; } /* * Cleanup */ lwkt_reltoken(&pp->p_token); if (uap->cap & __SYSCAP_INPARENT) PRELE(pp); /* from pfind */ return error; } /* * Adjust capabilities for exec after the point of no return. * * This function shifts the EXEC bits into the SELF bits and * replicates the EXEC bits. */ void caps_exec(struct proc *p) { struct ucred *cred; __syscapelm_t elm; int changed = 0; int i; /* * Dry-run caps inheritance, did anything change? * * caps inheritance basically shifts the EXEC bits into the SELF bits, * and then replicates the EXEC bits. We have to avoid shifting any * 1's from the SELF bits into the adjacent EXEC bits that may have * previously been 0. */ cred = p->p_ucred; for (i = 0; i < __SYSCAP_NUMELMS; ++i) { elm = cred->cr_caps.caps[i]; elm = ((elm & __SYSCAP_EXECMASK) >> 1) | (elm & __SYSCAP_EXECMASK); if (elm != cred->cr_caps.caps[i]) changed = 1; } /* * Yes, setup a new ucred for the process */ if (changed) { cratom_proc(p); cred = p->p_ucred; for (i = 0; i < __SYSCAP_NUMELMS; ++i) { elm = cred->cr_caps.caps[i]; elm = ((elm & __SYSCAP_EXECMASK) >> 1) | (elm & __SYSCAP_EXECMASK); cred->cr_caps.caps[i] = elm; } } } /* * Return the raw flags for the requested capability. */ int caps_get(struct ucred *cred, int cap) { int res; cap &= ~__SYSCAP_XFLAGS; if (cap < 0 || cap >= __SYSCAP_COUNT) return 0; res = (int)(cred->cr_caps.caps[__SYSCAP_INDEX(cap)] >> __SYSCAP_SHIFT(cap)); res &= __SYSCAP_BITS_MASK; return res; } /* * Set capability restriction bits */ void caps_set_locked(struct proc *p, int cap, int flags) { struct ucred *cred; __syscapelm_t elm; cap &= ~__SYSCAP_XFLAGS; if (cap < 0 || cap >= __SYSCAP_COUNT) return; cred = cratom_proc(p); elm = (__syscapelm_t)flags << __SYSCAP_SHIFT(SYSCAP_ANY); atomic_set_64(&cred->cr_caps.caps[0], elm); elm = (__syscapelm_t)flags << __SYSCAP_SHIFT(cap); atomic_set_64(&cred->cr_caps.caps[ __SYSCAP_INDEX(cap)], elm); } /* * Returns error code if restricted, 0 on success. * * These are more sophisticated versions of the baseline caps checks. * cr_prison capabilities are also checked, and some capabilities may * imply several tests. */ int caps_priv_check(struct ucred *cred, int cap) { int res; if (cred == NULL) { if (cap & __SYSCAP_NULLCRED) return 0; return EPERM; } /* * Uid must be 0 unless NOROOTTEST is requested. If requested * it means the caller is depending on e.g. /dev/blah perms. * * When root is generally required, the WHEELOK flag allows wheel * group rather than root. */ if (cred->cr_uid != 0 && (cap & __SYSCAP_NOROOTTEST) == 0) { if ((cap & __SYSCAP_WHEELOK) == 0 || !groupmember(0, cred)) return EPERM; } res = caps_check_cred(cred, cap); if (cap & __SYSCAP_GROUP_MASK) { cap = (cap & __SYSCAP_GROUP_MASK) >> __SYSCAP_GROUP_SHIFT; res |= caps_check_cred(cred, cap); } if (res & __SYSCAP_SELF) return EPERM; return (prison_priv_check(cred, cap)); } int caps_priv_check_td(thread_t td, int cap) { struct ucred *cred; if (td->td_lwp == NULL) /* not user thread */ return 0; cred = td->td_ucred; return caps_priv_check(cred, cap); } int caps_priv_check_self(int cap) { return (caps_priv_check_td(curthread, cap)); } |