2 * Copyright (c) 1999-2002 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001-2004 Networks Associates Technology, Inc.
7 * This software was developed by Robert Watson and Ilmar Habibulin for the
10 * This software was developed for the FreeBSD Project in part by Network
11 * Associates Laboratories, the Security Research Division of Network
12 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
13 * as part of the DARPA CHATS research program.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * MAC Framework sysctl namespace.
43 SYSCTL_DECL(_security);
44 SYSCTL_DECL(_security_mac);
46 SYSCTL_DECL(_security_mac_debug);
47 SYSCTL_DECL(_security_mac_debug_counters);
49 #endif /* SYSCTL_DECL */
52 * MAC Framework global types and typedefs.
54 LIST_HEAD(mac_policy_list_head, mac_policy_conf);
56 MALLOC_DECLARE(M_MACTEMP);
60 * MAC Framework global variables.
62 extern struct mac_policy_list_head mac_policy_list;
63 extern struct mac_policy_list_head mac_static_policy_list;
65 extern int mac_enforce_network;
66 extern int mac_enforce_process;
67 extern int mac_enforce_socket;
68 extern int mac_enforce_vm;
69 #ifndef MAC_ALWAYS_LABEL_MBUF
70 extern int mac_labelmbufs;
74 * MAC Framework object/access counter primitives, conditionally
78 #define MAC_DEBUG_COUNTER_INC(x) atomic_add_int(x, 1);
79 #define MAC_DEBUG_COUNTER_DEC(x) atomic_subtract_int(x, 1);
81 #define MAC_DEBUG_COUNTER_INC(x)
82 #define MAC_DEBUG_COUNTER_DEC(x)
86 * MAC Framework infrastructure functions.
88 int mac_error_select(int error1, int error2);
90 void mac_policy_grab_exclusive(void);
91 void mac_policy_assert_exclusive(void);
92 void mac_policy_release_exclusive(void);
93 void mac_policy_list_busy(void);
94 int mac_policy_list_conditional_busy(void);
95 void mac_policy_list_unbusy(void);
97 struct label *mac_labelzone_alloc(int flags);
98 void mac_labelzone_free(struct label *label);
99 void mac_labelzone_init(void);
101 void mac_init_label(struct label *label);
102 void mac_destroy_label(struct label *label);
103 int mac_check_structmac_consistent(struct mac *mac);
104 int mac_allocate_slot(void);
107 * MAC Framework per-object type functions. It's not yet clear how
108 * the namespaces, etc, should work for these, so for now, sort by
111 struct label *mac_pipe_label_alloc(void);
112 void mac_pipe_label_free(struct label *label);
113 struct label *mac_socket_label_alloc(int flag);
114 void mac_socket_label_free(struct label *label);
116 int mac_check_cred_relabel(struct ucred *cred, struct label *newlabel);
117 int mac_externalize_cred_label(struct label *label, char *elements,
118 char *outbuf, size_t outbuflen);
119 int mac_internalize_cred_label(struct label *label, char *string);
120 void mac_relabel_cred(struct ucred *cred, struct label *newlabel);
122 struct label *mac_mbuf_to_label(struct mbuf *m);
124 void mac_copy_pipe_label(struct label *src, struct label *dest);
125 int mac_externalize_pipe_label(struct label *label, char *elements,
126 char *outbuf, size_t outbuflen);
127 int mac_internalize_pipe_label(struct label *label, char *string);
129 int mac_socket_label_set(struct ucred *cred, struct socket *so,
130 struct label *label);
131 void mac_copy_socket_label(struct label *src, struct label *dest);
132 int mac_externalize_socket_label(struct label *label, char *elements,
133 char *outbuf, size_t outbuflen);
134 int mac_internalize_socket_label(struct label *label, char *string);
136 int mac_externalize_vnode_label(struct label *label, char *elements,
137 char *outbuf, size_t outbuflen);
138 int mac_internalize_vnode_label(struct label *label, char *string);
139 void mac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp,
141 int vn_setlabel(struct vnode *vp, struct label *intlabel,
145 * MAC_CHECK performs the designated check by walking the policy module
146 * list and checking with each as to how it feels about the request.
147 * Note that it returns its value via 'error' in the scope of the caller.
149 #define MAC_CHECK(check, args...) do { \
150 struct mac_policy_conf *mpc; \
154 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
155 if (mpc->mpc_ops->mpo_ ## check != NULL) \
156 error = mac_error_select( \
157 mpc->mpc_ops->mpo_ ## check (args), \
160 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \
161 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
162 if (mpc->mpc_ops->mpo_ ## check != NULL) \
163 error = mac_error_select( \
164 mpc->mpc_ops->mpo_ ## check (args), \
167 mac_policy_list_unbusy(); \
172 * MAC_BOOLEAN performs the designated boolean composition by walking
173 * the module list, invoking each instance of the operation, and
174 * combining the results using the passed C operator. Note that it
175 * returns its value via 'result' in the scope of the caller, which
176 * should be initialized by the caller in a meaningful way to get
177 * a meaningful result.
179 #define MAC_BOOLEAN(operation, composition, args...) do { \
180 struct mac_policy_conf *mpc; \
183 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
184 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
185 result = result composition \
186 mpc->mpc_ops->mpo_ ## operation (args); \
188 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \
189 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
190 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
191 result = result composition \
192 mpc->mpc_ops->mpo_ ## operation \
195 mac_policy_list_unbusy(); \
199 #define MAC_EXTERNALIZE(type, label, elementlist, outbuf, \
201 int claimed, first, ignorenotfound, savedlen; \
202 char *element_name, *element_temp; \
207 sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN); \
208 element_temp = elementlist; \
209 while ((element_name = strsep(&element_temp, ",")) != NULL) { \
210 if (element_name[0] == '?') { \
212 ignorenotfound = 1; \
214 ignorenotfound = 0; \
215 savedlen = sbuf_len(&sb); \
217 error = sbuf_printf(&sb, "%s/", element_name); \
219 error = sbuf_printf(&sb, ",%s/", element_name); \
221 error = EINVAL; /* XXX: E2BIG? */ \
225 MAC_CHECK(externalize_ ## type ## _label, label, \
226 element_name, &sb, &claimed); \
229 if (claimed == 0 && ignorenotfound) { \
230 /* Revert last label name. */ \
231 sbuf_setpos(&sb, savedlen); \
232 } else if (claimed != 1) { \
233 error = EINVAL; /* XXX: ENOLABEL? */ \
242 #define MAC_INTERNALIZE(type, label, instring) do { \
243 char *element, *element_name, *element_data; \
247 element = instring; \
248 while ((element_name = strsep(&element, ",")) != NULL) { \
249 element_data = element_name; \
250 element_name = strsep(&element_data, "/"); \
251 if (element_data == NULL) { \
256 MAC_CHECK(internalize_ ## type ## _label, label, \
257 element_name, element_data, &claimed); \
260 if (claimed != 1) { \
261 /* XXXMAC: Another error here? */ \
269 * MAC_PERFORM performs the designated operation by walking the policy
270 * module list and invoking that operation for each policy.
272 #define MAC_PERFORM(operation, args...) do { \
273 struct mac_policy_conf *mpc; \
276 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
277 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
278 mpc->mpc_ops->mpo_ ## operation (args); \
280 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \
281 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
282 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
283 mpc->mpc_ops->mpo_ ## operation (args); \
285 mac_policy_list_unbusy(); \