mac_internal.h revision 192881
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 192881 2009-05-27 09:41:58Z 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#include <sys/lock.h>
59#include <sys/rmlock.h>
60
61/*
62 * MAC Framework sysctl namespace.
63 */
64#ifdef SYSCTL_DECL
65SYSCTL_DECL(_security_mac);
66#endif /* SYSCTL_DECL */
67
68/*
69 * MAC Framework SDT DTrace probe namespace, macros for declaring entry
70 * point probes, macros for invoking them.
71 */
72#ifdef SDT_PROVIDER_DECLARE
73SDT_PROVIDER_DECLARE(mac);		/* MAC Framework-level events. */
74SDT_PROVIDER_DECLARE(mac_framework);	/* Entry points to MAC. */
75
76#define	MAC_CHECK_PROBE_DEFINE4(name, arg0, arg1, arg2, arg3)		\
77	SDT_PROBE_DEFINE5(mac_framework, kernel, name, mac_check_err,	\
78	    "int", arg0, arg1, arg2, arg3);				\
79	SDT_PROBE_DEFINE5(mac_framework, kernel, name, mac_check_ok,	\
80	    "int", arg0, arg1, arg2, arg3);
81
82#define	MAC_CHECK_PROBE_DEFINE3(name, arg0, arg1, arg2)			\
83	SDT_PROBE_DEFINE4(mac_framework, kernel, name, mac_check_err,	\
84	    "int", arg0, arg1, arg2);					\
85	SDT_PROBE_DEFINE4(mac_framework, kernel, name, mac_check_ok,	\
86	    "int", arg0, arg1, arg2);
87
88#define	MAC_CHECK_PROBE_DEFINE2(name, arg0, arg1)			\
89	SDT_PROBE_DEFINE3(mac_framework, kernel, name, mac_check_err,	\
90	    "int", arg0, arg1);						\
91	SDT_PROBE_DEFINE3(mac_framework, kernel, name, mac_check_ok,	\
92	    "int", arg0, arg1);
93
94#define	MAC_CHECK_PROBE_DEFINE1(name, arg0)				\
95	SDT_PROBE_DEFINE2(mac_framework, kernel, name, mac_check_err,	\
96	    "int", arg0);						\
97	SDT_PROBE_DEFINE2(mac_framework, kernel, name, mac_check_ok,	\
98	    "int", arg0);
99
100#define	MAC_CHECK_PROBE4(name, error, arg0, arg1, arg2, arg3)	do {	\
101	if (error) {							\
102		SDT_PROBE(mac_framework, kernel, name, mac_check_err,	\
103		    error, arg0, arg1, arg2, arg3);			\
104	} else {							\
105		SDT_PROBE(mac_framework, kernel, name, mac_check_ok,	\
106		    0, arg0, arg1, arg2, arg3);				\
107	}								\
108} while (0)
109
110#define	MAC_CHECK_PROBE3(name, error, arg0, arg1, arg2)			\
111	MAC_CHECK_PROBE4(name, error, arg0, arg1, arg2, 0)
112#define	MAC_CHECK_PROBE2(name, error, arg0, arg1)			\
113	MAC_CHECK_PROBE3(name, error, arg0, arg1, 0)
114#define	MAC_CHECK_PROBE1(name, error, arg0)				\
115	MAC_CHECK_PROBE2(name, error, arg0, 0)
116#endif
117
118#define	MAC_GRANT_PROBE_DEFINE2(name, arg0, arg1)			\
119	SDT_PROBE_DEFINE3(mac_framework, kernel, name, mac_grant_err,	\
120	    "int", arg0, arg1);						\
121	SDT_PROBE_DEFINE3(mac_framework, kernel, name, mac_grant_ok,	\
122	    "INT", arg0, arg1);
123
124#define	MAC_GRANT_PROBE2(name, error, arg0, arg1)	do {		\
125	if (error) {							\
126		SDT_PROBE(mac_framework, kernel, name, mac_grant_err,	\
127		    error, arg0, arg1, 0, 0);				\
128	} else {							\
129		SDT_PROBE(mac_framework, kernel, name, mac_grant_ok,	\
130		    error, arg0, arg1, 0, 0);				\
131	}								\
132} while (0)
133
134/*
135 * MAC Framework global types and typedefs.
136 */
137LIST_HEAD(mac_policy_list_head, mac_policy_conf);
138#ifdef MALLOC_DECLARE
139MALLOC_DECLARE(M_MACTEMP);
140#endif
141
142/*
143 * MAC labels -- in-kernel storage format.
144 *
145 * In general, struct label pointers are embedded in kernel data structures
146 * representing objects that may be labeled (and protected).  Struct label is
147 * opaque to both kernel services that invoke the MAC Framework and MAC
148 * policy modules.  In particular, we do not wish to encode the layout of the
149 * label structure into any ABIs.  Historically, the slot array contained
150 * unions of {long, void} but now contains uintptr_t.
151 */
152#define	MAC_MAX_SLOTS	4
153#define	MAC_FLAG_INITIALIZED	0x0000001	/* Is initialized for use. */
154struct label {
155	int		l_flags;
156	intptr_t	l_perpolicy[MAC_MAX_SLOTS];
157};
158
159
160/*
161 * Flags for mac_labeled, a bitmask of object types need across the union of
162 * all policies currently registered with the MAC Framework, used to key
163 * whether or not labels are allocated and constructors for the type are
164 * invoked.
165 */
166#define	MPC_OBJECT_CRED			0x0000000000000001
167#define	MPC_OBJECT_PROC			0x0000000000000002
168#define	MPC_OBJECT_VNODE		0x0000000000000004
169#define	MPC_OBJECT_INPCB		0x0000000000000008
170#define	MPC_OBJECT_SOCKET		0x0000000000000010
171#define	MPC_OBJECT_DEVFS		0x0000000000000020
172#define	MPC_OBJECT_MBUF			0x0000000000000040
173#define	MPC_OBJECT_IPQ			0x0000000000000080
174#define	MPC_OBJECT_IFNET		0x0000000000000100
175#define	MPC_OBJECT_BPFDESC		0x0000000000000200
176#define	MPC_OBJECT_PIPE			0x0000000000000400
177#define	MPC_OBJECT_MOUNT		0x0000000000000800
178#define	MPC_OBJECT_POSIXSEM		0x0000000000001000
179#define	MPC_OBJECT_POSIXSHM		0x0000000000002000
180#define	MPC_OBJECT_SYSVMSG		0x0000000000004000
181#define	MPC_OBJECT_SYSVMSQ		0x0000000000008000
182#define	MPC_OBJECT_SYSVSEM		0x0000000000010000
183#define	MPC_OBJECT_SYSVSHM		0x0000000000020000
184#define	MPC_OBJECT_SYNCACHE		0x0000000000040000
185#define	MPC_OBJECT_IP6Q			0x0000000000080000
186
187/*
188 * MAC Framework global variables.
189 */
190extern struct mac_policy_list_head	mac_policy_list;
191extern struct mac_policy_list_head	mac_static_policy_list;
192extern uint64_t				mac_labeled;
193extern struct mtx			mac_ifnet_mtx;
194
195/*
196 * MAC Framework infrastructure functions.
197 */
198int	mac_error_select(int error1, int error2);
199
200void	mac_policy_slock_nosleep(struct rm_priotracker *tracker);
201void	mac_policy_slock_sleep(void);
202void	mac_policy_sunlock_nosleep(struct rm_priotracker *tracker);
203void	mac_policy_sunlock_sleep(void);
204
205struct label	*mac_labelzone_alloc(int flags);
206void		 mac_labelzone_free(struct label *label);
207void		 mac_labelzone_init(void);
208
209void	mac_init_label(struct label *label);
210void	mac_destroy_label(struct label *label);
211int	mac_check_structmac_consistent(struct mac *mac);
212int	mac_allocate_slot(void);
213
214#define MAC_IFNET_LOCK(ifp)	mtx_lock(&mac_ifnet_mtx)
215#define MAC_IFNET_UNLOCK(ifp)	mtx_unlock(&mac_ifnet_mtx)
216
217/*
218 * MAC Framework per-object type functions.  It's not yet clear how the
219 * namespaces, etc, should work for these, so for now, sort by object type.
220 */
221struct label	*mac_cred_label_alloc(void);
222void		 mac_cred_label_free(struct label *label);
223struct label	*mac_pipe_label_alloc(void);
224void		 mac_pipe_label_free(struct label *label);
225struct label	*mac_socket_label_alloc(int flag);
226void		 mac_socket_label_free(struct label *label);
227struct label	*mac_vnode_label_alloc(void);
228void		 mac_vnode_label_free(struct label *label);
229
230int	mac_cred_check_relabel(struct ucred *cred, struct label *newlabel);
231int	mac_cred_externalize_label(struct label *label, char *elements,
232	    char *outbuf, size_t outbuflen);
233int	mac_cred_internalize_label(struct label *label, char *string);
234void	mac_cred_relabel(struct ucred *cred, struct label *newlabel);
235
236struct label	*mac_mbuf_to_label(struct mbuf *m);
237
238void	mac_pipe_copy_label(struct label *src, struct label *dest);
239int	mac_pipe_externalize_label(struct label *label, char *elements,
240	    char *outbuf, size_t outbuflen);
241int	mac_pipe_internalize_label(struct label *label, char *string);
242
243int	mac_socket_label_set(struct ucred *cred, struct socket *so,
244	    struct label *label);
245void	mac_socket_copy_label(struct label *src, struct label *dest);
246int	mac_socket_externalize_label(struct label *label, char *elements,
247	    char *outbuf, size_t outbuflen);
248int	mac_socket_internalize_label(struct label *label, char *string);
249
250int	mac_vnode_externalize_label(struct label *label, char *elements,
251	    char *outbuf, size_t outbuflen);
252int	mac_vnode_internalize_label(struct label *label, char *string);
253void	mac_vnode_check_mmap_downgrade(struct ucred *cred, struct vnode *vp,
254	    int *prot);
255int	vn_setlabel(struct vnode *vp, struct label *intlabel,
256	    struct ucred *cred);
257
258/*
259 * MAC Framework composition macros invoke all registered MAC policies for a
260 * specific entry point.  They come in two forms: one which permits policies
261 * to sleep/block, and another that does not.
262 *
263 * MAC_POLICY_CHECK performs the designated check by walking the policy
264 * module list and checking with each as to how it feels about the request.
265 * Note that it returns its value via 'error' in the scope of the caller.
266 */
267#define	MAC_POLICY_CHECK(check, args...) do {				\
268	struct mac_policy_conf *mpc;					\
269									\
270	error = 0;							\
271	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
272		if (mpc->mpc_ops->mpo_ ## check != NULL)		\
273			error = mac_error_select(			\
274			    mpc->mpc_ops->mpo_ ## check (args),		\
275			    error);					\
276	}								\
277	if (!LIST_EMPTY(&mac_policy_list)) {				\
278		mac_policy_slock_sleep();				\
279		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
280			if (mpc->mpc_ops->mpo_ ## check != NULL)	\
281				error = mac_error_select(		\
282				    mpc->mpc_ops->mpo_ ## check (args),	\
283				    error);				\
284		}							\
285		mac_policy_sunlock_sleep();				\
286	}								\
287} while (0)
288
289#define	MAC_POLICY_CHECK_NOSLEEP(check, args...) do {			\
290	struct mac_policy_conf *mpc;					\
291									\
292	error = 0;							\
293	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
294		if (mpc->mpc_ops->mpo_ ## check != NULL)		\
295			error = mac_error_select(			\
296			    mpc->mpc_ops->mpo_ ## check (args),		\
297			    error);					\
298	}								\
299	if (!LIST_EMPTY(&mac_policy_list)) {				\
300		struct rm_priotracker tracker;				\
301									\
302		mac_policy_slock_nosleep(&tracker);			\
303		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
304			if (mpc->mpc_ops->mpo_ ## check != NULL)	\
305				error = mac_error_select(		\
306				    mpc->mpc_ops->mpo_ ## check (args),	\
307				    error);				\
308		}							\
309		mac_policy_sunlock_nosleep(&tracker);			\
310	}								\
311} while (0)
312
313/*
314 * MAC_POLICY_GRANT performs the designated check by walking the policy
315 * module list and checking with each as to how it feels about the request.
316 * Unlike MAC_POLICY_CHECK, it grants if any policies return '0', and
317 * otherwise returns EPERM.  Note that it returns its value via 'error' in
318 * the scope of the caller.
319 */
320#define	MAC_POLICY_GRANT_NOSLEEP(check, args...) do {			\
321	struct mac_policy_conf *mpc;					\
322									\
323	error = EPERM;							\
324	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
325		if (mpc->mpc_ops->mpo_ ## check != NULL) {		\
326			if (mpc->mpc_ops->mpo_ ## check(args) == 0)	\
327				error = 0;				\
328		}							\
329	}								\
330	if (!LIST_EMPTY(&mac_policy_list)) {				\
331		struct rm_priotracker tracker;				\
332									\
333		mac_policy_slock_nosleep(&tracker);			\
334		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
335			if (mpc->mpc_ops->mpo_ ## check != NULL) {	\
336				if (mpc->mpc_ops->mpo_ ## check (args)	\
337				    == 0)				\
338					error = 0;			\
339			}						\
340		}							\
341		mac_policy_sunlock_nosleep(&tracker);			\
342	}								\
343} while (0)
344
345/*
346 * MAC_POLICY_BOOLEAN performs the designated boolean composition by walking
347 * the module list, invoking each instance of the operation, and combining
348 * the results using the passed C operator.  Note that it returns its value
349 * via 'result' in the scope of the caller, which should be initialized by
350 * the caller in a meaningful way to get a meaningful result.
351 */
352#define	MAC_POLICY_BOOLEAN(operation, composition, args...) do {	\
353	struct mac_policy_conf *mpc;					\
354									\
355	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
356		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
357			result = result composition			\
358			    mpc->mpc_ops->mpo_ ## operation (args);	\
359	}								\
360	if (!LIST_EMPTY(&mac_policy_list)) {				\
361		mac_policy_slock_sleep();				\
362		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
363			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
364				result = result composition		\
365				    mpc->mpc_ops->mpo_ ## operation	\
366				    (args);				\
367		}							\
368		mac_policy_sunlock_sleep();				\
369	}								\
370} while (0)
371
372#define	MAC_POLICY_BOOLEAN_NOSLEEP(operation, composition, args...) do {\
373	struct mac_policy_conf *mpc;					\
374									\
375	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
376		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
377			result = result composition			\
378			    mpc->mpc_ops->mpo_ ## operation (args);	\
379	}								\
380	if (!LIST_EMPTY(&mac_policy_list)) {				\
381		struct rm_priotracker tracker;				\
382									\
383		mac_policy_slock_nosleep(&tracker);			\
384		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
385			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
386				result = result composition		\
387				    mpc->mpc_ops->mpo_ ## operation	\
388				    (args);				\
389		}							\
390		mac_policy_sunlock_nosleep(&tracker);			\
391	}								\
392} while (0)
393
394/*
395 * MAC_POLICY_EXTERNALIZE queries each policy to see if it can generate an
396 * externalized version of a label element by name.  Policies declare whether
397 * they have matched a particular element name, parsed from the string by
398 * MAC_POLICY_EXTERNALIZE, and an error is returned if any element is matched
399 * by no policy.
400 */
401#define	MAC_POLICY_EXTERNALIZE(type, label, elementlist, outbuf, 	\
402    outbuflen) do {							\
403	int claimed, first, ignorenotfound, savedlen;			\
404	char *element_name, *element_temp;				\
405	struct sbuf sb;							\
406									\
407	error = 0;							\
408	first = 1;							\
409	sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN);		\
410	element_temp = elementlist;					\
411	while ((element_name = strsep(&element_temp, ",")) != NULL) {	\
412		if (element_name[0] == '?') {				\
413			element_name++;					\
414			ignorenotfound = 1;				\
415		 } else							\
416			ignorenotfound = 0;				\
417		savedlen = sbuf_len(&sb);				\
418		if (first)						\
419			error = sbuf_printf(&sb, "%s/", element_name);	\
420		else							\
421			error = sbuf_printf(&sb, ",%s/", element_name);	\
422		if (error == -1) {					\
423			error = EINVAL;	/* XXX: E2BIG? */		\
424			break;						\
425		}							\
426		claimed = 0;						\
427		MAC_POLICY_CHECK(type ## _externalize_label, label,	\
428		    element_name, &sb, &claimed);			\
429		if (error)						\
430			break;						\
431		if (claimed == 0 && ignorenotfound) {			\
432			/* Revert last label name. */			\
433			sbuf_setpos(&sb, savedlen);			\
434		} else if (claimed != 1) {				\
435			error = EINVAL;	/* XXX: ENOLABEL? */		\
436			break;						\
437		} else {						\
438			first = 0;					\
439		}							\
440	}								\
441	sbuf_finish(&sb);						\
442} while (0)
443
444/*
445 * MAC_POLICY_INTERNALIZE presents parsed element names and data to each
446 * policy to see if any is willing to claim it and internalize the label
447 * data.  If no policies match, an error is returned.
448 */
449#define	MAC_POLICY_INTERNALIZE(type, label, instring) do {		\
450	char *element, *element_name, *element_data;			\
451	int claimed;							\
452									\
453	error = 0;							\
454	element = instring;						\
455	while ((element_name = strsep(&element, ",")) != NULL) {	\
456		element_data = element_name;				\
457		element_name = strsep(&element_data, "/");		\
458		if (element_data == NULL) {				\
459			error = EINVAL;					\
460			break;						\
461		}							\
462		claimed = 0;						\
463		MAC_POLICY_CHECK(type ## _internalize_label, label,	\
464		    element_name, element_data, &claimed);		\
465		if (error)						\
466			break;						\
467		if (claimed != 1) {					\
468			/* XXXMAC: Another error here? */		\
469			error = EINVAL;					\
470			break;						\
471		}							\
472	}								\
473} while (0)
474
475/*
476 * MAC_POLICY_PERFORM performs the designated operation by walking the policy
477 * module list and invoking that operation for each policy.
478 */
479#define	MAC_POLICY_PERFORM(operation, args...) do {			\
480	struct mac_policy_conf *mpc;					\
481									\
482	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
483		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
484			mpc->mpc_ops->mpo_ ## operation (args);		\
485	}								\
486	if (!LIST_EMPTY(&mac_policy_list)) {				\
487		mac_policy_slock_sleep();				\
488		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
489			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
490				mpc->mpc_ops->mpo_ ## operation (args);	\
491		}							\
492		mac_policy_sunlock_sleep();				\
493	}								\
494} while (0)
495
496#define	MAC_POLICY_PERFORM_NOSLEEP(operation, args...) do {		\
497	struct mac_policy_conf *mpc;					\
498									\
499	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {		\
500		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
501			mpc->mpc_ops->mpo_ ## operation (args);		\
502	}								\
503	if (!LIST_EMPTY(&mac_policy_list)) {				\
504		struct rm_priotracker tracker;				\
505									\
506		mac_policy_slock_nosleep(&tracker);			\
507		LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {		\
508			if (mpc->mpc_ops->mpo_ ## operation != NULL)	\
509				mpc->mpc_ops->mpo_ ## operation (args);	\
510		}							\
511		mac_policy_sunlock_nosleep(&tracker);			\
512	}								\
513} while (0)
514
515#endif /* !_SECURITY_MAC_MAC_INTERNAL_H_ */
516