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