17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 530cbf0d2SCasper H.S. Dik * Common Development and Distribution License (the "License"). 630cbf0d2SCasper H.S. Dik * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*634e26ecSCasper H.S. Dik * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * Privilege implementation. 287c478bd9Sstevel@tonic-gate * 297c478bd9Sstevel@tonic-gate * This file provides the infrastructure for privilege sets and limits 307c478bd9Sstevel@tonic-gate * the number of files that requires to include <sys/cred_impl.h> and/or 317c478bd9Sstevel@tonic-gate * <sys/priv_impl.h>. 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * The Solaris privilege mechanism has been designed in a 347c478bd9Sstevel@tonic-gate * future proof manner. While the kernel may use fixed size arrays 357c478bd9Sstevel@tonic-gate * and fixed bitmasks and bit values, the representation of those 367c478bd9Sstevel@tonic-gate * is kernel private. All external interfaces as well as K-to-K interfaces 377c478bd9Sstevel@tonic-gate * have been constructed in a manner to provide the maximum flexibility. 387c478bd9Sstevel@tonic-gate * 397c478bd9Sstevel@tonic-gate * There can be X privilege sets each containing Y 32 bit words. 407c478bd9Sstevel@tonic-gate * <X, Y> are constant for a kernel invocation. 417c478bd9Sstevel@tonic-gate * 427c478bd9Sstevel@tonic-gate * As a consequence, all privilege set manipulation happens in functions 437c478bd9Sstevel@tonic-gate * below. 447c478bd9Sstevel@tonic-gate * 457c478bd9Sstevel@tonic-gate */ 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate #include <sys/systm.h> 487c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 497c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 507c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 517c478bd9Sstevel@tonic-gate #include <sys/errno.h> 527c478bd9Sstevel@tonic-gate #include <sys/debug.h> 537c478bd9Sstevel@tonic-gate #include <sys/priv_impl.h> 547c478bd9Sstevel@tonic-gate #include <sys/procfs.h> 557c478bd9Sstevel@tonic-gate #include <sys/policy.h> 567c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h> 577c478bd9Sstevel@tonic-gate #include <sys/devpolicy.h> 587c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* 617c478bd9Sstevel@tonic-gate * Privilege name to number mapping table consists in the generated 627c478bd9Sstevel@tonic-gate * priv_const.c file. This lock protects against updates of the privilege 637c478bd9Sstevel@tonic-gate * names and counts; all other priv_info fields are read-only. 647c478bd9Sstevel@tonic-gate * The actual protected values are: 657c478bd9Sstevel@tonic-gate * global variable nprivs 667c478bd9Sstevel@tonic-gate * the priv_max field 677c478bd9Sstevel@tonic-gate * the priv_names field 687c478bd9Sstevel@tonic-gate * the priv names info item (cnt/strings) 697c478bd9Sstevel@tonic-gate */ 707c478bd9Sstevel@tonic-gate krwlock_t privinfo_lock; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate static boolean_t priv_valid(const cred_t *); 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate priv_set_t priv_fullset; /* set of all privileges */ 757c478bd9Sstevel@tonic-gate priv_set_t priv_unsafe; /* unsafe to exec set-uid root if these are not in L */ 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate /* 787c478bd9Sstevel@tonic-gate * Privilege initialization functions. 797c478bd9Sstevel@tonic-gate * Called from common/os/cred.c when cred_init is called. 807c478bd9Sstevel@tonic-gate */ 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate void 837c478bd9Sstevel@tonic-gate priv_init(void) 847c478bd9Sstevel@tonic-gate { 857c478bd9Sstevel@tonic-gate rw_init(&privinfo_lock, NULL, RW_DRIVER, NULL); 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate PRIV_BASIC_ASSERT(priv_basic); 887c478bd9Sstevel@tonic-gate PRIV_UNSAFE_ASSERT(&priv_unsafe); 897c478bd9Sstevel@tonic-gate priv_fillset(&priv_fullset); 907c478bd9Sstevel@tonic-gate 91*634e26ecSCasper H.S. Dik /* 92*634e26ecSCasper H.S. Dik * When booting with priv_debug set, then we'll add an additional 93*634e26ecSCasper H.S. Dik * basic privilege and we verify that it is always present in E. 94*634e26ecSCasper H.S. Dik */ 95*634e26ecSCasper H.S. Dik if (priv_debug == 1 && 96*634e26ecSCasper H.S. Dik (priv_basic_test = priv_getbyname("basic_test", PRIV_ALLOC)) >= 0) { 97*634e26ecSCasper H.S. Dik priv_addset(priv_basic, priv_basic_test); 98*634e26ecSCasper H.S. Dik } 99*634e26ecSCasper H.S. Dik 1007c478bd9Sstevel@tonic-gate devpolicy_init(); 1017c478bd9Sstevel@tonic-gate } 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* Utility functions: privilege sets as opaque data types */ 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate /* 1067c478bd9Sstevel@tonic-gate * Guts of prgetprivsize. 1077c478bd9Sstevel@tonic-gate */ 1087c478bd9Sstevel@tonic-gate int 1097c478bd9Sstevel@tonic-gate priv_prgetprivsize(prpriv_t *tmpl) 1107c478bd9Sstevel@tonic-gate { 1117c478bd9Sstevel@tonic-gate return (sizeof (prpriv_t) + 1127c478bd9Sstevel@tonic-gate PRIV_SETBYTES - sizeof (priv_chunk_t) + 1137c478bd9Sstevel@tonic-gate (tmpl ? tmpl->pr_infosize : priv_info->priv_infosize)); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate /* 1177c478bd9Sstevel@tonic-gate * Guts of prgetpriv. 1187c478bd9Sstevel@tonic-gate */ 1197c478bd9Sstevel@tonic-gate void 1207c478bd9Sstevel@tonic-gate cred2prpriv(const cred_t *cp, prpriv_t *pr) 1217c478bd9Sstevel@tonic-gate { 1227c478bd9Sstevel@tonic-gate priv_set_t *psa; 1237c478bd9Sstevel@tonic-gate int i; 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate pr->pr_nsets = PRIV_NSET; 1267c478bd9Sstevel@tonic-gate pr->pr_setsize = PRIV_SETSIZE; 1277c478bd9Sstevel@tonic-gate pr->pr_infosize = priv_info->priv_infosize; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate psa = (priv_set_t *)pr->pr_sets; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate for (i = 0; i < PRIV_NSET; i++) 1327c478bd9Sstevel@tonic-gate psa[i] = *priv_getset(cp, i); 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate priv_getinfo(cp, (char *)pr + PRIV_PRPRIV_INFO_OFFSET(pr)); 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate /* 1387c478bd9Sstevel@tonic-gate * Guts of pr_spriv: 1397c478bd9Sstevel@tonic-gate * 1407c478bd9Sstevel@tonic-gate * Set the privileges of a process. 1417c478bd9Sstevel@tonic-gate * 1427c478bd9Sstevel@tonic-gate * In order to set the privileges, the setting process will need to 1437c478bd9Sstevel@tonic-gate * have those privileges in its effective set in order to prevent 1447c478bd9Sstevel@tonic-gate * specially privileged processes to easily gain additional privileges. 1457c478bd9Sstevel@tonic-gate * Pre-existing privileges can be retained. To change any privileges, 1467c478bd9Sstevel@tonic-gate * PRIV_PROC_OWNER needs to be asserted. 1477c478bd9Sstevel@tonic-gate * 1487c478bd9Sstevel@tonic-gate * In formula: 1497c478bd9Sstevel@tonic-gate * 1507c478bd9Sstevel@tonic-gate * S' <= S || S' <= S + Ea 1517c478bd9Sstevel@tonic-gate * 1527c478bd9Sstevel@tonic-gate * the new set must either be subset of the old set or a subset of 1537c478bd9Sstevel@tonic-gate * the oldset merged with the effective set of the acting process; or just: 1547c478bd9Sstevel@tonic-gate * 1557c478bd9Sstevel@tonic-gate * S' <= S + Ea 1567c478bd9Sstevel@tonic-gate * 1577c478bd9Sstevel@tonic-gate * It's not legal to grow the limit set this way. 1587c478bd9Sstevel@tonic-gate * 1597c478bd9Sstevel@tonic-gate */ 1607c478bd9Sstevel@tonic-gate int 1617c478bd9Sstevel@tonic-gate priv_pr_spriv(proc_t *p, prpriv_t *prpriv, const cred_t *cr) 1627c478bd9Sstevel@tonic-gate { 1637c478bd9Sstevel@tonic-gate cred_t *oldcred; 1647c478bd9Sstevel@tonic-gate cred_t *newcred; 1657c478bd9Sstevel@tonic-gate int i; 1667c478bd9Sstevel@tonic-gate int err = EPERM; 1677c478bd9Sstevel@tonic-gate cred_priv_t *cp, *ocp; 1687c478bd9Sstevel@tonic-gate priv_set_t eset; 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate /* 1737c478bd9Sstevel@tonic-gate * Set must have proper dimension; infosize must be absent 1747c478bd9Sstevel@tonic-gate * or properly sized. 1757c478bd9Sstevel@tonic-gate */ 1767c478bd9Sstevel@tonic-gate if (prpriv->pr_nsets != PRIV_NSET || 1777c478bd9Sstevel@tonic-gate prpriv->pr_setsize != PRIV_SETSIZE || 1787c478bd9Sstevel@tonic-gate (prpriv->pr_infosize & (sizeof (uint32_t) - 1)) != 0 || 1797c478bd9Sstevel@tonic-gate prpriv->pr_infosize > priv_info->priv_infosize || 1807c478bd9Sstevel@tonic-gate prpriv->pr_infosize < 0) 1817c478bd9Sstevel@tonic-gate return (EINVAL); 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate if (priv_proc_cred_perm(cr, p, &oldcred, VWRITE) != 0) { 1867c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 1877c478bd9Sstevel@tonic-gate return (EPERM); 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate newcred = crdup(oldcred); 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate /* Copy the privilege sets from prpriv to newcred */ 1937c478bd9Sstevel@tonic-gate bcopy(prpriv->pr_sets, CR_PRIVSETS(newcred), PRIV_SETBYTES); 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate cp = &newcred->cr_priv; 1967c478bd9Sstevel@tonic-gate ocp = &oldcred->cr_priv; 1977c478bd9Sstevel@tonic-gate eset = CR_OEPRIV(cr); 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate priv_intersect(&CR_LPRIV(oldcred), &eset); 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate /* 2027c478bd9Sstevel@tonic-gate * Verify the constraints laid out: 2037c478bd9Sstevel@tonic-gate * for the limit set, we require that the new set is a subset 2047c478bd9Sstevel@tonic-gate * of the old limit set. 2057c478bd9Sstevel@tonic-gate * for all other sets, we require that the new set is either a 2067c478bd9Sstevel@tonic-gate * subset of the old set or a subset of the intersection of 2077c478bd9Sstevel@tonic-gate * the old limit set and the effective set of the acting process. 2087c478bd9Sstevel@tonic-gate */ 2097c478bd9Sstevel@tonic-gate for (i = 0; i < PRIV_NSET; i++) 2107c478bd9Sstevel@tonic-gate if (!priv_issubset(&cp->crprivs[i], &ocp->crprivs[i]) && 2117c478bd9Sstevel@tonic-gate (i == PRIV_LIMIT || !priv_issubset(&cp->crprivs[i], &eset))) 2127c478bd9Sstevel@tonic-gate break; 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate crfree(oldcred); 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate if (i < PRIV_NSET || !priv_valid(newcred)) 2177c478bd9Sstevel@tonic-gate goto err; 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate /* Load the settable privilege information */ 2207c478bd9Sstevel@tonic-gate if (prpriv->pr_infosize > 0) { 2217c478bd9Sstevel@tonic-gate char *x = (char *)prpriv + PRIV_PRPRIV_INFO_OFFSET(prpriv); 2227c478bd9Sstevel@tonic-gate char *lastx = x + prpriv->pr_infosize; 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate while (x < lastx) { 2257c478bd9Sstevel@tonic-gate priv_info_t *pi = (priv_info_t *)x; 2267c478bd9Sstevel@tonic-gate priv_info_uint_t *pii; 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate switch (pi->priv_info_type) { 2297c478bd9Sstevel@tonic-gate case PRIV_INFO_FLAGS: 2307c478bd9Sstevel@tonic-gate pii = (priv_info_uint_t *)x; 2317c478bd9Sstevel@tonic-gate if (pii->info.priv_info_size != sizeof (*pii)) { 2327c478bd9Sstevel@tonic-gate err = EINVAL; 2337c478bd9Sstevel@tonic-gate goto err; 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate CR_FLAGS(newcred) &= ~PRIV_USER; 2367c478bd9Sstevel@tonic-gate CR_FLAGS(newcred) |= (pii->val & PRIV_USER); 2377c478bd9Sstevel@tonic-gate break; 2387c478bd9Sstevel@tonic-gate default: 2397c478bd9Sstevel@tonic-gate err = EINVAL; 2407c478bd9Sstevel@tonic-gate goto err; 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate /* Guarantee alignment and forward progress */ 2437c478bd9Sstevel@tonic-gate if ((pi->priv_info_size & (sizeof (uint32_t) - 1)) || 2447c478bd9Sstevel@tonic-gate pi->priv_info_size < sizeof (*pi) || 2457c478bd9Sstevel@tonic-gate lastx - x > pi->priv_info_size) { 2467c478bd9Sstevel@tonic-gate err = EINVAL; 2477c478bd9Sstevel@tonic-gate goto err; 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate x += pi->priv_info_size; 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate /* 2557c478bd9Sstevel@tonic-gate * We'll try to copy the privilege aware flag; but since the 2567c478bd9Sstevel@tonic-gate * privileges sets are all individually set, they are set 2577c478bd9Sstevel@tonic-gate * as if we're privilege aware. If PRIV_AWARE wasn't set 2587c478bd9Sstevel@tonic-gate * or was explicitely unset, we need to set the flag and then 2597c478bd9Sstevel@tonic-gate * try to get rid of it. 2607c478bd9Sstevel@tonic-gate */ 2617c478bd9Sstevel@tonic-gate if ((CR_FLAGS(newcred) & PRIV_AWARE) == 0) { 2627c478bd9Sstevel@tonic-gate CR_FLAGS(newcred) |= PRIV_AWARE; 2637c478bd9Sstevel@tonic-gate priv_adjust_PA(newcred); 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate mutex_enter(&p->p_crlock); 2677c478bd9Sstevel@tonic-gate oldcred = p->p_cred; 2687c478bd9Sstevel@tonic-gate p->p_cred = newcred; 2697c478bd9Sstevel@tonic-gate mutex_exit(&p->p_crlock); 2707c478bd9Sstevel@tonic-gate crfree(oldcred); 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 2737c478bd9Sstevel@tonic-gate return (0); 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate err: 2767c478bd9Sstevel@tonic-gate crfree(newcred); 2777c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 2787c478bd9Sstevel@tonic-gate return (err); 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate priv_impl_info_t 2827c478bd9Sstevel@tonic-gate *priv_hold_implinfo(void) 2837c478bd9Sstevel@tonic-gate { 2847c478bd9Sstevel@tonic-gate rw_enter(&privinfo_lock, RW_READER); 2857c478bd9Sstevel@tonic-gate return (priv_info); 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate void 2897c478bd9Sstevel@tonic-gate priv_release_implinfo(void) 2907c478bd9Sstevel@tonic-gate { 2917c478bd9Sstevel@tonic-gate rw_exit(&privinfo_lock); 2927c478bd9Sstevel@tonic-gate } 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate size_t 2957c478bd9Sstevel@tonic-gate priv_get_implinfo_size(void) 2967c478bd9Sstevel@tonic-gate { 2977c478bd9Sstevel@tonic-gate return (privinfosize); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate /* 3027c478bd9Sstevel@tonic-gate * Return the nth privilege set 3037c478bd9Sstevel@tonic-gate */ 3047c478bd9Sstevel@tonic-gate const priv_set_t * 3057c478bd9Sstevel@tonic-gate priv_getset(const cred_t *cr, int set) 3067c478bd9Sstevel@tonic-gate { 3077c478bd9Sstevel@tonic-gate ASSERT(PRIV_VALIDSET(set)); 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate if ((CR_FLAGS(cr) & PRIV_AWARE) == 0) 3107c478bd9Sstevel@tonic-gate switch (set) { 3117c478bd9Sstevel@tonic-gate case PRIV_EFFECTIVE: 3127c478bd9Sstevel@tonic-gate return (&CR_OEPRIV(cr)); 3137c478bd9Sstevel@tonic-gate case PRIV_PERMITTED: 3147c478bd9Sstevel@tonic-gate return (&CR_OPPRIV(cr)); 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate return (&CR_PRIVS(cr)->crprivs[set]); 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate /* 3207c478bd9Sstevel@tonic-gate * Buf must be allocated by caller and contain sufficient space to 3217c478bd9Sstevel@tonic-gate * contain all additional info structures using priv_info.priv_infosize. 3227c478bd9Sstevel@tonic-gate * The buffer must be properly aligned. 3237c478bd9Sstevel@tonic-gate */ 3247c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3257c478bd9Sstevel@tonic-gate void 3267c478bd9Sstevel@tonic-gate priv_getinfo(const cred_t *cr, void *buf) 3277c478bd9Sstevel@tonic-gate { 3287c478bd9Sstevel@tonic-gate struct priv_info_uint *ii; 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate ii = buf; 3317c478bd9Sstevel@tonic-gate ii->val = CR_FLAGS(cr); 3327c478bd9Sstevel@tonic-gate ii->info.priv_info_size = (uint32_t)sizeof (*ii); 3337c478bd9Sstevel@tonic-gate ii->info.priv_info_type = PRIV_INFO_FLAGS; 3347c478bd9Sstevel@tonic-gate } 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate int 3377c478bd9Sstevel@tonic-gate priv_getbyname(const char *name, uint_t flag) 3387c478bd9Sstevel@tonic-gate { 3397c478bd9Sstevel@tonic-gate int i; 3407c478bd9Sstevel@tonic-gate int wheld = 0; 3417c478bd9Sstevel@tonic-gate int len; 3427c478bd9Sstevel@tonic-gate char *p; 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate if (flag != 0 && flag != PRIV_ALLOC) 3457c478bd9Sstevel@tonic-gate return (-EINVAL); 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate if (strncasecmp(name, "priv_", 5) == 0) 3487c478bd9Sstevel@tonic-gate name += 5; 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate rw_enter(&privinfo_lock, RW_READER); 3517c478bd9Sstevel@tonic-gate rescan: 3527c478bd9Sstevel@tonic-gate for (i = 0; i < nprivs; i++) 3537c478bd9Sstevel@tonic-gate if (strcasecmp(priv_names[i], name) == 0) { 3547c478bd9Sstevel@tonic-gate rw_exit(&privinfo_lock); 3557c478bd9Sstevel@tonic-gate return (i); 3567c478bd9Sstevel@tonic-gate } 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate if (!wheld) { 3607c478bd9Sstevel@tonic-gate if (!(flag & PRIV_ALLOC)) { 3617c478bd9Sstevel@tonic-gate rw_exit(&privinfo_lock); 3627c478bd9Sstevel@tonic-gate return (-EINVAL); 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate /* check length, validity and available space */ 3667c478bd9Sstevel@tonic-gate len = strlen(name) + 1; 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate if (len > PRIVNAME_MAX) { 3697c478bd9Sstevel@tonic-gate rw_exit(&privinfo_lock); 3707c478bd9Sstevel@tonic-gate return (-ENAMETOOLONG); 3717c478bd9Sstevel@tonic-gate } 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate for (p = (char *)name; *p != '\0'; p++) { 3747c478bd9Sstevel@tonic-gate char c = *p; 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate if (!((c >= 'A' && c <= 'Z') || 3777c478bd9Sstevel@tonic-gate (c >= 'a' && c <= 'z') || 3787c478bd9Sstevel@tonic-gate (c >= '0' && c <= '9') || 3797c478bd9Sstevel@tonic-gate c == '_')) { 3807c478bd9Sstevel@tonic-gate rw_exit(&privinfo_lock); 3817c478bd9Sstevel@tonic-gate return (-EINVAL); 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate if (!rw_tryupgrade(&privinfo_lock)) { 3867c478bd9Sstevel@tonic-gate rw_exit(&privinfo_lock); 3877c478bd9Sstevel@tonic-gate rw_enter(&privinfo_lock, RW_WRITER); 3887c478bd9Sstevel@tonic-gate wheld = 1; 3897c478bd9Sstevel@tonic-gate /* Someone may have added our privilege */ 3907c478bd9Sstevel@tonic-gate goto rescan; 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate if (nprivs == MAX_PRIVILEGE || len + privbytes > maxprivbytes) { 3957c478bd9Sstevel@tonic-gate rw_exit(&privinfo_lock); 3967c478bd9Sstevel@tonic-gate return (-ENOMEM); 3977c478bd9Sstevel@tonic-gate } 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate priv_names[i] = p = priv_str + privbytes; 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate bcopy(name, p, len); 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate /* make the priv_names[i] and privilege name globally visible */ 4047c478bd9Sstevel@tonic-gate membar_producer(); 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate /* adjust priv count and bytes count */ 4077c478bd9Sstevel@tonic-gate priv_ninfo->cnt = priv_info->priv_max = ++nprivs; 4087c478bd9Sstevel@tonic-gate privbytes += len; 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate rw_exit(&privinfo_lock); 4117c478bd9Sstevel@tonic-gate return (i); 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate /* 4157c478bd9Sstevel@tonic-gate * We can't afford locking the privileges here because of the locations 4167c478bd9Sstevel@tonic-gate * we call this from; so we make sure that the privileges table 4177c478bd9Sstevel@tonic-gate * is visible to us; it is made visible before the value of nprivs is 4187c478bd9Sstevel@tonic-gate * updated. 4197c478bd9Sstevel@tonic-gate */ 4207c478bd9Sstevel@tonic-gate const char * 4217c478bd9Sstevel@tonic-gate priv_getbynum(int priv) 4227c478bd9Sstevel@tonic-gate { 4237c478bd9Sstevel@tonic-gate int maxpriv = nprivs; 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate membar_consumer(); 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate if (priv >= 0 && priv < maxpriv) 4287c478bd9Sstevel@tonic-gate return (priv_names[priv]); 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate return (NULL); 4317c478bd9Sstevel@tonic-gate } 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate const char * 4347c478bd9Sstevel@tonic-gate priv_getsetbynum(int setno) 4357c478bd9Sstevel@tonic-gate { 4367c478bd9Sstevel@tonic-gate if (!PRIV_VALIDSET(setno)) 4377c478bd9Sstevel@tonic-gate return (NULL); 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate return (priv_setnames[setno]); 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate /* 4437c478bd9Sstevel@tonic-gate * Privilege sanity checking when setting: E <= P. 4447c478bd9Sstevel@tonic-gate */ 4457c478bd9Sstevel@tonic-gate static boolean_t 4467c478bd9Sstevel@tonic-gate priv_valid(const cred_t *cr) 4477c478bd9Sstevel@tonic-gate { 4487c478bd9Sstevel@tonic-gate return (priv_issubset(&CR_EPRIV(cr), &CR_PPRIV(cr))); 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate /* 4527c478bd9Sstevel@tonic-gate * Privilege manipulation functions 4537c478bd9Sstevel@tonic-gate * 4547c478bd9Sstevel@tonic-gate * Without knowing the details of the privilege set implementation, 4557c478bd9Sstevel@tonic-gate * opaque pointers can be used to manipulate sets at will. 4567c478bd9Sstevel@tonic-gate */ 4577c478bd9Sstevel@tonic-gate void 4587c478bd9Sstevel@tonic-gate priv_emptyset(priv_set_t *set) 4597c478bd9Sstevel@tonic-gate { 4607c478bd9Sstevel@tonic-gate bzero(set, sizeof (*set)); 4617c478bd9Sstevel@tonic-gate } 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate void 4647c478bd9Sstevel@tonic-gate priv_fillset(priv_set_t *set) 4657c478bd9Sstevel@tonic-gate { 4667c478bd9Sstevel@tonic-gate int i; 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate /* memset? */ 4697c478bd9Sstevel@tonic-gate for (i = 0; i < PRIV_SETSIZE; i++) 4707c478bd9Sstevel@tonic-gate set->pbits[i] = ~(priv_chunk_t)0; 4717c478bd9Sstevel@tonic-gate } 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate void 4747c478bd9Sstevel@tonic-gate priv_addset(priv_set_t *set, int priv) 4757c478bd9Sstevel@tonic-gate { 4767c478bd9Sstevel@tonic-gate ASSERT(priv >= 0 && priv < MAX_PRIVILEGE); 4777c478bd9Sstevel@tonic-gate __PRIV_ASSERT(set, priv); 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate void 4817c478bd9Sstevel@tonic-gate priv_delset(priv_set_t *set, int priv) 4827c478bd9Sstevel@tonic-gate { 4837c478bd9Sstevel@tonic-gate ASSERT(priv >= 0 && priv < MAX_PRIVILEGE); 4847c478bd9Sstevel@tonic-gate __PRIV_CLEAR(set, priv); 4857c478bd9Sstevel@tonic-gate } 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate boolean_t 4887c478bd9Sstevel@tonic-gate priv_ismember(const priv_set_t *set, int priv) 4897c478bd9Sstevel@tonic-gate { 4907c478bd9Sstevel@tonic-gate ASSERT(priv >= 0 && priv < MAX_PRIVILEGE); 4917c478bd9Sstevel@tonic-gate return (__PRIV_ISASSERT(set, priv) ? B_TRUE : B_FALSE); 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate #define PRIV_TEST_BODY(test) \ 4957c478bd9Sstevel@tonic-gate int i; \ 4967c478bd9Sstevel@tonic-gate \ 4977c478bd9Sstevel@tonic-gate for (i = 0; i < PRIV_SETSIZE; i++) \ 4987c478bd9Sstevel@tonic-gate if (!(test)) \ 4997c478bd9Sstevel@tonic-gate return (B_FALSE); \ 5007c478bd9Sstevel@tonic-gate \ 5017c478bd9Sstevel@tonic-gate return (B_TRUE) 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate boolean_t 5047c478bd9Sstevel@tonic-gate priv_isequalset(const priv_set_t *a, const priv_set_t *b) 5057c478bd9Sstevel@tonic-gate { 5067c478bd9Sstevel@tonic-gate return ((boolean_t)(bcmp(a, b, sizeof (*a)) == 0)); 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate boolean_t 5107c478bd9Sstevel@tonic-gate priv_isemptyset(const priv_set_t *set) 5117c478bd9Sstevel@tonic-gate { 5127c478bd9Sstevel@tonic-gate PRIV_TEST_BODY(set->pbits[i] == 0); 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate boolean_t 5167c478bd9Sstevel@tonic-gate priv_isfullset(const priv_set_t *set) 5177c478bd9Sstevel@tonic-gate { 5187c478bd9Sstevel@tonic-gate PRIV_TEST_BODY(set->pbits[i] == ~(priv_chunk_t)0); 5197c478bd9Sstevel@tonic-gate } 5207c478bd9Sstevel@tonic-gate 5217c478bd9Sstevel@tonic-gate /* 5227c478bd9Sstevel@tonic-gate * Return true if a is a subset of b 5237c478bd9Sstevel@tonic-gate */ 5247c478bd9Sstevel@tonic-gate boolean_t 5257c478bd9Sstevel@tonic-gate priv_issubset(const priv_set_t *a, const priv_set_t *b) 5267c478bd9Sstevel@tonic-gate { 5277c478bd9Sstevel@tonic-gate PRIV_TEST_BODY((a->pbits[i] | b->pbits[i]) == b->pbits[i]); 5287c478bd9Sstevel@tonic-gate } 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate #define PRIV_CHANGE_BODY(a, op, b) \ 5317c478bd9Sstevel@tonic-gate int i; \ 5327c478bd9Sstevel@tonic-gate \ 5337c478bd9Sstevel@tonic-gate for (i = 0; i < PRIV_SETSIZE; i++) \ 5347c478bd9Sstevel@tonic-gate a->pbits[i] op b->pbits[i] 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate /* B = A ^ B */ 5377c478bd9Sstevel@tonic-gate void 5387c478bd9Sstevel@tonic-gate priv_intersect(const priv_set_t *a, priv_set_t *b) 5397c478bd9Sstevel@tonic-gate { 5407c478bd9Sstevel@tonic-gate /* CSTYLED */ 5417c478bd9Sstevel@tonic-gate PRIV_CHANGE_BODY(b, &=, a); 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate /* B = A v B */ 5457c478bd9Sstevel@tonic-gate void 5467c478bd9Sstevel@tonic-gate priv_union(const priv_set_t *a, priv_set_t *b) 5477c478bd9Sstevel@tonic-gate { 5487c478bd9Sstevel@tonic-gate /* CSTYLED */ 5497c478bd9Sstevel@tonic-gate PRIV_CHANGE_BODY(b, |=, a); 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate /* A = ! A */ 5537c478bd9Sstevel@tonic-gate void 5547c478bd9Sstevel@tonic-gate priv_inverse(priv_set_t *a) 5557c478bd9Sstevel@tonic-gate { 5567c478bd9Sstevel@tonic-gate PRIV_CHANGE_BODY(a, = ~, a); 5577c478bd9Sstevel@tonic-gate } 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate /* 5607c478bd9Sstevel@tonic-gate * Can the source cred act on the target credential? 5617c478bd9Sstevel@tonic-gate * 5627c478bd9Sstevel@tonic-gate * We will you allow to gain uids this way but not privileges. 5637c478bd9Sstevel@tonic-gate */ 5647c478bd9Sstevel@tonic-gate int 5657c478bd9Sstevel@tonic-gate priv_proc_cred_perm(const cred_t *scr, proc_t *tp, cred_t **pcr, int mode) 5667c478bd9Sstevel@tonic-gate { 5677c478bd9Sstevel@tonic-gate const priv_set_t *eset; 5687c478bd9Sstevel@tonic-gate int idsmatch; 5697c478bd9Sstevel@tonic-gate cred_t *tcr; 5707c478bd9Sstevel@tonic-gate int res = 0; 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate /* prevent the cred from going away */ 5737c478bd9Sstevel@tonic-gate mutex_enter(&tp->p_crlock); 5747c478bd9Sstevel@tonic-gate crhold(tcr = tp->p_cred); 5757c478bd9Sstevel@tonic-gate mutex_exit(&tp->p_crlock); 5767c478bd9Sstevel@tonic-gate 57730cbf0d2SCasper H.S. Dik if (scr == tcr && !(tp->p_flag & SNOCD)) 5787c478bd9Sstevel@tonic-gate goto out; 5797c478bd9Sstevel@tonic-gate 5807c478bd9Sstevel@tonic-gate idsmatch = (scr->cr_uid == tcr->cr_uid && 5817c478bd9Sstevel@tonic-gate scr->cr_uid == tcr->cr_ruid && 5827c478bd9Sstevel@tonic-gate scr->cr_uid == tcr->cr_suid && 5837c478bd9Sstevel@tonic-gate scr->cr_gid == tcr->cr_gid && 5847c478bd9Sstevel@tonic-gate scr->cr_gid == tcr->cr_rgid && 5857c478bd9Sstevel@tonic-gate scr->cr_gid == tcr->cr_sgid && 5867c478bd9Sstevel@tonic-gate !(tp->p_flag & SNOCD)); 5877c478bd9Sstevel@tonic-gate 5887c478bd9Sstevel@tonic-gate /* 5897c478bd9Sstevel@tonic-gate * Source credential must have the proc_zone privilege if referencing 5907c478bd9Sstevel@tonic-gate * a process in another zone. 5917c478bd9Sstevel@tonic-gate */ 5927c478bd9Sstevel@tonic-gate if (scr->cr_zone != tcr->cr_zone && secpolicy_proc_zone(scr) != 0) { 5937c478bd9Sstevel@tonic-gate res = EACCES; 5947c478bd9Sstevel@tonic-gate goto out; 5957c478bd9Sstevel@tonic-gate } 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate if (!(mode & VWRITE)) { 5987c478bd9Sstevel@tonic-gate if (!idsmatch && secpolicy_proc_owner(scr, tcr, 0) != 0) 5997c478bd9Sstevel@tonic-gate res = EACCES; 6007c478bd9Sstevel@tonic-gate goto out; 6017c478bd9Sstevel@tonic-gate } 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate /* 6047c478bd9Sstevel@tonic-gate * For writing, the effective set of scr must dominate all sets of tcr, 6057c478bd9Sstevel@tonic-gate * We test Pt <= Es (Et <= Pt so no need to test) and It <= Es 6067c478bd9Sstevel@tonic-gate * The Limit set of scr must be a superset of the limitset of 6077c478bd9Sstevel@tonic-gate * tcr. 6087c478bd9Sstevel@tonic-gate */ 6097c478bd9Sstevel@tonic-gate eset = &CR_OEPRIV(scr); 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate if (!priv_issubset(&CR_IPRIV(tcr), eset) || 6127c478bd9Sstevel@tonic-gate !priv_issubset(&CR_OPPRIV(tcr), eset) || 6137c478bd9Sstevel@tonic-gate !priv_issubset(&CR_LPRIV(tcr), &CR_LPRIV(scr)) || 6147c478bd9Sstevel@tonic-gate !idsmatch && secpolicy_proc_owner(scr, tcr, mode) != 0) 6157c478bd9Sstevel@tonic-gate res = EACCES; 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate out: 6187c478bd9Sstevel@tonic-gate if (res == 0 && pcr != NULL) 6197c478bd9Sstevel@tonic-gate *pcr = tcr; 6207c478bd9Sstevel@tonic-gate else 6217c478bd9Sstevel@tonic-gate crfree(tcr); 6227c478bd9Sstevel@tonic-gate return (res); 6237c478bd9Sstevel@tonic-gate } 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate /* 626982b4ad2SCasper H.S. Dik * Set the privilege aware bit, adding L to E/P if necessary. 627982b4ad2SCasper H.S. Dik * Each time we set it, we also clear PRIV_AWARE_RESET. 6287c478bd9Sstevel@tonic-gate */ 6297c478bd9Sstevel@tonic-gate void 6307c478bd9Sstevel@tonic-gate priv_set_PA(cred_t *cr) 6317c478bd9Sstevel@tonic-gate { 6327c478bd9Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 6337c478bd9Sstevel@tonic-gate 634982b4ad2SCasper H.S. Dik if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_RESET)) == PRIV_AWARE) 6357c478bd9Sstevel@tonic-gate return; 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate CR_FLAGS(cr) |= PRIV_AWARE; 638982b4ad2SCasper H.S. Dik CR_FLAGS(cr) &= ~PRIV_AWARE_RESET; 6397c478bd9Sstevel@tonic-gate 6407c478bd9Sstevel@tonic-gate if (cr->cr_uid == 0) 6417c478bd9Sstevel@tonic-gate priv_union(&CR_LPRIV(cr), &CR_EPRIV(cr)); 6427c478bd9Sstevel@tonic-gate 6437c478bd9Sstevel@tonic-gate if (cr->cr_uid == 0 || cr->cr_suid == 0 || cr->cr_ruid == 0) 6447c478bd9Sstevel@tonic-gate priv_union(&CR_LPRIV(cr), &CR_PPRIV(cr)); 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate boolean_t 6487c478bd9Sstevel@tonic-gate priv_can_clear_PA(const cred_t *cr) 6497c478bd9Sstevel@tonic-gate { 6507c478bd9Sstevel@tonic-gate /* 6517c478bd9Sstevel@tonic-gate * We can clear PA in the following cases: 6527c478bd9Sstevel@tonic-gate * 6537c478bd9Sstevel@tonic-gate * None of the uids are 0. 6547c478bd9Sstevel@tonic-gate * Any uid == 0 and P == L and (Euid != 0 or E == L) 6557c478bd9Sstevel@tonic-gate */ 6567c478bd9Sstevel@tonic-gate return ((cr->cr_suid != 0 && cr->cr_ruid != 0 && cr->cr_uid != 0) || 6577c478bd9Sstevel@tonic-gate priv_isequalset(&CR_PPRIV(cr), &CR_LPRIV(cr)) && 6587c478bd9Sstevel@tonic-gate (cr->cr_uid != 0 || priv_isequalset(&CR_EPRIV(cr), &CR_LPRIV(cr)))); 6597c478bd9Sstevel@tonic-gate } 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate /* 6627c478bd9Sstevel@tonic-gate * Clear privilege aware bit if it is an idempotent operation and by 6637c478bd9Sstevel@tonic-gate * clearing it the process cannot get to uid 0 and all privileges. 6647c478bd9Sstevel@tonic-gate * 6657c478bd9Sstevel@tonic-gate * This function should be called with caution as it may cause "E" to be 6667c478bd9Sstevel@tonic-gate * lost once a processes assumes euid 0 again. 6677c478bd9Sstevel@tonic-gate */ 6687c478bd9Sstevel@tonic-gate void 6697c478bd9Sstevel@tonic-gate priv_adjust_PA(cred_t *cr) 6707c478bd9Sstevel@tonic-gate { 6717c478bd9Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate if (!(CR_FLAGS(cr) & PRIV_AWARE) || 674982b4ad2SCasper H.S. Dik !priv_can_clear_PA(cr)) { 675982b4ad2SCasper H.S. Dik CR_FLAGS(cr) &= ~PRIV_AWARE_RESET; 6767c478bd9Sstevel@tonic-gate return; 677982b4ad2SCasper H.S. Dik } 6787c478bd9Sstevel@tonic-gate 6797c478bd9Sstevel@tonic-gate if (CR_FLAGS(cr) & PRIV_AWARE_INHERIT) 6807c478bd9Sstevel@tonic-gate return; 6817c478bd9Sstevel@tonic-gate 6827c478bd9Sstevel@tonic-gate /* 6837c478bd9Sstevel@tonic-gate * We now need to adjust P/E in those cases when uids 6847c478bd9Sstevel@tonic-gate * are zero; the rules are P' = I & L, E' = I & L; 6857c478bd9Sstevel@tonic-gate * but since P = L and E = L, we can use P &= I, E &= I, 6867c478bd9Sstevel@tonic-gate * depending on which uids are 0. 6877c478bd9Sstevel@tonic-gate */ 6887c478bd9Sstevel@tonic-gate if (cr->cr_suid == 0 || cr->cr_ruid == 0 || cr->cr_uid == 0) { 6897c478bd9Sstevel@tonic-gate if (cr->cr_uid == 0) 6907c478bd9Sstevel@tonic-gate priv_intersect(&CR_IPRIV(cr), &CR_EPRIV(cr)); 6917c478bd9Sstevel@tonic-gate priv_intersect(&CR_IPRIV(cr), &CR_PPRIV(cr)); 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate 694982b4ad2SCasper H.S. Dik CR_FLAGS(cr) &= ~(PRIV_AWARE|PRIV_AWARE_RESET); 695982b4ad2SCasper H.S. Dik } 696982b4ad2SCasper H.S. Dik 697982b4ad2SCasper H.S. Dik /* 698982b4ad2SCasper H.S. Dik * Reset privilege aware bit if so requested by setting the PRIV_AWARE_RESET 699982b4ad2SCasper H.S. Dik * flag. 700982b4ad2SCasper H.S. Dik */ 701982b4ad2SCasper H.S. Dik void 702982b4ad2SCasper H.S. Dik priv_reset_PA(cred_t *cr, boolean_t finalize) 703982b4ad2SCasper H.S. Dik { 704982b4ad2SCasper H.S. Dik ASSERT(cr->cr_ref <= 2); 705982b4ad2SCasper H.S. Dik 706982b4ad2SCasper H.S. Dik if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_RESET)) != 707982b4ad2SCasper H.S. Dik (PRIV_AWARE|PRIV_AWARE_RESET)) { 708982b4ad2SCasper H.S. Dik CR_FLAGS(cr) &= ~PRIV_AWARE_RESET; 709982b4ad2SCasper H.S. Dik return; 710982b4ad2SCasper H.S. Dik } 711982b4ad2SCasper H.S. Dik 712982b4ad2SCasper H.S. Dik /* 713982b4ad2SCasper H.S. Dik * When PRIV_AWARE_RESET is enabled, any change of uids causes 714982b4ad2SCasper H.S. Dik * a change to the P and E sets. Bracketing with 715982b4ad2SCasper H.S. Dik * seteuid(0) ... seteuid(uid)/setreuid(-1, 0) .. setreuid(-1, uid) 716982b4ad2SCasper H.S. Dik * will cause the privilege sets "do the right thing.". 717982b4ad2SCasper H.S. Dik * When the change of the uid is "final", e.g., by using setuid(uid), 718982b4ad2SCasper H.S. Dik * or setreuid(uid, uid) or when the last set*uid() call causes all 719982b4ad2SCasper H.S. Dik * uids to be the same, we set P and E to I & L, like when you exec. 720982b4ad2SCasper H.S. Dik * We make an exception when all the uids are 0; this is required 721982b4ad2SCasper H.S. Dik * when we login as root as in that particular case we cannot 722982b4ad2SCasper H.S. Dik * make a distinction between seteuid(0) and seteuid(uid). 723982b4ad2SCasper H.S. Dik * We rely on seteuid/setreuid/setuid to tell us with the 724982b4ad2SCasper H.S. Dik * "finalize" argument that we no longer expect new uid changes, 725982b4ad2SCasper H.S. Dik * cf. setreuid(uid, uid) and setuid(uid). 726982b4ad2SCasper H.S. Dik */ 727982b4ad2SCasper H.S. Dik if (cr->cr_suid == cr->cr_ruid && cr->cr_suid == cr->cr_uid) { 728982b4ad2SCasper H.S. Dik if (finalize || cr->cr_uid != 0) { 729982b4ad2SCasper H.S. Dik CR_EPRIV(cr) = CR_IPRIV(cr); 730982b4ad2SCasper H.S. Dik priv_intersect(&CR_LPRIV(cr), &CR_EPRIV(cr)); 731982b4ad2SCasper H.S. Dik CR_PPRIV(cr) = CR_EPRIV(cr); 732982b4ad2SCasper H.S. Dik CR_FLAGS(cr) &= ~(PRIV_AWARE|PRIV_AWARE_RESET); 733982b4ad2SCasper H.S. Dik } else { 734982b4ad2SCasper H.S. Dik CR_EPRIV(cr) = CR_PPRIV(cr); 735982b4ad2SCasper H.S. Dik } 736982b4ad2SCasper H.S. Dik } else if (cr->cr_uid != 0 && (cr->cr_ruid == 0 || cr->cr_suid == 0)) { 737982b4ad2SCasper H.S. Dik CR_EPRIV(cr) = CR_IPRIV(cr); 738982b4ad2SCasper H.S. Dik priv_intersect(&CR_LPRIV(cr), &CR_EPRIV(cr)); 739982b4ad2SCasper H.S. Dik } 7407c478bd9Sstevel@tonic-gate } 741