avtab.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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(struct avtab *a, void *fp, struct policydb *pol,
  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, vers = pol->policyvers;
  295. struct avtab_key key;
  296. struct avtab_datum datum;
  297. int i, rc;
  298. unsigned set;
  299. memset(&key, 0, sizeof(struct avtab_key));
  300. memset(&datum, 0, sizeof(struct avtab_datum));
  301. if (vers < POLICYDB_VERSION_AVTAB) {
  302. rc = next_entry(buf32, fp, sizeof(u32));
  303. if (rc < 0) {
  304. printk(KERN_ERR "security: avtab: truncated entry\n");
  305. return -1;
  306. }
  307. items2 = le32_to_cpu(buf32[0]);
  308. if (items2 > ARRAY_SIZE(buf32)) {
  309. printk(KERN_ERR "security: avtab: entry overflow\n");
  310. return -1;
  311. }
  312. rc = next_entry(buf32, fp, sizeof(u32)*items2);
  313. if (rc < 0) {
  314. printk(KERN_ERR "security: avtab: truncated entry\n");
  315. return -1;
  316. }
  317. items = 0;
  318. val = le32_to_cpu(buf32[items++]);
  319. key.source_type = (u16)val;
  320. if (key.source_type != val) {
  321. printk("security: avtab: truncated source type\n");
  322. return -1;
  323. }
  324. val = le32_to_cpu(buf32[items++]);
  325. key.target_type = (u16)val;
  326. if (key.target_type != val) {
  327. printk("security: avtab: truncated target type\n");
  328. return -1;
  329. }
  330. val = le32_to_cpu(buf32[items++]);
  331. key.target_class = (u16)val;
  332. if (key.target_class != val) {
  333. printk("security: avtab: truncated target class\n");
  334. return -1;
  335. }
  336. val = le32_to_cpu(buf32[items++]);
  337. enabled = (val & AVTAB_ENABLED_OLD) ? AVTAB_ENABLED : 0;
  338. if (!(val & (AVTAB_AV | AVTAB_TYPE))) {
  339. printk("security: avtab: null entry\n");
  340. return -1;
  341. }
  342. if ((val & AVTAB_AV) &&
  343. (val & AVTAB_TYPE)) {
  344. printk("security: avtab: entry has both access vectors and types\n");
  345. return -1;
  346. }
  347. for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
  348. if (val & spec_order[i]) {
  349. key.specified = spec_order[i] | enabled;
  350. datum.data = le32_to_cpu(buf32[items++]);
  351. rc = insertf(a, &key, &datum, p);
  352. if (rc) return rc;
  353. }
  354. }
  355. if (items != items2) {
  356. printk("security: avtab: entry only had %d items, expected %d\n", items2, items);
  357. return -1;
  358. }
  359. return 0;
  360. }
  361. rc = next_entry(buf16, fp, sizeof(u16)*4);
  362. if (rc < 0) {
  363. printk("security: avtab: truncated entry\n");
  364. return -1;
  365. }
  366. items = 0;
  367. key.source_type = le16_to_cpu(buf16[items++]);
  368. key.target_type = le16_to_cpu(buf16[items++]);
  369. key.target_class = le16_to_cpu(buf16[items++]);
  370. key.specified = le16_to_cpu(buf16[items++]);
  371. if (!policydb_type_isvalid(pol, key.source_type) ||
  372. !policydb_type_isvalid(pol, key.target_type) ||
  373. !policydb_class_isvalid(pol, key.target_class)) {
  374. printk(KERN_WARNING "security: avtab: invalid type or class\n");
  375. return -1;
  376. }
  377. set = 0;
  378. for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
  379. if (key.specified & spec_order[i])
  380. set++;
  381. }
  382. if (!set || set > 1) {
  383. printk(KERN_WARNING
  384. "security: avtab: more than one specifier\n");
  385. return -1;
  386. }
  387. rc = next_entry(buf32, fp, sizeof(u32));
  388. if (rc < 0) {
  389. printk("security: avtab: truncated entry\n");
  390. return -1;
  391. }
  392. datum.data = le32_to_cpu(*buf32);
  393. if ((key.specified & AVTAB_TYPE) &&
  394. !policydb_type_isvalid(pol, datum.data)) {
  395. printk(KERN_WARNING "security: avtab: invalid type\n");
  396. return -1;
  397. }
  398. return insertf(a, &key, &datum, p);
  399. }
  400. static int avtab_insertf(struct avtab *a, struct avtab_key *k,
  401. struct avtab_datum *d, void *p)
  402. {
  403. return avtab_insert(a, k, d);
  404. }
  405. int avtab_read(struct avtab *a, void *fp, struct policydb *pol)
  406. {
  407. int rc;
  408. __le32 buf[1];
  409. u32 nel, i;
  410. rc = next_entry(buf, fp, sizeof(u32));
  411. if (rc < 0) {
  412. printk(KERN_ERR "security: avtab: truncated table\n");
  413. goto bad;
  414. }
  415. nel = le32_to_cpu(buf[0]);
  416. if (!nel) {
  417. printk(KERN_ERR "security: avtab: table is empty\n");
  418. rc = -EINVAL;
  419. goto bad;
  420. }
  421. rc = avtab_alloc(a, nel);
  422. if (rc)
  423. goto bad;
  424. for (i = 0; i < nel; i++) {
  425. rc = avtab_read_item(a, fp, pol, avtab_insertf, NULL);
  426. if (rc) {
  427. if (rc == -ENOMEM)
  428. printk(KERN_ERR "security: avtab: out of memory\n");
  429. else if (rc == -EEXIST)
  430. printk(KERN_ERR "security: avtab: duplicate entry\n");
  431. else
  432. rc = -EINVAL;
  433. goto bad;
  434. }
  435. }
  436. rc = 0;
  437. out:
  438. return rc;
  439. bad:
  440. avtab_destroy(a);
  441. goto out;
  442. }
  443. void avtab_cache_init(void)
  444. {
  445. avtab_node_cachep = kmem_cache_create("avtab_node",
  446. sizeof(struct avtab_node),
  447. 0, SLAB_PANIC, NULL);
  448. }
  449. void avtab_cache_destroy(void)
  450. {
  451. kmem_cache_destroy (avtab_node_cachep);
  452. }