1 /*- 2 * Copyright (c) 1999-2002, 2006, 2009 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 * Copyright (c) 2006 SPARTA, Inc. 7 * Copyright (c) 2009 Apple, Inc. 8 * All rights reserved. 9 * 10 * This software was developed by Robert Watson and Ilmar Habibulin for the 11 * TrustedBSD Project. 12 * 13 * This software was developed for the FreeBSD Project in part by Network 14 * Associates Laboratories, the Security Research Division of Network 15 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 16 * as part of the DARPA CHATS research program. 17 * 18 * This software was developed by Robert N. M. Watson for the TrustedBSD 19 * Project under contract to nCircle Network Security, Inc. 20 * 21 * This software was enhanced by SPARTA ISSO under SPAWAR contract 22 * N66001-04-C-6019 ("SEFOS"). 23 * 24 * This software was developed at the University of Cambridge Computer 25 * Laboratory with support from a grant from Google, Inc. 26 * 27 * Redistribution and use in source and binary forms, with or without 28 * modification, are permitted provided that the following conditions 29 * are met: 30 * 1. Redistributions of source code must retain the above copyright 31 * notice, this list of conditions and the following disclaimer. 32 * 2. Redistributions in binary form must reproduce the above copyright 33 * notice, this list of conditions and the following disclaimer in the 34 * documentation and/or other materials provided with the distribution. 35 * 36 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 39 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * 48 * $FreeBSD$ 49 */ 50 51 #ifndef _SECURITY_MAC_MAC_INTERNAL_H_ 52 #define _SECURITY_MAC_MAC_INTERNAL_H_ 53 54 #ifndef _KERNEL 55 #error "no user-serviceable parts inside" 56 #endif 57 58 #include <sys/lock.h> 59 #include <sys/rmlock.h> 60 61 /* 62 * MAC Framework sysctl namespace. 63 */ 64 #ifdef SYSCTL_DECL 65 SYSCTL_DECL(_security_mac); 66 #endif /* SYSCTL_DECL */ 67 68 /* 69 * MAC Framework SDT DTrace probe namespace, macros for declaring entry 70 * point probes, macros for invoking them. 71 */ 72 #ifdef SDT_PROVIDER_DECLARE 73 SDT_PROVIDER_DECLARE(mac); /* MAC Framework-level events. */ 74 SDT_PROVIDER_DECLARE(mac_framework); /* Entry points to MAC. */ 75 76 #define MAC_CHECK_PROBE_DEFINE4(name, arg0, arg1, arg2, arg3) \ 77 SDT_PROBE_DEFINE5(mac_framework, kernel, name, mac_check_err, \ 78 mac-check-ok, "int", arg0, arg1, arg2, arg3); \ 79 SDT_PROBE_DEFINE5(mac_framework, kernel, name, mac_check_ok, \ 80 mac-check-ok, "int", arg0, arg1, arg2, arg3); 81 82 #define MAC_CHECK_PROBE_DEFINE3(name, arg0, arg1, arg2) \ 83 SDT_PROBE_DEFINE4(mac_framework, kernel, name, mac_check_err, \ 84 mac-check-err, "int", arg0, arg1, arg2); \ 85 SDT_PROBE_DEFINE4(mac_framework, kernel, name, mac_check_ok, \ 86 mac-check-ok, "int", arg0, arg1, arg2); 87 88 #define MAC_CHECK_PROBE_DEFINE2(name, arg0, arg1) \ 89 SDT_PROBE_DEFINE3(mac_framework, kernel, name, mac_check_err, \ 90 mac-check-err, "int", arg0, arg1); \ 91 SDT_PROBE_DEFINE3(mac_framework, kernel, name, mac_check_ok, \ 92 mac-check-ok, "int", arg0, arg1); 93 94 #define MAC_CHECK_PROBE_DEFINE1(name, arg0) \ 95 SDT_PROBE_DEFINE2(mac_framework, kernel, name, mac_check_err, \ 96 mac-check-err, "int", arg0); \ 97 SDT_PROBE_DEFINE2(mac_framework, kernel, name, mac_check_ok, \ 98 mac-check-ok, "int", arg0); 99 100 #define MAC_CHECK_PROBE4(name, error, arg0, arg1, arg2, arg3) do { \ 101 if (error) { \ 102 SDT_PROBE(mac_framework, kernel, name, mac_check_err, \ 103 error, arg0, arg1, arg2, arg3); \ 104 } else { \ 105 SDT_PROBE(mac_framework, kernel, name, mac_check_ok, \ 106 0, arg0, arg1, arg2, arg3); \ 107 } \ 108 } while (0) 109 110 #define MAC_CHECK_PROBE3(name, error, arg0, arg1, arg2) \ 111 MAC_CHECK_PROBE4(name, error, arg0, arg1, arg2, 0) 112 #define MAC_CHECK_PROBE2(name, error, arg0, arg1) \ 113 MAC_CHECK_PROBE3(name, error, arg0, arg1, 0) 114 #define MAC_CHECK_PROBE1(name, error, arg0) \ 115 MAC_CHECK_PROBE2(name, error, arg0, 0) 116 #endif 117 118 #define MAC_GRANT_PROBE_DEFINE2(name, arg0, arg1) \ 119 SDT_PROBE_DEFINE3(mac_framework, kernel, name, mac_grant_err, \ 120 mac-grant-err, "int", arg0, arg1); \ 121 SDT_PROBE_DEFINE3(mac_framework, kernel, name, mac_grant_ok, \ 122 mac-grant-ok, "INT", arg0, arg1); 123 124 #define MAC_GRANT_PROBE2(name, error, arg0, arg1) do { \ 125 if (error) { \ 126 SDT_PROBE(mac_framework, kernel, name, mac_grant_err, \ 127 error, arg0, arg1, 0, 0); \ 128 } else { \ 129 SDT_PROBE(mac_framework, kernel, name, mac_grant_ok, \ 130 error, arg0, arg1, 0, 0); \ 131 } \ 132 } while (0) 133 134 /* 135 * MAC Framework global types and typedefs. 136 */ 137 LIST_HEAD(mac_policy_list_head, mac_policy_conf); 138 #ifdef MALLOC_DECLARE 139 MALLOC_DECLARE(M_MACTEMP); 140 #endif 141 142 /* 143 * MAC labels -- in-kernel storage format. 144 * 145 * In general, struct label pointers are embedded in kernel data structures 146 * representing objects that may be labeled (and protected). Struct label is 147 * opaque to both kernel services that invoke the MAC Framework and MAC 148 * policy modules. In particular, we do not wish to encode the layout of the 149 * label structure into any ABIs. Historically, the slot array contained 150 * unions of {long, void} but now contains uintptr_t. 151 */ 152 #define MAC_MAX_SLOTS 4 153 #define MAC_FLAG_INITIALIZED 0x0000001 /* Is initialized for use. */ 154 struct label { 155 int l_flags; 156 intptr_t l_perpolicy[MAC_MAX_SLOTS]; 157 }; 158 159 160 /* 161 * Flags for mac_labeled, a bitmask of object types need across the union of 162 * all policies currently registered with the MAC Framework, used to key 163 * whether or not labels are allocated and constructors for the type are 164 * invoked. 165 */ 166 #define MPC_OBJECT_CRED 0x0000000000000001 167 #define MPC_OBJECT_PROC 0x0000000000000002 168 #define MPC_OBJECT_VNODE 0x0000000000000004 169 #define MPC_OBJECT_INPCB 0x0000000000000008 170 #define MPC_OBJECT_SOCKET 0x0000000000000010 171 #define MPC_OBJECT_DEVFS 0x0000000000000020 172 #define MPC_OBJECT_MBUF 0x0000000000000040 173 #define MPC_OBJECT_IPQ 0x0000000000000080 174 #define MPC_OBJECT_IFNET 0x0000000000000100 175 #define MPC_OBJECT_BPFDESC 0x0000000000000200 176 #define MPC_OBJECT_PIPE 0x0000000000000400 177 #define MPC_OBJECT_MOUNT 0x0000000000000800 178 #define MPC_OBJECT_POSIXSEM 0x0000000000001000 179 #define MPC_OBJECT_POSIXSHM 0x0000000000002000 180 #define MPC_OBJECT_SYSVMSG 0x0000000000004000 181 #define MPC_OBJECT_SYSVMSQ 0x0000000000008000 182 #define MPC_OBJECT_SYSVSEM 0x0000000000010000 183 #define MPC_OBJECT_SYSVSHM 0x0000000000020000 184 #define MPC_OBJECT_SYNCACHE 0x0000000000040000 185 #define MPC_OBJECT_IP6Q 0x0000000000080000 186 187 /* 188 * MAC Framework global variables. 189 */ 190 extern struct mac_policy_list_head mac_policy_list; 191 extern struct mac_policy_list_head mac_static_policy_list; 192 extern u_int mac_policy_count; 193 extern uint64_t mac_labeled; 194 extern struct mtx mac_ifnet_mtx; 195 196 /* 197 * MAC Framework infrastructure functions. 198 */ 199 int mac_error_select(int error1, int error2); 200 201 void mac_policy_slock_nosleep(struct rm_priotracker *tracker); 202 void mac_policy_slock_sleep(void); 203 void mac_policy_sunlock_nosleep(struct rm_priotracker *tracker); 204 void mac_policy_sunlock_sleep(void); 205 206 struct label *mac_labelzone_alloc(int flags); 207 void mac_labelzone_free(struct label *label); 208 void mac_labelzone_init(void); 209 210 void mac_init_label(struct label *label); 211 void mac_destroy_label(struct label *label); 212 int mac_check_structmac_consistent(struct mac *mac); 213 int mac_allocate_slot(void); 214 215 #define MAC_IFNET_LOCK(ifp) mtx_lock(&mac_ifnet_mtx) 216 #define MAC_IFNET_UNLOCK(ifp) mtx_unlock(&mac_ifnet_mtx) 217 218 /* 219 * MAC Framework per-object type functions. It's not yet clear how the 220 * namespaces, etc, should work for these, so for now, sort by object type. 221 */ 222 struct label *mac_cred_label_alloc(void); 223 void mac_cred_label_free(struct label *label); 224 struct label *mac_pipe_label_alloc(void); 225 void mac_pipe_label_free(struct label *label); 226 struct label *mac_socket_label_alloc(int flag); 227 void mac_socket_label_free(struct label *label); 228 struct label *mac_vnode_label_alloc(void); 229 void mac_vnode_label_free(struct label *label); 230 231 int mac_cred_check_relabel(struct ucred *cred, struct label *newlabel); 232 int mac_cred_externalize_label(struct label *label, char *elements, 233 char *outbuf, size_t outbuflen); 234 int mac_cred_internalize_label(struct label *label, char *string); 235 void mac_cred_relabel(struct ucred *cred, struct label *newlabel); 236 237 struct label *mac_mbuf_to_label(struct mbuf *m); 238 239 void mac_pipe_copy_label(struct label *src, struct label *dest); 240 int mac_pipe_externalize_label(struct label *label, char *elements, 241 char *outbuf, size_t outbuflen); 242 int mac_pipe_internalize_label(struct label *label, char *string); 243 244 int mac_socket_label_set(struct ucred *cred, struct socket *so, 245 struct label *label); 246 void mac_socket_copy_label(struct label *src, struct label *dest); 247 int mac_socket_externalize_label(struct label *label, char *elements, 248 char *outbuf, size_t outbuflen); 249 int mac_socket_internalize_label(struct label *label, char *string); 250 251 int mac_vnode_externalize_label(struct label *label, char *elements, 252 char *outbuf, size_t outbuflen); 253 int mac_vnode_internalize_label(struct label *label, char *string); 254 void mac_vnode_check_mmap_downgrade(struct ucred *cred, struct vnode *vp, 255 int *prot); 256 int vn_setlabel(struct vnode *vp, struct label *intlabel, 257 struct ucred *cred); 258 259 /* 260 * MAC Framework composition macros invoke all registered MAC policies for a 261 * specific entry point. They come in two forms: one which permits policies 262 * to sleep/block, and another that does not. 263 * 264 * MAC_POLICY_CHECK performs the designated check by walking the policy 265 * module list and checking with each as to how it feels about the request. 266 * Note that it returns its value via 'error' in the scope of the caller. 267 */ 268 #define MAC_POLICY_CHECK(check, args...) do { \ 269 struct mac_policy_conf *mpc; \ 270 \ 271 error = 0; \ 272 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 273 if (mpc->mpc_ops->mpo_ ## check != NULL) \ 274 error = mac_error_select( \ 275 mpc->mpc_ops->mpo_ ## check (args), \ 276 error); \ 277 } \ 278 if (!LIST_EMPTY(&mac_policy_list)) { \ 279 mac_policy_slock_sleep(); \ 280 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 281 if (mpc->mpc_ops->mpo_ ## check != NULL) \ 282 error = mac_error_select( \ 283 mpc->mpc_ops->mpo_ ## check (args), \ 284 error); \ 285 } \ 286 mac_policy_sunlock_sleep(); \ 287 } \ 288 } while (0) 289 290 #define MAC_POLICY_CHECK_NOSLEEP(check, args...) do { \ 291 struct mac_policy_conf *mpc; \ 292 \ 293 error = 0; \ 294 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 295 if (mpc->mpc_ops->mpo_ ## check != NULL) \ 296 error = mac_error_select( \ 297 mpc->mpc_ops->mpo_ ## check (args), \ 298 error); \ 299 } \ 300 if (!LIST_EMPTY(&mac_policy_list)) { \ 301 struct rm_priotracker tracker; \ 302 \ 303 mac_policy_slock_nosleep(&tracker); \ 304 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 305 if (mpc->mpc_ops->mpo_ ## check != NULL) \ 306 error = mac_error_select( \ 307 mpc->mpc_ops->mpo_ ## check (args), \ 308 error); \ 309 } \ 310 mac_policy_sunlock_nosleep(&tracker); \ 311 } \ 312 } while (0) 313 314 /* 315 * MAC_POLICY_GRANT performs the designated check by walking the policy 316 * module list and checking with each as to how it feels about the request. 317 * Unlike MAC_POLICY_CHECK, it grants if any policies return '0', and 318 * otherwise returns EPERM. Note that it returns its value via 'error' in 319 * the scope of the caller. 320 */ 321 #define MAC_POLICY_GRANT_NOSLEEP(check, args...) do { \ 322 struct mac_policy_conf *mpc; \ 323 \ 324 error = EPERM; \ 325 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 326 if (mpc->mpc_ops->mpo_ ## check != NULL) { \ 327 if (mpc->mpc_ops->mpo_ ## check(args) == 0) \ 328 error = 0; \ 329 } \ 330 } \ 331 if (!LIST_EMPTY(&mac_policy_list)) { \ 332 struct rm_priotracker tracker; \ 333 \ 334 mac_policy_slock_nosleep(&tracker); \ 335 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 336 if (mpc->mpc_ops->mpo_ ## check != NULL) { \ 337 if (mpc->mpc_ops->mpo_ ## check (args) \ 338 == 0) \ 339 error = 0; \ 340 } \ 341 } \ 342 mac_policy_sunlock_nosleep(&tracker); \ 343 } \ 344 } while (0) 345 346 /* 347 * MAC_POLICY_BOOLEAN performs the designated boolean composition by walking 348 * the module list, invoking each instance of the operation, and combining 349 * the results using the passed C operator. Note that it returns its value 350 * via 'result' in the scope of the caller, which should be initialized by 351 * the caller in a meaningful way to get a meaningful result. 352 */ 353 #define MAC_POLICY_BOOLEAN(operation, composition, args...) do { \ 354 struct mac_policy_conf *mpc; \ 355 \ 356 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 357 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 358 result = result composition \ 359 mpc->mpc_ops->mpo_ ## operation (args); \ 360 } \ 361 if (!LIST_EMPTY(&mac_policy_list)) { \ 362 mac_policy_slock_sleep(); \ 363 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 364 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 365 result = result composition \ 366 mpc->mpc_ops->mpo_ ## operation \ 367 (args); \ 368 } \ 369 mac_policy_sunlock_sleep(); \ 370 } \ 371 } while (0) 372 373 #define MAC_POLICY_BOOLEAN_NOSLEEP(operation, composition, args...) do {\ 374 struct mac_policy_conf *mpc; \ 375 \ 376 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 377 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 378 result = result composition \ 379 mpc->mpc_ops->mpo_ ## operation (args); \ 380 } \ 381 if (!LIST_EMPTY(&mac_policy_list)) { \ 382 struct rm_priotracker tracker; \ 383 \ 384 mac_policy_slock_nosleep(&tracker); \ 385 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 386 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 387 result = result composition \ 388 mpc->mpc_ops->mpo_ ## operation \ 389 (args); \ 390 } \ 391 mac_policy_sunlock_nosleep(&tracker); \ 392 } \ 393 } while (0) 394 395 /* 396 * MAC_POLICY_EXTERNALIZE queries each policy to see if it can generate an 397 * externalized version of a label element by name. Policies declare whether 398 * they have matched a particular element name, parsed from the string by 399 * MAC_POLICY_EXTERNALIZE, and an error is returned if any element is matched 400 * by no policy. 401 */ 402 #define MAC_POLICY_EXTERNALIZE(type, label, elementlist, outbuf, \ 403 outbuflen) do { \ 404 int claimed, first, ignorenotfound, savedlen; \ 405 char *element_name, *element_temp; \ 406 struct sbuf sb; \ 407 \ 408 error = 0; \ 409 first = 1; \ 410 sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN); \ 411 element_temp = elementlist; \ 412 while ((element_name = strsep(&element_temp, ",")) != NULL) { \ 413 if (element_name[0] == '?') { \ 414 element_name++; \ 415 ignorenotfound = 1; \ 416 } else \ 417 ignorenotfound = 0; \ 418 savedlen = sbuf_len(&sb); \ 419 if (first) \ 420 error = sbuf_printf(&sb, "%s/", element_name); \ 421 else \ 422 error = sbuf_printf(&sb, ",%s/", element_name); \ 423 if (error == -1) { \ 424 error = EINVAL; /* XXX: E2BIG? */ \ 425 break; \ 426 } \ 427 claimed = 0; \ 428 MAC_POLICY_CHECK(type ## _externalize_label, label, \ 429 element_name, &sb, &claimed); \ 430 if (error) \ 431 break; \ 432 if (claimed == 0 && ignorenotfound) { \ 433 /* Revert last label name. */ \ 434 sbuf_setpos(&sb, savedlen); \ 435 } else if (claimed != 1) { \ 436 error = EINVAL; /* XXX: ENOLABEL? */ \ 437 break; \ 438 } else { \ 439 first = 0; \ 440 } \ 441 } \ 442 sbuf_finish(&sb); \ 443 } while (0) 444 445 /* 446 * MAC_POLICY_INTERNALIZE presents parsed element names and data to each 447 * policy to see if any is willing to claim it and internalize the label 448 * data. If no policies match, an error is returned. 449 */ 450 #define MAC_POLICY_INTERNALIZE(type, label, instring) do { \ 451 char *element, *element_name, *element_data; \ 452 int claimed; \ 453 \ 454 error = 0; \ 455 element = instring; \ 456 while ((element_name = strsep(&element, ",")) != NULL) { \ 457 element_data = element_name; \ 458 element_name = strsep(&element_data, "/"); \ 459 if (element_data == NULL) { \ 460 error = EINVAL; \ 461 break; \ 462 } \ 463 claimed = 0; \ 464 MAC_POLICY_CHECK(type ## _internalize_label, label, \ 465 element_name, element_data, &claimed); \ 466 if (error) \ 467 break; \ 468 if (claimed != 1) { \ 469 /* XXXMAC: Another error here? */ \ 470 error = EINVAL; \ 471 break; \ 472 } \ 473 } \ 474 } while (0) 475 476 /* 477 * MAC_POLICY_PERFORM performs the designated operation by walking the policy 478 * module list and invoking that operation for each policy. 479 */ 480 #define MAC_POLICY_PERFORM(operation, args...) do { \ 481 struct mac_policy_conf *mpc; \ 482 \ 483 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 484 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 485 mpc->mpc_ops->mpo_ ## operation (args); \ 486 } \ 487 if (!LIST_EMPTY(&mac_policy_list)) { \ 488 mac_policy_slock_sleep(); \ 489 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 490 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 491 mpc->mpc_ops->mpo_ ## operation (args); \ 492 } \ 493 mac_policy_sunlock_sleep(); \ 494 } \ 495 } while (0) 496 497 #define MAC_POLICY_PERFORM_NOSLEEP(operation, args...) do { \ 498 struct mac_policy_conf *mpc; \ 499 \ 500 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ 501 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 502 mpc->mpc_ops->mpo_ ## operation (args); \ 503 } \ 504 if (!LIST_EMPTY(&mac_policy_list)) { \ 505 struct rm_priotracker tracker; \ 506 \ 507 mac_policy_slock_nosleep(&tracker); \ 508 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ 509 if (mpc->mpc_ops->mpo_ ## operation != NULL) \ 510 mpc->mpc_ops->mpo_ ## operation (args); \ 511 } \ 512 mac_policy_sunlock_nosleep(&tracker); \ 513 } \ 514 } while (0) 515 516 #endif /* !_SECURITY_MAC_MAC_INTERNAL_H_ */ 517