1 | /* |
2 | * Copyright (c) 2007-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 | /*- |
30 | * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson |
31 | * Copyright (c) 2001 Ilmar S. Habibulin |
32 | * Copyright (c) 2001, 2002, 2003, 2004 Networks Associates Technology, Inc. |
33 | * |
34 | * This software was developed by Robert Watson and Ilmar Habibulin for the |
35 | * TrustedBSD Project. |
36 | * |
37 | * This software was developed for the FreeBSD Project in part by Network |
38 | * Associates Laboratories, the Security Research Division of Network |
39 | * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), |
40 | * as part of the DARPA CHATS research program. |
41 | * |
42 | * Redistribution and use in source and binary forms, with or without |
43 | * modification, are permitted provided that the following conditions |
44 | * are met: |
45 | * 1. Redistributions of source code must retain the above copyright |
46 | * notice, this list of conditions and the following disclaimer. |
47 | * 2. Redistributions in binary form must reproduce the above copyright |
48 | * notice, this list of conditions and the following disclaimer in the |
49 | * documentation and/or other materials provided with the distribution. |
50 | * |
51 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
52 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
53 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
54 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
55 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
56 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
57 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
58 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
59 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
60 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
61 | * SUCH DAMAGE. |
62 | * |
63 | */ |
64 | |
65 | #include <string.h> |
66 | #include <sys/param.h> |
67 | #include <sys/ucred.h> |
68 | #include <sys/malloc.h> |
69 | #include <sys/sbuf.h> |
70 | #include <sys/vnode.h> |
71 | #include <sys/proc.h> |
72 | #include <sys/proc_internal.h> |
73 | #include <sys/kauth.h> |
74 | #include <sys/imgact.h> |
75 | #include <mach/mach_types.h> |
76 | #include <kern/task.h> |
77 | |
78 | #include <security/mac_internal.h> |
79 | #include <security/mac_mach_internal.h> |
80 | |
81 | #include <bsd/security/audit/audit.h> |
82 | |
83 | struct label * |
84 | mac_cred_label_alloc(void) |
85 | { |
86 | struct label *label; |
87 | |
88 | label = mac_labelzone_alloc(MAC_WAITOK); |
89 | if (label == NULL) |
90 | return (NULL); |
91 | MAC_PERFORM(cred_label_init, label); |
92 | return (label); |
93 | } |
94 | |
95 | void |
96 | mac_cred_label_init(struct ucred *cred) |
97 | { |
98 | cred->cr_label = mac_cred_label_alloc(); |
99 | } |
100 | |
101 | void |
102 | mac_cred_label_free(struct label *label) |
103 | { |
104 | MAC_PERFORM(cred_label_destroy, label); |
105 | mac_labelzone_free(label); |
106 | } |
107 | |
108 | int |
109 | mac_cred_label_compare(struct label *a, struct label *b) |
110 | { |
111 | return (bcmp(a, b, sizeof (*a)) == 0); |
112 | } |
113 | |
114 | int |
115 | mac_cred_label_externalize_audit(struct proc *p, struct mac *mac) |
116 | { |
117 | kauth_cred_t cr; |
118 | int error; |
119 | |
120 | cr = kauth_cred_proc_ref(p); |
121 | |
122 | error = MAC_EXTERNALIZE_AUDIT(cred, cr->cr_label, |
123 | mac->m_string, mac->m_buflen); |
124 | |
125 | kauth_cred_unref(&cr); |
126 | return (error); |
127 | } |
128 | |
129 | void |
130 | mac_cred_label_destroy(kauth_cred_t cred) |
131 | { |
132 | |
133 | mac_cred_label_free(cred->cr_label); |
134 | cred->cr_label = NULL; |
135 | } |
136 | |
137 | int |
138 | mac_cred_label_externalize(struct label *label, char *elements, |
139 | char *outbuf, size_t outbuflen, int flags __unused) |
140 | { |
141 | int error = 0; |
142 | |
143 | error = MAC_EXTERNALIZE(cred, label, elements, outbuf, outbuflen); |
144 | |
145 | return (error); |
146 | } |
147 | |
148 | int |
149 | mac_cred_label_internalize(struct label *label, char *string) |
150 | { |
151 | int error; |
152 | |
153 | error = MAC_INTERNALIZE(cred, label, string); |
154 | |
155 | return (error); |
156 | } |
157 | |
158 | /* |
159 | * By default, fork just adds a reference to the parent |
160 | * credential. Policies may need to know about this reference |
161 | * if they are tracking exit calls to know when to free the |
162 | * label. |
163 | */ |
164 | void |
165 | mac_cred_label_associate_fork(kauth_cred_t cred, proc_t proc) |
166 | { |
167 | MAC_PERFORM(cred_label_associate_fork, cred, proc); |
168 | } |
169 | |
170 | /* |
171 | * Initialize MAC label for the first kernel process, from which other |
172 | * kernel processes and threads are spawned. |
173 | */ |
174 | void |
175 | mac_cred_label_associate_kernel(kauth_cred_t cred) |
176 | { |
177 | |
178 | MAC_PERFORM(cred_label_associate_kernel, cred); |
179 | } |
180 | |
181 | /* |
182 | * Initialize MAC label for the first userland process, from which other |
183 | * userland processes and threads are spawned. |
184 | */ |
185 | void |
186 | mac_cred_label_associate_user(kauth_cred_t cred) |
187 | { |
188 | |
189 | MAC_PERFORM(cred_label_associate_user, cred); |
190 | } |
191 | |
192 | /* |
193 | * When a new process is created, its label must be initialized. Generally, |
194 | * this involves inheritence from the parent process, modulo possible |
195 | * deltas. This function allows that processing to take place. |
196 | */ |
197 | void |
198 | mac_cred_label_associate(struct ucred *parent_cred, struct ucred *child_cred) |
199 | { |
200 | |
201 | MAC_PERFORM(cred_label_associate, parent_cred, child_cred); |
202 | } |
203 | |
204 | int |
205 | mac_execve_enter(user_addr_t mac_p, struct image_params *imgp) |
206 | { |
207 | struct user_mac mac; |
208 | struct label *execlabel; |
209 | char *buffer; |
210 | int error; |
211 | size_t ulen; |
212 | |
213 | if (mac_p == USER_ADDR_NULL) |
214 | return (0); |
215 | |
216 | if (IS_64BIT_PROCESS(current_proc())) { |
217 | struct user64_mac mac64; |
218 | error = copyin(mac_p, &mac64, sizeof(mac64)); |
219 | mac.m_buflen = mac64.m_buflen; |
220 | mac.m_string = mac64.m_string; |
221 | } else { |
222 | struct user32_mac mac32; |
223 | error = copyin(mac_p, &mac32, sizeof(mac32)); |
224 | mac.m_buflen = mac32.m_buflen; |
225 | mac.m_string = mac32.m_string; |
226 | } |
227 | if (error) |
228 | return (error); |
229 | |
230 | error = mac_check_structmac_consistent(&mac); |
231 | if (error) |
232 | return (error); |
233 | |
234 | execlabel = mac_cred_label_alloc(); |
235 | MALLOC(buffer, char *, mac.m_buflen, M_MACTEMP, M_WAITOK); |
236 | error = copyinstr(CAST_USER_ADDR_T(mac.m_string), buffer, mac.m_buflen, &ulen); |
237 | if (error) |
238 | goto out; |
239 | AUDIT_ARG(mac_string, buffer); |
240 | |
241 | error = mac_cred_label_internalize(execlabel, buffer); |
242 | out: |
243 | if (error) { |
244 | mac_cred_label_free(execlabel); |
245 | execlabel = NULL; |
246 | } |
247 | imgp->ip_execlabelp = execlabel; |
248 | FREE(buffer, M_MACTEMP); |
249 | return (error); |
250 | } |
251 | |
252 | /* |
253 | * When the subject's label changes, it may require revocation of privilege |
254 | * to mapped objects. This can't be done on-the-fly later with a unified |
255 | * buffer cache. |
256 | * |
257 | * XXX: CRF_MAC_ENFORCE should be in a kauth_cred_t field, rather |
258 | * XXX: than a posix_cred_t field. |
259 | */ |
260 | void |
261 | mac_cred_label_update(kauth_cred_t cred, struct label *newlabel) |
262 | { |
263 | posix_cred_t pcred = posix_cred_get(cred); |
264 | |
265 | /* force label to be part of "matching" for credential */ |
266 | pcred->cr_flags |= CRF_MAC_ENFORCE; |
267 | |
268 | /* inform the policies of the update */ |
269 | MAC_PERFORM(cred_label_update, cred, newlabel); |
270 | } |
271 | |
272 | int |
273 | mac_cred_check_label_update(kauth_cred_t cred, struct label *newlabel) |
274 | { |
275 | int error; |
276 | |
277 | #if SECURITY_MAC_CHECK_ENFORCE |
278 | /* 21167099 - only check if we allow write */ |
279 | if (!mac_proc_enforce) |
280 | return 0; |
281 | #endif |
282 | |
283 | MAC_CHECK(cred_check_label_update, cred, newlabel); |
284 | |
285 | return (error); |
286 | } |
287 | |
288 | int |
289 | mac_cred_check_visible(kauth_cred_t u1, kauth_cred_t u2) |
290 | { |
291 | int error; |
292 | |
293 | #if SECURITY_MAC_CHECK_ENFORCE |
294 | /* 21167099 - only check if we allow write */ |
295 | if (!mac_proc_enforce) |
296 | return 0; |
297 | #endif |
298 | |
299 | MAC_CHECK(cred_check_visible, u1, u2); |
300 | |
301 | return (error); |
302 | } |
303 | |
304 | int |
305 | mac_proc_check_debug(proc_t curp, struct proc *proc) |
306 | { |
307 | kauth_cred_t cred; |
308 | int error; |
309 | |
310 | #if SECURITY_MAC_CHECK_ENFORCE |
311 | /* 21167099 - only check if we allow write */ |
312 | if (!mac_proc_enforce) |
313 | return 0; |
314 | #endif |
315 | if (!mac_proc_check_enforce(curp)) |
316 | return 0; |
317 | |
318 | cred = kauth_cred_proc_ref(curp); |
319 | MAC_CHECK(proc_check_debug, cred, proc); |
320 | kauth_cred_unref(&cred); |
321 | |
322 | return (error); |
323 | } |
324 | |
325 | int |
326 | mac_proc_check_fork(proc_t curp) |
327 | { |
328 | kauth_cred_t cred; |
329 | int error; |
330 | |
331 | #if SECURITY_MAC_CHECK_ENFORCE |
332 | /* 21167099 - only check if we allow write */ |
333 | if (!mac_proc_enforce) |
334 | return 0; |
335 | #endif |
336 | if (!mac_proc_check_enforce(curp)) |
337 | return 0; |
338 | |
339 | cred = kauth_cred_proc_ref(curp); |
340 | MAC_CHECK(proc_check_fork, cred, curp); |
341 | kauth_cred_unref(&cred); |
342 | |
343 | return (error); |
344 | } |
345 | |
346 | int |
347 | mac_proc_check_get_task_name(struct ucred *cred, struct proc *p) |
348 | { |
349 | int error; |
350 | |
351 | MAC_CHECK(proc_check_get_task_name, cred, p); |
352 | |
353 | return (error); |
354 | } |
355 | |
356 | int |
357 | mac_proc_check_get_task(struct ucred *cred, struct proc *p) |
358 | { |
359 | int error; |
360 | |
361 | MAC_CHECK(proc_check_get_task, cred, p); |
362 | |
363 | return (error); |
364 | } |
365 | |
366 | int |
367 | mac_proc_check_expose_task(struct ucred *cred, struct proc *p) |
368 | { |
369 | int error; |
370 | |
371 | MAC_CHECK(proc_check_expose_task, cred, p); |
372 | |
373 | return (error); |
374 | } |
375 | |
376 | int |
377 | mac_proc_check_inherit_ipc_ports(struct proc *p, struct vnode *cur_vp, off_t cur_offset, struct vnode *img_vp, off_t img_offset, struct vnode *scriptvp) |
378 | { |
379 | int error; |
380 | |
381 | MAC_CHECK(proc_check_inherit_ipc_ports, p, cur_vp, cur_offset, img_vp, img_offset, scriptvp); |
382 | |
383 | return (error); |
384 | } |
385 | |
386 | /* |
387 | * The type of maxprot in proc_check_map_anon must be equivalent to vm_prot_t |
388 | * (defined in <mach/vm_prot.h>). mac_policy.h does not include any header |
389 | * files, so cannot use the typedef itself. |
390 | */ |
391 | int |
392 | mac_proc_check_map_anon(proc_t proc, user_addr_t u_addr, |
393 | user_size_t u_size, int prot, int flags, int *maxprot) |
394 | { |
395 | kauth_cred_t cred; |
396 | int error; |
397 | |
398 | #if SECURITY_MAC_CHECK_ENFORCE |
399 | /* 21167099 - only check if we allow write */ |
400 | if (!mac_vm_enforce) |
401 | return 0; |
402 | #endif |
403 | if (!mac_proc_check_enforce(proc)) |
404 | return (0); |
405 | |
406 | cred = kauth_cred_proc_ref(proc); |
407 | MAC_CHECK(proc_check_map_anon, proc, cred, u_addr, u_size, prot, flags, maxprot); |
408 | kauth_cred_unref(&cred); |
409 | |
410 | return (error); |
411 | } |
412 | |
413 | int |
414 | mac_proc_check_mprotect(proc_t proc, |
415 | user_addr_t addr, user_size_t size, int prot) |
416 | { |
417 | kauth_cred_t cred; |
418 | int error; |
419 | |
420 | #if SECURITY_MAC_CHECK_ENFORCE |
421 | /* 21167099 - only check if we allow write */ |
422 | if (!mac_vm_enforce) |
423 | return 0; |
424 | #endif |
425 | if (!mac_proc_check_enforce(proc)) |
426 | return (0); |
427 | |
428 | cred = kauth_cred_proc_ref(proc); |
429 | MAC_CHECK(proc_check_mprotect, cred, proc, addr, size, prot); |
430 | kauth_cred_unref(&cred); |
431 | |
432 | return (error); |
433 | } |
434 | |
435 | int |
436 | mac_proc_check_run_cs_invalid(proc_t proc) |
437 | { |
438 | int error; |
439 | |
440 | #if SECURITY_MAC_CHECK_ENFORCE |
441 | /* 21167099 - only check if we allow write */ |
442 | if (!mac_vm_enforce) |
443 | return 0; |
444 | #endif |
445 | |
446 | MAC_CHECK(proc_check_run_cs_invalid, proc); |
447 | |
448 | return (error); |
449 | } |
450 | |
451 | int |
452 | mac_proc_check_sched(proc_t curp, struct proc *proc) |
453 | { |
454 | kauth_cred_t cred; |
455 | int error; |
456 | |
457 | #if SECURITY_MAC_CHECK_ENFORCE |
458 | /* 21167099 - only check if we allow write */ |
459 | if (!mac_proc_enforce) |
460 | return 0; |
461 | #endif |
462 | if (!mac_proc_check_enforce(curp)) |
463 | return 0; |
464 | |
465 | cred = kauth_cred_proc_ref(curp); |
466 | MAC_CHECK(proc_check_sched, cred, proc); |
467 | kauth_cred_unref(&cred); |
468 | |
469 | return (error); |
470 | } |
471 | |
472 | int |
473 | mac_proc_check_signal(proc_t curp, struct proc *proc, int signum) |
474 | { |
475 | kauth_cred_t cred; |
476 | int error; |
477 | |
478 | #if SECURITY_MAC_CHECK_ENFORCE |
479 | /* 21167099 - only check if we allow write */ |
480 | if (!mac_proc_enforce) |
481 | return 0; |
482 | #endif |
483 | if (!mac_proc_check_enforce(curp)) |
484 | return 0; |
485 | |
486 | cred = kauth_cred_proc_ref(curp); |
487 | MAC_CHECK(proc_check_signal, cred, proc, signum); |
488 | kauth_cred_unref(&cred); |
489 | |
490 | return (error); |
491 | } |
492 | |
493 | int |
494 | mac_proc_check_wait(proc_t curp, struct proc *proc) |
495 | { |
496 | kauth_cred_t cred; |
497 | int error; |
498 | |
499 | #if SECURITY_MAC_CHECK_ENFORCE |
500 | /* 21167099 - only check if we allow write */ |
501 | if (!mac_proc_enforce) |
502 | return 0; |
503 | #endif |
504 | if (!mac_proc_check_enforce(curp)) |
505 | return 0; |
506 | |
507 | cred = kauth_cred_proc_ref(curp); |
508 | MAC_CHECK(proc_check_wait, cred, proc); |
509 | kauth_cred_unref(&cred); |
510 | |
511 | return (error); |
512 | } |
513 | |
514 | void |
515 | mac_proc_notify_exit(struct proc *proc) |
516 | { |
517 | MAC_PERFORM(proc_notify_exit, proc); |
518 | } |
519 | |
520 | int |
521 | mac_proc_check_suspend_resume(proc_t curp, int sr) |
522 | { |
523 | kauth_cred_t cred; |
524 | int error; |
525 | |
526 | #if SECURITY_MAC_CHECK_ENFORCE |
527 | /* 21167099 - only check if we allow write */ |
528 | if (!mac_proc_enforce) |
529 | return 0; |
530 | #endif |
531 | if (!mac_proc_check_enforce(curp)) |
532 | return 0; |
533 | |
534 | cred = kauth_cred_proc_ref(curp); |
535 | MAC_CHECK(proc_check_suspend_resume, cred, curp, sr); |
536 | kauth_cred_unref(&cred); |
537 | |
538 | return (error); |
539 | } |
540 | |
541 | int |
542 | mac_proc_check_ledger(proc_t curp, proc_t proc, int ledger_op) |
543 | { |
544 | kauth_cred_t cred; |
545 | int error = 0; |
546 | |
547 | #if SECURITY_MAC_CHECK_ENFORCE |
548 | /* 21167099 - only check if we allow write */ |
549 | if (!mac_proc_enforce) |
550 | return 0; |
551 | #endif |
552 | if (!mac_proc_check_enforce(curp)) |
553 | return 0; |
554 | |
555 | cred = kauth_cred_proc_ref(curp); |
556 | MAC_CHECK(proc_check_ledger, cred, proc, ledger_op); |
557 | kauth_cred_unref(&cred); |
558 | |
559 | return (error); |
560 | } |
561 | |
562 | int |
563 | mac_proc_check_proc_info(proc_t curp, proc_t target, int callnum, int flavor) |
564 | { |
565 | kauth_cred_t cred; |
566 | int error = 0; |
567 | |
568 | #if SECURITY_MAC_CHECK_ENFORCE |
569 | /* 21167099 - only check if we allow write */ |
570 | if (!mac_proc_enforce) |
571 | return 0; |
572 | #endif |
573 | if (!mac_proc_check_enforce(curp)) |
574 | return 0; |
575 | |
576 | cred = kauth_cred_proc_ref(curp); |
577 | MAC_CHECK(proc_check_proc_info, cred, target, callnum, flavor); |
578 | kauth_cred_unref(&cred); |
579 | |
580 | return (error); |
581 | } |
582 | |
583 | int |
584 | mac_proc_check_get_cs_info(proc_t curp, proc_t target, unsigned int op) |
585 | { |
586 | kauth_cred_t cred; |
587 | int error = 0; |
588 | |
589 | #if SECURITY_MAC_CHECK_ENFORCE |
590 | /* 21167099 - only check if we allow write */ |
591 | if (!mac_proc_enforce) |
592 | return 0; |
593 | #endif |
594 | if (!mac_proc_check_enforce(curp)) |
595 | return 0; |
596 | |
597 | cred = kauth_cred_proc_ref(curp); |
598 | MAC_CHECK(proc_check_get_cs_info, cred, target, op); |
599 | kauth_cred_unref(&cred); |
600 | |
601 | return (error); |
602 | } |
603 | |
604 | int |
605 | mac_proc_check_set_cs_info(proc_t curp, proc_t target, unsigned int op) |
606 | { |
607 | kauth_cred_t cred; |
608 | int error = 0; |
609 | |
610 | #if SECURITY_MAC_CHECK_ENFORCE |
611 | /* 21167099 - only check if we allow write */ |
612 | if (!mac_proc_enforce) |
613 | return 0; |
614 | #endif |
615 | if (!mac_proc_check_enforce(curp)) |
616 | return 0; |
617 | |
618 | cred = kauth_cred_proc_ref(curp); |
619 | MAC_CHECK(proc_check_set_cs_info, cred, target, op); |
620 | kauth_cred_unref(&cred); |
621 | |
622 | return (error); |
623 | } |
624 | |
625 | |