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