mac_internal.h revision 162383
1/*-
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.
5 * All rights reserved.
6 *
7 * This software was developed by Robert Watson and Ilmar Habibulin for the
8 * TrustedBSD Project.
9 *
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.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 *
36 * $FreeBSD: head/sys/security/mac/mac_internal.h 162383 2006-09-17 20:00:36Z rwatson $
37 */
38
39/*
40 * MAC Framework sysctl namespace.
41 */
42#ifdef SYSCTL_DECL
43SYSCTL_DECL(_security_mac);
44#ifdef MAC_DEBUG
45SYSCTL_DECL(_security_mac_debug);
46SYSCTL_DECL(_security_mac_debug_counters);
47#endif
48#endif /* SYSCTL_DECL */
49
50/*
51 * MAC Framework global types and typedefs.
52 */
53LIST_HEAD(mac_policy_list_head, mac_policy_conf);
54#ifdef MALLOC_DECLARE
55MALLOC_DECLARE(M_MACTEMP);
56#endif
57
58/*
59 * MAC Framework global variables.
60 */
61extern struct mac_policy_list_head	mac_policy_list;
62extern struct mac_policy_list_head	mac_static_policy_list;
63extern int				mac_late;
64extern int				mac_enforce_network;
65extern int				mac_enforce_process;
66extern int				mac_enforce_socket;
67extern int				mac_enforce_vm;
68#ifndef MAC_ALWAYS_LABEL_MBUF
69extern int				mac_labelmbufs;
70#endif
71
72/*
73 * MAC Framework object/access counter primitives, conditionally
74 * compiled.
75 */
76#ifdef MAC_DEBUG
77#define	MAC_DEBUG_COUNTER_INC(x)	atomic_add_int(x, 1);
78#define	MAC_DEBUG_COUNTER_DEC(x)	atomic_subtract_int(x, 1);
79#else
80#define	MAC_DEBUG_COUNTER_INC(x)
81#define	MAC_DEBUG_COUNTER_DEC(x)
82#endif
83
84/*
85 * MAC Framework infrastructure functions.
86 */
87int	mac_error_select(int error1, int error2);
88
89void	mac_policy_grab_exclusive(void);
90void	mac_policy_assert_exclusive(void);
91void	mac_policy_release_exclusive(void);
92void	mac_policy_list_busy(void);
93int	mac_policy_list_conditional_busy(void);
94void	mac_policy_list_unbusy(void);
95
96struct label	*mac_labelzone_alloc(int flags);
97void		 mac_labelzone_free(struct label *label);
98void		 mac_labelzone_init(void);
99
100void	mac_init_label(struct label *label);
101void	mac_destroy_label(struct label *label);
102int	mac_check_structmac_consistent(struct mac *mac);
103int	mac_allocate_slot(void);
104
105/*
106 * MAC Framework per-object type functions.  It's not yet clear how
107 * the namespaces, etc, should work for these, so for now, sort by
108 * object type.
109 */
110struct label	*mac_pipe_label_alloc(void);
111void		 mac_pipe_label_free(struct label *label);
112struct label	*mac_socket_label_alloc(int flag);
113void		 mac_socket_label_free(struct label *label);
114
115int	mac_check_cred_relabel(struct ucred *cred, struct label *newlabel);
116int	mac_externalize_cred_label(struct label *label, char *elements,
117	    char *outbuf, size_t outbuflen);
118int	mac_internalize_cred_label(struct label *label, char *string);
119void	mac_relabel_cred(struct ucred *cred, struct label *newlabel);
120
121struct label	*mac_mbuf_to_label(struct mbuf *m);
122
123void	mac_copy_pipe_label(struct label *src, struct label *dest);
124int	mac_externalize_pipe_label(struct label *label, char *elements,
125	    char *outbuf, size_t outbuflen);
126int	mac_internalize_pipe_label(struct label *label, char *string);
127
128int	mac_socket_label_set(struct ucred *cred, struct socket *so,
129	    struct label *label);
130void	mac_copy_socket_label(struct label *src, struct label *dest);
131int	mac_externalize_socket_label(struct label *label, char *elements,
132	    char *outbuf, size_t outbuflen);
133int	mac_internalize_socket_label(struct label *label, char *string);
134
135int	mac_externalize_vnode_label(struct label *label, char *elements,
136	    char *outbuf, size_t outbuflen);
137int	mac_internalize_vnode_label(struct label *label, char *string);
138void	mac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp,
139	    int *prot);
140int	vn_setlabel(struct vnode *vp, struct label *intlabel,
141	    struct ucred *cred);
142
143/*
144 * MAC_CHECK performs the designated check by walking the policy module
145 * list and checking with each as to how it feels about the request.
146 * Note that it returns its value via 'error' in the scope of the caller.
147 */
148#define	MAC_CHECK(check, args...) do {					\
149	struct mac_policy_conf *mpc;					\
150	int entrycount;							\
151									\
152	error = 0;							\
153	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
154		if (mpc->mpc_ops->mpo_ ## check != NULL)		\
155			error = mac_error_select(			\
156			    mpc->mpc_ops->mpo_ ## check (args),		\
157			    error);					\
158	}								\
159	if ((entrycount = mac_policy_list_conditional_busy()) != 0) {	\
160		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
161			if (mpc->mpc_ops->mpo_ ## check != NULL)	\
162				error = mac_error_select(		\
163				    mpc->mpc_ops->mpo_ ## check (args),	\
164				    error);				\
165		}							\
166		mac_policy_list_unbusy();				\
167	}								\
168} while (0)
169
170/*
171 * MAC_BOOLEAN performs the designated boolean composition by walking
172 * the module list, invoking each instance of the operation, and
173 * combining the results using the passed C operator.  Note that it
174 * returns its value via 'result' in the scope of the caller, which
175 * should be initialized by the caller in a meaningful way to get
176 * a meaningful result.
177 */
178#define	MAC_BOOLEAN(operation, composition, args...) do {		\
179	struct mac_policy_conf *mpc;					\
180	int entrycount;							\
181									\
182	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
183		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
184			result = result composition			\
185			    mpc->mpc_ops->mpo_ ## operation (args);	\
186	}								\
187	if ((entrycount = mac_policy_list_conditional_busy()) != 0) {	\
188		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
189			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
190				result = result composition		\
191				    mpc->mpc_ops->mpo_ ## operation	\
192				    (args);				\
193		}							\
194		mac_policy_list_unbusy();				\
195	}								\
196} while (0)
197
198#define	MAC_EXTERNALIZE(type, label, elementlist, outbuf, 		\
199    outbuflen) do {							\
200	int claimed, first, ignorenotfound, savedlen;			\
201	char *element_name, *element_temp;				\
202	struct sbuf sb;							\
203									\
204	error = 0;							\
205	first = 1;							\
206	sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN);		\
207	element_temp = elementlist;					\
208	while ((element_name = strsep(&element_temp, ",")) != NULL) {	\
209		if (element_name[0] == '?') {				\
210			element_name++;					\
211			ignorenotfound = 1;				\
212		 } else							\
213			ignorenotfound = 0;				\
214		savedlen = sbuf_len(&sb);				\
215		if (first)						\
216			error = sbuf_printf(&sb, "%s/", element_name);	\
217		else							\
218			error = sbuf_printf(&sb, ",%s/", element_name);	\
219		if (error == -1) {					\
220			error = EINVAL;	/* XXX: E2BIG? */		\
221			break;						\
222		}							\
223		claimed = 0;						\
224		MAC_CHECK(externalize_ ## type ## _label, label,	\
225		    element_name, &sb, &claimed);			\
226		if (error)						\
227			break;						\
228		if (claimed == 0 && ignorenotfound) {			\
229			/* Revert last label name. */			\
230			sbuf_setpos(&sb, savedlen);			\
231		} else if (claimed != 1) {				\
232			error = EINVAL;	/* XXX: ENOLABEL? */		\
233			break;						\
234		} else {						\
235			first = 0;					\
236		}							\
237	}								\
238	sbuf_finish(&sb);						\
239} while (0)
240
241#define	MAC_INTERNALIZE(type, label, instring) do {			\
242	char *element, *element_name, *element_data;			\
243	int claimed;							\
244									\
245	error = 0;							\
246	element = instring;						\
247	while ((element_name = strsep(&element, ",")) != NULL) {	\
248		element_data = element_name;				\
249		element_name = strsep(&element_data, "/");		\
250		if (element_data == NULL) {				\
251			error = EINVAL;					\
252			break;						\
253		}							\
254		claimed = 0;						\
255		MAC_CHECK(internalize_ ## type ## _label, label,	\
256		    element_name, element_data, &claimed);		\
257		if (error)						\
258			break;						\
259		if (claimed != 1) {					\
260			/* XXXMAC: Another error here? */		\
261			error = EINVAL;					\
262			break;						\
263		}							\
264	}								\
265} while (0)
266
267/*
268 * MAC_PERFORM performs the designated operation by walking the policy
269 * module list and invoking that operation for each policy.
270 */
271#define	MAC_PERFORM(operation, args...) do {				\
272	struct mac_policy_conf *mpc;					\
273	int entrycount;							\
274									\
275	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
276		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
277			mpc->mpc_ops->mpo_ ## operation (args);		\
278	}								\
279	if ((entrycount = mac_policy_list_conditional_busy()) != 0) {	\
280		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
281			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
282				mpc->mpc_ops->mpo_ ## operation (args);	\
283		}							\
284		mac_policy_list_unbusy();				\
285	}								\
286} while (0)
287