4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2013, Ira Cooper. All rights reserved.
25 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 * University Copyright- Copyright (c) 1982, 1986, 1988
33 * The Regents of the University of California
36 * University Acknowledgment- Portions of this document are derived from
37 * software developed by the University of California, Berkeley, and its
41 #include <sys/types.h>
42 #include <sys/sysmacros.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/cred_impl.h>
46 #include <sys/policy.h>
47 #include <sys/vnode.h>
48 #include <sys/errno.h>
52 #include <sys/syscall.h>
53 #include <sys/debug.h>
54 #include <sys/atomic.h>
55 #include <sys/ucred.h>
56 #include <sys/prsystm.h>
57 #include <sys/modctl.h>
63 #include <sys/idmap.h>
65 #include <sys/varargs.h>
66 #include <sys/sysconf.h>
67 #include <util/qsort.h>
70 /* Ephemeral IDs Zones specific data */
71 typedef struct ephemeral_zsd
{
80 static void crgrphold(credgrp_t
*);
82 #define CREDGRPSZ(ngrp) (sizeof (credgrp_t) + ((ngrp - 1) * sizeof (gid_t)))
84 static kmutex_t ephemeral_zone_mutex
;
85 static zone_key_t ephemeral_zone_key
;
87 static struct kmem_cache
*cred_cache
;
88 static size_t crsize
= 0;
89 static int audoff
= 0;
92 static cred_t
*dummycr
;
94 int rstlink
; /* link(2) restricted to files owned by user? */
96 static int get_c2audit_load(void);
98 #define CR_AUINFO(c) (auditinfo_addr_t *)((audoff == 0) ? NULL : \
99 ((char *)(c)) + audoff)
101 #define REMOTE_PEER_CRED(c) ((c)->cr_gid == -1)
103 #define BIN_GROUP_SEARCH_CUTOFF 16
105 static boolean_t hasephids
= B_FALSE
;
107 static ephemeral_zsd_t
*
108 get_ephemeral_zsd(zone_t
*zone
)
110 ephemeral_zsd_t
*eph_zsd
;
112 eph_zsd
= zone_getspecific(ephemeral_zone_key
, zone
);
113 if (eph_zsd
!= NULL
) {
117 mutex_enter(&ephemeral_zone_mutex
);
118 eph_zsd
= zone_getspecific(ephemeral_zone_key
, zone
);
119 if (eph_zsd
== NULL
) {
120 eph_zsd
= kmem_zalloc(sizeof (ephemeral_zsd_t
), KM_SLEEP
);
121 eph_zsd
->min_uid
= MAXUID
;
122 eph_zsd
->last_uid
= IDMAP_WK__MAX_UID
;
123 eph_zsd
->min_gid
= MAXUID
;
124 eph_zsd
->last_gid
= IDMAP_WK__MAX_GID
;
125 mutex_init(&eph_zsd
->eph_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
128 * nobody is used to map SID containing CRs.
130 eph_zsd
->eph_nobody
= crdup(zone
->zone_kcred
);
131 (void) crsetugid(eph_zsd
->eph_nobody
, UID_NOBODY
, GID_NOBODY
);
132 CR_FLAGS(eph_zsd
->eph_nobody
) = 0;
133 eph_zsd
->eph_nobody
->cr_zone
= zone
;
135 (void) zone_setspecific(ephemeral_zone_key
, zone
, eph_zsd
);
137 mutex_exit(&ephemeral_zone_mutex
);
141 static cred_t
*crdup_flags(const cred_t
*, int);
142 static cred_t
*cralloc_flags(int);
145 * This function is called when a zone is destroyed
149 destroy_ephemeral_zsd(zoneid_t zone_id
, void *arg
)
151 ephemeral_zsd_t
*eph_zsd
= arg
;
152 if (eph_zsd
!= NULL
) {
153 mutex_destroy(&eph_zsd
->eph_lock
);
154 crfree(eph_zsd
->eph_nobody
);
155 kmem_free(eph_zsd
, sizeof (ephemeral_zsd_t
));
162 * Initialize credentials data structures.
170 crsize
= sizeof (cred_t
);
172 if (get_c2audit_load() > 0) {
174 /* assure audit context is 64-bit aligned */
176 sizeof (int64_t) - 1) & ~(sizeof (int64_t) - 1);
180 crsize
= audoff
+ sizeof (auditinfo_addr_t
);
181 crsize
= (crsize
+ sizeof (int) - 1) & ~(sizeof (int) - 1);
184 cred_cache
= kmem_cache_create("cred_cache", crsize
, 0,
185 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
188 * dummycr is used to copy initial state for creds.
191 bzero(dummycr
, crsize
);
193 dummycr
->cr_uid
= (uid_t
)-1;
194 dummycr
->cr_gid
= (gid_t
)-1;
195 dummycr
->cr_ruid
= (uid_t
)-1;
196 dummycr
->cr_rgid
= (gid_t
)-1;
197 dummycr
->cr_suid
= (uid_t
)-1;
198 dummycr
->cr_sgid
= (gid_t
)-1;
202 * kcred is used by anything that needs all privileges; it's
203 * also the template used for crget as it has all the compatible
208 bzero(kcred
, crsize
);
211 /* kcred is never freed, so we don't need zone_cred_hold here */
212 kcred
->cr_zone
= &zone0
;
214 priv_fillset(&CR_LPRIV(kcred
));
215 CR_IPRIV(kcred
) = *priv_basic
;
217 priv_addset(&CR_IPRIV(kcred
), PRIV_PROC_SECFLAGS
);
219 /* Not a basic privilege, if chown is not restricted add it to I0 */
221 priv_addset(&CR_IPRIV(kcred
), PRIV_FILE_CHOWN_SELF
);
223 /* Basic privilege, if link is restricted remove it from I0 */
225 priv_delset(&CR_IPRIV(kcred
), PRIV_FILE_LINK_ANY
);
227 CR_EPRIV(kcred
) = CR_PPRIV(kcred
) = CR_IPRIV(kcred
);
230 * Set up credentials of p0.
232 ttoproc(curthread
)->p_cred
= kcred
;
233 curthread
->t_cred
= kcred
;
235 ucredsize
= UCRED_SIZE
;
237 mutex_init(&ephemeral_zone_mutex
, NULL
, MUTEX_DEFAULT
, NULL
);
238 zone_key_create(&ephemeral_zone_key
, NULL
, NULL
, destroy_ephemeral_zsd
);
242 * Allocate (nearly) uninitialized cred_t.
245 cralloc_flags(int flgs
)
247 cred_t
*cr
= kmem_cache_alloc(cred_cache
, flgs
);
252 cr
->cr_ref
= 1; /* So we can crfree() */
263 return (cralloc_flags(KM_SLEEP
));
267 * As cralloc but prepared for ksid change (if appropriate).
272 cred_t
*cr
= cralloc();
274 cr
->cr_ksid
= kcrsid_alloc();
279 * Allocate a initialized cred structure and crhold() it.
280 * Initialized means: all ids 0, group count 0, L=Full, E=P=I=I0
285 cred_t
*cr
= kmem_cache_alloc(cred_cache
, KM_SLEEP
);
287 bcopy(kcred
, cr
, crsize
);
289 zone_cred_hold(cr
->cr_zone
);
290 ASSERT(cr
->cr_klpd
== NULL
);
291 ASSERT(cr
->cr_grps
== NULL
);
296 * Broadcast the cred to all the threads in the process.
297 * The current thread's credentials can be set right away, but other
298 * threads must wait until the start of the next system call or trap.
299 * This avoids changing the cred in the middle of a system call.
301 * The cred has already been held for the process and the thread (2 holds),
304 * p->p_crlock shouldn't be held here, since p_lock must be acquired.
307 crset(proc_t
*p
, cred_t
*cr
)
313 ASSERT(p
== curproc
); /* assumes p_lwpcnt can't change */
316 * DTrace accesses t_cred in probe context. t_cred must always be
317 * either NULL, or point to a valid, allocated cred structure.
321 t
->t_cred
= cr
; /* the cred is held by caller for this thread */
322 crfree(oldcr
); /* free the old cred for the thread */
325 * Broadcast to other threads, if any.
327 if (p
->p_lwpcnt
> 1) {
328 mutex_enter(&p
->p_lock
); /* to keep thread list safe */
330 for (t
= first
->t_forw
; t
!= first
; t
= t
->t_forw
)
331 t
->t_pre_sys
= 1; /* so syscall will get new cred */
332 mutex_exit(&p
->p_lock
);
337 * Put a hold on a cred structure.
342 ASSERT(cr
->cr_ref
!= 0xdeadbeef && cr
->cr_ref
!= 0);
343 atomic_inc_32(&cr
->cr_ref
);
347 * Release previous hold on a cred structure. Free it if refcnt == 0.
352 ASSERT(cr
->cr_ref
!= 0xdeadbeef && cr
->cr_ref
!= 0);
353 if (atomic_dec_32_nv(&cr
->cr_ref
) == 0) {
356 crklpd_rele(cr
->cr_klpd
);
358 zone_cred_rele(cr
->cr_zone
);
360 kcrsid_rele(cr
->cr_ksid
);
362 crgrprele(cr
->cr_grps
);
364 kmem_cache_free(cred_cache
, cr
);
369 * Copy a cred structure to a new one and free the old one.
370 * The new cred will have two references. One for the calling process,
371 * and one for the thread.
379 bcopy(cr
, newcr
, crsize
);
381 zone_cred_hold(newcr
->cr_zone
);
383 kcrsid_hold(newcr
->cr_ksid
);
385 crklpd_hold(newcr
->cr_klpd
);
387 crgrphold(newcr
->cr_grps
);
389 newcr
->cr_ref
= 2; /* caller gets two references */
394 * Copy a cred structure to a new one and free the old one.
395 * The new cred will have two references. One for the calling process,
396 * and one for the thread.
397 * This variation on crcopy uses a pre-allocated structure for the
401 crcopy_to(cred_t
*oldcr
, cred_t
*newcr
)
403 credsid_t
*nkcr
= newcr
->cr_ksid
;
405 bcopy(oldcr
, newcr
, crsize
);
407 zone_cred_hold(newcr
->cr_zone
);
409 crklpd_hold(newcr
->cr_klpd
);
411 crgrphold(newcr
->cr_grps
);
413 newcr
->cr_ksid
= nkcr
;
414 kcrsidcopy_to(oldcr
->cr_ksid
, newcr
->cr_ksid
);
415 } else if (newcr
->cr_ksid
)
416 kcrsid_hold(newcr
->cr_ksid
);
418 newcr
->cr_ref
= 2; /* caller gets two references */
422 * Dup a cred struct to a new held one.
423 * The old cred is not freed.
426 crdup_flags(const cred_t
*cr
, int flgs
)
430 newcr
= cralloc_flags(flgs
);
435 bcopy(cr
, newcr
, crsize
);
437 zone_cred_hold(newcr
->cr_zone
);
439 crklpd_hold(newcr
->cr_klpd
);
441 kcrsid_hold(newcr
->cr_ksid
);
443 crgrphold(newcr
->cr_grps
);
451 return (crdup_flags(cr
, KM_SLEEP
));
455 * Dup a cred struct to a new held one.
456 * The old cred is not freed.
457 * This variation on crdup uses a pre-allocated structure for the
461 crdup_to(cred_t
*oldcr
, cred_t
*newcr
)
463 credsid_t
*nkcr
= newcr
->cr_ksid
;
465 bcopy(oldcr
, newcr
, crsize
);
467 zone_cred_hold(newcr
->cr_zone
);
469 crklpd_hold(newcr
->cr_klpd
);
471 crgrphold(newcr
->cr_grps
);
473 newcr
->cr_ksid
= nkcr
;
474 kcrsidcopy_to(oldcr
->cr_ksid
, newcr
->cr_ksid
);
475 } else if (newcr
->cr_ksid
)
476 kcrsid_hold(newcr
->cr_ksid
);
481 * Return the (held) credentials for the current running process.
489 p
= ttoproc(curthread
);
490 mutex_enter(&p
->p_crlock
);
491 crhold(cr
= p
->p_cred
);
492 mutex_exit(&p
->p_crlock
);
497 * Backward compatibility check for suser().
498 * Accounting flag is now set in the policy functions; auditing is
499 * done through use of privilege in the audit trail.
504 return (PRIV_POLICY(cr
, PRIV_SYS_SUSER_COMPAT
, B_FALSE
, EPERM
, NULL
)
509 * Determine whether the supplied group id is a member of the group
510 * described by the supplied credentials.
513 groupmember(gid_t gid
, const cred_t
*cr
)
515 if (gid
== cr
->cr_gid
)
517 return (supgroupmember(gid
, cr
));
521 * As groupmember but only check against the supplemental groups.
524 supgroupmember(gid_t gid
, const cred_t
*cr
)
527 credgrp_t
*grps
= cr
->cr_grps
;
528 const gid_t
*gp
, *endgp
;
533 /* For a small number of groups, use sequentials search. */
534 if (grps
->crg_ngroups
<= BIN_GROUP_SEARCH_CUTOFF
) {
535 endgp
= &grps
->crg_groups
[grps
->crg_ngroups
];
536 for (gp
= grps
->crg_groups
; gp
< endgp
; gp
++)
542 /* We use binary search when we have many groups. */
544 hi
= grps
->crg_ngroups
- 1;
545 gp
= grps
->crg_groups
;
548 int m
= (lo
+ hi
) / 2;
552 else if (gid
< gp
[m
])
562 * This function is called to check whether the credentials set
563 * "scrp" has permission to act on credentials set "tcrp". It enforces the
564 * permission requirements needed to send a signal to a process.
565 * The same requirements are imposed by other system calls, however.
568 * (1) if the credentials are the same, the check succeeds
569 * (2) if the zone ids don't match, and scrp is not in the global zone or
570 * does not have the PRIV_PROC_ZONE privilege, the check fails
571 * (3) if the real or effective user id of scrp matches the real or saved
572 * user id of tcrp or scrp has the PRIV_PROC_OWNER privilege, the check
574 * (4) otherwise, the check fails
577 hasprocperm(const cred_t
*tcrp
, const cred_t
*scrp
)
581 if (scrp
->cr_zone
!= tcrp
->cr_zone
&&
582 (scrp
->cr_zone
!= global_zone
||
583 secpolicy_proc_zone(scrp
) != 0))
585 if (scrp
->cr_uid
== tcrp
->cr_ruid
||
586 scrp
->cr_ruid
== tcrp
->cr_ruid
||
587 scrp
->cr_uid
== tcrp
->cr_suid
||
588 scrp
->cr_ruid
== tcrp
->cr_suid
||
589 !PRIV_POLICY(scrp
, PRIV_PROC_OWNER
, B_FALSE
, EPERM
, "hasprocperm"))
595 * This interface replaces hasprocperm; it works like hasprocperm but
596 * additionally returns success if the proc_t's match
597 * It is the preferred interface for most uses.
598 * And it will acquire p_crlock itself, so it assert's that it shouldn't
602 prochasprocperm(proc_t
*tp
, proc_t
*sp
, const cred_t
*scrp
)
607 ASSERT(MUTEX_NOT_HELD(&tp
->p_crlock
));
612 if (tp
->p_sessp
!= sp
->p_sessp
&& secpolicy_basic_proc(scrp
) != 0)
615 mutex_enter(&tp
->p_crlock
);
616 crhold(tcrp
= tp
->p_cred
);
617 mutex_exit(&tp
->p_crlock
);
618 rets
= hasprocperm(tcrp
, scrp
);
625 * This routine is used to compare two credentials to determine if
626 * they refer to the same "user". If the pointers are equal, then
627 * they must refer to the same user. Otherwise, the contents of
628 * the credentials are compared to see whether they are equivalent.
630 * This routine returns 0 if the credentials refer to the same user,
634 crcmp(const cred_t
*cr1
, const cred_t
*cr2
)
636 credgrp_t
*grp1
, *grp2
;
641 if (cr1
->cr_uid
== cr2
->cr_uid
&&
642 cr1
->cr_gid
== cr2
->cr_gid
&&
643 cr1
->cr_ruid
== cr2
->cr_ruid
&&
644 cr1
->cr_rgid
== cr2
->cr_rgid
&&
645 cr1
->cr_zone
== cr2
->cr_zone
&&
646 ((grp1
= cr1
->cr_grps
) == (grp2
= cr2
->cr_grps
) ||
647 (grp1
!= NULL
&& grp2
!= NULL
&&
648 grp1
->crg_ngroups
== grp2
->crg_ngroups
&&
649 bcmp(grp1
->crg_groups
, grp2
->crg_groups
,
650 grp1
->crg_ngroups
* sizeof (gid_t
)) == 0))) {
651 return (!priv_isequalset(&CR_OEPRIV(cr1
), &CR_OEPRIV(cr2
)));
657 * Read access functions to cred_t.
660 crgetuid(const cred_t
*cr
)
666 crgetruid(const cred_t
*cr
)
668 return (cr
->cr_ruid
);
672 crgetsuid(const cred_t
*cr
)
674 return (cr
->cr_suid
);
678 crgetgid(const cred_t
*cr
)
684 crgetrgid(const cred_t
*cr
)
686 return (cr
->cr_rgid
);
690 crgetsgid(const cred_t
*cr
)
692 return (cr
->cr_sgid
);
695 const auditinfo_addr_t
*
696 crgetauinfo(const cred_t
*cr
)
698 return ((const auditinfo_addr_t
*)CR_AUINFO(cr
));
702 crgetauinfo_modifiable(cred_t
*cr
)
704 return (CR_AUINFO(cr
));
708 crgetzoneid(const cred_t
*cr
)
710 return (cr
->cr_zone
== NULL
?
711 (cr
->cr_uid
== -1 ? (zoneid_t
)-1 : GLOBAL_ZONEID
) :
712 cr
->cr_zone
->zone_id
);
716 crgetprojid(const cred_t
*cr
)
718 return (cr
->cr_projid
);
722 crgetzone(const cred_t
*cr
)
724 return (cr
->cr_zone
);
728 crisremote(const cred_t
*cr
)
730 return (REMOTE_PEER_CRED(cr
));
733 #define BADUID(x, zn) ((x) != -1 && !VALID_UID((x), (zn)))
734 #define BADGID(x, zn) ((x) != -1 && !VALID_GID((x), (zn)))
737 crsetresuid(cred_t
*cr
, uid_t r
, uid_t e
, uid_t s
)
739 zone_t
*zone
= crgetzone(cr
);
741 ASSERT(cr
->cr_ref
<= 2);
743 if (BADUID(r
, zone
) || BADUID(e
, zone
) || BADUID(s
, zone
))
757 crsetresgid(cred_t
*cr
, gid_t r
, gid_t e
, gid_t s
)
759 zone_t
*zone
= crgetzone(cr
);
761 ASSERT(cr
->cr_ref
<= 2);
763 if (BADGID(r
, zone
) || BADGID(e
, zone
) || BADGID(s
, zone
))
777 crsetugid(cred_t
*cr
, uid_t uid
, gid_t gid
)
779 zone_t
*zone
= crgetzone(cr
);
781 ASSERT(cr
->cr_ref
<= 2);
783 if (!VALID_UID(uid
, zone
) || !VALID_GID(gid
, zone
))
786 cr
->cr_uid
= cr
->cr_ruid
= cr
->cr_suid
= uid
;
787 cr
->cr_gid
= cr
->cr_rgid
= cr
->cr_sgid
= gid
;
793 gidcmp(const void *v1
, const void *v2
)
795 gid_t g1
= *(gid_t
*)v1
;
796 gid_t g2
= *(gid_t
*)v2
;
807 crsetgroups(cred_t
*cr
, int n
, gid_t
*grp
)
809 ASSERT(cr
->cr_ref
<= 2);
811 if (n
> ngroups_max
|| n
< 0)
814 if (cr
->cr_grps
!= NULL
)
815 crgrprele(cr
->cr_grps
);
818 cr
->cr_grps
= kmem_alloc(CREDGRPSZ(n
), KM_SLEEP
);
819 bcopy(grp
, cr
->cr_grps
->crg_groups
, n
* sizeof (gid_t
));
820 cr
->cr_grps
->crg_ref
= 1;
821 cr
->cr_grps
->crg_ngroups
= n
;
822 qsort(cr
->cr_grps
->crg_groups
, n
, sizeof (gid_t
), gidcmp
);
831 crsetprojid(cred_t
*cr
, projid_t projid
)
833 ASSERT(projid
>= 0 && projid
<= MAXPROJID
);
834 cr
->cr_projid
= projid
;
838 * This routine returns the pointer to the first element of the crg_groups
839 * array. It can move around in an implementation defined way.
840 * Note that when we have no grouplist, we return one element but the
841 * caller should never reference it.
844 crgetgroups(const cred_t
*cr
)
846 return (cr
->cr_grps
== NULL
? &cr
->cr_gid
: cr
->cr_grps
->crg_groups
);
850 crgetngroups(const cred_t
*cr
)
852 return (cr
->cr_grps
== NULL
? 0 : cr
->cr_grps
->crg_ngroups
);
856 cred2prcred(const cred_t
*cr
, prcred_t
*pcrp
)
858 pcrp
->pr_euid
= cr
->cr_uid
;
859 pcrp
->pr_ruid
= cr
->cr_ruid
;
860 pcrp
->pr_suid
= cr
->cr_suid
;
861 pcrp
->pr_egid
= cr
->cr_gid
;
862 pcrp
->pr_rgid
= cr
->cr_rgid
;
863 pcrp
->pr_sgid
= cr
->cr_sgid
;
864 pcrp
->pr_groups
[0] = 0; /* in case ngroups == 0 */
865 pcrp
->pr_ngroups
= cr
->cr_grps
== NULL
? 0 : cr
->cr_grps
->crg_ngroups
;
867 if (pcrp
->pr_ngroups
!= 0)
868 bcopy(cr
->cr_grps
->crg_groups
, pcrp
->pr_groups
,
869 sizeof (gid_t
) * pcrp
->pr_ngroups
);
873 cred2ucaud(const cred_t
*cr
, auditinfo64_addr_t
*ainfo
, const cred_t
*rcr
)
875 auditinfo_addr_t
*ai
;
878 if (secpolicy_audit_getattr(rcr
, B_TRUE
) != 0)
881 ai
= CR_AUINFO(cr
); /* caller makes sure this is non-NULL */
884 ainfo
->ai_auid
= ai
->ai_auid
;
885 ainfo
->ai_mask
= ai
->ai_mask
;
886 ainfo
->ai_asid
= ai
->ai_asid
;
888 ainfo
->ai_termid
.at_type
= tid
.at_type
;
889 bcopy(&tid
.at_addr
, &ainfo
->ai_termid
.at_addr
, 4 * sizeof (uint_t
));
891 ainfo
->ai_termid
.at_port
.at_major
= (uint32_t)getmajor(tid
.at_port
);
892 ainfo
->ai_termid
.at_port
.at_minor
= (uint32_t)getminor(tid
.at_port
);
898 * Convert a credential into a "ucred". Allow the caller to specify
899 * and aligned buffer, e.g., in an mblk, so we don't have to allocate
900 * memory and copy it twice.
902 * This function may call cred2ucaud(), which calls CRED(). Since this
903 * can be called from an interrupt thread, receiver's cred (rcr) is needed
904 * to determine whether audit info should be included.
907 cred2ucred(const cred_t
*cr
, pid_t pid
, void *buf
, const cred_t
*rcr
)
910 uint32_t realsz
= ucredminsize(cr
);
912 /* The structure isn't always completely filled in, so zero it */
914 uc
= kmem_zalloc(realsz
, KM_SLEEP
);
919 uc
->uc_size
= realsz
;
921 uc
->uc_projid
= cr
->cr_projid
;
922 uc
->uc_zoneid
= crgetzoneid(cr
);
924 if (!REMOTE_PEER_CRED(cr
)) {
925 uc
->uc_credoff
= UCRED_CRED_OFF
;
926 uc
->uc_privoff
= UCRED_PRIV_OFF
;
927 uc
->uc_audoff
= UCRED_AUD_OFF
;
929 cred2prcred(cr
, UCCRED(uc
));
930 cred2prpriv(cr
, UCPRIV(uc
));
932 if (audoff
== 0 || cred2ucaud(cr
, UCAUD(uc
), rcr
) != 0)
940 * Don't allocate the non-needed group entries. Note: this function
941 * must match the code in cred2ucred; they must agree about the
942 * minimal size of the ucred.
945 ucredminsize(const cred_t
*cr
)
952 if (REMOTE_PEER_CRED(cr
)) {
953 return (sizeof (struct ucred_s
));
956 if (cr
->cr_grps
== NULL
)
957 ndiff
= ngroups_max
- 1; /* Needs one for prcred_t */
959 ndiff
= ngroups_max
- cr
->cr_grps
->crg_ngroups
;
961 return (ucredsize
- ndiff
* sizeof (gid_t
));
965 * Get the "ucred" of a process.
973 mutex_enter(&p
->p_crlock
);
976 mutex_exit(&p
->p_crlock
);
978 uc
= cred2ucred(cr
, p
->p_pid
, NULL
, CRED());
985 * If the reply status is NFSERR_EACCES, it may be because we are
986 * root (no root net access). Check the real uid, if it isn't root
987 * make that the uid instead and retry the call.
988 * Private interface for NFS.
991 crnetadjust(cred_t
*cr
)
993 if (cr
->cr_uid
== 0 && cr
->cr_ruid
!= 0) {
995 cr
->cr_uid
= cr
->cr_ruid
;
1002 * The reference count is of interest when you want to check
1003 * whether it is ok to modify the credential in place.
1006 crgetref(const cred_t
*cr
)
1008 return (cr
->cr_ref
);
1012 get_c2audit_load(void)
1014 static int gotit
= 0;
1015 static int c2audit_load
;
1018 return (c2audit_load
);
1019 c2audit_load
= 1; /* set default value once */
1020 if (mod_sysctl(SYS_CHECK_EXCLUDE
, "c2audit") != 0)
1024 return (c2audit_load
);
1028 get_audit_ucrsize(void)
1030 return (get_c2audit_load() ? sizeof (auditinfo64_addr_t
) : 0);
1034 * Set zone pointer in credential to indicated value. First adds a
1035 * hold for the new zone, then drops the hold on previous zone (if any).
1036 * This is done in this order in case the old and new zones are the
1040 crsetzone(cred_t
*cr
, zone_t
*zptr
)
1042 zone_t
*oldzptr
= cr
->cr_zone
;
1044 ASSERT(cr
!= kcred
);
1045 ASSERT(cr
->cr_ref
<= 2);
1047 zone_cred_hold(zptr
);
1049 zone_cred_rele(oldzptr
);
1053 * This function returns a pointer to the kcred-equivalent in the current zone.
1060 if ((zone
= CRED()->cr_zone
) != NULL
)
1061 return (zone
->zone_kcred
);
1067 valid_ephemeral_uid(zone_t
*zone
, uid_t id
)
1069 ephemeral_zsd_t
*eph_zsd
;
1070 if (id
<= IDMAP_WK__MAX_UID
)
1073 eph_zsd
= get_ephemeral_zsd(zone
);
1074 ASSERT(eph_zsd
!= NULL
);
1076 return (id
> eph_zsd
->min_uid
&& id
<= eph_zsd
->last_uid
);
1080 valid_ephemeral_gid(zone_t
*zone
, gid_t id
)
1082 ephemeral_zsd_t
*eph_zsd
;
1083 if (id
<= IDMAP_WK__MAX_GID
)
1086 eph_zsd
= get_ephemeral_zsd(zone
);
1087 ASSERT(eph_zsd
!= NULL
);
1089 return (id
> eph_zsd
->min_gid
&& id
<= eph_zsd
->last_gid
);
1093 eph_uid_alloc(zone_t
*zone
, int flags
, uid_t
*start
, int count
)
1095 ephemeral_zsd_t
*eph_zsd
= get_ephemeral_zsd(zone
);
1097 ASSERT(eph_zsd
!= NULL
);
1099 mutex_enter(&eph_zsd
->eph_lock
);
1101 /* Test for unsigned integer wrap around */
1102 if (eph_zsd
->last_uid
+ count
< eph_zsd
->last_uid
) {
1103 mutex_exit(&eph_zsd
->eph_lock
);
1107 /* first call or idmap crashed and state corrupted */
1109 eph_zsd
->min_uid
= eph_zsd
->last_uid
;
1112 *start
= eph_zsd
->last_uid
+ 1;
1113 atomic_add_32(&eph_zsd
->last_uid
, count
);
1114 mutex_exit(&eph_zsd
->eph_lock
);
1119 eph_gid_alloc(zone_t
*zone
, int flags
, gid_t
*start
, int count
)
1121 ephemeral_zsd_t
*eph_zsd
= get_ephemeral_zsd(zone
);
1123 ASSERT(eph_zsd
!= NULL
);
1125 mutex_enter(&eph_zsd
->eph_lock
);
1127 /* Test for unsigned integer wrap around */
1128 if (eph_zsd
->last_gid
+ count
< eph_zsd
->last_gid
) {
1129 mutex_exit(&eph_zsd
->eph_lock
);
1133 /* first call or idmap crashed and state corrupted */
1135 eph_zsd
->min_gid
= eph_zsd
->last_gid
;
1138 *start
= eph_zsd
->last_gid
+ 1;
1139 atomic_add_32(&eph_zsd
->last_gid
, count
);
1140 mutex_exit(&eph_zsd
->eph_lock
);
1145 * IMPORTANT.The two functions get_ephemeral_data() and set_ephemeral_data()
1146 * are project private functions that are for use of the test system only and
1147 * are not to be used for other purposes.
1151 get_ephemeral_data(zone_t
*zone
, uid_t
*min_uid
, uid_t
*last_uid
,
1152 gid_t
*min_gid
, gid_t
*last_gid
)
1154 ephemeral_zsd_t
*eph_zsd
= get_ephemeral_zsd(zone
);
1156 ASSERT(eph_zsd
!= NULL
);
1158 mutex_enter(&eph_zsd
->eph_lock
);
1160 *min_uid
= eph_zsd
->min_uid
;
1161 *last_uid
= eph_zsd
->last_uid
;
1162 *min_gid
= eph_zsd
->min_gid
;
1163 *last_gid
= eph_zsd
->last_gid
;
1165 mutex_exit(&eph_zsd
->eph_lock
);
1170 set_ephemeral_data(zone_t
*zone
, uid_t min_uid
, uid_t last_uid
,
1171 gid_t min_gid
, gid_t last_gid
)
1173 ephemeral_zsd_t
*eph_zsd
= get_ephemeral_zsd(zone
);
1175 ASSERT(eph_zsd
!= NULL
);
1177 mutex_enter(&eph_zsd
->eph_lock
);
1180 eph_zsd
->min_uid
= min_uid
;
1182 eph_zsd
->last_uid
= last_uid
;
1184 eph_zsd
->min_gid
= min_gid
;
1186 eph_zsd
->last_gid
= last_gid
;
1188 mutex_exit(&eph_zsd
->eph_lock
);
1192 * If the credential user SID or group SID is mapped to an ephemeral
1193 * ID, map the credential to nobody.
1196 crgetmapped(const cred_t
*cr
)
1198 ephemeral_zsd_t
*eph_zsd
;
1200 * Someone incorrectly passed a NULL cred to a vnode operation
1201 * either on purpose or by calling CRED() in interrupt context.
1206 if (cr
->cr_ksid
!= NULL
) {
1207 if (cr
->cr_ksid
->kr_sidx
[KSID_USER
].ks_id
> MAXUID
) {
1208 eph_zsd
= get_ephemeral_zsd(crgetzone(cr
));
1209 return (eph_zsd
->eph_nobody
);
1212 if (cr
->cr_ksid
->kr_sidx
[KSID_GROUP
].ks_id
> MAXUID
) {
1213 eph_zsd
= get_ephemeral_zsd(crgetzone(cr
));
1214 return (eph_zsd
->eph_nobody
);
1218 return ((cred_t
*)cr
);
1221 /* index should be in range for a ksidindex_t */
1223 crsetsid(cred_t
*cr
, ksid_t
*ksp
, int index
)
1225 ASSERT(cr
->cr_ref
<= 2);
1226 ASSERT(index
>= 0 && index
< KSID_COUNT
);
1227 if (cr
->cr_ksid
== NULL
&& ksp
== NULL
)
1229 cr
->cr_ksid
= kcrsid_setsid(cr
->cr_ksid
, ksp
, index
);
1233 crsetsidlist(cred_t
*cr
, ksidlist_t
*ksl
)
1235 ASSERT(cr
->cr_ref
<= 2);
1236 if (cr
->cr_ksid
== NULL
&& ksl
== NULL
)
1238 cr
->cr_ksid
= kcrsid_setsidlist(cr
->cr_ksid
, ksl
);
1242 crgetsid(const cred_t
*cr
, int i
)
1244 ASSERT(i
>= 0 && i
< KSID_COUNT
);
1245 if (cr
->cr_ksid
!= NULL
&& cr
->cr_ksid
->kr_sidx
[i
].ks_domain
)
1246 return ((ksid_t
*)&cr
->cr_ksid
->kr_sidx
[i
]);
1251 crgetsidlist(const cred_t
*cr
)
1253 if (cr
->cr_ksid
!= NULL
)
1254 return (cr
->cr_ksid
->kr_sidlist
);
1259 * Interface to set the effective and permitted privileges for
1260 * a credential; this interface does no security checks and is
1261 * intended for kernel (file)servers creating credentials with
1262 * specific privileges.
1265 crsetpriv(cred_t
*cr
, ...)
1270 ASSERT(cr
->cr_ref
<= 2);
1276 while ((privnm
= va_arg(ap
, const char *)) != NULL
) {
1277 int priv
= priv_getbyname(privnm
, 0);
1281 priv_addset(&CR_PPRIV(cr
), priv
);
1282 priv_addset(&CR_EPRIV(cr
), priv
);
1290 * Interface to effectively set the PRIV_ALL for
1291 * a credential; this interface does no security checks and is
1292 * intended for kernel (file)servers to extend the user credentials
1293 * to be ALL, like either kcred or zcred.
1296 crset_zone_privall(cred_t
*cr
)
1298 zone_t
*zone
= crgetzone(cr
);
1300 priv_fillset(&CR_LPRIV(cr
));
1301 CR_EPRIV(cr
) = CR_PPRIV(cr
) = CR_IPRIV(cr
) = CR_LPRIV(cr
);
1302 priv_intersect(zone
->zone_privset
, &CR_LPRIV(cr
));
1303 priv_intersect(zone
->zone_privset
, &CR_EPRIV(cr
));
1304 priv_intersect(zone
->zone_privset
, &CR_IPRIV(cr
));
1305 priv_intersect(zone
->zone_privset
, &CR_PPRIV(cr
));
1309 crgetcrklpd(const cred_t
*cr
)
1311 return (cr
->cr_klpd
);
1315 crsetcrklpd(cred_t
*cr
, struct credklpd
*crklpd
)
1317 ASSERT(cr
->cr_ref
<= 2);
1319 if (cr
->cr_klpd
!= NULL
)
1320 crklpd_rele(cr
->cr_klpd
);
1321 cr
->cr_klpd
= crklpd
;
1325 crgrpcopyin(int n
, gid_t
*gidset
)
1328 size_t sz
= CREDGRPSZ(n
);
1332 mem
= kmem_alloc(sz
, KM_SLEEP
);
1334 if (copyin(gidset
, mem
->crg_groups
, sizeof (gid_t
) * n
)) {
1339 mem
->crg_ngroups
= n
;
1340 qsort(mem
->crg_groups
, n
, sizeof (gid_t
), gidcmp
);
1345 crgetggroups(const credgrp_t
*grps
)
1347 return (grps
->crg_groups
);
1351 crsetcredgrp(cred_t
*cr
, credgrp_t
*grps
)
1353 ASSERT(cr
->cr_ref
<= 2);
1355 if (cr
->cr_grps
!= NULL
)
1356 crgrprele(cr
->cr_grps
);
1362 crgrprele(credgrp_t
*grps
)
1364 if (atomic_dec_32_nv(&grps
->crg_ref
) == 0)
1365 kmem_free(grps
, CREDGRPSZ(grps
->crg_ngroups
));
1369 crgrphold(credgrp_t
*grps
)
1371 atomic_inc_32(&grps
->crg_ref
);