]> git.saurik.com Git - apple/xnu.git/blob - security/mac_internal.h
6e8ae3d2a803af504fc42c2b7c3ede4ec55449a5
[apple/xnu.git] / security / mac_internal.h
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 * Copyright (c) 2005 SPARTA, Inc.
34 * All rights reserved.
35 *
36 * This software was developed by Robert Watson and Ilmar Habibulin for the
37 * TrustedBSD Project.
38 *
39 * This software was developed for the FreeBSD Project in part by Network
40 * Associates Laboratories, the Security Research Division of Network
41 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
42 * as part of the DARPA CHATS research program.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 */
66
67 #ifndef _SECURITY_MAC_INTERNAL_H_
68 #define _SECURITY_MAC_INTERNAL_H_
69
70 #include <string.h>
71 #include <sys/param.h>
72 #include <sys/queue.h>
73 #include <security/mac.h>
74 #include <security/mac_framework.h>
75 #include <security/mac_policy.h>
76 #include <security/mac_data.h>
77 #include <sys/sysctl.h>
78 #include <kern/wait_queue.h>
79 #include <kern/locks.h>
80 #include <sys/kernel.h>
81 #include <sys/lock.h>
82 #include <sys/malloc.h>
83 #include <sys/sbuf.h>
84 #include <sys/proc.h>
85 #include <sys/systm.h>
86 #include <sys/socket.h>
87 #include <sys/socketvar.h>
88 #include <sys/vnode.h>
89
90 /*
91 * MAC Framework sysctl namespace.
92 */
93
94 SYSCTL_DECL(_security);
95 SYSCTL_DECL(_security_mac);
96
97 extern int mac_late;
98
99 struct mac_policy_list_element {
100 struct mac_policy_conf *mpc;
101 };
102
103 struct mac_policy_list {
104 u_int numloaded;
105 u_int max;
106 u_int maxindex;
107 u_int staticmax;
108 u_int chunks;
109 u_int freehint;
110 struct mac_policy_list_element *entries;
111 };
112
113 typedef struct mac_policy_list mac_policy_list_t;
114
115
116 /*
117 * Policy that has registered with the framework for a specific
118 * label namespace name.
119 */
120 struct mac_label_listener {
121 mac_policy_handle_t mll_handle;
122 LIST_ENTRY(mac_label_listener) mll_list;
123 };
124
125 LIST_HEAD(mac_label_listeners_t, mac_label_listener);
126
127 /*
128 * Type of list used to manage label namespace names.
129 */
130 struct mac_label_element {
131 char mle_name[MAC_MAX_LABEL_ELEMENT_NAME];
132 struct mac_label_listeners_t mle_listeners;
133 LIST_ENTRY(mac_label_element) mle_list;
134 };
135
136 LIST_HEAD(mac_label_element_list_t, mac_label_element);
137
138 /*
139 * Journal operations
140 */
141
142 #define MLJ_TYPE_PORT 1
143 #define MLJ_TYPE_TASK 2
144
145 #define MLJ_PORT_OP_INIT 0x0001
146 #define MLJ_PORT_OP_CREATE_K 0x0002
147 #define MLJ_PORT_OP_CREATE 0x0004
148 #define MLJ_PORT_OP_UPDATE 0x0008
149
150 #define MLJ_TASK_OP_INIT 0x0001
151 #define MLJ_TASK_OP_CREATE_K 0x0002
152
153 struct mac_label_journal {
154 struct label *l;
155 int type;
156 int ops;
157
158 int kotype; /* Kernel Port */
159
160 TAILQ_ENTRY(mac_label_journal) link;
161 };
162 TAILQ_HEAD(mac_label_journal_list_t, mac_label_journal);
163
164 int mac_label_journal_add (struct label *, int);
165 int mac_label_journal_remove(struct label *);
166 struct mac_label_journal *
167 mac_label_journal_find (struct label *);
168 int mac_label_journal (struct label *, int, ...);
169 void mac_label_journal_replay(void);
170
171
172 /*
173 * MAC Framework global variables.
174 */
175
176 extern struct mac_label_element_list_t mac_label_element_list;
177 extern struct mac_label_element_list_t mac_static_label_element_list;
178 extern struct mac_label_journal_list_t mac_label_journal_list;
179
180 extern struct mac_policy_list mac_policy_list;
181
182 /*
183 * global flags to control whether a MACF subsystem is configured
184 * at all in the system.
185 */
186 extern unsigned int mac_device_enforce;
187 extern unsigned int mac_file_enforce;
188 extern unsigned int mac_iokit_enforce;
189 extern unsigned int mac_pipe_enforce;
190 extern unsigned int mac_posixsem_enforce;
191 extern unsigned int mac_posixshm_enforce;
192 extern unsigned int mac_proc_enforce;
193 extern unsigned int mac_socket_enforce;
194 extern unsigned int mac_system_enforce;
195 extern unsigned int mac_sysvmsg_enforce;
196 extern unsigned int mac_sysvsem_enforce;
197 extern unsigned int mac_sysvshm_enforce;
198 extern unsigned int mac_vm_enforce;
199 extern unsigned int mac_vnode_enforce;
200
201 #if CONFIG_MACF_MACH
202 extern unsigned int mac_port_enforce;
203 extern unsigned int mac_task_enforce;
204 #endif
205
206 #if CONFIG_MACF_NET
207 extern unsigned int mac_label_mbufs;
208 #endif
209
210 extern unsigned int mac_label_vnodes;
211
212 static int mac_proc_check_enforce(proc_t p, int enforce_flag);
213
214 static __inline__ int mac_proc_check_enforce(proc_t p, int enforce_flags)
215 {
216 #if CONFIG_MACF
217 return ((p->p_mac_enforce & enforce_flags) != 0);
218 #else
219 return 0;
220 #endif
221 }
222
223 static int mac_context_check_enforce(vfs_context_t ctx, int enforce_flags);
224 static void mac_context_set_enforce(vfs_context_t ctx, int enforce_flags);
225
226 static __inline__ int mac_context_check_enforce(vfs_context_t ctx, int enforce_flags)
227 {
228 proc_t proc = vfs_context_proc(ctx);
229
230 if (proc == NULL)
231 return 0;
232
233 return (mac_proc_check_enforce(proc, enforce_flags));
234 }
235
236 static __inline__ void mac_context_set_enforce(vfs_context_t ctx, int enforce_flags)
237 {
238 proc_t proc = vfs_context_proc(ctx);
239
240 if (proc == NULL)
241 return;
242
243 mac_proc_set_enforce(proc, enforce_flags);
244 }
245
246
247 /*
248 * MAC Framework infrastructure functions.
249 */
250
251 int mac_error_select(int error1, int error2);
252
253 void mac_policy_list_busy(void);
254 int mac_policy_list_conditional_busy(void);
255 void mac_policy_list_unbusy(void);
256
257 void mac_labelzone_init(void);
258 struct label *mac_labelzone_alloc(int flags);
259 void mac_labelzone_free(struct label *label);
260
261 void mac_label_init(struct label *label);
262 void mac_label_destroy(struct label *label);
263 #if KERNEL
264 int mac_check_structmac_consistent(struct user_mac *mac);
265 #else
266 int mac_check_structmac_consistent(struct mac *mac);
267 #endif
268
269 int mac_cred_label_externalize(struct label *, char *e, char *out, size_t olen, int flags);
270 int mac_lctx_label_externalize(struct label *, char *e, char *out, size_t olen);
271 #if CONFIG_MACF_SOCKET
272 int mac_socket_label_externalize(struct label *, char *e, char *out, size_t olen);
273 #endif /* CONFIG_MACF_SOCKET */
274 int mac_vnode_label_externalize(struct label *, char *e, char *out, size_t olen, int flags);
275 int mac_pipe_label_externalize(struct label *label, char *elements,
276 char *outbuf, size_t outbuflen);
277
278 int mac_cred_label_internalize(struct label *label, char *string);
279 int mac_lctx_label_internalize(struct label *label, char *string);
280 #if CONFIG_MACF_SOCKET
281 int mac_socket_label_internalize(struct label *label, char *string);
282 #endif /* CONFIG_MACF_SOCKET */
283 int mac_vnode_label_internalize(struct label *label, char *string);
284 int mac_pipe_label_internalize(struct label *label, char *string);
285
286 #if CONFIG_MACF_SOCKET
287 /* internal socket label manipulation functions */
288 struct label *mac_socket_label_alloc(int flags);
289 void mac_socket_label_free(struct label *l);
290 int mac_socket_label_update(struct ucred *cred, struct socket *so, struct label *l);
291 #endif /* MAC_SOCKET */
292
293 #if CONFIG_MACF_NET
294 struct label *mac_mbuf_to_label(struct mbuf *m);
295 #else
296 #define mac_mbuf_to_label(m) (NULL)
297 #endif
298
299 /*
300 * MAC_CHECK performs the designated check by walking the policy
301 * module list and checking with each as to how it feels about the
302 * request. Note that it returns its value via 'error' in the scope
303 * of the caller.
304 */
305 #define MAC_CHECK(check, args...) do { \
306 struct mac_policy_conf *mpc; \
307 u_int i; \
308 \
309 error = 0; \
310 for (i = 0; i < mac_policy_list.staticmax; i++) { \
311 mpc = mac_policy_list.entries[i].mpc; \
312 if (mpc == NULL) \
313 continue; \
314 \
315 if (mpc->mpc_ops->mpo_ ## check != NULL) \
316 error = mac_error_select( \
317 mpc->mpc_ops->mpo_ ## check (args), \
318 error); \
319 } \
320 if (mac_policy_list_conditional_busy() != 0) { \
321 for (; i <= mac_policy_list.maxindex; i++) { \
322 mpc = mac_policy_list.entries[i].mpc; \
323 if (mpc == NULL) \
324 continue; \
325 \
326 if (mpc->mpc_ops->mpo_ ## check != NULL) \
327 error = mac_error_select( \
328 mpc->mpc_ops->mpo_ ## check (args), \
329 error); \
330 } \
331 mac_policy_list_unbusy(); \
332 } \
333 } while (0)
334
335 /*
336 * MAC_GRANT performs the designated check by walking the policy
337 * module list and checking with each as to how it feels about the
338 * request. Unlike MAC_CHECK, it grants if any policies return '0',
339 * and otherwise returns EPERM. Note that it returns its value via
340 * 'error' in the scope of the caller.
341 */
342 #define MAC_GRANT(check, args...) do { \
343 struct mac_policy_conf *mpc; \
344 u_int i; \
345 \
346 error = EPERM; \
347 for (i = 0; i < mac_policy_list.staticmax; i++) { \
348 mpc = mac_policy_list.entries[i].mpc; \
349 if (mpc == NULL) \
350 continue; \
351 \
352 if (mpc->mpc_ops->mpo_ ## check != NULL) { \
353 if (mpc->mpc_ops->mpo_ ## check (args) == 0) \
354 error = 0; \
355 } \
356 } \
357 if (mac_policy_list_conditional_busy() != 0) { \
358 for (; i <= mac_policy_list.maxindex; i++) { \
359 mpc = mac_policy_list.entries[i].mpc; \
360 if (mpc == NULL) \
361 continue; \
362 \
363 if (mpc->mpc_ops->mpo_ ## check != NULL) { \
364 if (mpc->mpc_ops->mpo_ ## check (args) \
365 == 0) \
366 error = 0; \
367 } \
368 } \
369 mac_policy_list_unbusy(); \
370 } \
371 } while (0)
372
373 /*
374 * MAC_BOOLEAN performs the designated boolean composition by walking
375 * the module list, invoking each instance of the operation, and
376 * combining the results using the passed C operator. Note that it
377 * returns its value via 'result' in the scope of the caller, which
378 * should be initialized by the caller in a meaningful way to get
379 * a meaningful result.
380 */
381 #define MAC_BOOLEAN(operation, composition, args...) do { \
382 struct mac_policy_conf *mpc; \
383 u_int i; \
384 \
385 for (i = 0; i < mac_policy_list.staticmax; i++) { \
386 mpc = mac_policy_list.entries[i].mpc; \
387 if (mpc == NULL) \
388 continue; \
389 \
390 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
391 result = result composition \
392 mpc->mpc_ops->mpo_ ## operation \
393 (args); \
394 } \
395 if (mac_policy_list_conditional_busy() != 0) { \
396 for (; i <= mac_policy_list.maxindex; i++) { \
397 mpc = mac_policy_list.entries[i].mpc; \
398 if (mpc == NULL) \
399 continue; \
400 \
401 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
402 result = result composition \
403 mpc->mpc_ops->mpo_ ## operation \
404 (args); \
405 } \
406 mac_policy_list_unbusy(); \
407 } \
408 } while (0)
409
410 #define MAC_INTERNALIZE(obj, label, instring) \
411 mac_internalize(offsetof(struct mac_policy_ops, mpo_ ## obj ## _label_internalize), label, instring)
412
413 #define MAC_EXTERNALIZE(obj, label, elementlist, outbuf, outbuflen) \
414 mac_externalize(offsetof(struct mac_policy_ops, mpo_ ## obj ## _label_externalize), label, elementlist, outbuf, outbuflen)
415
416 #define MAC_EXTERNALIZE_AUDIT(obj, label, outbuf, outbuflen) \
417 mac_externalize(offsetof(struct mac_policy_ops, mpo_ ## obj ## _label_externalize_audit), label, "*", outbuf, outbuflen)
418
419 /*
420 * MAC_PERFORM performs the designated operation by walking the policy
421 * module list and invoking that operation for each policy.
422 */
423 #define MAC_PERFORM(operation, args...) do { \
424 struct mac_policy_conf *mpc; \
425 u_int i; \
426 \
427 for (i = 0; i < mac_policy_list.staticmax; i++) { \
428 mpc = mac_policy_list.entries[i].mpc; \
429 if (mpc == NULL) \
430 continue; \
431 \
432 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
433 mpc->mpc_ops->mpo_ ## operation (args); \
434 } \
435 if (mac_policy_list_conditional_busy() != 0) { \
436 for (; i <= mac_policy_list.maxindex; i++) { \
437 mpc = mac_policy_list.entries[i].mpc; \
438 if (mpc == NULL) \
439 continue; \
440 \
441 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
442 mpc->mpc_ops->mpo_ ## operation (args); \
443 } \
444 mac_policy_list_unbusy(); \
445 } \
446 } while (0)
447
448 struct __mac_get_pid_args;
449 struct __mac_get_proc_args;
450 struct __mac_set_proc_args;
451 struct __mac_get_lcid_args;
452 struct __mac_get_lctx_args;
453 struct __mac_set_lctx_args;
454 struct __mac_get_fd_args;
455 struct __mac_get_file_args;
456 struct __mac_get_link_args;
457 struct __mac_set_fd_args;
458 struct __mac_set_file_args;
459 struct __mac_syscall_args;
460
461 void mac_policy_addto_labellist(const mac_policy_handle_t, int);
462 void mac_policy_removefrom_labellist(const mac_policy_handle_t);
463
464 int mac_externalize(size_t mpo_externalize_off, struct label *label,
465 const char *elementlist, char *outbuf, size_t outbuflen);
466 int mac_internalize(size_t mpo_internalize_off, struct label *label,
467 char *elementlist);
468 #endif /* !_SECURITY_MAC_INTERNAL_H_ */