mac_partition.c revision 164033
1/*-
2 * Copyright (c) 1999-2002 Robert N. M. Watson
3 * Copyright (c) 2001-2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed by Robert Watson for the TrustedBSD Project.
7 *
8 * This software was developed for the FreeBSD Project in part by Network
9 * Associates Laboratories, the Security Research Division of Network
10 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
11 * as part of the DARPA CHATS research program.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $FreeBSD: head/sys/security/mac_partition/mac_partition.c 164033 2006-11-06 13:42:10Z rwatson $
35 */
36
37/*
38 * Developed by the TrustedBSD Project.
39 * Experiment with a partition-like model.
40 */
41
42#include <sys/types.h>
43#include <sys/param.h>
44#include <sys/conf.h>
45#include <sys/kernel.h>
46#include <sys/mac.h>
47#include <sys/mount.h>
48#include <sys/priv.h>
49#include <sys/proc.h>
50#include <sys/sbuf.h>
51#include <sys/systm.h>
52#include <sys/sysproto.h>
53#include <sys/sysent.h>
54#include <sys/vnode.h>
55#include <sys/file.h>
56#include <sys/socket.h>
57#include <sys/socketvar.h>
58#include <sys/sx.h>
59#include <sys/sysctl.h>
60
61#include <fs/devfs/devfs.h>
62
63#include <net/bpfdesc.h>
64#include <net/if.h>
65#include <net/if_types.h>
66#include <net/if_var.h>
67
68#include <vm/vm.h>
69
70#include <sys/mac_policy.h>
71
72#include <security/mac_partition/mac_partition.h>
73
74SYSCTL_DECL(_security_mac);
75
76SYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0,
77    "TrustedBSD mac_partition policy controls");
78
79static int	mac_partition_enabled = 1;
80SYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW,
81    &mac_partition_enabled, 0, "Enforce partition policy");
82
83static int	partition_slot;
84#define	SLOT(l)	(LABEL_TO_SLOT((l), partition_slot).l_long)
85
86static void
87mac_partition_init(struct mac_policy_conf *conf)
88{
89
90}
91
92static void
93mac_partition_init_label(struct label *label)
94{
95
96	SLOT(label) = 0;
97}
98
99static void
100mac_partition_destroy_label(struct label *label)
101{
102
103	SLOT(label) = 0;
104}
105
106static void
107mac_partition_copy_label(struct label *src, struct label *dest)
108{
109
110	SLOT(dest) = SLOT(src);
111}
112
113static int
114mac_partition_externalize_label(struct label *label, char *element_name,
115    struct sbuf *sb, int *claimed)
116{
117
118	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
119		return (0);
120
121	(*claimed)++;
122
123	if (sbuf_printf(sb, "%ld", SLOT(label)) == -1)
124		return (EINVAL);
125	else
126		return (0);
127}
128
129static int
130mac_partition_internalize_label(struct label *label, char *element_name,
131    char *element_data, int *claimed)
132{
133
134	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
135		return (0);
136
137	(*claimed)++;
138	SLOT(label) = strtol(element_data, NULL, 10);
139	return (0);
140}
141
142static void
143mac_partition_create_proc0(struct ucred *cred)
144{
145
146	SLOT(cred->cr_label) = 0;
147}
148
149static void
150mac_partition_create_proc1(struct ucred *cred)
151{
152
153	SLOT(cred->cr_label) = 0;
154}
155
156static void
157mac_partition_relabel_cred(struct ucred *cred, struct label *newlabel)
158{
159
160	if (SLOT(newlabel) != 0)
161		SLOT(cred->cr_label) = SLOT(newlabel);
162}
163
164static int
165label_on_label(struct label *subject, struct label *object)
166{
167
168	if (mac_partition_enabled == 0)
169		return (0);
170
171	if (SLOT(subject) == 0)
172		return (0);
173
174	if (SLOT(subject) == SLOT(object))
175		return (0);
176
177	return (EPERM);
178}
179
180static int
181mac_partition_check_cred_relabel(struct ucred *cred, struct label *newlabel)
182{
183	int error;
184
185	error = 0;
186
187	/* Treat "0" as a no-op request. */
188	if (SLOT(newlabel) != 0) {
189		/*
190		 * Require BSD privilege in order to change the partition.
191		 * Originally we also required that the process not be
192		 * in a partition in the first place, but this didn't
193		 * interact well with sendmail.
194		 */
195		error = priv_check_cred(cred, PRIV_MAC_PARTITION, 0);
196	}
197
198	return (error);
199}
200
201static int
202mac_partition_check_cred_visible(struct ucred *u1, struct ucred *u2)
203{
204	int error;
205
206	error = label_on_label(u1->cr_label, u2->cr_label);
207
208	return (error == 0 ? 0 : ESRCH);
209}
210
211static int
212mac_partition_check_proc_debug(struct ucred *cred, struct proc *proc)
213{
214	int error;
215
216	error = label_on_label(cred->cr_label, proc->p_ucred->cr_label);
217
218	return (error ? ESRCH : 0);
219}
220
221static int
222mac_partition_check_proc_sched(struct ucred *cred, struct proc *proc)
223{
224	int error;
225
226	error = label_on_label(cred->cr_label, proc->p_ucred->cr_label);
227
228	return (error ? ESRCH : 0);
229}
230
231static int
232mac_partition_check_proc_signal(struct ucred *cred, struct proc *proc,
233    int signum)
234{
235	int error;
236
237	error = label_on_label(cred->cr_label, proc->p_ucred->cr_label);
238
239	return (error ? ESRCH : 0);
240}
241
242static int
243mac_partition_check_socket_visible(struct ucred *cred, struct socket *socket,
244    struct label *socketlabel)
245{
246	int error;
247
248	error = label_on_label(cred->cr_label, socketlabel);
249
250	return (error ? ENOENT : 0);
251}
252
253static int
254mac_partition_check_vnode_exec(struct ucred *cred, struct vnode *vp,
255    struct label *label, struct image_params *imgp, struct label *execlabel)
256{
257
258	if (execlabel != NULL) {
259		/*
260		 * We currently don't permit labels to be changed at
261		 * exec-time as part of the partition model, so disallow
262		 * non-NULL partition label changes in execlabel.
263		 */
264		if (SLOT(execlabel) != 0)
265			return (EINVAL);
266	}
267
268	return (0);
269}
270
271static struct mac_policy_ops mac_partition_ops =
272{
273	.mpo_init = mac_partition_init,
274	.mpo_init_cred_label = mac_partition_init_label,
275	.mpo_destroy_cred_label = mac_partition_destroy_label,
276	.mpo_copy_cred_label = mac_partition_copy_label,
277	.mpo_externalize_cred_label = mac_partition_externalize_label,
278	.mpo_internalize_cred_label = mac_partition_internalize_label,
279	.mpo_create_proc0 = mac_partition_create_proc0,
280	.mpo_create_proc1 = mac_partition_create_proc1,
281	.mpo_relabel_cred = mac_partition_relabel_cred,
282	.mpo_check_cred_relabel = mac_partition_check_cred_relabel,
283	.mpo_check_cred_visible = mac_partition_check_cred_visible,
284	.mpo_check_proc_debug = mac_partition_check_proc_debug,
285	.mpo_check_proc_sched = mac_partition_check_proc_sched,
286	.mpo_check_proc_signal = mac_partition_check_proc_signal,
287	.mpo_check_socket_visible = mac_partition_check_socket_visible,
288	.mpo_check_vnode_exec = mac_partition_check_vnode_exec,
289};
290
291MAC_POLICY_SET(&mac_partition_ops, mac_partition, "TrustedBSD MAC/Partition",
292    MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot);
293