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> 59*5331Samw #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> 65*5331Samw #include <sys/varargs.h> 664321Scasper 674321Scasper typedef struct ephidmap_data { 684321Scasper uid_t min_uid, last_uid; 694321Scasper gid_t min_gid, last_gid; 704321Scasper cred_t *nobody; 714321Scasper kmutex_t eph_lock; 724321Scasper } ephidmap_data_t; 730Sstevel@tonic-gate 740Sstevel@tonic-gate static struct kmem_cache *cred_cache; 750Sstevel@tonic-gate static size_t crsize = 0; 760Sstevel@tonic-gate static int audoff = 0; 770Sstevel@tonic-gate uint32_t ucredsize; 780Sstevel@tonic-gate cred_t *kcred; 791676Sjpk static cred_t *dummycr; 800Sstevel@tonic-gate 810Sstevel@tonic-gate int rstlink; /* link(2) restricted to files owned by user? */ 820Sstevel@tonic-gate 830Sstevel@tonic-gate static int get_c2audit_load(void); 840Sstevel@tonic-gate 850Sstevel@tonic-gate #define CR_AUINFO(c) (auditinfo_addr_t *)((audoff == 0) ? NULL : \ 860Sstevel@tonic-gate ((char *)(c)) + audoff) 870Sstevel@tonic-gate 881676Sjpk #define REMOTE_PEER_CRED(c) ((c)->cr_gid == -1) 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* 914321Scasper * XXX: should be per-zone. 924321Scasper * Start with an invalid value for atomic increments. 934321Scasper */ 944321Scasper static ephidmap_data_t ephemeral_data = { 954520Snw141292 MAXUID, IDMAP_WK__MAX_UID, MAXUID, IDMAP_WK__MAX_GID 964321Scasper }; 974321Scasper 984321Scasper static boolean_t hasephids = B_FALSE; 994321Scasper 1004321Scasper /* 1010Sstevel@tonic-gate * Initialize credentials data structures. 1020Sstevel@tonic-gate */ 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate void 1050Sstevel@tonic-gate cred_init(void) 1060Sstevel@tonic-gate { 1070Sstevel@tonic-gate priv_init(); 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate crsize = sizeof (cred_t) + sizeof (gid_t) * (ngroups_max - 1); 1100Sstevel@tonic-gate /* 1110Sstevel@tonic-gate * Make sure it's word-aligned. 1120Sstevel@tonic-gate */ 1130Sstevel@tonic-gate crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1); 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate if (get_c2audit_load() > 0) { 1160Sstevel@tonic-gate #ifdef _LP64 1170Sstevel@tonic-gate /* assure audit context is 64-bit aligned */ 1180Sstevel@tonic-gate audoff = (crsize + 1190Sstevel@tonic-gate sizeof (int64_t) - 1) & ~(sizeof (int64_t) - 1); 1200Sstevel@tonic-gate #else /* _LP64 */ 1210Sstevel@tonic-gate audoff = crsize; 1220Sstevel@tonic-gate #endif /* _LP64 */ 1230Sstevel@tonic-gate crsize = audoff + sizeof (auditinfo_addr_t); 1240Sstevel@tonic-gate crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1); 1250Sstevel@tonic-gate } 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate cred_cache = kmem_cache_create("cred_cache", crsize, 0, 1284520Snw141292 NULL, NULL, NULL, NULL, NULL, 0); 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate /* 1311676Sjpk * dummycr is used to copy initial state for creds. 1321676Sjpk */ 1331676Sjpk dummycr = cralloc(); 1341676Sjpk bzero(dummycr, crsize); 1351676Sjpk dummycr->cr_ref = 1; 1364321Scasper dummycr->cr_uid = (uid_t)-1; 1374321Scasper dummycr->cr_gid = (gid_t)-1; 1384321Scasper dummycr->cr_ruid = (uid_t)-1; 1394321Scasper dummycr->cr_rgid = (gid_t)-1; 1404321Scasper dummycr->cr_suid = (uid_t)-1; 1414321Scasper dummycr->cr_sgid = (gid_t)-1; 1424321Scasper 1431676Sjpk 1441676Sjpk /* 1450Sstevel@tonic-gate * kcred is used by anything that needs all privileges; it's 1460Sstevel@tonic-gate * also the template used for crget as it has all the compatible 1470Sstevel@tonic-gate * sets filled in. 1480Sstevel@tonic-gate */ 1490Sstevel@tonic-gate kcred = cralloc(); 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate bzero(kcred, crsize); 1520Sstevel@tonic-gate kcred->cr_ref = 1; 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* kcred is never freed, so we don't need zone_cred_hold here */ 1550Sstevel@tonic-gate kcred->cr_zone = &zone0; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate priv_fillset(&CR_LPRIV(kcred)); 1580Sstevel@tonic-gate CR_IPRIV(kcred) = *priv_basic; 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate /* Not a basic privilege, if chown is not restricted add it to I0 */ 1610Sstevel@tonic-gate if (!rstchown) 1620Sstevel@tonic-gate priv_addset(&CR_IPRIV(kcred), PRIV_FILE_CHOWN_SELF); 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate /* Basic privilege, if link is restricted remove it from I0 */ 1650Sstevel@tonic-gate if (rstlink) 1660Sstevel@tonic-gate priv_delset(&CR_IPRIV(kcred), PRIV_FILE_LINK_ANY); 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate CR_EPRIV(kcred) = CR_PPRIV(kcred) = CR_IPRIV(kcred); 1691676Sjpk 1701676Sjpk CR_FLAGS(kcred) = NET_MAC_AWARE; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate /* 1730Sstevel@tonic-gate * Set up credentials of p0. 1740Sstevel@tonic-gate */ 1750Sstevel@tonic-gate ttoproc(curthread)->p_cred = kcred; 1760Sstevel@tonic-gate curthread->t_cred = kcred; 1770Sstevel@tonic-gate 1784321Scasper /* 1794321Scasper * nobody is used to map SID containing CRs. 1804321Scasper */ 1814321Scasper ephemeral_data.nobody = crdup(kcred); 1824321Scasper (void) crsetugid(ephemeral_data.nobody, UID_NOBODY, GID_NOBODY); 1834923Scasper CR_FLAGS(ephemeral_data.nobody) = 0; 1844321Scasper 1850Sstevel@tonic-gate ucredsize = UCRED_SIZE; 1860Sstevel@tonic-gate } 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate /* 1890Sstevel@tonic-gate * Allocate (nearly) uninitialized cred_t. 1900Sstevel@tonic-gate */ 1910Sstevel@tonic-gate cred_t * 1920Sstevel@tonic-gate cralloc(void) 1930Sstevel@tonic-gate { 1940Sstevel@tonic-gate cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP); 1950Sstevel@tonic-gate cr->cr_ref = 1; /* So we can crfree() */ 1960Sstevel@tonic-gate cr->cr_zone = NULL; 1971676Sjpk cr->cr_label = NULL; 1984321Scasper cr->cr_ksid = NULL; 1994321Scasper return (cr); 2004321Scasper } 2014321Scasper 2024321Scasper /* 2034321Scasper * As cralloc but prepared for ksid change (if appropriate). 2044321Scasper */ 2054321Scasper cred_t * 2064321Scasper cralloc_ksid(void) 2074321Scasper { 2084321Scasper cred_t *cr = cralloc(); 2094321Scasper if (hasephids) 2104321Scasper cr->cr_ksid = kcrsid_alloc(); 2110Sstevel@tonic-gate return (cr); 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate /* 2150Sstevel@tonic-gate * Allocate a initialized cred structure and crhold() it. 2160Sstevel@tonic-gate * Initialized means: all ids 0, group count 0, L=Full, E=P=I=I0 2170Sstevel@tonic-gate */ 2180Sstevel@tonic-gate cred_t * 2190Sstevel@tonic-gate crget(void) 2200Sstevel@tonic-gate { 2210Sstevel@tonic-gate cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP); 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate bcopy(kcred, cr, crsize); 2240Sstevel@tonic-gate cr->cr_ref = 1; 2250Sstevel@tonic-gate zone_cred_hold(cr->cr_zone); 2261676Sjpk if (cr->cr_label) 2271676Sjpk label_hold(cr->cr_label); 2280Sstevel@tonic-gate return (cr); 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* 2320Sstevel@tonic-gate * Broadcast the cred to all the threads in the process. 2330Sstevel@tonic-gate * The current thread's credentials can be set right away, but other 2340Sstevel@tonic-gate * threads must wait until the start of the next system call or trap. 2350Sstevel@tonic-gate * This avoids changing the cred in the middle of a system call. 2360Sstevel@tonic-gate * 2370Sstevel@tonic-gate * The cred has already been held for the process and the thread (2 holds), 2380Sstevel@tonic-gate * and p->p_cred set. 2390Sstevel@tonic-gate * 2400Sstevel@tonic-gate * p->p_crlock shouldn't be held here, since p_lock must be acquired. 2410Sstevel@tonic-gate */ 2420Sstevel@tonic-gate void 2430Sstevel@tonic-gate crset(proc_t *p, cred_t *cr) 2440Sstevel@tonic-gate { 2450Sstevel@tonic-gate kthread_id_t t; 2460Sstevel@tonic-gate kthread_id_t first; 2470Sstevel@tonic-gate cred_t *oldcr; 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate ASSERT(p == curproc); /* assumes p_lwpcnt can't change */ 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* 2520Sstevel@tonic-gate * DTrace accesses t_cred in probe context. t_cred must always be 2530Sstevel@tonic-gate * either NULL, or point to a valid, allocated cred structure. 2540Sstevel@tonic-gate */ 2550Sstevel@tonic-gate t = curthread; 2560Sstevel@tonic-gate oldcr = t->t_cred; 2570Sstevel@tonic-gate t->t_cred = cr; /* the cred is held by caller for this thread */ 2580Sstevel@tonic-gate crfree(oldcr); /* free the old cred for the thread */ 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate /* 2610Sstevel@tonic-gate * Broadcast to other threads, if any. 2620Sstevel@tonic-gate */ 2630Sstevel@tonic-gate if (p->p_lwpcnt > 1) { 2640Sstevel@tonic-gate mutex_enter(&p->p_lock); /* to keep thread list safe */ 2650Sstevel@tonic-gate first = curthread; 2660Sstevel@tonic-gate for (t = first->t_forw; t != first; t = t->t_forw) 2670Sstevel@tonic-gate t->t_pre_sys = 1; /* so syscall will get new cred */ 2680Sstevel@tonic-gate mutex_exit(&p->p_lock); 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate /* 2730Sstevel@tonic-gate * Put a hold on a cred structure. 2740Sstevel@tonic-gate */ 2750Sstevel@tonic-gate void 2760Sstevel@tonic-gate crhold(cred_t *cr) 2770Sstevel@tonic-gate { 2780Sstevel@tonic-gate atomic_add_32(&cr->cr_ref, 1); 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate /* 2820Sstevel@tonic-gate * Release previous hold on a cred structure. Free it if refcnt == 0. 2831676Sjpk * If cred uses label different from zone label, free it. 2840Sstevel@tonic-gate */ 2850Sstevel@tonic-gate void 2860Sstevel@tonic-gate crfree(cred_t *cr) 2870Sstevel@tonic-gate { 2880Sstevel@tonic-gate if (atomic_add_32_nv(&cr->cr_ref, -1) == 0) { 2890Sstevel@tonic-gate ASSERT(cr != kcred); 2901676Sjpk if (cr->cr_label) 2911676Sjpk label_rele(cr->cr_label); 2920Sstevel@tonic-gate if (cr->cr_zone) 2930Sstevel@tonic-gate zone_cred_rele(cr->cr_zone); 2944321Scasper if (cr->cr_ksid) 2954321Scasper kcrsid_rele(cr->cr_ksid); 2960Sstevel@tonic-gate kmem_cache_free(cred_cache, cr); 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate /* 3010Sstevel@tonic-gate * Copy a cred structure to a new one and free the old one. 3020Sstevel@tonic-gate * The new cred will have two references. One for the calling process, 3030Sstevel@tonic-gate * and one for the thread. 3040Sstevel@tonic-gate */ 3050Sstevel@tonic-gate cred_t * 3060Sstevel@tonic-gate crcopy(cred_t *cr) 3070Sstevel@tonic-gate { 3080Sstevel@tonic-gate cred_t *newcr; 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate newcr = cralloc(); 3110Sstevel@tonic-gate bcopy(cr, newcr, crsize); 3120Sstevel@tonic-gate if (newcr->cr_zone) 3130Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 3141676Sjpk if (newcr->cr_label) 3151676Sjpk label_hold(cr->cr_label); 3164321Scasper if (newcr->cr_ksid) 3174321Scasper kcrsid_hold(cr->cr_ksid); 3180Sstevel@tonic-gate crfree(cr); 3190Sstevel@tonic-gate newcr->cr_ref = 2; /* caller gets two references */ 3200Sstevel@tonic-gate return (newcr); 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate /* 3240Sstevel@tonic-gate * Copy a cred structure to a new one and free the old one. 3250Sstevel@tonic-gate * The new cred will have two references. One for the calling process, 3260Sstevel@tonic-gate * and one for the thread. 3270Sstevel@tonic-gate * This variation on crcopy uses a pre-allocated structure for the 3280Sstevel@tonic-gate * "new" cred. 3290Sstevel@tonic-gate */ 3300Sstevel@tonic-gate void 3310Sstevel@tonic-gate crcopy_to(cred_t *oldcr, cred_t *newcr) 3320Sstevel@tonic-gate { 3334321Scasper credsid_t *nkcr = newcr->cr_ksid; 3344321Scasper 3350Sstevel@tonic-gate bcopy(oldcr, newcr, crsize); 3360Sstevel@tonic-gate if (newcr->cr_zone) 3370Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 3381676Sjpk if (newcr->cr_label) 3391676Sjpk label_hold(newcr->cr_label); 3404321Scasper if (nkcr) { 3414321Scasper newcr->cr_ksid = nkcr; 3424321Scasper kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid); 3434321Scasper } else if (newcr->cr_ksid) 3444321Scasper kcrsid_hold(newcr->cr_ksid); 3450Sstevel@tonic-gate crfree(oldcr); 3460Sstevel@tonic-gate newcr->cr_ref = 2; /* caller gets two references */ 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate /* 3500Sstevel@tonic-gate * Dup a cred struct to a new held one. 3510Sstevel@tonic-gate * The old cred is not freed. 3520Sstevel@tonic-gate */ 3530Sstevel@tonic-gate cred_t * 3540Sstevel@tonic-gate crdup(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(newcr->cr_label); 3644321Scasper if (newcr->cr_ksid) 3654321Scasper kcrsid_hold(newcr->cr_ksid); 3660Sstevel@tonic-gate newcr->cr_ref = 1; 3670Sstevel@tonic-gate return (newcr); 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate /* 3710Sstevel@tonic-gate * Dup a cred struct to a new held one. 3720Sstevel@tonic-gate * The old cred is not freed. 3730Sstevel@tonic-gate * This variation on crdup uses a pre-allocated structure for the 3740Sstevel@tonic-gate * "new" cred. 3750Sstevel@tonic-gate */ 3760Sstevel@tonic-gate void 3770Sstevel@tonic-gate crdup_to(cred_t *oldcr, cred_t *newcr) 3780Sstevel@tonic-gate { 3794321Scasper credsid_t *nkcr = newcr->cr_ksid; 3804321Scasper 3810Sstevel@tonic-gate bcopy(oldcr, newcr, crsize); 3820Sstevel@tonic-gate if (newcr->cr_zone) 3830Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 3841676Sjpk if (newcr->cr_label) 3851676Sjpk label_hold(newcr->cr_label); 3864321Scasper if (nkcr) { 3874321Scasper newcr->cr_ksid = nkcr; 3884321Scasper kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid); 3894321Scasper } else if (newcr->cr_ksid) 3904321Scasper kcrsid_hold(newcr->cr_ksid); 3910Sstevel@tonic-gate newcr->cr_ref = 1; 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate /* 3950Sstevel@tonic-gate * Return the (held) credentials for the current running process. 3960Sstevel@tonic-gate */ 3970Sstevel@tonic-gate cred_t * 3981676Sjpk crgetcred(void) 3990Sstevel@tonic-gate { 4000Sstevel@tonic-gate cred_t *cr; 4010Sstevel@tonic-gate proc_t *p; 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate p = ttoproc(curthread); 4040Sstevel@tonic-gate mutex_enter(&p->p_crlock); 4050Sstevel@tonic-gate crhold(cr = p->p_cred); 4060Sstevel@tonic-gate mutex_exit(&p->p_crlock); 4070Sstevel@tonic-gate return (cr); 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate /* 4110Sstevel@tonic-gate * Backward compatibility check for suser(). 4120Sstevel@tonic-gate * Accounting flag is now set in the policy functions; auditing is 4130Sstevel@tonic-gate * done through use of privilege in the audit trail. 4140Sstevel@tonic-gate */ 4150Sstevel@tonic-gate int 4160Sstevel@tonic-gate suser(cred_t *cr) 4170Sstevel@tonic-gate { 4180Sstevel@tonic-gate return (PRIV_POLICY(cr, PRIV_SYS_SUSER_COMPAT, B_FALSE, EPERM, NULL) 4190Sstevel@tonic-gate == 0); 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate /* 4230Sstevel@tonic-gate * Determine whether the supplied group id is a member of the group 4240Sstevel@tonic-gate * described by the supplied credentials. 4250Sstevel@tonic-gate */ 4260Sstevel@tonic-gate int 4270Sstevel@tonic-gate groupmember(gid_t gid, const cred_t *cr) 4280Sstevel@tonic-gate { 4290Sstevel@tonic-gate if (gid == cr->cr_gid) 4300Sstevel@tonic-gate return (1); 4310Sstevel@tonic-gate return (supgroupmember(gid, cr)); 4320Sstevel@tonic-gate } 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate /* 4350Sstevel@tonic-gate * As groupmember but only check against the supplemental groups. 4360Sstevel@tonic-gate */ 4370Sstevel@tonic-gate int 4380Sstevel@tonic-gate supgroupmember(gid_t gid, const cred_t *cr) 4390Sstevel@tonic-gate { 4400Sstevel@tonic-gate const gid_t *gp, *endgp; 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate endgp = &cr->cr_groups[cr->cr_ngroups]; 4430Sstevel@tonic-gate for (gp = cr->cr_groups; gp < endgp; gp++) 4440Sstevel@tonic-gate if (*gp == gid) 4450Sstevel@tonic-gate return (1); 4460Sstevel@tonic-gate return (0); 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate /* 4500Sstevel@tonic-gate * This function is called to check whether the credentials set 4510Sstevel@tonic-gate * "scrp" has permission to act on credentials set "tcrp". It enforces the 4520Sstevel@tonic-gate * permission requirements needed to send a signal to a process. 4530Sstevel@tonic-gate * The same requirements are imposed by other system calls, however. 4540Sstevel@tonic-gate * 4550Sstevel@tonic-gate * The rules are: 4560Sstevel@tonic-gate * (1) if the credentials are the same, the check succeeds 4570Sstevel@tonic-gate * (2) if the zone ids don't match, and scrp is not in the global zone or 4580Sstevel@tonic-gate * does not have the PRIV_PROC_ZONE privilege, the check fails 4590Sstevel@tonic-gate * (3) if the real or effective user id of scrp matches the real or saved 4600Sstevel@tonic-gate * user id of tcrp or scrp has the PRIV_PROC_OWNER privilege, the check 4610Sstevel@tonic-gate * succeeds 4620Sstevel@tonic-gate * (4) otherwise, the check fails 4630Sstevel@tonic-gate */ 4640Sstevel@tonic-gate int 4650Sstevel@tonic-gate hasprocperm(const cred_t *tcrp, const cred_t *scrp) 4660Sstevel@tonic-gate { 4670Sstevel@tonic-gate if (scrp == tcrp) 4680Sstevel@tonic-gate return (1); 4690Sstevel@tonic-gate if (scrp->cr_zone != tcrp->cr_zone && 4700Sstevel@tonic-gate (scrp->cr_zone != global_zone || 4710Sstevel@tonic-gate secpolicy_proc_zone(scrp) != 0)) 4720Sstevel@tonic-gate return (0); 4730Sstevel@tonic-gate if (scrp->cr_uid == tcrp->cr_ruid || 4740Sstevel@tonic-gate scrp->cr_ruid == tcrp->cr_ruid || 4750Sstevel@tonic-gate scrp->cr_uid == tcrp->cr_suid || 4760Sstevel@tonic-gate scrp->cr_ruid == tcrp->cr_suid || 4770Sstevel@tonic-gate !PRIV_POLICY(scrp, PRIV_PROC_OWNER, B_FALSE, EPERM, "hasprocperm")) 4780Sstevel@tonic-gate return (1); 4790Sstevel@tonic-gate return (0); 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate /* 4830Sstevel@tonic-gate * This interface replaces hasprocperm; it works like hasprocperm but 4840Sstevel@tonic-gate * additionally returns success if the proc_t's match 4850Sstevel@tonic-gate * It is the preferred interface for most uses. 4860Sstevel@tonic-gate * And it will acquire pcrlock itself, so it assert's that it shouldn't 4870Sstevel@tonic-gate * be held. 4880Sstevel@tonic-gate */ 4890Sstevel@tonic-gate int 4900Sstevel@tonic-gate prochasprocperm(proc_t *tp, proc_t *sp, const cred_t *scrp) 4910Sstevel@tonic-gate { 4920Sstevel@tonic-gate int rets; 4930Sstevel@tonic-gate cred_t *tcrp; 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&tp->p_crlock)); 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate if (tp == sp) 4980Sstevel@tonic-gate return (1); 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate if (tp->p_sessp != sp->p_sessp && secpolicy_basic_proc(scrp) != 0) 5010Sstevel@tonic-gate return (0); 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate mutex_enter(&tp->p_crlock); 5040Sstevel@tonic-gate tcrp = tp->p_cred; 5050Sstevel@tonic-gate rets = hasprocperm(tcrp, scrp); 5060Sstevel@tonic-gate mutex_exit(&tp->p_crlock); 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate return (rets); 5090Sstevel@tonic-gate } 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate /* 5120Sstevel@tonic-gate * This routine is used to compare two credentials to determine if 5130Sstevel@tonic-gate * they refer to the same "user". If the pointers are equal, then 5140Sstevel@tonic-gate * they must refer to the same user. Otherwise, the contents of 5150Sstevel@tonic-gate * the credentials are compared to see whether they are equivalent. 5160Sstevel@tonic-gate * 5170Sstevel@tonic-gate * This routine returns 0 if the credentials refer to the same user, 5180Sstevel@tonic-gate * 1 if they do not. 5190Sstevel@tonic-gate */ 5200Sstevel@tonic-gate int 5210Sstevel@tonic-gate crcmp(const cred_t *cr1, const cred_t *cr2) 5220Sstevel@tonic-gate { 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate if (cr1 == cr2) 5250Sstevel@tonic-gate return (0); 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate if (cr1->cr_uid == cr2->cr_uid && 5280Sstevel@tonic-gate cr1->cr_gid == cr2->cr_gid && 5290Sstevel@tonic-gate cr1->cr_ruid == cr2->cr_ruid && 5300Sstevel@tonic-gate cr1->cr_rgid == cr2->cr_rgid && 5310Sstevel@tonic-gate cr1->cr_ngroups == cr2->cr_ngroups && 5320Sstevel@tonic-gate cr1->cr_zone == cr2->cr_zone && 5330Sstevel@tonic-gate bcmp(cr1->cr_groups, cr2->cr_groups, 5344520Snw141292 cr1->cr_ngroups * sizeof (gid_t)) == 0) { 5350Sstevel@tonic-gate return (!priv_isequalset(&CR_OEPRIV(cr1), &CR_OEPRIV(cr2))); 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate return (1); 5380Sstevel@tonic-gate } 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate /* 5410Sstevel@tonic-gate * Read access functions to cred_t. 5420Sstevel@tonic-gate */ 5430Sstevel@tonic-gate uid_t 5440Sstevel@tonic-gate crgetuid(const cred_t *cr) 5450Sstevel@tonic-gate { 5460Sstevel@tonic-gate return (cr->cr_uid); 5470Sstevel@tonic-gate } 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate uid_t 5500Sstevel@tonic-gate crgetruid(const cred_t *cr) 5510Sstevel@tonic-gate { 5520Sstevel@tonic-gate return (cr->cr_ruid); 5530Sstevel@tonic-gate } 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate uid_t 5560Sstevel@tonic-gate crgetsuid(const cred_t *cr) 5570Sstevel@tonic-gate { 5580Sstevel@tonic-gate return (cr->cr_suid); 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate gid_t 5620Sstevel@tonic-gate crgetgid(const cred_t *cr) 5630Sstevel@tonic-gate { 5640Sstevel@tonic-gate return (cr->cr_gid); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate gid_t 5680Sstevel@tonic-gate crgetrgid(const cred_t *cr) 5690Sstevel@tonic-gate { 5700Sstevel@tonic-gate return (cr->cr_rgid); 5710Sstevel@tonic-gate } 5720Sstevel@tonic-gate 5730Sstevel@tonic-gate gid_t 5740Sstevel@tonic-gate crgetsgid(const cred_t *cr) 5750Sstevel@tonic-gate { 5760Sstevel@tonic-gate return (cr->cr_sgid); 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate const auditinfo_addr_t * 5800Sstevel@tonic-gate crgetauinfo(const cred_t *cr) 5810Sstevel@tonic-gate { 5820Sstevel@tonic-gate return ((const auditinfo_addr_t *)CR_AUINFO(cr)); 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate auditinfo_addr_t * 5860Sstevel@tonic-gate crgetauinfo_modifiable(cred_t *cr) 5870Sstevel@tonic-gate { 5880Sstevel@tonic-gate return (CR_AUINFO(cr)); 5890Sstevel@tonic-gate } 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate zoneid_t 5920Sstevel@tonic-gate crgetzoneid(const cred_t *cr) 5930Sstevel@tonic-gate { 5941676Sjpk return (cr->cr_zone == NULL ? 5951676Sjpk (cr->cr_uid == -1 ? (zoneid_t)-1 : GLOBAL_ZONEID) : 5961676Sjpk cr->cr_zone->zone_id); 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate projid_t 6000Sstevel@tonic-gate crgetprojid(const cred_t *cr) 6010Sstevel@tonic-gate { 6020Sstevel@tonic-gate return (cr->cr_projid); 6030Sstevel@tonic-gate } 6040Sstevel@tonic-gate 6051676Sjpk zone_t * 6061676Sjpk crgetzone(const cred_t *cr) 6071676Sjpk { 6081676Sjpk return (cr->cr_zone); 6091676Sjpk } 6101676Sjpk 6111676Sjpk struct ts_label_s * 6121676Sjpk crgetlabel(const cred_t *cr) 6131676Sjpk { 6141676Sjpk return (cr->cr_label ? 6151676Sjpk cr->cr_label : 6161676Sjpk (cr->cr_zone ? cr->cr_zone->zone_slabel : NULL)); 6171676Sjpk } 6181676Sjpk 6191676Sjpk boolean_t 6201676Sjpk crisremote(const cred_t *cr) 6211676Sjpk { 6221676Sjpk return (REMOTE_PEER_CRED(cr)); 6231676Sjpk } 6241676Sjpk 6254321Scasper #define BADUID(x) ((x) != -1 && !VALID_UID(x)) 6264321Scasper #define BADGID(x) ((x) != -1 && !VALID_GID(x)) 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate int 6290Sstevel@tonic-gate crsetresuid(cred_t *cr, uid_t r, uid_t e, uid_t s) 6300Sstevel@tonic-gate { 6310Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 6320Sstevel@tonic-gate 6334321Scasper if (BADUID(r) || BADUID(e) || BADUID(s)) 6340Sstevel@tonic-gate return (-1); 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate if (r != -1) 6370Sstevel@tonic-gate cr->cr_ruid = r; 6380Sstevel@tonic-gate if (e != -1) 6390Sstevel@tonic-gate cr->cr_uid = e; 6400Sstevel@tonic-gate if (s != -1) 6410Sstevel@tonic-gate cr->cr_suid = s; 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate return (0); 6440Sstevel@tonic-gate } 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate int 6470Sstevel@tonic-gate crsetresgid(cred_t *cr, gid_t r, gid_t e, gid_t s) 6480Sstevel@tonic-gate { 6490Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 6500Sstevel@tonic-gate 6514321Scasper if (BADGID(r) || BADGID(e) || BADGID(s)) 6520Sstevel@tonic-gate return (-1); 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate if (r != -1) 6550Sstevel@tonic-gate cr->cr_rgid = r; 6560Sstevel@tonic-gate if (e != -1) 6570Sstevel@tonic-gate cr->cr_gid = e; 6580Sstevel@tonic-gate if (s != -1) 6590Sstevel@tonic-gate cr->cr_sgid = s; 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate return (0); 6620Sstevel@tonic-gate } 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate int 6650Sstevel@tonic-gate crsetugid(cred_t *cr, uid_t uid, gid_t gid) 6660Sstevel@tonic-gate { 6670Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 6680Sstevel@tonic-gate 6694321Scasper if (!VALID_UID(uid) || !VALID_GID(gid)) 6700Sstevel@tonic-gate return (-1); 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate cr->cr_uid = cr->cr_ruid = cr->cr_suid = uid; 6730Sstevel@tonic-gate cr->cr_gid = cr->cr_rgid = cr->cr_sgid = gid; 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate return (0); 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate int 6790Sstevel@tonic-gate crsetgroups(cred_t *cr, int n, gid_t *grp) 6800Sstevel@tonic-gate { 6810Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate if (n > ngroups_max || n < 0) 6840Sstevel@tonic-gate return (-1); 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate cr->cr_ngroups = n; 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate if (n > 0) 6890Sstevel@tonic-gate bcopy(grp, cr->cr_groups, n * sizeof (gid_t)); 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate return (0); 6920Sstevel@tonic-gate } 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate void 6950Sstevel@tonic-gate crsetprojid(cred_t *cr, projid_t projid) 6960Sstevel@tonic-gate { 6970Sstevel@tonic-gate ASSERT(projid >= 0 && projid <= MAXPROJID); 6980Sstevel@tonic-gate cr->cr_projid = projid; 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate /* 7020Sstevel@tonic-gate * This routine returns the pointer to the first element of the cr_groups 7030Sstevel@tonic-gate * array. It can move around in an implementation defined way. 7040Sstevel@tonic-gate */ 7050Sstevel@tonic-gate const gid_t * 7060Sstevel@tonic-gate crgetgroups(const cred_t *cr) 7070Sstevel@tonic-gate { 7080Sstevel@tonic-gate return (cr->cr_groups); 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate int 7120Sstevel@tonic-gate crgetngroups(const cred_t *cr) 7130Sstevel@tonic-gate { 7140Sstevel@tonic-gate return (cr->cr_ngroups); 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate void 7180Sstevel@tonic-gate cred2prcred(const cred_t *cr, prcred_t *pcrp) 7190Sstevel@tonic-gate { 7200Sstevel@tonic-gate pcrp->pr_euid = cr->cr_uid; 7210Sstevel@tonic-gate pcrp->pr_ruid = cr->cr_ruid; 7220Sstevel@tonic-gate pcrp->pr_suid = cr->cr_suid; 7230Sstevel@tonic-gate pcrp->pr_egid = cr->cr_gid; 7240Sstevel@tonic-gate pcrp->pr_rgid = cr->cr_rgid; 7250Sstevel@tonic-gate pcrp->pr_sgid = cr->cr_sgid; 7260Sstevel@tonic-gate pcrp->pr_ngroups = MIN(cr->cr_ngroups, (uint_t)ngroups_max); 7270Sstevel@tonic-gate pcrp->pr_groups[0] = 0; /* in case ngroups == 0 */ 7280Sstevel@tonic-gate 7290Sstevel@tonic-gate if (pcrp->pr_ngroups != 0) 7300Sstevel@tonic-gate bcopy(cr->cr_groups, pcrp->pr_groups, 7310Sstevel@tonic-gate sizeof (gid_t) * cr->cr_ngroups); 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate 7340Sstevel@tonic-gate static int 7351676Sjpk cred2ucaud(const cred_t *cr, auditinfo64_addr_t *ainfo, const cred_t *rcr) 7360Sstevel@tonic-gate { 7370Sstevel@tonic-gate auditinfo_addr_t *ai; 7380Sstevel@tonic-gate au_tid_addr_t tid; 7390Sstevel@tonic-gate 7401676Sjpk if (secpolicy_audit_getattr(rcr) != 0) 7410Sstevel@tonic-gate return (-1); 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate ai = CR_AUINFO(cr); /* caller makes sure this is non-NULL */ 7440Sstevel@tonic-gate tid = ai->ai_termid; 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate ainfo->ai_auid = ai->ai_auid; 7470Sstevel@tonic-gate ainfo->ai_mask = ai->ai_mask; 7480Sstevel@tonic-gate ainfo->ai_asid = ai->ai_asid; 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate ainfo->ai_termid.at_type = tid.at_type; 7510Sstevel@tonic-gate bcopy(&tid.at_addr, &ainfo->ai_termid.at_addr, 4 * sizeof (uint_t)); 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate ainfo->ai_termid.at_port.at_major = (uint32_t)getmajor(tid.at_port); 7540Sstevel@tonic-gate ainfo->ai_termid.at_port.at_minor = (uint32_t)getminor(tid.at_port); 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate return (0); 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate 7591676Sjpk void 7601676Sjpk cred2uclabel(const cred_t *cr, bslabel_t *labelp) 7611676Sjpk { 7621676Sjpk ts_label_t *tslp; 7631676Sjpk 7641676Sjpk if ((tslp = crgetlabel(cr)) != NULL) 7651676Sjpk bcopy(&tslp->tsl_label, labelp, sizeof (bslabel_t)); 7661676Sjpk } 7671676Sjpk 7680Sstevel@tonic-gate /* 7690Sstevel@tonic-gate * Convert a credential into a "ucred". Allow the caller to specify 7700Sstevel@tonic-gate * and aligned buffer, e.g., in an mblk, so we don't have to allocate 7710Sstevel@tonic-gate * memory and copy it twice. 7721676Sjpk * 7731676Sjpk * This function may call cred2ucaud(), which calls CRED(). Since this 7741676Sjpk * can be called from an interrupt thread, receiver's cred (rcr) is needed 7751676Sjpk * to determine whether audit info should be included. 7760Sstevel@tonic-gate */ 7770Sstevel@tonic-gate struct ucred_s * 7781676Sjpk cred2ucred(const cred_t *cr, pid_t pid, void *buf, const cred_t *rcr) 7790Sstevel@tonic-gate { 7800Sstevel@tonic-gate struct ucred_s *uc; 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate /* The structure isn't always completely filled in, so zero it */ 7830Sstevel@tonic-gate if (buf == NULL) { 7840Sstevel@tonic-gate uc = kmem_zalloc(ucredsize, KM_SLEEP); 7850Sstevel@tonic-gate } else { 7860Sstevel@tonic-gate bzero(buf, ucredsize); 7870Sstevel@tonic-gate uc = buf; 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate uc->uc_size = ucredsize; 7900Sstevel@tonic-gate uc->uc_credoff = UCRED_CRED_OFF; 7910Sstevel@tonic-gate uc->uc_privoff = UCRED_PRIV_OFF; 7920Sstevel@tonic-gate uc->uc_audoff = UCRED_AUD_OFF; 7931676Sjpk uc->uc_labeloff = UCRED_LABEL_OFF; 7940Sstevel@tonic-gate uc->uc_pid = pid; 7950Sstevel@tonic-gate uc->uc_projid = cr->cr_projid; 7960Sstevel@tonic-gate uc->uc_zoneid = crgetzoneid(cr); 7970Sstevel@tonic-gate 7981676Sjpk /* 7991676Sjpk * Note that cred2uclabel() call should not be factored out 8001676Sjpk * to the bottom of the if-else. UCXXX() macros depend on 8011676Sjpk * uc_xxxoff values to work correctly. 8021676Sjpk */ 8031676Sjpk if (REMOTE_PEER_CRED(cr)) { 8041676Sjpk /* 8051676Sjpk * other than label, the rest of cred info about a 8061676Sjpk * remote peer isn't available. 8071676Sjpk */ 8081676Sjpk cred2uclabel(cr, UCLABEL(uc)); 8091676Sjpk uc->uc_credoff = 0; 8101676Sjpk uc->uc_privoff = 0; 8110Sstevel@tonic-gate uc->uc_audoff = 0; 8121676Sjpk } else { 8131676Sjpk cred2prcred(cr, UCCRED(uc)); 8141676Sjpk cred2prpriv(cr, UCPRIV(uc)); 8151676Sjpk if (audoff == 0 || cred2ucaud(cr, UCAUD(uc), rcr) != 0) 8161676Sjpk uc->uc_audoff = 0; 8171676Sjpk cred2uclabel(cr, UCLABEL(uc)); 8181676Sjpk } 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate return (uc); 8210Sstevel@tonic-gate } 8220Sstevel@tonic-gate 8230Sstevel@tonic-gate /* 8240Sstevel@tonic-gate * Get the "ucred" of a process. 8250Sstevel@tonic-gate */ 8260Sstevel@tonic-gate struct ucred_s * 8270Sstevel@tonic-gate pgetucred(proc_t *p) 8280Sstevel@tonic-gate { 8290Sstevel@tonic-gate cred_t *cr; 8300Sstevel@tonic-gate struct ucred_s *uc; 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate mutex_enter(&p->p_crlock); 8330Sstevel@tonic-gate cr = p->p_cred; 8340Sstevel@tonic-gate crhold(cr); 8350Sstevel@tonic-gate mutex_exit(&p->p_crlock); 8360Sstevel@tonic-gate 8371676Sjpk uc = cred2ucred(cr, p->p_pid, NULL, CRED()); 8380Sstevel@tonic-gate crfree(cr); 8390Sstevel@tonic-gate 8400Sstevel@tonic-gate return (uc); 8410Sstevel@tonic-gate } 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate /* 8440Sstevel@tonic-gate * If the reply status is NFSERR_EACCES, it may be because we are 8450Sstevel@tonic-gate * root (no root net access). Check the real uid, if it isn't root 8460Sstevel@tonic-gate * make that the uid instead and retry the call. 8470Sstevel@tonic-gate * Private interface for NFS. 8480Sstevel@tonic-gate */ 8490Sstevel@tonic-gate cred_t * 8500Sstevel@tonic-gate crnetadjust(cred_t *cr) 8510Sstevel@tonic-gate { 8520Sstevel@tonic-gate if (cr->cr_uid == 0 && cr->cr_ruid != 0) { 8530Sstevel@tonic-gate cr = crdup(cr); 8540Sstevel@tonic-gate cr->cr_uid = cr->cr_ruid; 8550Sstevel@tonic-gate return (cr); 8560Sstevel@tonic-gate } 8570Sstevel@tonic-gate return (NULL); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate /* 8610Sstevel@tonic-gate * The reference count is of interest when you want to check 8620Sstevel@tonic-gate * whether it is ok to modify the credential in place. 8630Sstevel@tonic-gate */ 8640Sstevel@tonic-gate uint_t 8650Sstevel@tonic-gate crgetref(const cred_t *cr) 8660Sstevel@tonic-gate { 8670Sstevel@tonic-gate return (cr->cr_ref); 8680Sstevel@tonic-gate } 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate static int 8710Sstevel@tonic-gate get_c2audit_load(void) 8720Sstevel@tonic-gate { 8730Sstevel@tonic-gate static int gotit = 0; 8740Sstevel@tonic-gate static int c2audit_load; 8750Sstevel@tonic-gate u_longlong_t audit_load_val; 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate if (gotit) 8780Sstevel@tonic-gate return (c2audit_load); 8790Sstevel@tonic-gate audit_load_val = 0; /* set default value once */ 8800Sstevel@tonic-gate (void) mod_sysvar("c2audit", "audit_load", &audit_load_val); 8810Sstevel@tonic-gate c2audit_load = (int)audit_load_val; 8820Sstevel@tonic-gate gotit++; 8830Sstevel@tonic-gate return (c2audit_load); 8840Sstevel@tonic-gate } 8850Sstevel@tonic-gate 8860Sstevel@tonic-gate int 8870Sstevel@tonic-gate get_audit_ucrsize(void) 8880Sstevel@tonic-gate { 8890Sstevel@tonic-gate return (get_c2audit_load() ? sizeof (auditinfo64_addr_t) : 0); 8900Sstevel@tonic-gate } 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate /* 8930Sstevel@tonic-gate * Set zone pointer in credential to indicated value. First adds a 8940Sstevel@tonic-gate * hold for the new zone, then drops the hold on previous zone (if any). 8950Sstevel@tonic-gate * This is done in this order in case the old and new zones are the 8960Sstevel@tonic-gate * same. 8970Sstevel@tonic-gate */ 8980Sstevel@tonic-gate void 8990Sstevel@tonic-gate crsetzone(cred_t *cr, zone_t *zptr) 9000Sstevel@tonic-gate { 9010Sstevel@tonic-gate zone_t *oldzptr = cr->cr_zone; 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate ASSERT(cr != kcred); 9040Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 9050Sstevel@tonic-gate cr->cr_zone = zptr; 9060Sstevel@tonic-gate zone_cred_hold(zptr); 9070Sstevel@tonic-gate if (oldzptr) 9080Sstevel@tonic-gate zone_cred_rele(oldzptr); 9090Sstevel@tonic-gate } 9101676Sjpk 9111676Sjpk /* 9121676Sjpk * Create a new cred based on the supplied label 9131676Sjpk */ 9141676Sjpk cred_t * 9151676Sjpk newcred_from_bslabel(bslabel_t *blabel, uint32_t doi, int flags) 9161676Sjpk { 9171676Sjpk ts_label_t *lbl = labelalloc(blabel, doi, flags); 9181676Sjpk cred_t *cr = NULL; 9191676Sjpk 9201676Sjpk if (lbl != NULL) { 9211676Sjpk if ((cr = kmem_cache_alloc(cred_cache, flags)) != NULL) { 9221676Sjpk bcopy(dummycr, cr, crsize); 9231676Sjpk cr->cr_label = lbl; 9241676Sjpk } else { 9251676Sjpk label_rele(lbl); 9261676Sjpk } 9271676Sjpk } 9281676Sjpk 9291676Sjpk return (cr); 9301676Sjpk } 9311676Sjpk 9321676Sjpk /* 9331676Sjpk * Derive a new cred from the existing cred, but with a different label. 9341676Sjpk * To be used when a cred is being shared, but the label needs to be changed 9351676Sjpk * by a caller without affecting other users 9361676Sjpk */ 9371676Sjpk cred_t * 9381676Sjpk copycred_from_bslabel(cred_t *cr, bslabel_t *blabel, uint32_t doi, int flags) 9391676Sjpk { 9401676Sjpk ts_label_t *lbl = labelalloc(blabel, doi, flags); 9411676Sjpk cred_t *newcr = NULL; 9421676Sjpk 9431676Sjpk if (lbl != NULL) { 9441676Sjpk if ((newcr = kmem_cache_alloc(cred_cache, flags)) != NULL) { 9451676Sjpk bcopy(cr, newcr, crsize); 9461676Sjpk if (newcr->cr_zone) 9471676Sjpk zone_cred_hold(newcr->cr_zone); 9481676Sjpk newcr->cr_label = lbl; 9491676Sjpk newcr->cr_ref = 1; 9501676Sjpk } else { 9511676Sjpk label_rele(lbl); 9521676Sjpk } 9531676Sjpk } 9541676Sjpk 9551676Sjpk return (newcr); 9561676Sjpk } 9571676Sjpk 9581676Sjpk /* 9591676Sjpk * This function returns a pointer to the kcred-equivalent in the current zone. 9601676Sjpk */ 9611676Sjpk cred_t * 9621676Sjpk zone_kcred(void) 9631676Sjpk { 9641676Sjpk zone_t *zone; 9651676Sjpk 9661676Sjpk if ((zone = CRED()->cr_zone) != NULL) 9671676Sjpk return (zone->zone_kcred); 9681676Sjpk else 9691676Sjpk return (kcred); 9701676Sjpk } 9714321Scasper 9724321Scasper boolean_t 9734321Scasper valid_ephemeral_uid(uid_t id) 9744321Scasper { 9754321Scasper membar_consumer(); 9764520Snw141292 return (id < IDMAP_WK__MAX_UID || 9774520Snw141292 (id > ephemeral_data.min_uid && id <= ephemeral_data.last_uid)); 9784321Scasper } 9794321Scasper 9804321Scasper boolean_t 9814321Scasper valid_ephemeral_gid(gid_t id) 9824321Scasper { 9834321Scasper membar_consumer(); 9844520Snw141292 return (id < IDMAP_WK__MAX_GID || 9854520Snw141292 (id > ephemeral_data.min_gid && id <= ephemeral_data.last_gid)); 9864321Scasper } 9874321Scasper 9884321Scasper int 9894321Scasper eph_uid_alloc(int flags, uid_t *start, int count) 9904321Scasper { 9914321Scasper mutex_enter(&ephemeral_data.eph_lock); 9924321Scasper 9934321Scasper /* Test for unsigned integer wrap around */ 9944321Scasper if (ephemeral_data.last_uid + count < ephemeral_data.last_uid) { 9954321Scasper mutex_exit(&ephemeral_data.eph_lock); 9964321Scasper return (-1); 9974321Scasper } 9984321Scasper 9994321Scasper /* first call or idmap crashed and state corrupted */ 10004321Scasper if (flags != 0) 10014321Scasper ephemeral_data.min_uid = ephemeral_data.last_uid; 10024321Scasper 10034321Scasper hasephids = B_TRUE; 10044321Scasper *start = ephemeral_data.last_uid + 1; 10054321Scasper atomic_add_32(&ephemeral_data.last_uid, count); 10064321Scasper mutex_exit(&ephemeral_data.eph_lock); 10074321Scasper return (0); 10084321Scasper } 10094321Scasper 10104321Scasper int 10114321Scasper eph_gid_alloc(int flags, gid_t *start, int count) 10124321Scasper { 10134321Scasper mutex_enter(&ephemeral_data.eph_lock); 10144321Scasper 10154321Scasper /* Test for unsigned integer wrap around */ 10164321Scasper if (ephemeral_data.last_gid + count < ephemeral_data.last_gid) { 10174321Scasper mutex_exit(&ephemeral_data.eph_lock); 10184321Scasper return (-1); 10194321Scasper } 10204321Scasper 10214321Scasper /* first call or idmap crashed and state corrupted */ 10224321Scasper if (flags != 0) 10234321Scasper ephemeral_data.min_gid = ephemeral_data.last_gid; 10244321Scasper 10254321Scasper hasephids = B_TRUE; 10264321Scasper *start = ephemeral_data.last_gid + 1; 10274321Scasper atomic_add_32(&ephemeral_data.last_gid, count); 10284321Scasper mutex_exit(&ephemeral_data.eph_lock); 10294321Scasper return (0); 10304321Scasper } 10314321Scasper 10324321Scasper /* 1033*5331Samw * If the credential user SID or group SID is mapped to an ephemeral 1034*5331Samw * ID, map the credential to nobody. 10354321Scasper */ 10364321Scasper cred_t * 10374321Scasper crgetmapped(const cred_t *cr) 10384321Scasper { 10394406Scasper /* 10404406Scasper * Someone incorrectly passed a NULL cred to a vnode operation 10414406Scasper * either on purpose or by calling CRED() in interrupt context. 10424406Scasper */ 10434406Scasper if (cr == NULL) 10444406Scasper return (NULL); 10454406Scasper 10464321Scasper if (cr->cr_ksid != NULL) { 1047*5331Samw if (cr->cr_ksid->kr_sidx[KSID_USER].ks_id > MAXUID) 1048*5331Samw return (ephemeral_data.nobody); 10494321Scasper 1050*5331Samw if (cr->cr_ksid->kr_sidx[KSID_GROUP].ks_id > MAXUID) 1051*5331Samw return (ephemeral_data.nobody); 10524321Scasper } 10534321Scasper 10544321Scasper return ((cred_t *)cr); 10554321Scasper } 10564321Scasper 10574321Scasper /* index should be in range for a ksidindex_t */ 10584321Scasper void 10594321Scasper crsetsid(cred_t *cr, ksid_t *ksp, int index) 10604321Scasper { 10614321Scasper ASSERT(cr->cr_ref <= 2); 10624321Scasper ASSERT(index >= 0 && index < KSID_COUNT); 10634321Scasper if (cr->cr_ksid == NULL && ksp == NULL) 10644321Scasper return; 10654321Scasper cr->cr_ksid = kcrsid_setsid(cr->cr_ksid, ksp, index); 10664321Scasper } 10674321Scasper 10684321Scasper void 10694321Scasper crsetsidlist(cred_t *cr, ksidlist_t *ksl) 10704321Scasper { 10714321Scasper ASSERT(cr->cr_ref <= 2); 10724321Scasper if (cr->cr_ksid == NULL && ksl == NULL) 10734321Scasper return; 10744321Scasper cr->cr_ksid = kcrsid_setsidlist(cr->cr_ksid, ksl); 10754321Scasper } 10764321Scasper 10774321Scasper ksid_t * 10784321Scasper crgetsid(const cred_t *cr, int i) 10794321Scasper { 10804321Scasper ASSERT(i >= 0 && i < KSID_COUNT); 10814321Scasper if (cr->cr_ksid != NULL && cr->cr_ksid->kr_sidx[i].ks_domain) 10824321Scasper return ((ksid_t *)&cr->cr_ksid->kr_sidx[i]); 10834321Scasper return (NULL); 10844321Scasper } 10854321Scasper 10864321Scasper ksidlist_t * 10874321Scasper crgetsidlist(const cred_t *cr) 10884321Scasper { 1089*5331Samw if (cr->cr_ksid != NULL) 10904923Scasper return (cr->cr_ksid->kr_sidlist); 10914321Scasper return (NULL); 10924321Scasper } 1093*5331Samw 1094*5331Samw /* 1095*5331Samw * Interface to set the effective and permitted privileges for 1096*5331Samw * a credential; this interface does no security checks and is 1097*5331Samw * intended for kernel (file)servers creating credentials with 1098*5331Samw * specific privileges. 1099*5331Samw */ 1100*5331Samw int 1101*5331Samw crsetpriv(cred_t *cr, ...) 1102*5331Samw { 1103*5331Samw va_list ap; 1104*5331Samw const char *privnm; 1105*5331Samw 1106*5331Samw ASSERT(cr->cr_ref <= 2); 1107*5331Samw 1108*5331Samw priv_set_PA(cr); 1109*5331Samw 1110*5331Samw va_start(ap, cr); 1111*5331Samw 1112*5331Samw while ((privnm = va_arg(ap, const char *)) != NULL) { 1113*5331Samw int priv = priv_getbyname(privnm, 0); 1114*5331Samw if (priv < 0) 1115*5331Samw return (-1); 1116*5331Samw 1117*5331Samw priv_addset(&CR_PPRIV(cr), priv); 1118*5331Samw priv_addset(&CR_EPRIV(cr), priv); 1119*5331Samw } 1120*5331Samw priv_adjust_PA(cr); 1121*5331Samw va_end(ap); 1122*5331Samw return (0); 1123*5331Samw } 1124