1100894Srwatson/*- 2189503Srwatson * Copyright (c) 1999-2002, 2006, 2009 Robert N. M. Watson 3100894Srwatson * Copyright (c) 2001 Ilmar S. Habibulin 4126262Srwatson * Copyright (c) 2001-2004 Networks Associates Technology, Inc. 5164032Srwatson * Copyright (c) 2006 nCircle Network Security, Inc. 6172930Srwatson * Copyright (c) 2006 SPARTA, Inc. 7187016Srwatson * Copyright (c) 2009 Apple, Inc. 8100894Srwatson * All rights reserved. 9100894Srwatson * 10100894Srwatson * This software was developed by Robert Watson and Ilmar Habibulin for the 11100894Srwatson * TrustedBSD Project. 12100894Srwatson * 13106392Srwatson * This software was developed for the FreeBSD Project in part by Network 14106392Srwatson * Associates Laboratories, the Security Research Division of Network 15106392Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 16106392Srwatson * as part of the DARPA CHATS research program. 17100894Srwatson * 18164032Srwatson * This software was developed by Robert N. M. Watson for the TrustedBSD 19164032Srwatson * Project under contract to nCircle Network Security, Inc. 20164032Srwatson * 21172930Srwatson * This software was enhanced by SPARTA ISSO under SPAWAR contract 22172930Srwatson * N66001-04-C-6019 ("SEFOS"). 23172930Srwatson * 24189503Srwatson * This software was developed at the University of Cambridge Computer 25189503Srwatson * Laboratory with support from a grant from Google, Inc. 26189503Srwatson * 27100894Srwatson * Redistribution and use in source and binary forms, with or without 28100894Srwatson * modification, are permitted provided that the following conditions 29100894Srwatson * are met: 30100894Srwatson * 1. Redistributions of source code must retain the above copyright 31100894Srwatson * notice, this list of conditions and the following disclaimer. 32100894Srwatson * 2. Redistributions in binary form must reproduce the above copyright 33100894Srwatson * notice, this list of conditions and the following disclaimer in the 34100894Srwatson * documentation and/or other materials provided with the distribution. 35100894Srwatson * 36100894Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 37100894Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38100894Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 39100894Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 40100894Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 41100894Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 42100894Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43100894Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 44100894Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 45100894Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46100894Srwatson * SUCH DAMAGE. 47121352Srwatson * 48121352Srwatson * $FreeBSD$ 49100894Srwatson */ 50116182Sobrien 51178184Srwatson#ifndef _SECURITY_MAC_MAC_INTERNAL_H_ 52178184Srwatson#define _SECURITY_MAC_MAC_INTERNAL_H_ 53165607Srwatson 54166531Srwatson#ifndef _KERNEL 55166531Srwatson#error "no user-serviceable parts inside" 56166531Srwatson#endif 57166531Srwatson 58192881Srwatson#include <sys/lock.h> 59192881Srwatson#include <sys/rmlock.h> 60192881Srwatson 61100894Srwatson/* 62121352Srwatson * MAC Framework sysctl namespace. 63100894Srwatson */ 64122705Sbde#ifdef SYSCTL_DECL 65121352SrwatsonSYSCTL_DECL(_security_mac); 66122705Sbde#endif /* SYSCTL_DECL */ 67100894Srwatson 68101712Srwatson/* 69189503Srwatson * MAC Framework SDT DTrace probe namespace, macros for declaring entry 70189503Srwatson * point probes, macros for invoking them. 71189503Srwatson */ 72189503Srwatson#ifdef SDT_PROVIDER_DECLARE 73189503SrwatsonSDT_PROVIDER_DECLARE(mac); /* MAC Framework-level events. */ 74189503SrwatsonSDT_PROVIDER_DECLARE(mac_framework); /* Entry points to MAC. */ 75189503Srwatson 76189503Srwatson#define MAC_CHECK_PROBE_DEFINE4(name, arg0, arg1, arg2, arg3) \ 77302237Sbdrewery SDT_PROBE_DEFINE5(mac_framework, , name, mac__check__err, \ 78260817Savg "int", arg0, arg1, arg2, arg3); \ 79302237Sbdrewery SDT_PROBE_DEFINE5(mac_framework, , name, mac__check__ok, \ 80260817Savg "int", arg0, arg1, arg2, arg3); 81189503Srwatson 82189503Srwatson#define MAC_CHECK_PROBE_DEFINE3(name, arg0, arg1, arg2) \ 83302237Sbdrewery SDT_PROBE_DEFINE4(mac_framework, , name, mac__check__err, \ 84260817Savg "int", arg0, arg1, arg2); \ 85302237Sbdrewery SDT_PROBE_DEFINE4(mac_framework, , name, mac__check__ok, \ 86260817Savg "int", arg0, arg1, arg2); 87189503Srwatson 88189503Srwatson#define MAC_CHECK_PROBE_DEFINE2(name, arg0, arg1) \ 89302237Sbdrewery SDT_PROBE_DEFINE3(mac_framework, , name, mac__check__err, \ 90260817Savg "int", arg0, arg1); \ 91302237Sbdrewery SDT_PROBE_DEFINE3(mac_framework, , name, mac__check__ok, \ 92260817Savg "int", arg0, arg1); 93189503Srwatson 94189503Srwatson#define MAC_CHECK_PROBE_DEFINE1(name, arg0) \ 95302237Sbdrewery SDT_PROBE_DEFINE2(mac_framework, , name, mac__check__err, \ 96260817Savg "int", arg0); \ 97302237Sbdrewery SDT_PROBE_DEFINE2(mac_framework, , name, mac__check__ok, \ 98260817Savg "int", arg0); 99189503Srwatson 100189503Srwatson#define MAC_CHECK_PROBE4(name, error, arg0, arg1, arg2, arg3) do { \ 101189503Srwatson if (error) { \ 102302237Sbdrewery SDT_PROBE5(mac_framework, , name, mac__check__err, \ 103189503Srwatson error, arg0, arg1, arg2, arg3); \ 104189503Srwatson } else { \ 105302237Sbdrewery SDT_PROBE5(mac_framework, , name, mac__check__ok, \ 106189503Srwatson 0, arg0, arg1, arg2, arg3); \ 107189503Srwatson } \ 108189503Srwatson} while (0) 109189503Srwatson 110189503Srwatson#define MAC_CHECK_PROBE3(name, error, arg0, arg1, arg2) \ 111189503Srwatson MAC_CHECK_PROBE4(name, error, arg0, arg1, arg2, 0) 112189503Srwatson#define MAC_CHECK_PROBE2(name, error, arg0, arg1) \ 113189503Srwatson MAC_CHECK_PROBE3(name, error, arg0, arg1, 0) 114189503Srwatson#define MAC_CHECK_PROBE1(name, error, arg0) \ 115189503Srwatson MAC_CHECK_PROBE2(name, error, arg0, 0) 116189503Srwatson#endif 117189503Srwatson 118189503Srwatson#define MAC_GRANT_PROBE_DEFINE2(name, arg0, arg1) \ 119302237Sbdrewery SDT_PROBE_DEFINE3(mac_framework, , name, mac__grant__err, \ 120260817Savg "int", arg0, arg1); \ 121302237Sbdrewery SDT_PROBE_DEFINE3(mac_framework, , name, mac__grant__ok, \ 122260817Savg "int", arg0, arg1); 123189503Srwatson 124189503Srwatson#define MAC_GRANT_PROBE2(name, error, arg0, arg1) do { \ 125189503Srwatson if (error) { \ 126302237Sbdrewery SDT_PROBE3(mac_framework, , name, mac__grant__err, \ 127302237Sbdrewery error, arg0, arg1); \ 128189503Srwatson } else { \ 129302237Sbdrewery SDT_PROBE3(mac_framework, , name, mac__grant__ok, \ 130302237Sbdrewery error, arg0, arg1); \ 131189503Srwatson } \ 132189503Srwatson} while (0) 133189503Srwatson 134189503Srwatson/* 135121352Srwatson * MAC Framework global types and typedefs. 136101712Srwatson */ 137121352SrwatsonLIST_HEAD(mac_policy_list_head, mac_policy_conf); 138122705Sbde#ifdef MALLOC_DECLARE 139121490SrwatsonMALLOC_DECLARE(M_MACTEMP); 140122705Sbde#endif 141101712Srwatson 142105959Srwatson/* 143166533Srwatson * MAC labels -- in-kernel storage format. 144166533Srwatson * 145166533Srwatson * In general, struct label pointers are embedded in kernel data structures 146166533Srwatson * representing objects that may be labeled (and protected). Struct label is 147166533Srwatson * opaque to both kernel services that invoke the MAC Framework and MAC 148166533Srwatson * policy modules. In particular, we do not wish to encode the layout of the 149166533Srwatson * label structure into any ABIs. Historically, the slot array contained 150166533Srwatson * unions of {long, void} but now contains uintptr_t. 151166533Srwatson */ 152166533Srwatson#define MAC_MAX_SLOTS 4 153166533Srwatson#define MAC_FLAG_INITIALIZED 0x0000001 /* Is initialized for use. */ 154166533Srwatsonstruct label { 155166533Srwatson int l_flags; 156166533Srwatson intptr_t l_perpolicy[MAC_MAX_SLOTS]; 157166533Srwatson}; 158166533Srwatson 159187016Srwatson 160166533Srwatson/* 161187016Srwatson * Flags for mac_labeled, a bitmask of object types need across the union of 162187016Srwatson * all policies currently registered with the MAC Framework, used to key 163187016Srwatson * whether or not labels are allocated and constructors for the type are 164187016Srwatson * invoked. 165187016Srwatson */ 166187016Srwatson#define MPC_OBJECT_CRED 0x0000000000000001 167187016Srwatson#define MPC_OBJECT_PROC 0x0000000000000002 168187016Srwatson#define MPC_OBJECT_VNODE 0x0000000000000004 169187016Srwatson#define MPC_OBJECT_INPCB 0x0000000000000008 170187016Srwatson#define MPC_OBJECT_SOCKET 0x0000000000000010 171187016Srwatson#define MPC_OBJECT_DEVFS 0x0000000000000020 172187016Srwatson#define MPC_OBJECT_MBUF 0x0000000000000040 173187016Srwatson#define MPC_OBJECT_IPQ 0x0000000000000080 174187016Srwatson#define MPC_OBJECT_IFNET 0x0000000000000100 175187016Srwatson#define MPC_OBJECT_BPFDESC 0x0000000000000200 176187016Srwatson#define MPC_OBJECT_PIPE 0x0000000000000400 177187016Srwatson#define MPC_OBJECT_MOUNT 0x0000000000000800 178187016Srwatson#define MPC_OBJECT_POSIXSEM 0x0000000000001000 179187016Srwatson#define MPC_OBJECT_POSIXSHM 0x0000000000002000 180187016Srwatson#define MPC_OBJECT_SYSVMSG 0x0000000000004000 181187016Srwatson#define MPC_OBJECT_SYSVMSQ 0x0000000000008000 182187016Srwatson#define MPC_OBJECT_SYSVSEM 0x0000000000010000 183187016Srwatson#define MPC_OBJECT_SYSVSHM 0x0000000000020000 184187016Srwatson#define MPC_OBJECT_SYNCACHE 0x0000000000040000 185187016Srwatson#define MPC_OBJECT_IP6Q 0x0000000000080000 186187016Srwatson 187187016Srwatson/* 188121352Srwatson * MAC Framework global variables. 189105959Srwatson */ 190121352Srwatsonextern struct mac_policy_list_head mac_policy_list; 191121352Srwatsonextern struct mac_policy_list_head mac_static_policy_list; 192193332Srwatsonextern u_int mac_policy_count; 193182063Srwatsonextern uint64_t mac_labeled; 194173095Srwatsonextern struct mtx mac_ifnet_mtx; 195100979Srwatson 196105988Srwatson/* 197121352Srwatson * MAC Framework infrastructure functions. 198100979Srwatson */ 199121352Srwatsonint mac_error_select(int error1, int error2); 200100979Srwatson 201192881Srwatsonvoid mac_policy_slock_nosleep(struct rm_priotracker *tracker); 202189797Srwatsonvoid mac_policy_slock_sleep(void); 203192881Srwatsonvoid mac_policy_sunlock_nosleep(struct rm_priotracker *tracker); 204189797Srwatsonvoid mac_policy_sunlock_sleep(void); 205106856Srwatson 206122524Srwatsonstruct label *mac_labelzone_alloc(int flags); 207122524Srwatsonvoid mac_labelzone_free(struct label *label); 208122524Srwatsonvoid mac_labelzone_init(void); 209122524Srwatson 210121352Srwatsonvoid mac_init_label(struct label *label); 211121352Srwatsonvoid mac_destroy_label(struct label *label); 212121352Srwatsonint mac_check_structmac_consistent(struct mac *mac); 213121352Srwatsonint mac_allocate_slot(void); 214113487Srwatson 215173095Srwatson#define MAC_IFNET_LOCK(ifp) mtx_lock(&mac_ifnet_mtx) 216173095Srwatson#define MAC_IFNET_UNLOCK(ifp) mtx_unlock(&mac_ifnet_mtx) 217173095Srwatson 218122202Srwatson/* 219165421Srwatson * MAC Framework per-object type functions. It's not yet clear how the 220165421Srwatson * namespaces, etc, should work for these, so for now, sort by object type. 221121352Srwatson */ 222182063Srwatsonstruct label *mac_cred_label_alloc(void); 223182063Srwatsonvoid mac_cred_label_free(struct label *label); 224122524Srwatsonstruct label *mac_pipe_label_alloc(void); 225122524Srwatsonvoid mac_pipe_label_free(struct label *label); 226122820Srwatsonstruct label *mac_socket_label_alloc(int flag); 227122820Srwatsonvoid mac_socket_label_free(struct label *label); 228182063Srwatsonstruct label *mac_vnode_label_alloc(void); 229182063Srwatsonvoid mac_vnode_label_free(struct label *label); 230122524Srwatson 231172930Srwatsonint mac_cred_check_relabel(struct ucred *cred, struct label *newlabel); 232172930Srwatsonint mac_cred_externalize_label(struct label *label, char *elements, 233122159Srwatson char *outbuf, size_t outbuflen); 234172930Srwatsonint mac_cred_internalize_label(struct label *label, char *string); 235172930Srwatsonvoid mac_cred_relabel(struct ucred *cred, struct label *newlabel); 236100979Srwatson 237126262Srwatsonstruct label *mac_mbuf_to_label(struct mbuf *m); 238126262Srwatson 239172930Srwatsonvoid mac_pipe_copy_label(struct label *src, struct label *dest); 240172930Srwatsonint mac_pipe_externalize_label(struct label *label, char *elements, 241122159Srwatson char *outbuf, size_t outbuflen); 242172930Srwatsonint mac_pipe_internalize_label(struct label *label, char *string); 243114806Srwatson 244122809Srwatsonint mac_socket_label_set(struct ucred *cred, struct socket *so, 245122809Srwatson struct label *label); 246172930Srwatsonvoid mac_socket_copy_label(struct label *src, struct label *dest); 247172930Srwatsonint mac_socket_externalize_label(struct label *label, char *elements, 248122820Srwatson char *outbuf, size_t outbuflen); 249172930Srwatsonint mac_socket_internalize_label(struct label *label, char *string); 250122809Srwatson 251172930Srwatsonint mac_vnode_externalize_label(struct label *label, char *elements, 252122159Srwatson char *outbuf, size_t outbuflen); 253172930Srwatsonint mac_vnode_internalize_label(struct label *label, char *string); 254172930Srwatsonvoid mac_vnode_check_mmap_downgrade(struct ucred *cred, struct vnode *vp, 255121352Srwatson int *prot); 256121352Srwatsonint vn_setlabel(struct vnode *vp, struct label *intlabel, 257121352Srwatson struct ucred *cred); 258114806Srwatson 259100979Srwatson/* 260189797Srwatson * MAC Framework composition macros invoke all registered MAC policies for a 261189797Srwatson * specific entry point. They come in two forms: one which permits policies 262189797Srwatson * to sleep/block, and another that does not. 263189797Srwatson * 264191731Srwatson * MAC_POLICY_CHECK performs the designated check by walking the policy 265191731Srwatson * module list and checking with each as to how it feels about the request. 266191731Srwatson * Note that it returns its value via 'error' in the scope of the caller. 267100979Srwatson */ 268191731Srwatson#define MAC_POLICY_CHECK(check, args...) do { \ 269100979Srwatson struct mac_policy_conf *mpc; \ 270100979Srwatson \ 271100979Srwatson error = 0; \ 272114806Srwatson LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 273100979Srwatson if (mpc->mpc_ops->mpo_ ## check != NULL) \ 274121352Srwatson error = mac_error_select( \ 275100979Srwatson mpc->mpc_ops->mpo_ ## check (args), \ 276100979Srwatson error); \ 277100979Srwatson } \ 278189797Srwatson if (!LIST_EMPTY(&mac_policy_list)) { \ 279189797Srwatson mac_policy_slock_sleep(); \ 280114806Srwatson LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 281114806Srwatson if (mpc->mpc_ops->mpo_ ## check != NULL) \ 282121352Srwatson error = mac_error_select( \ 283114806Srwatson mpc->mpc_ops->mpo_ ## check (args), \ 284114806Srwatson error); \ 285114806Srwatson } \ 286189797Srwatson mac_policy_sunlock_sleep(); \ 287114806Srwatson } \ 288100979Srwatson} while (0) 289100979Srwatson 290191731Srwatson#define MAC_POLICY_CHECK_NOSLEEP(check, args...) do { \ 291189797Srwatson struct mac_policy_conf *mpc; \ 292189797Srwatson \ 293189797Srwatson error = 0; \ 294189797Srwatson LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 295189797Srwatson if (mpc->mpc_ops->mpo_ ## check != NULL) \ 296189797Srwatson error = mac_error_select( \ 297189797Srwatson mpc->mpc_ops->mpo_ ## check (args), \ 298189797Srwatson error); \ 299189797Srwatson } \ 300189797Srwatson if (!LIST_EMPTY(&mac_policy_list)) { \ 301192881Srwatson struct rm_priotracker tracker; \ 302192881Srwatson \ 303192881Srwatson mac_policy_slock_nosleep(&tracker); \ 304189797Srwatson LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 305189797Srwatson if (mpc->mpc_ops->mpo_ ## check != NULL) \ 306189797Srwatson error = mac_error_select( \ 307189797Srwatson mpc->mpc_ops->mpo_ ## check (args), \ 308189797Srwatson error); \ 309189797Srwatson } \ 310192881Srwatson mac_policy_sunlock_nosleep(&tracker); \ 311189797Srwatson } \ 312189797Srwatson} while (0) 313189797Srwatson 314100979Srwatson/* 315191731Srwatson * MAC_POLICY_GRANT performs the designated check by walking the policy 316191731Srwatson * module list and checking with each as to how it feels about the request. 317191731Srwatson * Unlike MAC_POLICY_CHECK, it grants if any policies return '0', and 318191731Srwatson * otherwise returns EPERM. Note that it returns its value via 'error' in 319191731Srwatson * the scope of the caller. 320164032Srwatson */ 321191731Srwatson#define MAC_POLICY_GRANT_NOSLEEP(check, args...) do { \ 322164032Srwatson struct mac_policy_conf *mpc; \ 323164032Srwatson \ 324164032Srwatson error = EPERM; \ 325164032Srwatson LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 326164032Srwatson if (mpc->mpc_ops->mpo_ ## check != NULL) { \ 327164032Srwatson if (mpc->mpc_ops->mpo_ ## check(args) == 0) \ 328164032Srwatson error = 0; \ 329164032Srwatson } \ 330164032Srwatson } \ 331189797Srwatson if (!LIST_EMPTY(&mac_policy_list)) { \ 332192881Srwatson struct rm_priotracker tracker; \ 333192881Srwatson \ 334192881Srwatson mac_policy_slock_nosleep(&tracker); \ 335164032Srwatson LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 336164032Srwatson if (mpc->mpc_ops->mpo_ ## check != NULL) { \ 337164032Srwatson if (mpc->mpc_ops->mpo_ ## check (args) \ 338164032Srwatson == 0) \ 339164032Srwatson error = 0; \ 340164032Srwatson } \ 341164032Srwatson } \ 342192881Srwatson mac_policy_sunlock_nosleep(&tracker); \ 343164032Srwatson } \ 344164032Srwatson} while (0) 345164032Srwatson 346164032Srwatson/* 347191731Srwatson * MAC_POLICY_BOOLEAN performs the designated boolean composition by walking 348191731Srwatson * the module list, invoking each instance of the operation, and combining 349191731Srwatson * the results using the passed C operator. Note that it returns its value 350191731Srwatson * via 'result' in the scope of the caller, which should be initialized by 351191731Srwatson * the caller in a meaningful way to get a meaningful result. 352100979Srwatson */ 353191731Srwatson#define MAC_POLICY_BOOLEAN(operation, composition, args...) do { \ 354100979Srwatson struct mac_policy_conf *mpc; \ 355100979Srwatson \ 356114806Srwatson LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 357100979Srwatson if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 358100979Srwatson result = result composition \ 359100979Srwatson mpc->mpc_ops->mpo_ ## operation (args); \ 360100979Srwatson } \ 361189797Srwatson if (!LIST_EMPTY(&mac_policy_list)) { \ 362189797Srwatson mac_policy_slock_sleep(); \ 363114806Srwatson LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 364114806Srwatson if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 365114806Srwatson result = result composition \ 366114806Srwatson mpc->mpc_ops->mpo_ ## operation \ 367114806Srwatson (args); \ 368114806Srwatson } \ 369189797Srwatson mac_policy_sunlock_sleep(); \ 370114806Srwatson } \ 371100979Srwatson} while (0) 372100979Srwatson 373191731Srwatson#define MAC_POLICY_BOOLEAN_NOSLEEP(operation, composition, args...) do {\ 374189797Srwatson struct mac_policy_conf *mpc; \ 375189797Srwatson \ 376189797Srwatson LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 377189797Srwatson if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 378189797Srwatson result = result composition \ 379189797Srwatson mpc->mpc_ops->mpo_ ## operation (args); \ 380189797Srwatson } \ 381189797Srwatson if (!LIST_EMPTY(&mac_policy_list)) { \ 382192881Srwatson struct rm_priotracker tracker; \ 383192881Srwatson \ 384192881Srwatson mac_policy_slock_nosleep(&tracker); \ 385189797Srwatson LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 386189797Srwatson if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 387189797Srwatson result = result composition \ 388189797Srwatson mpc->mpc_ops->mpo_ ## operation \ 389189797Srwatson (args); \ 390189797Srwatson } \ 391192881Srwatson mac_policy_sunlock_nosleep(&tracker); \ 392189797Srwatson } \ 393189797Srwatson} while (0) 394189797Srwatson 395165421Srwatson/* 396191731Srwatson * MAC_POLICY_EXTERNALIZE queries each policy to see if it can generate an 397165421Srwatson * externalized version of a label element by name. Policies declare whether 398165421Srwatson * they have matched a particular element name, parsed from the string by 399191731Srwatson * MAC_POLICY_EXTERNALIZE, and an error is returned if any element is matched 400191731Srwatson * by no policy. 401165421Srwatson */ 402191731Srwatson#define MAC_POLICY_EXTERNALIZE(type, label, elementlist, outbuf, \ 403105694Srwatson outbuflen) do { \ 404116701Srwatson int claimed, first, ignorenotfound, savedlen; \ 405116701Srwatson char *element_name, *element_temp; \ 406116701Srwatson struct sbuf sb; \ 407105694Srwatson \ 408105694Srwatson error = 0; \ 409116701Srwatson first = 1; \ 410116701Srwatson sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN); \ 411105694Srwatson element_temp = elementlist; \ 412105694Srwatson while ((element_name = strsep(&element_temp, ",")) != NULL) { \ 413105694Srwatson if (element_name[0] == '?') { \ 414105694Srwatson element_name++; \ 415105694Srwatson ignorenotfound = 1; \ 416116701Srwatson } else \ 417105694Srwatson ignorenotfound = 0; \ 418116701Srwatson savedlen = sbuf_len(&sb); \ 419121352Srwatson if (first) \ 420116701Srwatson error = sbuf_printf(&sb, "%s/", element_name); \ 421121352Srwatson else \ 422116701Srwatson error = sbuf_printf(&sb, ",%s/", element_name); \ 423116701Srwatson if (error == -1) { \ 424116701Srwatson error = EINVAL; /* XXX: E2BIG? */ \ 425105694Srwatson break; \ 426105694Srwatson } \ 427116701Srwatson claimed = 0; \ 428191731Srwatson MAC_POLICY_CHECK(type ## _externalize_label, label, \ 429121507Srwatson element_name, &sb, &claimed); \ 430105694Srwatson if (error) \ 431105694Srwatson break; \ 432116701Srwatson if (claimed == 0 && ignorenotfound) { \ 433116701Srwatson /* Revert last label name. */ \ 434116701Srwatson sbuf_setpos(&sb, savedlen); \ 435116701Srwatson } else if (claimed != 1) { \ 436116701Srwatson error = EINVAL; /* XXX: ENOLABEL? */ \ 437105694Srwatson break; \ 438121352Srwatson } else { \ 439121352Srwatson first = 0; \ 440105694Srwatson } \ 441105694Srwatson } \ 442116701Srwatson sbuf_finish(&sb); \ 443105694Srwatson} while (0) 444105694Srwatson 445165421Srwatson/* 446191731Srwatson * MAC_POLICY_INTERNALIZE presents parsed element names and data to each 447191731Srwatson * policy to see if any is willing to claim it and internalize the label 448191731Srwatson * data. If no policies match, an error is returned. 449165421Srwatson */ 450191731Srwatson#define MAC_POLICY_INTERNALIZE(type, label, instring) do { \ 451105694Srwatson char *element, *element_name, *element_data; \ 452105694Srwatson int claimed; \ 453105694Srwatson \ 454105694Srwatson error = 0; \ 455105694Srwatson element = instring; \ 456105694Srwatson while ((element_name = strsep(&element, ",")) != NULL) { \ 457105694Srwatson element_data = element_name; \ 458105694Srwatson element_name = strsep(&element_data, "/"); \ 459105694Srwatson if (element_data == NULL) { \ 460105694Srwatson error = EINVAL; \ 461105694Srwatson break; \ 462105694Srwatson } \ 463105694Srwatson claimed = 0; \ 464191731Srwatson MAC_POLICY_CHECK(type ## _internalize_label, label, \ 465121507Srwatson element_name, element_data, &claimed); \ 466105694Srwatson if (error) \ 467105694Srwatson break; \ 468105694Srwatson if (claimed != 1) { \ 469105694Srwatson /* XXXMAC: Another error here? */ \ 470105694Srwatson error = EINVAL; \ 471105694Srwatson break; \ 472105694Srwatson } \ 473105694Srwatson } \ 474105694Srwatson} while (0) 475105694Srwatson 476100979Srwatson/* 477191731Srwatson * MAC_POLICY_PERFORM performs the designated operation by walking the policy 478191731Srwatson * module list and invoking that operation for each policy. 479100979Srwatson */ 480191731Srwatson#define MAC_POLICY_PERFORM(operation, args...) do { \ 481100979Srwatson struct mac_policy_conf *mpc; \ 482100979Srwatson \ 483114806Srwatson LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 484100979Srwatson if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 485100979Srwatson mpc->mpc_ops->mpo_ ## operation (args); \ 486100979Srwatson } \ 487189797Srwatson if (!LIST_EMPTY(&mac_policy_list)) { \ 488189797Srwatson mac_policy_slock_sleep(); \ 489114806Srwatson LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 490114806Srwatson if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 491114806Srwatson mpc->mpc_ops->mpo_ ## operation (args); \ 492114806Srwatson } \ 493189797Srwatson mac_policy_sunlock_sleep(); \ 494114806Srwatson } \ 495100979Srwatson} while (0) 496165607Srwatson 497191731Srwatson#define MAC_POLICY_PERFORM_NOSLEEP(operation, args...) do { \ 498189797Srwatson struct mac_policy_conf *mpc; \ 499189797Srwatson \ 500189797Srwatson LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 501189797Srwatson if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 502189797Srwatson mpc->mpc_ops->mpo_ ## operation (args); \ 503189797Srwatson } \ 504189797Srwatson if (!LIST_EMPTY(&mac_policy_list)) { \ 505192881Srwatson struct rm_priotracker tracker; \ 506192881Srwatson \ 507192881Srwatson mac_policy_slock_nosleep(&tracker); \ 508189797Srwatson LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 509189797Srwatson if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 510189797Srwatson mpc->mpc_ops->mpo_ ## operation (args); \ 511189797Srwatson } \ 512192881Srwatson mac_policy_sunlock_nosleep(&tracker); \ 513189797Srwatson } \ 514189797Srwatson} while (0) 515189797Srwatson 516178184Srwatson#endif /* !_SECURITY_MAC_MAC_INTERNAL_H_ */ 517