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