xref: /xnu-11417.140.69/bsd/sys/ucred.h (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
29*43a90889SApple OSS Distributions /*
30*43a90889SApple OSS Distributions  * Copyright (c) 1989, 1993
31*43a90889SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
32*43a90889SApple OSS Distributions  *
33*43a90889SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
34*43a90889SApple OSS Distributions  * modification, are permitted provided that the following conditions
35*43a90889SApple OSS Distributions  * are met:
36*43a90889SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
37*43a90889SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
38*43a90889SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
39*43a90889SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
40*43a90889SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
41*43a90889SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
42*43a90889SApple OSS Distributions  *    must display the following acknowledgement:
43*43a90889SApple OSS Distributions  *	This product includes software developed by the University of
44*43a90889SApple OSS Distributions  *	California, Berkeley and its contributors.
45*43a90889SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
46*43a90889SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
47*43a90889SApple OSS Distributions  *    without specific prior written permission.
48*43a90889SApple OSS Distributions  *
49*43a90889SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50*43a90889SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51*43a90889SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52*43a90889SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53*43a90889SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54*43a90889SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55*43a90889SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56*43a90889SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57*43a90889SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58*43a90889SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59*43a90889SApple OSS Distributions  * SUCH DAMAGE.
60*43a90889SApple OSS Distributions  *
61*43a90889SApple OSS Distributions  *	@(#)ucred.h	8.4 (Berkeley) 1/9/95
62*43a90889SApple OSS Distributions  */
63*43a90889SApple OSS Distributions /*
64*43a90889SApple OSS Distributions  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65*43a90889SApple OSS Distributions  * support for mandatory and extensible security protections.  This notice
66*43a90889SApple OSS Distributions  * is included in support of clause 2.2 (b) of the Apple Public License,
67*43a90889SApple OSS Distributions  * Version 2.0.
68*43a90889SApple OSS Distributions  */
69*43a90889SApple OSS Distributions 
70*43a90889SApple OSS Distributions #ifndef _SYS_UCRED_H_
71*43a90889SApple OSS Distributions #define _SYS_UCRED_H_
72*43a90889SApple OSS Distributions 
73*43a90889SApple OSS Distributions #include <sys/appleapiopts.h>
74*43a90889SApple OSS Distributions #include <sys/cdefs.h>
75*43a90889SApple OSS Distributions #include <sys/param.h>
76*43a90889SApple OSS Distributions #include <bsm/audit.h>
77*43a90889SApple OSS Distributions 
78*43a90889SApple OSS Distributions struct label;
79*43a90889SApple OSS Distributions 
80*43a90889SApple OSS Distributions #ifdef __APPLE_API_UNSTABLE
81*43a90889SApple OSS Distributions #ifdef KERNEL
82*43a90889SApple OSS Distributions #include <sys/queue.h>
83*43a90889SApple OSS Distributions #include <os/base.h>
84*43a90889SApple OSS Distributions 
85*43a90889SApple OSS Distributions /*!
86*43a90889SApple OSS Distributions  * @struct ucred
87*43a90889SApple OSS Distributions  *
88*43a90889SApple OSS Distributions  * @brief
89*43a90889SApple OSS Distributions  * In-kernel credential structure.
90*43a90889SApple OSS Distributions  *
91*43a90889SApple OSS Distributions  * @discussion
92*43a90889SApple OSS Distributions  * Note that this structure should not be used outside the kernel,
93*43a90889SApple OSS Distributions  * nor should it or copies of it be exported outside.
94*43a90889SApple OSS Distributions  *
95*43a90889SApple OSS Distributions  * A credential has a relatively simple lifetime, with 3 phases:
96*43a90889SApple OSS Distributions  * 1. construction
97*43a90889SApple OSS Distributions  * 2. publication
98*43a90889SApple OSS Distributions  * 3. death
99*43a90889SApple OSS Distributions  *
100*43a90889SApple OSS Distributions  *
101*43a90889SApple OSS Distributions  * Construction
102*43a90889SApple OSS Distributions  * ~~~~~~~~~~~~
103*43a90889SApple OSS Distributions  *
104*43a90889SApple OSS Distributions  * The construction phase happens via various MACF hooks,
105*43a90889SApple OSS Distributions  * typically with the "associate" or "update" suffix.
106*43a90889SApple OSS Distributions  *
107*43a90889SApple OSS Distributions  * During this phase, the credential structure is completely private,
108*43a90889SApple OSS Distributions  * and can't be looked up. All "associate" and "update" callouts are
109*43a90889SApple OSS Distributions  * made serially (so no locking is required for clients to ensure
110*43a90889SApple OSS Distributions  * atomicity of updates) and keeping references on the cred is forbidden.
111*43a90889SApple OSS Distributions  *
112*43a90889SApple OSS Distributions  *
113*43a90889SApple OSS Distributions  * Publication
114*43a90889SApple OSS Distributions  * ~~~~~~~~~~~
115*43a90889SApple OSS Distributions  *
116*43a90889SApple OSS Distributions  * Once the credential has been constructed, it is being published
117*43a90889SApple OSS Distributions  * on its owning structure (typically the proc) and added into
118*43a90889SApple OSS Distributions  * a uniquing hash table.
119*43a90889SApple OSS Distributions  *
120*43a90889SApple OSS Distributions  * After this point, the credential becomes a refcounted immutable
121*43a90889SApple OSS Distributions  * "value type" data structure. MACF clients which have set labels
122*43a90889SApple OSS Distributions  * are not allowed to modify this label pointer anymore (though
123*43a90889SApple OSS Distributions  * their label itself might be mutable or contain caches).
124*43a90889SApple OSS Distributions  *
125*43a90889SApple OSS Distributions  * It means that while a client holds a reference on a credential,
126*43a90889SApple OSS Distributions  * it can consult labels without further synchronization or references.
127*43a90889SApple OSS Distributions  *
128*43a90889SApple OSS Distributions  *
129*43a90889SApple OSS Distributions  * Death
130*43a90889SApple OSS Distributions  * ~~~~~
131*43a90889SApple OSS Distributions  *
132*43a90889SApple OSS Distributions  * Credentials are managed under the smr_kauth_cred domain,
133*43a90889SApple OSS Distributions  * and retired according to the <kern/smr.h> rules.
134*43a90889SApple OSS Distributions  *
135*43a90889SApple OSS Distributions  * Once it is safe for the credential to be freed,
136*43a90889SApple OSS Distributions  * callbacks will clean up the resources the credential
137*43a90889SApple OSS Distributions  * holds onto via the MACF cred_label_destroy() hook.
138*43a90889SApple OSS Distributions  *
139*43a90889SApple OSS Distributions  * It means that under an smr_kauth_cred critical section,
140*43a90889SApple OSS Distributions  * clients can consult labels without further synchronization
141*43a90889SApple OSS Distributions  * or references, even after the credential hit a "0" refcount.
142*43a90889SApple OSS Distributions  *
143*43a90889SApple OSS Distributions  *
144*43a90889SApple OSS Distributions  * KPIs to interact with this data structure live in <sys/kauth.h>
145*43a90889SApple OSS Distributions  */
146*43a90889SApple OSS Distributions struct ucred {
147*43a90889SApple OSS Distributions #if BSD_KERNEL_PRIVATE
148*43a90889SApple OSS Distributions 	struct ucred_rw        *cr_rw;
149*43a90889SApple OSS Distributions 	void                   *cr_unused;
150*43a90889SApple OSS Distributions #else
151*43a90889SApple OSS Distributions 	LIST_ENTRY(ucred)       cr_link; /* never modify this without KAUTH_CRED_HASH_LOCK */
152*43a90889SApple OSS Distributions #endif
153*43a90889SApple OSS Distributions 	u_long                  cr_ref;  /* reference count */
154*43a90889SApple OSS Distributions 
155*43a90889SApple OSS Distributions 	struct posix_cred {
156*43a90889SApple OSS Distributions 		/*
157*43a90889SApple OSS Distributions 		 * The credential hash depends on everything from this point on
158*43a90889SApple OSS Distributions 		 * (see kauth_cred_get_hashkey)
159*43a90889SApple OSS Distributions 		 */
160*43a90889SApple OSS Distributions 		uid_t   cr_uid;         /* effective user id */
161*43a90889SApple OSS Distributions 		uid_t   cr_ruid;        /* real user id */
162*43a90889SApple OSS Distributions 		uid_t   cr_svuid;       /* saved user id */
163*43a90889SApple OSS Distributions 		u_short cr_ngroups;     /* number of groups in advisory list */
164*43a90889SApple OSS Distributions #if XNU_KERNEL_PRIVATE
165*43a90889SApple OSS Distributions 		u_short __cr_padding;
166*43a90889SApple OSS Distributions #endif
167*43a90889SApple OSS Distributions 		gid_t   cr_groups[NGROUPS];/* advisory group list */
168*43a90889SApple OSS Distributions 		gid_t   cr_rgid;        /* real group id */
169*43a90889SApple OSS Distributions 		gid_t   cr_svgid;       /* saved group id */
170*43a90889SApple OSS Distributions 		uid_t   cr_gmuid;       /* UID for group membership purposes */
171*43a90889SApple OSS Distributions 		int     cr_flags;       /* flags on credential */
172*43a90889SApple OSS Distributions 	} cr_posix;
173*43a90889SApple OSS Distributions 	struct label    * OS_PTRAUTH_SIGNED_PTR_AUTH_NULL("ucred.cr_label") cr_label;     /* MAC label */
174*43a90889SApple OSS Distributions 
175*43a90889SApple OSS Distributions 	/*
176*43a90889SApple OSS Distributions 	 * NOTE: If anything else (besides the flags)
177*43a90889SApple OSS Distributions 	 * added after the label, you must change
178*43a90889SApple OSS Distributions 	 * kauth_cred_find().
179*43a90889SApple OSS Distributions 	 */
180*43a90889SApple OSS Distributions 	struct au_session cr_audit;             /* user auditing data */
181*43a90889SApple OSS Distributions };
182*43a90889SApple OSS Distributions #else /* KERNEL */
183*43a90889SApple OSS Distributions struct ucred;
184*43a90889SApple OSS Distributions struct posix_cred;
185*43a90889SApple OSS Distributions #endif /* KERNEL */
186*43a90889SApple OSS Distributions 
187*43a90889SApple OSS Distributions #ifndef _KAUTH_CRED_T
188*43a90889SApple OSS Distributions #define _KAUTH_CRED_T
189*43a90889SApple OSS Distributions typedef struct ucred *kauth_cred_t;
190*43a90889SApple OSS Distributions typedef struct posix_cred *posix_cred_t;
191*43a90889SApple OSS Distributions #endif  /* !_KAUTH_CRED_T */
192*43a90889SApple OSS Distributions 
193*43a90889SApple OSS Distributions /*
194*43a90889SApple OSS Distributions  * Credential flags that can be set on a credential
195*43a90889SApple OSS Distributions  */
196*43a90889SApple OSS Distributions #define CRF_NOMEMBERD   0x00000001      /* memberd opt out by setgroups() */
197*43a90889SApple OSS Distributions #define CRF_MAC_ENFORCE 0x00000002      /* force entry through MAC Framework */
198*43a90889SApple OSS Distributions                                         /* also forces credential cache miss */
199*43a90889SApple OSS Distributions 
200*43a90889SApple OSS Distributions /*
201*43a90889SApple OSS Distributions  * This is the external representation of struct ucred.
202*43a90889SApple OSS Distributions  */
203*43a90889SApple OSS Distributions struct xucred {
204*43a90889SApple OSS Distributions 	u_int   cr_version;             /* structure layout version */
205*43a90889SApple OSS Distributions 	uid_t   cr_uid;                 /* effective user id */
206*43a90889SApple OSS Distributions 	short   cr_ngroups;             /* number of advisory groups */
207*43a90889SApple OSS Distributions 	gid_t   cr_groups[NGROUPS];     /* advisory group list */
208*43a90889SApple OSS Distributions };
209*43a90889SApple OSS Distributions #define XUCRED_VERSION  0
210*43a90889SApple OSS Distributions 
211*43a90889SApple OSS Distributions #define cr_gid cr_groups[0]
212*43a90889SApple OSS Distributions #define NOCRED ((kauth_cred_t )0)       /* no credential available */
213*43a90889SApple OSS Distributions #define FSCRED ((kauth_cred_t )-1)      /* filesystem credential */
214*43a90889SApple OSS Distributions 
215*43a90889SApple OSS Distributions #define IS_VALID_CRED(_cr)      ((_cr) != NOCRED && (_cr) != FSCRED)
216*43a90889SApple OSS Distributions 
217*43a90889SApple OSS Distributions #ifdef KERNEL
218*43a90889SApple OSS Distributions #ifdef __APPLE_API_OBSOLETE
219*43a90889SApple OSS Distributions __BEGIN_DECLS
220*43a90889SApple OSS Distributions int             suser(kauth_cred_t cred, u_short *acflag);
221*43a90889SApple OSS Distributions int             set_security_token(struct proc *p, struct ucred *cred);
222*43a90889SApple OSS Distributions void            cru2x(kauth_cred_t cr, struct xucred *xcr);
223*43a90889SApple OSS Distributions __END_DECLS
224*43a90889SApple OSS Distributions #endif /* __APPLE_API_OBSOLETE */
225*43a90889SApple OSS Distributions #endif /* KERNEL */
226*43a90889SApple OSS Distributions #endif /* __APPLE_API_UNSTABLE */
227*43a90889SApple OSS Distributions 
228*43a90889SApple OSS Distributions #endif /* !_SYS_UCRED_H_ */
229