avtab.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  54. if (!h)
  55. return -EINVAL;
  56. hvalue = AVTAB_HASH(key);
  57. for (prev = NULL, cur = h->htable[hvalue];
  58. cur;
  59. prev = cur, cur = cur->next) {
  60. if (key->source_type == cur->key.source_type &&
  61. key->target_type == cur->key.target_type &&
  62. key->target_class == cur->key.target_class &&
  63. (specified & cur->key.specified))
  64. return -EEXIST;
  65. if (key->source_type < cur->key.source_type)
  66. break;
  67. if (key->source_type == cur->key.source_type &&
  68. key->target_type < cur->key.target_type)
  69. break;
  70. if (key->source_type == cur->key.source_type &&
  71. key->target_type == cur->key.target_type &&
  72. key->target_class < cur->key.target_class)
  73. break;
  74. }
  75. newnode = avtab_insert_node(h, hvalue, prev, cur, key, datum);
  76. if(!newnode)
  77. return -ENOMEM;
  78. return 0;
  79. }
  80. /* Unlike avtab_insert(), this function allow multiple insertions of the same
  81. * key/specified mask into the table, as needed by the conditional avtab.
  82. * It also returns a pointer to the node inserted.
  83. */
  84. struct avtab_node *
  85. avtab_insert_nonunique(struct avtab * h, struct avtab_key * key, struct avtab_datum * datum)
  86. {
  87. int hvalue;
  88. struct avtab_node *prev, *cur, *newnode;
  89. u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  90. if (!h)
  91. return NULL;
  92. hvalue = AVTAB_HASH(key);
  93. for (prev = NULL, cur = h->htable[hvalue];
  94. cur;
  95. prev = cur, cur = cur->next) {
  96. if (key->source_type == cur->key.source_type &&
  97. key->target_type == cur->key.target_type &&
  98. key->target_class == cur->key.target_class &&
  99. (specified & cur->key.specified))
  100. break;
  101. if (key->source_type < cur->key.source_type)
  102. break;
  103. if (key->source_type == cur->key.source_type &&
  104. key->target_type < cur->key.target_type)
  105. break;
  106. if (key->source_type == cur->key.source_type &&
  107. key->target_type == cur->key.target_type &&
  108. key->target_class < cur->key.target_class)
  109. break;
  110. }
  111. newnode = avtab_insert_node(h, hvalue, prev, cur, key, datum);
  112. return newnode;
  113. }
  114. struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *key)
  115. {
  116. int hvalue;
  117. struct avtab_node *cur;
  118. u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  119. if (!h)
  120. return NULL;
  121. hvalue = AVTAB_HASH(key);
  122. for (cur = h->htable[hvalue]; cur; cur = cur->next) {
  123. if (key->source_type == cur->key.source_type &&
  124. key->target_type == cur->key.target_type &&
  125. key->target_class == cur->key.target_class &&
  126. (specified & cur->key.specified))
  127. return &cur->datum;
  128. if (key->source_type < cur->key.source_type)
  129. break;
  130. if (key->source_type == cur->key.source_type &&
  131. key->target_type < cur->key.target_type)
  132. break;
  133. if (key->source_type == cur->key.source_type &&
  134. key->target_type == cur->key.target_type &&
  135. key->target_class < cur->key.target_class)
  136. break;
  137. }
  138. return NULL;
  139. }
  140. /* This search function returns a node pointer, and can be used in
  141. * conjunction with avtab_search_next_node()
  142. */
  143. struct avtab_node*
  144. avtab_search_node(struct avtab *h, struct avtab_key *key)
  145. {
  146. int hvalue;
  147. struct avtab_node *cur;
  148. u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  149. if (!h)
  150. return NULL;
  151. hvalue = AVTAB_HASH(key);
  152. for (cur = h->htable[hvalue]; cur; cur = cur->next) {
  153. if (key->source_type == cur->key.source_type &&
  154. key->target_type == cur->key.target_type &&
  155. key->target_class == cur->key.target_class &&
  156. (specified & cur->key.specified))
  157. return cur;
  158. if (key->source_type < cur->key.source_type)
  159. break;
  160. if (key->source_type == cur->key.source_type &&
  161. key->target_type < cur->key.target_type)
  162. break;
  163. if (key->source_type == cur->key.source_type &&
  164. key->target_type == cur->key.target_type &&
  165. key->target_class < cur->key.target_class)
  166. break;
  167. }
  168. return NULL;
  169. }
  170. struct avtab_node*
  171. avtab_search_node_next(struct avtab_node *node, int specified)
  172. {
  173. struct avtab_node *cur;
  174. if (!node)
  175. return NULL;
  176. specified &= ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  177. for (cur = node->next; cur; cur = cur->next) {
  178. if (node->key.source_type == cur->key.source_type &&
  179. node->key.target_type == cur->key.target_type &&
  180. node->key.target_class == cur->key.target_class &&
  181. (specified & cur->key.specified))
  182. return cur;
  183. if (node->key.source_type < cur->key.source_type)
  184. break;
  185. if (node->key.source_type == cur->key.source_type &&
  186. node->key.target_type < cur->key.target_type)
  187. break;
  188. if (node->key.source_type == cur->key.source_type &&
  189. node->key.target_type == cur->key.target_type &&
  190. node->key.target_class < cur->key.target_class)
  191. break;
  192. }
  193. return NULL;
  194. }
  195. void avtab_destroy(struct avtab *h)
  196. {
  197. int i;
  198. struct avtab_node *cur, *temp;
  199. if (!h || !h->htable)
  200. return;
  201. for (i = 0; i < AVTAB_SIZE; i++) {
  202. cur = h->htable[i];
  203. while (cur != NULL) {
  204. temp = cur;
  205. cur = cur->next;
  206. kmem_cache_free(avtab_node_cachep, temp);
  207. }
  208. h->htable[i] = NULL;
  209. }
  210. vfree(h->htable);
  211. h->htable = NULL;
  212. }
  213. int avtab_init(struct avtab *h)
  214. {
  215. int i;
  216. h->htable = vmalloc(sizeof(*(h->htable)) * AVTAB_SIZE);
  217. if (!h->htable)
  218. return -ENOMEM;
  219. for (i = 0; i < AVTAB_SIZE; i++)
  220. h->htable[i] = NULL;
  221. h->nel = 0;
  222. return 0;
  223. }
  224. void avtab_hash_eval(struct avtab *h, char *tag)
  225. {
  226. int i, chain_len, slots_used, max_chain_len;
  227. struct avtab_node *cur;
  228. slots_used = 0;
  229. max_chain_len = 0;
  230. for (i = 0; i < AVTAB_SIZE; i++) {
  231. cur = h->htable[i];
  232. if (cur) {
  233. slots_used++;
  234. chain_len = 0;
  235. while (cur) {
  236. chain_len++;
  237. cur = cur->next;
  238. }
  239. if (chain_len > max_chain_len)
  240. max_chain_len = chain_len;
  241. }
  242. }
  243. printk(KERN_INFO "%s: %d entries and %d/%d buckets used, longest "
  244. "chain length %d\n", tag, h->nel, slots_used, AVTAB_SIZE,
  245. max_chain_len);
  246. }
  247. static uint16_t spec_order[] = {
  248. AVTAB_ALLOWED,
  249. AVTAB_AUDITDENY,
  250. AVTAB_AUDITALLOW,
  251. AVTAB_TRANSITION,
  252. AVTAB_CHANGE,
  253. AVTAB_MEMBER
  254. };
  255. int avtab_read_item(void *fp, u32 vers, struct avtab *a,
  256. int (*insertf)(struct avtab *a, struct avtab_key *k,
  257. struct avtab_datum *d, void *p),
  258. void *p)
  259. {
  260. __le16 buf16[4];
  261. u16 enabled;
  262. __le32 buf32[7];
  263. u32 items, items2, val;
  264. struct avtab_key key;
  265. struct avtab_datum datum;
  266. int i, rc;
  267. memset(&key, 0, sizeof(struct avtab_key));
  268. memset(&datum, 0, sizeof(struct avtab_datum));
  269. if (vers < POLICYDB_VERSION_AVTAB) {
  270. rc = next_entry(buf32, fp, sizeof(u32));
  271. if (rc < 0) {
  272. printk(KERN_ERR "security: avtab: truncated entry\n");
  273. return -1;
  274. }
  275. items2 = le32_to_cpu(buf32[0]);
  276. if (items2 > ARRAY_SIZE(buf32)) {
  277. printk(KERN_ERR "security: avtab: entry overflow\n");
  278. return -1;
  279. }
  280. rc = next_entry(buf32, fp, sizeof(u32)*items2);
  281. if (rc < 0) {
  282. printk(KERN_ERR "security: avtab: truncated entry\n");
  283. return -1;
  284. }
  285. items = 0;
  286. val = le32_to_cpu(buf32[items++]);
  287. key.source_type = (u16)val;
  288. if (key.source_type != val) {
  289. printk("security: avtab: truncated source type\n");
  290. return -1;
  291. }
  292. val = le32_to_cpu(buf32[items++]);
  293. key.target_type = (u16)val;
  294. if (key.target_type != val) {
  295. printk("security: avtab: truncated target type\n");
  296. return -1;
  297. }
  298. val = le32_to_cpu(buf32[items++]);
  299. key.target_class = (u16)val;
  300. if (key.target_class != val) {
  301. printk("security: avtab: truncated target class\n");
  302. return -1;
  303. }
  304. val = le32_to_cpu(buf32[items++]);
  305. enabled = (val & AVTAB_ENABLED_OLD) ? AVTAB_ENABLED : 0;
  306. if (!(val & (AVTAB_AV | AVTAB_TYPE))) {
  307. printk("security: avtab: null entry\n");
  308. return -1;
  309. }
  310. if ((val & AVTAB_AV) &&
  311. (val & AVTAB_TYPE)) {
  312. printk("security: avtab: entry has both access vectors and types\n");
  313. return -1;
  314. }
  315. for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
  316. if (val & spec_order[i]) {
  317. key.specified = spec_order[i] | enabled;
  318. datum.data = le32_to_cpu(buf32[items++]);
  319. rc = insertf(a, &key, &datum, p);
  320. if (rc) return rc;
  321. }
  322. }
  323. if (items != items2) {
  324. printk("security: avtab: entry only had %d items, expected %d\n", items2, items);
  325. return -1;
  326. }
  327. return 0;
  328. }
  329. rc = next_entry(buf16, fp, sizeof(u16)*4);
  330. if (rc < 0) {
  331. printk("security: avtab: truncated entry\n");
  332. return -1;
  333. }
  334. items = 0;
  335. key.source_type = le16_to_cpu(buf16[items++]);
  336. key.target_type = le16_to_cpu(buf16[items++]);
  337. key.target_class = le16_to_cpu(buf16[items++]);
  338. key.specified = le16_to_cpu(buf16[items++]);
  339. rc = next_entry(buf32, fp, sizeof(u32));
  340. if (rc < 0) {
  341. printk("security: avtab: truncated entry\n");
  342. return -1;
  343. }
  344. datum.data = le32_to_cpu(*buf32);
  345. return insertf(a, &key, &datum, p);
  346. }
  347. static int avtab_insertf(struct avtab *a, struct avtab_key *k,
  348. struct avtab_datum *d, void *p)
  349. {
  350. return avtab_insert(a, k, d);
  351. }
  352. int avtab_read(struct avtab *a, void *fp, u32 vers)
  353. {
  354. int rc;
  355. __le32 buf[1];
  356. u32 nel, i;
  357. rc = next_entry(buf, fp, sizeof(u32));
  358. if (rc < 0) {
  359. printk(KERN_ERR "security: avtab: truncated table\n");
  360. goto bad;
  361. }
  362. nel = le32_to_cpu(buf[0]);
  363. if (!nel) {
  364. printk(KERN_ERR "security: avtab: table is empty\n");
  365. rc = -EINVAL;
  366. goto bad;
  367. }
  368. for (i = 0; i < nel; i++) {
  369. rc = avtab_read_item(fp,vers, a, avtab_insertf, NULL);
  370. if (rc) {
  371. if (rc == -ENOMEM)
  372. printk(KERN_ERR "security: avtab: out of memory\n");
  373. else if (rc == -EEXIST)
  374. printk(KERN_ERR "security: avtab: duplicate entry\n");
  375. else
  376. rc = -EINVAL;
  377. goto bad;
  378. }
  379. }
  380. rc = 0;
  381. out:
  382. return rc;
  383. bad:
  384. avtab_destroy(a);
  385. goto out;
  386. }
  387. void avtab_cache_init(void)
  388. {
  389. avtab_node_cachep = kmem_cache_create("avtab_node",
  390. sizeof(struct avtab_node),
  391. 0, SLAB_PANIC, NULL, NULL);
  392. }
  393. void avtab_cache_destroy(void)
  394. {
  395. kmem_cache_destroy (avtab_node_cachep);
  396. }