sidtab.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. struct context *sidtab_search(struct sidtab *s, u32 sid)
  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 (cur == NULL || sid != cur->sid) {
  88. /* Remap invalid SIDs to the unlabeled SID. */
  89. sid = SECINITSID_UNLABELED;
  90. hvalue = SIDTAB_HASH(sid);
  91. cur = s->htable[hvalue];
  92. while (cur != NULL && sid > cur->sid)
  93. cur = cur->next;
  94. if (!cur || sid != cur->sid)
  95. return NULL;
  96. }
  97. return &cur->context;
  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 != NULL) {
  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. void sidtab_map_remove_on_error(struct sidtab *s,
  122. int (*apply) (u32 sid,
  123. struct context *context,
  124. void *args),
  125. void *args)
  126. {
  127. int i, ret;
  128. struct sidtab_node *last, *cur, *temp;
  129. if (!s)
  130. return;
  131. for (i = 0; i < SIDTAB_SIZE; i++) {
  132. last = NULL;
  133. cur = s->htable[i];
  134. while (cur != NULL) {
  135. ret = apply(cur->sid, &cur->context, args);
  136. if (ret) {
  137. if (last)
  138. last->next = cur->next;
  139. else
  140. s->htable[i] = cur->next;
  141. temp = cur;
  142. cur = cur->next;
  143. context_destroy(&temp->context);
  144. kfree(temp);
  145. s->nel--;
  146. } else {
  147. last = cur;
  148. cur = cur->next;
  149. }
  150. }
  151. }
  152. return;
  153. }
  154. static inline u32 sidtab_search_context(struct sidtab *s,
  155. struct context *context)
  156. {
  157. int i;
  158. struct sidtab_node *cur;
  159. for (i = 0; i < SIDTAB_SIZE; i++) {
  160. cur = s->htable[i];
  161. while (cur != NULL) {
  162. if (context_cmp(&cur->context, context))
  163. return cur->sid;
  164. cur = cur->next;
  165. }
  166. }
  167. return 0;
  168. }
  169. int sidtab_context_to_sid(struct sidtab *s,
  170. struct context *context,
  171. u32 *out_sid)
  172. {
  173. u32 sid;
  174. int ret = 0;
  175. unsigned long flags;
  176. *out_sid = SECSID_NULL;
  177. sid = sidtab_search_context(s, context);
  178. if (!sid) {
  179. SIDTAB_LOCK(s, flags);
  180. /* Rescan now that we hold the lock. */
  181. sid = sidtab_search_context(s, context);
  182. if (sid)
  183. goto unlock_out;
  184. /* No SID exists for the context. Allocate a new one. */
  185. if (s->next_sid == UINT_MAX || s->shutdown) {
  186. ret = -ENOMEM;
  187. goto unlock_out;
  188. }
  189. sid = s->next_sid++;
  190. ret = sidtab_insert(s, sid, context);
  191. if (ret)
  192. s->next_sid--;
  193. unlock_out:
  194. SIDTAB_UNLOCK(s, flags);
  195. }
  196. if (ret)
  197. return ret;
  198. *out_sid = sid;
  199. return 0;
  200. }
  201. void sidtab_hash_eval(struct sidtab *h, char *tag)
  202. {
  203. int i, chain_len, slots_used, max_chain_len;
  204. struct sidtab_node *cur;
  205. slots_used = 0;
  206. max_chain_len = 0;
  207. for (i = 0; i < SIDTAB_SIZE; i++) {
  208. cur = h->htable[i];
  209. if (cur) {
  210. slots_used++;
  211. chain_len = 0;
  212. while (cur) {
  213. chain_len++;
  214. cur = cur->next;
  215. }
  216. if (chain_len > max_chain_len)
  217. max_chain_len = chain_len;
  218. }
  219. }
  220. printk(KERN_DEBUG "%s: %d entries and %d/%d buckets used, longest "
  221. "chain length %d\n", tag, h->nel, slots_used, SIDTAB_SIZE,
  222. max_chain_len);
  223. }
  224. void sidtab_destroy(struct sidtab *s)
  225. {
  226. int i;
  227. struct sidtab_node *cur, *temp;
  228. if (!s)
  229. return;
  230. for (i = 0; i < SIDTAB_SIZE; i++) {
  231. cur = s->htable[i];
  232. while (cur != NULL) {
  233. temp = cur;
  234. cur = cur->next;
  235. context_destroy(&temp->context);
  236. kfree(temp);
  237. }
  238. s->htable[i] = NULL;
  239. }
  240. kfree(s->htable);
  241. s->htable = NULL;
  242. s->nel = 0;
  243. s->next_sid = 1;
  244. }
  245. void sidtab_set(struct sidtab *dst, struct sidtab *src)
  246. {
  247. unsigned long flags;
  248. SIDTAB_LOCK(src, flags);
  249. dst->htable = src->htable;
  250. dst->nel = src->nel;
  251. dst->next_sid = src->next_sid;
  252. dst->shutdown = 0;
  253. SIDTAB_UNLOCK(src, flags);
  254. }
  255. void sidtab_shutdown(struct sidtab *s)
  256. {
  257. unsigned long flags;
  258. SIDTAB_LOCK(s, flags);
  259. s->shutdown = 1;
  260. SIDTAB_UNLOCK(s, flags);
  261. }