sidtab.c 5.1 KB

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