xref: /freebsd/sys/security/mac_partition/mac_partition.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * Copyright (c) 1999-2002, 2007-2008 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 
40 /*
41  * Developed by the TrustedBSD Project.
42  *
43  * Experiment with a partition-like model.
44  */
45 
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/module.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/sbuf.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/systm.h>
55 #include <sys/sysctl.h>
56 
57 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_pcb.h>
60 
61 #include <security/mac/mac_policy.h>
62 #include <security/mac_partition/mac_partition.h>
63 
64 SYSCTL_DECL(_security_mac);
65 
66 static SYSCTL_NODE(_security_mac, OID_AUTO, partition,
67     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
68     "TrustedBSD mac_partition policy controls");
69 
70 static int	partition_enabled = 1;
71 SYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW,
72     &partition_enabled, 0, "Enforce partition policy");
73 
74 static int	partition_slot;
75 #define	SLOT(l)	mac_label_get((l), partition_slot)
76 #define	SLOT_SET(l, v)	mac_label_set((l), partition_slot, (v))
77 
78 static int
79 partition_check(struct label *subject, struct label *object)
80 {
81 
82 	if (partition_enabled == 0)
83 		return (0);
84 
85 	if (subject == NULL)
86 		return (0);
87 
88 	if (SLOT(subject) == 0)
89 		return (0);
90 
91 	/*
92 	 * If the object label hasn't been allocated, then it's effectively
93 	 * not in a partition, and we know the subject is as it has a label
94 	 * and it's not 0, so reject.
95 	 */
96 	if (object == NULL)
97 		return (EPERM);
98 
99 	if (SLOT(subject) == SLOT(object))
100 		return (0);
101 
102 	return (EPERM);
103 }
104 
105 /*
106  * Object-specific entry points are sorted alphabetically by object type name
107  * and then by operation.
108  */
109 static int
110 partition_cred_check_relabel(struct ucred *cred, struct label *newlabel)
111 {
112 	int error;
113 
114 	error = 0;
115 
116 	/*
117 	 * Treat "0" as a no-op request because it reflects an unset
118 	 * partition label.  If we ever want to support switching back to an
119 	 * unpartitioned state for a process, we'll need to differentiate the
120 	 * "not in a partition" and "no partition defined during internalize"
121 	 * conditions.
122 	 */
123 	if (SLOT(newlabel) != 0) {
124 		/*
125 		 * Require BSD privilege in order to change the partition.
126 		 * Originally we also required that the process not be in a
127 		 * partition in the first place, but this didn't interact
128 		 * well with sendmail.
129 		 */
130 		error = priv_check_cred(cred, PRIV_MAC_PARTITION);
131 	}
132 
133 	return (error);
134 }
135 
136 static int
137 partition_cred_check_visible(struct ucred *cr1, struct ucred *cr2)
138 {
139 	int error;
140 
141 	error = partition_check(cr1->cr_label, cr2->cr_label);
142 
143 	return (error == 0 ? 0 : ESRCH);
144 }
145 
146 static void
147 partition_cred_copy_label(struct label *src, struct label *dest)
148 {
149 
150 	if (src != NULL && dest != NULL)
151 		SLOT_SET(dest, SLOT(src));
152 	else if (dest != NULL)
153 		SLOT_SET(dest, 0);
154 }
155 
156 static void
157 partition_cred_create_init(struct ucred *cred)
158 {
159 
160 	SLOT_SET(cred->cr_label, 0);
161 }
162 
163 static void
164 partition_cred_create_swapper(struct ucred *cred)
165 {
166 
167 	SLOT_SET(cred->cr_label, 0);
168 }
169 
170 static void
171 partition_cred_destroy_label(struct label *label)
172 {
173 
174 	SLOT_SET(label, 0);
175 }
176 
177 static int
178 partition_cred_externalize_label(struct label *label, char *element_name,
179     struct sbuf *sb, int *claimed)
180 {
181 
182 	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
183 		return (0);
184 
185 	(*claimed)++;
186 
187 	if (label != NULL) {
188 		if (sbuf_printf(sb, "%jd", (intmax_t)SLOT(label)) == -1)
189 			return (EINVAL);
190 	} else {
191 		if (sbuf_printf(sb, "0") == -1)
192 			return (EINVAL);
193 	}
194 	return (0);
195 }
196 
197 static void
198 partition_cred_init_label(struct label *label)
199 {
200 
201 	SLOT_SET(label, 0);
202 }
203 
204 static int
205 partition_cred_internalize_label(struct label *label, char *element_name,
206     char *element_data, int *claimed)
207 {
208 
209 	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
210 		return (0);
211 
212 	(*claimed)++;
213 	SLOT_SET(label, strtol(element_data, NULL, 10));
214 	return (0);
215 }
216 
217 static void
218 partition_cred_relabel(struct ucred *cred, struct label *newlabel)
219 {
220 
221 	if (newlabel != NULL && SLOT(newlabel) != 0)
222 		SLOT_SET(cred->cr_label, SLOT(newlabel));
223 }
224 
225 static int
226 partition_inpcb_check_visible(struct ucred *cred, struct inpcb *inp,
227     struct label *inplabel)
228 {
229 	int error;
230 
231 	error = partition_check(cred->cr_label, inp->inp_cred->cr_label);
232 
233 	return (error ? ENOENT : 0);
234 }
235 
236 static int
237 partition_proc_check_debug(struct ucred *cred, struct proc *p)
238 {
239 	int error;
240 
241 	error = partition_check(cred->cr_label, p->p_ucred->cr_label);
242 
243 	return (error ? ESRCH : 0);
244 }
245 
246 static int
247 partition_proc_check_sched(struct ucred *cred, struct proc *p)
248 {
249 	int error;
250 
251 	error = partition_check(cred->cr_label, p->p_ucred->cr_label);
252 
253 	return (error ? ESRCH : 0);
254 }
255 
256 static int
257 partition_proc_check_signal(struct ucred *cred, struct proc *p,
258     int signum)
259 {
260 	int error;
261 
262 	error = partition_check(cred->cr_label, p->p_ucred->cr_label);
263 
264 	return (error ? ESRCH : 0);
265 }
266 
267 static int
268 partition_socket_check_visible(struct ucred *cred, struct socket *so,
269     struct label *solabel)
270 {
271 	int error;
272 
273 	error = partition_check(cred->cr_label, so->so_cred->cr_label);
274 
275 	return (error ? ENOENT : 0);
276 }
277 
278 static int
279 partition_vnode_check_exec(struct ucred *cred, struct vnode *vp,
280     struct label *vplabel, struct image_params *imgp,
281     struct label *execlabel)
282 {
283 
284 	if (execlabel != NULL) {
285 		/*
286 		 * We currently don't permit labels to be changed at
287 		 * exec-time as part of the partition model, so disallow
288 		 * non-NULL partition label changes in execlabel.
289 		 */
290 		if (SLOT(execlabel) != 0)
291 			return (EINVAL);
292 	}
293 
294 	return (0);
295 }
296 
297 static struct mac_policy_ops partition_ops =
298 {
299 	.mpo_cred_check_relabel = partition_cred_check_relabel,
300 	.mpo_cred_check_visible = partition_cred_check_visible,
301 	.mpo_cred_copy_label = partition_cred_copy_label,
302 	.mpo_cred_create_init = partition_cred_create_init,
303 	.mpo_cred_create_swapper = partition_cred_create_swapper,
304 	.mpo_cred_destroy_label = partition_cred_destroy_label,
305 	.mpo_cred_externalize_label = partition_cred_externalize_label,
306 	.mpo_cred_init_label = partition_cred_init_label,
307 	.mpo_cred_internalize_label = partition_cred_internalize_label,
308 	.mpo_cred_relabel = partition_cred_relabel,
309 	.mpo_inpcb_check_visible = partition_inpcb_check_visible,
310 	.mpo_proc_check_debug = partition_proc_check_debug,
311 	.mpo_proc_check_sched = partition_proc_check_sched,
312 	.mpo_proc_check_signal = partition_proc_check_signal,
313 	.mpo_socket_check_visible = partition_socket_check_visible,
314 	.mpo_vnode_check_exec = partition_vnode_check_exec,
315 };
316 
317 MAC_POLICY_SET(&partition_ops, mac_partition, "TrustedBSD MAC/Partition",
318     MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot);
319