mac_partition.c revision 182063
1/*-
2 * Copyright (c) 1999-2002, 2007 Robert N. M. Watson
3 * Copyright (c) 2001-2002 Networks Associates Technology, Inc.
4 * Copyright (c) 2006 SPARTA, Inc.
5 * Copyright (c) 2008 Apple Inc.
6 * All rights reserved.
7 *
8 * This software was developed by Robert Watson for the 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 * This software was enhanced by SPARTA ISSO under SPAWAR contract
16 * N66001-04-C-6019 ("SEFOS").
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * $FreeBSD: head/sys/security/mac_partition/mac_partition.c 182063 2008-08-23 15:26:36Z rwatson $
40 */
41
42/*
43 * Developed by the TrustedBSD Project.
44 *
45 * Experiment with a partition-like model.
46 */
47
48#include <sys/param.h>
49#include <sys/kernel.h>
50#include <sys/module.h>
51#include <sys/priv.h>
52#include <sys/proc.h>
53#include <sys/sbuf.h>
54#include <sys/systm.h>
55#include <sys/sysctl.h>
56
57#include <security/mac/mac_policy.h>
58#include <security/mac_partition/mac_partition.h>
59
60SYSCTL_DECL(_security_mac);
61
62SYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0,
63    "TrustedBSD mac_partition policy controls");
64
65static int	partition_enabled = 1;
66SYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW,
67    &partition_enabled, 0, "Enforce partition policy");
68
69static int	partition_slot;
70#define	SLOT(l)	mac_label_get((l), partition_slot)
71#define	SLOT_SET(l, v)	mac_label_set((l), partition_slot, (v))
72
73static int
74label_on_label(struct label *subject, struct label *object)
75{
76
77	if (partition_enabled == 0)
78		return (0);
79
80	if (subject == NULL)
81		return (0);
82
83	if (SLOT(subject) == 0)
84		return (0);
85
86	/*
87	 * If the object label hasn't been allocated, then it's effectively
88	 * not in a partition, and we know the subject is as it has a label
89	 * and it's not 0, so reject.
90	 */
91	if (object == NULL)
92		return (EPERM);
93
94	if (SLOT(subject) == SLOT(object))
95		return (0);
96
97	return (EPERM);
98}
99
100/*
101 * Object-specific entry points are sorted alphabetically by object type name
102 * and then by operation.
103 */
104static int
105partition_cred_check_relabel(struct ucred *cred, struct label *newlabel)
106{
107	int error;
108
109	error = 0;
110
111	/* Treat "0" as a no-op request. */
112	if (SLOT(newlabel) != 0) {
113		/*
114		 * Require BSD privilege in order to change the partition.
115		 * Originally we also required that the process not be in a
116		 * partition in the first place, but this didn't interact
117		 * well with sendmail.
118		 */
119		error = priv_check_cred(cred, PRIV_MAC_PARTITION, 0);
120	}
121
122	return (error);
123}
124
125static int
126partition_cred_check_visible(struct ucred *cr1, struct ucred *cr2)
127{
128	int error;
129
130	error = label_on_label(cr1->cr_label, cr2->cr_label);
131
132	return (error == 0 ? 0 : ESRCH);
133}
134
135static void
136partition_cred_copy_label(struct label *src, struct label *dest)
137{
138
139	if (src != NULL && dest != NULL)
140		SLOT_SET(dest, SLOT(src));
141	else if (dest != NULL)
142		SLOT_SET(dest, 0);
143}
144
145static void
146partition_cred_destroy_label(struct label *label)
147{
148
149	SLOT_SET(label, 0);
150}
151
152static int
153partition_cred_externalize_label(struct label *label, char *element_name,
154    struct sbuf *sb, int *claimed)
155{
156
157	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
158		return (0);
159
160	(*claimed)++;
161
162	if (label != NULL) {
163		if (sbuf_printf(sb, "%jd", (intmax_t)SLOT(label)) == -1)
164			return (EINVAL);
165	} else {
166		if (sbuf_printf(sb, "0") == -1)
167			return (EINVAL);
168	}
169	return (0);
170}
171
172static void
173partition_cred_init_label(struct label *label)
174{
175
176	SLOT_SET(label, 0);
177}
178
179static int
180partition_cred_internalize_label(struct label *label, char *element_name,
181    char *element_data, int *claimed)
182{
183
184	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
185		return (0);
186
187	(*claimed)++;
188	SLOT_SET(label, strtol(element_data, NULL, 10));
189	return (0);
190}
191
192static void
193partition_cred_relabel(struct ucred *cred, struct label *newlabel)
194{
195
196	if (newlabel != NULL && SLOT(newlabel) != 0)
197		SLOT_SET(cred->cr_label, SLOT(newlabel));
198}
199
200static int
201partition_proc_check_debug(struct ucred *cred, struct proc *p)
202{
203	int error;
204
205	error = label_on_label(cred->cr_label, p->p_ucred->cr_label);
206
207	return (error ? ESRCH : 0);
208}
209
210static int
211partition_proc_check_sched(struct ucred *cred, struct proc *p)
212{
213	int error;
214
215	error = label_on_label(cred->cr_label, p->p_ucred->cr_label);
216
217	return (error ? ESRCH : 0);
218}
219
220static int
221partition_proc_check_signal(struct ucred *cred, struct proc *p,
222    int signum)
223{
224	int error;
225
226	error = label_on_label(cred->cr_label, p->p_ucred->cr_label);
227
228	return (error ? ESRCH : 0);
229}
230
231static void
232partition_proc_create_init(struct ucred *cred)
233{
234
235	SLOT_SET(cred->cr_label, 0);
236}
237
238static void
239partition_proc_create_swapper(struct ucred *cred)
240{
241
242	SLOT_SET(cred->cr_label, 0);
243}
244
245static int
246partition_socket_check_visible(struct ucred *cred, struct socket *so,
247    struct label *solabel)
248{
249	int error;
250
251	error = label_on_label(cred->cr_label, solabel);
252
253	return (error ? ENOENT : 0);
254}
255
256static int
257partition_vnode_check_exec(struct ucred *cred, struct vnode *vp,
258    struct label *vplabel, struct image_params *imgp,
259    struct label *execlabel)
260{
261
262	if (execlabel != NULL) {
263		/*
264		 * We currently don't permit labels to be changed at
265		 * exec-time as part of the partition model, so disallow
266		 * non-NULL partition label changes in execlabel.
267		 */
268		if (SLOT(execlabel) != 0)
269			return (EINVAL);
270	}
271
272	return (0);
273}
274
275static struct mac_policy_ops partition_ops =
276{
277	.mpo_cred_check_relabel = partition_cred_check_relabel,
278	.mpo_cred_check_visible = partition_cred_check_visible,
279	.mpo_cred_copy_label = partition_cred_copy_label,
280	.mpo_cred_destroy_label = partition_cred_destroy_label,
281	.mpo_cred_externalize_label = partition_cred_externalize_label,
282	.mpo_cred_init_label = partition_cred_init_label,
283	.mpo_cred_internalize_label = partition_cred_internalize_label,
284	.mpo_cred_relabel = partition_cred_relabel,
285	.mpo_proc_check_debug = partition_proc_check_debug,
286	.mpo_proc_check_sched = partition_proc_check_sched,
287	.mpo_proc_check_signal = partition_proc_check_signal,
288	.mpo_proc_create_init = partition_proc_create_init,
289	.mpo_proc_create_swapper = partition_proc_create_swapper,
290	.mpo_socket_check_visible = partition_socket_check_visible,
291	.mpo_vnode_check_exec = partition_vnode_check_exec,
292};
293
294MAC_POLICY_SET(&partition_ops, mac_partition, "TrustedBSD MAC/Partition",
295    MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot, MPC_OBJECT_CRED);
296