avtab.c 12 KB

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