mac_partition.c revision 164033
1105828Srwatson/*- 2126097Srwatson * Copyright (c) 1999-2002 Robert N. M. Watson 3126097Srwatson * Copyright (c) 2001-2002 Networks Associates Technology, Inc. 4105828Srwatson * All rights reserved. 5105828Srwatson * 6105828Srwatson * This software was developed by Robert Watson for the TrustedBSD Project. 7105828Srwatson * 8106393Srwatson * This software was developed for the FreeBSD Project in part by Network 9106393Srwatson * Associates Laboratories, the Security Research Division of Network 10106393Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 11106393Srwatson * as part of the DARPA CHATS research program. 12105828Srwatson * 13105828Srwatson * Redistribution and use in source and binary forms, with or without 14105828Srwatson * modification, are permitted provided that the following conditions 15105828Srwatson * are met: 16105828Srwatson * 1. Redistributions of source code must retain the above copyright 17105828Srwatson * notice, this list of conditions and the following disclaimer. 18105828Srwatson * 2. Redistributions in binary form must reproduce the above copyright 19105828Srwatson * notice, this list of conditions and the following disclaimer in the 20105828Srwatson * documentation and/or other materials provided with the distribution. 21105828Srwatson * 22105828Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23105828Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24105828Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25105828Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26105828Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27105828Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28105828Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29105828Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30105828Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31105828Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32105828Srwatson * SUCH DAMAGE. 33105828Srwatson * 34105828Srwatson * $FreeBSD: head/sys/security/mac_partition/mac_partition.c 164033 2006-11-06 13:42:10Z rwatson $ 35105828Srwatson */ 36105828Srwatson 37105828Srwatson/* 38105828Srwatson * Developed by the TrustedBSD Project. 39105828Srwatson * Experiment with a partition-like model. 40105828Srwatson */ 41105828Srwatson 42105828Srwatson#include <sys/types.h> 43105828Srwatson#include <sys/param.h> 44105828Srwatson#include <sys/conf.h> 45105828Srwatson#include <sys/kernel.h> 46105828Srwatson#include <sys/mac.h> 47105828Srwatson#include <sys/mount.h> 48164033Srwatson#include <sys/priv.h> 49105828Srwatson#include <sys/proc.h> 50116701Srwatson#include <sys/sbuf.h> 51105828Srwatson#include <sys/systm.h> 52105828Srwatson#include <sys/sysproto.h> 53105828Srwatson#include <sys/sysent.h> 54105828Srwatson#include <sys/vnode.h> 55105828Srwatson#include <sys/file.h> 56105828Srwatson#include <sys/socket.h> 57105828Srwatson#include <sys/socketvar.h> 58150340Sphk#include <sys/sx.h> 59105828Srwatson#include <sys/sysctl.h> 60105828Srwatson 61105828Srwatson#include <fs/devfs/devfs.h> 62105828Srwatson 63105828Srwatson#include <net/bpfdesc.h> 64105828Srwatson#include <net/if.h> 65105828Srwatson#include <net/if_types.h> 66105828Srwatson#include <net/if_var.h> 67105828Srwatson 68105828Srwatson#include <vm/vm.h> 69105828Srwatson 70105828Srwatson#include <sys/mac_policy.h> 71105828Srwatson 72105828Srwatson#include <security/mac_partition/mac_partition.h> 73105828Srwatson 74105828SrwatsonSYSCTL_DECL(_security_mac); 75105828Srwatson 76105828SrwatsonSYSCTL_NODE(_security_mac, OID_AUTO, partition, CTLFLAG_RW, 0, 77105828Srwatson "TrustedBSD mac_partition policy controls"); 78105828Srwatson 79105828Srwatsonstatic int mac_partition_enabled = 1; 80105828SrwatsonSYSCTL_INT(_security_mac_partition, OID_AUTO, enabled, CTLFLAG_RW, 81105828Srwatson &mac_partition_enabled, 0, "Enforce partition policy"); 82105828Srwatson 83105828Srwatsonstatic int partition_slot; 84105828Srwatson#define SLOT(l) (LABEL_TO_SLOT((l), partition_slot).l_long) 85105828Srwatson 86105828Srwatsonstatic void 87105828Srwatsonmac_partition_init(struct mac_policy_conf *conf) 88105828Srwatson{ 89105828Srwatson 90105828Srwatson} 91105828Srwatson 92105828Srwatsonstatic void 93105828Srwatsonmac_partition_init_label(struct label *label) 94105828Srwatson{ 95105828Srwatson 96105828Srwatson SLOT(label) = 0; 97105828Srwatson} 98105828Srwatson 99105828Srwatsonstatic void 100105828Srwatsonmac_partition_destroy_label(struct label *label) 101105828Srwatson{ 102105828Srwatson 103105828Srwatson SLOT(label) = 0; 104105828Srwatson} 105105828Srwatson 106123173Srwatsonstatic void 107123173Srwatsonmac_partition_copy_label(struct label *src, struct label *dest) 108123173Srwatson{ 109123173Srwatson 110123173Srwatson SLOT(dest) = SLOT(src); 111123173Srwatson} 112123173Srwatson 113105828Srwatsonstatic int 114105828Srwatsonmac_partition_externalize_label(struct label *label, char *element_name, 115116701Srwatson struct sbuf *sb, int *claimed) 116105828Srwatson{ 117105828Srwatson 118105828Srwatson if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 119105828Srwatson return (0); 120105828Srwatson 121105828Srwatson (*claimed)++; 122116701Srwatson 123116701Srwatson if (sbuf_printf(sb, "%ld", SLOT(label)) == -1) 124116701Srwatson return (EINVAL); 125116701Srwatson else 126116701Srwatson return (0); 127105828Srwatson} 128105828Srwatson 129105828Srwatsonstatic int 130105828Srwatsonmac_partition_internalize_label(struct label *label, char *element_name, 131105828Srwatson char *element_data, int *claimed) 132105828Srwatson{ 133105828Srwatson 134105828Srwatson if (strcmp(MAC_PARTITION_LABEL_NAME, element_name) != 0) 135105828Srwatson return (0); 136105828Srwatson 137105828Srwatson (*claimed)++; 138105828Srwatson SLOT(label) = strtol(element_data, NULL, 10); 139105828Srwatson return (0); 140105828Srwatson} 141105828Srwatson 142105828Srwatsonstatic void 143105828Srwatsonmac_partition_create_proc0(struct ucred *cred) 144105828Srwatson{ 145105828Srwatson 146122524Srwatson SLOT(cred->cr_label) = 0; 147105828Srwatson} 148105828Srwatson 149105828Srwatsonstatic void 150105828Srwatsonmac_partition_create_proc1(struct ucred *cred) 151105828Srwatson{ 152105828Srwatson 153122524Srwatson SLOT(cred->cr_label) = 0; 154105828Srwatson} 155105828Srwatson 156105828Srwatsonstatic void 157105828Srwatsonmac_partition_relabel_cred(struct ucred *cred, struct label *newlabel) 158105828Srwatson{ 159105828Srwatson 160105828Srwatson if (SLOT(newlabel) != 0) 161122524Srwatson SLOT(cred->cr_label) = SLOT(newlabel); 162105828Srwatson} 163105828Srwatson 164105828Srwatsonstatic int 165105828Srwatsonlabel_on_label(struct label *subject, struct label *object) 166105828Srwatson{ 167105828Srwatson 168105828Srwatson if (mac_partition_enabled == 0) 169105828Srwatson return (0); 170105828Srwatson 171105828Srwatson if (SLOT(subject) == 0) 172105828Srwatson return (0); 173105828Srwatson 174105828Srwatson if (SLOT(subject) == SLOT(object)) 175105828Srwatson return (0); 176105828Srwatson 177105828Srwatson return (EPERM); 178105828Srwatson} 179105828Srwatson 180105828Srwatsonstatic int 181105828Srwatsonmac_partition_check_cred_relabel(struct ucred *cred, struct label *newlabel) 182105828Srwatson{ 183105828Srwatson int error; 184105828Srwatson 185105828Srwatson error = 0; 186105828Srwatson 187105828Srwatson /* Treat "0" as a no-op request. */ 188105828Srwatson if (SLOT(newlabel) != 0) { 189105828Srwatson /* 190106367Srwatson * Require BSD privilege in order to change the partition. 191106367Srwatson * Originally we also required that the process not be 192106367Srwatson * in a partition in the first place, but this didn't 193106367Srwatson * interact well with sendmail. 194105828Srwatson */ 195164033Srwatson error = priv_check_cred(cred, PRIV_MAC_PARTITION, 0); 196105828Srwatson } 197105828Srwatson 198105828Srwatson return (error); 199105828Srwatson} 200105828Srwatson 201105828Srwatsonstatic int 202105828Srwatsonmac_partition_check_cred_visible(struct ucred *u1, struct ucred *u2) 203105828Srwatson{ 204105828Srwatson int error; 205105828Srwatson 206122524Srwatson error = label_on_label(u1->cr_label, u2->cr_label); 207105828Srwatson 208105828Srwatson return (error == 0 ? 0 : ESRCH); 209105828Srwatson} 210105828Srwatson 211105828Srwatsonstatic int 212105828Srwatsonmac_partition_check_proc_debug(struct ucred *cred, struct proc *proc) 213105828Srwatson{ 214105828Srwatson int error; 215105828Srwatson 216122524Srwatson error = label_on_label(cred->cr_label, proc->p_ucred->cr_label); 217105828Srwatson 218105828Srwatson return (error ? ESRCH : 0); 219105828Srwatson} 220105828Srwatson 221105828Srwatsonstatic int 222105828Srwatsonmac_partition_check_proc_sched(struct ucred *cred, struct proc *proc) 223105828Srwatson{ 224105828Srwatson int error; 225105828Srwatson 226122524Srwatson error = label_on_label(cred->cr_label, proc->p_ucred->cr_label); 227105828Srwatson 228105828Srwatson return (error ? ESRCH : 0); 229105828Srwatson} 230105828Srwatson 231105828Srwatsonstatic int 232105828Srwatsonmac_partition_check_proc_signal(struct ucred *cred, struct proc *proc, 233105828Srwatson int signum) 234105828Srwatson{ 235105828Srwatson int error; 236105828Srwatson 237122524Srwatson error = label_on_label(cred->cr_label, proc->p_ucred->cr_label); 238105828Srwatson 239105828Srwatson return (error ? ESRCH : 0); 240105828Srwatson} 241105828Srwatson 242105828Srwatsonstatic int 243105828Srwatsonmac_partition_check_socket_visible(struct ucred *cred, struct socket *socket, 244105828Srwatson struct label *socketlabel) 245105828Srwatson{ 246105828Srwatson int error; 247105828Srwatson 248122524Srwatson error = label_on_label(cred->cr_label, socketlabel); 249105828Srwatson 250105828Srwatson return (error ? ENOENT : 0); 251105828Srwatson} 252105828Srwatson 253106648Srwatsonstatic int 254106648Srwatsonmac_partition_check_vnode_exec(struct ucred *cred, struct vnode *vp, 255106648Srwatson struct label *label, struct image_params *imgp, struct label *execlabel) 256106648Srwatson{ 257106648Srwatson 258106648Srwatson if (execlabel != NULL) { 259106648Srwatson /* 260106648Srwatson * We currently don't permit labels to be changed at 261106648Srwatson * exec-time as part of the partition model, so disallow 262106648Srwatson * non-NULL partition label changes in execlabel. 263106648Srwatson */ 264106648Srwatson if (SLOT(execlabel) != 0) 265106648Srwatson return (EINVAL); 266106648Srwatson } 267106648Srwatson 268106648Srwatson return (0); 269106648Srwatson} 270106648Srwatson 271106217Srwatsonstatic struct mac_policy_ops mac_partition_ops = 272105828Srwatson{ 273106217Srwatson .mpo_init = mac_partition_init, 274106217Srwatson .mpo_init_cred_label = mac_partition_init_label, 275106217Srwatson .mpo_destroy_cred_label = mac_partition_destroy_label, 276123173Srwatson .mpo_copy_cred_label = mac_partition_copy_label, 277106217Srwatson .mpo_externalize_cred_label = mac_partition_externalize_label, 278106217Srwatson .mpo_internalize_cred_label = mac_partition_internalize_label, 279106217Srwatson .mpo_create_proc0 = mac_partition_create_proc0, 280106217Srwatson .mpo_create_proc1 = mac_partition_create_proc1, 281106217Srwatson .mpo_relabel_cred = mac_partition_relabel_cred, 282106217Srwatson .mpo_check_cred_relabel = mac_partition_check_cred_relabel, 283106217Srwatson .mpo_check_cred_visible = mac_partition_check_cred_visible, 284106217Srwatson .mpo_check_proc_debug = mac_partition_check_proc_debug, 285106217Srwatson .mpo_check_proc_sched = mac_partition_check_proc_sched, 286106217Srwatson .mpo_check_proc_signal = mac_partition_check_proc_signal, 287106217Srwatson .mpo_check_socket_visible = mac_partition_check_socket_visible, 288106648Srwatson .mpo_check_vnode_exec = mac_partition_check_vnode_exec, 289105828Srwatson}; 290105828Srwatson 291112717SrwatsonMAC_POLICY_SET(&mac_partition_ops, mac_partition, "TrustedBSD MAC/Partition", 292112717Srwatson MPC_LOADTIME_FLAG_UNLOADOK, &partition_slot); 293