mac_partition.c revision 126097
1105828Srwatson/*-
2126097Srwatson * Copyright (c) 1999-2002 Robert N. M. Watson
3126097Srwatson * Copyright (c) 2001-2002 Networks Associates Technology, Inc.
4105828Srwatson * All rights reserved.
5105828Srwatson *
6105828Srwatson * This software was developed by Robert Watson for the TrustedBSD Project.
7105828Srwatson *
8106393Srwatson * This software was developed for the FreeBSD Project in part by Network
9106393Srwatson * Associates Laboratories, the Security Research Division of Network
10106393Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
11106393Srwatson * as part of the DARPA CHATS research program.
12105828Srwatson *
13105828Srwatson * Redistribution and use in source and binary forms, with or without
14105828Srwatson * modification, are permitted provided that the following conditions
15105828Srwatson * are met:
16105828Srwatson * 1. Redistributions of source code must retain the above copyright
17105828Srwatson *    notice, this list of conditions and the following disclaimer.
18105828Srwatson * 2. Redistributions in binary form must reproduce the above copyright
19105828Srwatson *    notice, this list of conditions and the following disclaimer in the
20105828Srwatson *    documentation and/or other materials provided with the distribution.
21105828Srwatson *
22105828Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23105828Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24105828Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25105828Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26105828Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27105828Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28105828Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29105828Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30105828Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31105828Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32105828Srwatson * SUCH DAMAGE.
33105828Srwatson *
34105828Srwatson * $FreeBSD: head/sys/security/mac_partition/mac_partition.c 126097 2004-02-22 00:33:12Z rwatson $
35105828Srwatson */
36105828Srwatson
37105828Srwatson/*
38105828Srwatson * Developed by the TrustedBSD Project.
39105828Srwatson * Experiment with a partition-like model.
40105828Srwatson */
41105828Srwatson
42105828Srwatson#include <sys/types.h>
43105828Srwatson#include <sys/param.h>
44105828Srwatson#include <sys/conf.h>
45105828Srwatson#include <sys/kernel.h>
46105828Srwatson#include <sys/mac.h>
47105828Srwatson#include <sys/mount.h>
48105828Srwatson#include <sys/proc.h>
49116701Srwatson#include <sys/sbuf.h>
50105828Srwatson#include <sys/systm.h>
51105828Srwatson#include <sys/sysproto.h>
52105828Srwatson#include <sys/sysent.h>
53105828Srwatson#include <sys/vnode.h>
54105828Srwatson#include <sys/file.h>
55105828Srwatson#include <sys/socket.h>
56105828Srwatson#include <sys/socketvar.h>
57105828Srwatson#include <sys/sysctl.h>
58105828Srwatson
59105828Srwatson#include <fs/devfs/devfs.h>
60105828Srwatson
61105828Srwatson#include <net/bpfdesc.h>
62105828Srwatson#include <net/if.h>
63105828Srwatson#include <net/if_types.h>
64105828Srwatson#include <net/if_var.h>
65105828Srwatson
66105828Srwatson#include <vm/vm.h>
67105828Srwatson
68105828Srwatson#include <sys/mac_policy.h>
69105828Srwatson
70105828Srwatson#include <security/mac_partition/mac_partition.h>
71105828Srwatson
72105828SrwatsonSYSCTL_DECL(_security_mac);
73105828Srwatson
74105828SrwatsonSYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0,
75105828Srwatson    "TrustedBSD mac_partition policy controls");
76105828Srwatson
77105828Srwatsonstatic int	mac_partition_enabled = 1;
78105828SrwatsonSYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW,
79105828Srwatson    &mac_partition_enabled, 0, "Enforce partition policy");
80105828Srwatson
81105828Srwatsonstatic int	partition_slot;
82105828Srwatson#define	SLOT(l)	(LABEL_TO_SLOT((l), partition_slot).l_long)
83105828Srwatson
84105828Srwatsonstatic void
85105828Srwatsonmac_partition_init(struct mac_policy_conf *conf)
86105828Srwatson{
87105828Srwatson
88105828Srwatson}
89105828Srwatson
90105828Srwatsonstatic void
91105828Srwatsonmac_partition_init_label(struct label *label)
92105828Srwatson{
93105828Srwatson
94105828Srwatson	SLOT(label) = 0;
95105828Srwatson}
96105828Srwatson
97105828Srwatsonstatic void
98105828Srwatsonmac_partition_destroy_label(struct label *label)
99105828Srwatson{
100105828Srwatson
101105828Srwatson	SLOT(label) = 0;
102105828Srwatson}
103105828Srwatson
104123173Srwatsonstatic void
105123173Srwatsonmac_partition_copy_label(struct label *src, struct label *dest)
106123173Srwatson{
107123173Srwatson
108123173Srwatson	SLOT(dest) = SLOT(src);
109123173Srwatson}
110123173Srwatson
111105828Srwatsonstatic int
112105828Srwatsonmac_partition_externalize_label(struct label *label, char *element_name,
113116701Srwatson    struct sbuf *sb, int *claimed)
114105828Srwatson{
115105828Srwatson
116105828Srwatson	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
117105828Srwatson		return (0);
118105828Srwatson
119105828Srwatson	(*claimed)++;
120116701Srwatson
121116701Srwatson	if (sbuf_printf(sb, "%ld", SLOT(label)) == -1)
122116701Srwatson		return (EINVAL);
123116701Srwatson	else
124116701Srwatson		return (0);
125105828Srwatson}
126105828Srwatson
127105828Srwatsonstatic int
128105828Srwatsonmac_partition_internalize_label(struct label *label, char *element_name,
129105828Srwatson    char *element_data, int *claimed)
130105828Srwatson{
131105828Srwatson
132105828Srwatson	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
133105828Srwatson		return (0);
134105828Srwatson
135105828Srwatson	(*claimed)++;
136105828Srwatson	SLOT(label) = strtol(element_data, NULL, 10);
137105828Srwatson	return (0);
138105828Srwatson}
139105828Srwatson
140105828Srwatsonstatic void
141105828Srwatsonmac_partition_create_proc0(struct ucred *cred)
142105828Srwatson{
143105828Srwatson
144122524Srwatson	SLOT(cred->cr_label) = 0;
145105828Srwatson}
146105828Srwatson
147105828Srwatsonstatic void
148105828Srwatsonmac_partition_create_proc1(struct ucred *cred)
149105828Srwatson{
150105828Srwatson
151122524Srwatson	SLOT(cred->cr_label) = 0;
152105828Srwatson}
153105828Srwatson
154105828Srwatsonstatic void
155105828Srwatsonmac_partition_relabel_cred(struct ucred *cred, struct label *newlabel)
156105828Srwatson{
157105828Srwatson
158105828Srwatson	if (SLOT(newlabel) != 0)
159122524Srwatson		SLOT(cred->cr_label) = SLOT(newlabel);
160105828Srwatson}
161105828Srwatson
162105828Srwatsonstatic int
163105828Srwatsonlabel_on_label(struct label *subject, struct label *object)
164105828Srwatson{
165105828Srwatson
166105828Srwatson	if (mac_partition_enabled == 0)
167105828Srwatson		return (0);
168105828Srwatson
169105828Srwatson	if (SLOT(subject) == 0)
170105828Srwatson		return (0);
171105828Srwatson
172105828Srwatson	if (SLOT(subject) == SLOT(object))
173105828Srwatson		return (0);
174105828Srwatson
175105828Srwatson	return (EPERM);
176105828Srwatson}
177105828Srwatson
178105828Srwatsonstatic int
179105828Srwatsonmac_partition_check_cred_relabel(struct ucred *cred, struct label *newlabel)
180105828Srwatson{
181105828Srwatson	int error;
182105828Srwatson
183105828Srwatson	error = 0;
184105828Srwatson
185105828Srwatson	/* Treat "0" as a no-op request. */
186105828Srwatson	if (SLOT(newlabel) != 0) {
187105828Srwatson		/*
188106367Srwatson		 * Require BSD privilege in order to change the partition.
189106367Srwatson		 * Originally we also required that the process not be
190106367Srwatson		 * in a partition in the first place, but this didn't
191106367Srwatson		 * interact well with sendmail.
192105828Srwatson		 */
193105828Srwatson		error = suser_cred(cred, 0);
194105828Srwatson	}
195105828Srwatson
196105828Srwatson	return (error);
197105828Srwatson}
198105828Srwatson
199105828Srwatsonstatic int
200105828Srwatsonmac_partition_check_cred_visible(struct ucred *u1, struct ucred *u2)
201105828Srwatson{
202105828Srwatson	int error;
203105828Srwatson
204122524Srwatson	error = label_on_label(u1->cr_label, u2->cr_label);
205105828Srwatson
206105828Srwatson	return (error == 0 ? 0 : ESRCH);
207105828Srwatson}
208105828Srwatson
209105828Srwatsonstatic int
210105828Srwatsonmac_partition_check_proc_debug(struct ucred *cred, struct proc *proc)
211105828Srwatson{
212105828Srwatson	int error;
213105828Srwatson
214122524Srwatson	error = label_on_label(cred->cr_label, proc->p_ucred->cr_label);
215105828Srwatson
216105828Srwatson	return (error ? ESRCH : 0);
217105828Srwatson}
218105828Srwatson
219105828Srwatsonstatic int
220105828Srwatsonmac_partition_check_proc_sched(struct ucred *cred, struct proc *proc)
221105828Srwatson{
222105828Srwatson	int error;
223105828Srwatson
224122524Srwatson	error = label_on_label(cred->cr_label, proc->p_ucred->cr_label);
225105828Srwatson
226105828Srwatson	return (error ? ESRCH : 0);
227105828Srwatson}
228105828Srwatson
229105828Srwatsonstatic int
230105828Srwatsonmac_partition_check_proc_signal(struct ucred *cred, struct proc *proc,
231105828Srwatson    int signum)
232105828Srwatson{
233105828Srwatson	int error;
234105828Srwatson
235122524Srwatson	error = label_on_label(cred->cr_label, proc->p_ucred->cr_label);
236105828Srwatson
237105828Srwatson	return (error ? ESRCH : 0);
238105828Srwatson}
239105828Srwatson
240105828Srwatsonstatic int
241105828Srwatsonmac_partition_check_socket_visible(struct ucred *cred, struct socket *socket,
242105828Srwatson    struct label *socketlabel)
243105828Srwatson{
244105828Srwatson	int error;
245105828Srwatson
246122524Srwatson	error = label_on_label(cred->cr_label, socketlabel);
247105828Srwatson
248105828Srwatson	return (error ? ENOENT : 0);
249105828Srwatson}
250105828Srwatson
251106648Srwatsonstatic int
252106648Srwatsonmac_partition_check_vnode_exec(struct ucred *cred, struct vnode *vp,
253106648Srwatson    struct label *label, struct image_params *imgp, struct label *execlabel)
254106648Srwatson{
255106648Srwatson
256106648Srwatson	if (execlabel != NULL) {
257106648Srwatson		/*
258106648Srwatson		 * We currently don't permit labels to be changed at
259106648Srwatson		 * exec-time as part of the partition model, so disallow
260106648Srwatson		 * non-NULL partition label changes in execlabel.
261106648Srwatson		 */
262106648Srwatson		if (SLOT(execlabel) != 0)
263106648Srwatson			return (EINVAL);
264106648Srwatson	}
265106648Srwatson
266106648Srwatson	return (0);
267106648Srwatson}
268106648Srwatson
269106217Srwatsonstatic struct mac_policy_ops mac_partition_ops =
270105828Srwatson{
271106217Srwatson	.mpo_init = mac_partition_init,
272106217Srwatson	.mpo_init_cred_label = mac_partition_init_label,
273106217Srwatson	.mpo_destroy_cred_label = mac_partition_destroy_label,
274123173Srwatson	.mpo_copy_cred_label = mac_partition_copy_label,
275106217Srwatson	.mpo_externalize_cred_label = mac_partition_externalize_label,
276106217Srwatson	.mpo_internalize_cred_label = mac_partition_internalize_label,
277106217Srwatson	.mpo_create_proc0 = mac_partition_create_proc0,
278106217Srwatson	.mpo_create_proc1 = mac_partition_create_proc1,
279106217Srwatson	.mpo_relabel_cred = mac_partition_relabel_cred,
280106217Srwatson	.mpo_check_cred_relabel = mac_partition_check_cred_relabel,
281106217Srwatson	.mpo_check_cred_visible = mac_partition_check_cred_visible,
282106217Srwatson	.mpo_check_proc_debug = mac_partition_check_proc_debug,
283106217Srwatson	.mpo_check_proc_sched = mac_partition_check_proc_sched,
284106217Srwatson	.mpo_check_proc_signal = mac_partition_check_proc_signal,
285106217Srwatson	.mpo_check_socket_visible = mac_partition_check_socket_visible,
286106648Srwatson	.mpo_check_vnode_exec = mac_partition_check_vnode_exec,
287105828Srwatson};
288105828Srwatson
289112717SrwatsonMAC_POLICY_SET(&mac_partition_ops, mac_partition, "TrustedBSD MAC/Partition",
290112717Srwatson    MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot);
291