sidtab.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Implementation of the SID table type.
  4. *
  5. * Author : Stephen Smalley, <sds@tycho.nsa.gov>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/slab.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/errno.h>
  11. #include "flask.h"
  12. #include "security.h"
  13. #include "sidtab.h"
  14. #define SIDTAB_HASH(sid) \
  15. (sid & SIDTAB_HASH_MASK)
  16. int sidtab_init(struct sidtab *s)
  17. {
  18. int i;
  19. s->htable = kmalloc_array(SIDTAB_SIZE, sizeof(*s->htable), GFP_ATOMIC);
  20. if (!s->htable)
  21. return -ENOMEM;
  22. for (i = 0; i < SIDTAB_SIZE; i++)
  23. s->htable[i] = NULL;
  24. s->nel = 0;
  25. s->next_sid = 1;
  26. s->shutdown = 0;
  27. spin_lock_init(&s->lock);
  28. return 0;
  29. }
  30. int sidtab_insert(struct sidtab *s, u32 sid, struct context *context)
  31. {
  32. int hvalue;
  33. struct sidtab_node *prev, *cur, *newnode;
  34. if (!s)
  35. return -ENOMEM;
  36. hvalue = SIDTAB_HASH(sid);
  37. prev = NULL;
  38. cur = s->htable[hvalue];
  39. while (cur && sid > cur->sid) {
  40. prev = cur;
  41. cur = cur->next;
  42. }
  43. if (cur && sid == cur->sid)
  44. return -EEXIST;
  45. newnode = kmalloc(sizeof(*newnode), GFP_ATOMIC);
  46. if (!newnode)
  47. return -ENOMEM;
  48. newnode->sid = sid;
  49. if (context_cpy(&newnode->context, context)) {
  50. kfree(newnode);
  51. return -ENOMEM;
  52. }
  53. if (prev) {
  54. newnode->next = prev->next;
  55. wmb();
  56. prev->next = newnode;
  57. } else {
  58. newnode->next = s->htable[hvalue];
  59. wmb();
  60. s->htable[hvalue] = newnode;
  61. }
  62. s->nel++;
  63. if (sid >= s->next_sid)
  64. s->next_sid = sid + 1;
  65. return 0;
  66. }
  67. static struct context *sidtab_search_core(struct sidtab *s, u32 sid, int force)
  68. {
  69. int hvalue;
  70. struct sidtab_node *cur;
  71. if (!s)
  72. return NULL;
  73. hvalue = SIDTAB_HASH(sid);
  74. cur = s->htable[hvalue];
  75. while (cur && sid > cur->sid)
  76. cur = cur->next;
  77. if (force && cur && sid == cur->sid && cur->context.len)
  78. return &cur->context;
  79. if (!cur || sid != cur->sid || cur->context.len) {
  80. /* Remap invalid SIDs to the unlabeled SID. */
  81. sid = SECINITSID_UNLABELED;
  82. hvalue = SIDTAB_HASH(sid);
  83. cur = s->htable[hvalue];
  84. while (cur && sid > cur->sid)
  85. cur = cur->next;
  86. if (!cur || sid != cur->sid)
  87. return NULL;
  88. }
  89. return &cur->context;
  90. }
  91. struct context *sidtab_search(struct sidtab *s, u32 sid)
  92. {
  93. return sidtab_search_core(s, sid, 0);
  94. }
  95. struct context *sidtab_search_force(struct sidtab *s, u32 sid)
  96. {
  97. return sidtab_search_core(s, sid, 1);
  98. }
  99. int sidtab_map(struct sidtab *s,
  100. int (*apply) (u32 sid,
  101. struct context *context,
  102. void *args),
  103. void *args)
  104. {
  105. int i, rc = 0;
  106. struct sidtab_node *cur;
  107. if (!s)
  108. goto out;
  109. for (i = 0; i < SIDTAB_SIZE; i++) {
  110. cur = s->htable[i];
  111. while (cur) {
  112. rc = apply(cur->sid, &cur->context, args);
  113. if (rc)
  114. goto out;
  115. cur = cur->next;
  116. }
  117. }
  118. out:
  119. return rc;
  120. }
  121. static void sidtab_update_cache(struct sidtab *s, struct sidtab_node *n, int loc)
  122. {
  123. BUG_ON(loc >= SIDTAB_CACHE_LEN);
  124. while (loc > 0) {
  125. s->cache[loc] = s->cache[loc - 1];
  126. loc--;
  127. }
  128. s->cache[0] = n;
  129. }
  130. static inline u32 sidtab_search_context(struct sidtab *s,
  131. struct context *context)
  132. {
  133. int i;
  134. struct sidtab_node *cur;
  135. for (i = 0; i < SIDTAB_SIZE; i++) {
  136. cur = s->htable[i];
  137. while (cur) {
  138. if (context_cmp(&cur->context, context)) {
  139. sidtab_update_cache(s, cur, SIDTAB_CACHE_LEN - 1);
  140. return cur->sid;
  141. }
  142. cur = cur->next;
  143. }
  144. }
  145. return 0;
  146. }
  147. static inline u32 sidtab_search_cache(struct sidtab *s, struct context *context)
  148. {
  149. int i;
  150. struct sidtab_node *node;
  151. for (i = 0; i < SIDTAB_CACHE_LEN; i++) {
  152. node = s->cache[i];
  153. if (unlikely(!node))
  154. return 0;
  155. if (context_cmp(&node->context, context)) {
  156. sidtab_update_cache(s, node, i);
  157. return node->sid;
  158. }
  159. }
  160. return 0;
  161. }
  162. int sidtab_context_to_sid(struct sidtab *s,
  163. struct context *context,
  164. u32 *out_sid)
  165. {
  166. u32 sid;
  167. int ret = 0;
  168. unsigned long flags;
  169. *out_sid = SECSID_NULL;
  170. sid = sidtab_search_cache(s, context);
  171. if (!sid)
  172. sid = sidtab_search_context(s, context);
  173. if (!sid) {
  174. spin_lock_irqsave(&s->lock, flags);
  175. /* Rescan now that we hold the lock. */
  176. sid = sidtab_search_context(s, context);
  177. if (sid)
  178. goto unlock_out;
  179. /* No SID exists for the context. Allocate a new one. */
  180. if (s->next_sid == UINT_MAX || s->shutdown) {
  181. ret = -ENOMEM;
  182. goto unlock_out;
  183. }
  184. sid = s->next_sid++;
  185. if (context->len)
  186. pr_info("SELinux: Context %s is not valid (left unmapped).\n",
  187. context->str);
  188. ret = sidtab_insert(s, sid, context);
  189. if (ret)
  190. s->next_sid--;
  191. unlock_out:
  192. spin_unlock_irqrestore(&s->lock, flags);
  193. }
  194. if (ret)
  195. return ret;
  196. *out_sid = sid;
  197. return 0;
  198. }
  199. void sidtab_hash_eval(struct sidtab *h, char *tag)
  200. {
  201. int i, chain_len, slots_used, max_chain_len;
  202. struct sidtab_node *cur;
  203. slots_used = 0;
  204. max_chain_len = 0;
  205. for (i = 0; i < SIDTAB_SIZE; i++) {
  206. cur = h->htable[i];
  207. if (cur) {
  208. slots_used++;
  209. chain_len = 0;
  210. while (cur) {
  211. chain_len++;
  212. cur = cur->next;
  213. }
  214. if (chain_len > max_chain_len)
  215. max_chain_len = chain_len;
  216. }
  217. }
  218. pr_debug("%s: %d entries and %d/%d buckets used, longest "
  219. "chain length %d\n", tag, h->nel, slots_used, SIDTAB_SIZE,
  220. max_chain_len);
  221. }
  222. void sidtab_destroy(struct sidtab *s)
  223. {
  224. int i;
  225. struct sidtab_node *cur, *temp;
  226. if (!s)
  227. return;
  228. for (i = 0; i < SIDTAB_SIZE; i++) {
  229. cur = s->htable[i];
  230. while (cur) {
  231. temp = cur;
  232. cur = cur->next;
  233. context_destroy(&temp->context);
  234. kfree(temp);
  235. }
  236. s->htable[i] = NULL;
  237. }
  238. kfree(s->htable);
  239. s->htable = NULL;
  240. s->nel = 0;
  241. s->next_sid = 1;
  242. }
  243. void sidtab_set(struct sidtab *dst, struct sidtab *src)
  244. {
  245. unsigned long flags;
  246. int i;
  247. spin_lock_irqsave(&src->lock, flags);
  248. dst->htable = src->htable;
  249. dst->nel = src->nel;
  250. dst->next_sid = src->next_sid;
  251. dst->shutdown = 0;
  252. for (i = 0; i < SIDTAB_CACHE_LEN; i++)
  253. dst->cache[i] = NULL;
  254. spin_unlock_irqrestore(&src->lock, flags);
  255. }
  256. void sidtab_shutdown(struct sidtab *s)
  257. {
  258. unsigned long flags;
  259. spin_lock_irqsave(&s->lock, flags);
  260. s->shutdown = 1;
  261. spin_unlock_irqrestore(&s->lock, flags);
  262. }