xref: /freebsd/sys/security/mac_test/mac_test.c (revision 721351876cd4d3a8a700f62d2061331fa951a488)
1 /*-
2  * Copyright (c) 1999-2002, 2007 Robert N. M. Watson
3  * Copyright (c) 2001-2005 McAfee, Inc.
4  * Copyright (c) 2006 SPARTA, Inc.
5  * All rights reserved.
6  *
7  * This software was developed by Robert Watson for the TrustedBSD Project.
8  *
9  * This software was developed for the FreeBSD Project in part by McAfee
10  * Research, the Security Research Division of McAfee, Inc. under
11  * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
12  * CHATS research program.
13  *
14  * This software was enhanced by SPARTA ISSO under SPAWAR contract
15  * N66001-04-C-6019 ("SEFOS").
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * $FreeBSD$
39  */
40 
41 /*
42  * Developed by the TrustedBSD Project.
43  *
44  * MAC Test policy - tests MAC Framework labeling by assigning object class
45  * magic numbers to each label and validates that each time an object label
46  * is passed into the policy, it has a consistent object type, catching
47  * incorrectly passed labels, labels passed after free, etc.
48  */
49 
50 #include <sys/param.h>
51 #include <sys/acl.h>
52 #include <sys/kdb.h>
53 #include <sys/kernel.h>
54 #include <sys/ksem.h>
55 #include <sys/malloc.h>
56 #include <sys/module.h>
57 #include <sys/mount.h>
58 #include <sys/msg.h>
59 #include <sys/proc.h>
60 #include <sys/vnode.h>
61 #include <sys/sem.h>
62 #include <sys/shm.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sx.h>
66 #include <sys/sysctl.h>
67 
68 #include <fs/devfs/devfs.h>
69 
70 #include <net/bpfdesc.h>
71 #include <net/if.h>
72 #include <net/if_types.h>
73 #include <net/if_var.h>
74 
75 #include <security/mac/mac_policy.h>
76 
77 SYSCTL_DECL(_security_mac);
78 
79 SYSCTL_NODE(_security_mac, OID_AUTO, test, CTLFLAG_RW, 0,
80     "TrustedBSD mac_test policy controls");
81 
82 #define	MAGIC_BPF	0xfe1ad1b6
83 #define	MAGIC_DEVFS	0x9ee79c32
84 #define	MAGIC_IFNET	0xc218b120
85 #define	MAGIC_INPCB	0x4440f7bb
86 #define	MAGIC_IPQ	0x206188ef
87 #define	MAGIC_MBUF	0xbbefa5bb
88 #define	MAGIC_MOUNT	0xc7c46e47
89 #define	MAGIC_SOCKET	0x9199c6cd
90 #define	MAGIC_SYNCACHE	0x7fb838a8
91 #define	MAGIC_SYSV_MSG	0x8bbba61e
92 #define	MAGIC_SYSV_MSQ	0xea672391
93 #define	MAGIC_SYSV_SEM	0x896e8a0b
94 #define	MAGIC_SYSV_SHM	0x76119ab0
95 #define	MAGIC_PIPE	0xdc6c9919
96 #define	MAGIC_POSIX_SEM	0x78ae980c
97 #define	MAGIC_POSIX_SHM	0x4e853fc9
98 #define	MAGIC_PROC	0x3b4be98f
99 #define	MAGIC_CRED	0x9a5a4987
100 #define	MAGIC_VNODE	0x1a67a45c
101 #define	MAGIC_FREE	0x849ba1fd
102 
103 #define	SLOT(x)	mac_label_get((x), test_slot)
104 #define	SLOT_SET(x, v)	mac_label_set((x), test_slot, (v))
105 
106 static int	test_slot;
107 SYSCTL_INT(_security_mac_test, OID_AUTO, slot, CTLFLAG_RD,
108     &test_slot, 0, "Slot allocated by framework");
109 
110 SYSCTL_NODE(_security_mac_test, OID_AUTO, counter, CTLFLAG_RW, 0,
111     "TrustedBSD mac_test counters controls");
112 
113 #define	COUNTER_DECL(variable)						\
114 	static int counter_##variable;					\
115 	SYSCTL_INT(_security_mac_test_counter, OID_AUTO, variable,	\
116 	CTLFLAG_RD, &counter_##variable, 0, #variable)
117 
118 #define	COUNTER_INC(variable)	atomic_add_int(&counter_##variable, 1)
119 
120 #ifdef KDB
121 #define	DEBUGGER(func, string)	kdb_enter(KDB_WHY_MAC, (string))
122 #else
123 #define	DEBUGGER(func, string)	printf("mac_test: %s: %s\n", (func), (string))
124 #endif
125 
126 #define	LABEL_CHECK(label, magic) do {					\
127 	if (label != NULL) {						\
128 		KASSERT(SLOT(label) == magic ||	SLOT(label) == 0,	\
129 		    ("%s: bad %s label", __func__, #magic));		\
130 	}								\
131 } while (0)
132 
133 #define	LABEL_DESTROY(label, magic) do {				\
134 	if (SLOT(label) == magic || SLOT(label) == 0) {			\
135 		SLOT_SET(label, MAGIC_FREE);				\
136 	} else if (SLOT(label) == MAGIC_FREE) {				\
137 		DEBUGGER("%s: dup destroy", __func__);			\
138 	} else {							\
139 		DEBUGGER("%s: corrupted label", __func__);		\
140 	}								\
141 } while (0)
142 
143 #define	LABEL_INIT(label, magic) do {					\
144 	SLOT_SET(label, magic);						\
145 } while (0)
146 
147 #define	LABEL_NOTFREE(label) do {					\
148 	KASSERT(SLOT(label) != MAGIC_FREE,				\
149 	    ("%s: destroyed label", __func__));				\
150 } while (0)
151 
152 /*
153  * Object-specific entry point implementations are sorted alphabetically by
154  * object type name and then by operation.
155  */
156 COUNTER_DECL(bpfdesc_check_receive);
157 static int
158 test_bpfdesc_check_receive(struct bpf_d *d, struct label *dlabel,
159     struct ifnet *ifp, struct label *ifplabel)
160 {
161 
162 	LABEL_CHECK(dlabel, MAGIC_BPF);
163 	LABEL_CHECK(ifplabel, MAGIC_IFNET);
164 	COUNTER_INC(bpfdesc_check_receive);
165 
166 	return (0);
167 }
168 
169 COUNTER_DECL(bpfdesc_create);
170 static void
171 test_bpfdesc_create(struct ucred *cred, struct bpf_d *d,
172     struct label *dlabel)
173 {
174 
175 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
176 	LABEL_CHECK(dlabel, MAGIC_BPF);
177 	COUNTER_INC(bpfdesc_create);
178 }
179 
180 COUNTER_DECL(bpfdesc_create_mbuf);
181 static void
182 test_bpfdesc_create_mbuf(struct bpf_d *d, struct label *dlabel,
183     struct mbuf *m, struct label *mlabel)
184 {
185 
186 	LABEL_CHECK(dlabel, MAGIC_BPF);
187 	LABEL_CHECK(mlabel, MAGIC_MBUF);
188 	COUNTER_INC(bpfdesc_create_mbuf);
189 }
190 
191 COUNTER_DECL(bpfdesc_destroy_label);
192 static void
193 test_bpfdesc_destroy_label(struct label *label)
194 {
195 
196 	LABEL_DESTROY(label, MAGIC_BPF);
197 	COUNTER_INC(bpfdesc_destroy_label);
198 }
199 
200 COUNTER_DECL(bpfdesc_init_label);
201 static void
202 test_bpfdesc_init_label(struct label *label)
203 {
204 
205 	LABEL_INIT(label, MAGIC_BPF);
206 	COUNTER_INC(bpfdesc_init_label);
207 }
208 
209 COUNTER_DECL(cred_check_relabel);
210 static int
211 test_cred_check_relabel(struct ucred *cred, struct label *newlabel)
212 {
213 
214 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
215 	LABEL_CHECK(newlabel, MAGIC_CRED);
216 	COUNTER_INC(cred_check_relabel);
217 
218 	return (0);
219 }
220 
221 COUNTER_DECL(cred_check_visible);
222 static int
223 test_cred_check_visible(struct ucred *u1, struct ucred *u2)
224 {
225 
226 	LABEL_CHECK(u1->cr_label, MAGIC_CRED);
227 	LABEL_CHECK(u2->cr_label, MAGIC_CRED);
228 	COUNTER_INC(cred_check_visible);
229 
230 	return (0);
231 }
232 
233 COUNTER_DECL(cred_copy_label);
234 static void
235 test_cred_copy_label(struct label *src, struct label *dest)
236 {
237 
238 	LABEL_CHECK(src, MAGIC_CRED);
239 	LABEL_CHECK(dest, MAGIC_CRED);
240 	COUNTER_INC(cred_copy_label);
241 }
242 
243 COUNTER_DECL(cred_destroy_label);
244 static void
245 test_cred_destroy_label(struct label *label)
246 {
247 
248 	LABEL_DESTROY(label, MAGIC_CRED);
249 	COUNTER_INC(cred_destroy_label);
250 }
251 
252 COUNTER_DECL(cred_externalize_label);
253 static int
254 test_cred_externalize_label(struct label *label, char *element_name,
255     struct sbuf *sb, int *claimed)
256 {
257 
258 	LABEL_CHECK(label, MAGIC_CRED);
259 	COUNTER_INC(cred_externalize_label);
260 
261 	return (0);
262 }
263 
264 COUNTER_DECL(cred_init_label);
265 static void
266 test_cred_init_label(struct label *label)
267 {
268 
269 	LABEL_INIT(label, MAGIC_CRED);
270 	COUNTER_INC(cred_init_label);
271 }
272 
273 COUNTER_DECL(cred_internalize_label);
274 static int
275 test_cred_internalize_label(struct label *label, char *element_name,
276     char *element_data, int *claimed)
277 {
278 
279 	LABEL_CHECK(label, MAGIC_CRED);
280 	COUNTER_INC(cred_internalize_label);
281 
282 	return (0);
283 }
284 
285 COUNTER_DECL(cred_relabel);
286 static void
287 test_cred_relabel(struct ucred *cred, struct label *newlabel)
288 {
289 
290 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
291 	LABEL_CHECK(newlabel, MAGIC_CRED);
292 	COUNTER_INC(cred_relabel);
293 }
294 
295 COUNTER_DECL(devfs_create_device);
296 static void
297 test_devfs_create_device(struct ucred *cred, struct mount *mp,
298     struct cdev *dev, struct devfs_dirent *de, struct label *delabel)
299 {
300 
301 	if (cred != NULL)
302 		LABEL_CHECK(cred->cr_label, MAGIC_CRED);
303 	LABEL_CHECK(delabel, MAGIC_DEVFS);
304 	COUNTER_INC(devfs_create_device);
305 }
306 
307 COUNTER_DECL(devfs_create_directory);
308 static void
309 test_devfs_create_directory(struct mount *mp, char *dirname,
310     int dirnamelen, struct devfs_dirent *de, struct label *delabel)
311 {
312 
313 	LABEL_CHECK(delabel, MAGIC_DEVFS);
314 	COUNTER_INC(devfs_create_directory);
315 }
316 
317 COUNTER_DECL(devfs_create_symlink);
318 static void
319 test_devfs_create_symlink(struct ucred *cred, struct mount *mp,
320     struct devfs_dirent *dd, struct label *ddlabel, struct devfs_dirent *de,
321     struct label *delabel)
322 {
323 
324 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
325 	LABEL_CHECK(ddlabel, MAGIC_DEVFS);
326 	LABEL_CHECK(delabel, MAGIC_DEVFS);
327 	COUNTER_INC(devfs_create_symlink);
328 }
329 
330 COUNTER_DECL(devfs_destroy_label);
331 static void
332 test_devfs_destroy_label(struct label *label)
333 {
334 
335 	LABEL_DESTROY(label, MAGIC_DEVFS);
336 	COUNTER_INC(devfs_destroy_label);
337 }
338 
339 COUNTER_DECL(devfs_init_label);
340 static void
341 test_devfs_init_label(struct label *label)
342 {
343 
344 	LABEL_INIT(label, MAGIC_DEVFS);
345 	COUNTER_INC(devfs_init_label);
346 }
347 
348 COUNTER_DECL(devfs_update);
349 static void
350 test_devfs_update(struct mount *mp, struct devfs_dirent *devfs_dirent,
351     struct label *direntlabel, struct vnode *vp, struct label *vplabel)
352 {
353 
354 	LABEL_CHECK(direntlabel, MAGIC_DEVFS);
355 	LABEL_CHECK(vplabel, MAGIC_VNODE);
356 	COUNTER_INC(devfs_update);
357 }
358 
359 COUNTER_DECL(devfs_vnode_associate);
360 static void
361 test_devfs_vnode_associate(struct mount *mp, struct label *mplabel,
362     struct devfs_dirent *de, struct label *delabel, struct vnode *vp,
363     struct label *vplabel)
364 {
365 
366 	LABEL_CHECK(mplabel, MAGIC_MOUNT);
367 	LABEL_CHECK(delabel, MAGIC_DEVFS);
368 	LABEL_CHECK(vplabel, MAGIC_VNODE);
369 	COUNTER_INC(devfs_vnode_associate);
370 }
371 
372 COUNTER_DECL(ifnet_check_relabel);
373 static int
374 test_ifnet_check_relabel(struct ucred *cred, struct ifnet *ifp,
375     struct label *ifplabel, struct label *newlabel)
376 {
377 
378 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
379 	LABEL_CHECK(ifplabel, MAGIC_IFNET);
380 	LABEL_CHECK(newlabel, MAGIC_IFNET);
381 	COUNTER_INC(ifnet_check_relabel);
382 
383 	return (0);
384 }
385 
386 COUNTER_DECL(ifnet_check_transmit);
387 static int
388 test_ifnet_check_transmit(struct ifnet *ifp, struct label *ifplabel,
389     struct mbuf *m, struct label *mlabel)
390 {
391 
392 	LABEL_CHECK(ifplabel, MAGIC_IFNET);
393 	LABEL_CHECK(mlabel, MAGIC_MBUF);
394 	COUNTER_INC(ifnet_check_transmit);
395 
396 	return (0);
397 }
398 
399 COUNTER_DECL(ifnet_copy_label);
400 static void
401 test_ifnet_copy_label(struct label *src, struct label *dest)
402 {
403 
404 	LABEL_CHECK(src, MAGIC_IFNET);
405 	LABEL_CHECK(dest, MAGIC_IFNET);
406 	COUNTER_INC(ifnet_copy_label);
407 }
408 
409 COUNTER_DECL(ifnet_create);
410 static void
411 test_ifnet_create(struct ifnet *ifp, struct label *ifplabel)
412 {
413 
414 	LABEL_CHECK(ifplabel, MAGIC_IFNET);
415 	COUNTER_INC(ifnet_create);
416 }
417 
418 COUNTER_DECL(ifnet_create_mbuf);
419 static void
420 test_ifnet_create_mbuf(struct ifnet *ifp, struct label *ifplabel,
421     struct mbuf *m, struct label *mlabel)
422 {
423 
424 	LABEL_CHECK(ifplabel, MAGIC_IFNET);
425 	LABEL_CHECK(mlabel, MAGIC_MBUF);
426 	COUNTER_INC(ifnet_create_mbuf);
427 }
428 
429 COUNTER_DECL(ifnet_destroy_label);
430 static void
431 test_ifnet_destroy_label(struct label *label)
432 {
433 
434 	LABEL_DESTROY(label, MAGIC_IFNET);
435 	COUNTER_INC(ifnet_destroy_label);
436 }
437 
438 COUNTER_DECL(ifnet_externalize_label);
439 static int
440 test_ifnet_externalize_label(struct label *label, char *element_name,
441     struct sbuf *sb, int *claimed)
442 {
443 
444 	LABEL_CHECK(label, MAGIC_IFNET);
445 	COUNTER_INC(ifnet_externalize_label);
446 
447 	return (0);
448 }
449 
450 COUNTER_DECL(ifnet_init_label);
451 static void
452 test_ifnet_init_label(struct label *label)
453 {
454 
455 	LABEL_INIT(label, MAGIC_IFNET);
456 	COUNTER_INC(ifnet_init_label);
457 }
458 
459 COUNTER_DECL(ifnet_internalize_label);
460 static int
461 test_ifnet_internalize_label(struct label *label, char *element_name,
462     char *element_data, int *claimed)
463 {
464 
465 	LABEL_CHECK(label, MAGIC_IFNET);
466 	COUNTER_INC(ifnet_internalize_label);
467 
468 	return (0);
469 }
470 
471 COUNTER_DECL(ifnet_relabel);
472 static void
473 test_ifnet_relabel(struct ucred *cred, struct ifnet *ifp,
474     struct label *ifplabel, struct label *newlabel)
475 {
476 
477 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
478 	LABEL_CHECK(ifplabel, MAGIC_IFNET);
479 	LABEL_CHECK(newlabel, MAGIC_IFNET);
480 	COUNTER_INC(ifnet_relabel);
481 }
482 
483 COUNTER_DECL(inpcb_check_deliver);
484 static int
485 test_inpcb_check_deliver(struct inpcb *inp, struct label *inplabel,
486     struct mbuf *m, struct label *mlabel)
487 {
488 
489 	LABEL_CHECK(inplabel, MAGIC_INPCB);
490 	LABEL_CHECK(mlabel, MAGIC_MBUF);
491 	COUNTER_INC(inpcb_check_deliver);
492 
493 	return (0);
494 }
495 
496 COUNTER_DECL(inpcb_create);
497 static void
498 test_inpcb_create(struct socket *so, struct label *solabel,
499     struct inpcb *inp, struct label *inplabel)
500 {
501 
502 	LABEL_CHECK(solabel, MAGIC_SOCKET);
503 	LABEL_CHECK(inplabel, MAGIC_INPCB);
504 	COUNTER_INC(inpcb_create);
505 }
506 
507 COUNTER_DECL(inpcb_create_mbuf);
508 static void
509 test_inpcb_create_mbuf(struct inpcb *inp, struct label *inplabel,
510     struct mbuf *m, struct label *mlabel)
511 {
512 
513 	LABEL_CHECK(inplabel, MAGIC_INPCB);
514 	LABEL_CHECK(mlabel, MAGIC_MBUF);
515 	COUNTER_INC(inpcb_create_mbuf);
516 }
517 
518 COUNTER_DECL(inpcb_destroy_label);
519 static void
520 test_inpcb_destroy_label(struct label *label)
521 {
522 
523 	LABEL_DESTROY(label, MAGIC_INPCB);
524 	COUNTER_INC(inpcb_destroy_label);
525 }
526 
527 COUNTER_DECL(inpcb_init_label);
528 static int
529 test_inpcb_init_label(struct label *label, int flag)
530 {
531 
532 	if (flag & M_WAITOK)
533 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
534 		    "test_inpcb_init_label() at %s:%d", __FILE__,
535 		    __LINE__);
536 
537 	LABEL_INIT(label, MAGIC_INPCB);
538 	COUNTER_INC(inpcb_init_label);
539 	return (0);
540 }
541 
542 COUNTER_DECL(inpcb_sosetlabel);
543 static void
544 test_inpcb_sosetlabel(struct socket *so, struct label *solabel,
545     struct inpcb *inp, struct label *inplabel)
546 {
547 
548 	LABEL_CHECK(solabel, MAGIC_SOCKET);
549 	LABEL_CHECK(inplabel, MAGIC_INPCB);
550 	COUNTER_INC(inpcb_sosetlabel);
551 }
552 
553 COUNTER_DECL(ipq_create);
554 static void
555 test_ipq_create(struct mbuf *fragment, struct label *fragmentlabel,
556     struct ipq *q, struct label *qlabel)
557 {
558 
559 	LABEL_CHECK(fragmentlabel, MAGIC_MBUF);
560 	LABEL_CHECK(qlabel, MAGIC_IPQ);
561 	COUNTER_INC(ipq_create);
562 }
563 
564 COUNTER_DECL(ipq_destroy_label);
565 static void
566 test_ipq_destroy_label(struct label *label)
567 {
568 
569 	LABEL_DESTROY(label, MAGIC_IPQ);
570 	COUNTER_INC(ipq_destroy_label);
571 }
572 
573 COUNTER_DECL(ipq_init_label);
574 static int
575 test_ipq_init_label(struct label *label, int flag)
576 {
577 
578 	if (flag & M_WAITOK)
579 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
580 		    "test_ipq_init_label() at %s:%d", __FILE__,
581 		    __LINE__);
582 
583 	LABEL_INIT(label, MAGIC_IPQ);
584 	COUNTER_INC(ipq_init_label);
585 	return (0);
586 }
587 
588 COUNTER_DECL(ipq_match);
589 static int
590 test_ipq_match(struct mbuf *fragment, struct label *fragmentlabel,
591     struct ipq *q, struct label *qlabel)
592 {
593 
594 	LABEL_CHECK(fragmentlabel, MAGIC_MBUF);
595 	LABEL_CHECK(qlabel, MAGIC_IPQ);
596 	COUNTER_INC(ipq_match);
597 
598 	return (1);
599 }
600 
601 COUNTER_DECL(ipq_reassemble);
602 static void
603 test_ipq_reassemble(struct ipq *q, struct label *qlabel, struct mbuf *m,
604    struct label *mlabel)
605 {
606 
607 	LABEL_CHECK(qlabel, MAGIC_IPQ);
608 	LABEL_CHECK(mlabel, MAGIC_MBUF);
609 	COUNTER_INC(ipq_reassemble);
610 }
611 
612 COUNTER_DECL(ipq_update);
613 static void
614 test_ipq_update(struct mbuf *m, struct label *mlabel, struct ipq *q,
615     struct label *qlabel)
616 {
617 
618 	LABEL_CHECK(mlabel, MAGIC_MBUF);
619 	LABEL_CHECK(qlabel, MAGIC_IPQ);
620 	COUNTER_INC(ipq_update);
621 }
622 
623 COUNTER_DECL(kenv_check_dump);
624 static int
625 test_kenv_check_dump(struct ucred *cred)
626 {
627 
628 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
629 	COUNTER_INC(kenv_check_dump);
630 
631 	return (0);
632 }
633 
634 COUNTER_DECL(kenv_check_get);
635 static int
636 test_kenv_check_get(struct ucred *cred, char *name)
637 {
638 
639 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
640 	COUNTER_INC(kenv_check_get);
641 
642 	return (0);
643 }
644 
645 COUNTER_DECL(kenv_check_set);
646 static int
647 test_kenv_check_set(struct ucred *cred, char *name, char *value)
648 {
649 
650 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
651 	COUNTER_INC(kenv_check_set);
652 
653 	return (0);
654 }
655 
656 COUNTER_DECL(kenv_check_unset);
657 static int
658 test_kenv_check_unset(struct ucred *cred, char *name)
659 {
660 
661 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
662 	COUNTER_INC(kenv_check_unset);
663 
664 	return (0);
665 }
666 
667 COUNTER_DECL(kld_check_load);
668 static int
669 test_kld_check_load(struct ucred *cred, struct vnode *vp,
670     struct label *label)
671 {
672 
673 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
674 	LABEL_CHECK(label, MAGIC_VNODE);
675 	COUNTER_INC(kld_check_load);
676 
677 	return (0);
678 }
679 
680 COUNTER_DECL(kld_check_stat);
681 static int
682 test_kld_check_stat(struct ucred *cred)
683 {
684 
685 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
686 	COUNTER_INC(kld_check_stat);
687 
688 	return (0);
689 }
690 
691 COUNTER_DECL(mbuf_copy_label);
692 static void
693 test_mbuf_copy_label(struct label *src, struct label *dest)
694 {
695 
696 	LABEL_CHECK(src, MAGIC_MBUF);
697 	LABEL_CHECK(dest, MAGIC_MBUF);
698 	COUNTER_INC(mbuf_copy_label);
699 }
700 
701 COUNTER_DECL(mbuf_destroy_label);
702 static void
703 test_mbuf_destroy_label(struct label *label)
704 {
705 
706 	/*
707 	 * If we're loaded dynamically, there may be mbufs in flight that
708 	 * didn't have label storage allocated for them.  Handle this
709 	 * gracefully.
710 	 */
711 	if (label == NULL)
712 		return;
713 
714 	LABEL_DESTROY(label, MAGIC_MBUF);
715 	COUNTER_INC(mbuf_destroy_label);
716 }
717 
718 COUNTER_DECL(mbuf_init_label);
719 static int
720 test_mbuf_init_label(struct label *label, int flag)
721 {
722 
723 	if (flag & M_WAITOK)
724 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
725 		    "test_mbuf_init_label() at %s:%d", __FILE__,
726 		    __LINE__);
727 
728 	LABEL_INIT(label, MAGIC_MBUF);
729 	COUNTER_INC(mbuf_init_label);
730 	return (0);
731 }
732 
733 COUNTER_DECL(mount_check_stat);
734 static int
735 test_mount_check_stat(struct ucred *cred, struct mount *mp,
736     struct label *mplabel)
737 {
738 
739 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
740 	LABEL_CHECK(mplabel, MAGIC_MOUNT);
741 	COUNTER_INC(mount_check_stat);
742 
743 	return (0);
744 }
745 
746 COUNTER_DECL(mount_create);
747 static void
748 test_mount_create(struct ucred *cred, struct mount *mp,
749     struct label *mplabel)
750 {
751 
752 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
753 	LABEL_CHECK(mplabel, MAGIC_MOUNT);
754 	COUNTER_INC(mount_create);
755 }
756 
757 COUNTER_DECL(mount_destroy_label);
758 static void
759 test_mount_destroy_label(struct label *label)
760 {
761 
762 	LABEL_DESTROY(label, MAGIC_MOUNT);
763 	COUNTER_INC(mount_destroy_label);
764 }
765 
766 COUNTER_DECL(mount_init_label);
767 static void
768 test_mount_init_label(struct label *label)
769 {
770 
771 	LABEL_INIT(label, MAGIC_MOUNT);
772 	COUNTER_INC(mount_init_label);
773 }
774 
775 COUNTER_DECL(netatalk_aarp_send);
776 static void
777 test_netatalk_aarp_send(struct ifnet *ifp, struct label *ifplabel,
778     struct mbuf *m, struct label *mlabel)
779 {
780 
781 	LABEL_CHECK(ifplabel, MAGIC_IFNET);
782 	LABEL_CHECK(mlabel, MAGIC_MBUF);
783 	COUNTER_INC(netatalk_aarp_send);
784 }
785 
786 COUNTER_DECL(netinet_arp_send);
787 static void
788 test_netinet_arp_send(struct ifnet *ifp, struct label *ifplabel,
789     struct mbuf *m, struct label *mlabel)
790 {
791 
792 	LABEL_CHECK(ifplabel, MAGIC_IFNET);
793 	LABEL_CHECK(mlabel, MAGIC_MBUF);
794 	COUNTER_INC(netinet_arp_send);
795 }
796 
797 COUNTER_DECL(netinet_fragment);
798 static void
799 test_netinet_fragment(struct mbuf *m, struct label *mlabel,
800     struct mbuf *frag, struct label *fraglabel)
801 {
802 
803 	LABEL_CHECK(mlabel, MAGIC_MBUF);
804 	LABEL_CHECK(fraglabel, MAGIC_MBUF);
805 	COUNTER_INC(netinet_fragment);
806 }
807 
808 COUNTER_DECL(netinet_icmp_reply);
809 static void
810 test_netinet_icmp_reply(struct mbuf *mrecv, struct label *mrecvlabel,
811     struct mbuf *msend, struct label *msendlabel)
812 {
813 
814 	LABEL_CHECK(mrecvlabel, MAGIC_MBUF);
815 	LABEL_CHECK(msendlabel, MAGIC_MBUF);
816 	COUNTER_INC(netinet_icmp_reply);
817 }
818 
819 COUNTER_DECL(netinet_icmp_replyinplace);
820 static void
821 test_netinet_icmp_replyinplace(struct mbuf *m, struct label *mlabel)
822 {
823 
824 	LABEL_CHECK(mlabel, MAGIC_MBUF);
825 	COUNTER_INC(netinet_icmp_replyinplace);
826 }
827 
828 COUNTER_DECL(netinet_igmp_send);
829 static void
830 test_netinet_igmp_send(struct ifnet *ifp, struct label *ifplabel,
831     struct mbuf *m, struct label *mlabel)
832 {
833 
834 	LABEL_CHECK(ifplabel, MAGIC_IFNET);
835 	LABEL_CHECK(mlabel, MAGIC_MBUF);
836 	COUNTER_INC(netinet_igmp_send);
837 }
838 
839 COUNTER_DECL(netinet_tcp_reply);
840 static void
841 test_netinet_tcp_reply(struct mbuf *m, struct label *mlabel)
842 {
843 
844 	LABEL_CHECK(mlabel, MAGIC_MBUF);
845 	COUNTER_INC(netinet_tcp_reply);
846 }
847 
848 COUNTER_DECL(netinet6_nd6_send);
849 static void
850 test_netinet6_nd6_send(struct ifnet *ifp, struct label *ifplabel,
851     struct mbuf *m, struct label *mlabel)
852 {
853 
854 	LABEL_CHECK(ifplabel, MAGIC_IFNET);
855 	LABEL_CHECK(mlabel, MAGIC_MBUF);
856 	COUNTER_INC(netinet6_nd6_send);
857 }
858 
859 COUNTER_DECL(pipe_check_ioctl);
860 static int
861 test_pipe_check_ioctl(struct ucred *cred, struct pipepair *pp,
862     struct label *pplabel, unsigned long cmd, void /* caddr_t */ *data)
863 {
864 
865 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
866 	LABEL_CHECK(pplabel, MAGIC_PIPE);
867 	COUNTER_INC(pipe_check_ioctl);
868 
869 	return (0);
870 }
871 
872 COUNTER_DECL(pipe_check_poll);
873 static int
874 test_pipe_check_poll(struct ucred *cred, struct pipepair *pp,
875     struct label *pplabel)
876 {
877 
878 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
879 	LABEL_CHECK(pplabel, MAGIC_PIPE);
880 	COUNTER_INC(pipe_check_poll);
881 
882 	return (0);
883 }
884 
885 COUNTER_DECL(pipe_check_read);
886 static int
887 test_pipe_check_read(struct ucred *cred, struct pipepair *pp,
888     struct label *pplabel)
889 {
890 
891 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
892 	LABEL_CHECK(pplabel, MAGIC_PIPE);
893 	COUNTER_INC(pipe_check_read);
894 
895 	return (0);
896 }
897 
898 COUNTER_DECL(pipe_check_relabel);
899 static int
900 test_pipe_check_relabel(struct ucred *cred, struct pipepair *pp,
901     struct label *pplabel, struct label *newlabel)
902 {
903 
904 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
905 	LABEL_CHECK(pplabel, MAGIC_PIPE);
906 	LABEL_CHECK(newlabel, MAGIC_PIPE);
907 	COUNTER_INC(pipe_check_relabel);
908 
909 	return (0);
910 }
911 
912 COUNTER_DECL(pipe_check_stat);
913 static int
914 test_pipe_check_stat(struct ucred *cred, struct pipepair *pp,
915     struct label *pplabel)
916 {
917 
918 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
919 	LABEL_CHECK(pplabel, MAGIC_PIPE);
920 	COUNTER_INC(pipe_check_stat);
921 
922 	return (0);
923 }
924 
925 COUNTER_DECL(pipe_check_write);
926 static int
927 test_pipe_check_write(struct ucred *cred, struct pipepair *pp,
928     struct label *pplabel)
929 {
930 
931 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
932 	LABEL_CHECK(pplabel, MAGIC_PIPE);
933 	COUNTER_INC(pipe_check_write);
934 
935 	return (0);
936 }
937 
938 COUNTER_DECL(pipe_copy_label);
939 static void
940 test_pipe_copy_label(struct label *src, struct label *dest)
941 {
942 
943 	LABEL_CHECK(src, MAGIC_PIPE);
944 	LABEL_CHECK(dest, MAGIC_PIPE);
945 	COUNTER_INC(pipe_copy_label);
946 }
947 
948 COUNTER_DECL(pipe_create);
949 static void
950 test_pipe_create(struct ucred *cred, struct pipepair *pp,
951    struct label *pplabel)
952 {
953 
954 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
955 	LABEL_CHECK(pplabel, MAGIC_PIPE);
956 	COUNTER_INC(pipe_create);
957 }
958 
959 COUNTER_DECL(pipe_destroy_label);
960 static void
961 test_pipe_destroy_label(struct label *label)
962 {
963 
964 	LABEL_DESTROY(label, MAGIC_PIPE);
965 	COUNTER_INC(pipe_destroy_label);
966 }
967 
968 COUNTER_DECL(pipe_externalize_label);
969 static int
970 test_pipe_externalize_label(struct label *label, char *element_name,
971     struct sbuf *sb, int *claimed)
972 {
973 
974 	LABEL_CHECK(label, MAGIC_PIPE);
975 	COUNTER_INC(pipe_externalize_label);
976 
977 	return (0);
978 }
979 
980 COUNTER_DECL(pipe_init_label);
981 static void
982 test_pipe_init_label(struct label *label)
983 {
984 
985 	LABEL_INIT(label, MAGIC_PIPE);
986 	COUNTER_INC(pipe_init_label);
987 }
988 
989 COUNTER_DECL(pipe_internalize_label);
990 static int
991 test_pipe_internalize_label(struct label *label, char *element_name,
992     char *element_data, int *claimed)
993 {
994 
995 	LABEL_CHECK(label, MAGIC_PIPE);
996 	COUNTER_INC(pipe_internalize_label);
997 
998 	return (0);
999 }
1000 
1001 COUNTER_DECL(pipe_relabel);
1002 static void
1003 test_pipe_relabel(struct ucred *cred, struct pipepair *pp,
1004     struct label *pplabel, struct label *newlabel)
1005 {
1006 
1007 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1008 	LABEL_CHECK(pplabel, MAGIC_PIPE);
1009 	LABEL_CHECK(newlabel, MAGIC_PIPE);
1010 	COUNTER_INC(pipe_relabel);
1011 }
1012 
1013 COUNTER_DECL(posixsem_check_getvalue);
1014 static int
1015 test_posixsem_check_getvalue(struct ucred *cred, struct ksem *ks,
1016     struct label *kslabel)
1017 {
1018 
1019 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1020 	LABEL_CHECK(kslabel, MAGIC_POSIX_SEM);
1021 	COUNTER_INC(posixsem_check_getvalue);
1022 
1023 	return (0);
1024 }
1025 
1026 COUNTER_DECL(posixsem_check_open);
1027 static int
1028 test_posixsem_check_open(struct ucred *cred, struct ksem *ks,
1029     struct label *kslabel)
1030 {
1031 
1032 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1033 	LABEL_CHECK(kslabel, MAGIC_POSIX_SEM);
1034 	COUNTER_INC(posixsem_check_open);
1035 
1036 	return (0);
1037 }
1038 
1039 COUNTER_DECL(posixsem_check_post);
1040 static int
1041 test_posixsem_check_post(struct ucred *cred, struct ksem *ks,
1042     struct label *kslabel)
1043 {
1044 
1045 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1046 	LABEL_CHECK(kslabel, MAGIC_POSIX_SEM);
1047 	COUNTER_INC(posixsem_check_post);
1048 
1049 	return (0);
1050 }
1051 
1052 COUNTER_DECL(posixsem_check_unlink);
1053 static int
1054 test_posixsem_check_unlink(struct ucred *cred, struct ksem *ks,
1055     struct label *kslabel)
1056 {
1057 
1058 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1059 	LABEL_CHECK(kslabel, MAGIC_POSIX_SEM);
1060 	COUNTER_INC(posixsem_check_unlink);
1061 
1062 	return (0);
1063 }
1064 
1065 COUNTER_DECL(posixsem_check_wait);
1066 static int
1067 test_posixsem_check_wait(struct ucred *cred, struct ksem *ks,
1068     struct label *kslabel)
1069 {
1070 
1071 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1072 	LABEL_CHECK(kslabel, MAGIC_POSIX_SEM);
1073 	COUNTER_INC(posixsem_check_wait);
1074 
1075 	return (0);
1076 }
1077 
1078 COUNTER_DECL(posixsem_create);
1079 static void
1080 test_posixsem_create(struct ucred *cred, struct ksem *ks,
1081    struct label *kslabel)
1082 {
1083 
1084 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1085 	LABEL_CHECK(kslabel, MAGIC_POSIX_SEM);
1086 	COUNTER_INC(posixsem_create);
1087 }
1088 
1089 COUNTER_DECL(posixsem_destroy_label);
1090 static void
1091 test_posixsem_destroy_label(struct label *label)
1092 {
1093 
1094 	LABEL_DESTROY(label, MAGIC_POSIX_SEM);
1095 	COUNTER_INC(posixsem_destroy_label);
1096 }
1097 
1098 COUNTER_DECL(posixsem_init_label);
1099 static void
1100 test_posixsem_init_label(struct label *label)
1101 {
1102 
1103 	LABEL_INIT(label, MAGIC_POSIX_SEM);
1104 	COUNTER_INC(posixsem_init_label);
1105 }
1106 
1107 COUNTER_DECL(posixshm_check_mmap);
1108 static int
1109 test_posixshm_check_mmap(struct ucred *cred, struct shmfd *shmfd,
1110     struct label *shmfdlabel, int prot, int flags)
1111 {
1112 
1113 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1114 	LABEL_CHECK(shmfdlabel, MAGIC_POSIX_SHM);
1115 	return (0);
1116 }
1117 
1118 COUNTER_DECL(posixshm_check_open);
1119 static int
1120 test_posixshm_check_open(struct ucred *cred, struct shmfd *shmfd,
1121     struct label *shmfdlabel)
1122 {
1123 
1124 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1125 	LABEL_CHECK(shmfdlabel, MAGIC_POSIX_SHM);
1126 	return (0);
1127 }
1128 
1129 COUNTER_DECL(posixshm_check_stat);
1130 static int
1131 test_posixshm_check_stat(struct ucred *active_cred,
1132     struct ucred *file_cred, struct shmfd *shmfd, struct label *shmfdlabel)
1133 {
1134 
1135 	LABEL_CHECK(active_cred->cr_label, MAGIC_CRED);
1136 	LABEL_CHECK(file_cred->cr_label, MAGIC_CRED);
1137 	LABEL_CHECK(shmfdlabel, MAGIC_POSIX_SHM);
1138 	return (0);
1139 }
1140 
1141 COUNTER_DECL(posixshm_check_truncate);
1142 static int
1143 test_posixshm_check_truncate(struct ucred *active_cred,
1144     struct ucred *file_cred, struct shmfd *shmfd, struct label *shmfdlabel)
1145 {
1146 
1147 	LABEL_CHECK(active_cred->cr_label, MAGIC_CRED);
1148 	LABEL_CHECK(file_cred->cr_label, MAGIC_CRED);
1149 	LABEL_CHECK(shmfdlabel, MAGIC_POSIX_SHM);
1150 	return (0);
1151 }
1152 
1153 COUNTER_DECL(posixshm_check_unlink);
1154 static int
1155 test_posixshm_check_unlink(struct ucred *cred, struct shmfd *shmfd,
1156     struct label *shmfdlabel)
1157 {
1158 
1159 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1160 	LABEL_CHECK(shmfdlabel, MAGIC_POSIX_SHM);
1161 	return (0);
1162 }
1163 
1164 COUNTER_DECL(posixshm_create);
1165 static void
1166 test_posixshm_create(struct ucred *cred, struct shmfd *shmfd,
1167    struct label *shmfdlabel)
1168 {
1169 
1170 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1171 	LABEL_CHECK(shmfdlabel, MAGIC_POSIX_SHM);
1172 	COUNTER_INC(posixshm_create);
1173 }
1174 
1175 COUNTER_DECL(posixshm_destroy_label);
1176 static void
1177 test_posixshm_destroy_label(struct label *label)
1178 {
1179 
1180 	LABEL_DESTROY(label, MAGIC_POSIX_SHM);
1181 	COUNTER_INC(posixshm_destroy_label);
1182 }
1183 
1184 COUNTER_DECL(posixshm_init_label);
1185 static void
1186 test_posixshm_init_label(struct label *label)
1187 {
1188 
1189 	LABEL_INIT(label, MAGIC_POSIX_SHM);
1190 	COUNTER_INC(posixshm_init_label);
1191 }
1192 
1193 COUNTER_DECL(proc_check_debug);
1194 static int
1195 test_proc_check_debug(struct ucred *cred, struct proc *p)
1196 {
1197 
1198 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1199 	LABEL_CHECK(p->p_ucred->cr_label, MAGIC_CRED);
1200 	COUNTER_INC(proc_check_debug);
1201 
1202 	return (0);
1203 }
1204 
1205 COUNTER_DECL(proc_check_sched);
1206 static int
1207 test_proc_check_sched(struct ucred *cred, struct proc *p)
1208 {
1209 
1210 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1211 	LABEL_CHECK(p->p_ucred->cr_label, MAGIC_CRED);
1212 	COUNTER_INC(proc_check_sched);
1213 
1214 	return (0);
1215 }
1216 
1217 COUNTER_DECL(proc_check_signal);
1218 static int
1219 test_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
1220 {
1221 
1222 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1223 	LABEL_CHECK(p->p_ucred->cr_label, MAGIC_CRED);
1224 	COUNTER_INC(proc_check_signal);
1225 
1226 	return (0);
1227 }
1228 
1229 COUNTER_DECL(proc_check_setaudit);
1230 static int
1231 test_proc_check_setaudit(struct ucred *cred, struct auditinfo *ai)
1232 {
1233 
1234 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1235 	COUNTER_INC(proc_check_setaudit);
1236 
1237 	return (0);
1238 }
1239 
1240 COUNTER_DECL(proc_check_setaudit_addr);
1241 static int
1242 test_proc_check_setaudit_addr(struct ucred *cred,
1243     struct auditinfo_addr *aia)
1244 {
1245 
1246 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1247 	COUNTER_INC(proc_check_setaudit_addr);
1248 
1249 	return (0);
1250 }
1251 
1252 COUNTER_DECL(proc_check_setauid);
1253 static int
1254 test_proc_check_setauid(struct ucred *cred, uid_t auid)
1255 {
1256 
1257 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1258 	COUNTER_INC(proc_check_setauid);
1259 
1260 	return (0);
1261 }
1262 
1263 COUNTER_DECL(proc_check_setegid);
1264 static int
1265 test_proc_check_setegid(struct ucred *cred, gid_t egid)
1266 {
1267 
1268 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1269 	COUNTER_INC(proc_check_setegid);
1270 
1271 	return (0);
1272 }
1273 
1274 COUNTER_DECL(proc_check_euid);
1275 static int
1276 test_proc_check_seteuid(struct ucred *cred, uid_t euid)
1277 {
1278 
1279 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1280 	COUNTER_INC(proc_check_euid);
1281 
1282 	return (0);
1283 }
1284 
1285 COUNTER_DECL(proc_check_setregid);
1286 static int
1287 test_proc_check_setregid(struct ucred *cred, gid_t rgid, gid_t egid)
1288 {
1289 
1290 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1291 	COUNTER_INC(proc_check_setregid);
1292 
1293 	return (0);
1294 }
1295 
1296 COUNTER_DECL(proc_check_setreuid);
1297 static int
1298 test_proc_check_setreuid(struct ucred *cred, uid_t ruid, uid_t euid)
1299 {
1300 
1301 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1302 	COUNTER_INC(proc_check_setreuid);
1303 
1304 	return (0);
1305 }
1306 
1307 COUNTER_DECL(proc_check_setgid);
1308 static int
1309 test_proc_check_setgid(struct ucred *cred, gid_t gid)
1310 {
1311 
1312 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1313 	COUNTER_INC(proc_check_setgid);
1314 
1315 	return (0);
1316 }
1317 
1318 COUNTER_DECL(proc_check_setgroups);
1319 static int
1320 test_proc_check_setgroups(struct ucred *cred, int ngroups,
1321 	gid_t *gidset)
1322 {
1323 
1324 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1325 	COUNTER_INC(proc_check_setgroups);
1326 
1327 	return (0);
1328 }
1329 
1330 COUNTER_DECL(proc_check_setresgid);
1331 static int
1332 test_proc_check_setresgid(struct ucred *cred, gid_t rgid, gid_t egid,
1333 	gid_t sgid)
1334 {
1335 
1336 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1337 	COUNTER_INC(proc_check_setresgid);
1338 
1339 	return (0);
1340 }
1341 
1342 COUNTER_DECL(proc_check_setresuid);
1343 static int
1344 test_proc_check_setresuid(struct ucred *cred, uid_t ruid, uid_t euid,
1345 	uid_t suid)
1346 {
1347 
1348 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1349 	COUNTER_INC(proc_check_setresuid);
1350 
1351 	return (0);
1352 }
1353 
1354 COUNTER_DECL(proc_check_setuid);
1355 static int
1356 test_proc_check_setuid(struct ucred *cred, uid_t uid)
1357 {
1358 
1359 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1360 	COUNTER_INC(proc_check_setuid);
1361 
1362 	return (0);
1363 }
1364 
1365 COUNTER_DECL(proc_check_wait);
1366 static int
1367 test_proc_check_wait(struct ucred *cred, struct proc *p)
1368 {
1369 
1370 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1371 	LABEL_CHECK(p->p_ucred->cr_label, MAGIC_CRED);
1372 	COUNTER_INC(proc_check_wait);
1373 
1374 	return (0);
1375 }
1376 
1377 COUNTER_DECL(proc_create_init);
1378 static void
1379 test_proc_create_init(struct ucred *cred)
1380 {
1381 
1382 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1383 	COUNTER_INC(proc_create_init);
1384 }
1385 
1386 COUNTER_DECL(proc_create_swapper);
1387 static void
1388 test_proc_create_swapper(struct ucred *cred)
1389 {
1390 
1391 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1392 	COUNTER_INC(proc_create_swapper);
1393 }
1394 
1395 COUNTER_DECL(proc_destroy_label);
1396 static void
1397 test_proc_destroy_label(struct label *label)
1398 {
1399 
1400 	LABEL_DESTROY(label, MAGIC_PROC);
1401 	COUNTER_INC(proc_destroy_label);
1402 }
1403 
1404 COUNTER_DECL(proc_init_label);
1405 static void
1406 test_proc_init_label(struct label *label)
1407 {
1408 
1409 	LABEL_INIT(label, MAGIC_PROC);
1410 	COUNTER_INC(proc_init_label);
1411 }
1412 
1413 COUNTER_DECL(socket_check_accept);
1414 static int
1415 test_socket_check_accept(struct ucred *cred, struct socket *so,
1416     struct label *solabel)
1417 {
1418 
1419 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1420 	LABEL_CHECK(solabel, MAGIC_SOCKET);
1421 	COUNTER_INC(socket_check_accept);
1422 
1423 	return (0);
1424 }
1425 
1426 COUNTER_DECL(socket_check_bind);
1427 static int
1428 test_socket_check_bind(struct ucred *cred, struct socket *so,
1429     struct label *solabel, struct sockaddr *sa)
1430 {
1431 
1432 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1433 	LABEL_CHECK(solabel, MAGIC_SOCKET);
1434 	COUNTER_INC(socket_check_bind);
1435 
1436 	return (0);
1437 }
1438 
1439 COUNTER_DECL(socket_check_connect);
1440 static int
1441 test_socket_check_connect(struct ucred *cred, struct socket *so,
1442     struct label *solabel, struct sockaddr *sa)
1443 {
1444 
1445 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1446 	LABEL_CHECK(solabel, MAGIC_SOCKET);
1447 	COUNTER_INC(socket_check_connect);
1448 
1449 	return (0);
1450 }
1451 
1452 COUNTER_DECL(socket_check_deliver);
1453 static int
1454 test_socket_check_deliver(struct socket *so, struct label *solabel,
1455     struct mbuf *m, struct label *mlabel)
1456 {
1457 
1458 	LABEL_CHECK(solabel, MAGIC_SOCKET);
1459 	LABEL_CHECK(mlabel, MAGIC_MBUF);
1460 	COUNTER_INC(socket_check_deliver);
1461 
1462 	return (0);
1463 }
1464 
1465 COUNTER_DECL(socket_check_listen);
1466 static int
1467 test_socket_check_listen(struct ucred *cred, struct socket *so,
1468     struct label *solabel)
1469 {
1470 
1471 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1472 	LABEL_CHECK(solabel, MAGIC_SOCKET);
1473 	COUNTER_INC(socket_check_listen);
1474 
1475 	return (0);
1476 }
1477 
1478 COUNTER_DECL(socket_check_poll);
1479 static int
1480 test_socket_check_poll(struct ucred *cred, struct socket *so,
1481     struct label *solabel)
1482 {
1483 
1484 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1485 	LABEL_CHECK(solabel, MAGIC_SOCKET);
1486 	COUNTER_INC(socket_check_poll);
1487 
1488 	return (0);
1489 }
1490 
1491 COUNTER_DECL(socket_check_receive);
1492 static int
1493 test_socket_check_receive(struct ucred *cred, struct socket *so,
1494     struct label *solabel)
1495 {
1496 
1497 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1498 	LABEL_CHECK(solabel, MAGIC_SOCKET);
1499 	COUNTER_INC(socket_check_receive);
1500 
1501 	return (0);
1502 }
1503 
1504 COUNTER_DECL(socket_check_relabel);
1505 static int
1506 test_socket_check_relabel(struct ucred *cred, struct socket *so,
1507     struct label *solabel, struct label *newlabel)
1508 {
1509 
1510 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1511 	LABEL_CHECK(solabel, MAGIC_SOCKET);
1512 	LABEL_CHECK(newlabel, MAGIC_SOCKET);
1513 	COUNTER_INC(socket_check_relabel);
1514 
1515 	return (0);
1516 }
1517 
1518 COUNTER_DECL(socket_check_send);
1519 static int
1520 test_socket_check_send(struct ucred *cred, struct socket *so,
1521     struct label *solabel)
1522 {
1523 
1524 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1525 	LABEL_CHECK(solabel, MAGIC_SOCKET);
1526 	COUNTER_INC(socket_check_send);
1527 
1528 	return (0);
1529 }
1530 
1531 COUNTER_DECL(socket_check_stat);
1532 static int
1533 test_socket_check_stat(struct ucred *cred, struct socket *so,
1534     struct label *solabel)
1535 {
1536 
1537 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1538 	LABEL_CHECK(solabel, MAGIC_SOCKET);
1539 	COUNTER_INC(socket_check_stat);
1540 
1541 	return (0);
1542 }
1543 
1544 COUNTER_DECL(socket_check_visible);
1545 static int
1546 test_socket_check_visible(struct ucred *cred, struct socket *so,
1547     struct label *solabel)
1548 {
1549 
1550 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1551 	LABEL_CHECK(solabel, MAGIC_SOCKET);
1552 	COUNTER_INC(socket_check_visible);
1553 
1554 	return (0);
1555 }
1556 
1557 COUNTER_DECL(socket_copy_label);
1558 static void
1559 test_socket_copy_label(struct label *src, struct label *dest)
1560 {
1561 
1562 	LABEL_CHECK(src, MAGIC_SOCKET);
1563 	LABEL_CHECK(dest, MAGIC_SOCKET);
1564 	COUNTER_INC(socket_copy_label);
1565 }
1566 
1567 COUNTER_DECL(socket_create);
1568 static void
1569 test_socket_create(struct ucred *cred, struct socket *so,
1570     struct label *solabel)
1571 {
1572 
1573 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1574 	LABEL_CHECK(solabel, MAGIC_SOCKET);
1575 	COUNTER_INC(socket_create);
1576 }
1577 
1578 COUNTER_DECL(socket_create_mbuf);
1579 static void
1580 test_socket_create_mbuf(struct socket *so, struct label *socketlabel,
1581     struct mbuf *m, struct label *mlabel)
1582 {
1583 
1584 	LABEL_CHECK(socketlabel, MAGIC_SOCKET);
1585 	LABEL_CHECK(mlabel, MAGIC_MBUF);
1586 	COUNTER_INC(socket_create_mbuf);
1587 }
1588 
1589 COUNTER_DECL(socket_destroy_label);
1590 static void
1591 test_socket_destroy_label(struct label *label)
1592 {
1593 
1594 	LABEL_DESTROY(label, MAGIC_SOCKET);
1595 	COUNTER_INC(socket_destroy_label);
1596 }
1597 
1598 COUNTER_DECL(socket_externalize_label);
1599 static int
1600 test_socket_externalize_label(struct label *label, char *element_name,
1601     struct sbuf *sb, int *claimed)
1602 {
1603 
1604 	LABEL_CHECK(label, MAGIC_SOCKET);
1605 	COUNTER_INC(socket_externalize_label);
1606 
1607 	return (0);
1608 }
1609 
1610 COUNTER_DECL(socket_init_label);
1611 static int
1612 test_socket_init_label(struct label *label, int flag)
1613 {
1614 
1615 	if (flag & M_WAITOK)
1616 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1617 		    "test_socket_init_label() at %s:%d", __FILE__,
1618 		    __LINE__);
1619 
1620 	LABEL_INIT(label, MAGIC_SOCKET);
1621 	COUNTER_INC(socket_init_label);
1622 	return (0);
1623 }
1624 
1625 COUNTER_DECL(socket_internalize_label);
1626 static int
1627 test_socket_internalize_label(struct label *label, char *element_name,
1628     char *element_data, int *claimed)
1629 {
1630 
1631 	LABEL_CHECK(label, MAGIC_SOCKET);
1632 	COUNTER_INC(socket_internalize_label);
1633 
1634 	return (0);
1635 }
1636 
1637 COUNTER_DECL(socket_newconn);
1638 static void
1639 test_socket_newconn(struct socket *oldso, struct label *oldsolabel,
1640     struct socket *newso, struct label *newsolabel)
1641 {
1642 
1643 	LABEL_CHECK(oldsolabel, MAGIC_SOCKET);
1644 	LABEL_CHECK(newsolabel, MAGIC_SOCKET);
1645 	COUNTER_INC(socket_newconn);
1646 }
1647 
1648 COUNTER_DECL(socket_relabel);
1649 static void
1650 test_socket_relabel(struct ucred *cred, struct socket *so,
1651     struct label *solabel, struct label *newlabel)
1652 {
1653 
1654 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1655 	LABEL_CHECK(solabel, MAGIC_SOCKET);
1656 	LABEL_CHECK(newlabel, MAGIC_SOCKET);
1657 	COUNTER_INC(socket_relabel);
1658 }
1659 
1660 COUNTER_DECL(socketpeer_destroy_label);
1661 static void
1662 test_socketpeer_destroy_label(struct label *label)
1663 {
1664 
1665 	LABEL_DESTROY(label, MAGIC_SOCKET);
1666 	COUNTER_INC(socketpeer_destroy_label);
1667 }
1668 
1669 COUNTER_DECL(socketpeer_externalize_label);
1670 static int
1671 test_socketpeer_externalize_label(struct label *label, char *element_name,
1672     struct sbuf *sb, int *claimed)
1673 {
1674 
1675 	LABEL_CHECK(label, MAGIC_SOCKET);
1676 	COUNTER_INC(socketpeer_externalize_label);
1677 
1678 	return (0);
1679 }
1680 
1681 COUNTER_DECL(socketpeer_init_label);
1682 static int
1683 test_socketpeer_init_label(struct label *label, int flag)
1684 {
1685 
1686 	if (flag & M_WAITOK)
1687 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1688 		    "test_socketpeer_init_label() at %s:%d", __FILE__,
1689 		    __LINE__);
1690 
1691 	LABEL_INIT(label, MAGIC_SOCKET);
1692 	COUNTER_INC(socketpeer_init_label);
1693 	return (0);
1694 }
1695 
1696 COUNTER_DECL(socketpeer_set_from_mbuf);
1697 static void
1698 test_socketpeer_set_from_mbuf(struct mbuf *m, struct label *mlabel,
1699     struct socket *socket, struct label *socketpeerlabel)
1700 {
1701 
1702 	LABEL_CHECK(mlabel, MAGIC_MBUF);
1703 	LABEL_CHECK(socketpeerlabel, MAGIC_SOCKET);
1704 	COUNTER_INC(socketpeer_set_from_mbuf);
1705 }
1706 
1707 COUNTER_DECL(socketpeer_set_from_socket);
1708 static void
1709 test_socketpeer_set_from_socket(struct socket *oldso,
1710     struct label *oldsolabel, struct socket *newso,
1711     struct label *newsopeerlabel)
1712 {
1713 
1714 	LABEL_CHECK(oldsolabel, MAGIC_SOCKET);
1715 	LABEL_CHECK(newsopeerlabel, MAGIC_SOCKET);
1716 	COUNTER_INC(socketpeer_set_from_socket);
1717 }
1718 
1719 COUNTER_DECL(syncache_create);
1720 static void
1721 test_syncache_create(struct label *label, struct inpcb *inp)
1722 {
1723 
1724 	LABEL_CHECK(label, MAGIC_SYNCACHE);
1725 	COUNTER_INC(syncache_create);
1726 }
1727 
1728 COUNTER_DECL(syncache_create_mbuf);
1729 static void
1730 test_syncache_create_mbuf(struct label *sc_label, struct mbuf *m,
1731     struct label *mlabel)
1732 {
1733 
1734 	LABEL_CHECK(sc_label, MAGIC_SYNCACHE);
1735 	LABEL_CHECK(mlabel, MAGIC_MBUF);
1736 	COUNTER_INC(syncache_create_mbuf);
1737 }
1738 
1739 COUNTER_DECL(syncache_destroy_label);
1740 static void
1741 test_syncache_destroy_label(struct label *label)
1742 {
1743 
1744 	LABEL_DESTROY(label, MAGIC_SYNCACHE);
1745 	COUNTER_INC(syncache_destroy_label);
1746 }
1747 
1748 COUNTER_DECL(syncache_init_label);
1749 static int
1750 test_syncache_init_label(struct label *label, int flag)
1751 {
1752 
1753 	if (flag & M_WAITOK)
1754 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1755 		    "test_syncache_init_label() at %s:%d", __FILE__,
1756 		    __LINE__);
1757 	LABEL_INIT(label, MAGIC_SYNCACHE);
1758 	COUNTER_INC(syncache_init_label);
1759 	return (0);
1760 }
1761 
1762 COUNTER_DECL(system_check_acct);
1763 static int
1764 test_system_check_acct(struct ucred *cred, struct vnode *vp,
1765     struct label *vplabel)
1766 {
1767 
1768 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1769 	LABEL_CHECK(vplabel, MAGIC_VNODE);
1770 	COUNTER_INC(system_check_acct);
1771 
1772 	return (0);
1773 }
1774 
1775 COUNTER_DECL(system_check_audit);
1776 static int
1777 test_system_check_audit(struct ucred *cred, void *record, int length)
1778 {
1779 
1780 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1781 	COUNTER_INC(system_check_audit);
1782 
1783 	return (0);
1784 }
1785 
1786 COUNTER_DECL(system_check_auditctl);
1787 static int
1788 test_system_check_auditctl(struct ucred *cred, struct vnode *vp,
1789     struct label *vplabel)
1790 {
1791 
1792 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1793 	LABEL_CHECK(vplabel, MAGIC_VNODE);
1794 	COUNTER_INC(system_check_auditctl);
1795 
1796 	return (0);
1797 }
1798 
1799 COUNTER_DECL(system_check_auditon);
1800 static int
1801 test_system_check_auditon(struct ucred *cred, int cmd)
1802 {
1803 
1804 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1805 	COUNTER_INC(system_check_auditon);
1806 
1807 	return (0);
1808 }
1809 
1810 COUNTER_DECL(system_check_reboot);
1811 static int
1812 test_system_check_reboot(struct ucred *cred, int how)
1813 {
1814 
1815 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1816 	COUNTER_INC(system_check_reboot);
1817 
1818 	return (0);
1819 }
1820 
1821 COUNTER_DECL(system_check_swapoff);
1822 static int
1823 test_system_check_swapoff(struct ucred *cred, struct vnode *vp,
1824     struct label *vplabel)
1825 {
1826 
1827 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1828 	LABEL_CHECK(vplabel, MAGIC_VNODE);
1829 	COUNTER_INC(system_check_swapoff);
1830 
1831 	return (0);
1832 }
1833 
1834 COUNTER_DECL(system_check_swapon);
1835 static int
1836 test_system_check_swapon(struct ucred *cred, struct vnode *vp,
1837     struct label *vplabel)
1838 {
1839 
1840 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1841 	LABEL_CHECK(vplabel, MAGIC_VNODE);
1842 	COUNTER_INC(system_check_swapon);
1843 
1844 	return (0);
1845 }
1846 
1847 COUNTER_DECL(system_check_sysctl);
1848 static int
1849 test_system_check_sysctl(struct ucred *cred, struct sysctl_oid *oidp,
1850     void *arg1, int arg2, struct sysctl_req *req)
1851 {
1852 
1853 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1854 	COUNTER_INC(system_check_sysctl);
1855 
1856 	return (0);
1857 }
1858 
1859 COUNTER_DECL(sysvmsg_cleanup);
1860 static void
1861 test_sysvmsg_cleanup(struct label *msglabel)
1862 {
1863 
1864 	LABEL_CHECK(msglabel, MAGIC_SYSV_MSG);
1865 	COUNTER_INC(sysvmsg_cleanup);
1866 }
1867 
1868 COUNTER_DECL(sysvmsg_create);
1869 static void
1870 test_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr,
1871     struct label *msqlabel, struct msg *msgptr, struct label *msglabel)
1872 {
1873 
1874 	LABEL_CHECK(msglabel, MAGIC_SYSV_MSG);
1875 	LABEL_CHECK(msqlabel, MAGIC_SYSV_MSQ);
1876 	COUNTER_INC(sysvmsg_create);
1877 }
1878 
1879 COUNTER_DECL(sysvmsg_destroy_label);
1880 static void
1881 test_sysvmsg_destroy_label(struct label *label)
1882 {
1883 
1884 	LABEL_DESTROY(label, MAGIC_SYSV_MSG);
1885 	COUNTER_INC(sysvmsg_destroy_label);
1886 }
1887 
1888 COUNTER_DECL(sysvmsg_init_label);
1889 static void
1890 test_sysvmsg_init_label(struct label *label)
1891 {
1892 	LABEL_INIT(label, MAGIC_SYSV_MSG);
1893 	COUNTER_INC(sysvmsg_init_label);
1894 }
1895 
1896 COUNTER_DECL(sysvmsq_check_msgmsq);
1897 static int
1898 test_sysvmsq_check_msgmsq(struct ucred *cred, struct msg *msgptr,
1899     struct label *msglabel, struct msqid_kernel *msqkptr,
1900     struct label *msqklabel)
1901 {
1902 
1903 	LABEL_CHECK(msqklabel, MAGIC_SYSV_MSQ);
1904 	LABEL_CHECK(msglabel, MAGIC_SYSV_MSG);
1905 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1906 	COUNTER_INC(sysvmsq_check_msgmsq);
1907 
1908   	return (0);
1909 }
1910 
1911 COUNTER_DECL(sysvmsq_check_msgrcv);
1912 static int
1913 test_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr,
1914     struct label *msglabel)
1915 {
1916 
1917 	LABEL_CHECK(msglabel, MAGIC_SYSV_MSG);
1918 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1919 	COUNTER_INC(sysvmsq_check_msgrcv);
1920 
1921 	return (0);
1922 }
1923 
1924 COUNTER_DECL(sysvmsq_check_msgrmid);
1925 static int
1926 test_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr,
1927     struct label *msglabel)
1928 {
1929 
1930 	LABEL_CHECK(msglabel, MAGIC_SYSV_MSG);
1931 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1932 	COUNTER_INC(sysvmsq_check_msgrmid);
1933 
1934 	return (0);
1935 }
1936 
1937 COUNTER_DECL(sysvmsq_check_msqget);
1938 static int
1939 test_sysvmsq_check_msqget(struct ucred *cred,
1940     struct msqid_kernel *msqkptr, struct label *msqklabel)
1941 {
1942 
1943 	LABEL_CHECK(msqklabel, MAGIC_SYSV_MSQ);
1944 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1945 	COUNTER_INC(sysvmsq_check_msqget);
1946 
1947 	return (0);
1948 }
1949 
1950 COUNTER_DECL(sysvmsq_check_msqsnd);
1951 static int
1952 test_sysvmsq_check_msqsnd(struct ucred *cred,
1953     struct msqid_kernel *msqkptr, struct label *msqklabel)
1954 {
1955 
1956 	LABEL_CHECK(msqklabel, MAGIC_SYSV_MSQ);
1957 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1958 	COUNTER_INC(sysvmsq_check_msqsnd);
1959 
1960 	return (0);
1961 }
1962 
1963 COUNTER_DECL(sysvmsq_check_msqrcv);
1964 static int
1965 test_sysvmsq_check_msqrcv(struct ucred *cred,
1966     struct msqid_kernel *msqkptr, struct label *msqklabel)
1967 {
1968 
1969 	LABEL_CHECK(msqklabel, MAGIC_SYSV_MSQ);
1970 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1971 	COUNTER_INC(sysvmsq_check_msqrcv);
1972 
1973 	return (0);
1974 }
1975 
1976 COUNTER_DECL(sysvmsq_check_msqctl);
1977 static int
1978 test_sysvmsq_check_msqctl(struct ucred *cred,
1979     struct msqid_kernel *msqkptr, struct label *msqklabel, int cmd)
1980 {
1981 
1982 	LABEL_CHECK(msqklabel, MAGIC_SYSV_MSQ);
1983 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
1984 	COUNTER_INC(sysvmsq_check_msqctl);
1985 
1986 	return (0);
1987 }
1988 
1989 COUNTER_DECL(sysvmsq_cleanup);
1990 static void
1991 test_sysvmsq_cleanup(struct label *msqlabel)
1992 {
1993 
1994 	LABEL_CHECK(msqlabel, MAGIC_SYSV_MSQ);
1995 	COUNTER_INC(sysvmsq_cleanup);
1996 }
1997 
1998 COUNTER_DECL(sysvmsq_create);
1999 static void
2000 test_sysvmsq_create(struct ucred *cred,
2001     struct msqid_kernel *msqkptr, struct label *msqlabel)
2002 {
2003 
2004 	LABEL_CHECK(msqlabel, MAGIC_SYSV_MSQ);
2005 	COUNTER_INC(sysvmsq_create);
2006 }
2007 
2008 COUNTER_DECL(sysvmsq_destroy_label);
2009 static void
2010 test_sysvmsq_destroy_label(struct label *label)
2011 {
2012 
2013 	LABEL_DESTROY(label, MAGIC_SYSV_MSQ);
2014 	COUNTER_INC(sysvmsq_destroy_label);
2015 }
2016 
2017 COUNTER_DECL(sysvmsq_init_label);
2018 static void
2019 test_sysvmsq_init_label(struct label *label)
2020 {
2021 	LABEL_INIT(label, MAGIC_SYSV_MSQ);
2022 	COUNTER_INC(sysvmsq_init_label);
2023 }
2024 
2025 COUNTER_DECL(sysvsem_check_semctl);
2026 static int
2027 test_sysvsem_check_semctl(struct ucred *cred,
2028     struct semid_kernel *semakptr, struct label *semaklabel, int cmd)
2029 {
2030 
2031 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2032 	LABEL_CHECK(semaklabel, MAGIC_SYSV_SEM);
2033 	COUNTER_INC(sysvsem_check_semctl);
2034 
2035   	return (0);
2036 }
2037 
2038 COUNTER_DECL(sysvsem_check_semget);
2039 static int
2040 test_sysvsem_check_semget(struct ucred *cred,
2041     struct semid_kernel *semakptr, struct label *semaklabel)
2042 {
2043 
2044 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2045 	LABEL_CHECK(semaklabel, MAGIC_SYSV_SEM);
2046 	COUNTER_INC(sysvsem_check_semget);
2047 
2048 	return (0);
2049 }
2050 
2051 COUNTER_DECL(sysvsem_check_semop);
2052 static int
2053 test_sysvsem_check_semop(struct ucred *cred,
2054     struct semid_kernel *semakptr, struct label *semaklabel, size_t accesstype)
2055 {
2056 
2057 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2058 	LABEL_CHECK(semaklabel, MAGIC_SYSV_SEM);
2059 	COUNTER_INC(sysvsem_check_semop);
2060 
2061 	return (0);
2062 }
2063 
2064 COUNTER_DECL(sysvsem_cleanup);
2065 static void
2066 test_sysvsem_cleanup(struct label *semalabel)
2067 {
2068 
2069 	LABEL_CHECK(semalabel, MAGIC_SYSV_SEM);
2070 	COUNTER_INC(sysvsem_cleanup);
2071 }
2072 
2073 COUNTER_DECL(sysvsem_create);
2074 static void
2075 test_sysvsem_create(struct ucred *cred, struct semid_kernel *semakptr,
2076     struct label *semalabel)
2077 {
2078 
2079 	LABEL_CHECK(semalabel, MAGIC_SYSV_SEM);
2080 	COUNTER_INC(sysvsem_create);
2081 }
2082 
2083 COUNTER_DECL(sysvsem_destroy_label);
2084 static void
2085 test_sysvsem_destroy_label(struct label *label)
2086 {
2087 
2088 	LABEL_DESTROY(label, MAGIC_SYSV_SEM);
2089 	COUNTER_INC(sysvsem_destroy_label);
2090 }
2091 
2092 COUNTER_DECL(sysvsem_init_label);
2093 static void
2094 test_sysvsem_init_label(struct label *label)
2095 {
2096 	LABEL_INIT(label, MAGIC_SYSV_SEM);
2097 	COUNTER_INC(sysvsem_init_label);
2098 }
2099 
2100 COUNTER_DECL(sysvshm_check_shmat);
2101 static int
2102 test_sysvshm_check_shmat(struct ucred *cred,
2103     struct shmid_kernel *shmsegptr, struct label *shmseglabel, int shmflg)
2104 {
2105 
2106 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2107 	LABEL_CHECK(shmseglabel, MAGIC_SYSV_SHM);
2108 	COUNTER_INC(sysvshm_check_shmat);
2109 
2110   	return (0);
2111 }
2112 
2113 COUNTER_DECL(sysvshm_check_shmctl);
2114 static int
2115 test_sysvshm_check_shmctl(struct ucred *cred,
2116     struct shmid_kernel *shmsegptr, struct label *shmseglabel, int cmd)
2117 {
2118 
2119 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2120 	LABEL_CHECK(shmseglabel, MAGIC_SYSV_SHM);
2121 	COUNTER_INC(sysvshm_check_shmctl);
2122 
2123   	return (0);
2124 }
2125 
2126 COUNTER_DECL(sysvshm_check_shmdt);
2127 static int
2128 test_sysvshm_check_shmdt(struct ucred *cred,
2129     struct shmid_kernel *shmsegptr, struct label *shmseglabel)
2130 {
2131 
2132 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2133 	LABEL_CHECK(shmseglabel, MAGIC_SYSV_SHM);
2134 	COUNTER_INC(sysvshm_check_shmdt);
2135 
2136 	return (0);
2137 }
2138 
2139 COUNTER_DECL(sysvshm_check_shmget);
2140 static int
2141 test_sysvshm_check_shmget(struct ucred *cred,
2142     struct shmid_kernel *shmsegptr, struct label *shmseglabel, int shmflg)
2143 {
2144 
2145 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2146 	LABEL_CHECK(shmseglabel, MAGIC_SYSV_SHM);
2147 	COUNTER_INC(sysvshm_check_shmget);
2148 
2149 	return (0);
2150 }
2151 
2152 COUNTER_DECL(sysvshm_cleanup);
2153 static void
2154 test_sysvshm_cleanup(struct label *shmlabel)
2155 {
2156 
2157 	LABEL_CHECK(shmlabel, MAGIC_SYSV_SHM);
2158 	COUNTER_INC(sysvshm_cleanup);
2159 }
2160 
2161 COUNTER_DECL(sysvshm_create);
2162 static void
2163 test_sysvshm_create(struct ucred *cred, struct shmid_kernel *shmsegptr,
2164     struct label *shmlabel)
2165 {
2166 
2167 	LABEL_CHECK(shmlabel, MAGIC_SYSV_SHM);
2168 	COUNTER_INC(sysvshm_create);
2169 }
2170 
2171 COUNTER_DECL(sysvshm_destroy_label);
2172 static void
2173 test_sysvshm_destroy_label(struct label *label)
2174 {
2175 
2176 	LABEL_DESTROY(label, MAGIC_SYSV_SHM);
2177 	COUNTER_INC(sysvshm_destroy_label);
2178 }
2179 
2180 COUNTER_DECL(sysvshm_init_label);
2181 static void
2182 test_sysvshm_init_label(struct label *label)
2183 {
2184 	LABEL_INIT(label, MAGIC_SYSV_SHM);
2185 	COUNTER_INC(sysvshm_init_label);
2186 }
2187 
2188 COUNTER_DECL(thread_userret);
2189 static void
2190 test_thread_userret(struct thread *td)
2191 {
2192 
2193 	COUNTER_INC(thread_userret);
2194 }
2195 
2196 COUNTER_DECL(vnode_associate_extattr);
2197 static int
2198 test_vnode_associate_extattr(struct mount *mp, struct label *mplabel,
2199     struct vnode *vp, struct label *vplabel)
2200 {
2201 
2202 	LABEL_CHECK(mplabel, MAGIC_MOUNT);
2203 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2204 	COUNTER_INC(vnode_associate_extattr);
2205 
2206 	return (0);
2207 }
2208 
2209 COUNTER_DECL(vnode_associate_singlelabel);
2210 static void
2211 test_vnode_associate_singlelabel(struct mount *mp, struct label *mplabel,
2212     struct vnode *vp, struct label *vplabel)
2213 {
2214 
2215 	LABEL_CHECK(mplabel, MAGIC_MOUNT);
2216 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2217 	COUNTER_INC(vnode_associate_singlelabel);
2218 }
2219 
2220 COUNTER_DECL(vnode_check_access);
2221 static int
2222 test_vnode_check_access(struct ucred *cred, struct vnode *vp,
2223     struct label *vplabel, int acc_mode)
2224 {
2225 
2226 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2227 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2228 	COUNTER_INC(vnode_check_access);
2229 
2230 	return (0);
2231 }
2232 
2233 COUNTER_DECL(vnode_check_chdir);
2234 static int
2235 test_vnode_check_chdir(struct ucred *cred, struct vnode *dvp,
2236     struct label *dvplabel)
2237 {
2238 
2239 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2240 	LABEL_CHECK(dvplabel, MAGIC_VNODE);
2241 	COUNTER_INC(vnode_check_chdir);
2242 
2243 	return (0);
2244 }
2245 
2246 COUNTER_DECL(vnode_check_chroot);
2247 static int
2248 test_vnode_check_chroot(struct ucred *cred, struct vnode *dvp,
2249     struct label *dvplabel)
2250 {
2251 
2252 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2253 	LABEL_CHECK(dvplabel, MAGIC_VNODE);
2254 	COUNTER_INC(vnode_check_chroot);
2255 
2256 	return (0);
2257 }
2258 
2259 COUNTER_DECL(vnode_check_create);
2260 static int
2261 test_vnode_check_create(struct ucred *cred, struct vnode *dvp,
2262     struct label *dvplabel, struct componentname *cnp, struct vattr *vap)
2263 {
2264 
2265 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2266 	LABEL_CHECK(dvplabel, MAGIC_VNODE);
2267 	COUNTER_INC(vnode_check_create);
2268 
2269 	return (0);
2270 }
2271 
2272 COUNTER_DECL(vnode_check_deleteacl);
2273 static int
2274 test_vnode_check_deleteacl(struct ucred *cred, struct vnode *vp,
2275     struct label *vplabel, acl_type_t type)
2276 {
2277 
2278 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2279 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2280 	COUNTER_INC(vnode_check_deleteacl);
2281 
2282 	return (0);
2283 }
2284 
2285 COUNTER_DECL(vnode_check_deleteextattr);
2286 static int
2287 test_vnode_check_deleteextattr(struct ucred *cred, struct vnode *vp,
2288     struct label *vplabel, int attrnamespace, const char *name)
2289 {
2290 
2291 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2292 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2293 	COUNTER_INC(vnode_check_deleteextattr);
2294 
2295 	return (0);
2296 }
2297 
2298 COUNTER_DECL(vnode_check_exec);
2299 static int
2300 test_vnode_check_exec(struct ucred *cred, struct vnode *vp,
2301     struct label *vplabel, struct image_params *imgp,
2302     struct label *execlabel)
2303 {
2304 
2305 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2306 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2307 	LABEL_CHECK(execlabel, MAGIC_CRED);
2308 	COUNTER_INC(vnode_check_exec);
2309 
2310 	return (0);
2311 }
2312 
2313 COUNTER_DECL(vnode_check_getacl);
2314 static int
2315 test_vnode_check_getacl(struct ucred *cred, struct vnode *vp,
2316     struct label *vplabel, acl_type_t type)
2317 {
2318 
2319 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2320 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2321 	COUNTER_INC(vnode_check_getacl);
2322 
2323 	return (0);
2324 }
2325 
2326 COUNTER_DECL(vnode_check_getextattr);
2327 static int
2328 test_vnode_check_getextattr(struct ucred *cred, struct vnode *vp,
2329     struct label *vplabel, int attrnamespace, const char *name,
2330     struct uio *uio)
2331 {
2332 
2333 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2334 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2335 	COUNTER_INC(vnode_check_getextattr);
2336 
2337 	return (0);
2338 }
2339 
2340 COUNTER_DECL(vnode_check_link);
2341 static int
2342 test_vnode_check_link(struct ucred *cred, struct vnode *dvp,
2343     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2344     struct componentname *cnp)
2345 {
2346 
2347 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2348 	LABEL_CHECK(dvplabel, MAGIC_VNODE);
2349 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2350 	COUNTER_INC(vnode_check_link);
2351 
2352 	return (0);
2353 }
2354 
2355 COUNTER_DECL(vnode_check_listextattr);
2356 static int
2357 test_vnode_check_listextattr(struct ucred *cred, struct vnode *vp,
2358     struct label *vplabel, int attrnamespace)
2359 {
2360 
2361 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2362 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2363 	COUNTER_INC(vnode_check_listextattr);
2364 
2365 	return (0);
2366 }
2367 
2368 COUNTER_DECL(vnode_check_lookup);
2369 static int
2370 test_vnode_check_lookup(struct ucred *cred, struct vnode *dvp,
2371     struct label *dvplabel, struct componentname *cnp)
2372 {
2373 
2374 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2375 	LABEL_CHECK(dvplabel, MAGIC_VNODE);
2376 	COUNTER_INC(vnode_check_lookup);
2377 
2378 	return (0);
2379 }
2380 
2381 COUNTER_DECL(vnode_check_mmap);
2382 static int
2383 test_vnode_check_mmap(struct ucred *cred, struct vnode *vp,
2384     struct label *vplabel, int prot, int flags)
2385 {
2386 
2387 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2388 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2389 	COUNTER_INC(vnode_check_mmap);
2390 
2391 	return (0);
2392 }
2393 
2394 COUNTER_DECL(vnode_check_open);
2395 static int
2396 test_vnode_check_open(struct ucred *cred, struct vnode *vp,
2397     struct label *vplabel, int acc_mode)
2398 {
2399 
2400 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2401 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2402 	COUNTER_INC(vnode_check_open);
2403 
2404 	return (0);
2405 }
2406 
2407 COUNTER_DECL(vnode_check_poll);
2408 static int
2409 test_vnode_check_poll(struct ucred *active_cred, struct ucred *file_cred,
2410     struct vnode *vp, struct label *vplabel)
2411 {
2412 
2413 	LABEL_CHECK(active_cred->cr_label, MAGIC_CRED);
2414 	if (file_cred != NULL)
2415 		LABEL_CHECK(file_cred->cr_label, MAGIC_CRED);
2416 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2417 	COUNTER_INC(vnode_check_poll);
2418 
2419 	return (0);
2420 }
2421 
2422 COUNTER_DECL(vnode_check_read);
2423 static int
2424 test_vnode_check_read(struct ucred *active_cred, struct ucred *file_cred,
2425     struct vnode *vp, struct label *vplabel)
2426 {
2427 
2428 	LABEL_CHECK(active_cred->cr_label, MAGIC_CRED);
2429 	if (file_cred != NULL)
2430 		LABEL_CHECK(file_cred->cr_label, MAGIC_CRED);
2431 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2432 	COUNTER_INC(vnode_check_read);
2433 
2434 	return (0);
2435 }
2436 
2437 COUNTER_DECL(vnode_check_readdir);
2438 static int
2439 test_vnode_check_readdir(struct ucred *cred, struct vnode *dvp,
2440     struct label *dvplabel)
2441 {
2442 
2443 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2444 	LABEL_CHECK(dvplabel, MAGIC_VNODE);
2445 	COUNTER_INC(vnode_check_readdir);
2446 
2447 	return (0);
2448 }
2449 
2450 COUNTER_DECL(vnode_check_readlink);
2451 static int
2452 test_vnode_check_readlink(struct ucred *cred, struct vnode *vp,
2453     struct label *vplabel)
2454 {
2455 
2456 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2457 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2458 	COUNTER_INC(vnode_check_readlink);
2459 
2460 	return (0);
2461 }
2462 
2463 COUNTER_DECL(vnode_check_relabel);
2464 static int
2465 test_vnode_check_relabel(struct ucred *cred, struct vnode *vp,
2466     struct label *vplabel, struct label *newlabel)
2467 {
2468 
2469 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2470 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2471 	LABEL_CHECK(newlabel, MAGIC_VNODE);
2472 	COUNTER_INC(vnode_check_relabel);
2473 
2474 	return (0);
2475 }
2476 
2477 COUNTER_DECL(vnode_check_rename_from);
2478 static int
2479 test_vnode_check_rename_from(struct ucred *cred, struct vnode *dvp,
2480     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2481     struct componentname *cnp)
2482 {
2483 
2484 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2485 	LABEL_CHECK(dvplabel, MAGIC_VNODE);
2486 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2487 	COUNTER_INC(vnode_check_rename_from);
2488 
2489 	return (0);
2490 }
2491 
2492 COUNTER_DECL(vnode_check_rename_to);
2493 static int
2494 test_vnode_check_rename_to(struct ucred *cred, struct vnode *dvp,
2495     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2496     int samedir, struct componentname *cnp)
2497 {
2498 
2499 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2500 	LABEL_CHECK(dvplabel, MAGIC_VNODE);
2501 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2502 	COUNTER_INC(vnode_check_rename_to);
2503 
2504 	return (0);
2505 }
2506 
2507 COUNTER_DECL(vnode_check_revoke);
2508 static int
2509 test_vnode_check_revoke(struct ucred *cred, struct vnode *vp,
2510     struct label *vplabel)
2511 {
2512 
2513 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2514 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2515 	COUNTER_INC(vnode_check_revoke);
2516 
2517 	return (0);
2518 }
2519 
2520 COUNTER_DECL(vnode_check_setacl);
2521 static int
2522 test_vnode_check_setacl(struct ucred *cred, struct vnode *vp,
2523     struct label *vplabel, acl_type_t type, struct acl *acl)
2524 {
2525 
2526 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2527 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2528 	COUNTER_INC(vnode_check_setacl);
2529 
2530 	return (0);
2531 }
2532 
2533 COUNTER_DECL(vnode_check_setextattr);
2534 static int
2535 test_vnode_check_setextattr(struct ucred *cred, struct vnode *vp,
2536     struct label *vplabel, int attrnamespace, const char *name,
2537     struct uio *uio)
2538 {
2539 
2540 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2541 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2542 	COUNTER_INC(vnode_check_setextattr);
2543 
2544 	return (0);
2545 }
2546 
2547 COUNTER_DECL(vnode_check_setflags);
2548 static int
2549 test_vnode_check_setflags(struct ucred *cred, struct vnode *vp,
2550     struct label *vplabel, u_long flags)
2551 {
2552 
2553 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2554 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2555 	COUNTER_INC(vnode_check_setflags);
2556 
2557 	return (0);
2558 }
2559 
2560 COUNTER_DECL(vnode_check_setmode);
2561 static int
2562 test_vnode_check_setmode(struct ucred *cred, struct vnode *vp,
2563     struct label *vplabel, mode_t mode)
2564 {
2565 
2566 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2567 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2568 	COUNTER_INC(vnode_check_setmode);
2569 
2570 	return (0);
2571 }
2572 
2573 COUNTER_DECL(vnode_check_setowner);
2574 static int
2575 test_vnode_check_setowner(struct ucred *cred, struct vnode *vp,
2576     struct label *vplabel, uid_t uid, gid_t gid)
2577 {
2578 
2579 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2580 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2581 	COUNTER_INC(vnode_check_setowner);
2582 
2583 	return (0);
2584 }
2585 
2586 COUNTER_DECL(vnode_check_setutimes);
2587 static int
2588 test_vnode_check_setutimes(struct ucred *cred, struct vnode *vp,
2589     struct label *vplabel, struct timespec atime, struct timespec mtime)
2590 {
2591 
2592 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2593 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2594 	COUNTER_INC(vnode_check_setutimes);
2595 
2596 	return (0);
2597 }
2598 
2599 COUNTER_DECL(vnode_check_stat);
2600 static int
2601 test_vnode_check_stat(struct ucred *active_cred, struct ucred *file_cred,
2602     struct vnode *vp, struct label *vplabel)
2603 {
2604 
2605 	LABEL_CHECK(active_cred->cr_label, MAGIC_CRED);
2606 	if (file_cred != NULL)
2607 		LABEL_CHECK(file_cred->cr_label, MAGIC_CRED);
2608 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2609 	COUNTER_INC(vnode_check_stat);
2610 
2611 	return (0);
2612 }
2613 
2614 COUNTER_DECL(vnode_check_unlink);
2615 static int
2616 test_vnode_check_unlink(struct ucred *cred, struct vnode *dvp,
2617     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2618     struct componentname *cnp)
2619 {
2620 
2621 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2622 	LABEL_CHECK(dvplabel, MAGIC_VNODE);
2623 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2624 	COUNTER_INC(vnode_check_unlink);
2625 
2626 	return (0);
2627 }
2628 
2629 COUNTER_DECL(vnode_check_write);
2630 static int
2631 test_vnode_check_write(struct ucred *active_cred,
2632     struct ucred *file_cred, struct vnode *vp, struct label *vplabel)
2633 {
2634 
2635 	LABEL_CHECK(active_cred->cr_label, MAGIC_CRED);
2636 	if (file_cred != NULL)
2637 		LABEL_CHECK(file_cred->cr_label, MAGIC_CRED);
2638 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2639 	COUNTER_INC(vnode_check_write);
2640 
2641 	return (0);
2642 }
2643 
2644 COUNTER_DECL(vnode_copy_label);
2645 static void
2646 test_vnode_copy_label(struct label *src, struct label *dest)
2647 {
2648 
2649 	LABEL_CHECK(src, MAGIC_VNODE);
2650 	LABEL_CHECK(dest, MAGIC_VNODE);
2651 	COUNTER_INC(vnode_copy_label);
2652 }
2653 
2654 COUNTER_DECL(vnode_create_extattr);
2655 static int
2656 test_vnode_create_extattr(struct ucred *cred, struct mount *mp,
2657     struct label *mplabel, struct vnode *dvp, struct label *dvplabel,
2658     struct vnode *vp, struct label *vplabel, struct componentname *cnp)
2659 {
2660 
2661 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2662 	LABEL_CHECK(mplabel, MAGIC_MOUNT);
2663 	LABEL_CHECK(dvplabel, MAGIC_VNODE);
2664 	COUNTER_INC(vnode_create_extattr);
2665 
2666 	return (0);
2667 }
2668 
2669 COUNTER_DECL(vnode_destroy_label);
2670 static void
2671 test_vnode_destroy_label(struct label *label)
2672 {
2673 
2674 	LABEL_DESTROY(label, MAGIC_VNODE);
2675 	COUNTER_INC(vnode_destroy_label);
2676 }
2677 
2678 COUNTER_DECL(vnode_execve_transition);
2679 static void
2680 test_vnode_execve_transition(struct ucred *old, struct ucred *new,
2681     struct vnode *vp, struct label *filelabel,
2682     struct label *interpvplabel, struct image_params *imgp,
2683     struct label *execlabel)
2684 {
2685 
2686 	LABEL_CHECK(old->cr_label, MAGIC_CRED);
2687 	LABEL_CHECK(new->cr_label, MAGIC_CRED);
2688 	LABEL_CHECK(filelabel, MAGIC_VNODE);
2689 	LABEL_CHECK(interpvplabel, MAGIC_VNODE);
2690 	LABEL_CHECK(execlabel, MAGIC_CRED);
2691 	COUNTER_INC(vnode_execve_transition);
2692 }
2693 
2694 COUNTER_DECL(vnode_execve_will_transition);
2695 static int
2696 test_vnode_execve_will_transition(struct ucred *old, struct vnode *vp,
2697     struct label *filelabel, struct label *interpvplabel,
2698     struct image_params *imgp, struct label *execlabel)
2699 {
2700 
2701 	LABEL_CHECK(old->cr_label, MAGIC_CRED);
2702 	LABEL_CHECK(filelabel, MAGIC_VNODE);
2703 	LABEL_CHECK(interpvplabel, MAGIC_VNODE);
2704 	LABEL_CHECK(execlabel, MAGIC_CRED);
2705 	COUNTER_INC(vnode_execve_will_transition);
2706 
2707 	return (0);
2708 }
2709 
2710 COUNTER_DECL(vnode_externalize_label);
2711 static int
2712 test_vnode_externalize_label(struct label *label, char *element_name,
2713     struct sbuf *sb, int *claimed)
2714 {
2715 
2716 	LABEL_CHECK(label, MAGIC_VNODE);
2717 	COUNTER_INC(vnode_externalize_label);
2718 
2719 	return (0);
2720 }
2721 
2722 COUNTER_DECL(vnode_init_label);
2723 static void
2724 test_vnode_init_label(struct label *label)
2725 {
2726 
2727 	LABEL_INIT(label, MAGIC_VNODE);
2728 	COUNTER_INC(vnode_init_label);
2729 }
2730 
2731 COUNTER_DECL(vnode_internalize_label);
2732 static int
2733 test_vnode_internalize_label(struct label *label, char *element_name,
2734     char *element_data, int *claimed)
2735 {
2736 
2737 	LABEL_CHECK(label, MAGIC_VNODE);
2738 	COUNTER_INC(vnode_internalize_label);
2739 
2740 	return (0);
2741 }
2742 
2743 COUNTER_DECL(vnode_relabel);
2744 static void
2745 test_vnode_relabel(struct ucred *cred, struct vnode *vp,
2746     struct label *vplabel, struct label *label)
2747 {
2748 
2749 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2750 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2751 	LABEL_CHECK(label, MAGIC_VNODE);
2752 	COUNTER_INC(vnode_relabel);
2753 }
2754 
2755 COUNTER_DECL(vnode_setlabel_extattr);
2756 static int
2757 test_vnode_setlabel_extattr(struct ucred *cred, struct vnode *vp,
2758     struct label *vplabel, struct label *intlabel)
2759 {
2760 
2761 	LABEL_CHECK(cred->cr_label, MAGIC_CRED);
2762 	LABEL_CHECK(vplabel, MAGIC_VNODE);
2763 	LABEL_CHECK(intlabel, MAGIC_VNODE);
2764 	COUNTER_INC(vnode_setlabel_extattr);
2765 
2766 	return (0);
2767 }
2768 
2769 static struct mac_policy_ops test_ops =
2770 {
2771 	.mpo_bpfdesc_check_receive = test_bpfdesc_check_receive,
2772 	.mpo_bpfdesc_create = test_bpfdesc_create,
2773 	.mpo_bpfdesc_create_mbuf = test_bpfdesc_create_mbuf,
2774 	.mpo_bpfdesc_destroy_label = test_bpfdesc_destroy_label,
2775 	.mpo_bpfdesc_init_label = test_bpfdesc_init_label,
2776 
2777 	.mpo_cred_check_relabel = test_cred_check_relabel,
2778 	.mpo_cred_check_visible = test_cred_check_visible,
2779 	.mpo_cred_copy_label = test_cred_copy_label,
2780 	.mpo_cred_destroy_label = test_cred_destroy_label,
2781 	.mpo_cred_externalize_label = test_cred_externalize_label,
2782 	.mpo_cred_init_label = test_cred_init_label,
2783 	.mpo_cred_internalize_label = test_cred_internalize_label,
2784 	.mpo_cred_relabel = test_cred_relabel,
2785 
2786 	.mpo_devfs_create_device = test_devfs_create_device,
2787 	.mpo_devfs_create_directory = test_devfs_create_directory,
2788 	.mpo_devfs_create_symlink = test_devfs_create_symlink,
2789 	.mpo_devfs_destroy_label = test_devfs_destroy_label,
2790 	.mpo_devfs_init_label = test_devfs_init_label,
2791 	.mpo_devfs_update = test_devfs_update,
2792 	.mpo_devfs_vnode_associate = test_devfs_vnode_associate,
2793 
2794 	.mpo_ifnet_check_relabel = test_ifnet_check_relabel,
2795 	.mpo_ifnet_check_transmit = test_ifnet_check_transmit,
2796 	.mpo_ifnet_copy_label = test_ifnet_copy_label,
2797 	.mpo_ifnet_create = test_ifnet_create,
2798 	.mpo_ifnet_create_mbuf = test_ifnet_create_mbuf,
2799 	.mpo_ifnet_destroy_label = test_ifnet_destroy_label,
2800 	.mpo_ifnet_externalize_label = test_ifnet_externalize_label,
2801 	.mpo_ifnet_init_label = test_ifnet_init_label,
2802 	.mpo_ifnet_internalize_label = test_ifnet_internalize_label,
2803 	.mpo_ifnet_relabel = test_ifnet_relabel,
2804 
2805 	.mpo_syncache_destroy_label = test_syncache_destroy_label,
2806 	.mpo_syncache_init_label = test_syncache_init_label,
2807 
2808 	.mpo_sysvmsg_destroy_label = test_sysvmsg_destroy_label,
2809 	.mpo_sysvmsg_init_label = test_sysvmsg_init_label,
2810 
2811 	.mpo_sysvmsq_destroy_label = test_sysvmsq_destroy_label,
2812 	.mpo_sysvmsq_init_label = test_sysvmsq_init_label,
2813 
2814 	.mpo_sysvsem_destroy_label = test_sysvsem_destroy_label,
2815 	.mpo_sysvsem_init_label = test_sysvsem_init_label,
2816 
2817 	.mpo_sysvshm_destroy_label = test_sysvshm_destroy_label,
2818 	.mpo_sysvshm_init_label = test_sysvshm_init_label,
2819 
2820 	.mpo_inpcb_check_deliver = test_inpcb_check_deliver,
2821 	.mpo_inpcb_create = test_inpcb_create,
2822 	.mpo_inpcb_create_mbuf = test_inpcb_create_mbuf,
2823 	.mpo_inpcb_destroy_label = test_inpcb_destroy_label,
2824 	.mpo_inpcb_init_label = test_inpcb_init_label,
2825 	.mpo_inpcb_sosetlabel = test_inpcb_sosetlabel,
2826 
2827 	.mpo_ipq_create = test_ipq_create,
2828 	.mpo_ipq_destroy_label = test_ipq_destroy_label,
2829 	.mpo_ipq_init_label = test_ipq_init_label,
2830 	.mpo_ipq_match = test_ipq_match,
2831 	.mpo_ipq_reassemble = test_ipq_reassemble,
2832 	.mpo_ipq_update = test_ipq_update,
2833 
2834 	.mpo_kenv_check_dump = test_kenv_check_dump,
2835 	.mpo_kenv_check_get = test_kenv_check_get,
2836 	.mpo_kenv_check_set = test_kenv_check_set,
2837 	.mpo_kenv_check_unset = test_kenv_check_unset,
2838 
2839 	.mpo_kld_check_load = test_kld_check_load,
2840 	.mpo_kld_check_stat = test_kld_check_stat,
2841 
2842 	.mpo_mbuf_copy_label = test_mbuf_copy_label,
2843 	.mpo_mbuf_destroy_label = test_mbuf_destroy_label,
2844 	.mpo_mbuf_init_label = test_mbuf_init_label,
2845 
2846 	.mpo_mount_check_stat = test_mount_check_stat,
2847 	.mpo_mount_create = test_mount_create,
2848 	.mpo_mount_destroy_label = test_mount_destroy_label,
2849 	.mpo_mount_init_label = test_mount_init_label,
2850 
2851 	.mpo_netatalk_aarp_send = test_netatalk_aarp_send,
2852 
2853 	.mpo_netinet_arp_send = test_netinet_arp_send,
2854 	.mpo_netinet_fragment = test_netinet_fragment,
2855 	.mpo_netinet_icmp_reply = test_netinet_icmp_reply,
2856 	.mpo_netinet_icmp_replyinplace = test_netinet_icmp_replyinplace,
2857 	.mpo_netinet_igmp_send = test_netinet_igmp_send,
2858 	.mpo_netinet_tcp_reply = test_netinet_tcp_reply,
2859 
2860 	.mpo_netinet6_nd6_send = test_netinet6_nd6_send,
2861 
2862 	.mpo_pipe_check_ioctl = test_pipe_check_ioctl,
2863 	.mpo_pipe_check_poll = test_pipe_check_poll,
2864 	.mpo_pipe_check_read = test_pipe_check_read,
2865 	.mpo_pipe_check_relabel = test_pipe_check_relabel,
2866 	.mpo_pipe_check_stat = test_pipe_check_stat,
2867 	.mpo_pipe_check_write = test_pipe_check_write,
2868 	.mpo_pipe_copy_label = test_pipe_copy_label,
2869 	.mpo_pipe_create = test_pipe_create,
2870 	.mpo_pipe_destroy_label = test_pipe_destroy_label,
2871 	.mpo_pipe_externalize_label = test_pipe_externalize_label,
2872 	.mpo_pipe_init_label = test_pipe_init_label,
2873 	.mpo_pipe_internalize_label = test_pipe_internalize_label,
2874 	.mpo_pipe_relabel = test_pipe_relabel,
2875 
2876 	.mpo_posixsem_check_getvalue = test_posixsem_check_getvalue,
2877 	.mpo_posixsem_check_open = test_posixsem_check_open,
2878 	.mpo_posixsem_check_post = test_posixsem_check_post,
2879 	.mpo_posixsem_check_unlink = test_posixsem_check_unlink,
2880 	.mpo_posixsem_check_wait = test_posixsem_check_wait,
2881 	.mpo_posixsem_create = test_posixsem_create,
2882 	.mpo_posixsem_destroy_label = test_posixsem_destroy_label,
2883 	.mpo_posixsem_init_label = test_posixsem_init_label,
2884 
2885 	.mpo_posixshm_check_mmap = test_posixshm_check_mmap,
2886 	.mpo_posixshm_check_open = test_posixshm_check_open,
2887 	.mpo_posixshm_check_stat = test_posixshm_check_stat,
2888 	.mpo_posixshm_check_truncate = test_posixshm_check_truncate,
2889 	.mpo_posixshm_check_unlink = test_posixshm_check_unlink,
2890 	.mpo_posixshm_create = test_posixshm_create,
2891 	.mpo_posixshm_destroy_label = test_posixshm_destroy_label,
2892 	.mpo_posixshm_init_label = test_posixshm_init_label,
2893 
2894 	.mpo_proc_check_debug = test_proc_check_debug,
2895 	.mpo_proc_check_sched = test_proc_check_sched,
2896 	.mpo_proc_check_setaudit = test_proc_check_setaudit,
2897 	.mpo_proc_check_setaudit_addr = test_proc_check_setaudit_addr,
2898 	.mpo_proc_check_setauid = test_proc_check_setauid,
2899 	.mpo_proc_check_seteuid = test_proc_check_seteuid,
2900 	.mpo_proc_check_setegid = test_proc_check_setegid,
2901 	.mpo_proc_check_setgid = test_proc_check_setgid,
2902 	.mpo_proc_check_setgroups = test_proc_check_setgroups,
2903 	.mpo_proc_check_setregid = test_proc_check_setregid,
2904 	.mpo_proc_check_setresgid = test_proc_check_setresgid,
2905 	.mpo_proc_check_setresuid = test_proc_check_setresuid,
2906 	.mpo_proc_check_setreuid = test_proc_check_setreuid,
2907 	.mpo_proc_check_setuid = test_proc_check_setuid,
2908 	.mpo_proc_check_signal = test_proc_check_signal,
2909 	.mpo_proc_check_wait = test_proc_check_wait,
2910 	.mpo_proc_create_init = test_proc_create_init,
2911 	.mpo_proc_create_swapper = test_proc_create_swapper,
2912 	.mpo_proc_destroy_label = test_proc_destroy_label,
2913 	.mpo_proc_init_label = test_proc_init_label,
2914 
2915 	.mpo_socket_check_accept = test_socket_check_accept,
2916 	.mpo_socket_check_bind = test_socket_check_bind,
2917 	.mpo_socket_check_connect = test_socket_check_connect,
2918 	.mpo_socket_check_deliver = test_socket_check_deliver,
2919 	.mpo_socket_check_listen = test_socket_check_listen,
2920 	.mpo_socket_check_poll = test_socket_check_poll,
2921 	.mpo_socket_check_receive = test_socket_check_receive,
2922 	.mpo_socket_check_relabel = test_socket_check_relabel,
2923 	.mpo_socket_check_send = test_socket_check_send,
2924 	.mpo_socket_check_stat = test_socket_check_stat,
2925 	.mpo_socket_check_visible = test_socket_check_visible,
2926 	.mpo_socket_copy_label = test_socket_copy_label,
2927 	.mpo_socket_create = test_socket_create,
2928 	.mpo_socket_create_mbuf = test_socket_create_mbuf,
2929 	.mpo_socket_destroy_label = test_socket_destroy_label,
2930 	.mpo_socket_externalize_label = test_socket_externalize_label,
2931 	.mpo_socket_init_label = test_socket_init_label,
2932 	.mpo_socket_internalize_label = test_socket_internalize_label,
2933 	.mpo_socket_newconn = test_socket_newconn,
2934 	.mpo_socket_relabel = test_socket_relabel,
2935 
2936 	.mpo_socketpeer_destroy_label = test_socketpeer_destroy_label,
2937 	.mpo_socketpeer_externalize_label = test_socketpeer_externalize_label,
2938 	.mpo_socketpeer_init_label = test_socketpeer_init_label,
2939 	.mpo_socketpeer_set_from_mbuf = test_socketpeer_set_from_mbuf,
2940 	.mpo_socketpeer_set_from_socket = test_socketpeer_set_from_socket,
2941 
2942 	.mpo_syncache_create = test_syncache_create,
2943 	.mpo_syncache_create_mbuf = test_syncache_create_mbuf,
2944 
2945 	.mpo_system_check_acct = test_system_check_acct,
2946 	.mpo_system_check_audit = test_system_check_audit,
2947 	.mpo_system_check_auditctl = test_system_check_auditctl,
2948 	.mpo_system_check_auditon = test_system_check_auditon,
2949 	.mpo_system_check_reboot = test_system_check_reboot,
2950 	.mpo_system_check_swapoff = test_system_check_swapoff,
2951 	.mpo_system_check_swapon = test_system_check_swapon,
2952 	.mpo_system_check_sysctl = test_system_check_sysctl,
2953 
2954 	.mpo_vnode_check_access = test_vnode_check_access,
2955 	.mpo_sysvmsg_cleanup = test_sysvmsg_cleanup,
2956 	.mpo_sysvmsg_create = test_sysvmsg_create,
2957 
2958 	.mpo_sysvmsq_check_msgmsq = test_sysvmsq_check_msgmsq,
2959 	.mpo_sysvmsq_check_msgrcv = test_sysvmsq_check_msgrcv,
2960 	.mpo_sysvmsq_check_msgrmid = test_sysvmsq_check_msgrmid,
2961 	.mpo_sysvmsq_check_msqget = test_sysvmsq_check_msqget,
2962 	.mpo_sysvmsq_check_msqsnd = test_sysvmsq_check_msqsnd,
2963 	.mpo_sysvmsq_check_msqrcv = test_sysvmsq_check_msqrcv,
2964 	.mpo_sysvmsq_check_msqctl = test_sysvmsq_check_msqctl,
2965 	.mpo_sysvmsq_cleanup = test_sysvmsq_cleanup,
2966 	.mpo_sysvmsq_create = test_sysvmsq_create,
2967 
2968 	.mpo_sysvsem_check_semctl = test_sysvsem_check_semctl,
2969 	.mpo_sysvsem_check_semget = test_sysvsem_check_semget,
2970 	.mpo_sysvsem_check_semop = test_sysvsem_check_semop,
2971 	.mpo_sysvsem_cleanup = test_sysvsem_cleanup,
2972 	.mpo_sysvsem_create = test_sysvsem_create,
2973 
2974 	.mpo_sysvshm_check_shmat = test_sysvshm_check_shmat,
2975 	.mpo_sysvshm_check_shmctl = test_sysvshm_check_shmctl,
2976 	.mpo_sysvshm_check_shmdt = test_sysvshm_check_shmdt,
2977 	.mpo_sysvshm_check_shmget = test_sysvshm_check_shmget,
2978 	.mpo_sysvshm_cleanup = test_sysvshm_cleanup,
2979 	.mpo_sysvshm_create = test_sysvshm_create,
2980 
2981 	.mpo_thread_userret = test_thread_userret,
2982 
2983 	.mpo_vnode_associate_extattr = test_vnode_associate_extattr,
2984 	.mpo_vnode_associate_singlelabel = test_vnode_associate_singlelabel,
2985 	.mpo_vnode_check_chdir = test_vnode_check_chdir,
2986 	.mpo_vnode_check_chroot = test_vnode_check_chroot,
2987 	.mpo_vnode_check_create = test_vnode_check_create,
2988 	.mpo_vnode_check_deleteacl = test_vnode_check_deleteacl,
2989 	.mpo_vnode_check_deleteextattr = test_vnode_check_deleteextattr,
2990 	.mpo_vnode_check_exec = test_vnode_check_exec,
2991 	.mpo_vnode_check_getacl = test_vnode_check_getacl,
2992 	.mpo_vnode_check_getextattr = test_vnode_check_getextattr,
2993 	.mpo_vnode_check_link = test_vnode_check_link,
2994 	.mpo_vnode_check_listextattr = test_vnode_check_listextattr,
2995 	.mpo_vnode_check_lookup = test_vnode_check_lookup,
2996 	.mpo_vnode_check_mmap = test_vnode_check_mmap,
2997 	.mpo_vnode_check_open = test_vnode_check_open,
2998 	.mpo_vnode_check_poll = test_vnode_check_poll,
2999 	.mpo_vnode_check_read = test_vnode_check_read,
3000 	.mpo_vnode_check_readdir = test_vnode_check_readdir,
3001 	.mpo_vnode_check_readlink = test_vnode_check_readlink,
3002 	.mpo_vnode_check_relabel = test_vnode_check_relabel,
3003 	.mpo_vnode_check_rename_from = test_vnode_check_rename_from,
3004 	.mpo_vnode_check_rename_to = test_vnode_check_rename_to,
3005 	.mpo_vnode_check_revoke = test_vnode_check_revoke,
3006 	.mpo_vnode_check_setacl = test_vnode_check_setacl,
3007 	.mpo_vnode_check_setextattr = test_vnode_check_setextattr,
3008 	.mpo_vnode_check_setflags = test_vnode_check_setflags,
3009 	.mpo_vnode_check_setmode = test_vnode_check_setmode,
3010 	.mpo_vnode_check_setowner = test_vnode_check_setowner,
3011 	.mpo_vnode_check_setutimes = test_vnode_check_setutimes,
3012 	.mpo_vnode_check_stat = test_vnode_check_stat,
3013 	.mpo_vnode_check_unlink = test_vnode_check_unlink,
3014 	.mpo_vnode_check_write = test_vnode_check_write,
3015 	.mpo_vnode_copy_label = test_vnode_copy_label,
3016 	.mpo_vnode_create_extattr = test_vnode_create_extattr,
3017 	.mpo_vnode_destroy_label = test_vnode_destroy_label,
3018 	.mpo_vnode_execve_transition = test_vnode_execve_transition,
3019 	.mpo_vnode_execve_will_transition = test_vnode_execve_will_transition,
3020 	.mpo_vnode_externalize_label = test_vnode_externalize_label,
3021 	.mpo_vnode_init_label = test_vnode_init_label,
3022 	.mpo_vnode_internalize_label = test_vnode_internalize_label,
3023 	.mpo_vnode_relabel = test_vnode_relabel,
3024 	.mpo_vnode_setlabel_extattr = test_vnode_setlabel_extattr,
3025 };
3026 
3027 MAC_POLICY_SET(&test_ops, mac_test, "TrustedBSD MAC/Test",
3028     MPC_LOADTIME_FLAG_UNLOADOK | MPC_LOADTIME_FLAG_LABELMBUFS, &test_slot);
3029