sidtab.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Implementation of the SID table type.
  4. *
  5. * Original author: Stephen Smalley, <stephen.smalley.work@gmail.com>
  6. * Author: Ondrej Mosnacek, <omosnacek@gmail.com>
  7. *
  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"
  21. #include "services.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[] __counted_by(len);
  28. };
  29. #define index_to_sid(index) ((index) + SECINITSID_NUM + 1)
  30. #define sid_to_index(sid) ((sid) - (SECINITSID_NUM + 1))
  31. int sidtab_init(struct sidtab *s)
  32. {
  33. u32 i;
  34. memset(s->roots, 0, sizeof(s->roots));
  35. for (i = 0; i < SECINITSID_NUM; i++)
  36. s->isids[i].set = 0;
  37. s->frozen = false;
  38. s->count = 0;
  39. s->convert = NULL;
  40. hash_init(s->context_to_sid);
  41. spin_lock_init(&s->lock);
  42. #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0
  43. s->cache_free_slots = CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE;
  44. INIT_LIST_HEAD(&s->cache_lru_list);
  45. spin_lock_init(&s->cache_lock);
  46. #endif
  47. return 0;
  48. }
  49. static u32 context_to_sid(struct sidtab *s, struct context *context, u32 hash)
  50. {
  51. struct sidtab_entry *entry;
  52. u32 sid = 0;
  53. rcu_read_lock();
  54. hash_for_each_possible_rcu(s->context_to_sid, entry, list, hash) {
  55. if (entry->hash != hash)
  56. continue;
  57. if (context_cmp(&entry->context, context)) {
  58. sid = entry->sid;
  59. break;
  60. }
  61. }
  62. rcu_read_unlock();
  63. return sid;
  64. }
  65. int sidtab_set_initial(struct sidtab *s, u32 sid, struct context *context)
  66. {
  67. struct sidtab_isid_entry *isid;
  68. u32 hash;
  69. int rc;
  70. if (sid == 0 || sid > SECINITSID_NUM)
  71. return -EINVAL;
  72. isid = &s->isids[sid - 1];
  73. rc = context_cpy(&isid->entry.context, context);
  74. if (rc)
  75. return rc;
  76. #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0
  77. isid->entry.cache = NULL;
  78. #endif
  79. isid->set = 1;
  80. hash = context_compute_hash(context);
  81. /*
  82. * Multiple initial sids may map to the same context. Check that this
  83. * context is not already represented in the context_to_sid hashtable
  84. * to avoid duplicate entries and long linked lists upon hash
  85. * collision.
  86. */
  87. if (!context_to_sid(s, context, hash)) {
  88. isid->entry.sid = sid;
  89. isid->entry.hash = hash;
  90. hash_add(s->context_to_sid, &isid->entry.list, hash);
  91. }
  92. return 0;
  93. }
  94. int sidtab_hash_stats(struct sidtab *sidtab, char *page)
  95. {
  96. int i;
  97. int chain_len = 0;
  98. int slots_used = 0;
  99. int entries = 0;
  100. int max_chain_len = 0;
  101. int cur_bucket = 0;
  102. struct sidtab_entry *entry;
  103. rcu_read_lock();
  104. hash_for_each_rcu(sidtab->context_to_sid, i, entry, list) {
  105. entries++;
  106. if (i == cur_bucket) {
  107. chain_len++;
  108. if (chain_len == 1)
  109. slots_used++;
  110. } else {
  111. cur_bucket = i;
  112. if (chain_len > max_chain_len)
  113. max_chain_len = chain_len;
  114. chain_len = 0;
  115. }
  116. }
  117. rcu_read_unlock();
  118. if (chain_len > max_chain_len)
  119. max_chain_len = chain_len;
  120. return scnprintf(page, PAGE_SIZE,
  121. "entries: %d\nbuckets used: %d/%d\n"
  122. "longest chain: %d\n",
  123. entries, slots_used, SIDTAB_HASH_BUCKETS,
  124. max_chain_len);
  125. }
  126. static u32 sidtab_level_from_count(u32 count)
  127. {
  128. u32 capacity = SIDTAB_LEAF_ENTRIES;
  129. u32 level = 0;
  130. while (count > capacity) {
  131. capacity <<= SIDTAB_INNER_SHIFT;
  132. ++level;
  133. }
  134. return level;
  135. }
  136. static int sidtab_alloc_roots(struct sidtab *s, u32 level)
  137. {
  138. u32 l;
  139. if (!s->roots[0].ptr_leaf) {
  140. s->roots[0].ptr_leaf =
  141. kzalloc(SIDTAB_NODE_ALLOC_SIZE, GFP_ATOMIC);
  142. if (!s->roots[0].ptr_leaf)
  143. return -ENOMEM;
  144. }
  145. for (l = 1; l <= level; ++l)
  146. if (!s->roots[l].ptr_inner) {
  147. s->roots[l].ptr_inner =
  148. kzalloc(SIDTAB_NODE_ALLOC_SIZE, GFP_ATOMIC);
  149. if (!s->roots[l].ptr_inner)
  150. return -ENOMEM;
  151. s->roots[l].ptr_inner->entries[0] = s->roots[l - 1];
  152. }
  153. return 0;
  154. }
  155. static struct sidtab_entry *sidtab_do_lookup(struct sidtab *s, u32 index,
  156. int alloc)
  157. {
  158. union sidtab_entry_inner *entry;
  159. u32 level, capacity_shift, leaf_index = index / SIDTAB_LEAF_ENTRIES;
  160. /* find the level of the subtree we need */
  161. level = sidtab_level_from_count(index + 1);
  162. capacity_shift = level * SIDTAB_INNER_SHIFT;
  163. /* allocate roots if needed */
  164. if (alloc && sidtab_alloc_roots(s, level) != 0)
  165. return NULL;
  166. /* lookup inside the subtree */
  167. entry = &s->roots[level];
  168. while (level != 0) {
  169. capacity_shift -= SIDTAB_INNER_SHIFT;
  170. --level;
  171. entry = &entry->ptr_inner->entries[leaf_index >> capacity_shift];
  172. leaf_index &= ((u32)1 << capacity_shift) - 1;
  173. if (!entry->ptr_inner) {
  174. if (alloc)
  175. entry->ptr_inner = kzalloc(
  176. SIDTAB_NODE_ALLOC_SIZE, GFP_ATOMIC);
  177. if (!entry->ptr_inner)
  178. return NULL;
  179. }
  180. }
  181. if (!entry->ptr_leaf) {
  182. if (alloc)
  183. entry->ptr_leaf =
  184. kzalloc(SIDTAB_NODE_ALLOC_SIZE, GFP_ATOMIC);
  185. if (!entry->ptr_leaf)
  186. return NULL;
  187. }
  188. return &entry->ptr_leaf->entries[index % SIDTAB_LEAF_ENTRIES];
  189. }
  190. static struct sidtab_entry *sidtab_lookup(struct sidtab *s, u32 index)
  191. {
  192. /* read entries only after reading count */
  193. u32 count = smp_load_acquire(&s->count);
  194. if (index >= count)
  195. return NULL;
  196. return sidtab_do_lookup(s, index, 0);
  197. }
  198. static struct sidtab_entry *sidtab_lookup_initial(struct sidtab *s, u32 sid)
  199. {
  200. return s->isids[sid - 1].set ? &s->isids[sid - 1].entry : NULL;
  201. }
  202. static struct sidtab_entry *sidtab_search_core(struct sidtab *s, u32 sid,
  203. int force)
  204. {
  205. if (sid != 0) {
  206. struct sidtab_entry *entry;
  207. if (sid > SECINITSID_NUM)
  208. entry = sidtab_lookup(s, sid_to_index(sid));
  209. else
  210. entry = sidtab_lookup_initial(s, sid);
  211. if (entry && (!entry->context.len || force))
  212. return entry;
  213. }
  214. return sidtab_lookup_initial(s, SECINITSID_UNLABELED);
  215. }
  216. struct sidtab_entry *sidtab_search_entry(struct sidtab *s, u32 sid)
  217. {
  218. return sidtab_search_core(s, sid, 0);
  219. }
  220. struct sidtab_entry *sidtab_search_entry_force(struct sidtab *s, u32 sid)
  221. {
  222. return sidtab_search_core(s, sid, 1);
  223. }
  224. int sidtab_context_to_sid(struct sidtab *s, struct context *context, u32 *sid)
  225. {
  226. unsigned long flags;
  227. u32 count, hash = context_compute_hash(context);
  228. struct sidtab_convert_params *convert;
  229. struct sidtab_entry *dst, *dst_convert;
  230. int rc;
  231. *sid = context_to_sid(s, context, hash);
  232. if (*sid)
  233. return 0;
  234. /* lock-free search failed: lock, re-search, and insert if not found */
  235. spin_lock_irqsave(&s->lock, flags);
  236. rc = 0;
  237. *sid = context_to_sid(s, context, hash);
  238. if (*sid)
  239. goto out_unlock;
  240. if (unlikely(s->frozen)) {
  241. /*
  242. * This sidtab is now frozen - tell the caller to abort and
  243. * get the new one.
  244. */
  245. rc = -ESTALE;
  246. goto out_unlock;
  247. }
  248. count = s->count;
  249. /* bail out if we already reached max entries */
  250. rc = -EOVERFLOW;
  251. if (count >= SIDTAB_MAX)
  252. goto out_unlock;
  253. /* insert context into new entry */
  254. rc = -ENOMEM;
  255. dst = sidtab_do_lookup(s, count, 1);
  256. if (!dst)
  257. goto out_unlock;
  258. dst->sid = index_to_sid(count);
  259. dst->hash = hash;
  260. rc = context_cpy(&dst->context, context);
  261. if (rc)
  262. goto out_unlock;
  263. /*
  264. * if we are building a new sidtab, we need to convert the context
  265. * and insert it there as well
  266. */
  267. convert = s->convert;
  268. if (convert) {
  269. struct sidtab *target = convert->target;
  270. rc = -ENOMEM;
  271. dst_convert = sidtab_do_lookup(target, count, 1);
  272. if (!dst_convert) {
  273. context_destroy(&dst->context);
  274. goto out_unlock;
  275. }
  276. rc = services_convert_context(convert->args, context,
  277. &dst_convert->context,
  278. GFP_ATOMIC);
  279. if (rc) {
  280. context_destroy(&dst->context);
  281. goto out_unlock;
  282. }
  283. dst_convert->sid = index_to_sid(count);
  284. dst_convert->hash = context_compute_hash(&dst_convert->context);
  285. target->count = count + 1;
  286. hash_add_rcu(target->context_to_sid, &dst_convert->list,
  287. dst_convert->hash);
  288. }
  289. if (context->len)
  290. pr_info("SELinux: Context %s is not valid (left unmapped).\n",
  291. context->str);
  292. *sid = index_to_sid(count);
  293. /* write entries before updating count */
  294. smp_store_release(&s->count, count + 1);
  295. hash_add_rcu(s->context_to_sid, &dst->list, dst->hash);
  296. rc = 0;
  297. out_unlock:
  298. spin_unlock_irqrestore(&s->lock, flags);
  299. return rc;
  300. }
  301. static void sidtab_convert_hashtable(struct sidtab *s, u32 count)
  302. {
  303. struct sidtab_entry *entry;
  304. u32 i;
  305. for (i = 0; i < count; i++) {
  306. entry = sidtab_do_lookup(s, i, 0);
  307. entry->sid = index_to_sid(i);
  308. entry->hash = context_compute_hash(&entry->context);
  309. hash_add_rcu(s->context_to_sid, &entry->list, entry->hash);
  310. }
  311. }
  312. static int sidtab_convert_tree(union sidtab_entry_inner *edst,
  313. union sidtab_entry_inner *esrc, u32 *pos,
  314. u32 count, u32 level,
  315. struct sidtab_convert_params *convert)
  316. {
  317. int rc;
  318. u32 i;
  319. if (level != 0) {
  320. if (!edst->ptr_inner) {
  321. edst->ptr_inner =
  322. kzalloc(SIDTAB_NODE_ALLOC_SIZE, GFP_KERNEL);
  323. if (!edst->ptr_inner)
  324. return -ENOMEM;
  325. }
  326. i = 0;
  327. while (i < SIDTAB_INNER_ENTRIES && *pos < count) {
  328. rc = sidtab_convert_tree(&edst->ptr_inner->entries[i],
  329. &esrc->ptr_inner->entries[i],
  330. pos, count, level - 1,
  331. convert);
  332. if (rc)
  333. return rc;
  334. i++;
  335. }
  336. } else {
  337. if (!edst->ptr_leaf) {
  338. edst->ptr_leaf =
  339. kzalloc(SIDTAB_NODE_ALLOC_SIZE, GFP_KERNEL);
  340. if (!edst->ptr_leaf)
  341. return -ENOMEM;
  342. }
  343. i = 0;
  344. while (i < SIDTAB_LEAF_ENTRIES && *pos < count) {
  345. rc = services_convert_context(
  346. convert->args,
  347. &esrc->ptr_leaf->entries[i].context,
  348. &edst->ptr_leaf->entries[i].context,
  349. GFP_KERNEL);
  350. if (rc)
  351. return rc;
  352. (*pos)++;
  353. i++;
  354. }
  355. cond_resched();
  356. }
  357. return 0;
  358. }
  359. int sidtab_convert(struct sidtab *s, struct sidtab_convert_params *params)
  360. {
  361. unsigned long flags;
  362. u32 count, level, pos;
  363. int rc;
  364. spin_lock_irqsave(&s->lock, flags);
  365. /* concurrent policy loads are not allowed */
  366. if (s->convert) {
  367. spin_unlock_irqrestore(&s->lock, flags);
  368. return -EBUSY;
  369. }
  370. count = s->count;
  371. level = sidtab_level_from_count(count);
  372. /* allocate last leaf in the new sidtab (to avoid race with
  373. * live convert)
  374. */
  375. rc = sidtab_do_lookup(params->target, count - 1, 1) ? 0 : -ENOMEM;
  376. if (rc) {
  377. spin_unlock_irqrestore(&s->lock, flags);
  378. return rc;
  379. }
  380. /* set count in case no new entries are added during conversion */
  381. params->target->count = count;
  382. /* enable live convert of new entries */
  383. s->convert = params;
  384. /* we can safely convert the tree outside the lock */
  385. spin_unlock_irqrestore(&s->lock, flags);
  386. pr_info("SELinux: Converting %u SID table entries...\n", count);
  387. /* convert all entries not covered by live convert */
  388. pos = 0;
  389. rc = sidtab_convert_tree(&params->target->roots[level],
  390. &s->roots[level], &pos, count, level, params);
  391. if (rc) {
  392. /* we need to keep the old table - disable live convert */
  393. spin_lock_irqsave(&s->lock, flags);
  394. s->convert = NULL;
  395. spin_unlock_irqrestore(&s->lock, flags);
  396. return rc;
  397. }
  398. /*
  399. * The hashtable can also be modified in sidtab_context_to_sid()
  400. * so we must re-acquire the lock here.
  401. */
  402. spin_lock_irqsave(&s->lock, flags);
  403. sidtab_convert_hashtable(params->target, count);
  404. spin_unlock_irqrestore(&s->lock, flags);
  405. return 0;
  406. }
  407. void sidtab_cancel_convert(struct sidtab *s)
  408. {
  409. unsigned long flags;
  410. /* cancelling policy load - disable live convert of sidtab */
  411. spin_lock_irqsave(&s->lock, flags);
  412. s->convert = NULL;
  413. spin_unlock_irqrestore(&s->lock, flags);
  414. }
  415. void sidtab_freeze_begin(struct sidtab *s, unsigned long *flags)
  416. __acquires(&s->lock)
  417. {
  418. spin_lock_irqsave(&s->lock, *flags);
  419. s->frozen = true;
  420. s->convert = NULL;
  421. }
  422. void sidtab_freeze_end(struct sidtab *s, unsigned long *flags)
  423. __releases(&s->lock)
  424. {
  425. spin_unlock_irqrestore(&s->lock, *flags);
  426. }
  427. static void sidtab_destroy_entry(struct sidtab_entry *entry)
  428. {
  429. context_destroy(&entry->context);
  430. #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0
  431. kfree(rcu_dereference_raw(entry->cache));
  432. #endif
  433. }
  434. static void sidtab_destroy_tree(union sidtab_entry_inner entry, u32 level)
  435. {
  436. u32 i;
  437. if (level != 0) {
  438. struct sidtab_node_inner *node = entry.ptr_inner;
  439. if (!node)
  440. return;
  441. for (i = 0; i < SIDTAB_INNER_ENTRIES; i++)
  442. sidtab_destroy_tree(node->entries[i], level - 1);
  443. kfree(node);
  444. } else {
  445. struct sidtab_node_leaf *node = entry.ptr_leaf;
  446. if (!node)
  447. return;
  448. for (i = 0; i < SIDTAB_LEAF_ENTRIES; i++)
  449. sidtab_destroy_entry(&node->entries[i]);
  450. kfree(node);
  451. }
  452. }
  453. void sidtab_destroy(struct sidtab *s)
  454. {
  455. u32 i, level;
  456. for (i = 0; i < SECINITSID_NUM; i++)
  457. if (s->isids[i].set)
  458. sidtab_destroy_entry(&s->isids[i].entry);
  459. level = SIDTAB_MAX_LEVEL;
  460. while (level && !s->roots[level].ptr_inner)
  461. --level;
  462. sidtab_destroy_tree(s->roots[level], level);
  463. /*
  464. * The context_to_sid hashtable's objects are all shared
  465. * with the isids array and context tree, and so don't need
  466. * to be cleaned up here.
  467. */
  468. }
  469. #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0
  470. void sidtab_sid2str_put(struct sidtab *s, struct sidtab_entry *entry,
  471. const char *str, u32 str_len)
  472. {
  473. struct sidtab_str_cache *cache, *victim = NULL;
  474. unsigned long flags;
  475. /* do not cache invalid contexts */
  476. if (entry->context.len)
  477. return;
  478. spin_lock_irqsave(&s->cache_lock, flags);
  479. cache = rcu_dereference_protected(entry->cache,
  480. lockdep_is_held(&s->cache_lock));
  481. if (cache) {
  482. /* entry in cache - just bump to the head of LRU list */
  483. list_move(&cache->lru_member, &s->cache_lru_list);
  484. goto out_unlock;
  485. }
  486. cache = kmalloc(struct_size(cache, str, str_len), GFP_ATOMIC);
  487. if (!cache)
  488. goto out_unlock;
  489. if (s->cache_free_slots == 0) {
  490. /* pop a cache entry from the tail and free it */
  491. victim = container_of(s->cache_lru_list.prev,
  492. struct sidtab_str_cache, lru_member);
  493. list_del(&victim->lru_member);
  494. rcu_assign_pointer(victim->parent->cache, NULL);
  495. } else {
  496. s->cache_free_slots--;
  497. }
  498. cache->parent = entry;
  499. cache->len = str_len;
  500. memcpy(cache->str, str, str_len);
  501. list_add(&cache->lru_member, &s->cache_lru_list);
  502. rcu_assign_pointer(entry->cache, cache);
  503. out_unlock:
  504. spin_unlock_irqrestore(&s->cache_lock, flags);
  505. kfree_rcu(victim, rcu_member);
  506. }
  507. int sidtab_sid2str_get(struct sidtab *s, struct sidtab_entry *entry, char **out,
  508. u32 *out_len)
  509. {
  510. struct sidtab_str_cache *cache;
  511. int rc = 0;
  512. if (entry->context.len)
  513. return -ENOENT; /* do not cache invalid contexts */
  514. rcu_read_lock();
  515. cache = rcu_dereference(entry->cache);
  516. if (!cache) {
  517. rc = -ENOENT;
  518. } else {
  519. *out_len = cache->len;
  520. if (out) {
  521. *out = kmemdup(cache->str, cache->len, GFP_ATOMIC);
  522. if (!*out)
  523. rc = -ENOMEM;
  524. }
  525. }
  526. rcu_read_unlock();
  527. if (!rc && out)
  528. sidtab_sid2str_put(s, entry, *out, *out_len);
  529. return rc;
  530. }
  531. #endif /* CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0 */