mac_partition.c revision 184402
1105828Srwatson/*- 2166533Srwatson * Copyright (c) 1999-2002, 2007 Robert N. M. Watson 3126097Srwatson * Copyright (c) 2001-2002 Networks Associates Technology, Inc. 4172930Srwatson * Copyright (c) 2006 SPARTA, Inc. 5182063Srwatson * Copyright (c) 2008 Apple Inc. 6105828Srwatson * All rights reserved. 7105828Srwatson * 8105828Srwatson * This software was developed by Robert Watson for the TrustedBSD Project. 9105828Srwatson * 10106393Srwatson * This software was developed for the FreeBSD Project in part by Network 11106393Srwatson * Associates Laboratories, the Security Research Division of Network 12106393Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 13106393Srwatson * as part of the DARPA CHATS research program. 14105828Srwatson * 15172930Srwatson * This software was enhanced by SPARTA ISSO under SPAWAR contract 16172930Srwatson * N66001-04-C-6019 ("SEFOS"). 17172930Srwatson * 18105828Srwatson * Redistribution and use in source and binary forms, with or without 19105828Srwatson * modification, are permitted provided that the following conditions 20105828Srwatson * are met: 21105828Srwatson * 1. Redistributions of source code must retain the above copyright 22105828Srwatson * notice, this list of conditions and the following disclaimer. 23105828Srwatson * 2. Redistributions in binary form must reproduce the above copyright 24105828Srwatson * notice, this list of conditions and the following disclaimer in the 25105828Srwatson * documentation and/or other materials provided with the distribution. 26105828Srwatson * 27105828Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 28105828Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29105828Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30105828Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 31105828Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32105828Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33105828Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34105828Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35105828Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36105828Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37105828Srwatson * SUCH DAMAGE. 38105828Srwatson * 39105828Srwatson * $FreeBSD: head/sys/security/mac_partition/mac_partition.c 184402 2008-10-28 09:16:34Z rwatson $ 40105828Srwatson */ 41105828Srwatson 42105828Srwatson/* 43105828Srwatson * Developed by the TrustedBSD Project. 44172955Srwatson * 45105828Srwatson * Experiment with a partition-like model. 46105828Srwatson */ 47105828Srwatson 48105828Srwatson#include <sys/param.h> 49105828Srwatson#include <sys/kernel.h> 50166905Srwatson#include <sys/module.h> 51164033Srwatson#include <sys/priv.h> 52105828Srwatson#include <sys/proc.h> 53116701Srwatson#include <sys/sbuf.h> 54183980Sbz#include <sys/socket.h> 55183970Sbz#include <sys/socketvar.h> 56105828Srwatson#include <sys/systm.h> 57105828Srwatson#include <sys/sysctl.h> 58105828Srwatson 59183980Sbz#include <net/route.h> 60183980Sbz#include <netinet/in.h> 61183980Sbz#include <netinet/in_pcb.h> 62183980Sbz 63165469Srwatson#include <security/mac/mac_policy.h> 64105828Srwatson#include <security/mac_partition/mac_partition.h> 65105828Srwatson 66105828SrwatsonSYSCTL_DECL(_security_mac); 67105828Srwatson 68105828SrwatsonSYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0, 69105828Srwatson "TrustedBSD mac_partition policy controls"); 70105828Srwatson 71181213Srwatsonstatic int partition_enabled = 1; 72105828SrwatsonSYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW, 73181213Srwatson &partition_enabled, 0, "Enforce partition policy"); 74105828Srwatson 75105828Srwatsonstatic int partition_slot; 76166533Srwatson#define SLOT(l) mac_label_get((l), partition_slot) 77166533Srwatson#define SLOT_SET(l, v) mac_label_set((l), partition_slot, (v)) 78105828Srwatson 79173138Srwatsonstatic int 80184401Srwatsonpartition_check(struct label *subject, struct label *object) 81105828Srwatson{ 82105828Srwatson 83181213Srwatson if (partition_enabled == 0) 84173138Srwatson return (0); 85105828Srwatson 86182063Srwatson if (subject == NULL) 87182063Srwatson return (0); 88182063Srwatson 89173138Srwatson if (SLOT(subject) == 0) 90173138Srwatson return (0); 91105828Srwatson 92182063Srwatson /* 93182063Srwatson * If the object label hasn't been allocated, then it's effectively 94182063Srwatson * not in a partition, and we know the subject is as it has a label 95182063Srwatson * and it's not 0, so reject. 96182063Srwatson */ 97182063Srwatson if (object == NULL) 98182063Srwatson return (EPERM); 99182063Srwatson 100173138Srwatson if (SLOT(subject) == SLOT(object)) 101173138Srwatson return (0); 102105828Srwatson 103173138Srwatson return (EPERM); 104123173Srwatson} 105123173Srwatson 106173138Srwatson/* 107173138Srwatson * Object-specific entry points are sorted alphabetically by object type name 108173138Srwatson * and then by operation. 109173138Srwatson */ 110105828Srwatsonstatic int 111173138Srwatsonpartition_cred_check_relabel(struct ucred *cred, struct label *newlabel) 112105828Srwatson{ 113173138Srwatson int error; 114105828Srwatson 115173138Srwatson error = 0; 116105828Srwatson 117184402Srwatson /* 118184402Srwatson * Treat "0" as a no-op request because it reflects an unset 119184402Srwatson * partition label. If we ever want to support switching back to an 120184402Srwatson * unpartitioned state for a process, we'll need to differentiate the 121184402Srwatson * "not in a partition" and "no partition defined during internalize" 122184402Srwatson * conditions. 123184402Srwatson */ 124173138Srwatson if (SLOT(newlabel) != 0) { 125173138Srwatson /* 126173138Srwatson * Require BSD privilege in order to change the partition. 127173138Srwatson * Originally we also required that the process not be in a 128173138Srwatson * partition in the first place, but this didn't interact 129173138Srwatson * well with sendmail. 130173138Srwatson */ 131173138Srwatson error = priv_check_cred(cred, PRIV_MAC_PARTITION, 0); 132173138Srwatson } 133116701Srwatson 134173138Srwatson return (error); 135105828Srwatson} 136105828Srwatson 137105828Srwatsonstatic int 138173138Srwatsonpartition_cred_check_visible(struct ucred *cr1, struct ucred *cr2) 139105828Srwatson{ 140173138Srwatson int error; 141105828Srwatson 142184401Srwatson error = partition_check(cr1->cr_label, cr2->cr_label); 143105828Srwatson 144173138Srwatson return (error == 0 ? 0 : ESRCH); 145105828Srwatson} 146105828Srwatson 147105828Srwatsonstatic void 148173138Srwatsonpartition_cred_copy_label(struct label *src, struct label *dest) 149105828Srwatson{ 150105828Srwatson 151182063Srwatson if (src != NULL && dest != NULL) 152182063Srwatson SLOT_SET(dest, SLOT(src)); 153182063Srwatson else if (dest != NULL) 154182063Srwatson SLOT_SET(dest, 0); 155105828Srwatson} 156105828Srwatson 157105828Srwatsonstatic void 158173138Srwatsonpartition_cred_destroy_label(struct label *label) 159105828Srwatson{ 160105828Srwatson 161173138Srwatson SLOT_SET(label, 0); 162105828Srwatson} 163105828Srwatson 164105828Srwatsonstatic int 165173138Srwatsonpartition_cred_externalize_label(struct label *label, char *element_name, 166173138Srwatson struct sbuf *sb, int *claimed) 167105828Srwatson{ 168105828Srwatson 169173138Srwatson if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 170105828Srwatson return (0); 171105828Srwatson 172173138Srwatson (*claimed)++; 173105828Srwatson 174182063Srwatson if (label != NULL) { 175182063Srwatson if (sbuf_printf(sb, "%jd", (intmax_t)SLOT(label)) == -1) 176182063Srwatson return (EINVAL); 177182063Srwatson } else { 178182063Srwatson if (sbuf_printf(sb, "0") == -1) 179182063Srwatson return (EINVAL); 180182063Srwatson } 181182063Srwatson return (0); 182173138Srwatson} 183105828Srwatson 184173138Srwatsonstatic void 185173138Srwatsonpartition_cred_init_label(struct label *label) 186173138Srwatson{ 187173138Srwatson 188173138Srwatson SLOT_SET(label, 0); 189105828Srwatson} 190105828Srwatson 191105828Srwatsonstatic int 192173138Srwatsonpartition_cred_internalize_label(struct label *label, char *element_name, 193173138Srwatson char *element_data, int *claimed) 194105828Srwatson{ 195105828Srwatson 196173138Srwatson if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 197173138Srwatson return (0); 198105828Srwatson 199173138Srwatson (*claimed)++; 200173138Srwatson SLOT_SET(label, strtol(element_data, NULL, 10)); 201173138Srwatson return (0); 202105828Srwatson} 203105828Srwatson 204173138Srwatsonstatic void 205173138Srwatsonpartition_cred_relabel(struct ucred *cred, struct label *newlabel) 206105828Srwatson{ 207105828Srwatson 208182063Srwatson if (newlabel != NULL && SLOT(newlabel) != 0) 209173138Srwatson SLOT_SET(cred->cr_label, SLOT(newlabel)); 210105828Srwatson} 211105828Srwatson 212105828Srwatsonstatic int 213183980Sbzpartition_inpcb_check_visible(struct ucred *cred, struct inpcb *inp, 214183980Sbz struct label *inplabel) 215183980Sbz{ 216183980Sbz int error; 217183980Sbz 218184401Srwatson error = partition_check(cred->cr_label, inp->inp_cred->cr_label); 219183980Sbz 220183980Sbz return (error ? ENOENT : 0); 221183980Sbz} 222183980Sbz 223183980Sbzstatic int 224172955Srwatsonpartition_proc_check_debug(struct ucred *cred, struct proc *p) 225105828Srwatson{ 226105828Srwatson int error; 227105828Srwatson 228184401Srwatson error = partition_check(cred->cr_label, p->p_ucred->cr_label); 229105828Srwatson 230105828Srwatson return (error ? ESRCH : 0); 231105828Srwatson} 232105828Srwatson 233105828Srwatsonstatic int 234172955Srwatsonpartition_proc_check_sched(struct ucred *cred, struct proc *p) 235105828Srwatson{ 236105828Srwatson int error; 237105828Srwatson 238184401Srwatson error = partition_check(cred->cr_label, p->p_ucred->cr_label); 239105828Srwatson 240105828Srwatson return (error ? ESRCH : 0); 241105828Srwatson} 242105828Srwatson 243105828Srwatsonstatic int 244172955Srwatsonpartition_proc_check_signal(struct ucred *cred, struct proc *p, 245105828Srwatson int signum) 246105828Srwatson{ 247105828Srwatson int error; 248105828Srwatson 249184401Srwatson error = partition_check(cred->cr_label, p->p_ucred->cr_label); 250105828Srwatson 251105828Srwatson return (error ? ESRCH : 0); 252105828Srwatson} 253105828Srwatson 254173138Srwatsonstatic void 255173138Srwatsonpartition_proc_create_init(struct ucred *cred) 256173138Srwatson{ 257173138Srwatson 258173138Srwatson SLOT_SET(cred->cr_label, 0); 259173138Srwatson} 260173138Srwatson 261173138Srwatsonstatic void 262173138Srwatsonpartition_proc_create_swapper(struct ucred *cred) 263173138Srwatson{ 264173138Srwatson 265173138Srwatson SLOT_SET(cred->cr_label, 0); 266173138Srwatson} 267173138Srwatson 268105828Srwatsonstatic int 269172955Srwatsonpartition_socket_check_visible(struct ucred *cred, struct socket *so, 270168976Srwatson struct label *solabel) 271105828Srwatson{ 272105828Srwatson int error; 273105828Srwatson 274184401Srwatson error = partition_check(cred->cr_label, so->so_cred->cr_label); 275105828Srwatson 276105828Srwatson return (error ? ENOENT : 0); 277105828Srwatson} 278105828Srwatson 279106648Srwatsonstatic int 280172955Srwatsonpartition_vnode_check_exec(struct ucred *cred, struct vnode *vp, 281168976Srwatson struct label *vplabel, struct image_params *imgp, 282168976Srwatson struct label *execlabel) 283106648Srwatson{ 284106648Srwatson 285106648Srwatson if (execlabel != NULL) { 286106648Srwatson /* 287106648Srwatson * We currently don't permit labels to be changed at 288106648Srwatson * exec-time as part of the partition model, so disallow 289106648Srwatson * non-NULL partition label changes in execlabel. 290106648Srwatson */ 291106648Srwatson if (SLOT(execlabel) != 0) 292106648Srwatson return (EINVAL); 293106648Srwatson } 294106648Srwatson 295106648Srwatson return (0); 296106648Srwatson} 297106648Srwatson 298172955Srwatsonstatic struct mac_policy_ops partition_ops = 299105828Srwatson{ 300172955Srwatson .mpo_cred_check_relabel = partition_cred_check_relabel, 301172955Srwatson .mpo_cred_check_visible = partition_cred_check_visible, 302173138Srwatson .mpo_cred_copy_label = partition_cred_copy_label, 303173138Srwatson .mpo_cred_destroy_label = partition_cred_destroy_label, 304173138Srwatson .mpo_cred_externalize_label = partition_cred_externalize_label, 305173138Srwatson .mpo_cred_init_label = partition_cred_init_label, 306173138Srwatson .mpo_cred_internalize_label = partition_cred_internalize_label, 307173138Srwatson .mpo_cred_relabel = partition_cred_relabel, 308183980Sbz .mpo_inpcb_check_visible = partition_inpcb_check_visible, 309172955Srwatson .mpo_proc_check_debug = partition_proc_check_debug, 310172955Srwatson .mpo_proc_check_sched = partition_proc_check_sched, 311172955Srwatson .mpo_proc_check_signal = partition_proc_check_signal, 312173138Srwatson .mpo_proc_create_init = partition_proc_create_init, 313173138Srwatson .mpo_proc_create_swapper = partition_proc_create_swapper, 314172955Srwatson .mpo_socket_check_visible = partition_socket_check_visible, 315172955Srwatson .mpo_vnode_check_exec = partition_vnode_check_exec, 316105828Srwatson}; 317105828Srwatson 318172955SrwatsonMAC_POLICY_SET(&partition_ops, mac_partition, "TrustedBSD MAC/Partition", 319182063Srwatson MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot, MPC_OBJECT_CRED); 320