xref: /xnu-10002.81.5/bsd/sys/kauth.h (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1 /*
2  * Copyright (c) 2004-2010 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
30  * support for mandatory and extensible security protections.  This notice
31  * is included in support of clause 2.2 (b) of the Apple Public License,
32  * Version 2.0.
33  */
34 
35 #ifndef _SYS_KAUTH_H
36 #define _SYS_KAUTH_H
37 
38 #include <sys/appleapiopts.h>
39 #include <sys/cdefs.h>
40 #include <mach/boolean.h>
41 #include <machine/types.h>      /* u_int8_t, etc. */
42 #include <sys/_types.h>         /* __offsetof() */
43 #include <sys/_types/_uid_t.h>  /* uid_t */
44 #include <sys/_types/_gid_t.h>     /* gid_t */
45 #include <sys/_types/_guid_t.h>
46 #include <sys/syslimits.h>      /* NGROUPS_MAX */
47 #ifdef KERNEL
48 #include <stdbool.h>
49 #include <sys/ucred.h>
50 #include <sys/lock.h>   /* lck_grp_t */
51 #endif /* KERNEL */
52 #if KERNEL_PRIVATE
53 #include <kern/smr_types.h>
54 #endif
55 
56 __BEGIN_DECLS
57 
58 #ifdef __APPLE_API_EVOLVING
59 
60 /*
61  * Identities.
62  */
63 
64 #define KAUTH_UID_NONE  (~(uid_t)0 - 100)       /* not a valid UID */
65 #define KAUTH_GID_NONE  (~(gid_t)0 - 100)       /* not a valid GID */
66 
67 /* NT Security Identifier, structure as defined by Microsoft */
68 #pragma pack(1)    /* push packing of 1 byte */
69 typedef struct {
70 	u_int8_t                sid_kind;
71 	u_int8_t                sid_authcount;
72 	u_int8_t                sid_authority[6];
73 #define KAUTH_NTSID_MAX_AUTHORITIES 16
74 	u_int32_t       sid_authorities[KAUTH_NTSID_MAX_AUTHORITIES];
75 } ntsid_t;
76 #pragma pack()    /* pop packing to previous packing level */
77 #define _NTSID_T
78 
79 /* valid byte count inside a SID structure */
80 #define KAUTH_NTSID_HDRSIZE     (8)
81 #define KAUTH_NTSID_SIZE(_s)    (KAUTH_NTSID_HDRSIZE + ((_s)->sid_authcount * sizeof(u_int32_t)))
82 
83 /*
84  * External lookup message payload; this structure is shared between the
85  * kernel group membership resolver, and the user space group membership
86  * resolver daemon, and is use to communicate resolution requests from the
87  * kernel to user space, and the result of that request from user space to
88  * the kernel.
89  */
90 struct kauth_identity_extlookup {
91 	u_int32_t       el_seqno;       /* request sequence number */
92 	u_int32_t       el_result;      /* lookup result */
93 #define KAUTH_EXTLOOKUP_SUCCESS         0       /* results here are good */
94 #define KAUTH_EXTLOOKUP_BADRQ           1       /* request badly formatted */
95 #define KAUTH_EXTLOOKUP_FAILURE         2       /* transient failure during lookup */
96 #define KAUTH_EXTLOOKUP_FATAL           3       /* permanent failure during lookup */
97 #define KAUTH_EXTLOOKUP_INPROG          100     /* request in progress */
98 	u_int32_t       el_flags;
99 #define KAUTH_EXTLOOKUP_VALID_UID       (1<<0)
100 #define KAUTH_EXTLOOKUP_VALID_UGUID     (1<<1)
101 #define KAUTH_EXTLOOKUP_VALID_USID      (1<<2)
102 #define KAUTH_EXTLOOKUP_VALID_GID       (1<<3)
103 #define KAUTH_EXTLOOKUP_VALID_GGUID     (1<<4)
104 #define KAUTH_EXTLOOKUP_VALID_GSID      (1<<5)
105 #define KAUTH_EXTLOOKUP_WANT_UID        (1<<6)
106 #define KAUTH_EXTLOOKUP_WANT_UGUID      (1<<7)
107 #define KAUTH_EXTLOOKUP_WANT_USID       (1<<8)
108 #define KAUTH_EXTLOOKUP_WANT_GID        (1<<9)
109 #define KAUTH_EXTLOOKUP_WANT_GGUID      (1<<10)
110 #define KAUTH_EXTLOOKUP_WANT_GSID       (1<<11)
111 #define KAUTH_EXTLOOKUP_WANT_MEMBERSHIP (1<<12)
112 #define KAUTH_EXTLOOKUP_VALID_MEMBERSHIP (1<<13)
113 #define KAUTH_EXTLOOKUP_ISMEMBER        (1<<14)
114 #define KAUTH_EXTLOOKUP_VALID_PWNAM     (1<<15)
115 #define KAUTH_EXTLOOKUP_WANT_PWNAM      (1<<16)
116 #define KAUTH_EXTLOOKUP_VALID_GRNAM     (1<<17)
117 #define KAUTH_EXTLOOKUP_WANT_GRNAM      (1<<18)
118 #define KAUTH_EXTLOOKUP_VALID_SUPGRPS   (1<<19)
119 #define KAUTH_EXTLOOKUP_WANT_SUPGRPS    (1<<20)
120 
121 	__darwin_pid_t  el_info_pid;            /* request on behalf of PID */
122 	u_int64_t       el_extend;              /* extension field */
123 	u_int32_t       el_info_reserved_1;     /* reserved (APPLE) */
124 
125 	uid_t           el_uid;         /* user ID */
126 	guid_t          el_uguid;       /* user GUID */
127 	u_int32_t       el_uguid_valid; /* TTL on translation result (seconds) */
128 	ntsid_t         el_usid;        /* user NT SID */
129 	u_int32_t       el_usid_valid;  /* TTL on translation result (seconds) */
130 	gid_t           el_gid;         /* group ID */
131 	guid_t          el_gguid;       /* group GUID */
132 	u_int32_t       el_gguid_valid; /* TTL on translation result (seconds) */
133 	ntsid_t         el_gsid;        /* group SID */
134 	u_int32_t       el_gsid_valid;  /* TTL on translation result (seconds) */
135 	u_int32_t       el_member_valid; /* TTL on group lookup result */
136 	u_int32_t       el_sup_grp_cnt;  /* count of supplemental groups up to NGROUPS */
137 	gid_t           el_sup_groups[NGROUPS_MAX];     /* supplemental group list */
138 };
139 
140 struct kauth_cache_sizes {
141 	u_int32_t kcs_group_size;
142 	u_int32_t kcs_id_size;
143 };
144 
145 #define KAUTH_EXTLOOKUP_REGISTER        (0)
146 #define KAUTH_EXTLOOKUP_RESULT          (1<<0)
147 #define KAUTH_EXTLOOKUP_WORKER          (1<<1)
148 #define KAUTH_EXTLOOKUP_DEREGISTER      (1<<2)
149 #define KAUTH_GET_CACHE_SIZES           (1<<3)
150 #define KAUTH_SET_CACHE_SIZES           (1<<4)
151 #define KAUTH_CLEAR_CACHES              (1<<5)
152 
153 #define IDENTITYSVC_ENTITLEMENT         "com.apple.private.identitysvc"
154 
155 #ifdef KERNEL
156 #pragma mark - kauth_cred
157 
158 /*!
159  * @brief
160  * Retains a credential data structure.
161  *
162  * @Description
163  * The reference returned must be released with @c kauth_cred_unref().
164  */
165 extern void         kauth_cred_ref(kauth_cred_t cred);
166 
167 /*!
168  * @brief
169  * Releases a credential data structure, and nils out the pointer.
170  *
171  * @Description
172  * @c credp must be non NULL, but can point to a NULL/NOCRED credential.
173  */
174 extern void         kauth_cred_unref(kauth_cred_t *credp);
175 
176 
177 /*!
178  * @brief
179  * Returns the current thread assumed credentials.
180  *
181  * @discussion
182  * These might differ from the proc's credential if settid() has been called.
183  * This never returns NULL/NOCRED.
184  *
185  * This function doesn't take a reference, and the returned pointer is valid
186  * for the duration of the current syscall.
187  */
188 extern kauth_cred_t kauth_cred_get(void);
189 
190 /*!
191  * @brief
192  * Returns the current thread assumed credentials, with a reference.
193  *
194  * @discussion
195  * These might differ from the proc's credential if settid() has been called.
196  * This never returns NULL/NOCRED.
197  *
198  * The caller must call kauth_cred_unref() to dispose of the returned value.
199  *
200  * This is equivalent to @c kauth_cred_ref(kauth_cred_get())
201  */
202 extern kauth_cred_t kauth_cred_get_with_ref(void);
203 
204 /*!
205  * @brief
206  * Returns the specified proc credentials, with a reference.
207  *
208  * @discussion
209  * The caller must call kauth_cred_unref() to dispose of the returned value.
210  * This never returns NULL/NOCRED.
211  *
212  * The caller must call kauth_cred_unref() to dispose of the returned value.
213  */
214 extern kauth_cred_t kauth_cred_proc_ref(proc_t procp);
215 
216 
217 /*!
218  * @brief
219  * Obsolete way to create a valid posix-only credential structure out of
220  * a model, DO NOT USE.
221  */
222 extern kauth_cred_t kauth_cred_create(kauth_cred_t cred);
223 
224 
225 #pragma mark kauth_cred: accessors
226 
227 /*!
228  * @brief
229  * Returns the effective user ID for the specified @c kauth_cred_t.
230  */
231 extern uid_t        kauth_cred_getuid(kauth_cred_t _cred);
232 
233 /*!
234  * @brief
235  * Returns the real user ID for the specified @c kauth_cred_t.
236  */
237 extern uid_t        kauth_cred_getruid(kauth_cred_t _cred);
238 
239 /*!
240  * @brief
241  * Returns the saved user ID for the specified @c kauth_cred_t.
242  */
243 extern uid_t        kauth_cred_getsvuid(kauth_cred_t _cred);
244 
245 /*!
246  * @brief
247  * Returns whether the current credential effective user ID is the super user.
248  */
249 extern int          kauth_cred_issuser(kauth_cred_t _cred);
250 
251 /*!
252  * @brief
253  * Returns the effective group ID for the specified @c kauth_cred_t.
254  */
255 extern gid_t        kauth_cred_getgid(kauth_cred_t _cred);
256 
257 /*!
258  * @brief
259  * Returns the real group ID for the specified @c kauth_cred_t.
260  */
261 extern gid_t        kauth_cred_getrgid(kauth_cred_t _cred);
262 
263 /*!
264  * @brief
265  * Returns the saved group ID for the specified @c kauth_cred_t.
266  */
267 extern gid_t        kauth_cred_getsvgid(kauth_cred_t _cred);
268 
269 /*!
270  * @brief
271  * Returns the effective user ID for the current thread.
272  *
273  * @Description
274  * Equivalent to @c kauth_getuid(kauth_cred_get())
275  */
276 extern uid_t        kauth_getuid(void);
277 
278 /*!
279  * @brief
280  * Returns the real user ID for the current thread.
281  *
282  * @Description
283  * Equivalent to @c kauth_getruid(kauth_cred_get())
284  */
285 extern uid_t        kauth_getruid(void);
286 
287 /*!
288  * @brief
289  * Returns the effective group ID for the current thread.
290  *
291  * @Description
292  * Equivalent to @c kauth_getgid(kauth_cred_get())
293  */
294 extern gid_t        kauth_getgid(void);
295 
296 /*!
297  * @brief
298  * Returns the real group ID for the current thread.
299  *
300  * @Description
301  * Equivalent to @c kauth_getrgid(kauth_cred_get())
302  */
303 extern gid_t        kauth_getrgid(void);
304 
305 
306 #pragma mark kauth_cred: MACF label updates
307 
308 struct label;
309 
310 /*!
311  * @brief
312  * Updates the MAC label associated with a credential.
313  *
314  * @discussion
315  * This function returns a new credential where the passed
316  * in label is updated with the specified one.
317  *
318  * This will cause the @c cred_label_associate() MAC hook to be invoked
319  * first (so that all MAC policies have a chance to make the newly formed
320  * credential inherit labels) then the @c cred_label_update() hook
321  * (which will allow the newly made credential labels to be overridden).
322  *
323  * This never returns NULL/NOCRED.
324  *
325  * @param cred          (ref consumed) the credentials to use as a model.
326  * @param label         the model label to copy from.
327  */
328 extern kauth_cred_t kauth_cred_label_update(
329 	kauth_cred_t            cred,
330 	struct label           *label);
331 
332 
333 /*!
334  * @brief
335  * Updates the MAC label insde the proc's credentials.
336  *
337  * @discussion
338  * This function applies @c kauth_cred_label_update() to the specified
339  * process's credntials, and updates the process's credentials
340  * with the outcome.
341  *
342  * This function never fails (returns 0 all the time).
343  *
344  * @param proc          the process which creedntials must be updated.
345  * @param label         the model label to copy from.
346  */
347 extern int          kauth_proc_label_update(
348 	struct proc            *proc,
349 	struct label           *label);
350 
351 
352 #pragma mark kauth_cred: group membership (private API)
353 /*
354  * This part of the kernel is considered private and prototypes
355  * will be eventually be removed from the public headers.
356  */
357 
358 extern int          kauth_cred_pwnam2guid(char *pwnam, guid_t *guidp);
359 extern int          kauth_cred_grnam2guid(char *grnam, guid_t *guidp);
360 extern int          kauth_cred_guid2pwnam(guid_t *guidp, char *pwnam);
361 extern int          kauth_cred_guid2grnam(guid_t *guidp, char *grnam);
362 extern int          kauth_cred_guid2uid(guid_t *_guid, uid_t *_uidp);
363 extern int          kauth_cred_guid2gid(guid_t *_guid, gid_t *_gidp);
364 extern int          kauth_cred_ntsid2uid(ntsid_t *_sid, uid_t *_uidp);
365 extern int          kauth_cred_ntsid2gid(ntsid_t *_sid, gid_t *_gidp);
366 extern int          kauth_cred_ntsid2guid(ntsid_t *_sid, guid_t *_guidp);
367 extern int          kauth_cred_uid2guid(uid_t _uid, guid_t *_guidp);
368 extern int          kauth_cred_getguid(kauth_cred_t _cred, guid_t *_guidp);
369 extern int          kauth_cred_gid2guid(gid_t _gid, guid_t *_guidp);
370 extern int          kauth_cred_uid2ntsid(uid_t _uid, ntsid_t *_sidp);
371 extern int          kauth_cred_getntsid(kauth_cred_t _cred, ntsid_t *_sidp);
372 extern int          kauth_cred_gid2ntsid(gid_t _gid, ntsid_t *_sidp);
373 extern int          kauth_cred_guid2ntsid(guid_t *_guid, ntsid_t *_sidp);
374 extern int          kauth_cred_ismember_gid(kauth_cred_t _cred, gid_t _gid, int *_resultp);
375 extern int          kauth_cred_ismember_guid(kauth_cred_t _cred, guid_t *_guidp, int *_resultp);
376 extern int          kauth_cred_nfs4domain2dsnode(char *nfs4domain, char *dsnode);
377 extern int          kauth_cred_dsnode2nfs4domain(char *dsnode, char *nfs4domain);
378 
379 extern int          groupmember(gid_t gid, kauth_cred_t cred);
380 
381 
382 #ifdef KERNEL_PRIVATE
383 extern int          kauth_cred_getgroups(kauth_cred_t _cred, gid_t *_groups, size_t *_groupcount);
384 
385 #endif /* KERNEL_PRIVATE */
386 #ifdef XNU_KERNEL_PRIVATE
387 #pragma mark kauth_cred: XNU only
388 #pragma GCC visibility push(hidden)
389 
390 extern lck_grp_t    kauth_lck_grp;
391 
392 extern void         kauth_init(void);
393 
394 #pragma mark XNU only: kauth_cred interfaces
395 
396 extern kauth_cred_t posix_cred_create(posix_cred_t pcred);
397 extern posix_cred_t posix_cred_get(kauth_cred_t cred) __pure2;
398 extern int          posix_cred_access(kauth_cred_t cred, id_t object_uid, id_t object_gid, mode_t object_mode, mode_t mode_req);
399 
400 extern int          cantrace(proc_t cur_procp, kauth_cred_t creds, proc_t traced_procp, int *errp);
401 extern kauth_cred_t kauth_cred_copy_real(kauth_cred_t cred);
402 
403 
404 /*!
405  * @brief
406  * Type of functions used to derive credentials from an original one.
407  *
408  * @description
409  * The @c model argument is an on-stack template that is to be mutated
410  * in place, and that will be used to make the derived credential.
411  *
412  * The function should return false if it made no modifications,
413  * and true if it did (it is OK to return true incorrectly,
414  * it is just a little less efficient).
415  */
416 typedef bool (^kauth_cred_derive_t)(kauth_cred_t parent, kauth_cred_t model);
417 
418 /*!
419  * @brief
420  * Derive a credential from a given cred.
421  *
422  * @description
423  * This function never returns NULL.
424  */
425 extern kauth_cred_t kauth_cred_derive(kauth_cred_t cred, kauth_cred_derive_t fn);
426 
427 __enum_decl(proc_settoken_t, uint32_t, {
428 	PROC_SETTOKEN_NONE       = 0x0000,
429 	PROC_SETTOKEN_LAZY       = 0x0001, /* set the security token on change */
430 	PROC_SETTOKEN_ALWAYS     = 0x0002, /* set the security token all the time */
431 	PROC_SETTOKEN_SETUGID    = 0x0003, /* PROC_SETTOKEN_LAZY + set P_SUGID */
432 });
433 
434 /*!
435  * @brief
436  * Update the credential on the specified proc.
437  *
438  * @description
439  * The function returns true if an update took place.
440  */
441 extern bool         kauth_cred_proc_update(proc_t p, proc_settoken_t action, kauth_cred_derive_t fn);
442 
443 extern bool         kauth_cred_model_setresuid(kauth_cred_t model, uid_t ruid, uid_t euid, uid_t svuid, uid_t gmuid);
444 extern bool         kauth_cred_model_setresgid(kauth_cred_t model, gid_t rgid, gid_t egid, gid_t svgid);
445 extern bool         kauth_cred_model_setuidgid(kauth_cred_t model, uid_t uid, gid_t gid);
446 extern bool         kauth_cred_model_setgroups(kauth_cred_t model, gid_t *groups, size_t groupcount, uid_t gmuid);
447 extern bool         kauth_cred_model_setauditinfo(kauth_cred_t model, au_session_t *);
448 
449 extern void         kauth_cred_thread_update(struct thread *, proc_t);
450 #ifdef CONFIG_MACF
451 extern void         kauth_proc_label_update_execve(struct proc *p, struct vfs_context *ctx, struct vnode *vp, off_t offset, struct vnode *scriptvp, struct label *scriptlabel, struct label *execlabel, unsigned int *csflags, void *psattr, int *disjoint, int *update_return);
452 #endif
453 extern int          kauth_cred_gid_subset(kauth_cred_t _cred1, kauth_cred_t _cred2, int *_resultp);
454 
455 extern kauth_cred_t kauth_cred_require(kauth_cred_t cred) __pure2;
456 
457 extern void         kauth_cred_set(kauth_cred_t *credp, kauth_cred_t new_cred);
458 extern void         kauth_cred_set_and_unref(kauth_cred_t *credp, kauth_cred_t *new_credp);
459 #if CONFIG_EXT_RESOLVER
460 extern void         kauth_resolver_identity_reset(void);
461 #endif
462 
463 /*
464  * `kauth_cred_set` and `kauth_cred_unref` take pointers to a
465  * `kauth_cred_t`, which the compiler considers strictly different from a
466  * pointer to a signed `kauth_cred_t` (as it should do).  These macros
467  * therefore authenticate the arguments into naked locals, pass them to the
468  * function and then write back the results, signing them in the process.
469  */
470 #define kauth_cred_set(credp, new_cred) \
471     do { \
472 	    kauth_cred_t _cred __single = *(credp); \
473 	    (kauth_cred_set)(&_cred, (new_cred)); \
474 	    *(credp) = _cred; \
475     } while (0)
476 
477 #define kauth_cred_set_and_unref(credp, new_credp) \
478     do { \
479 	    kauth_cred_t _cred __single = *(credp); \
480 	    (kauth_cred_set_and_unref)(&_cred, (new_credp)); \
481 	    *(credp) = _cred; \
482     } while (0)
483 
484 #define kauth_cred_unref(credp) \
485     do { \
486 	    kauth_cred_t _credp __single = *(credp); \
487 	    (kauth_cred_unref)(&_credp); \
488 	    *(credp) = _credp; \
489     } while (0)
490 
491 #pragma GCC visibility pop
492 #endif /* XNU_KERNEL_PRIVATE */
493 #endif /* KERNEL */
494 #if defined(KERNEL) || defined (_SYS_ACL_H)
495 #pragma mark - kauth
496 #pragma mark kauth: Generic Access Control Lists.
497 
498 typedef u_int32_t kauth_ace_rights_t;
499 
500 /* Access Control List Entry (ACE) */
501 struct kauth_ace {
502 	guid_t          ace_applicable;
503 	u_int32_t       ace_flags;
504 #define KAUTH_ACE_KINDMASK              0xf
505 #define KAUTH_ACE_PERMIT                1
506 #define KAUTH_ACE_DENY                  2
507 #define KAUTH_ACE_AUDIT                 3       /* not implemented */
508 #define KAUTH_ACE_ALARM                 4       /* not implemented */
509 #define KAUTH_ACE_INHERITED             (1<<4)
510 #define KAUTH_ACE_FILE_INHERIT          (1<<5)
511 #define KAUTH_ACE_DIRECTORY_INHERIT     (1<<6)
512 #define KAUTH_ACE_LIMIT_INHERIT         (1<<7)
513 #define KAUTH_ACE_ONLY_INHERIT          (1<<8)
514 #define KAUTH_ACE_SUCCESS               (1<<9)  /* not implemented (AUDIT/ALARM) */
515 #define KAUTH_ACE_FAILURE               (1<<10) /* not implemented (AUDIT/ALARM) */
516 /* All flag bits controlling ACE inheritance */
517 #define KAUTH_ACE_INHERIT_CONTROL_FLAGS         \
518 	        (KAUTH_ACE_FILE_INHERIT |       \
519 	         KAUTH_ACE_DIRECTORY_INHERIT |  \
520 	         KAUTH_ACE_LIMIT_INHERIT |      \
521 	         KAUTH_ACE_ONLY_INHERIT)
522 	kauth_ace_rights_t ace_rights;          /* scope specific */
523 	/* These rights are never tested, but may be present in an ACL */
524 #define KAUTH_ACE_GENERIC_ALL           (1<<21)
525 #define KAUTH_ACE_GENERIC_EXECUTE       (1<<22)
526 #define KAUTH_ACE_GENERIC_WRITE         (1<<23)
527 #define KAUTH_ACE_GENERIC_READ          (1<<24)
528 };
529 
530 #ifndef _KAUTH_ACE
531 #define _KAUTH_ACE
532 typedef struct kauth_ace *kauth_ace_t;
533 #endif
534 
535 
536 /* Access Control List */
537 struct kauth_acl {
538 	u_int32_t       acl_entrycount;
539 	u_int32_t       acl_flags;
540 
541 	struct kauth_ace acl_ace[1];
542 };
543 
544 /*
545  * XXX this value needs to be raised - 3893388
546  */
547 #define KAUTH_ACL_MAX_ENTRIES           128
548 
549 /*
550  * The low 16 bits of the flags field are reserved for filesystem
551  * internal use and must be preserved by all APIs.  This includes
552  * round-tripping flags through user-space interfaces.
553  */
554 #define KAUTH_ACL_FLAGS_PRIVATE (0xffff)
555 
556 /*
557  * The high 16 bits of the flags are used to store attributes and
558  * to request specific handling of the ACL.
559  */
560 
561 /* inheritance will be deferred until the first rename operation */
562 #define KAUTH_ACL_DEFER_INHERIT (1<<16)
563 /* this ACL must not be overwritten as part of an inheritance operation */
564 #define KAUTH_ACL_NO_INHERIT    (1<<17)
565 
566 /* acl_entrycount that tells us the ACL is not valid */
567 #define KAUTH_FILESEC_NOACL ((u_int32_t)(-1))
568 
569 /*
570  * If the acl_entrycount field is KAUTH_FILESEC_NOACL, then the size is the
571  * same as a kauth_acl structure; the intent is to put an actual entrycount of
572  * KAUTH_FILESEC_NOACL on disk to distinguish a kauth_filesec_t with an empty
573  * entry (Windows treats this as "deny all") from one that merely indicates a
574  * file group and/or owner guid values.
575  */
576 #define KAUTH_ACL_SIZE(c)       (__offsetof(struct kauth_acl, acl_ace) + ((u_int32_t)(c) != KAUTH_FILESEC_NOACL ? ((c) * sizeof(struct kauth_ace)) : 0))
577 #define KAUTH_ACL_COPYSIZE(p)   KAUTH_ACL_SIZE((p)->acl_entrycount)
578 
579 
580 #ifndef _KAUTH_ACL
581 #define _KAUTH_ACL
582 typedef struct kauth_acl *kauth_acl_t;
583 #endif
584 
585 #ifdef KERNEL
586 kauth_acl_t     kauth_acl_alloc(int size);
587 void            kauth_acl_free(kauth_acl_t fsp);
588 #endif
589 
590 
591 /*
592  * Extended File Security.
593  */
594 
595 /* File Security information */
596 struct kauth_filesec {
597 	u_int32_t       fsec_magic;
598 #define KAUTH_FILESEC_MAGIC     0x012cc16d
599 	guid_t          fsec_owner;
600 	guid_t          fsec_group;
601 
602 	struct kauth_acl fsec_acl;
603 };
604 
605 /* backwards compatibility */
606 #define fsec_entrycount fsec_acl.acl_entrycount
607 #define fsec_flags      fsec_acl.acl_flags
608 #define fsec_ace        fsec_acl.acl_ace
609 #define KAUTH_FILESEC_FLAGS_PRIVATE     KAUTH_ACL_FLAGS_PRIVATE
610 #define KAUTH_FILESEC_DEFER_INHERIT     KAUTH_ACL_DEFER_INHERIT
611 #define KAUTH_FILESEC_NO_INHERIT        KAUTH_ACL_NO_INHERIT
612 #define KAUTH_FILESEC_NONE      ((kauth_filesec_t)0)
613 #define KAUTH_FILESEC_WANTED    ((kauth_filesec_t)1)
614 
615 #ifndef _KAUTH_FILESEC
616 #define _KAUTH_FILESEC
617 typedef struct kauth_filesec *kauth_filesec_t;
618 #endif
619 
620 #define KAUTH_FILESEC_SIZE(c)           (__offsetof(struct kauth_filesec, fsec_acl) + __offsetof(struct kauth_acl, acl_ace) + (c) * sizeof(struct kauth_ace))
621 #define KAUTH_FILESEC_COPYSIZE(p)       KAUTH_FILESEC_SIZE(((p)->fsec_entrycount == KAUTH_FILESEC_NOACL) ? 0 : (p)->fsec_entrycount)
622 #define KAUTH_FILESEC_COUNT(s)          (((s)  - KAUTH_FILESEC_SIZE(0)) / sizeof(struct kauth_ace))
623 #define KAUTH_FILESEC_VALID(s)          ((s) >= KAUTH_FILESEC_SIZE(0) && (((s) - KAUTH_FILESEC_SIZE(0)) % sizeof(struct kauth_ace)) == 0)
624 
625 #define KAUTH_FILESEC_XATTR     "com.apple.system.Security"
626 
627 /* Allowable first arguments to kauth_filesec_acl_setendian() */
628 #define KAUTH_ENDIAN_HOST       0x00000001      /* set host endianness */
629 #define KAUTH_ENDIAN_DISK       0x00000002      /* set disk endianness */
630 
631 #endif /* KERNEL || <sys/acl.h> */
632 #ifdef KERNEL
633 #pragma mark kauth: Scope management
634 
635 struct kauth_scope;
636 typedef struct kauth_scope *kauth_scope_t;
637 struct kauth_listener;
638 typedef struct kauth_listener *kauth_listener_t;
639 #ifndef _KAUTH_ACTION_T
640 typedef int kauth_action_t;
641 # define _KAUTH_ACTION_T
642 #endif
643 
644 typedef int (* kauth_scope_callback_t)(kauth_cred_t _credential,
645     void *_idata,
646     kauth_action_t _action,
647     uintptr_t _arg0,
648     uintptr_t _arg1,
649     uintptr_t _arg2,
650     uintptr_t _arg3);
651 
652 #define KAUTH_RESULT_ALLOW      (1)
653 #define KAUTH_RESULT_DENY       (2)
654 #define KAUTH_RESULT_DEFER      (3)
655 
656 struct kauth_acl_eval {
657 	kauth_ace_t             ae_acl;
658 	int                     ae_count;
659 	kauth_ace_rights_t      ae_requested;
660 	kauth_ace_rights_t      ae_residual;
661 	int                     ae_result;
662 	boolean_t               ae_found_deny;
663 	int                     ae_options;
664 #define KAUTH_AEVAL_IS_OWNER    (1<<0)          /* authorizing operation for owner */
665 #define KAUTH_AEVAL_IN_GROUP    (1<<1)          /* authorizing operation for groupmember */
666 #define KAUTH_AEVAL_IN_GROUP_UNKNOWN    (1<<2)          /* authorizing operation for unknown group membership */
667 	/* expansions for 'generic' rights bits */
668 	kauth_ace_rights_t      ae_exp_gall;
669 	kauth_ace_rights_t      ae_exp_gread;
670 	kauth_ace_rights_t      ae_exp_gwrite;
671 	kauth_ace_rights_t      ae_exp_gexec;
672 };
673 
674 typedef struct kauth_acl_eval *kauth_acl_eval_t;
675 
676 kauth_filesec_t kauth_filesec_alloc(int size);
677 void            kauth_filesec_free(kauth_filesec_t fsp);
678 extern kauth_scope_t kauth_register_scope(const char *_identifier, kauth_scope_callback_t _callback, void *_idata);
679 extern void     kauth_deregister_scope(kauth_scope_t _scope);
680 __kpi_deprecated("Use EndpointSecurity instead")
681 extern kauth_listener_t kauth_listen_scope(const char *_identifier, kauth_scope_callback_t _callback, void *_idata);
682 __kpi_deprecated("Use EndpointSecurity instead")
683 extern void     kauth_unlisten_scope(kauth_listener_t _scope);
684 extern int      kauth_authorize_action(kauth_scope_t _scope, kauth_cred_t _credential, kauth_action_t _action,
685     uintptr_t _arg0, uintptr_t _arg1, uintptr_t _arg2, uintptr_t _arg3);
686 
687 /*
688  * Generic scope.
689  */
690 #define KAUTH_SCOPE_GENERIC     "com.apple.kauth.generic"
691 
692 /* Actions */
693 #define KAUTH_GENERIC_ISSUSER                   1
694 
695 /*
696  * Process/task scope.
697  */
698 #define KAUTH_SCOPE_PROCESS     "com.apple.kauth.process"
699 
700 /* Actions */
701 #define KAUTH_PROCESS_CANSIGNAL                 1
702 #define KAUTH_PROCESS_CANTRACE                  2
703 
704 extern int      kauth_authorize_process(kauth_cred_t _credential, kauth_action_t _action,
705     struct proc *_process, uintptr_t _arg1, uintptr_t _arg2, uintptr_t _arg3);
706 
707 /*
708  * Vnode operation scope.
709  *
710  * Prototype for vnode_authorize is in vnode.h
711  */
712 #define KAUTH_SCOPE_VNODE       "com.apple.kauth.vnode"
713 
714 /*
715  * File system operation scope.
716  *
717  */
718 #define KAUTH_SCOPE_FILEOP      "com.apple.kauth.fileop"
719 
720 /* Actions */
721 #define KAUTH_FILEOP_OPEN                       1
722 #define KAUTH_FILEOP_CLOSE                      2
723 #define KAUTH_FILEOP_RENAME                     3
724 #define KAUTH_FILEOP_EXCHANGE                   4
725 #define KAUTH_FILEOP_LINK                       5
726 #define KAUTH_FILEOP_EXEC                       6
727 #define KAUTH_FILEOP_DELETE                     7
728 #define KAUTH_FILEOP_WILL_RENAME                8
729 
730 /*
731  * arguments passed to KAUTH_FILEOP_OPEN listeners
732  *		arg0 is pointer to vnode (vnode *) for given user path.
733  *		arg1 is pointer to path (char *) passed in to open.
734  * arguments passed to KAUTH_FILEOP_CLOSE listeners
735  *		arg0 is pointer to vnode (vnode *) for file to be closed.
736  *		arg1 is pointer to path (char *) of file to be closed.
737  *		arg2 is close flags.
738  * arguments passed to KAUTH_FILEOP_WILL_RENAME listeners
739  *		arg0 is pointer to vnode (vnode *) of the file being renamed
740  *		arg1 is pointer to the "from" path (char *)
741  *		arg2 is pointer to the "to" path (char *)
742  * arguments passed to KAUTH_FILEOP_RENAME listeners
743  *		arg0 is pointer to "from" path (char *).
744  *		arg1 is pointer to "to" path (char *).
745  * arguments passed to KAUTH_FILEOP_EXCHANGE listeners
746  *		arg0 is pointer to file 1 path (char *).
747  *		arg1 is pointer to file 2 path (char *).
748  * arguments passed to KAUTH_FILEOP_LINK listeners
749  *		arg0 is pointer to path to file we are linking to (char *).
750  *		arg1 is pointer to path to the new link file (char *).
751  * arguments passed to KAUTH_FILEOP_EXEC listeners
752  *		arg0 is pointer to vnode (vnode *) for executable.
753  *		arg1 is pointer to path (char *) to executable.
754  * arguments passed to KAUTH_FILEOP_DELETE listeners
755  *		arg0 is pointer to vnode (vnode *) of file/dir that was deleted.
756  *		arg1 is pointer to path (char *) of file/dir that was deleted.
757  */
758 
759 /* Flag values returned to close listeners. */
760 #define KAUTH_FILEOP_CLOSE_MODIFIED                     (1<<1)
761 
762 /* GUID, NTSID helpers */
763 extern guid_t   kauth_null_guid;
764 extern int      kauth_guid_equal(guid_t *_guid1, guid_t *_guid2);
765 
766 #ifdef KERNEL_PRIVATE
767 
768 extern int      kauth_acl_evaluate(kauth_cred_t _credential, kauth_acl_eval_t _eval);
769 
770 #endif /* KERNEL_PRIVATE */
771 #ifdef XNU_KERNEL_PRIVATE
772 #pragma mark kauth: XNU only
773 #pragma GCC visibility push(hidden)
774 
775 void            kauth_filesec_acl_setendian(int, kauth_filesec_t, kauth_acl_t);
776 int             kauth_copyinfilesec(user_addr_t xsecurity, kauth_filesec_t *xsecdestpp);
777 extern int      kauth_acl_inherit(vnode_t _dvp, kauth_acl_t _initial, kauth_acl_t *_product, int _isdir, vfs_context_t _ctx);
778 
779 extern int      kauth_authorize_allow(kauth_cred_t _credential, void *_idata, kauth_action_t _action,
780     uintptr_t _arg0, uintptr_t _arg1, uintptr_t _arg2, uintptr_t _arg3);
781 
782 extern int      kauth_authorize_generic(kauth_cred_t credential, kauth_action_t action);
783 
784 extern int      kauth_authorize_fileop_has_listeners(void);
785 
786 extern int      kauth_authorize_fileop(kauth_cred_t _credential, kauth_action_t _action,
787     uintptr_t _arg0, uintptr_t _arg1);
788 
789 extern int      kauth_ntsid_equal(ntsid_t *_sid1, ntsid_t *_sid2);
790 
791 extern int      kauth_wellknown_guid(guid_t *_guid);
792 #define KAUTH_WKG_NOT           0       /* not a well-known GUID */
793 #define KAUTH_WKG_OWNER         1
794 #define KAUTH_WKG_GROUP         2
795 #define KAUTH_WKG_NOBODY        3
796 #define KAUTH_WKG_EVERYBODY     4
797 
798 #pragma GCC visibility pop
799 #endif /* XNU_KERNEL_PRIVATE */
800 #endif /* KERNEL */
801 
802 /* Actions, also rights bits in an ACE */
803 
804 #if defined(KERNEL) || defined (_SYS_ACL_H)
805 #define KAUTH_VNODE_READ_DATA                   (1U<<1)
806 #define KAUTH_VNODE_LIST_DIRECTORY              KAUTH_VNODE_READ_DATA
807 #define KAUTH_VNODE_WRITE_DATA                  (1U<<2)
808 #define KAUTH_VNODE_ADD_FILE                    KAUTH_VNODE_WRITE_DATA
809 #define KAUTH_VNODE_EXECUTE                     (1U<<3)
810 #define KAUTH_VNODE_SEARCH                      KAUTH_VNODE_EXECUTE
811 #define KAUTH_VNODE_DELETE                      (1U<<4)
812 #define KAUTH_VNODE_APPEND_DATA                 (1U<<5)
813 #define KAUTH_VNODE_ADD_SUBDIRECTORY            KAUTH_VNODE_APPEND_DATA
814 #define KAUTH_VNODE_DELETE_CHILD                (1U<<6)
815 #define KAUTH_VNODE_READ_ATTRIBUTES             (1U<<7)
816 #define KAUTH_VNODE_WRITE_ATTRIBUTES            (1U<<8)
817 #define KAUTH_VNODE_READ_EXTATTRIBUTES          (1U<<9)
818 #define KAUTH_VNODE_WRITE_EXTATTRIBUTES         (1U<<10)
819 #define KAUTH_VNODE_READ_SECURITY               (1U<<11)
820 #define KAUTH_VNODE_WRITE_SECURITY              (1U<<12)
821 #define KAUTH_VNODE_TAKE_OWNERSHIP              (1U<<13)
822 
823 /* backwards compatibility only */
824 #define KAUTH_VNODE_CHANGE_OWNER                KAUTH_VNODE_TAKE_OWNERSHIP
825 
826 /* For Windows interoperability only */
827 #define KAUTH_VNODE_SYNCHRONIZE                 (1U<<20)
828 
829 /* (1<<21) - (1<<24) are reserved for generic rights bits */
830 
831 /* Actions not expressed as rights bits */
832 /*
833  * Authorizes the vnode as the target of a hard link.
834  */
835 #define KAUTH_VNODE_LINKTARGET                  (1U<<25)
836 
837 /*
838  * Indicates that other steps have been taken to authorise the action,
839  * but authorisation should be denied for immutable objects.
840  */
841 #define KAUTH_VNODE_CHECKIMMUTABLE              (1U<<26)
842 
843 /* Action modifiers */
844 /*
845  * The KAUTH_VNODE_ACCESS bit is passed to the callback if the authorisation
846  * request in progress is advisory, rather than authoritative.  Listeners
847  * performing consequential work (i.e. not strictly checking authorisation)
848  * may test this flag to avoid performing unnecessary work.
849  *
850  * This bit will never be present in an ACE.
851  */
852 #define KAUTH_VNODE_ACCESS                      (1U<<31)
853 
854 /*
855  * The KAUTH_VNODE_NOIMMUTABLE bit is passed to the callback along with the
856  * KAUTH_VNODE_WRITE_SECURITY bit (and no others) to indicate that the
857  * caller wishes to change one or more of the immutable flags, and the
858  * state of these flags should not be considered when authorizing the request.
859  * The system immutable flags are only ignored when the system securelevel
860  * is low enough to allow their removal.
861  */
862 #define KAUTH_VNODE_NOIMMUTABLE                 (1U<<30)
863 
864 
865 /*
866  * fake right that is composed by the following...
867  * vnode must have search for owner, group and world allowed
868  * plus there must be no deny modes present for SEARCH... this fake
869  * right is used by the fast lookup path to avoid checking
870  * for an exact match on the last credential to lookup
871  * the component being acted on
872  */
873 #define KAUTH_VNODE_SEARCHBYANYONE              (1U<<29)
874 
875 
876 /*
877  * when passed as an 'action' to "vnode_uncache_authorized_actions"
878  * it indicates that all of the cached authorizations for that
879  * vnode should be invalidated
880  */
881 #define KAUTH_INVALIDATE_CACHED_RIGHTS          ((kauth_action_t)~0)
882 
883 
884 
885 /* The expansions of the GENERIC bits at evaluation time */
886 #define KAUTH_VNODE_GENERIC_READ_BITS   (KAUTH_VNODE_READ_DATA |                \
887 	                                KAUTH_VNODE_READ_ATTRIBUTES |           \
888 	                                KAUTH_VNODE_READ_EXTATTRIBUTES |        \
889 	                                KAUTH_VNODE_READ_SECURITY)
890 
891 #define KAUTH_VNODE_GENERIC_WRITE_BITS  (KAUTH_VNODE_WRITE_DATA |               \
892 	                                KAUTH_VNODE_APPEND_DATA |               \
893 	                                KAUTH_VNODE_DELETE |                    \
894 	                                KAUTH_VNODE_DELETE_CHILD |              \
895 	                                KAUTH_VNODE_WRITE_ATTRIBUTES |          \
896 	                                KAUTH_VNODE_WRITE_EXTATTRIBUTES |       \
897 	                                KAUTH_VNODE_WRITE_SECURITY)
898 
899 #define KAUTH_VNODE_GENERIC_EXECUTE_BITS (KAUTH_VNODE_EXECUTE)
900 
901 #define KAUTH_VNODE_GENERIC_ALL_BITS    (KAUTH_VNODE_GENERIC_READ_BITS |        \
902 	                                KAUTH_VNODE_GENERIC_WRITE_BITS |        \
903 	                                KAUTH_VNODE_GENERIC_EXECUTE_BITS)
904 
905 /*
906  * Some sets of bits, defined here for convenience.
907  */
908 #define KAUTH_VNODE_WRITE_RIGHTS        (KAUTH_VNODE_ADD_FILE |                         \
909 	                                KAUTH_VNODE_ADD_SUBDIRECTORY |                  \
910 	                                KAUTH_VNODE_DELETE_CHILD |                      \
911 	                                KAUTH_VNODE_WRITE_DATA |                        \
912 	                                KAUTH_VNODE_APPEND_DATA |                       \
913 	                                KAUTH_VNODE_DELETE |                            \
914 	                                KAUTH_VNODE_WRITE_ATTRIBUTES |                  \
915 	                                KAUTH_VNODE_WRITE_EXTATTRIBUTES |               \
916 	                                KAUTH_VNODE_WRITE_SECURITY |                    \
917 	                                KAUTH_VNODE_TAKE_OWNERSHIP |                    \
918 	                                KAUTH_VNODE_LINKTARGET |                        \
919 	                                KAUTH_VNODE_CHECKIMMUTABLE)
920 
921 
922 #endif /* KERNEL || <sys/acl.h> */
923 
924 #ifdef KERNEL
925 /*
926  * Debugging
927  *
928  * XXX this wouldn't be necessary if we had a *real* debug-logging system.
929  */
930 #if 0
931 # ifndef _FN_KPRINTF
932 #  define       _FN_KPRINTF
933 void kprintf(const char *fmt, ...) __printflike(1, 2);
934 # endif /* !_FN_KPRINTF */
935 # define KAUTH_DEBUG_ENABLE
936 # define K_UUID_FMT "%08x:%08x:%08x:%08x"
937 # define K_UUID_ARG(_u) &_u.g_guid_asint[0],&_u.g_guid_asint[1],&_u.g_guid_asint[2],&_u.g_guid_asint[3]
938 # define KAUTH_DEBUG(fmt, args...)      do { kprintf("%s:%d: " fmt "\n", __PRETTY_FUNCTION__, __LINE__ , ##args); } while (0)
939 # define KAUTH_DEBUG_CTX(_c)            KAUTH_DEBUG("p = %p c = %p", _c->vc_proc, _c->vc_ucred)
940 # define VFS_DEBUG(_ctx, _vp, fmt, args...)                                             \
941 	do {                                                                            \
942 	        kprintf("%p '%s' %s:%d " fmt "\n",                                      \
943 	            _ctx,                                                               \
944 	            (_vp != NULL && _vp->v_name != NULL) ? _vp->v_name : "????",        \
945 	            __PRETTY_FUNCTION__, __LINE__ ,                                     \
946 	            ##args);                                                            \
947 	} while(0)
948 #else   /* !0 */
949 # define KAUTH_DEBUG(fmt, args...)              do { } while (0)
950 # define VFS_DEBUG(ctx, vp, fmt, args...)       do { } while(0)
951 #endif  /* !0 */
952 #endif  /* KERNEL */
953 
954 #endif /* __APPLE_API_EVOLVING */
955 
956 __END_DECLS
957 
958 #endif /* _SYS_KAUTH_H */
959