1 /*- 2 * Copyright (c) 1999-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$ 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> 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/sx.h> 58 #include <sys/sysctl.h> 59 60 #include <fs/devfs/devfs.h> 61 62 #include <net/bpfdesc.h> 63 #include <net/if.h> 64 #include <net/if_types.h> 65 #include <net/if_var.h> 66 67 #include <vm/vm.h> 68 69 #include <sys/mac_policy.h> 70 71 #include <security/mac_partition/mac_partition.h> 72 73 SYSCTL_DECL(_security_mac); 74 75 SYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0, 76 "TrustedBSD mac_partition policy controls"); 77 78 static int mac_partition_enabled = 1; 79 SYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW, 80 &mac_partition_enabled, 0, "Enforce partition policy"); 81 82 static int partition_slot; 83 #define SLOT(l) (LABEL_TO_SLOT((l), partition_slot).l_long) 84 85 static void 86 mac_partition_init(struct mac_policy_conf *conf) 87 { 88 89 } 90 91 static void 92 mac_partition_init_label(struct label *label) 93 { 94 95 SLOT(label) = 0; 96 } 97 98 static void 99 mac_partition_destroy_label(struct label *label) 100 { 101 102 SLOT(label) = 0; 103 } 104 105 static void 106 mac_partition_copy_label(struct label *src, struct label *dest) 107 { 108 109 SLOT(dest) = SLOT(src); 110 } 111 112 static int 113 mac_partition_externalize_label(struct label *label, char *element_name, 114 struct sbuf *sb, int *claimed) 115 { 116 117 if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 118 return (0); 119 120 (*claimed)++; 121 122 if (sbuf_printf(sb, "%ld", SLOT(label)) == -1) 123 return (EINVAL); 124 else 125 return (0); 126 } 127 128 static int 129 mac_partition_internalize_label(struct label *label, char *element_name, 130 char *element_data, int *claimed) 131 { 132 133 if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 134 return (0); 135 136 (*claimed)++; 137 SLOT(label) = strtol(element_data, NULL, 10); 138 return (0); 139 } 140 141 static void 142 mac_partition_create_proc0(struct ucred *cred) 143 { 144 145 SLOT(cred->cr_label) = 0; 146 } 147 148 static void 149 mac_partition_create_proc1(struct ucred *cred) 150 { 151 152 SLOT(cred->cr_label) = 0; 153 } 154 155 static void 156 mac_partition_relabel_cred(struct ucred *cred, struct label *newlabel) 157 { 158 159 if (SLOT(newlabel) != 0) 160 SLOT(cred->cr_label) = SLOT(newlabel); 161 } 162 163 static int 164 label_on_label(struct label *subject, struct label *object) 165 { 166 167 if (mac_partition_enabled == 0) 168 return (0); 169 170 if (SLOT(subject) == 0) 171 return (0); 172 173 if (SLOT(subject) == SLOT(object)) 174 return (0); 175 176 return (EPERM); 177 } 178 179 static int 180 mac_partition_check_cred_relabel(struct ucred *cred, struct label *newlabel) 181 { 182 int error; 183 184 error = 0; 185 186 /* Treat "0" as a no-op request. */ 187 if (SLOT(newlabel) != 0) { 188 /* 189 * Require BSD privilege in order to change the partition. 190 * Originally we also required that the process not be 191 * in a partition in the first place, but this didn't 192 * interact well with sendmail. 193 */ 194 error = suser_cred(cred, 0); 195 } 196 197 return (error); 198 } 199 200 static int 201 mac_partition_check_cred_visible(struct ucred *u1, struct ucred *u2) 202 { 203 int error; 204 205 error = label_on_label(u1->cr_label, u2->cr_label); 206 207 return (error == 0 ? 0 : ESRCH); 208 } 209 210 static int 211 mac_partition_check_proc_debug(struct ucred *cred, struct proc *proc) 212 { 213 int error; 214 215 error = label_on_label(cred->cr_label, proc->p_ucred->cr_label); 216 217 return (error ? ESRCH : 0); 218 } 219 220 static int 221 mac_partition_check_proc_sched(struct ucred *cred, struct proc *proc) 222 { 223 int error; 224 225 error = label_on_label(cred->cr_label, proc->p_ucred->cr_label); 226 227 return (error ? ESRCH : 0); 228 } 229 230 static int 231 mac_partition_check_proc_signal(struct ucred *cred, struct proc *proc, 232 int signum) 233 { 234 int error; 235 236 error = label_on_label(cred->cr_label, proc->p_ucred->cr_label); 237 238 return (error ? ESRCH : 0); 239 } 240 241 static int 242 mac_partition_check_socket_visible(struct ucred *cred, struct socket *socket, 243 struct label *socketlabel) 244 { 245 int error; 246 247 error = label_on_label(cred->cr_label, socketlabel); 248 249 return (error ? ENOENT : 0); 250 } 251 252 static int 253 mac_partition_check_vnode_exec(struct ucred *cred, struct vnode *vp, 254 struct label *label, struct image_params *imgp, struct label *execlabel) 255 { 256 257 if (execlabel != NULL) { 258 /* 259 * We currently don't permit labels to be changed at 260 * exec-time as part of the partition model, so disallow 261 * non-NULL partition label changes in execlabel. 262 */ 263 if (SLOT(execlabel) != 0) 264 return (EINVAL); 265 } 266 267 return (0); 268 } 269 270 static struct mac_policy_ops mac_partition_ops = 271 { 272 .mpo_init = mac_partition_init, 273 .mpo_init_cred_label = mac_partition_init_label, 274 .mpo_destroy_cred_label = mac_partition_destroy_label, 275 .mpo_copy_cred_label = mac_partition_copy_label, 276 .mpo_externalize_cred_label = mac_partition_externalize_label, 277 .mpo_internalize_cred_label = mac_partition_internalize_label, 278 .mpo_create_proc0 = mac_partition_create_proc0, 279 .mpo_create_proc1 = mac_partition_create_proc1, 280 .mpo_relabel_cred = mac_partition_relabel_cred, 281 .mpo_check_cred_relabel = mac_partition_check_cred_relabel, 282 .mpo_check_cred_visible = mac_partition_check_cred_visible, 283 .mpo_check_proc_debug = mac_partition_check_proc_debug, 284 .mpo_check_proc_sched = mac_partition_check_proc_sched, 285 .mpo_check_proc_signal = mac_partition_check_proc_signal, 286 .mpo_check_socket_visible = mac_partition_check_socket_visible, 287 .mpo_check_vnode_exec = mac_partition_check_vnode_exec, 288 }; 289 290 MAC_POLICY_SET(&mac_partition_ops, mac_partition, "TrustedBSD MAC/Partition", 291 MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot); 292