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