mac_partition.c revision 166905
1/*-
2 * Copyright (c) 1999-2002, 2007 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 166905 2007-02-23 14:39:04Z rwatson $
35 */
36
37/*
38 * Developed by the TrustedBSD Project.
39 * Experiment with a partition-like model.
40 */
41
42#include <sys/param.h>
43#include <sys/kernel.h>
44#include <sys/module.h>
45#include <sys/priv.h>
46#include <sys/proc.h>
47#include <sys/sbuf.h>
48#include <sys/systm.h>
49#include <sys/sysctl.h>
50
51#include <security/mac/mac_policy.h>
52#include <security/mac_partition/mac_partition.h>
53
54SYSCTL_DECL(_security_mac);
55
56SYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0,
57    "TrustedBSD mac_partition policy controls");
58
59static int	mac_partition_enabled = 1;
60SYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW,
61    &mac_partition_enabled, 0, "Enforce partition policy");
62
63static int	partition_slot;
64#define	SLOT(l)	mac_label_get((l), partition_slot)
65#define	SLOT_SET(l, v)	mac_label_set((l), partition_slot, (v))
66
67static void
68mac_partition_init_label(struct label *label)
69{
70
71	SLOT_SET(label, 0);
72}
73
74static void
75mac_partition_destroy_label(struct label *label)
76{
77
78	SLOT_SET(label, 0);
79}
80
81static void
82mac_partition_copy_label(struct label *src, struct label *dest)
83{
84
85	SLOT_SET(dest, SLOT(src));
86}
87
88static int
89mac_partition_externalize_label(struct label *label, char *element_name,
90    struct sbuf *sb, int *claimed)
91{
92
93	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
94		return (0);
95
96	(*claimed)++;
97
98	if (sbuf_printf(sb, "%jd", (intmax_t)SLOT(label)) == -1)
99		return (EINVAL);
100	else
101		return (0);
102}
103
104static int
105mac_partition_internalize_label(struct label *label, char *element_name,
106    char *element_data, int *claimed)
107{
108
109	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
110		return (0);
111
112	(*claimed)++;
113	SLOT_SET(label, strtol(element_data, NULL, 10));
114	return (0);
115}
116
117static void
118mac_partition_create_proc0(struct ucred *cred)
119{
120
121	SLOT_SET(cred->cr_label, 0);
122}
123
124static void
125mac_partition_create_proc1(struct ucred *cred)
126{
127
128	SLOT_SET(cred->cr_label, 0);
129}
130
131static void
132mac_partition_relabel_cred(struct ucred *cred, struct label *newlabel)
133{
134
135	if (SLOT(newlabel) != 0)
136		SLOT_SET(cred->cr_label, SLOT(newlabel));
137}
138
139static int
140label_on_label(struct label *subject, struct label *object)
141{
142
143	if (mac_partition_enabled == 0)
144		return (0);
145
146	if (SLOT(subject) == 0)
147		return (0);
148
149	if (SLOT(subject) == SLOT(object))
150		return (0);
151
152	return (EPERM);
153}
154
155static int
156mac_partition_check_cred_relabel(struct ucred *cred, struct label *newlabel)
157{
158	int error;
159
160	error = 0;
161
162	/* Treat "0" as a no-op request. */
163	if (SLOT(newlabel) != 0) {
164		/*
165		 * Require BSD privilege in order to change the partition.
166		 * Originally we also required that the process not be
167		 * in a partition in the first place, but this didn't
168		 * interact well with sendmail.
169		 */
170		error = priv_check_cred(cred, PRIV_MAC_PARTITION, 0);
171	}
172
173	return (error);
174}
175
176static int
177mac_partition_check_cred_visible(struct ucred *u1, struct ucred *u2)
178{
179	int error;
180
181	error = label_on_label(u1->cr_label, u2->cr_label);
182
183	return (error == 0 ? 0 : ESRCH);
184}
185
186static int
187mac_partition_check_proc_debug(struct ucred *cred, struct proc *proc)
188{
189	int error;
190
191	error = label_on_label(cred->cr_label, proc->p_ucred->cr_label);
192
193	return (error ? ESRCH : 0);
194}
195
196static int
197mac_partition_check_proc_sched(struct ucred *cred, struct proc *proc)
198{
199	int error;
200
201	error = label_on_label(cred->cr_label, proc->p_ucred->cr_label);
202
203	return (error ? ESRCH : 0);
204}
205
206static int
207mac_partition_check_proc_signal(struct ucred *cred, struct proc *proc,
208    int signum)
209{
210	int error;
211
212	error = label_on_label(cred->cr_label, proc->p_ucred->cr_label);
213
214	return (error ? ESRCH : 0);
215}
216
217static int
218mac_partition_check_socket_visible(struct ucred *cred, struct socket *socket,
219    struct label *socketlabel)
220{
221	int error;
222
223	error = label_on_label(cred->cr_label, socketlabel);
224
225	return (error ? ENOENT : 0);
226}
227
228static int
229mac_partition_check_vnode_exec(struct ucred *cred, struct vnode *vp,
230    struct label *label, struct image_params *imgp, struct label *execlabel)
231{
232
233	if (execlabel != NULL) {
234		/*
235		 * We currently don't permit labels to be changed at
236		 * exec-time as part of the partition model, so disallow
237		 * non-NULL partition label changes in execlabel.
238		 */
239		if (SLOT(execlabel) != 0)
240			return (EINVAL);
241	}
242
243	return (0);
244}
245
246static struct mac_policy_ops mac_partition_ops =
247{
248	.mpo_init_cred_label = mac_partition_init_label,
249	.mpo_destroy_cred_label = mac_partition_destroy_label,
250	.mpo_copy_cred_label = mac_partition_copy_label,
251	.mpo_externalize_cred_label = mac_partition_externalize_label,
252	.mpo_internalize_cred_label = mac_partition_internalize_label,
253	.mpo_create_proc0 = mac_partition_create_proc0,
254	.mpo_create_proc1 = mac_partition_create_proc1,
255	.mpo_relabel_cred = mac_partition_relabel_cred,
256	.mpo_check_cred_relabel = mac_partition_check_cred_relabel,
257	.mpo_check_cred_visible = mac_partition_check_cred_visible,
258	.mpo_check_proc_debug = mac_partition_check_proc_debug,
259	.mpo_check_proc_sched = mac_partition_check_proc_sched,
260	.mpo_check_proc_signal = mac_partition_check_proc_signal,
261	.mpo_check_socket_visible = mac_partition_check_socket_visible,
262	.mpo_check_vnode_exec = mac_partition_check_vnode_exec,
263};
264
265MAC_POLICY_SET(&mac_partition_ops, mac_partition, "TrustedBSD MAC/Partition",
266    MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot);
267