sidtab.c 5.3 KB

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