2 * Copyright (c) 2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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.
36 * This software was developed by Robert Watson and Ilmar Habibulin for the
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.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
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.
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
67 #ifndef _SECURITY_MAC_INTERNAL_H_
68 #define _SECURITY_MAC_INTERNAL_H_
71 #warning "MAC policy is not KPI, see Technical Q&A QA1574, this header will be removed in next version"
75 #include <sys/param.h>
76 #include <sys/queue.h>
77 #include <security/mac.h>
78 #include <security/mac_framework.h>
79 #include <security/mac_policy.h>
80 #include <security/mac_data.h>
81 #include <sys/sysctl.h>
82 #include <kern/wait_queue.h>
83 #include <kern/locks.h>
84 #include <sys/kernel.h>
86 #include <sys/malloc.h>
89 #include <sys/systm.h>
90 #include <sys/socket.h>
91 #include <sys/socketvar.h>
92 #include <sys/vnode.h>
95 * MAC Framework sysctl namespace.
98 SYSCTL_DECL(_security
);
99 SYSCTL_DECL(_security_mac
);
103 struct mac_policy_list_element
{
104 struct mac_policy_conf
*mpc
;
107 struct mac_policy_list
{
114 struct mac_policy_list_element
*entries
;
117 typedef struct mac_policy_list mac_policy_list_t
;
121 * Policy that has registered with the framework for a specific
122 * label namespace name.
124 struct mac_label_listener
{
125 mac_policy_handle_t mll_handle
;
126 LIST_ENTRY(mac_label_listener
) mll_list
;
129 LIST_HEAD(mac_label_listeners_t
, mac_label_listener
);
132 * Type of list used to manage label namespace names.
134 struct mac_label_element
{
135 char mle_name
[MAC_MAX_LABEL_ELEMENT_NAME
];
136 struct mac_label_listeners_t mle_listeners
;
137 LIST_ENTRY(mac_label_element
) mle_list
;
140 LIST_HEAD(mac_label_element_list_t
, mac_label_element
);
146 #define MLJ_TYPE_PORT 1
147 #define MLJ_TYPE_TASK 2
149 #define MLJ_PORT_OP_INIT 0x0001
150 #define MLJ_PORT_OP_CREATE_K 0x0002
151 #define MLJ_PORT_OP_CREATE 0x0004
152 #define MLJ_PORT_OP_UPDATE 0x0008
154 #define MLJ_TASK_OP_INIT 0x0001
155 #define MLJ_TASK_OP_CREATE_K 0x0002
157 struct mac_label_journal
{
162 int kotype
; /* Kernel Port */
164 TAILQ_ENTRY(mac_label_journal
) link
;
166 TAILQ_HEAD(mac_label_journal_list_t
, mac_label_journal
);
168 int mac_label_journal_add (struct label
*, int);
169 int mac_label_journal_remove(struct label
*);
170 struct mac_label_journal
*
171 mac_label_journal_find (struct label
*);
172 int mac_label_journal (struct label
*, int, ...);
173 void mac_label_journal_replay(void);
177 * MAC Framework global variables.
180 extern struct mac_label_element_list_t mac_label_element_list
;
181 extern struct mac_label_element_list_t mac_static_label_element_list
;
182 extern struct mac_label_journal_list_t mac_label_journal_list
;
184 extern struct mac_policy_list mac_policy_list
;
187 * global flags to control whether a MACF subsystem is configured
188 * at all in the system.
190 extern unsigned int mac_device_enforce
;
191 extern unsigned int mac_pipe_enforce
;
192 extern unsigned int mac_posixsem_enforce
;
193 extern unsigned int mac_posixshm_enforce
;
194 extern unsigned int mac_proc_enforce
;
195 extern unsigned int mac_socket_enforce
;
196 extern unsigned int mac_system_enforce
;
197 extern unsigned int mac_sysvmsg_enforce
;
198 extern unsigned int mac_sysvsem_enforce
;
199 extern unsigned int mac_sysvshm_enforce
;
200 extern unsigned int mac_vm_enforce
;
201 extern unsigned int mac_vnode_enforce
;
204 extern unsigned int mac_label_mbufs
;
207 extern unsigned int mac_label_vnodes
;
209 static int mac_proc_check_enforce(proc_t p
, int enforce_flags
);
211 static __inline__
int mac_proc_check_enforce(proc_t p
, int enforce_flags
)
214 return ((p
->p_mac_enforce
& enforce_flags
) != 0);
216 #pragma unused(p,enforce_flags)
221 static int mac_context_check_enforce(vfs_context_t ctx
, int enforce_flags
);
222 static void mac_context_set_enforce(vfs_context_t ctx
, int enforce_flags
);
224 static __inline__
int mac_context_check_enforce(vfs_context_t ctx
, int enforce_flags
)
226 proc_t proc
= vfs_context_proc(ctx
);
231 return (mac_proc_check_enforce(proc
, enforce_flags
));
234 static __inline__
void mac_context_set_enforce(vfs_context_t ctx
, int enforce_flags
)
237 proc_t proc
= vfs_context_proc(ctx
);
242 mac_proc_set_enforce(proc
, enforce_flags
);
244 #pragma unused(ctx,enforce_flags)
250 * MAC Framework infrastructure functions.
253 int mac_error_select(int error1
, int error2
);
255 void mac_policy_list_busy(void);
256 int mac_policy_list_conditional_busy(void);
257 void mac_policy_list_unbusy(void);
259 void mac_labelzone_init(void);
260 struct label
*mac_labelzone_alloc(int flags
);
261 void mac_labelzone_free(struct label
*label
);
263 void mac_label_init(struct label
*label
);
264 void mac_label_destroy(struct label
*label
);
266 int mac_check_structmac_consistent(struct user_mac
*mac
);
268 int mac_check_structmac_consistent(struct mac
*mac
);
271 int mac_cred_label_externalize(struct label
*, char *e
, char *out
, size_t olen
, int flags
);
272 int mac_lctx_label_externalize(struct label
*, char *e
, char *out
, size_t olen
);
273 #if CONFIG_MACF_SOCKET
274 int mac_socket_label_externalize(struct label
*, char *e
, char *out
, size_t olen
);
275 #endif /* CONFIG_MACF_SOCKET */
276 int mac_vnode_label_externalize(struct label
*, char *e
, char *out
, size_t olen
, int flags
);
277 int mac_pipe_label_externalize(struct label
*label
, char *elements
,
278 char *outbuf
, size_t outbuflen
);
280 int mac_cred_label_internalize(struct label
*label
, char *string
);
281 int mac_lctx_label_internalize(struct label
*label
, char *string
);
282 #if CONFIG_MACF_SOCKET
283 int mac_socket_label_internalize(struct label
*label
, char *string
);
284 #endif /* CONFIG_MACF_SOCKET */
285 int mac_vnode_label_internalize(struct label
*label
, char *string
);
286 int mac_pipe_label_internalize(struct label
*label
, char *string
);
288 #if CONFIG_MACF_SOCKET
289 /* internal socket label manipulation functions */
290 struct label
*mac_socket_label_alloc(int flags
);
291 void mac_socket_label_free(struct label
*l
);
292 int mac_socket_label_update(struct ucred
*cred
, struct socket
*so
, struct label
*l
);
293 #endif /* MAC_SOCKET */
296 struct label
*mac_mbuf_to_label(struct mbuf
*m
);
298 #define mac_mbuf_to_label(m) (NULL)
302 * MAC_CHECK performs the designated check by walking the policy
303 * module list and checking with each as to how it feels about the
304 * request. Note that it returns its value via 'error' in the scope
307 #define MAC_CHECK(check, args...) do { \
308 struct mac_policy_conf *mpc; \
312 for (i = 0; i < mac_policy_list.staticmax; i++) { \
313 mpc = mac_policy_list.entries[i].mpc; \
317 if (mpc->mpc_ops->mpo_ ## check != NULL) \
318 error = mac_error_select( \
319 mpc->mpc_ops->mpo_ ## check (args), \
322 if (mac_policy_list_conditional_busy() != 0) { \
323 for (; i <= mac_policy_list.maxindex; i++) { \
324 mpc = mac_policy_list.entries[i].mpc; \
328 if (mpc->mpc_ops->mpo_ ## check != NULL) \
329 error = mac_error_select( \
330 mpc->mpc_ops->mpo_ ## check (args), \
333 mac_policy_list_unbusy(); \
338 * MAC_GRANT performs the designated check by walking the policy
339 * module list and checking with each as to how it feels about the
340 * request. Unlike MAC_CHECK, it grants if any policies return '0',
341 * and otherwise returns EPERM. Note that it returns its value via
342 * 'error' in the scope of the caller.
344 #define MAC_GRANT(check, args...) do { \
345 struct mac_policy_conf *mpc; \
349 for (i = 0; i < mac_policy_list.staticmax; i++) { \
350 mpc = mac_policy_list.entries[i].mpc; \
354 if (mpc->mpc_ops->mpo_ ## check != NULL) { \
355 if (mpc->mpc_ops->mpo_ ## check (args) == 0) \
359 if (mac_policy_list_conditional_busy() != 0) { \
360 for (; i <= mac_policy_list.maxindex; i++) { \
361 mpc = mac_policy_list.entries[i].mpc; \
365 if (mpc->mpc_ops->mpo_ ## check != NULL) { \
366 if (mpc->mpc_ops->mpo_ ## check (args) \
371 mac_policy_list_unbusy(); \
376 * MAC_BOOLEAN performs the designated boolean composition by walking
377 * the module list, invoking each instance of the operation, and
378 * combining the results using the passed C operator. Note that it
379 * returns its value via 'result' in the scope of the caller, which
380 * should be initialized by the caller in a meaningful way to get
381 * a meaningful result.
383 #define MAC_BOOLEAN(operation, composition, args...) do { \
384 struct mac_policy_conf *mpc; \
387 for (i = 0; i < mac_policy_list.staticmax; i++) { \
388 mpc = mac_policy_list.entries[i].mpc; \
392 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
393 result = result composition \
394 mpc->mpc_ops->mpo_ ## operation \
397 if (mac_policy_list_conditional_busy() != 0) { \
398 for (; i <= mac_policy_list.maxindex; i++) { \
399 mpc = mac_policy_list.entries[i].mpc; \
403 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
404 result = result composition \
405 mpc->mpc_ops->mpo_ ## operation \
408 mac_policy_list_unbusy(); \
412 #define MAC_INTERNALIZE(obj, label, instring) \
413 mac_internalize(offsetof(struct mac_policy_ops, mpo_ ## obj ## _label_internalize), label, instring)
415 #define MAC_EXTERNALIZE(obj, label, elementlist, outbuf, outbuflen) \
416 mac_externalize(offsetof(struct mac_policy_ops, mpo_ ## obj ## _label_externalize), label, elementlist, outbuf, outbuflen)
418 #define MAC_EXTERNALIZE_AUDIT(obj, label, outbuf, outbuflen) \
419 mac_externalize(offsetof(struct mac_policy_ops, mpo_ ## obj ## _label_externalize_audit), label, "*", outbuf, outbuflen)
422 * MAC_PERFORM performs the designated operation by walking the policy
423 * module list and invoking that operation for each policy.
425 #define MAC_PERFORM(operation, args...) do { \
426 struct mac_policy_conf *mpc; \
429 for (i = 0; i < mac_policy_list.staticmax; i++) { \
430 mpc = mac_policy_list.entries[i].mpc; \
434 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
435 mpc->mpc_ops->mpo_ ## operation (args); \
437 if (mac_policy_list_conditional_busy() != 0) { \
438 for (; i <= mac_policy_list.maxindex; i++) { \
439 mpc = mac_policy_list.entries[i].mpc; \
443 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
444 mpc->mpc_ops->mpo_ ## operation (args); \
446 mac_policy_list_unbusy(); \
450 struct __mac_get_pid_args
;
451 struct __mac_get_proc_args
;
452 struct __mac_set_proc_args
;
453 struct __mac_get_lcid_args
;
454 struct __mac_get_lctx_args
;
455 struct __mac_set_lctx_args
;
456 struct __mac_get_fd_args
;
457 struct __mac_get_file_args
;
458 struct __mac_get_link_args
;
459 struct __mac_set_fd_args
;
460 struct __mac_set_file_args
;
461 struct __mac_syscall_args
;
463 void mac_policy_addto_labellist(const mac_policy_handle_t
, int);
464 void mac_policy_removefrom_labellist(const mac_policy_handle_t
);
466 int mac_externalize(size_t mpo_externalize_off
, struct label
*label
,
467 const char *elementlist
, char *outbuf
, size_t outbuflen
);
468 int mac_internalize(size_t mpo_internalize_off
, struct label
*label
,
470 #endif /* !_SECURITY_MAC_INTERNAL_H_ */