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