xref: /freebsd/sys/security/mac/mac_label.c (revision 0957b409)
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 
97 static int
98 mac_labelzone_ctor(void *mem, int size, void *arg, int flags)
99 {
100 	struct label *label;
101 
102 	KASSERT(size == sizeof(*label), ("mac_labelzone_ctor: wrong size\n"));
103 	label = mem;
104 	mac_init_label(label);
105 	return (0);
106 }
107 
108 static void
109 mac_labelzone_dtor(void *mem, int size, void *arg)
110 {
111 	struct label *label;
112 
113 	KASSERT(size == sizeof(*label), ("mac_labelzone_dtor: wrong size\n"));
114 	label = mem;
115 	mac_destroy_label(label);
116 }
117 
118 struct label *
119 mac_labelzone_alloc(int flags)
120 {
121 
122 	return (uma_zalloc(zone_label, flags));
123 }
124 
125 void
126 mac_labelzone_free(struct label *label)
127 {
128 
129 	uma_zfree(zone_label, label);
130 }
131 
132 /*
133  * Functions used by policy modules to get and set label values.
134  */
135 intptr_t
136 mac_label_get(struct label *l, int slot)
137 {
138 
139 	KASSERT(l != NULL, ("mac_label_get: NULL label"));
140 
141 	return (l->l_perpolicy[slot]);
142 }
143 
144 void
145 mac_label_set(struct label *l, int slot, intptr_t v)
146 {
147 
148 	KASSERT(l != NULL, ("mac_label_set: NULL label"));
149 
150 	l->l_perpolicy[slot] = v;
151 }
152