blob: 6ae08efc5ae78dfd3a00885dd1ff45bc5952f788 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the SID table type.
3 *
Stephen Smalley7efbb602017-08-17 13:32:36 -04004 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6#include <linux/kernel.h>
7#include <linux/slab.h>
8#include <linux/spinlock.h>
9#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "flask.h"
11#include "security.h"
12#include "sidtab.h"
13
14#define SIDTAB_HASH(sid) \
15(sid & SIDTAB_HASH_MASK)
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017int sidtab_init(struct sidtab *s)
18{
19 int i;
20
Markus Elfringb380f782017-01-15 13:13:19 +010021 s->htable = kmalloc_array(SIDTAB_SIZE, sizeof(*s->htable), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 if (!s->htable)
23 return -ENOMEM;
24 for (i = 0; i < SIDTAB_SIZE; i++)
25 s->htable[i] = NULL;
26 s->nel = 0;
27 s->next_sid = 1;
28 s->shutdown = 0;
James Morrisbdd581c2008-06-06 18:50:12 +100029 spin_lock_init(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return 0;
31}
32
33int sidtab_insert(struct sidtab *s, u32 sid, struct context *context)
34{
Markus Elfring46be14d2017-04-04 11:33:53 +020035 int hvalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct sidtab_node *prev, *cur, *newnode;
37
Markus Elfring46be14d2017-04-04 11:33:53 +020038 if (!s)
39 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 hvalue = SIDTAB_HASH(sid);
42 prev = NULL;
43 cur = s->htable[hvalue];
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +030044 while (cur && sid > cur->sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 prev = cur;
46 cur = cur->next;
47 }
48
Markus Elfring46be14d2017-04-04 11:33:53 +020049 if (cur && sid == cur->sid)
50 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 newnode = kmalloc(sizeof(*newnode), GFP_ATOMIC);
Markus Elfring46be14d2017-04-04 11:33:53 +020053 if (!newnode)
54 return -ENOMEM;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 newnode->sid = sid;
57 if (context_cpy(&newnode->context, context)) {
58 kfree(newnode);
Markus Elfring46be14d2017-04-04 11:33:53 +020059 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
61
62 if (prev) {
63 newnode->next = prev->next;
64 wmb();
65 prev->next = newnode;
66 } else {
67 newnode->next = s->htable[hvalue];
68 wmb();
69 s->htable[hvalue] = newnode;
70 }
71
72 s->nel++;
73 if (sid >= s->next_sid)
74 s->next_sid = sid + 1;
Markus Elfring46be14d2017-04-04 11:33:53 +020075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Stephen Smalley12b29f32008-05-07 13:03:20 -040078static struct context *sidtab_search_core(struct sidtab *s, u32 sid, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 int hvalue;
81 struct sidtab_node *cur;
82
83 if (!s)
84 return NULL;
85
86 hvalue = SIDTAB_HASH(sid);
87 cur = s->htable[hvalue];
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +030088 while (cur && sid > cur->sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 cur = cur->next;
90
Stephen Smalley12b29f32008-05-07 13:03:20 -040091 if (force && cur && sid == cur->sid && cur->context.len)
92 return &cur->context;
93
Markus Elfring8ee45862017-01-15 13:30:20 +010094 if (!cur || sid != cur->sid || cur->context.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 /* Remap invalid SIDs to the unlabeled SID. */
96 sid = SECINITSID_UNLABELED;
97 hvalue = SIDTAB_HASH(sid);
98 cur = s->htable[hvalue];
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +030099 while (cur && sid > cur->sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 cur = cur->next;
101 if (!cur || sid != cur->sid)
102 return NULL;
103 }
104
105 return &cur->context;
106}
107
Stephen Smalley12b29f32008-05-07 13:03:20 -0400108struct context *sidtab_search(struct sidtab *s, u32 sid)
109{
110 return sidtab_search_core(s, sid, 0);
111}
112
113struct context *sidtab_search_force(struct sidtab *s, u32 sid)
114{
115 return sidtab_search_core(s, sid, 1);
116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118int sidtab_map(struct sidtab *s,
119 int (*apply) (u32 sid,
120 struct context *context,
121 void *args),
122 void *args)
123{
124 int i, rc = 0;
125 struct sidtab_node *cur;
126
127 if (!s)
128 goto out;
129
130 for (i = 0; i < SIDTAB_SIZE; i++) {
131 cur = s->htable[i];
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300132 while (cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 rc = apply(cur->sid, &cur->context, args);
134 if (rc)
135 goto out;
136 cur = cur->next;
137 }
138 }
139out:
140 return rc;
141}
142
Eric Paris73ff5fc02010-12-07 16:17:28 -0500143static void sidtab_update_cache(struct sidtab *s, struct sidtab_node *n, int loc)
144{
145 BUG_ON(loc >= SIDTAB_CACHE_LEN);
146
147 while (loc > 0) {
148 s->cache[loc] = s->cache[loc - 1];
149 loc--;
150 }
151 s->cache[0] = n;
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static inline u32 sidtab_search_context(struct sidtab *s,
155 struct context *context)
156{
157 int i;
158 struct sidtab_node *cur;
159
160 for (i = 0; i < SIDTAB_SIZE; i++) {
161 cur = s->htable[i];
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300162 while (cur) {
Eric Paris73ff5fc02010-12-07 16:17:28 -0500163 if (context_cmp(&cur->context, context)) {
164 sidtab_update_cache(s, cur, SIDTAB_CACHE_LEN - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return cur->sid;
Eric Paris73ff5fc02010-12-07 16:17:28 -0500166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 cur = cur->next;
168 }
169 }
170 return 0;
171}
172
Eric Paris73ff5fc02010-12-07 16:17:28 -0500173static inline u32 sidtab_search_cache(struct sidtab *s, struct context *context)
174{
175 int i;
176 struct sidtab_node *node;
177
178 for (i = 0; i < SIDTAB_CACHE_LEN; i++) {
179 node = s->cache[i];
180 if (unlikely(!node))
181 return 0;
182 if (context_cmp(&node->context, context)) {
183 sidtab_update_cache(s, node, i);
184 return node->sid;
185 }
186 }
187 return 0;
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190int sidtab_context_to_sid(struct sidtab *s,
191 struct context *context,
192 u32 *out_sid)
193{
194 u32 sid;
195 int ret = 0;
196 unsigned long flags;
197
198 *out_sid = SECSID_NULL;
199
Eric Paris73ff5fc02010-12-07 16:17:28 -0500200 sid = sidtab_search_cache(s, context);
201 if (!sid)
202 sid = sidtab_search_context(s, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (!sid) {
James Morrisbdd581c2008-06-06 18:50:12 +1000204 spin_lock_irqsave(&s->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* Rescan now that we hold the lock. */
206 sid = sidtab_search_context(s, context);
207 if (sid)
208 goto unlock_out;
209 /* No SID exists for the context. Allocate a new one. */
210 if (s->next_sid == UINT_MAX || s->shutdown) {
211 ret = -ENOMEM;
212 goto unlock_out;
213 }
214 sid = s->next_sid++;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400215 if (context->len)
216 printk(KERN_INFO
217 "SELinux: Context %s is not valid (left unmapped).\n",
218 context->str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 ret = sidtab_insert(s, sid, context);
220 if (ret)
221 s->next_sid--;
222unlock_out:
James Morrisbdd581c2008-06-06 18:50:12 +1000223 spin_unlock_irqrestore(&s->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
226 if (ret)
227 return ret;
228
229 *out_sid = sid;
230 return 0;
231}
232
233void sidtab_hash_eval(struct sidtab *h, char *tag)
234{
235 int i, chain_len, slots_used, max_chain_len;
236 struct sidtab_node *cur;
237
238 slots_used = 0;
239 max_chain_len = 0;
240 for (i = 0; i < SIDTAB_SIZE; i++) {
241 cur = h->htable[i];
242 if (cur) {
243 slots_used++;
244 chain_len = 0;
245 while (cur) {
246 chain_len++;
247 cur = cur->next;
248 }
249
250 if (chain_len > max_chain_len)
251 max_chain_len = chain_len;
252 }
253 }
254
Eric Parisfadcdb42007-02-22 18:11:31 -0500255 printk(KERN_DEBUG "%s: %d entries and %d/%d buckets used, longest "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 "chain length %d\n", tag, h->nel, slots_used, SIDTAB_SIZE,
257 max_chain_len);
258}
259
260void sidtab_destroy(struct sidtab *s)
261{
262 int i;
263 struct sidtab_node *cur, *temp;
264
265 if (!s)
266 return;
267
268 for (i = 0; i < SIDTAB_SIZE; i++) {
269 cur = s->htable[i];
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300270 while (cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 temp = cur;
272 cur = cur->next;
273 context_destroy(&temp->context);
274 kfree(temp);
275 }
276 s->htable[i] = NULL;
277 }
278 kfree(s->htable);
279 s->htable = NULL;
280 s->nel = 0;
281 s->next_sid = 1;
282}
283
284void sidtab_set(struct sidtab *dst, struct sidtab *src)
285{
286 unsigned long flags;
Eric Paris73ff5fc02010-12-07 16:17:28 -0500287 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
James Morrisbdd581c2008-06-06 18:50:12 +1000289 spin_lock_irqsave(&src->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 dst->htable = src->htable;
291 dst->nel = src->nel;
292 dst->next_sid = src->next_sid;
293 dst->shutdown = 0;
Eric Paris73ff5fc02010-12-07 16:17:28 -0500294 for (i = 0; i < SIDTAB_CACHE_LEN; i++)
295 dst->cache[i] = NULL;
James Morrisbdd581c2008-06-06 18:50:12 +1000296 spin_unlock_irqrestore(&src->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299void sidtab_shutdown(struct sidtab *s)
300{
301 unsigned long flags;
302
James Morrisbdd581c2008-06-06 18:50:12 +1000303 spin_lock_irqsave(&s->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 s->shutdown = 1;
James Morrisbdd581c2008-06-06 18:50:12 +1000305 spin_unlock_irqrestore(&s->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}