Deleted Added
sdiff udiff text old ( 165600 ) new ( 168955 )
full compact
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.

--- 21 unchanged lines hidden (view full) ---

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_net.c 165600 2006-12-28 21:57:59Z 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>

--- 30 unchanged lines hidden (view full) ---

77/*
78 * Retrieve the label associated with an mbuf by searching for the tag.
79 * Depending on the value of mac_labelmbufs, it's possible that a label will
80 * not be present, in which case NULL is returned. Policies must handle the
81 * possibility of an mbuf not having label storage if they do not enforce
82 * early loading.
83 */
84struct label *
85mac_mbuf_to_label(struct mbuf *mbuf)
86{
87 struct m_tag *tag;
88 struct label *label;
89
90 if (mbuf == NULL)
91 return (NULL);
92 tag = m_tag_find(mbuf, PACKET_TAG_MACLABEL, NULL);
93 if (tag == NULL)
94 return (NULL);
95 label = (struct label *)(tag+1);
96 return (label);
97}
98
99static struct label *
100mac_bpfdesc_label_alloc(void)
101{
102 struct label *label;
103
104 label = mac_labelzone_alloc(M_WAITOK);
105 MAC_PERFORM(init_bpfdesc_label, label);
106 return (label);
107}
108
109void
110mac_init_bpfdesc(struct bpf_d *bpf_d)
111{
112
113 bpf_d->bd_label = mac_bpfdesc_label_alloc();
114}
115
116static struct label *
117mac_ifnet_label_alloc(void)
118{
119 struct label *label;
120
121 label = mac_labelzone_alloc(M_WAITOK);

--- 58 unchanged lines hidden (view full) ---

180mac_bpfdesc_label_free(struct label *label)
181{
182
183 MAC_PERFORM(destroy_bpfdesc_label, label);
184 mac_labelzone_free(label);
185}
186
187void
188mac_destroy_bpfdesc(struct bpf_d *bpf_d)
189{
190
191 mac_bpfdesc_label_free(bpf_d->bd_label);
192 bpf_d->bd_label = NULL;
193}
194
195static void
196mac_ifnet_label_free(struct label *label)
197{
198
199 MAC_PERFORM(destroy_ifnet_label, label);
200 mac_labelzone_free(label);

--- 72 unchanged lines hidden (view full) ---

273 int error;
274
275 MAC_INTERNALIZE(ifnet, label, string);
276
277 return (error);
278}
279
280void
281mac_create_ifnet(struct ifnet *ifnet)
282{
283
284 MAC_IFNET_LOCK(ifnet);
285 MAC_PERFORM(create_ifnet, ifnet, ifnet->if_label);
286 MAC_IFNET_UNLOCK(ifnet);
287}
288
289void
290mac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d)
291{
292
293 MAC_PERFORM(create_bpfdesc, cred, bpf_d, bpf_d->bd_label);
294}
295
296void
297mac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf)
298{
299 struct label *label;
300
301 BPFD_LOCK_ASSERT(bpf_d);
302
303 label = mac_mbuf_to_label(mbuf);
304
305 MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, bpf_d->bd_label, mbuf,
306 label);
307}
308
309void
310mac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf)
311{
312 struct label *label;
313
314 label = mac_mbuf_to_label(mbuf);
315
316 MAC_IFNET_LOCK(ifnet);
317 MAC_PERFORM(create_mbuf_linklayer, ifnet, ifnet->if_label, mbuf,
318 label);
319 MAC_IFNET_UNLOCK(ifnet);
320}
321
322void
323mac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf)
324{
325 struct label *label;
326
327 label = mac_mbuf_to_label(mbuf);
328
329 MAC_IFNET_LOCK(ifnet);
330 MAC_PERFORM(create_mbuf_from_ifnet, ifnet, ifnet->if_label, mbuf,
331 label);
332 MAC_IFNET_UNLOCK(ifnet);
333}
334
335void
336mac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet,
337 struct mbuf *newmbuf)
338{
339 struct label *oldmbuflabel, *newmbuflabel;
340
341 oldmbuflabel = mac_mbuf_to_label(oldmbuf);
342 newmbuflabel = mac_mbuf_to_label(newmbuf);
343
344 MAC_IFNET_LOCK(ifnet);
345 MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf, oldmbuflabel,
346 ifnet, ifnet->if_label, newmbuf, newmbuflabel);
347 MAC_IFNET_UNLOCK(ifnet);
348}
349
350void
351mac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf)
352{
353 struct label *oldmbuflabel, *newmbuflabel;
354
355 oldmbuflabel = mac_mbuf_to_label(oldmbuf);
356 newmbuflabel = mac_mbuf_to_label(newmbuf);
357
358 MAC_PERFORM(create_mbuf_netlayer, oldmbuf, oldmbuflabel, newmbuf,
359 newmbuflabel);
360}
361
362int
363mac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet)
364{
365 int error;
366
367 BPFD_LOCK_ASSERT(bpf_d);
368
369 MAC_IFNET_LOCK(ifnet);
370 MAC_CHECK(check_bpfdesc_receive, bpf_d, bpf_d->bd_label, ifnet,
371 ifnet->if_label);
372 MAC_IFNET_UNLOCK(ifnet);
373
374 return (error);
375}
376
377int
378mac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf)
379{
380 struct label *label;
381 int error;
382
383 M_ASSERTPKTHDR(mbuf);
384
385 label = mac_mbuf_to_label(mbuf);
386
387 MAC_IFNET_LOCK(ifnet);
388 MAC_CHECK(check_ifnet_transmit, ifnet, ifnet->if_label, mbuf,
389 label);
390 MAC_IFNET_UNLOCK(ifnet);
391
392 return (error);
393}
394
395int
396mac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr,
397 struct ifnet *ifnet)
398{
399 char *elements, *buffer;
400 struct label *intlabel;
401 struct mac mac;
402 int error;
403
404 error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
405 if (error)

--- 7 unchanged lines hidden (view full) ---

413 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
414 if (error) {
415 free(elements, M_MACTEMP);
416 return (error);
417 }
418
419 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
420 intlabel = mac_ifnet_label_alloc();
421 MAC_IFNET_LOCK(ifnet);
422 mac_copy_ifnet_label(ifnet->if_label, intlabel);
423 MAC_IFNET_UNLOCK(ifnet);
424 error = mac_externalize_ifnet_label(intlabel, elements, buffer,
425 mac.m_buflen);
426 mac_ifnet_label_free(intlabel);
427 if (error == 0)
428 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
429
430 free(buffer, M_MACTEMP);
431 free(elements, M_MACTEMP);
432
433 return (error);
434}
435
436int
437mac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr,
438 struct ifnet *ifnet)
439{
440 struct label *intlabel;
441 struct mac mac;
442 char *buffer;
443 int error;
444
445 error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
446 if (error)

--- 24 unchanged lines hidden (view full) ---

471 * Eventually, this should go away.
472 */
473 error = priv_check_cred(cred, PRIV_NET_SETIFMAC, 0);
474 if (error) {
475 mac_ifnet_label_free(intlabel);
476 return (error);
477 }
478
479 MAC_IFNET_LOCK(ifnet);
480 MAC_CHECK(check_ifnet_relabel, cred, ifnet, ifnet->if_label,
481 intlabel);
482 if (error) {
483 MAC_IFNET_UNLOCK(ifnet);
484 mac_ifnet_label_free(intlabel);
485 return (error);
486 }
487
488 MAC_PERFORM(relabel_ifnet, cred, ifnet, ifnet->if_label, intlabel);
489 MAC_IFNET_UNLOCK(ifnet);
490
491 mac_ifnet_label_free(intlabel);
492 return (0);
493}