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 51676Sjpk * Common Development and Distribution License (the "License"). 61676Sjpk * 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 /* 224321Scasper * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 310Sstevel@tonic-gate * The Regents of the University of California 320Sstevel@tonic-gate * All Rights Reserved 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 350Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 360Sstevel@tonic-gate * contributors. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 400Sstevel@tonic-gate 410Sstevel@tonic-gate #include <sys/types.h> 420Sstevel@tonic-gate #include <sys/sysmacros.h> 430Sstevel@tonic-gate #include <sys/param.h> 440Sstevel@tonic-gate #include <sys/systm.h> 450Sstevel@tonic-gate #include <sys/cred_impl.h> 460Sstevel@tonic-gate #include <sys/policy.h> 470Sstevel@tonic-gate #include <sys/vnode.h> 480Sstevel@tonic-gate #include <sys/errno.h> 490Sstevel@tonic-gate #include <sys/kmem.h> 500Sstevel@tonic-gate #include <sys/user.h> 510Sstevel@tonic-gate #include <sys/proc.h> 520Sstevel@tonic-gate #include <sys/syscall.h> 530Sstevel@tonic-gate #include <sys/debug.h> 540Sstevel@tonic-gate #include <sys/atomic.h> 550Sstevel@tonic-gate #include <sys/ucred.h> 560Sstevel@tonic-gate #include <sys/prsystm.h> 570Sstevel@tonic-gate #include <sys/modctl.h> 584321Scasper #include <sys/avl.h> 590Sstevel@tonic-gate #include <c2/audit.h> 600Sstevel@tonic-gate #include <sys/zone.h> 611676Sjpk #include <sys/tsol/label.h> 624321Scasper #include <sys/sid.h> 63*4520Snw141292 #include <sys/idmap.h> 644321Scasper 654321Scasper typedef struct ephidmap_data { 664321Scasper uid_t min_uid, last_uid; 674321Scasper gid_t min_gid, last_gid; 684321Scasper cred_t *nobody; 694321Scasper kmutex_t eph_lock; 704321Scasper } ephidmap_data_t; 710Sstevel@tonic-gate 720Sstevel@tonic-gate static struct kmem_cache *cred_cache; 730Sstevel@tonic-gate static size_t crsize = 0; 740Sstevel@tonic-gate static int audoff = 0; 750Sstevel@tonic-gate uint32_t ucredsize; 760Sstevel@tonic-gate cred_t *kcred; 771676Sjpk static cred_t *dummycr; 780Sstevel@tonic-gate 790Sstevel@tonic-gate int rstlink; /* link(2) restricted to files owned by user? */ 800Sstevel@tonic-gate 810Sstevel@tonic-gate static int get_c2audit_load(void); 820Sstevel@tonic-gate 830Sstevel@tonic-gate #define CR_AUINFO(c) (auditinfo_addr_t *)((audoff == 0) ? NULL : \ 840Sstevel@tonic-gate ((char *)(c)) + audoff) 850Sstevel@tonic-gate 861676Sjpk #define REMOTE_PEER_CRED(c) ((c)->cr_gid == -1) 870Sstevel@tonic-gate 880Sstevel@tonic-gate /* 894321Scasper * XXX: should be per-zone. 904321Scasper * Start with an invalid value for atomic increments. 914321Scasper */ 924321Scasper static ephidmap_data_t ephemeral_data = { 93*4520Snw141292 MAXUID, IDMAP_WK__MAX_UID, MAXUID, IDMAP_WK__MAX_GID 944321Scasper }; 954321Scasper 964321Scasper static boolean_t hasephids = B_FALSE; 974321Scasper 984321Scasper /* 990Sstevel@tonic-gate * Initialize credentials data structures. 1000Sstevel@tonic-gate */ 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate void 1030Sstevel@tonic-gate cred_init(void) 1040Sstevel@tonic-gate { 1050Sstevel@tonic-gate priv_init(); 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate crsize = sizeof (cred_t) + sizeof (gid_t) * (ngroups_max - 1); 1080Sstevel@tonic-gate /* 1090Sstevel@tonic-gate * Make sure it's word-aligned. 1100Sstevel@tonic-gate */ 1110Sstevel@tonic-gate crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1); 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate if (get_c2audit_load() > 0) { 1140Sstevel@tonic-gate #ifdef _LP64 1150Sstevel@tonic-gate /* assure audit context is 64-bit aligned */ 1160Sstevel@tonic-gate audoff = (crsize + 1170Sstevel@tonic-gate sizeof (int64_t) - 1) & ~(sizeof (int64_t) - 1); 1180Sstevel@tonic-gate #else /* _LP64 */ 1190Sstevel@tonic-gate audoff = crsize; 1200Sstevel@tonic-gate #endif /* _LP64 */ 1210Sstevel@tonic-gate crsize = audoff + sizeof (auditinfo_addr_t); 1220Sstevel@tonic-gate crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1); 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate cred_cache = kmem_cache_create("cred_cache", crsize, 0, 126*4520Snw141292 NULL, NULL, NULL, NULL, NULL, 0); 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate /* 1291676Sjpk * dummycr is used to copy initial state for creds. 1301676Sjpk */ 1311676Sjpk dummycr = cralloc(); 1321676Sjpk bzero(dummycr, crsize); 1331676Sjpk dummycr->cr_ref = 1; 1344321Scasper dummycr->cr_uid = (uid_t)-1; 1354321Scasper dummycr->cr_gid = (gid_t)-1; 1364321Scasper dummycr->cr_ruid = (uid_t)-1; 1374321Scasper dummycr->cr_rgid = (gid_t)-1; 1384321Scasper dummycr->cr_suid = (uid_t)-1; 1394321Scasper dummycr->cr_sgid = (gid_t)-1; 1404321Scasper 1411676Sjpk 1421676Sjpk /* 1430Sstevel@tonic-gate * kcred is used by anything that needs all privileges; it's 1440Sstevel@tonic-gate * also the template used for crget as it has all the compatible 1450Sstevel@tonic-gate * sets filled in. 1460Sstevel@tonic-gate */ 1470Sstevel@tonic-gate kcred = cralloc(); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate bzero(kcred, crsize); 1500Sstevel@tonic-gate kcred->cr_ref = 1; 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate /* kcred is never freed, so we don't need zone_cred_hold here */ 1530Sstevel@tonic-gate kcred->cr_zone = &zone0; 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate priv_fillset(&CR_LPRIV(kcred)); 1560Sstevel@tonic-gate CR_IPRIV(kcred) = *priv_basic; 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate /* Not a basic privilege, if chown is not restricted add it to I0 */ 1590Sstevel@tonic-gate if (!rstchown) 1600Sstevel@tonic-gate priv_addset(&CR_IPRIV(kcred), PRIV_FILE_CHOWN_SELF); 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate /* Basic privilege, if link is restricted remove it from I0 */ 1630Sstevel@tonic-gate if (rstlink) 1640Sstevel@tonic-gate priv_delset(&CR_IPRIV(kcred), PRIV_FILE_LINK_ANY); 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate CR_EPRIV(kcred) = CR_PPRIV(kcred) = CR_IPRIV(kcred); 1671676Sjpk 1681676Sjpk CR_FLAGS(kcred) = NET_MAC_AWARE; 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate /* 1710Sstevel@tonic-gate * Set up credentials of p0. 1720Sstevel@tonic-gate */ 1730Sstevel@tonic-gate ttoproc(curthread)->p_cred = kcred; 1740Sstevel@tonic-gate curthread->t_cred = kcred; 1750Sstevel@tonic-gate 1764321Scasper /* 1774321Scasper * nobody is used to map SID containing CRs. 1784321Scasper */ 1794321Scasper ephemeral_data.nobody = crdup(kcred); 1804321Scasper (void) crsetugid(ephemeral_data.nobody, UID_NOBODY, GID_NOBODY); 1814321Scasper CR_FLAGS(kcred) = 0; 1824321Scasper 1830Sstevel@tonic-gate ucredsize = UCRED_SIZE; 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* 1870Sstevel@tonic-gate * Allocate (nearly) uninitialized cred_t. 1880Sstevel@tonic-gate */ 1890Sstevel@tonic-gate cred_t * 1900Sstevel@tonic-gate cralloc(void) 1910Sstevel@tonic-gate { 1920Sstevel@tonic-gate cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP); 1930Sstevel@tonic-gate cr->cr_ref = 1; /* So we can crfree() */ 1940Sstevel@tonic-gate cr->cr_zone = NULL; 1951676Sjpk cr->cr_label = NULL; 1964321Scasper cr->cr_ksid = NULL; 1974321Scasper return (cr); 1984321Scasper } 1994321Scasper 2004321Scasper /* 2014321Scasper * As cralloc but prepared for ksid change (if appropriate). 2024321Scasper */ 2034321Scasper cred_t * 2044321Scasper cralloc_ksid(void) 2054321Scasper { 2064321Scasper cred_t *cr = cralloc(); 2074321Scasper if (hasephids) 2084321Scasper cr->cr_ksid = kcrsid_alloc(); 2090Sstevel@tonic-gate return (cr); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate /* 2130Sstevel@tonic-gate * Allocate a initialized cred structure and crhold() it. 2140Sstevel@tonic-gate * Initialized means: all ids 0, group count 0, L=Full, E=P=I=I0 2150Sstevel@tonic-gate */ 2160Sstevel@tonic-gate cred_t * 2170Sstevel@tonic-gate crget(void) 2180Sstevel@tonic-gate { 2190Sstevel@tonic-gate cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP); 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate bcopy(kcred, cr, crsize); 2220Sstevel@tonic-gate cr->cr_ref = 1; 2230Sstevel@tonic-gate zone_cred_hold(cr->cr_zone); 2241676Sjpk if (cr->cr_label) 2251676Sjpk label_hold(cr->cr_label); 2260Sstevel@tonic-gate return (cr); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate /* 2300Sstevel@tonic-gate * Broadcast the cred to all the threads in the process. 2310Sstevel@tonic-gate * The current thread's credentials can be set right away, but other 2320Sstevel@tonic-gate * threads must wait until the start of the next system call or trap. 2330Sstevel@tonic-gate * This avoids changing the cred in the middle of a system call. 2340Sstevel@tonic-gate * 2350Sstevel@tonic-gate * The cred has already been held for the process and the thread (2 holds), 2360Sstevel@tonic-gate * and p->p_cred set. 2370Sstevel@tonic-gate * 2380Sstevel@tonic-gate * p->p_crlock shouldn't be held here, since p_lock must be acquired. 2390Sstevel@tonic-gate */ 2400Sstevel@tonic-gate void 2410Sstevel@tonic-gate crset(proc_t *p, cred_t *cr) 2420Sstevel@tonic-gate { 2430Sstevel@tonic-gate kthread_id_t t; 2440Sstevel@tonic-gate kthread_id_t first; 2450Sstevel@tonic-gate cred_t *oldcr; 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate ASSERT(p == curproc); /* assumes p_lwpcnt can't change */ 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate /* 2500Sstevel@tonic-gate * DTrace accesses t_cred in probe context. t_cred must always be 2510Sstevel@tonic-gate * either NULL, or point to a valid, allocated cred structure. 2520Sstevel@tonic-gate */ 2530Sstevel@tonic-gate t = curthread; 2540Sstevel@tonic-gate oldcr = t->t_cred; 2550Sstevel@tonic-gate t->t_cred = cr; /* the cred is held by caller for this thread */ 2560Sstevel@tonic-gate crfree(oldcr); /* free the old cred for the thread */ 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate /* 2590Sstevel@tonic-gate * Broadcast to other threads, if any. 2600Sstevel@tonic-gate */ 2610Sstevel@tonic-gate if (p->p_lwpcnt > 1) { 2620Sstevel@tonic-gate mutex_enter(&p->p_lock); /* to keep thread list safe */ 2630Sstevel@tonic-gate first = curthread; 2640Sstevel@tonic-gate for (t = first->t_forw; t != first; t = t->t_forw) 2650Sstevel@tonic-gate t->t_pre_sys = 1; /* so syscall will get new cred */ 2660Sstevel@tonic-gate mutex_exit(&p->p_lock); 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate /* 2710Sstevel@tonic-gate * Put a hold on a cred structure. 2720Sstevel@tonic-gate */ 2730Sstevel@tonic-gate void 2740Sstevel@tonic-gate crhold(cred_t *cr) 2750Sstevel@tonic-gate { 2760Sstevel@tonic-gate atomic_add_32(&cr->cr_ref, 1); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate /* 2800Sstevel@tonic-gate * Release previous hold on a cred structure. Free it if refcnt == 0. 2811676Sjpk * If cred uses label different from zone label, free it. 2820Sstevel@tonic-gate */ 2830Sstevel@tonic-gate void 2840Sstevel@tonic-gate crfree(cred_t *cr) 2850Sstevel@tonic-gate { 2860Sstevel@tonic-gate if (atomic_add_32_nv(&cr->cr_ref, -1) == 0) { 2870Sstevel@tonic-gate ASSERT(cr != kcred); 2881676Sjpk if (cr->cr_label) 2891676Sjpk label_rele(cr->cr_label); 2900Sstevel@tonic-gate if (cr->cr_zone) 2910Sstevel@tonic-gate zone_cred_rele(cr->cr_zone); 2924321Scasper if (cr->cr_ksid) 2934321Scasper kcrsid_rele(cr->cr_ksid); 2940Sstevel@tonic-gate kmem_cache_free(cred_cache, cr); 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate /* 2990Sstevel@tonic-gate * Copy a cred structure to a new one and free the old one. 3000Sstevel@tonic-gate * The new cred will have two references. One for the calling process, 3010Sstevel@tonic-gate * and one for the thread. 3020Sstevel@tonic-gate */ 3030Sstevel@tonic-gate cred_t * 3040Sstevel@tonic-gate crcopy(cred_t *cr) 3050Sstevel@tonic-gate { 3060Sstevel@tonic-gate cred_t *newcr; 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate newcr = cralloc(); 3090Sstevel@tonic-gate bcopy(cr, newcr, crsize); 3100Sstevel@tonic-gate if (newcr->cr_zone) 3110Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 3121676Sjpk if (newcr->cr_label) 3131676Sjpk label_hold(cr->cr_label); 3144321Scasper if (newcr->cr_ksid) 3154321Scasper kcrsid_hold(cr->cr_ksid); 3160Sstevel@tonic-gate crfree(cr); 3170Sstevel@tonic-gate newcr->cr_ref = 2; /* caller gets two references */ 3180Sstevel@tonic-gate return (newcr); 3190Sstevel@tonic-gate } 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate /* 3220Sstevel@tonic-gate * Copy a cred structure to a new one and free the old one. 3230Sstevel@tonic-gate * The new cred will have two references. One for the calling process, 3240Sstevel@tonic-gate * and one for the thread. 3250Sstevel@tonic-gate * This variation on crcopy uses a pre-allocated structure for the 3260Sstevel@tonic-gate * "new" cred. 3270Sstevel@tonic-gate */ 3280Sstevel@tonic-gate void 3290Sstevel@tonic-gate crcopy_to(cred_t *oldcr, cred_t *newcr) 3300Sstevel@tonic-gate { 3314321Scasper credsid_t *nkcr = newcr->cr_ksid; 3324321Scasper 3330Sstevel@tonic-gate bcopy(oldcr, newcr, crsize); 3340Sstevel@tonic-gate if (newcr->cr_zone) 3350Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 3361676Sjpk if (newcr->cr_label) 3371676Sjpk label_hold(newcr->cr_label); 3384321Scasper if (nkcr) { 3394321Scasper newcr->cr_ksid = nkcr; 3404321Scasper kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid); 3414321Scasper } else if (newcr->cr_ksid) 3424321Scasper kcrsid_hold(newcr->cr_ksid); 3430Sstevel@tonic-gate crfree(oldcr); 3440Sstevel@tonic-gate newcr->cr_ref = 2; /* caller gets two references */ 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate /* 3480Sstevel@tonic-gate * Dup a cred struct to a new held one. 3490Sstevel@tonic-gate * The old cred is not freed. 3500Sstevel@tonic-gate */ 3510Sstevel@tonic-gate cred_t * 3520Sstevel@tonic-gate crdup(cred_t *cr) 3530Sstevel@tonic-gate { 3540Sstevel@tonic-gate cred_t *newcr; 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate newcr = cralloc(); 3570Sstevel@tonic-gate bcopy(cr, newcr, crsize); 3580Sstevel@tonic-gate if (newcr->cr_zone) 3590Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 3601676Sjpk if (newcr->cr_label) 3611676Sjpk label_hold(newcr->cr_label); 3624321Scasper if (newcr->cr_ksid) 3634321Scasper kcrsid_hold(newcr->cr_ksid); 3640Sstevel@tonic-gate newcr->cr_ref = 1; 3650Sstevel@tonic-gate return (newcr); 3660Sstevel@tonic-gate } 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate /* 3690Sstevel@tonic-gate * Dup a cred struct to a new held one. 3700Sstevel@tonic-gate * The old cred is not freed. 3710Sstevel@tonic-gate * This variation on crdup uses a pre-allocated structure for the 3720Sstevel@tonic-gate * "new" cred. 3730Sstevel@tonic-gate */ 3740Sstevel@tonic-gate void 3750Sstevel@tonic-gate crdup_to(cred_t *oldcr, cred_t *newcr) 3760Sstevel@tonic-gate { 3774321Scasper credsid_t *nkcr = newcr->cr_ksid; 3784321Scasper 3790Sstevel@tonic-gate bcopy(oldcr, newcr, crsize); 3800Sstevel@tonic-gate if (newcr->cr_zone) 3810Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 3821676Sjpk if (newcr->cr_label) 3831676Sjpk label_hold(newcr->cr_label); 3844321Scasper if (nkcr) { 3854321Scasper newcr->cr_ksid = nkcr; 3864321Scasper kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid); 3874321Scasper } else if (newcr->cr_ksid) 3884321Scasper kcrsid_hold(newcr->cr_ksid); 3890Sstevel@tonic-gate newcr->cr_ref = 1; 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate /* 3930Sstevel@tonic-gate * Return the (held) credentials for the current running process. 3940Sstevel@tonic-gate */ 3950Sstevel@tonic-gate cred_t * 3961676Sjpk crgetcred(void) 3970Sstevel@tonic-gate { 3980Sstevel@tonic-gate cred_t *cr; 3990Sstevel@tonic-gate proc_t *p; 4000Sstevel@tonic-gate 4010Sstevel@tonic-gate p = ttoproc(curthread); 4020Sstevel@tonic-gate mutex_enter(&p->p_crlock); 4030Sstevel@tonic-gate crhold(cr = p->p_cred); 4040Sstevel@tonic-gate mutex_exit(&p->p_crlock); 4050Sstevel@tonic-gate return (cr); 4060Sstevel@tonic-gate } 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate /* 4090Sstevel@tonic-gate * Backward compatibility check for suser(). 4100Sstevel@tonic-gate * Accounting flag is now set in the policy functions; auditing is 4110Sstevel@tonic-gate * done through use of privilege in the audit trail. 4120Sstevel@tonic-gate */ 4130Sstevel@tonic-gate int 4140Sstevel@tonic-gate suser(cred_t *cr) 4150Sstevel@tonic-gate { 4160Sstevel@tonic-gate return (PRIV_POLICY(cr, PRIV_SYS_SUSER_COMPAT, B_FALSE, EPERM, NULL) 4170Sstevel@tonic-gate == 0); 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate /* 4210Sstevel@tonic-gate * Determine whether the supplied group id is a member of the group 4220Sstevel@tonic-gate * described by the supplied credentials. 4230Sstevel@tonic-gate */ 4240Sstevel@tonic-gate int 4250Sstevel@tonic-gate groupmember(gid_t gid, const cred_t *cr) 4260Sstevel@tonic-gate { 4270Sstevel@tonic-gate if (gid == cr->cr_gid) 4280Sstevel@tonic-gate return (1); 4290Sstevel@tonic-gate return (supgroupmember(gid, cr)); 4300Sstevel@tonic-gate } 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate /* 4330Sstevel@tonic-gate * As groupmember but only check against the supplemental groups. 4340Sstevel@tonic-gate */ 4350Sstevel@tonic-gate int 4360Sstevel@tonic-gate supgroupmember(gid_t gid, const cred_t *cr) 4370Sstevel@tonic-gate { 4380Sstevel@tonic-gate const gid_t *gp, *endgp; 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate endgp = &cr->cr_groups[cr->cr_ngroups]; 4410Sstevel@tonic-gate for (gp = cr->cr_groups; gp < endgp; gp++) 4420Sstevel@tonic-gate if (*gp == gid) 4430Sstevel@tonic-gate return (1); 4440Sstevel@tonic-gate return (0); 4450Sstevel@tonic-gate } 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate /* 4480Sstevel@tonic-gate * This function is called to check whether the credentials set 4490Sstevel@tonic-gate * "scrp" has permission to act on credentials set "tcrp". It enforces the 4500Sstevel@tonic-gate * permission requirements needed to send a signal to a process. 4510Sstevel@tonic-gate * The same requirements are imposed by other system calls, however. 4520Sstevel@tonic-gate * 4530Sstevel@tonic-gate * The rules are: 4540Sstevel@tonic-gate * (1) if the credentials are the same, the check succeeds 4550Sstevel@tonic-gate * (2) if the zone ids don't match, and scrp is not in the global zone or 4560Sstevel@tonic-gate * does not have the PRIV_PROC_ZONE privilege, the check fails 4570Sstevel@tonic-gate * (3) if the real or effective user id of scrp matches the real or saved 4580Sstevel@tonic-gate * user id of tcrp or scrp has the PRIV_PROC_OWNER privilege, the check 4590Sstevel@tonic-gate * succeeds 4600Sstevel@tonic-gate * (4) otherwise, the check fails 4610Sstevel@tonic-gate */ 4620Sstevel@tonic-gate int 4630Sstevel@tonic-gate hasprocperm(const cred_t *tcrp, const cred_t *scrp) 4640Sstevel@tonic-gate { 4650Sstevel@tonic-gate if (scrp == tcrp) 4660Sstevel@tonic-gate return (1); 4670Sstevel@tonic-gate if (scrp->cr_zone != tcrp->cr_zone && 4680Sstevel@tonic-gate (scrp->cr_zone != global_zone || 4690Sstevel@tonic-gate secpolicy_proc_zone(scrp) != 0)) 4700Sstevel@tonic-gate return (0); 4710Sstevel@tonic-gate if (scrp->cr_uid == tcrp->cr_ruid || 4720Sstevel@tonic-gate scrp->cr_ruid == tcrp->cr_ruid || 4730Sstevel@tonic-gate scrp->cr_uid == tcrp->cr_suid || 4740Sstevel@tonic-gate scrp->cr_ruid == tcrp->cr_suid || 4750Sstevel@tonic-gate !PRIV_POLICY(scrp, PRIV_PROC_OWNER, B_FALSE, EPERM, "hasprocperm")) 4760Sstevel@tonic-gate return (1); 4770Sstevel@tonic-gate return (0); 4780Sstevel@tonic-gate } 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate /* 4810Sstevel@tonic-gate * This interface replaces hasprocperm; it works like hasprocperm but 4820Sstevel@tonic-gate * additionally returns success if the proc_t's match 4830Sstevel@tonic-gate * It is the preferred interface for most uses. 4840Sstevel@tonic-gate * And it will acquire pcrlock itself, so it assert's that it shouldn't 4850Sstevel@tonic-gate * be held. 4860Sstevel@tonic-gate */ 4870Sstevel@tonic-gate int 4880Sstevel@tonic-gate prochasprocperm(proc_t *tp, proc_t *sp, const cred_t *scrp) 4890Sstevel@tonic-gate { 4900Sstevel@tonic-gate int rets; 4910Sstevel@tonic-gate cred_t *tcrp; 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&tp->p_crlock)); 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate if (tp == sp) 4960Sstevel@tonic-gate return (1); 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate if (tp->p_sessp != sp->p_sessp && secpolicy_basic_proc(scrp) != 0) 4990Sstevel@tonic-gate return (0); 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate mutex_enter(&tp->p_crlock); 5020Sstevel@tonic-gate tcrp = tp->p_cred; 5030Sstevel@tonic-gate rets = hasprocperm(tcrp, scrp); 5040Sstevel@tonic-gate mutex_exit(&tp->p_crlock); 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate return (rets); 5070Sstevel@tonic-gate } 5080Sstevel@tonic-gate 5090Sstevel@tonic-gate /* 5100Sstevel@tonic-gate * This routine is used to compare two credentials to determine if 5110Sstevel@tonic-gate * they refer to the same "user". If the pointers are equal, then 5120Sstevel@tonic-gate * they must refer to the same user. Otherwise, the contents of 5130Sstevel@tonic-gate * the credentials are compared to see whether they are equivalent. 5140Sstevel@tonic-gate * 5150Sstevel@tonic-gate * This routine returns 0 if the credentials refer to the same user, 5160Sstevel@tonic-gate * 1 if they do not. 5170Sstevel@tonic-gate */ 5180Sstevel@tonic-gate int 5190Sstevel@tonic-gate crcmp(const cred_t *cr1, const cred_t *cr2) 5200Sstevel@tonic-gate { 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate if (cr1 == cr2) 5230Sstevel@tonic-gate return (0); 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate if (cr1->cr_uid == cr2->cr_uid && 5260Sstevel@tonic-gate cr1->cr_gid == cr2->cr_gid && 5270Sstevel@tonic-gate cr1->cr_ruid == cr2->cr_ruid && 5280Sstevel@tonic-gate cr1->cr_rgid == cr2->cr_rgid && 5290Sstevel@tonic-gate cr1->cr_ngroups == cr2->cr_ngroups && 5300Sstevel@tonic-gate cr1->cr_zone == cr2->cr_zone && 5310Sstevel@tonic-gate bcmp(cr1->cr_groups, cr2->cr_groups, 532*4520Snw141292 cr1->cr_ngroups * sizeof (gid_t)) == 0) { 5330Sstevel@tonic-gate return (!priv_isequalset(&CR_OEPRIV(cr1), &CR_OEPRIV(cr2))); 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate return (1); 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate /* 5390Sstevel@tonic-gate * Read access functions to cred_t. 5400Sstevel@tonic-gate */ 5410Sstevel@tonic-gate uid_t 5420Sstevel@tonic-gate crgetuid(const cred_t *cr) 5430Sstevel@tonic-gate { 5440Sstevel@tonic-gate return (cr->cr_uid); 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate uid_t 5480Sstevel@tonic-gate crgetruid(const cred_t *cr) 5490Sstevel@tonic-gate { 5500Sstevel@tonic-gate return (cr->cr_ruid); 5510Sstevel@tonic-gate } 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate uid_t 5540Sstevel@tonic-gate crgetsuid(const cred_t *cr) 5550Sstevel@tonic-gate { 5560Sstevel@tonic-gate return (cr->cr_suid); 5570Sstevel@tonic-gate } 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate gid_t 5600Sstevel@tonic-gate crgetgid(const cred_t *cr) 5610Sstevel@tonic-gate { 5620Sstevel@tonic-gate return (cr->cr_gid); 5630Sstevel@tonic-gate } 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate gid_t 5660Sstevel@tonic-gate crgetrgid(const cred_t *cr) 5670Sstevel@tonic-gate { 5680Sstevel@tonic-gate return (cr->cr_rgid); 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate gid_t 5720Sstevel@tonic-gate crgetsgid(const cred_t *cr) 5730Sstevel@tonic-gate { 5740Sstevel@tonic-gate return (cr->cr_sgid); 5750Sstevel@tonic-gate } 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate const auditinfo_addr_t * 5780Sstevel@tonic-gate crgetauinfo(const cred_t *cr) 5790Sstevel@tonic-gate { 5800Sstevel@tonic-gate return ((const auditinfo_addr_t *)CR_AUINFO(cr)); 5810Sstevel@tonic-gate } 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate auditinfo_addr_t * 5840Sstevel@tonic-gate crgetauinfo_modifiable(cred_t *cr) 5850Sstevel@tonic-gate { 5860Sstevel@tonic-gate return (CR_AUINFO(cr)); 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate zoneid_t 5900Sstevel@tonic-gate crgetzoneid(const cred_t *cr) 5910Sstevel@tonic-gate { 5921676Sjpk return (cr->cr_zone == NULL ? 5931676Sjpk (cr->cr_uid == -1 ? (zoneid_t)-1 : GLOBAL_ZONEID) : 5941676Sjpk cr->cr_zone->zone_id); 5950Sstevel@tonic-gate } 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate projid_t 5980Sstevel@tonic-gate crgetprojid(const cred_t *cr) 5990Sstevel@tonic-gate { 6000Sstevel@tonic-gate return (cr->cr_projid); 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate 6031676Sjpk zone_t * 6041676Sjpk crgetzone(const cred_t *cr) 6051676Sjpk { 6061676Sjpk return (cr->cr_zone); 6071676Sjpk } 6081676Sjpk 6091676Sjpk struct ts_label_s * 6101676Sjpk crgetlabel(const cred_t *cr) 6111676Sjpk { 6121676Sjpk return (cr->cr_label ? 6131676Sjpk cr->cr_label : 6141676Sjpk (cr->cr_zone ? cr->cr_zone->zone_slabel : NULL)); 6151676Sjpk } 6161676Sjpk 6171676Sjpk boolean_t 6181676Sjpk crisremote(const cred_t *cr) 6191676Sjpk { 6201676Sjpk return (REMOTE_PEER_CRED(cr)); 6211676Sjpk } 6221676Sjpk 6234321Scasper #define BADUID(x) ((x) != -1 && !VALID_UID(x)) 6244321Scasper #define BADGID(x) ((x) != -1 && !VALID_GID(x)) 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate int 6270Sstevel@tonic-gate crsetresuid(cred_t *cr, uid_t r, uid_t e, uid_t s) 6280Sstevel@tonic-gate { 6290Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 6300Sstevel@tonic-gate 6314321Scasper if (BADUID(r) || BADUID(e) || BADUID(s)) 6320Sstevel@tonic-gate return (-1); 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate if (r != -1) 6350Sstevel@tonic-gate cr->cr_ruid = r; 6360Sstevel@tonic-gate if (e != -1) 6370Sstevel@tonic-gate cr->cr_uid = e; 6380Sstevel@tonic-gate if (s != -1) 6390Sstevel@tonic-gate cr->cr_suid = s; 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate return (0); 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate int 6450Sstevel@tonic-gate crsetresgid(cred_t *cr, gid_t r, gid_t e, gid_t s) 6460Sstevel@tonic-gate { 6470Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 6480Sstevel@tonic-gate 6494321Scasper if (BADGID(r) || BADGID(e) || BADGID(s)) 6500Sstevel@tonic-gate return (-1); 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate if (r != -1) 6530Sstevel@tonic-gate cr->cr_rgid = r; 6540Sstevel@tonic-gate if (e != -1) 6550Sstevel@tonic-gate cr->cr_gid = e; 6560Sstevel@tonic-gate if (s != -1) 6570Sstevel@tonic-gate cr->cr_sgid = s; 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate return (0); 6600Sstevel@tonic-gate } 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate int 6630Sstevel@tonic-gate crsetugid(cred_t *cr, uid_t uid, gid_t gid) 6640Sstevel@tonic-gate { 6650Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 6660Sstevel@tonic-gate 6674321Scasper if (!VALID_UID(uid) || !VALID_GID(gid)) 6680Sstevel@tonic-gate return (-1); 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate cr->cr_uid = cr->cr_ruid = cr->cr_suid = uid; 6710Sstevel@tonic-gate cr->cr_gid = cr->cr_rgid = cr->cr_sgid = gid; 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate return (0); 6740Sstevel@tonic-gate } 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate int 6770Sstevel@tonic-gate crsetgroups(cred_t *cr, int n, gid_t *grp) 6780Sstevel@tonic-gate { 6790Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate if (n > ngroups_max || n < 0) 6820Sstevel@tonic-gate return (-1); 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate cr->cr_ngroups = n; 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate if (n > 0) 6870Sstevel@tonic-gate bcopy(grp, cr->cr_groups, n * sizeof (gid_t)); 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate return (0); 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate void 6930Sstevel@tonic-gate crsetprojid(cred_t *cr, projid_t projid) 6940Sstevel@tonic-gate { 6950Sstevel@tonic-gate ASSERT(projid >= 0 && projid <= MAXPROJID); 6960Sstevel@tonic-gate cr->cr_projid = projid; 6970Sstevel@tonic-gate } 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate /* 7000Sstevel@tonic-gate * This routine returns the pointer to the first element of the cr_groups 7010Sstevel@tonic-gate * array. It can move around in an implementation defined way. 7020Sstevel@tonic-gate */ 7030Sstevel@tonic-gate const gid_t * 7040Sstevel@tonic-gate crgetgroups(const cred_t *cr) 7050Sstevel@tonic-gate { 7060Sstevel@tonic-gate return (cr->cr_groups); 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate 7090Sstevel@tonic-gate int 7100Sstevel@tonic-gate crgetngroups(const cred_t *cr) 7110Sstevel@tonic-gate { 7120Sstevel@tonic-gate return (cr->cr_ngroups); 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate void 7160Sstevel@tonic-gate cred2prcred(const cred_t *cr, prcred_t *pcrp) 7170Sstevel@tonic-gate { 7180Sstevel@tonic-gate pcrp->pr_euid = cr->cr_uid; 7190Sstevel@tonic-gate pcrp->pr_ruid = cr->cr_ruid; 7200Sstevel@tonic-gate pcrp->pr_suid = cr->cr_suid; 7210Sstevel@tonic-gate pcrp->pr_egid = cr->cr_gid; 7220Sstevel@tonic-gate pcrp->pr_rgid = cr->cr_rgid; 7230Sstevel@tonic-gate pcrp->pr_sgid = cr->cr_sgid; 7240Sstevel@tonic-gate pcrp->pr_ngroups = MIN(cr->cr_ngroups, (uint_t)ngroups_max); 7250Sstevel@tonic-gate pcrp->pr_groups[0] = 0; /* in case ngroups == 0 */ 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate if (pcrp->pr_ngroups != 0) 7280Sstevel@tonic-gate bcopy(cr->cr_groups, pcrp->pr_groups, 7290Sstevel@tonic-gate sizeof (gid_t) * cr->cr_ngroups); 7300Sstevel@tonic-gate } 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate static int 7331676Sjpk cred2ucaud(const cred_t *cr, auditinfo64_addr_t *ainfo, const cred_t *rcr) 7340Sstevel@tonic-gate { 7350Sstevel@tonic-gate auditinfo_addr_t *ai; 7360Sstevel@tonic-gate au_tid_addr_t tid; 7370Sstevel@tonic-gate 7381676Sjpk if (secpolicy_audit_getattr(rcr) != 0) 7390Sstevel@tonic-gate return (-1); 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate ai = CR_AUINFO(cr); /* caller makes sure this is non-NULL */ 7420Sstevel@tonic-gate tid = ai->ai_termid; 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate ainfo->ai_auid = ai->ai_auid; 7450Sstevel@tonic-gate ainfo->ai_mask = ai->ai_mask; 7460Sstevel@tonic-gate ainfo->ai_asid = ai->ai_asid; 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate ainfo->ai_termid.at_type = tid.at_type; 7490Sstevel@tonic-gate bcopy(&tid.at_addr, &ainfo->ai_termid.at_addr, 4 * sizeof (uint_t)); 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate ainfo->ai_termid.at_port.at_major = (uint32_t)getmajor(tid.at_port); 7520Sstevel@tonic-gate ainfo->ai_termid.at_port.at_minor = (uint32_t)getminor(tid.at_port); 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate return (0); 7550Sstevel@tonic-gate } 7560Sstevel@tonic-gate 7571676Sjpk void 7581676Sjpk cred2uclabel(const cred_t *cr, bslabel_t *labelp) 7591676Sjpk { 7601676Sjpk ts_label_t *tslp; 7611676Sjpk 7621676Sjpk if ((tslp = crgetlabel(cr)) != NULL) 7631676Sjpk bcopy(&tslp->tsl_label, labelp, sizeof (bslabel_t)); 7641676Sjpk } 7651676Sjpk 7660Sstevel@tonic-gate /* 7670Sstevel@tonic-gate * Convert a credential into a "ucred". Allow the caller to specify 7680Sstevel@tonic-gate * and aligned buffer, e.g., in an mblk, so we don't have to allocate 7690Sstevel@tonic-gate * memory and copy it twice. 7701676Sjpk * 7711676Sjpk * This function may call cred2ucaud(), which calls CRED(). Since this 7721676Sjpk * can be called from an interrupt thread, receiver's cred (rcr) is needed 7731676Sjpk * to determine whether audit info should be included. 7740Sstevel@tonic-gate */ 7750Sstevel@tonic-gate struct ucred_s * 7761676Sjpk cred2ucred(const cred_t *cr, pid_t pid, void *buf, const cred_t *rcr) 7770Sstevel@tonic-gate { 7780Sstevel@tonic-gate struct ucred_s *uc; 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate /* The structure isn't always completely filled in, so zero it */ 7810Sstevel@tonic-gate if (buf == NULL) { 7820Sstevel@tonic-gate uc = kmem_zalloc(ucredsize, KM_SLEEP); 7830Sstevel@tonic-gate } else { 7840Sstevel@tonic-gate bzero(buf, ucredsize); 7850Sstevel@tonic-gate uc = buf; 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate uc->uc_size = ucredsize; 7880Sstevel@tonic-gate uc->uc_credoff = UCRED_CRED_OFF; 7890Sstevel@tonic-gate uc->uc_privoff = UCRED_PRIV_OFF; 7900Sstevel@tonic-gate uc->uc_audoff = UCRED_AUD_OFF; 7911676Sjpk uc->uc_labeloff = UCRED_LABEL_OFF; 7920Sstevel@tonic-gate uc->uc_pid = pid; 7930Sstevel@tonic-gate uc->uc_projid = cr->cr_projid; 7940Sstevel@tonic-gate uc->uc_zoneid = crgetzoneid(cr); 7950Sstevel@tonic-gate 7961676Sjpk /* 7971676Sjpk * Note that cred2uclabel() call should not be factored out 7981676Sjpk * to the bottom of the if-else. UCXXX() macros depend on 7991676Sjpk * uc_xxxoff values to work correctly. 8001676Sjpk */ 8011676Sjpk if (REMOTE_PEER_CRED(cr)) { 8021676Sjpk /* 8031676Sjpk * other than label, the rest of cred info about a 8041676Sjpk * remote peer isn't available. 8051676Sjpk */ 8061676Sjpk cred2uclabel(cr, UCLABEL(uc)); 8071676Sjpk uc->uc_credoff = 0; 8081676Sjpk uc->uc_privoff = 0; 8090Sstevel@tonic-gate uc->uc_audoff = 0; 8101676Sjpk } else { 8111676Sjpk cred2prcred(cr, UCCRED(uc)); 8121676Sjpk cred2prpriv(cr, UCPRIV(uc)); 8131676Sjpk if (audoff == 0 || cred2ucaud(cr, UCAUD(uc), rcr) != 0) 8141676Sjpk uc->uc_audoff = 0; 8151676Sjpk cred2uclabel(cr, UCLABEL(uc)); 8161676Sjpk } 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate return (uc); 8190Sstevel@tonic-gate } 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate /* 8220Sstevel@tonic-gate * Get the "ucred" of a process. 8230Sstevel@tonic-gate */ 8240Sstevel@tonic-gate struct ucred_s * 8250Sstevel@tonic-gate pgetucred(proc_t *p) 8260Sstevel@tonic-gate { 8270Sstevel@tonic-gate cred_t *cr; 8280Sstevel@tonic-gate struct ucred_s *uc; 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate mutex_enter(&p->p_crlock); 8310Sstevel@tonic-gate cr = p->p_cred; 8320Sstevel@tonic-gate crhold(cr); 8330Sstevel@tonic-gate mutex_exit(&p->p_crlock); 8340Sstevel@tonic-gate 8351676Sjpk uc = cred2ucred(cr, p->p_pid, NULL, CRED()); 8360Sstevel@tonic-gate crfree(cr); 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate return (uc); 8390Sstevel@tonic-gate } 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate /* 8420Sstevel@tonic-gate * If the reply status is NFSERR_EACCES, it may be because we are 8430Sstevel@tonic-gate * root (no root net access). Check the real uid, if it isn't root 8440Sstevel@tonic-gate * make that the uid instead and retry the call. 8450Sstevel@tonic-gate * Private interface for NFS. 8460Sstevel@tonic-gate */ 8470Sstevel@tonic-gate cred_t * 8480Sstevel@tonic-gate crnetadjust(cred_t *cr) 8490Sstevel@tonic-gate { 8500Sstevel@tonic-gate if (cr->cr_uid == 0 && cr->cr_ruid != 0) { 8510Sstevel@tonic-gate cr = crdup(cr); 8520Sstevel@tonic-gate cr->cr_uid = cr->cr_ruid; 8530Sstevel@tonic-gate return (cr); 8540Sstevel@tonic-gate } 8550Sstevel@tonic-gate return (NULL); 8560Sstevel@tonic-gate } 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate /* 8590Sstevel@tonic-gate * The reference count is of interest when you want to check 8600Sstevel@tonic-gate * whether it is ok to modify the credential in place. 8610Sstevel@tonic-gate */ 8620Sstevel@tonic-gate uint_t 8630Sstevel@tonic-gate crgetref(const cred_t *cr) 8640Sstevel@tonic-gate { 8650Sstevel@tonic-gate return (cr->cr_ref); 8660Sstevel@tonic-gate } 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate static int 8690Sstevel@tonic-gate get_c2audit_load(void) 8700Sstevel@tonic-gate { 8710Sstevel@tonic-gate static int gotit = 0; 8720Sstevel@tonic-gate static int c2audit_load; 8730Sstevel@tonic-gate u_longlong_t audit_load_val; 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate if (gotit) 8760Sstevel@tonic-gate return (c2audit_load); 8770Sstevel@tonic-gate audit_load_val = 0; /* set default value once */ 8780Sstevel@tonic-gate (void) mod_sysvar("c2audit", "audit_load", &audit_load_val); 8790Sstevel@tonic-gate c2audit_load = (int)audit_load_val; 8800Sstevel@tonic-gate gotit++; 8810Sstevel@tonic-gate return (c2audit_load); 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate int 8850Sstevel@tonic-gate get_audit_ucrsize(void) 8860Sstevel@tonic-gate { 8870Sstevel@tonic-gate return (get_c2audit_load() ? sizeof (auditinfo64_addr_t) : 0); 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate /* 8910Sstevel@tonic-gate * Set zone pointer in credential to indicated value. First adds a 8920Sstevel@tonic-gate * hold for the new zone, then drops the hold on previous zone (if any). 8930Sstevel@tonic-gate * This is done in this order in case the old and new zones are the 8940Sstevel@tonic-gate * same. 8950Sstevel@tonic-gate */ 8960Sstevel@tonic-gate void 8970Sstevel@tonic-gate crsetzone(cred_t *cr, zone_t *zptr) 8980Sstevel@tonic-gate { 8990Sstevel@tonic-gate zone_t *oldzptr = cr->cr_zone; 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate ASSERT(cr != kcred); 9020Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 9030Sstevel@tonic-gate cr->cr_zone = zptr; 9040Sstevel@tonic-gate zone_cred_hold(zptr); 9050Sstevel@tonic-gate if (oldzptr) 9060Sstevel@tonic-gate zone_cred_rele(oldzptr); 9070Sstevel@tonic-gate } 9081676Sjpk 9091676Sjpk /* 9101676Sjpk * Create a new cred based on the supplied label 9111676Sjpk */ 9121676Sjpk cred_t * 9131676Sjpk newcred_from_bslabel(bslabel_t *blabel, uint32_t doi, int flags) 9141676Sjpk { 9151676Sjpk ts_label_t *lbl = labelalloc(blabel, doi, flags); 9161676Sjpk cred_t *cr = NULL; 9171676Sjpk 9181676Sjpk if (lbl != NULL) { 9191676Sjpk if ((cr = kmem_cache_alloc(cred_cache, flags)) != NULL) { 9201676Sjpk bcopy(dummycr, cr, crsize); 9211676Sjpk cr->cr_label = lbl; 9221676Sjpk } else { 9231676Sjpk label_rele(lbl); 9241676Sjpk } 9251676Sjpk } 9261676Sjpk 9271676Sjpk return (cr); 9281676Sjpk } 9291676Sjpk 9301676Sjpk /* 9311676Sjpk * Derive a new cred from the existing cred, but with a different label. 9321676Sjpk * To be used when a cred is being shared, but the label needs to be changed 9331676Sjpk * by a caller without affecting other users 9341676Sjpk */ 9351676Sjpk cred_t * 9361676Sjpk copycred_from_bslabel(cred_t *cr, bslabel_t *blabel, uint32_t doi, int flags) 9371676Sjpk { 9381676Sjpk ts_label_t *lbl = labelalloc(blabel, doi, flags); 9391676Sjpk cred_t *newcr = NULL; 9401676Sjpk 9411676Sjpk if (lbl != NULL) { 9421676Sjpk if ((newcr = kmem_cache_alloc(cred_cache, flags)) != NULL) { 9431676Sjpk bcopy(cr, newcr, crsize); 9441676Sjpk if (newcr->cr_zone) 9451676Sjpk zone_cred_hold(newcr->cr_zone); 9461676Sjpk newcr->cr_label = lbl; 9471676Sjpk newcr->cr_ref = 1; 9481676Sjpk } else { 9491676Sjpk label_rele(lbl); 9501676Sjpk } 9511676Sjpk } 9521676Sjpk 9531676Sjpk return (newcr); 9541676Sjpk } 9551676Sjpk 9561676Sjpk /* 9571676Sjpk * This function returns a pointer to the kcred-equivalent in the current zone. 9581676Sjpk */ 9591676Sjpk cred_t * 9601676Sjpk zone_kcred(void) 9611676Sjpk { 9621676Sjpk zone_t *zone; 9631676Sjpk 9641676Sjpk if ((zone = CRED()->cr_zone) != NULL) 9651676Sjpk return (zone->zone_kcred); 9661676Sjpk else 9671676Sjpk return (kcred); 9681676Sjpk } 9694321Scasper 9704321Scasper boolean_t 9714321Scasper valid_ephemeral_uid(uid_t id) 9724321Scasper { 9734321Scasper membar_consumer(); 974*4520Snw141292 return (id < IDMAP_WK__MAX_UID || 975*4520Snw141292 (id > ephemeral_data.min_uid && id <= ephemeral_data.last_uid)); 9764321Scasper } 9774321Scasper 9784321Scasper boolean_t 9794321Scasper valid_ephemeral_gid(gid_t id) 9804321Scasper { 9814321Scasper membar_consumer(); 982*4520Snw141292 return (id < IDMAP_WK__MAX_GID || 983*4520Snw141292 (id > ephemeral_data.min_gid && id <= ephemeral_data.last_gid)); 9844321Scasper } 9854321Scasper 9864321Scasper int 9874321Scasper eph_uid_alloc(int flags, uid_t *start, int count) 9884321Scasper { 9894321Scasper mutex_enter(&ephemeral_data.eph_lock); 9904321Scasper 9914321Scasper /* Test for unsigned integer wrap around */ 9924321Scasper if (ephemeral_data.last_uid + count < ephemeral_data.last_uid) { 9934321Scasper mutex_exit(&ephemeral_data.eph_lock); 9944321Scasper return (-1); 9954321Scasper } 9964321Scasper 9974321Scasper /* first call or idmap crashed and state corrupted */ 9984321Scasper if (flags != 0) 9994321Scasper ephemeral_data.min_uid = ephemeral_data.last_uid; 10004321Scasper 10014321Scasper hasephids = B_TRUE; 10024321Scasper *start = ephemeral_data.last_uid + 1; 10034321Scasper atomic_add_32(&ephemeral_data.last_uid, count); 10044321Scasper mutex_exit(&ephemeral_data.eph_lock); 10054321Scasper return (0); 10064321Scasper } 10074321Scasper 10084321Scasper int 10094321Scasper eph_gid_alloc(int flags, gid_t *start, int count) 10104321Scasper { 10114321Scasper mutex_enter(&ephemeral_data.eph_lock); 10124321Scasper 10134321Scasper /* Test for unsigned integer wrap around */ 10144321Scasper if (ephemeral_data.last_gid + count < ephemeral_data.last_gid) { 10154321Scasper mutex_exit(&ephemeral_data.eph_lock); 10164321Scasper return (-1); 10174321Scasper } 10184321Scasper 10194321Scasper /* first call or idmap crashed and state corrupted */ 10204321Scasper if (flags != 0) 10214321Scasper ephemeral_data.min_gid = ephemeral_data.last_gid; 10224321Scasper 10234321Scasper hasephids = B_TRUE; 10244321Scasper *start = ephemeral_data.last_gid + 1; 10254321Scasper atomic_add_32(&ephemeral_data.last_gid, count); 10264321Scasper mutex_exit(&ephemeral_data.eph_lock); 10274321Scasper return (0); 10284321Scasper } 10294321Scasper 10304321Scasper /* 10314321Scasper * If the credential contains any ephemeral IDs, map the credential 10324321Scasper * to nobody. 10334321Scasper */ 10344321Scasper cred_t * 10354321Scasper crgetmapped(const cred_t *cr) 10364321Scasper { 10374406Scasper /* 10384406Scasper * Someone incorrectly passed a NULL cred to a vnode operation 10394406Scasper * either on purpose or by calling CRED() in interrupt context. 10404406Scasper */ 10414406Scasper if (cr == NULL) 10424406Scasper return (NULL); 10434406Scasper 10444321Scasper if (cr->cr_ksid != NULL) { 10454321Scasper int i; 10464321Scasper 10474321Scasper for (i = 0; i < KSID_COUNT; i++) 10484321Scasper if (cr->cr_ksid->kr_sidx[i].ks_id > MAXUID) 10494321Scasper return (ephemeral_data.nobody); 10504321Scasper if (cr->cr_ksid->kr_sidlist != NULL && 10514321Scasper cr->cr_ksid->kr_sidlist->ksl_neid > 0) { 10524321Scasper return (ephemeral_data.nobody); 10534321Scasper } 10544321Scasper } 10554321Scasper 10564321Scasper return ((cred_t *)cr); 10574321Scasper } 10584321Scasper 10594321Scasper /* index should be in range for a ksidindex_t */ 10604321Scasper void 10614321Scasper crsetsid(cred_t *cr, ksid_t *ksp, int index) 10624321Scasper { 10634321Scasper ASSERT(cr->cr_ref <= 2); 10644321Scasper ASSERT(index >= 0 && index < KSID_COUNT); 10654321Scasper if (cr->cr_ksid == NULL && ksp == NULL) 10664321Scasper return; 10674321Scasper cr->cr_ksid = kcrsid_setsid(cr->cr_ksid, ksp, index); 10684321Scasper } 10694321Scasper 10704321Scasper void 10714321Scasper crsetsidlist(cred_t *cr, ksidlist_t *ksl) 10724321Scasper { 10734321Scasper ASSERT(cr->cr_ref <= 2); 10744321Scasper if (cr->cr_ksid == NULL && ksl == NULL) 10754321Scasper return; 10764321Scasper cr->cr_ksid = kcrsid_setsidlist(cr->cr_ksid, ksl); 10774321Scasper } 10784321Scasper 10794321Scasper ksid_t * 10804321Scasper crgetsid(const cred_t *cr, int i) 10814321Scasper { 10824321Scasper ASSERT(i >= 0 && i < KSID_COUNT); 10834321Scasper if (cr->cr_ksid != NULL && cr->cr_ksid->kr_sidx[i].ks_domain) 10844321Scasper return ((ksid_t *)&cr->cr_ksid->kr_sidx[i]); 10854321Scasper return (NULL); 10864321Scasper } 10874321Scasper 10884321Scasper ksidlist_t * 10894321Scasper crgetsidlist(const cred_t *cr) 10904321Scasper { 10914321Scasper if (cr->cr_ksid != NULL && cr->cr_ksid->kr_sidlist != NULL) 10924321Scasper return ((ksidlist_t *)&cr->cr_ksid->kr_sidlist); 10934321Scasper return (NULL); 10944321Scasper } 1095