2 * Copyright (c) 1999-2002, 2007 Robert N. M. Watson
3 * Copyright (c) 2001-2002 Networks Associates Technology, Inc.
6 * This software was developed by Robert Watson for the TrustedBSD Project.
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.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
38 * Developed by the TrustedBSD Project.
40 * Experiment with a partition-like model.
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/systm.h>
52 #include <sys/sysctl.h>
54 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/in_pcb.h>
58 #include <security/mac/mac_policy.h>
59 #include <security/mac_partition/mac_partition.h>
61 SYSCTL_DECL(_security_mac);
63 SYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0,
64 "TrustedBSD mac_partition policy controls");
66 static int partition_enabled = 1;
67 SYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW,
68 &partition_enabled, 0, "Enforce partition policy");
70 static int partition_slot;
71 #define SLOT(l) mac_label_get((l), partition_slot)
72 #define SLOT_SET(l, v) mac_label_set((l), partition_slot, (v))
75 partition_init_label(struct label *label)
82 partition_destroy_label(struct label *label)
89 partition_copy_label(struct label *src, struct label *dest)
92 SLOT_SET(dest, SLOT(src));
96 partition_externalize_label(struct label *label, char *element_name,
97 struct sbuf *sb, int *claimed)
100 if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
105 if (sbuf_printf(sb, "%jd", (intmax_t)SLOT(label)) == -1)
112 partition_internalize_label(struct label *label, char *element_name,
113 char *element_data, int *claimed)
116 if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0)
120 SLOT_SET(label, strtol(element_data, NULL, 10));
125 partition_create_proc0(struct ucred *cred)
128 SLOT_SET(cred->cr_label, 0);
132 partition_create_proc1(struct ucred *cred)
135 SLOT_SET(cred->cr_label, 0);
139 partition_relabel_cred(struct ucred *cred, struct label *newlabel)
142 if (SLOT(newlabel) != 0)
143 SLOT_SET(cred->cr_label, SLOT(newlabel));
147 label_on_label(struct label *subject, struct label *object)
150 if (partition_enabled == 0)
153 if (SLOT(subject) == 0)
156 if (SLOT(subject) == SLOT(object))
163 partition_check_cred_relabel(struct ucred *cred, struct label *newlabel)
169 /* Treat "0" as a no-op request. */
170 if (SLOT(newlabel) != 0) {
172 * Require BSD privilege in order to change the partition.
173 * Originally we also required that the process not be
174 * in a partition in the first place, but this didn't
175 * interact well with sendmail.
177 error = priv_check_cred(cred, PRIV_MAC_PARTITION, 0);
184 partition_check_cred_visible(struct ucred *cr1, struct ucred *cr2)
188 error = label_on_label(cr1->cr_label, cr2->cr_label);
190 return (error == 0 ? 0 : ESRCH);
194 partition_check_inpcb_visible(struct ucred *cred, struct inpcb *inp,
195 struct label *inplabel)
199 error = label_on_label(cred->cr_label, inp->inp_cred->cr_label);
201 return (error ? ENOENT : 0);
205 partition_check_proc_debug(struct ucred *cred, struct proc *p)
209 error = label_on_label(cred->cr_label, p->p_ucred->cr_label);
211 return (error ? ESRCH : 0);
215 partition_check_proc_sched(struct ucred *cred, struct proc *p)
219 error = label_on_label(cred->cr_label, p->p_ucred->cr_label);
221 return (error ? ESRCH : 0);
225 partition_check_proc_signal(struct ucred *cred, struct proc *p,
230 error = label_on_label(cred->cr_label, p->p_ucred->cr_label);
232 return (error ? ESRCH : 0);
236 partition_check_socket_visible(struct ucred *cred, struct socket *so,
237 struct label *solabel)
241 error = label_on_label(cred->cr_label, so->so_cred->cr_label);
243 return (error ? ENOENT : 0);
247 partition_check_vnode_exec(struct ucred *cred, struct vnode *vp,
248 struct label *vplabel, struct image_params *imgp,
249 struct label *execlabel)
252 if (execlabel != NULL) {
254 * We currently don't permit labels to be changed at
255 * exec-time as part of the partition model, so disallow
256 * non-NULL partition label changes in execlabel.
258 if (SLOT(execlabel) != 0)
265 static struct mac_policy_ops partition_ops =
267 .mpo_init_cred_label = partition_init_label,
268 .mpo_destroy_cred_label = partition_destroy_label,
269 .mpo_copy_cred_label = partition_copy_label,
270 .mpo_externalize_cred_label = partition_externalize_label,
271 .mpo_internalize_cred_label = partition_internalize_label,
272 .mpo_create_proc0 = partition_create_proc0,
273 .mpo_create_proc1 = partition_create_proc1,
274 .mpo_relabel_cred = partition_relabel_cred,
275 .mpo_check_cred_relabel = partition_check_cred_relabel,
276 .mpo_check_cred_visible = partition_check_cred_visible,
277 .mpo_check_inpcb_visible = partition_check_inpcb_visible,
278 .mpo_check_proc_debug = partition_check_proc_debug,
279 .mpo_check_proc_sched = partition_check_proc_sched,
280 .mpo_check_proc_signal = partition_check_proc_signal,
281 .mpo_check_socket_visible = partition_check_socket_visible,
282 .mpo_check_vnode_exec = partition_check_vnode_exec,
285 MAC_POLICY_SET(&partition_ops, mac_partition, "TrustedBSD MAC/Partition",
286 MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot);