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