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 /* 22*5771Sjp151216 * Copyright 2008 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> 595331Samw #include <sys/door.h> 600Sstevel@tonic-gate #include <c2/audit.h> 610Sstevel@tonic-gate #include <sys/zone.h> 621676Sjpk #include <sys/tsol/label.h> 634321Scasper #include <sys/sid.h> 644520Snw141292 #include <sys/idmap.h> 655331Samw #include <sys/varargs.h> 664321Scasper 67*5771Sjp151216 68*5771Sjp151216 /* Ephemeral IDs Zones specific data */ 69*5771Sjp151216 typedef struct ephemeral_zsd { 70*5771Sjp151216 uid_t min_uid; 71*5771Sjp151216 uid_t last_uid; 72*5771Sjp151216 gid_t min_gid; 73*5771Sjp151216 gid_t last_gid; 744321Scasper kmutex_t eph_lock; 75*5771Sjp151216 cred_t *eph_nobody; 76*5771Sjp151216 } ephemeral_zsd_t; 77*5771Sjp151216 78*5771Sjp151216 79*5771Sjp151216 static kmutex_t ephemeral_zone_mutex; 80*5771Sjp151216 static zone_key_t ephemeral_zone_key; 810Sstevel@tonic-gate 820Sstevel@tonic-gate static struct kmem_cache *cred_cache; 83*5771Sjp151216 static size_t crsize = 0; 84*5771Sjp151216 static int audoff = 0; 85*5771Sjp151216 uint32_t ucredsize; 86*5771Sjp151216 cred_t *kcred; 87*5771Sjp151216 static cred_t *dummycr; 880Sstevel@tonic-gate 890Sstevel@tonic-gate int rstlink; /* link(2) restricted to files owned by user? */ 900Sstevel@tonic-gate 910Sstevel@tonic-gate static int get_c2audit_load(void); 920Sstevel@tonic-gate 930Sstevel@tonic-gate #define CR_AUINFO(c) (auditinfo_addr_t *)((audoff == 0) ? NULL : \ 940Sstevel@tonic-gate ((char *)(c)) + audoff) 950Sstevel@tonic-gate 961676Sjpk #define REMOTE_PEER_CRED(c) ((c)->cr_gid == -1) 970Sstevel@tonic-gate 984321Scasper 994321Scasper static boolean_t hasephids = B_FALSE; 1004321Scasper 101*5771Sjp151216 static ephemeral_zsd_t * 102*5771Sjp151216 get_ephemeral_zsd(zone_t *zone) 103*5771Sjp151216 { 104*5771Sjp151216 ephemeral_zsd_t *eph_zsd; 105*5771Sjp151216 106*5771Sjp151216 eph_zsd = zone_getspecific(ephemeral_zone_key, zone); 107*5771Sjp151216 if (eph_zsd != NULL) { 108*5771Sjp151216 return (eph_zsd); 109*5771Sjp151216 } 110*5771Sjp151216 111*5771Sjp151216 mutex_enter(&ephemeral_zone_mutex); 112*5771Sjp151216 eph_zsd = zone_getspecific(ephemeral_zone_key, zone); 113*5771Sjp151216 if (eph_zsd == NULL) { 114*5771Sjp151216 eph_zsd = kmem_zalloc(sizeof (ephemeral_zsd_t), KM_SLEEP); 115*5771Sjp151216 eph_zsd->min_uid = MAXUID; 116*5771Sjp151216 eph_zsd->last_uid = IDMAP_WK__MAX_UID; 117*5771Sjp151216 eph_zsd->min_gid = MAXUID; 118*5771Sjp151216 eph_zsd->last_gid = IDMAP_WK__MAX_GID; 119*5771Sjp151216 mutex_init(&eph_zsd->eph_lock, NULL, MUTEX_DEFAULT, NULL); 120*5771Sjp151216 121*5771Sjp151216 /* 122*5771Sjp151216 * nobody is used to map SID containing CRs. 123*5771Sjp151216 */ 124*5771Sjp151216 eph_zsd->eph_nobody = crdup(zone->zone_kcred); 125*5771Sjp151216 (void) crsetugid(eph_zsd->eph_nobody, UID_NOBODY, GID_NOBODY); 126*5771Sjp151216 CR_FLAGS(eph_zsd->eph_nobody) = 0; 127*5771Sjp151216 eph_zsd->eph_nobody->cr_zone = zone; 128*5771Sjp151216 129*5771Sjp151216 (void) zone_setspecific(ephemeral_zone_key, zone, eph_zsd); 130*5771Sjp151216 } 131*5771Sjp151216 mutex_exit(&ephemeral_zone_mutex); 132*5771Sjp151216 return (eph_zsd); 133*5771Sjp151216 } 134*5771Sjp151216 135*5771Sjp151216 /* 136*5771Sjp151216 * This function is called when a zone is destroyed 137*5771Sjp151216 */ 138*5771Sjp151216 static void 139*5771Sjp151216 /* ARGSUSED */ 140*5771Sjp151216 destroy_ephemeral_zsd(zoneid_t zone_id, void *arg) 141*5771Sjp151216 { 142*5771Sjp151216 ephemeral_zsd_t *eph_zsd = arg; 143*5771Sjp151216 if (eph_zsd != NULL) { 144*5771Sjp151216 mutex_destroy(&eph_zsd->eph_lock); 145*5771Sjp151216 crfree(eph_zsd->eph_nobody); 146*5771Sjp151216 kmem_free(eph_zsd, sizeof (ephemeral_zsd_t)); 147*5771Sjp151216 } 148*5771Sjp151216 } 149*5771Sjp151216 150*5771Sjp151216 151*5771Sjp151216 1524321Scasper /* 1530Sstevel@tonic-gate * Initialize credentials data structures. 1540Sstevel@tonic-gate */ 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate void 1570Sstevel@tonic-gate cred_init(void) 1580Sstevel@tonic-gate { 1590Sstevel@tonic-gate priv_init(); 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate crsize = sizeof (cred_t) + sizeof (gid_t) * (ngroups_max - 1); 1620Sstevel@tonic-gate /* 1630Sstevel@tonic-gate * Make sure it's word-aligned. 1640Sstevel@tonic-gate */ 1650Sstevel@tonic-gate crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1); 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate if (get_c2audit_load() > 0) { 1680Sstevel@tonic-gate #ifdef _LP64 1690Sstevel@tonic-gate /* assure audit context is 64-bit aligned */ 1700Sstevel@tonic-gate audoff = (crsize + 1710Sstevel@tonic-gate sizeof (int64_t) - 1) & ~(sizeof (int64_t) - 1); 1720Sstevel@tonic-gate #else /* _LP64 */ 1730Sstevel@tonic-gate audoff = crsize; 1740Sstevel@tonic-gate #endif /* _LP64 */ 1750Sstevel@tonic-gate crsize = audoff + sizeof (auditinfo_addr_t); 1760Sstevel@tonic-gate crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1); 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate cred_cache = kmem_cache_create("cred_cache", crsize, 0, 1804520Snw141292 NULL, NULL, NULL, NULL, NULL, 0); 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* 1831676Sjpk * dummycr is used to copy initial state for creds. 1841676Sjpk */ 1851676Sjpk dummycr = cralloc(); 1861676Sjpk bzero(dummycr, crsize); 1871676Sjpk dummycr->cr_ref = 1; 1884321Scasper dummycr->cr_uid = (uid_t)-1; 1894321Scasper dummycr->cr_gid = (gid_t)-1; 1904321Scasper dummycr->cr_ruid = (uid_t)-1; 1914321Scasper dummycr->cr_rgid = (gid_t)-1; 1924321Scasper dummycr->cr_suid = (uid_t)-1; 1934321Scasper dummycr->cr_sgid = (gid_t)-1; 1944321Scasper 1951676Sjpk 1961676Sjpk /* 1970Sstevel@tonic-gate * kcred is used by anything that needs all privileges; it's 1980Sstevel@tonic-gate * also the template used for crget as it has all the compatible 1990Sstevel@tonic-gate * sets filled in. 2000Sstevel@tonic-gate */ 2010Sstevel@tonic-gate kcred = cralloc(); 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate bzero(kcred, crsize); 2040Sstevel@tonic-gate kcred->cr_ref = 1; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate /* kcred is never freed, so we don't need zone_cred_hold here */ 2070Sstevel@tonic-gate kcred->cr_zone = &zone0; 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate priv_fillset(&CR_LPRIV(kcred)); 2100Sstevel@tonic-gate CR_IPRIV(kcred) = *priv_basic; 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate /* Not a basic privilege, if chown is not restricted add it to I0 */ 2130Sstevel@tonic-gate if (!rstchown) 2140Sstevel@tonic-gate priv_addset(&CR_IPRIV(kcred), PRIV_FILE_CHOWN_SELF); 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* Basic privilege, if link is restricted remove it from I0 */ 2170Sstevel@tonic-gate if (rstlink) 2180Sstevel@tonic-gate priv_delset(&CR_IPRIV(kcred), PRIV_FILE_LINK_ANY); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate CR_EPRIV(kcred) = CR_PPRIV(kcred) = CR_IPRIV(kcred); 2211676Sjpk 2221676Sjpk CR_FLAGS(kcred) = NET_MAC_AWARE; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /* 2250Sstevel@tonic-gate * Set up credentials of p0. 2260Sstevel@tonic-gate */ 2270Sstevel@tonic-gate ttoproc(curthread)->p_cred = kcred; 2280Sstevel@tonic-gate curthread->t_cred = kcred; 2290Sstevel@tonic-gate 230*5771Sjp151216 ucredsize = UCRED_SIZE; 2314321Scasper 232*5771Sjp151216 mutex_init(&ephemeral_zone_mutex, NULL, MUTEX_DEFAULT, NULL); 233*5771Sjp151216 zone_key_create(&ephemeral_zone_key, NULL, NULL, destroy_ephemeral_zsd); 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate /* 2370Sstevel@tonic-gate * Allocate (nearly) uninitialized cred_t. 2380Sstevel@tonic-gate */ 2390Sstevel@tonic-gate cred_t * 2400Sstevel@tonic-gate cralloc(void) 2410Sstevel@tonic-gate { 2420Sstevel@tonic-gate cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP); 2430Sstevel@tonic-gate cr->cr_ref = 1; /* So we can crfree() */ 2440Sstevel@tonic-gate cr->cr_zone = NULL; 2451676Sjpk cr->cr_label = NULL; 2464321Scasper cr->cr_ksid = NULL; 2474321Scasper return (cr); 2484321Scasper } 2494321Scasper 2504321Scasper /* 2514321Scasper * As cralloc but prepared for ksid change (if appropriate). 2524321Scasper */ 2534321Scasper cred_t * 2544321Scasper cralloc_ksid(void) 2554321Scasper { 2564321Scasper cred_t *cr = cralloc(); 2574321Scasper if (hasephids) 2584321Scasper cr->cr_ksid = kcrsid_alloc(); 2590Sstevel@tonic-gate return (cr); 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate /* 2630Sstevel@tonic-gate * Allocate a initialized cred structure and crhold() it. 2640Sstevel@tonic-gate * Initialized means: all ids 0, group count 0, L=Full, E=P=I=I0 2650Sstevel@tonic-gate */ 2660Sstevel@tonic-gate cred_t * 2670Sstevel@tonic-gate crget(void) 2680Sstevel@tonic-gate { 2690Sstevel@tonic-gate cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP); 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate bcopy(kcred, cr, crsize); 2720Sstevel@tonic-gate cr->cr_ref = 1; 2730Sstevel@tonic-gate zone_cred_hold(cr->cr_zone); 2741676Sjpk if (cr->cr_label) 2751676Sjpk label_hold(cr->cr_label); 2760Sstevel@tonic-gate return (cr); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate /* 2800Sstevel@tonic-gate * Broadcast the cred to all the threads in the process. 2810Sstevel@tonic-gate * The current thread's credentials can be set right away, but other 2820Sstevel@tonic-gate * threads must wait until the start of the next system call or trap. 2830Sstevel@tonic-gate * This avoids changing the cred in the middle of a system call. 2840Sstevel@tonic-gate * 2850Sstevel@tonic-gate * The cred has already been held for the process and the thread (2 holds), 2860Sstevel@tonic-gate * and p->p_cred set. 2870Sstevel@tonic-gate * 2880Sstevel@tonic-gate * p->p_crlock shouldn't be held here, since p_lock must be acquired. 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate void 2910Sstevel@tonic-gate crset(proc_t *p, cred_t *cr) 2920Sstevel@tonic-gate { 2930Sstevel@tonic-gate kthread_id_t t; 2940Sstevel@tonic-gate kthread_id_t first; 2950Sstevel@tonic-gate cred_t *oldcr; 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate ASSERT(p == curproc); /* assumes p_lwpcnt can't change */ 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate /* 3000Sstevel@tonic-gate * DTrace accesses t_cred in probe context. t_cred must always be 3010Sstevel@tonic-gate * either NULL, or point to a valid, allocated cred structure. 3020Sstevel@tonic-gate */ 3030Sstevel@tonic-gate t = curthread; 3040Sstevel@tonic-gate oldcr = t->t_cred; 3050Sstevel@tonic-gate t->t_cred = cr; /* the cred is held by caller for this thread */ 3060Sstevel@tonic-gate crfree(oldcr); /* free the old cred for the thread */ 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate /* 3090Sstevel@tonic-gate * Broadcast to other threads, if any. 3100Sstevel@tonic-gate */ 3110Sstevel@tonic-gate if (p->p_lwpcnt > 1) { 3120Sstevel@tonic-gate mutex_enter(&p->p_lock); /* to keep thread list safe */ 3130Sstevel@tonic-gate first = curthread; 3140Sstevel@tonic-gate for (t = first->t_forw; t != first; t = t->t_forw) 3150Sstevel@tonic-gate t->t_pre_sys = 1; /* so syscall will get new cred */ 3160Sstevel@tonic-gate mutex_exit(&p->p_lock); 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate /* 3210Sstevel@tonic-gate * Put a hold on a cred structure. 3220Sstevel@tonic-gate */ 3230Sstevel@tonic-gate void 3240Sstevel@tonic-gate crhold(cred_t *cr) 3250Sstevel@tonic-gate { 3260Sstevel@tonic-gate atomic_add_32(&cr->cr_ref, 1); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate /* 3300Sstevel@tonic-gate * Release previous hold on a cred structure. Free it if refcnt == 0. 3311676Sjpk * If cred uses label different from zone label, free it. 3320Sstevel@tonic-gate */ 3330Sstevel@tonic-gate void 3340Sstevel@tonic-gate crfree(cred_t *cr) 3350Sstevel@tonic-gate { 3360Sstevel@tonic-gate if (atomic_add_32_nv(&cr->cr_ref, -1) == 0) { 3370Sstevel@tonic-gate ASSERT(cr != kcred); 3381676Sjpk if (cr->cr_label) 3391676Sjpk label_rele(cr->cr_label); 3400Sstevel@tonic-gate if (cr->cr_zone) 3410Sstevel@tonic-gate zone_cred_rele(cr->cr_zone); 3424321Scasper if (cr->cr_ksid) 3434321Scasper kcrsid_rele(cr->cr_ksid); 3440Sstevel@tonic-gate kmem_cache_free(cred_cache, cr); 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate /* 3490Sstevel@tonic-gate * Copy a cred structure to a new one and free the old one. 3500Sstevel@tonic-gate * The new cred will have two references. One for the calling process, 3510Sstevel@tonic-gate * and one for the thread. 3520Sstevel@tonic-gate */ 3530Sstevel@tonic-gate cred_t * 3540Sstevel@tonic-gate crcopy(cred_t *cr) 3550Sstevel@tonic-gate { 3560Sstevel@tonic-gate cred_t *newcr; 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate newcr = cralloc(); 3590Sstevel@tonic-gate bcopy(cr, newcr, crsize); 3600Sstevel@tonic-gate if (newcr->cr_zone) 3610Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 3621676Sjpk if (newcr->cr_label) 3631676Sjpk label_hold(cr->cr_label); 3644321Scasper if (newcr->cr_ksid) 3654321Scasper kcrsid_hold(cr->cr_ksid); 3660Sstevel@tonic-gate crfree(cr); 3670Sstevel@tonic-gate newcr->cr_ref = 2; /* caller gets two references */ 3680Sstevel@tonic-gate return (newcr); 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate /* 3720Sstevel@tonic-gate * Copy a cred structure to a new one and free the old one. 3730Sstevel@tonic-gate * The new cred will have two references. One for the calling process, 3740Sstevel@tonic-gate * and one for the thread. 3750Sstevel@tonic-gate * This variation on crcopy uses a pre-allocated structure for the 3760Sstevel@tonic-gate * "new" cred. 3770Sstevel@tonic-gate */ 3780Sstevel@tonic-gate void 3790Sstevel@tonic-gate crcopy_to(cred_t *oldcr, cred_t *newcr) 3800Sstevel@tonic-gate { 3814321Scasper credsid_t *nkcr = newcr->cr_ksid; 3824321Scasper 3830Sstevel@tonic-gate bcopy(oldcr, newcr, crsize); 3840Sstevel@tonic-gate if (newcr->cr_zone) 3850Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 3861676Sjpk if (newcr->cr_label) 3871676Sjpk label_hold(newcr->cr_label); 3884321Scasper if (nkcr) { 3894321Scasper newcr->cr_ksid = nkcr; 3904321Scasper kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid); 3914321Scasper } else if (newcr->cr_ksid) 3924321Scasper kcrsid_hold(newcr->cr_ksid); 3930Sstevel@tonic-gate crfree(oldcr); 3940Sstevel@tonic-gate newcr->cr_ref = 2; /* caller gets two references */ 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate /* 3980Sstevel@tonic-gate * Dup a cred struct to a new held one. 3990Sstevel@tonic-gate * The old cred is not freed. 4000Sstevel@tonic-gate */ 4010Sstevel@tonic-gate cred_t * 4020Sstevel@tonic-gate crdup(cred_t *cr) 4030Sstevel@tonic-gate { 4040Sstevel@tonic-gate cred_t *newcr; 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate newcr = cralloc(); 4070Sstevel@tonic-gate bcopy(cr, newcr, crsize); 4080Sstevel@tonic-gate if (newcr->cr_zone) 4090Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 4101676Sjpk if (newcr->cr_label) 4111676Sjpk label_hold(newcr->cr_label); 4124321Scasper if (newcr->cr_ksid) 4134321Scasper kcrsid_hold(newcr->cr_ksid); 4140Sstevel@tonic-gate newcr->cr_ref = 1; 4150Sstevel@tonic-gate return (newcr); 4160Sstevel@tonic-gate } 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate /* 4190Sstevel@tonic-gate * Dup a cred struct to a new held one. 4200Sstevel@tonic-gate * The old cred is not freed. 4210Sstevel@tonic-gate * This variation on crdup uses a pre-allocated structure for the 4220Sstevel@tonic-gate * "new" cred. 4230Sstevel@tonic-gate */ 4240Sstevel@tonic-gate void 4250Sstevel@tonic-gate crdup_to(cred_t *oldcr, cred_t *newcr) 4260Sstevel@tonic-gate { 4274321Scasper credsid_t *nkcr = newcr->cr_ksid; 4284321Scasper 4290Sstevel@tonic-gate bcopy(oldcr, newcr, crsize); 4300Sstevel@tonic-gate if (newcr->cr_zone) 4310Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 4321676Sjpk if (newcr->cr_label) 4331676Sjpk label_hold(newcr->cr_label); 4344321Scasper if (nkcr) { 4354321Scasper newcr->cr_ksid = nkcr; 4364321Scasper kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid); 4374321Scasper } else if (newcr->cr_ksid) 4384321Scasper kcrsid_hold(newcr->cr_ksid); 4390Sstevel@tonic-gate newcr->cr_ref = 1; 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate /* 4430Sstevel@tonic-gate * Return the (held) credentials for the current running process. 4440Sstevel@tonic-gate */ 4450Sstevel@tonic-gate cred_t * 4461676Sjpk crgetcred(void) 4470Sstevel@tonic-gate { 4480Sstevel@tonic-gate cred_t *cr; 4490Sstevel@tonic-gate proc_t *p; 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate p = ttoproc(curthread); 4520Sstevel@tonic-gate mutex_enter(&p->p_crlock); 4530Sstevel@tonic-gate crhold(cr = p->p_cred); 4540Sstevel@tonic-gate mutex_exit(&p->p_crlock); 4550Sstevel@tonic-gate return (cr); 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate /* 4590Sstevel@tonic-gate * Backward compatibility check for suser(). 4600Sstevel@tonic-gate * Accounting flag is now set in the policy functions; auditing is 4610Sstevel@tonic-gate * done through use of privilege in the audit trail. 4620Sstevel@tonic-gate */ 4630Sstevel@tonic-gate int 4640Sstevel@tonic-gate suser(cred_t *cr) 4650Sstevel@tonic-gate { 4660Sstevel@tonic-gate return (PRIV_POLICY(cr, PRIV_SYS_SUSER_COMPAT, B_FALSE, EPERM, NULL) 4670Sstevel@tonic-gate == 0); 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate /* 4710Sstevel@tonic-gate * Determine whether the supplied group id is a member of the group 4720Sstevel@tonic-gate * described by the supplied credentials. 4730Sstevel@tonic-gate */ 4740Sstevel@tonic-gate int 4750Sstevel@tonic-gate groupmember(gid_t gid, const cred_t *cr) 4760Sstevel@tonic-gate { 4770Sstevel@tonic-gate if (gid == cr->cr_gid) 4780Sstevel@tonic-gate return (1); 4790Sstevel@tonic-gate return (supgroupmember(gid, cr)); 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate /* 4830Sstevel@tonic-gate * As groupmember but only check against the supplemental groups. 4840Sstevel@tonic-gate */ 4850Sstevel@tonic-gate int 4860Sstevel@tonic-gate supgroupmember(gid_t gid, const cred_t *cr) 4870Sstevel@tonic-gate { 4880Sstevel@tonic-gate const gid_t *gp, *endgp; 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate endgp = &cr->cr_groups[cr->cr_ngroups]; 4910Sstevel@tonic-gate for (gp = cr->cr_groups; gp < endgp; gp++) 4920Sstevel@tonic-gate if (*gp == gid) 4930Sstevel@tonic-gate return (1); 4940Sstevel@tonic-gate return (0); 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate /* 4980Sstevel@tonic-gate * This function is called to check whether the credentials set 4990Sstevel@tonic-gate * "scrp" has permission to act on credentials set "tcrp". It enforces the 5000Sstevel@tonic-gate * permission requirements needed to send a signal to a process. 5010Sstevel@tonic-gate * The same requirements are imposed by other system calls, however. 5020Sstevel@tonic-gate * 5030Sstevel@tonic-gate * The rules are: 5040Sstevel@tonic-gate * (1) if the credentials are the same, the check succeeds 5050Sstevel@tonic-gate * (2) if the zone ids don't match, and scrp is not in the global zone or 5060Sstevel@tonic-gate * does not have the PRIV_PROC_ZONE privilege, the check fails 5070Sstevel@tonic-gate * (3) if the real or effective user id of scrp matches the real or saved 5080Sstevel@tonic-gate * user id of tcrp or scrp has the PRIV_PROC_OWNER privilege, the check 5090Sstevel@tonic-gate * succeeds 5100Sstevel@tonic-gate * (4) otherwise, the check fails 5110Sstevel@tonic-gate */ 5120Sstevel@tonic-gate int 5130Sstevel@tonic-gate hasprocperm(const cred_t *tcrp, const cred_t *scrp) 5140Sstevel@tonic-gate { 5150Sstevel@tonic-gate if (scrp == tcrp) 5160Sstevel@tonic-gate return (1); 5170Sstevel@tonic-gate if (scrp->cr_zone != tcrp->cr_zone && 5180Sstevel@tonic-gate (scrp->cr_zone != global_zone || 5190Sstevel@tonic-gate secpolicy_proc_zone(scrp) != 0)) 5200Sstevel@tonic-gate return (0); 5210Sstevel@tonic-gate if (scrp->cr_uid == tcrp->cr_ruid || 5220Sstevel@tonic-gate scrp->cr_ruid == tcrp->cr_ruid || 5230Sstevel@tonic-gate scrp->cr_uid == tcrp->cr_suid || 5240Sstevel@tonic-gate scrp->cr_ruid == tcrp->cr_suid || 5250Sstevel@tonic-gate !PRIV_POLICY(scrp, PRIV_PROC_OWNER, B_FALSE, EPERM, "hasprocperm")) 5260Sstevel@tonic-gate return (1); 5270Sstevel@tonic-gate return (0); 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate /* 5310Sstevel@tonic-gate * This interface replaces hasprocperm; it works like hasprocperm but 5320Sstevel@tonic-gate * additionally returns success if the proc_t's match 5330Sstevel@tonic-gate * It is the preferred interface for most uses. 5340Sstevel@tonic-gate * And it will acquire pcrlock itself, so it assert's that it shouldn't 5350Sstevel@tonic-gate * be held. 5360Sstevel@tonic-gate */ 5370Sstevel@tonic-gate int 5380Sstevel@tonic-gate prochasprocperm(proc_t *tp, proc_t *sp, const cred_t *scrp) 5390Sstevel@tonic-gate { 5400Sstevel@tonic-gate int rets; 5410Sstevel@tonic-gate cred_t *tcrp; 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&tp->p_crlock)); 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate if (tp == sp) 5460Sstevel@tonic-gate return (1); 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate if (tp->p_sessp != sp->p_sessp && secpolicy_basic_proc(scrp) != 0) 5490Sstevel@tonic-gate return (0); 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate mutex_enter(&tp->p_crlock); 5520Sstevel@tonic-gate tcrp = tp->p_cred; 5530Sstevel@tonic-gate rets = hasprocperm(tcrp, scrp); 5540Sstevel@tonic-gate mutex_exit(&tp->p_crlock); 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate return (rets); 5570Sstevel@tonic-gate } 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate /* 5600Sstevel@tonic-gate * This routine is used to compare two credentials to determine if 5610Sstevel@tonic-gate * they refer to the same "user". If the pointers are equal, then 5620Sstevel@tonic-gate * they must refer to the same user. Otherwise, the contents of 5630Sstevel@tonic-gate * the credentials are compared to see whether they are equivalent. 5640Sstevel@tonic-gate * 5650Sstevel@tonic-gate * This routine returns 0 if the credentials refer to the same user, 5660Sstevel@tonic-gate * 1 if they do not. 5670Sstevel@tonic-gate */ 5680Sstevel@tonic-gate int 5690Sstevel@tonic-gate crcmp(const cred_t *cr1, const cred_t *cr2) 5700Sstevel@tonic-gate { 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate if (cr1 == cr2) 5730Sstevel@tonic-gate return (0); 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate if (cr1->cr_uid == cr2->cr_uid && 5760Sstevel@tonic-gate cr1->cr_gid == cr2->cr_gid && 5770Sstevel@tonic-gate cr1->cr_ruid == cr2->cr_ruid && 5780Sstevel@tonic-gate cr1->cr_rgid == cr2->cr_rgid && 5790Sstevel@tonic-gate cr1->cr_ngroups == cr2->cr_ngroups && 5800Sstevel@tonic-gate cr1->cr_zone == cr2->cr_zone && 5810Sstevel@tonic-gate bcmp(cr1->cr_groups, cr2->cr_groups, 5824520Snw141292 cr1->cr_ngroups * sizeof (gid_t)) == 0) { 5830Sstevel@tonic-gate return (!priv_isequalset(&CR_OEPRIV(cr1), &CR_OEPRIV(cr2))); 5840Sstevel@tonic-gate } 5850Sstevel@tonic-gate return (1); 5860Sstevel@tonic-gate } 5870Sstevel@tonic-gate 5880Sstevel@tonic-gate /* 5890Sstevel@tonic-gate * Read access functions to cred_t. 5900Sstevel@tonic-gate */ 5910Sstevel@tonic-gate uid_t 5920Sstevel@tonic-gate crgetuid(const cred_t *cr) 5930Sstevel@tonic-gate { 5940Sstevel@tonic-gate return (cr->cr_uid); 5950Sstevel@tonic-gate } 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate uid_t 5980Sstevel@tonic-gate crgetruid(const cred_t *cr) 5990Sstevel@tonic-gate { 6000Sstevel@tonic-gate return (cr->cr_ruid); 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate uid_t 6040Sstevel@tonic-gate crgetsuid(const cred_t *cr) 6050Sstevel@tonic-gate { 6060Sstevel@tonic-gate return (cr->cr_suid); 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate gid_t 6100Sstevel@tonic-gate crgetgid(const cred_t *cr) 6110Sstevel@tonic-gate { 6120Sstevel@tonic-gate return (cr->cr_gid); 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate gid_t 6160Sstevel@tonic-gate crgetrgid(const cred_t *cr) 6170Sstevel@tonic-gate { 6180Sstevel@tonic-gate return (cr->cr_rgid); 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate gid_t 6220Sstevel@tonic-gate crgetsgid(const cred_t *cr) 6230Sstevel@tonic-gate { 6240Sstevel@tonic-gate return (cr->cr_sgid); 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate 6270Sstevel@tonic-gate const auditinfo_addr_t * 6280Sstevel@tonic-gate crgetauinfo(const cred_t *cr) 6290Sstevel@tonic-gate { 6300Sstevel@tonic-gate return ((const auditinfo_addr_t *)CR_AUINFO(cr)); 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate auditinfo_addr_t * 6340Sstevel@tonic-gate crgetauinfo_modifiable(cred_t *cr) 6350Sstevel@tonic-gate { 6360Sstevel@tonic-gate return (CR_AUINFO(cr)); 6370Sstevel@tonic-gate } 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate zoneid_t 6400Sstevel@tonic-gate crgetzoneid(const cred_t *cr) 6410Sstevel@tonic-gate { 6421676Sjpk return (cr->cr_zone == NULL ? 6431676Sjpk (cr->cr_uid == -1 ? (zoneid_t)-1 : GLOBAL_ZONEID) : 6441676Sjpk cr->cr_zone->zone_id); 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate projid_t 6480Sstevel@tonic-gate crgetprojid(const cred_t *cr) 6490Sstevel@tonic-gate { 6500Sstevel@tonic-gate return (cr->cr_projid); 6510Sstevel@tonic-gate } 6520Sstevel@tonic-gate 6531676Sjpk zone_t * 6541676Sjpk crgetzone(const cred_t *cr) 6551676Sjpk { 6561676Sjpk return (cr->cr_zone); 6571676Sjpk } 6581676Sjpk 6591676Sjpk struct ts_label_s * 6601676Sjpk crgetlabel(const cred_t *cr) 6611676Sjpk { 6621676Sjpk return (cr->cr_label ? 6631676Sjpk cr->cr_label : 6641676Sjpk (cr->cr_zone ? cr->cr_zone->zone_slabel : NULL)); 6651676Sjpk } 6661676Sjpk 6671676Sjpk boolean_t 6681676Sjpk crisremote(const cred_t *cr) 6691676Sjpk { 6701676Sjpk return (REMOTE_PEER_CRED(cr)); 6711676Sjpk } 6721676Sjpk 673*5771Sjp151216 #define BADUID(x, zn) ((x) != -1 && !VALID_UID((x), (zn))) 674*5771Sjp151216 #define BADGID(x, zn) ((x) != -1 && !VALID_GID((x), (zn))) 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate int 6770Sstevel@tonic-gate crsetresuid(cred_t *cr, uid_t r, uid_t e, uid_t s) 6780Sstevel@tonic-gate { 679*5771Sjp151216 zone_t *zone = crgetzone(cr); 680*5771Sjp151216 6810Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 6820Sstevel@tonic-gate 683*5771Sjp151216 if (BADUID(r, zone) || BADUID(e, zone) || BADUID(s, zone)) 6840Sstevel@tonic-gate return (-1); 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate if (r != -1) 6870Sstevel@tonic-gate cr->cr_ruid = r; 6880Sstevel@tonic-gate if (e != -1) 6890Sstevel@tonic-gate cr->cr_uid = e; 6900Sstevel@tonic-gate if (s != -1) 6910Sstevel@tonic-gate cr->cr_suid = s; 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate return (0); 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate int 6970Sstevel@tonic-gate crsetresgid(cred_t *cr, gid_t r, gid_t e, gid_t s) 6980Sstevel@tonic-gate { 699*5771Sjp151216 zone_t *zone = crgetzone(cr); 700*5771Sjp151216 7010Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 7020Sstevel@tonic-gate 703*5771Sjp151216 if (BADGID(r, zone) || BADGID(e, zone) || BADGID(s, zone)) 7040Sstevel@tonic-gate return (-1); 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate if (r != -1) 7070Sstevel@tonic-gate cr->cr_rgid = r; 7080Sstevel@tonic-gate if (e != -1) 7090Sstevel@tonic-gate cr->cr_gid = e; 7100Sstevel@tonic-gate if (s != -1) 7110Sstevel@tonic-gate cr->cr_sgid = s; 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate return (0); 7140Sstevel@tonic-gate } 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate int 7170Sstevel@tonic-gate crsetugid(cred_t *cr, uid_t uid, gid_t gid) 7180Sstevel@tonic-gate { 719*5771Sjp151216 zone_t *zone = crgetzone(cr); 720*5771Sjp151216 7210Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 7220Sstevel@tonic-gate 723*5771Sjp151216 if (!VALID_UID(uid, zone) || !VALID_GID(gid, zone)) 7240Sstevel@tonic-gate return (-1); 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate cr->cr_uid = cr->cr_ruid = cr->cr_suid = uid; 7270Sstevel@tonic-gate cr->cr_gid = cr->cr_rgid = cr->cr_sgid = gid; 7280Sstevel@tonic-gate 7290Sstevel@tonic-gate return (0); 7300Sstevel@tonic-gate } 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate int 7330Sstevel@tonic-gate crsetgroups(cred_t *cr, int n, gid_t *grp) 7340Sstevel@tonic-gate { 7350Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 7360Sstevel@tonic-gate 7370Sstevel@tonic-gate if (n > ngroups_max || n < 0) 7380Sstevel@tonic-gate return (-1); 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate cr->cr_ngroups = n; 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate if (n > 0) 7430Sstevel@tonic-gate bcopy(grp, cr->cr_groups, n * sizeof (gid_t)); 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate return (0); 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate void 7490Sstevel@tonic-gate crsetprojid(cred_t *cr, projid_t projid) 7500Sstevel@tonic-gate { 7510Sstevel@tonic-gate ASSERT(projid >= 0 && projid <= MAXPROJID); 7520Sstevel@tonic-gate cr->cr_projid = projid; 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate /* 7560Sstevel@tonic-gate * This routine returns the pointer to the first element of the cr_groups 7570Sstevel@tonic-gate * array. It can move around in an implementation defined way. 7580Sstevel@tonic-gate */ 7590Sstevel@tonic-gate const gid_t * 7600Sstevel@tonic-gate crgetgroups(const cred_t *cr) 7610Sstevel@tonic-gate { 7620Sstevel@tonic-gate return (cr->cr_groups); 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate int 7660Sstevel@tonic-gate crgetngroups(const cred_t *cr) 7670Sstevel@tonic-gate { 7680Sstevel@tonic-gate return (cr->cr_ngroups); 7690Sstevel@tonic-gate } 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate void 7720Sstevel@tonic-gate cred2prcred(const cred_t *cr, prcred_t *pcrp) 7730Sstevel@tonic-gate { 7740Sstevel@tonic-gate pcrp->pr_euid = cr->cr_uid; 7750Sstevel@tonic-gate pcrp->pr_ruid = cr->cr_ruid; 7760Sstevel@tonic-gate pcrp->pr_suid = cr->cr_suid; 7770Sstevel@tonic-gate pcrp->pr_egid = cr->cr_gid; 7780Sstevel@tonic-gate pcrp->pr_rgid = cr->cr_rgid; 7790Sstevel@tonic-gate pcrp->pr_sgid = cr->cr_sgid; 7800Sstevel@tonic-gate pcrp->pr_ngroups = MIN(cr->cr_ngroups, (uint_t)ngroups_max); 7810Sstevel@tonic-gate pcrp->pr_groups[0] = 0; /* in case ngroups == 0 */ 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate if (pcrp->pr_ngroups != 0) 7840Sstevel@tonic-gate bcopy(cr->cr_groups, pcrp->pr_groups, 7850Sstevel@tonic-gate sizeof (gid_t) * cr->cr_ngroups); 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate 7880Sstevel@tonic-gate static int 7891676Sjpk cred2ucaud(const cred_t *cr, auditinfo64_addr_t *ainfo, const cred_t *rcr) 7900Sstevel@tonic-gate { 7910Sstevel@tonic-gate auditinfo_addr_t *ai; 7920Sstevel@tonic-gate au_tid_addr_t tid; 7930Sstevel@tonic-gate 7941676Sjpk if (secpolicy_audit_getattr(rcr) != 0) 7950Sstevel@tonic-gate return (-1); 7960Sstevel@tonic-gate 7970Sstevel@tonic-gate ai = CR_AUINFO(cr); /* caller makes sure this is non-NULL */ 7980Sstevel@tonic-gate tid = ai->ai_termid; 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate ainfo->ai_auid = ai->ai_auid; 8010Sstevel@tonic-gate ainfo->ai_mask = ai->ai_mask; 8020Sstevel@tonic-gate ainfo->ai_asid = ai->ai_asid; 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate ainfo->ai_termid.at_type = tid.at_type; 8050Sstevel@tonic-gate bcopy(&tid.at_addr, &ainfo->ai_termid.at_addr, 4 * sizeof (uint_t)); 8060Sstevel@tonic-gate 8070Sstevel@tonic-gate ainfo->ai_termid.at_port.at_major = (uint32_t)getmajor(tid.at_port); 8080Sstevel@tonic-gate ainfo->ai_termid.at_port.at_minor = (uint32_t)getminor(tid.at_port); 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate return (0); 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate 8131676Sjpk void 8141676Sjpk cred2uclabel(const cred_t *cr, bslabel_t *labelp) 8151676Sjpk { 8161676Sjpk ts_label_t *tslp; 8171676Sjpk 8181676Sjpk if ((tslp = crgetlabel(cr)) != NULL) 8191676Sjpk bcopy(&tslp->tsl_label, labelp, sizeof (bslabel_t)); 8201676Sjpk } 8211676Sjpk 8220Sstevel@tonic-gate /* 8230Sstevel@tonic-gate * Convert a credential into a "ucred". Allow the caller to specify 8240Sstevel@tonic-gate * and aligned buffer, e.g., in an mblk, so we don't have to allocate 8250Sstevel@tonic-gate * memory and copy it twice. 8261676Sjpk * 8271676Sjpk * This function may call cred2ucaud(), which calls CRED(). Since this 8281676Sjpk * can be called from an interrupt thread, receiver's cred (rcr) is needed 8291676Sjpk * to determine whether audit info should be included. 8300Sstevel@tonic-gate */ 8310Sstevel@tonic-gate struct ucred_s * 8321676Sjpk cred2ucred(const cred_t *cr, pid_t pid, void *buf, const cred_t *rcr) 8330Sstevel@tonic-gate { 8340Sstevel@tonic-gate struct ucred_s *uc; 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate /* The structure isn't always completely filled in, so zero it */ 8370Sstevel@tonic-gate if (buf == NULL) { 8380Sstevel@tonic-gate uc = kmem_zalloc(ucredsize, KM_SLEEP); 8390Sstevel@tonic-gate } else { 8400Sstevel@tonic-gate bzero(buf, ucredsize); 8410Sstevel@tonic-gate uc = buf; 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate uc->uc_size = ucredsize; 8440Sstevel@tonic-gate uc->uc_credoff = UCRED_CRED_OFF; 8450Sstevel@tonic-gate uc->uc_privoff = UCRED_PRIV_OFF; 8460Sstevel@tonic-gate uc->uc_audoff = UCRED_AUD_OFF; 8471676Sjpk uc->uc_labeloff = UCRED_LABEL_OFF; 8480Sstevel@tonic-gate uc->uc_pid = pid; 8490Sstevel@tonic-gate uc->uc_projid = cr->cr_projid; 8500Sstevel@tonic-gate uc->uc_zoneid = crgetzoneid(cr); 8510Sstevel@tonic-gate 8521676Sjpk /* 8531676Sjpk * Note that cred2uclabel() call should not be factored out 8541676Sjpk * to the bottom of the if-else. UCXXX() macros depend on 8551676Sjpk * uc_xxxoff values to work correctly. 8561676Sjpk */ 8571676Sjpk if (REMOTE_PEER_CRED(cr)) { 8581676Sjpk /* 8591676Sjpk * other than label, the rest of cred info about a 8601676Sjpk * remote peer isn't available. 8611676Sjpk */ 8621676Sjpk cred2uclabel(cr, UCLABEL(uc)); 8631676Sjpk uc->uc_credoff = 0; 8641676Sjpk uc->uc_privoff = 0; 8650Sstevel@tonic-gate uc->uc_audoff = 0; 8661676Sjpk } else { 8671676Sjpk cred2prcred(cr, UCCRED(uc)); 8681676Sjpk cred2prpriv(cr, UCPRIV(uc)); 8691676Sjpk if (audoff == 0 || cred2ucaud(cr, UCAUD(uc), rcr) != 0) 8701676Sjpk uc->uc_audoff = 0; 8711676Sjpk cred2uclabel(cr, UCLABEL(uc)); 8721676Sjpk } 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate return (uc); 8750Sstevel@tonic-gate } 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate /* 8780Sstevel@tonic-gate * Get the "ucred" of a process. 8790Sstevel@tonic-gate */ 8800Sstevel@tonic-gate struct ucred_s * 8810Sstevel@tonic-gate pgetucred(proc_t *p) 8820Sstevel@tonic-gate { 8830Sstevel@tonic-gate cred_t *cr; 8840Sstevel@tonic-gate struct ucred_s *uc; 8850Sstevel@tonic-gate 8860Sstevel@tonic-gate mutex_enter(&p->p_crlock); 8870Sstevel@tonic-gate cr = p->p_cred; 8880Sstevel@tonic-gate crhold(cr); 8890Sstevel@tonic-gate mutex_exit(&p->p_crlock); 8900Sstevel@tonic-gate 8911676Sjpk uc = cred2ucred(cr, p->p_pid, NULL, CRED()); 8920Sstevel@tonic-gate crfree(cr); 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate return (uc); 8950Sstevel@tonic-gate } 8960Sstevel@tonic-gate 8970Sstevel@tonic-gate /* 8980Sstevel@tonic-gate * If the reply status is NFSERR_EACCES, it may be because we are 8990Sstevel@tonic-gate * root (no root net access). Check the real uid, if it isn't root 9000Sstevel@tonic-gate * make that the uid instead and retry the call. 9010Sstevel@tonic-gate * Private interface for NFS. 9020Sstevel@tonic-gate */ 9030Sstevel@tonic-gate cred_t * 9040Sstevel@tonic-gate crnetadjust(cred_t *cr) 9050Sstevel@tonic-gate { 9060Sstevel@tonic-gate if (cr->cr_uid == 0 && cr->cr_ruid != 0) { 9070Sstevel@tonic-gate cr = crdup(cr); 9080Sstevel@tonic-gate cr->cr_uid = cr->cr_ruid; 9090Sstevel@tonic-gate return (cr); 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate return (NULL); 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate /* 9150Sstevel@tonic-gate * The reference count is of interest when you want to check 9160Sstevel@tonic-gate * whether it is ok to modify the credential in place. 9170Sstevel@tonic-gate */ 9180Sstevel@tonic-gate uint_t 9190Sstevel@tonic-gate crgetref(const cred_t *cr) 9200Sstevel@tonic-gate { 9210Sstevel@tonic-gate return (cr->cr_ref); 9220Sstevel@tonic-gate } 9230Sstevel@tonic-gate 9240Sstevel@tonic-gate static int 9250Sstevel@tonic-gate get_c2audit_load(void) 9260Sstevel@tonic-gate { 9270Sstevel@tonic-gate static int gotit = 0; 9280Sstevel@tonic-gate static int c2audit_load; 9290Sstevel@tonic-gate u_longlong_t audit_load_val; 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate if (gotit) 9320Sstevel@tonic-gate return (c2audit_load); 9330Sstevel@tonic-gate audit_load_val = 0; /* set default value once */ 9340Sstevel@tonic-gate (void) mod_sysvar("c2audit", "audit_load", &audit_load_val); 9350Sstevel@tonic-gate c2audit_load = (int)audit_load_val; 9360Sstevel@tonic-gate gotit++; 9370Sstevel@tonic-gate return (c2audit_load); 9380Sstevel@tonic-gate } 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate int 9410Sstevel@tonic-gate get_audit_ucrsize(void) 9420Sstevel@tonic-gate { 9430Sstevel@tonic-gate return (get_c2audit_load() ? sizeof (auditinfo64_addr_t) : 0); 9440Sstevel@tonic-gate } 9450Sstevel@tonic-gate 9460Sstevel@tonic-gate /* 9470Sstevel@tonic-gate * Set zone pointer in credential to indicated value. First adds a 9480Sstevel@tonic-gate * hold for the new zone, then drops the hold on previous zone (if any). 9490Sstevel@tonic-gate * This is done in this order in case the old and new zones are the 9500Sstevel@tonic-gate * same. 9510Sstevel@tonic-gate */ 9520Sstevel@tonic-gate void 9530Sstevel@tonic-gate crsetzone(cred_t *cr, zone_t *zptr) 9540Sstevel@tonic-gate { 9550Sstevel@tonic-gate zone_t *oldzptr = cr->cr_zone; 9560Sstevel@tonic-gate 9570Sstevel@tonic-gate ASSERT(cr != kcred); 9580Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 9590Sstevel@tonic-gate cr->cr_zone = zptr; 9600Sstevel@tonic-gate zone_cred_hold(zptr); 9610Sstevel@tonic-gate if (oldzptr) 9620Sstevel@tonic-gate zone_cred_rele(oldzptr); 9630Sstevel@tonic-gate } 9641676Sjpk 9651676Sjpk /* 9661676Sjpk * Create a new cred based on the supplied label 9671676Sjpk */ 9681676Sjpk cred_t * 9691676Sjpk newcred_from_bslabel(bslabel_t *blabel, uint32_t doi, int flags) 9701676Sjpk { 9711676Sjpk ts_label_t *lbl = labelalloc(blabel, doi, flags); 9721676Sjpk cred_t *cr = NULL; 9731676Sjpk 9741676Sjpk if (lbl != NULL) { 9751676Sjpk if ((cr = kmem_cache_alloc(cred_cache, flags)) != NULL) { 9761676Sjpk bcopy(dummycr, cr, crsize); 9771676Sjpk cr->cr_label = lbl; 9781676Sjpk } else { 9791676Sjpk label_rele(lbl); 9801676Sjpk } 9811676Sjpk } 9821676Sjpk 9831676Sjpk return (cr); 9841676Sjpk } 9851676Sjpk 9861676Sjpk /* 9871676Sjpk * Derive a new cred from the existing cred, but with a different label. 9881676Sjpk * To be used when a cred is being shared, but the label needs to be changed 9891676Sjpk * by a caller without affecting other users 9901676Sjpk */ 9911676Sjpk cred_t * 9921676Sjpk copycred_from_bslabel(cred_t *cr, bslabel_t *blabel, uint32_t doi, int flags) 9931676Sjpk { 9941676Sjpk ts_label_t *lbl = labelalloc(blabel, doi, flags); 9951676Sjpk cred_t *newcr = NULL; 9961676Sjpk 9971676Sjpk if (lbl != NULL) { 9981676Sjpk if ((newcr = kmem_cache_alloc(cred_cache, flags)) != NULL) { 9991676Sjpk bcopy(cr, newcr, crsize); 10001676Sjpk if (newcr->cr_zone) 10011676Sjpk zone_cred_hold(newcr->cr_zone); 10021676Sjpk newcr->cr_label = lbl; 10031676Sjpk newcr->cr_ref = 1; 10041676Sjpk } else { 10051676Sjpk label_rele(lbl); 10061676Sjpk } 10071676Sjpk } 10081676Sjpk 10091676Sjpk return (newcr); 10101676Sjpk } 10111676Sjpk 10121676Sjpk /* 10131676Sjpk * This function returns a pointer to the kcred-equivalent in the current zone. 10141676Sjpk */ 10151676Sjpk cred_t * 10161676Sjpk zone_kcred(void) 10171676Sjpk { 10181676Sjpk zone_t *zone; 10191676Sjpk 10201676Sjpk if ((zone = CRED()->cr_zone) != NULL) 10211676Sjpk return (zone->zone_kcred); 10221676Sjpk else 10231676Sjpk return (kcred); 10241676Sjpk } 10254321Scasper 10264321Scasper boolean_t 1027*5771Sjp151216 valid_ephemeral_uid(zone_t *zone, uid_t id) 10284321Scasper { 1029*5771Sjp151216 ephemeral_zsd_t *eph_zsd; 1030*5771Sjp151216 if (id < IDMAP_WK__MAX_UID) 1031*5771Sjp151216 return (B_TRUE); 1032*5771Sjp151216 1033*5771Sjp151216 eph_zsd = get_ephemeral_zsd(zone); 1034*5771Sjp151216 ASSERT(eph_zsd != NULL); 10354321Scasper membar_consumer(); 1036*5771Sjp151216 return (id > eph_zsd->min_uid && id <= eph_zsd->last_uid); 10374321Scasper } 10384321Scasper 10394321Scasper boolean_t 1040*5771Sjp151216 valid_ephemeral_gid(zone_t *zone, gid_t id) 10414321Scasper { 1042*5771Sjp151216 ephemeral_zsd_t *eph_zsd; 1043*5771Sjp151216 if (id < IDMAP_WK__MAX_GID) 1044*5771Sjp151216 return (B_TRUE); 1045*5771Sjp151216 1046*5771Sjp151216 eph_zsd = get_ephemeral_zsd(zone); 1047*5771Sjp151216 ASSERT(eph_zsd != NULL); 10484321Scasper membar_consumer(); 1049*5771Sjp151216 return (id > eph_zsd->min_gid && id <= eph_zsd->last_gid); 10504321Scasper } 10514321Scasper 10524321Scasper int 1053*5771Sjp151216 eph_uid_alloc(zone_t *zone, int flags, uid_t *start, int count) 10544321Scasper { 1055*5771Sjp151216 ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone); 1056*5771Sjp151216 1057*5771Sjp151216 ASSERT(eph_zsd != NULL); 1058*5771Sjp151216 1059*5771Sjp151216 mutex_enter(&eph_zsd->eph_lock); 10604321Scasper 10614321Scasper /* Test for unsigned integer wrap around */ 1062*5771Sjp151216 if (eph_zsd->last_uid + count < eph_zsd->last_uid) { 1063*5771Sjp151216 mutex_exit(&eph_zsd->eph_lock); 10644321Scasper return (-1); 10654321Scasper } 10664321Scasper 10674321Scasper /* first call or idmap crashed and state corrupted */ 10684321Scasper if (flags != 0) 1069*5771Sjp151216 eph_zsd->min_uid = eph_zsd->last_uid; 10704321Scasper 10714321Scasper hasephids = B_TRUE; 1072*5771Sjp151216 *start = eph_zsd->last_uid + 1; 1073*5771Sjp151216 atomic_add_32(&eph_zsd->last_uid, count); 1074*5771Sjp151216 mutex_exit(&eph_zsd->eph_lock); 10754321Scasper return (0); 10764321Scasper } 10774321Scasper 10784321Scasper int 1079*5771Sjp151216 eph_gid_alloc(zone_t *zone, int flags, gid_t *start, int count) 10804321Scasper { 1081*5771Sjp151216 ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone); 1082*5771Sjp151216 1083*5771Sjp151216 ASSERT(eph_zsd != NULL); 1084*5771Sjp151216 1085*5771Sjp151216 mutex_enter(&eph_zsd->eph_lock); 10864321Scasper 10874321Scasper /* Test for unsigned integer wrap around */ 1088*5771Sjp151216 if (eph_zsd->last_gid + count < eph_zsd->last_gid) { 1089*5771Sjp151216 mutex_exit(&eph_zsd->eph_lock); 10904321Scasper return (-1); 10914321Scasper } 10924321Scasper 10934321Scasper /* first call or idmap crashed and state corrupted */ 10944321Scasper if (flags != 0) 1095*5771Sjp151216 eph_zsd->min_gid = eph_zsd->last_gid; 10964321Scasper 10974321Scasper hasephids = B_TRUE; 1098*5771Sjp151216 *start = eph_zsd->last_gid + 1; 1099*5771Sjp151216 atomic_add_32(&eph_zsd->last_gid, count); 1100*5771Sjp151216 mutex_exit(&eph_zsd->eph_lock); 11014321Scasper return (0); 11024321Scasper } 11034321Scasper 11044321Scasper /* 1105*5771Sjp151216 * IMPORTANT.The two functions get_ephemeral_data() and set_ephemeral_data() 1106*5771Sjp151216 * are project private functions that are for use of the test system only and 1107*5771Sjp151216 * are not to be used for other purposes. 1108*5771Sjp151216 */ 1109*5771Sjp151216 1110*5771Sjp151216 void 1111*5771Sjp151216 get_ephemeral_data(zone_t *zone, uid_t *min_uid, uid_t *last_uid, 1112*5771Sjp151216 gid_t *min_gid, gid_t *last_gid) 1113*5771Sjp151216 { 1114*5771Sjp151216 ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone); 1115*5771Sjp151216 1116*5771Sjp151216 ASSERT(eph_zsd != NULL); 1117*5771Sjp151216 1118*5771Sjp151216 mutex_enter(&eph_zsd->eph_lock); 1119*5771Sjp151216 1120*5771Sjp151216 *min_uid = eph_zsd->min_uid; 1121*5771Sjp151216 *last_uid = eph_zsd->last_uid; 1122*5771Sjp151216 *min_gid = eph_zsd->min_gid; 1123*5771Sjp151216 *last_gid = eph_zsd->last_gid; 1124*5771Sjp151216 1125*5771Sjp151216 mutex_exit(&eph_zsd->eph_lock); 1126*5771Sjp151216 } 1127*5771Sjp151216 1128*5771Sjp151216 1129*5771Sjp151216 void 1130*5771Sjp151216 set_ephemeral_data(zone_t *zone, uid_t min_uid, uid_t last_uid, 1131*5771Sjp151216 gid_t min_gid, gid_t last_gid) 1132*5771Sjp151216 { 1133*5771Sjp151216 ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone); 1134*5771Sjp151216 1135*5771Sjp151216 ASSERT(eph_zsd != NULL); 1136*5771Sjp151216 1137*5771Sjp151216 mutex_enter(&eph_zsd->eph_lock); 1138*5771Sjp151216 1139*5771Sjp151216 if (min_uid != 0) 1140*5771Sjp151216 eph_zsd->min_uid = min_uid; 1141*5771Sjp151216 if (last_uid != 0) 1142*5771Sjp151216 eph_zsd->last_uid = last_uid; 1143*5771Sjp151216 if (min_gid != 0) 1144*5771Sjp151216 eph_zsd->min_gid = min_gid; 1145*5771Sjp151216 if (last_gid != 0) 1146*5771Sjp151216 eph_zsd->last_gid = last_gid; 1147*5771Sjp151216 1148*5771Sjp151216 mutex_exit(&eph_zsd->eph_lock); 1149*5771Sjp151216 } 1150*5771Sjp151216 1151*5771Sjp151216 /* 11525331Samw * If the credential user SID or group SID is mapped to an ephemeral 11535331Samw * ID, map the credential to nobody. 11544321Scasper */ 11554321Scasper cred_t * 11564321Scasper crgetmapped(const cred_t *cr) 11574321Scasper { 1158*5771Sjp151216 ephemeral_zsd_t *eph_zsd; 11594406Scasper /* 11604406Scasper * Someone incorrectly passed a NULL cred to a vnode operation 11614406Scasper * either on purpose or by calling CRED() in interrupt context. 11624406Scasper */ 11634406Scasper if (cr == NULL) 11644406Scasper return (NULL); 11654406Scasper 11664321Scasper if (cr->cr_ksid != NULL) { 1167*5771Sjp151216 if (cr->cr_ksid->kr_sidx[KSID_USER].ks_id > MAXUID) { 1168*5771Sjp151216 eph_zsd = get_ephemeral_zsd(crgetzone(cr)); 1169*5771Sjp151216 return (eph_zsd->eph_nobody); 1170*5771Sjp151216 } 11714321Scasper 1172*5771Sjp151216 if (cr->cr_ksid->kr_sidx[KSID_GROUP].ks_id > MAXUID) { 1173*5771Sjp151216 eph_zsd = get_ephemeral_zsd(crgetzone(cr)); 1174*5771Sjp151216 return (eph_zsd->eph_nobody); 1175*5771Sjp151216 } 11764321Scasper } 11774321Scasper 11784321Scasper return ((cred_t *)cr); 11794321Scasper } 11804321Scasper 11814321Scasper /* index should be in range for a ksidindex_t */ 11824321Scasper void 11834321Scasper crsetsid(cred_t *cr, ksid_t *ksp, int index) 11844321Scasper { 11854321Scasper ASSERT(cr->cr_ref <= 2); 11864321Scasper ASSERT(index >= 0 && index < KSID_COUNT); 11874321Scasper if (cr->cr_ksid == NULL && ksp == NULL) 11884321Scasper return; 11894321Scasper cr->cr_ksid = kcrsid_setsid(cr->cr_ksid, ksp, index); 11904321Scasper } 11914321Scasper 11924321Scasper void 11934321Scasper crsetsidlist(cred_t *cr, ksidlist_t *ksl) 11944321Scasper { 11954321Scasper ASSERT(cr->cr_ref <= 2); 11964321Scasper if (cr->cr_ksid == NULL && ksl == NULL) 11974321Scasper return; 11984321Scasper cr->cr_ksid = kcrsid_setsidlist(cr->cr_ksid, ksl); 11994321Scasper } 12004321Scasper 12014321Scasper ksid_t * 12024321Scasper crgetsid(const cred_t *cr, int i) 12034321Scasper { 12044321Scasper ASSERT(i >= 0 && i < KSID_COUNT); 12054321Scasper if (cr->cr_ksid != NULL && cr->cr_ksid->kr_sidx[i].ks_domain) 12064321Scasper return ((ksid_t *)&cr->cr_ksid->kr_sidx[i]); 12074321Scasper return (NULL); 12084321Scasper } 12094321Scasper 12104321Scasper ksidlist_t * 12114321Scasper crgetsidlist(const cred_t *cr) 12124321Scasper { 12135331Samw if (cr->cr_ksid != NULL) 12144923Scasper return (cr->cr_ksid->kr_sidlist); 12154321Scasper return (NULL); 12164321Scasper } 12175331Samw 12185331Samw /* 12195331Samw * Interface to set the effective and permitted privileges for 12205331Samw * a credential; this interface does no security checks and is 12215331Samw * intended for kernel (file)servers creating credentials with 12225331Samw * specific privileges. 12235331Samw */ 12245331Samw int 12255331Samw crsetpriv(cred_t *cr, ...) 12265331Samw { 12275331Samw va_list ap; 12285331Samw const char *privnm; 12295331Samw 12305331Samw ASSERT(cr->cr_ref <= 2); 12315331Samw 12325331Samw priv_set_PA(cr); 12335331Samw 12345331Samw va_start(ap, cr); 12355331Samw 12365331Samw while ((privnm = va_arg(ap, const char *)) != NULL) { 12375331Samw int priv = priv_getbyname(privnm, 0); 12385331Samw if (priv < 0) 12395331Samw return (-1); 12405331Samw 12415331Samw priv_addset(&CR_PPRIV(cr), priv); 12425331Samw priv_addset(&CR_EPRIV(cr), priv); 12435331Samw } 12445331Samw priv_adjust_PA(cr); 12455331Samw va_end(ap); 12465331Samw return (0); 12475331Samw } 1248