2 * Copyright (c) 2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #ifndef _SYS_PERSONA_H_
29 #define _SYS_PERSONA_H_
32 #include <sys/param.h>
41 PERSONA_SYSTEM_PROXY
= 6,
43 PERSONA_ENTERPRISE
= 8,
45 PERSONA_TYPE_MAX
= PERSONA_ENTERPRISE
,
48 #define PERSONA_ID_NONE ((uid_t)-1)
50 struct kpersona_info
{
51 uint32_t persona_info_version
;
53 uid_t persona_id
; /* overlaps with UID */
56 uint32_t persona_ngroups
;
57 gid_t persona_groups
[NGROUPS
];
59 char persona_name
[MAXLOGNAME
+ 1];
61 /* TODO: MAC policies?! */
64 #define PERSONA_INFO_V1 1
65 #define PERSONA_INFO_V1_SIZE (sizeof(struct kpersona_info))
68 #define PERSONA_OP_ALLOC 1
69 #define PERSONA_OP_PALLOC 2
70 #define PERSONA_OP_DEALLOC 3
71 #define PERSONA_OP_GET 4
72 #define PERSONA_OP_INFO 5
73 #define PERSONA_OP_PIDINFO 6
74 #define PERSONA_OP_FIND 7
75 #define PERSONA_OP_GETPATH 8
76 #define PERSONA_OP_FIND_BY_TYPE 9
78 #define PERSONA_MGMT_ENTITLEMENT "com.apple.private.persona-mgmt"
82 * user space persona interface
86 * kpersona_alloc: Allocate a new in-kernel persona
89 * info: Pointer to persona info structure describing the
90 * attributes of the persona to create / allocate.
92 * id: output: set to the ID of the created persona
95 * The 'persona_id' field of the 'info' parameter is ignored.
101 int kpersona_alloc(struct kpersona_info
*info
, uid_t
*id
);
104 * kpersona_palloc: Allocate a new in-kernel persona with a directory
108 * info: Pointer to persona info structure describing the
109 * attributes of the persona to create / allocate.
111 * path: Pointer to directory name that stores persona specific
112 * data. Assumes path buffer length = MAXPATHLEN and is a
113 * null-terminated string.
115 * id: output: set to the ID of the created persona
118 * The 'persona_id' field of the 'info' parameter is ignored.
124 int kpersona_palloc(struct kpersona_info
*info
, uid_t
*id
, char path
[MAXPATHLEN
]);
127 * kpersona_dealloc: delete / destroy an in-kernel persona
130 * id: the ID of the persona to destroy
136 int kpersona_dealloc(uid_t id
);
139 * kpersona_get: retrieve the persona with which the current thread is running
141 * To find the proc's persona id use kpersona_pidinfo
144 * id: output: will be filled with the persona id from the voucher adopted
145 * on the current thread. If that voucher contains no persona information
146 * or there is no such voucher, then it defaults to the proc's persona id.
149 * < 0: Thread is not running under any persona
150 * 0: Success (uuid is filled with running persona UUID)
152 int kpersona_get(uid_t
*id
);
155 * kpersona_get_path: retrieve the given persona's path
158 * id: ID of the persona
160 * path: output: filled in with path on success.
161 * Assumes path buffer length = MAXPATHLEN
167 int kpersona_getpath(uid_t id
, char path
[MAXPATHLEN
]);
170 * kpersona_info: gather info about the given persona
173 * id: ID of the persona to investigate
174 * If set to 0, it uses persona id from the voucher adopted on the current
175 * thread. If that voucher contains no persona information or there is no
176 * such voucher, then it defaults to the proc's persona id.
178 * info: output: filled in with persona attributes on success.
184 int kpersona_info(uid_t id
, struct kpersona_info
*info
);
187 * kpersona_pidinfo: gather persona info about the given PID
190 * pid: PID of the process whose persona info we're to return
192 * info: output: filled in with persona attributes on success.
198 int kpersona_pidinfo(pid_t pid
, struct kpersona_info
*info
);
201 * kpersona_find: lookup the kernel's UUID of a persona
204 * name: Local login name of the persona.
205 * Set this to NULL to find personas by 'uid'.
207 * uid: UID of the persona.
208 * Set this to -1 to find personas by 'name'
210 * id: output: the ID(s) matching the input parameters
213 * idlen: input - size of 'id' buffer (in number of IDs)
214 * output - the total required size of the 'id' buffer
215 * (in number of IDs) - may be larger than input size
217 * At least one of 'name' or 'uid' must be set.
221 * >= 0: The number of IDs found to match the input parameters
223 int kpersona_find(const char *name
, uid_t uid
, uid_t
*id
, size_t *idlen
);
226 * kpersona_find_by_type: lookup the persona ids by type
229 * persona_type: Type of persona id (see enum)
231 * id: output: the ID(s) matching the input parameters
234 * idlen: input - size of 'id' buffer (in number of IDs)
235 * output - the total required size of the 'id' buffer
236 * (in number of IDs) - may be larger than input size
239 * >= 0: The number of IDs found to match the input parameters
241 int kpersona_find_by_type(int persona_type
, uid_t
*id
, size_t *idlen
);
244 #ifdef KERNEL_PRIVATE
245 /* XNU + kext private interface */
246 #include <sys/cdefs.h>
247 #include <sys/kauth.h>
248 #include <libkern/libkern.h>
249 #include <os/refcnt.h>
253 #define persona_dbg(fmt, ...) \
254 os_log(OS_LOG_DEFAULT, "[%4d] %s: " fmt "\n", \
255 current_proc() ? current_proc()->p_pid : -1, \
256 __func__, ## __VA_ARGS__)
258 #define persona_dbg(fmt, ...) do { } while (0)
264 #ifdef XNU_KERNEL_PRIVATE
265 /* only XNU proper needs to see the persona structure */
267 os_refcnt_t pna_refcount
;
272 char pna_login
[MAXLOGNAME
+ 1];
275 kauth_cred_t pna_cred
;
278 int pna_cred_locked
; /* set upon first adoption */
280 LIST_ENTRY(persona
) pna_list
;
282 /* this could go away if we used a coalition */
283 LIST_HEAD(, proc
) pna_members
;
288 * We can add things here such as PID maps, UID maps, etc.
295 #define persona_lock(persona) lck_mtx_lock(&(persona)->pna_lock)
296 #define persona_unlock(persona) lck_mtx_unlock(&(persona)->pna_lock)
297 #define persona_try_lock(persona) lck_mtx_try_lock(&(persona)->pna_lock)
299 #define persona_lock_assert_held(persona) \
300 LCK_MTX_ASSERT(&(persona)->pna_lock, LCK_MTX_ASSERT_OWNED)
303 static inline const char *
304 persona_desc(struct persona
*persona
, int locked
)
310 if (persona
->pna_desc
[0] != 0) {
311 return persona
->pna_desc
;
315 persona_lock(persona
);
317 if (persona
->pna_desc
[0] != 0) {
321 char *p
= &persona
->pna_desc
[0];
322 char *end
= p
+ sizeof(persona
->pna_desc
) - 1;
325 p
+= scnprintf(p
, end
- p
, "%s/%d:%d",
327 kauth_cred_getuid(persona
->pna_cred
),
328 kauth_cred_getgid(persona
->pna_cred
));
335 persona_unlock(persona
);
338 return persona
->pna_desc
;
340 #else /* !PERSONA_DEBUG */
341 static inline const char *
342 persona_desc(struct persona
*persona
, int locked
)
350 #else /* !XNU_KERNEL_PRIVATE */
351 /* kexts should only see an opaque persona structure */
357 #ifndef _KAUTH_CRED_T
358 #define _KAUTH_CRED_T
359 typedef struct ucred
*kauth_cred_t
;
360 #endif /* !_KAUTH_CRED_T */
362 /* returns the persona ID for the given pesona structure */
363 uid_t
persona_get_id(struct persona
*persona
);
365 /* returns the type of the persona (see enum above: PERSONA_GUEST, etc.) */
366 int persona_get_type(struct persona
*persona
);
368 /* returns ref on kauth_cred_t that must be dropped via kauth_cred_unref() */
369 kauth_cred_t
persona_get_cred(struct persona
*persona
);
371 /* returns a reference that must be released with persona_put() */
372 struct persona
*persona_lookup(uid_t id
);
375 * Search for personas based on name or uid
378 * name: Local login name of the persona.
379 * Set this to NULL to find personas by 'uid'.
381 * uid: UID of the persona.
382 * Set this to -1 to find personas by 'name'
384 * persona: output - array of persona pointers. Each non-NULL value
385 * must* be released with persona_put. This can be NULL.
387 * plen: input - size of 'persona' buffer (in number of pointers)
388 * output - the total required size of the 'persona' buffer (could be larger than input value)
392 * != 0: failure (BSD errno value ESRCH or EINVAL)
394 int persona_find(const char *login
, uid_t uid
,
395 struct persona
**persona
, size_t *plen
);
397 /* returns a reference that must be released with persona_put() */
398 struct persona
*persona_proc_get(pid_t pid
);
400 /* returns a reference to the persona tied to the current thread (also uses adopted voucher) */
401 struct persona
*current_persona_get(void);
403 /* get a reference to a persona structure */
404 struct persona
*persona_get(struct persona
*persona
);
406 /* release a reference to a persona structure */
407 void persona_put(struct persona
*persona
);
410 * Search for personas of a given type, 'persona_type'.
413 * persona_type: Type of persona (see enum)
415 * persona: output - array of persona pointers. Each non-NULL value
416 * must* be released with persona_put. This can be NULL.
418 * plen: input - size of 'persona' buffer (in number of pointers)
419 * output - the total required size of the 'persona' buffer (could be larger than input value)
423 * != 0: failure (BSD errno value ESRCH or EINVAL)
425 int persona_find_by_type(int persona_type
, struct persona
**persona
,
428 #ifdef XNU_KERNEL_PRIVATE
431 #include <sys/proc_internal.h>
434 * In-kernel persona API
436 extern uint32_t g_max_personas
;
438 void personas_bootstrap(void);
440 struct persona
*persona_alloc(uid_t id
, const char *login
,
441 int type
, char *path
, int *error
);
443 int persona_init_begin(struct persona
*persona
);
444 void persona_init_end(struct persona
*persona
, int error
);
446 struct persona
*persona_lookup_and_invalidate(uid_t id
);
447 int persona_verify_and_set_uniqueness(struct persona
*persona
);
448 boolean_t
persona_is_unique(struct persona
*persona
);
451 proc_has_persona(proc_t p
)
453 if (p
&& p
->p_persona
) {
460 persona_id_from_proc(proc_t p
)
462 if (p
&& p
->p_persona
) {
463 return p
->p_persona
->pna_id
;
465 return PERSONA_ID_NONE
;
468 int persona_proc_inherit(proc_t child
, proc_t parent
);
470 int persona_proc_adopt_id(proc_t p
, uid_t id
,
471 kauth_cred_t auth_override
);
472 int persona_proc_adopt(proc_t p
, struct persona
*persona
,
473 kauth_cred_t auth_override
);
474 int persona_proc_drop(proc_t p
);
476 int persona_set_cred(struct persona
*persona
, kauth_cred_t cred
);
477 int persona_set_cred_from_proc(struct persona
*persona
, proc_t proc
);
479 uid_t
persona_get_uid(struct persona
*persona
);
481 int persona_set_gid(struct persona
*persona
, gid_t gid
);
482 gid_t
persona_get_gid(struct persona
*persona
);
484 int persona_set_groups(struct persona
*persona
, gid_t
*groups
, unsigned ngroups
, uid_t gmuid
);
485 int persona_get_groups(struct persona
*persona
, unsigned *ngroups
, gid_t
*groups
, unsigned groups_sz
);
487 uid_t
persona_get_gmuid(struct persona
*persona
);
489 int persona_get_login(struct persona
*persona
, char login
[MAXLOGNAME
+ 1]);
491 /* returns a reference that must be released with persona_put() */
492 struct persona
*persona_proc_get(pid_t pid
);
494 int persona_find_all(const char *login
, uid_t uid
, int persona_type
,
495 struct persona
**persona
, size_t *plen
);
497 #else /* !CONFIG_PERSONAS */
500 proc_has_persona(__unused proc_t p
)
506 persona_id_from_proc(__unused proc_t p
)
508 return PERSONA_ID_NONE
;
511 #endif /* CONFIG_PERSONAS */
512 #endif /* XNU_KERNEL_PRIVATE */
515 #endif /* KERNEL_PRIVATE */
518 #endif /* _SYS_PERSONA_H_ */