]>
Commit | Line | Data |
---|---|---|
2d21ac55 A |
1 | /* |
2 | * Copyright (c) 2007 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 | ||
76 | #include <security/mac_internal.h> | |
77 | ||
b0d623f7 | 78 | #include <bsd/security/audit/audit.h> |
2d21ac55 A |
79 | |
80 | struct label * | |
81 | mac_cred_label_alloc(void) | |
82 | { | |
83 | struct label *label; | |
84 | ||
85 | label = mac_labelzone_alloc(MAC_WAITOK); | |
86 | if (label == NULL) | |
87 | return (NULL); | |
88 | MAC_PERFORM(cred_label_init, label); | |
89 | return (label); | |
90 | } | |
91 | ||
92 | void | |
93 | mac_cred_label_init(struct ucred *cred) | |
94 | { | |
95 | cred->cr_label = mac_cred_label_alloc(); | |
96 | } | |
97 | ||
98 | void | |
99 | mac_cred_label_free(struct label *label) | |
100 | { | |
101 | MAC_PERFORM(cred_label_destroy, label); | |
102 | mac_labelzone_free(label); | |
103 | } | |
104 | ||
105 | int | |
106 | mac_cred_label_externalize_audit(struct proc *p, struct mac *mac) | |
107 | { | |
108 | kauth_cred_t cr; | |
109 | int error; | |
110 | ||
111 | cr = kauth_cred_proc_ref(p); | |
112 | ||
113 | error = MAC_EXTERNALIZE_AUDIT(cred, cr->cr_label, | |
114 | mac->m_string, mac->m_buflen); | |
115 | ||
116 | kauth_cred_unref(&cr); | |
117 | return (error); | |
118 | } | |
119 | ||
120 | void | |
121 | mac_cred_label_destroy(kauth_cred_t cred) | |
122 | { | |
123 | ||
124 | mac_cred_label_free(cred->cr_label); | |
125 | cred->cr_label = NULL; | |
126 | } | |
127 | ||
128 | int | |
129 | mac_cred_label_externalize(struct label *label, char *elements, | |
130 | char *outbuf, size_t outbuflen, int flags __unused) | |
131 | { | |
132 | int error = 0; | |
133 | ||
134 | error = MAC_EXTERNALIZE(cred, label, elements, outbuf, outbuflen); | |
135 | ||
136 | return (error); | |
137 | } | |
138 | ||
139 | int | |
140 | mac_cred_label_internalize(struct label *label, char *string) | |
141 | { | |
142 | int error; | |
143 | ||
144 | error = MAC_INTERNALIZE(cred, label, string); | |
145 | ||
146 | return (error); | |
147 | } | |
148 | ||
149 | /* | |
150 | * By default, fork just adds a reference to the parent | |
151 | * credential. Policies may need to know about this reference | |
152 | * if they are tracking exit calls to know when to free the | |
153 | * label. | |
154 | */ | |
155 | void | |
156 | mac_cred_label_associate_fork(kauth_cred_t cred, proc_t proc) | |
157 | { | |
158 | MAC_PERFORM(cred_label_associate_fork, cred, proc); | |
159 | } | |
160 | ||
161 | /* | |
162 | * Initialize MAC label for the first kernel process, from which other | |
163 | * kernel processes and threads are spawned. | |
164 | */ | |
165 | void | |
166 | mac_cred_label_associate_kernel(kauth_cred_t cred) | |
167 | { | |
168 | ||
169 | MAC_PERFORM(cred_label_associate_kernel, cred); | |
170 | } | |
171 | ||
172 | /* | |
173 | * Initialize MAC label for the first userland process, from which other | |
174 | * userland processes and threads are spawned. | |
175 | */ | |
176 | void | |
177 | mac_cred_label_associate_user(kauth_cred_t cred) | |
178 | { | |
179 | ||
180 | MAC_PERFORM(cred_label_associate_user, cred); | |
181 | } | |
182 | ||
183 | /* | |
184 | * When a new process is created, its label must be initialized. Generally, | |
185 | * this involves inheritence from the parent process, modulo possible | |
186 | * deltas. This function allows that processing to take place. | |
187 | */ | |
188 | void | |
189 | mac_cred_label_associate(struct ucred *parent_cred, struct ucred *child_cred) | |
190 | { | |
191 | ||
192 | MAC_PERFORM(cred_label_associate, parent_cred, child_cred); | |
193 | } | |
194 | ||
195 | int | |
196 | mac_execve_enter(user_addr_t mac_p, struct image_params *imgp) | |
197 | { | |
198 | struct user_mac mac; | |
199 | struct label *execlabel; | |
200 | char *buffer; | |
201 | int error; | |
202 | size_t ulen; | |
203 | ||
204 | if (mac_p == USER_ADDR_NULL) | |
205 | return (0); | |
206 | ||
207 | if (IS_64BIT_PROCESS(current_proc())) { | |
208 | error = copyin(mac_p, &mac, sizeof(mac)); | |
209 | } else { | |
210 | struct mac mac32; | |
211 | error = copyin(mac_p, &mac32, sizeof(mac32)); | |
212 | mac.m_buflen = mac32.m_buflen; | |
213 | mac.m_string = CAST_USER_ADDR_T(mac32.m_string); | |
214 | } | |
215 | if (error) | |
216 | return (error); | |
217 | ||
218 | error = mac_check_structmac_consistent(&mac); | |
219 | if (error) | |
220 | return (error); | |
221 | ||
222 | execlabel = mac_cred_label_alloc(); | |
223 | MALLOC(buffer, char *, mac.m_buflen, M_MACTEMP, M_WAITOK); | |
224 | error = copyinstr(CAST_USER_ADDR_T(mac.m_string), buffer, mac.m_buflen, &ulen); | |
225 | if (error) | |
226 | goto out; | |
227 | AUDIT_ARG(mac_string, buffer); | |
228 | ||
229 | error = mac_cred_label_internalize(execlabel, buffer); | |
230 | out: | |
231 | if (error) { | |
232 | mac_cred_label_free(execlabel); | |
233 | execlabel = NULL; | |
234 | } | |
235 | imgp->ip_execlabelp = execlabel; | |
236 | FREE(buffer, M_MACTEMP); | |
237 | return (error); | |
238 | } | |
239 | ||
240 | /* | |
241 | * When the subject's label changes, it may require revocation of privilege | |
242 | * to mapped objects. This can't be done on-the-fly later with a unified | |
243 | * buffer cache. | |
244 | */ | |
245 | void | |
246 | mac_cred_label_update(kauth_cred_t cred, struct label *newlabel) | |
247 | { | |
248 | ||
249 | /* force label to be part of "matching" for credential */ | |
250 | cred->cr_flags |= CRF_MAC_ENFORCE; | |
251 | ||
252 | /* inform the policies of the update */ | |
253 | MAC_PERFORM(cred_label_update, cred, newlabel); | |
254 | } | |
255 | ||
256 | int | |
257 | mac_cred_check_label_update(kauth_cred_t cred, struct label *newlabel) | |
258 | { | |
259 | int error; | |
260 | ||
261 | if (!mac_proc_enforce) | |
262 | return (0); | |
263 | ||
264 | MAC_CHECK(cred_check_label_update, cred, newlabel); | |
265 | ||
266 | return (error); | |
267 | } | |
268 | ||
269 | int | |
270 | mac_cred_check_visible(kauth_cred_t u1, kauth_cred_t u2) | |
271 | { | |
272 | int error; | |
273 | ||
274 | ||
275 | ||
276 | if (!mac_proc_enforce) | |
277 | return (0); | |
278 | ||
279 | ||
280 | ||
281 | MAC_CHECK(cred_check_visible, u1, u2); | |
282 | ||
283 | ||
284 | return (error); | |
285 | } | |
286 | ||
287 | /* | |
288 | * called with process locked. | |
289 | */ | |
290 | void mac_proc_set_enforce(proc_t p, int enforce_flags) | |
291 | { | |
292 | p->p_mac_enforce |= enforce_flags; | |
293 | } | |
294 | ||
295 | int | |
296 | mac_proc_check_debug(proc_t curp, struct proc *proc) | |
297 | { | |
298 | kauth_cred_t cred; | |
299 | int error; | |
300 | ||
301 | ||
302 | ||
303 | if (!mac_proc_enforce || | |
304 | !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE)) | |
305 | return (0); | |
306 | ||
307 | cred = kauth_cred_proc_ref(curp); | |
308 | MAC_CHECK(proc_check_debug, cred, proc); | |
309 | kauth_cred_unref(&cred); | |
310 | ||
311 | return (error); | |
312 | } | |
313 | ||
314 | int | |
315 | mac_proc_check_fork(proc_t curp) | |
316 | { | |
317 | kauth_cred_t cred; | |
318 | int error; | |
319 | ||
320 | if (!mac_proc_enforce || | |
321 | !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE)) | |
322 | return (0); | |
323 | ||
324 | cred = kauth_cred_proc_ref(curp); | |
325 | MAC_CHECK(proc_check_fork, cred, curp); | |
326 | kauth_cred_unref(&cred); | |
327 | ||
328 | return (error); | |
329 | } | |
330 | ||
331 | int | |
332 | mac_proc_check_get_task_name(struct ucred *cred, struct proc *p) | |
333 | { | |
334 | int error; | |
335 | ||
336 | MAC_CHECK(proc_check_get_task_name, cred, p); | |
337 | ||
338 | return (error); | |
339 | } | |
340 | ||
341 | int | |
342 | mac_proc_check_get_task(struct ucred *cred, struct proc *p) | |
343 | { | |
344 | int error; | |
345 | ||
346 | MAC_CHECK(proc_check_get_task, cred, p); | |
347 | ||
348 | return (error); | |
349 | } | |
350 | ||
351 | int | |
352 | mac_proc_check_mprotect(proc_t proc, | |
353 | user_addr_t addr, user_size_t size, int prot) | |
354 | { | |
355 | kauth_cred_t cred; | |
356 | int error; | |
357 | ||
358 | if (!mac_vm_enforce || | |
359 | !mac_proc_check_enforce(proc, MAC_VM_ENFORCE)) | |
360 | return (0); | |
361 | ||
362 | cred = kauth_cred_proc_ref(proc); | |
363 | MAC_CHECK(proc_check_mprotect, cred, proc, addr, size, prot); | |
364 | kauth_cred_unref(&cred); | |
365 | ||
366 | return (error); | |
367 | } | |
368 | ||
593a1d5f | 369 | int |
b0d623f7 | 370 | mac_proc_check_run_cs_invalid(proc_t proc) |
593a1d5f | 371 | { |
593a1d5f A |
372 | int error; |
373 | ||
374 | if (!mac_vm_enforce) return (0); | |
375 | ||
b0d623f7 | 376 | MAC_CHECK(proc_check_run_cs_invalid, proc); |
593a1d5f A |
377 | |
378 | return (error); | |
379 | } | |
380 | ||
2d21ac55 A |
381 | int |
382 | mac_proc_check_sched(proc_t curp, struct proc *proc) | |
383 | { | |
384 | kauth_cred_t cred; | |
385 | int error; | |
386 | ||
387 | ||
388 | ||
389 | if (!mac_proc_enforce || | |
390 | !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE)) | |
391 | return (0); | |
392 | ||
393 | cred = kauth_cred_proc_ref(curp); | |
394 | MAC_CHECK(proc_check_sched, cred, proc); | |
395 | kauth_cred_unref(&cred); | |
396 | ||
397 | return (error); | |
398 | } | |
399 | ||
400 | int | |
401 | mac_proc_check_signal(proc_t curp, struct proc *proc, int signum) | |
402 | { | |
403 | kauth_cred_t cred; | |
404 | int error; | |
405 | ||
406 | ||
407 | ||
408 | if (!mac_proc_enforce || | |
409 | !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE)) | |
410 | return (0); | |
411 | ||
412 | cred = kauth_cred_proc_ref(curp); | |
413 | MAC_CHECK(proc_check_signal, cred, proc, signum); | |
414 | kauth_cred_unref(&cred); | |
415 | ||
416 | return (error); | |
417 | } | |
418 | ||
419 | int | |
420 | mac_proc_check_wait(proc_t curp, struct proc *proc) | |
421 | { | |
422 | kauth_cred_t cred; | |
423 | int error; | |
424 | ||
425 | ||
426 | ||
427 | if (!mac_proc_enforce || | |
428 | !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE)) | |
429 | return (0); | |
430 | ||
431 | cred = kauth_cred_proc_ref(curp); | |
432 | MAC_CHECK(proc_check_wait, cred, proc); | |
433 | kauth_cred_unref(&cred); | |
434 | ||
435 | return (error); | |
436 | } | |
437 | ||
438 | #if CONFIG_LCTX | |
439 | /* | |
440 | * Login Context | |
441 | */ | |
442 | ||
443 | int | |
444 | mac_proc_check_setlcid (struct proc *p0, struct proc *p, | |
445 | pid_t pid, pid_t lcid) | |
446 | { | |
447 | int error; | |
448 | ||
449 | if (!mac_proc_enforce || | |
450 | !mac_proc_check_enforce(p0, MAC_PROC_ENFORCE)) | |
451 | return (0); | |
452 | ||
453 | MAC_CHECK(proc_check_setlcid, p0, p, pid, lcid); | |
454 | return (error); | |
455 | } | |
456 | ||
457 | int | |
458 | mac_proc_check_getlcid (struct proc *p0, struct proc *p, pid_t pid) | |
459 | { | |
460 | int error; | |
461 | ||
462 | if (!mac_proc_enforce || | |
463 | !mac_proc_check_enforce(p0, MAC_PROC_ENFORCE)) | |
464 | return (0); | |
465 | ||
466 | MAC_CHECK(proc_check_getlcid, p0, p, pid); | |
467 | return (error); | |
468 | } | |
469 | ||
470 | void | |
471 | mac_lctx_notify_create (struct proc *p, struct lctx *l) | |
472 | { | |
473 | MAC_PERFORM(lctx_notify_create, p, l); | |
474 | } | |
475 | ||
476 | void | |
477 | mac_lctx_notify_join (struct proc *p, struct lctx *l) | |
478 | { | |
479 | MAC_PERFORM(lctx_notify_join, p, l); | |
480 | } | |
481 | ||
482 | void | |
483 | mac_lctx_notify_leave (struct proc *p, struct lctx *l) | |
484 | { | |
485 | MAC_PERFORM(lctx_notify_leave, p, l); | |
486 | } | |
487 | ||
488 | struct label * | |
489 | mac_lctx_label_alloc(void) | |
490 | { | |
491 | struct label *label; | |
492 | ||
493 | label = mac_labelzone_alloc(MAC_WAITOK); | |
494 | if (label == NULL) | |
495 | return (NULL); | |
496 | MAC_PERFORM(lctx_label_init, label); | |
497 | return (label); | |
498 | } | |
499 | ||
500 | void | |
501 | mac_lctx_label_free(struct label *label) | |
502 | { | |
503 | ||
504 | MAC_PERFORM(lctx_label_destroy, label); | |
505 | mac_labelzone_free(label); | |
506 | } | |
507 | ||
508 | int | |
509 | mac_lctx_label_externalize(struct label *label, char *elements, | |
510 | char *outbuf, size_t outbuflen) | |
511 | { | |
512 | int error; | |
513 | ||
514 | error = MAC_EXTERNALIZE(lctx, label, elements, outbuf, outbuflen); | |
515 | ||
516 | return (error); | |
517 | } | |
518 | ||
519 | int | |
520 | mac_lctx_label_internalize(struct label *label, char *string) | |
521 | { | |
522 | int error; | |
523 | ||
524 | error = MAC_INTERNALIZE(lctx, label, string); | |
525 | ||
526 | return (error); | |
527 | } | |
528 | ||
529 | void | |
530 | mac_lctx_label_update(struct lctx *l, struct label *newlabel) | |
531 | { | |
532 | ||
533 | MAC_PERFORM(lctx_label_update, l, newlabel); | |
534 | } | |
535 | ||
536 | int | |
537 | mac_lctx_check_label_update(struct lctx *l, struct label *newlabel) | |
538 | { | |
539 | int error; | |
540 | ||
541 | MAC_CHECK(lctx_check_label_update, l, newlabel); | |
542 | ||
543 | return (error); | |
544 | } | |
545 | #endif /* LCTX */ | |
546 | ||
547 | ||
548 | void | |
549 | mac_thread_userret(int code, int error, struct thread *thread) | |
550 | { | |
551 | ||
552 | if (mac_late) | |
553 | MAC_PERFORM(thread_userret, code, error, thread); | |
554 | } |