Revert "tty: hvc: Fix data abort due to race in hvc_open"
[linux/fpc-iii.git] / security / selinux / ss / sidtab.c
blobf511ffccb131cbe533a733a176f5a17ff22de3fc
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Implementation of the SID table type.
5 * Original author: Stephen Smalley, <sds@tycho.nsa.gov>
6 * Author: Ondrej Mosnacek, <omosnacek@gmail.com>
8 * Copyright (C) 2018 Red Hat, Inc.
9 */
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/rcupdate.h>
14 #include <linux/slab.h>
15 #include <linux/sched.h>
16 #include <linux/spinlock.h>
17 #include <asm/barrier.h>
18 #include "flask.h"
19 #include "security.h"
20 #include "sidtab.h"
22 struct sidtab_str_cache {
23 struct rcu_head rcu_member;
24 struct list_head lru_member;
25 struct sidtab_entry *parent;
26 u32 len;
27 char str[];
30 #define index_to_sid(index) (index + SECINITSID_NUM + 1)
31 #define sid_to_index(sid) (sid - (SECINITSID_NUM + 1))
33 int sidtab_init(struct sidtab *s)
35 u32 i;
37 memset(s->roots, 0, sizeof(s->roots));
39 for (i = 0; i < SECINITSID_NUM; i++)
40 s->isids[i].set = 0;
42 s->count = 0;
43 s->convert = NULL;
44 hash_init(s->context_to_sid);
46 spin_lock_init(&s->lock);
48 #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0
49 s->cache_free_slots = CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE;
50 INIT_LIST_HEAD(&s->cache_lru_list);
51 spin_lock_init(&s->cache_lock);
52 #endif
54 return 0;
57 static u32 context_to_sid(struct sidtab *s, struct context *context)
59 struct sidtab_entry *entry;
60 u32 sid = 0;
62 rcu_read_lock();
63 hash_for_each_possible_rcu(s->context_to_sid, entry, list,
64 context->hash) {
65 if (context_cmp(&entry->context, context)) {
66 sid = entry->sid;
67 break;
70 rcu_read_unlock();
71 return sid;
74 int sidtab_set_initial(struct sidtab *s, u32 sid, struct context *context)
76 struct sidtab_isid_entry *isid;
77 int rc;
79 if (sid == 0 || sid > SECINITSID_NUM)
80 return -EINVAL;
82 isid = &s->isids[sid - 1];
84 rc = context_cpy(&isid->entry.context, context);
85 if (rc)
86 return rc;
88 #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0
89 isid->entry.cache = NULL;
90 #endif
91 isid->set = 1;
94 * Multiple initial sids may map to the same context. Check that this
95 * context is not already represented in the context_to_sid hashtable
96 * to avoid duplicate entries and long linked lists upon hash
97 * collision.
99 if (!context_to_sid(s, context)) {
100 isid->entry.sid = sid;
101 hash_add(s->context_to_sid, &isid->entry.list, context->hash);
104 return 0;
107 int sidtab_hash_stats(struct sidtab *sidtab, char *page)
109 int i;
110 int chain_len = 0;
111 int slots_used = 0;
112 int entries = 0;
113 int max_chain_len = 0;
114 int cur_bucket = 0;
115 struct sidtab_entry *entry;
117 rcu_read_lock();
118 hash_for_each_rcu(sidtab->context_to_sid, i, entry, list) {
119 entries++;
120 if (i == cur_bucket) {
121 chain_len++;
122 if (chain_len == 1)
123 slots_used++;
124 } else {
125 cur_bucket = i;
126 if (chain_len > max_chain_len)
127 max_chain_len = chain_len;
128 chain_len = 0;
131 rcu_read_unlock();
133 if (chain_len > max_chain_len)
134 max_chain_len = chain_len;
136 return scnprintf(page, PAGE_SIZE, "entries: %d\nbuckets used: %d/%d\n"
137 "longest chain: %d\n", entries,
138 slots_used, SIDTAB_HASH_BUCKETS, max_chain_len);
141 static u32 sidtab_level_from_count(u32 count)
143 u32 capacity = SIDTAB_LEAF_ENTRIES;
144 u32 level = 0;
146 while (count > capacity) {
147 capacity <<= SIDTAB_INNER_SHIFT;
148 ++level;
150 return level;
153 static int sidtab_alloc_roots(struct sidtab *s, u32 level)
155 u32 l;
157 if (!s->roots[0].ptr_leaf) {
158 s->roots[0].ptr_leaf = kzalloc(SIDTAB_NODE_ALLOC_SIZE,
159 GFP_ATOMIC);
160 if (!s->roots[0].ptr_leaf)
161 return -ENOMEM;
163 for (l = 1; l <= level; ++l)
164 if (!s->roots[l].ptr_inner) {
165 s->roots[l].ptr_inner = kzalloc(SIDTAB_NODE_ALLOC_SIZE,
166 GFP_ATOMIC);
167 if (!s->roots[l].ptr_inner)
168 return -ENOMEM;
169 s->roots[l].ptr_inner->entries[0] = s->roots[l - 1];
171 return 0;
174 static struct sidtab_entry *sidtab_do_lookup(struct sidtab *s, u32 index,
175 int alloc)
177 union sidtab_entry_inner *entry;
178 u32 level, capacity_shift, leaf_index = index / SIDTAB_LEAF_ENTRIES;
180 /* find the level of the subtree we need */
181 level = sidtab_level_from_count(index + 1);
182 capacity_shift = level * SIDTAB_INNER_SHIFT;
184 /* allocate roots if needed */
185 if (alloc && sidtab_alloc_roots(s, level) != 0)
186 return NULL;
188 /* lookup inside the subtree */
189 entry = &s->roots[level];
190 while (level != 0) {
191 capacity_shift -= SIDTAB_INNER_SHIFT;
192 --level;
194 entry = &entry->ptr_inner->entries[leaf_index >> capacity_shift];
195 leaf_index &= ((u32)1 << capacity_shift) - 1;
197 if (!entry->ptr_inner) {
198 if (alloc)
199 entry->ptr_inner = kzalloc(SIDTAB_NODE_ALLOC_SIZE,
200 GFP_ATOMIC);
201 if (!entry->ptr_inner)
202 return NULL;
205 if (!entry->ptr_leaf) {
206 if (alloc)
207 entry->ptr_leaf = kzalloc(SIDTAB_NODE_ALLOC_SIZE,
208 GFP_ATOMIC);
209 if (!entry->ptr_leaf)
210 return NULL;
212 return &entry->ptr_leaf->entries[index % SIDTAB_LEAF_ENTRIES];
215 static struct sidtab_entry *sidtab_lookup(struct sidtab *s, u32 index)
217 /* read entries only after reading count */
218 u32 count = smp_load_acquire(&s->count);
220 if (index >= count)
221 return NULL;
223 return sidtab_do_lookup(s, index, 0);
226 static struct sidtab_entry *sidtab_lookup_initial(struct sidtab *s, u32 sid)
228 return s->isids[sid - 1].set ? &s->isids[sid - 1].entry : NULL;
231 static struct sidtab_entry *sidtab_search_core(struct sidtab *s, u32 sid,
232 int force)
234 if (sid != 0) {
235 struct sidtab_entry *entry;
237 if (sid > SECINITSID_NUM)
238 entry = sidtab_lookup(s, sid_to_index(sid));
239 else
240 entry = sidtab_lookup_initial(s, sid);
241 if (entry && (!entry->context.len || force))
242 return entry;
245 return sidtab_lookup_initial(s, SECINITSID_UNLABELED);
248 struct sidtab_entry *sidtab_search_entry(struct sidtab *s, u32 sid)
250 return sidtab_search_core(s, sid, 0);
253 struct sidtab_entry *sidtab_search_entry_force(struct sidtab *s, u32 sid)
255 return sidtab_search_core(s, sid, 1);
258 int sidtab_context_to_sid(struct sidtab *s, struct context *context,
259 u32 *sid)
261 unsigned long flags;
262 u32 count;
263 struct sidtab_convert_params *convert;
264 struct sidtab_entry *dst, *dst_convert;
265 int rc;
267 *sid = context_to_sid(s, context);
268 if (*sid)
269 return 0;
271 /* lock-free search failed: lock, re-search, and insert if not found */
272 spin_lock_irqsave(&s->lock, flags);
274 rc = 0;
275 *sid = context_to_sid(s, context);
276 if (*sid)
277 goto out_unlock;
279 /* read entries only after reading count */
280 count = smp_load_acquire(&s->count);
281 convert = s->convert;
283 /* bail out if we already reached max entries */
284 rc = -EOVERFLOW;
285 if (count >= SIDTAB_MAX)
286 goto out_unlock;
288 /* insert context into new entry */
289 rc = -ENOMEM;
290 dst = sidtab_do_lookup(s, count, 1);
291 if (!dst)
292 goto out_unlock;
294 dst->sid = index_to_sid(count);
296 rc = context_cpy(&dst->context, context);
297 if (rc)
298 goto out_unlock;
301 * if we are building a new sidtab, we need to convert the context
302 * and insert it there as well
304 if (convert) {
305 rc = -ENOMEM;
306 dst_convert = sidtab_do_lookup(convert->target, count, 1);
307 if (!dst_convert) {
308 context_destroy(&dst->context);
309 goto out_unlock;
312 rc = convert->func(context, &dst_convert->context,
313 convert->args);
314 if (rc) {
315 context_destroy(&dst->context);
316 goto out_unlock;
318 dst_convert->sid = index_to_sid(count);
319 convert->target->count = count + 1;
321 hash_add_rcu(convert->target->context_to_sid,
322 &dst_convert->list, dst_convert->context.hash);
325 if (context->len)
326 pr_info("SELinux: Context %s is not valid (left unmapped).\n",
327 context->str);
329 *sid = index_to_sid(count);
331 /* write entries before updating count */
332 smp_store_release(&s->count, count + 1);
333 hash_add_rcu(s->context_to_sid, &dst->list, dst->context.hash);
335 rc = 0;
336 out_unlock:
337 spin_unlock_irqrestore(&s->lock, flags);
338 return rc;
341 static void sidtab_convert_hashtable(struct sidtab *s, u32 count)
343 struct sidtab_entry *entry;
344 u32 i;
346 for (i = 0; i < count; i++) {
347 entry = sidtab_do_lookup(s, i, 0);
348 entry->sid = index_to_sid(i);
350 hash_add_rcu(s->context_to_sid, &entry->list,
351 entry->context.hash);
356 static int sidtab_convert_tree(union sidtab_entry_inner *edst,
357 union sidtab_entry_inner *esrc,
358 u32 *pos, u32 count, u32 level,
359 struct sidtab_convert_params *convert)
361 int rc;
362 u32 i;
364 if (level != 0) {
365 if (!edst->ptr_inner) {
366 edst->ptr_inner = kzalloc(SIDTAB_NODE_ALLOC_SIZE,
367 GFP_KERNEL);
368 if (!edst->ptr_inner)
369 return -ENOMEM;
371 i = 0;
372 while (i < SIDTAB_INNER_ENTRIES && *pos < count) {
373 rc = sidtab_convert_tree(&edst->ptr_inner->entries[i],
374 &esrc->ptr_inner->entries[i],
375 pos, count, level - 1,
376 convert);
377 if (rc)
378 return rc;
379 i++;
381 } else {
382 if (!edst->ptr_leaf) {
383 edst->ptr_leaf = kzalloc(SIDTAB_NODE_ALLOC_SIZE,
384 GFP_KERNEL);
385 if (!edst->ptr_leaf)
386 return -ENOMEM;
388 i = 0;
389 while (i < SIDTAB_LEAF_ENTRIES && *pos < count) {
390 rc = convert->func(&esrc->ptr_leaf->entries[i].context,
391 &edst->ptr_leaf->entries[i].context,
392 convert->args);
393 if (rc)
394 return rc;
395 (*pos)++;
396 i++;
398 cond_resched();
400 return 0;
403 int sidtab_convert(struct sidtab *s, struct sidtab_convert_params *params)
405 unsigned long flags;
406 u32 count, level, pos;
407 int rc;
409 spin_lock_irqsave(&s->lock, flags);
411 /* concurrent policy loads are not allowed */
412 if (s->convert) {
413 spin_unlock_irqrestore(&s->lock, flags);
414 return -EBUSY;
417 count = s->count;
418 level = sidtab_level_from_count(count);
420 /* allocate last leaf in the new sidtab (to avoid race with
421 * live convert)
423 rc = sidtab_do_lookup(params->target, count - 1, 1) ? 0 : -ENOMEM;
424 if (rc) {
425 spin_unlock_irqrestore(&s->lock, flags);
426 return rc;
429 /* set count in case no new entries are added during conversion */
430 params->target->count = count;
432 /* enable live convert of new entries */
433 s->convert = params;
435 /* we can safely convert the tree outside the lock */
436 spin_unlock_irqrestore(&s->lock, flags);
438 pr_info("SELinux: Converting %u SID table entries...\n", count);
440 /* convert all entries not covered by live convert */
441 pos = 0;
442 rc = sidtab_convert_tree(&params->target->roots[level],
443 &s->roots[level], &pos, count, level, params);
444 if (rc) {
445 /* we need to keep the old table - disable live convert */
446 spin_lock_irqsave(&s->lock, flags);
447 s->convert = NULL;
448 spin_unlock_irqrestore(&s->lock, flags);
449 return rc;
452 * The hashtable can also be modified in sidtab_context_to_sid()
453 * so we must re-acquire the lock here.
455 spin_lock_irqsave(&s->lock, flags);
456 sidtab_convert_hashtable(params->target, count);
457 spin_unlock_irqrestore(&s->lock, flags);
459 return 0;
462 static void sidtab_destroy_entry(struct sidtab_entry *entry)
464 context_destroy(&entry->context);
465 #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0
466 kfree(rcu_dereference_raw(entry->cache));
467 #endif
470 static void sidtab_destroy_tree(union sidtab_entry_inner entry, u32 level)
472 u32 i;
474 if (level != 0) {
475 struct sidtab_node_inner *node = entry.ptr_inner;
477 if (!node)
478 return;
480 for (i = 0; i < SIDTAB_INNER_ENTRIES; i++)
481 sidtab_destroy_tree(node->entries[i], level - 1);
482 kfree(node);
483 } else {
484 struct sidtab_node_leaf *node = entry.ptr_leaf;
486 if (!node)
487 return;
489 for (i = 0; i < SIDTAB_LEAF_ENTRIES; i++)
490 sidtab_destroy_entry(&node->entries[i]);
491 kfree(node);
495 void sidtab_destroy(struct sidtab *s)
497 u32 i, level;
499 for (i = 0; i < SECINITSID_NUM; i++)
500 if (s->isids[i].set)
501 sidtab_destroy_entry(&s->isids[i].entry);
503 level = SIDTAB_MAX_LEVEL;
504 while (level && !s->roots[level].ptr_inner)
505 --level;
507 sidtab_destroy_tree(s->roots[level], level);
509 * The context_to_sid hashtable's objects are all shared
510 * with the isids array and context tree, and so don't need
511 * to be cleaned up here.
515 #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0
517 void sidtab_sid2str_put(struct sidtab *s, struct sidtab_entry *entry,
518 const char *str, u32 str_len)
520 struct sidtab_str_cache *cache, *victim = NULL;
521 unsigned long flags;
523 /* do not cache invalid contexts */
524 if (entry->context.len)
525 return;
527 spin_lock_irqsave(&s->cache_lock, flags);
529 cache = rcu_dereference_protected(entry->cache,
530 lockdep_is_held(&s->cache_lock));
531 if (cache) {
532 /* entry in cache - just bump to the head of LRU list */
533 list_move(&cache->lru_member, &s->cache_lru_list);
534 goto out_unlock;
537 cache = kmalloc(sizeof(struct sidtab_str_cache) + str_len, GFP_ATOMIC);
538 if (!cache)
539 goto out_unlock;
541 if (s->cache_free_slots == 0) {
542 /* pop a cache entry from the tail and free it */
543 victim = container_of(s->cache_lru_list.prev,
544 struct sidtab_str_cache, lru_member);
545 list_del(&victim->lru_member);
546 rcu_assign_pointer(victim->parent->cache, NULL);
547 } else {
548 s->cache_free_slots--;
550 cache->parent = entry;
551 cache->len = str_len;
552 memcpy(cache->str, str, str_len);
553 list_add(&cache->lru_member, &s->cache_lru_list);
555 rcu_assign_pointer(entry->cache, cache);
557 out_unlock:
558 spin_unlock_irqrestore(&s->cache_lock, flags);
559 kfree_rcu(victim, rcu_member);
562 int sidtab_sid2str_get(struct sidtab *s, struct sidtab_entry *entry,
563 char **out, u32 *out_len)
565 struct sidtab_str_cache *cache;
566 int rc = 0;
568 if (entry->context.len)
569 return -ENOENT; /* do not cache invalid contexts */
571 rcu_read_lock();
573 cache = rcu_dereference(entry->cache);
574 if (!cache) {
575 rc = -ENOENT;
576 } else {
577 *out_len = cache->len;
578 if (out) {
579 *out = kmemdup(cache->str, cache->len, GFP_ATOMIC);
580 if (!*out)
581 rc = -ENOMEM;
585 rcu_read_unlock();
587 if (!rc && out)
588 sidtab_sid2str_put(s, entry, *out, *out_len);
589 return rc;
592 #endif /* CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0 */