]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/kauth.h
xnu-4903.270.47.tar.gz
[apple/xnu.git] / bsd / sys / kauth.h
CommitLineData
91447636 1/*
6d2010ae 2 * Copyright (c) 2004-2010 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55
A
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.
91447636
A
33 */
34
35#ifndef _SYS_KAUTH_H
36#define _SYS_KAUTH_H
37
38#include <sys/appleapiopts.h>
39#include <sys/cdefs.h>
2d21ac55 40#include <mach/boolean.h>
0a7de745
A
41#include <sys/_types.h> /* __offsetof() */
42#include <sys/syslimits.h> /* NGROUPS_MAX */
91447636
A
43
44#ifdef __APPLE_API_EVOLVING
45
46/*
47 * Identities.
48 */
49
0a7de745
A
50#define KAUTH_UID_NONE (~(uid_t)0 - 100) /* not a valid UID */
51#define KAUTH_GID_NONE (~(gid_t)0 - 100) /* not a valid GID */
91447636 52
39236c6e 53#include <sys/_types/_guid_t.h>
91447636
A
54
55/* NT Security Identifier, structure as defined by Microsoft */
56#pragma pack(1) /* push packing of 1 byte */
57typedef struct {
0a7de745
A
58 u_int8_t sid_kind;
59 u_int8_t sid_authcount;
60 u_int8_t sid_authority[6];
91447636 61#define KAUTH_NTSID_MAX_AUTHORITIES 16
0a7de745 62 u_int32_t sid_authorities[KAUTH_NTSID_MAX_AUTHORITIES];
91447636
A
63} ntsid_t;
64#pragma pack() /* pop packing to previous packing level */
65#define _NTSID_T
66
67/* valid byte count inside a SID structure */
0a7de745
A
68#define KAUTH_NTSID_HDRSIZE (8)
69#define KAUTH_NTSID_SIZE(_s) (KAUTH_NTSID_HDRSIZE + ((_s)->sid_authcount * sizeof(u_int32_t)))
91447636
A
70
71/*
2d21ac55
A
72 * External lookup message payload; this structure is shared between the
73 * kernel group membership resolver, and the user space group membership
74 * resolver daemon, and is use to communicate resolution requests from the
75 * kernel to user space, and the result of that request from user space to
76 * the kernel.
91447636
A
77 */
78struct kauth_identity_extlookup {
0a7de745
A
79 u_int32_t el_seqno; /* request sequence number */
80 u_int32_t el_result; /* lookup result */
81#define KAUTH_EXTLOOKUP_SUCCESS 0 /* results here are good */
82#define KAUTH_EXTLOOKUP_BADRQ 1 /* request badly formatted */
83#define KAUTH_EXTLOOKUP_FAILURE 2 /* transient failure during lookup */
84#define KAUTH_EXTLOOKUP_FATAL 3 /* permanent failure during lookup */
85#define KAUTH_EXTLOOKUP_INPROG 100 /* request in progress */
86 u_int32_t el_flags;
87#define KAUTH_EXTLOOKUP_VALID_UID (1<<0)
88#define KAUTH_EXTLOOKUP_VALID_UGUID (1<<1)
89#define KAUTH_EXTLOOKUP_VALID_USID (1<<2)
90#define KAUTH_EXTLOOKUP_VALID_GID (1<<3)
91#define KAUTH_EXTLOOKUP_VALID_GGUID (1<<4)
92#define KAUTH_EXTLOOKUP_VALID_GSID (1<<5)
93#define KAUTH_EXTLOOKUP_WANT_UID (1<<6)
94#define KAUTH_EXTLOOKUP_WANT_UGUID (1<<7)
95#define KAUTH_EXTLOOKUP_WANT_USID (1<<8)
96#define KAUTH_EXTLOOKUP_WANT_GID (1<<9)
97#define KAUTH_EXTLOOKUP_WANT_GGUID (1<<10)
98#define KAUTH_EXTLOOKUP_WANT_GSID (1<<11)
99#define KAUTH_EXTLOOKUP_WANT_MEMBERSHIP (1<<12)
91447636 100#define KAUTH_EXTLOOKUP_VALID_MEMBERSHIP (1<<13)
0a7de745
A
101#define KAUTH_EXTLOOKUP_ISMEMBER (1<<14)
102#define KAUTH_EXTLOOKUP_VALID_PWNAM (1<<15)
103#define KAUTH_EXTLOOKUP_WANT_PWNAM (1<<16)
104#define KAUTH_EXTLOOKUP_VALID_GRNAM (1<<17)
105#define KAUTH_EXTLOOKUP_WANT_GRNAM (1<<18)
106#define KAUTH_EXTLOOKUP_VALID_SUPGRPS (1<<19)
107#define KAUTH_EXTLOOKUP_WANT_SUPGRPS (1<<20)
108
109 __darwin_pid_t el_info_pid; /* request on behalf of PID */
110 u_int64_t el_extend; /* extension field */
111 u_int32_t el_info_reserved_1; /* reserved (APPLE) */
112
113 uid_t el_uid; /* user ID */
114 guid_t el_uguid; /* user GUID */
115 u_int32_t el_uguid_valid; /* TTL on translation result (seconds) */
116 ntsid_t el_usid; /* user NT SID */
117 u_int32_t el_usid_valid; /* TTL on translation result (seconds) */
118 gid_t el_gid; /* group ID */
119 guid_t el_gguid; /* group GUID */
120 u_int32_t el_gguid_valid; /* TTL on translation result (seconds) */
121 ntsid_t el_gsid; /* group SID */
122 u_int32_t el_gsid_valid; /* TTL on translation result (seconds) */
123 u_int32_t el_member_valid; /* TTL on group lookup result */
124 u_int32_t el_sup_grp_cnt; /* count of supplemental groups up to NGROUPS */
125 gid_t el_sup_groups[NGROUPS_MAX]; /* supplemental group list */
91447636
A
126};
127
316670eb
A
128struct kauth_cache_sizes {
129 u_int32_t kcs_group_size;
130 u_int32_t kcs_id_size;
131};
132
0a7de745
A
133#define KAUTH_EXTLOOKUP_REGISTER (0)
134#define KAUTH_EXTLOOKUP_RESULT (1<<0)
135#define KAUTH_EXTLOOKUP_WORKER (1<<1)
136#define KAUTH_EXTLOOKUP_DEREGISTER (1<<2)
137#define KAUTH_GET_CACHE_SIZES (1<<3)
138#define KAUTH_SET_CACHE_SIZES (1<<4)
139#define KAUTH_CLEAR_CACHES (1<<5)
91447636 140
0a7de745 141#define IDENTITYSVC_ENTITLEMENT "com.apple.private.identitysvc"
5ba3f43e 142
91447636
A
143
144#ifdef KERNEL
145/*
146 * Credentials.
147 */
148
149#if 0
150/*
151 * Supplemental credential data.
152 *
153 * This interface allows us to associate arbitrary data with a credential.
154 * As with the credential, the data is considered immutable.
155 */
156struct kauth_cred_supplement {
157 TAILQ_ENTRY(kauth_cred_supplement) kcs_link;
158
0a7de745
A
159 int kcs_ref; /* reference count */
160 int kcs_id; /* vended identifier */
161 size_t kcs_size; /* size of data field */
162 char kcs_data[0];
91447636
A
163};
164
165typedef struct kauth_cred_supplement *kauth_cred_supplement_t;
166
167struct kauth_cred {
0a7de745
A
168 TAILQ_ENTRY(kauth_cred) kc_link;
169
170 int kc_ref; /* reference count */
171 uid_t kc_uid; /* effective user id */
172 uid_t kc_ruid; /* real user id */
173 uid_t kc_svuid; /* saved user id */
174 gid_t kc_gid; /* effective group id */
175 gid_t kc_rgid; /* real group id */
176 gid_t kc_svgid; /* saved group id */
177
178 int kc_flags;
179#define KAUTH_CRED_GRPOVERRIDE (1<<0) /* private group list is authoritative */
180
181 int kc_npvtgroups; /* private group list, advisory or authoritative */
182 gid_t kc_pvtgroups[NGROUPS]; /* based on KAUTH_CRED_GRPOVERRIDE flag */
183
184 int kc_nsuppgroups; /* supplementary group list */
185 gid_t *kc_suppgroups;
186
187 int kc_nwhtgroups; /* whiteout group list */
188 gid_t *kc_whtgroups;
189
190 struct au_session cr_audit; /* user auditing data */
191
192 int kc_nsupplement; /* entry count in supplemental data pointer array */
91447636
A
193 kauth_cred_supplement_t *kc_supplement;
194};
195#else
196
197/* XXX just for now */
198#include <sys/ucred.h>
199// typedef struct ucred *kauth_cred_t;
200#endif
201
202/* Kernel SPI for now */
203__BEGIN_DECLS
6d2010ae
A
204/*
205 * Routines specific to credentials with POSIX credential labels attached
206 *
207 * XXX Should be in policy_posix.h, with struct posix_cred
208 */
209extern kauth_cred_t posix_cred_create(posix_cred_t pcred);
210extern posix_cred_t posix_cred_get(kauth_cred_t cred);
211extern void posix_cred_label(kauth_cred_t cred, posix_cred_t pcred);
212extern int posix_cred_access(kauth_cred_t cred, id_t object_uid, id_t object_gid, mode_t object_mode, mode_t mode_req);
213
0a7de745
A
214extern uid_t kauth_getuid(void);
215extern uid_t kauth_getruid(void);
216extern gid_t kauth_getgid(void);
91447636
A
217extern kauth_cred_t kauth_cred_get(void);
218extern kauth_cred_t kauth_cred_get_with_ref(void);
219extern kauth_cred_t kauth_cred_proc_ref(proc_t procp);
91447636 220extern kauth_cred_t kauth_cred_create(kauth_cred_t cred);
0a7de745 221extern void kauth_cred_ref(kauth_cred_t _cred);
b0d623f7 222#ifndef __LP64__
2d21ac55 223/* Use kauth_cred_unref(), not kauth_cred_rele() */
0a7de745 224extern void kauth_cred_rele(kauth_cred_t _cred) __deprecated;
b0d623f7 225#endif
0a7de745 226extern void kauth_cred_unref(kauth_cred_t *_cred);
b0d623f7 227
2d21ac55
A
228#if CONFIG_MACF
229struct label;
0a7de745 230extern kauth_cred_t kauth_cred_label_update(kauth_cred_t cred, struct label *label);
2d21ac55 231extern int kauth_proc_label_update(struct proc *p, struct label *label);
2d21ac55 232#else
0a7de745 233/* this is a temp hack to cover us when MAC is not built in a kernel configuration.
2d21ac55 234 * Since we cannot build our export list based on the kernel configuration we need
0a7de745 235 * to define a stub.
2d21ac55 236 */
0a7de745 237extern kauth_cred_t kauth_cred_label_update(kauth_cred_t cred, void *label);
2d21ac55
A
238extern int kauth_proc_label_update(struct proc *p, void *label);
239#endif
240
0a7de745 241__deprecated_msg("Unsafe interface: requires lock holds that aren't exposed")
91447636 242extern kauth_cred_t kauth_cred_find(kauth_cred_t cred);
0a7de745
A
243extern uid_t kauth_cred_getuid(kauth_cred_t _cred);
244extern uid_t kauth_cred_getruid(kauth_cred_t _cred);
245extern uid_t kauth_cred_getsvuid(kauth_cred_t _cred);
246extern gid_t kauth_cred_getgid(kauth_cred_t _cred);
247extern gid_t kauth_cred_getrgid(kauth_cred_t _cred);
248extern gid_t kauth_cred_getsvgid(kauth_cred_t _cred);
249extern int kauth_cred_pwnam2guid(char *pwnam, guid_t *guidp);
250extern int kauth_cred_grnam2guid(char *grnam, guid_t *guidp);
251extern int kauth_cred_guid2pwnam(guid_t *guidp, char *pwnam);
252extern int kauth_cred_guid2grnam(guid_t *guidp, char *grnam);
91447636
A
253extern int kauth_cred_guid2uid(guid_t *_guid, uid_t *_uidp);
254extern int kauth_cred_guid2gid(guid_t *_guid, gid_t *_gidp);
255extern int kauth_cred_ntsid2uid(ntsid_t *_sid, uid_t *_uidp);
256extern int kauth_cred_ntsid2gid(ntsid_t *_sid, gid_t *_gidp);
257extern int kauth_cred_ntsid2guid(ntsid_t *_sid, guid_t *_guidp);
258extern int kauth_cred_uid2guid(uid_t _uid, guid_t *_guidp);
0a7de745 259extern int kauth_cred_getguid(kauth_cred_t _cred, guid_t *_guidp);
91447636
A
260extern int kauth_cred_gid2guid(gid_t _gid, guid_t *_guidp);
261extern int kauth_cred_uid2ntsid(uid_t _uid, ntsid_t *_sidp);
0a7de745 262extern int kauth_cred_getntsid(kauth_cred_t _cred, ntsid_t *_sidp);
91447636
A
263extern int kauth_cred_gid2ntsid(gid_t _gid, ntsid_t *_sidp);
264extern int kauth_cred_guid2ntsid(guid_t *_guid, ntsid_t *_sidp);
0a7de745
A
265extern int kauth_cred_ismember_gid(kauth_cred_t _cred, gid_t _gid, int *_resultp);
266extern int kauth_cred_ismember_guid(kauth_cred_t _cred, guid_t *_guidp, int *_resultp);
267extern int kauth_cred_nfs4domain2dsnode(char *nfs4domain, char *dsnode);
268extern int kauth_cred_dsnode2nfs4domain(char *dsnode, char *nfs4domain);
2d21ac55 269
0a7de745 270extern int groupmember(gid_t gid, kauth_cred_t cred);
91447636 271
2d21ac55 272/* currently only exported in unsupported for use by seatbelt */
0a7de745 273extern int kauth_cred_issuser(kauth_cred_t _cred);
91447636
A
274
275
276/* GUID, NTSID helpers */
0a7de745
A
277extern guid_t kauth_null_guid;
278extern int kauth_guid_equal(guid_t *_guid1, guid_t *_guid2);
b0d623f7 279#ifdef XNU_KERNEL_PRIVATE
0a7de745 280extern int kauth_ntsid_equal(ntsid_t *_sid1, ntsid_t *_sid2);
91447636 281
0a7de745
A
282extern int kauth_wellknown_guid(guid_t *_guid);
283#define KAUTH_WKG_NOT 0 /* not a well-known GUID */
284#define KAUTH_WKG_OWNER 1
285#define KAUTH_WKG_GROUP 2
286#define KAUTH_WKG_NOBODY 3
287#define KAUTH_WKG_EVERYBODY 4
288
289extern gid_t kauth_getrgid(void);
290extern int cantrace(proc_t cur_procp, kauth_cred_t creds, proc_t traced_procp, int *errp);
b0d623f7 291extern kauth_cred_t kauth_cred_copy_real(kauth_cred_t cred);
0a7de745
A
292extern kauth_cred_t kauth_cred_setresuid(kauth_cred_t cred, uid_t ruid, uid_t euid, uid_t svuid, uid_t gmuid);
293extern kauth_cred_t kauth_cred_setresgid(kauth_cred_t cred, gid_t rgid, gid_t egid, gid_t svgid);
b0d623f7
A
294extern kauth_cred_t kauth_cred_setuidgid(kauth_cred_t cred, uid_t uid, gid_t gid);
295extern kauth_cred_t kauth_cred_setsvuidgid(kauth_cred_t cred, uid_t uid, gid_t gid);
0a7de745 296extern kauth_cred_t kauth_cred_setgroups(kauth_cred_t cred, gid_t *groups, int groupcount, uid_t gmuid);
b0d623f7 297struct uthread;
0a7de745 298extern void kauth_cred_uthread_update(struct uthread *, proc_t);
b0d623f7 299#ifdef CONFIG_MACF
fe8ab488 300extern 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);
b0d623f7 301#endif
0a7de745
A
302extern int kauth_cred_getgroups(kauth_cred_t _cred, gid_t *_groups, int *_groupcount);
303extern int kauth_cred_gid_subset(kauth_cred_t _cred1, kauth_cred_t _cred2, int *_resultp);
b0d623f7
A
304struct auditinfo_addr;
305extern kauth_cred_t kauth_cred_setauditinfo(kauth_cred_t, au_session_t *);
0a7de745
A
306extern int kauth_cred_supplementary_register(const char *name, int *ident);
307extern int kauth_cred_supplementary_add(kauth_cred_t cred, int ident, const void *data, size_t datasize);
308extern int kauth_cred_supplementary_remove(kauth_cred_t cred, int ident);
91447636 309
b0d623f7 310#endif /* XNU_KERNEL_PRIVATE */
91447636
A
311__END_DECLS
312
313#endif /* KERNEL */
314
315/*
316 * Generic Access Control Lists.
317 */
318#if defined(KERNEL) || defined (_SYS_ACL_H)
319
320typedef u_int32_t kauth_ace_rights_t;
321
322/* Access Control List Entry (ACE) */
323struct kauth_ace {
0a7de745
A
324 guid_t ace_applicable;
325 u_int32_t ace_flags;
326#define KAUTH_ACE_KINDMASK 0xf
327#define KAUTH_ACE_PERMIT 1
328#define KAUTH_ACE_DENY 2
329#define KAUTH_ACE_AUDIT 3 /* not implemented */
330#define KAUTH_ACE_ALARM 4 /* not implemented */
331#define KAUTH_ACE_INHERITED (1<<4)
332#define KAUTH_ACE_FILE_INHERIT (1<<5)
333#define KAUTH_ACE_DIRECTORY_INHERIT (1<<6)
334#define KAUTH_ACE_LIMIT_INHERIT (1<<7)
335#define KAUTH_ACE_ONLY_INHERIT (1<<8)
336#define KAUTH_ACE_SUCCESS (1<<9) /* not implemented (AUDIT/ALARM) */
337#define KAUTH_ACE_FAILURE (1<<10) /* not implemented (AUDIT/ALARM) */
2d21ac55 338/* All flag bits controlling ACE inheritance */
0a7de745
A
339#define KAUTH_ACE_INHERIT_CONTROL_FLAGS \
340 (KAUTH_ACE_FILE_INHERIT | \
341 KAUTH_ACE_DIRECTORY_INHERIT | \
342 KAUTH_ACE_LIMIT_INHERIT | \
343 KAUTH_ACE_ONLY_INHERIT)
344 kauth_ace_rights_t ace_rights; /* scope specific */
91447636 345 /* These rights are never tested, but may be present in an ACL */
0a7de745
A
346#define KAUTH_ACE_GENERIC_ALL (1<<21)
347#define KAUTH_ACE_GENERIC_EXECUTE (1<<22)
348#define KAUTH_ACE_GENERIC_WRITE (1<<23)
349#define KAUTH_ACE_GENERIC_READ (1<<24)
91447636
A
350};
351
352#ifndef _KAUTH_ACE
353#define _KAUTH_ACE
354typedef struct kauth_ace *kauth_ace_t;
355#endif
356
357
358/* Access Control List */
359struct kauth_acl {
0a7de745
A
360 u_int32_t acl_entrycount;
361 u_int32_t acl_flags;
362
b0d623f7 363 struct kauth_ace acl_ace[1];
91447636
A
364};
365
366/*
367 * XXX this value needs to be raised - 3893388
368 */
0a7de745 369#define KAUTH_ACL_MAX_ENTRIES 128
91447636
A
370
371/*
372 * The low 16 bits of the flags field are reserved for filesystem
373 * internal use and must be preserved by all APIs. This includes
374 * round-tripping flags through user-space interfaces.
375 */
0a7de745 376#define KAUTH_ACL_FLAGS_PRIVATE (0xffff)
91447636
A
377
378/*
379 * The high 16 bits of the flags are used to store attributes and
380 * to request specific handling of the ACL.
381 */
382
383/* inheritance will be deferred until the first rename operation */
0a7de745 384#define KAUTH_ACL_DEFER_INHERIT (1<<16)
91447636 385/* this ACL must not be overwritten as part of an inheritance operation */
0a7de745 386#define KAUTH_ACL_NO_INHERIT (1<<17)
91447636 387
0c530ab8
A
388/* acl_entrycount that tells us the ACL is not valid */
389#define KAUTH_FILESEC_NOACL ((u_int32_t)(-1))
390
391/*
392 * If the acl_entrycount field is KAUTH_FILESEC_NOACL, then the size is the
393 * same as a kauth_acl structure; the intent is to put an actual entrycount of
394 * KAUTH_FILESEC_NOACL on disk to distinguish a kauth_filesec_t with an empty
395 * entry (Windows treats this as "deny all") from one that merely indicates a
396 * file group and/or owner guid values.
397 */
0a7de745
A
398#define KAUTH_ACL_SIZE(c) (__offsetof(struct kauth_acl, acl_ace) + ((u_int32_t)(c) != KAUTH_FILESEC_NOACL ? ((c) * sizeof(struct kauth_ace)) : 0))
399#define KAUTH_ACL_COPYSIZE(p) KAUTH_ACL_SIZE((p)->acl_entrycount)
91447636
A
400
401
402#ifndef _KAUTH_ACL
403#define _KAUTH_ACL
404typedef struct kauth_acl *kauth_acl_t;
405#endif
406
407#ifdef KERNEL
408__BEGIN_DECLS
0a7de745
A
409kauth_acl_t kauth_acl_alloc(int size);
410void kauth_acl_free(kauth_acl_t fsp);
91447636
A
411__END_DECLS
412#endif
413
414
415/*
416 * Extended File Security.
417 */
418
419/* File Security information */
420struct kauth_filesec {
0a7de745
A
421 u_int32_t fsec_magic;
422#define KAUTH_FILESEC_MAGIC 0x012cc16d
423 guid_t fsec_owner;
424 guid_t fsec_group;
91447636
A
425
426 struct kauth_acl fsec_acl;
91447636
A
427};
428
429/* backwards compatibility */
430#define fsec_entrycount fsec_acl.acl_entrycount
0a7de745
A
431#define fsec_flags fsec_acl.acl_flags
432#define fsec_ace fsec_acl.acl_ace
433#define KAUTH_FILESEC_FLAGS_PRIVATE KAUTH_ACL_FLAGS_PRIVATE
434#define KAUTH_FILESEC_DEFER_INHERIT KAUTH_ACL_DEFER_INHERIT
435#define KAUTH_FILESEC_NO_INHERIT KAUTH_ACL_NO_INHERIT
436#define KAUTH_FILESEC_NONE ((kauth_filesec_t)0)
437#define KAUTH_FILESEC_WANTED ((kauth_filesec_t)1)
438
91447636
A
439#ifndef _KAUTH_FILESEC
440#define _KAUTH_FILESEC
441typedef struct kauth_filesec *kauth_filesec_t;
442#endif
443
0a7de745
A
444#define KAUTH_FILESEC_SIZE(c) (__offsetof(struct kauth_filesec, fsec_acl) + __offsetof(struct kauth_acl, acl_ace) + (c) * sizeof(struct kauth_ace))
445#define KAUTH_FILESEC_COPYSIZE(p) KAUTH_FILESEC_SIZE(((p)->fsec_entrycount == KAUTH_FILESEC_NOACL) ? 0 : (p)->fsec_entrycount)
446#define KAUTH_FILESEC_COUNT(s) (((s) - KAUTH_FILESEC_SIZE(0)) / sizeof(struct kauth_ace))
447#define KAUTH_FILESEC_VALID(s) ((s) >= KAUTH_FILESEC_SIZE(0) && (((s) - KAUTH_FILESEC_SIZE(0)) % sizeof(struct kauth_ace)) == 0)
91447636 448
0a7de745 449#define KAUTH_FILESEC_XATTR "com.apple.system.Security"
91447636 450
0c530ab8 451/* Allowable first arguments to kauth_filesec_acl_setendian() */
0a7de745
A
452#define KAUTH_ENDIAN_HOST 0x00000001 /* set host endianness */
453#define KAUTH_ENDIAN_DISK 0x00000002 /* set disk endianness */
0c530ab8 454
2d21ac55
A
455#endif /* KERNEL || <sys/acl.h> */
456
457
458#ifdef KERNEL
459
91447636 460
91447636
A
461/*
462 * Scope management.
463 */
464struct kauth_scope;
465typedef struct kauth_scope *kauth_scope_t;
466struct kauth_listener;
467typedef struct kauth_listener *kauth_listener_t;
468#ifndef _KAUTH_ACTION_T
469typedef int kauth_action_t;
470# define _KAUTH_ACTION_T
471#endif
472
473typedef int (* kauth_scope_callback_t)(kauth_cred_t _credential,
0a7de745
A
474 void *_idata,
475 kauth_action_t _action,
476 uintptr_t _arg0,
477 uintptr_t _arg1,
478 uintptr_t _arg2,
479 uintptr_t _arg3);
91447636 480
0a7de745
A
481#define KAUTH_RESULT_ALLOW (1)
482#define KAUTH_RESULT_DENY (2)
483#define KAUTH_RESULT_DEFER (3)
91447636
A
484
485struct kauth_acl_eval {
0a7de745
A
486 kauth_ace_t ae_acl;
487 int ae_count;
488 kauth_ace_rights_t ae_requested;
489 kauth_ace_rights_t ae_residual;
490 int ae_result;
491 boolean_t ae_found_deny;
492 int ae_options;
493#define KAUTH_AEVAL_IS_OWNER (1<<0) /* authorizing operation for owner */
494#define KAUTH_AEVAL_IN_GROUP (1<<1) /* authorizing operation for groupmember */
495#define KAUTH_AEVAL_IN_GROUP_UNKNOWN (1<<2) /* authorizing operation for unknown group membership */
91447636 496 /* expansions for 'generic' rights bits */
0a7de745
A
497 kauth_ace_rights_t ae_exp_gall;
498 kauth_ace_rights_t ae_exp_gread;
499 kauth_ace_rights_t ae_exp_gwrite;
500 kauth_ace_rights_t ae_exp_gexec;
91447636
A
501};
502
503typedef struct kauth_acl_eval *kauth_acl_eval_t;
0a7de745 504
91447636 505__BEGIN_DECLS
0a7de745
A
506kauth_filesec_t kauth_filesec_alloc(int size);
507void kauth_filesec_free(kauth_filesec_t fsp);
91447636 508extern kauth_scope_t kauth_register_scope(const char *_identifier, kauth_scope_callback_t _callback, void *_idata);
0a7de745 509extern void kauth_deregister_scope(kauth_scope_t _scope);
91447636 510extern kauth_listener_t kauth_listen_scope(const char *_identifier, kauth_scope_callback_t _callback, void *_idata);
0a7de745
A
511extern void kauth_unlisten_scope(kauth_listener_t _scope);
512extern int kauth_authorize_action(kauth_scope_t _scope, kauth_cred_t _credential, kauth_action_t _action,
513 uintptr_t _arg0, uintptr_t _arg1, uintptr_t _arg2, uintptr_t _arg3);
91447636
A
514
515/* default scope handlers */
0a7de745 516extern int kauth_authorize_allow(kauth_cred_t _credential, void *_idata, kauth_action_t _action,
91447636 517 uintptr_t _arg0, uintptr_t _arg1, uintptr_t _arg2, uintptr_t _arg3);
0a7de745 518
b0d623f7
A
519
520#ifdef XNU_KERNEL_PRIVATE
0a7de745
A
521void kauth_filesec_acl_setendian(int, kauth_filesec_t, kauth_acl_t);
522int kauth_copyinfilesec(user_addr_t xsecurity, kauth_filesec_t *xsecdestpp);
523extern int kauth_acl_evaluate(kauth_cred_t _credential, kauth_acl_eval_t _eval);
524extern int kauth_acl_inherit(vnode_t _dvp, kauth_acl_t _initial, kauth_acl_t *_product, int _isdir, vfs_context_t _ctx);
b0d623f7
A
525
526#endif /* XNU_KERNEL_PRIVATE */
527
528
91447636
A
529__END_DECLS
530
531/*
532 * Generic scope.
533 */
0a7de745 534#define KAUTH_SCOPE_GENERIC "com.apple.kauth.generic"
91447636
A
535
536/* Actions */
0a7de745 537#define KAUTH_GENERIC_ISSUSER 1
91447636 538
b0d623f7 539#ifdef XNU_KERNEL_PRIVATE
91447636 540__BEGIN_DECLS
0a7de745 541extern int kauth_authorize_generic(kauth_cred_t credential, kauth_action_t action);
91447636 542__END_DECLS
b0d623f7 543#endif /* XNU_KERNEL_PRIVATE */
91447636
A
544
545/*
546 * Process/task scope.
547 */
0a7de745 548#define KAUTH_SCOPE_PROCESS "com.apple.kauth.process"
91447636
A
549
550/* Actions */
0a7de745
A
551#define KAUTH_PROCESS_CANSIGNAL 1
552#define KAUTH_PROCESS_CANTRACE 2
91447636
A
553
554__BEGIN_DECLS
0a7de745 555extern int kauth_authorize_process(kauth_cred_t _credential, kauth_action_t _action,
91447636
A
556 struct proc *_process, uintptr_t _arg1, uintptr_t _arg2, uintptr_t _arg3);
557__END_DECLS
558
559/*
560 * Vnode operation scope.
561 *
562 * Prototype for vnode_authorize is in vnode.h
563 */
0a7de745 564#define KAUTH_SCOPE_VNODE "com.apple.kauth.vnode"
91447636
A
565
566/*
567 * File system operation scope.
568 *
569 */
0a7de745 570#define KAUTH_SCOPE_FILEOP "com.apple.kauth.fileop"
91447636
A
571
572/* Actions */
0a7de745
A
573#define KAUTH_FILEOP_OPEN 1
574#define KAUTH_FILEOP_CLOSE 2
575#define KAUTH_FILEOP_RENAME 3
576#define KAUTH_FILEOP_EXCHANGE 4
577#define KAUTH_FILEOP_LINK 5
578#define KAUTH_FILEOP_EXEC 6
579#define KAUTH_FILEOP_DELETE 7
580#define KAUTH_FILEOP_WILL_RENAME 8
91447636
A
581
582/*
583 * arguments passed to KAUTH_FILEOP_OPEN listeners
584 * arg0 is pointer to vnode (vnode *) for given user path.
585 * arg1 is pointer to path (char *) passed in to open.
586 * arguments passed to KAUTH_FILEOP_CLOSE listeners
587 * arg0 is pointer to vnode (vnode *) for file to be closed.
588 * arg1 is pointer to path (char *) of file to be closed.
589 * arg2 is close flags.
d9a64523
A
590 * arguments passed to KAUTH_FILEOP_WILL_RENAME listeners
591 * arg0 is pointer to vnode (vnode *) of the file being renamed
592 * arg1 is pointer to the "from" path (char *)
593 * arg2 is pointer to the "to" path (char *)
91447636
A
594 * arguments passed to KAUTH_FILEOP_RENAME listeners
595 * arg0 is pointer to "from" path (char *).
596 * arg1 is pointer to "to" path (char *).
597 * arguments passed to KAUTH_FILEOP_EXCHANGE listeners
598 * arg0 is pointer to file 1 path (char *).
599 * arg1 is pointer to file 2 path (char *).
600 * arguments passed to KAUTH_FILEOP_LINK listeners
601 * arg0 is pointer to path to file we are linking to (char *).
602 * arg1 is pointer to path to the new link file (char *).
603 * arguments passed to KAUTH_FILEOP_EXEC listeners
604 * arg0 is pointer to vnode (vnode *) for executable.
605 * arg1 is pointer to path (char *) to executable.
2d21ac55
A
606 * arguments passed to KAUTH_FILEOP_DELETE listeners
607 * arg0 is pointer to vnode (vnode *) of file/dir that was deleted.
608 * arg1 is pointer to path (char *) of file/dir that was deleted.
91447636 609 */
0a7de745 610
91447636 611/* Flag values returned to close listeners. */
0a7de745 612#define KAUTH_FILEOP_CLOSE_MODIFIED (1<<1)
91447636
A
613
614__BEGIN_DECLS
b0d623f7 615#ifdef XNU_KERNEL_PRIVATE
0a7de745 616extern int kauth_authorize_fileop_has_listeners(void);
b0d623f7 617#endif /* XNU_KERNEL_PRIVATE */
0a7de745 618extern int kauth_authorize_fileop(kauth_cred_t _credential, kauth_action_t _action,
91447636
A
619 uintptr_t _arg0, uintptr_t _arg1);
620__END_DECLS
621
622#endif /* KERNEL */
623
624/* Actions, also rights bits in an ACE */
625
626#if defined(KERNEL) || defined (_SYS_ACL_H)
0a7de745
A
627#define KAUTH_VNODE_READ_DATA (1<<1)
628#define KAUTH_VNODE_LIST_DIRECTORY KAUTH_VNODE_READ_DATA
629#define KAUTH_VNODE_WRITE_DATA (1<<2)
630#define KAUTH_VNODE_ADD_FILE KAUTH_VNODE_WRITE_DATA
631#define KAUTH_VNODE_EXECUTE (1<<3)
632#define KAUTH_VNODE_SEARCH KAUTH_VNODE_EXECUTE
633#define KAUTH_VNODE_DELETE (1<<4)
634#define KAUTH_VNODE_APPEND_DATA (1<<5)
635#define KAUTH_VNODE_ADD_SUBDIRECTORY KAUTH_VNODE_APPEND_DATA
636#define KAUTH_VNODE_DELETE_CHILD (1<<6)
637#define KAUTH_VNODE_READ_ATTRIBUTES (1<<7)
638#define KAUTH_VNODE_WRITE_ATTRIBUTES (1<<8)
639#define KAUTH_VNODE_READ_EXTATTRIBUTES (1<<9)
640#define KAUTH_VNODE_WRITE_EXTATTRIBUTES (1<<10)
641#define KAUTH_VNODE_READ_SECURITY (1<<11)
642#define KAUTH_VNODE_WRITE_SECURITY (1<<12)
643#define KAUTH_VNODE_TAKE_OWNERSHIP (1<<13)
91447636
A
644
645/* backwards compatibility only */
0a7de745 646#define KAUTH_VNODE_CHANGE_OWNER KAUTH_VNODE_TAKE_OWNERSHIP
91447636
A
647
648/* For Windows interoperability only */
0a7de745 649#define KAUTH_VNODE_SYNCHRONIZE (1<<20)
91447636
A
650
651/* (1<<21) - (1<<24) are reserved for generic rights bits */
652
653/* Actions not expressed as rights bits */
654/*
655 * Authorizes the vnode as the target of a hard link.
656 */
0a7de745 657#define KAUTH_VNODE_LINKTARGET (1<<25)
91447636
A
658
659/*
660 * Indicates that other steps have been taken to authorise the action,
661 * but authorisation should be denied for immutable objects.
662 */
0a7de745 663#define KAUTH_VNODE_CHECKIMMUTABLE (1<<26)
91447636
A
664
665/* Action modifiers */
666/*
667 * The KAUTH_VNODE_ACCESS bit is passed to the callback if the authorisation
668 * request in progress is advisory, rather than authoritative. Listeners
669 * performing consequential work (i.e. not strictly checking authorisation)
670 * may test this flag to avoid performing unnecessary work.
671 *
672 * This bit will never be present in an ACE.
673 */
0a7de745 674#define KAUTH_VNODE_ACCESS (1<<31)
91447636
A
675
676/*
677 * The KAUTH_VNODE_NOIMMUTABLE bit is passed to the callback along with the
678 * KAUTH_VNODE_WRITE_SECURITY bit (and no others) to indicate that the
679 * caller wishes to change one or more of the immutable flags, and the
680 * state of these flags should not be considered when authorizing the request.
681 * The system immutable flags are only ignored when the system securelevel
682 * is low enough to allow their removal.
683 */
0a7de745 684#define KAUTH_VNODE_NOIMMUTABLE (1<<30)
91447636 685
2d21ac55
A
686
687/*
688 * fake right that is composed by the following...
689 * vnode must have search for owner, group and world allowed
690 * plus there must be no deny modes present for SEARCH... this fake
691 * right is used by the fast lookup path to avoid checking
692 * for an exact match on the last credential to lookup
693 * the component being acted on
694 */
0a7de745 695#define KAUTH_VNODE_SEARCHBYANYONE (1<<29)
2d21ac55
A
696
697
698/*
699 * when passed as an 'action' to "vnode_uncache_authorized_actions"
700 * it indicates that all of the cached authorizations for that
0a7de745 701 * vnode should be invalidated
2d21ac55 702 */
0a7de745 703#define KAUTH_INVALIDATE_CACHED_RIGHTS ((kauth_action_t)~0)
2d21ac55
A
704
705
706
91447636 707/* The expansions of the GENERIC bits at evaluation time */
0a7de745
A
708#define KAUTH_VNODE_GENERIC_READ_BITS (KAUTH_VNODE_READ_DATA | \
709 KAUTH_VNODE_READ_ATTRIBUTES | \
710 KAUTH_VNODE_READ_EXTATTRIBUTES | \
711 KAUTH_VNODE_READ_SECURITY)
712
713#define KAUTH_VNODE_GENERIC_WRITE_BITS (KAUTH_VNODE_WRITE_DATA | \
714 KAUTH_VNODE_APPEND_DATA | \
715 KAUTH_VNODE_DELETE | \
716 KAUTH_VNODE_DELETE_CHILD | \
717 KAUTH_VNODE_WRITE_ATTRIBUTES | \
718 KAUTH_VNODE_WRITE_EXTATTRIBUTES | \
719 KAUTH_VNODE_WRITE_SECURITY)
720
91447636 721#define KAUTH_VNODE_GENERIC_EXECUTE_BITS (KAUTH_VNODE_EXECUTE)
0a7de745
A
722
723#define KAUTH_VNODE_GENERIC_ALL_BITS (KAUTH_VNODE_GENERIC_READ_BITS | \
724 KAUTH_VNODE_GENERIC_WRITE_BITS | \
725 KAUTH_VNODE_GENERIC_EXECUTE_BITS)
726
91447636
A
727/*
728 * Some sets of bits, defined here for convenience.
729 */
0a7de745
A
730#define KAUTH_VNODE_WRITE_RIGHTS (KAUTH_VNODE_ADD_FILE | \
731 KAUTH_VNODE_ADD_SUBDIRECTORY | \
732 KAUTH_VNODE_DELETE_CHILD | \
733 KAUTH_VNODE_WRITE_DATA | \
734 KAUTH_VNODE_APPEND_DATA | \
735 KAUTH_VNODE_DELETE | \
736 KAUTH_VNODE_WRITE_ATTRIBUTES | \
737 KAUTH_VNODE_WRITE_EXTATTRIBUTES | \
738 KAUTH_VNODE_WRITE_SECURITY | \
739 KAUTH_VNODE_TAKE_OWNERSHIP | \
740 KAUTH_VNODE_LINKTARGET | \
741 KAUTH_VNODE_CHECKIMMUTABLE)
91447636
A
742
743
744#endif /* KERNEL || <sys/acl.h> */
745
746#ifdef KERNEL
0a7de745 747#include <sys/lock.h> /* lck_grp_t */
91447636
A
748
749/*
750 * Debugging
751 *
752 * XXX this wouldn't be necessary if we had a *real* debug-logging system.
753 */
754#if 0
755# ifndef _FN_KPRINTF
0a7de745 756# define _FN_KPRINTF
91447636 757void kprintf(const char *fmt, ...);
0a7de745 758# endif /* !_FN_KPRINTF */
91447636
A
759# define KAUTH_DEBUG_ENABLE
760# define K_UUID_FMT "%08x:%08x:%08x:%08x"
761# define K_UUID_ARG(_u) *(int *)&_u.g_guid[0],*(int *)&_u.g_guid[4],*(int *)&_u.g_guid[8],*(int *)&_u.g_guid[12]
0a7de745
A
762# define KAUTH_DEBUG(fmt, args...) do { kprintf("%s:%d: " fmt "\n", __PRETTY_FUNCTION__, __LINE__ , ##args); } while (0)
763# define KAUTH_DEBUG_CTX(_c) KAUTH_DEBUG("p = %p c = %p", _c->vc_proc, _c->vc_ucred)
764# define VFS_DEBUG(_ctx, _vp, fmt, args...) \
765 do { \
766 kprintf("%p '%s' %s:%d " fmt "\n", \
767 _ctx, \
768 (_vp != NULL && _vp->v_name != NULL) ? _vp->v_name : "????", \
769 __PRETTY_FUNCTION__, __LINE__ , \
770 ##args); \
91447636 771 } while(0)
0a7de745
A
772#else /* !0 */
773# define KAUTH_DEBUG(fmt, args...) do { } while (0)
774# define VFS_DEBUG(ctx, vp, fmt, args...) do { } while(0)
775#endif /* !0 */
91447636
A
776
777/*
778 * Initialisation.
779 */
780extern lck_grp_t *kauth_lck_grp;
b0d623f7 781#ifdef XNU_KERNEL_PRIVATE
91447636 782__BEGIN_DECLS
0a7de745
A
783extern void kauth_init(void);
784extern void kauth_cred_init(void);
316670eb 785#if CONFIG_EXT_RESOLVER
0a7de745
A
786extern void kauth_identity_init(void);
787extern void kauth_groups_init(void);
788extern void kauth_resolver_init(void);
316670eb 789#endif
91447636 790__END_DECLS
b0d623f7
A
791#endif /* XNU_KERNEL_PRIVATE */
792
0a7de745 793#endif /* KERNEL */
91447636
A
794
795#endif /* __APPLE_API_EVOLVING */
796#endif /* _SYS_KAUTH_H */