1*5c2921b0SApple OSS Distributions /*-
2*5c2921b0SApple OSS Distributions * Copyright (c) 2004 Networks Associates Technology, Inc.
3*5c2921b0SApple OSS Distributions * Copyright (c) 2005 SPARTA, Inc.
4*5c2921b0SApple OSS Distributions * All rights reserved.
5*5c2921b0SApple OSS Distributions *
6*5c2921b0SApple OSS Distributions * This software was developed for the FreeBSD Project in part by Network
7*5c2921b0SApple OSS Distributions * Associates Laboratories, the Security Research Division of Network
8*5c2921b0SApple OSS Distributions * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
9*5c2921b0SApple OSS Distributions * as part of the DARPA CHATS research program.
10*5c2921b0SApple OSS Distributions *
11*5c2921b0SApple OSS Distributions * Redistribution and use in source and binary forms, with or without
12*5c2921b0SApple OSS Distributions * modification, are permitted provided that the following conditions
13*5c2921b0SApple OSS Distributions * are met:
14*5c2921b0SApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
15*5c2921b0SApple OSS Distributions * notice, this list of conditions and the following disclaimer.
16*5c2921b0SApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
17*5c2921b0SApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
18*5c2921b0SApple OSS Distributions * documentation and/or other materials provided with the distribution.
19*5c2921b0SApple OSS Distributions *
20*5c2921b0SApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21*5c2921b0SApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*5c2921b0SApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*5c2921b0SApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24*5c2921b0SApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*5c2921b0SApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*5c2921b0SApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*5c2921b0SApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*5c2921b0SApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*5c2921b0SApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*5c2921b0SApple OSS Distributions * SUCH DAMAGE.
31*5c2921b0SApple OSS Distributions */
32*5c2921b0SApple OSS Distributions
33*5c2921b0SApple OSS Distributions #include <kern/zalloc.h>
34*5c2921b0SApple OSS Distributions #include <security/_label.h>
35*5c2921b0SApple OSS Distributions #include <sys/param.h>
36*5c2921b0SApple OSS Distributions #include <sys/queue.h>
37*5c2921b0SApple OSS Distributions #include <sys/systm.h>
38*5c2921b0SApple OSS Distributions #include <security/mac_internal.h>
39*5c2921b0SApple OSS Distributions
40*5c2921b0SApple OSS Distributions ZONE_DEFINE_ID(ZONE_ID_MAC_LABEL, "MAC Labels", struct label,
41*5c2921b0SApple OSS Distributions ZC_READONLY | ZC_ZFREE_CLEARMEM);
42*5c2921b0SApple OSS Distributions
43*5c2921b0SApple OSS Distributions #define MAC_LABEL_NULL_SLOT (void *)~0ULL
44*5c2921b0SApple OSS Distributions
45*5c2921b0SApple OSS Distributions /*
46*5c2921b0SApple OSS Distributions * Number of initial values matches logic in security/_label.h
47*5c2921b0SApple OSS Distributions */
48*5c2921b0SApple OSS Distributions static const struct label empty_label = {
49*5c2921b0SApple OSS Distributions .l_perpolicy = {
50*5c2921b0SApple OSS Distributions /* at least 3 */
51*5c2921b0SApple OSS Distributions MAC_LABEL_NULL_SLOT,
52*5c2921b0SApple OSS Distributions MAC_LABEL_NULL_SLOT,
53*5c2921b0SApple OSS Distributions MAC_LABEL_NULL_SLOT,
54*5c2921b0SApple OSS Distributions #if defined(XNU_TARGET_OS_OSX)
55*5c2921b0SApple OSS Distributions /* +4 for macOS = 7 */
56*5c2921b0SApple OSS Distributions MAC_LABEL_NULL_SLOT,
57*5c2921b0SApple OSS Distributions MAC_LABEL_NULL_SLOT,
58*5c2921b0SApple OSS Distributions MAC_LABEL_NULL_SLOT,
59*5c2921b0SApple OSS Distributions MAC_LABEL_NULL_SLOT,
60*5c2921b0SApple OSS Distributions #elif CONFIG_VNGUARD
61*5c2921b0SApple OSS Distributions /* otherwise, +1 for vnguard = 4 */
62*5c2921b0SApple OSS Distributions MAC_LABEL_NULL_SLOT,
63*5c2921b0SApple OSS Distributions #endif
64*5c2921b0SApple OSS Distributions }
65*5c2921b0SApple OSS Distributions };
66*5c2921b0SApple OSS Distributions
67*5c2921b0SApple OSS Distributions static struct label *
label_alloc_noinit(int flags)68*5c2921b0SApple OSS Distributions label_alloc_noinit(int flags)
69*5c2921b0SApple OSS Distributions {
70*5c2921b0SApple OSS Distributions static_assert(MAC_NOWAIT == Z_NOWAIT);
71*5c2921b0SApple OSS Distributions return zalloc_ro(ZONE_ID_MAC_LABEL, Z_ZERO | (flags & MAC_NOWAIT));
72*5c2921b0SApple OSS Distributions }
73*5c2921b0SApple OSS Distributions
74*5c2921b0SApple OSS Distributions struct label *
mac_labelzone_alloc(int flags)75*5c2921b0SApple OSS Distributions mac_labelzone_alloc(int flags)
76*5c2921b0SApple OSS Distributions {
77*5c2921b0SApple OSS Distributions struct label *label;
78*5c2921b0SApple OSS Distributions
79*5c2921b0SApple OSS Distributions label = label_alloc_noinit(flags);
80*5c2921b0SApple OSS Distributions if (label) {
81*5c2921b0SApple OSS Distributions zalloc_ro_update_elem(ZONE_ID_MAC_LABEL, label, &empty_label);
82*5c2921b0SApple OSS Distributions }
83*5c2921b0SApple OSS Distributions
84*5c2921b0SApple OSS Distributions return label;
85*5c2921b0SApple OSS Distributions }
86*5c2921b0SApple OSS Distributions
87*5c2921b0SApple OSS Distributions struct label *
88*5c2921b0SApple OSS Distributions mac_labelzone_alloc_for_owner(struct label **labelp, int flags,
89*5c2921b0SApple OSS Distributions void (^extra_setup)(struct label *))
90*5c2921b0SApple OSS Distributions {
91*5c2921b0SApple OSS Distributions struct label *label;
92*5c2921b0SApple OSS Distributions
93*5c2921b0SApple OSS Distributions if (labelp) {
94*5c2921b0SApple OSS Distributions struct label tmp_label = empty_label;
95*5c2921b0SApple OSS Distributions
96*5c2921b0SApple OSS Distributions label = zalloc_ro(ZONE_ID_MAC_LABEL, Z_ZERO | (flags & MAC_NOWAIT));
97*5c2921b0SApple OSS Distributions
98*5c2921b0SApple OSS Distributions tmp_label.l_owner = labelp;
99*5c2921b0SApple OSS Distributions zalloc_ro_update_elem(ZONE_ID_MAC_LABEL, label, &tmp_label);
100*5c2921b0SApple OSS Distributions } else {
101*5c2921b0SApple OSS Distributions label = mac_labelzone_alloc(flags);
102*5c2921b0SApple OSS Distributions }
103*5c2921b0SApple OSS Distributions
104*5c2921b0SApple OSS Distributions if (label && extra_setup) {
105*5c2921b0SApple OSS Distributions extra_setup(label);
106*5c2921b0SApple OSS Distributions }
107*5c2921b0SApple OSS Distributions
108*5c2921b0SApple OSS Distributions return label;
109*5c2921b0SApple OSS Distributions }
110*5c2921b0SApple OSS Distributions
111*5c2921b0SApple OSS Distributions struct label *
112*5c2921b0SApple OSS Distributions mac_labelzone_alloc_owned(struct label **labelp, int flags,
113*5c2921b0SApple OSS Distributions void (^extra_setup)(struct label *))
114*5c2921b0SApple OSS Distributions {
115*5c2921b0SApple OSS Distributions struct label *label;
116*5c2921b0SApple OSS Distributions
117*5c2921b0SApple OSS Distributions label = mac_labelzone_alloc_for_owner(labelp, flags, extra_setup);
118*5c2921b0SApple OSS Distributions
119*5c2921b0SApple OSS Distributions if (labelp) {
120*5c2921b0SApple OSS Distributions *labelp = label;
121*5c2921b0SApple OSS Distributions }
122*5c2921b0SApple OSS Distributions
123*5c2921b0SApple OSS Distributions return label;
124*5c2921b0SApple OSS Distributions }
125*5c2921b0SApple OSS Distributions
126*5c2921b0SApple OSS Distributions void
mac_labelzone_free(struct label * label)127*5c2921b0SApple OSS Distributions mac_labelzone_free(struct label *label)
128*5c2921b0SApple OSS Distributions {
129*5c2921b0SApple OSS Distributions if (label == NULL) {
130*5c2921b0SApple OSS Distributions panic("Free of NULL MAC label");
131*5c2921b0SApple OSS Distributions }
132*5c2921b0SApple OSS Distributions
133*5c2921b0SApple OSS Distributions zfree_ro(ZONE_ID_MAC_LABEL, label);
134*5c2921b0SApple OSS Distributions }
135*5c2921b0SApple OSS Distributions
136*5c2921b0SApple OSS Distributions void
137*5c2921b0SApple OSS Distributions mac_labelzone_free_owned(struct label **labelp,
138*5c2921b0SApple OSS Distributions void (^extra_deinit)(struct label *))
139*5c2921b0SApple OSS Distributions {
140*5c2921b0SApple OSS Distributions struct label *label;
141*5c2921b0SApple OSS Distributions
142*5c2921b0SApple OSS Distributions label = mac_label_verify(labelp);
143*5c2921b0SApple OSS Distributions if (label) {
144*5c2921b0SApple OSS Distributions if (extra_deinit) {
145*5c2921b0SApple OSS Distributions extra_deinit(label);
146*5c2921b0SApple OSS Distributions }
147*5c2921b0SApple OSS Distributions
148*5c2921b0SApple OSS Distributions *labelp = NULL;
149*5c2921b0SApple OSS Distributions mac_labelzone_free(label);
150*5c2921b0SApple OSS Distributions }
151*5c2921b0SApple OSS Distributions }
152*5c2921b0SApple OSS Distributions
153*5c2921b0SApple OSS Distributions __abortlike
154*5c2921b0SApple OSS Distributions static void
mac_label_verify_panic(struct label ** labelp)155*5c2921b0SApple OSS Distributions mac_label_verify_panic(struct label **labelp)
156*5c2921b0SApple OSS Distributions {
157*5c2921b0SApple OSS Distributions panic("label backref mismatch: labelp:%p label:%p l_owner:%p", labelp,
158*5c2921b0SApple OSS Distributions *labelp, (*labelp)->l_owner);
159*5c2921b0SApple OSS Distributions }
160*5c2921b0SApple OSS Distributions
161*5c2921b0SApple OSS Distributions struct label *
mac_label_verify(struct label ** labelp)162*5c2921b0SApple OSS Distributions mac_label_verify(struct label **labelp)
163*5c2921b0SApple OSS Distributions {
164*5c2921b0SApple OSS Distributions struct label *label = *labelp;
165*5c2921b0SApple OSS Distributions
166*5c2921b0SApple OSS Distributions if (label != NULL) {
167*5c2921b0SApple OSS Distributions zone_require_ro(ZONE_ID_MAC_LABEL, sizeof(struct label), label);
168*5c2921b0SApple OSS Distributions
169*5c2921b0SApple OSS Distributions if (__improbable(label->l_owner != labelp)) {
170*5c2921b0SApple OSS Distributions mac_label_verify_panic(labelp);
171*5c2921b0SApple OSS Distributions }
172*5c2921b0SApple OSS Distributions }
173*5c2921b0SApple OSS Distributions
174*5c2921b0SApple OSS Distributions return label;
175*5c2921b0SApple OSS Distributions }
176*5c2921b0SApple OSS Distributions
177*5c2921b0SApple OSS Distributions static intptr_t
mac_label_slot_encode(intptr_t p)178*5c2921b0SApple OSS Distributions mac_label_slot_encode(intptr_t p)
179*5c2921b0SApple OSS Distributions {
180*5c2921b0SApple OSS Distributions return (p == 0) ? (intptr_t)MAC_LABEL_NULL_SLOT : p;
181*5c2921b0SApple OSS Distributions }
182*5c2921b0SApple OSS Distributions
183*5c2921b0SApple OSS Distributions static intptr_t
mac_label_slot_decode(intptr_t p)184*5c2921b0SApple OSS Distributions mac_label_slot_decode(intptr_t p)
185*5c2921b0SApple OSS Distributions {
186*5c2921b0SApple OSS Distributions return (p == (intptr_t)MAC_LABEL_NULL_SLOT) ? 0 : p;
187*5c2921b0SApple OSS Distributions }
188*5c2921b0SApple OSS Distributions
189*5c2921b0SApple OSS Distributions /*
190*5c2921b0SApple OSS Distributions * Functions used by policy modules to get and set label values.
191*5c2921b0SApple OSS Distributions */
192*5c2921b0SApple OSS Distributions intptr_t
mac_label_get(struct label * label,int slot)193*5c2921b0SApple OSS Distributions mac_label_get(struct label *label, int slot)
194*5c2921b0SApple OSS Distributions {
195*5c2921b0SApple OSS Distributions KASSERT(label != NULL, ("mac_label_get: NULL label"));
196*5c2921b0SApple OSS Distributions
197*5c2921b0SApple OSS Distributions zone_require_ro(ZONE_ID_MAC_LABEL, sizeof(struct label), label);
198*5c2921b0SApple OSS Distributions return mac_label_slot_decode((intptr_t) label->l_perpolicy[slot]);
199*5c2921b0SApple OSS Distributions }
200*5c2921b0SApple OSS Distributions
201*5c2921b0SApple OSS Distributions __abortlike
202*5c2921b0SApple OSS Distributions static void
panic_label_set_sentinel(void)203*5c2921b0SApple OSS Distributions panic_label_set_sentinel(void)
204*5c2921b0SApple OSS Distributions {
205*5c2921b0SApple OSS Distributions panic("cannot set mac label to ~0");
206*5c2921b0SApple OSS Distributions }
207*5c2921b0SApple OSS Distributions
208*5c2921b0SApple OSS Distributions void
mac_label_set(struct label * label,int slot,intptr_t v)209*5c2921b0SApple OSS Distributions mac_label_set(struct label *label, int slot, intptr_t v)
210*5c2921b0SApple OSS Distributions {
211*5c2921b0SApple OSS Distributions KASSERT(label != NULL, ("mac_label_set: NULL label"));
212*5c2921b0SApple OSS Distributions
213*5c2921b0SApple OSS Distributions if (v == (intptr_t)MAC_LABEL_NULL_SLOT) {
214*5c2921b0SApple OSS Distributions panic_label_set_sentinel();
215*5c2921b0SApple OSS Distributions }
216*5c2921b0SApple OSS Distributions
217*5c2921b0SApple OSS Distributions v = mac_label_slot_encode(v);
218*5c2921b0SApple OSS Distributions zalloc_ro_update_field(ZONE_ID_MAC_LABEL, label, l_perpolicy[slot],
219*5c2921b0SApple OSS Distributions (void **)&v);
220*5c2921b0SApple OSS Distributions }
221