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