]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/security/mac/mac_inet.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / security / mac / mac_inet.c
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 *q, int flag)
117 {
118
119         q->ipq_label = mac_ipq_label_alloc(flag);
120         if (q->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 *q)
151 {
152
153         mac_ipq_label_free(q->ipq_label);
154         q->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 *q, struct mbuf *m)
167 {
168         struct label *label;
169
170         label = mac_mbuf_to_label(m);
171
172         MAC_PERFORM(create_datagram_from_ipq, q, q->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 *q)
188 {
189         struct label *label;
190
191         label = mac_mbuf_to_label(m);
192
193         MAC_PERFORM(create_ipq, m, label, q, q->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 *q)
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, q, q->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 *q)
243 {
244         struct label *label;
245
246         label = mac_mbuf_to_label(m);
247
248         MAC_PERFORM(update_ipq, m, label, q, q->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 int
267 mac_check_inpcb_visible(struct ucred *cred, struct inpcb *inp)
268 {
269         int error;
270
271         INP_LOCK_ASSERT(inp);
272
273         MAC_CHECK(check_inpcb_visible, cred, inp, inp->inp_label);
274
275         return (error);
276 }
277
278 void
279 mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp)
280 {
281
282         INP_WLOCK_ASSERT(inp);
283         SOCK_LOCK_ASSERT(so);
284         MAC_PERFORM(inpcb_sosetlabel, so, so->so_label, inp, inp->inp_label);
285 }
286
287 void
288 mac_create_mbuf_from_firewall(struct mbuf *m)
289 {
290         struct label *label;
291
292         M_ASSERTPKTHDR(m);
293         label = mac_mbuf_to_label(m);
294         MAC_PERFORM(create_mbuf_from_firewall, m, label);
295 }
296
297 /*
298  * These functions really should be referencing the syncache structure
299  * instead of the label.  However, due to some of the complexities associated
300  * with exposing this syncache structure we operate directly on it's label
301  * pointer.  This should be OK since we aren't making any access control
302  * decisions within this code directly, we are merely allocating and copying
303  * label storage so we can properly initialize mbuf labels for any packets
304  * the syncache code might create.
305  */
306 void
307 mac_destroy_syncache(struct label **label)
308 {
309
310         MAC_PERFORM(destroy_syncache_label, *label);
311         mac_labelzone_free(*label);
312         *label = NULL;
313 }
314
315 int
316 mac_init_syncache(struct label **label)
317 {
318         int error;
319
320         *label = mac_labelzone_alloc(M_NOWAIT);
321         if (*label == NULL)
322                 return (ENOMEM);
323         /*
324          * Since we are holding the inpcb locks the policy can not allocate
325          * policy specific label storage using M_WAITOK.  So we need to do a
326          * MAC_CHECK instead of the typical MAC_PERFORM so we can propagate
327          * allocation failures back to the syncache code.
328          */
329         MAC_CHECK(init_syncache_label, *label, M_NOWAIT);
330         if (error) {
331                 MAC_PERFORM(destroy_syncache_label, *label);
332                 mac_labelzone_free(*label);
333         }
334         return (error);
335 }
336
337 void
338 mac_init_syncache_from_inpcb(struct label *label, struct inpcb *inp)
339 {
340
341         INP_WLOCK_ASSERT(inp);
342         MAC_PERFORM(init_syncache_from_inpcb, label, inp);
343 }
344
345 void
346 mac_create_mbuf_from_syncache(struct label *sc_label, struct mbuf *m)
347 {
348         struct label *mlabel;
349
350         M_ASSERTPKTHDR(m);
351         mlabel = mac_mbuf_to_label(m);
352         MAC_PERFORM(create_mbuf_from_syncache, sc_label, m, mlabel);
353 }