mac_internal.h revision 122809
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 122809 2003-11-16 20:01:50Z rwatson $ 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_socket_label_set(struct ucred *cred, struct socket *so, 125 struct label *label); 126 127int mac_externalize_vnode_label(struct label *label, char *elements, 128 char *outbuf, size_t outbuflen); 129int mac_internalize_vnode_label(struct label *label, char *string); 130void mac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp, 131 int *prot); 132int vn_setlabel(struct vnode *vp, struct label *intlabel, 133 struct ucred *cred); 134 135/* 136 * MAC_CHECK performs the designated check by walking the policy module 137 * list and checking with each as to how it feels about the request. 138 * Note that it returns its value via 'error' in the scope of the caller. 139 */ 140#define MAC_CHECK(check, args...) do { \ 141 struct mac_policy_conf *mpc; \ 142 int entrycount; \ 143 \ 144 error = 0; \ 145 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 146 if (mpc->mpc_ops->mpo_ ## check != NULL) \ 147 error = mac_error_select( \ 148 mpc->mpc_ops->mpo_ ## check (args), \ 149 error); \ 150 } \ 151 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ 152 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 153 if (mpc->mpc_ops->mpo_ ## check != NULL) \ 154 error = mac_error_select( \ 155 mpc->mpc_ops->mpo_ ## check (args), \ 156 error); \ 157 } \ 158 mac_policy_list_unbusy(); \ 159 } \ 160} while (0) 161 162/* 163 * MAC_BOOLEAN performs the designated boolean composition by walking 164 * the module list, invoking each instance of the operation, and 165 * combining the results using the passed C operator. Note that it 166 * returns its value via 'result' in the scope of the caller, which 167 * should be initialized by the caller in a meaningful way to get 168 * a meaningful result. 169 */ 170#define MAC_BOOLEAN(operation, composition, args...) do { \ 171 struct mac_policy_conf *mpc; \ 172 int entrycount; \ 173 \ 174 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 175 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 176 result = result composition \ 177 mpc->mpc_ops->mpo_ ## operation (args); \ 178 } \ 179 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ 180 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 181 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 182 result = result composition \ 183 mpc->mpc_ops->mpo_ ## operation \ 184 (args); \ 185 } \ 186 mac_policy_list_unbusy(); \ 187 } \ 188} while (0) 189 190#define MAC_EXTERNALIZE(type, label, elementlist, outbuf, \ 191 outbuflen) do { \ 192 int claimed, first, ignorenotfound, savedlen; \ 193 char *element_name, *element_temp; \ 194 struct sbuf sb; \ 195 \ 196 error = 0; \ 197 first = 1; \ 198 sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN); \ 199 element_temp = elementlist; \ 200 while ((element_name = strsep(&element_temp, ",")) != NULL) { \ 201 if (element_name[0] == '?') { \ 202 element_name++; \ 203 ignorenotfound = 1; \ 204 } else \ 205 ignorenotfound = 0; \ 206 savedlen = sbuf_len(&sb); \ 207 if (first) \ 208 error = sbuf_printf(&sb, "%s/", element_name); \ 209 else \ 210 error = sbuf_printf(&sb, ",%s/", element_name); \ 211 if (error == -1) { \ 212 error = EINVAL; /* XXX: E2BIG? */ \ 213 break; \ 214 } \ 215 claimed = 0; \ 216 MAC_CHECK(externalize_ ## type ## _label, label, \ 217 element_name, &sb, &claimed); \ 218 if (error) \ 219 break; \ 220 if (claimed == 0 && ignorenotfound) { \ 221 /* Revert last label name. */ \ 222 sbuf_setpos(&sb, savedlen); \ 223 } else if (claimed != 1) { \ 224 error = EINVAL; /* XXX: ENOLABEL? */ \ 225 break; \ 226 } else { \ 227 first = 0; \ 228 } \ 229 } \ 230 sbuf_finish(&sb); \ 231} while (0) 232 233#define MAC_INTERNALIZE(type, label, instring) do { \ 234 char *element, *element_name, *element_data; \ 235 int claimed; \ 236 \ 237 error = 0; \ 238 element = instring; \ 239 while ((element_name = strsep(&element, ",")) != NULL) { \ 240 element_data = element_name; \ 241 element_name = strsep(&element_data, "/"); \ 242 if (element_data == NULL) { \ 243 error = EINVAL; \ 244 break; \ 245 } \ 246 claimed = 0; \ 247 MAC_CHECK(internalize_ ## type ## _label, label, \ 248 element_name, element_data, &claimed); \ 249 if (error) \ 250 break; \ 251 if (claimed != 1) { \ 252 /* XXXMAC: Another error here? */ \ 253 error = EINVAL; \ 254 break; \ 255 } \ 256 } \ 257} while (0) 258 259/* 260 * MAC_PERFORM performs the designated operation by walking the policy 261 * module list and invoking that operation for each policy. 262 */ 263#define MAC_PERFORM(operation, args...) do { \ 264 struct mac_policy_conf *mpc; \ 265 int entrycount; \ 266 \ 267 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 268 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 269 mpc->mpc_ops->mpo_ ## operation (args); \ 270 } \ 271 if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ 272 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 273 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 274 mpc->mpc_ops->mpo_ ## operation (args); \ 275 } \ 276 mac_policy_list_unbusy(); \ 277 } \ 278} while (0) 279