mac_internal.h revision 121490
1/*- 2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson 3 * Copyright (c) 2001 Ilmar S. Habibulin 4 * Copyright (c) 2001, 2002, 2003 Networks Associates Technology, Inc. 5 * All rights reserved. 6 * 7 * This software was developed by Robert Watson and Ilmar Habibulin for the 8 * TrustedBSD Project. 9 * 10 * This software was developed for the FreeBSD Project in part by Network 11 * Associates Laboratories, the Security Research Division of Network 12 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 13 * as part of the DARPA CHATS research program. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * $FreeBSD: head/sys/security/mac/mac_internal.h 121490 2003-10-25 03:50:44Z rwatson $ 37 */ 38 39/* 40 * MAC Framework sysctl namespace. 41 */ 42SYSCTL_DECL(_security); 43SYSCTL_DECL(_security_mac); 44#ifdef MAC_DEBUG 45SYSCTL_DECL(_security_mac_debug); 46SYSCTL_DECL(_security_mac_debug_counters); 47#endif 48 49/* 50 * MAC Framework global types and typedefs. 51 */ 52LIST_HEAD(mac_policy_list_head, mac_policy_conf); 53MALLOC_DECLARE(M_MACTEMP); 54 55/* 56 * MAC Framework global variables. 57 */ 58extern struct mac_policy_list_head mac_policy_list; 59extern struct mac_policy_list_head mac_static_policy_list; 60extern int mac_late; 61extern int mac_enforce_process; 62extern int mac_enforce_vm; 63#ifndef MAC_ALWAYS_LABEL_MBUF 64extern int mac_labelmbufs; 65#endif 66 67/* 68 * MAC Framework object/access counter primitives, conditionally 69 * compiled. 70 */ 71#ifdef MAC_DEBUG 72#define MAC_DEBUG_COUNTER_INC(x) atomic_add_int(x, 1); 73#define MAC_DEBUG_COUNTER_DEC(x) atomic_subtract_int(x, 1); 74#else 75#define MAC_DEBUG_COUNTER_INC(x) 76#define MAC_DEBUG_COUNTER_DEC(x) 77#endif 78 79/* 80 * MAC Framework infrastructure functions. 81 */ 82int mac_error_select(int error1, int error2); 83 84void mac_policy_grab_exclusive(void); 85void mac_policy_assert_exclusive(void); 86void mac_policy_release_exclusive(void); 87void mac_policy_list_busy(void); 88int mac_policy_list_conditional_busy(void); 89void mac_policy_list_unbusy(void); 90 91void mac_init_label(struct label *label); 92void mac_destroy_label(struct label *label); 93int mac_check_structmac_consistent(struct mac *mac); 94int mac_allocate_slot(void); 95 96/* 97 * MAC Framework per-object type functions. It's not yet clear how 98 * the namespaces, etc, should work for these, so for now, sort by 99 * object type. 100 */ 101int mac_check_cred_relabel(struct ucred *cred, struct label *newlabel); 102void mac_destroy_cred_label(struct label *label); 103int mac_externalize_cred_label(struct label *label, char *elements, 104 char *outbuf, size_t outbuflen, int flags); 105void mac_init_cred_label(struct label *label); 106int mac_internalize_cred_label(struct label *label, char *string); 107void mac_relabel_cred(struct ucred *cred, struct label *newlabel); 108 109void mac_copy_pipe_label(struct label *src, struct label *dest); 110void mac_destroy_pipe_label(struct label *label); 111int mac_externalize_pipe_label(struct label *label, char *elements, 112 char *outbuf, size_t outbuflen, int flags); 113void mac_init_pipe_label(struct label *label); 114int mac_internalize_pipe_label(struct label *label, char *string); 115 116int mac_externalize_vnode_label(struct label *label, char *elements, 117 char *outbuf, size_t outbuflen, int flags); 118int mac_internalize_vnode_label(struct label *label, char *string); 119void mac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp, 120 int *prot); 121int vn_setlabel(struct vnode *vp, struct label *intlabel, 122 struct ucred *cred); 123 124/* 125 * MAC_CHECK performs the designated check by walking the policy module 126 * list and checking with each as to how it feels about the request. 127 * Note that it returns its value via 'error' in the scope of the caller. 128 */ 129#define MAC_CHECK(check, args...) do { \ 130 struct mac_policy_conf *mpc; \ 131 int entrycount; \ 132 \ 133 error = 0; \ 134 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 135 if (mpc->mpc_ops->mpo_ ## check != NULL) \ 136 error = mac_error_select( \ 137 mpc->mpc_ops->mpo_ ## check (args), \ 138 error); \ 139 } \ 140 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ 141 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 142 if (mpc->mpc_ops->mpo_ ## check != NULL) \ 143 error = mac_error_select( \ 144 mpc->mpc_ops->mpo_ ## check (args), \ 145 error); \ 146 } \ 147 mac_policy_list_unbusy(); \ 148 } \ 149} while (0) 150 151/* 152 * MAC_BOOLEAN performs the designated boolean composition by walking 153 * the module list, invoking each instance of the operation, and 154 * combining the results using the passed C operator. Note that it 155 * returns its value via 'result' in the scope of the caller, which 156 * should be initialized by the caller in a meaningful way to get 157 * a meaningful result. 158 */ 159#define MAC_BOOLEAN(operation, composition, args...) do { \ 160 struct mac_policy_conf *mpc; \ 161 int entrycount; \ 162 \ 163 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 164 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 165 result = result composition \ 166 mpc->mpc_ops->mpo_ ## operation (args); \ 167 } \ 168 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ 169 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 170 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 171 result = result composition \ 172 mpc->mpc_ops->mpo_ ## operation \ 173 (args); \ 174 } \ 175 mac_policy_list_unbusy(); \ 176 } \ 177} while (0) 178 179#define MAC_EXTERNALIZE(type, label, elementlist, outbuf, \ 180 outbuflen) do { \ 181 int claimed, first, ignorenotfound, savedlen; \ 182 char *element_name, *element_temp; \ 183 struct sbuf sb; \ 184 \ 185 error = 0; \ 186 first = 1; \ 187 sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN); \ 188 element_temp = elementlist; \ 189 while ((element_name = strsep(&element_temp, ",")) != NULL) { \ 190 if (element_name[0] == '?') { \ 191 element_name++; \ 192 ignorenotfound = 1; \ 193 } else \ 194 ignorenotfound = 0; \ 195 savedlen = sbuf_len(&sb); \ 196 if (first) \ 197 error = sbuf_printf(&sb, "%s/", element_name); \ 198 else \ 199 error = sbuf_printf(&sb, ",%s/", element_name); \ 200 if (error == -1) { \ 201 error = EINVAL; /* XXX: E2BIG? */ \ 202 break; \ 203 } \ 204 claimed = 0; \ 205 MAC_CHECK(externalize_ ## type, label, element_name, \ 206 &sb, &claimed); \ 207 if (error) \ 208 break; \ 209 if (claimed == 0 && ignorenotfound) { \ 210 /* Revert last label name. */ \ 211 sbuf_setpos(&sb, savedlen); \ 212 } else if (claimed != 1) { \ 213 error = EINVAL; /* XXX: ENOLABEL? */ \ 214 break; \ 215 } else { \ 216 first = 0; \ 217 } \ 218 } \ 219 sbuf_finish(&sb); \ 220} while (0) 221 222#define MAC_INTERNALIZE(type, label, instring) do { \ 223 char *element, *element_name, *element_data; \ 224 int claimed; \ 225 \ 226 error = 0; \ 227 element = instring; \ 228 while ((element_name = strsep(&element, ",")) != NULL) { \ 229 element_data = element_name; \ 230 element_name = strsep(&element_data, "/"); \ 231 if (element_data == NULL) { \ 232 error = EINVAL; \ 233 break; \ 234 } \ 235 claimed = 0; \ 236 MAC_CHECK(internalize_ ## type, label, element_name, \ 237 element_data, &claimed); \ 238 if (error) \ 239 break; \ 240 if (claimed != 1) { \ 241 /* XXXMAC: Another error here? */ \ 242 error = EINVAL; \ 243 break; \ 244 } \ 245 } \ 246} while (0) 247 248/* 249 * MAC_PERFORM performs the designated operation by walking the policy 250 * module list and invoking that operation for each policy. 251 */ 252#define MAC_PERFORM(operation, args...) do { \ 253 struct mac_policy_conf *mpc; \ 254 int entrycount; \ 255 \ 256 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 257 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 258 mpc->mpc_ops->mpo_ ## operation (args); \ 259 } \ 260 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ 261 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 262 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 263 mpc->mpc_ops->mpo_ ## operation (args); \ 264 } \ 265 mac_policy_list_unbusy(); \ 266 } \ 267} while (0) 268