Deleted Added
full compact
mac_inet.c (166531) mac_inet.c (168955)
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>
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: head/sys/security/mac/mac_inet.c 166531 2007-02-06 10:59:23Z rwatson $");
38__FBSDID("$FreeBSD: head/sys/security/mac/mac_inet.c 168955 2007-04-22 19:55:56Z rwatson $");
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
68static struct label *
69mac_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
86int
87mac_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
96static struct label *
97mac_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
115int
116mac_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
125static void
126mac_inpcb_label_free(struct label *label)
127{
128
129 MAC_PERFORM(destroy_inpcb_label, label);
130 mac_labelzone_free(label);
131}
132
133void
134mac_destroy_inpcb(struct inpcb *inp)
135{
136
137 mac_inpcb_label_free(inp->inp_label);
138 inp->inp_label = NULL;
139}
140
141static void
142mac_ipq_label_free(struct label *label)
143{
144
145 MAC_PERFORM(destroy_ipq_label, label);
146 mac_labelzone_free(label);
147}
148
149void
150mac_destroy_ipq(struct ipq *ipq)
151{
152
153 mac_ipq_label_free(ipq->ipq_label);
154 ipq->ipq_label = NULL;
155}
156
157void
158mac_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
165void
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
68static struct label *
69mac_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
86int
87mac_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
96static struct label *
97mac_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
115int
116mac_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
125static void
126mac_inpcb_label_free(struct label *label)
127{
128
129 MAC_PERFORM(destroy_inpcb_label, label);
130 mac_labelzone_free(label);
131}
132
133void
134mac_destroy_inpcb(struct inpcb *inp)
135{
136
137 mac_inpcb_label_free(inp->inp_label);
138 inp->inp_label = NULL;
139}
140
141static void
142mac_ipq_label_free(struct label *label)
143{
144
145 MAC_PERFORM(destroy_ipq_label, label);
146 mac_labelzone_free(label);
147}
148
149void
150mac_destroy_ipq(struct ipq *ipq)
151{
152
153 mac_ipq_label_free(ipq->ipq_label);
154 ipq->ipq_label = NULL;
155}
156
157void
158mac_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
165void
166mac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram)
166mac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *m)
167{
168 struct label *label;
169
167{
168 struct label *label;
169
170 label = mac_mbuf_to_label(datagram);
170 label = mac_mbuf_to_label(m);
171
171
172 MAC_PERFORM(create_datagram_from_ipq, ipq, ipq->ipq_label,
173 datagram, label);
172 MAC_PERFORM(create_datagram_from_ipq, ipq, ipq->ipq_label, m, label);
174}
175
176void
173}
174
175void
177mac_create_fragment(struct mbuf *datagram, struct mbuf *fragment)
176mac_create_fragment(struct mbuf *m, struct mbuf *frag)
178{
177{
179 struct label *datagramlabel, *fragmentlabel;
178 struct label *mlabel, *fraglabel;
180
179
181 datagramlabel = mac_mbuf_to_label(datagram);
182 fragmentlabel = mac_mbuf_to_label(fragment);
180 mlabel = mac_mbuf_to_label(m);
181 fraglabel = mac_mbuf_to_label(frag);
183
182
184 MAC_PERFORM(create_fragment, datagram, datagramlabel, fragment,
185 fragmentlabel);
183 MAC_PERFORM(create_fragment, m, mlabel, frag, fraglabel);
186}
187
188void
184}
185
186void
189mac_create_ipq(struct mbuf *fragment, struct ipq *ipq)
187mac_create_ipq(struct mbuf *m, struct ipq *ipq)
190{
191 struct label *label;
192
188{
189 struct label *label;
190
193 label = mac_mbuf_to_label(fragment);
191 label = mac_mbuf_to_label(m);
194
192
195 MAC_PERFORM(create_ipq, fragment, label, ipq, ipq->ipq_label);
193 MAC_PERFORM(create_ipq, m, label, ipq, ipq->ipq_label);
196}
197
198void
199mac_create_mbuf_from_inpcb(struct inpcb *inp, struct mbuf *m)
200{
201 struct label *mlabel;
202
203 INP_LOCK_ASSERT(inp);
204 mlabel = mac_mbuf_to_label(m);
205
206 MAC_PERFORM(create_mbuf_from_inpcb, inp, inp->inp_label, m, mlabel);
207}
208
209int
194}
195
196void
197mac_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
207int
210mac_fragment_match(struct mbuf *fragment, struct ipq *ipq)
208mac_fragment_match(struct mbuf *m, struct ipq *ipq)
211{
212 struct label *label;
213 int result;
214
209{
210 struct label *label;
211 int result;
212
215 label = mac_mbuf_to_label(fragment);
213 label = mac_mbuf_to_label(m);
216
217 result = 1;
214
215 result = 1;
218 MAC_BOOLEAN(fragment_match, &&, fragment, label, ipq,
219 ipq->ipq_label);
216 MAC_BOOLEAN(fragment_match, &&, m, label, ipq, ipq->ipq_label);
220
221 return (result);
222}
223
224void
225mac_reflect_mbuf_icmp(struct mbuf *m)
226{
227 struct label *label;
228
229 label = mac_mbuf_to_label(m);
230
231 MAC_PERFORM(reflect_mbuf_icmp, m, label);
232}
217
218 return (result);
219}
220
221void
222mac_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
233void
234mac_reflect_mbuf_tcp(struct mbuf *m)
235{
236 struct label *label;
237
238 label = mac_mbuf_to_label(m);
239
240 MAC_PERFORM(reflect_mbuf_tcp, m, label);
241}
242
243void
231void
232mac_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
241void
244mac_update_ipq(struct mbuf *fragment, struct ipq *ipq)
242mac_update_ipq(struct mbuf *m, struct ipq *ipq)
245{
246 struct label *label;
247
243{
244 struct label *label;
245
248 label = mac_mbuf_to_label(fragment);
246 label = mac_mbuf_to_label(m);
249
247
250 MAC_PERFORM(update_ipq, fragment, label, ipq, ipq->ipq_label);
248 MAC_PERFORM(update_ipq, m, label, ipq, ipq->ipq_label);
251}
252
253int
254mac_check_inpcb_deliver(struct inpcb *inp, struct mbuf *m)
255{
256 struct label *label;
257 int error;
258
259 M_ASSERTPKTHDR(m);
260
261 label = mac_mbuf_to_label(m);
262
263 MAC_CHECK(check_inpcb_deliver, inp, inp->inp_label, m, label);
264
265 return (error);
266}
267
268void
269mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp)
270{
271
272 INP_LOCK_ASSERT(inp);
273 SOCK_LOCK_ASSERT(so);
274 MAC_PERFORM(inpcb_sosetlabel, so, so->so_label, inp, inp->inp_label);
275}
276
277void
278mac_create_mbuf_from_firewall(struct mbuf *m)
279{
280 struct label *label;
281
282 M_ASSERTPKTHDR(m);
283 label = mac_mbuf_to_label(m);
284 MAC_PERFORM(create_mbuf_from_firewall, m, label);
285}
286
287/*
288 * These functions really should be referencing the syncache structure
289 * instead of the label. However, due to some of the complexities associated
290 * with exposing this syncache structure we operate directly on it's label
291 * pointer. This should be OK since we aren't making any access control
292 * decisions within this code directly, we are merely allocating and copying
293 * label storage so we can properly initialize mbuf labels for any packets
294 * the syncache code might create.
295 */
296void
297mac_destroy_syncache(struct label **label)
298{
299
300 MAC_PERFORM(destroy_syncache_label, *label);
301 mac_labelzone_free(*label);
302 *label = NULL;
303}
304
305int
306mac_init_syncache(struct label **label)
307{
308 int error;
309
310 *label = mac_labelzone_alloc(M_NOWAIT);
311 if (*label == NULL)
312 return (ENOMEM);
313 /*
314 * Since we are holding the inpcb locks the policy can not allocate
315 * policy specific label storage using M_WAITOK. So we need to do a
316 * MAC_CHECK instead of the typical MAC_PERFORM so we can propagate
317 * allocation failures back to the syncache code.
318 */
319 MAC_CHECK(init_syncache_label, *label, M_NOWAIT);
320 return (error);
321}
322
323void
324mac_init_syncache_from_inpcb(struct label *label, struct inpcb *inp)
325{
326
327 INP_LOCK_ASSERT(inp);
328 MAC_PERFORM(init_syncache_from_inpcb, label, inp);
329}
330
331void
332mac_create_mbuf_from_syncache(struct label *sc_label, struct mbuf *m)
333{
249}
250
251int
252mac_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
266void
267mac_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
275void
276mac_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 */
294void
295mac_destroy_syncache(struct label **label)
296{
297
298 MAC_PERFORM(destroy_syncache_label, *label);
299 mac_labelzone_free(*label);
300 *label = NULL;
301}
302
303int
304mac_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
321void
322mac_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
329void
330mac_create_mbuf_from_syncache(struct label *sc_label, struct mbuf *m)
331{
334 struct label *mbuf_label;
332 struct label *mlabel;
335
336 M_ASSERTPKTHDR(m);
333
334 M_ASSERTPKTHDR(m);
337 mbuf_label = mac_mbuf_to_label(m);
338 MAC_PERFORM(create_mbuf_from_syncache, sc_label, m, mbuf_label);
335 mlabel = mac_mbuf_to_label(m);
336 MAC_PERFORM(create_mbuf_from_syncache, sc_label, m, mlabel);
339}
337}