avtab.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Implementation of the access vector table type.
  3. *
  4. * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
  5. */
  6. /* Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
  7. *
  8. * Added conditional policy language extensions
  9. *
  10. * Copyright (C) 2003 Tresys Technology, LLC
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, version 2.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/errno.h>
  19. #include "avtab.h"
  20. #include "policydb.h"
  21. #define AVTAB_HASH(keyp) \
  22. ((keyp->target_class + \
  23. (keyp->target_type << 2) + \
  24. (keyp->source_type << 9)) & \
  25. AVTAB_HASH_MASK)
  26. static kmem_cache_t *avtab_node_cachep;
  27. static struct avtab_node*
  28. avtab_insert_node(struct avtab *h, int hvalue,
  29. struct avtab_node * prev, struct avtab_node * cur,
  30. struct avtab_key *key, struct avtab_datum *datum)
  31. {
  32. struct avtab_node * newnode;
  33. newnode = kmem_cache_alloc(avtab_node_cachep, SLAB_KERNEL);
  34. if (newnode == NULL)
  35. return NULL;
  36. memset(newnode, 0, sizeof(struct avtab_node));
  37. newnode->key = *key;
  38. newnode->datum = *datum;
  39. if (prev) {
  40. newnode->next = prev->next;
  41. prev->next = newnode;
  42. } else {
  43. newnode->next = h->htable[hvalue];
  44. h->htable[hvalue] = newnode;
  45. }
  46. h->nel++;
  47. return newnode;
  48. }
  49. static int avtab_insert(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum)
  50. {
  51. int hvalue;
  52. struct avtab_node *prev, *cur, *newnode;
  53. if (!h)
  54. return -EINVAL;
  55. hvalue = AVTAB_HASH(key);
  56. for (prev = NULL, cur = h->htable[hvalue];
  57. cur;
  58. prev = cur, cur = cur->next) {
  59. if (key->source_type == cur->key.source_type &&
  60. key->target_type == cur->key.target_type &&
  61. key->target_class == cur->key.target_class &&
  62. (datum->specified & cur->datum.specified))
  63. return -EEXIST;
  64. if (key->source_type < cur->key.source_type)
  65. break;
  66. if (key->source_type == cur->key.source_type &&
  67. key->target_type < cur->key.target_type)
  68. break;
  69. if (key->source_type == cur->key.source_type &&
  70. key->target_type == cur->key.target_type &&
  71. key->target_class < cur->key.target_class)
  72. break;
  73. }
  74. newnode = avtab_insert_node(h, hvalue, prev, cur, key, datum);
  75. if(!newnode)
  76. return -ENOMEM;
  77. return 0;
  78. }
  79. /* Unlike avtab_insert(), this function allow multiple insertions of the same
  80. * key/specified mask into the table, as needed by the conditional avtab.
  81. * It also returns a pointer to the node inserted.
  82. */
  83. struct avtab_node *
  84. avtab_insert_nonunique(struct avtab * h, struct avtab_key * key, struct avtab_datum * datum)
  85. {
  86. int hvalue;
  87. struct avtab_node *prev, *cur, *newnode;
  88. if (!h)
  89. return NULL;
  90. hvalue = AVTAB_HASH(key);
  91. for (prev = NULL, cur = h->htable[hvalue];
  92. cur;
  93. prev = cur, cur = cur->next) {
  94. if (key->source_type == cur->key.source_type &&
  95. key->target_type == cur->key.target_type &&
  96. key->target_class == cur->key.target_class &&
  97. (datum->specified & cur->datum.specified))
  98. break;
  99. if (key->source_type < cur->key.source_type)
  100. break;
  101. if (key->source_type == cur->key.source_type &&
  102. key->target_type < cur->key.target_type)
  103. break;
  104. if (key->source_type == cur->key.source_type &&
  105. key->target_type == cur->key.target_type &&
  106. key->target_class < cur->key.target_class)
  107. break;
  108. }
  109. newnode = avtab_insert_node(h, hvalue, prev, cur, key, datum);
  110. return newnode;
  111. }
  112. struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *key, int specified)
  113. {
  114. int hvalue;
  115. struct avtab_node *cur;
  116. if (!h)
  117. return NULL;
  118. hvalue = AVTAB_HASH(key);
  119. for (cur = h->htable[hvalue]; cur; cur = cur->next) {
  120. if (key->source_type == cur->key.source_type &&
  121. key->target_type == cur->key.target_type &&
  122. key->target_class == cur->key.target_class &&
  123. (specified & cur->datum.specified))
  124. return &cur->datum;
  125. if (key->source_type < cur->key.source_type)
  126. break;
  127. if (key->source_type == cur->key.source_type &&
  128. key->target_type < cur->key.target_type)
  129. break;
  130. if (key->source_type == cur->key.source_type &&
  131. key->target_type == cur->key.target_type &&
  132. key->target_class < cur->key.target_class)
  133. break;
  134. }
  135. return NULL;
  136. }
  137. /* This search function returns a node pointer, and can be used in
  138. * conjunction with avtab_search_next_node()
  139. */
  140. struct avtab_node*
  141. avtab_search_node(struct avtab *h, struct avtab_key *key, int specified)
  142. {
  143. int hvalue;
  144. struct avtab_node *cur;
  145. if (!h)
  146. return NULL;
  147. hvalue = AVTAB_HASH(key);
  148. for (cur = h->htable[hvalue]; cur; cur = cur->next) {
  149. if (key->source_type == cur->key.source_type &&
  150. key->target_type == cur->key.target_type &&
  151. key->target_class == cur->key.target_class &&
  152. (specified & cur->datum.specified))
  153. return cur;
  154. if (key->source_type < cur->key.source_type)
  155. break;
  156. if (key->source_type == cur->key.source_type &&
  157. key->target_type < cur->key.target_type)
  158. break;
  159. if (key->source_type == cur->key.source_type &&
  160. key->target_type == cur->key.target_type &&
  161. key->target_class < cur->key.target_class)
  162. break;
  163. }
  164. return NULL;
  165. }
  166. struct avtab_node*
  167. avtab_search_node_next(struct avtab_node *node, int specified)
  168. {
  169. struct avtab_node *cur;
  170. if (!node)
  171. return NULL;
  172. for (cur = node->next; cur; cur = cur->next) {
  173. if (node->key.source_type == cur->key.source_type &&
  174. node->key.target_type == cur->key.target_type &&
  175. node->key.target_class == cur->key.target_class &&
  176. (specified & cur->datum.specified))
  177. return cur;
  178. if (node->key.source_type < cur->key.source_type)
  179. break;
  180. if (node->key.source_type == cur->key.source_type &&
  181. node->key.target_type < cur->key.target_type)
  182. break;
  183. if (node->key.source_type == cur->key.source_type &&
  184. node->key.target_type == cur->key.target_type &&
  185. node->key.target_class < cur->key.target_class)
  186. break;
  187. }
  188. return NULL;
  189. }
  190. void avtab_destroy(struct avtab *h)
  191. {
  192. int i;
  193. struct avtab_node *cur, *temp;
  194. if (!h || !h->htable)
  195. return;
  196. for (i = 0; i < AVTAB_SIZE; i++) {
  197. cur = h->htable[i];
  198. while (cur != NULL) {
  199. temp = cur;
  200. cur = cur->next;
  201. kmem_cache_free(avtab_node_cachep, temp);
  202. }
  203. h->htable[i] = NULL;
  204. }
  205. vfree(h->htable);
  206. h->htable = NULL;
  207. }
  208. int avtab_init(struct avtab *h)
  209. {
  210. int i;
  211. h->htable = vmalloc(sizeof(*(h->htable)) * AVTAB_SIZE);
  212. if (!h->htable)
  213. return -ENOMEM;
  214. for (i = 0; i < AVTAB_SIZE; i++)
  215. h->htable[i] = NULL;
  216. h->nel = 0;
  217. return 0;
  218. }
  219. void avtab_hash_eval(struct avtab *h, char *tag)
  220. {
  221. int i, chain_len, slots_used, max_chain_len;
  222. struct avtab_node *cur;
  223. slots_used = 0;
  224. max_chain_len = 0;
  225. for (i = 0; i < AVTAB_SIZE; i++) {
  226. cur = h->htable[i];
  227. if (cur) {
  228. slots_used++;
  229. chain_len = 0;
  230. while (cur) {
  231. chain_len++;
  232. cur = cur->next;
  233. }
  234. if (chain_len > max_chain_len)
  235. max_chain_len = chain_len;
  236. }
  237. }
  238. printk(KERN_INFO "%s: %d entries and %d/%d buckets used, longest "
  239. "chain length %d\n", tag, h->nel, slots_used, AVTAB_SIZE,
  240. max_chain_len);
  241. }
  242. int avtab_read_item(void *fp, struct avtab_datum *avdatum, struct avtab_key *avkey)
  243. {
  244. u32 buf[7];
  245. u32 items, items2;
  246. int rc;
  247. memset(avkey, 0, sizeof(struct avtab_key));
  248. memset(avdatum, 0, sizeof(struct avtab_datum));
  249. rc = next_entry(buf, fp, sizeof(u32));
  250. if (rc < 0) {
  251. printk(KERN_ERR "security: avtab: truncated entry\n");
  252. goto bad;
  253. }
  254. items2 = le32_to_cpu(buf[0]);
  255. if (items2 > ARRAY_SIZE(buf)) {
  256. printk(KERN_ERR "security: avtab: entry overflow\n");
  257. goto bad;
  258. }
  259. rc = next_entry(buf, fp, sizeof(u32)*items2);
  260. if (rc < 0) {
  261. printk(KERN_ERR "security: avtab: truncated entry\n");
  262. goto bad;
  263. }
  264. items = 0;
  265. avkey->source_type = le32_to_cpu(buf[items++]);
  266. avkey->target_type = le32_to_cpu(buf[items++]);
  267. avkey->target_class = le32_to_cpu(buf[items++]);
  268. avdatum->specified = le32_to_cpu(buf[items++]);
  269. if (!(avdatum->specified & (AVTAB_AV | AVTAB_TYPE))) {
  270. printk(KERN_ERR "security: avtab: null entry\n");
  271. goto bad;
  272. }
  273. if ((avdatum->specified & AVTAB_AV) &&
  274. (avdatum->specified & AVTAB_TYPE)) {
  275. printk(KERN_ERR "security: avtab: entry has both access vectors and types\n");
  276. goto bad;
  277. }
  278. if (avdatum->specified & AVTAB_AV) {
  279. if (avdatum->specified & AVTAB_ALLOWED)
  280. avtab_allowed(avdatum) = le32_to_cpu(buf[items++]);
  281. if (avdatum->specified & AVTAB_AUDITDENY)
  282. avtab_auditdeny(avdatum) = le32_to_cpu(buf[items++]);
  283. if (avdatum->specified & AVTAB_AUDITALLOW)
  284. avtab_auditallow(avdatum) = le32_to_cpu(buf[items++]);
  285. } else {
  286. if (avdatum->specified & AVTAB_TRANSITION)
  287. avtab_transition(avdatum) = le32_to_cpu(buf[items++]);
  288. if (avdatum->specified & AVTAB_CHANGE)
  289. avtab_change(avdatum) = le32_to_cpu(buf[items++]);
  290. if (avdatum->specified & AVTAB_MEMBER)
  291. avtab_member(avdatum) = le32_to_cpu(buf[items++]);
  292. }
  293. if (items != items2) {
  294. printk(KERN_ERR "security: avtab: entry only had %d items, expected %d\n",
  295. items2, items);
  296. goto bad;
  297. }
  298. return 0;
  299. bad:
  300. return -1;
  301. }
  302. int avtab_read(struct avtab *a, void *fp, u32 config)
  303. {
  304. int rc;
  305. struct avtab_key avkey;
  306. struct avtab_datum avdatum;
  307. u32 buf[1];
  308. u32 nel, i;
  309. rc = next_entry(buf, fp, sizeof(u32));
  310. if (rc < 0) {
  311. printk(KERN_ERR "security: avtab: truncated table\n");
  312. goto bad;
  313. }
  314. nel = le32_to_cpu(buf[0]);
  315. if (!nel) {
  316. printk(KERN_ERR "security: avtab: table is empty\n");
  317. rc = -EINVAL;
  318. goto bad;
  319. }
  320. for (i = 0; i < nel; i++) {
  321. if (avtab_read_item(fp, &avdatum, &avkey)) {
  322. rc = -EINVAL;
  323. goto bad;
  324. }
  325. rc = avtab_insert(a, &avkey, &avdatum);
  326. if (rc) {
  327. if (rc == -ENOMEM)
  328. printk(KERN_ERR "security: avtab: out of memory\n");
  329. if (rc == -EEXIST)
  330. printk(KERN_ERR "security: avtab: duplicate entry\n");
  331. goto bad;
  332. }
  333. }
  334. rc = 0;
  335. out:
  336. return rc;
  337. bad:
  338. avtab_destroy(a);
  339. goto out;
  340. }
  341. void avtab_cache_init(void)
  342. {
  343. avtab_node_cachep = kmem_cache_create("avtab_node",
  344. sizeof(struct avtab_node),
  345. 0, SLAB_PANIC, NULL, NULL);
  346. }
  347. void avtab_cache_destroy(void)
  348. {
  349. kmem_cache_destroy (avtab_node_cachep);
  350. }