mac_inet.c revision 165149
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 165149 2006-12-13 06:00:57Z csjp $");
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
70static struct label *
71mac_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
88int
89mac_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
98static struct label *
99mac_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
117int
118mac_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
127static void
128mac_inpcb_label_free(struct label *label)
129{
130
131	MAC_PERFORM(destroy_inpcb_label, label);
132	mac_labelzone_free(label);
133}
134
135void
136mac_destroy_inpcb(struct inpcb *inp)
137{
138
139	mac_inpcb_label_free(inp->inp_label);
140	inp->inp_label = NULL;
141}
142
143static void
144mac_ipq_label_free(struct label *label)
145{
146
147	MAC_PERFORM(destroy_ipq_label, label);
148	mac_labelzone_free(label);
149}
150
151void
152mac_destroy_ipq(struct ipq *ipq)
153{
154
155	mac_ipq_label_free(ipq->ipq_label);
156	ipq->ipq_label = NULL;
157}
158
159void
160mac_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
167void
168mac_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
178void
179mac_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
190void
191mac_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
200void
201mac_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
211int
212mac_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
226void
227mac_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}
235void
236mac_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
245void
246mac_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
255int
256mac_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
273void
274mac_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
282void
283mac_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
292/*
293 * These functions really should be referencing the syncache structure instead
294 * of the label.  However, due to some of the complexities associated with
295 * exposing this syncache structure we operate directly on it's label pointer.
296 * This should be OK since we aren't making any access control decisions within
297 * this code directly, we are merely allocating and copying label storage so
298 * we can properly initialize mbuf labels for any packets the syncache code
299 * might create.
300 */
301void
302mac_destroy_syncache(struct label **label)
303{
304
305	MAC_PERFORM(destroy_syncache_label, *label);
306	mac_labelzone_free(*label);
307	*label = NULL;
308}
309
310int
311mac_init_syncache(struct label **label)
312{
313	int error;
314
315	*label = mac_labelzone_alloc(M_NOWAIT);
316	if (*label == NULL)
317		return (ENOMEM);
318	/*
319	 * Since we are holding the inpcb locks the policy can not allocate
320	 * policy specific label storage using M_WAITOK.  So we need to do a
321	 * MAC_CHECK instead of the typical MAC_PERFORM so we can propagate
322	 * allocation failures back to the syncache code.
323	 */
324	MAC_CHECK(init_syncache_label, *label, M_NOWAIT);
325	return (error);
326}
327
328void
329mac_init_syncache_from_inpcb(struct label *label, struct inpcb *inp)
330{
331
332	INP_LOCK_ASSERT(inp);
333	MAC_PERFORM(init_syncache_from_inpcb, label, inp);
334}
335
336void
337mac_create_mbuf_from_syncache(struct label *sc_label, struct mbuf *m)
338{
339	struct label *mbuf_label;
340
341	M_ASSERTPKTHDR(m);
342	mbuf_label = mac_mbuf_to_label(m);
343	MAC_PERFORM(create_mbuf_from_syncache, sc_label, m, mbuf_label);
344}
345