mac_partition.c revision 183970
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 183970 2008-10-17 08:58:33Z bz $ 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> 54183970Sbz#include <sys/socketvar.h> 55105828Srwatson#include <sys/systm.h> 56105828Srwatson#include <sys/sysctl.h> 57105828Srwatson 58165469Srwatson#include <security/mac/mac_policy.h> 59105828Srwatson#include <security/mac_partition/mac_partition.h> 60105828Srwatson 61105828SrwatsonSYSCTL_DECL(_security_mac); 62105828Srwatson 63105828SrwatsonSYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0, 64105828Srwatson "TrustedBSD mac_partition policy controls"); 65105828Srwatson 66181213Srwatsonstatic int partition_enabled = 1; 67105828SrwatsonSYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW, 68181213Srwatson &partition_enabled, 0, "Enforce partition policy"); 69105828Srwatson 70105828Srwatsonstatic int partition_slot; 71166533Srwatson#define SLOT(l) mac_label_get((l), partition_slot) 72166533Srwatson#define SLOT_SET(l, v) mac_label_set((l), partition_slot, (v)) 73105828Srwatson 74173138Srwatsonstatic int 75173138Srwatsonlabel_on_label(struct label *subject, struct label *object) 76105828Srwatson{ 77105828Srwatson 78181213Srwatson if (partition_enabled == 0) 79173138Srwatson return (0); 80105828Srwatson 81182063Srwatson if (subject == NULL) 82182063Srwatson return (0); 83182063Srwatson 84173138Srwatson if (SLOT(subject) == 0) 85173138Srwatson return (0); 86105828Srwatson 87182063Srwatson /* 88182063Srwatson * If the object label hasn't been allocated, then it's effectively 89182063Srwatson * not in a partition, and we know the subject is as it has a label 90182063Srwatson * and it's not 0, so reject. 91182063Srwatson */ 92182063Srwatson if (object == NULL) 93182063Srwatson return (EPERM); 94182063Srwatson 95173138Srwatson if (SLOT(subject) == SLOT(object)) 96173138Srwatson return (0); 97105828Srwatson 98173138Srwatson return (EPERM); 99123173Srwatson} 100123173Srwatson 101173138Srwatson/* 102173138Srwatson * Object-specific entry points are sorted alphabetically by object type name 103173138Srwatson * and then by operation. 104173138Srwatson */ 105105828Srwatsonstatic int 106173138Srwatsonpartition_cred_check_relabel(struct ucred *cred, struct label *newlabel) 107105828Srwatson{ 108173138Srwatson int error; 109105828Srwatson 110173138Srwatson error = 0; 111105828Srwatson 112173138Srwatson /* Treat "0" as a no-op request. */ 113173138Srwatson if (SLOT(newlabel) != 0) { 114173138Srwatson /* 115173138Srwatson * Require BSD privilege in order to change the partition. 116173138Srwatson * Originally we also required that the process not be in a 117173138Srwatson * partition in the first place, but this didn't interact 118173138Srwatson * well with sendmail. 119173138Srwatson */ 120173138Srwatson error = priv_check_cred(cred, PRIV_MAC_PARTITION, 0); 121173138Srwatson } 122116701Srwatson 123173138Srwatson return (error); 124105828Srwatson} 125105828Srwatson 126105828Srwatsonstatic int 127173138Srwatsonpartition_cred_check_visible(struct ucred *cr1, struct ucred *cr2) 128105828Srwatson{ 129173138Srwatson int error; 130105828Srwatson 131173138Srwatson error = label_on_label(cr1->cr_label, cr2->cr_label); 132105828Srwatson 133173138Srwatson return (error == 0 ? 0 : ESRCH); 134105828Srwatson} 135105828Srwatson 136105828Srwatsonstatic void 137173138Srwatsonpartition_cred_copy_label(struct label *src, struct label *dest) 138105828Srwatson{ 139105828Srwatson 140182063Srwatson if (src != NULL && dest != NULL) 141182063Srwatson SLOT_SET(dest, SLOT(src)); 142182063Srwatson else if (dest != NULL) 143182063Srwatson SLOT_SET(dest, 0); 144105828Srwatson} 145105828Srwatson 146105828Srwatsonstatic void 147173138Srwatsonpartition_cred_destroy_label(struct label *label) 148105828Srwatson{ 149105828Srwatson 150173138Srwatson SLOT_SET(label, 0); 151105828Srwatson} 152105828Srwatson 153105828Srwatsonstatic int 154173138Srwatsonpartition_cred_externalize_label(struct label *label, char *element_name, 155173138Srwatson struct sbuf *sb, int *claimed) 156105828Srwatson{ 157105828Srwatson 158173138Srwatson if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 159105828Srwatson return (0); 160105828Srwatson 161173138Srwatson (*claimed)++; 162105828Srwatson 163182063Srwatson if (label != NULL) { 164182063Srwatson if (sbuf_printf(sb, "%jd", (intmax_t)SLOT(label)) == -1) 165182063Srwatson return (EINVAL); 166182063Srwatson } else { 167182063Srwatson if (sbuf_printf(sb, "0") == -1) 168182063Srwatson return (EINVAL); 169182063Srwatson } 170182063Srwatson return (0); 171173138Srwatson} 172105828Srwatson 173173138Srwatsonstatic void 174173138Srwatsonpartition_cred_init_label(struct label *label) 175173138Srwatson{ 176173138Srwatson 177173138Srwatson SLOT_SET(label, 0); 178105828Srwatson} 179105828Srwatson 180105828Srwatsonstatic int 181173138Srwatsonpartition_cred_internalize_label(struct label *label, char *element_name, 182173138Srwatson char *element_data, int *claimed) 183105828Srwatson{ 184105828Srwatson 185173138Srwatson if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 186173138Srwatson return (0); 187105828Srwatson 188173138Srwatson (*claimed)++; 189173138Srwatson SLOT_SET(label, strtol(element_data, NULL, 10)); 190173138Srwatson return (0); 191105828Srwatson} 192105828Srwatson 193173138Srwatsonstatic void 194173138Srwatsonpartition_cred_relabel(struct ucred *cred, struct label *newlabel) 195105828Srwatson{ 196105828Srwatson 197182063Srwatson if (newlabel != NULL && SLOT(newlabel) != 0) 198173138Srwatson SLOT_SET(cred->cr_label, SLOT(newlabel)); 199105828Srwatson} 200105828Srwatson 201105828Srwatsonstatic int 202172955Srwatsonpartition_proc_check_debug(struct ucred *cred, struct proc *p) 203105828Srwatson{ 204105828Srwatson int error; 205105828Srwatson 206168976Srwatson error = label_on_label(cred->cr_label, p->p_ucred->cr_label); 207105828Srwatson 208105828Srwatson return (error ? ESRCH : 0); 209105828Srwatson} 210105828Srwatson 211105828Srwatsonstatic int 212172955Srwatsonpartition_proc_check_sched(struct ucred *cred, struct proc *p) 213105828Srwatson{ 214105828Srwatson int error; 215105828Srwatson 216168976Srwatson error = label_on_label(cred->cr_label, p->p_ucred->cr_label); 217105828Srwatson 218105828Srwatson return (error ? ESRCH : 0); 219105828Srwatson} 220105828Srwatson 221105828Srwatsonstatic int 222172955Srwatsonpartition_proc_check_signal(struct ucred *cred, struct proc *p, 223105828Srwatson int signum) 224105828Srwatson{ 225105828Srwatson int error; 226105828Srwatson 227168976Srwatson error = label_on_label(cred->cr_label, p->p_ucred->cr_label); 228105828Srwatson 229105828Srwatson return (error ? ESRCH : 0); 230105828Srwatson} 231105828Srwatson 232173138Srwatsonstatic void 233173138Srwatsonpartition_proc_create_init(struct ucred *cred) 234173138Srwatson{ 235173138Srwatson 236173138Srwatson SLOT_SET(cred->cr_label, 0); 237173138Srwatson} 238173138Srwatson 239173138Srwatsonstatic void 240173138Srwatsonpartition_proc_create_swapper(struct ucred *cred) 241173138Srwatson{ 242173138Srwatson 243173138Srwatson SLOT_SET(cred->cr_label, 0); 244173138Srwatson} 245173138Srwatson 246105828Srwatsonstatic int 247172955Srwatsonpartition_socket_check_visible(struct ucred *cred, struct socket *so, 248168976Srwatson struct label *solabel) 249105828Srwatson{ 250105828Srwatson int error; 251105828Srwatson 252183970Sbz error = label_on_label(cred->cr_label, so->so_cred->cr_label); 253105828Srwatson 254105828Srwatson return (error ? ENOENT : 0); 255105828Srwatson} 256105828Srwatson 257106648Srwatsonstatic int 258172955Srwatsonpartition_vnode_check_exec(struct ucred *cred, struct vnode *vp, 259168976Srwatson struct label *vplabel, struct image_params *imgp, 260168976Srwatson struct label *execlabel) 261106648Srwatson{ 262106648Srwatson 263106648Srwatson if (execlabel != NULL) { 264106648Srwatson /* 265106648Srwatson * We currently don't permit labels to be changed at 266106648Srwatson * exec-time as part of the partition model, so disallow 267106648Srwatson * non-NULL partition label changes in execlabel. 268106648Srwatson */ 269106648Srwatson if (SLOT(execlabel) != 0) 270106648Srwatson return (EINVAL); 271106648Srwatson } 272106648Srwatson 273106648Srwatson return (0); 274106648Srwatson} 275106648Srwatson 276172955Srwatsonstatic struct mac_policy_ops partition_ops = 277105828Srwatson{ 278172955Srwatson .mpo_cred_check_relabel = partition_cred_check_relabel, 279172955Srwatson .mpo_cred_check_visible = partition_cred_check_visible, 280173138Srwatson .mpo_cred_copy_label = partition_cred_copy_label, 281173138Srwatson .mpo_cred_destroy_label = partition_cred_destroy_label, 282173138Srwatson .mpo_cred_externalize_label = partition_cred_externalize_label, 283173138Srwatson .mpo_cred_init_label = partition_cred_init_label, 284173138Srwatson .mpo_cred_internalize_label = partition_cred_internalize_label, 285173138Srwatson .mpo_cred_relabel = partition_cred_relabel, 286172955Srwatson .mpo_proc_check_debug = partition_proc_check_debug, 287172955Srwatson .mpo_proc_check_sched = partition_proc_check_sched, 288172955Srwatson .mpo_proc_check_signal = partition_proc_check_signal, 289173138Srwatson .mpo_proc_create_init = partition_proc_create_init, 290173138Srwatson .mpo_proc_create_swapper = partition_proc_create_swapper, 291172955Srwatson .mpo_socket_check_visible = partition_socket_check_visible, 292172955Srwatson .mpo_vnode_check_exec = partition_vnode_check_exec, 293105828Srwatson}; 294105828Srwatson 295172955SrwatsonMAC_POLICY_SET(&partition_ops, mac_partition, "TrustedBSD MAC/Partition", 296182063Srwatson MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot, MPC_OBJECT_CRED); 297