mac_internal.h revision 166531
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 166531 2007-02-06 10:59:23Z rwatson $ 41 */ 42 43#ifndef _SYS_SECURITY_MAC_MAC_INTERNAL_H_ 44#define _SYS_SECURITY_MAC_MAC_INTERNAL_H_ 45 46#ifndef _KERNEL 47#error "no user-serviceable parts inside" 48#endif 49 50/* 51 * MAC Framework sysctl namespace. 52 */ 53#ifdef SYSCTL_DECL 54SYSCTL_DECL(_security_mac); 55#endif /* SYSCTL_DECL */ 56 57/* 58 * MAC Framework global types and typedefs. 59 */ 60LIST_HEAD(mac_policy_list_head, mac_policy_conf); 61#ifdef MALLOC_DECLARE 62MALLOC_DECLARE(M_MACTEMP); 63#endif 64 65/* 66 * MAC Framework global variables. 67 */ 68extern struct mac_policy_list_head mac_policy_list; 69extern struct mac_policy_list_head mac_static_policy_list; 70#ifndef MAC_ALWAYS_LABEL_MBUF 71extern int mac_labelmbufs; 72#endif 73 74/* 75 * MAC Framework infrastructure functions. 76 */ 77int mac_error_select(int error1, int error2); 78 79void mac_policy_grab_exclusive(void); 80void mac_policy_assert_exclusive(void); 81void mac_policy_release_exclusive(void); 82void mac_policy_list_busy(void); 83int mac_policy_list_conditional_busy(void); 84void mac_policy_list_unbusy(void); 85 86struct label *mac_labelzone_alloc(int flags); 87void mac_labelzone_free(struct label *label); 88void mac_labelzone_init(void); 89 90void mac_init_label(struct label *label); 91void mac_destroy_label(struct label *label); 92int mac_check_structmac_consistent(struct mac *mac); 93int mac_allocate_slot(void); 94 95/* 96 * MAC Framework per-object type functions. It's not yet clear how the 97 * namespaces, etc, should work for these, so for now, sort by object type. 98 */ 99struct label *mac_pipe_label_alloc(void); 100void mac_pipe_label_free(struct label *label); 101struct label *mac_socket_label_alloc(int flag); 102void mac_socket_label_free(struct label *label); 103 104int mac_check_cred_relabel(struct ucred *cred, struct label *newlabel); 105int mac_externalize_cred_label(struct label *label, char *elements, 106 char *outbuf, size_t outbuflen); 107int mac_internalize_cred_label(struct label *label, char *string); 108void mac_relabel_cred(struct ucred *cred, struct label *newlabel); 109 110struct label *mac_mbuf_to_label(struct mbuf *m); 111 112void mac_copy_pipe_label(struct label *src, struct label *dest); 113int mac_externalize_pipe_label(struct label *label, char *elements, 114 char *outbuf, size_t outbuflen); 115int mac_internalize_pipe_label(struct label *label, char *string); 116 117int mac_socket_label_set(struct ucred *cred, struct socket *so, 118 struct label *label); 119void mac_copy_socket_label(struct label *src, struct label *dest); 120int mac_externalize_socket_label(struct label *label, char *elements, 121 char *outbuf, size_t outbuflen); 122int mac_internalize_socket_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 list 134 * and checking with each as to how it feels about the request. Note that it 135 * 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_GRANT performs the designated check by walking the policy module list 161 * and checking with each as to how it feels about the request. Unlike 162 * MAC_CHECK, it grants if any policies return '0', and otherwise returns 163 * EPERM. Note that it returns its value via 'error' in the scope of the 164 * caller. 165 */ 166#define MAC_GRANT(check, args...) do { \ 167 struct mac_policy_conf *mpc; \ 168 int entrycount; \ 169 \ 170 error = EPERM; \ 171 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 172 if (mpc->mpc_ops->mpo_ ## check != NULL) { \ 173 if (mpc->mpc_ops->mpo_ ## check(args) == 0) \ 174 error = 0; \ 175 } \ 176 } \ 177 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ 178 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 179 if (mpc->mpc_ops->mpo_ ## check != NULL) { \ 180 if (mpc->mpc_ops->mpo_ ## check (args) \ 181 == 0) \ 182 error = 0; \ 183 } \ 184 } \ 185 mac_policy_list_unbusy(); \ 186 } \ 187} while (0) 188 189/* 190 * MAC_BOOLEAN performs the designated boolean composition by walking the 191 * module list, invoking each instance of the operation, and combining the 192 * results using the passed C operator. Note that it returns its value via 193 * 'result' in the scope of the caller, which should be initialized by the 194 * caller in a meaningful way to get a meaningful result. 195 */ 196#define MAC_BOOLEAN(operation, composition, args...) do { \ 197 struct mac_policy_conf *mpc; \ 198 int entrycount; \ 199 \ 200 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 201 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 202 result = result composition \ 203 mpc->mpc_ops->mpo_ ## operation (args); \ 204 } \ 205 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ 206 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 207 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 208 result = result composition \ 209 mpc->mpc_ops->mpo_ ## operation \ 210 (args); \ 211 } \ 212 mac_policy_list_unbusy(); \ 213 } \ 214} while (0) 215 216/* 217 * MAC_EXTERNALIZE queries each policy to see if it can generate an 218 * externalized version of a label element by name. Policies declare whether 219 * they have matched a particular element name, parsed from the string by 220 * MAC_EXTERNALIZE, and an error is returned if any element is matched by no 221 * policy. 222 */ 223#define MAC_EXTERNALIZE(type, label, elementlist, outbuf, \ 224 outbuflen) do { \ 225 int claimed, first, ignorenotfound, savedlen; \ 226 char *element_name, *element_temp; \ 227 struct sbuf sb; \ 228 \ 229 error = 0; \ 230 first = 1; \ 231 sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN); \ 232 element_temp = elementlist; \ 233 while ((element_name = strsep(&element_temp, ",")) != NULL) { \ 234 if (element_name[0] == '?') { \ 235 element_name++; \ 236 ignorenotfound = 1; \ 237 } else \ 238 ignorenotfound = 0; \ 239 savedlen = sbuf_len(&sb); \ 240 if (first) \ 241 error = sbuf_printf(&sb, "%s/", element_name); \ 242 else \ 243 error = sbuf_printf(&sb, ",%s/", element_name); \ 244 if (error == -1) { \ 245 error = EINVAL; /* XXX: E2BIG? */ \ 246 break; \ 247 } \ 248 claimed = 0; \ 249 MAC_CHECK(externalize_ ## type ## _label, label, \ 250 element_name, &sb, &claimed); \ 251 if (error) \ 252 break; \ 253 if (claimed == 0 && ignorenotfound) { \ 254 /* Revert last label name. */ \ 255 sbuf_setpos(&sb, savedlen); \ 256 } else if (claimed != 1) { \ 257 error = EINVAL; /* XXX: ENOLABEL? */ \ 258 break; \ 259 } else { \ 260 first = 0; \ 261 } \ 262 } \ 263 sbuf_finish(&sb); \ 264} while (0) 265 266/* 267 * MAC_INTERNALIZE presents parsed element names and data to each policy to 268 * see if any is willing to claim it and internalize the label data. If no 269 * policies match, an error is returned. 270 */ 271#define MAC_INTERNALIZE(type, label, instring) do { \ 272 char *element, *element_name, *element_data; \ 273 int claimed; \ 274 \ 275 error = 0; \ 276 element = instring; \ 277 while ((element_name = strsep(&element, ",")) != NULL) { \ 278 element_data = element_name; \ 279 element_name = strsep(&element_data, "/"); \ 280 if (element_data == NULL) { \ 281 error = EINVAL; \ 282 break; \ 283 } \ 284 claimed = 0; \ 285 MAC_CHECK(internalize_ ## type ## _label, label, \ 286 element_name, element_data, &claimed); \ 287 if (error) \ 288 break; \ 289 if (claimed != 1) { \ 290 /* XXXMAC: Another error here? */ \ 291 error = EINVAL; \ 292 break; \ 293 } \ 294 } \ 295} while (0) 296 297/* 298 * MAC_PERFORM performs the designated operation by walking the policy module 299 * list and invoking that operation for each policy. 300 */ 301#define MAC_PERFORM(operation, args...) do { \ 302 struct mac_policy_conf *mpc; \ 303 int entrycount; \ 304 \ 305 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 306 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 307 mpc->mpc_ops->mpo_ ## operation (args); \ 308 } \ 309 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ 310 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 311 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 312 mpc->mpc_ops->mpo_ ## operation (args); \ 313 } \ 314 mac_policy_list_unbusy(); \ 315 } \ 316} while (0) 317 318#endif /* !_SYS_SECURITY_MAC_MAC_INTERNAL_H_ */ 319