sidtab.c 5.5 KB

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