1 /*- 2 * Copyright (c) 1999-2002, 2007 Robert N. M. Watson 3 * Copyright (c) 2001-2002 Networks Associates Technology, Inc. 4 * Copyright (c) 2006 SPARTA, Inc. 5 * All rights reserved. 6 * 7 * This software was developed by Robert Watson for the TrustedBSD Project. 8 * 9 * This software was developed for the FreeBSD Project in part by Network 10 * Associates Laboratories, the Security Research Division of Network 11 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 12 * as part of the DARPA CHATS research program. 13 * 14 * This software was enhanced by SPARTA ISSO under SPAWAR contract 15 * N66001-04-C-6019 ("SEFOS"). 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * $FreeBSD$ 39 */ 40 41 /* 42 * Developed by the TrustedBSD Project. 43 * 44 * Experiment with a partition-like model. 45 */ 46 47 #include <sys/param.h> 48 #include <sys/kernel.h> 49 #include <sys/module.h> 50 #include <sys/priv.h> 51 #include <sys/proc.h> 52 #include <sys/sbuf.h> 53 #include <sys/systm.h> 54 #include <sys/sysctl.h> 55 56 #include <security/mac/mac_policy.h> 57 #include <security/mac_partition/mac_partition.h> 58 59 SYSCTL_DECL(_security_mac); 60 61 SYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0, 62 "TrustedBSD mac_partition policy controls"); 63 64 static int mac_partition_enabled = 1; 65 SYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW, 66 &mac_partition_enabled, 0, "Enforce partition policy"); 67 68 static int partition_slot; 69 #define SLOT(l) mac_label_get((l), partition_slot) 70 #define SLOT_SET(l, v) mac_label_set((l), partition_slot, (v)) 71 72 static int 73 label_on_label(struct label *subject, struct label *object) 74 { 75 76 if (mac_partition_enabled == 0) 77 return (0); 78 79 if (SLOT(subject) == 0) 80 return (0); 81 82 if (SLOT(subject) == SLOT(object)) 83 return (0); 84 85 return (EPERM); 86 } 87 88 /* 89 * Object-specific entry points are sorted alphabetically by object type name 90 * and then by operation. 91 */ 92 static int 93 partition_cred_check_relabel(struct ucred *cred, struct label *newlabel) 94 { 95 int error; 96 97 error = 0; 98 99 /* Treat "0" as a no-op request. */ 100 if (SLOT(newlabel) != 0) { 101 /* 102 * Require BSD privilege in order to change the partition. 103 * Originally we also required that the process not be in a 104 * partition in the first place, but this didn't interact 105 * well with sendmail. 106 */ 107 error = priv_check_cred(cred, PRIV_MAC_PARTITION, 0); 108 } 109 110 return (error); 111 } 112 113 static int 114 partition_cred_check_visible(struct ucred *cr1, struct ucred *cr2) 115 { 116 int error; 117 118 error = label_on_label(cr1->cr_label, cr2->cr_label); 119 120 return (error == 0 ? 0 : ESRCH); 121 } 122 123 static void 124 partition_cred_copy_label(struct label *src, struct label *dest) 125 { 126 127 SLOT_SET(dest, SLOT(src)); 128 } 129 130 static void 131 partition_cred_destroy_label(struct label *label) 132 { 133 134 SLOT_SET(label, 0); 135 } 136 137 static int 138 partition_cred_externalize_label(struct label *label, char *element_name, 139 struct sbuf *sb, int *claimed) 140 { 141 142 if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 143 return (0); 144 145 (*claimed)++; 146 147 if (sbuf_printf(sb, "%jd", (intmax_t)SLOT(label)) == -1) 148 return (EINVAL); 149 else 150 return (0); 151 } 152 153 static void 154 partition_cred_init_label(struct label *label) 155 { 156 157 SLOT_SET(label, 0); 158 } 159 160 static int 161 partition_cred_internalize_label(struct label *label, char *element_name, 162 char *element_data, int *claimed) 163 { 164 165 if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 166 return (0); 167 168 (*claimed)++; 169 SLOT_SET(label, strtol(element_data, NULL, 10)); 170 return (0); 171 } 172 173 static void 174 partition_cred_relabel(struct ucred *cred, struct label *newlabel) 175 { 176 177 if (SLOT(newlabel) != 0) 178 SLOT_SET(cred->cr_label, SLOT(newlabel)); 179 } 180 181 static int 182 partition_proc_check_debug(struct ucred *cred, struct proc *p) 183 { 184 int error; 185 186 error = label_on_label(cred->cr_label, p->p_ucred->cr_label); 187 188 return (error ? ESRCH : 0); 189 } 190 191 static int 192 partition_proc_check_sched(struct ucred *cred, struct proc *p) 193 { 194 int error; 195 196 error = label_on_label(cred->cr_label, p->p_ucred->cr_label); 197 198 return (error ? ESRCH : 0); 199 } 200 201 static int 202 partition_proc_check_signal(struct ucred *cred, struct proc *p, 203 int signum) 204 { 205 int error; 206 207 error = label_on_label(cred->cr_label, p->p_ucred->cr_label); 208 209 return (error ? ESRCH : 0); 210 } 211 212 static void 213 partition_proc_create_init(struct ucred *cred) 214 { 215 216 SLOT_SET(cred->cr_label, 0); 217 } 218 219 static void 220 partition_proc_create_swapper(struct ucred *cred) 221 { 222 223 SLOT_SET(cred->cr_label, 0); 224 } 225 226 static int 227 partition_socket_check_visible(struct ucred *cred, struct socket *so, 228 struct label *solabel) 229 { 230 int error; 231 232 error = label_on_label(cred->cr_label, solabel); 233 234 return (error ? ENOENT : 0); 235 } 236 237 static int 238 partition_vnode_check_exec(struct ucred *cred, struct vnode *vp, 239 struct label *vplabel, struct image_params *imgp, 240 struct label *execlabel) 241 { 242 243 if (execlabel != NULL) { 244 /* 245 * We currently don't permit labels to be changed at 246 * exec-time as part of the partition model, so disallow 247 * non-NULL partition label changes in execlabel. 248 */ 249 if (SLOT(execlabel) != 0) 250 return (EINVAL); 251 } 252 253 return (0); 254 } 255 256 static struct mac_policy_ops partition_ops = 257 { 258 .mpo_cred_check_relabel = partition_cred_check_relabel, 259 .mpo_cred_check_visible = partition_cred_check_visible, 260 .mpo_cred_copy_label = partition_cred_copy_label, 261 .mpo_cred_destroy_label = partition_cred_destroy_label, 262 .mpo_cred_externalize_label = partition_cred_externalize_label, 263 .mpo_cred_init_label = partition_cred_init_label, 264 .mpo_cred_internalize_label = partition_cred_internalize_label, 265 .mpo_cred_relabel = partition_cred_relabel, 266 .mpo_proc_check_debug = partition_proc_check_debug, 267 .mpo_proc_check_sched = partition_proc_check_sched, 268 .mpo_proc_check_signal = partition_proc_check_signal, 269 .mpo_proc_create_init = partition_proc_create_init, 270 .mpo_proc_create_swapper = partition_proc_create_swapper, 271 .mpo_socket_check_visible = partition_socket_check_visible, 272 .mpo_vnode_check_exec = partition_vnode_check_exec, 273 }; 274 275 MAC_POLICY_SET(&partition_ops, mac_partition, "TrustedBSD MAC/Partition", 276 MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot); 277