]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/sys_persona.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / kern / sys_persona.c
1 /*
2 * Copyright (c) 2015 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 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/kernel_types.h>
31 #include <sys/sysproto.h>
32
33 #include <sys/kauth.h>
34 #include <sys/malloc.h>
35 #include <sys/persona.h>
36 #include <sys/proc.h>
37
38 #include <kern/task.h>
39 #include <kern/thread.h>
40 #include <mach/thread_act.h>
41 #include <mach/mach_types.h>
42
43 #include <libkern/libkern.h>
44 #include <IOKit/IOBSD.h>
45
46 extern kern_return_t bank_get_bank_ledger_thread_group_and_persona(void *voucher,
47 void *bankledger, void **banktg, uint32_t *persona_id);
48
49 static int
50 kpersona_copyin(user_addr_t infop, struct kpersona_info *kinfo)
51 {
52 uint32_t info_v = 0;
53 int error;
54
55 error = copyin(infop, &info_v, sizeof(info_v));
56 if (error) {
57 return error;
58 }
59
60 /* only support a single version of the struct for now */
61 if (info_v != PERSONA_INFO_V1) {
62 return EINVAL;
63 }
64
65 error = copyin(infop, kinfo, sizeof(*kinfo));
66
67 /* enforce NULL termination on strings */
68 kinfo->persona_name[MAXLOGNAME] = 0;
69
70 return error;
71 }
72
73 static int
74 kpersona_copyout(struct kpersona_info *kinfo, user_addr_t infop)
75 {
76 uint32_t info_v;
77 int error;
78
79 error = copyin(infop, &info_v, sizeof(info_v));
80 if (error) {
81 return error;
82 }
83
84 /* only support a single version of the struct for now */
85 /* TODO: in the future compare info_v to kinfo->persona_info_version */
86 if (info_v != PERSONA_INFO_V1) {
87 return EINVAL;
88 }
89
90 error = copyout(kinfo, infop, sizeof(*kinfo));
91 return error;
92 }
93
94
95 static int
96 kpersona_alloc_syscall(user_addr_t infop, user_addr_t idp, user_addr_t path)
97 {
98 int error;
99 struct kpersona_info kinfo;
100 struct persona *persona = NULL;
101 uid_t id = PERSONA_ID_NONE;
102 const char *login;
103 char *pna_path = NULL;
104
105 if (!IOTaskHasEntitlement(current_task(), PERSONA_MGMT_ENTITLEMENT)) {
106 return EPERM;
107 }
108
109 error = kpersona_copyin(infop, &kinfo);
110 if (error) {
111 return error;
112 }
113
114 login = kinfo.persona_name[0] ? kinfo.persona_name : NULL;
115 if (kinfo.persona_id != PERSONA_ID_NONE && kinfo.persona_id != (uid_t)0) {
116 id = kinfo.persona_id;
117 }
118
119 if (path) {
120 pna_path = zalloc_flags(ZV_NAMEI, Z_WAITOK | Z_ZERO);
121
122 size_t pathlen;
123 error = copyinstr(path, (void *)pna_path, MAXPATHLEN, &pathlen);
124 if (error) {
125 zfree(ZV_NAMEI, pna_path);
126 return error;
127 }
128 }
129
130 error = 0;
131 persona = persona_alloc(id, login, kinfo.persona_type, pna_path, &error);
132 if (!persona) {
133 if (pna_path != NULL) {
134 zfree(ZV_NAMEI, pna_path);
135 }
136 return error;
137 }
138
139 /* persona struct contains a reference to pna_path */
140 pna_path = NULL;
141
142 error = persona_init_begin(persona);
143 if (error) {
144 goto out_persona_err;
145 }
146
147 if (kinfo.persona_gid) {
148 error = persona_set_gid(persona, kinfo.persona_gid);
149 if (error) {
150 goto out_persona_err;
151 }
152 }
153
154 if (kinfo.persona_ngroups > 0) {
155 /* force gmuid 0 to *opt-out* of memberd */
156 if (kinfo.persona_gmuid == 0) {
157 kinfo.persona_gmuid = KAUTH_UID_NONE;
158 }
159
160 error = persona_set_groups(persona, kinfo.persona_groups,
161 kinfo.persona_ngroups,
162 kinfo.persona_gmuid);
163 if (error) {
164 goto out_persona_err;
165 }
166 }
167
168 error = copyout(&persona->pna_id, idp, sizeof(persona->pna_id));
169 if (error) {
170 goto out_persona_err;
171 }
172
173 kinfo.persona_id = persona->pna_id;
174 error = kpersona_copyout(&kinfo, infop);
175 if (error) {
176 goto out_persona_err;
177 }
178
179 error = persona_verify_and_set_uniqueness(persona);
180 if (error) {
181 goto out_persona_err;
182 }
183
184 persona_init_end(persona, error);
185
186 /*
187 * On success, we have a persona structure in the global list with a
188 * single reference count on it. The corresponding _dealloc() call
189 * will release this reference.
190 */
191 return error;
192
193 out_persona_err:
194 assert(error != 0);
195 persona_init_end(persona, error);
196
197 #if PERSONA_DEBUG
198 printf("%s: ERROR:%d\n", __func__, error);
199 #endif
200 if (persona) {
201 persona_put(persona);
202 }
203 return error;
204 }
205
206 static int
207 kpersona_dealloc_syscall(user_addr_t idp)
208 {
209 int error = 0;
210 uid_t persona_id;
211 struct persona *persona;
212
213 if (!IOTaskHasEntitlement(current_task(), PERSONA_MGMT_ENTITLEMENT)) {
214 return EPERM;
215 }
216
217 error = copyin(idp, &persona_id, sizeof(persona_id));
218 if (error) {
219 return error;
220 }
221
222 /* invalidate the persona (deny subsequent spawn/fork) */
223 persona = persona_lookup_and_invalidate(persona_id);
224
225 if (!persona) {
226 return ESRCH;
227 }
228
229 /* one reference from the _lookup() */
230 persona_put(persona);
231
232 /* one reference from the _alloc() */
233 persona_put(persona);
234
235 return error;
236 }
237
238 static int
239 kpersona_get_syscall(user_addr_t idp)
240 {
241 int error;
242 struct persona *persona;
243
244 persona = current_persona_get();
245
246 if (!persona) {
247 return ESRCH;
248 }
249
250 error = copyout(&persona->pna_id, idp, sizeof(persona->pna_id));
251 persona_put(persona);
252
253 return error;
254 }
255
256 static int
257 kpersona_getpath_syscall(user_addr_t idp, user_addr_t path)
258 {
259 int error;
260 uid_t persona_id;
261 struct persona *persona;
262 size_t pathlen;
263 uid_t current_persona_id = PERSONA_ID_NONE;
264
265 if (!path) {
266 return EINVAL;
267 }
268
269 error = copyin(idp, &persona_id, sizeof(persona_id));
270 if (error) {
271 return error;
272 }
273
274 /* Get current thread's persona id to compare if the
275 * input persona_id matches the current persona id
276 */
277 persona = current_persona_get();
278 if (persona) {
279 current_persona_id = persona->pna_id;
280 }
281
282 if (persona_id && persona_id != current_persona_id) {
283 /* Release the reference on the current persona id's persona */
284 persona_put(persona);
285 if (!kauth_cred_issuser(kauth_cred_get()) &&
286 !IOTaskHasEntitlement(current_task(), PERSONA_MGMT_ENTITLEMENT)) {
287 return EPERM;
288 }
289 persona = persona_lookup(persona_id);
290 }
291
292 if (!persona) {
293 return ESRCH;
294 }
295
296 if (persona->pna_path) {
297 error = copyoutstr((void *)persona->pna_path, path, MAXPATHLEN, &pathlen);
298 }
299
300 persona_put(persona);
301
302 return error;
303 }
304
305 static int
306 kpersona_info_syscall(user_addr_t idp, user_addr_t infop)
307 {
308 int error;
309 uid_t current_persona_id = PERSONA_ID_NONE;
310 uid_t persona_id;
311 struct persona *persona;
312 struct kpersona_info kinfo;
313
314 error = copyin(idp, &persona_id, sizeof(persona_id));
315 if (error) {
316 return error;
317 }
318
319 /* Get current thread's persona id to compare if the
320 * input persona_id matches the current persona id
321 */
322 persona = current_persona_get();
323 if (persona) {
324 current_persona_id = persona->pna_id;
325 }
326
327 if (persona_id && persona_id != current_persona_id) {
328 /* Release the reference on the current persona id's persona */
329 persona_put(persona);
330 if (!kauth_cred_issuser(kauth_cred_get()) &&
331 !IOTaskHasEntitlement(current_task(), PERSONA_MGMT_ENTITLEMENT)) {
332 return EPERM;
333 }
334 persona = persona_lookup(persona_id);
335 }
336
337 if (!persona) {
338 return ESRCH;
339 }
340
341 persona_dbg("FOUND: persona: id:%d, gid:%d, login:\"%s\"",
342 persona->pna_id, persona_get_gid(persona),
343 persona->pna_login);
344
345 memset(&kinfo, 0, sizeof(kinfo));
346 kinfo.persona_info_version = PERSONA_INFO_V1;
347 kinfo.persona_id = persona->pna_id;
348 kinfo.persona_type = persona->pna_type;
349 kinfo.persona_gid = persona_get_gid(persona);
350 size_t ngroups = 0;
351 persona_get_groups(persona, &ngroups, kinfo.persona_groups, NGROUPS);
352 kinfo.persona_ngroups = (uint32_t)ngroups;
353 kinfo.persona_gmuid = persona_get_gmuid(persona);
354
355 /*
356 * NULL termination is assured b/c persona_name is
357 * exactly MAXLOGNAME + 1 bytes (and has been memset to 0)
358 */
359 strncpy(kinfo.persona_name, persona->pna_login, MAXLOGNAME);
360
361 persona_put(persona);
362
363 error = kpersona_copyout(&kinfo, infop);
364
365 return error;
366 }
367
368 static int
369 kpersona_pidinfo_syscall(user_addr_t idp, user_addr_t infop)
370 {
371 int error;
372 pid_t pid;
373 struct persona *persona;
374 struct kpersona_info kinfo;
375
376 error = copyin(idp, &pid, sizeof(pid));
377 if (error) {
378 return error;
379 }
380
381 if (!kauth_cred_issuser(kauth_cred_get())
382 && (pid != current_proc()->p_pid)) {
383 return EPERM;
384 }
385
386 persona = persona_proc_get(pid);
387 if (!persona) {
388 return ESRCH;
389 }
390
391 memset(&kinfo, 0, sizeof(kinfo));
392 kinfo.persona_info_version = PERSONA_INFO_V1;
393 kinfo.persona_id = persona->pna_id;
394 kinfo.persona_type = persona->pna_type;
395 kinfo.persona_gid = persona_get_gid(persona);
396 size_t ngroups = 0;
397 persona_get_groups(persona, &ngroups, kinfo.persona_groups, NGROUPS);
398 kinfo.persona_ngroups = (uint32_t)ngroups;
399 kinfo.persona_gmuid = persona_get_gmuid(persona);
400
401 strncpy(kinfo.persona_name, persona->pna_login, MAXLOGNAME);
402
403 persona_put(persona);
404
405 error = kpersona_copyout(&kinfo, infop);
406
407 return error;
408 }
409
410 static int
411 kpersona_find_syscall(user_addr_t infop, user_addr_t idp, user_addr_t idlenp)
412 {
413 int error;
414 struct kpersona_info kinfo;
415 const char *login;
416 size_t u_idlen, k_idlen = 0;
417 struct persona **persona = NULL;
418
419 error = copyin(idlenp, &u_idlen, sizeof(u_idlen));
420 if (error) {
421 return error;
422 }
423
424 if (u_idlen > g_max_personas) {
425 u_idlen = g_max_personas;
426 }
427
428 error = kpersona_copyin(infop, &kinfo);
429 if (error) {
430 goto out;
431 }
432
433 login = kinfo.persona_name[0] ? kinfo.persona_name : NULL;
434
435 if (u_idlen > 0) {
436 persona = kheap_alloc(KHEAP_TEMP, sizeof(*persona) * u_idlen,
437 Z_WAITOK | Z_ZERO);
438 if (!persona) {
439 error = ENOMEM;
440 goto out;
441 }
442 }
443
444 k_idlen = u_idlen;
445 error = persona_find_all(login, kinfo.persona_id, (persona_type_t)kinfo.persona_type, persona, &k_idlen);
446 if (error) {
447 goto out;
448 }
449
450 /* copyout all the IDs of each persona we found */
451 for (size_t i = 0; i < k_idlen; i++) {
452 if (i >= u_idlen) {
453 break;
454 }
455 error = copyout(&persona[i]->pna_id,
456 idp + (i * sizeof(persona[i]->pna_id)),
457 sizeof(persona[i]->pna_id));
458 if (error) {
459 goto out;
460 }
461 }
462
463 out:
464 if (persona) {
465 for (size_t i = 0; i < u_idlen; i++) {
466 persona_put(persona[i]);
467 }
468 kheap_free(KHEAP_TEMP, persona, sizeof(*persona) * u_idlen);
469 }
470
471 (void)copyout(&k_idlen, idlenp, sizeof(u_idlen));
472
473 return error;
474 }
475
476 /*
477 * Syscall entry point / demux.
478 */
479 int
480 persona(__unused proc_t p, struct persona_args *pargs, __unused int32_t *retval)
481 {
482 int error;
483 uint32_t op = pargs->operation;
484 /* uint32_t flags = pargs->flags; */
485 user_addr_t infop = pargs->info;
486 user_addr_t idp = pargs->id;
487 user_addr_t path = pargs->path;
488
489 switch (op) {
490 case PERSONA_OP_ALLOC:
491 error = kpersona_alloc_syscall(infop, idp, USER_ADDR_NULL);
492 break;
493 case PERSONA_OP_PALLOC:
494 error = kpersona_alloc_syscall(infop, idp, path);
495 break;
496 case PERSONA_OP_DEALLOC:
497 error = kpersona_dealloc_syscall(idp);
498 break;
499 case PERSONA_OP_GET:
500 error = kpersona_get_syscall(idp);
501 break;
502 case PERSONA_OP_GETPATH:
503 error = kpersona_getpath_syscall(idp, path);
504 break;
505 case PERSONA_OP_INFO:
506 error = kpersona_info_syscall(idp, infop);
507 break;
508 case PERSONA_OP_PIDINFO:
509 error = kpersona_pidinfo_syscall(idp, infop);
510 break;
511 case PERSONA_OP_FIND:
512 case PERSONA_OP_FIND_BY_TYPE:
513 error = kpersona_find_syscall(infop, idp, pargs->idlen);
514 break;
515 default:
516 error = ENOSYS;
517 break;
518 }
519
520 return error;
521 }