mac_partition.c revision 184401
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 184401 2008-10-28 09:12:13Z 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 117173138Srwatson /* Treat "0" as a no-op request. */ 118173138Srwatson if (SLOT(newlabel) != 0) { 119173138Srwatson /* 120173138Srwatson * Require BSD privilege in order to change the partition. 121173138Srwatson * Originally we also required that the process not be in a 122173138Srwatson * partition in the first place, but this didn't interact 123173138Srwatson * well with sendmail. 124173138Srwatson */ 125173138Srwatson error = priv_check_cred(cred, PRIV_MAC_PARTITION, 0); 126173138Srwatson } 127116701Srwatson 128173138Srwatson return (error); 129105828Srwatson} 130105828Srwatson 131105828Srwatsonstatic int 132173138Srwatsonpartition_cred_check_visible(struct ucred *cr1, struct ucred *cr2) 133105828Srwatson{ 134173138Srwatson int error; 135105828Srwatson 136184401Srwatson error = partition_check(cr1->cr_label, cr2->cr_label); 137105828Srwatson 138173138Srwatson return (error == 0 ? 0 : ESRCH); 139105828Srwatson} 140105828Srwatson 141105828Srwatsonstatic void 142173138Srwatsonpartition_cred_copy_label(struct label *src, struct label *dest) 143105828Srwatson{ 144105828Srwatson 145182063Srwatson if (src != NULL && dest != NULL) 146182063Srwatson SLOT_SET(dest, SLOT(src)); 147182063Srwatson else if (dest != NULL) 148182063Srwatson SLOT_SET(dest, 0); 149105828Srwatson} 150105828Srwatson 151105828Srwatsonstatic void 152173138Srwatsonpartition_cred_destroy_label(struct label *label) 153105828Srwatson{ 154105828Srwatson 155173138Srwatson SLOT_SET(label, 0); 156105828Srwatson} 157105828Srwatson 158105828Srwatsonstatic int 159173138Srwatsonpartition_cred_externalize_label(struct label *label, char *element_name, 160173138Srwatson struct sbuf *sb, int *claimed) 161105828Srwatson{ 162105828Srwatson 163173138Srwatson if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 164105828Srwatson return (0); 165105828Srwatson 166173138Srwatson (*claimed)++; 167105828Srwatson 168182063Srwatson if (label != NULL) { 169182063Srwatson if (sbuf_printf(sb, "%jd", (intmax_t)SLOT(label)) == -1) 170182063Srwatson return (EINVAL); 171182063Srwatson } else { 172182063Srwatson if (sbuf_printf(sb, "0") == -1) 173182063Srwatson return (EINVAL); 174182063Srwatson } 175182063Srwatson return (0); 176173138Srwatson} 177105828Srwatson 178173138Srwatsonstatic void 179173138Srwatsonpartition_cred_init_label(struct label *label) 180173138Srwatson{ 181173138Srwatson 182173138Srwatson SLOT_SET(label, 0); 183105828Srwatson} 184105828Srwatson 185105828Srwatsonstatic int 186173138Srwatsonpartition_cred_internalize_label(struct label *label, char *element_name, 187173138Srwatson char *element_data, int *claimed) 188105828Srwatson{ 189105828Srwatson 190173138Srwatson if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 191173138Srwatson return (0); 192105828Srwatson 193173138Srwatson (*claimed)++; 194173138Srwatson SLOT_SET(label, strtol(element_data, NULL, 10)); 195173138Srwatson return (0); 196105828Srwatson} 197105828Srwatson 198173138Srwatsonstatic void 199173138Srwatsonpartition_cred_relabel(struct ucred *cred, struct label *newlabel) 200105828Srwatson{ 201105828Srwatson 202182063Srwatson if (newlabel != NULL && SLOT(newlabel) != 0) 203173138Srwatson SLOT_SET(cred->cr_label, SLOT(newlabel)); 204105828Srwatson} 205105828Srwatson 206105828Srwatsonstatic int 207183980Sbzpartition_inpcb_check_visible(struct ucred *cred, struct inpcb *inp, 208183980Sbz struct label *inplabel) 209183980Sbz{ 210183980Sbz int error; 211183980Sbz 212184401Srwatson error = partition_check(cred->cr_label, inp->inp_cred->cr_label); 213183980Sbz 214183980Sbz return (error ? ENOENT : 0); 215183980Sbz} 216183980Sbz 217183980Sbzstatic int 218172955Srwatsonpartition_proc_check_debug(struct ucred *cred, struct proc *p) 219105828Srwatson{ 220105828Srwatson int error; 221105828Srwatson 222184401Srwatson error = partition_check(cred->cr_label, p->p_ucred->cr_label); 223105828Srwatson 224105828Srwatson return (error ? ESRCH : 0); 225105828Srwatson} 226105828Srwatson 227105828Srwatsonstatic int 228172955Srwatsonpartition_proc_check_sched(struct ucred *cred, struct proc *p) 229105828Srwatson{ 230105828Srwatson int error; 231105828Srwatson 232184401Srwatson error = partition_check(cred->cr_label, p->p_ucred->cr_label); 233105828Srwatson 234105828Srwatson return (error ? ESRCH : 0); 235105828Srwatson} 236105828Srwatson 237105828Srwatsonstatic int 238172955Srwatsonpartition_proc_check_signal(struct ucred *cred, struct proc *p, 239105828Srwatson int signum) 240105828Srwatson{ 241105828Srwatson int error; 242105828Srwatson 243184401Srwatson error = partition_check(cred->cr_label, p->p_ucred->cr_label); 244105828Srwatson 245105828Srwatson return (error ? ESRCH : 0); 246105828Srwatson} 247105828Srwatson 248173138Srwatsonstatic void 249173138Srwatsonpartition_proc_create_init(struct ucred *cred) 250173138Srwatson{ 251173138Srwatson 252173138Srwatson SLOT_SET(cred->cr_label, 0); 253173138Srwatson} 254173138Srwatson 255173138Srwatsonstatic void 256173138Srwatsonpartition_proc_create_swapper(struct ucred *cred) 257173138Srwatson{ 258173138Srwatson 259173138Srwatson SLOT_SET(cred->cr_label, 0); 260173138Srwatson} 261173138Srwatson 262105828Srwatsonstatic int 263172955Srwatsonpartition_socket_check_visible(struct ucred *cred, struct socket *so, 264168976Srwatson struct label *solabel) 265105828Srwatson{ 266105828Srwatson int error; 267105828Srwatson 268184401Srwatson error = partition_check(cred->cr_label, so->so_cred->cr_label); 269105828Srwatson 270105828Srwatson return (error ? ENOENT : 0); 271105828Srwatson} 272105828Srwatson 273106648Srwatsonstatic int 274172955Srwatsonpartition_vnode_check_exec(struct ucred *cred, struct vnode *vp, 275168976Srwatson struct label *vplabel, struct image_params *imgp, 276168976Srwatson struct label *execlabel) 277106648Srwatson{ 278106648Srwatson 279106648Srwatson if (execlabel != NULL) { 280106648Srwatson /* 281106648Srwatson * We currently don't permit labels to be changed at 282106648Srwatson * exec-time as part of the partition model, so disallow 283106648Srwatson * non-NULL partition label changes in execlabel. 284106648Srwatson */ 285106648Srwatson if (SLOT(execlabel) != 0) 286106648Srwatson return (EINVAL); 287106648Srwatson } 288106648Srwatson 289106648Srwatson return (0); 290106648Srwatson} 291106648Srwatson 292172955Srwatsonstatic struct mac_policy_ops partition_ops = 293105828Srwatson{ 294172955Srwatson .mpo_cred_check_relabel = partition_cred_check_relabel, 295172955Srwatson .mpo_cred_check_visible = partition_cred_check_visible, 296173138Srwatson .mpo_cred_copy_label = partition_cred_copy_label, 297173138Srwatson .mpo_cred_destroy_label = partition_cred_destroy_label, 298173138Srwatson .mpo_cred_externalize_label = partition_cred_externalize_label, 299173138Srwatson .mpo_cred_init_label = partition_cred_init_label, 300173138Srwatson .mpo_cred_internalize_label = partition_cred_internalize_label, 301173138Srwatson .mpo_cred_relabel = partition_cred_relabel, 302183980Sbz .mpo_inpcb_check_visible = partition_inpcb_check_visible, 303172955Srwatson .mpo_proc_check_debug = partition_proc_check_debug, 304172955Srwatson .mpo_proc_check_sched = partition_proc_check_sched, 305172955Srwatson .mpo_proc_check_signal = partition_proc_check_signal, 306173138Srwatson .mpo_proc_create_init = partition_proc_create_init, 307173138Srwatson .mpo_proc_create_swapper = partition_proc_create_swapper, 308172955Srwatson .mpo_socket_check_visible = partition_socket_check_visible, 309172955Srwatson .mpo_vnode_check_exec = partition_vnode_check_exec, 310105828Srwatson}; 311105828Srwatson 312172955SrwatsonMAC_POLICY_SET(&partition_ops, mac_partition, "TrustedBSD MAC/Partition", 313182063Srwatson MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot, MPC_OBJECT_CRED); 314