10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 59539SCasper.Dik@Sun.COM * Common Development and Distribution License (the "License"). 69539SCasper.Dik@Sun.COM * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 229539SCasper.Dik@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* 270Sstevel@tonic-gate * Privilege implementation. 280Sstevel@tonic-gate * 290Sstevel@tonic-gate * This file provides the infrastructure for privilege sets and limits 300Sstevel@tonic-gate * the number of files that requires to include <sys/cred_impl.h> and/or 310Sstevel@tonic-gate * <sys/priv_impl.h>. 320Sstevel@tonic-gate * 330Sstevel@tonic-gate * The Solaris privilege mechanism has been designed in a 340Sstevel@tonic-gate * future proof manner. While the kernel may use fixed size arrays 350Sstevel@tonic-gate * and fixed bitmasks and bit values, the representation of those 360Sstevel@tonic-gate * is kernel private. All external interfaces as well as K-to-K interfaces 370Sstevel@tonic-gate * have been constructed in a manner to provide the maximum flexibility. 380Sstevel@tonic-gate * 390Sstevel@tonic-gate * There can be X privilege sets each containing Y 32 bit words. 400Sstevel@tonic-gate * <X, Y> are constant for a kernel invocation. 410Sstevel@tonic-gate * 420Sstevel@tonic-gate * As a consequence, all privilege set manipulation happens in functions 430Sstevel@tonic-gate * below. 440Sstevel@tonic-gate * 450Sstevel@tonic-gate */ 460Sstevel@tonic-gate 470Sstevel@tonic-gate #include <sys/systm.h> 480Sstevel@tonic-gate #include <sys/ddi.h> 490Sstevel@tonic-gate #include <sys/kmem.h> 500Sstevel@tonic-gate #include <sys/sunddi.h> 510Sstevel@tonic-gate #include <sys/errno.h> 520Sstevel@tonic-gate #include <sys/debug.h> 530Sstevel@tonic-gate #include <sys/priv_impl.h> 540Sstevel@tonic-gate #include <sys/procfs.h> 550Sstevel@tonic-gate #include <sys/policy.h> 560Sstevel@tonic-gate #include <sys/cred_impl.h> 570Sstevel@tonic-gate #include <sys/devpolicy.h> 580Sstevel@tonic-gate #include <sys/atomic.h> 590Sstevel@tonic-gate 600Sstevel@tonic-gate /* 610Sstevel@tonic-gate * Privilege name to number mapping table consists in the generated 620Sstevel@tonic-gate * priv_const.c file. This lock protects against updates of the privilege 630Sstevel@tonic-gate * names and counts; all other priv_info fields are read-only. 640Sstevel@tonic-gate * The actual protected values are: 650Sstevel@tonic-gate * global variable nprivs 660Sstevel@tonic-gate * the priv_max field 670Sstevel@tonic-gate * the priv_names field 680Sstevel@tonic-gate * the priv names info item (cnt/strings) 690Sstevel@tonic-gate */ 700Sstevel@tonic-gate krwlock_t privinfo_lock; 710Sstevel@tonic-gate 720Sstevel@tonic-gate static boolean_t priv_valid(const cred_t *); 730Sstevel@tonic-gate 740Sstevel@tonic-gate priv_set_t priv_fullset; /* set of all privileges */ 750Sstevel@tonic-gate priv_set_t priv_unsafe; /* unsafe to exec set-uid root if these are not in L */ 760Sstevel@tonic-gate 770Sstevel@tonic-gate /* 780Sstevel@tonic-gate * Privilege initialization functions. 790Sstevel@tonic-gate * Called from common/os/cred.c when cred_init is called. 800Sstevel@tonic-gate */ 810Sstevel@tonic-gate 820Sstevel@tonic-gate void 830Sstevel@tonic-gate priv_init(void) 840Sstevel@tonic-gate { 850Sstevel@tonic-gate rw_init(&privinfo_lock, NULL, RW_DRIVER, NULL); 860Sstevel@tonic-gate 870Sstevel@tonic-gate PRIV_BASIC_ASSERT(priv_basic); 880Sstevel@tonic-gate PRIV_UNSAFE_ASSERT(&priv_unsafe); 890Sstevel@tonic-gate priv_fillset(&priv_fullset); 900Sstevel@tonic-gate 910Sstevel@tonic-gate devpolicy_init(); 920Sstevel@tonic-gate } 930Sstevel@tonic-gate 940Sstevel@tonic-gate /* Utility functions: privilege sets as opaque data types */ 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* 970Sstevel@tonic-gate * Guts of prgetprivsize. 980Sstevel@tonic-gate */ 990Sstevel@tonic-gate int 1000Sstevel@tonic-gate priv_prgetprivsize(prpriv_t *tmpl) 1010Sstevel@tonic-gate { 1020Sstevel@tonic-gate return (sizeof (prpriv_t) + 1039539SCasper.Dik@Sun.COM PRIV_SETBYTES - sizeof (priv_chunk_t) + 1049539SCasper.Dik@Sun.COM (tmpl ? tmpl->pr_infosize : priv_info->priv_infosize)); 1050Sstevel@tonic-gate } 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate /* 1080Sstevel@tonic-gate * Guts of prgetpriv. 1090Sstevel@tonic-gate */ 1100Sstevel@tonic-gate void 1110Sstevel@tonic-gate cred2prpriv(const cred_t *cp, prpriv_t *pr) 1120Sstevel@tonic-gate { 1130Sstevel@tonic-gate priv_set_t *psa; 1140Sstevel@tonic-gate int i; 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate pr->pr_nsets = PRIV_NSET; 1170Sstevel@tonic-gate pr->pr_setsize = PRIV_SETSIZE; 1180Sstevel@tonic-gate pr->pr_infosize = priv_info->priv_infosize; 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate psa = (priv_set_t *)pr->pr_sets; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate for (i = 0; i < PRIV_NSET; i++) 1230Sstevel@tonic-gate psa[i] = *priv_getset(cp, i); 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate priv_getinfo(cp, (char *)pr + PRIV_PRPRIV_INFO_OFFSET(pr)); 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate /* 1290Sstevel@tonic-gate * Guts of pr_spriv: 1300Sstevel@tonic-gate * 1310Sstevel@tonic-gate * Set the privileges of a process. 1320Sstevel@tonic-gate * 1330Sstevel@tonic-gate * In order to set the privileges, the setting process will need to 1340Sstevel@tonic-gate * have those privileges in its effective set in order to prevent 1350Sstevel@tonic-gate * specially privileged processes to easily gain additional privileges. 1360Sstevel@tonic-gate * Pre-existing privileges can be retained. To change any privileges, 1370Sstevel@tonic-gate * PRIV_PROC_OWNER needs to be asserted. 1380Sstevel@tonic-gate * 1390Sstevel@tonic-gate * In formula: 1400Sstevel@tonic-gate * 1410Sstevel@tonic-gate * S' <= S || S' <= S + Ea 1420Sstevel@tonic-gate * 1430Sstevel@tonic-gate * the new set must either be subset of the old set or a subset of 1440Sstevel@tonic-gate * the oldset merged with the effective set of the acting process; or just: 1450Sstevel@tonic-gate * 1460Sstevel@tonic-gate * S' <= S + Ea 1470Sstevel@tonic-gate * 1480Sstevel@tonic-gate * It's not legal to grow the limit set this way. 1490Sstevel@tonic-gate * 1500Sstevel@tonic-gate */ 1510Sstevel@tonic-gate int 1520Sstevel@tonic-gate priv_pr_spriv(proc_t *p, prpriv_t *prpriv, const cred_t *cr) 1530Sstevel@tonic-gate { 1540Sstevel@tonic-gate cred_t *oldcred; 1550Sstevel@tonic-gate cred_t *newcred; 1560Sstevel@tonic-gate int i; 1570Sstevel@tonic-gate int err = EPERM; 1580Sstevel@tonic-gate cred_priv_t *cp, *ocp; 1590Sstevel@tonic-gate priv_set_t eset; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate /* 1640Sstevel@tonic-gate * Set must have proper dimension; infosize must be absent 1650Sstevel@tonic-gate * or properly sized. 1660Sstevel@tonic-gate */ 1670Sstevel@tonic-gate if (prpriv->pr_nsets != PRIV_NSET || 1680Sstevel@tonic-gate prpriv->pr_setsize != PRIV_SETSIZE || 1690Sstevel@tonic-gate (prpriv->pr_infosize & (sizeof (uint32_t) - 1)) != 0 || 1700Sstevel@tonic-gate prpriv->pr_infosize > priv_info->priv_infosize || 1710Sstevel@tonic-gate prpriv->pr_infosize < 0) 1729539SCasper.Dik@Sun.COM return (EINVAL); 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate mutex_exit(&p->p_lock); 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate if (priv_proc_cred_perm(cr, p, &oldcred, VWRITE) != 0) { 1770Sstevel@tonic-gate mutex_enter(&p->p_lock); 1780Sstevel@tonic-gate return (EPERM); 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate newcred = crdup(oldcred); 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate /* Copy the privilege sets from prpriv to newcred */ 1840Sstevel@tonic-gate bcopy(prpriv->pr_sets, CR_PRIVSETS(newcred), PRIV_SETBYTES); 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate cp = &newcred->cr_priv; 1870Sstevel@tonic-gate ocp = &oldcred->cr_priv; 1880Sstevel@tonic-gate eset = CR_OEPRIV(cr); 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate priv_intersect(&CR_LPRIV(oldcred), &eset); 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* 1930Sstevel@tonic-gate * Verify the constraints laid out: 1940Sstevel@tonic-gate * for the limit set, we require that the new set is a subset 1950Sstevel@tonic-gate * of the old limit set. 1960Sstevel@tonic-gate * for all other sets, we require that the new set is either a 1970Sstevel@tonic-gate * subset of the old set or a subset of the intersection of 1980Sstevel@tonic-gate * the old limit set and the effective set of the acting process. 1990Sstevel@tonic-gate */ 2000Sstevel@tonic-gate for (i = 0; i < PRIV_NSET; i++) 2010Sstevel@tonic-gate if (!priv_issubset(&cp->crprivs[i], &ocp->crprivs[i]) && 2020Sstevel@tonic-gate (i == PRIV_LIMIT || !priv_issubset(&cp->crprivs[i], &eset))) 2030Sstevel@tonic-gate break; 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate crfree(oldcred); 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate if (i < PRIV_NSET || !priv_valid(newcred)) 2080Sstevel@tonic-gate goto err; 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate /* Load the settable privilege information */ 2110Sstevel@tonic-gate if (prpriv->pr_infosize > 0) { 2120Sstevel@tonic-gate char *x = (char *)prpriv + PRIV_PRPRIV_INFO_OFFSET(prpriv); 2130Sstevel@tonic-gate char *lastx = x + prpriv->pr_infosize; 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate while (x < lastx) { 2160Sstevel@tonic-gate priv_info_t *pi = (priv_info_t *)x; 2170Sstevel@tonic-gate priv_info_uint_t *pii; 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate switch (pi->priv_info_type) { 2200Sstevel@tonic-gate case PRIV_INFO_FLAGS: 2210Sstevel@tonic-gate pii = (priv_info_uint_t *)x; 2220Sstevel@tonic-gate if (pii->info.priv_info_size != sizeof (*pii)) { 2230Sstevel@tonic-gate err = EINVAL; 2240Sstevel@tonic-gate goto err; 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate CR_FLAGS(newcred) &= ~PRIV_USER; 2270Sstevel@tonic-gate CR_FLAGS(newcred) |= (pii->val & PRIV_USER); 2280Sstevel@tonic-gate break; 2290Sstevel@tonic-gate default: 2300Sstevel@tonic-gate err = EINVAL; 2310Sstevel@tonic-gate goto err; 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate /* Guarantee alignment and forward progress */ 2340Sstevel@tonic-gate if ((pi->priv_info_size & (sizeof (uint32_t) - 1)) || 2350Sstevel@tonic-gate pi->priv_info_size < sizeof (*pi) || 2360Sstevel@tonic-gate lastx - x > pi->priv_info_size) { 2370Sstevel@tonic-gate err = EINVAL; 2380Sstevel@tonic-gate goto err; 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate x += pi->priv_info_size; 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* 2460Sstevel@tonic-gate * We'll try to copy the privilege aware flag; but since the 2470Sstevel@tonic-gate * privileges sets are all individually set, they are set 2480Sstevel@tonic-gate * as if we're privilege aware. If PRIV_AWARE wasn't set 2490Sstevel@tonic-gate * or was explicitely unset, we need to set the flag and then 2500Sstevel@tonic-gate * try to get rid of it. 2510Sstevel@tonic-gate */ 2520Sstevel@tonic-gate if ((CR_FLAGS(newcred) & PRIV_AWARE) == 0) { 2530Sstevel@tonic-gate CR_FLAGS(newcred) |= PRIV_AWARE; 2540Sstevel@tonic-gate priv_adjust_PA(newcred); 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate mutex_enter(&p->p_crlock); 2580Sstevel@tonic-gate oldcred = p->p_cred; 2590Sstevel@tonic-gate p->p_cred = newcred; 2600Sstevel@tonic-gate mutex_exit(&p->p_crlock); 2610Sstevel@tonic-gate crfree(oldcred); 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate mutex_enter(&p->p_lock); 2640Sstevel@tonic-gate return (0); 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate err: 2670Sstevel@tonic-gate crfree(newcred); 2680Sstevel@tonic-gate mutex_enter(&p->p_lock); 2690Sstevel@tonic-gate return (err); 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate priv_impl_info_t 2730Sstevel@tonic-gate *priv_hold_implinfo(void) 2740Sstevel@tonic-gate { 2750Sstevel@tonic-gate rw_enter(&privinfo_lock, RW_READER); 2760Sstevel@tonic-gate return (priv_info); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate void 2800Sstevel@tonic-gate priv_release_implinfo(void) 2810Sstevel@tonic-gate { 2820Sstevel@tonic-gate rw_exit(&privinfo_lock); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate size_t 2860Sstevel@tonic-gate priv_get_implinfo_size(void) 2870Sstevel@tonic-gate { 2880Sstevel@tonic-gate return (privinfosize); 2890Sstevel@tonic-gate } 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate /* 2930Sstevel@tonic-gate * Return the nth privilege set 2940Sstevel@tonic-gate */ 2950Sstevel@tonic-gate const priv_set_t * 2960Sstevel@tonic-gate priv_getset(const cred_t *cr, int set) 2970Sstevel@tonic-gate { 2980Sstevel@tonic-gate ASSERT(PRIV_VALIDSET(set)); 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate if ((CR_FLAGS(cr) & PRIV_AWARE) == 0) 3010Sstevel@tonic-gate switch (set) { 3020Sstevel@tonic-gate case PRIV_EFFECTIVE: 3030Sstevel@tonic-gate return (&CR_OEPRIV(cr)); 3040Sstevel@tonic-gate case PRIV_PERMITTED: 3050Sstevel@tonic-gate return (&CR_OPPRIV(cr)); 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate return (&CR_PRIVS(cr)->crprivs[set]); 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate /* 3110Sstevel@tonic-gate * Buf must be allocated by caller and contain sufficient space to 3120Sstevel@tonic-gate * contain all additional info structures using priv_info.priv_infosize. 3130Sstevel@tonic-gate * The buffer must be properly aligned. 3140Sstevel@tonic-gate */ 3150Sstevel@tonic-gate /*ARGSUSED*/ 3160Sstevel@tonic-gate void 3170Sstevel@tonic-gate priv_getinfo(const cred_t *cr, void *buf) 3180Sstevel@tonic-gate { 3190Sstevel@tonic-gate struct priv_info_uint *ii; 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate ii = buf; 3220Sstevel@tonic-gate ii->val = CR_FLAGS(cr); 3230Sstevel@tonic-gate ii->info.priv_info_size = (uint32_t)sizeof (*ii); 3240Sstevel@tonic-gate ii->info.priv_info_type = PRIV_INFO_FLAGS; 3250Sstevel@tonic-gate } 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate int 3280Sstevel@tonic-gate priv_getbyname(const char *name, uint_t flag) 3290Sstevel@tonic-gate { 3300Sstevel@tonic-gate int i; 3310Sstevel@tonic-gate int wheld = 0; 3320Sstevel@tonic-gate int len; 3330Sstevel@tonic-gate char *p; 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate if (flag != 0 && flag != PRIV_ALLOC) 3360Sstevel@tonic-gate return (-EINVAL); 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate if (strncasecmp(name, "priv_", 5) == 0) 3390Sstevel@tonic-gate name += 5; 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate rw_enter(&privinfo_lock, RW_READER); 3420Sstevel@tonic-gate rescan: 3430Sstevel@tonic-gate for (i = 0; i < nprivs; i++) 3440Sstevel@tonic-gate if (strcasecmp(priv_names[i], name) == 0) { 3450Sstevel@tonic-gate rw_exit(&privinfo_lock); 3460Sstevel@tonic-gate return (i); 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate if (!wheld) { 3510Sstevel@tonic-gate if (!(flag & PRIV_ALLOC)) { 3520Sstevel@tonic-gate rw_exit(&privinfo_lock); 3530Sstevel@tonic-gate return (-EINVAL); 3540Sstevel@tonic-gate } 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate /* check length, validity and available space */ 3570Sstevel@tonic-gate len = strlen(name) + 1; 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate if (len > PRIVNAME_MAX) { 3600Sstevel@tonic-gate rw_exit(&privinfo_lock); 3610Sstevel@tonic-gate return (-ENAMETOOLONG); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate for (p = (char *)name; *p != '\0'; p++) { 3650Sstevel@tonic-gate char c = *p; 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate if (!((c >= 'A' && c <= 'Z') || 3680Sstevel@tonic-gate (c >= 'a' && c <= 'z') || 3690Sstevel@tonic-gate (c >= '0' && c <= '9') || 3700Sstevel@tonic-gate c == '_')) { 3710Sstevel@tonic-gate rw_exit(&privinfo_lock); 3720Sstevel@tonic-gate return (-EINVAL); 3730Sstevel@tonic-gate } 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate if (!rw_tryupgrade(&privinfo_lock)) { 3770Sstevel@tonic-gate rw_exit(&privinfo_lock); 3780Sstevel@tonic-gate rw_enter(&privinfo_lock, RW_WRITER); 3790Sstevel@tonic-gate wheld = 1; 3800Sstevel@tonic-gate /* Someone may have added our privilege */ 3810Sstevel@tonic-gate goto rescan; 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate } 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate if (nprivs == MAX_PRIVILEGE || len + privbytes > maxprivbytes) { 3860Sstevel@tonic-gate rw_exit(&privinfo_lock); 3870Sstevel@tonic-gate return (-ENOMEM); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate priv_names[i] = p = priv_str + privbytes; 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate bcopy(name, p, len); 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate /* make the priv_names[i] and privilege name globally visible */ 3950Sstevel@tonic-gate membar_producer(); 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate /* adjust priv count and bytes count */ 3980Sstevel@tonic-gate priv_ninfo->cnt = priv_info->priv_max = ++nprivs; 3990Sstevel@tonic-gate privbytes += len; 4000Sstevel@tonic-gate 4010Sstevel@tonic-gate rw_exit(&privinfo_lock); 4020Sstevel@tonic-gate return (i); 4030Sstevel@tonic-gate } 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate /* 4060Sstevel@tonic-gate * We can't afford locking the privileges here because of the locations 4070Sstevel@tonic-gate * we call this from; so we make sure that the privileges table 4080Sstevel@tonic-gate * is visible to us; it is made visible before the value of nprivs is 4090Sstevel@tonic-gate * updated. 4100Sstevel@tonic-gate */ 4110Sstevel@tonic-gate const char * 4120Sstevel@tonic-gate priv_getbynum(int priv) 4130Sstevel@tonic-gate { 4140Sstevel@tonic-gate int maxpriv = nprivs; 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate membar_consumer(); 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate if (priv >= 0 && priv < maxpriv) 4190Sstevel@tonic-gate return (priv_names[priv]); 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate return (NULL); 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate const char * 4250Sstevel@tonic-gate priv_getsetbynum(int setno) 4260Sstevel@tonic-gate { 4270Sstevel@tonic-gate if (!PRIV_VALIDSET(setno)) 4280Sstevel@tonic-gate return (NULL); 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate return (priv_setnames[setno]); 4310Sstevel@tonic-gate } 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate * Privilege sanity checking when setting: E <= P. 4350Sstevel@tonic-gate */ 4360Sstevel@tonic-gate static boolean_t 4370Sstevel@tonic-gate priv_valid(const cred_t *cr) 4380Sstevel@tonic-gate { 4390Sstevel@tonic-gate return (priv_issubset(&CR_EPRIV(cr), &CR_PPRIV(cr))); 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate /* 4430Sstevel@tonic-gate * Privilege manipulation functions 4440Sstevel@tonic-gate * 4450Sstevel@tonic-gate * Without knowing the details of the privilege set implementation, 4460Sstevel@tonic-gate * opaque pointers can be used to manipulate sets at will. 4470Sstevel@tonic-gate */ 4480Sstevel@tonic-gate void 4490Sstevel@tonic-gate priv_emptyset(priv_set_t *set) 4500Sstevel@tonic-gate { 4510Sstevel@tonic-gate bzero(set, sizeof (*set)); 4520Sstevel@tonic-gate } 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate void 4550Sstevel@tonic-gate priv_fillset(priv_set_t *set) 4560Sstevel@tonic-gate { 4570Sstevel@tonic-gate int i; 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate /* memset? */ 4600Sstevel@tonic-gate for (i = 0; i < PRIV_SETSIZE; i++) 4610Sstevel@tonic-gate set->pbits[i] = ~(priv_chunk_t)0; 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate void 4650Sstevel@tonic-gate priv_addset(priv_set_t *set, int priv) 4660Sstevel@tonic-gate { 4670Sstevel@tonic-gate ASSERT(priv >= 0 && priv < MAX_PRIVILEGE); 4680Sstevel@tonic-gate __PRIV_ASSERT(set, priv); 4690Sstevel@tonic-gate } 4700Sstevel@tonic-gate 4710Sstevel@tonic-gate void 4720Sstevel@tonic-gate priv_delset(priv_set_t *set, int priv) 4730Sstevel@tonic-gate { 4740Sstevel@tonic-gate ASSERT(priv >= 0 && priv < MAX_PRIVILEGE); 4750Sstevel@tonic-gate __PRIV_CLEAR(set, priv); 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate boolean_t 4790Sstevel@tonic-gate priv_ismember(const priv_set_t *set, int priv) 4800Sstevel@tonic-gate { 4810Sstevel@tonic-gate ASSERT(priv >= 0 && priv < MAX_PRIVILEGE); 4820Sstevel@tonic-gate return (__PRIV_ISASSERT(set, priv) ? B_TRUE : B_FALSE); 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate #define PRIV_TEST_BODY(test) \ 4860Sstevel@tonic-gate int i; \ 4870Sstevel@tonic-gate \ 4880Sstevel@tonic-gate for (i = 0; i < PRIV_SETSIZE; i++) \ 4890Sstevel@tonic-gate if (!(test)) \ 4900Sstevel@tonic-gate return (B_FALSE); \ 4910Sstevel@tonic-gate \ 4920Sstevel@tonic-gate return (B_TRUE) 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate boolean_t 4950Sstevel@tonic-gate priv_isequalset(const priv_set_t *a, const priv_set_t *b) 4960Sstevel@tonic-gate { 4970Sstevel@tonic-gate return ((boolean_t)(bcmp(a, b, sizeof (*a)) == 0)); 4980Sstevel@tonic-gate } 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate boolean_t 5010Sstevel@tonic-gate priv_isemptyset(const priv_set_t *set) 5020Sstevel@tonic-gate { 5030Sstevel@tonic-gate PRIV_TEST_BODY(set->pbits[i] == 0); 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate boolean_t 5070Sstevel@tonic-gate priv_isfullset(const priv_set_t *set) 5080Sstevel@tonic-gate { 5090Sstevel@tonic-gate PRIV_TEST_BODY(set->pbits[i] == ~(priv_chunk_t)0); 5100Sstevel@tonic-gate } 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate /* 5130Sstevel@tonic-gate * Return true if a is a subset of b 5140Sstevel@tonic-gate */ 5150Sstevel@tonic-gate boolean_t 5160Sstevel@tonic-gate priv_issubset(const priv_set_t *a, const priv_set_t *b) 5170Sstevel@tonic-gate { 5180Sstevel@tonic-gate PRIV_TEST_BODY((a->pbits[i] | b->pbits[i]) == b->pbits[i]); 5190Sstevel@tonic-gate } 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate #define PRIV_CHANGE_BODY(a, op, b) \ 5220Sstevel@tonic-gate int i; \ 5230Sstevel@tonic-gate \ 5240Sstevel@tonic-gate for (i = 0; i < PRIV_SETSIZE; i++) \ 5250Sstevel@tonic-gate a->pbits[i] op b->pbits[i] 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate /* B = A ^ B */ 5280Sstevel@tonic-gate void 5290Sstevel@tonic-gate priv_intersect(const priv_set_t *a, priv_set_t *b) 5300Sstevel@tonic-gate { 5310Sstevel@tonic-gate /* CSTYLED */ 5320Sstevel@tonic-gate PRIV_CHANGE_BODY(b, &=, a); 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate /* B = A v B */ 5360Sstevel@tonic-gate void 5370Sstevel@tonic-gate priv_union(const priv_set_t *a, priv_set_t *b) 5380Sstevel@tonic-gate { 5390Sstevel@tonic-gate /* CSTYLED */ 5400Sstevel@tonic-gate PRIV_CHANGE_BODY(b, |=, a); 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate /* A = ! A */ 5440Sstevel@tonic-gate void 5450Sstevel@tonic-gate priv_inverse(priv_set_t *a) 5460Sstevel@tonic-gate { 5470Sstevel@tonic-gate PRIV_CHANGE_BODY(a, = ~, a); 5480Sstevel@tonic-gate } 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate /* 5510Sstevel@tonic-gate * Can the source cred act on the target credential? 5520Sstevel@tonic-gate * 5530Sstevel@tonic-gate * We will you allow to gain uids this way but not privileges. 5540Sstevel@tonic-gate */ 5550Sstevel@tonic-gate int 5560Sstevel@tonic-gate priv_proc_cred_perm(const cred_t *scr, proc_t *tp, cred_t **pcr, int mode) 5570Sstevel@tonic-gate { 5580Sstevel@tonic-gate const priv_set_t *eset; 5590Sstevel@tonic-gate int idsmatch; 5600Sstevel@tonic-gate cred_t *tcr; 5610Sstevel@tonic-gate int res = 0; 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate /* prevent the cred from going away */ 5640Sstevel@tonic-gate mutex_enter(&tp->p_crlock); 5650Sstevel@tonic-gate crhold(tcr = tp->p_cred); 5660Sstevel@tonic-gate mutex_exit(&tp->p_crlock); 5670Sstevel@tonic-gate 5689539SCasper.Dik@Sun.COM if (scr == tcr && !(tp->p_flag & SNOCD)) 5690Sstevel@tonic-gate goto out; 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate idsmatch = (scr->cr_uid == tcr->cr_uid && 5729539SCasper.Dik@Sun.COM scr->cr_uid == tcr->cr_ruid && 5739539SCasper.Dik@Sun.COM scr->cr_uid == tcr->cr_suid && 5749539SCasper.Dik@Sun.COM scr->cr_gid == tcr->cr_gid && 5759539SCasper.Dik@Sun.COM scr->cr_gid == tcr->cr_rgid && 5769539SCasper.Dik@Sun.COM scr->cr_gid == tcr->cr_sgid && 5779539SCasper.Dik@Sun.COM !(tp->p_flag & SNOCD)); 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate /* 5800Sstevel@tonic-gate * Source credential must have the proc_zone privilege if referencing 5810Sstevel@tonic-gate * a process in another zone. 5820Sstevel@tonic-gate */ 5830Sstevel@tonic-gate if (scr->cr_zone != tcr->cr_zone && secpolicy_proc_zone(scr) != 0) { 5840Sstevel@tonic-gate res = EACCES; 5850Sstevel@tonic-gate goto out; 5860Sstevel@tonic-gate } 5870Sstevel@tonic-gate 5880Sstevel@tonic-gate if (!(mode & VWRITE)) { 5890Sstevel@tonic-gate if (!idsmatch && secpolicy_proc_owner(scr, tcr, 0) != 0) 5900Sstevel@tonic-gate res = EACCES; 5910Sstevel@tonic-gate goto out; 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate /* 5950Sstevel@tonic-gate * For writing, the effective set of scr must dominate all sets of tcr, 5960Sstevel@tonic-gate * We test Pt <= Es (Et <= Pt so no need to test) and It <= Es 5970Sstevel@tonic-gate * The Limit set of scr must be a superset of the limitset of 5980Sstevel@tonic-gate * tcr. 5990Sstevel@tonic-gate */ 6000Sstevel@tonic-gate eset = &CR_OEPRIV(scr); 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate if (!priv_issubset(&CR_IPRIV(tcr), eset) || 6030Sstevel@tonic-gate !priv_issubset(&CR_OPPRIV(tcr), eset) || 6040Sstevel@tonic-gate !priv_issubset(&CR_LPRIV(tcr), &CR_LPRIV(scr)) || 6050Sstevel@tonic-gate !idsmatch && secpolicy_proc_owner(scr, tcr, mode) != 0) 6060Sstevel@tonic-gate res = EACCES; 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate out: 6090Sstevel@tonic-gate if (res == 0 && pcr != NULL) 6100Sstevel@tonic-gate *pcr = tcr; 6110Sstevel@tonic-gate else 6120Sstevel@tonic-gate crfree(tcr); 6130Sstevel@tonic-gate return (res); 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate /* 617*9799SCasper.Dik@Sun.COM * Set the privilege aware bit, adding L to E/P if necessary. 618*9799SCasper.Dik@Sun.COM * Each time we set it, we also clear PRIV_AWARE_RESET. 6190Sstevel@tonic-gate */ 6200Sstevel@tonic-gate void 6210Sstevel@tonic-gate priv_set_PA(cred_t *cr) 6220Sstevel@tonic-gate { 6230Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 6240Sstevel@tonic-gate 625*9799SCasper.Dik@Sun.COM if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_RESET)) == PRIV_AWARE) 6260Sstevel@tonic-gate return; 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate CR_FLAGS(cr) |= PRIV_AWARE; 629*9799SCasper.Dik@Sun.COM CR_FLAGS(cr) &= ~PRIV_AWARE_RESET; 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate if (cr->cr_uid == 0) 6320Sstevel@tonic-gate priv_union(&CR_LPRIV(cr), &CR_EPRIV(cr)); 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate if (cr->cr_uid == 0 || cr->cr_suid == 0 || cr->cr_ruid == 0) 6350Sstevel@tonic-gate priv_union(&CR_LPRIV(cr), &CR_PPRIV(cr)); 6360Sstevel@tonic-gate } 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate boolean_t 6390Sstevel@tonic-gate priv_can_clear_PA(const cred_t *cr) 6400Sstevel@tonic-gate { 6410Sstevel@tonic-gate /* 6420Sstevel@tonic-gate * We can clear PA in the following cases: 6430Sstevel@tonic-gate * 6440Sstevel@tonic-gate * None of the uids are 0. 6450Sstevel@tonic-gate * Any uid == 0 and P == L and (Euid != 0 or E == L) 6460Sstevel@tonic-gate */ 6470Sstevel@tonic-gate return ((cr->cr_suid != 0 && cr->cr_ruid != 0 && cr->cr_uid != 0) || 6480Sstevel@tonic-gate priv_isequalset(&CR_PPRIV(cr), &CR_LPRIV(cr)) && 6490Sstevel@tonic-gate (cr->cr_uid != 0 || priv_isequalset(&CR_EPRIV(cr), &CR_LPRIV(cr)))); 6500Sstevel@tonic-gate } 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate /* 6530Sstevel@tonic-gate * Clear privilege aware bit if it is an idempotent operation and by 6540Sstevel@tonic-gate * clearing it the process cannot get to uid 0 and all privileges. 6550Sstevel@tonic-gate * 6560Sstevel@tonic-gate * This function should be called with caution as it may cause "E" to be 6570Sstevel@tonic-gate * lost once a processes assumes euid 0 again. 6580Sstevel@tonic-gate */ 6590Sstevel@tonic-gate void 6600Sstevel@tonic-gate priv_adjust_PA(cred_t *cr) 6610Sstevel@tonic-gate { 6620Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate if (!(CR_FLAGS(cr) & PRIV_AWARE) || 665*9799SCasper.Dik@Sun.COM !priv_can_clear_PA(cr)) { 666*9799SCasper.Dik@Sun.COM CR_FLAGS(cr) &= ~PRIV_AWARE_RESET; 6670Sstevel@tonic-gate return; 668*9799SCasper.Dik@Sun.COM } 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate if (CR_FLAGS(cr) & PRIV_AWARE_INHERIT) 6710Sstevel@tonic-gate return; 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* 6740Sstevel@tonic-gate * We now need to adjust P/E in those cases when uids 6750Sstevel@tonic-gate * are zero; the rules are P' = I & L, E' = I & L; 6760Sstevel@tonic-gate * but since P = L and E = L, we can use P &= I, E &= I, 6770Sstevel@tonic-gate * depending on which uids are 0. 6780Sstevel@tonic-gate */ 6790Sstevel@tonic-gate if (cr->cr_suid == 0 || cr->cr_ruid == 0 || cr->cr_uid == 0) { 6800Sstevel@tonic-gate if (cr->cr_uid == 0) 6810Sstevel@tonic-gate priv_intersect(&CR_IPRIV(cr), &CR_EPRIV(cr)); 6820Sstevel@tonic-gate priv_intersect(&CR_IPRIV(cr), &CR_PPRIV(cr)); 6830Sstevel@tonic-gate } 6840Sstevel@tonic-gate 685*9799SCasper.Dik@Sun.COM CR_FLAGS(cr) &= ~(PRIV_AWARE|PRIV_AWARE_RESET); 6860Sstevel@tonic-gate } 687*9799SCasper.Dik@Sun.COM 688*9799SCasper.Dik@Sun.COM /* 689*9799SCasper.Dik@Sun.COM * Reset privilege aware bit if so requested by setting the PRIV_AWARE_RESET 690*9799SCasper.Dik@Sun.COM * flag. 691*9799SCasper.Dik@Sun.COM */ 692*9799SCasper.Dik@Sun.COM void 693*9799SCasper.Dik@Sun.COM priv_reset_PA(cred_t *cr, boolean_t finalize) 694*9799SCasper.Dik@Sun.COM { 695*9799SCasper.Dik@Sun.COM ASSERT(cr->cr_ref <= 2); 696*9799SCasper.Dik@Sun.COM 697*9799SCasper.Dik@Sun.COM if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_RESET)) != 698*9799SCasper.Dik@Sun.COM (PRIV_AWARE|PRIV_AWARE_RESET)) { 699*9799SCasper.Dik@Sun.COM CR_FLAGS(cr) &= ~PRIV_AWARE_RESET; 700*9799SCasper.Dik@Sun.COM return; 701*9799SCasper.Dik@Sun.COM } 702*9799SCasper.Dik@Sun.COM 703*9799SCasper.Dik@Sun.COM /* 704*9799SCasper.Dik@Sun.COM * When PRIV_AWARE_RESET is enabled, any change of uids causes 705*9799SCasper.Dik@Sun.COM * a change to the P and E sets. Bracketing with 706*9799SCasper.Dik@Sun.COM * seteuid(0) ... seteuid(uid)/setreuid(-1, 0) .. setreuid(-1, uid) 707*9799SCasper.Dik@Sun.COM * will cause the privilege sets "do the right thing.". 708*9799SCasper.Dik@Sun.COM * When the change of the uid is "final", e.g., by using setuid(uid), 709*9799SCasper.Dik@Sun.COM * or setreuid(uid, uid) or when the last set*uid() call causes all 710*9799SCasper.Dik@Sun.COM * uids to be the same, we set P and E to I & L, like when you exec. 711*9799SCasper.Dik@Sun.COM * We make an exception when all the uids are 0; this is required 712*9799SCasper.Dik@Sun.COM * when we login as root as in that particular case we cannot 713*9799SCasper.Dik@Sun.COM * make a distinction between seteuid(0) and seteuid(uid). 714*9799SCasper.Dik@Sun.COM * We rely on seteuid/setreuid/setuid to tell us with the 715*9799SCasper.Dik@Sun.COM * "finalize" argument that we no longer expect new uid changes, 716*9799SCasper.Dik@Sun.COM * cf. setreuid(uid, uid) and setuid(uid). 717*9799SCasper.Dik@Sun.COM */ 718*9799SCasper.Dik@Sun.COM if (cr->cr_suid == cr->cr_ruid && cr->cr_suid == cr->cr_uid) { 719*9799SCasper.Dik@Sun.COM if (finalize || cr->cr_uid != 0) { 720*9799SCasper.Dik@Sun.COM CR_EPRIV(cr) = CR_IPRIV(cr); 721*9799SCasper.Dik@Sun.COM priv_intersect(&CR_LPRIV(cr), &CR_EPRIV(cr)); 722*9799SCasper.Dik@Sun.COM CR_PPRIV(cr) = CR_EPRIV(cr); 723*9799SCasper.Dik@Sun.COM CR_FLAGS(cr) &= ~(PRIV_AWARE|PRIV_AWARE_RESET); 724*9799SCasper.Dik@Sun.COM } else { 725*9799SCasper.Dik@Sun.COM CR_EPRIV(cr) = CR_PPRIV(cr); 726*9799SCasper.Dik@Sun.COM } 727*9799SCasper.Dik@Sun.COM } else if (cr->cr_uid != 0 && (cr->cr_ruid == 0 || cr->cr_suid == 0)) { 728*9799SCasper.Dik@Sun.COM CR_EPRIV(cr) = CR_IPRIV(cr); 729*9799SCasper.Dik@Sun.COM priv_intersect(&CR_LPRIV(cr), &CR_EPRIV(cr)); 730*9799SCasper.Dik@Sun.COM } 731*9799SCasper.Dik@Sun.COM } 732