avtab.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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;
  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. return avtab_insert_node(h, hvalue, prev, cur, key, datum);
  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 || !h->htable)
  120. return NULL;
  121. hvalue = avtab_hash(key, h->mask);
  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 || !h->htable)
  150. return NULL;
  151. hvalue = avtab_hash(key, h->mask);
  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 < h->nslot; i++) {
  202. cur = h->htable[i];
  203. while (cur) {
  204. temp = cur;
  205. cur = cur->next;
  206. kmem_cache_free(avtab_node_cachep, temp);
  207. }
  208. h->htable[i] = NULL;
  209. }
  210. kfree(h->htable);
  211. h->htable = NULL;
  212. h->nslot = 0;
  213. h->mask = 0;
  214. }
  215. int avtab_init(struct avtab *h)
  216. {
  217. h->htable = NULL;
  218. h->nel = 0;
  219. return 0;
  220. }
  221. int avtab_alloc(struct avtab *h, u32 nrules)
  222. {
  223. u16 mask = 0;
  224. u32 shift = 0;
  225. u32 work = nrules;
  226. u32 nslot = 0;
  227. if (nrules == 0)
  228. goto avtab_alloc_out;
  229. while (work) {
  230. work = work >> 1;
  231. shift++;
  232. }
  233. if (shift > 2)
  234. shift = shift - 2;
  235. nslot = 1 << shift;
  236. if (nslot > MAX_AVTAB_HASH_BUCKETS)
  237. nslot = MAX_AVTAB_HASH_BUCKETS;
  238. mask = nslot - 1;
  239. h->htable = kcalloc(nslot, sizeof(*(h->htable)), GFP_KERNEL);
  240. if (!h->htable)
  241. return -ENOMEM;
  242. avtab_alloc_out:
  243. h->nel = 0;
  244. h->nslot = nslot;
  245. h->mask = mask;
  246. printk(KERN_DEBUG "SELinux: %d avtab hash slots, %d rules.\n",
  247. h->nslot, nrules);
  248. return 0;
  249. }
  250. void avtab_hash_eval(struct avtab *h, char *tag)
  251. {
  252. int i, chain_len, slots_used, max_chain_len;
  253. unsigned long long chain2_len_sum;
  254. struct avtab_node *cur;
  255. slots_used = 0;
  256. max_chain_len = 0;
  257. chain2_len_sum = 0;
  258. for (i = 0; i < h->nslot; i++) {
  259. cur = h->htable[i];
  260. if (cur) {
  261. slots_used++;
  262. chain_len = 0;
  263. while (cur) {
  264. chain_len++;
  265. cur = cur->next;
  266. }
  267. if (chain_len > max_chain_len)
  268. max_chain_len = chain_len;
  269. chain2_len_sum += chain_len * chain_len;
  270. }
  271. }
  272. printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
  273. "longest chain length %d sum of chain length^2 %llu\n",
  274. tag, h->nel, slots_used, h->nslot, max_chain_len,
  275. chain2_len_sum);
  276. }
  277. static uint16_t spec_order[] = {
  278. AVTAB_ALLOWED,
  279. AVTAB_AUDITDENY,
  280. AVTAB_AUDITALLOW,
  281. AVTAB_TRANSITION,
  282. AVTAB_CHANGE,
  283. AVTAB_MEMBER
  284. };
  285. int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
  286. int (*insertf)(struct avtab *a, struct avtab_key *k,
  287. struct avtab_datum *d, void *p),
  288. void *p)
  289. {
  290. __le16 buf16[4];
  291. u16 enabled;
  292. __le32 buf32[7];
  293. u32 items, items2, val, vers = pol->policyvers;
  294. struct avtab_key key;
  295. struct avtab_datum datum;
  296. int i, rc;
  297. unsigned set;
  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) {
  303. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  304. return rc;
  305. }
  306. items2 = le32_to_cpu(buf32[0]);
  307. if (items2 > ARRAY_SIZE(buf32)) {
  308. printk(KERN_ERR "SELinux: avtab: entry overflow\n");
  309. return -EINVAL;
  310. }
  311. rc = next_entry(buf32, fp, sizeof(u32)*items2);
  312. if (rc) {
  313. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  314. return rc;
  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(KERN_ERR "SELinux: avtab: truncated source type\n");
  321. return -EINVAL;
  322. }
  323. val = le32_to_cpu(buf32[items++]);
  324. key.target_type = (u16)val;
  325. if (key.target_type != val) {
  326. printk(KERN_ERR "SELinux: avtab: truncated target type\n");
  327. return -EINVAL;
  328. }
  329. val = le32_to_cpu(buf32[items++]);
  330. key.target_class = (u16)val;
  331. if (key.target_class != val) {
  332. printk(KERN_ERR "SELinux: avtab: truncated target class\n");
  333. return -EINVAL;
  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(KERN_ERR "SELinux: avtab: null entry\n");
  339. return -EINVAL;
  340. }
  341. if ((val & AVTAB_AV) &&
  342. (val & AVTAB_TYPE)) {
  343. printk(KERN_ERR "SELinux: avtab: entry has both access vectors and types\n");
  344. return -EINVAL;
  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)
  352. return rc;
  353. }
  354. }
  355. if (items != items2) {
  356. printk(KERN_ERR "SELinux: avtab: entry only had %d items, expected %d\n", items2, items);
  357. return -EINVAL;
  358. }
  359. return 0;
  360. }
  361. rc = next_entry(buf16, fp, sizeof(u16)*4);
  362. if (rc) {
  363. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  364. return rc;
  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_ERR "SELinux: avtab: invalid type or class\n");
  375. return -EINVAL;
  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_ERR "SELinux: avtab: more than one specifier\n");
  384. return -EINVAL;
  385. }
  386. rc = next_entry(buf32, fp, sizeof(u32));
  387. if (rc) {
  388. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  389. return rc;
  390. }
  391. datum.data = le32_to_cpu(*buf32);
  392. if ((key.specified & AVTAB_TYPE) &&
  393. !policydb_type_isvalid(pol, datum.data)) {
  394. printk(KERN_ERR "SELinux: avtab: invalid type\n");
  395. return -EINVAL;
  396. }
  397. return insertf(a, &key, &datum, p);
  398. }
  399. static int avtab_insertf(struct avtab *a, struct avtab_key *k,
  400. struct avtab_datum *d, void *p)
  401. {
  402. return avtab_insert(a, k, d);
  403. }
  404. int avtab_read(struct avtab *a, void *fp, struct policydb *pol)
  405. {
  406. int rc;
  407. __le32 buf[1];
  408. u32 nel, i;
  409. rc = next_entry(buf, fp, sizeof(u32));
  410. if (rc < 0) {
  411. printk(KERN_ERR "SELinux: avtab: truncated table\n");
  412. goto bad;
  413. }
  414. nel = le32_to_cpu(buf[0]);
  415. if (!nel) {
  416. printk(KERN_ERR "SELinux: avtab: table is empty\n");
  417. rc = -EINVAL;
  418. goto bad;
  419. }
  420. rc = avtab_alloc(a, nel);
  421. if (rc)
  422. goto bad;
  423. for (i = 0; i < nel; i++) {
  424. rc = avtab_read_item(a, fp, pol, avtab_insertf, NULL);
  425. if (rc) {
  426. if (rc == -ENOMEM)
  427. printk(KERN_ERR "SELinux: avtab: out of memory\n");
  428. else if (rc == -EEXIST)
  429. printk(KERN_ERR "SELinux: avtab: duplicate entry\n");
  430. goto bad;
  431. }
  432. }
  433. rc = 0;
  434. out:
  435. return rc;
  436. bad:
  437. avtab_destroy(a);
  438. goto out;
  439. }
  440. int avtab_write_item(struct policydb *p, struct avtab_node *cur, void *fp)
  441. {
  442. __le16 buf16[4];
  443. __le32 buf32[1];
  444. int rc;
  445. buf16[0] = cpu_to_le16(cur->key.source_type);
  446. buf16[1] = cpu_to_le16(cur->key.target_type);
  447. buf16[2] = cpu_to_le16(cur->key.target_class);
  448. buf16[3] = cpu_to_le16(cur->key.specified);
  449. rc = put_entry(buf16, sizeof(u16), 4, fp);
  450. if (rc)
  451. return rc;
  452. buf32[0] = cpu_to_le32(cur->datum.data);
  453. rc = put_entry(buf32, sizeof(u32), 1, fp);
  454. if (rc)
  455. return rc;
  456. return 0;
  457. }
  458. int avtab_write(struct policydb *p, struct avtab *a, void *fp)
  459. {
  460. unsigned int i;
  461. int rc = 0;
  462. struct avtab_node *cur;
  463. __le32 buf[1];
  464. buf[0] = cpu_to_le32(a->nel);
  465. rc = put_entry(buf, sizeof(u32), 1, fp);
  466. if (rc)
  467. return rc;
  468. for (i = 0; i < a->nslot; i++) {
  469. for (cur = a->htable[i]; cur; cur = cur->next) {
  470. rc = avtab_write_item(p, cur, fp);
  471. if (rc)
  472. return rc;
  473. }
  474. }
  475. return rc;
  476. }
  477. void avtab_cache_init(void)
  478. {
  479. avtab_node_cachep = kmem_cache_create("avtab_node",
  480. sizeof(struct avtab_node),
  481. 0, SLAB_PANIC, NULL);
  482. }
  483. void avtab_cache_destroy(void)
  484. {
  485. kmem_cache_destroy(avtab_node_cachep);
  486. }