xref: /freebsd/sys/security/mac/mac_label.c (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1 /*-
2  * Copyright (c) 2003-2004 Networks Associates Technology, Inc.
3  * Copyright (c) 2007 Robert N. M. Watson
4  * All rights reserved.
5  *
6  * This software was developed for the FreeBSD Project in part by Network
7  * Associates Laboratories, the Security Research Division of Network
8  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
9  * as part of the DARPA CHATS research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_mac.h"
37 
38 #include <sys/param.h>
39 #include <sys/module.h>
40 #include <sys/sysctl.h>
41 #include <sys/systm.h>
42 
43 #include <vm/uma.h>
44 
45 #include <security/mac/mac_framework.h>
46 #include <security/mac/mac_internal.h>
47 #include <security/mac/mac_policy.h>
48 
49 /*
50  * zone_label is the UMA zone from which most labels are allocated.  Label
51  * structures are initialized to zero bytes so that policies see a NULL/0
52  * slot on first use, even if the policy is loaded after the label is
53  * allocated for an object.
54  */
55 static uma_zone_t	zone_label;
56 
57 static int	mac_labelzone_ctor(void *mem, int size, void *arg, int flags);
58 static void	mac_labelzone_dtor(void *mem, int size, void *arg);
59 
60 void
61 mac_labelzone_init(void)
62 {
63 
64 	zone_label = uma_zcreate("MAC labels", sizeof(struct label),
65 	    mac_labelzone_ctor, mac_labelzone_dtor, NULL, NULL,
66 	    UMA_ALIGN_PTR, 0);
67 }
68 
69 /*
70  * mac_init_label() and mac_destroy_label() are exported so that they can be
71  * used in mbuf tag initialization, where labels are not slab allocated from
72  * the zone_label zone.
73  */
74 void
75 mac_init_label(struct label *label)
76 {
77 
78 	bzero(label, sizeof(*label));
79 	label->l_flags = MAC_FLAG_INITIALIZED;
80 }
81 
82 void
83 mac_destroy_label(struct label *label)
84 {
85 
86 	KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,
87 	    ("destroying uninitialized label"));
88 
89 #ifdef DIAGNOSTIC
90 	bzero(label, sizeof(*label));
91 #else
92 	label->l_flags &= ~MAC_FLAG_INITIALIZED;
93 #endif
94 }
95 
96 static int
97 mac_labelzone_ctor(void *mem, int size, void *arg, int flags)
98 {
99 	struct label *label;
100 
101 	KASSERT(size == sizeof(*label), ("mac_labelzone_ctor: wrong size\n"));
102 	label = mem;
103 	mac_init_label(label);
104 	return (0);
105 }
106 
107 static void
108 mac_labelzone_dtor(void *mem, int size, void *arg)
109 {
110 	struct label *label;
111 
112 	KASSERT(size == sizeof(*label), ("mac_labelzone_dtor: wrong size\n"));
113 	label = mem;
114 	mac_destroy_label(label);
115 }
116 
117 struct label *
118 mac_labelzone_alloc(int flags)
119 {
120 
121 	return (uma_zalloc(zone_label, flags));
122 }
123 
124 void
125 mac_labelzone_free(struct label *label)
126 {
127 
128 	uma_zfree(zone_label, label);
129 }
130 
131 /*
132  * Functions used by policy modules to get and set label values.
133  */
134 intptr_t
135 mac_label_get(struct label *l, int slot)
136 {
137 
138 	KASSERT(l != NULL, ("mac_label_get: NULL label"));
139 
140 	return (l->l_perpolicy[slot]);
141 }
142 
143 void
144 mac_label_set(struct label *l, int slot, intptr_t v)
145 {
146 
147 	KASSERT(l != NULL, ("mac_label_set: NULL label"));
148 
149 	l->l_perpolicy[slot] = v;
150 }
151