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 /* 225771Sjp151216 * 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> 65*6134Scasper #include <sys/klpd.h> 665331Samw #include <sys/varargs.h> 674321Scasper 685771Sjp151216 695771Sjp151216 /* Ephemeral IDs Zones specific data */ 705771Sjp151216 typedef struct ephemeral_zsd { 715771Sjp151216 uid_t min_uid; 725771Sjp151216 uid_t last_uid; 735771Sjp151216 gid_t min_gid; 745771Sjp151216 gid_t last_gid; 754321Scasper kmutex_t eph_lock; 765771Sjp151216 cred_t *eph_nobody; 775771Sjp151216 } ephemeral_zsd_t; 785771Sjp151216 795771Sjp151216 805771Sjp151216 static kmutex_t ephemeral_zone_mutex; 815771Sjp151216 static zone_key_t ephemeral_zone_key; 820Sstevel@tonic-gate 830Sstevel@tonic-gate static struct kmem_cache *cred_cache; 845771Sjp151216 static size_t crsize = 0; 855771Sjp151216 static int audoff = 0; 865771Sjp151216 uint32_t ucredsize; 875771Sjp151216 cred_t *kcred; 885771Sjp151216 static cred_t *dummycr; 890Sstevel@tonic-gate 900Sstevel@tonic-gate int rstlink; /* link(2) restricted to files owned by user? */ 910Sstevel@tonic-gate 920Sstevel@tonic-gate static int get_c2audit_load(void); 930Sstevel@tonic-gate 940Sstevel@tonic-gate #define CR_AUINFO(c) (auditinfo_addr_t *)((audoff == 0) ? NULL : \ 950Sstevel@tonic-gate ((char *)(c)) + audoff) 960Sstevel@tonic-gate 971676Sjpk #define REMOTE_PEER_CRED(c) ((c)->cr_gid == -1) 980Sstevel@tonic-gate 994321Scasper 1004321Scasper static boolean_t hasephids = B_FALSE; 1014321Scasper 1025771Sjp151216 static ephemeral_zsd_t * 1035771Sjp151216 get_ephemeral_zsd(zone_t *zone) 1045771Sjp151216 { 1055771Sjp151216 ephemeral_zsd_t *eph_zsd; 1065771Sjp151216 1075771Sjp151216 eph_zsd = zone_getspecific(ephemeral_zone_key, zone); 1085771Sjp151216 if (eph_zsd != NULL) { 1095771Sjp151216 return (eph_zsd); 1105771Sjp151216 } 1115771Sjp151216 1125771Sjp151216 mutex_enter(&ephemeral_zone_mutex); 1135771Sjp151216 eph_zsd = zone_getspecific(ephemeral_zone_key, zone); 1145771Sjp151216 if (eph_zsd == NULL) { 1155771Sjp151216 eph_zsd = kmem_zalloc(sizeof (ephemeral_zsd_t), KM_SLEEP); 1165771Sjp151216 eph_zsd->min_uid = MAXUID; 1175771Sjp151216 eph_zsd->last_uid = IDMAP_WK__MAX_UID; 1185771Sjp151216 eph_zsd->min_gid = MAXUID; 1195771Sjp151216 eph_zsd->last_gid = IDMAP_WK__MAX_GID; 1205771Sjp151216 mutex_init(&eph_zsd->eph_lock, NULL, MUTEX_DEFAULT, NULL); 1215771Sjp151216 1225771Sjp151216 /* 1235771Sjp151216 * nobody is used to map SID containing CRs. 1245771Sjp151216 */ 1255771Sjp151216 eph_zsd->eph_nobody = crdup(zone->zone_kcred); 1265771Sjp151216 (void) crsetugid(eph_zsd->eph_nobody, UID_NOBODY, GID_NOBODY); 1275771Sjp151216 CR_FLAGS(eph_zsd->eph_nobody) = 0; 1285771Sjp151216 eph_zsd->eph_nobody->cr_zone = zone; 1295771Sjp151216 1305771Sjp151216 (void) zone_setspecific(ephemeral_zone_key, zone, eph_zsd); 1315771Sjp151216 } 1325771Sjp151216 mutex_exit(&ephemeral_zone_mutex); 1335771Sjp151216 return (eph_zsd); 1345771Sjp151216 } 1355771Sjp151216 136*6134Scasper static cred_t *crdup_flags(cred_t *, int); 137*6134Scasper static cred_t *cralloc_flags(int); 138*6134Scasper 1395771Sjp151216 /* 1405771Sjp151216 * This function is called when a zone is destroyed 1415771Sjp151216 */ 1425771Sjp151216 static void 1435771Sjp151216 /* ARGSUSED */ 1445771Sjp151216 destroy_ephemeral_zsd(zoneid_t zone_id, void *arg) 1455771Sjp151216 { 1465771Sjp151216 ephemeral_zsd_t *eph_zsd = arg; 1475771Sjp151216 if (eph_zsd != NULL) { 1485771Sjp151216 mutex_destroy(&eph_zsd->eph_lock); 1495771Sjp151216 crfree(eph_zsd->eph_nobody); 1505771Sjp151216 kmem_free(eph_zsd, sizeof (ephemeral_zsd_t)); 1515771Sjp151216 } 1525771Sjp151216 } 1535771Sjp151216 1545771Sjp151216 1555771Sjp151216 1564321Scasper /* 1570Sstevel@tonic-gate * Initialize credentials data structures. 1580Sstevel@tonic-gate */ 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate void 1610Sstevel@tonic-gate cred_init(void) 1620Sstevel@tonic-gate { 1630Sstevel@tonic-gate priv_init(); 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate crsize = sizeof (cred_t) + sizeof (gid_t) * (ngroups_max - 1); 1660Sstevel@tonic-gate /* 1670Sstevel@tonic-gate * Make sure it's word-aligned. 1680Sstevel@tonic-gate */ 1690Sstevel@tonic-gate crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1); 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate if (get_c2audit_load() > 0) { 1720Sstevel@tonic-gate #ifdef _LP64 1730Sstevel@tonic-gate /* assure audit context is 64-bit aligned */ 1740Sstevel@tonic-gate audoff = (crsize + 1750Sstevel@tonic-gate sizeof (int64_t) - 1) & ~(sizeof (int64_t) - 1); 1760Sstevel@tonic-gate #else /* _LP64 */ 1770Sstevel@tonic-gate audoff = crsize; 1780Sstevel@tonic-gate #endif /* _LP64 */ 1790Sstevel@tonic-gate crsize = audoff + sizeof (auditinfo_addr_t); 1800Sstevel@tonic-gate crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1); 1810Sstevel@tonic-gate } 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate cred_cache = kmem_cache_create("cred_cache", crsize, 0, 1844520Snw141292 NULL, NULL, NULL, NULL, NULL, 0); 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* 1871676Sjpk * dummycr is used to copy initial state for creds. 1881676Sjpk */ 1891676Sjpk dummycr = cralloc(); 1901676Sjpk bzero(dummycr, crsize); 1911676Sjpk dummycr->cr_ref = 1; 1924321Scasper dummycr->cr_uid = (uid_t)-1; 1934321Scasper dummycr->cr_gid = (gid_t)-1; 1944321Scasper dummycr->cr_ruid = (uid_t)-1; 1954321Scasper dummycr->cr_rgid = (gid_t)-1; 1964321Scasper dummycr->cr_suid = (uid_t)-1; 1974321Scasper dummycr->cr_sgid = (gid_t)-1; 1984321Scasper 1991676Sjpk 2001676Sjpk /* 2010Sstevel@tonic-gate * kcred is used by anything that needs all privileges; it's 2020Sstevel@tonic-gate * also the template used for crget as it has all the compatible 2030Sstevel@tonic-gate * sets filled in. 2040Sstevel@tonic-gate */ 2050Sstevel@tonic-gate kcred = cralloc(); 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate bzero(kcred, crsize); 2080Sstevel@tonic-gate kcred->cr_ref = 1; 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate /* kcred is never freed, so we don't need zone_cred_hold here */ 2110Sstevel@tonic-gate kcred->cr_zone = &zone0; 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate priv_fillset(&CR_LPRIV(kcred)); 2140Sstevel@tonic-gate CR_IPRIV(kcred) = *priv_basic; 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* Not a basic privilege, if chown is not restricted add it to I0 */ 2170Sstevel@tonic-gate if (!rstchown) 2180Sstevel@tonic-gate priv_addset(&CR_IPRIV(kcred), PRIV_FILE_CHOWN_SELF); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate /* Basic privilege, if link is restricted remove it from I0 */ 2210Sstevel@tonic-gate if (rstlink) 2220Sstevel@tonic-gate priv_delset(&CR_IPRIV(kcred), PRIV_FILE_LINK_ANY); 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate CR_EPRIV(kcred) = CR_PPRIV(kcred) = CR_IPRIV(kcred); 2251676Sjpk 2261676Sjpk CR_FLAGS(kcred) = NET_MAC_AWARE; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* 2290Sstevel@tonic-gate * Set up credentials of p0. 2300Sstevel@tonic-gate */ 2310Sstevel@tonic-gate ttoproc(curthread)->p_cred = kcred; 2320Sstevel@tonic-gate curthread->t_cred = kcred; 2330Sstevel@tonic-gate 2345771Sjp151216 ucredsize = UCRED_SIZE; 2354321Scasper 2365771Sjp151216 mutex_init(&ephemeral_zone_mutex, NULL, MUTEX_DEFAULT, NULL); 2375771Sjp151216 zone_key_create(&ephemeral_zone_key, NULL, NULL, destroy_ephemeral_zsd); 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate /* 2410Sstevel@tonic-gate * Allocate (nearly) uninitialized cred_t. 2420Sstevel@tonic-gate */ 243*6134Scasper static cred_t * 244*6134Scasper cralloc_flags(int flgs) 2450Sstevel@tonic-gate { 246*6134Scasper cred_t *cr = kmem_cache_alloc(cred_cache, flgs); 247*6134Scasper 248*6134Scasper if (cr == NULL) 249*6134Scasper return (NULL); 250*6134Scasper 2510Sstevel@tonic-gate cr->cr_ref = 1; /* So we can crfree() */ 2520Sstevel@tonic-gate cr->cr_zone = NULL; 2531676Sjpk cr->cr_label = NULL; 2544321Scasper cr->cr_ksid = NULL; 255*6134Scasper cr->cr_klpd = NULL; 2564321Scasper return (cr); 2574321Scasper } 2584321Scasper 259*6134Scasper cred_t * 260*6134Scasper cralloc(void) 261*6134Scasper { 262*6134Scasper return (cralloc_flags(KM_SLEEP)); 263*6134Scasper } 264*6134Scasper 2654321Scasper /* 2664321Scasper * As cralloc but prepared for ksid change (if appropriate). 2674321Scasper */ 2684321Scasper cred_t * 2694321Scasper cralloc_ksid(void) 2704321Scasper { 2714321Scasper cred_t *cr = cralloc(); 2724321Scasper if (hasephids) 2734321Scasper cr->cr_ksid = kcrsid_alloc(); 2740Sstevel@tonic-gate return (cr); 2750Sstevel@tonic-gate } 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate /* 2780Sstevel@tonic-gate * Allocate a initialized cred structure and crhold() it. 2790Sstevel@tonic-gate * Initialized means: all ids 0, group count 0, L=Full, E=P=I=I0 2800Sstevel@tonic-gate */ 2810Sstevel@tonic-gate cred_t * 2820Sstevel@tonic-gate crget(void) 2830Sstevel@tonic-gate { 2840Sstevel@tonic-gate cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP); 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate bcopy(kcred, cr, crsize); 2870Sstevel@tonic-gate cr->cr_ref = 1; 2880Sstevel@tonic-gate zone_cred_hold(cr->cr_zone); 2891676Sjpk if (cr->cr_label) 2901676Sjpk label_hold(cr->cr_label); 291*6134Scasper ASSERT(cr->cr_klpd == NULL); 2920Sstevel@tonic-gate return (cr); 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate /* 2960Sstevel@tonic-gate * Broadcast the cred to all the threads in the process. 2970Sstevel@tonic-gate * The current thread's credentials can be set right away, but other 2980Sstevel@tonic-gate * threads must wait until the start of the next system call or trap. 2990Sstevel@tonic-gate * This avoids changing the cred in the middle of a system call. 3000Sstevel@tonic-gate * 3010Sstevel@tonic-gate * The cred has already been held for the process and the thread (2 holds), 3020Sstevel@tonic-gate * and p->p_cred set. 3030Sstevel@tonic-gate * 3040Sstevel@tonic-gate * p->p_crlock shouldn't be held here, since p_lock must be acquired. 3050Sstevel@tonic-gate */ 3060Sstevel@tonic-gate void 3070Sstevel@tonic-gate crset(proc_t *p, cred_t *cr) 3080Sstevel@tonic-gate { 3090Sstevel@tonic-gate kthread_id_t t; 3100Sstevel@tonic-gate kthread_id_t first; 3110Sstevel@tonic-gate cred_t *oldcr; 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate ASSERT(p == curproc); /* assumes p_lwpcnt can't change */ 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate /* 3160Sstevel@tonic-gate * DTrace accesses t_cred in probe context. t_cred must always be 3170Sstevel@tonic-gate * either NULL, or point to a valid, allocated cred structure. 3180Sstevel@tonic-gate */ 3190Sstevel@tonic-gate t = curthread; 3200Sstevel@tonic-gate oldcr = t->t_cred; 3210Sstevel@tonic-gate t->t_cred = cr; /* the cred is held by caller for this thread */ 3220Sstevel@tonic-gate crfree(oldcr); /* free the old cred for the thread */ 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate /* 3250Sstevel@tonic-gate * Broadcast to other threads, if any. 3260Sstevel@tonic-gate */ 3270Sstevel@tonic-gate if (p->p_lwpcnt > 1) { 3280Sstevel@tonic-gate mutex_enter(&p->p_lock); /* to keep thread list safe */ 3290Sstevel@tonic-gate first = curthread; 3300Sstevel@tonic-gate for (t = first->t_forw; t != first; t = t->t_forw) 3310Sstevel@tonic-gate t->t_pre_sys = 1; /* so syscall will get new cred */ 3320Sstevel@tonic-gate mutex_exit(&p->p_lock); 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate /* 3370Sstevel@tonic-gate * Put a hold on a cred structure. 3380Sstevel@tonic-gate */ 3390Sstevel@tonic-gate void 3400Sstevel@tonic-gate crhold(cred_t *cr) 3410Sstevel@tonic-gate { 3420Sstevel@tonic-gate atomic_add_32(&cr->cr_ref, 1); 3430Sstevel@tonic-gate } 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate /* 3460Sstevel@tonic-gate * Release previous hold on a cred structure. Free it if refcnt == 0. 3471676Sjpk * If cred uses label different from zone label, free it. 3480Sstevel@tonic-gate */ 3490Sstevel@tonic-gate void 3500Sstevel@tonic-gate crfree(cred_t *cr) 3510Sstevel@tonic-gate { 3520Sstevel@tonic-gate if (atomic_add_32_nv(&cr->cr_ref, -1) == 0) { 3530Sstevel@tonic-gate ASSERT(cr != kcred); 3541676Sjpk if (cr->cr_label) 3551676Sjpk label_rele(cr->cr_label); 356*6134Scasper if (cr->cr_klpd) 357*6134Scasper crklpd_rele(cr->cr_klpd); 3580Sstevel@tonic-gate if (cr->cr_zone) 3590Sstevel@tonic-gate zone_cred_rele(cr->cr_zone); 3604321Scasper if (cr->cr_ksid) 3614321Scasper kcrsid_rele(cr->cr_ksid); 3620Sstevel@tonic-gate kmem_cache_free(cred_cache, cr); 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate /* 3670Sstevel@tonic-gate * Copy a cred structure to a new one and free the old one. 3680Sstevel@tonic-gate * The new cred will have two references. One for the calling process, 3690Sstevel@tonic-gate * and one for the thread. 3700Sstevel@tonic-gate */ 3710Sstevel@tonic-gate cred_t * 3720Sstevel@tonic-gate crcopy(cred_t *cr) 3730Sstevel@tonic-gate { 3740Sstevel@tonic-gate cred_t *newcr; 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate newcr = cralloc(); 3770Sstevel@tonic-gate bcopy(cr, newcr, crsize); 3780Sstevel@tonic-gate if (newcr->cr_zone) 3790Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 3801676Sjpk if (newcr->cr_label) 381*6134Scasper label_hold(newcr->cr_label); 3824321Scasper if (newcr->cr_ksid) 383*6134Scasper kcrsid_hold(newcr->cr_ksid); 384*6134Scasper if (newcr->cr_klpd) 385*6134Scasper crklpd_hold(newcr->cr_klpd); 3860Sstevel@tonic-gate crfree(cr); 3870Sstevel@tonic-gate newcr->cr_ref = 2; /* caller gets two references */ 3880Sstevel@tonic-gate return (newcr); 3890Sstevel@tonic-gate } 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate /* 3920Sstevel@tonic-gate * Copy a cred structure to a new one and free the old one. 3930Sstevel@tonic-gate * The new cred will have two references. One for the calling process, 3940Sstevel@tonic-gate * and one for the thread. 3950Sstevel@tonic-gate * This variation on crcopy uses a pre-allocated structure for the 3960Sstevel@tonic-gate * "new" cred. 3970Sstevel@tonic-gate */ 3980Sstevel@tonic-gate void 3990Sstevel@tonic-gate crcopy_to(cred_t *oldcr, cred_t *newcr) 4000Sstevel@tonic-gate { 4014321Scasper credsid_t *nkcr = newcr->cr_ksid; 4024321Scasper 4030Sstevel@tonic-gate bcopy(oldcr, newcr, crsize); 4040Sstevel@tonic-gate if (newcr->cr_zone) 4050Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 4061676Sjpk if (newcr->cr_label) 4071676Sjpk label_hold(newcr->cr_label); 408*6134Scasper if (newcr->cr_klpd) 409*6134Scasper crklpd_hold(newcr->cr_klpd); 4104321Scasper if (nkcr) { 4114321Scasper newcr->cr_ksid = nkcr; 4124321Scasper kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid); 4134321Scasper } else if (newcr->cr_ksid) 4144321Scasper kcrsid_hold(newcr->cr_ksid); 4150Sstevel@tonic-gate crfree(oldcr); 4160Sstevel@tonic-gate newcr->cr_ref = 2; /* caller gets two references */ 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate /* 4200Sstevel@tonic-gate * Dup a cred struct to a new held one. 4210Sstevel@tonic-gate * The old cred is not freed. 4220Sstevel@tonic-gate */ 423*6134Scasper static cred_t * 424*6134Scasper crdup_flags(cred_t *cr, int flgs) 4250Sstevel@tonic-gate { 4260Sstevel@tonic-gate cred_t *newcr; 4270Sstevel@tonic-gate 428*6134Scasper newcr = cralloc_flags(flgs); 429*6134Scasper 430*6134Scasper if (newcr == NULL) 431*6134Scasper return (NULL); 432*6134Scasper 4330Sstevel@tonic-gate bcopy(cr, newcr, crsize); 4340Sstevel@tonic-gate if (newcr->cr_zone) 4350Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 4361676Sjpk if (newcr->cr_label) 4371676Sjpk label_hold(newcr->cr_label); 438*6134Scasper if (newcr->cr_klpd) 439*6134Scasper crklpd_hold(newcr->cr_klpd); 4404321Scasper if (newcr->cr_ksid) 4414321Scasper kcrsid_hold(newcr->cr_ksid); 4420Sstevel@tonic-gate newcr->cr_ref = 1; 4430Sstevel@tonic-gate return (newcr); 4440Sstevel@tonic-gate } 4450Sstevel@tonic-gate 446*6134Scasper cred_t * 447*6134Scasper crdup(cred_t *cr) 448*6134Scasper { 449*6134Scasper return (crdup_flags(cr, KM_SLEEP)); 450*6134Scasper } 451*6134Scasper 4520Sstevel@tonic-gate /* 4530Sstevel@tonic-gate * Dup a cred struct to a new held one. 4540Sstevel@tonic-gate * The old cred is not freed. 4550Sstevel@tonic-gate * This variation on crdup uses a pre-allocated structure for the 4560Sstevel@tonic-gate * "new" cred. 4570Sstevel@tonic-gate */ 4580Sstevel@tonic-gate void 4590Sstevel@tonic-gate crdup_to(cred_t *oldcr, cred_t *newcr) 4600Sstevel@tonic-gate { 4614321Scasper credsid_t *nkcr = newcr->cr_ksid; 4624321Scasper 4630Sstevel@tonic-gate bcopy(oldcr, newcr, crsize); 4640Sstevel@tonic-gate if (newcr->cr_zone) 4650Sstevel@tonic-gate zone_cred_hold(newcr->cr_zone); 4661676Sjpk if (newcr->cr_label) 4671676Sjpk label_hold(newcr->cr_label); 468*6134Scasper if (newcr->cr_klpd) 469*6134Scasper crklpd_hold(newcr->cr_klpd); 4704321Scasper if (nkcr) { 4714321Scasper newcr->cr_ksid = nkcr; 4724321Scasper kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid); 4734321Scasper } else if (newcr->cr_ksid) 4744321Scasper kcrsid_hold(newcr->cr_ksid); 4750Sstevel@tonic-gate newcr->cr_ref = 1; 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate /* 4790Sstevel@tonic-gate * Return the (held) credentials for the current running process. 4800Sstevel@tonic-gate */ 4810Sstevel@tonic-gate cred_t * 4821676Sjpk crgetcred(void) 4830Sstevel@tonic-gate { 4840Sstevel@tonic-gate cred_t *cr; 4850Sstevel@tonic-gate proc_t *p; 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate p = ttoproc(curthread); 4880Sstevel@tonic-gate mutex_enter(&p->p_crlock); 4890Sstevel@tonic-gate crhold(cr = p->p_cred); 4900Sstevel@tonic-gate mutex_exit(&p->p_crlock); 4910Sstevel@tonic-gate return (cr); 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate /* 4950Sstevel@tonic-gate * Backward compatibility check for suser(). 4960Sstevel@tonic-gate * Accounting flag is now set in the policy functions; auditing is 4970Sstevel@tonic-gate * done through use of privilege in the audit trail. 4980Sstevel@tonic-gate */ 4990Sstevel@tonic-gate int 5000Sstevel@tonic-gate suser(cred_t *cr) 5010Sstevel@tonic-gate { 5020Sstevel@tonic-gate return (PRIV_POLICY(cr, PRIV_SYS_SUSER_COMPAT, B_FALSE, EPERM, NULL) 5030Sstevel@tonic-gate == 0); 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate /* 5070Sstevel@tonic-gate * Determine whether the supplied group id is a member of the group 5080Sstevel@tonic-gate * described by the supplied credentials. 5090Sstevel@tonic-gate */ 5100Sstevel@tonic-gate int 5110Sstevel@tonic-gate groupmember(gid_t gid, const cred_t *cr) 5120Sstevel@tonic-gate { 5130Sstevel@tonic-gate if (gid == cr->cr_gid) 5140Sstevel@tonic-gate return (1); 5150Sstevel@tonic-gate return (supgroupmember(gid, cr)); 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate /* 5190Sstevel@tonic-gate * As groupmember but only check against the supplemental groups. 5200Sstevel@tonic-gate */ 5210Sstevel@tonic-gate int 5220Sstevel@tonic-gate supgroupmember(gid_t gid, const cred_t *cr) 5230Sstevel@tonic-gate { 5240Sstevel@tonic-gate const gid_t *gp, *endgp; 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate endgp = &cr->cr_groups[cr->cr_ngroups]; 5270Sstevel@tonic-gate for (gp = cr->cr_groups; gp < endgp; gp++) 5280Sstevel@tonic-gate if (*gp == gid) 5290Sstevel@tonic-gate return (1); 5300Sstevel@tonic-gate return (0); 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate /* 5340Sstevel@tonic-gate * This function is called to check whether the credentials set 5350Sstevel@tonic-gate * "scrp" has permission to act on credentials set "tcrp". It enforces the 5360Sstevel@tonic-gate * permission requirements needed to send a signal to a process. 5370Sstevel@tonic-gate * The same requirements are imposed by other system calls, however. 5380Sstevel@tonic-gate * 5390Sstevel@tonic-gate * The rules are: 5400Sstevel@tonic-gate * (1) if the credentials are the same, the check succeeds 5410Sstevel@tonic-gate * (2) if the zone ids don't match, and scrp is not in the global zone or 5420Sstevel@tonic-gate * does not have the PRIV_PROC_ZONE privilege, the check fails 5430Sstevel@tonic-gate * (3) if the real or effective user id of scrp matches the real or saved 5440Sstevel@tonic-gate * user id of tcrp or scrp has the PRIV_PROC_OWNER privilege, the check 5450Sstevel@tonic-gate * succeeds 5460Sstevel@tonic-gate * (4) otherwise, the check fails 5470Sstevel@tonic-gate */ 5480Sstevel@tonic-gate int 5490Sstevel@tonic-gate hasprocperm(const cred_t *tcrp, const cred_t *scrp) 5500Sstevel@tonic-gate { 5510Sstevel@tonic-gate if (scrp == tcrp) 5520Sstevel@tonic-gate return (1); 5530Sstevel@tonic-gate if (scrp->cr_zone != tcrp->cr_zone && 5540Sstevel@tonic-gate (scrp->cr_zone != global_zone || 5550Sstevel@tonic-gate secpolicy_proc_zone(scrp) != 0)) 5560Sstevel@tonic-gate return (0); 5570Sstevel@tonic-gate if (scrp->cr_uid == tcrp->cr_ruid || 5580Sstevel@tonic-gate scrp->cr_ruid == tcrp->cr_ruid || 5590Sstevel@tonic-gate scrp->cr_uid == tcrp->cr_suid || 5600Sstevel@tonic-gate scrp->cr_ruid == tcrp->cr_suid || 5610Sstevel@tonic-gate !PRIV_POLICY(scrp, PRIV_PROC_OWNER, B_FALSE, EPERM, "hasprocperm")) 5620Sstevel@tonic-gate return (1); 5630Sstevel@tonic-gate return (0); 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate /* 5670Sstevel@tonic-gate * This interface replaces hasprocperm; it works like hasprocperm but 5680Sstevel@tonic-gate * additionally returns success if the proc_t's match 5690Sstevel@tonic-gate * It is the preferred interface for most uses. 570*6134Scasper * And it will acquire p_crlock itself, so it assert's that it shouldn't 5710Sstevel@tonic-gate * be held. 5720Sstevel@tonic-gate */ 5730Sstevel@tonic-gate int 5740Sstevel@tonic-gate prochasprocperm(proc_t *tp, proc_t *sp, const cred_t *scrp) 5750Sstevel@tonic-gate { 5760Sstevel@tonic-gate int rets; 5770Sstevel@tonic-gate cred_t *tcrp; 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&tp->p_crlock)); 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate if (tp == sp) 5820Sstevel@tonic-gate return (1); 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate if (tp->p_sessp != sp->p_sessp && secpolicy_basic_proc(scrp) != 0) 5850Sstevel@tonic-gate return (0); 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate mutex_enter(&tp->p_crlock); 588*6134Scasper crhold(tcrp = tp->p_cred); 589*6134Scasper mutex_exit(&tp->p_crlock); 5900Sstevel@tonic-gate rets = hasprocperm(tcrp, scrp); 591*6134Scasper crfree(tcrp); 5920Sstevel@tonic-gate 5930Sstevel@tonic-gate return (rets); 5940Sstevel@tonic-gate } 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate /* 5970Sstevel@tonic-gate * This routine is used to compare two credentials to determine if 5980Sstevel@tonic-gate * they refer to the same "user". If the pointers are equal, then 5990Sstevel@tonic-gate * they must refer to the same user. Otherwise, the contents of 6000Sstevel@tonic-gate * the credentials are compared to see whether they are equivalent. 6010Sstevel@tonic-gate * 6020Sstevel@tonic-gate * This routine returns 0 if the credentials refer to the same user, 6030Sstevel@tonic-gate * 1 if they do not. 6040Sstevel@tonic-gate */ 6050Sstevel@tonic-gate int 6060Sstevel@tonic-gate crcmp(const cred_t *cr1, const cred_t *cr2) 6070Sstevel@tonic-gate { 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate if (cr1 == cr2) 6100Sstevel@tonic-gate return (0); 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate if (cr1->cr_uid == cr2->cr_uid && 6130Sstevel@tonic-gate cr1->cr_gid == cr2->cr_gid && 6140Sstevel@tonic-gate cr1->cr_ruid == cr2->cr_ruid && 6150Sstevel@tonic-gate cr1->cr_rgid == cr2->cr_rgid && 6160Sstevel@tonic-gate cr1->cr_ngroups == cr2->cr_ngroups && 6170Sstevel@tonic-gate cr1->cr_zone == cr2->cr_zone && 6180Sstevel@tonic-gate bcmp(cr1->cr_groups, cr2->cr_groups, 6194520Snw141292 cr1->cr_ngroups * sizeof (gid_t)) == 0) { 6200Sstevel@tonic-gate return (!priv_isequalset(&CR_OEPRIV(cr1), &CR_OEPRIV(cr2))); 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate return (1); 6230Sstevel@tonic-gate } 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate /* 6260Sstevel@tonic-gate * Read access functions to cred_t. 6270Sstevel@tonic-gate */ 6280Sstevel@tonic-gate uid_t 6290Sstevel@tonic-gate crgetuid(const cred_t *cr) 6300Sstevel@tonic-gate { 6310Sstevel@tonic-gate return (cr->cr_uid); 6320Sstevel@tonic-gate } 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate uid_t 6350Sstevel@tonic-gate crgetruid(const cred_t *cr) 6360Sstevel@tonic-gate { 6370Sstevel@tonic-gate return (cr->cr_ruid); 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate uid_t 6410Sstevel@tonic-gate crgetsuid(const cred_t *cr) 6420Sstevel@tonic-gate { 6430Sstevel@tonic-gate return (cr->cr_suid); 6440Sstevel@tonic-gate } 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate gid_t 6470Sstevel@tonic-gate crgetgid(const cred_t *cr) 6480Sstevel@tonic-gate { 6490Sstevel@tonic-gate return (cr->cr_gid); 6500Sstevel@tonic-gate } 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate gid_t 6530Sstevel@tonic-gate crgetrgid(const cred_t *cr) 6540Sstevel@tonic-gate { 6550Sstevel@tonic-gate return (cr->cr_rgid); 6560Sstevel@tonic-gate } 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate gid_t 6590Sstevel@tonic-gate crgetsgid(const cred_t *cr) 6600Sstevel@tonic-gate { 6610Sstevel@tonic-gate return (cr->cr_sgid); 6620Sstevel@tonic-gate } 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate const auditinfo_addr_t * 6650Sstevel@tonic-gate crgetauinfo(const cred_t *cr) 6660Sstevel@tonic-gate { 6670Sstevel@tonic-gate return ((const auditinfo_addr_t *)CR_AUINFO(cr)); 6680Sstevel@tonic-gate } 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate auditinfo_addr_t * 6710Sstevel@tonic-gate crgetauinfo_modifiable(cred_t *cr) 6720Sstevel@tonic-gate { 6730Sstevel@tonic-gate return (CR_AUINFO(cr)); 6740Sstevel@tonic-gate } 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate zoneid_t 6770Sstevel@tonic-gate crgetzoneid(const cred_t *cr) 6780Sstevel@tonic-gate { 6791676Sjpk return (cr->cr_zone == NULL ? 6801676Sjpk (cr->cr_uid == -1 ? (zoneid_t)-1 : GLOBAL_ZONEID) : 6811676Sjpk cr->cr_zone->zone_id); 6820Sstevel@tonic-gate } 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate projid_t 6850Sstevel@tonic-gate crgetprojid(const cred_t *cr) 6860Sstevel@tonic-gate { 6870Sstevel@tonic-gate return (cr->cr_projid); 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate 6901676Sjpk zone_t * 6911676Sjpk crgetzone(const cred_t *cr) 6921676Sjpk { 6931676Sjpk return (cr->cr_zone); 6941676Sjpk } 6951676Sjpk 6961676Sjpk struct ts_label_s * 6971676Sjpk crgetlabel(const cred_t *cr) 6981676Sjpk { 6991676Sjpk return (cr->cr_label ? 7001676Sjpk cr->cr_label : 7011676Sjpk (cr->cr_zone ? cr->cr_zone->zone_slabel : NULL)); 7021676Sjpk } 7031676Sjpk 7041676Sjpk boolean_t 7051676Sjpk crisremote(const cred_t *cr) 7061676Sjpk { 7071676Sjpk return (REMOTE_PEER_CRED(cr)); 7081676Sjpk } 7091676Sjpk 7105771Sjp151216 #define BADUID(x, zn) ((x) != -1 && !VALID_UID((x), (zn))) 7115771Sjp151216 #define BADGID(x, zn) ((x) != -1 && !VALID_GID((x), (zn))) 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate int 7140Sstevel@tonic-gate crsetresuid(cred_t *cr, uid_t r, uid_t e, uid_t s) 7150Sstevel@tonic-gate { 7165771Sjp151216 zone_t *zone = crgetzone(cr); 7175771Sjp151216 7180Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 7190Sstevel@tonic-gate 7205771Sjp151216 if (BADUID(r, zone) || BADUID(e, zone) || BADUID(s, zone)) 7210Sstevel@tonic-gate return (-1); 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate if (r != -1) 7240Sstevel@tonic-gate cr->cr_ruid = r; 7250Sstevel@tonic-gate if (e != -1) 7260Sstevel@tonic-gate cr->cr_uid = e; 7270Sstevel@tonic-gate if (s != -1) 7280Sstevel@tonic-gate cr->cr_suid = s; 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate return (0); 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate int 7340Sstevel@tonic-gate crsetresgid(cred_t *cr, gid_t r, gid_t e, gid_t s) 7350Sstevel@tonic-gate { 7365771Sjp151216 zone_t *zone = crgetzone(cr); 7375771Sjp151216 7380Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 7390Sstevel@tonic-gate 7405771Sjp151216 if (BADGID(r, zone) || BADGID(e, zone) || BADGID(s, zone)) 7410Sstevel@tonic-gate return (-1); 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate if (r != -1) 7440Sstevel@tonic-gate cr->cr_rgid = r; 7450Sstevel@tonic-gate if (e != -1) 7460Sstevel@tonic-gate cr->cr_gid = e; 7470Sstevel@tonic-gate if (s != -1) 7480Sstevel@tonic-gate cr->cr_sgid = s; 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate return (0); 7510Sstevel@tonic-gate } 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate int 7540Sstevel@tonic-gate crsetugid(cred_t *cr, uid_t uid, gid_t gid) 7550Sstevel@tonic-gate { 7565771Sjp151216 zone_t *zone = crgetzone(cr); 7575771Sjp151216 7580Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 7590Sstevel@tonic-gate 7605771Sjp151216 if (!VALID_UID(uid, zone) || !VALID_GID(gid, zone)) 7610Sstevel@tonic-gate return (-1); 7620Sstevel@tonic-gate 7630Sstevel@tonic-gate cr->cr_uid = cr->cr_ruid = cr->cr_suid = uid; 7640Sstevel@tonic-gate cr->cr_gid = cr->cr_rgid = cr->cr_sgid = gid; 7650Sstevel@tonic-gate 7660Sstevel@tonic-gate return (0); 7670Sstevel@tonic-gate } 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate int 7700Sstevel@tonic-gate crsetgroups(cred_t *cr, int n, gid_t *grp) 7710Sstevel@tonic-gate { 7720Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate if (n > ngroups_max || n < 0) 7750Sstevel@tonic-gate return (-1); 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate cr->cr_ngroups = n; 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate if (n > 0) 7800Sstevel@tonic-gate bcopy(grp, cr->cr_groups, n * sizeof (gid_t)); 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate return (0); 7830Sstevel@tonic-gate } 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate void 7860Sstevel@tonic-gate crsetprojid(cred_t *cr, projid_t projid) 7870Sstevel@tonic-gate { 7880Sstevel@tonic-gate ASSERT(projid >= 0 && projid <= MAXPROJID); 7890Sstevel@tonic-gate cr->cr_projid = projid; 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate /* 7930Sstevel@tonic-gate * This routine returns the pointer to the first element of the cr_groups 7940Sstevel@tonic-gate * array. It can move around in an implementation defined way. 7950Sstevel@tonic-gate */ 7960Sstevel@tonic-gate const gid_t * 7970Sstevel@tonic-gate crgetgroups(const cred_t *cr) 7980Sstevel@tonic-gate { 7990Sstevel@tonic-gate return (cr->cr_groups); 8000Sstevel@tonic-gate } 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate int 8030Sstevel@tonic-gate crgetngroups(const cred_t *cr) 8040Sstevel@tonic-gate { 8050Sstevel@tonic-gate return (cr->cr_ngroups); 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate void 8090Sstevel@tonic-gate cred2prcred(const cred_t *cr, prcred_t *pcrp) 8100Sstevel@tonic-gate { 8110Sstevel@tonic-gate pcrp->pr_euid = cr->cr_uid; 8120Sstevel@tonic-gate pcrp->pr_ruid = cr->cr_ruid; 8130Sstevel@tonic-gate pcrp->pr_suid = cr->cr_suid; 8140Sstevel@tonic-gate pcrp->pr_egid = cr->cr_gid; 8150Sstevel@tonic-gate pcrp->pr_rgid = cr->cr_rgid; 8160Sstevel@tonic-gate pcrp->pr_sgid = cr->cr_sgid; 8170Sstevel@tonic-gate pcrp->pr_ngroups = MIN(cr->cr_ngroups, (uint_t)ngroups_max); 8180Sstevel@tonic-gate pcrp->pr_groups[0] = 0; /* in case ngroups == 0 */ 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate if (pcrp->pr_ngroups != 0) 8210Sstevel@tonic-gate bcopy(cr->cr_groups, pcrp->pr_groups, 8220Sstevel@tonic-gate sizeof (gid_t) * cr->cr_ngroups); 8230Sstevel@tonic-gate } 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate static int 8261676Sjpk cred2ucaud(const cred_t *cr, auditinfo64_addr_t *ainfo, const cred_t *rcr) 8270Sstevel@tonic-gate { 8280Sstevel@tonic-gate auditinfo_addr_t *ai; 8290Sstevel@tonic-gate au_tid_addr_t tid; 8300Sstevel@tonic-gate 8311676Sjpk if (secpolicy_audit_getattr(rcr) != 0) 8320Sstevel@tonic-gate return (-1); 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate ai = CR_AUINFO(cr); /* caller makes sure this is non-NULL */ 8350Sstevel@tonic-gate tid = ai->ai_termid; 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate ainfo->ai_auid = ai->ai_auid; 8380Sstevel@tonic-gate ainfo->ai_mask = ai->ai_mask; 8390Sstevel@tonic-gate ainfo->ai_asid = ai->ai_asid; 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate ainfo->ai_termid.at_type = tid.at_type; 8420Sstevel@tonic-gate bcopy(&tid.at_addr, &ainfo->ai_termid.at_addr, 4 * sizeof (uint_t)); 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate ainfo->ai_termid.at_port.at_major = (uint32_t)getmajor(tid.at_port); 8450Sstevel@tonic-gate ainfo->ai_termid.at_port.at_minor = (uint32_t)getminor(tid.at_port); 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate return (0); 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8501676Sjpk void 8511676Sjpk cred2uclabel(const cred_t *cr, bslabel_t *labelp) 8521676Sjpk { 8531676Sjpk ts_label_t *tslp; 8541676Sjpk 8551676Sjpk if ((tslp = crgetlabel(cr)) != NULL) 8561676Sjpk bcopy(&tslp->tsl_label, labelp, sizeof (bslabel_t)); 8571676Sjpk } 8581676Sjpk 8590Sstevel@tonic-gate /* 8600Sstevel@tonic-gate * Convert a credential into a "ucred". Allow the caller to specify 8610Sstevel@tonic-gate * and aligned buffer, e.g., in an mblk, so we don't have to allocate 8620Sstevel@tonic-gate * memory and copy it twice. 8631676Sjpk * 8641676Sjpk * This function may call cred2ucaud(), which calls CRED(). Since this 8651676Sjpk * can be called from an interrupt thread, receiver's cred (rcr) is needed 8661676Sjpk * to determine whether audit info should be included. 8670Sstevel@tonic-gate */ 8680Sstevel@tonic-gate struct ucred_s * 8691676Sjpk cred2ucred(const cred_t *cr, pid_t pid, void *buf, const cred_t *rcr) 8700Sstevel@tonic-gate { 8710Sstevel@tonic-gate struct ucred_s *uc; 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate /* The structure isn't always completely filled in, so zero it */ 8740Sstevel@tonic-gate if (buf == NULL) { 8750Sstevel@tonic-gate uc = kmem_zalloc(ucredsize, KM_SLEEP); 8760Sstevel@tonic-gate } else { 8770Sstevel@tonic-gate bzero(buf, ucredsize); 8780Sstevel@tonic-gate uc = buf; 8790Sstevel@tonic-gate } 8800Sstevel@tonic-gate uc->uc_size = ucredsize; 8810Sstevel@tonic-gate uc->uc_credoff = UCRED_CRED_OFF; 8820Sstevel@tonic-gate uc->uc_privoff = UCRED_PRIV_OFF; 8830Sstevel@tonic-gate uc->uc_audoff = UCRED_AUD_OFF; 8841676Sjpk uc->uc_labeloff = UCRED_LABEL_OFF; 8850Sstevel@tonic-gate uc->uc_pid = pid; 8860Sstevel@tonic-gate uc->uc_projid = cr->cr_projid; 8870Sstevel@tonic-gate uc->uc_zoneid = crgetzoneid(cr); 8880Sstevel@tonic-gate 8891676Sjpk /* 8901676Sjpk * Note that cred2uclabel() call should not be factored out 8911676Sjpk * to the bottom of the if-else. UCXXX() macros depend on 8921676Sjpk * uc_xxxoff values to work correctly. 8931676Sjpk */ 8941676Sjpk if (REMOTE_PEER_CRED(cr)) { 8951676Sjpk /* 8961676Sjpk * other than label, the rest of cred info about a 8971676Sjpk * remote peer isn't available. 8981676Sjpk */ 8991676Sjpk cred2uclabel(cr, UCLABEL(uc)); 9001676Sjpk uc->uc_credoff = 0; 9011676Sjpk uc->uc_privoff = 0; 9020Sstevel@tonic-gate uc->uc_audoff = 0; 9031676Sjpk } else { 9041676Sjpk cred2prcred(cr, UCCRED(uc)); 9051676Sjpk cred2prpriv(cr, UCPRIV(uc)); 9061676Sjpk if (audoff == 0 || cred2ucaud(cr, UCAUD(uc), rcr) != 0) 9071676Sjpk uc->uc_audoff = 0; 9081676Sjpk cred2uclabel(cr, UCLABEL(uc)); 9091676Sjpk } 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate return (uc); 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate /* 9150Sstevel@tonic-gate * Get the "ucred" of a process. 9160Sstevel@tonic-gate */ 9170Sstevel@tonic-gate struct ucred_s * 9180Sstevel@tonic-gate pgetucred(proc_t *p) 9190Sstevel@tonic-gate { 9200Sstevel@tonic-gate cred_t *cr; 9210Sstevel@tonic-gate struct ucred_s *uc; 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate mutex_enter(&p->p_crlock); 9240Sstevel@tonic-gate cr = p->p_cred; 9250Sstevel@tonic-gate crhold(cr); 9260Sstevel@tonic-gate mutex_exit(&p->p_crlock); 9270Sstevel@tonic-gate 9281676Sjpk uc = cred2ucred(cr, p->p_pid, NULL, CRED()); 9290Sstevel@tonic-gate crfree(cr); 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate return (uc); 9320Sstevel@tonic-gate } 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate /* 9350Sstevel@tonic-gate * If the reply status is NFSERR_EACCES, it may be because we are 9360Sstevel@tonic-gate * root (no root net access). Check the real uid, if it isn't root 9370Sstevel@tonic-gate * make that the uid instead and retry the call. 9380Sstevel@tonic-gate * Private interface for NFS. 9390Sstevel@tonic-gate */ 9400Sstevel@tonic-gate cred_t * 9410Sstevel@tonic-gate crnetadjust(cred_t *cr) 9420Sstevel@tonic-gate { 9430Sstevel@tonic-gate if (cr->cr_uid == 0 && cr->cr_ruid != 0) { 9440Sstevel@tonic-gate cr = crdup(cr); 9450Sstevel@tonic-gate cr->cr_uid = cr->cr_ruid; 9460Sstevel@tonic-gate return (cr); 9470Sstevel@tonic-gate } 9480Sstevel@tonic-gate return (NULL); 9490Sstevel@tonic-gate } 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate /* 9520Sstevel@tonic-gate * The reference count is of interest when you want to check 9530Sstevel@tonic-gate * whether it is ok to modify the credential in place. 9540Sstevel@tonic-gate */ 9550Sstevel@tonic-gate uint_t 9560Sstevel@tonic-gate crgetref(const cred_t *cr) 9570Sstevel@tonic-gate { 9580Sstevel@tonic-gate return (cr->cr_ref); 9590Sstevel@tonic-gate } 9600Sstevel@tonic-gate 9610Sstevel@tonic-gate static int 9620Sstevel@tonic-gate get_c2audit_load(void) 9630Sstevel@tonic-gate { 9640Sstevel@tonic-gate static int gotit = 0; 9650Sstevel@tonic-gate static int c2audit_load; 9660Sstevel@tonic-gate u_longlong_t audit_load_val; 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate if (gotit) 9690Sstevel@tonic-gate return (c2audit_load); 9700Sstevel@tonic-gate audit_load_val = 0; /* set default value once */ 9710Sstevel@tonic-gate (void) mod_sysvar("c2audit", "audit_load", &audit_load_val); 9720Sstevel@tonic-gate c2audit_load = (int)audit_load_val; 9730Sstevel@tonic-gate gotit++; 9740Sstevel@tonic-gate return (c2audit_load); 9750Sstevel@tonic-gate } 9760Sstevel@tonic-gate 9770Sstevel@tonic-gate int 9780Sstevel@tonic-gate get_audit_ucrsize(void) 9790Sstevel@tonic-gate { 9800Sstevel@tonic-gate return (get_c2audit_load() ? sizeof (auditinfo64_addr_t) : 0); 9810Sstevel@tonic-gate } 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate /* 9840Sstevel@tonic-gate * Set zone pointer in credential to indicated value. First adds a 9850Sstevel@tonic-gate * hold for the new zone, then drops the hold on previous zone (if any). 9860Sstevel@tonic-gate * This is done in this order in case the old and new zones are the 9870Sstevel@tonic-gate * same. 9880Sstevel@tonic-gate */ 9890Sstevel@tonic-gate void 9900Sstevel@tonic-gate crsetzone(cred_t *cr, zone_t *zptr) 9910Sstevel@tonic-gate { 9920Sstevel@tonic-gate zone_t *oldzptr = cr->cr_zone; 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate ASSERT(cr != kcred); 9950Sstevel@tonic-gate ASSERT(cr->cr_ref <= 2); 9960Sstevel@tonic-gate cr->cr_zone = zptr; 9970Sstevel@tonic-gate zone_cred_hold(zptr); 9980Sstevel@tonic-gate if (oldzptr) 9990Sstevel@tonic-gate zone_cred_rele(oldzptr); 10000Sstevel@tonic-gate } 10011676Sjpk 10021676Sjpk /* 10031676Sjpk * Create a new cred based on the supplied label 10041676Sjpk */ 10051676Sjpk cred_t * 10061676Sjpk newcred_from_bslabel(bslabel_t *blabel, uint32_t doi, int flags) 10071676Sjpk { 10081676Sjpk ts_label_t *lbl = labelalloc(blabel, doi, flags); 10091676Sjpk cred_t *cr = NULL; 10101676Sjpk 10111676Sjpk if (lbl != NULL) { 1012*6134Scasper if ((cr = crdup_flags(dummycr, flags)) != NULL) { 10131676Sjpk cr->cr_label = lbl; 10141676Sjpk } else { 10151676Sjpk label_rele(lbl); 10161676Sjpk } 10171676Sjpk } 10181676Sjpk 10191676Sjpk return (cr); 10201676Sjpk } 10211676Sjpk 10221676Sjpk /* 10231676Sjpk * Derive a new cred from the existing cred, but with a different label. 10241676Sjpk * To be used when a cred is being shared, but the label needs to be changed 10251676Sjpk * by a caller without affecting other users 10261676Sjpk */ 10271676Sjpk cred_t * 10281676Sjpk copycred_from_bslabel(cred_t *cr, bslabel_t *blabel, uint32_t doi, int flags) 10291676Sjpk { 10301676Sjpk ts_label_t *lbl = labelalloc(blabel, doi, flags); 10311676Sjpk cred_t *newcr = NULL; 10321676Sjpk 10331676Sjpk if (lbl != NULL) { 1034*6134Scasper if ((newcr = crdup_flags(cr, flags)) != NULL) { 1035*6134Scasper if (newcr->cr_label != NULL) 1036*6134Scasper label_rele(newcr->cr_label); 10371676Sjpk newcr->cr_label = lbl; 10381676Sjpk } else { 10391676Sjpk label_rele(lbl); 10401676Sjpk } 10411676Sjpk } 10421676Sjpk 10431676Sjpk return (newcr); 10441676Sjpk } 10451676Sjpk 10461676Sjpk /* 10471676Sjpk * This function returns a pointer to the kcred-equivalent in the current zone. 10481676Sjpk */ 10491676Sjpk cred_t * 10501676Sjpk zone_kcred(void) 10511676Sjpk { 10521676Sjpk zone_t *zone; 10531676Sjpk 10541676Sjpk if ((zone = CRED()->cr_zone) != NULL) 10551676Sjpk return (zone->zone_kcred); 10561676Sjpk else 10571676Sjpk return (kcred); 10581676Sjpk } 10594321Scasper 10604321Scasper boolean_t 10615771Sjp151216 valid_ephemeral_uid(zone_t *zone, uid_t id) 10624321Scasper { 10635771Sjp151216 ephemeral_zsd_t *eph_zsd; 10645908Sjp151216 if (id <= IDMAP_WK__MAX_UID) 10655771Sjp151216 return (B_TRUE); 10665771Sjp151216 10675771Sjp151216 eph_zsd = get_ephemeral_zsd(zone); 10685771Sjp151216 ASSERT(eph_zsd != NULL); 10694321Scasper membar_consumer(); 10705771Sjp151216 return (id > eph_zsd->min_uid && id <= eph_zsd->last_uid); 10714321Scasper } 10724321Scasper 10734321Scasper boolean_t 10745771Sjp151216 valid_ephemeral_gid(zone_t *zone, gid_t id) 10754321Scasper { 10765771Sjp151216 ephemeral_zsd_t *eph_zsd; 10775908Sjp151216 if (id <= IDMAP_WK__MAX_GID) 10785771Sjp151216 return (B_TRUE); 10795771Sjp151216 10805771Sjp151216 eph_zsd = get_ephemeral_zsd(zone); 10815771Sjp151216 ASSERT(eph_zsd != NULL); 10824321Scasper membar_consumer(); 10835771Sjp151216 return (id > eph_zsd->min_gid && id <= eph_zsd->last_gid); 10844321Scasper } 10854321Scasper 10864321Scasper int 10875771Sjp151216 eph_uid_alloc(zone_t *zone, int flags, uid_t *start, int count) 10884321Scasper { 10895771Sjp151216 ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone); 10905771Sjp151216 10915771Sjp151216 ASSERT(eph_zsd != NULL); 10925771Sjp151216 10935771Sjp151216 mutex_enter(&eph_zsd->eph_lock); 10944321Scasper 10954321Scasper /* Test for unsigned integer wrap around */ 10965771Sjp151216 if (eph_zsd->last_uid + count < eph_zsd->last_uid) { 10975771Sjp151216 mutex_exit(&eph_zsd->eph_lock); 10984321Scasper return (-1); 10994321Scasper } 11004321Scasper 11014321Scasper /* first call or idmap crashed and state corrupted */ 11024321Scasper if (flags != 0) 11035771Sjp151216 eph_zsd->min_uid = eph_zsd->last_uid; 11044321Scasper 11054321Scasper hasephids = B_TRUE; 11065771Sjp151216 *start = eph_zsd->last_uid + 1; 11075771Sjp151216 atomic_add_32(&eph_zsd->last_uid, count); 11085771Sjp151216 mutex_exit(&eph_zsd->eph_lock); 11094321Scasper return (0); 11104321Scasper } 11114321Scasper 11124321Scasper int 11135771Sjp151216 eph_gid_alloc(zone_t *zone, int flags, gid_t *start, int count) 11144321Scasper { 11155771Sjp151216 ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone); 11165771Sjp151216 11175771Sjp151216 ASSERT(eph_zsd != NULL); 11185771Sjp151216 11195771Sjp151216 mutex_enter(&eph_zsd->eph_lock); 11204321Scasper 11214321Scasper /* Test for unsigned integer wrap around */ 11225771Sjp151216 if (eph_zsd->last_gid + count < eph_zsd->last_gid) { 11235771Sjp151216 mutex_exit(&eph_zsd->eph_lock); 11244321Scasper return (-1); 11254321Scasper } 11264321Scasper 11274321Scasper /* first call or idmap crashed and state corrupted */ 11284321Scasper if (flags != 0) 11295771Sjp151216 eph_zsd->min_gid = eph_zsd->last_gid; 11304321Scasper 11314321Scasper hasephids = B_TRUE; 11325771Sjp151216 *start = eph_zsd->last_gid + 1; 11335771Sjp151216 atomic_add_32(&eph_zsd->last_gid, count); 11345771Sjp151216 mutex_exit(&eph_zsd->eph_lock); 11354321Scasper return (0); 11364321Scasper } 11374321Scasper 11384321Scasper /* 11395771Sjp151216 * IMPORTANT.The two functions get_ephemeral_data() and set_ephemeral_data() 11405771Sjp151216 * are project private functions that are for use of the test system only and 11415771Sjp151216 * are not to be used for other purposes. 11425771Sjp151216 */ 11435771Sjp151216 11445771Sjp151216 void 11455771Sjp151216 get_ephemeral_data(zone_t *zone, uid_t *min_uid, uid_t *last_uid, 11465771Sjp151216 gid_t *min_gid, gid_t *last_gid) 11475771Sjp151216 { 11485771Sjp151216 ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone); 11495771Sjp151216 11505771Sjp151216 ASSERT(eph_zsd != NULL); 11515771Sjp151216 11525771Sjp151216 mutex_enter(&eph_zsd->eph_lock); 11535771Sjp151216 11545771Sjp151216 *min_uid = eph_zsd->min_uid; 11555771Sjp151216 *last_uid = eph_zsd->last_uid; 11565771Sjp151216 *min_gid = eph_zsd->min_gid; 11575771Sjp151216 *last_gid = eph_zsd->last_gid; 11585771Sjp151216 11595771Sjp151216 mutex_exit(&eph_zsd->eph_lock); 11605771Sjp151216 } 11615771Sjp151216 11625771Sjp151216 11635771Sjp151216 void 11645771Sjp151216 set_ephemeral_data(zone_t *zone, uid_t min_uid, uid_t last_uid, 11655771Sjp151216 gid_t min_gid, gid_t last_gid) 11665771Sjp151216 { 11675771Sjp151216 ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone); 11685771Sjp151216 11695771Sjp151216 ASSERT(eph_zsd != NULL); 11705771Sjp151216 11715771Sjp151216 mutex_enter(&eph_zsd->eph_lock); 11725771Sjp151216 11735771Sjp151216 if (min_uid != 0) 11745771Sjp151216 eph_zsd->min_uid = min_uid; 11755771Sjp151216 if (last_uid != 0) 11765771Sjp151216 eph_zsd->last_uid = last_uid; 11775771Sjp151216 if (min_gid != 0) 11785771Sjp151216 eph_zsd->min_gid = min_gid; 11795771Sjp151216 if (last_gid != 0) 11805771Sjp151216 eph_zsd->last_gid = last_gid; 11815771Sjp151216 11825771Sjp151216 mutex_exit(&eph_zsd->eph_lock); 11835771Sjp151216 } 11845771Sjp151216 11855771Sjp151216 /* 11865331Samw * If the credential user SID or group SID is mapped to an ephemeral 11875331Samw * ID, map the credential to nobody. 11884321Scasper */ 11894321Scasper cred_t * 11904321Scasper crgetmapped(const cred_t *cr) 11914321Scasper { 11925771Sjp151216 ephemeral_zsd_t *eph_zsd; 11934406Scasper /* 11944406Scasper * Someone incorrectly passed a NULL cred to a vnode operation 11954406Scasper * either on purpose or by calling CRED() in interrupt context. 11964406Scasper */ 11974406Scasper if (cr == NULL) 11984406Scasper return (NULL); 11994406Scasper 12004321Scasper if (cr->cr_ksid != NULL) { 12015771Sjp151216 if (cr->cr_ksid->kr_sidx[KSID_USER].ks_id > MAXUID) { 12025771Sjp151216 eph_zsd = get_ephemeral_zsd(crgetzone(cr)); 12035771Sjp151216 return (eph_zsd->eph_nobody); 12045771Sjp151216 } 12054321Scasper 12065771Sjp151216 if (cr->cr_ksid->kr_sidx[KSID_GROUP].ks_id > MAXUID) { 12075771Sjp151216 eph_zsd = get_ephemeral_zsd(crgetzone(cr)); 12085771Sjp151216 return (eph_zsd->eph_nobody); 12095771Sjp151216 } 12104321Scasper } 12114321Scasper 12124321Scasper return ((cred_t *)cr); 12134321Scasper } 12144321Scasper 12154321Scasper /* index should be in range for a ksidindex_t */ 12164321Scasper void 12174321Scasper crsetsid(cred_t *cr, ksid_t *ksp, int index) 12184321Scasper { 12194321Scasper ASSERT(cr->cr_ref <= 2); 12204321Scasper ASSERT(index >= 0 && index < KSID_COUNT); 12214321Scasper if (cr->cr_ksid == NULL && ksp == NULL) 12224321Scasper return; 12234321Scasper cr->cr_ksid = kcrsid_setsid(cr->cr_ksid, ksp, index); 12244321Scasper } 12254321Scasper 12264321Scasper void 12274321Scasper crsetsidlist(cred_t *cr, ksidlist_t *ksl) 12284321Scasper { 12294321Scasper ASSERT(cr->cr_ref <= 2); 12304321Scasper if (cr->cr_ksid == NULL && ksl == NULL) 12314321Scasper return; 12324321Scasper cr->cr_ksid = kcrsid_setsidlist(cr->cr_ksid, ksl); 12334321Scasper } 12344321Scasper 12354321Scasper ksid_t * 12364321Scasper crgetsid(const cred_t *cr, int i) 12374321Scasper { 12384321Scasper ASSERT(i >= 0 && i < KSID_COUNT); 12394321Scasper if (cr->cr_ksid != NULL && cr->cr_ksid->kr_sidx[i].ks_domain) 12404321Scasper return ((ksid_t *)&cr->cr_ksid->kr_sidx[i]); 12414321Scasper return (NULL); 12424321Scasper } 12434321Scasper 12444321Scasper ksidlist_t * 12454321Scasper crgetsidlist(const cred_t *cr) 12464321Scasper { 12475331Samw if (cr->cr_ksid != NULL) 12484923Scasper return (cr->cr_ksid->kr_sidlist); 12494321Scasper return (NULL); 12504321Scasper } 12515331Samw 12525331Samw /* 12535331Samw * Interface to set the effective and permitted privileges for 12545331Samw * a credential; this interface does no security checks and is 12555331Samw * intended for kernel (file)servers creating credentials with 12565331Samw * specific privileges. 12575331Samw */ 12585331Samw int 12595331Samw crsetpriv(cred_t *cr, ...) 12605331Samw { 12615331Samw va_list ap; 12625331Samw const char *privnm; 12635331Samw 12645331Samw ASSERT(cr->cr_ref <= 2); 12655331Samw 12665331Samw priv_set_PA(cr); 12675331Samw 12685331Samw va_start(ap, cr); 12695331Samw 12705331Samw while ((privnm = va_arg(ap, const char *)) != NULL) { 12715331Samw int priv = priv_getbyname(privnm, 0); 12725331Samw if (priv < 0) 12735331Samw return (-1); 12745331Samw 12755331Samw priv_addset(&CR_PPRIV(cr), priv); 12765331Samw priv_addset(&CR_EPRIV(cr), priv); 12775331Samw } 12785331Samw priv_adjust_PA(cr); 12795331Samw va_end(ap); 12805331Samw return (0); 12815331Samw } 1282*6134Scasper 1283*6134Scasper struct credklpd * 1284*6134Scasper crgetcrklpd(const cred_t *cr) 1285*6134Scasper { 1286*6134Scasper return (cr->cr_klpd); 1287*6134Scasper } 1288*6134Scasper 1289*6134Scasper void 1290*6134Scasper crsetcrklpd(cred_t *cr, struct credklpd *crklpd) 1291*6134Scasper { 1292*6134Scasper ASSERT(cr->cr_ref <= 2); 1293*6134Scasper 1294*6134Scasper if (cr->cr_klpd != NULL) 1295*6134Scasper crklpd_rele(cr->cr_klpd); 1296*6134Scasper cr->cr_klpd = crklpd; 1297*6134Scasper } 1298