mac_internal.h revision 189503
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: head/sys/security/mac/mac_internal.h 189503 2009-03-08 00:50:37Z rwatson $
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
62SYSCTL_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
70SDT_PROVIDER_DECLARE(mac);		/* MAC Framework-level events. */
71SDT_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 */
134LIST_HEAD(mac_policy_list_head, mac_policy_conf);
135#ifdef MALLOC_DECLARE
136MALLOC_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. */
151struct 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 */
187extern struct mac_policy_list_head	mac_policy_list;
188extern struct mac_policy_list_head	mac_static_policy_list;
189extern uint64_t				mac_labeled;
190extern struct mtx			mac_ifnet_mtx;
191
192/*
193 * MAC Framework infrastructure functions.
194 */
195int	mac_error_select(int error1, int error2);
196
197void	mac_policy_grab_exclusive(void);
198void	mac_policy_assert_exclusive(void);
199void	mac_policy_release_exclusive(void);
200void	mac_policy_list_busy(void);
201int	mac_policy_list_conditional_busy(void);
202void	mac_policy_list_unbusy(void);
203
204struct label	*mac_labelzone_alloc(int flags);
205void		 mac_labelzone_free(struct label *label);
206void		 mac_labelzone_init(void);
207
208void	mac_init_label(struct label *label);
209void	mac_destroy_label(struct label *label);
210int	mac_check_structmac_consistent(struct mac *mac);
211int	mac_allocate_slot(void);
212
213#define MAC_IFNET_LOCK(ifp)	mtx_lock(&mac_ifnet_mtx)
214#define MAC_IFNET_UNLOCK(ifp)	mtx_unlock(&mac_ifnet_mtx)
215
216/*
217 * MAC Framework per-object type functions.  It's not yet clear how the
218 * namespaces, etc, should work for these, so for now, sort by object type.
219 */
220struct label	*mac_cred_label_alloc(void);
221void		 mac_cred_label_free(struct label *label);
222struct label	*mac_pipe_label_alloc(void);
223void		 mac_pipe_label_free(struct label *label);
224struct label	*mac_socket_label_alloc(int flag);
225void		 mac_socket_label_free(struct label *label);
226struct label	*mac_vnode_label_alloc(void);
227void		 mac_vnode_label_free(struct label *label);
228
229int	mac_cred_check_relabel(struct ucred *cred, struct label *newlabel);
230int	mac_cred_externalize_label(struct label *label, char *elements,
231	    char *outbuf, size_t outbuflen);
232int	mac_cred_internalize_label(struct label *label, char *string);
233void	mac_cred_relabel(struct ucred *cred, struct label *newlabel);
234
235struct label	*mac_mbuf_to_label(struct mbuf *m);
236
237void	mac_pipe_copy_label(struct label *src, struct label *dest);
238int	mac_pipe_externalize_label(struct label *label, char *elements,
239	    char *outbuf, size_t outbuflen);
240int	mac_pipe_internalize_label(struct label *label, char *string);
241
242int	mac_socket_label_set(struct ucred *cred, struct socket *so,
243	    struct label *label);
244void	mac_socket_copy_label(struct label *src, struct label *dest);
245int	mac_socket_externalize_label(struct label *label, char *elements,
246	    char *outbuf, size_t outbuflen);
247int	mac_socket_internalize_label(struct label *label, char *string);
248
249int	mac_vnode_externalize_label(struct label *label, char *elements,
250	    char *outbuf, size_t outbuflen);
251int	mac_vnode_internalize_label(struct label *label, char *string);
252void	mac_vnode_check_mmap_downgrade(struct ucred *cred, struct vnode *vp,
253	    int *prot);
254int	vn_setlabel(struct vnode *vp, struct label *intlabel,
255	    struct ucred *cred);
256
257/*
258 * MAC_CHECK performs the designated check by walking the policy module list
259 * and checking with each as to how it feels about the request.  Note that it
260 * returns its value via 'error' in the scope of the caller.
261 */
262#define	MAC_CHECK(check, args...) do {					\
263	struct mac_policy_conf *mpc;					\
264	int entrycount;							\
265									\
266	error = 0;							\
267	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
268		if (mpc->mpc_ops->mpo_ ## check != NULL)		\
269			error = mac_error_select(			\
270			    mpc->mpc_ops->mpo_ ## check (args),		\
271			    error);					\
272	}								\
273	if ((entrycount = mac_policy_list_conditional_busy()) != 0) {	\
274		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
275			if (mpc->mpc_ops->mpo_ ## check != NULL)	\
276				error = mac_error_select(		\
277				    mpc->mpc_ops->mpo_ ## check (args),	\
278				    error);				\
279		}							\
280		mac_policy_list_unbusy();				\
281	}								\
282} while (0)
283
284/*
285 * MAC_GRANT performs the designated check by walking the policy module list
286 * and checking with each as to how it feels about the request.  Unlike
287 * MAC_CHECK, it grants if any policies return '0', and otherwise returns
288 * EPERM.  Note that it returns its value via 'error' in the scope of the
289 * caller.
290 */
291#define	MAC_GRANT(check, args...) do {					\
292	struct mac_policy_conf *mpc;					\
293	int entrycount;							\
294									\
295	error = EPERM;							\
296	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
297		if (mpc->mpc_ops->mpo_ ## check != NULL) {		\
298			if (mpc->mpc_ops->mpo_ ## check(args) == 0)	\
299				error = 0;				\
300		}							\
301	}								\
302	if ((entrycount = mac_policy_list_conditional_busy()) != 0) {	\
303		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
304			if (mpc->mpc_ops->mpo_ ## check != NULL) {	\
305				if (mpc->mpc_ops->mpo_ ## check (args)	\
306				    == 0)				\
307					error = 0;			\
308			}						\
309		}							\
310		mac_policy_list_unbusy();				\
311	}								\
312} while (0)
313
314/*
315 * MAC_BOOLEAN performs the designated boolean composition by walking the
316 * module list, invoking each instance of the operation, and combining the
317 * results using the passed C operator.  Note that it returns its value via
318 * 'result' in the scope of the caller, which should be initialized by the
319 * caller in a meaningful way to get a meaningful result.
320 */
321#define	MAC_BOOLEAN(operation, composition, args...) do {		\
322	struct mac_policy_conf *mpc;					\
323	int entrycount;							\
324									\
325	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
326		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
327			result = result composition			\
328			    mpc->mpc_ops->mpo_ ## operation (args);	\
329	}								\
330	if ((entrycount = mac_policy_list_conditional_busy()) != 0) {	\
331		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
332			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
333				result = result composition		\
334				    mpc->mpc_ops->mpo_ ## operation	\
335				    (args);				\
336		}							\
337		mac_policy_list_unbusy();				\
338	}								\
339} while (0)
340
341/*
342 * MAC_EXTERNALIZE queries each policy to see if it can generate an
343 * externalized version of a label element by name.  Policies declare whether
344 * they have matched a particular element name, parsed from the string by
345 * MAC_EXTERNALIZE, and an error is returned if any element is matched by no
346 * policy.
347 */
348#define	MAC_EXTERNALIZE(type, label, elementlist, outbuf, 		\
349    outbuflen) do {							\
350	int claimed, first, ignorenotfound, savedlen;			\
351	char *element_name, *element_temp;				\
352	struct sbuf sb;							\
353									\
354	error = 0;							\
355	first = 1;							\
356	sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN);		\
357	element_temp = elementlist;					\
358	while ((element_name = strsep(&element_temp, ",")) != NULL) {	\
359		if (element_name[0] == '?') {				\
360			element_name++;					\
361			ignorenotfound = 1;				\
362		 } else							\
363			ignorenotfound = 0;				\
364		savedlen = sbuf_len(&sb);				\
365		if (first)						\
366			error = sbuf_printf(&sb, "%s/", element_name);	\
367		else							\
368			error = sbuf_printf(&sb, ",%s/", element_name);	\
369		if (error == -1) {					\
370			error = EINVAL;	/* XXX: E2BIG? */		\
371			break;						\
372		}							\
373		claimed = 0;						\
374		MAC_CHECK(type ## _externalize_label, label,		\
375		    element_name, &sb, &claimed);			\
376		if (error)						\
377			break;						\
378		if (claimed == 0 && ignorenotfound) {			\
379			/* Revert last label name. */			\
380			sbuf_setpos(&sb, savedlen);			\
381		} else if (claimed != 1) {				\
382			error = EINVAL;	/* XXX: ENOLABEL? */		\
383			break;						\
384		} else {						\
385			first = 0;					\
386		}							\
387	}								\
388	sbuf_finish(&sb);						\
389} while (0)
390
391/*
392 * MAC_INTERNALIZE presents parsed element names and data to each policy to
393 * see if any is willing to claim it and internalize the label data.  If no
394 * policies match, an error is returned.
395 */
396#define	MAC_INTERNALIZE(type, label, instring) do {			\
397	char *element, *element_name, *element_data;			\
398	int claimed;							\
399									\
400	error = 0;							\
401	element = instring;						\
402	while ((element_name = strsep(&element, ",")) != NULL) {	\
403		element_data = element_name;				\
404		element_name = strsep(&element_data, "/");		\
405		if (element_data == NULL) {				\
406			error = EINVAL;					\
407			break;						\
408		}							\
409		claimed = 0;						\
410		MAC_CHECK(type ## _internalize_label, label,		\
411		    element_name, element_data, &claimed);		\
412		if (error)						\
413			break;						\
414		if (claimed != 1) {					\
415			/* XXXMAC: Another error here? */		\
416			error = EINVAL;					\
417			break;						\
418		}							\
419	}								\
420} while (0)
421
422/*
423 * MAC_PERFORM performs the designated operation by walking the policy module
424 * list and invoking that operation for each policy.
425 */
426#define	MAC_PERFORM(operation, args...) do {				\
427	struct mac_policy_conf *mpc;					\
428	int entrycount;							\
429									\
430	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
431		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
432			mpc->mpc_ops->mpo_ ## operation (args);		\
433	}								\
434	if ((entrycount = mac_policy_list_conditional_busy()) != 0) {	\
435		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
436			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
437				mpc->mpc_ops->mpo_ ## operation (args);	\
438		}							\
439		mac_policy_list_unbusy();				\
440	}								\
441} while (0)
442
443#endif /* !_SECURITY_MAC_MAC_INTERNAL_H_ */
444