]> git.saurik.com Git - apple/xnu.git/blame - security/mac_process.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / security / mac_process.c
CommitLineData
2d21ac55 1/*
6d2010ae 2 * Copyright (c) 2007-2010 Apple Inc. All rights reserved.
2d21ac55
A
3 *
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
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.
0a7de745 25 *
2d21ac55
A
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>
316670eb 75#include <mach/mach_types.h>
3e170ce0 76#include <kern/task.h>
2d21ac55
A
77
78#include <security/mac_internal.h>
316670eb 79#include <security/mac_mach_internal.h>
2d21ac55 80
b0d623f7 81#include <bsd/security/audit/audit.h>
2d21ac55
A
82
83struct label *
84mac_cred_label_alloc(void)
85{
86 struct label *label;
87
88 label = mac_labelzone_alloc(MAC_WAITOK);
0a7de745
A
89 if (label == NULL) {
90 return NULL;
91 }
2d21ac55 92 MAC_PERFORM(cred_label_init, label);
0a7de745 93 return label;
2d21ac55
A
94}
95
96void
97mac_cred_label_init(struct ucred *cred)
98{
99 cred->cr_label = mac_cred_label_alloc();
100}
101
102void
103mac_cred_label_free(struct label *label)
104{
105 MAC_PERFORM(cred_label_destroy, label);
106 mac_labelzone_free(label);
107}
108
316670eb
A
109int
110mac_cred_label_compare(struct label *a, struct label *b)
111{
0a7de745 112 return bcmp(a, b, sizeof(*a)) == 0;
316670eb
A
113}
114
2d21ac55
A
115int
116mac_cred_label_externalize_audit(struct proc *p, struct mac *mac)
117{
118 kauth_cred_t cr;
119 int error;
120
121 cr = kauth_cred_proc_ref(p);
122
123 error = MAC_EXTERNALIZE_AUDIT(cred, cr->cr_label,
124 mac->m_string, mac->m_buflen);
125
126 kauth_cred_unref(&cr);
0a7de745 127 return error;
2d21ac55
A
128}
129
130void
131mac_cred_label_destroy(kauth_cred_t cred)
132{
2d21ac55
A
133 mac_cred_label_free(cred->cr_label);
134 cred->cr_label = NULL;
135}
136
137int
138mac_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
0a7de745 145 return error;
2d21ac55
A
146}
147
148int
149mac_cred_label_internalize(struct label *label, char *string)
150{
151 int error;
152
153 error = MAC_INTERNALIZE(cred, label, string);
154
0a7de745 155 return error;
2d21ac55
A
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 */
164void
165mac_cred_label_associate_fork(kauth_cred_t cred, proc_t proc)
166{
167 MAC_PERFORM(cred_label_associate_fork, cred, proc);
168}
0a7de745 169
2d21ac55
A
170/*
171 * Initialize MAC label for the first kernel process, from which other
172 * kernel processes and threads are spawned.
173 */
174void
175mac_cred_label_associate_kernel(kauth_cred_t cred)
176{
2d21ac55
A
177 MAC_PERFORM(cred_label_associate_kernel, cred);
178}
179
180/*
181 * Initialize MAC label for the first userland process, from which other
182 * userland processes and threads are spawned.
183 */
184void
185mac_cred_label_associate_user(kauth_cred_t cred)
186{
2d21ac55
A
187 MAC_PERFORM(cred_label_associate_user, cred);
188}
189
190/*
191 * When a new process is created, its label must be initialized. Generally,
192 * this involves inheritence from the parent process, modulo possible
193 * deltas. This function allows that processing to take place.
194 */
195void
196mac_cred_label_associate(struct ucred *parent_cred, struct ucred *child_cred)
197{
2d21ac55
A
198 MAC_PERFORM(cred_label_associate, parent_cred, child_cred);
199}
200
201int
202mac_execve_enter(user_addr_t mac_p, struct image_params *imgp)
203{
204 struct user_mac mac;
205 struct label *execlabel;
206 char *buffer;
207 int error;
208 size_t ulen;
209
0a7de745
A
210 if (mac_p == USER_ADDR_NULL) {
211 return 0;
212 }
2d21ac55
A
213
214 if (IS_64BIT_PROCESS(current_proc())) {
6d2010ae
A
215 struct user64_mac mac64;
216 error = copyin(mac_p, &mac64, sizeof(mac64));
217 mac.m_buflen = mac64.m_buflen;
218 mac.m_string = mac64.m_string;
2d21ac55 219 } else {
6d2010ae 220 struct user32_mac mac32;
2d21ac55
A
221 error = copyin(mac_p, &mac32, sizeof(mac32));
222 mac.m_buflen = mac32.m_buflen;
6d2010ae 223 mac.m_string = mac32.m_string;
2d21ac55 224 }
0a7de745
A
225 if (error) {
226 return error;
227 }
2d21ac55
A
228
229 error = mac_check_structmac_consistent(&mac);
0a7de745
A
230 if (error) {
231 return error;
232 }
2d21ac55
A
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);
0a7de745 237 if (error) {
2d21ac55 238 goto out;
0a7de745 239 }
2d21ac55
A
240 AUDIT_ARG(mac_string, buffer);
241
242 error = mac_cred_label_internalize(execlabel, buffer);
243out:
244 if (error) {
245 mac_cred_label_free(execlabel);
246 execlabel = NULL;
247 }
248 imgp->ip_execlabelp = execlabel;
249 FREE(buffer, M_MACTEMP);
0a7de745 250 return error;
2d21ac55
A
251}
252
253/*
254 * When the subject's label changes, it may require revocation of privilege
255 * to mapped objects. This can't be done on-the-fly later with a unified
256 * buffer cache.
6d2010ae
A
257 *
258 * XXX: CRF_MAC_ENFORCE should be in a kauth_cred_t field, rather
259 * XXX: than a posix_cred_t field.
2d21ac55
A
260 */
261void
262mac_cred_label_update(kauth_cred_t cred, struct label *newlabel)
263{
6d2010ae 264 posix_cred_t pcred = posix_cred_get(cred);
2d21ac55
A
265
266 /* force label to be part of "matching" for credential */
6d2010ae 267 pcred->cr_flags |= CRF_MAC_ENFORCE;
2d21ac55
A
268
269 /* inform the policies of the update */
270 MAC_PERFORM(cred_label_update, cred, newlabel);
271}
272
273int
274mac_cred_check_label_update(kauth_cred_t cred, struct label *newlabel)
275{
276 int error;
277
3e170ce0 278#if SECURITY_MAC_CHECK_ENFORCE
0a7de745
A
279 /* 21167099 - only check if we allow write */
280 if (!mac_proc_enforce) {
281 return 0;
282 }
3e170ce0 283#endif
2d21ac55
A
284
285 MAC_CHECK(cred_check_label_update, cred, newlabel);
286
0a7de745 287 return error;
2d21ac55
A
288}
289
290int
291mac_cred_check_visible(kauth_cred_t u1, kauth_cred_t u2)
292{
293 int error;
294
3e170ce0 295#if SECURITY_MAC_CHECK_ENFORCE
39037602 296 /* 21167099 - only check if we allow write */
0a7de745 297 if (!mac_proc_enforce) {
39037602 298 return 0;
0a7de745 299 }
3e170ce0 300#endif
2d21ac55
A
301
302 MAC_CHECK(cred_check_visible, u1, u2);
303
0a7de745 304 return error;
2d21ac55
A
305}
306
2d21ac55
A
307int
308mac_proc_check_debug(proc_t curp, struct proc *proc)
309{
310 kauth_cred_t cred;
311 int error;
312
3e170ce0 313#if SECURITY_MAC_CHECK_ENFORCE
39037602 314 /* 21167099 - only check if we allow write */
0a7de745 315 if (!mac_proc_enforce) {
39037602 316 return 0;
0a7de745 317 }
3e170ce0 318#endif
0a7de745 319 if (!mac_proc_check_enforce(curp)) {
39037602 320 return 0;
0a7de745 321 }
2d21ac55
A
322
323 cred = kauth_cred_proc_ref(curp);
324 MAC_CHECK(proc_check_debug, cred, proc);
325 kauth_cred_unref(&cred);
326
0a7de745 327 return error;
2d21ac55
A
328}
329
330int
331mac_proc_check_fork(proc_t curp)
332{
333 kauth_cred_t cred;
334 int error;
335
3e170ce0 336#if SECURITY_MAC_CHECK_ENFORCE
39037602 337 /* 21167099 - only check if we allow write */
0a7de745 338 if (!mac_proc_enforce) {
39037602 339 return 0;
0a7de745 340 }
3e170ce0 341#endif
0a7de745 342 if (!mac_proc_check_enforce(curp)) {
39037602 343 return 0;
0a7de745 344 }
2d21ac55
A
345
346 cred = kauth_cred_proc_ref(curp);
347 MAC_CHECK(proc_check_fork, cred, curp);
348 kauth_cred_unref(&cred);
349
0a7de745 350 return error;
2d21ac55
A
351}
352
353int
354mac_proc_check_get_task_name(struct ucred *cred, struct proc *p)
355{
356 int error;
357
358 MAC_CHECK(proc_check_get_task_name, cred, p);
359
0a7de745 360 return error;
2d21ac55
A
361}
362
363int
364mac_proc_check_get_task(struct ucred *cred, struct proc *p)
365{
366 int error;
367
368 MAC_CHECK(proc_check_get_task, cred, p);
369
0a7de745 370 return error;
2d21ac55
A
371}
372
3e170ce0
A
373int
374mac_proc_check_expose_task(struct ucred *cred, struct proc *p)
375{
376 int error;
377
378 MAC_CHECK(proc_check_expose_task, cred, p);
379
0a7de745 380 return error;
3e170ce0
A
381}
382
fe8ab488
A
383int
384mac_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)
385{
386 int error;
387
388 MAC_CHECK(proc_check_inherit_ipc_ports, p, cur_vp, cur_offset, img_vp, img_offset, scriptvp);
389
0a7de745 390 return error;
fe8ab488
A
391}
392
6d2010ae
A
393/*
394 * The type of maxprot in proc_check_map_anon must be equivalent to vm_prot_t
395 * (defined in <mach/vm_prot.h>). mac_policy.h does not include any header
396 * files, so cannot use the typedef itself.
397 */
398int
399mac_proc_check_map_anon(proc_t proc, user_addr_t u_addr,
400 user_size_t u_size, int prot, int flags, int *maxprot)
401{
402 kauth_cred_t cred;
403 int error;
404
3e170ce0 405#if SECURITY_MAC_CHECK_ENFORCE
39037602 406 /* 21167099 - only check if we allow write */
0a7de745 407 if (!mac_vm_enforce) {
39037602 408 return 0;
0a7de745 409 }
3e170ce0 410#endif
0a7de745
A
411 if (!mac_proc_check_enforce(proc)) {
412 return 0;
413 }
6d2010ae
A
414
415 cred = kauth_cred_proc_ref(proc);
416 MAC_CHECK(proc_check_map_anon, proc, cred, u_addr, u_size, prot, flags, maxprot);
417 kauth_cred_unref(&cred);
418
0a7de745 419 return error;
6d2010ae
A
420}
421
2d21ac55
A
422int
423mac_proc_check_mprotect(proc_t proc,
424 user_addr_t addr, user_size_t size, int prot)
425{
426 kauth_cred_t cred;
427 int error;
428
3e170ce0 429#if SECURITY_MAC_CHECK_ENFORCE
39037602 430 /* 21167099 - only check if we allow write */
0a7de745 431 if (!mac_vm_enforce) {
39037602 432 return 0;
0a7de745 433 }
3e170ce0 434#endif
0a7de745
A
435 if (!mac_proc_check_enforce(proc)) {
436 return 0;
437 }
2d21ac55
A
438
439 cred = kauth_cred_proc_ref(proc);
440 MAC_CHECK(proc_check_mprotect, cred, proc, addr, size, prot);
441 kauth_cred_unref(&cred);
442
0a7de745 443 return error;
2d21ac55
A
444}
445
593a1d5f 446int
b0d623f7 447mac_proc_check_run_cs_invalid(proc_t proc)
593a1d5f 448{
593a1d5f 449 int error;
0a7de745 450
3e170ce0 451#if SECURITY_MAC_CHECK_ENFORCE
0a7de745
A
452 /* 21167099 - only check if we allow write */
453 if (!mac_vm_enforce) {
454 return 0;
455 }
3e170ce0 456#endif
0a7de745 457
b0d623f7 458 MAC_CHECK(proc_check_run_cs_invalid, proc);
0a7de745
A
459
460 return error;
593a1d5f 461}
0a7de745 462
2d21ac55
A
463int
464mac_proc_check_sched(proc_t curp, struct proc *proc)
465{
466 kauth_cred_t cred;
467 int error;
468
3e170ce0 469#if SECURITY_MAC_CHECK_ENFORCE
39037602 470 /* 21167099 - only check if we allow write */
0a7de745 471 if (!mac_proc_enforce) {
39037602 472 return 0;
0a7de745 473 }
3e170ce0 474#endif
0a7de745 475 if (!mac_proc_check_enforce(curp)) {
39037602 476 return 0;
0a7de745 477 }
2d21ac55
A
478
479 cred = kauth_cred_proc_ref(curp);
480 MAC_CHECK(proc_check_sched, cred, proc);
481 kauth_cred_unref(&cred);
482
0a7de745 483 return error;
2d21ac55
A
484}
485
486int
487mac_proc_check_signal(proc_t curp, struct proc *proc, int signum)
488{
489 kauth_cred_t cred;
490 int error;
491
3e170ce0 492#if SECURITY_MAC_CHECK_ENFORCE
39037602 493 /* 21167099 - only check if we allow write */
0a7de745 494 if (!mac_proc_enforce) {
39037602 495 return 0;
0a7de745 496 }
3e170ce0 497#endif
0a7de745 498 if (!mac_proc_check_enforce(curp)) {
39037602 499 return 0;
0a7de745 500 }
2d21ac55
A
501
502 cred = kauth_cred_proc_ref(curp);
503 MAC_CHECK(proc_check_signal, cred, proc, signum);
504 kauth_cred_unref(&cred);
505
0a7de745 506 return error;
2d21ac55
A
507}
508
509int
510mac_proc_check_wait(proc_t curp, struct proc *proc)
511{
512 kauth_cred_t cred;
513 int error;
514
3e170ce0 515#if SECURITY_MAC_CHECK_ENFORCE
39037602 516 /* 21167099 - only check if we allow write */
0a7de745 517 if (!mac_proc_enforce) {
39037602 518 return 0;
0a7de745 519 }
3e170ce0 520#endif
0a7de745 521 if (!mac_proc_check_enforce(curp)) {
39037602 522 return 0;
0a7de745 523 }
2d21ac55
A
524
525 cred = kauth_cred_proc_ref(curp);
526 MAC_CHECK(proc_check_wait, cred, proc);
527 kauth_cred_unref(&cred);
528
0a7de745 529 return error;
2d21ac55
A
530}
531
5ba3f43e
A
532void
533mac_proc_notify_exit(struct proc *proc)
534{
535 MAC_PERFORM(proc_notify_exit, proc);
536}
537
d1ecb069
A
538int
539mac_proc_check_suspend_resume(proc_t curp, int sr)
540{
541 kauth_cred_t cred;
542 int error;
543
3e170ce0 544#if SECURITY_MAC_CHECK_ENFORCE
39037602 545 /* 21167099 - only check if we allow write */
0a7de745 546 if (!mac_proc_enforce) {
39037602 547 return 0;
0a7de745 548 }
3e170ce0 549#endif
0a7de745 550 if (!mac_proc_check_enforce(curp)) {
39037602 551 return 0;
0a7de745 552 }
d1ecb069
A
553
554 cred = kauth_cred_proc_ref(curp);
555 MAC_CHECK(proc_check_suspend_resume, cred, curp, sr);
556 kauth_cred_unref(&cred);
557
0a7de745 558 return error;
d1ecb069 559}
316670eb
A
560
561int
562mac_proc_check_ledger(proc_t curp, proc_t proc, int ledger_op)
563{
564 kauth_cred_t cred;
565 int error = 0;
566
3e170ce0 567#if SECURITY_MAC_CHECK_ENFORCE
39037602 568 /* 21167099 - only check if we allow write */
0a7de745 569 if (!mac_proc_enforce) {
39037602 570 return 0;
0a7de745 571 }
3e170ce0 572#endif
0a7de745 573 if (!mac_proc_check_enforce(curp)) {
39037602 574 return 0;
0a7de745 575 }
316670eb
A
576
577 cred = kauth_cred_proc_ref(curp);
578 MAC_CHECK(proc_check_ledger, cred, proc, ledger_op);
579 kauth_cred_unref(&cred);
580
0a7de745 581 return error;
316670eb
A
582}
583
39236c6e
A
584int
585mac_proc_check_proc_info(proc_t curp, proc_t target, int callnum, int flavor)
586{
587 kauth_cred_t cred;
588 int error = 0;
589
3e170ce0 590#if SECURITY_MAC_CHECK_ENFORCE
39037602 591 /* 21167099 - only check if we allow write */
0a7de745 592 if (!mac_proc_enforce) {
39037602 593 return 0;
0a7de745 594 }
3e170ce0 595#endif
0a7de745 596 if (!mac_proc_check_enforce(curp)) {
39037602 597 return 0;
0a7de745 598 }
39236c6e
A
599
600 cred = kauth_cred_proc_ref(curp);
601 MAC_CHECK(proc_check_proc_info, cred, target, callnum, flavor);
602 kauth_cred_unref(&cred);
603
0a7de745 604 return error;
39236c6e
A
605}
606
7e41aa88
A
607int
608mac_proc_check_get_cs_info(proc_t curp, proc_t target, unsigned int op)
609{
610 kauth_cred_t cred;
611 int error = 0;
612
613#if SECURITY_MAC_CHECK_ENFORCE
614 /* 21167099 - only check if we allow write */
0a7de745 615 if (!mac_proc_enforce) {
7e41aa88 616 return 0;
0a7de745 617 }
7e41aa88 618#endif
0a7de745 619 if (!mac_proc_check_enforce(curp)) {
7e41aa88 620 return 0;
0a7de745 621 }
7e41aa88
A
622
623 cred = kauth_cred_proc_ref(curp);
624 MAC_CHECK(proc_check_get_cs_info, cred, target, op);
625 kauth_cred_unref(&cred);
626
0a7de745 627 return error;
7e41aa88
A
628}
629
630int
631mac_proc_check_set_cs_info(proc_t curp, proc_t target, unsigned int op)
632{
633 kauth_cred_t cred;
634 int error = 0;
635
636#if SECURITY_MAC_CHECK_ENFORCE
637 /* 21167099 - only check if we allow write */
0a7de745 638 if (!mac_proc_enforce) {
7e41aa88 639 return 0;
0a7de745 640 }
7e41aa88 641#endif
0a7de745 642 if (!mac_proc_check_enforce(curp)) {
7e41aa88 643 return 0;
0a7de745 644 }
7e41aa88
A
645
646 cred = kauth_cred_proc_ref(curp);
647 MAC_CHECK(proc_check_set_cs_info, cred, target, op);
648 kauth_cred_unref(&cred);
649
0a7de745 650 return error;
7e41aa88 651}