mac_framework.c revision 193355
1/*-
2 * Copyright (c) 1999-2002, 2006, 2009 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001-2005 Networks Associates Technology, Inc.
5 * Copyright (c) 2005-2006 SPARTA, Inc.
6 * Copyright (c) 2008-2009 Apple Inc.
7 * All rights reserved.
8 *
9 * This software was developed by Robert Watson and Ilmar Habibulin for the
10 * TrustedBSD Project.
11 *
12 * This software was developed for the FreeBSD Project in part by Network
13 * Associates Laboratories, the Security Research Division of Network
14 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
15 * as part of the DARPA CHATS research program.
16 *
17 * This software was enhanced by SPARTA ISSO under SPAWAR contract
18 * N66001-04-C-6019 ("SEFOS").
19 *
20 * This software was developed at the University of Cambridge Computer
21 * Laboratory with support from a grant from Google, Inc.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 *    notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 *    notice, this list of conditions and the following disclaimer in the
30 *    documentation and/or other materials provided with the distribution.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 */
44
45/*-
46 * Framework for extensible kernel access control.  This file contains core
47 * kernel infrastructure for the TrustedBSD MAC Framework, including policy
48 * registration, versioning, locking, error composition operator, and system
49 * calls.
50 *
51 * The MAC Framework implements three programming interfaces:
52 *
53 * - The kernel MAC interface, defined in mac_framework.h, and invoked
54 *   throughout the kernel to request security decisions, notify of security
55 *   related events, etc.
56 *
57 * - The MAC policy module interface, defined in mac_policy.h, which is
58 *   implemented by MAC policy modules and invoked by the MAC Framework to
59 *   forward kernel security requests and notifications to policy modules.
60 *
61 * - The user MAC API, defined in mac.h, which allows user programs to query
62 *   and set label state on objects.
63 *
64 * The majority of the MAC Framework implementation may be found in
65 * src/sys/security/mac.  Sample policy modules may be found in
66 * src/sys/security/mac_*.
67 */
68
69#include "opt_kdtrace.h"
70#include "opt_mac.h"
71
72#include <sys/cdefs.h>
73__FBSDID("$FreeBSD: head/sys/security/mac/mac_framework.c 193355 2009-06-02 22:22:09Z rwatson $");
74
75#include <sys/param.h>
76#include <sys/condvar.h>
77#include <sys/kernel.h>
78#include <sys/lock.h>
79#include <sys/mac.h>
80#include <sys/module.h>
81#include <sys/rmlock.h>
82#include <sys/sdt.h>
83#include <sys/sx.h>
84#include <sys/systm.h>
85#include <sys/sysctl.h>
86
87#include <security/mac/mac_framework.h>
88#include <security/mac/mac_internal.h>
89#include <security/mac/mac_policy.h>
90
91/*
92 * DTrace SDT providers for MAC.
93 */
94SDT_PROVIDER_DEFINE(mac);
95SDT_PROVIDER_DEFINE(mac_framework);
96
97SDT_PROBE_DEFINE2(mac, kernel, policy, modevent, "int",
98    "struct mac_policy_conf *mpc");
99SDT_PROBE_DEFINE1(mac, kernel, policy, register, "struct mac_policy_conf *");
100SDT_PROBE_DEFINE1(mac, kernel, policy, unregister, "struct mac_policy_conf *");
101
102/*
103 * Root sysctl node for all MAC and MAC policy controls.
104 */
105SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0,
106    "TrustedBSD MAC policy controls");
107
108/*
109 * Declare that the kernel provides MAC support, version 3 (FreeBSD 7.x).
110 * This permits modules to refuse to be loaded if the necessary support isn't
111 * present, even if it's pre-boot.
112 */
113MODULE_VERSION(kernel_mac_support, MAC_VERSION);
114
115static unsigned int	mac_version = MAC_VERSION;
116SYSCTL_UINT(_security_mac, OID_AUTO, version, CTLFLAG_RD, &mac_version, 0,
117    "");
118
119/*
120 * Labels consist of a indexed set of "slots", which are allocated policies
121 * as required.  The MAC Framework maintains a bitmask of slots allocated so
122 * far to prevent reuse.  Slots cannot be reused, as the MAC Framework
123 * guarantees that newly allocated slots in labels will be NULL unless
124 * otherwise initialized, and because we do not have a mechanism to garbage
125 * collect slots on policy unload.  As labeled policies tend to be statically
126 * loaded during boot, and not frequently unloaded and reloaded, this is not
127 * generally an issue.
128 */
129#if MAC_MAX_SLOTS > 32
130#error "MAC_MAX_SLOTS too large"
131#endif
132
133static unsigned int mac_max_slots = MAC_MAX_SLOTS;
134static unsigned int mac_slot_offsets_free = (1 << MAC_MAX_SLOTS) - 1;
135SYSCTL_UINT(_security_mac, OID_AUTO, max_slots, CTLFLAG_RD, &mac_max_slots,
136    0, "");
137
138/*
139 * Has the kernel started generating labeled objects yet?  All read/write
140 * access to this variable is serialized during the boot process.  Following
141 * the end of serialization, we don't update this flag; no locking.
142 */
143static int	mac_late = 0;
144
145/*
146 * Each policy declares a mask of object types requiring labels to be
147 * allocated for them.  For convenience, we combine and cache the bitwise or
148 * of the per-policy object flags to track whether we will allocate a label
149 * for an object type at run-time.
150 */
151uint64_t	mac_labeled;
152SYSCTL_QUAD(_security_mac, OID_AUTO, labeled, CTLFLAG_RD, &mac_labeled, 0,
153    "Mask of object types being labeled");
154
155MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage");
156
157/*
158 * MAC policy modules are placed in one of two lists: mac_static_policy_list,
159 * for policies that are loaded early and cannot be unloaded, and
160 * mac_policy_list, which holds policies either loaded later in the boot
161 * cycle or that may be unloaded.  The static policy list does not require
162 * locks to iterate over, but the dynamic list requires synchronization.
163 * Support for dynamic policy loading can be compiled out using the
164 * MAC_STATIC kernel option.
165 *
166 * The dynamic policy list is protected by two locks: modifying the list
167 * requires both locks to be held exclusively.  One of the locks,
168 * mac_policy_rm, is acquired over policy entry points that will never sleep;
169 * the other, mac_policy_sx, is acquire over policy entry points that may
170 * sleep.  The former category will be used when kernel locks may be held
171 * over calls to the MAC Framework, during network processing in ithreads,
172 * etc.  The latter will tend to involve potentially blocking memory
173 * allocations, extended attribute I/O, etc.
174 */
175#ifndef MAC_STATIC
176static struct rmlock mac_policy_rm;	/* Non-sleeping entry points. */
177static struct sx mac_policy_sx;		/* Sleeping entry points. */
178#endif
179
180struct mac_policy_list_head mac_policy_list;
181struct mac_policy_list_head mac_static_policy_list;
182u_int mac_policy_count;			/* Registered policy count. */
183
184static void	mac_policy_xlock(void);
185static void	mac_policy_xlock_assert(void);
186static void	mac_policy_xunlock(void);
187
188void
189mac_policy_slock_nosleep(struct rm_priotracker *tracker)
190{
191
192#ifndef MAC_STATIC
193	if (!mac_late)
194		return;
195
196	rm_rlock(&mac_policy_rm, tracker);
197#endif
198}
199
200void
201mac_policy_slock_sleep(void)
202{
203
204	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
205 	    "mac_policy_slock_sleep");
206
207#ifndef MAC_STATIC
208	if (!mac_late)
209		return;
210
211	sx_slock(&mac_policy_sx);
212#endif
213}
214
215void
216mac_policy_sunlock_nosleep(struct rm_priotracker *tracker)
217{
218
219#ifndef MAC_STATIC
220	if (!mac_late)
221		return;
222
223	rm_runlock(&mac_policy_rm, tracker);
224#endif
225}
226
227void
228mac_policy_sunlock_sleep(void)
229{
230
231#ifndef MAC_STATIC
232	if (!mac_late)
233		return;
234
235	sx_sunlock(&mac_policy_sx);
236#endif
237}
238
239static void
240mac_policy_xlock(void)
241{
242
243	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
244 	    "mac_policy_xlock()");
245
246#ifndef MAC_STATIC
247	if (!mac_late)
248		return;
249
250	sx_xlock(&mac_policy_sx);
251	rm_wlock(&mac_policy_rm);
252#endif
253}
254
255static void
256mac_policy_xunlock(void)
257{
258
259#ifndef MAC_STATIC
260	if (!mac_late)
261		return;
262
263	rm_wunlock(&mac_policy_rm);
264	sx_xunlock(&mac_policy_sx);
265#endif
266}
267
268static void
269mac_policy_xlock_assert(void)
270{
271
272#ifndef MAC_STATIC
273	if (!mac_late)
274		return;
275
276	/* XXXRW: rm_assert(&mac_policy_rm, RA_WLOCKED); */
277	sx_assert(&mac_policy_sx, SA_XLOCKED);
278#endif
279}
280
281/*
282 * Initialize the MAC subsystem, including appropriate SMP locks.
283 */
284static void
285mac_init(void)
286{
287
288	LIST_INIT(&mac_static_policy_list);
289	LIST_INIT(&mac_policy_list);
290	mac_labelzone_init();
291
292#ifndef MAC_STATIC
293	rm_init_flags(&mac_policy_rm, "mac_policy_rm", RM_NOWITNESS);
294	sx_init_flags(&mac_policy_sx, "mac_policy_sx", SX_NOWITNESS);
295#endif
296}
297
298/*
299 * For the purposes of modules that want to know if they were loaded "early",
300 * set the mac_late flag once we've processed modules either linked into the
301 * kernel, or loaded before the kernel startup.
302 */
303static void
304mac_late_init(void)
305{
306
307	mac_late = 1;
308}
309
310/*
311 * Given a policy, derive from its set of non-NULL label init methods what
312 * object types the policy is interested in.
313 */
314static uint64_t
315mac_policy_getlabeled(struct mac_policy_conf *mpc)
316{
317	uint64_t labeled;
318
319#define	MPC_FLAG(method, flag)					\
320	if (mpc->mpc_ops->mpo_ ## method != NULL)			\
321		labeled |= (flag);					\
322
323	labeled = 0;
324	MPC_FLAG(cred_init_label, MPC_OBJECT_CRED);
325	MPC_FLAG(proc_init_label, MPC_OBJECT_PROC);
326	MPC_FLAG(vnode_init_label, MPC_OBJECT_VNODE);
327	MPC_FLAG(inpcb_init_label, MPC_OBJECT_INPCB);
328	MPC_FLAG(socket_init_label, MPC_OBJECT_SOCKET);
329	MPC_FLAG(devfs_init_label, MPC_OBJECT_DEVFS);
330	MPC_FLAG(mbuf_init_label, MPC_OBJECT_MBUF);
331	MPC_FLAG(ipq_init_label, MPC_OBJECT_IPQ);
332	MPC_FLAG(ifnet_init_label, MPC_OBJECT_IFNET);
333	MPC_FLAG(bpfdesc_init_label, MPC_OBJECT_BPFDESC);
334	MPC_FLAG(pipe_init_label, MPC_OBJECT_PIPE);
335	MPC_FLAG(mount_init_label, MPC_OBJECT_MOUNT);
336	MPC_FLAG(posixsem_init_label, MPC_OBJECT_POSIXSEM);
337	MPC_FLAG(posixshm_init_label, MPC_OBJECT_POSIXSHM);
338	MPC_FLAG(sysvmsg_init_label, MPC_OBJECT_SYSVMSG);
339	MPC_FLAG(sysvmsq_init_label, MPC_OBJECT_SYSVMSQ);
340	MPC_FLAG(sysvsem_init_label, MPC_OBJECT_SYSVSEM);
341	MPC_FLAG(sysvshm_init_label, MPC_OBJECT_SYSVSHM);
342	MPC_FLAG(syncache_init_label, MPC_OBJECT_SYNCACHE);
343	MPC_FLAG(ip6q_init_label, MPC_OBJECT_IP6Q);
344
345#undef MPC_FLAG
346	return (labeled);
347}
348
349/*
350 * When policies are loaded or unloaded, walk the list of registered policies
351 * and built mac_labeled, a bitmask representing the union of all objects
352 * requiring labels across all policies.
353 */
354static void
355mac_policy_update(void)
356{
357	struct mac_policy_conf *mpc;
358
359	mac_policy_xlock_assert();
360
361	mac_labeled = 0;
362	mac_policy_count = 0;
363	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {
364		mac_labeled |= mac_policy_getlabeled(mpc);
365		mac_policy_count++;
366	}
367	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
368		mac_labeled |= mac_policy_getlabeled(mpc);
369		mac_policy_count++;
370	}
371}
372
373static int
374mac_policy_register(struct mac_policy_conf *mpc)
375{
376	struct mac_policy_conf *tmpc;
377	int error, slot, static_entry;
378
379	error = 0;
380
381	/*
382	 * We don't technically need exclusive access while !mac_late, but
383	 * hold it for assertion consistency.
384	 */
385	mac_policy_xlock();
386
387	/*
388	 * If the module can potentially be unloaded, or we're loading late,
389	 * we have to stick it in the non-static list and pay an extra
390	 * performance overhead.  Otherwise, we can pay a light locking cost
391	 * and stick it in the static list.
392	 */
393	static_entry = (!mac_late &&
394	    !(mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK));
395
396	if (static_entry) {
397		LIST_FOREACH(tmpc, &mac_static_policy_list, mpc_list) {
398			if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
399				error = EEXIST;
400				goto out;
401			}
402		}
403	} else {
404		LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
405			if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
406				error = EEXIST;
407				goto out;
408			}
409		}
410	}
411	if (mpc->mpc_field_off != NULL) {
412		slot = ffs(mac_slot_offsets_free);
413		if (slot == 0) {
414			error = ENOMEM;
415			goto out;
416		}
417		slot--;
418		mac_slot_offsets_free &= ~(1 << slot);
419		*mpc->mpc_field_off = slot;
420	}
421	mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
422
423	/*
424	 * If we're loading a MAC module after the framework has initialized,
425	 * it has to go into the dynamic list.  If we're loading it before
426	 * we've finished initializing, it can go into the static list with
427	 * weaker locker requirements.
428	 */
429	if (static_entry)
430		LIST_INSERT_HEAD(&mac_static_policy_list, mpc, mpc_list);
431	else
432		LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);
433
434	/*
435	 * Per-policy initialization.  Currently, this takes place under the
436	 * exclusive lock, so policies must not sleep in their init method.
437	 * In the future, we may want to separate "init" from "start", with
438	 * "init" occuring without the lock held.  Likewise, on tear-down,
439	 * breaking out "stop" from "destroy".
440	 */
441	if (mpc->mpc_ops->mpo_init != NULL)
442		(*(mpc->mpc_ops->mpo_init))(mpc);
443	mac_policy_update();
444
445	SDT_PROBE(mac, kernel, policy, register, mpc, 0, 0, 0, 0);
446	printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
447	    mpc->mpc_name);
448
449out:
450	mac_policy_xunlock();
451	return (error);
452}
453
454static int
455mac_policy_unregister(struct mac_policy_conf *mpc)
456{
457
458	/*
459	 * If we fail the load, we may get a request to unload.  Check to see
460	 * if we did the run-time registration, and if not, silently succeed.
461	 */
462	mac_policy_xlock();
463	if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
464		mac_policy_xunlock();
465		return (0);
466	}
467#if 0
468	/*
469	 * Don't allow unloading modules with private data.
470	 */
471	if (mpc->mpc_field_off != NULL) {
472		mac_policy_xunlock();
473		return (EBUSY);
474	}
475#endif
476	/*
477	 * Only allow the unload to proceed if the module is unloadable by
478	 * its own definition.
479	 */
480	if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
481		mac_policy_xunlock();
482		return (EBUSY);
483	}
484	if (mpc->mpc_ops->mpo_destroy != NULL)
485		(*(mpc->mpc_ops->mpo_destroy))(mpc);
486
487	LIST_REMOVE(mpc, mpc_list);
488	mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED;
489	mac_policy_update();
490	mac_policy_xunlock();
491
492	SDT_PROBE(mac, kernel, policy, unregister, mpc, 0, 0, 0, 0);
493	printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
494	    mpc->mpc_name);
495
496	return (0);
497}
498
499/*
500 * Allow MAC policy modules to register during boot, etc.
501 */
502int
503mac_policy_modevent(module_t mod, int type, void *data)
504{
505	struct mac_policy_conf *mpc;
506	int error;
507
508	error = 0;
509	mpc = (struct mac_policy_conf *) data;
510
511#ifdef MAC_STATIC
512	if (mac_late) {
513		printf("mac_policy_modevent: MAC_STATIC and late\n");
514		return (EBUSY);
515	}
516#endif
517
518	SDT_PROBE(mac, kernel, policy, modevent, type, mpc, 0, 0, 0);
519	switch (type) {
520	case MOD_LOAD:
521		if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
522		    mac_late) {
523			printf("mac_policy_modevent: can't load %s policy "
524			    "after booting\n", mpc->mpc_name);
525			error = EBUSY;
526			break;
527		}
528		error = mac_policy_register(mpc);
529		break;
530	case MOD_UNLOAD:
531		/* Don't unregister the module if it was never registered. */
532		if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
533		    != 0)
534			error = mac_policy_unregister(mpc);
535		else
536			error = 0;
537		break;
538	default:
539		error = EOPNOTSUPP;
540		break;
541	}
542
543	return (error);
544}
545
546/*
547 * Define an error value precedence, and given two arguments, selects the
548 * value with the higher precedence.
549 */
550int
551mac_error_select(int error1, int error2)
552{
553
554	/* Certain decision-making errors take top priority. */
555	if (error1 == EDEADLK || error2 == EDEADLK)
556		return (EDEADLK);
557
558	/* Invalid arguments should be reported where possible. */
559	if (error1 == EINVAL || error2 == EINVAL)
560		return (EINVAL);
561
562	/* Precedence goes to "visibility", with both process and file. */
563	if (error1 == ESRCH || error2 == ESRCH)
564		return (ESRCH);
565
566	if (error1 == ENOENT || error2 == ENOENT)
567		return (ENOENT);
568
569	/* Precedence goes to DAC/MAC protections. */
570	if (error1 == EACCES || error2 == EACCES)
571		return (EACCES);
572
573	/* Precedence goes to privilege. */
574	if (error1 == EPERM || error2 == EPERM)
575		return (EPERM);
576
577	/* Precedence goes to error over success; otherwise, arbitrary. */
578	if (error1 != 0)
579		return (error1);
580	return (error2);
581}
582
583int
584mac_check_structmac_consistent(struct mac *mac)
585{
586
587	if (mac->m_buflen < 0 ||
588	    mac->m_buflen > MAC_MAX_LABEL_BUF_LEN)
589		return (EINVAL);
590
591	return (0);
592}
593
594SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
595SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);
596