mac_partition.c revision 106393
1105828Srwatson/*-
2105828Srwatson * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
3105828Srwatson * 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 106393 2002-11-04 01:53: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>
49105828Srwatson#include <sys/systm.h>
50105828Srwatson#include <sys/sysproto.h>
51105828Srwatson#include <sys/sysent.h>
52105828Srwatson#include <sys/vnode.h>
53105828Srwatson#include <sys/file.h>
54105828Srwatson#include <sys/socket.h>
55105828Srwatson#include <sys/socketvar.h>
56105828Srwatson#include <sys/sysctl.h>
57105828Srwatson
58105828Srwatson#include <fs/devfs/devfs.h>
59105828Srwatson
60105828Srwatson#include <net/bpfdesc.h>
61105828Srwatson#include <net/if.h>
62105828Srwatson#include <net/if_types.h>
63105828Srwatson#include <net/if_var.h>
64105828Srwatson
65105828Srwatson#include <vm/vm.h>
66105828Srwatson
67105828Srwatson#include <sys/mac_policy.h>
68105828Srwatson
69105828Srwatson#include <security/mac_partition/mac_partition.h>
70105828Srwatson
71105828SrwatsonSYSCTL_DECL(_security_mac);
72105828Srwatson
73105828SrwatsonSYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0,
74105828Srwatson    "TrustedBSD mac_partition policy controls");
75105828Srwatson
76105828Srwatsonstatic int	mac_partition_enabled = 1;
77105828SrwatsonSYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW,
78105828Srwatson    &mac_partition_enabled, 0, "Enforce partition policy");
79105828Srwatson
80105828Srwatsonstatic int	partition_slot;
81105828Srwatson#define	SLOT(l)	(LABEL_TO_SLOT((l), partition_slot).l_long)
82105828Srwatson
83105828Srwatsonstatic void
84105828Srwatsonmac_partition_init(struct mac_policy_conf *conf)
85105828Srwatson{
86105828Srwatson
87105828Srwatson}
88105828Srwatson
89105828Srwatsonstatic void
90105828Srwatsonmac_partition_init_label(struct label *label)
91105828Srwatson{
92105828Srwatson
93105828Srwatson	SLOT(label) = 0;
94105828Srwatson}
95105828Srwatson
96105828Srwatsonstatic void
97105828Srwatsonmac_partition_destroy_label(struct label *label)
98105828Srwatson{
99105828Srwatson
100105828Srwatson	SLOT(label) = 0;
101105828Srwatson}
102105828Srwatson
103105828Srwatsonstatic int
104105828Srwatsonmac_partition_externalize_label(struct label *label, char *element_name,
105105828Srwatson    char *element_data, size_t size, size_t *len, int *claimed)
106105828Srwatson{
107105828Srwatson
108105828Srwatson	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
109105828Srwatson		return (0);
110105828Srwatson
111105828Srwatson	(*claimed)++;
112105828Srwatson	*len = snprintf(element_data, size, "%ld", SLOT(label));
113105828Srwatson	return (0);
114105828Srwatson}
115105828Srwatson
116105828Srwatsonstatic int
117105828Srwatsonmac_partition_internalize_label(struct label *label, char *element_name,
118105828Srwatson    char *element_data, int *claimed)
119105828Srwatson{
120105828Srwatson
121105828Srwatson	if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
122105828Srwatson		return (0);
123105828Srwatson
124105828Srwatson	(*claimed)++;
125105828Srwatson	SLOT(label) = strtol(element_data, NULL, 10);
126105828Srwatson	return (0);
127105828Srwatson}
128105828Srwatson
129105828Srwatsonstatic void
130105828Srwatsonmac_partition_create_cred(struct ucred *cred_parent, struct ucred *cred_child)
131105828Srwatson{
132105828Srwatson
133105828Srwatson	SLOT(&cred_child->cr_label) = SLOT(&cred_parent->cr_label);
134105828Srwatson}
135105828Srwatson
136105828Srwatsonstatic void
137105828Srwatsonmac_partition_create_proc0(struct ucred *cred)
138105828Srwatson{
139105828Srwatson
140105828Srwatson	SLOT(&cred->cr_label) = 0;
141105828Srwatson}
142105828Srwatson
143105828Srwatsonstatic void
144105828Srwatsonmac_partition_create_proc1(struct ucred *cred)
145105828Srwatson{
146105828Srwatson
147105828Srwatson	SLOT(&cred->cr_label) = 0;
148105828Srwatson}
149105828Srwatson
150105828Srwatsonstatic void
151105828Srwatsonmac_partition_relabel_cred(struct ucred *cred, struct label *newlabel)
152105828Srwatson{
153105828Srwatson
154105828Srwatson	if (SLOT(newlabel) != 0)
155105828Srwatson		SLOT(&cred->cr_label) = SLOT(newlabel);
156105828Srwatson}
157105828Srwatson
158105828Srwatsonstatic int
159105828Srwatsonlabel_on_label(struct label *subject, struct label *object)
160105828Srwatson{
161105828Srwatson
162105828Srwatson	if (mac_partition_enabled == 0)
163105828Srwatson		return (0);
164105828Srwatson
165105828Srwatson	if (SLOT(subject) == 0)
166105828Srwatson		return (0);
167105828Srwatson
168105828Srwatson	if (SLOT(subject) == SLOT(object))
169105828Srwatson		return (0);
170105828Srwatson
171105828Srwatson	return (EPERM);
172105828Srwatson}
173105828Srwatson
174105828Srwatsonstatic int
175105828Srwatsonmac_partition_check_cred_relabel(struct ucred *cred, struct label *newlabel)
176105828Srwatson{
177105828Srwatson	int error;
178105828Srwatson
179105828Srwatson	error = 0;
180105828Srwatson
181105828Srwatson	/* Treat "0" as a no-op request. */
182105828Srwatson	if (SLOT(newlabel) != 0) {
183105828Srwatson		/*
184106367Srwatson		 * Require BSD privilege in order to change the partition.
185106367Srwatson		 * Originally we also required that the process not be
186106367Srwatson		 * in a partition in the first place, but this didn't
187106367Srwatson		 * interact well with sendmail.
188105828Srwatson		 */
189105828Srwatson		error = suser_cred(cred, 0);
190105828Srwatson	}
191105828Srwatson
192105828Srwatson	return (error);
193105828Srwatson}
194105828Srwatson
195105828Srwatsonstatic int
196105828Srwatsonmac_partition_check_cred_visible(struct ucred *u1, struct ucred *u2)
197105828Srwatson{
198105828Srwatson	int error;
199105828Srwatson
200105828Srwatson	error = label_on_label(&u1->cr_label, &u2->cr_label);
201105828Srwatson
202105828Srwatson	return (error == 0 ? 0 : ESRCH);
203105828Srwatson}
204105828Srwatson
205105828Srwatsonstatic int
206105828Srwatsonmac_partition_check_proc_debug(struct ucred *cred, struct proc *proc)
207105828Srwatson{
208105828Srwatson	int error;
209105828Srwatson
210105828Srwatson	error = label_on_label(&cred->cr_label, &proc->p_ucred->cr_label);
211105828Srwatson
212105828Srwatson	return (error ? ESRCH : 0);
213105828Srwatson}
214105828Srwatson
215105828Srwatsonstatic int
216105828Srwatsonmac_partition_check_proc_sched(struct ucred *cred, struct proc *proc)
217105828Srwatson{
218105828Srwatson	int error;
219105828Srwatson
220105828Srwatson	error = label_on_label(&cred->cr_label, &proc->p_ucred->cr_label);
221105828Srwatson
222105828Srwatson	return (error ? ESRCH : 0);
223105828Srwatson}
224105828Srwatson
225105828Srwatsonstatic int
226105828Srwatsonmac_partition_check_proc_signal(struct ucred *cred, struct proc *proc,
227105828Srwatson    int signum)
228105828Srwatson{
229105828Srwatson	int error;
230105828Srwatson
231105828Srwatson	error = label_on_label(&cred->cr_label, &proc->p_ucred->cr_label);
232105828Srwatson
233105828Srwatson	return (error ? ESRCH : 0);
234105828Srwatson}
235105828Srwatson
236105828Srwatsonstatic int
237105828Srwatsonmac_partition_check_socket_visible(struct ucred *cred, struct socket *socket,
238105828Srwatson    struct label *socketlabel)
239105828Srwatson{
240105828Srwatson	int error;
241105828Srwatson
242105828Srwatson	error = label_on_label(&cred->cr_label, socketlabel);
243105828Srwatson
244105828Srwatson	return (error ? ENOENT : 0);
245105828Srwatson}
246105828Srwatson
247106217Srwatsonstatic struct mac_policy_ops mac_partition_ops =
248105828Srwatson{
249106217Srwatson	.mpo_init = mac_partition_init,
250106217Srwatson	.mpo_init_cred_label = mac_partition_init_label,
251106217Srwatson	.mpo_destroy_cred_label = mac_partition_destroy_label,
252106217Srwatson	.mpo_externalize_cred_label = mac_partition_externalize_label,
253106217Srwatson	.mpo_internalize_cred_label = mac_partition_internalize_label,
254106217Srwatson	.mpo_create_cred = mac_partition_create_cred,
255106217Srwatson	.mpo_create_proc0 = mac_partition_create_proc0,
256106217Srwatson	.mpo_create_proc1 = mac_partition_create_proc1,
257106217Srwatson	.mpo_relabel_cred = mac_partition_relabel_cred,
258106217Srwatson	.mpo_check_cred_relabel = mac_partition_check_cred_relabel,
259106217Srwatson	.mpo_check_cred_visible = mac_partition_check_cred_visible,
260106217Srwatson	.mpo_check_proc_debug = mac_partition_check_proc_debug,
261106217Srwatson	.mpo_check_proc_sched = mac_partition_check_proc_sched,
262106217Srwatson	.mpo_check_proc_signal = mac_partition_check_proc_signal,
263106217Srwatson	.mpo_check_socket_visible = mac_partition_check_socket_visible,
264105828Srwatson};
265105828Srwatson
266106217SrwatsonMAC_POLICY_SET(&mac_partition_ops, trustedbsd_mac_partition,
267105828Srwatson    "TrustedBSD MAC/Partition", MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot);
268