1105828Srwatson/*-
2184407Srwatson * Copyright (c) 1999-2002, 2007-2008 Robert N. M. Watson
3126097Srwatson * Copyright (c) 2001-2002 Networks Associates Technology, Inc.
4172930Srwatson * Copyright (c) 2006 SPARTA, Inc.
5182063Srwatson * Copyright (c) 2008 Apple Inc.
6105828Srwatson * All rights reserved.
7105828Srwatson *
8105828Srwatson * This software was developed by Robert Watson for the TrustedBSD Project.
9105828Srwatson *
10106393Srwatson * This software was developed for the FreeBSD Project in part by Network
11106393Srwatson * Associates Laboratories, the Security Research Division of Network
12106393Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
13106393Srwatson * as part of the DARPA CHATS research program.
14105828Srwatson *
15172930Srwatson * This software was enhanced by SPARTA ISSO under SPAWAR contract
16172930Srwatson * N66001-04-C-6019 ("SEFOS").
17172930Srwatson *
18105828Srwatson * Redistribution and use in source and binary forms, with or without
19105828Srwatson * modification, are permitted provided that the following conditions
20105828Srwatson * are met:
21105828Srwatson * 1. Redistributions of source code must retain the above copyright
22105828Srwatson *    notice, this list of conditions and the following disclaimer.
23105828Srwatson * 2. Redistributions in binary form must reproduce the above copyright
24105828Srwatson *    notice, this list of conditions and the following disclaimer in the
25105828Srwatson *    documentation and/or other materials provided with the distribution.
26105828Srwatson *
27105828Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28105828Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29105828Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30105828Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31105828Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32105828Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33105828Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34105828Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35105828Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36105828Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37105828Srwatson * SUCH DAMAGE.
38105828Srwatson *
39105828Srwatson * $FreeBSD$
40105828Srwatson */
41105828Srwatson
42105828Srwatson/*
43105828Srwatson * Developed by the TrustedBSD Project.
44172955Srwatson *
45105828Srwatson * Experiment with a partition-like model.
46105828Srwatson */
47105828Srwatson
48105828Srwatson#include <sys/param.h>
49105828Srwatson#include <sys/kernel.h>
50166905Srwatson#include <sys/module.h>
51164033Srwatson#include <sys/priv.h>
52105828Srwatson#include <sys/proc.h>
53116701Srwatson#include <sys/sbuf.h>
54183980Sbz#include <sys/socket.h>
55183970Sbz#include <sys/socketvar.h>
56105828Srwatson#include <sys/systm.h>
57105828Srwatson#include <sys/sysctl.h>
58105828Srwatson
59183980Sbz#include <net/route.h>
60183980Sbz#include <netinet/in.h>
61183980Sbz#include <netinet/in_pcb.h>
62183980Sbz
63165469Srwatson#include <security/mac/mac_policy.h>
64105828Srwatson#include <security/mac_partition/mac_partition.h>
65105828Srwatson
66105828SrwatsonSYSCTL_DECL(_security_mac);
67105828Srwatson
68227309Sedstatic SYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0,
69105828Srwatson    "TrustedBSD mac_partition policy controls");
70105828Srwatson
71181213Srwatsonstatic int	partition_enabled = 1;
72105828SrwatsonSYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW,
73181213Srwatson    &partition_enabled, 0, "Enforce partition policy");
74105828Srwatson
75105828Srwatsonstatic int	partition_slot;
76166533Srwatson#define	SLOT(l)	mac_label_get((l), partition_slot)
77166533Srwatson#define	SLOT_SET(l, v)	mac_label_set((l), partition_slot, (v))
78105828Srwatson
79173138Srwatsonstatic int
80184401Srwatsonpartition_check(struct label *subject, struct label *object)
81105828Srwatson{
82105828Srwatson
83181213Srwatson	if (partition_enabled == 0)
84173138Srwatson		return (0);
85105828Srwatson
86182063Srwatson	if (subject == NULL)
87182063Srwatson		return (0);
88182063Srwatson
89173138Srwatson	if (SLOT(subject) == 0)
90173138Srwatson		return (0);
91105828Srwatson
92182063Srwatson	/*
93182063Srwatson	 * If the object label hasn't been allocated, then it's effectively
94182063Srwatson	 * not in a partition, and we know the subject is as it has a label
95182063Srwatson	 * and it's not 0, so reject.
96182063Srwatson	 */
97182063Srwatson	if (object == NULL)
98182063Srwatson		return (EPERM);
99182063Srwatson
100173138Srwatson	if (SLOT(subject) == SLOT(object))
101173138Srwatson		return (0);
102105828Srwatson
103173138Srwatson	return (EPERM);
104123173Srwatson}
105123173Srwatson
106173138Srwatson/*
107173138Srwatson * Object-specific entry points are sorted alphabetically by object type name
108173138Srwatson * and then by operation.
109173138Srwatson */
110105828Srwatsonstatic int
111173138Srwatsonpartition_cred_check_relabel(struct ucred *cred, struct label *newlabel)
112105828Srwatson{
113173138Srwatson	int error;
114105828Srwatson
115173138Srwatson	error = 0;
116105828Srwatson
117184402Srwatson	/*
118184402Srwatson	 * Treat "0" as a no-op request because it reflects an unset
119184402Srwatson	 * partition label.  If we ever want to support switching back to an
120184402Srwatson	 * unpartitioned state for a process, we'll need to differentiate the
121184402Srwatson	 * "not in a partition" and "no partition defined during internalize"
122184402Srwatson	 * conditions.
123184402Srwatson	 */
124173138Srwatson	if (SLOT(newlabel) != 0) {
125173138Srwatson		/*
126173138Srwatson		 * Require BSD privilege in order to change the partition.
127173138Srwatson		 * Originally we also required that the process not be in a
128173138Srwatson		 * partition in the first place, but this didn't interact
129173138Srwatson		 * well with sendmail.
130173138Srwatson		 */
131173138Srwatson		error = priv_check_cred(cred, PRIV_MAC_PARTITION, 0);
132173138Srwatson	}
133116701Srwatson
134173138Srwatson	return (error);
135105828Srwatson}
136105828Srwatson
137105828Srwatsonstatic int
138173138Srwatsonpartition_cred_check_visible(struct ucred *cr1, struct ucred *cr2)
139105828Srwatson{
140173138Srwatson	int error;
141105828Srwatson
142184401Srwatson	error = partition_check(cr1->cr_label, cr2->cr_label);
143105828Srwatson
144173138Srwatson	return (error == 0 ? 0 : ESRCH);
145105828Srwatson}
146105828Srwatson
147105828Srwatsonstatic void
148173138Srwatsonpartition_cred_copy_label(struct label *src, struct label *dest)
149105828Srwatson{
150105828Srwatson
151182063Srwatson	if (src != NULL && dest != NULL)
152182063Srwatson		SLOT_SET(dest, SLOT(src));
153182063Srwatson	else if (dest != NULL)
154182063Srwatson		SLOT_SET(dest, 0);
155105828Srwatson}
156105828Srwatson
157105828Srwatsonstatic void
158184407Srwatsonpartition_cred_create_init(struct ucred *cred)
159184407Srwatson{
160184407Srwatson
161184407Srwatson	SLOT_SET(cred->cr_label, 0);
162184407Srwatson}
163184407Srwatson
164184407Srwatsonstatic void
165184407Srwatsonpartition_cred_create_swapper(struct ucred *cred)
166184407Srwatson{
167184407Srwatson
168184407Srwatson	SLOT_SET(cred->cr_label, 0);
169184407Srwatson}
170184407Srwatson
171184407Srwatsonstatic void
172173138Srwatsonpartition_cred_destroy_label(struct label *label)
173105828Srwatson{
174105828Srwatson
175173138Srwatson	SLOT_SET(label, 0);
176105828Srwatson}
177105828Srwatson
178105828Srwatsonstatic int
179173138Srwatsonpartition_cred_externalize_label(struct label *label, char *element_name,
180173138Srwatson    struct sbuf *sb, int *claimed)
181105828Srwatson{
182105828Srwatson
183173138Srwatson	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
184105828Srwatson		return (0);
185105828Srwatson
186173138Srwatson	(*claimed)++;
187105828Srwatson
188182063Srwatson	if (label != NULL) {
189182063Srwatson		if (sbuf_printf(sb, "%jd", (intmax_t)SLOT(label)) == -1)
190182063Srwatson			return (EINVAL);
191182063Srwatson	} else {
192182063Srwatson		if (sbuf_printf(sb, "0") == -1)
193182063Srwatson			return (EINVAL);
194182063Srwatson	}
195182063Srwatson	return (0);
196173138Srwatson}
197105828Srwatson
198173138Srwatsonstatic void
199173138Srwatsonpartition_cred_init_label(struct label *label)
200173138Srwatson{
201173138Srwatson
202173138Srwatson	SLOT_SET(label, 0);
203105828Srwatson}
204105828Srwatson
205105828Srwatsonstatic int
206173138Srwatsonpartition_cred_internalize_label(struct label *label, char *element_name,
207173138Srwatson    char *element_data, int *claimed)
208105828Srwatson{
209105828Srwatson
210173138Srwatson	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
211173138Srwatson		return (0);
212105828Srwatson
213173138Srwatson	(*claimed)++;
214173138Srwatson	SLOT_SET(label, strtol(element_data, NULL, 10));
215173138Srwatson	return (0);
216105828Srwatson}
217105828Srwatson
218173138Srwatsonstatic void
219173138Srwatsonpartition_cred_relabel(struct ucred *cred, struct label *newlabel)
220105828Srwatson{
221105828Srwatson
222182063Srwatson	if (newlabel != NULL && SLOT(newlabel) != 0)
223173138Srwatson		SLOT_SET(cred->cr_label, SLOT(newlabel));
224105828Srwatson}
225105828Srwatson
226105828Srwatsonstatic int
227183980Sbzpartition_inpcb_check_visible(struct ucred *cred, struct inpcb *inp,
228183980Sbz    struct label *inplabel)
229183980Sbz{
230183980Sbz	int error;
231183980Sbz
232184401Srwatson	error = partition_check(cred->cr_label, inp->inp_cred->cr_label);
233183980Sbz
234183980Sbz	return (error ? ENOENT : 0);
235183980Sbz}
236183980Sbz
237183980Sbzstatic int
238172955Srwatsonpartition_proc_check_debug(struct ucred *cred, struct proc *p)
239105828Srwatson{
240105828Srwatson	int error;
241105828Srwatson
242184401Srwatson	error = partition_check(cred->cr_label, p->p_ucred->cr_label);
243105828Srwatson
244105828Srwatson	return (error ? ESRCH : 0);
245105828Srwatson}
246105828Srwatson
247105828Srwatsonstatic int
248172955Srwatsonpartition_proc_check_sched(struct ucred *cred, struct proc *p)
249105828Srwatson{
250105828Srwatson	int error;
251105828Srwatson
252184401Srwatson	error = partition_check(cred->cr_label, p->p_ucred->cr_label);
253105828Srwatson
254105828Srwatson	return (error ? ESRCH : 0);
255105828Srwatson}
256105828Srwatson
257105828Srwatsonstatic int
258172955Srwatsonpartition_proc_check_signal(struct ucred *cred, struct proc *p,
259105828Srwatson    int signum)
260105828Srwatson{
261105828Srwatson	int error;
262105828Srwatson
263184401Srwatson	error = partition_check(cred->cr_label, p->p_ucred->cr_label);
264105828Srwatson
265105828Srwatson	return (error ? ESRCH : 0);
266105828Srwatson}
267105828Srwatson
268105828Srwatsonstatic int
269172955Srwatsonpartition_socket_check_visible(struct ucred *cred, struct socket *so,
270168976Srwatson    struct label *solabel)
271105828Srwatson{
272105828Srwatson	int error;
273105828Srwatson
274184401Srwatson	error = partition_check(cred->cr_label, so->so_cred->cr_label);
275105828Srwatson
276105828Srwatson	return (error ? ENOENT : 0);
277105828Srwatson}
278105828Srwatson
279106648Srwatsonstatic int
280172955Srwatsonpartition_vnode_check_exec(struct ucred *cred, struct vnode *vp,
281168976Srwatson    struct label *vplabel, struct image_params *imgp,
282168976Srwatson    struct label *execlabel)
283106648Srwatson{
284106648Srwatson
285106648Srwatson	if (execlabel != NULL) {
286106648Srwatson		/*
287106648Srwatson		 * We currently don't permit labels to be changed at
288106648Srwatson		 * exec-time as part of the partition model, so disallow
289106648Srwatson		 * non-NULL partition label changes in execlabel.
290106648Srwatson		 */
291106648Srwatson		if (SLOT(execlabel) != 0)
292106648Srwatson			return (EINVAL);
293106648Srwatson	}
294106648Srwatson
295106648Srwatson	return (0);
296106648Srwatson}
297106648Srwatson
298172955Srwatsonstatic struct mac_policy_ops partition_ops =
299105828Srwatson{
300172955Srwatson	.mpo_cred_check_relabel = partition_cred_check_relabel,
301172955Srwatson	.mpo_cred_check_visible = partition_cred_check_visible,
302173138Srwatson	.mpo_cred_copy_label = partition_cred_copy_label,
303184407Srwatson	.mpo_cred_create_init = partition_cred_create_init,
304184407Srwatson	.mpo_cred_create_swapper = partition_cred_create_swapper,
305173138Srwatson	.mpo_cred_destroy_label = partition_cred_destroy_label,
306173138Srwatson	.mpo_cred_externalize_label = partition_cred_externalize_label,
307173138Srwatson	.mpo_cred_init_label = partition_cred_init_label,
308173138Srwatson	.mpo_cred_internalize_label = partition_cred_internalize_label,
309173138Srwatson	.mpo_cred_relabel = partition_cred_relabel,
310183980Sbz	.mpo_inpcb_check_visible = partition_inpcb_check_visible,
311172955Srwatson	.mpo_proc_check_debug = partition_proc_check_debug,
312172955Srwatson	.mpo_proc_check_sched = partition_proc_check_sched,
313172955Srwatson	.mpo_proc_check_signal = partition_proc_check_signal,
314172955Srwatson	.mpo_socket_check_visible = partition_socket_check_visible,
315172955Srwatson	.mpo_vnode_check_exec = partition_vnode_check_exec,
316105828Srwatson};
317105828Srwatson
318172955SrwatsonMAC_POLICY_SET(&partition_ops, mac_partition, "TrustedBSD MAC/Partition",
319187016Srwatson    MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot);
320