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 51059Scasper * Common Development and Distribution License (the "License"). 61059Scasper * 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 */ 213864Sraf 220Sstevel@tonic-gate /* 23*11537SCasper.Dik@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 276812Sraf #pragma weak _getprivimplinfo = getprivimplinfo 286812Sraf #pragma weak _priv_addset = priv_addset 296812Sraf #pragma weak _priv_allocset = priv_allocset 306812Sraf #pragma weak _priv_copyset = priv_copyset 316812Sraf #pragma weak _priv_delset = priv_delset 326812Sraf #pragma weak _priv_emptyset = priv_emptyset 33*11537SCasper.Dik@Sun.COM #pragma weak _priv_basicset = priv_basicset 346812Sraf #pragma weak _priv_fillset = priv_fillset 356812Sraf #pragma weak _priv_freeset = priv_freeset 366812Sraf #pragma weak _priv_getbyname = priv_getbyname 376812Sraf #pragma weak _priv_getbynum = priv_getbynum 386812Sraf #pragma weak _priv_getsetbyname = priv_getsetbyname 396812Sraf #pragma weak _priv_getsetbynum = priv_getsetbynum 406812Sraf #pragma weak _priv_ineffect = priv_ineffect 416812Sraf #pragma weak _priv_intersect = priv_intersect 426812Sraf #pragma weak _priv_inverse = priv_inverse 436812Sraf #pragma weak _priv_isemptyset = priv_isemptyset 446812Sraf #pragma weak _priv_isequalset = priv_isequalset 456812Sraf #pragma weak _priv_isfullset = priv_isfullset 466812Sraf #pragma weak _priv_ismember = priv_ismember 476812Sraf #pragma weak _priv_issubset = priv_issubset 486812Sraf #pragma weak _priv_set = priv_set 496812Sraf #pragma weak _priv_union = priv_union 500Sstevel@tonic-gate 516812Sraf #include "lint.h" 520Sstevel@tonic-gate 530Sstevel@tonic-gate #define _STRUCTURED_PROC 1 540Sstevel@tonic-gate 550Sstevel@tonic-gate #include "priv_private.h" 560Sstevel@tonic-gate #include "mtlib.h" 570Sstevel@tonic-gate #include "libc.h" 580Sstevel@tonic-gate #include <errno.h> 590Sstevel@tonic-gate #include <stdarg.h> 600Sstevel@tonic-gate #include <stdlib.h> 610Sstevel@tonic-gate #include <unistd.h> 620Sstevel@tonic-gate #include <strings.h> 630Sstevel@tonic-gate #include <synch.h> 640Sstevel@tonic-gate #include <alloca.h> 653864Sraf #include <atomic.h> 660Sstevel@tonic-gate #include <sys/ucred.h> 670Sstevel@tonic-gate #include <sys/procfs.h> 680Sstevel@tonic-gate #include <sys/param.h> 690Sstevel@tonic-gate #include <sys/corectl.h> 700Sstevel@tonic-gate #include <priv_utils.h> 710Sstevel@tonic-gate #include <zone.h> 720Sstevel@tonic-gate 730Sstevel@tonic-gate /* Include each string only once - until the compiler/linker are fixed */ 740Sstevel@tonic-gate static const char *permitted = PRIV_PERMITTED; 750Sstevel@tonic-gate static const char *effective = PRIV_EFFECTIVE; 760Sstevel@tonic-gate static const char *limit = PRIV_LIMIT; 770Sstevel@tonic-gate static const char *inheritable = PRIV_INHERITABLE; 780Sstevel@tonic-gate /* 790Sstevel@tonic-gate * Data independent privilege set operations. 800Sstevel@tonic-gate * 810Sstevel@tonic-gate * Only a few functions are provided that do not default to 820Sstevel@tonic-gate * the system implementation of privileges. A limited set of 830Sstevel@tonic-gate * interfaces is provided that accepts a priv_data_t * 840Sstevel@tonic-gate * argument; this set of interfaces is a private interface between libc 850Sstevel@tonic-gate * and libproc. It is delivered in order to interpret privilege sets 860Sstevel@tonic-gate * in debuggers in a implementation independent way. As such, we 870Sstevel@tonic-gate * don't need to provide the bulk of the interfaces, only a few 880Sstevel@tonic-gate * boolean tests (isfull, isempty) the name<->num mappings and 890Sstevel@tonic-gate * set pretty print functions. The boolean tests are only needed for 900Sstevel@tonic-gate * the latter, so those aren't provided externally. 910Sstevel@tonic-gate * 920Sstevel@tonic-gate * Additionally, we provide the function that maps the kernel implementation 930Sstevel@tonic-gate * structure into a libc private data structure. 940Sstevel@tonic-gate */ 950Sstevel@tonic-gate 960Sstevel@tonic-gate priv_data_t *privdata; 970Sstevel@tonic-gate 980Sstevel@tonic-gate static mutex_t pd_lock = DEFAULTMUTEX; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate static int 1010Sstevel@tonic-gate parseninfo(priv_info_names_t *na, char ***buf, int *cp) 1020Sstevel@tonic-gate { 1030Sstevel@tonic-gate char *q; 1040Sstevel@tonic-gate int i; 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate *buf = libc_malloc(sizeof (char *) * na->cnt); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate if (*buf == NULL) 1090Sstevel@tonic-gate return (-1); 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate q = na->names; 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate for (i = 0; i < na->cnt; i++) { 1140Sstevel@tonic-gate int l = strlen(q); 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate (*buf)[i] = q; 1170Sstevel@tonic-gate q += l + 1; 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate *cp = na->cnt; 1200Sstevel@tonic-gate return (0); 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate struct strint { 1240Sstevel@tonic-gate char *name; 1250Sstevel@tonic-gate int rank; 1260Sstevel@tonic-gate }; 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate static int 1290Sstevel@tonic-gate strintcmp(const void *a, const void *b) 1300Sstevel@tonic-gate { 1310Sstevel@tonic-gate const struct strint *ap = a; 1320Sstevel@tonic-gate const struct strint *bp = b; 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate return (strcasecmp(ap->name, bp->name)); 1350Sstevel@tonic-gate } 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate priv_data_t * 1380Sstevel@tonic-gate __priv_parse_info(priv_impl_info_t *ip) 1390Sstevel@tonic-gate { 1400Sstevel@tonic-gate priv_data_t *tmp; 1410Sstevel@tonic-gate char *x; 1420Sstevel@tonic-gate size_t size = PRIV_IMPL_INFO_SIZE(ip); 1430Sstevel@tonic-gate int i; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate tmp = libc_malloc(sizeof (*tmp)); 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate if (tmp == NULL) 1480Sstevel@tonic-gate return (NULL); 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate (void) memset(tmp, 0, sizeof (*tmp)); 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate tmp->pd_pinfo = ip; 1530Sstevel@tonic-gate tmp->pd_setsize = sizeof (priv_chunk_t) * ip->priv_setsize; 1540Sstevel@tonic-gate tmp->pd_ucredsize = UCRED_SIZE(ip); 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate x = (char *)ip; 1570Sstevel@tonic-gate x += ip->priv_headersize; 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate while (x < ((char *)ip) + size) { 1600Sstevel@tonic-gate /* LINTED: alignment */ 1610Sstevel@tonic-gate priv_info_names_t *na = (priv_info_names_t *)x; 1620Sstevel@tonic-gate /* LINTED: alignment */ 1630Sstevel@tonic-gate priv_info_set_t *st = (priv_info_set_t *)x; 1640Sstevel@tonic-gate struct strint *tmparr; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate switch (na->info.priv_info_type) { 1670Sstevel@tonic-gate case PRIV_INFO_SETNAMES: 1680Sstevel@tonic-gate if (parseninfo(na, &tmp->pd_setnames, &tmp->pd_nsets)) 1690Sstevel@tonic-gate goto out; 1700Sstevel@tonic-gate break; 1710Sstevel@tonic-gate case PRIV_INFO_PRIVNAMES: 1720Sstevel@tonic-gate if (parseninfo(na, &tmp->pd_privnames, &tmp->pd_nprivs)) 1730Sstevel@tonic-gate goto out; 1740Sstevel@tonic-gate /* 1750Sstevel@tonic-gate * We compute a sorted index which allows us 1760Sstevel@tonic-gate * to present a sorted list of privileges 1770Sstevel@tonic-gate * without actually having to sort it each time. 1780Sstevel@tonic-gate */ 1790Sstevel@tonic-gate tmp->pd_setsort = libc_malloc(tmp->pd_nprivs * 1800Sstevel@tonic-gate sizeof (int)); 1810Sstevel@tonic-gate if (tmp->pd_setsort == NULL) 1820Sstevel@tonic-gate goto out; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate tmparr = libc_malloc(tmp->pd_nprivs * 1850Sstevel@tonic-gate sizeof (struct strint)); 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate if (tmparr == NULL) 1880Sstevel@tonic-gate goto out; 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate for (i = 0; i < tmp->pd_nprivs; i++) { 1910Sstevel@tonic-gate tmparr[i].rank = i; 1920Sstevel@tonic-gate tmparr[i].name = tmp->pd_privnames[i]; 1930Sstevel@tonic-gate } 1940Sstevel@tonic-gate qsort(tmparr, tmp->pd_nprivs, sizeof (struct strint), 1956812Sraf strintcmp); 1960Sstevel@tonic-gate for (i = 0; i < tmp->pd_nprivs; i++) 1970Sstevel@tonic-gate tmp->pd_setsort[i] = tmparr[i].rank; 1980Sstevel@tonic-gate libc_free(tmparr); 1990Sstevel@tonic-gate break; 2000Sstevel@tonic-gate case PRIV_INFO_BASICPRIVS: 2010Sstevel@tonic-gate tmp->pd_basicset = (priv_set_t *)&st->set[0]; 2020Sstevel@tonic-gate break; 2030Sstevel@tonic-gate default: 2040Sstevel@tonic-gate /* unknown, ignore */ 2050Sstevel@tonic-gate break; 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate x += na->info.priv_info_size; 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate return (tmp); 2100Sstevel@tonic-gate out: 2110Sstevel@tonic-gate libc_free(tmp->pd_setnames); 2120Sstevel@tonic-gate libc_free(tmp->pd_privnames); 2130Sstevel@tonic-gate libc_free(tmp->pd_setsort); 2140Sstevel@tonic-gate libc_free(tmp); 2150Sstevel@tonic-gate return (NULL); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate /* 2190Sstevel@tonic-gate * Caller must have allocated d->pd_pinfo and should free it, 2200Sstevel@tonic-gate * if necessary. 2210Sstevel@tonic-gate */ 2220Sstevel@tonic-gate void 2230Sstevel@tonic-gate __priv_free_info(priv_data_t *d) 2240Sstevel@tonic-gate { 2250Sstevel@tonic-gate libc_free(d->pd_setnames); 2260Sstevel@tonic-gate libc_free(d->pd_privnames); 2270Sstevel@tonic-gate libc_free(d->pd_setsort); 2280Sstevel@tonic-gate libc_free(d); 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* 2321059Scasper * Return with the pd_lock held and data loaded or indicate failure. 2330Sstevel@tonic-gate */ 2341059Scasper int 2350Sstevel@tonic-gate lock_data(void) 2360Sstevel@tonic-gate { 2373864Sraf if (__priv_getdata() == NULL) 2381059Scasper return (-1); 2391059Scasper 2400Sstevel@tonic-gate lmutex_lock(&pd_lock); 2411059Scasper return (0); 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate boolean_t 2450Sstevel@tonic-gate refresh_data(void) 2460Sstevel@tonic-gate { 2470Sstevel@tonic-gate priv_impl_info_t *ip, ii; 2480Sstevel@tonic-gate priv_data_t *tmp; 2490Sstevel@tonic-gate char *p0, *q0; 2500Sstevel@tonic-gate int oldn, newn; 2510Sstevel@tonic-gate int i; 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate if (getprivinfo(&ii, sizeof (ii)) != 0 || 2540Sstevel@tonic-gate ii.priv_max == privdata->pd_nprivs) 2550Sstevel@tonic-gate return (B_FALSE); 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate ip = alloca(PRIV_IMPL_INFO_SIZE(&ii)); 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate (void) getprivinfo(ip, PRIV_IMPL_INFO_SIZE(&ii)); 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate /* Parse the info; then copy the additional bits */ 2620Sstevel@tonic-gate tmp = __priv_parse_info(ip); 2630Sstevel@tonic-gate if (tmp == NULL) 2640Sstevel@tonic-gate return (B_FALSE); 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate oldn = privdata->pd_nprivs; 2670Sstevel@tonic-gate p0 = privdata->pd_privnames[0]; 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate newn = tmp->pd_nprivs; 2700Sstevel@tonic-gate q0 = tmp->pd_privnames[0]; 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate /* copy the extra information to the old datastructure */ 2730Sstevel@tonic-gate (void) memcpy((char *)privdata->pd_pinfo + sizeof (priv_impl_info_t), 2746812Sraf (char *)ip + sizeof (priv_impl_info_t), 2756812Sraf PRIV_IMPL_INFO_SIZE(ip) - sizeof (priv_impl_info_t)); 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate /* Copy the first oldn pointers */ 2780Sstevel@tonic-gate (void) memcpy(tmp->pd_privnames, privdata->pd_privnames, 2790Sstevel@tonic-gate oldn * sizeof (char *)); 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate /* Adjust the rest */ 2820Sstevel@tonic-gate for (i = oldn; i < newn; i++) 2830Sstevel@tonic-gate tmp->pd_privnames[i] += p0 - q0; 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate /* Install the larger arrays */ 2860Sstevel@tonic-gate libc_free(privdata->pd_privnames); 2870Sstevel@tonic-gate privdata->pd_privnames = tmp->pd_privnames; 2880Sstevel@tonic-gate tmp->pd_privnames = NULL; 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate libc_free(privdata->pd_setsort); 2910Sstevel@tonic-gate privdata->pd_setsort = tmp->pd_setsort; 2920Sstevel@tonic-gate tmp->pd_setsort = NULL; 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate /* Copy the rest of the data */ 2950Sstevel@tonic-gate *privdata->pd_pinfo = *ip; 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate privdata->pd_nprivs = newn; 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate __priv_free_info(tmp); 3000Sstevel@tonic-gate return (B_TRUE); 3010Sstevel@tonic-gate } 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate void 3040Sstevel@tonic-gate unlock_data(void) 3050Sstevel@tonic-gate { 3060Sstevel@tonic-gate lmutex_unlock(&pd_lock); 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate static priv_set_t *__priv_allocset(priv_data_t *); 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate priv_data_t * 3120Sstevel@tonic-gate __priv_getdata(void) 3130Sstevel@tonic-gate { 3140Sstevel@tonic-gate if (privdata == NULL) { 3153864Sraf lmutex_lock(&pd_lock); 3163864Sraf if (privdata == NULL) { 3173864Sraf priv_data_t *tmp; 3183864Sraf priv_impl_info_t *ip; 3193864Sraf size_t size = sizeof (priv_impl_info_t) + 2048; 3203864Sraf size_t realsize; 3213864Sraf priv_impl_info_t *aip = alloca(size); 3220Sstevel@tonic-gate 3233864Sraf if (getprivinfo(aip, size) != 0) 3243864Sraf goto out; 3253864Sraf 3263864Sraf realsize = PRIV_IMPL_INFO_SIZE(aip); 3273864Sraf 3283864Sraf ip = libc_malloc(realsize); 3293864Sraf 3303864Sraf if (ip == NULL) 3313864Sraf goto out; 3320Sstevel@tonic-gate 3333864Sraf if (realsize <= size) { 3343864Sraf (void) memcpy(ip, aip, realsize); 3353864Sraf } else if (getprivinfo(ip, realsize) != 0) { 3363864Sraf libc_free(ip); 3373864Sraf goto out; 3383864Sraf } 3390Sstevel@tonic-gate 3403864Sraf if ((tmp = __priv_parse_info(ip)) == NULL) { 3413864Sraf libc_free(ip); 3423864Sraf goto out; 3433864Sraf } 3440Sstevel@tonic-gate 3453864Sraf /* Allocate the zoneset just once, here */ 3463864Sraf tmp->pd_zoneset = __priv_allocset(tmp); 3473864Sraf if (tmp->pd_zoneset == NULL) 3483864Sraf goto clean; 3490Sstevel@tonic-gate 3503864Sraf if (zone_getattr(getzoneid(), ZONE_ATTR_PRIVSET, 3513864Sraf tmp->pd_zoneset, tmp->pd_setsize) 3523864Sraf == tmp->pd_setsize) { 3533864Sraf membar_producer(); 3543864Sraf privdata = tmp; 3553864Sraf goto out; 3563864Sraf } 3573864Sraf 3583864Sraf priv_freeset(tmp->pd_zoneset); 3593864Sraf clean: 3603864Sraf __priv_free_info(tmp); 3610Sstevel@tonic-gate libc_free(ip); 3620Sstevel@tonic-gate } 3633864Sraf out: 3643864Sraf lmutex_unlock(&pd_lock); 3650Sstevel@tonic-gate } 3663864Sraf membar_consumer(); 3670Sstevel@tonic-gate return (privdata); 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate const priv_impl_info_t * 3716812Sraf getprivimplinfo(void) 3720Sstevel@tonic-gate { 3730Sstevel@tonic-gate priv_data_t *d; 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate LOADPRIVDATA(d); 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate return (d->pd_pinfo); 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate static priv_set_t * 3810Sstevel@tonic-gate priv_vlist(va_list ap) 3820Sstevel@tonic-gate { 3830Sstevel@tonic-gate priv_set_t *pset = priv_allocset(); 3840Sstevel@tonic-gate const char *priv; 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate if (pset == NULL) 3870Sstevel@tonic-gate return (NULL); 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate priv_emptyset(pset); 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate while ((priv = va_arg(ap, const char *)) != NULL) { 3920Sstevel@tonic-gate if (priv_addset(pset, priv) < 0) { 3930Sstevel@tonic-gate priv_freeset(pset); 3940Sstevel@tonic-gate return (NULL); 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate } 3970Sstevel@tonic-gate return (pset); 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate /* 4010Sstevel@tonic-gate * priv_set(op, set, priv_id1, priv_id2, ..., NULL) 4020Sstevel@tonic-gate * 4030Sstevel@tonic-gate * Library routine to enable a user process to set a specific 4040Sstevel@tonic-gate * privilege set appropriately using a single call. User is 4050Sstevel@tonic-gate * required to terminate the list of privileges with NULL. 4060Sstevel@tonic-gate */ 4070Sstevel@tonic-gate int 4080Sstevel@tonic-gate priv_set(priv_op_t op, priv_ptype_t setname, ...) 4090Sstevel@tonic-gate { 4100Sstevel@tonic-gate va_list ap; 4110Sstevel@tonic-gate priv_set_t *pset; 4120Sstevel@tonic-gate int ret; 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate va_start(ap, setname); 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate pset = priv_vlist(ap); 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate va_end(ap); 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate if (pset == NULL) 4210Sstevel@tonic-gate return (-1); 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate /* All sets */ 4240Sstevel@tonic-gate if (setname == NULL) { 4250Sstevel@tonic-gate priv_data_t *d; 4260Sstevel@tonic-gate int set; 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate LOADPRIVDATA(d); 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate for (set = 0; set < d->pd_nsets; set++) 4310Sstevel@tonic-gate if ((ret = syscall(SYS_privsys, PRIVSYS_SETPPRIV, op, 4326812Sraf set, (void *)pset, d->pd_setsize)) != 0) 4330Sstevel@tonic-gate break; 4340Sstevel@tonic-gate } else { 4350Sstevel@tonic-gate ret = setppriv(op, setname, pset); 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate priv_freeset(pset); 4390Sstevel@tonic-gate return (ret); 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate /* 4430Sstevel@tonic-gate * priv_ineffect(privilege). 4445331Samw * tests the existence of a privilege against the effective set. 4450Sstevel@tonic-gate */ 4460Sstevel@tonic-gate boolean_t 4470Sstevel@tonic-gate priv_ineffect(const char *priv) 4480Sstevel@tonic-gate { 4490Sstevel@tonic-gate priv_set_t *curset; 4500Sstevel@tonic-gate boolean_t res; 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate curset = priv_allocset(); 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate if (curset == NULL) 4550Sstevel@tonic-gate return (B_FALSE); 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate if (getppriv(effective, curset) != 0 || 4580Sstevel@tonic-gate !priv_ismember(curset, priv)) 4590Sstevel@tonic-gate res = B_FALSE; 4600Sstevel@tonic-gate else 4610Sstevel@tonic-gate res = B_TRUE; 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate priv_freeset(curset); 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate return (res); 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate /* 4690Sstevel@tonic-gate * The routine __init_daemon_priv() is private to Solaris and is 4700Sstevel@tonic-gate * used by daemons to limit the privileges they can use and 4710Sstevel@tonic-gate * to set the uid they run under. 4720Sstevel@tonic-gate */ 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate static const char root_cp[] = "/core.%f.%t"; 4750Sstevel@tonic-gate static const char daemon_cp[] = "/var/tmp/core.%f.%t"; 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate int 4780Sstevel@tonic-gate __init_daemon_priv(int flags, uid_t uid, gid_t gid, ...) 4790Sstevel@tonic-gate { 4800Sstevel@tonic-gate priv_set_t *nset; 4810Sstevel@tonic-gate priv_set_t *perm = NULL; 4820Sstevel@tonic-gate va_list pa; 4830Sstevel@tonic-gate priv_data_t *d; 4840Sstevel@tonic-gate int ret = -1; 4850Sstevel@tonic-gate char buf[1024]; 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate LOADPRIVDATA(d); 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate va_start(pa, gid); 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate nset = priv_vlist(pa); 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate va_end(pa); 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate if (nset == NULL) 4960Sstevel@tonic-gate return (-1); 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate /* Always add the basic set */ 4990Sstevel@tonic-gate if (d->pd_basicset != NULL) 5000Sstevel@tonic-gate priv_union(d->pd_basicset, nset); 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate /* 5030Sstevel@tonic-gate * This is not a significant failure: it allows us to start programs 5040Sstevel@tonic-gate * with sufficient privileges and with the proper uid. We don't 5050Sstevel@tonic-gate * care enough about the extra groups in that case. 5060Sstevel@tonic-gate */ 5070Sstevel@tonic-gate if (flags & PU_RESETGROUPS) 5080Sstevel@tonic-gate (void) setgroups(0, NULL); 5090Sstevel@tonic-gate 5104321Scasper if (gid != (gid_t)-1 && setgid(gid) != 0) 5110Sstevel@tonic-gate goto end; 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate perm = priv_allocset(); 5140Sstevel@tonic-gate if (perm == NULL) 5150Sstevel@tonic-gate goto end; 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate /* E = P */ 5180Sstevel@tonic-gate (void) getppriv(permitted, perm); 5190Sstevel@tonic-gate (void) setppriv(PRIV_SET, effective, perm); 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate /* Now reset suid and euid */ 5224321Scasper if (uid != (uid_t)-1 && setreuid(uid, uid) != 0) 5230Sstevel@tonic-gate goto end; 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate /* Check for the limit privs */ 5260Sstevel@tonic-gate if ((flags & PU_LIMITPRIVS) && 5270Sstevel@tonic-gate setppriv(PRIV_SET, limit, nset) != 0) 5280Sstevel@tonic-gate goto end; 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate if (flags & PU_CLEARLIMITSET) { 5310Sstevel@tonic-gate priv_emptyset(perm); 5320Sstevel@tonic-gate if (setppriv(PRIV_SET, limit, perm) != 0) 5330Sstevel@tonic-gate goto end; 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate /* Remove the privileges from all the other sets */ 5370Sstevel@tonic-gate if (setppriv(PRIV_SET, permitted, nset) != 0) 5380Sstevel@tonic-gate goto end; 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate if (!(flags & PU_INHERITPRIVS)) 5410Sstevel@tonic-gate priv_emptyset(nset); 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate ret = setppriv(PRIV_SET, inheritable, nset); 5440Sstevel@tonic-gate end: 5450Sstevel@tonic-gate priv_freeset(nset); 5460Sstevel@tonic-gate priv_freeset(perm); 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate if (core_get_process_path(buf, sizeof (buf), getpid()) == 0 && 5490Sstevel@tonic-gate strcmp(buf, "core") == 0) { 5500Sstevel@tonic-gate 5514321Scasper if ((uid == (uid_t)-1 ? geteuid() : uid) == 0) { 5520Sstevel@tonic-gate (void) core_set_process_path(root_cp, sizeof (root_cp), 5530Sstevel@tonic-gate getpid()); 5540Sstevel@tonic-gate } else { 5550Sstevel@tonic-gate (void) core_set_process_path(daemon_cp, 5560Sstevel@tonic-gate sizeof (daemon_cp), getpid()); 5570Sstevel@tonic-gate } 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate (void) setpflags(__PROC_PROTECT, 0); 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate return (ret); 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate /* 5650Sstevel@tonic-gate * The routine __fini_daemon_priv() is private to Solaris and is 5660Sstevel@tonic-gate * used by daemons to clear remaining unwanted privileges and 5670Sstevel@tonic-gate * reenable core dumps. 5680Sstevel@tonic-gate */ 5690Sstevel@tonic-gate void 5700Sstevel@tonic-gate __fini_daemon_priv(const char *priv, ...) 5710Sstevel@tonic-gate { 5720Sstevel@tonic-gate priv_set_t *nset; 5730Sstevel@tonic-gate va_list pa; 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate va_start(pa, priv); 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate if (priv != NULL) { 5780Sstevel@tonic-gate nset = priv_vlist(pa); 5790Sstevel@tonic-gate if (nset == NULL) 5800Sstevel@tonic-gate return; 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate (void) priv_addset(nset, priv); 5830Sstevel@tonic-gate (void) setppriv(PRIV_OFF, permitted, nset); 5840Sstevel@tonic-gate priv_freeset(nset); 5850Sstevel@tonic-gate } 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate va_end(pa); 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate (void) setpflags(__PROC_PROTECT, 0); 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate /* 5930Sstevel@tonic-gate * The routine __init_suid_priv() is private to Solaris and is 5940Sstevel@tonic-gate * used by set-uid root programs to limit the privileges acquired 5950Sstevel@tonic-gate * to those actually needed. 5960Sstevel@tonic-gate */ 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate static priv_set_t *bracketpriv; 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate int 6010Sstevel@tonic-gate __init_suid_priv(int flags, ...) 6020Sstevel@tonic-gate { 6030Sstevel@tonic-gate priv_set_t *nset = NULL; 6040Sstevel@tonic-gate priv_set_t *tmpset = NULL; 6050Sstevel@tonic-gate va_list pa; 6060Sstevel@tonic-gate int r = -1; 6070Sstevel@tonic-gate uid_t ruid, euid; 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate euid = geteuid(); 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate /* If we're not set-uid root, don't reset the uid */ 6120Sstevel@tonic-gate if (euid == 0) { 6130Sstevel@tonic-gate ruid = getuid(); 6140Sstevel@tonic-gate /* If we're running as root, keep everything */ 6150Sstevel@tonic-gate if (ruid == 0) 6160Sstevel@tonic-gate return (0); 6170Sstevel@tonic-gate } 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate /* Can call this only once */ 6200Sstevel@tonic-gate if (bracketpriv != NULL) 6210Sstevel@tonic-gate return (-1); 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate va_start(pa, flags); 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate nset = priv_vlist(pa); 6260Sstevel@tonic-gate 6270Sstevel@tonic-gate va_end(pa); 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate if (nset == NULL) 6300Sstevel@tonic-gate goto end; 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate tmpset = priv_allocset(); 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate if (tmpset == NULL) 6350Sstevel@tonic-gate goto end; 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate /* We cannot grow our privileges beyond P, so start there */ 6380Sstevel@tonic-gate (void) getppriv(permitted, tmpset); 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate /* Is the privilege we need even in P? */ 6410Sstevel@tonic-gate if (!priv_issubset(nset, tmpset)) 6420Sstevel@tonic-gate goto end; 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate bracketpriv = priv_allocset(); 6450Sstevel@tonic-gate if (bracketpriv == NULL) 6460Sstevel@tonic-gate goto end; 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate priv_copyset(nset, bracketpriv); 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate /* Always add the basic set */ 6510Sstevel@tonic-gate priv_union(priv_basic(), nset); 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate /* But don't add what we don't have */ 6540Sstevel@tonic-gate priv_intersect(tmpset, nset); 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate (void) getppriv(inheritable, tmpset); 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate /* And stir in the inheritable privileges */ 6590Sstevel@tonic-gate priv_union(tmpset, nset); 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate if ((r = setppriv(PRIV_SET, effective, tmpset)) != 0) 6620Sstevel@tonic-gate goto end; 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate if ((r = setppriv(PRIV_SET, permitted, nset)) != 0) 6650Sstevel@tonic-gate goto end; 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate if (flags & PU_CLEARLIMITSET) 6680Sstevel@tonic-gate priv_emptyset(nset); 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate if ((flags & (PU_LIMITPRIVS|PU_CLEARLIMITSET)) != 0 && 6710Sstevel@tonic-gate (r = setppriv(PRIV_SET, limit, nset)) != 0) 6720Sstevel@tonic-gate goto end; 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate if (euid == 0) 6750Sstevel@tonic-gate r = setreuid(ruid, ruid); 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate end: 6780Sstevel@tonic-gate priv_freeset(tmpset); 6790Sstevel@tonic-gate priv_freeset(nset); 6800Sstevel@tonic-gate if (r != 0) { 6810Sstevel@tonic-gate /* Fail without leaving uid 0 around */ 6820Sstevel@tonic-gate if (euid == 0) 6830Sstevel@tonic-gate (void) setreuid(ruid, ruid); 6840Sstevel@tonic-gate priv_freeset(bracketpriv); 6850Sstevel@tonic-gate bracketpriv = NULL; 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate return (r); 6890Sstevel@tonic-gate } 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate /* 6920Sstevel@tonic-gate * Toggle privileges on/off in the effective set. 6930Sstevel@tonic-gate */ 6940Sstevel@tonic-gate int 6950Sstevel@tonic-gate __priv_bracket(priv_op_t op) 6960Sstevel@tonic-gate { 6970Sstevel@tonic-gate /* We're running fully privileged or didn't check errors first time */ 6980Sstevel@tonic-gate if (bracketpriv == NULL) 6990Sstevel@tonic-gate return (0); 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate /* Only PRIV_ON and PRIV_OFF are valid */ 7020Sstevel@tonic-gate if (op == PRIV_SET) 7030Sstevel@tonic-gate return (-1); 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate return (setppriv(op, effective, bracketpriv)); 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate /* 7090Sstevel@tonic-gate * Remove privileges from E & P. 7100Sstevel@tonic-gate */ 7110Sstevel@tonic-gate void 7120Sstevel@tonic-gate __priv_relinquish(void) 7130Sstevel@tonic-gate { 7140Sstevel@tonic-gate if (bracketpriv != NULL) { 7150Sstevel@tonic-gate (void) setppriv(PRIV_OFF, permitted, bracketpriv); 7160Sstevel@tonic-gate priv_freeset(bracketpriv); 7170Sstevel@tonic-gate bracketpriv = NULL; 7180Sstevel@tonic-gate } 7190Sstevel@tonic-gate } 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate /* 7220Sstevel@tonic-gate * Use binary search on the ordered list. 7230Sstevel@tonic-gate */ 7240Sstevel@tonic-gate int 7250Sstevel@tonic-gate __priv_getbyname(const priv_data_t *d, const char *name) 7260Sstevel@tonic-gate { 7271059Scasper char *const *list; 7281059Scasper const int *order; 7290Sstevel@tonic-gate int lo = 0; 7301059Scasper int hi; 7311059Scasper 7321059Scasper if (d == NULL) 7331059Scasper return (-1); 7341059Scasper 7351059Scasper list = d->pd_privnames; 7361059Scasper order = d->pd_setsort; 7371059Scasper hi = d->pd_nprivs - 1; 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate if (strncasecmp(name, "priv_", 5) == 0) 7400Sstevel@tonic-gate name += 5; 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate do { 7430Sstevel@tonic-gate int mid = (lo + hi) / 2; 7440Sstevel@tonic-gate int res = strcasecmp(name, list[order[mid]]); 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate if (res == 0) 7470Sstevel@tonic-gate return (order[mid]); 7480Sstevel@tonic-gate else if (res < 0) 7490Sstevel@tonic-gate hi = mid - 1; 7500Sstevel@tonic-gate else 7510Sstevel@tonic-gate lo = mid + 1; 7520Sstevel@tonic-gate } while (lo <= hi); 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate errno = EINVAL; 7550Sstevel@tonic-gate return (-1); 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate int 7590Sstevel@tonic-gate priv_getbyname(const char *name) 7600Sstevel@tonic-gate { 7610Sstevel@tonic-gate WITHPRIVLOCKED(int, -1, __priv_getbyname(GETPRIVDATA(), name)); 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate int 7650Sstevel@tonic-gate __priv_getsetbyname(const priv_data_t *d, const char *name) 7660Sstevel@tonic-gate { 7670Sstevel@tonic-gate int i; 7680Sstevel@tonic-gate int n = d->pd_nsets; 7690Sstevel@tonic-gate char *const *list = d->pd_setnames; 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate if (strncasecmp(name, "priv_", 5) == 0) 7720Sstevel@tonic-gate name += 5; 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate for (i = 0; i < n; i++) { 7750Sstevel@tonic-gate if (strcasecmp(list[i], name) == 0) 7760Sstevel@tonic-gate return (i); 7770Sstevel@tonic-gate } 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate errno = EINVAL; 7800Sstevel@tonic-gate return (-1); 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate int 7840Sstevel@tonic-gate priv_getsetbyname(const char *name) 7850Sstevel@tonic-gate { 7860Sstevel@tonic-gate /* Not locked: sets don't change */ 7870Sstevel@tonic-gate return (__priv_getsetbyname(GETPRIVDATA(), name)); 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate static const char * 7910Sstevel@tonic-gate priv_bynum(int i, int n, char **list) 7920Sstevel@tonic-gate { 7930Sstevel@tonic-gate if (i < 0 || i >= n) 7940Sstevel@tonic-gate return (NULL); 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate return (list[i]); 7970Sstevel@tonic-gate } 7980Sstevel@tonic-gate 7990Sstevel@tonic-gate const char * 8000Sstevel@tonic-gate __priv_getbynum(const priv_data_t *d, int num) 8010Sstevel@tonic-gate { 8021059Scasper if (d == NULL) 8031059Scasper return (NULL); 8040Sstevel@tonic-gate return (priv_bynum(num, d->pd_nprivs, d->pd_privnames)); 8050Sstevel@tonic-gate } 8060Sstevel@tonic-gate 8070Sstevel@tonic-gate const char * 8080Sstevel@tonic-gate priv_getbynum(int num) 8090Sstevel@tonic-gate { 8100Sstevel@tonic-gate WITHPRIVLOCKED(const char *, NULL, __priv_getbynum(GETPRIVDATA(), num)); 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate const char * 8140Sstevel@tonic-gate __priv_getsetbynum(const priv_data_t *d, int num) 8150Sstevel@tonic-gate { 8161059Scasper if (d == NULL) 8171059Scasper return (NULL); 8180Sstevel@tonic-gate return (priv_bynum(num, d->pd_nsets, d->pd_setnames)); 8190Sstevel@tonic-gate } 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate const char * 8220Sstevel@tonic-gate priv_getsetbynum(int num) 8230Sstevel@tonic-gate { 8240Sstevel@tonic-gate return (__priv_getsetbynum(GETPRIVDATA(), num)); 8250Sstevel@tonic-gate } 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate /* 8290Sstevel@tonic-gate * Privilege manipulation functions 8300Sstevel@tonic-gate * 8310Sstevel@tonic-gate * Without knowing the details of the privilege set implementation, 8320Sstevel@tonic-gate * opaque pointers can be used to manipulate sets at will. 8330Sstevel@tonic-gate */ 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate static priv_set_t * 8360Sstevel@tonic-gate __priv_allocset(priv_data_t *d) 8370Sstevel@tonic-gate { 8381059Scasper if (d == NULL) 8391059Scasper return (NULL); 8401059Scasper 8410Sstevel@tonic-gate return (libc_malloc(d->pd_setsize)); 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate priv_set_t * 8450Sstevel@tonic-gate priv_allocset(void) 8460Sstevel@tonic-gate { 8470Sstevel@tonic-gate return (__priv_allocset(GETPRIVDATA())); 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate void 8510Sstevel@tonic-gate priv_freeset(priv_set_t *p) 8520Sstevel@tonic-gate { 8530Sstevel@tonic-gate int er = errno; 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate libc_free(p); 8560Sstevel@tonic-gate errno = er; 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate void 8600Sstevel@tonic-gate __priv_emptyset(priv_data_t *d, priv_set_t *set) 8610Sstevel@tonic-gate { 8620Sstevel@tonic-gate (void) memset(set, 0, d->pd_setsize); 8630Sstevel@tonic-gate } 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate void 8660Sstevel@tonic-gate priv_emptyset(priv_set_t *set) 8670Sstevel@tonic-gate { 8680Sstevel@tonic-gate __priv_emptyset(GETPRIVDATA(), set); 8690Sstevel@tonic-gate } 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate void 872*11537SCasper.Dik@Sun.COM priv_basicset(priv_set_t *set) 873*11537SCasper.Dik@Sun.COM { 874*11537SCasper.Dik@Sun.COM priv_data_t *d; 875*11537SCasper.Dik@Sun.COM 876*11537SCasper.Dik@Sun.COM LOADPRIVDATA(d); 877*11537SCasper.Dik@Sun.COM 878*11537SCasper.Dik@Sun.COM priv_copyset(d->pd_basicset, set); 879*11537SCasper.Dik@Sun.COM } 880*11537SCasper.Dik@Sun.COM 881*11537SCasper.Dik@Sun.COM void 8820Sstevel@tonic-gate __priv_fillset(priv_data_t *d, priv_set_t *set) 8830Sstevel@tonic-gate { 8840Sstevel@tonic-gate (void) memset(set, ~0, d->pd_setsize); 8850Sstevel@tonic-gate } 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate void 8880Sstevel@tonic-gate priv_fillset(priv_set_t *set) 8890Sstevel@tonic-gate { 8900Sstevel@tonic-gate __priv_fillset(GETPRIVDATA(), set); 8910Sstevel@tonic-gate } 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate #define PRIV_TEST_BODY_D(d, test) \ 8950Sstevel@tonic-gate int i; \ 8960Sstevel@tonic-gate \ 8970Sstevel@tonic-gate for (i = d->pd_pinfo->priv_setsize; i-- > 0; ) \ 8980Sstevel@tonic-gate if (!(test)) \ 8990Sstevel@tonic-gate return (B_FALSE); \ 9000Sstevel@tonic-gate \ 9010Sstevel@tonic-gate return (B_TRUE) 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate boolean_t 9040Sstevel@tonic-gate priv_isequalset(const priv_set_t *a, const priv_set_t *b) 9050Sstevel@tonic-gate { 9060Sstevel@tonic-gate priv_data_t *d; 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate LOADPRIVDATA(d); 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate return ((boolean_t)(memcmp(a, b, d->pd_setsize) == 0)); 9110Sstevel@tonic-gate } 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate boolean_t 9140Sstevel@tonic-gate __priv_isemptyset(priv_data_t *d, const priv_set_t *set) 9150Sstevel@tonic-gate { 9160Sstevel@tonic-gate PRIV_TEST_BODY_D(d, ((priv_chunk_t *)set)[i] == 0); 9170Sstevel@tonic-gate } 9180Sstevel@tonic-gate 9190Sstevel@tonic-gate boolean_t 9200Sstevel@tonic-gate priv_isemptyset(const priv_set_t *set) 9210Sstevel@tonic-gate { 9220Sstevel@tonic-gate return (__priv_isemptyset(GETPRIVDATA(), set)); 9230Sstevel@tonic-gate } 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate boolean_t 9260Sstevel@tonic-gate __priv_isfullset(priv_data_t *d, const priv_set_t *set) 9270Sstevel@tonic-gate { 9280Sstevel@tonic-gate PRIV_TEST_BODY_D(d, ((priv_chunk_t *)set)[i] == ~(priv_chunk_t)0); 9290Sstevel@tonic-gate } 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate boolean_t 9320Sstevel@tonic-gate priv_isfullset(const priv_set_t *set) 9330Sstevel@tonic-gate { 9340Sstevel@tonic-gate return (__priv_isfullset(GETPRIVDATA(), set)); 9350Sstevel@tonic-gate } 9360Sstevel@tonic-gate 9370Sstevel@tonic-gate /* 9380Sstevel@tonic-gate * Return true if a is a subset of b 9390Sstevel@tonic-gate */ 9400Sstevel@tonic-gate boolean_t 9410Sstevel@tonic-gate __priv_issubset(priv_data_t *d, const priv_set_t *a, const priv_set_t *b) 9420Sstevel@tonic-gate { 9430Sstevel@tonic-gate PRIV_TEST_BODY_D(d, (((priv_chunk_t *)a)[i] | ((priv_chunk_t *)b)[i]) == 9446812Sraf ((priv_chunk_t *)b)[i]); 9450Sstevel@tonic-gate } 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate boolean_t 9480Sstevel@tonic-gate priv_issubset(const priv_set_t *a, const priv_set_t *b) 9490Sstevel@tonic-gate { 9500Sstevel@tonic-gate return (__priv_issubset(GETPRIVDATA(), a, b)); 9510Sstevel@tonic-gate } 9520Sstevel@tonic-gate 9530Sstevel@tonic-gate #define PRIV_CHANGE_BODY(a, op, b) \ 9540Sstevel@tonic-gate int i; \ 9550Sstevel@tonic-gate priv_data_t *d; \ 9560Sstevel@tonic-gate \ 9570Sstevel@tonic-gate LOADPRIVDATA(d); \ 9580Sstevel@tonic-gate \ 9590Sstevel@tonic-gate for (i = 0; i < d->pd_pinfo->priv_setsize; i++) \ 9600Sstevel@tonic-gate ((priv_chunk_t *)a)[i] op \ 9610Sstevel@tonic-gate ((priv_chunk_t *)b)[i] 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate /* B = A ^ B */ 9640Sstevel@tonic-gate void 9650Sstevel@tonic-gate priv_intersect(const priv_set_t *a, priv_set_t *b) 9660Sstevel@tonic-gate { 9670Sstevel@tonic-gate /* CSTYLED */ 9680Sstevel@tonic-gate PRIV_CHANGE_BODY(b, &=, a); 9690Sstevel@tonic-gate } 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate /* B = A */ 9720Sstevel@tonic-gate void 9730Sstevel@tonic-gate priv_copyset(const priv_set_t *a, priv_set_t *b) 9740Sstevel@tonic-gate { 9750Sstevel@tonic-gate /* CSTYLED */ 9760Sstevel@tonic-gate PRIV_CHANGE_BODY(b, =, a); 9770Sstevel@tonic-gate } 9780Sstevel@tonic-gate 9790Sstevel@tonic-gate /* B = A v B */ 9800Sstevel@tonic-gate void 9810Sstevel@tonic-gate priv_union(const priv_set_t *a, priv_set_t *b) 9820Sstevel@tonic-gate { 9830Sstevel@tonic-gate /* CSTYLED */ 9840Sstevel@tonic-gate PRIV_CHANGE_BODY(b, |=, a); 9850Sstevel@tonic-gate } 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate /* A = ! A */ 9880Sstevel@tonic-gate void 9890Sstevel@tonic-gate priv_inverse(priv_set_t *a) 9900Sstevel@tonic-gate { 9910Sstevel@tonic-gate PRIV_CHANGE_BODY(a, = ~, a); 9920Sstevel@tonic-gate } 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate /* 9950Sstevel@tonic-gate * Manipulating single privileges. 9960Sstevel@tonic-gate */ 9970Sstevel@tonic-gate 9980Sstevel@tonic-gate int 9990Sstevel@tonic-gate priv_addset(priv_set_t *a, const char *p) 10000Sstevel@tonic-gate { 10010Sstevel@tonic-gate int priv = priv_getbyname(p); 10020Sstevel@tonic-gate 10030Sstevel@tonic-gate if (priv < 0) 10040Sstevel@tonic-gate return (-1); 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate PRIV_ADDSET(a, priv); 10070Sstevel@tonic-gate 10080Sstevel@tonic-gate return (0); 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate int 10120Sstevel@tonic-gate priv_delset(priv_set_t *a, const char *p) 10130Sstevel@tonic-gate { 10140Sstevel@tonic-gate int priv = priv_getbyname(p); 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate if (priv < 0) 10170Sstevel@tonic-gate return (-1); 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate PRIV_DELSET(a, priv); 10200Sstevel@tonic-gate return (0); 10210Sstevel@tonic-gate } 10220Sstevel@tonic-gate 10230Sstevel@tonic-gate boolean_t 10240Sstevel@tonic-gate priv_ismember(const priv_set_t *a, const char *p) 10250Sstevel@tonic-gate { 10260Sstevel@tonic-gate int priv = priv_getbyname(p); 10270Sstevel@tonic-gate 10280Sstevel@tonic-gate if (priv < 0) 10290Sstevel@tonic-gate return (B_FALSE); 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate return ((boolean_t)PRIV_ISMEMBER(a, priv)); 10320Sstevel@tonic-gate } 1033