mac_internal.h revision 122705
1254885Sdumbbell/*- 2254885Sdumbbell * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson 3254885Sdumbbell * Copyright (c) 2001 Ilmar S. Habibulin 4254885Sdumbbell * Copyright (c) 2001, 2002, 2003 Networks Associates Technology, Inc. 5254885Sdumbbell * All rights reserved. 6254885Sdumbbell * 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 122705 2003-11-14 21:18:04Z bde $ 37 */ 38 39/* 40 * MAC Framework sysctl namespace. 41 */ 42#ifdef SYSCTL_DECL 43SYSCTL_DECL(_security); 44SYSCTL_DECL(_security_mac); 45#ifdef MAC_DEBUG 46SYSCTL_DECL(_security_mac_debug); 47SYSCTL_DECL(_security_mac_debug_counters); 48#endif 49#endif /* SYSCTL_DECL */ 50 51/* 52 * MAC Framework global types and typedefs. 53 */ 54LIST_HEAD(mac_policy_list_head, mac_policy_conf); 55#ifdef MALLOC_DECLARE 56MALLOC_DECLARE(M_MACTEMP); 57#endif 58 59/* 60 * MAC Framework global variables. 61 */ 62extern struct mac_policy_list_head mac_policy_list; 63extern struct mac_policy_list_head mac_static_policy_list; 64extern int mac_late; 65extern int mac_enforce_process; 66extern int mac_enforce_sysv; 67extern int mac_enforce_vm; 68#ifndef MAC_ALWAYS_LABEL_MBUF 69extern int mac_labelmbufs; 70#endif 71 72/* 73 * MAC Framework object/access counter primitives, conditionally 74 * compiled. 75 */ 76#ifdef MAC_DEBUG 77#define MAC_DEBUG_COUNTER_INC(x) atomic_add_int(x, 1); 78#define MAC_DEBUG_COUNTER_DEC(x) atomic_subtract_int(x, 1); 79#else 80#define MAC_DEBUG_COUNTER_INC(x) 81#define MAC_DEBUG_COUNTER_DEC(x) 82#endif 83 84/* 85 * MAC Framework infrastructure functions. 86 */ 87int mac_error_select(int error1, int error2); 88 89void mac_policy_grab_exclusive(void); 90void mac_policy_assert_exclusive(void); 91void mac_policy_release_exclusive(void); 92void mac_policy_list_busy(void); 93int mac_policy_list_conditional_busy(void); 94void mac_policy_list_unbusy(void); 95 96struct label *mac_labelzone_alloc(int flags); 97void mac_labelzone_free(struct label *label); 98void mac_labelzone_init(void); 99 100void mac_init_label(struct label *label); 101void mac_destroy_label(struct label *label); 102int mac_check_structmac_consistent(struct mac *mac); 103int mac_allocate_slot(void); 104 105/* 106 * MAC Framework per-object type functions. It's not yet clear how 107 * the namespaces, etc, should work for these, so for now, sort by 108 * object type. 109 */ 110struct label *mac_pipe_label_alloc(void); 111void mac_pipe_label_free(struct label *label); 112 113int mac_check_cred_relabel(struct ucred *cred, struct label *newlabel); 114int mac_externalize_cred_label(struct label *label, char *elements, 115 char *outbuf, size_t outbuflen); 116int mac_internalize_cred_label(struct label *label, char *string); 117void mac_relabel_cred(struct ucred *cred, struct label *newlabel); 118 119void mac_copy_pipe_label(struct label *src, struct label *dest); 120int mac_externalize_pipe_label(struct label *label, char *elements, 121 char *outbuf, size_t outbuflen); 122int mac_internalize_pipe_label(struct label *label, char *string); 123 124int mac_externalize_vnode_label(struct label *label, char *elements, 125 char *outbuf, size_t outbuflen); 126int mac_internalize_vnode_label(struct label *label, char *string); 127void mac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp, 128 int *prot); 129int vn_setlabel(struct vnode *vp, struct label *intlabel, 130 struct ucred *cred); 131 132/* 133 * MAC_CHECK performs the designated check by walking the policy module 134 * list and checking with each as to how it feels about the request. 135 * Note that it returns its value via 'error' in the scope of the caller. 136 */ 137#define MAC_CHECK(check, args...) do { \ 138 struct mac_policy_conf *mpc; \ 139 int entrycount; \ 140 \ 141 error = 0; \ 142 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 143 if (mpc->mpc_ops->mpo_ ## check != NULL) \ 144 error = mac_error_select( \ 145 mpc->mpc_ops->mpo_ ## check (args), \ 146 error); \ 147 } \ 148 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ 149 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 150 if (mpc->mpc_ops->mpo_ ## check != NULL) \ 151 error = mac_error_select( \ 152 mpc->mpc_ops->mpo_ ## check (args), \ 153 error); \ 154 } \ 155 mac_policy_list_unbusy(); \ 156 } \ 157} while (0) 158 159/* 160 * MAC_BOOLEAN performs the designated boolean composition by walking 161 * the module list, invoking each instance of the operation, and 162 * combining the results using the passed C operator. Note that it 163 * returns its value via 'result' in the scope of the caller, which 164 * should be initialized by the caller in a meaningful way to get 165 * a meaningful result. 166 */ 167#define MAC_BOOLEAN(operation, composition, args...) do { \ 168 struct mac_policy_conf *mpc; \ 169 int entrycount; \ 170 \ 171 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 172 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 173 result = result composition \ 174 mpc->mpc_ops->mpo_ ## operation (args); \ 175 } \ 176 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ 177 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 178 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 179 result = result composition \ 180 mpc->mpc_ops->mpo_ ## operation \ 181 (args); \ 182 } \ 183 mac_policy_list_unbusy(); \ 184 } \ 185} while (0) 186 187#define MAC_EXTERNALIZE(type, label, elementlist, outbuf, \ 188 outbuflen) do { \ 189 int claimed, first, ignorenotfound, savedlen; \ 190 char *element_name, *element_temp; \ 191 struct sbuf sb; \ 192 \ 193 error = 0; \ 194 first = 1; \ 195 sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN); \ 196 element_temp = elementlist; \ 197 while ((element_name = strsep(&element_temp, ",")) != NULL) { \ 198 if (element_name[0] == '?') { \ 199 element_name++; \ 200 ignorenotfound = 1; \ 201 } else \ 202 ignorenotfound = 0; \ 203 savedlen = sbuf_len(&sb); \ 204 if (first) \ 205 error = sbuf_printf(&sb, "%s/", element_name); \ 206 else \ 207 error = sbuf_printf(&sb, ",%s/", element_name); \ 208 if (error == -1) { \ 209 error = EINVAL; /* XXX: E2BIG? */ \ 210 break; \ 211 } \ 212 claimed = 0; \ 213 MAC_CHECK(externalize_ ## type ## _label, label, \ 214 element_name, &sb, &claimed); \ 215 if (error) \ 216 break; \ 217 if (claimed == 0 && ignorenotfound) { \ 218 /* Revert last label name. */ \ 219 sbuf_setpos(&sb, savedlen); \ 220 } else if (claimed != 1) { \ 221 error = EINVAL; /* XXX: ENOLABEL? */ \ 222 break; \ 223 } else { \ 224 first = 0; \ 225 } \ 226 } \ 227 sbuf_finish(&sb); \ 228} while (0) 229 230#define MAC_INTERNALIZE(type, label, instring) do { \ 231 char *element, *element_name, *element_data; \ 232 int claimed; \ 233 \ 234 error = 0; \ 235 element = instring; \ 236 while ((element_name = strsep(&element, ",")) != NULL) { \ 237 element_data = element_name; \ 238 element_name = strsep(&element_data, "/"); \ 239 if (element_data == NULL) { \ 240 error = EINVAL; \ 241 break; \ 242 } \ 243 claimed = 0; \ 244 MAC_CHECK(internalize_ ## type ## _label, label, \ 245 element_name, element_data, &claimed); \ 246 if (error) \ 247 break; \ 248 if (claimed != 1) { \ 249 /* XXXMAC: Another error here? */ \ 250 error = EINVAL; \ 251 break; \ 252 } \ 253 } \ 254} while (0) 255 256/* 257 * MAC_PERFORM performs the designated operation by walking the policy 258 * module list and invoking that operation for each policy. 259 */ 260#define MAC_PERFORM(operation, args...) do { \ 261 struct mac_policy_conf *mpc; \ 262 int entrycount; \ 263 \ 264 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 265 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 266 mpc->mpc_ops->mpo_ ## operation (args); \ 267 } \ 268 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ 269 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 270 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 271 mpc->mpc_ops->mpo_ ## operation (args); \ 272 } \ 273 mac_policy_list_unbusy(); \ 274 } \ 275} while (0) 276