mac_internal.h revision 122820
1/*-
2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001, 2002, 2003 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 122820 2003-11-16 23:31:45Z rwatson $
37 */
38
39/*
40 * MAC Framework sysctl namespace.
41 */
42#ifdef SYSCTL_DECL
43SYSCTL_DECL(_security);
44SYSCTL_DECL(_security_mac);
45#ifdef MAC_DEBUG
46SYSCTL_DECL(_security_mac_debug);
47SYSCTL_DECL(_security_mac_debug_counters);
48#endif
49#endif /* SYSCTL_DECL */
50
51/*
52 * MAC Framework global types and typedefs.
53 */
54LIST_HEAD(mac_policy_list_head, mac_policy_conf);
55#ifdef MALLOC_DECLARE
56MALLOC_DECLARE(M_MACTEMP);
57#endif
58
59/*
60 * MAC Framework global variables.
61 */
62extern struct mac_policy_list_head	mac_policy_list;
63extern struct mac_policy_list_head	mac_static_policy_list;
64extern int				mac_late;
65extern int				mac_enforce_process;
66extern int				mac_enforce_sysv;
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
121void	mac_copy_pipe_label(struct label *src, struct label *dest);
122int	mac_externalize_pipe_label(struct label *label, char *elements,
123	    char *outbuf, size_t outbuflen);
124int	mac_internalize_pipe_label(struct label *label, char *string);
125
126int	mac_socket_label_set(struct ucred *cred, struct socket *so,
127	    struct label *label);
128void	mac_copy_socket_label(struct label *src, struct label *dest);
129int	mac_externalize_socket_label(struct label *label, char *elements,
130	    char *outbuf, size_t outbuflen);
131int	mac_internalize_socket_label(struct label *label, char *string);
132
133int	mac_externalize_vnode_label(struct label *label, char *elements,
134	    char *outbuf, size_t outbuflen);
135int	mac_internalize_vnode_label(struct label *label, char *string);
136void	mac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp,
137	    int *prot);
138int	vn_setlabel(struct vnode *vp, struct label *intlabel,
139	    struct ucred *cred);
140
141/*
142 * MAC_CHECK performs the designated check by walking the policy module
143 * list and checking with each as to how it feels about the request.
144 * Note that it returns its value via 'error' in the scope of the caller.
145 */
146#define	MAC_CHECK(check, args...) do {					\
147	struct mac_policy_conf *mpc;					\
148	int entrycount;							\
149									\
150	error = 0;							\
151	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
152		if (mpc->mpc_ops->mpo_ ## check != NULL)		\
153			error = mac_error_select(			\
154			    mpc->mpc_ops->mpo_ ## check (args),		\
155			    error);					\
156	}								\
157	if ((entrycount = mac_policy_list_conditional_busy()) != 0) {	\
158		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
159			if (mpc->mpc_ops->mpo_ ## check != NULL)	\
160				error = mac_error_select(		\
161				    mpc->mpc_ops->mpo_ ## check (args),	\
162				    error);				\
163		}							\
164		mac_policy_list_unbusy();				\
165	}								\
166} while (0)
167
168/*
169 * MAC_BOOLEAN performs the designated boolean composition by walking
170 * the module list, invoking each instance of the operation, and
171 * combining the results using the passed C operator.  Note that it
172 * returns its value via 'result' in the scope of the caller, which
173 * should be initialized by the caller in a meaningful way to get
174 * a meaningful result.
175 */
176#define	MAC_BOOLEAN(operation, composition, args...) do {		\
177	struct mac_policy_conf *mpc;					\
178	int entrycount;							\
179									\
180	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
181		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
182			result = result composition			\
183			    mpc->mpc_ops->mpo_ ## operation (args);	\
184	}								\
185	if ((entrycount = mac_policy_list_conditional_busy()) != 0) {	\
186		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
187			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
188				result = result composition		\
189				    mpc->mpc_ops->mpo_ ## operation	\
190				    (args);				\
191		}							\
192		mac_policy_list_unbusy();				\
193	}								\
194} while (0)
195
196#define	MAC_EXTERNALIZE(type, label, elementlist, outbuf, 		\
197    outbuflen) do {							\
198	int claimed, first, ignorenotfound, savedlen;			\
199	char *element_name, *element_temp;				\
200	struct sbuf sb;							\
201									\
202	error = 0;							\
203	first = 1;							\
204	sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN);		\
205	element_temp = elementlist;					\
206	while ((element_name = strsep(&element_temp, ",")) != NULL) {	\
207		if (element_name[0] == '?') {				\
208			element_name++;					\
209			ignorenotfound = 1;				\
210		 } else							\
211			ignorenotfound = 0;				\
212		savedlen = sbuf_len(&sb);				\
213		if (first)						\
214			error = sbuf_printf(&sb, "%s/", element_name);	\
215		else							\
216			error = sbuf_printf(&sb, ",%s/", element_name);	\
217		if (error == -1) {					\
218			error = EINVAL;	/* XXX: E2BIG? */		\
219			break;						\
220		}							\
221		claimed = 0;						\
222		MAC_CHECK(externalize_ ## type ## _label, label,	\
223		    element_name, &sb, &claimed);			\
224		if (error)						\
225			break;						\
226		if (claimed == 0 && ignorenotfound) {			\
227			/* Revert last label name. */			\
228			sbuf_setpos(&sb, savedlen);			\
229		} else if (claimed != 1) {				\
230			error = EINVAL;	/* XXX: ENOLABEL? */		\
231			break;						\
232		} else {						\
233			first = 0;					\
234		}							\
235	}								\
236	sbuf_finish(&sb);						\
237} while (0)
238
239#define	MAC_INTERNALIZE(type, label, instring) do {			\
240	char *element, *element_name, *element_data;			\
241	int claimed;							\
242									\
243	error = 0;							\
244	element = instring;						\
245	while ((element_name = strsep(&element, ",")) != NULL) {	\
246		element_data = element_name;				\
247		element_name = strsep(&element_data, "/");		\
248		if (element_data == NULL) {				\
249			error = EINVAL;					\
250			break;						\
251		}							\
252		claimed = 0;						\
253		MAC_CHECK(internalize_ ## type ## _label, label,	\
254		    element_name, element_data, &claimed);		\
255		if (error)						\
256			break;						\
257		if (claimed != 1) {					\
258			/* XXXMAC: Another error here? */		\
259			error = EINVAL;					\
260			break;						\
261		}							\
262	}								\
263} while (0)
264
265/*
266 * MAC_PERFORM performs the designated operation by walking the policy
267 * module list and invoking that operation for each policy.
268 */
269#define	MAC_PERFORM(operation, args...) do {				\
270	struct mac_policy_conf *mpc;					\
271	int entrycount;							\
272									\
273	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
274		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
275			mpc->mpc_ops->mpo_ ## operation (args);		\
276	}								\
277	if ((entrycount = mac_policy_list_conditional_busy()) != 0) {	\
278		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
279			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
280				mpc->mpc_ops->mpo_ ## operation (args);	\
281		}							\
282		mac_policy_list_unbusy();				\
283	}								\
284} while (0)
285