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