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 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_mac.h" 41 42 #include <sys/param.h> 43 #include <sys/kernel.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/mutex.h> 47 #include <sys/sbuf.h> 48 #include <sys/systm.h> 49 #include <sys/mount.h> 50 #include <sys/file.h> 51 #include <sys/namei.h> 52 #include <sys/protosw.h> 53 #include <sys/socket.h> 54 #include <sys/socketvar.h> 55 #include <sys/sysctl.h> 56 57 #include <net/if.h> 58 #include <net/if_var.h> 59 60 #include <netinet/in.h> 61 #include <netinet/in_pcb.h> 62 #include <netinet/ip_var.h> 63 64 #include <security/mac/mac_framework.h> 65 #include <security/mac/mac_internal.h> 66 #include <security/mac/mac_policy.h> 67 68 static struct label * 69 mac_inpcb_label_alloc(int flag) 70 { 71 struct label *label; 72 int error; 73 74 label = mac_labelzone_alloc(flag); 75 if (label == NULL) 76 return (NULL); 77 MAC_CHECK(init_inpcb_label, label, flag); 78 if (error) { 79 MAC_PERFORM(destroy_inpcb_label, label); 80 mac_labelzone_free(label); 81 return (NULL); 82 } 83 return (label); 84 } 85 86 int 87 mac_init_inpcb(struct inpcb *inp, int flag) 88 { 89 90 inp->inp_label = mac_inpcb_label_alloc(flag); 91 if (inp->inp_label == NULL) 92 return (ENOMEM); 93 return (0); 94 } 95 96 static struct label * 97 mac_ipq_label_alloc(int flag) 98 { 99 struct label *label; 100 int error; 101 102 label = mac_labelzone_alloc(flag); 103 if (label == NULL) 104 return (NULL); 105 106 MAC_CHECK(init_ipq_label, label, flag); 107 if (error) { 108 MAC_PERFORM(destroy_ipq_label, label); 109 mac_labelzone_free(label); 110 return (NULL); 111 } 112 return (label); 113 } 114 115 int 116 mac_init_ipq(struct ipq *ipq, int flag) 117 { 118 119 ipq->ipq_label = mac_ipq_label_alloc(flag); 120 if (ipq->ipq_label == NULL) 121 return (ENOMEM); 122 return (0); 123 } 124 125 static void 126 mac_inpcb_label_free(struct label *label) 127 { 128 129 MAC_PERFORM(destroy_inpcb_label, label); 130 mac_labelzone_free(label); 131 } 132 133 void 134 mac_destroy_inpcb(struct inpcb *inp) 135 { 136 137 mac_inpcb_label_free(inp->inp_label); 138 inp->inp_label = NULL; 139 } 140 141 static void 142 mac_ipq_label_free(struct label *label) 143 { 144 145 MAC_PERFORM(destroy_ipq_label, label); 146 mac_labelzone_free(label); 147 } 148 149 void 150 mac_destroy_ipq(struct ipq *ipq) 151 { 152 153 mac_ipq_label_free(ipq->ipq_label); 154 ipq->ipq_label = NULL; 155 } 156 157 void 158 mac_create_inpcb_from_socket(struct socket *so, struct inpcb *inp) 159 { 160 161 MAC_PERFORM(create_inpcb_from_socket, so, so->so_label, inp, 162 inp->inp_label); 163 } 164 165 void 166 mac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *m) 167 { 168 struct label *label; 169 170 label = mac_mbuf_to_label(m); 171 172 MAC_PERFORM(create_datagram_from_ipq, ipq, ipq->ipq_label, m, label); 173 } 174 175 void 176 mac_create_fragment(struct mbuf *m, struct mbuf *frag) 177 { 178 struct label *mlabel, *fraglabel; 179 180 mlabel = mac_mbuf_to_label(m); 181 fraglabel = mac_mbuf_to_label(frag); 182 183 MAC_PERFORM(create_fragment, m, mlabel, frag, fraglabel); 184 } 185 186 void 187 mac_create_ipq(struct mbuf *m, struct ipq *ipq) 188 { 189 struct label *label; 190 191 label = mac_mbuf_to_label(m); 192 193 MAC_PERFORM(create_ipq, m, label, ipq, ipq->ipq_label); 194 } 195 196 void 197 mac_create_mbuf_from_inpcb(struct inpcb *inp, struct mbuf *m) 198 { 199 struct label *mlabel; 200 201 INP_LOCK_ASSERT(inp); 202 mlabel = mac_mbuf_to_label(m); 203 204 MAC_PERFORM(create_mbuf_from_inpcb, inp, inp->inp_label, m, mlabel); 205 } 206 207 int 208 mac_fragment_match(struct mbuf *m, struct ipq *ipq) 209 { 210 struct label *label; 211 int result; 212 213 label = mac_mbuf_to_label(m); 214 215 result = 1; 216 MAC_BOOLEAN(fragment_match, &&, m, label, ipq, ipq->ipq_label); 217 218 return (result); 219 } 220 221 void 222 mac_reflect_mbuf_icmp(struct mbuf *m) 223 { 224 struct label *label; 225 226 label = mac_mbuf_to_label(m); 227 228 MAC_PERFORM(reflect_mbuf_icmp, m, label); 229 } 230 231 void 232 mac_reflect_mbuf_tcp(struct mbuf *m) 233 { 234 struct label *label; 235 236 label = mac_mbuf_to_label(m); 237 238 MAC_PERFORM(reflect_mbuf_tcp, m, label); 239 } 240 241 void 242 mac_update_ipq(struct mbuf *m, struct ipq *ipq) 243 { 244 struct label *label; 245 246 label = mac_mbuf_to_label(m); 247 248 MAC_PERFORM(update_ipq, m, label, ipq, ipq->ipq_label); 249 } 250 251 int 252 mac_check_inpcb_deliver(struct inpcb *inp, struct mbuf *m) 253 { 254 struct label *label; 255 int error; 256 257 M_ASSERTPKTHDR(m); 258 259 label = mac_mbuf_to_label(m); 260 261 MAC_CHECK(check_inpcb_deliver, inp, inp->inp_label, m, label); 262 263 return (error); 264 } 265 266 void 267 mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp) 268 { 269 270 INP_LOCK_ASSERT(inp); 271 SOCK_LOCK_ASSERT(so); 272 MAC_PERFORM(inpcb_sosetlabel, so, so->so_label, inp, inp->inp_label); 273 } 274 275 void 276 mac_create_mbuf_from_firewall(struct mbuf *m) 277 { 278 struct label *label; 279 280 M_ASSERTPKTHDR(m); 281 label = mac_mbuf_to_label(m); 282 MAC_PERFORM(create_mbuf_from_firewall, m, label); 283 } 284 285 /* 286 * These functions really should be referencing the syncache structure 287 * instead of the label. However, due to some of the complexities associated 288 * with exposing this syncache structure we operate directly on it's label 289 * pointer. This should be OK since we aren't making any access control 290 * decisions within this code directly, we are merely allocating and copying 291 * label storage so we can properly initialize mbuf labels for any packets 292 * the syncache code might create. 293 */ 294 void 295 mac_destroy_syncache(struct label **label) 296 { 297 298 MAC_PERFORM(destroy_syncache_label, *label); 299 mac_labelzone_free(*label); 300 *label = NULL; 301 } 302 303 int 304 mac_init_syncache(struct label **label) 305 { 306 int error; 307 308 *label = mac_labelzone_alloc(M_NOWAIT); 309 if (*label == NULL) 310 return (ENOMEM); 311 /* 312 * Since we are holding the inpcb locks the policy can not allocate 313 * policy specific label storage using M_WAITOK. So we need to do a 314 * MAC_CHECK instead of the typical MAC_PERFORM so we can propagate 315 * allocation failures back to the syncache code. 316 */ 317 MAC_CHECK(init_syncache_label, *label, M_NOWAIT); 318 return (error); 319 } 320 321 void 322 mac_init_syncache_from_inpcb(struct label *label, struct inpcb *inp) 323 { 324 325 INP_LOCK_ASSERT(inp); 326 MAC_PERFORM(init_syncache_from_inpcb, label, inp); 327 } 328 329 void 330 mac_create_mbuf_from_syncache(struct label *sc_label, struct mbuf *m) 331 { 332 struct label *mlabel; 333 334 M_ASSERTPKTHDR(m); 335 mlabel = mac_mbuf_to_label(m); 336 MAC_PERFORM(create_mbuf_from_syncache, sc_label, m, mlabel); 337 } 338