]> git.saurik.com Git - apple/xnu.git/blame - security/mac_net.c
xnu-6153.121.1.tar.gz
[apple/xnu.git] / security / mac_net.c
CommitLineData
2d21ac55
A
1/*
2 * Copyright (c) 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
2d21ac55
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*-
29 * Copyright (c) 1999-2002 Robert N. M. Watson
30 * Copyright (c) 2001 Ilmar S. Habibulin
31 * Copyright (c) 2001-2004 Networks Associates Technology, Inc.
32 * Copyright (c) 2006 SPARTA, Inc.
33 * All rights reserved.
34 *
35 * This software was developed by Robert Watson and Ilmar Habibulin for the
36 * TrustedBSD Project.
37 *
38 * This software was developed for the FreeBSD Project in part by Network
39 * Associates Laboratories, the Security Research Division of Network
40 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
41 * as part of the DARPA CHATS research program.
42 *
43 * This software was enhanced by SPARTA ISSO under SPAWAR contract
44 * N66001-04-C-6019 ("SEFOS").
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 */
67
68#include <sys/param.h>
69#include <sys/kernel.h>
70#include <sys/mbuf.h>
71
72#include <net/bpf.h>
73#include <net/if.h>
74
75#include <bsd/bsm/audit.h>
b0d623f7 76#include <bsd/security/audit/audit.h>
2d21ac55
A
77
78#include <security/mac_internal.h>
79
80struct label *
81mac_mbuf_to_label(struct mbuf *mbuf)
82{
83 struct m_tag *tag;
84 struct label *label;
85
0a7de745
A
86 if (mbuf == NULL) {
87 return NULL;
88 }
2d21ac55
A
89
90 if ((mbuf->m_flags & M_PKTHDR) == 0) {
91 printf("%s() got non-header MBUF!\n", __func__);
0a7de745 92 return NULL;
2d21ac55
A
93 }
94
95 tag = m_tag_locate(mbuf, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_MACLABEL,
0a7de745 96 NULL);
2d21ac55
A
97 if (tag == NULL) {
98 printf("%s() m_tag_locate() returned NULL! (m->flags %04x)\n",
0a7de745
A
99 __func__, mbuf->m_flags);
100 return NULL;
2d21ac55 101 }
0a7de745
A
102 label = (struct label *)(tag + 1);
103 return label;
2d21ac55
A
104}
105
106static struct label *
107mac_bpfdesc_label_alloc(void)
108{
109 struct label *label;
110
111 label = mac_labelzone_alloc(M_WAITOK);
0a7de745
A
112 if (label == NULL) {
113 return NULL;
114 }
2d21ac55 115 MAC_PERFORM(bpfdesc_label_init, label);
0a7de745 116 return label;
2d21ac55
A
117}
118
119void
120mac_bpfdesc_label_init(struct bpf_d *bpf_d)
121{
122 struct label *label;
123
124 label = mac_bpfdesc_label_alloc();
125 mac_bpfdesc_label_set(bpf_d, label);
126}
127
128static struct label *
129mac_ifnet_label_alloc(void)
130{
131 struct label *label;
132
133 label = mac_labelzone_alloc(M_WAITOK);
0a7de745
A
134 if (label == NULL) {
135 return NULL;
136 }
2d21ac55 137 MAC_PERFORM(ifnet_label_init, label);
0a7de745 138 return label;
2d21ac55
A
139}
140
141void
142mac_ifnet_label_init(struct ifnet *ifp)
143{
2d21ac55
A
144 ifp->if_label = mac_ifnet_label_alloc();
145}
146
147/*
148 * On failure, caller should cleanup with m_tag_free().
149 */
150int
151mac_mbuf_tag_init(struct m_tag *tag, int flag)
152{
153 struct label *label;
154 int error;
155
156 label = (struct label *) (tag + 1);
157 mac_label_init(label);
158
159 MAC_CHECK(mbuf_label_init, label, flag);
0a7de745 160 if (error) {
2d21ac55 161 printf("%s(): mpo_mbuf_label_init() failed!\n", __func__);
0a7de745 162 }
2d21ac55 163
0a7de745 164 return error;
2d21ac55
A
165}
166
167static void
168mac_bpfdesc_label_free(struct label *label)
169{
2d21ac55
A
170 MAC_PERFORM(bpfdesc_label_destroy, label);
171 mac_labelzone_free(label);
172}
173
174void
175mac_bpfdesc_label_destroy(struct bpf_d *bpf_d)
176{
177 struct label *label;
178
179 label = mac_bpfdesc_label_get(bpf_d);
180 mac_bpfdesc_label_free(label);
181 mac_bpfdesc_label_set(bpf_d, NULL);
182}
183
184static void
185mac_ifnet_label_free(struct label *label)
186{
2d21ac55
A
187 MAC_PERFORM(ifnet_label_destroy, label);
188 mac_labelzone_free(label);
189}
190
191void
192mac_ifnet_label_destroy(struct ifnet *ifp)
193{
2d21ac55
A
194 mac_ifnet_label_free(ifp->if_label);
195 ifp->if_label = NULL;
196}
197
198void
199mac_ifnet_label_recycle(struct ifnet *ifp)
200{
2d21ac55
A
201 MAC_PERFORM(ifnet_label_recycle, ifp->if_label);
202}
203
204void
205mac_mbuf_tag_destroy(struct m_tag *tag)
206{
207 struct label *label;
208
209 label = (struct label *)(tag + 1);
210 MAC_PERFORM(mbuf_label_destroy, label);
211 mac_label_destroy(label);
212
213 return;
214}
215
216void
217mac_mbuf_tag_copy(struct m_tag *src, struct m_tag *dest)
218{
219 struct label *src_label, *dest_label;
220
221 src_label = (struct label *)(src + 1);
222 dest_label = (struct label *)(dest + 1);
223
0a7de745 224 if (src_label == NULL || dest_label == NULL) {
2d21ac55 225 return;
0a7de745 226 }
2d21ac55
A
227
228 /*
229 * mac_mbuf_tag_init() is called on the target tag
230 * in m_tag_copy(), so we don't need to call it here.
231 */
232 MAC_PERFORM(mbuf_label_copy, src_label, dest_label);
233
234 return;
235}
236
237void
238mac_mbuf_label_copy(struct mbuf *m_from, struct mbuf *m_to)
239{
240 struct label *src_label, *dest_label;
241
242 src_label = mac_mbuf_to_label(m_from);
243 dest_label = mac_mbuf_to_label(m_to);
244
245 MAC_PERFORM(mbuf_label_copy, src_label, dest_label);
246}
247
248static void
249mac_ifnet_label_copy(struct label *src, struct label *dest)
250{
2d21ac55
A
251 MAC_PERFORM(ifnet_label_copy, src, dest);
252}
253
254static int
255mac_ifnet_label_externalize(struct label *label, char *elements,
256 char *outbuf, size_t outbuflen)
257{
0a7de745 258 return MAC_EXTERNALIZE(ifnet, label, elements, outbuf, outbuflen);
2d21ac55
A
259}
260
261static int
262mac_ifnet_label_internalize(struct label *label, char *string)
263{
0a7de745 264 return MAC_INTERNALIZE(ifnet, label, string);
2d21ac55
A
265}
266
267void
268mac_ifnet_label_associate(struct ifnet *ifp)
269{
2d21ac55
A
270 MAC_PERFORM(ifnet_label_associate, ifp, ifp->if_label);
271}
272
273void
274mac_bpfdesc_label_associate(struct ucred *cred, struct bpf_d *bpf_d)
275{
276 struct label *label;
277
278 label = mac_bpfdesc_label_get(bpf_d);
279 MAC_PERFORM(bpfdesc_label_associate, cred, bpf_d, label);
280}
281
282int
283mac_bpfdesc_check_receive(struct bpf_d *bpf_d, struct ifnet *ifp)
284{
285 struct label *label;
286 int error;
287
288 label = mac_bpfdesc_label_get(bpf_d);
289 ifnet_lock_shared(ifp);
290 MAC_CHECK(bpfdesc_check_receive, bpf_d, label, ifp,
291 ifp->if_label);
292 ifnet_lock_done(ifp);
293
0a7de745 294 return error;
2d21ac55
A
295}
296
297int
298mac_mbuf_label_init(struct mbuf *m, int flag)
299{
300 struct m_tag *tag;
301 int error;
302
0a7de745
A
303 if (mac_label_mbufs == 0) {
304 return 0;
305 }
2d21ac55 306
6d2010ae 307 tag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_MACLABEL,
0a7de745 308 sizeof(struct label), flag, m);
2d21ac55
A
309 if (tag == NULL) {
310 printf("%s(): m_tag_alloc() failed!\n", __func__);
0a7de745 311 return ENOBUFS;
2d21ac55
A
312 }
313 error = mac_mbuf_tag_init(tag, flag);
314 if (error) {
315 printf("%s(): mac_mbuf_tag_init() failed!\n", __func__);
316 m_tag_free(tag);
0a7de745 317 return error;
2d21ac55
A
318 }
319 m_tag_prepend(m, tag);
0a7de745 320 return 0;
2d21ac55
A
321}
322
323void
324mac_mbuf_label_associate_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf)
325{
326 struct label *m_label, *b_label;
327
328 /* bpf_d must be locked */
329
330 m_label = mac_mbuf_to_label(mbuf);
331 b_label = mac_bpfdesc_label_get(bpf_d);
332
333 MAC_PERFORM(mbuf_label_associate_bpfdesc, bpf_d, b_label, mbuf,
334 m_label);
335}
336
337void
338mac_mbuf_label_associate_ifnet(struct ifnet *ifp, struct mbuf *mbuf)
339{
340 struct label *m_label;
341
342 /* ifp must be locked */
343
344 m_label = mac_mbuf_to_label(mbuf);
345
346 MAC_PERFORM(mbuf_label_associate_ifnet, ifp, ifp->if_label, mbuf,
347 m_label);
348}
349
350void
351mac_mbuf_label_associate_linklayer(struct ifnet *ifp, struct mbuf *mbuf)
352{
353 struct label *m_label;
354
355 /* ifp must be locked */
356
357 m_label = mac_mbuf_to_label(mbuf);
358
359 MAC_PERFORM(mbuf_label_associate_linklayer, ifp, ifp->if_label, mbuf,
360 m_label);
361}
362
363void
364mac_mbuf_label_associate_multicast_encap(struct mbuf *oldmbuf,
365 struct ifnet *ifp, struct mbuf *newmbuf)
366{
367 struct label *oldmbuflabel, *newmbuflabel;
368
369 oldmbuflabel = mac_mbuf_to_label(oldmbuf);
370 newmbuflabel = mac_mbuf_to_label(newmbuf);
371
372 /* ifp must be locked */
373
374 MAC_PERFORM(mbuf_label_associate_multicast_encap, oldmbuf, oldmbuflabel,
375 ifp, ifp->if_label, newmbuf, newmbuflabel);
376}
377
378void
379mac_mbuf_label_associate_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf)
380{
381 struct label *oldmbuflabel, *newmbuflabel;
382
383 oldmbuflabel = mac_mbuf_to_label(oldmbuf);
384 newmbuflabel = mac_mbuf_to_label(newmbuf);
385
386 MAC_PERFORM(mbuf_label_associate_netlayer, oldmbuf, oldmbuflabel,
387 newmbuf, newmbuflabel);
388}
389
390void
391mac_mbuf_label_associate_socket(struct socket *socket, struct mbuf *mbuf)
392{
393 struct label *label;
394 struct xsocket xso;
395
396 /* socket must be locked */
397
398 label = mac_mbuf_to_label(mbuf);
399
400 sotoxsocket(socket, &xso);
401 MAC_PERFORM(mbuf_label_associate_socket, &xso, socket->so_label,
0a7de745 402 mbuf, label);
2d21ac55
A
403}
404
405int
406mac_ifnet_check_transmit(struct ifnet *ifp, struct mbuf *mbuf, int family,
407 int type)
408{
409 struct label *label;
410 int error;
411
412 label = mac_mbuf_to_label(mbuf);
413
414 ifnet_lock_shared(ifp);
415 MAC_CHECK(ifnet_check_transmit, ifp, ifp->if_label, mbuf, label,
416 family, type);
417 ifnet_lock_done(ifp);
418
0a7de745 419 return error;
2d21ac55
A
420}
421
422int
423mac_ifnet_label_get(__unused struct ucred *cred, struct ifreq *ifr,
424 struct ifnet *ifp)
425{
426 char *elements, *buffer;
427 struct label *intlabel;
428 struct mac mac;
429 int error;
430 size_t len;
431
432 error = copyin(CAST_USER_ADDR_T(ifr->ifr_ifru.ifru_data),
433 &mac, sizeof(mac));
0a7de745
A
434 if (error) {
435 return error;
436 }
2d21ac55
A
437
438 error = mac_check_structmac_consistent(&mac);
0a7de745
A
439 if (error) {
440 return error;
441 }
2d21ac55
A
442
443 MALLOC(elements, char *, mac.m_buflen, M_MACTEMP, M_WAITOK);
444 error = copyinstr(CAST_USER_ADDR_T(mac.m_string), elements,
445 mac.m_buflen, &len);
446 if (error) {
447 FREE(elements, M_MACTEMP);
0a7de745 448 return error;
2d21ac55
A
449 }
450 AUDIT_ARG(mac_string, elements);
451
452 MALLOC(buffer, char *, mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
453 intlabel = mac_ifnet_label_alloc();
454 ifnet_lock_shared(ifp);
455 mac_ifnet_label_copy(ifp->if_label, intlabel);
456 ifnet_lock_done(ifp);
457 error = mac_ifnet_label_externalize(intlabel, elements,
458 buffer, mac.m_buflen);
459 mac_ifnet_label_free(intlabel);
460 FREE(elements, M_MACTEMP);
461
0a7de745 462 if (error == 0) {
2d21ac55
A
463 error = copyout(buffer, CAST_USER_ADDR_T(mac.m_string),
464 strlen(buffer) + 1);
0a7de745 465 }
2d21ac55
A
466 FREE(buffer, M_MACTEMP);
467
0a7de745 468 return error;
2d21ac55
A
469}
470
471int
472mac_ifnet_label_set(struct ucred *cred, struct ifreq *ifr,
473 struct ifnet *ifp)
474{
475 struct label *intlabel;
476 struct mac mac;
477 char *buffer;
478 int error;
479 size_t len;
480
481 error = copyin(CAST_USER_ADDR_T(ifr->ifr_ifru.ifru_data),
482 &mac, sizeof(mac));
0a7de745
A
483 if (error) {
484 return error;
485 }
2d21ac55
A
486
487 error = mac_check_structmac_consistent(&mac);
0a7de745
A
488 if (error) {
489 return error;
490 }
2d21ac55
A
491
492 MALLOC(buffer, char *, mac.m_buflen, M_MACTEMP, M_WAITOK);
493 error = copyinstr(CAST_USER_ADDR_T(mac.m_string), buffer,
494 mac.m_buflen, &len);
495 if (error) {
496 FREE(buffer, M_MACTEMP);
0a7de745 497 return error;
2d21ac55
A
498 }
499 AUDIT_ARG(mac_string, buffer);
500
501 intlabel = mac_ifnet_label_alloc();
502 error = mac_ifnet_label_internalize(intlabel, buffer);
503 FREE(buffer, M_MACTEMP);
504 if (error) {
505 mac_ifnet_label_free(intlabel);
0a7de745 506 return error;
2d21ac55
A
507 }
508
509 /*
510 * XXX: Note that this is a redundant privilege check, since
511 * policies impose this check themselves if required by the
512 * policy. Eventually, this should go away.
513 */
514 error = suser(cred, NULL);
515 if (error) {
516 mac_ifnet_label_free(intlabel);
0a7de745 517 return error;
2d21ac55
A
518 }
519
520 ifnet_lock_exclusive(ifp);
521 MAC_CHECK(ifnet_check_label_update, cred, ifp, ifp->if_label,
522 intlabel);
523 if (error) {
524 ifnet_lock_done(ifp);
525 mac_ifnet_label_free(intlabel);
0a7de745 526 return error;
2d21ac55
A
527 }
528
529 MAC_PERFORM(ifnet_label_update, cred, ifp, ifp->if_label, intlabel);
530 ifnet_lock_done(ifp);
531 mac_ifnet_label_free(intlabel);
532
0a7de745 533 return 0;
2d21ac55 534}