1/*- 2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson 3 * Copyright (c) 2001, 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed by Robert Watson for the TrustedBSD Project. 7 * 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. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 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. 21 * 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 32 * SUCH DAMAGE. 33 *
| 1/*- 2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson 3 * Copyright (c) 2001, 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed by Robert Watson for the TrustedBSD Project. 7 * 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. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 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. 21 * 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 32 * SUCH DAMAGE. 33 *
|
34 * $FreeBSD: head/sys/security/mac_partition/mac_partition.c 112717 2003-03-27 19:26:39Z rwatson $
| 34 * $FreeBSD: head/sys/security/mac_partition/mac_partition.c 116701 2003-06-23 01:26:34Z rwatson $
|
35 */ 36 37/* 38 * Developed by the TrustedBSD Project. 39 * Experiment with a partition-like model. 40 */ 41 42#include <sys/types.h> 43#include <sys/param.h> 44#include <sys/conf.h> 45#include <sys/kernel.h> 46#include <sys/mac.h> 47#include <sys/mount.h> 48#include <sys/proc.h>
| 35 */ 36 37/* 38 * Developed by the TrustedBSD Project. 39 * Experiment with a partition-like model. 40 */ 41 42#include <sys/types.h> 43#include <sys/param.h> 44#include <sys/conf.h> 45#include <sys/kernel.h> 46#include <sys/mac.h> 47#include <sys/mount.h> 48#include <sys/proc.h>
|
| 49#include <sys/sbuf.h>
|
49#include <sys/systm.h> 50#include <sys/sysproto.h> 51#include <sys/sysent.h> 52#include <sys/vnode.h> 53#include <sys/file.h> 54#include <sys/socket.h> 55#include <sys/socketvar.h> 56#include <sys/sysctl.h> 57 58#include <fs/devfs/devfs.h> 59 60#include <net/bpfdesc.h> 61#include <net/if.h> 62#include <net/if_types.h> 63#include <net/if_var.h> 64 65#include <vm/vm.h> 66 67#include <sys/mac_policy.h> 68 69#include <security/mac_partition/mac_partition.h> 70 71SYSCTL_DECL(_security_mac); 72 73SYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0, 74 "TrustedBSD mac_partition policy controls"); 75 76static int mac_partition_enabled = 1; 77SYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW, 78 &mac_partition_enabled, 0, "Enforce partition policy"); 79 80static int partition_slot; 81#define SLOT(l) (LABEL_TO_SLOT((l), partition_slot).l_long) 82 83static void 84mac_partition_init(struct mac_policy_conf *conf) 85{ 86 87} 88 89static void 90mac_partition_init_label(struct label *label) 91{ 92 93 SLOT(label) = 0; 94} 95 96static void 97mac_partition_destroy_label(struct label *label) 98{ 99 100 SLOT(label) = 0; 101} 102 103static int 104mac_partition_externalize_label(struct label *label, char *element_name,
| 50#include <sys/systm.h> 51#include <sys/sysproto.h> 52#include <sys/sysent.h> 53#include <sys/vnode.h> 54#include <sys/file.h> 55#include <sys/socket.h> 56#include <sys/socketvar.h> 57#include <sys/sysctl.h> 58 59#include <fs/devfs/devfs.h> 60 61#include <net/bpfdesc.h> 62#include <net/if.h> 63#include <net/if_types.h> 64#include <net/if_var.h> 65 66#include <vm/vm.h> 67 68#include <sys/mac_policy.h> 69 70#include <security/mac_partition/mac_partition.h> 71 72SYSCTL_DECL(_security_mac); 73 74SYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0, 75 "TrustedBSD mac_partition policy controls"); 76 77static int mac_partition_enabled = 1; 78SYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW, 79 &mac_partition_enabled, 0, "Enforce partition policy"); 80 81static int partition_slot; 82#define SLOT(l) (LABEL_TO_SLOT((l), partition_slot).l_long) 83 84static void 85mac_partition_init(struct mac_policy_conf *conf) 86{ 87 88} 89 90static void 91mac_partition_init_label(struct label *label) 92{ 93 94 SLOT(label) = 0; 95} 96 97static void 98mac_partition_destroy_label(struct label *label) 99{ 100 101 SLOT(label) = 0; 102} 103 104static int 105mac_partition_externalize_label(struct label *label, char *element_name,
|
105 char *element_data, size_t size, size_t *len, int *claimed)
| 106 struct sbuf *sb, int *claimed)
|
106{ 107 108 if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 109 return (0); 110 111 (*claimed)++;
| 107{ 108 109 if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 110 return (0); 111 112 (*claimed)++;
|
112 *len = snprintf(element_data, size, "%ld", SLOT(label)); 113 return (0);
| 113 114 if (sbuf_printf(sb, "%ld", SLOT(label)) == -1) 115 return (EINVAL); 116 else 117 return (0);
|
114} 115 116static int 117mac_partition_internalize_label(struct label *label, char *element_name, 118 char *element_data, int *claimed) 119{ 120 121 if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 122 return (0); 123 124 (*claimed)++; 125 SLOT(label) = strtol(element_data, NULL, 10); 126 return (0); 127} 128 129static void 130mac_partition_create_cred(struct ucred *cred_parent, struct ucred *cred_child) 131{ 132 133 SLOT(&cred_child->cr_label) = SLOT(&cred_parent->cr_label); 134} 135 136static void 137mac_partition_create_proc0(struct ucred *cred) 138{ 139 140 SLOT(&cred->cr_label) = 0; 141} 142 143static void 144mac_partition_create_proc1(struct ucred *cred) 145{ 146 147 SLOT(&cred->cr_label) = 0; 148} 149 150static void 151mac_partition_relabel_cred(struct ucred *cred, struct label *newlabel) 152{ 153 154 if (SLOT(newlabel) != 0) 155 SLOT(&cred->cr_label) = SLOT(newlabel); 156} 157 158static int 159label_on_label(struct label *subject, struct label *object) 160{ 161 162 if (mac_partition_enabled == 0) 163 return (0); 164 165 if (SLOT(subject) == 0) 166 return (0); 167 168 if (SLOT(subject) == SLOT(object)) 169 return (0); 170 171 return (EPERM); 172} 173 174static int 175mac_partition_check_cred_relabel(struct ucred *cred, struct label *newlabel) 176{ 177 int error; 178 179 error = 0; 180 181 /* Treat "0" as a no-op request. */ 182 if (SLOT(newlabel) != 0) { 183 /* 184 * Require BSD privilege in order to change the partition. 185 * Originally we also required that the process not be 186 * in a partition in the first place, but this didn't 187 * interact well with sendmail. 188 */ 189 error = suser_cred(cred, 0); 190 } 191 192 return (error); 193} 194 195static int 196mac_partition_check_cred_visible(struct ucred *u1, struct ucred *u2) 197{ 198 int error; 199 200 error = label_on_label(&u1->cr_label, &u2->cr_label); 201 202 return (error == 0 ? 0 : ESRCH); 203} 204 205static int 206mac_partition_check_proc_debug(struct ucred *cred, struct proc *proc) 207{ 208 int error; 209 210 error = label_on_label(&cred->cr_label, &proc->p_ucred->cr_label); 211 212 return (error ? ESRCH : 0); 213} 214 215static int 216mac_partition_check_proc_sched(struct ucred *cred, struct proc *proc) 217{ 218 int error; 219 220 error = label_on_label(&cred->cr_label, &proc->p_ucred->cr_label); 221 222 return (error ? ESRCH : 0); 223} 224 225static int 226mac_partition_check_proc_signal(struct ucred *cred, struct proc *proc, 227 int signum) 228{ 229 int error; 230 231 error = label_on_label(&cred->cr_label, &proc->p_ucred->cr_label); 232 233 return (error ? ESRCH : 0); 234} 235 236static int 237mac_partition_check_socket_visible(struct ucred *cred, struct socket *socket, 238 struct label *socketlabel) 239{ 240 int error; 241 242 error = label_on_label(&cred->cr_label, socketlabel); 243 244 return (error ? ENOENT : 0); 245} 246 247static int 248mac_partition_check_vnode_exec(struct ucred *cred, struct vnode *vp, 249 struct label *label, struct image_params *imgp, struct label *execlabel) 250{ 251 252 if (execlabel != NULL) { 253 /* 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. 257 */ 258 if (SLOT(execlabel) != 0) 259 return (EINVAL); 260 } 261 262 return (0); 263} 264 265static struct mac_policy_ops mac_partition_ops = 266{ 267 .mpo_init = mac_partition_init, 268 .mpo_init_cred_label = mac_partition_init_label, 269 .mpo_destroy_cred_label = mac_partition_destroy_label, 270 .mpo_externalize_cred_label = mac_partition_externalize_label, 271 .mpo_internalize_cred_label = mac_partition_internalize_label, 272 .mpo_create_cred = mac_partition_create_cred, 273 .mpo_create_proc0 = mac_partition_create_proc0, 274 .mpo_create_proc1 = mac_partition_create_proc1, 275 .mpo_relabel_cred = mac_partition_relabel_cred, 276 .mpo_check_cred_relabel = mac_partition_check_cred_relabel, 277 .mpo_check_cred_visible = mac_partition_check_cred_visible, 278 .mpo_check_proc_debug = mac_partition_check_proc_debug, 279 .mpo_check_proc_sched = mac_partition_check_proc_sched, 280 .mpo_check_proc_signal = mac_partition_check_proc_signal, 281 .mpo_check_socket_visible = mac_partition_check_socket_visible, 282 .mpo_check_vnode_exec = mac_partition_check_vnode_exec, 283}; 284 285MAC_POLICY_SET(&mac_partition_ops, mac_partition, "TrustedBSD MAC/Partition", 286 MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot);
| 118} 119 120static int 121mac_partition_internalize_label(struct label *label, char *element_name, 122 char *element_data, int *claimed) 123{ 124 125 if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 126 return (0); 127 128 (*claimed)++; 129 SLOT(label) = strtol(element_data, NULL, 10); 130 return (0); 131} 132 133static void 134mac_partition_create_cred(struct ucred *cred_parent, struct ucred *cred_child) 135{ 136 137 SLOT(&cred_child->cr_label) = SLOT(&cred_parent->cr_label); 138} 139 140static void 141mac_partition_create_proc0(struct ucred *cred) 142{ 143 144 SLOT(&cred->cr_label) = 0; 145} 146 147static void 148mac_partition_create_proc1(struct ucred *cred) 149{ 150 151 SLOT(&cred->cr_label) = 0; 152} 153 154static void 155mac_partition_relabel_cred(struct ucred *cred, struct label *newlabel) 156{ 157 158 if (SLOT(newlabel) != 0) 159 SLOT(&cred->cr_label) = SLOT(newlabel); 160} 161 162static int 163label_on_label(struct label *subject, struct label *object) 164{ 165 166 if (mac_partition_enabled == 0) 167 return (0); 168 169 if (SLOT(subject) == 0) 170 return (0); 171 172 if (SLOT(subject) == SLOT(object)) 173 return (0); 174 175 return (EPERM); 176} 177 178static int 179mac_partition_check_cred_relabel(struct ucred *cred, struct label *newlabel) 180{ 181 int error; 182 183 error = 0; 184 185 /* Treat "0" as a no-op request. */ 186 if (SLOT(newlabel) != 0) { 187 /* 188 * Require BSD privilege in order to change the partition. 189 * Originally we also required that the process not be 190 * in a partition in the first place, but this didn't 191 * interact well with sendmail. 192 */ 193 error = suser_cred(cred, 0); 194 } 195 196 return (error); 197} 198 199static int 200mac_partition_check_cred_visible(struct ucred *u1, struct ucred *u2) 201{ 202 int error; 203 204 error = label_on_label(&u1->cr_label, &u2->cr_label); 205 206 return (error == 0 ? 0 : ESRCH); 207} 208 209static int 210mac_partition_check_proc_debug(struct ucred *cred, struct proc *proc) 211{ 212 int error; 213 214 error = label_on_label(&cred->cr_label, &proc->p_ucred->cr_label); 215 216 return (error ? ESRCH : 0); 217} 218 219static int 220mac_partition_check_proc_sched(struct ucred *cred, struct proc *proc) 221{ 222 int error; 223 224 error = label_on_label(&cred->cr_label, &proc->p_ucred->cr_label); 225 226 return (error ? ESRCH : 0); 227} 228 229static int 230mac_partition_check_proc_signal(struct ucred *cred, struct proc *proc, 231 int signum) 232{ 233 int error; 234 235 error = label_on_label(&cred->cr_label, &proc->p_ucred->cr_label); 236 237 return (error ? ESRCH : 0); 238} 239 240static int 241mac_partition_check_socket_visible(struct ucred *cred, struct socket *socket, 242 struct label *socketlabel) 243{ 244 int error; 245 246 error = label_on_label(&cred->cr_label, socketlabel); 247 248 return (error ? ENOENT : 0); 249} 250 251static int 252mac_partition_check_vnode_exec(struct ucred *cred, struct vnode *vp, 253 struct label *label, struct image_params *imgp, struct label *execlabel) 254{ 255 256 if (execlabel != NULL) { 257 /* 258 * We currently don't permit labels to be changed at 259 * exec-time as part of the partition model, so disallow 260 * non-NULL partition label changes in execlabel. 261 */ 262 if (SLOT(execlabel) != 0) 263 return (EINVAL); 264 } 265 266 return (0); 267} 268 269static struct mac_policy_ops mac_partition_ops = 270{ 271 .mpo_init = mac_partition_init, 272 .mpo_init_cred_label = mac_partition_init_label, 273 .mpo_destroy_cred_label = mac_partition_destroy_label, 274 .mpo_externalize_cred_label = mac_partition_externalize_label, 275 .mpo_internalize_cred_label = mac_partition_internalize_label, 276 .mpo_create_cred = mac_partition_create_cred, 277 .mpo_create_proc0 = mac_partition_create_proc0, 278 .mpo_create_proc1 = mac_partition_create_proc1, 279 .mpo_relabel_cred = mac_partition_relabel_cred, 280 .mpo_check_cred_relabel = mac_partition_check_cred_relabel, 281 .mpo_check_cred_visible = mac_partition_check_cred_visible, 282 .mpo_check_proc_debug = mac_partition_check_proc_debug, 283 .mpo_check_proc_sched = mac_partition_check_proc_sched, 284 .mpo_check_proc_signal = mac_partition_check_proc_signal, 285 .mpo_check_socket_visible = mac_partition_check_socket_visible, 286 .mpo_check_vnode_exec = mac_partition_check_vnode_exec, 287}; 288 289MAC_POLICY_SET(&mac_partition_ops, mac_partition, "TrustedBSD MAC/Partition", 290 MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot);
|