2 * Copyright (c) 1999-2002, 2006, 2009, 2019 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.
7 * Copyright (c) 2009 Apple, Inc.
10 * This software was developed by Robert Watson and Ilmar Habibulin for the
13 * This software was developed for the FreeBSD Project in part by Network
14 * Associates Laboratories, the Security Research Division of Network
15 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
16 * as part of the DARPA CHATS research program.
18 * This software was developed by Robert N. M. Watson for the TrustedBSD
19 * Project under contract to nCircle Network Security, Inc.
21 * This software was enhanced by SPARTA ISSO under SPAWAR contract
22 * N66001-04-C-6019 ("SEFOS").
24 * This software was developed at the University of Cambridge Computer
25 * Laboratory with support from a grant from Google, Inc.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
36 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 #ifndef _SECURITY_MAC_MAC_INTERNAL_H_
52 #define _SECURITY_MAC_MAC_INTERNAL_H_
55 #error "no user-serviceable parts inside"
59 #include <sys/rmlock.h>
62 * MAC Framework sysctl namespace.
65 SYSCTL_DECL(_security_mac);
66 #endif /* SYSCTL_DECL */
69 * MAC Framework SDT DTrace probe namespace, macros for declaring entry
70 * point probes, macros for invoking them.
72 #ifdef SDT_PROVIDER_DECLARE
73 SDT_PROVIDER_DECLARE(mac); /* MAC Framework-level events. */
74 SDT_PROVIDER_DECLARE(mac_framework); /* Entry points to MAC. */
76 #define MAC_CHECK_PROBE_DEFINE4(name, arg0, arg1, arg2, arg3) \
77 SDT_PROBE_DEFINE5(mac_framework, , name, mac__check__err, \
78 "int", arg0, arg1, arg2, arg3); \
79 SDT_PROBE_DEFINE5(mac_framework, , name, mac__check__ok, \
80 "int", arg0, arg1, arg2, arg3);
82 #define MAC_CHECK_PROBE_DEFINE3(name, arg0, arg1, arg2) \
83 SDT_PROBE_DEFINE4(mac_framework, , name, mac__check__err, \
84 "int", arg0, arg1, arg2); \
85 SDT_PROBE_DEFINE4(mac_framework, , name, mac__check__ok, \
86 "int", arg0, arg1, arg2);
88 #define MAC_CHECK_PROBE_DEFINE2(name, arg0, arg1) \
89 SDT_PROBE_DEFINE3(mac_framework, , name, mac__check__err, \
91 SDT_PROBE_DEFINE3(mac_framework, , name, mac__check__ok, \
94 #define MAC_CHECK_PROBE_DEFINE1(name, arg0) \
95 SDT_PROBE_DEFINE2(mac_framework, , name, mac__check__err, \
97 SDT_PROBE_DEFINE2(mac_framework, , name, mac__check__ok, \
100 #define MAC_CHECK_PROBE4(name, error, arg0, arg1, arg2, arg3) do { \
101 if (SDT_PROBES_ENABLED()) { \
103 SDT_PROBE5(mac_framework, , name, mac__check__err,\
104 error, arg0, arg1, arg2, arg3); \
106 SDT_PROBE5(mac_framework, , name, mac__check__ok,\
107 0, arg0, arg1, arg2, arg3); \
112 #define MAC_CHECK_PROBE3(name, error, arg0, arg1, arg2) \
113 MAC_CHECK_PROBE4(name, error, arg0, arg1, arg2, 0)
114 #define MAC_CHECK_PROBE2(name, error, arg0, arg1) \
115 MAC_CHECK_PROBE3(name, error, arg0, arg1, 0)
116 #define MAC_CHECK_PROBE1(name, error, arg0) \
117 MAC_CHECK_PROBE2(name, error, arg0, 0)
120 #define MAC_GRANT_PROBE_DEFINE2(name, arg0, arg1) \
121 SDT_PROBE_DEFINE3(mac_framework, , name, mac__grant__err, \
122 "int", arg0, arg1); \
123 SDT_PROBE_DEFINE3(mac_framework, , name, mac__grant__ok, \
126 #define MAC_GRANT_PROBE2(name, error, arg0, arg1) do { \
127 if (SDT_PROBES_ENABLED()) { \
129 SDT_PROBE3(mac_framework, , name, mac__grant__err,\
130 error, arg0, arg1); \
132 SDT_PROBE3(mac_framework, , name, mac__grant__ok,\
133 error, arg0, arg1); \
139 * MAC Framework global types and typedefs.
141 LIST_HEAD(mac_policy_list_head, mac_policy_conf);
142 #ifdef MALLOC_DECLARE
143 MALLOC_DECLARE(M_MACTEMP);
147 * MAC labels -- in-kernel storage format.
149 * In general, struct label pointers are embedded in kernel data structures
150 * representing objects that may be labeled (and protected). Struct label is
151 * opaque to both kernel services that invoke the MAC Framework and MAC
152 * policy modules. In particular, we do not wish to encode the layout of the
153 * label structure into any ABIs. Historically, the slot array contained
154 * unions of {long, void} but now contains uintptr_t.
156 #define MAC_MAX_SLOTS 4
157 #define MAC_FLAG_INITIALIZED 0x0000001 /* Is initialized for use. */
160 intptr_t l_perpolicy[MAC_MAX_SLOTS];
165 * Flags for mac_labeled, a bitmask of object types need across the union of
166 * all policies currently registered with the MAC Framework, used to key
167 * whether or not labels are allocated and constructors for the type are
170 #define MPC_OBJECT_CRED 0x0000000000000001
171 #define MPC_OBJECT_PROC 0x0000000000000002
172 #define MPC_OBJECT_VNODE 0x0000000000000004
173 #define MPC_OBJECT_INPCB 0x0000000000000008
174 #define MPC_OBJECT_SOCKET 0x0000000000000010
175 #define MPC_OBJECT_DEVFS 0x0000000000000020
176 #define MPC_OBJECT_MBUF 0x0000000000000040
177 #define MPC_OBJECT_IPQ 0x0000000000000080
178 #define MPC_OBJECT_IFNET 0x0000000000000100
179 #define MPC_OBJECT_BPFDESC 0x0000000000000200
180 #define MPC_OBJECT_PIPE 0x0000000000000400
181 #define MPC_OBJECT_MOUNT 0x0000000000000800
182 #define MPC_OBJECT_POSIXSEM 0x0000000000001000
183 #define MPC_OBJECT_POSIXSHM 0x0000000000002000
184 #define MPC_OBJECT_SYSVMSG 0x0000000000004000
185 #define MPC_OBJECT_SYSVMSQ 0x0000000000008000
186 #define MPC_OBJECT_SYSVSEM 0x0000000000010000
187 #define MPC_OBJECT_SYSVSHM 0x0000000000020000
188 #define MPC_OBJECT_SYNCACHE 0x0000000000040000
189 #define MPC_OBJECT_IP6Q 0x0000000000080000
192 * MAC Framework global variables.
194 extern struct mac_policy_list_head mac_policy_list;
195 extern struct mac_policy_list_head mac_static_policy_list;
196 extern u_int mac_policy_count;
197 extern uint64_t mac_labeled;
198 extern struct mtx mac_ifnet_mtx;
201 * MAC Framework infrastructure functions.
203 int mac_error_select(int error1, int error2);
205 void mac_policy_slock_nosleep(struct rm_priotracker *tracker);
206 void mac_policy_slock_sleep(void);
207 void mac_policy_sunlock_nosleep(struct rm_priotracker *tracker);
208 void mac_policy_sunlock_sleep(void);
210 struct label *mac_labelzone_alloc(int flags);
211 void mac_labelzone_free(struct label *label);
212 void mac_labelzone_init(void);
214 void mac_init_label(struct label *label);
215 void mac_destroy_label(struct label *label);
216 int mac_check_structmac_consistent(struct mac *mac);
217 int mac_allocate_slot(void);
220 * Lock ifnets to protect labels only if ifnet labels are in use.
222 #define MAC_IFNET_LOCK(ifp, locked) do { \
223 if (mac_labeled & MPC_OBJECT_IFNET) { \
224 mtx_lock(&mac_ifnet_mtx); \
231 #define MAC_IFNET_UNLOCK(ifp, locked) do { \
233 mtx_unlock(&mac_ifnet_mtx); \
239 * MAC Framework per-object type functions. It's not yet clear how the
240 * namespaces, etc, should work for these, so for now, sort by object type.
242 struct label *mac_cred_label_alloc(void);
243 void mac_cred_label_free(struct label *label);
244 struct label *mac_pipe_label_alloc(void);
245 void mac_pipe_label_free(struct label *label);
246 struct label *mac_socket_label_alloc(int flag);
247 void mac_socket_label_free(struct label *label);
248 struct label *mac_vnode_label_alloc(void);
249 void mac_vnode_label_free(struct label *label);
251 int mac_cred_check_relabel(struct ucred *cred, struct label *newlabel);
252 int mac_cred_externalize_label(struct label *label, char *elements,
253 char *outbuf, size_t outbuflen);
254 int mac_cred_internalize_label(struct label *label, char *string);
255 void mac_cred_relabel(struct ucred *cred, struct label *newlabel);
257 struct label *mac_mbuf_to_label(struct mbuf *m);
259 void mac_pipe_copy_label(struct label *src, struct label *dest);
260 int mac_pipe_externalize_label(struct label *label, char *elements,
261 char *outbuf, size_t outbuflen);
262 int mac_pipe_internalize_label(struct label *label, char *string);
264 int mac_socket_label_set(struct ucred *cred, struct socket *so,
265 struct label *label);
266 void mac_socket_copy_label(struct label *src, struct label *dest);
267 int mac_socket_externalize_label(struct label *label, char *elements,
268 char *outbuf, size_t outbuflen);
269 int mac_socket_internalize_label(struct label *label, char *string);
271 int mac_vnode_externalize_label(struct label *label, char *elements,
272 char *outbuf, size_t outbuflen);
273 int mac_vnode_internalize_label(struct label *label, char *string);
274 void mac_vnode_check_mmap_downgrade(struct ucred *cred, struct vnode *vp,
276 int vn_setlabel(struct vnode *vp, struct label *intlabel,
280 * MAC Framework composition macros invoke all registered MAC policies for a
281 * specific entry point. They come in two forms: one which permits policies
282 * to sleep/block, and another that does not.
284 * MAC_POLICY_CHECK performs the designated check by walking the policy
285 * module list and checking with each as to how it feels about the request.
286 * Note that it returns its value via 'error' in the scope of the caller.
288 #define MAC_POLICY_CHECK(check, args...) do { \
289 struct mac_policy_conf *mpc; \
292 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
293 if (mpc->mpc_ops->mpo_ ## check != NULL) \
294 error = mac_error_select( \
295 mpc->mpc_ops->mpo_ ## check (args), \
298 if (!LIST_EMPTY(&mac_policy_list)) { \
299 mac_policy_slock_sleep(); \
300 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
301 if (mpc->mpc_ops->mpo_ ## check != NULL) \
302 error = mac_error_select( \
303 mpc->mpc_ops->mpo_ ## check (args), \
306 mac_policy_sunlock_sleep(); \
310 #define MAC_POLICY_CHECK_NOSLEEP(check, args...) do { \
311 struct mac_policy_conf *mpc; \
314 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
315 if (mpc->mpc_ops->mpo_ ## check != NULL) \
316 error = mac_error_select( \
317 mpc->mpc_ops->mpo_ ## check (args), \
320 if (!LIST_EMPTY(&mac_policy_list)) { \
321 struct rm_priotracker tracker; \
323 mac_policy_slock_nosleep(&tracker); \
324 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
325 if (mpc->mpc_ops->mpo_ ## check != NULL) \
326 error = mac_error_select( \
327 mpc->mpc_ops->mpo_ ## check (args), \
330 mac_policy_sunlock_nosleep(&tracker); \
335 * MAC_POLICY_GRANT performs the designated check by walking the policy
336 * module list and checking with each as to how it feels about the request.
337 * Unlike MAC_POLICY_CHECK, it grants if any policies return '0', and
338 * otherwise returns EPERM. Note that it returns its value via 'error' in
339 * the scope of the caller.
341 #define MAC_POLICY_GRANT_NOSLEEP(check, args...) do { \
342 struct mac_policy_conf *mpc; \
345 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
346 if (mpc->mpc_ops->mpo_ ## check != NULL) { \
347 if (mpc->mpc_ops->mpo_ ## check(args) == 0) \
351 if (!LIST_EMPTY(&mac_policy_list)) { \
352 struct rm_priotracker tracker; \
354 mac_policy_slock_nosleep(&tracker); \
355 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
356 if (mpc->mpc_ops->mpo_ ## check != NULL) { \
357 if (mpc->mpc_ops->mpo_ ## check (args) \
362 mac_policy_sunlock_nosleep(&tracker); \
367 * MAC_POLICY_BOOLEAN performs the designated boolean composition by walking
368 * the module list, invoking each instance of the operation, and combining
369 * the results using the passed C operator. Note that it returns its value
370 * via 'result' in the scope of the caller, which should be initialized by
371 * the caller in a meaningful way to get a meaningful result.
373 #define MAC_POLICY_BOOLEAN(operation, composition, args...) do { \
374 struct mac_policy_conf *mpc; \
376 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
377 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
378 result = result composition \
379 mpc->mpc_ops->mpo_ ## operation (args); \
381 if (!LIST_EMPTY(&mac_policy_list)) { \
382 mac_policy_slock_sleep(); \
383 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
384 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
385 result = result composition \
386 mpc->mpc_ops->mpo_ ## operation \
389 mac_policy_sunlock_sleep(); \
393 #define MAC_POLICY_BOOLEAN_NOSLEEP(operation, composition, args...) do {\
394 struct mac_policy_conf *mpc; \
396 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
397 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
398 result = result composition \
399 mpc->mpc_ops->mpo_ ## operation (args); \
401 if (!LIST_EMPTY(&mac_policy_list)) { \
402 struct rm_priotracker tracker; \
404 mac_policy_slock_nosleep(&tracker); \
405 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
406 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
407 result = result composition \
408 mpc->mpc_ops->mpo_ ## operation \
411 mac_policy_sunlock_nosleep(&tracker); \
416 * MAC_POLICY_EXTERNALIZE queries each policy to see if it can generate an
417 * externalized version of a label element by name. Policies declare whether
418 * they have matched a particular element name, parsed from the string by
419 * MAC_POLICY_EXTERNALIZE, and an error is returned if any element is matched
422 #define MAC_POLICY_EXTERNALIZE(type, label, elementlist, outbuf, \
424 int claimed, first, ignorenotfound, savedlen; \
425 char *element_name, *element_temp; \
430 sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN); \
431 element_temp = elementlist; \
432 while ((element_name = strsep(&element_temp, ",")) != NULL) { \
433 if (element_name[0] == '?') { \
435 ignorenotfound = 1; \
437 ignorenotfound = 0; \
438 savedlen = sbuf_len(&sb); \
440 error = sbuf_printf(&sb, "%s/", element_name); \
442 error = sbuf_printf(&sb, ",%s/", element_name); \
444 error = EINVAL; /* XXX: E2BIG? */ \
448 MAC_POLICY_CHECK(type ## _externalize_label, label, \
449 element_name, &sb, &claimed); \
452 if (claimed == 0 && ignorenotfound) { \
453 /* Revert last label name. */ \
454 sbuf_setpos(&sb, savedlen); \
455 } else if (claimed != 1) { \
456 error = EINVAL; /* XXX: ENOLABEL? */ \
466 * MAC_POLICY_INTERNALIZE presents parsed element names and data to each
467 * policy to see if any is willing to claim it and internalize the label
468 * data. If no policies match, an error is returned.
470 #define MAC_POLICY_INTERNALIZE(type, label, instring) do { \
471 char *element, *element_name, *element_data; \
475 element = instring; \
476 while ((element_name = strsep(&element, ",")) != NULL) { \
477 element_data = element_name; \
478 element_name = strsep(&element_data, "/"); \
479 if (element_data == NULL) { \
484 MAC_POLICY_CHECK(type ## _internalize_label, label, \
485 element_name, element_data, &claimed); \
488 if (claimed != 1) { \
489 /* XXXMAC: Another error here? */ \
497 * MAC_POLICY_PERFORM performs the designated operation by walking the policy
498 * module list and invoking that operation for each policy.
500 #define MAC_POLICY_PERFORM(operation, args...) do { \
501 struct mac_policy_conf *mpc; \
503 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
504 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
505 mpc->mpc_ops->mpo_ ## operation (args); \
507 if (!LIST_EMPTY(&mac_policy_list)) { \
508 mac_policy_slock_sleep(); \
509 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
510 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
511 mpc->mpc_ops->mpo_ ## operation (args); \
513 mac_policy_sunlock_sleep(); \
517 #define MAC_POLICY_PERFORM_NOSLEEP(operation, args...) do { \
518 struct mac_policy_conf *mpc; \
520 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \
521 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
522 mpc->mpc_ops->mpo_ ## operation (args); \
524 if (!LIST_EMPTY(&mac_policy_list)) { \
525 struct rm_priotracker tracker; \
527 mac_policy_slock_nosleep(&tracker); \
528 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
529 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
530 mpc->mpc_ops->mpo_ ## operation (args); \
532 mac_policy_sunlock_nosleep(&tracker); \
536 #endif /* !_SECURITY_MAC_MAC_INTERNAL_H_ */