1 /*-
2 * Copyright (c) 1999-2002, 2007, 2009, 2019 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 developed for the FreeBSD Project in part by Network
13 * Associates Laboratories, the Security Research Division of Network
14 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
15 * as part of the DARPA CHATS research program.
16 *
17 * This software was enhanced by SPARTA ISSO under SPAWAR contract
18 * N66001-04-C-6019 ("SEFOS").
19 *
20 * This software was developed at the University of Cambridge Computer
21 * Laboratory with support from a grant from Google, Inc.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 */
44
45 #include <sys/cdefs.h>
46 #include "opt_mac.h"
47
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/lock.h>
51 #include <sys/malloc.h>
52 #include <sys/mutex.h>
53 #include <sys/sbuf.h>
54 #include <sys/sdt.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/if.h>
65 #include <net/if_var.h>
66
67 #include <netinet/in.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/ip_var.h>
70
71 #include <security/mac/mac_framework.h>
72 #include <security/mac/mac_internal.h>
73 #include <security/mac/mac_policy.h>
74
75 static struct label *
mac_inpcb_label_alloc(int flag)76 mac_inpcb_label_alloc(int flag)
77 {
78 struct label *label;
79 int error;
80
81 label = mac_labelzone_alloc(flag);
82 if (label == NULL)
83 return (NULL);
84 if (flag & M_WAITOK)
85 MAC_POLICY_CHECK(inpcb_init_label, label, flag);
86 else
87 MAC_POLICY_CHECK_NOSLEEP(inpcb_init_label, label, flag);
88 if (error) {
89 MAC_POLICY_PERFORM_NOSLEEP(inpcb_destroy_label, label);
90 mac_labelzone_free(label);
91 return (NULL);
92 }
93 return (label);
94 }
95
96 int
mac_inpcb_init(struct inpcb * inp,int flag)97 mac_inpcb_init(struct inpcb *inp, int flag)
98 {
99
100 if (mac_labeled & MPC_OBJECT_INPCB) {
101 inp->inp_label = mac_inpcb_label_alloc(flag);
102 if (inp->inp_label == NULL)
103 return (ENOMEM);
104 } else
105 inp->inp_label = NULL;
106 return (0);
107 }
108
109 static struct label *
mac_ipq_label_alloc(int flag)110 mac_ipq_label_alloc(int flag)
111 {
112 struct label *label;
113 int error;
114
115 label = mac_labelzone_alloc(flag);
116 if (label == NULL)
117 return (NULL);
118
119 if (flag & M_WAITOK)
120 MAC_POLICY_CHECK(ipq_init_label, label, flag);
121 else
122 MAC_POLICY_CHECK_NOSLEEP(ipq_init_label, label, flag);
123 if (error) {
124 MAC_POLICY_PERFORM_NOSLEEP(ipq_destroy_label, label);
125 mac_labelzone_free(label);
126 return (NULL);
127 }
128 return (label);
129 }
130
131 int
mac_ipq_init(struct ipq * q,int flag)132 mac_ipq_init(struct ipq *q, int flag)
133 {
134
135 if (mac_labeled & MPC_OBJECT_IPQ) {
136 q->ipq_label = mac_ipq_label_alloc(flag);
137 if (q->ipq_label == NULL)
138 return (ENOMEM);
139 } else
140 q->ipq_label = NULL;
141 return (0);
142 }
143
144 static void
mac_inpcb_label_free(struct label * label)145 mac_inpcb_label_free(struct label *label)
146 {
147
148 MAC_POLICY_PERFORM_NOSLEEP(inpcb_destroy_label, label);
149 mac_labelzone_free(label);
150 }
151
152 void
mac_inpcb_destroy(struct inpcb * inp)153 mac_inpcb_destroy(struct inpcb *inp)
154 {
155
156 if (inp->inp_label != NULL) {
157 mac_inpcb_label_free(inp->inp_label);
158 inp->inp_label = NULL;
159 }
160 }
161
162 static void
mac_ipq_label_free(struct label * label)163 mac_ipq_label_free(struct label *label)
164 {
165
166 MAC_POLICY_PERFORM_NOSLEEP(ipq_destroy_label, label);
167 mac_labelzone_free(label);
168 }
169
170 void
mac_ipq_destroy(struct ipq * q)171 mac_ipq_destroy(struct ipq *q)
172 {
173
174 if (q->ipq_label != NULL) {
175 mac_ipq_label_free(q->ipq_label);
176 q->ipq_label = NULL;
177 }
178 }
179
180 void
mac_inpcb_create(struct socket * so,struct inpcb * inp)181 mac_inpcb_create(struct socket *so, struct inpcb *inp)
182 {
183
184 MAC_POLICY_PERFORM_NOSLEEP(inpcb_create, so, so->so_label, inp,
185 inp->inp_label);
186 }
187
188 void
mac_ipq_reassemble(struct ipq * q,struct mbuf * m)189 mac_ipq_reassemble(struct ipq *q, struct mbuf *m)
190 {
191 struct label *label;
192
193 if (mac_policy_count == 0)
194 return;
195
196 label = mac_mbuf_to_label(m);
197
198 MAC_POLICY_PERFORM_NOSLEEP(ipq_reassemble, q, q->ipq_label, m,
199 label);
200 }
201
202 void
mac_netinet_fragment(struct mbuf * m,struct mbuf * frag)203 mac_netinet_fragment(struct mbuf *m, struct mbuf *frag)
204 {
205 struct label *mlabel, *fraglabel;
206
207 if (mac_policy_count == 0)
208 return;
209
210 mlabel = mac_mbuf_to_label(m);
211 fraglabel = mac_mbuf_to_label(frag);
212
213 MAC_POLICY_PERFORM_NOSLEEP(netinet_fragment, m, mlabel, frag,
214 fraglabel);
215 }
216
217 void
mac_ipq_create(struct mbuf * m,struct ipq * q)218 mac_ipq_create(struct mbuf *m, struct ipq *q)
219 {
220 struct label *label;
221
222 if (mac_policy_count == 0)
223 return;
224
225 label = mac_mbuf_to_label(m);
226
227 MAC_POLICY_PERFORM_NOSLEEP(ipq_create, m, label, q, q->ipq_label);
228 }
229
230 void
mac_inpcb_create_mbuf(struct inpcb * inp,struct mbuf * m)231 mac_inpcb_create_mbuf(struct inpcb *inp, struct mbuf *m)
232 {
233 struct label *mlabel;
234
235 INP_LOCK_ASSERT(inp);
236
237 if (mac_policy_count == 0)
238 return;
239
240 mlabel = mac_mbuf_to_label(m);
241
242 MAC_POLICY_PERFORM_NOSLEEP(inpcb_create_mbuf, inp, inp->inp_label, m,
243 mlabel);
244 }
245
246 int
mac_ipq_match(struct mbuf * m,struct ipq * q)247 mac_ipq_match(struct mbuf *m, struct ipq *q)
248 {
249 struct label *label;
250 int result;
251
252 if (mac_policy_count == 0)
253 return (1);
254
255 label = mac_mbuf_to_label(m);
256
257 result = 1;
258 MAC_POLICY_BOOLEAN_NOSLEEP(ipq_match, &&, m, label, q, q->ipq_label);
259
260 return (result);
261 }
262
263 void
mac_netinet_arp_send(struct ifnet * ifp,struct mbuf * m)264 mac_netinet_arp_send(struct ifnet *ifp, struct mbuf *m)
265 {
266 struct label *mlabel;
267 int locked;
268
269 if (mac_policy_count == 0)
270 return;
271
272 mlabel = mac_mbuf_to_label(m);
273
274 MAC_IFNET_LOCK(ifp, locked);
275 MAC_POLICY_PERFORM_NOSLEEP(netinet_arp_send, ifp, ifp->if_label, m,
276 mlabel);
277 MAC_IFNET_UNLOCK(ifp, locked);
278 }
279
280 void
mac_netinet_icmp_reply(struct mbuf * mrecv,struct mbuf * msend)281 mac_netinet_icmp_reply(struct mbuf *mrecv, struct mbuf *msend)
282 {
283 struct label *mrecvlabel, *msendlabel;
284
285 if (mac_policy_count == 0)
286 return;
287
288 mrecvlabel = mac_mbuf_to_label(mrecv);
289 msendlabel = mac_mbuf_to_label(msend);
290
291 MAC_POLICY_PERFORM_NOSLEEP(netinet_icmp_reply, mrecv, mrecvlabel,
292 msend, msendlabel);
293 }
294
295 void
mac_netinet_icmp_replyinplace(struct mbuf * m)296 mac_netinet_icmp_replyinplace(struct mbuf *m)
297 {
298 struct label *label;
299
300 if (mac_policy_count == 0)
301 return;
302
303 label = mac_mbuf_to_label(m);
304
305 MAC_POLICY_PERFORM_NOSLEEP(netinet_icmp_replyinplace, m, label);
306 }
307
308 void
mac_netinet_igmp_send(struct ifnet * ifp,struct mbuf * m)309 mac_netinet_igmp_send(struct ifnet *ifp, struct mbuf *m)
310 {
311 struct label *mlabel;
312 int locked;
313
314 if (mac_policy_count == 0)
315 return;
316
317 mlabel = mac_mbuf_to_label(m);
318
319 MAC_IFNET_LOCK(ifp, locked);
320 MAC_POLICY_PERFORM_NOSLEEP(netinet_igmp_send, ifp, ifp->if_label, m,
321 mlabel);
322 MAC_IFNET_UNLOCK(ifp, locked);
323 }
324
325 void
mac_netinet_tcp_reply(struct mbuf * m)326 mac_netinet_tcp_reply(struct mbuf *m)
327 {
328 struct label *label;
329
330 if (mac_policy_count == 0)
331 return;
332
333 label = mac_mbuf_to_label(m);
334
335 MAC_POLICY_PERFORM_NOSLEEP(netinet_tcp_reply, m, label);
336 }
337
338 void
mac_ipq_update(struct mbuf * m,struct ipq * q)339 mac_ipq_update(struct mbuf *m, struct ipq *q)
340 {
341 struct label *label;
342
343 if (mac_policy_count == 0)
344 return;
345
346 label = mac_mbuf_to_label(m);
347
348 MAC_POLICY_PERFORM_NOSLEEP(ipq_update, m, label, q, q->ipq_label);
349 }
350
351 MAC_CHECK_PROBE_DEFINE2(inpcb_check_deliver, "struct inpcb *",
352 "struct mbuf *");
353
354 int
mac_inpcb_check_deliver(struct inpcb * inp,struct mbuf * m)355 mac_inpcb_check_deliver(struct inpcb *inp, struct mbuf *m)
356 {
357 struct label *label;
358 int error;
359
360 M_ASSERTPKTHDR(m);
361
362 if (mac_policy_count == 0)
363 return (0);
364
365 label = mac_mbuf_to_label(m);
366
367 MAC_POLICY_CHECK_NOSLEEP(inpcb_check_deliver, inp, inp->inp_label, m,
368 label);
369 MAC_CHECK_PROBE2(inpcb_check_deliver, error, inp, m);
370
371 return (error);
372 }
373
374 MAC_CHECK_PROBE_DEFINE2(inpcb_check_visible, "struct ucred *",
375 "struct inpcb *");
376
377 int
mac_inpcb_check_visible(struct ucred * cred,struct inpcb * inp)378 mac_inpcb_check_visible(struct ucred *cred, struct inpcb *inp)
379 {
380 int error;
381
382 INP_LOCK_ASSERT(inp);
383
384 MAC_POLICY_CHECK_NOSLEEP(inpcb_check_visible, cred, inp,
385 inp->inp_label);
386 MAC_CHECK_PROBE2(inpcb_check_visible, error, cred, inp);
387
388 return (error);
389 }
390
391 void
mac_inpcb_sosetlabel(struct socket * so,struct inpcb * inp)392 mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp)
393 {
394
395 INP_WLOCK_ASSERT(inp);
396 SOCK_LOCK_ASSERT(so);
397
398 MAC_POLICY_PERFORM_NOSLEEP(inpcb_sosetlabel, so, so->so_label, inp,
399 inp->inp_label);
400 }
401
402 void
mac_netinet_firewall_reply(struct mbuf * mrecv,struct mbuf * msend)403 mac_netinet_firewall_reply(struct mbuf *mrecv, struct mbuf *msend)
404 {
405 struct label *mrecvlabel, *msendlabel;
406
407 M_ASSERTPKTHDR(mrecv);
408 M_ASSERTPKTHDR(msend);
409
410 if (mac_policy_count == 0)
411 return;
412
413 mrecvlabel = mac_mbuf_to_label(mrecv);
414 msendlabel = mac_mbuf_to_label(msend);
415
416 MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_reply, mrecv, mrecvlabel,
417 msend, msendlabel);
418 }
419
420 void
mac_netinet_firewall_send(struct mbuf * m)421 mac_netinet_firewall_send(struct mbuf *m)
422 {
423 struct label *label;
424
425 M_ASSERTPKTHDR(m);
426
427 if (mac_policy_count == 0)
428 return;
429
430 label = mac_mbuf_to_label(m);
431
432 MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_send, m, label);
433 }
434
435 /*
436 * These functions really should be referencing the syncache structure
437 * instead of the label. However, due to some of the complexities associated
438 * with exposing this syncache structure we operate directly on its label
439 * pointer. This should be OK since we aren't making any access control
440 * decisions within this code directly, we are merely allocating and copying
441 * label storage so we can properly initialize mbuf labels for any packets
442 * the syncache code might create.
443 */
444 void
mac_syncache_destroy(struct label ** label)445 mac_syncache_destroy(struct label **label)
446 {
447
448 if (*label != NULL) {
449 MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label, *label);
450 mac_labelzone_free(*label);
451 *label = NULL;
452 }
453 }
454
455 int
mac_syncache_init(struct label ** label)456 mac_syncache_init(struct label **label)
457 {
458 int error;
459
460 if (mac_labeled & MPC_OBJECT_SYNCACHE) {
461 *label = mac_labelzone_alloc(M_NOWAIT);
462 if (*label == NULL)
463 return (ENOMEM);
464 /*
465 * Since we are holding the inpcb locks the policy can not
466 * allocate policy specific label storage using M_WAITOK. So
467 * we need to do a MAC_CHECK instead of the typical
468 * MAC_PERFORM so we can propagate allocation failures back
469 * to the syncache code.
470 */
471 MAC_POLICY_CHECK_NOSLEEP(syncache_init_label, *label,
472 M_NOWAIT);
473 if (error) {
474 MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label,
475 *label);
476 mac_labelzone_free(*label);
477 *label = NULL;
478 }
479 return (error);
480 } else
481 *label = NULL;
482 return (0);
483 }
484
485 void
mac_syncache_create(struct label * label,struct inpcb * inp)486 mac_syncache_create(struct label *label, struct inpcb *inp)
487 {
488
489 INP_WLOCK_ASSERT(inp);
490
491 MAC_POLICY_PERFORM_NOSLEEP(syncache_create, label, inp);
492 }
493
494 void
mac_syncache_create_mbuf(struct label * sc_label,struct mbuf * m)495 mac_syncache_create_mbuf(struct label *sc_label, struct mbuf *m)
496 {
497 struct label *mlabel;
498
499 M_ASSERTPKTHDR(m);
500
501 if (mac_policy_count == 0)
502 return;
503
504 mlabel = mac_mbuf_to_label(m);
505
506 MAC_POLICY_PERFORM_NOSLEEP(syncache_create_mbuf, sc_label, m,
507 mlabel);
508 }
509