xref: /freebsd/sys/security/mac/mac_inet.c (revision 91c878a6935c5c2e99866eb267e5bc3028bf6d2f)
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/mac.h>
48 #include <sys/sbuf.h>
49 #include <sys/systm.h>
50 #include <sys/mount.h>
51 #include <sys/file.h>
52 #include <sys/namei.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/sysctl.h>
57 
58 #include <sys/mac_policy.h>
59 
60 #include <net/if.h>
61 #include <net/if_var.h>
62 
63 #include <netinet/in.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/ip_var.h>
66 
67 #include <security/mac/mac_framework.h>
68 #include <security/mac/mac_internal.h>
69 
70 static struct label *
71 mac_inpcb_label_alloc(int flag)
72 {
73 	struct label *label;
74 	int error;
75 
76 	label = mac_labelzone_alloc(flag);
77 	if (label == NULL)
78 		return (NULL);
79 	MAC_CHECK(init_inpcb_label, label, flag);
80 	if (error) {
81 		MAC_PERFORM(destroy_inpcb_label, label);
82 		mac_labelzone_free(label);
83 		return (NULL);
84 	}
85 	return (label);
86 }
87 
88 int
89 mac_init_inpcb(struct inpcb *inp, int flag)
90 {
91 
92 	inp->inp_label = mac_inpcb_label_alloc(flag);
93 	if (inp->inp_label == NULL)
94 		return (ENOMEM);
95 	return (0);
96 }
97 
98 static struct label *
99 mac_ipq_label_alloc(int flag)
100 {
101 	struct label *label;
102 	int error;
103 
104 	label = mac_labelzone_alloc(flag);
105 	if (label == NULL)
106 		return (NULL);
107 
108 	MAC_CHECK(init_ipq_label, label, flag);
109 	if (error) {
110 		MAC_PERFORM(destroy_ipq_label, label);
111 		mac_labelzone_free(label);
112 		return (NULL);
113 	}
114 	return (label);
115 }
116 
117 int
118 mac_init_ipq(struct ipq *ipq, int flag)
119 {
120 
121 	ipq->ipq_label = mac_ipq_label_alloc(flag);
122 	if (ipq->ipq_label == NULL)
123 		return (ENOMEM);
124 	return (0);
125 }
126 
127 static void
128 mac_inpcb_label_free(struct label *label)
129 {
130 
131 	MAC_PERFORM(destroy_inpcb_label, label);
132 	mac_labelzone_free(label);
133 }
134 
135 void
136 mac_destroy_inpcb(struct inpcb *inp)
137 {
138 
139 	mac_inpcb_label_free(inp->inp_label);
140 	inp->inp_label = NULL;
141 }
142 
143 static void
144 mac_ipq_label_free(struct label *label)
145 {
146 
147 	MAC_PERFORM(destroy_ipq_label, label);
148 	mac_labelzone_free(label);
149 }
150 
151 void
152 mac_destroy_ipq(struct ipq *ipq)
153 {
154 
155 	mac_ipq_label_free(ipq->ipq_label);
156 	ipq->ipq_label = NULL;
157 }
158 
159 void
160 mac_create_inpcb_from_socket(struct socket *so, struct inpcb *inp)
161 {
162 
163 	MAC_PERFORM(create_inpcb_from_socket, so, so->so_label, inp,
164 	    inp->inp_label);
165 }
166 
167 void
168 mac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram)
169 {
170 	struct label *label;
171 
172 	label = mac_mbuf_to_label(datagram);
173 
174 	MAC_PERFORM(create_datagram_from_ipq, ipq, ipq->ipq_label,
175 	    datagram, label);
176 }
177 
178 void
179 mac_create_fragment(struct mbuf *datagram, struct mbuf *fragment)
180 {
181 	struct label *datagramlabel, *fragmentlabel;
182 
183 	datagramlabel = mac_mbuf_to_label(datagram);
184 	fragmentlabel = mac_mbuf_to_label(fragment);
185 
186 	MAC_PERFORM(create_fragment, datagram, datagramlabel, fragment,
187 	    fragmentlabel);
188 }
189 
190 void
191 mac_create_ipq(struct mbuf *fragment, struct ipq *ipq)
192 {
193 	struct label *label;
194 
195 	label = mac_mbuf_to_label(fragment);
196 
197 	MAC_PERFORM(create_ipq, fragment, label, ipq, ipq->ipq_label);
198 }
199 
200 void
201 mac_create_mbuf_from_inpcb(struct inpcb *inp, struct mbuf *m)
202 {
203 	struct label *mlabel;
204 
205 	INP_LOCK_ASSERT(inp);
206 	mlabel = mac_mbuf_to_label(m);
207 
208 	MAC_PERFORM(create_mbuf_from_inpcb, inp, inp->inp_label, m, mlabel);
209 }
210 
211 int
212 mac_fragment_match(struct mbuf *fragment, struct ipq *ipq)
213 {
214 	struct label *label;
215 	int result;
216 
217 	label = mac_mbuf_to_label(fragment);
218 
219 	result = 1;
220 	MAC_BOOLEAN(fragment_match, &&, fragment, label, ipq,
221 	    ipq->ipq_label);
222 
223 	return (result);
224 }
225 
226 void
227 mac_reflect_mbuf_icmp(struct mbuf *m)
228 {
229 	struct label *label;
230 
231 	label = mac_mbuf_to_label(m);
232 
233 	MAC_PERFORM(reflect_mbuf_icmp, m, label);
234 }
235 void
236 mac_reflect_mbuf_tcp(struct mbuf *m)
237 {
238 	struct label *label;
239 
240 	label = mac_mbuf_to_label(m);
241 
242 	MAC_PERFORM(reflect_mbuf_tcp, m, label);
243 }
244 
245 void
246 mac_update_ipq(struct mbuf *fragment, struct ipq *ipq)
247 {
248 	struct label *label;
249 
250 	label = mac_mbuf_to_label(fragment);
251 
252 	MAC_PERFORM(update_ipq, fragment, label, ipq, ipq->ipq_label);
253 }
254 
255 int
256 mac_check_inpcb_deliver(struct inpcb *inp, struct mbuf *m)
257 {
258 	struct label *label;
259 	int error;
260 
261 	M_ASSERTPKTHDR(m);
262 
263 	if (!mac_enforce_socket)
264 		return (0);
265 
266 	label = mac_mbuf_to_label(m);
267 
268 	MAC_CHECK(check_inpcb_deliver, inp, inp->inp_label, m, label);
269 
270 	return (error);
271 }
272 
273 void
274 mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp)
275 {
276 
277 	/* XXX: assert socket lock. */
278 	INP_LOCK_ASSERT(inp);
279 	MAC_PERFORM(inpcb_sosetlabel, so, so->so_label, inp, inp->inp_label);
280 }
281 
282 void
283 mac_create_mbuf_from_firewall(struct mbuf *m)
284 {
285 	struct label *label;
286 
287 	M_ASSERTPKTHDR(m);
288 	label = mac_mbuf_to_label(m);
289 	MAC_PERFORM(create_mbuf_from_firewall, m, label);
290 }
291