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