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
5e3895e32Scasper * Common Development and Distribution License (the "License").
6e3895e32Scasper * 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 */
21cb620785Sraf
227c478bd9Sstevel@tonic-gate /*
23*75598e10SGary Mills * Copyright 2015 Gary Mills
24134a1f4eSCasper H.S. Dik * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277257d1b4Sraf #pragma weak _getprivimplinfo = getprivimplinfo
287257d1b4Sraf #pragma weak _priv_addset = priv_addset
297257d1b4Sraf #pragma weak _priv_allocset = priv_allocset
307257d1b4Sraf #pragma weak _priv_copyset = priv_copyset
317257d1b4Sraf #pragma weak _priv_delset = priv_delset
327257d1b4Sraf #pragma weak _priv_emptyset = priv_emptyset
33634e26ecSCasper H.S. Dik #pragma weak _priv_basicset = priv_basicset
347257d1b4Sraf #pragma weak _priv_fillset = priv_fillset
357257d1b4Sraf #pragma weak _priv_freeset = priv_freeset
367257d1b4Sraf #pragma weak _priv_getbyname = priv_getbyname
377257d1b4Sraf #pragma weak _priv_getbynum = priv_getbynum
387257d1b4Sraf #pragma weak _priv_getsetbyname = priv_getsetbyname
397257d1b4Sraf #pragma weak _priv_getsetbynum = priv_getsetbynum
407257d1b4Sraf #pragma weak _priv_ineffect = priv_ineffect
417257d1b4Sraf #pragma weak _priv_intersect = priv_intersect
427257d1b4Sraf #pragma weak _priv_inverse = priv_inverse
437257d1b4Sraf #pragma weak _priv_isemptyset = priv_isemptyset
447257d1b4Sraf #pragma weak _priv_isequalset = priv_isequalset
457257d1b4Sraf #pragma weak _priv_isfullset = priv_isfullset
467257d1b4Sraf #pragma weak _priv_ismember = priv_ismember
477257d1b4Sraf #pragma weak _priv_issubset = priv_issubset
487257d1b4Sraf #pragma weak _priv_set = priv_set
497257d1b4Sraf #pragma weak _priv_union = priv_union
507c478bd9Sstevel@tonic-gate
517257d1b4Sraf #include "lint.h"
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #define _STRUCTURED_PROC 1
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #include "priv_private.h"
567c478bd9Sstevel@tonic-gate #include "mtlib.h"
577c478bd9Sstevel@tonic-gate #include "libc.h"
587c478bd9Sstevel@tonic-gate #include <errno.h>
597c478bd9Sstevel@tonic-gate #include <stdarg.h>
607c478bd9Sstevel@tonic-gate #include <stdlib.h>
617c478bd9Sstevel@tonic-gate #include <unistd.h>
627c478bd9Sstevel@tonic-gate #include <strings.h>
637c478bd9Sstevel@tonic-gate #include <synch.h>
647c478bd9Sstevel@tonic-gate #include <alloca.h>
65cb620785Sraf #include <atomic.h>
667c478bd9Sstevel@tonic-gate #include <sys/ucred.h>
677c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
687c478bd9Sstevel@tonic-gate #include <sys/param.h>
697c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
707c478bd9Sstevel@tonic-gate #include <priv_utils.h>
717c478bd9Sstevel@tonic-gate #include <zone.h>
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate /* Include each string only once - until the compiler/linker are fixed */
747c478bd9Sstevel@tonic-gate static const char *permitted = PRIV_PERMITTED;
757c478bd9Sstevel@tonic-gate static const char *effective = PRIV_EFFECTIVE;
767c478bd9Sstevel@tonic-gate static const char *limit = PRIV_LIMIT;
777c478bd9Sstevel@tonic-gate static const char *inheritable = PRIV_INHERITABLE;
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate * Data independent privilege set operations.
807c478bd9Sstevel@tonic-gate *
817c478bd9Sstevel@tonic-gate * Only a few functions are provided that do not default to
827c478bd9Sstevel@tonic-gate * the system implementation of privileges. A limited set of
837c478bd9Sstevel@tonic-gate * interfaces is provided that accepts a priv_data_t *
847c478bd9Sstevel@tonic-gate * argument; this set of interfaces is a private interface between libc
857c478bd9Sstevel@tonic-gate * and libproc. It is delivered in order to interpret privilege sets
867c478bd9Sstevel@tonic-gate * in debuggers in a implementation independent way. As such, we
877c478bd9Sstevel@tonic-gate * don't need to provide the bulk of the interfaces, only a few
887c478bd9Sstevel@tonic-gate * boolean tests (isfull, isempty) the name<->num mappings and
897c478bd9Sstevel@tonic-gate * set pretty print functions. The boolean tests are only needed for
907c478bd9Sstevel@tonic-gate * the latter, so those aren't provided externally.
917c478bd9Sstevel@tonic-gate *
927c478bd9Sstevel@tonic-gate * Additionally, we provide the function that maps the kernel implementation
937c478bd9Sstevel@tonic-gate * structure into a libc private data structure.
947c478bd9Sstevel@tonic-gate */
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate priv_data_t *privdata;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate static mutex_t pd_lock = DEFAULTMUTEX;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate static int
parseninfo(priv_info_names_t * na,char *** buf,int * cp)1017c478bd9Sstevel@tonic-gate parseninfo(priv_info_names_t *na, char ***buf, int *cp)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate char *q;
1047c478bd9Sstevel@tonic-gate int i;
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate *buf = libc_malloc(sizeof (char *) * na->cnt);
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate if (*buf == NULL)
1097c478bd9Sstevel@tonic-gate return (-1);
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate q = na->names;
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate for (i = 0; i < na->cnt; i++) {
1147c478bd9Sstevel@tonic-gate int l = strlen(q);
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate (*buf)[i] = q;
1177c478bd9Sstevel@tonic-gate q += l + 1;
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate *cp = na->cnt;
1207c478bd9Sstevel@tonic-gate return (0);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate struct strint {
1247c478bd9Sstevel@tonic-gate char *name;
1257c478bd9Sstevel@tonic-gate int rank;
1267c478bd9Sstevel@tonic-gate };
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate static int
strintcmp(const void * a,const void * b)1297c478bd9Sstevel@tonic-gate strintcmp(const void *a, const void *b)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate const struct strint *ap = a;
1327c478bd9Sstevel@tonic-gate const struct strint *bp = b;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate return (strcasecmp(ap->name, bp->name));
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate priv_data_t *
__priv_parse_info(priv_impl_info_t * ip)1387c478bd9Sstevel@tonic-gate __priv_parse_info(priv_impl_info_t *ip)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate priv_data_t *tmp;
1417c478bd9Sstevel@tonic-gate char *x;
1427c478bd9Sstevel@tonic-gate size_t size = PRIV_IMPL_INFO_SIZE(ip);
1437c478bd9Sstevel@tonic-gate int i;
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate tmp = libc_malloc(sizeof (*tmp));
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate if (tmp == NULL)
1487c478bd9Sstevel@tonic-gate return (NULL);
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate (void) memset(tmp, 0, sizeof (*tmp));
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate tmp->pd_pinfo = ip;
1537c478bd9Sstevel@tonic-gate tmp->pd_setsize = sizeof (priv_chunk_t) * ip->priv_setsize;
1547c478bd9Sstevel@tonic-gate tmp->pd_ucredsize = UCRED_SIZE(ip);
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate x = (char *)ip;
1577c478bd9Sstevel@tonic-gate x += ip->priv_headersize;
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate while (x < ((char *)ip) + size) {
1607c478bd9Sstevel@tonic-gate /* LINTED: alignment */
1617c478bd9Sstevel@tonic-gate priv_info_names_t *na = (priv_info_names_t *)x;
1627c478bd9Sstevel@tonic-gate /* LINTED: alignment */
1637c478bd9Sstevel@tonic-gate priv_info_set_t *st = (priv_info_set_t *)x;
1647c478bd9Sstevel@tonic-gate struct strint *tmparr;
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate switch (na->info.priv_info_type) {
1677c478bd9Sstevel@tonic-gate case PRIV_INFO_SETNAMES:
1687c478bd9Sstevel@tonic-gate if (parseninfo(na, &tmp->pd_setnames, &tmp->pd_nsets))
1697c478bd9Sstevel@tonic-gate goto out;
1707c478bd9Sstevel@tonic-gate break;
1717c478bd9Sstevel@tonic-gate case PRIV_INFO_PRIVNAMES:
1727c478bd9Sstevel@tonic-gate if (parseninfo(na, &tmp->pd_privnames, &tmp->pd_nprivs))
1737c478bd9Sstevel@tonic-gate goto out;
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate * We compute a sorted index which allows us
1767c478bd9Sstevel@tonic-gate * to present a sorted list of privileges
1777c478bd9Sstevel@tonic-gate * without actually having to sort it each time.
1787c478bd9Sstevel@tonic-gate */
1797c478bd9Sstevel@tonic-gate tmp->pd_setsort = libc_malloc(tmp->pd_nprivs *
1807c478bd9Sstevel@tonic-gate sizeof (int));
1817c478bd9Sstevel@tonic-gate if (tmp->pd_setsort == NULL)
1827c478bd9Sstevel@tonic-gate goto out;
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate tmparr = libc_malloc(tmp->pd_nprivs *
1857c478bd9Sstevel@tonic-gate sizeof (struct strint));
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate if (tmparr == NULL)
1887c478bd9Sstevel@tonic-gate goto out;
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate for (i = 0; i < tmp->pd_nprivs; i++) {
1917c478bd9Sstevel@tonic-gate tmparr[i].rank = i;
1927c478bd9Sstevel@tonic-gate tmparr[i].name = tmp->pd_privnames[i];
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate qsort(tmparr, tmp->pd_nprivs, sizeof (struct strint),
1957c478bd9Sstevel@tonic-gate strintcmp);
1967c478bd9Sstevel@tonic-gate for (i = 0; i < tmp->pd_nprivs; i++)
1977c478bd9Sstevel@tonic-gate tmp->pd_setsort[i] = tmparr[i].rank;
1987c478bd9Sstevel@tonic-gate libc_free(tmparr);
1997c478bd9Sstevel@tonic-gate break;
2007c478bd9Sstevel@tonic-gate case PRIV_INFO_BASICPRIVS:
2017c478bd9Sstevel@tonic-gate tmp->pd_basicset = (priv_set_t *)&st->set[0];
2027c478bd9Sstevel@tonic-gate break;
2037c478bd9Sstevel@tonic-gate default:
2047c478bd9Sstevel@tonic-gate /* unknown, ignore */
2057c478bd9Sstevel@tonic-gate break;
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate x += na->info.priv_info_size;
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate return (tmp);
2107c478bd9Sstevel@tonic-gate out:
2117c478bd9Sstevel@tonic-gate libc_free(tmp->pd_setnames);
2127c478bd9Sstevel@tonic-gate libc_free(tmp->pd_privnames);
2137c478bd9Sstevel@tonic-gate libc_free(tmp->pd_setsort);
2147c478bd9Sstevel@tonic-gate libc_free(tmp);
2157c478bd9Sstevel@tonic-gate return (NULL);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate * Caller must have allocated d->pd_pinfo and should free it,
2207c478bd9Sstevel@tonic-gate * if necessary.
2217c478bd9Sstevel@tonic-gate */
2227c478bd9Sstevel@tonic-gate void
__priv_free_info(priv_data_t * d)2237c478bd9Sstevel@tonic-gate __priv_free_info(priv_data_t *d)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate libc_free(d->pd_setnames);
2267c478bd9Sstevel@tonic-gate libc_free(d->pd_privnames);
2277c478bd9Sstevel@tonic-gate libc_free(d->pd_setsort);
2287c478bd9Sstevel@tonic-gate libc_free(d);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate /*
232e3895e32Scasper * Return with the pd_lock held and data loaded or indicate failure.
2337c478bd9Sstevel@tonic-gate */
234e3895e32Scasper int
lock_data(void)2357c478bd9Sstevel@tonic-gate lock_data(void)
2367c478bd9Sstevel@tonic-gate {
237cb620785Sraf if (__priv_getdata() == NULL)
238e3895e32Scasper return (-1);
239e3895e32Scasper
2407c478bd9Sstevel@tonic-gate lmutex_lock(&pd_lock);
241e3895e32Scasper return (0);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate boolean_t
refresh_data(void)2457c478bd9Sstevel@tonic-gate refresh_data(void)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate priv_impl_info_t *ip, ii;
2487c478bd9Sstevel@tonic-gate priv_data_t *tmp;
2497c478bd9Sstevel@tonic-gate char *p0, *q0;
2507c478bd9Sstevel@tonic-gate int oldn, newn;
2517c478bd9Sstevel@tonic-gate int i;
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate if (getprivinfo(&ii, sizeof (ii)) != 0 ||
2547c478bd9Sstevel@tonic-gate ii.priv_max == privdata->pd_nprivs)
2557c478bd9Sstevel@tonic-gate return (B_FALSE);
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate ip = alloca(PRIV_IMPL_INFO_SIZE(&ii));
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate (void) getprivinfo(ip, PRIV_IMPL_INFO_SIZE(&ii));
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate /* Parse the info; then copy the additional bits */
2627c478bd9Sstevel@tonic-gate tmp = __priv_parse_info(ip);
2637c478bd9Sstevel@tonic-gate if (tmp == NULL)
2647c478bd9Sstevel@tonic-gate return (B_FALSE);
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate oldn = privdata->pd_nprivs;
2677c478bd9Sstevel@tonic-gate p0 = privdata->pd_privnames[0];
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate newn = tmp->pd_nprivs;
2707c478bd9Sstevel@tonic-gate q0 = tmp->pd_privnames[0];
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate /* copy the extra information to the old datastructure */
2737c478bd9Sstevel@tonic-gate (void) memcpy((char *)privdata->pd_pinfo + sizeof (priv_impl_info_t),
2747c478bd9Sstevel@tonic-gate (char *)ip + sizeof (priv_impl_info_t),
2757c478bd9Sstevel@tonic-gate PRIV_IMPL_INFO_SIZE(ip) - sizeof (priv_impl_info_t));
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate /* Copy the first oldn pointers */
2787c478bd9Sstevel@tonic-gate (void) memcpy(tmp->pd_privnames, privdata->pd_privnames,
2797c478bd9Sstevel@tonic-gate oldn * sizeof (char *));
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /* Adjust the rest */
2827c478bd9Sstevel@tonic-gate for (i = oldn; i < newn; i++)
2837c478bd9Sstevel@tonic-gate tmp->pd_privnames[i] += p0 - q0;
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate /* Install the larger arrays */
2867c478bd9Sstevel@tonic-gate libc_free(privdata->pd_privnames);
2877c478bd9Sstevel@tonic-gate privdata->pd_privnames = tmp->pd_privnames;
2887c478bd9Sstevel@tonic-gate tmp->pd_privnames = NULL;
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate libc_free(privdata->pd_setsort);
2917c478bd9Sstevel@tonic-gate privdata->pd_setsort = tmp->pd_setsort;
2927c478bd9Sstevel@tonic-gate tmp->pd_setsort = NULL;
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate /* Copy the rest of the data */
2957c478bd9Sstevel@tonic-gate *privdata->pd_pinfo = *ip;
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate privdata->pd_nprivs = newn;
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate __priv_free_info(tmp);
3007c478bd9Sstevel@tonic-gate return (B_TRUE);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate void
unlock_data(void)3047c478bd9Sstevel@tonic-gate unlock_data(void)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate lmutex_unlock(&pd_lock);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate static priv_set_t *__priv_allocset(priv_data_t *);
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate priv_data_t *
__priv_getdata(void)3127c478bd9Sstevel@tonic-gate __priv_getdata(void)
3137c478bd9Sstevel@tonic-gate {
314cb620785Sraf if (privdata == NULL) {
3157c478bd9Sstevel@tonic-gate lmutex_lock(&pd_lock);
3167c478bd9Sstevel@tonic-gate if (privdata == NULL) {
3177c478bd9Sstevel@tonic-gate priv_data_t *tmp;
3187c478bd9Sstevel@tonic-gate priv_impl_info_t *ip;
3197c478bd9Sstevel@tonic-gate size_t size = sizeof (priv_impl_info_t) + 2048;
3207c478bd9Sstevel@tonic-gate size_t realsize;
3217c478bd9Sstevel@tonic-gate priv_impl_info_t *aip = alloca(size);
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate if (getprivinfo(aip, size) != 0)
3247c478bd9Sstevel@tonic-gate goto out;
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate realsize = PRIV_IMPL_INFO_SIZE(aip);
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate ip = libc_malloc(realsize);
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate if (ip == NULL)
3317c478bd9Sstevel@tonic-gate goto out;
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate if (realsize <= size) {
3347c478bd9Sstevel@tonic-gate (void) memcpy(ip, aip, realsize);
3357c478bd9Sstevel@tonic-gate } else if (getprivinfo(ip, realsize) != 0) {
3367c478bd9Sstevel@tonic-gate libc_free(ip);
3377c478bd9Sstevel@tonic-gate goto out;
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate if ((tmp = __priv_parse_info(ip)) == NULL) {
3417c478bd9Sstevel@tonic-gate libc_free(ip);
3427c478bd9Sstevel@tonic-gate goto out;
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate /* Allocate the zoneset just once, here */
3467c478bd9Sstevel@tonic-gate tmp->pd_zoneset = __priv_allocset(tmp);
3477c478bd9Sstevel@tonic-gate if (tmp->pd_zoneset == NULL)
3487c478bd9Sstevel@tonic-gate goto clean;
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate if (zone_getattr(getzoneid(), ZONE_ATTR_PRIVSET,
351cb620785Sraf tmp->pd_zoneset, tmp->pd_setsize)
352cb620785Sraf == tmp->pd_setsize) {
353cb620785Sraf membar_producer();
3547c478bd9Sstevel@tonic-gate privdata = tmp;
3557c478bd9Sstevel@tonic-gate goto out;
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate priv_freeset(tmp->pd_zoneset);
3597c478bd9Sstevel@tonic-gate clean:
3607c478bd9Sstevel@tonic-gate __priv_free_info(tmp);
3617c478bd9Sstevel@tonic-gate libc_free(ip);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate out:
3647c478bd9Sstevel@tonic-gate lmutex_unlock(&pd_lock);
365cb620785Sraf }
366cb620785Sraf membar_consumer();
3677c478bd9Sstevel@tonic-gate return (privdata);
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate const priv_impl_info_t *
getprivimplinfo(void)3717257d1b4Sraf getprivimplinfo(void)
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate priv_data_t *d;
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate LOADPRIVDATA(d);
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate return (d->pd_pinfo);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate static priv_set_t *
priv_vlist(va_list ap)3817c478bd9Sstevel@tonic-gate priv_vlist(va_list ap)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate priv_set_t *pset = priv_allocset();
3847c478bd9Sstevel@tonic-gate const char *priv;
3857c478bd9Sstevel@tonic-gate
3867c478bd9Sstevel@tonic-gate if (pset == NULL)
3877c478bd9Sstevel@tonic-gate return (NULL);
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate priv_emptyset(pset);
3907c478bd9Sstevel@tonic-gate
3917c478bd9Sstevel@tonic-gate while ((priv = va_arg(ap, const char *)) != NULL) {
3927c478bd9Sstevel@tonic-gate if (priv_addset(pset, priv) < 0) {
3937c478bd9Sstevel@tonic-gate priv_freeset(pset);
3947c478bd9Sstevel@tonic-gate return (NULL);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate return (pset);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate * priv_set(op, set, priv_id1, priv_id2, ..., NULL)
4027c478bd9Sstevel@tonic-gate *
4037c478bd9Sstevel@tonic-gate * Library routine to enable a user process to set a specific
4047c478bd9Sstevel@tonic-gate * privilege set appropriately using a single call. User is
4057c478bd9Sstevel@tonic-gate * required to terminate the list of privileges with NULL.
4067c478bd9Sstevel@tonic-gate */
4077c478bd9Sstevel@tonic-gate int
priv_set(priv_op_t op,priv_ptype_t setname,...)4087c478bd9Sstevel@tonic-gate priv_set(priv_op_t op, priv_ptype_t setname, ...)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate va_list ap;
4117c478bd9Sstevel@tonic-gate priv_set_t *pset;
4127c478bd9Sstevel@tonic-gate int ret;
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate va_start(ap, setname);
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate pset = priv_vlist(ap);
4177c478bd9Sstevel@tonic-gate
4187c478bd9Sstevel@tonic-gate va_end(ap);
4197c478bd9Sstevel@tonic-gate
4207c478bd9Sstevel@tonic-gate if (pset == NULL)
4217c478bd9Sstevel@tonic-gate return (-1);
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate /* All sets */
4247c478bd9Sstevel@tonic-gate if (setname == NULL) {
4257c478bd9Sstevel@tonic-gate priv_data_t *d;
4267c478bd9Sstevel@tonic-gate int set;
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate LOADPRIVDATA(d);
4297c478bd9Sstevel@tonic-gate
4307c478bd9Sstevel@tonic-gate for (set = 0; set < d->pd_nsets; set++)
4317c478bd9Sstevel@tonic-gate if ((ret = syscall(SYS_privsys, PRIVSYS_SETPPRIV, op,
4327c478bd9Sstevel@tonic-gate set, (void *)pset, d->pd_setsize)) != 0)
4337c478bd9Sstevel@tonic-gate break;
4347c478bd9Sstevel@tonic-gate } else {
4357c478bd9Sstevel@tonic-gate ret = setppriv(op, setname, pset);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate priv_freeset(pset);
4397c478bd9Sstevel@tonic-gate return (ret);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate * priv_ineffect(privilege).
444da6c28aaSamw * tests the existence of a privilege against the effective set.
4457c478bd9Sstevel@tonic-gate */
4467c478bd9Sstevel@tonic-gate boolean_t
priv_ineffect(const char * priv)4477c478bd9Sstevel@tonic-gate priv_ineffect(const char *priv)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate priv_set_t *curset;
4507c478bd9Sstevel@tonic-gate boolean_t res;
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate curset = priv_allocset();
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate if (curset == NULL)
4557c478bd9Sstevel@tonic-gate return (B_FALSE);
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate if (getppriv(effective, curset) != 0 ||
4587c478bd9Sstevel@tonic-gate !priv_ismember(curset, priv))
4597c478bd9Sstevel@tonic-gate res = B_FALSE;
4607c478bd9Sstevel@tonic-gate else
4617c478bd9Sstevel@tonic-gate res = B_TRUE;
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate priv_freeset(curset);
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate return (res);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate * The routine __init_daemon_priv() is private to Solaris and is
4707c478bd9Sstevel@tonic-gate * used by daemons to limit the privileges they can use and
4717c478bd9Sstevel@tonic-gate * to set the uid they run under.
4727c478bd9Sstevel@tonic-gate */
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate static const char root_cp[] = "/core.%f.%t";
4757c478bd9Sstevel@tonic-gate static const char daemon_cp[] = "/var/tmp/core.%f.%t";
4767c478bd9Sstevel@tonic-gate
4777c478bd9Sstevel@tonic-gate int
__init_daemon_priv(int flags,uid_t uid,gid_t gid,...)4787c478bd9Sstevel@tonic-gate __init_daemon_priv(int flags, uid_t uid, gid_t gid, ...)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate priv_set_t *nset;
4817c478bd9Sstevel@tonic-gate priv_set_t *perm = NULL;
4827c478bd9Sstevel@tonic-gate va_list pa;
4837c478bd9Sstevel@tonic-gate priv_data_t *d;
4847c478bd9Sstevel@tonic-gate int ret = -1;
4857c478bd9Sstevel@tonic-gate char buf[1024];
4867c478bd9Sstevel@tonic-gate
4877c478bd9Sstevel@tonic-gate LOADPRIVDATA(d);
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate va_start(pa, gid);
4907c478bd9Sstevel@tonic-gate
4917c478bd9Sstevel@tonic-gate nset = priv_vlist(pa);
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate va_end(pa);
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate if (nset == NULL)
4967c478bd9Sstevel@tonic-gate return (-1);
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate /* Always add the basic set */
4997c478bd9Sstevel@tonic-gate if (d->pd_basicset != NULL)
5007c478bd9Sstevel@tonic-gate priv_union(d->pd_basicset, nset);
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate * This is not a significant failure: it allows us to start programs
5047c478bd9Sstevel@tonic-gate * with sufficient privileges and with the proper uid. We don't
5057c478bd9Sstevel@tonic-gate * care enough about the extra groups in that case.
5067c478bd9Sstevel@tonic-gate */
5077c478bd9Sstevel@tonic-gate if (flags & PU_RESETGROUPS)
5087c478bd9Sstevel@tonic-gate (void) setgroups(0, NULL);
5097c478bd9Sstevel@tonic-gate
510f48205beScasper if (gid != (gid_t)-1 && setgid(gid) != 0)
5117c478bd9Sstevel@tonic-gate goto end;
5127c478bd9Sstevel@tonic-gate
5137c478bd9Sstevel@tonic-gate perm = priv_allocset();
5147c478bd9Sstevel@tonic-gate if (perm == NULL)
5157c478bd9Sstevel@tonic-gate goto end;
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate /* E = P */
5187c478bd9Sstevel@tonic-gate (void) getppriv(permitted, perm);
5197c478bd9Sstevel@tonic-gate (void) setppriv(PRIV_SET, effective, perm);
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate /* Now reset suid and euid */
522f48205beScasper if (uid != (uid_t)-1 && setreuid(uid, uid) != 0)
5237c478bd9Sstevel@tonic-gate goto end;
5247c478bd9Sstevel@tonic-gate
5257c478bd9Sstevel@tonic-gate /* Check for the limit privs */
5267c478bd9Sstevel@tonic-gate if ((flags & PU_LIMITPRIVS) &&
5277c478bd9Sstevel@tonic-gate setppriv(PRIV_SET, limit, nset) != 0)
5287c478bd9Sstevel@tonic-gate goto end;
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate if (flags & PU_CLEARLIMITSET) {
5317c478bd9Sstevel@tonic-gate priv_emptyset(perm);
5327c478bd9Sstevel@tonic-gate if (setppriv(PRIV_SET, limit, perm) != 0)
5337c478bd9Sstevel@tonic-gate goto end;
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate /* Remove the privileges from all the other sets */
5377c478bd9Sstevel@tonic-gate if (setppriv(PRIV_SET, permitted, nset) != 0)
5387c478bd9Sstevel@tonic-gate goto end;
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate if (!(flags & PU_INHERITPRIVS))
5417c478bd9Sstevel@tonic-gate priv_emptyset(nset);
5427c478bd9Sstevel@tonic-gate
5437c478bd9Sstevel@tonic-gate ret = setppriv(PRIV_SET, inheritable, nset);
5447c478bd9Sstevel@tonic-gate end:
5457c478bd9Sstevel@tonic-gate priv_freeset(nset);
5467c478bd9Sstevel@tonic-gate priv_freeset(perm);
5477c478bd9Sstevel@tonic-gate
5487c478bd9Sstevel@tonic-gate if (core_get_process_path(buf, sizeof (buf), getpid()) == 0 &&
5497c478bd9Sstevel@tonic-gate strcmp(buf, "core") == 0) {
5507c478bd9Sstevel@tonic-gate
551f48205beScasper if ((uid == (uid_t)-1 ? geteuid() : uid) == 0) {
5527c478bd9Sstevel@tonic-gate (void) core_set_process_path(root_cp, sizeof (root_cp),
5537c478bd9Sstevel@tonic-gate getpid());
5547c478bd9Sstevel@tonic-gate } else {
5557c478bd9Sstevel@tonic-gate (void) core_set_process_path(daemon_cp,
5567c478bd9Sstevel@tonic-gate sizeof (daemon_cp), getpid());
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate (void) setpflags(__PROC_PROTECT, 0);
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate return (ret);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate * The routine __fini_daemon_priv() is private to Solaris and is
5667c478bd9Sstevel@tonic-gate * used by daemons to clear remaining unwanted privileges and
5677c478bd9Sstevel@tonic-gate * reenable core dumps.
5687c478bd9Sstevel@tonic-gate */
5697c478bd9Sstevel@tonic-gate void
__fini_daemon_priv(const char * priv,...)5707c478bd9Sstevel@tonic-gate __fini_daemon_priv(const char *priv, ...)
5717c478bd9Sstevel@tonic-gate {
5727c478bd9Sstevel@tonic-gate priv_set_t *nset;
5737c478bd9Sstevel@tonic-gate va_list pa;
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate if (priv != NULL) {
576*75598e10SGary Mills
577*75598e10SGary Mills va_start(pa, priv);
5787c478bd9Sstevel@tonic-gate nset = priv_vlist(pa);
579*75598e10SGary Mills va_end(pa);
580*75598e10SGary Mills
5817c478bd9Sstevel@tonic-gate if (nset == NULL)
5827c478bd9Sstevel@tonic-gate return;
5837c478bd9Sstevel@tonic-gate
5847c478bd9Sstevel@tonic-gate (void) priv_addset(nset, priv);
5857c478bd9Sstevel@tonic-gate (void) setppriv(PRIV_OFF, permitted, nset);
5867c478bd9Sstevel@tonic-gate priv_freeset(nset);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate
5897c478bd9Sstevel@tonic-gate (void) setpflags(__PROC_PROTECT, 0);
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate /*
5937c478bd9Sstevel@tonic-gate * The routine __init_suid_priv() is private to Solaris and is
5947c478bd9Sstevel@tonic-gate * used by set-uid root programs to limit the privileges acquired
5957c478bd9Sstevel@tonic-gate * to those actually needed.
5967c478bd9Sstevel@tonic-gate */
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate static priv_set_t *bracketpriv;
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate int
__init_suid_priv(int flags,...)6017c478bd9Sstevel@tonic-gate __init_suid_priv(int flags, ...)
6027c478bd9Sstevel@tonic-gate {
6037c478bd9Sstevel@tonic-gate priv_set_t *nset = NULL;
6047c478bd9Sstevel@tonic-gate priv_set_t *tmpset = NULL;
6057c478bd9Sstevel@tonic-gate va_list pa;
6067c478bd9Sstevel@tonic-gate int r = -1;
6077c478bd9Sstevel@tonic-gate uid_t ruid, euid;
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate euid = geteuid();
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate /* If we're not set-uid root, don't reset the uid */
6127c478bd9Sstevel@tonic-gate if (euid == 0) {
6137c478bd9Sstevel@tonic-gate ruid = getuid();
6147c478bd9Sstevel@tonic-gate /* If we're running as root, keep everything */
6157c478bd9Sstevel@tonic-gate if (ruid == 0)
6167c478bd9Sstevel@tonic-gate return (0);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate
6197c478bd9Sstevel@tonic-gate /* Can call this only once */
6207c478bd9Sstevel@tonic-gate if (bracketpriv != NULL)
6217c478bd9Sstevel@tonic-gate return (-1);
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate va_start(pa, flags);
6247c478bd9Sstevel@tonic-gate
6257c478bd9Sstevel@tonic-gate nset = priv_vlist(pa);
6267c478bd9Sstevel@tonic-gate
6277c478bd9Sstevel@tonic-gate va_end(pa);
6287c478bd9Sstevel@tonic-gate
6297c478bd9Sstevel@tonic-gate if (nset == NULL)
6307c478bd9Sstevel@tonic-gate goto end;
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate tmpset = priv_allocset();
6337c478bd9Sstevel@tonic-gate
6347c478bd9Sstevel@tonic-gate if (tmpset == NULL)
6357c478bd9Sstevel@tonic-gate goto end;
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate /* We cannot grow our privileges beyond P, so start there */
6387c478bd9Sstevel@tonic-gate (void) getppriv(permitted, tmpset);
6397c478bd9Sstevel@tonic-gate
6407c478bd9Sstevel@tonic-gate /* Is the privilege we need even in P? */
6417c478bd9Sstevel@tonic-gate if (!priv_issubset(nset, tmpset))
6427c478bd9Sstevel@tonic-gate goto end;
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate bracketpriv = priv_allocset();
6457c478bd9Sstevel@tonic-gate if (bracketpriv == NULL)
6467c478bd9Sstevel@tonic-gate goto end;
6477c478bd9Sstevel@tonic-gate
6487c478bd9Sstevel@tonic-gate priv_copyset(nset, bracketpriv);
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate /* Always add the basic set */
6517c478bd9Sstevel@tonic-gate priv_union(priv_basic(), nset);
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate /* But don't add what we don't have */
6547c478bd9Sstevel@tonic-gate priv_intersect(tmpset, nset);
6557c478bd9Sstevel@tonic-gate
6567c478bd9Sstevel@tonic-gate (void) getppriv(inheritable, tmpset);
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate /* And stir in the inheritable privileges */
6597c478bd9Sstevel@tonic-gate priv_union(tmpset, nset);
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate if ((r = setppriv(PRIV_SET, effective, tmpset)) != 0)
6627c478bd9Sstevel@tonic-gate goto end;
6637c478bd9Sstevel@tonic-gate
6647c478bd9Sstevel@tonic-gate if ((r = setppriv(PRIV_SET, permitted, nset)) != 0)
6657c478bd9Sstevel@tonic-gate goto end;
6667c478bd9Sstevel@tonic-gate
6677c478bd9Sstevel@tonic-gate if (flags & PU_CLEARLIMITSET)
6687c478bd9Sstevel@tonic-gate priv_emptyset(nset);
6697c478bd9Sstevel@tonic-gate
6707c478bd9Sstevel@tonic-gate if ((flags & (PU_LIMITPRIVS|PU_CLEARLIMITSET)) != 0 &&
6717c478bd9Sstevel@tonic-gate (r = setppriv(PRIV_SET, limit, nset)) != 0)
6727c478bd9Sstevel@tonic-gate goto end;
6737c478bd9Sstevel@tonic-gate
6747c478bd9Sstevel@tonic-gate if (euid == 0)
6757c478bd9Sstevel@tonic-gate r = setreuid(ruid, ruid);
6767c478bd9Sstevel@tonic-gate
6777c478bd9Sstevel@tonic-gate end:
6787c478bd9Sstevel@tonic-gate priv_freeset(tmpset);
6797c478bd9Sstevel@tonic-gate priv_freeset(nset);
6807c478bd9Sstevel@tonic-gate if (r != 0) {
6817c478bd9Sstevel@tonic-gate /* Fail without leaving uid 0 around */
6827c478bd9Sstevel@tonic-gate if (euid == 0)
6837c478bd9Sstevel@tonic-gate (void) setreuid(ruid, ruid);
6847c478bd9Sstevel@tonic-gate priv_freeset(bracketpriv);
6857c478bd9Sstevel@tonic-gate bracketpriv = NULL;
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate
6887c478bd9Sstevel@tonic-gate return (r);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate
6917c478bd9Sstevel@tonic-gate /*
6927c478bd9Sstevel@tonic-gate * Toggle privileges on/off in the effective set.
6937c478bd9Sstevel@tonic-gate */
6947c478bd9Sstevel@tonic-gate int
__priv_bracket(priv_op_t op)6957c478bd9Sstevel@tonic-gate __priv_bracket(priv_op_t op)
6967c478bd9Sstevel@tonic-gate {
6977c478bd9Sstevel@tonic-gate /* We're running fully privileged or didn't check errors first time */
6987c478bd9Sstevel@tonic-gate if (bracketpriv == NULL)
6997c478bd9Sstevel@tonic-gate return (0);
7007c478bd9Sstevel@tonic-gate
7017c478bd9Sstevel@tonic-gate /* Only PRIV_ON and PRIV_OFF are valid */
7027c478bd9Sstevel@tonic-gate if (op == PRIV_SET)
7037c478bd9Sstevel@tonic-gate return (-1);
7047c478bd9Sstevel@tonic-gate
7057c478bd9Sstevel@tonic-gate return (setppriv(op, effective, bracketpriv));
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate
7087c478bd9Sstevel@tonic-gate /*
7097c478bd9Sstevel@tonic-gate * Remove privileges from E & P.
7107c478bd9Sstevel@tonic-gate */
7117c478bd9Sstevel@tonic-gate void
__priv_relinquish(void)7127c478bd9Sstevel@tonic-gate __priv_relinquish(void)
7137c478bd9Sstevel@tonic-gate {
7147c478bd9Sstevel@tonic-gate if (bracketpriv != NULL) {
7157c478bd9Sstevel@tonic-gate (void) setppriv(PRIV_OFF, permitted, bracketpriv);
7167c478bd9Sstevel@tonic-gate priv_freeset(bracketpriv);
7177c478bd9Sstevel@tonic-gate bracketpriv = NULL;
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate /*
7227c478bd9Sstevel@tonic-gate * Use binary search on the ordered list.
7237c478bd9Sstevel@tonic-gate */
7247c478bd9Sstevel@tonic-gate int
__priv_getbyname(const priv_data_t * d,const char * name)7257c478bd9Sstevel@tonic-gate __priv_getbyname(const priv_data_t *d, const char *name)
7267c478bd9Sstevel@tonic-gate {
727e3895e32Scasper char *const *list;
728e3895e32Scasper const int *order;
7297c478bd9Sstevel@tonic-gate int lo = 0;
730e3895e32Scasper int hi;
731e3895e32Scasper
732e3895e32Scasper if (d == NULL)
733e3895e32Scasper return (-1);
734e3895e32Scasper
735e3895e32Scasper list = d->pd_privnames;
736e3895e32Scasper order = d->pd_setsort;
737e3895e32Scasper hi = d->pd_nprivs - 1;
7387c478bd9Sstevel@tonic-gate
7397c478bd9Sstevel@tonic-gate if (strncasecmp(name, "priv_", 5) == 0)
7407c478bd9Sstevel@tonic-gate name += 5;
7417c478bd9Sstevel@tonic-gate
7427c478bd9Sstevel@tonic-gate do {
7437c478bd9Sstevel@tonic-gate int mid = (lo + hi) / 2;
7447c478bd9Sstevel@tonic-gate int res = strcasecmp(name, list[order[mid]]);
7457c478bd9Sstevel@tonic-gate
7467c478bd9Sstevel@tonic-gate if (res == 0)
7477c478bd9Sstevel@tonic-gate return (order[mid]);
7487c478bd9Sstevel@tonic-gate else if (res < 0)
7497c478bd9Sstevel@tonic-gate hi = mid - 1;
7507c478bd9Sstevel@tonic-gate else
7517c478bd9Sstevel@tonic-gate lo = mid + 1;
7527c478bd9Sstevel@tonic-gate } while (lo <= hi);
7537c478bd9Sstevel@tonic-gate
7547c478bd9Sstevel@tonic-gate errno = EINVAL;
7557c478bd9Sstevel@tonic-gate return (-1);
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate
7587c478bd9Sstevel@tonic-gate int
priv_getbyname(const char * name)7597c478bd9Sstevel@tonic-gate priv_getbyname(const char *name)
7607c478bd9Sstevel@tonic-gate {
761ad8ef92aSMilan Jurik WITHPRIVLOCKED(int, -1, __priv_getbyname(GETPRIVDATA(), name))
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate
7647c478bd9Sstevel@tonic-gate int
__priv_getsetbyname(const priv_data_t * d,const char * name)7657c478bd9Sstevel@tonic-gate __priv_getsetbyname(const priv_data_t *d, const char *name)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate int i;
7687c478bd9Sstevel@tonic-gate int n = d->pd_nsets;
7697c478bd9Sstevel@tonic-gate char *const *list = d->pd_setnames;
7707c478bd9Sstevel@tonic-gate
7717c478bd9Sstevel@tonic-gate if (strncasecmp(name, "priv_", 5) == 0)
7727c478bd9Sstevel@tonic-gate name += 5;
7737c478bd9Sstevel@tonic-gate
7747c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) {
7757c478bd9Sstevel@tonic-gate if (strcasecmp(list[i], name) == 0)
7767c478bd9Sstevel@tonic-gate return (i);
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate
7797c478bd9Sstevel@tonic-gate errno = EINVAL;
7807c478bd9Sstevel@tonic-gate return (-1);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate
7837c478bd9Sstevel@tonic-gate int
priv_getsetbyname(const char * name)7847c478bd9Sstevel@tonic-gate priv_getsetbyname(const char *name)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate /* Not locked: sets don't change */
7877c478bd9Sstevel@tonic-gate return (__priv_getsetbyname(GETPRIVDATA(), name));
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate
7907c478bd9Sstevel@tonic-gate static const char *
priv_bynum(int i,int n,char ** list)7917c478bd9Sstevel@tonic-gate priv_bynum(int i, int n, char **list)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate if (i < 0 || i >= n)
7947c478bd9Sstevel@tonic-gate return (NULL);
7957c478bd9Sstevel@tonic-gate
7967c478bd9Sstevel@tonic-gate return (list[i]);
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate
7997c478bd9Sstevel@tonic-gate const char *
__priv_getbynum(const priv_data_t * d,int num)8007c478bd9Sstevel@tonic-gate __priv_getbynum(const priv_data_t *d, int num)
8017c478bd9Sstevel@tonic-gate {
802e3895e32Scasper if (d == NULL)
803e3895e32Scasper return (NULL);
8047c478bd9Sstevel@tonic-gate return (priv_bynum(num, d->pd_nprivs, d->pd_privnames));
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate const char *
priv_getbynum(int num)8087c478bd9Sstevel@tonic-gate priv_getbynum(int num)
8097c478bd9Sstevel@tonic-gate {
810ad8ef92aSMilan Jurik WITHPRIVLOCKED(const char *, NULL, __priv_getbynum(GETPRIVDATA(), num))
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate
8137c478bd9Sstevel@tonic-gate const char *
__priv_getsetbynum(const priv_data_t * d,int num)8147c478bd9Sstevel@tonic-gate __priv_getsetbynum(const priv_data_t *d, int num)
8157c478bd9Sstevel@tonic-gate {
816e3895e32Scasper if (d == NULL)
817e3895e32Scasper return (NULL);
8187c478bd9Sstevel@tonic-gate return (priv_bynum(num, d->pd_nsets, d->pd_setnames));
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate
8217c478bd9Sstevel@tonic-gate const char *
priv_getsetbynum(int num)8227c478bd9Sstevel@tonic-gate priv_getsetbynum(int num)
8237c478bd9Sstevel@tonic-gate {
8247c478bd9Sstevel@tonic-gate return (__priv_getsetbynum(GETPRIVDATA(), num));
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate
8277c478bd9Sstevel@tonic-gate
8287c478bd9Sstevel@tonic-gate /*
8297c478bd9Sstevel@tonic-gate * Privilege manipulation functions
8307c478bd9Sstevel@tonic-gate *
8317c478bd9Sstevel@tonic-gate * Without knowing the details of the privilege set implementation,
8327c478bd9Sstevel@tonic-gate * opaque pointers can be used to manipulate sets at will.
8337c478bd9Sstevel@tonic-gate */
8347c478bd9Sstevel@tonic-gate
8357c478bd9Sstevel@tonic-gate static priv_set_t *
__priv_allocset(priv_data_t * d)8367c478bd9Sstevel@tonic-gate __priv_allocset(priv_data_t *d)
8377c478bd9Sstevel@tonic-gate {
838e3895e32Scasper if (d == NULL)
839e3895e32Scasper return (NULL);
840e3895e32Scasper
8417c478bd9Sstevel@tonic-gate return (libc_malloc(d->pd_setsize));
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate
8447c478bd9Sstevel@tonic-gate priv_set_t *
priv_allocset(void)8457c478bd9Sstevel@tonic-gate priv_allocset(void)
8467c478bd9Sstevel@tonic-gate {
8477c478bd9Sstevel@tonic-gate return (__priv_allocset(GETPRIVDATA()));
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate
8507c478bd9Sstevel@tonic-gate void
priv_freeset(priv_set_t * p)8517c478bd9Sstevel@tonic-gate priv_freeset(priv_set_t *p)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate int er = errno;
8547c478bd9Sstevel@tonic-gate
8557c478bd9Sstevel@tonic-gate libc_free(p);
8567c478bd9Sstevel@tonic-gate errno = er;
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate
8597c478bd9Sstevel@tonic-gate void
__priv_emptyset(priv_data_t * d,priv_set_t * set)8607c478bd9Sstevel@tonic-gate __priv_emptyset(priv_data_t *d, priv_set_t *set)
8617c478bd9Sstevel@tonic-gate {
8627c478bd9Sstevel@tonic-gate (void) memset(set, 0, d->pd_setsize);
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate
8657c478bd9Sstevel@tonic-gate void
priv_emptyset(priv_set_t * set)8667c478bd9Sstevel@tonic-gate priv_emptyset(priv_set_t *set)
8677c478bd9Sstevel@tonic-gate {
8687c478bd9Sstevel@tonic-gate __priv_emptyset(GETPRIVDATA(), set);
8697c478bd9Sstevel@tonic-gate }
8707c478bd9Sstevel@tonic-gate
8717c478bd9Sstevel@tonic-gate void
priv_basicset(priv_set_t * set)872634e26ecSCasper H.S. Dik priv_basicset(priv_set_t *set)
873634e26ecSCasper H.S. Dik {
874134a1f4eSCasper H.S. Dik priv_copyset(priv_basic(), set);
875634e26ecSCasper H.S. Dik }
876634e26ecSCasper H.S. Dik
877634e26ecSCasper H.S. Dik void
__priv_fillset(priv_data_t * d,priv_set_t * set)8787c478bd9Sstevel@tonic-gate __priv_fillset(priv_data_t *d, priv_set_t *set)
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate (void) memset(set, ~0, d->pd_setsize);
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate
8837c478bd9Sstevel@tonic-gate void
priv_fillset(priv_set_t * set)8847c478bd9Sstevel@tonic-gate priv_fillset(priv_set_t *set)
8857c478bd9Sstevel@tonic-gate {
8867c478bd9Sstevel@tonic-gate __priv_fillset(GETPRIVDATA(), set);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate
8897c478bd9Sstevel@tonic-gate
8907c478bd9Sstevel@tonic-gate #define PRIV_TEST_BODY_D(d, test) \
8917c478bd9Sstevel@tonic-gate int i; \
8927c478bd9Sstevel@tonic-gate \
8937c478bd9Sstevel@tonic-gate for (i = d->pd_pinfo->priv_setsize; i-- > 0; ) \
8947c478bd9Sstevel@tonic-gate if (!(test)) \
8957c478bd9Sstevel@tonic-gate return (B_FALSE); \
8967c478bd9Sstevel@tonic-gate \
8977c478bd9Sstevel@tonic-gate return (B_TRUE)
8987c478bd9Sstevel@tonic-gate
8997c478bd9Sstevel@tonic-gate boolean_t
priv_isequalset(const priv_set_t * a,const priv_set_t * b)9007c478bd9Sstevel@tonic-gate priv_isequalset(const priv_set_t *a, const priv_set_t *b)
9017c478bd9Sstevel@tonic-gate {
9027c478bd9Sstevel@tonic-gate priv_data_t *d;
9037c478bd9Sstevel@tonic-gate
9047c478bd9Sstevel@tonic-gate LOADPRIVDATA(d);
9057c478bd9Sstevel@tonic-gate
9067c478bd9Sstevel@tonic-gate return ((boolean_t)(memcmp(a, b, d->pd_setsize) == 0));
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate
9097c478bd9Sstevel@tonic-gate boolean_t
__priv_isemptyset(priv_data_t * d,const priv_set_t * set)9107c478bd9Sstevel@tonic-gate __priv_isemptyset(priv_data_t *d, const priv_set_t *set)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate PRIV_TEST_BODY_D(d, ((priv_chunk_t *)set)[i] == 0);
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate
9157c478bd9Sstevel@tonic-gate boolean_t
priv_isemptyset(const priv_set_t * set)9167c478bd9Sstevel@tonic-gate priv_isemptyset(const priv_set_t *set)
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate return (__priv_isemptyset(GETPRIVDATA(), set));
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate
9217c478bd9Sstevel@tonic-gate boolean_t
__priv_isfullset(priv_data_t * d,const priv_set_t * set)9227c478bd9Sstevel@tonic-gate __priv_isfullset(priv_data_t *d, const priv_set_t *set)
9237c478bd9Sstevel@tonic-gate {
9247c478bd9Sstevel@tonic-gate PRIV_TEST_BODY_D(d, ((priv_chunk_t *)set)[i] == ~(priv_chunk_t)0);
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate boolean_t
priv_isfullset(const priv_set_t * set)9287c478bd9Sstevel@tonic-gate priv_isfullset(const priv_set_t *set)
9297c478bd9Sstevel@tonic-gate {
9307c478bd9Sstevel@tonic-gate return (__priv_isfullset(GETPRIVDATA(), set));
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate
9337c478bd9Sstevel@tonic-gate /*
9347c478bd9Sstevel@tonic-gate * Return true if a is a subset of b
9357c478bd9Sstevel@tonic-gate */
9367c478bd9Sstevel@tonic-gate boolean_t
__priv_issubset(priv_data_t * d,const priv_set_t * a,const priv_set_t * b)9377c478bd9Sstevel@tonic-gate __priv_issubset(priv_data_t *d, const priv_set_t *a, const priv_set_t *b)
9387c478bd9Sstevel@tonic-gate {
9397c478bd9Sstevel@tonic-gate PRIV_TEST_BODY_D(d, (((priv_chunk_t *)a)[i] | ((priv_chunk_t *)b)[i]) ==
9407c478bd9Sstevel@tonic-gate ((priv_chunk_t *)b)[i]);
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate
9437c478bd9Sstevel@tonic-gate boolean_t
priv_issubset(const priv_set_t * a,const priv_set_t * b)9447c478bd9Sstevel@tonic-gate priv_issubset(const priv_set_t *a, const priv_set_t *b)
9457c478bd9Sstevel@tonic-gate {
9467c478bd9Sstevel@tonic-gate return (__priv_issubset(GETPRIVDATA(), a, b));
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate
9497c478bd9Sstevel@tonic-gate #define PRIV_CHANGE_BODY(a, op, b) \
9507c478bd9Sstevel@tonic-gate int i; \
9517c478bd9Sstevel@tonic-gate priv_data_t *d; \
9527c478bd9Sstevel@tonic-gate \
9537c478bd9Sstevel@tonic-gate LOADPRIVDATA(d); \
9547c478bd9Sstevel@tonic-gate \
9557c478bd9Sstevel@tonic-gate for (i = 0; i < d->pd_pinfo->priv_setsize; i++) \
9567c478bd9Sstevel@tonic-gate ((priv_chunk_t *)a)[i] op \
9577c478bd9Sstevel@tonic-gate ((priv_chunk_t *)b)[i]
9587c478bd9Sstevel@tonic-gate
9597c478bd9Sstevel@tonic-gate /* B = A ^ B */
9607c478bd9Sstevel@tonic-gate void
priv_intersect(const priv_set_t * a,priv_set_t * b)9617c478bd9Sstevel@tonic-gate priv_intersect(const priv_set_t *a, priv_set_t *b)
9627c478bd9Sstevel@tonic-gate {
9637c478bd9Sstevel@tonic-gate /* CSTYLED */
9647c478bd9Sstevel@tonic-gate PRIV_CHANGE_BODY(b, &=, a);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate
9677c478bd9Sstevel@tonic-gate /* B = A */
9687c478bd9Sstevel@tonic-gate void
priv_copyset(const priv_set_t * a,priv_set_t * b)9697c478bd9Sstevel@tonic-gate priv_copyset(const priv_set_t *a, priv_set_t *b)
9707c478bd9Sstevel@tonic-gate {
9717c478bd9Sstevel@tonic-gate /* CSTYLED */
9727c478bd9Sstevel@tonic-gate PRIV_CHANGE_BODY(b, =, a);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate
9757c478bd9Sstevel@tonic-gate /* B = A v B */
9767c478bd9Sstevel@tonic-gate void
priv_union(const priv_set_t * a,priv_set_t * b)9777c478bd9Sstevel@tonic-gate priv_union(const priv_set_t *a, priv_set_t *b)
9787c478bd9Sstevel@tonic-gate {
9797c478bd9Sstevel@tonic-gate /* CSTYLED */
9807c478bd9Sstevel@tonic-gate PRIV_CHANGE_BODY(b, |=, a);
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate
9837c478bd9Sstevel@tonic-gate /* A = ! A */
9847c478bd9Sstevel@tonic-gate void
priv_inverse(priv_set_t * a)9857c478bd9Sstevel@tonic-gate priv_inverse(priv_set_t *a)
9867c478bd9Sstevel@tonic-gate {
9877c478bd9Sstevel@tonic-gate PRIV_CHANGE_BODY(a, = ~, a);
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate
9907c478bd9Sstevel@tonic-gate /*
9917c478bd9Sstevel@tonic-gate * Manipulating single privileges.
9927c478bd9Sstevel@tonic-gate */
9937c478bd9Sstevel@tonic-gate
9947c478bd9Sstevel@tonic-gate int
priv_addset(priv_set_t * a,const char * p)9957c478bd9Sstevel@tonic-gate priv_addset(priv_set_t *a, const char *p)
9967c478bd9Sstevel@tonic-gate {
9977c478bd9Sstevel@tonic-gate int priv = priv_getbyname(p);
9987c478bd9Sstevel@tonic-gate
9997c478bd9Sstevel@tonic-gate if (priv < 0)
10007c478bd9Sstevel@tonic-gate return (-1);
10017c478bd9Sstevel@tonic-gate
10027c478bd9Sstevel@tonic-gate PRIV_ADDSET(a, priv);
10037c478bd9Sstevel@tonic-gate
10047c478bd9Sstevel@tonic-gate return (0);
10057c478bd9Sstevel@tonic-gate }
10067c478bd9Sstevel@tonic-gate
10077c478bd9Sstevel@tonic-gate int
priv_delset(priv_set_t * a,const char * p)10087c478bd9Sstevel@tonic-gate priv_delset(priv_set_t *a, const char *p)
10097c478bd9Sstevel@tonic-gate {
10107c478bd9Sstevel@tonic-gate int priv = priv_getbyname(p);
10117c478bd9Sstevel@tonic-gate
10127c478bd9Sstevel@tonic-gate if (priv < 0)
10137c478bd9Sstevel@tonic-gate return (-1);
10147c478bd9Sstevel@tonic-gate
10157c478bd9Sstevel@tonic-gate PRIV_DELSET(a, priv);
10167c478bd9Sstevel@tonic-gate return (0);
10177c478bd9Sstevel@tonic-gate }
10187c478bd9Sstevel@tonic-gate
10197c478bd9Sstevel@tonic-gate boolean_t
priv_ismember(const priv_set_t * a,const char * p)10207c478bd9Sstevel@tonic-gate priv_ismember(const priv_set_t *a, const char *p)
10217c478bd9Sstevel@tonic-gate {
10227c478bd9Sstevel@tonic-gate int priv = priv_getbyname(p);
10237c478bd9Sstevel@tonic-gate
10247c478bd9Sstevel@tonic-gate if (priv < 0)
10257c478bd9Sstevel@tonic-gate return (B_FALSE);
10267c478bd9Sstevel@tonic-gate
10277c478bd9Sstevel@tonic-gate return ((boolean_t)PRIV_ISMEMBER(a, priv));
10287c478bd9Sstevel@tonic-gate }
1029