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
55753Sgww * Common Development and Distribution License (the "License").
65753Sgww * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*11861SMarek.Pospisil@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * Device policy implementation.
280Sstevel@tonic-gate *
290Sstevel@tonic-gate * Maintains the device policy table and defines the lookup functions.
300Sstevel@tonic-gate *
310Sstevel@tonic-gate * The table contains one entry for each major device number; each
320Sstevel@tonic-gate * major bucket has a list of minor number specific entries. First
330Sstevel@tonic-gate * match gets it. Not even simple minor names are expanded as that
340Sstevel@tonic-gate * would cause the device to be loaded. Non-wildcard entries are expanded
350Sstevel@tonic-gate * on first match. Wildcard entries are matched each open but the actual
360Sstevel@tonic-gate * policy is cached with the common snode, so the matching code will
370Sstevel@tonic-gate * probably be called infrequently. The trivial wildcard ``*'' does
380Sstevel@tonic-gate * not cause expensive string expansions and matches.
390Sstevel@tonic-gate *
400Sstevel@tonic-gate * When the policy is updated, the the generation count is increased;
410Sstevel@tonic-gate * whenever a cached policy is used, the generation count is compared;
420Sstevel@tonic-gate * if there's no match, the device policy is refreshed.
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * The special policy "nullpolicy" is used to mean "no checking beyond DAC
450Sstevel@tonic-gate * needed". It too will change when the policy is rev'ed to make sure
460Sstevel@tonic-gate * that devices with nullpolicy are also refreshed.
470Sstevel@tonic-gate *
480Sstevel@tonic-gate * The special policy "dfltpolicy" is used for those devices with no
490Sstevel@tonic-gate * matching policy. On boot, it is "all privileges required".
500Sstevel@tonic-gate * This restriction on boot functions as a fail-safe; if no device policy
510Sstevel@tonic-gate * is loaded a "no restriction policy" would lead to security problems that
520Sstevel@tonic-gate * are not immediately noticable.
530Sstevel@tonic-gate */
540Sstevel@tonic-gate
550Sstevel@tonic-gate #include <sys/priv_impl.h>
560Sstevel@tonic-gate #include <sys/policy.h>
570Sstevel@tonic-gate #include <sys/atomic.h>
580Sstevel@tonic-gate #include <sys/autoconf.h>
590Sstevel@tonic-gate #include <sys/sysmacros.h>
600Sstevel@tonic-gate #include <sys/systm.h>
610Sstevel@tonic-gate #include <sys/vnode.h>
620Sstevel@tonic-gate #include <sys/devpolicy.h>
630Sstevel@tonic-gate #include <sys/priv.h>
640Sstevel@tonic-gate #include <sys/kmem.h>
650Sstevel@tonic-gate #include <sys/ksynch.h>
660Sstevel@tonic-gate #include <sys/errno.h>
670Sstevel@tonic-gate #include <sys/sunddi.h>
680Sstevel@tonic-gate #include <c2/audit.h>
690Sstevel@tonic-gate #include <sys/fs/dv_node.h>
700Sstevel@tonic-gate
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate * Internal data structures definitions.
730Sstevel@tonic-gate */
740Sstevel@tonic-gate
750Sstevel@tonic-gate typedef struct devplcyent devplcyent_t;
760Sstevel@tonic-gate
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate * The device policy entry; if there is an expression string, the
790Sstevel@tonic-gate * minor numbers are not relevant. This is indicated by dpe_len > 0.
800Sstevel@tonic-gate */
810Sstevel@tonic-gate struct devplcyent {
820Sstevel@tonic-gate devplcyent_t *dpe_next; /* next entry in this list */
830Sstevel@tonic-gate devplcy_t *dpe_plcy; /* policy for this entry */
840Sstevel@tonic-gate char *dpe_expr; /* expression matching minor mode */
850Sstevel@tonic-gate int dpe_len; /* size of allocated mem for expr */
860Sstevel@tonic-gate uint32_t dpe_flags; /* flags */
870Sstevel@tonic-gate minor_t dpe_lomin; /* expanded: low minor number */
880Sstevel@tonic-gate minor_t dpe_himin; /* expanded: high minor number */
890Sstevel@tonic-gate vtype_t dpe_spec; /* expanded: VBLK or VCHR */
900Sstevel@tonic-gate };
910Sstevel@tonic-gate
920Sstevel@tonic-gate #define DPE_WILDC 0x01 /* Expression has wildcard */
930Sstevel@tonic-gate #define DPE_ALLMINOR 0x02 /* Matches all minor numbers */
940Sstevel@tonic-gate #define DPE_EXPANDED 0x04 /* Minor numbers expanded */
950Sstevel@tonic-gate
960Sstevel@tonic-gate typedef struct tableent {
970Sstevel@tonic-gate devplcyent_t *t_ent; /* list of policies by minor */
980Sstevel@tonic-gate major_t t_major; /* device major number */
990Sstevel@tonic-gate } tableent_t;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate * The data store.
1030Sstevel@tonic-gate */
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate static int ntabent; /* # of major numbers */
1060Sstevel@tonic-gate static int totitems; /* Number of entries in all buckets + dflt */
1070Sstevel@tonic-gate static tableent_t *devpolicy; /* The device policy itself */
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate static krwlock_t policyrw; /* protects the table */
1100Sstevel@tonic-gate static kmutex_t policymutex; /* allows only one concurrent devpolicy_load */
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate devplcy_t *nullpolicy; /* public because it's used for shortcuts */
1130Sstevel@tonic-gate static devplcy_t *dfltpolicy;
1140Sstevel@tonic-gate static devplcy_t *netpolicy;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate * Device policy generation count; only device policies matching the
1180Sstevel@tonic-gate * generation count are still valid.
1190Sstevel@tonic-gate */
1200Sstevel@tonic-gate volatile uint32_t devplcy_gen;
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate * Tunable: maximum number of device policy entries to load in
1240Sstevel@tonic-gate * a system call. (Protects KM_SLEEP call)
1250Sstevel@tonic-gate */
1260Sstevel@tonic-gate int maxdevpolicy = MAXDEVPOLICY;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate * Initialize the device policy code
1300Sstevel@tonic-gate */
1310Sstevel@tonic-gate void
devpolicy_init(void)1320Sstevel@tonic-gate devpolicy_init(void)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate rw_init(&policyrw, NULL, RW_DRIVER, NULL);
1350Sstevel@tonic-gate mutex_init(&policymutex, NULL, MUTEX_DRIVER, NULL);
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate /* The mutex is held here in order to satisfy the ASSERT in dpget() */
1380Sstevel@tonic-gate mutex_enter(&policymutex);
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate nullpolicy = dpget();
1410Sstevel@tonic-gate dfltpolicy = dpget();
1420Sstevel@tonic-gate netpolicy = dpget();
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate * Initially, we refuse access to all devices except
1460Sstevel@tonic-gate * to processes with all privileges.
1470Sstevel@tonic-gate */
1480Sstevel@tonic-gate priv_fillset(&dfltpolicy->dp_rdp);
1490Sstevel@tonic-gate priv_fillset(&dfltpolicy->dp_wrp);
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate totitems = 1;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate devplcy_gen++;
1540Sstevel@tonic-gate mutex_exit(&policymutex);
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate /* initialize default network privilege */
1570Sstevel@tonic-gate priv_emptyset(&netpolicy->dp_rdp);
1580Sstevel@tonic-gate priv_emptyset(&netpolicy->dp_wrp);
1590Sstevel@tonic-gate priv_addset(&netpolicy->dp_rdp, PRIV_NET_RAWACCESS);
1600Sstevel@tonic-gate priv_addset(&netpolicy->dp_wrp, PRIV_NET_RAWACCESS);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate * Devpolicy reference counting/allocation routines.
1650Sstevel@tonic-gate * cf. crget()/crhold()/crfree().
1660Sstevel@tonic-gate */
1670Sstevel@tonic-gate devplcy_t *
dpget(void)1680Sstevel@tonic-gate dpget(void)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate devplcy_t *dp = kmem_zalloc(sizeof (*dp), KM_SLEEP);
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate ASSERT(MUTEX_HELD(&policymutex));
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate dp->dp_ref = 1;
1750Sstevel@tonic-gate /* New ones belong to the next generation */
1760Sstevel@tonic-gate dp->dp_gen = devplcy_gen + 1;
1770Sstevel@tonic-gate return (dp);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate void
dphold(devplcy_t * dp)1810Sstevel@tonic-gate dphold(devplcy_t *dp)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate ASSERT(dp->dp_ref != 0xdeadbeef && dp->dp_ref != 0);
1840Sstevel@tonic-gate atomic_add_32(&dp->dp_ref, 1);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate void
dpfree(devplcy_t * dp)1880Sstevel@tonic-gate dpfree(devplcy_t *dp)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate ASSERT(dp->dp_ref != 0xdeadbeef && dp->dp_ref != 0);
1910Sstevel@tonic-gate if (atomic_add_32_nv(&dp->dp_ref, -1) == 0)
1920Sstevel@tonic-gate kmem_free(dp, sizeof (*dp));
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate * Find the policy that matches this device.
1970Sstevel@tonic-gate */
1980Sstevel@tonic-gate static devplcy_t *
match_policy(devplcyent_t * de,dev_t dev,vtype_t spec)1990Sstevel@tonic-gate match_policy(devplcyent_t *de, dev_t dev, vtype_t spec)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate char *mname = NULL;
2020Sstevel@tonic-gate minor_t min = getminor(dev);
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate for (; de != NULL; de = de->dpe_next) {
2050Sstevel@tonic-gate if (de->dpe_flags & DPE_ALLMINOR)
2060Sstevel@tonic-gate break;
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate if (de->dpe_flags & DPE_EXPANDED) {
2090Sstevel@tonic-gate if (min >= de->dpe_lomin && min <= de->dpe_himin &&
2100Sstevel@tonic-gate spec == de->dpe_spec) {
2110Sstevel@tonic-gate break;
2120Sstevel@tonic-gate } else {
2130Sstevel@tonic-gate continue;
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate * We now need the minor name to match string or
2190Sstevel@tonic-gate * simle regexp. Could we use csp->s_dip and not
2200Sstevel@tonic-gate * allocate a string here?
2210Sstevel@tonic-gate */
2220Sstevel@tonic-gate if (mname == NULL &&
2230Sstevel@tonic-gate ddi_lyr_get_minor_name(dev, spec, &mname) != DDI_SUCCESS)
2240Sstevel@tonic-gate /* mname can be set after the function fails */
2250Sstevel@tonic-gate return (dfltpolicy);
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate /* Simple wildcard, with only one ``*'' */
2280Sstevel@tonic-gate if (de->dpe_flags & DPE_WILDC) {
2290Sstevel@tonic-gate int plen = de->dpe_len - 1;
2300Sstevel@tonic-gate int slen = strlen(mname);
2310Sstevel@tonic-gate char *pp = de->dpe_expr;
2320Sstevel@tonic-gate char *sp = mname;
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate /* string must be at least as long as pattern w/o '*' */
2350Sstevel@tonic-gate if (slen < plen - 1)
2360Sstevel@tonic-gate continue;
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate /* skip prefix */
2390Sstevel@tonic-gate while (*pp == *sp && *pp != '\0') {
2400Sstevel@tonic-gate pp++;
2410Sstevel@tonic-gate sp++;
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate /* matched single '*' */
2440Sstevel@tonic-gate if (*pp == '\0')
2450Sstevel@tonic-gate if (*sp == '\0')
2460Sstevel@tonic-gate break;
2470Sstevel@tonic-gate else
2480Sstevel@tonic-gate continue;
2490Sstevel@tonic-gate if (*pp != '*')
2500Sstevel@tonic-gate continue;
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate pp++;
2530Sstevel@tonic-gate /*
2540Sstevel@tonic-gate * skip characters matched by '*': difference of
2550Sstevel@tonic-gate * length of s and length of pattern sans '*'
2560Sstevel@tonic-gate */
2570Sstevel@tonic-gate sp += slen - (plen - 1);
2580Sstevel@tonic-gate if (strcmp(pp, sp) == 0) /* match! */
2590Sstevel@tonic-gate break;
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate } else if (strcmp(de->dpe_expr, mname) == 0) {
2620Sstevel@tonic-gate /* Store minor number, if no contention */
2630Sstevel@tonic-gate if (rw_tryupgrade(&policyrw)) {
2640Sstevel@tonic-gate de->dpe_lomin = de->dpe_himin = min;
2650Sstevel@tonic-gate de->dpe_spec = spec;
2660Sstevel@tonic-gate de->dpe_flags |= DPE_EXPANDED;
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate break;
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate if (mname != NULL)
2740Sstevel@tonic-gate kmem_free(mname, strlen(mname) + 1);
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate return (de != NULL ? de->dpe_plcy : dfltpolicy);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate static int
devpolicyent_bymajor(major_t maj)2800Sstevel@tonic-gate devpolicyent_bymajor(major_t maj)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate int lo, hi;
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate ASSERT(RW_LOCK_HELD(&policyrw));
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate lo = 0;
2870Sstevel@tonic-gate hi = ntabent - 1;
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate /* Binary search for major number */
2900Sstevel@tonic-gate while (lo <= hi) {
2910Sstevel@tonic-gate int mid = (lo + hi) / 2;
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate if (devpolicy[mid].t_major == maj)
2940Sstevel@tonic-gate return (mid);
2950Sstevel@tonic-gate else if (maj < devpolicy[mid].t_major)
2960Sstevel@tonic-gate hi = mid - 1;
2970Sstevel@tonic-gate else
2980Sstevel@tonic-gate lo = mid + 1;
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate return (-1);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate * Returns held device policy for the specific device node.
3050Sstevel@tonic-gate * Note devfs_devpolicy returns with a hold on the policy.
3060Sstevel@tonic-gate */
3070Sstevel@tonic-gate devplcy_t *
devpolicy_find(vnode_t * vp)3080Sstevel@tonic-gate devpolicy_find(vnode_t *vp)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate dev_t dev = vp->v_rdev;
3110Sstevel@tonic-gate vtype_t spec = vp->v_type;
3120Sstevel@tonic-gate major_t maj = getmajor(dev);
3130Sstevel@tonic-gate int i;
3140Sstevel@tonic-gate devplcy_t *res;
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate if (maj == clone_major)
3170Sstevel@tonic-gate maj = getminor(dev);
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate rw_enter(&policyrw, RW_READER);
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate i = devpolicyent_bymajor(maj);
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate if (i != -1) {
3240Sstevel@tonic-gate res = match_policy(devpolicy[i].t_ent, dev, spec);
3250Sstevel@tonic-gate dphold(res);
3260Sstevel@tonic-gate } else if (devfs_devpolicy(vp, &res) != 0) {
3270Sstevel@tonic-gate res = NETWORK_DRV(maj) ? netpolicy : dfltpolicy;
3280Sstevel@tonic-gate dphold(res);
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate rw_exit(&policyrw);
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate return (res);
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate static devplcyent_t *
parse_policy(devplcysys_t * ds,devplcy_t * nullp,devplcy_t * defp)3370Sstevel@tonic-gate parse_policy(devplcysys_t *ds, devplcy_t *nullp, devplcy_t *defp)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate devplcyent_t *de = kmem_zalloc(sizeof (*de), KM_SLEEP);
3400Sstevel@tonic-gate devplcy_t *np;
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate if (priv_isemptyset(&ds->dps_rdp) && priv_isemptyset(&ds->dps_wrp))
3430Sstevel@tonic-gate dphold(np = nullp);
3440Sstevel@tonic-gate else if (defp != nullp &&
3455753Sgww priv_isequalset(&ds->dps_rdp, &defp->dp_rdp) &&
3465753Sgww priv_isequalset(&ds->dps_wrp, &defp->dp_wrp))
3470Sstevel@tonic-gate dphold(np = defp);
3480Sstevel@tonic-gate else {
3490Sstevel@tonic-gate np = dpget();
3500Sstevel@tonic-gate np->dp_rdp = ds->dps_rdp;
3510Sstevel@tonic-gate np->dp_wrp = ds->dps_wrp;
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate if (ds->dps_minornm[0] != '\0') {
3550Sstevel@tonic-gate de->dpe_len = strlen(ds->dps_minornm) + 1;
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate if (strchr(ds->dps_minornm, '*') != NULL) {
3580Sstevel@tonic-gate if (de->dpe_len == 2) { /* "*\0" */
3590Sstevel@tonic-gate de->dpe_flags = DPE_ALLMINOR;
3600Sstevel@tonic-gate de->dpe_len = 0;
3610Sstevel@tonic-gate } else
3620Sstevel@tonic-gate de->dpe_flags = DPE_WILDC;
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate if (de->dpe_len != 0) {
3650Sstevel@tonic-gate de->dpe_expr = kmem_alloc(de->dpe_len, KM_SLEEP);
3660Sstevel@tonic-gate (void) strcpy(de->dpe_expr, ds->dps_minornm);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate } else {
3690Sstevel@tonic-gate de->dpe_lomin = ds->dps_lomin;
3700Sstevel@tonic-gate de->dpe_himin = ds->dps_himin;
3710Sstevel@tonic-gate de->dpe_flags = DPE_EXPANDED;
3720Sstevel@tonic-gate de->dpe_spec = ds->dps_isblock ? VBLK : VCHR;
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate de->dpe_plcy = np;
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate ASSERT((de->dpe_flags & (DPE_ALLMINOR|DPE_EXPANDED)) ||
3775753Sgww de->dpe_expr != NULL);
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate return (de);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate static void
freechain(devplcyent_t * de)3830Sstevel@tonic-gate freechain(devplcyent_t *de)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate devplcyent_t *dn;
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate do {
3880Sstevel@tonic-gate dn = de->dpe_next;
3890Sstevel@tonic-gate dpfree(de->dpe_plcy);
3900Sstevel@tonic-gate if (de->dpe_len != 0)
3910Sstevel@tonic-gate kmem_free(de->dpe_expr, de->dpe_len);
3920Sstevel@tonic-gate kmem_free(de, sizeof (*de));
3930Sstevel@tonic-gate de = dn;
3940Sstevel@tonic-gate } while (de != NULL);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate * Load the device policy.
3990Sstevel@tonic-gate * The device policy currently makes nu distinction between the
4000Sstevel@tonic-gate * block and characters devices; that is generally not a problem
4010Sstevel@tonic-gate * as the names of those devices cannot clash.
4020Sstevel@tonic-gate */
4030Sstevel@tonic-gate int
devpolicy_load(int nitems,size_t sz,devplcysys_t * uitmp)4040Sstevel@tonic-gate devpolicy_load(int nitems, size_t sz, devplcysys_t *uitmp)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate int i, j;
4070Sstevel@tonic-gate int nmaj = 0;
4080Sstevel@tonic-gate major_t lastmajor;
4090Sstevel@tonic-gate devplcysys_t *items;
4100Sstevel@tonic-gate size_t mem;
4110Sstevel@tonic-gate major_t curmaj;
4120Sstevel@tonic-gate devplcyent_t **last, *de;
4130Sstevel@tonic-gate
4140Sstevel@tonic-gate tableent_t *newpolicy, *oldpolicy;
4150Sstevel@tonic-gate devplcy_t *newnull, *newdflt, *oldnull, *olddflt;
4160Sstevel@tonic-gate int oldcnt;
4170Sstevel@tonic-gate int lastlen;
4180Sstevel@tonic-gate int lastwild;
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate #ifdef lint
4210Sstevel@tonic-gate /* Lint can't figure out that the "i == 1" test protects all */
4220Sstevel@tonic-gate lastlen = 0;
4230Sstevel@tonic-gate lastwild = 0;
4240Sstevel@tonic-gate lastmajor = 0;
4250Sstevel@tonic-gate #endif
4260Sstevel@tonic-gate /*
4270Sstevel@tonic-gate * The application must agree with the kernel on the size of each
4280Sstevel@tonic-gate * item; it must not exceed the maximum number and must be
4290Sstevel@tonic-gate * at least 1 item in size.
4300Sstevel@tonic-gate */
4310Sstevel@tonic-gate if (sz != sizeof (devplcysys_t) || nitems > maxdevpolicy || nitems < 1)
4320Sstevel@tonic-gate return (EINVAL);
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate mem = nitems * sz;
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate items = kmem_alloc(mem, KM_SLEEP);
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate if (copyin(uitmp, items, mem)) {
4390Sstevel@tonic-gate kmem_free(items, mem);
4400Sstevel@tonic-gate return (EFAULT);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate /* Check for default policy, it must exist and be sorted first */
4440Sstevel@tonic-gate if (items[0].dps_maj != DEVPOLICY_DFLT_MAJ) {
4450Sstevel@tonic-gate kmem_free(items, mem);
4460Sstevel@tonic-gate return (EINVAL);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate /*
4500Sstevel@tonic-gate * Application must deliver entries sorted.
4510Sstevel@tonic-gate * Sorted meaning here:
4520Sstevel@tonic-gate * In major number order
4530Sstevel@tonic-gate * For each major number, we first need to have the explicit
4540Sstevel@tonic-gate * entries, then the wild card entries, longest first.
4550Sstevel@tonic-gate */
4560Sstevel@tonic-gate for (i = 1; i < nitems; i++) {
4570Sstevel@tonic-gate int len, wild;
4580Sstevel@tonic-gate char *tmp;
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate curmaj = items[i].dps_maj;
4610Sstevel@tonic-gate len = strlen(items[i].dps_minornm);
4620Sstevel@tonic-gate wild = len > 0 &&
4635753Sgww (tmp = strchr(items[i].dps_minornm, '*')) != NULL;
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate /* Another default major, string too long or too many ``*'' */
4660Sstevel@tonic-gate if (curmaj == DEVPOLICY_DFLT_MAJ ||
4670Sstevel@tonic-gate len >= sizeof (items[i].dps_minornm) ||
4680Sstevel@tonic-gate wild && strchr(tmp + 1, '*') != NULL) {
4690Sstevel@tonic-gate kmem_free(items, mem);
4700Sstevel@tonic-gate return (EINVAL);
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate if (i == 1 || lastmajor < curmaj) {
4730Sstevel@tonic-gate lastmajor = curmaj;
4740Sstevel@tonic-gate nmaj++;
4750Sstevel@tonic-gate } else if (lastmajor > curmaj || lastwild > wild ||
4765753Sgww lastwild && lastlen < len) {
4770Sstevel@tonic-gate kmem_free(items, mem);
4780Sstevel@tonic-gate return (EINVAL);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate lastlen = len;
4810Sstevel@tonic-gate lastwild = wild;
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate
484*11861SMarek.Pospisil@Sun.COM if (AU_AUDITING())
4850Sstevel@tonic-gate audit_devpolicy(nitems, items);
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate /*
4880Sstevel@tonic-gate * Parse the policy. We create an array for all major numbers
4890Sstevel@tonic-gate * and in each major number bucket we'll have a linked list of
4900Sstevel@tonic-gate * entries. Each item may contain either a lo,hi minor pair
4910Sstevel@tonic-gate * or a string/wild card matching a minor node.
4920Sstevel@tonic-gate */
4930Sstevel@tonic-gate if (nmaj > 0)
4940Sstevel@tonic-gate newpolicy = kmem_zalloc(nmaj * sizeof (tableent_t), KM_SLEEP);
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate /*
4970Sstevel@tonic-gate * We want to lock out concurrent updates but we don't want to
4980Sstevel@tonic-gate * lock out device opens while we still need to allocate memory.
4990Sstevel@tonic-gate * As soon as we allocate new devplcy_t's we commit to the next
5000Sstevel@tonic-gate * generation number, so we must lock out other updates from here.
5010Sstevel@tonic-gate */
5020Sstevel@tonic-gate mutex_enter(&policymutex);
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate /* New default and NULL policy */
5050Sstevel@tonic-gate newnull = dpget();
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate if (priv_isemptyset(&items[0].dps_rdp) &&
5080Sstevel@tonic-gate priv_isemptyset(&items[0].dps_wrp)) {
5090Sstevel@tonic-gate newdflt = newnull;
5100Sstevel@tonic-gate dphold(newdflt);
5110Sstevel@tonic-gate } else {
5120Sstevel@tonic-gate newdflt = dpget();
5130Sstevel@tonic-gate newdflt->dp_rdp = items[0].dps_rdp;
5140Sstevel@tonic-gate newdflt->dp_wrp = items[0].dps_wrp;
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate j = -1;
5180Sstevel@tonic-gate
5190Sstevel@tonic-gate /* Userland made sure sorting was ok */
5200Sstevel@tonic-gate for (i = 1; i < nitems; i++) {
5210Sstevel@tonic-gate de = parse_policy(&items[i], newnull, newdflt);
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate if (j == -1 || curmaj != items[i].dps_maj) {
5240Sstevel@tonic-gate j++;
5250Sstevel@tonic-gate newpolicy[j].t_major = curmaj = items[i].dps_maj;
5260Sstevel@tonic-gate last = &newpolicy[j].t_ent;
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate *last = de;
5290Sstevel@tonic-gate last = &de->dpe_next;
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate /* Done parsing, throw away input */
5330Sstevel@tonic-gate kmem_free(items, mem);
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate /* Lock out all devpolicy_find()s */
5360Sstevel@tonic-gate rw_enter(&policyrw, RW_WRITER);
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate /* Install the new global data */
5390Sstevel@tonic-gate oldnull = nullpolicy;
5400Sstevel@tonic-gate nullpolicy = newnull;
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate olddflt = dfltpolicy;
5430Sstevel@tonic-gate dfltpolicy = newdflt;
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate oldcnt = ntabent;
5460Sstevel@tonic-gate ntabent = nmaj;
5470Sstevel@tonic-gate
5480Sstevel@tonic-gate totitems = nitems;
5490Sstevel@tonic-gate
5500Sstevel@tonic-gate oldpolicy = devpolicy;
5510Sstevel@tonic-gate devpolicy = newpolicy;
5520Sstevel@tonic-gate
5530Sstevel@tonic-gate /* Force all calls by devpolicy_find() */
5540Sstevel@tonic-gate devplcy_gen++;
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate /* Reenable policy finds */
5570Sstevel@tonic-gate rw_exit(&policyrw);
5580Sstevel@tonic-gate mutex_exit(&policymutex);
5590Sstevel@tonic-gate
5600Sstevel@tonic-gate /* Free old stuff */
5610Sstevel@tonic-gate if (oldcnt != 0) {
5620Sstevel@tonic-gate for (i = 0; i < oldcnt; i++)
5630Sstevel@tonic-gate freechain(oldpolicy[i].t_ent);
5640Sstevel@tonic-gate kmem_free(oldpolicy, oldcnt * sizeof (*oldpolicy));
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate dpfree(oldnull);
5680Sstevel@tonic-gate dpfree(olddflt);
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate return (0);
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate /*
5740Sstevel@tonic-gate * Get device policy: argument one is a pointer to an integer holding
5750Sstevel@tonic-gate * the number of items allocated for the 3rd argument; the size argument
5760Sstevel@tonic-gate * is a revision check between kernel and userland.
5770Sstevel@tonic-gate */
5780Sstevel@tonic-gate int
devpolicy_get(int * nitemp,size_t sz,devplcysys_t * uitmp)5790Sstevel@tonic-gate devpolicy_get(int *nitemp, size_t sz, devplcysys_t *uitmp)
5800Sstevel@tonic-gate {
5810Sstevel@tonic-gate int i;
5820Sstevel@tonic-gate devplcyent_t *de;
5830Sstevel@tonic-gate devplcysys_t *itmp;
5840Sstevel@tonic-gate int ind;
5850Sstevel@tonic-gate int nitems;
5860Sstevel@tonic-gate int err = 0;
5870Sstevel@tonic-gate size_t alloced;
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate if (sz != sizeof (devplcysys_t))
5900Sstevel@tonic-gate return (EINVAL);
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate if (copyin(nitemp, &nitems, sizeof (nitems)))
5930Sstevel@tonic-gate return (EFAULT);
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate rw_enter(&policyrw, RW_READER);
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate if (copyout(&totitems, nitemp, sizeof (totitems)))
5980Sstevel@tonic-gate err = EFAULT;
5990Sstevel@tonic-gate else if (nitems < totitems)
6000Sstevel@tonic-gate err = ENOMEM;
6010Sstevel@tonic-gate
6020Sstevel@tonic-gate if (err != 0) {
6030Sstevel@tonic-gate rw_exit(&policyrw);
6040Sstevel@tonic-gate return (err);
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate alloced = totitems * sizeof (devplcysys_t);
6080Sstevel@tonic-gate itmp = kmem_zalloc(alloced, KM_SLEEP);
6090Sstevel@tonic-gate
6100Sstevel@tonic-gate itmp[0].dps_rdp = dfltpolicy->dp_rdp;
6110Sstevel@tonic-gate itmp[0].dps_wrp = dfltpolicy->dp_wrp;
6120Sstevel@tonic-gate itmp[0].dps_maj = DEVPOLICY_DFLT_MAJ;
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate ind = 1;
6150Sstevel@tonic-gate
6160Sstevel@tonic-gate for (i = 0; i < ntabent; i++) {
6170Sstevel@tonic-gate for (de = devpolicy[i].t_ent; de != NULL; de = de->dpe_next) {
6180Sstevel@tonic-gate itmp[ind].dps_maj = devpolicy[i].t_major;
6190Sstevel@tonic-gate itmp[ind].dps_rdp = de->dpe_plcy->dp_rdp;
6200Sstevel@tonic-gate itmp[ind].dps_wrp = de->dpe_plcy->dp_wrp;
6210Sstevel@tonic-gate if (de->dpe_len)
6220Sstevel@tonic-gate (void) strcpy(itmp[ind].dps_minornm,
6235753Sgww de->dpe_expr);
6240Sstevel@tonic-gate else if (de->dpe_flags & DPE_ALLMINOR)
6250Sstevel@tonic-gate (void) strcpy(itmp[ind].dps_minornm, "*");
6260Sstevel@tonic-gate else {
6270Sstevel@tonic-gate itmp[ind].dps_lomin = de->dpe_lomin;
6280Sstevel@tonic-gate itmp[ind].dps_himin = de->dpe_himin;
6290Sstevel@tonic-gate itmp[ind].dps_isblock = de->dpe_spec == VBLK;
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate ind++;
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate rw_exit(&policyrw);
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate if (copyout(itmp, uitmp, alloced))
6380Sstevel@tonic-gate err = EFAULT;
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate kmem_free(itmp, alloced);
6410Sstevel@tonic-gate return (err);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate
6440Sstevel@tonic-gate /*
6450Sstevel@tonic-gate * Get device policy by device name.
6460Sstevel@tonic-gate * This is the implementation of MODGETDEVPOLICYBYNAME
6470Sstevel@tonic-gate */
6480Sstevel@tonic-gate int
devpolicy_getbyname(size_t sz,devplcysys_t * uitmp,char * devname)6490Sstevel@tonic-gate devpolicy_getbyname(size_t sz, devplcysys_t *uitmp, char *devname)
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate devplcysys_t itm;
6520Sstevel@tonic-gate devplcy_t *plcy;
6530Sstevel@tonic-gate vtype_t spec;
6540Sstevel@tonic-gate vnode_t *vp;
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate if (sz != sizeof (devplcysys_t))
6570Sstevel@tonic-gate return (EINVAL);
6580Sstevel@tonic-gate
6590Sstevel@tonic-gate if (lookupname(devname, UIO_USERSPACE, FOLLOW,
6600Sstevel@tonic-gate NULLVPP, &vp) != 0)
6610Sstevel@tonic-gate return (EINVAL);
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate spec = vp->v_type;
6640Sstevel@tonic-gate if (spec != VBLK && spec != VCHR) {
6650Sstevel@tonic-gate VN_RELE(vp);
6660Sstevel@tonic-gate return (EINVAL);
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate
6690Sstevel@tonic-gate plcy = devpolicy_find(vp);
6700Sstevel@tonic-gate VN_RELE(vp);
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate bzero(&itm, sizeof (itm));
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate /* These are the only values of interest */
6750Sstevel@tonic-gate itm.dps_rdp = plcy->dp_rdp;
6760Sstevel@tonic-gate itm.dps_wrp = plcy->dp_wrp;
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate dpfree(plcy);
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate if (copyout(&itm, uitmp, sz))
6810Sstevel@tonic-gate return (EFAULT);
6820Sstevel@tonic-gate else
6830Sstevel@tonic-gate return (0);
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate
6860Sstevel@tonic-gate static void
priv_str_to_set(const char * priv_name,priv_set_t * priv_set)6870Sstevel@tonic-gate priv_str_to_set(const char *priv_name, priv_set_t *priv_set)
6880Sstevel@tonic-gate {
6890Sstevel@tonic-gate if (priv_name == NULL || strcmp(priv_name, "none") == 0) {
6900Sstevel@tonic-gate priv_emptyset(priv_set);
6910Sstevel@tonic-gate } else if (strcmp(priv_name, "all") == 0) {
6920Sstevel@tonic-gate priv_fillset(priv_set);
6930Sstevel@tonic-gate } else {
6940Sstevel@tonic-gate int priv;
6950Sstevel@tonic-gate priv = priv_getbyname(priv_name, PRIV_ALLOC);
6960Sstevel@tonic-gate if (priv < 0) {
6970Sstevel@tonic-gate cmn_err(CE_WARN, "fail to allocate privilege: %s",
6980Sstevel@tonic-gate priv_name);
6990Sstevel@tonic-gate return;
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate priv_emptyset(priv_set);
7020Sstevel@tonic-gate priv_addset(priv_set, priv);
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate
7060Sstevel@tonic-gate /*
7070Sstevel@tonic-gate * Return device privileges by privilege name
7080Sstevel@tonic-gate * Called by ddi_create_priv_minor_node()
7090Sstevel@tonic-gate */
7100Sstevel@tonic-gate devplcy_t *
devpolicy_priv_by_name(const char * read_priv,const char * write_priv)7110Sstevel@tonic-gate devpolicy_priv_by_name(const char *read_priv, const char *write_priv)
7120Sstevel@tonic-gate {
7130Sstevel@tonic-gate devplcy_t *dp;
7140Sstevel@tonic-gate mutex_enter(&policymutex);
7150Sstevel@tonic-gate dp = dpget();
7160Sstevel@tonic-gate mutex_exit(&policymutex);
7170Sstevel@tonic-gate priv_str_to_set(read_priv, &dp->dp_rdp);
7180Sstevel@tonic-gate priv_str_to_set(write_priv, &dp->dp_wrp);
7190Sstevel@tonic-gate
7200Sstevel@tonic-gate return (dp);
7210Sstevel@tonic-gate }
722