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