]>
Commit | Line | Data |
---|---|---|
490019cf A |
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 | ||
cb323159 A |
38 | #include <kern/task.h> |
39 | #include <kern/thread.h> | |
40 | #include <mach/thread_act.h> | |
41 | #include <mach/mach_types.h> | |
42 | ||
490019cf | 43 | #include <libkern/libkern.h> |
cb323159 A |
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); | |
490019cf | 48 | |
0a7de745 A |
49 | static int |
50 | kpersona_copyin(user_addr_t infop, struct kpersona_info *kinfo) | |
490019cf A |
51 | { |
52 | uint32_t info_v = 0; | |
53 | int error; | |
54 | ||
55 | error = copyin(infop, &info_v, sizeof(info_v)); | |
0a7de745 | 56 | if (error) { |
490019cf | 57 | return error; |
0a7de745 | 58 | } |
490019cf A |
59 | |
60 | /* only support a single version of the struct for now */ | |
0a7de745 | 61 | if (info_v != PERSONA_INFO_V1) { |
490019cf | 62 | return EINVAL; |
0a7de745 | 63 | } |
490019cf A |
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 | ||
0a7de745 A |
73 | static int |
74 | kpersona_copyout(struct kpersona_info *kinfo, user_addr_t infop) | |
490019cf A |
75 | { |
76 | uint32_t info_v; | |
77 | int error; | |
78 | ||
79 | error = copyin(infop, &info_v, sizeof(info_v)); | |
0a7de745 | 80 | if (error) { |
490019cf | 81 | return error; |
0a7de745 | 82 | } |
490019cf A |
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 */ | |
0a7de745 | 86 | if (info_v != PERSONA_INFO_V1) { |
490019cf | 87 | return EINVAL; |
0a7de745 | 88 | } |
490019cf A |
89 | |
90 | error = copyout(kinfo, infop, sizeof(*kinfo)); | |
91 | return error; | |
92 | } | |
93 | ||
94 | ||
0a7de745 | 95 | static int |
cb323159 | 96 | kpersona_alloc_syscall(user_addr_t infop, user_addr_t idp, user_addr_t path) |
490019cf A |
97 | { |
98 | int error; | |
99 | struct kpersona_info kinfo; | |
cb323159 | 100 | struct persona *persona = NULL; |
490019cf A |
101 | uid_t id = PERSONA_ID_NONE; |
102 | const char *login; | |
cb323159 | 103 | char *pna_path = NULL; |
490019cf | 104 | |
cb323159 | 105 | if (!IOTaskHasEntitlement(current_task(), PERSONA_MGMT_ENTITLEMENT)) { |
490019cf | 106 | return EPERM; |
0a7de745 | 107 | } |
490019cf A |
108 | |
109 | error = kpersona_copyin(infop, &kinfo); | |
0a7de745 | 110 | if (error) { |
490019cf | 111 | return error; |
0a7de745 | 112 | } |
490019cf A |
113 | |
114 | login = kinfo.persona_name[0] ? kinfo.persona_name : NULL; | |
0a7de745 | 115 | if (kinfo.persona_id != PERSONA_ID_NONE && kinfo.persona_id != (uid_t)0) { |
490019cf | 116 | id = kinfo.persona_id; |
0a7de745 | 117 | } |
490019cf | 118 | |
cb323159 | 119 | if (path) { |
f427ee49 A |
120 | pna_path = zalloc_flags(ZV_NAMEI, Z_WAITOK | Z_ZERO); |
121 | ||
cb323159 A |
122 | size_t pathlen; |
123 | error = copyinstr(path, (void *)pna_path, MAXPATHLEN, &pathlen); | |
124 | if (error) { | |
f427ee49 | 125 | zfree(ZV_NAMEI, pna_path); |
cb323159 A |
126 | return error; |
127 | } | |
128 | } | |
129 | ||
490019cf | 130 | error = 0; |
cb323159 | 131 | persona = persona_alloc(id, login, kinfo.persona_type, pna_path, &error); |
0a7de745 | 132 | if (!persona) { |
cb323159 | 133 | if (pna_path != NULL) { |
f427ee49 | 134 | zfree(ZV_NAMEI, pna_path); |
cb323159 | 135 | } |
490019cf | 136 | return error; |
0a7de745 | 137 | } |
490019cf | 138 | |
cb323159 A |
139 | /* persona struct contains a reference to pna_path */ |
140 | pna_path = NULL; | |
141 | ||
d9a64523 A |
142 | error = persona_init_begin(persona); |
143 | if (error) { | |
144 | goto out_persona_err; | |
145 | } | |
146 | ||
490019cf A |
147 | if (kinfo.persona_gid) { |
148 | error = persona_set_gid(persona, kinfo.persona_gid); | |
0a7de745 | 149 | if (error) { |
d9a64523 | 150 | goto out_persona_err; |
0a7de745 | 151 | } |
490019cf A |
152 | } |
153 | ||
154 | if (kinfo.persona_ngroups > 0) { | |
155 | /* force gmuid 0 to *opt-out* of memberd */ | |
0a7de745 | 156 | if (kinfo.persona_gmuid == 0) { |
490019cf | 157 | kinfo.persona_gmuid = KAUTH_UID_NONE; |
0a7de745 | 158 | } |
490019cf A |
159 | |
160 | error = persona_set_groups(persona, kinfo.persona_groups, | |
0a7de745 A |
161 | kinfo.persona_ngroups, |
162 | kinfo.persona_gmuid); | |
163 | if (error) { | |
d9a64523 | 164 | goto out_persona_err; |
0a7de745 | 165 | } |
490019cf A |
166 | } |
167 | ||
168 | error = copyout(&persona->pna_id, idp, sizeof(persona->pna_id)); | |
d9a64523 A |
169 | if (error) { |
170 | goto out_persona_err; | |
171 | } | |
172 | ||
173 | kinfo.persona_id = persona->pna_id; | |
490019cf | 174 | error = kpersona_copyout(&kinfo, infop); |
d9a64523 A |
175 | if (error) { |
176 | goto out_persona_err; | |
177 | } | |
178 | ||
cb323159 A |
179 | error = persona_verify_and_set_uniqueness(persona); |
180 | if (error) { | |
181 | goto out_persona_err; | |
182 | } | |
183 | ||
d9a64523 | 184 | persona_init_end(persona, error); |
490019cf A |
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 | ||
d9a64523 A |
193 | out_persona_err: |
194 | assert(error != 0); | |
195 | persona_init_end(persona, error); | |
196 | ||
197 | #if PERSONA_DEBUG | |
490019cf | 198 | printf("%s: ERROR:%d\n", __func__, error); |
d9a64523 | 199 | #endif |
0a7de745 | 200 | if (persona) { |
490019cf | 201 | persona_put(persona); |
0a7de745 | 202 | } |
490019cf A |
203 | return error; |
204 | } | |
205 | ||
0a7de745 A |
206 | static int |
207 | kpersona_dealloc_syscall(user_addr_t idp) | |
490019cf | 208 | { |
a39ff7e2 | 209 | int error = 0; |
490019cf A |
210 | uid_t persona_id; |
211 | struct persona *persona; | |
212 | ||
cb323159 | 213 | if (!IOTaskHasEntitlement(current_task(), PERSONA_MGMT_ENTITLEMENT)) { |
490019cf | 214 | return EPERM; |
0a7de745 | 215 | } |
490019cf A |
216 | |
217 | error = copyin(idp, &persona_id, sizeof(persona_id)); | |
0a7de745 | 218 | if (error) { |
490019cf | 219 | return error; |
0a7de745 | 220 | } |
490019cf | 221 | |
a39ff7e2 A |
222 | /* invalidate the persona (deny subsequent spawn/fork) */ |
223 | persona = persona_lookup_and_invalidate(persona_id); | |
224 | ||
0a7de745 | 225 | if (!persona) { |
490019cf | 226 | return ESRCH; |
0a7de745 | 227 | } |
490019cf | 228 | |
490019cf A |
229 | /* one reference from the _lookup() */ |
230 | persona_put(persona); | |
231 | ||
232 | /* one reference from the _alloc() */ | |
a39ff7e2 | 233 | persona_put(persona); |
490019cf A |
234 | |
235 | return error; | |
236 | } | |
237 | ||
0a7de745 A |
238 | static int |
239 | kpersona_get_syscall(user_addr_t idp) | |
490019cf A |
240 | { |
241 | int error; | |
cb323159 A |
242 | struct persona *persona; |
243 | ||
244 | persona = current_persona_get(); | |
490019cf | 245 | |
0a7de745 | 246 | if (!persona) { |
490019cf | 247 | return ESRCH; |
0a7de745 | 248 | } |
490019cf A |
249 | |
250 | error = copyout(&persona->pna_id, idp, sizeof(persona->pna_id)); | |
251 | persona_put(persona); | |
252 | ||
253 | return error; | |
254 | } | |
255 | ||
cb323159 A |
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 | ||
0a7de745 A |
305 | static int |
306 | kpersona_info_syscall(user_addr_t idp, user_addr_t infop) | |
490019cf A |
307 | { |
308 | int error; | |
cb323159 | 309 | uid_t current_persona_id = PERSONA_ID_NONE; |
490019cf A |
310 | uid_t persona_id; |
311 | struct persona *persona; | |
312 | struct kpersona_info kinfo; | |
313 | ||
314 | error = copyin(idp, &persona_id, sizeof(persona_id)); | |
0a7de745 | 315 | if (error) { |
490019cf | 316 | return error; |
0a7de745 | 317 | } |
490019cf | 318 | |
cb323159 A |
319 | /* Get current thread's persona id to compare if the |
320 | * input persona_id matches the current persona id | |
490019cf | 321 | */ |
cb323159 A |
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 | } | |
490019cf | 336 | |
0a7de745 | 337 | if (!persona) { |
490019cf | 338 | return ESRCH; |
0a7de745 | 339 | } |
490019cf | 340 | |
d9a64523 | 341 | persona_dbg("FOUND: persona: id:%d, gid:%d, login:\"%s\"", |
0a7de745 A |
342 | persona->pna_id, persona_get_gid(persona), |
343 | persona->pna_login); | |
490019cf A |
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); | |
f427ee49 | 350 | size_t ngroups = 0; |
490019cf | 351 | persona_get_groups(persona, &ngroups, kinfo.persona_groups, NGROUPS); |
f427ee49 | 352 | kinfo.persona_ngroups = (uint32_t)ngroups; |
490019cf A |
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 | ||
0a7de745 A |
368 | static int |
369 | kpersona_pidinfo_syscall(user_addr_t idp, user_addr_t infop) | |
490019cf A |
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)); | |
0a7de745 | 377 | if (error) { |
490019cf | 378 | return error; |
0a7de745 | 379 | } |
490019cf A |
380 | |
381 | if (!kauth_cred_issuser(kauth_cred_get()) | |
0a7de745 | 382 | && (pid != current_proc()->p_pid)) { |
490019cf | 383 | return EPERM; |
0a7de745 | 384 | } |
490019cf A |
385 | |
386 | persona = persona_proc_get(pid); | |
0a7de745 | 387 | if (!persona) { |
490019cf | 388 | return ESRCH; |
0a7de745 | 389 | } |
490019cf A |
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); | |
f427ee49 | 396 | size_t ngroups = 0; |
490019cf | 397 | persona_get_groups(persona, &ngroups, kinfo.persona_groups, NGROUPS); |
f427ee49 | 398 | kinfo.persona_ngroups = (uint32_t)ngroups; |
490019cf A |
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 | ||
0a7de745 A |
410 | static int |
411 | kpersona_find_syscall(user_addr_t infop, user_addr_t idp, user_addr_t idlenp) | |
490019cf A |
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)); | |
0a7de745 | 420 | if (error) { |
490019cf | 421 | return error; |
0a7de745 | 422 | } |
490019cf | 423 | |
0a7de745 | 424 | if (u_idlen > g_max_personas) { |
490019cf | 425 | u_idlen = g_max_personas; |
0a7de745 | 426 | } |
490019cf A |
427 | |
428 | error = kpersona_copyin(infop, &kinfo); | |
0a7de745 | 429 | if (error) { |
490019cf | 430 | goto out; |
0a7de745 | 431 | } |
490019cf A |
432 | |
433 | login = kinfo.persona_name[0] ? kinfo.persona_name : NULL; | |
434 | ||
435 | if (u_idlen > 0) { | |
c3c9b80d A |
436 | persona = kheap_alloc(KHEAP_TEMP, sizeof(*persona) * u_idlen, |
437 | Z_WAITOK | Z_ZERO); | |
490019cf A |
438 | if (!persona) { |
439 | error = ENOMEM; | |
440 | goto out; | |
441 | } | |
442 | } | |
443 | ||
444 | k_idlen = u_idlen; | |
f427ee49 | 445 | error = persona_find_all(login, kinfo.persona_id, (persona_type_t)kinfo.persona_type, persona, &k_idlen); |
0a7de745 | 446 | if (error) { |
490019cf | 447 | goto out; |
0a7de745 | 448 | } |
490019cf A |
449 | |
450 | /* copyout all the IDs of each persona we found */ | |
451 | for (size_t i = 0; i < k_idlen; i++) { | |
0a7de745 | 452 | if (i >= u_idlen) { |
490019cf | 453 | break; |
0a7de745 | 454 | } |
490019cf | 455 | error = copyout(&persona[i]->pna_id, |
0a7de745 A |
456 | idp + (i * sizeof(persona[i]->pna_id)), |
457 | sizeof(persona[i]->pna_id)); | |
458 | if (error) { | |
490019cf | 459 | goto out; |
0a7de745 | 460 | } |
490019cf A |
461 | } |
462 | ||
463 | out: | |
464 | if (persona) { | |
0a7de745 | 465 | for (size_t i = 0; i < u_idlen; i++) { |
490019cf | 466 | persona_put(persona[i]); |
0a7de745 | 467 | } |
c3c9b80d | 468 | kheap_free(KHEAP_TEMP, persona, sizeof(*persona) * u_idlen); |
490019cf A |
469 | } |
470 | ||
471 | (void)copyout(&k_idlen, idlenp, sizeof(u_idlen)); | |
472 | ||
473 | return error; | |
474 | } | |
475 | ||
490019cf A |
476 | /* |
477 | * Syscall entry point / demux. | |
478 | */ | |
0a7de745 A |
479 | int |
480 | persona(__unused proc_t p, struct persona_args *pargs, __unused int32_t *retval) | |
490019cf A |
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; | |
cb323159 | 487 | user_addr_t path = pargs->path; |
490019cf A |
488 | |
489 | switch (op) { | |
490 | case PERSONA_OP_ALLOC: | |
cb323159 A |
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); | |
490019cf A |
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; | |
cb323159 A |
502 | case PERSONA_OP_GETPATH: |
503 | error = kpersona_getpath_syscall(idp, path); | |
504 | break; | |
490019cf A |
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: | |
cb323159 | 512 | case PERSONA_OP_FIND_BY_TYPE: |
490019cf A |
513 | error = kpersona_find_syscall(infop, idp, pargs->idlen); |
514 | break; | |
515 | default: | |
516 | error = ENOSYS; | |
517 | break; | |
518 | } | |
519 | ||
520 | return error; | |
521 | } |