2 * Copyright (c) 1999-2002, 2006 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001-2004 Networks Associates Technology, Inc.
5 * Copyright (c) 2006 nCircle Network Security, Inc.
6 * Copyright (c) 2006 SPARTA, Inc.
9 * This software was developed by Robert Watson and Ilmar Habibulin for the
12 * This software was developed for the FreeBSD Project in part by Network
13 * Associates Laboratories, the Security Research Division of Network
14 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
15 * as part of the DARPA CHATS research program.
17 * This software was developed by Robert N. M. Watson for the TrustedBSD
18 * Project under contract to nCircle Network Security, Inc.
20 * This software was enhanced by SPARTA ISSO under SPAWAR contract
21 * N66001-04-C-6019 ("SEFOS").
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 #ifndef _SECURITY_MAC_MAC_INTERNAL_H_
48 #define _SECURITY_MAC_MAC_INTERNAL_H_
51 #error "no user-serviceable parts inside"
55 * MAC Framework sysctl namespace.
58 SYSCTL_DECL(_security_mac);
59 #endif /* SYSCTL_DECL */
62 * MAC Framework global types and typedefs.
64 LIST_HEAD(mac_policy_list_head, mac_policy_conf);
66 MALLOC_DECLARE(M_MACTEMP);
70 * MAC labels -- in-kernel storage format.
72 * In general, struct label pointers are embedded in kernel data structures
73 * representing objects that may be labeled (and protected). Struct label is
74 * opaque to both kernel services that invoke the MAC Framework and MAC
75 * policy modules. In particular, we do not wish to encode the layout of the
76 * label structure into any ABIs. Historically, the slot array contained
77 * unions of {long, void} but now contains uintptr_t.
79 #define MAC_MAX_SLOTS 4
80 #define MAC_FLAG_INITIALIZED 0x0000001 /* Is initialized for use. */
83 intptr_t l_perpolicy[MAC_MAX_SLOTS];
87 * MAC Framework global variables.
89 extern struct mac_policy_list_head mac_policy_list;
90 extern struct mac_policy_list_head mac_static_policy_list;
91 extern uint64_t mac_labeled;
92 extern struct mtx mac_ifnet_mtx;
95 * MAC Framework infrastructure functions.
97 int mac_error_select(int error1, int error2);
99 void mac_policy_grab_exclusive(void);
100 void mac_policy_assert_exclusive(void);
101 void mac_policy_release_exclusive(void);
102 void mac_policy_list_busy(void);
103 int mac_policy_list_conditional_busy(void);
104 void mac_policy_list_unbusy(void);
106 struct label *mac_labelzone_alloc(int flags);
107 void mac_labelzone_free(struct label *label);
108 void mac_labelzone_init(void);
110 void mac_init_label(struct label *label);
111 void mac_destroy_label(struct label *label);
112 int mac_check_structmac_consistent(struct mac *mac);
113 int mac_allocate_slot(void);
115 #define MAC_IFNET_LOCK(ifp) mtx_lock(&mac_ifnet_mtx)
116 #define MAC_IFNET_UNLOCK(ifp) mtx_unlock(&mac_ifnet_mtx)
119 * MAC Framework per-object type functions. It's not yet clear how the
120 * namespaces, etc, should work for these, so for now, sort by object type.
122 struct label *mac_cred_label_alloc(void);
123 void mac_cred_label_free(struct label *label);
124 struct label *mac_pipe_label_alloc(void);
125 void mac_pipe_label_free(struct label *label);
126 struct label *mac_socket_label_alloc(int flag);
127 void mac_socket_label_free(struct label *label);
128 struct label *mac_vnode_label_alloc(void);
129 void mac_vnode_label_free(struct label *label);
131 int mac_cred_check_relabel(struct ucred *cred, struct label *newlabel);
132 int mac_cred_externalize_label(struct label *label, char *elements,
133 char *outbuf, size_t outbuflen);
134 int mac_cred_internalize_label(struct label *label, char *string);
135 void mac_cred_relabel(struct ucred *cred, struct label *newlabel);
137 struct label *mac_mbuf_to_label(struct mbuf *m);
139 void mac_pipe_copy_label(struct label *src, struct label *dest);
140 int mac_pipe_externalize_label(struct label *label, char *elements,
141 char *outbuf, size_t outbuflen);
142 int mac_pipe_internalize_label(struct label *label, char *string);
144 int mac_socket_label_set(struct ucred *cred, struct socket *so,
145 struct label *label);
146 void mac_socket_copy_label(struct label *src, struct label *dest);
147 int mac_socket_externalize_label(struct label *label, char *elements,
148 char *outbuf, size_t outbuflen);
149 int mac_socket_internalize_label(struct label *label, char *string);
151 int mac_vnode_externalize_label(struct label *label, char *elements,
152 char *outbuf, size_t outbuflen);
153 int mac_vnode_internalize_label(struct label *label, char *string);
154 void mac_vnode_check_mmap_downgrade(struct ucred *cred, struct vnode *vp,
156 int vn_setlabel(struct vnode *vp, struct label *intlabel,
160 * MAC_CHECK performs the designated check by walking the policy module list
161 * and checking with each as to how it feels about the request. Note that it
162 * returns its value via 'error' in the scope of the caller.
164 #define MAC_CHECK(check, args...) do { \
165 struct mac_policy_conf *mpc; \
169 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
170 if (mpc->mpc_ops->mpo_ ## check != NULL) \
171 error = mac_error_select( \
172 mpc->mpc_ops->mpo_ ## check (args), \
175 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \
176 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
177 if (mpc->mpc_ops->mpo_ ## check != NULL) \
178 error = mac_error_select( \
179 mpc->mpc_ops->mpo_ ## check (args), \
182 mac_policy_list_unbusy(); \
187 * MAC_GRANT performs the designated check by walking the policy module list
188 * and checking with each as to how it feels about the request. Unlike
189 * MAC_CHECK, it grants if any policies return '0', and otherwise returns
190 * EPERM. Note that it returns its value via 'error' in the scope of the
193 #define MAC_GRANT(check, args...) do { \
194 struct mac_policy_conf *mpc; \
198 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
199 if (mpc->mpc_ops->mpo_ ## check != NULL) { \
200 if (mpc->mpc_ops->mpo_ ## check(args) == 0) \
204 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \
205 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
206 if (mpc->mpc_ops->mpo_ ## check != NULL) { \
207 if (mpc->mpc_ops->mpo_ ## check (args) \
212 mac_policy_list_unbusy(); \
217 * MAC_BOOLEAN performs the designated boolean composition by walking the
218 * module list, invoking each instance of the operation, and combining the
219 * results using the passed C operator. Note that it returns its value via
220 * 'result' in the scope of the caller, which should be initialized by the
221 * caller in a meaningful way to get a meaningful result.
223 #define MAC_BOOLEAN(operation, composition, args...) do { \
224 struct mac_policy_conf *mpc; \
227 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
228 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
229 result = result composition \
230 mpc->mpc_ops->mpo_ ## operation (args); \
232 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \
233 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
234 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
235 result = result composition \
236 mpc->mpc_ops->mpo_ ## operation \
239 mac_policy_list_unbusy(); \
244 * MAC_EXTERNALIZE queries each policy to see if it can generate an
245 * externalized version of a label element by name. Policies declare whether
246 * they have matched a particular element name, parsed from the string by
247 * MAC_EXTERNALIZE, and an error is returned if any element is matched by no
250 #define MAC_EXTERNALIZE(type, label, elementlist, outbuf, \
252 int claimed, first, ignorenotfound, savedlen; \
253 char *element_name, *element_temp; \
258 sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN); \
259 element_temp = elementlist; \
260 while ((element_name = strsep(&element_temp, ",")) != NULL) { \
261 if (element_name[0] == '?') { \
263 ignorenotfound = 1; \
265 ignorenotfound = 0; \
266 savedlen = sbuf_len(&sb); \
268 error = sbuf_printf(&sb, "%s/", element_name); \
270 error = sbuf_printf(&sb, ",%s/", element_name); \
272 error = EINVAL; /* XXX: E2BIG? */ \
276 MAC_CHECK(type ## _externalize_label, label, \
277 element_name, &sb, &claimed); \
280 if (claimed == 0 && ignorenotfound) { \
281 /* Revert last label name. */ \
282 sbuf_setpos(&sb, savedlen); \
283 } else if (claimed != 1) { \
284 error = EINVAL; /* XXX: ENOLABEL? */ \
294 * MAC_INTERNALIZE presents parsed element names and data to each policy to
295 * see if any is willing to claim it and internalize the label data. If no
296 * policies match, an error is returned.
298 #define MAC_INTERNALIZE(type, label, instring) do { \
299 char *element, *element_name, *element_data; \
303 element = instring; \
304 while ((element_name = strsep(&element, ",")) != NULL) { \
305 element_data = element_name; \
306 element_name = strsep(&element_data, "/"); \
307 if (element_data == NULL) { \
312 MAC_CHECK(type ## _internalize_label, label, \
313 element_name, element_data, &claimed); \
316 if (claimed != 1) { \
317 /* XXXMAC: Another error here? */ \
325 * MAC_PERFORM performs the designated operation by walking the policy module
326 * list and invoking that operation for each policy.
328 #define MAC_PERFORM(operation, args...) do { \
329 struct mac_policy_conf *mpc; \
332 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
333 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
334 mpc->mpc_ops->mpo_ ## operation (args); \
336 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \
337 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
338 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
339 mpc->mpc_ops->mpo_ ## operation (args); \
341 mac_policy_list_unbusy(); \
345 #endif /* !_SECURITY_MAC_MAC_INTERNAL_H_ */