xref: /freebsd/sys/security/mac/mac_internal.h (revision f4f8f02054f3abb6ceb84aefcdecc78d5c8b462f)
1 /*-
2  * Copyright (c) 1999-2002, 2006, 2009 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.
8  * All rights reserved.
9  *
10  * This software was developed by Robert Watson and Ilmar Habibulin for the
11  * TrustedBSD Project.
12  *
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.
17  *
18  * This software was developed by Robert N. M. Watson for the TrustedBSD
19  * Project under contract to nCircle Network Security, Inc.
20  *
21  * This software was enhanced by SPARTA ISSO under SPAWAR contract
22  * N66001-04-C-6019 ("SEFOS").
23  *
24  * This software was developed at the University of Cambridge Computer
25  * Laboratory with support from a grant from Google, Inc.
26  *
27  * Redistribution and use in source and binary forms, with or without
28  * modification, are permitted provided that the following conditions
29  * are met:
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.
35  *
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
46  * SUCH DAMAGE.
47  *
48  * $FreeBSD$
49  */
50 
51 #ifndef _SECURITY_MAC_MAC_INTERNAL_H_
52 #define	_SECURITY_MAC_MAC_INTERNAL_H_
53 
54 #ifndef _KERNEL
55 #error "no user-serviceable parts inside"
56 #endif
57 
58 /*
59  * MAC Framework sysctl namespace.
60  */
61 #ifdef SYSCTL_DECL
62 SYSCTL_DECL(_security_mac);
63 #endif /* SYSCTL_DECL */
64 
65 /*
66  * MAC Framework SDT DTrace probe namespace, macros for declaring entry
67  * point probes, macros for invoking them.
68  */
69 #ifdef SDT_PROVIDER_DECLARE
70 SDT_PROVIDER_DECLARE(mac);		/* MAC Framework-level events. */
71 SDT_PROVIDER_DECLARE(mac_framework);	/* Entry points to MAC. */
72 
73 #define	MAC_CHECK_PROBE_DEFINE4(name, arg0, arg1, arg2, arg3)		\
74 	SDT_PROBE_DEFINE5(mac_framework, kernel, name, mac_check_err,	\
75 	    "int", arg0, arg1, arg2, arg3);				\
76 	SDT_PROBE_DEFINE5(mac_framework, kernel, name, mac_check_ok,	\
77 	    "int", arg0, arg1, arg2, arg3);
78 
79 #define	MAC_CHECK_PROBE_DEFINE3(name, arg0, arg1, arg2)			\
80 	SDT_PROBE_DEFINE4(mac_framework, kernel, name, mac_check_err,	\
81 	    "int", arg0, arg1, arg2);					\
82 	SDT_PROBE_DEFINE4(mac_framework, kernel, name, mac_check_ok,	\
83 	    "int", arg0, arg1, arg2);
84 
85 #define	MAC_CHECK_PROBE_DEFINE2(name, arg0, arg1)			\
86 	SDT_PROBE_DEFINE3(mac_framework, kernel, name, mac_check_err,	\
87 	    "int", arg0, arg1);						\
88 	SDT_PROBE_DEFINE3(mac_framework, kernel, name, mac_check_ok,	\
89 	    "int", arg0, arg1);
90 
91 #define	MAC_CHECK_PROBE_DEFINE1(name, arg0)				\
92 	SDT_PROBE_DEFINE2(mac_framework, kernel, name, mac_check_err,	\
93 	    "int", arg0);						\
94 	SDT_PROBE_DEFINE2(mac_framework, kernel, name, mac_check_ok,	\
95 	    "int", arg0);
96 
97 #define	MAC_CHECK_PROBE4(name, error, arg0, arg1, arg2, arg3)	do {	\
98 	if (error) {							\
99 		SDT_PROBE(mac_framework, kernel, name, mac_check_err,	\
100 		    error, arg0, arg1, arg2, arg3);			\
101 	} else {							\
102 		SDT_PROBE(mac_framework, kernel, name, mac_check_ok,	\
103 		    0, arg0, arg1, arg2, arg3);				\
104 	}								\
105 } while (0)
106 
107 #define	MAC_CHECK_PROBE3(name, error, arg0, arg1, arg2)			\
108 	MAC_CHECK_PROBE4(name, error, arg0, arg1, arg2, 0)
109 #define	MAC_CHECK_PROBE2(name, error, arg0, arg1)			\
110 	MAC_CHECK_PROBE3(name, error, arg0, arg1, 0)
111 #define	MAC_CHECK_PROBE1(name, error, arg0)				\
112 	MAC_CHECK_PROBE2(name, error, arg0, 0)
113 #endif
114 
115 #define	MAC_GRANT_PROBE_DEFINE2(name, arg0, arg1)			\
116 	SDT_PROBE_DEFINE3(mac_framework, kernel, name, mac_grant_err,	\
117 	    "int", arg0, arg1);						\
118 	SDT_PROBE_DEFINE3(mac_framework, kernel, name, mac_grant_ok,	\
119 	    "INT", arg0, arg1);
120 
121 #define	MAC_GRANT_PROBE2(name, error, arg0, arg1)	do {		\
122 	if (error) {							\
123 		SDT_PROBE(mac_framework, kernel, name, mac_grant_err,	\
124 		    error, arg0, arg1, 0, 0);				\
125 	} else {							\
126 		SDT_PROBE(mac_framework, kernel, name, mac_grant_ok,	\
127 		    error, arg0, arg1, 0, 0);				\
128 	}								\
129 } while (0)
130 
131 /*
132  * MAC Framework global types and typedefs.
133  */
134 LIST_HEAD(mac_policy_list_head, mac_policy_conf);
135 #ifdef MALLOC_DECLARE
136 MALLOC_DECLARE(M_MACTEMP);
137 #endif
138 
139 /*
140  * MAC labels -- in-kernel storage format.
141  *
142  * In general, struct label pointers are embedded in kernel data structures
143  * representing objects that may be labeled (and protected).  Struct label is
144  * opaque to both kernel services that invoke the MAC Framework and MAC
145  * policy modules.  In particular, we do not wish to encode the layout of the
146  * label structure into any ABIs.  Historically, the slot array contained
147  * unions of {long, void} but now contains uintptr_t.
148  */
149 #define	MAC_MAX_SLOTS	4
150 #define	MAC_FLAG_INITIALIZED	0x0000001	/* Is initialized for use. */
151 struct label {
152 	int		l_flags;
153 	intptr_t	l_perpolicy[MAC_MAX_SLOTS];
154 };
155 
156 
157 /*
158  * Flags for mac_labeled, a bitmask of object types need across the union of
159  * all policies currently registered with the MAC Framework, used to key
160  * whether or not labels are allocated and constructors for the type are
161  * invoked.
162  */
163 #define	MPC_OBJECT_CRED			0x0000000000000001
164 #define	MPC_OBJECT_PROC			0x0000000000000002
165 #define	MPC_OBJECT_VNODE		0x0000000000000004
166 #define	MPC_OBJECT_INPCB		0x0000000000000008
167 #define	MPC_OBJECT_SOCKET		0x0000000000000010
168 #define	MPC_OBJECT_DEVFS		0x0000000000000020
169 #define	MPC_OBJECT_MBUF			0x0000000000000040
170 #define	MPC_OBJECT_IPQ			0x0000000000000080
171 #define	MPC_OBJECT_IFNET		0x0000000000000100
172 #define	MPC_OBJECT_BPFDESC		0x0000000000000200
173 #define	MPC_OBJECT_PIPE			0x0000000000000400
174 #define	MPC_OBJECT_MOUNT		0x0000000000000800
175 #define	MPC_OBJECT_POSIXSEM		0x0000000000001000
176 #define	MPC_OBJECT_POSIXSHM		0x0000000000002000
177 #define	MPC_OBJECT_SYSVMSG		0x0000000000004000
178 #define	MPC_OBJECT_SYSVMSQ		0x0000000000008000
179 #define	MPC_OBJECT_SYSVSEM		0x0000000000010000
180 #define	MPC_OBJECT_SYSVSHM		0x0000000000020000
181 #define	MPC_OBJECT_SYNCACHE		0x0000000000040000
182 #define	MPC_OBJECT_IP6Q			0x0000000000080000
183 
184 /*
185  * MAC Framework global variables.
186  */
187 extern struct mac_policy_list_head	mac_policy_list;
188 extern struct mac_policy_list_head	mac_static_policy_list;
189 extern uint64_t				mac_labeled;
190 extern struct mtx			mac_ifnet_mtx;
191 
192 /*
193  * MAC Framework infrastructure functions.
194  */
195 int	mac_error_select(int error1, int error2);
196 
197 void	mac_policy_slock_nosleep(void);
198 void	mac_policy_slock_sleep(void);
199 void	mac_policy_sunlock_nosleep(void);
200 void	mac_policy_sunlock_sleep(void);
201 
202 struct label	*mac_labelzone_alloc(int flags);
203 void		 mac_labelzone_free(struct label *label);
204 void		 mac_labelzone_init(void);
205 
206 void	mac_init_label(struct label *label);
207 void	mac_destroy_label(struct label *label);
208 int	mac_check_structmac_consistent(struct mac *mac);
209 int	mac_allocate_slot(void);
210 
211 #define MAC_IFNET_LOCK(ifp)	mtx_lock(&mac_ifnet_mtx)
212 #define MAC_IFNET_UNLOCK(ifp)	mtx_unlock(&mac_ifnet_mtx)
213 
214 /*
215  * MAC Framework per-object type functions.  It's not yet clear how the
216  * namespaces, etc, should work for these, so for now, sort by object type.
217  */
218 struct label	*mac_cred_label_alloc(void);
219 void		 mac_cred_label_free(struct label *label);
220 struct label	*mac_pipe_label_alloc(void);
221 void		 mac_pipe_label_free(struct label *label);
222 struct label	*mac_socket_label_alloc(int flag);
223 void		 mac_socket_label_free(struct label *label);
224 struct label	*mac_vnode_label_alloc(void);
225 void		 mac_vnode_label_free(struct label *label);
226 
227 int	mac_cred_check_relabel(struct ucred *cred, struct label *newlabel);
228 int	mac_cred_externalize_label(struct label *label, char *elements,
229 	    char *outbuf, size_t outbuflen);
230 int	mac_cred_internalize_label(struct label *label, char *string);
231 void	mac_cred_relabel(struct ucred *cred, struct label *newlabel);
232 
233 struct label	*mac_mbuf_to_label(struct mbuf *m);
234 
235 void	mac_pipe_copy_label(struct label *src, struct label *dest);
236 int	mac_pipe_externalize_label(struct label *label, char *elements,
237 	    char *outbuf, size_t outbuflen);
238 int	mac_pipe_internalize_label(struct label *label, char *string);
239 
240 int	mac_socket_label_set(struct ucred *cred, struct socket *so,
241 	    struct label *label);
242 void	mac_socket_copy_label(struct label *src, struct label *dest);
243 int	mac_socket_externalize_label(struct label *label, char *elements,
244 	    char *outbuf, size_t outbuflen);
245 int	mac_socket_internalize_label(struct label *label, char *string);
246 
247 int	mac_vnode_externalize_label(struct label *label, char *elements,
248 	    char *outbuf, size_t outbuflen);
249 int	mac_vnode_internalize_label(struct label *label, char *string);
250 void	mac_vnode_check_mmap_downgrade(struct ucred *cred, struct vnode *vp,
251 	    int *prot);
252 int	vn_setlabel(struct vnode *vp, struct label *intlabel,
253 	    struct ucred *cred);
254 
255 /*
256  * MAC Framework composition macros invoke all registered MAC policies for a
257  * specific entry point.  They come in two forms: one which permits policies
258  * to sleep/block, and another that does not.
259  *
260  * MAC_POLICY_CHECK performs the designated check by walking the policy
261  * module list and checking with each as to how it feels about the request.
262  * Note that it returns its value via 'error' in the scope of the caller.
263  */
264 #define	MAC_POLICY_CHECK(check, args...) do {				\
265 	struct mac_policy_conf *mpc;					\
266 									\
267 	error = 0;							\
268 	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
269 		if (mpc->mpc_ops->mpo_ ## check != NULL)		\
270 			error = mac_error_select(			\
271 			    mpc->mpc_ops->mpo_ ## check (args),		\
272 			    error);					\
273 	}								\
274 	if (!LIST_EMPTY(&mac_policy_list)) {				\
275 		mac_policy_slock_sleep();				\
276 		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
277 			if (mpc->mpc_ops->mpo_ ## check != NULL)	\
278 				error = mac_error_select(		\
279 				    mpc->mpc_ops->mpo_ ## check (args),	\
280 				    error);				\
281 		}							\
282 		mac_policy_sunlock_sleep();				\
283 	}								\
284 } while (0)
285 
286 #define	MAC_POLICY_CHECK_NOSLEEP(check, args...) do {			\
287 	struct mac_policy_conf *mpc;					\
288 									\
289 	error = 0;							\
290 	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
291 		if (mpc->mpc_ops->mpo_ ## check != NULL)		\
292 			error = mac_error_select(			\
293 			    mpc->mpc_ops->mpo_ ## check (args),		\
294 			    error);					\
295 	}								\
296 	if (!LIST_EMPTY(&mac_policy_list)) {				\
297 		mac_policy_slock_nosleep();				\
298 		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
299 			if (mpc->mpc_ops->mpo_ ## check != NULL)	\
300 				error = mac_error_select(		\
301 				    mpc->mpc_ops->mpo_ ## check (args),	\
302 				    error);				\
303 		}							\
304 		mac_policy_sunlock_nosleep();				\
305 	}								\
306 } while (0)
307 
308 /*
309  * MAC_POLICY_GRANT performs the designated check by walking the policy
310  * module list and checking with each as to how it feels about the request.
311  * Unlike MAC_POLICY_CHECK, it grants if any policies return '0', and
312  * otherwise returns EPERM.  Note that it returns its value via 'error' in
313  * the scope of the caller.
314  */
315 #define	MAC_POLICY_GRANT_NOSLEEP(check, args...) do {			\
316 	struct mac_policy_conf *mpc;					\
317 									\
318 	error = EPERM;							\
319 	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
320 		if (mpc->mpc_ops->mpo_ ## check != NULL) {		\
321 			if (mpc->mpc_ops->mpo_ ## check(args) == 0)	\
322 				error = 0;				\
323 		}							\
324 	}								\
325 	if (!LIST_EMPTY(&mac_policy_list)) {				\
326 		mac_policy_slock_nosleep();				\
327 		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
328 			if (mpc->mpc_ops->mpo_ ## check != NULL) {	\
329 				if (mpc->mpc_ops->mpo_ ## check (args)	\
330 				    == 0)				\
331 					error = 0;			\
332 			}						\
333 		}							\
334 		mac_policy_sunlock_nosleep();				\
335 	}								\
336 } while (0)
337 
338 /*
339  * MAC_POLICY_BOOLEAN performs the designated boolean composition by walking
340  * the module list, invoking each instance of the operation, and combining
341  * the results using the passed C operator.  Note that it returns its value
342  * via 'result' in the scope of the caller, which should be initialized by
343  * the caller in a meaningful way to get a meaningful result.
344  */
345 #define	MAC_POLICY_BOOLEAN(operation, composition, args...) do {	\
346 	struct mac_policy_conf *mpc;					\
347 									\
348 	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
349 		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
350 			result = result composition			\
351 			    mpc->mpc_ops->mpo_ ## operation (args);	\
352 	}								\
353 	if (!LIST_EMPTY(&mac_policy_list)) {				\
354 		mac_policy_slock_sleep();				\
355 		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
356 			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
357 				result = result composition		\
358 				    mpc->mpc_ops->mpo_ ## operation	\
359 				    (args);				\
360 		}							\
361 		mac_policy_sunlock_sleep();				\
362 	}								\
363 } while (0)
364 
365 #define	MAC_POLICY_BOOLEAN_NOSLEEP(operation, composition, args...) do {\
366 	struct mac_policy_conf *mpc;					\
367 									\
368 	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
369 		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
370 			result = result composition			\
371 			    mpc->mpc_ops->mpo_ ## operation (args);	\
372 	}								\
373 	if (!LIST_EMPTY(&mac_policy_list)) {				\
374 		mac_policy_slock_nosleep();				\
375 		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
376 			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
377 				result = result composition		\
378 				    mpc->mpc_ops->mpo_ ## operation	\
379 				    (args);				\
380 		}							\
381 		mac_policy_sunlock_nosleep();				\
382 	}								\
383 } while (0)
384 
385 /*
386  * MAC_POLICY_EXTERNALIZE queries each policy to see if it can generate an
387  * externalized version of a label element by name.  Policies declare whether
388  * they have matched a particular element name, parsed from the string by
389  * MAC_POLICY_EXTERNALIZE, and an error is returned if any element is matched
390  * by no policy.
391  */
392 #define	MAC_POLICY_EXTERNALIZE(type, label, elementlist, outbuf, 	\
393     outbuflen) do {							\
394 	int claimed, first, ignorenotfound, savedlen;			\
395 	char *element_name, *element_temp;				\
396 	struct sbuf sb;							\
397 									\
398 	error = 0;							\
399 	first = 1;							\
400 	sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN);		\
401 	element_temp = elementlist;					\
402 	while ((element_name = strsep(&element_temp, ",")) != NULL) {	\
403 		if (element_name[0] == '?') {				\
404 			element_name++;					\
405 			ignorenotfound = 1;				\
406 		 } else							\
407 			ignorenotfound = 0;				\
408 		savedlen = sbuf_len(&sb);				\
409 		if (first)						\
410 			error = sbuf_printf(&sb, "%s/", element_name);	\
411 		else							\
412 			error = sbuf_printf(&sb, ",%s/", element_name);	\
413 		if (error == -1) {					\
414 			error = EINVAL;	/* XXX: E2BIG? */		\
415 			break;						\
416 		}							\
417 		claimed = 0;						\
418 		MAC_POLICY_CHECK(type ## _externalize_label, label,	\
419 		    element_name, &sb, &claimed);			\
420 		if (error)						\
421 			break;						\
422 		if (claimed == 0 && ignorenotfound) {			\
423 			/* Revert last label name. */			\
424 			sbuf_setpos(&sb, savedlen);			\
425 		} else if (claimed != 1) {				\
426 			error = EINVAL;	/* XXX: ENOLABEL? */		\
427 			break;						\
428 		} else {						\
429 			first = 0;					\
430 		}							\
431 	}								\
432 	sbuf_finish(&sb);						\
433 } while (0)
434 
435 /*
436  * MAC_POLICY_INTERNALIZE presents parsed element names and data to each
437  * policy to see if any is willing to claim it and internalize the label
438  * data.  If no policies match, an error is returned.
439  */
440 #define	MAC_POLICY_INTERNALIZE(type, label, instring) do {		\
441 	char *element, *element_name, *element_data;			\
442 	int claimed;							\
443 									\
444 	error = 0;							\
445 	element = instring;						\
446 	while ((element_name = strsep(&element, ",")) != NULL) {	\
447 		element_data = element_name;				\
448 		element_name = strsep(&element_data, "/");		\
449 		if (element_data == NULL) {				\
450 			error = EINVAL;					\
451 			break;						\
452 		}							\
453 		claimed = 0;						\
454 		MAC_POLICY_CHECK(type ## _internalize_label, label,	\
455 		    element_name, element_data, &claimed);		\
456 		if (error)						\
457 			break;						\
458 		if (claimed != 1) {					\
459 			/* XXXMAC: Another error here? */		\
460 			error = EINVAL;					\
461 			break;						\
462 		}							\
463 	}								\
464 } while (0)
465 
466 /*
467  * MAC_POLICY_PERFORM performs the designated operation by walking the policy
468  * module list and invoking that operation for each policy.
469  */
470 #define	MAC_POLICY_PERFORM(operation, args...) do {			\
471 	struct mac_policy_conf *mpc;					\
472 									\
473 	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
474 		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
475 			mpc->mpc_ops->mpo_ ## operation (args);		\
476 	}								\
477 	if (!LIST_EMPTY(&mac_policy_list)) {				\
478 		mac_policy_slock_sleep();				\
479 		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
480 			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
481 				mpc->mpc_ops->mpo_ ## operation (args);	\
482 		}							\
483 		mac_policy_sunlock_sleep();				\
484 	}								\
485 } while (0)
486 
487 #define	MAC_POLICY_PERFORM_NOSLEEP(operation, args...) do {		\
488 	struct mac_policy_conf *mpc;					\
489 									\
490 	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
491 		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
492 			mpc->mpc_ops->mpo_ ## operation (args);		\
493 	}								\
494 	if (!LIST_EMPTY(&mac_policy_list)) {				\
495 		mac_policy_slock_nosleep();				\
496 		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
497 			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
498 				mpc->mpc_ops->mpo_ ## operation (args);	\
499 		}							\
500 		mac_policy_sunlock_nosleep();				\
501 	}								\
502 } while (0)
503 
504 #endif /* !_SECURITY_MAC_MAC_INTERNAL_H_ */
505