]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/security/mac/mac_net.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / security / mac / mac_net.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  * Copyright (c) 2006 SPARTA, Inc.
6  * Copyright (c) 2008 Apple Inc.
7  * All rights reserved.
8  *
9  * This software was developed by Robert Watson and Ilmar Habibulin for the
10  * TrustedBSD Project.
11  *
12  * This software was enhanced by SPARTA ISSO under SPAWAR contract
13  * N66001-04-C-6019 ("SEFOS").
14  *
15  * This software was developed for the FreeBSD Project in part by Network
16  * Associates Laboratories, the Security Research Division of Network
17  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
18  * as part of the DARPA CHATS research program.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  * 2. Redistributions in binary form must reproduce the above copyright
26  *    notice, this list of conditions and the following disclaimer in the
27  *    documentation and/or other materials provided with the distribution.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  */
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include "opt_mac.h"
46
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/malloc.h>
51 #include <sys/mutex.h>
52 #include <sys/mac.h>
53 #include <sys/priv.h>
54 #include <sys/sbuf.h>
55 #include <sys/systm.h>
56 #include <sys/mount.h>
57 #include <sys/file.h>
58 #include <sys/namei.h>
59 #include <sys/protosw.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/sysctl.h>
63
64 #include <net/bpfdesc.h>
65 #include <net/if.h>
66 #include <net/if_var.h>
67
68 #include <security/mac/mac_framework.h>
69 #include <security/mac/mac_internal.h>
70 #include <security/mac/mac_policy.h>
71
72 /*
73  * XXXRW: struct ifnet locking is incomplete in the network code, so we use
74  * our own global mutex for struct ifnet.  Non-ideal, but should help in the
75  * SMP environment.
76  */
77 struct mtx mac_ifnet_mtx;
78 MTX_SYSINIT(mac_ifnet_mtx, &mac_ifnet_mtx, "mac_ifnet", MTX_DEF);
79
80 /*
81  * Retrieve the label associated with an mbuf by searching for the tag.
82  * Depending on the value of mac_labelmbufs, it's possible that a label will
83  * not be present, in which case NULL is returned.  Policies must handle the
84  * possibility of an mbuf not having label storage if they do not enforce
85  * early loading.
86  */
87 struct label *
88 mac_mbuf_to_label(struct mbuf *m)
89 {
90         struct m_tag *tag;
91         struct label *label;
92
93         if (m == NULL)
94                 return (NULL);
95         tag = m_tag_find(m, PACKET_TAG_MACLABEL, NULL);
96         if (tag == NULL)
97                 return (NULL);
98         label = (struct label *)(tag+1);
99         return (label);
100 }
101
102 static struct label *
103 mac_bpfdesc_label_alloc(void)
104 {
105         struct label *label;
106
107         label = mac_labelzone_alloc(M_WAITOK);
108         MAC_PERFORM(bpfdesc_init_label, label);
109         return (label);
110 }
111
112 void
113 mac_bpfdesc_init(struct bpf_d *d)
114 {
115
116         if (mac_labeled & MPC_OBJECT_BPFDESC)
117                 d->bd_label = mac_bpfdesc_label_alloc();
118         else
119                 d->bd_label = NULL;
120 }
121
122 static struct label *
123 mac_ifnet_label_alloc(void)
124 {
125         struct label *label;
126
127         label = mac_labelzone_alloc(M_WAITOK);
128         MAC_PERFORM(ifnet_init_label, label);
129         return (label);
130 }
131
132 void
133 mac_ifnet_init(struct ifnet *ifp)
134 {
135
136         if (mac_labeled & MPC_OBJECT_IFNET)
137                 ifp->if_label = mac_ifnet_label_alloc();
138         else
139                 ifp->if_label = NULL;
140 }
141
142 int
143 mac_mbuf_tag_init(struct m_tag *tag, int flag)
144 {
145         struct label *label;
146         int error;
147
148         label = (struct label *) (tag + 1);
149         mac_init_label(label);
150
151         MAC_CHECK(mbuf_init_label, label, flag);
152         if (error) {
153                 MAC_PERFORM(mbuf_destroy_label, label);
154                 mac_destroy_label(label);
155         }
156         return (error);
157 }
158
159 int
160 mac_mbuf_init(struct mbuf *m, int flag)
161 {
162         struct m_tag *tag;
163         int error;
164
165         M_ASSERTPKTHDR(m);
166
167         if (mac_labeled & MPC_OBJECT_MBUF) {
168                 tag = m_tag_get(PACKET_TAG_MACLABEL, sizeof(struct label),
169                     flag);
170                 if (tag == NULL)
171                         return (ENOMEM);
172                 error = mac_mbuf_tag_init(tag, flag);
173                 if (error) {
174                         m_tag_free(tag);
175                         return (error);
176                 }
177                 m_tag_prepend(m, tag);
178         }
179         return (0);
180 }
181
182 static void
183 mac_bpfdesc_label_free(struct label *label)
184 {
185
186         MAC_PERFORM(bpfdesc_destroy_label, label);
187         mac_labelzone_free(label);
188 }
189
190 void
191 mac_bpfdesc_destroy(struct bpf_d *d)
192 {
193
194         if (d->bd_label != NULL) {
195                 mac_bpfdesc_label_free(d->bd_label);
196                 d->bd_label = NULL;
197         }
198 }
199
200 static void
201 mac_ifnet_label_free(struct label *label)
202 {
203
204         MAC_PERFORM(ifnet_destroy_label, label);
205         mac_labelzone_free(label);
206 }
207
208 void
209 mac_ifnet_destroy(struct ifnet *ifp)
210 {
211
212         if (ifp->if_label != NULL) {
213                 mac_ifnet_label_free(ifp->if_label);
214                 ifp->if_label = NULL;
215         }
216 }
217
218 void
219 mac_mbuf_tag_destroy(struct m_tag *tag)
220 {
221         struct label *label;
222
223         label = (struct label *)(tag+1);
224
225         MAC_PERFORM(mbuf_destroy_label, label);
226         mac_destroy_label(label);
227 }
228
229 /*
230  * mac_mbuf_tag_copy is called when an mbuf header is duplicated, in which
231  * case the labels must also be duplicated.
232  */
233 void
234 mac_mbuf_tag_copy(struct m_tag *src, struct m_tag *dest)
235 {
236         struct label *src_label, *dest_label;
237
238         src_label = (struct label *)(src+1);
239         dest_label = (struct label *)(dest+1);
240
241         /*
242          * mac_mbuf_tag_init() is called on the target tag in m_tag_copy(),
243          * so we don't need to call it here.
244          */
245         MAC_PERFORM(mbuf_copy_label, src_label, dest_label);
246 }
247
248 void
249 mac_mbuf_copy(struct mbuf *m_from, struct mbuf *m_to)
250 {
251         struct label *src_label, *dest_label;
252
253         src_label = mac_mbuf_to_label(m_from);
254         dest_label = mac_mbuf_to_label(m_to);
255
256         MAC_PERFORM(mbuf_copy_label, src_label, dest_label);
257 }
258
259 static void
260 mac_ifnet_copy_label(struct label *src, struct label *dest)
261 {
262
263         MAC_PERFORM(ifnet_copy_label, src, dest);
264 }
265
266 static int
267 mac_ifnet_externalize_label(struct label *label, char *elements,
268     char *outbuf, size_t outbuflen)
269 {
270         int error;
271
272         MAC_EXTERNALIZE(ifnet, label, elements, outbuf, outbuflen);
273
274         return (error);
275 }
276
277 static int
278 mac_ifnet_internalize_label(struct label *label, char *string)
279 {
280         int error;
281
282         MAC_INTERNALIZE(ifnet, label, string);
283
284         return (error);
285 }
286
287 void
288 mac_ifnet_create(struct ifnet *ifp)
289 {
290
291         MAC_IFNET_LOCK(ifp);
292         MAC_PERFORM(ifnet_create, ifp, ifp->if_label);
293         MAC_IFNET_UNLOCK(ifp);
294 }
295
296 void
297 mac_bpfdesc_create(struct ucred *cred, struct bpf_d *d)
298 {
299
300         MAC_PERFORM(bpfdesc_create, cred, d, d->bd_label);
301 }
302
303 void
304 mac_bpfdesc_create_mbuf(struct bpf_d *d, struct mbuf *m)
305 {
306         struct label *label;
307
308         BPFD_LOCK_ASSERT(d);
309
310         label = mac_mbuf_to_label(m);
311
312         MAC_PERFORM(bpfdesc_create_mbuf, d, d->bd_label, m, label);
313 }
314
315 void
316 mac_ifnet_create_mbuf(struct ifnet *ifp, struct mbuf *m)
317 {
318         struct label *label;
319
320         label = mac_mbuf_to_label(m);
321
322         MAC_IFNET_LOCK(ifp);
323         MAC_PERFORM(ifnet_create_mbuf, ifp, ifp->if_label, m, label);
324         MAC_IFNET_UNLOCK(ifp);
325 }
326
327 int
328 mac_bpfdesc_check_receive(struct bpf_d *d, struct ifnet *ifp)
329 {
330         int error;
331
332         BPFD_LOCK_ASSERT(d);
333
334         MAC_IFNET_LOCK(ifp);
335         MAC_CHECK(bpfdesc_check_receive, d, d->bd_label, ifp, ifp->if_label);
336         MAC_IFNET_UNLOCK(ifp);
337
338         return (error);
339 }
340
341 int
342 mac_ifnet_check_transmit(struct ifnet *ifp, struct mbuf *m)
343 {
344         struct label *label;
345         int error;
346
347         M_ASSERTPKTHDR(m);
348
349         label = mac_mbuf_to_label(m);
350
351         MAC_IFNET_LOCK(ifp);
352         MAC_CHECK(ifnet_check_transmit, ifp, ifp->if_label, m, label);
353         MAC_IFNET_UNLOCK(ifp);
354
355         return (error);
356 }
357
358 int
359 mac_ifnet_ioctl_get(struct ucred *cred, struct ifreq *ifr,
360     struct ifnet *ifp)
361 {
362         char *elements, *buffer;
363         struct label *intlabel;
364         struct mac mac;
365         int error;
366
367         if (!(mac_labeled & MPC_OBJECT_IFNET))
368                 return (EINVAL);
369
370         error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
371         if (error)
372                 return (error);
373
374         error = mac_check_structmac_consistent(&mac);
375         if (error)
376                 return (error);
377
378         elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
379         error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
380         if (error) {
381                 free(elements, M_MACTEMP);
382                 return (error);
383         }
384
385         buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
386         intlabel = mac_ifnet_label_alloc();
387         MAC_IFNET_LOCK(ifp);
388         mac_ifnet_copy_label(ifp->if_label, intlabel);
389         MAC_IFNET_UNLOCK(ifp);
390         error = mac_ifnet_externalize_label(intlabel, elements, buffer,
391             mac.m_buflen);
392         mac_ifnet_label_free(intlabel);
393         if (error == 0)
394                 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
395
396         free(buffer, M_MACTEMP);
397         free(elements, M_MACTEMP);
398
399         return (error);
400 }
401
402 int
403 mac_ifnet_ioctl_set(struct ucred *cred, struct ifreq *ifr, struct ifnet *ifp)
404 {
405         struct label *intlabel;
406         struct mac mac;
407         char *buffer;
408         int error;
409
410         if (!(mac_labeled & MPC_OBJECT_IFNET))
411                 return (EINVAL);
412
413         error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
414         if (error)
415                 return (error);
416
417         error = mac_check_structmac_consistent(&mac);
418         if (error)
419                 return (error);
420
421         buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
422         error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
423         if (error) {
424                 free(buffer, M_MACTEMP);
425                 return (error);
426         }
427
428         intlabel = mac_ifnet_label_alloc();
429         error = mac_ifnet_internalize_label(intlabel, buffer);
430         free(buffer, M_MACTEMP);
431         if (error) {
432                 mac_ifnet_label_free(intlabel);
433                 return (error);
434         }
435
436         /*
437          * XXX: Note that this is a redundant privilege check, since policies
438          * impose this check themselves if required by the policy
439          * Eventually, this should go away.
440          */
441         error = priv_check_cred(cred, PRIV_NET_SETIFMAC, 0);
442         if (error) {
443                 mac_ifnet_label_free(intlabel);
444                 return (error);
445         }
446
447         MAC_IFNET_LOCK(ifp);
448         MAC_CHECK(ifnet_check_relabel, cred, ifp, ifp->if_label, intlabel);
449         if (error) {
450                 MAC_IFNET_UNLOCK(ifp);
451                 mac_ifnet_label_free(intlabel);
452                 return (error);
453         }
454
455         MAC_PERFORM(ifnet_relabel, cred, ifp, ifp->if_label, intlabel);
456         MAC_IFNET_UNLOCK(ifp);
457
458         mac_ifnet_label_free(intlabel);
459         return (0);
460 }