avtab.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. /* Based on MurmurHash3, written by Austin Appleby and placed in the
  25. * public domain.
  26. */
  27. static inline int avtab_hash(struct avtab_key *keyp, u32 mask)
  28. {
  29. static const u32 c1 = 0xcc9e2d51;
  30. static const u32 c2 = 0x1b873593;
  31. static const u32 r1 = 15;
  32. static const u32 r2 = 13;
  33. static const u32 m = 5;
  34. static const u32 n = 0xe6546b64;
  35. u32 hash = 0;
  36. #define mix(input) { \
  37. u32 v = input; \
  38. v *= c1; \
  39. v = (v << r1) | (v >> (32 - r1)); \
  40. v *= c2; \
  41. hash ^= v; \
  42. hash = (hash << r2) | (hash >> (32 - r2)); \
  43. hash = hash * m + n; \
  44. }
  45. mix(keyp->target_class);
  46. mix(keyp->target_type);
  47. mix(keyp->source_type);
  48. #undef mix
  49. hash ^= hash >> 16;
  50. hash *= 0x85ebca6b;
  51. hash ^= hash >> 13;
  52. hash *= 0xc2b2ae35;
  53. hash ^= hash >> 16;
  54. return hash & mask;
  55. }
  56. static struct avtab_node*
  57. avtab_insert_node(struct avtab *h, int hvalue,
  58. struct avtab_node *prev, struct avtab_node *cur,
  59. struct avtab_key *key, struct avtab_datum *datum)
  60. {
  61. struct avtab_node *newnode;
  62. newnode = kmem_cache_zalloc(avtab_node_cachep, GFP_KERNEL);
  63. if (newnode == NULL)
  64. return NULL;
  65. newnode->key = *key;
  66. newnode->datum = *datum;
  67. if (prev) {
  68. newnode->next = prev->next;
  69. prev->next = newnode;
  70. } else {
  71. newnode->next = flex_array_get_ptr(h->htable, hvalue);
  72. if (flex_array_put_ptr(h->htable, hvalue, newnode,
  73. GFP_KERNEL|__GFP_ZERO)) {
  74. kmem_cache_free(avtab_node_cachep, newnode);
  75. return NULL;
  76. }
  77. }
  78. h->nel++;
  79. return newnode;
  80. }
  81. static int avtab_insert(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum)
  82. {
  83. int hvalue;
  84. struct avtab_node *prev, *cur, *newnode;
  85. u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  86. if (!h || !h->htable)
  87. return -EINVAL;
  88. hvalue = avtab_hash(key, h->mask);
  89. for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue);
  90. cur;
  91. prev = cur, cur = cur->next) {
  92. if (key->source_type == cur->key.source_type &&
  93. key->target_type == cur->key.target_type &&
  94. key->target_class == cur->key.target_class &&
  95. (specified & cur->key.specified))
  96. return -EEXIST;
  97. if (key->source_type < cur->key.source_type)
  98. break;
  99. if (key->source_type == cur->key.source_type &&
  100. key->target_type < cur->key.target_type)
  101. break;
  102. if (key->source_type == cur->key.source_type &&
  103. key->target_type == cur->key.target_type &&
  104. key->target_class < cur->key.target_class)
  105. break;
  106. }
  107. newnode = avtab_insert_node(h, hvalue, prev, cur, key, datum);
  108. if (!newnode)
  109. return -ENOMEM;
  110. return 0;
  111. }
  112. /* Unlike avtab_insert(), this function allow multiple insertions of the same
  113. * key/specified mask into the table, as needed by the conditional avtab.
  114. * It also returns a pointer to the node inserted.
  115. */
  116. struct avtab_node *
  117. avtab_insert_nonunique(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum)
  118. {
  119. int hvalue;
  120. struct avtab_node *prev, *cur;
  121. u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  122. if (!h || !h->htable)
  123. return NULL;
  124. hvalue = avtab_hash(key, h->mask);
  125. for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue);
  126. cur;
  127. prev = cur, cur = cur->next) {
  128. if (key->source_type == cur->key.source_type &&
  129. key->target_type == cur->key.target_type &&
  130. key->target_class == cur->key.target_class &&
  131. (specified & cur->key.specified))
  132. break;
  133. if (key->source_type < cur->key.source_type)
  134. break;
  135. if (key->source_type == cur->key.source_type &&
  136. key->target_type < cur->key.target_type)
  137. break;
  138. if (key->source_type == cur->key.source_type &&
  139. key->target_type == cur->key.target_type &&
  140. key->target_class < cur->key.target_class)
  141. break;
  142. }
  143. return avtab_insert_node(h, hvalue, prev, cur, key, datum);
  144. }
  145. struct avtab_datum *avtab_search(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 = flex_array_get_ptr(h->htable, hvalue); cur;
  154. cur = cur->next) {
  155. if (key->source_type == cur->key.source_type &&
  156. key->target_type == cur->key.target_type &&
  157. key->target_class == cur->key.target_class &&
  158. (specified & cur->key.specified))
  159. return &cur->datum;
  160. if (key->source_type < cur->key.source_type)
  161. break;
  162. if (key->source_type == cur->key.source_type &&
  163. key->target_type < cur->key.target_type)
  164. break;
  165. if (key->source_type == cur->key.source_type &&
  166. key->target_type == cur->key.target_type &&
  167. key->target_class < cur->key.target_class)
  168. break;
  169. }
  170. return NULL;
  171. }
  172. /* This search function returns a node pointer, and can be used in
  173. * conjunction with avtab_search_next_node()
  174. */
  175. struct avtab_node*
  176. avtab_search_node(struct avtab *h, struct avtab_key *key)
  177. {
  178. int hvalue;
  179. struct avtab_node *cur;
  180. u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  181. if (!h || !h->htable)
  182. return NULL;
  183. hvalue = avtab_hash(key, h->mask);
  184. for (cur = flex_array_get_ptr(h->htable, hvalue); cur;
  185. cur = cur->next) {
  186. if (key->source_type == cur->key.source_type &&
  187. key->target_type == cur->key.target_type &&
  188. key->target_class == cur->key.target_class &&
  189. (specified & cur->key.specified))
  190. return cur;
  191. if (key->source_type < cur->key.source_type)
  192. break;
  193. if (key->source_type == cur->key.source_type &&
  194. key->target_type < cur->key.target_type)
  195. break;
  196. if (key->source_type == cur->key.source_type &&
  197. key->target_type == cur->key.target_type &&
  198. key->target_class < cur->key.target_class)
  199. break;
  200. }
  201. return NULL;
  202. }
  203. struct avtab_node*
  204. avtab_search_node_next(struct avtab_node *node, int specified)
  205. {
  206. struct avtab_node *cur;
  207. if (!node)
  208. return NULL;
  209. specified &= ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
  210. for (cur = node->next; cur; cur = cur->next) {
  211. if (node->key.source_type == cur->key.source_type &&
  212. node->key.target_type == cur->key.target_type &&
  213. node->key.target_class == cur->key.target_class &&
  214. (specified & cur->key.specified))
  215. return cur;
  216. if (node->key.source_type < cur->key.source_type)
  217. break;
  218. if (node->key.source_type == cur->key.source_type &&
  219. node->key.target_type < cur->key.target_type)
  220. break;
  221. if (node->key.source_type == cur->key.source_type &&
  222. node->key.target_type == cur->key.target_type &&
  223. node->key.target_class < cur->key.target_class)
  224. break;
  225. }
  226. return NULL;
  227. }
  228. void avtab_destroy(struct avtab *h)
  229. {
  230. int i;
  231. struct avtab_node *cur, *temp;
  232. if (!h || !h->htable)
  233. return;
  234. for (i = 0; i < h->nslot; i++) {
  235. cur = flex_array_get_ptr(h->htable, i);
  236. while (cur) {
  237. temp = cur;
  238. cur = cur->next;
  239. kmem_cache_free(avtab_node_cachep, temp);
  240. }
  241. }
  242. flex_array_free(h->htable);
  243. h->htable = NULL;
  244. h->nslot = 0;
  245. h->mask = 0;
  246. }
  247. int avtab_init(struct avtab *h)
  248. {
  249. h->htable = NULL;
  250. h->nel = 0;
  251. return 0;
  252. }
  253. int avtab_alloc(struct avtab *h, u32 nrules)
  254. {
  255. u32 mask = 0;
  256. u32 shift = 0;
  257. u32 work = nrules;
  258. u32 nslot = 0;
  259. if (nrules == 0)
  260. goto avtab_alloc_out;
  261. while (work) {
  262. work = work >> 1;
  263. shift++;
  264. }
  265. if (shift > 2)
  266. shift = shift - 2;
  267. nslot = 1 << shift;
  268. if (nslot > MAX_AVTAB_HASH_BUCKETS)
  269. nslot = MAX_AVTAB_HASH_BUCKETS;
  270. mask = nslot - 1;
  271. h->htable = flex_array_alloc(sizeof(struct avtab_node *), nslot,
  272. GFP_KERNEL | __GFP_ZERO);
  273. if (!h->htable)
  274. return -ENOMEM;
  275. avtab_alloc_out:
  276. h->nel = 0;
  277. h->nslot = nslot;
  278. h->mask = mask;
  279. printk(KERN_DEBUG "SELinux: %d avtab hash slots, %d rules.\n",
  280. h->nslot, nrules);
  281. return 0;
  282. }
  283. void avtab_hash_eval(struct avtab *h, char *tag)
  284. {
  285. int i, chain_len, slots_used, max_chain_len;
  286. unsigned long long chain2_len_sum;
  287. struct avtab_node *cur;
  288. slots_used = 0;
  289. max_chain_len = 0;
  290. chain2_len_sum = 0;
  291. for (i = 0; i < h->nslot; i++) {
  292. cur = flex_array_get_ptr(h->htable, i);
  293. if (cur) {
  294. slots_used++;
  295. chain_len = 0;
  296. while (cur) {
  297. chain_len++;
  298. cur = cur->next;
  299. }
  300. if (chain_len > max_chain_len)
  301. max_chain_len = chain_len;
  302. chain2_len_sum += chain_len * chain_len;
  303. }
  304. }
  305. printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
  306. "longest chain length %d sum of chain length^2 %llu\n",
  307. tag, h->nel, slots_used, h->nslot, max_chain_len,
  308. chain2_len_sum);
  309. }
  310. static uint16_t spec_order[] = {
  311. AVTAB_ALLOWED,
  312. AVTAB_AUDITDENY,
  313. AVTAB_AUDITALLOW,
  314. AVTAB_TRANSITION,
  315. AVTAB_CHANGE,
  316. AVTAB_MEMBER
  317. };
  318. int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
  319. int (*insertf)(struct avtab *a, struct avtab_key *k,
  320. struct avtab_datum *d, void *p),
  321. void *p)
  322. {
  323. __le16 buf16[4];
  324. u16 enabled;
  325. __le32 buf32[7];
  326. u32 items, items2, val, vers = pol->policyvers;
  327. struct avtab_key key;
  328. struct avtab_datum datum;
  329. int i, rc;
  330. unsigned set;
  331. memset(&key, 0, sizeof(struct avtab_key));
  332. memset(&datum, 0, sizeof(struct avtab_datum));
  333. if (vers < POLICYDB_VERSION_AVTAB) {
  334. rc = next_entry(buf32, fp, sizeof(u32));
  335. if (rc) {
  336. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  337. return rc;
  338. }
  339. items2 = le32_to_cpu(buf32[0]);
  340. if (items2 > ARRAY_SIZE(buf32)) {
  341. printk(KERN_ERR "SELinux: avtab: entry overflow\n");
  342. return -EINVAL;
  343. }
  344. rc = next_entry(buf32, fp, sizeof(u32)*items2);
  345. if (rc) {
  346. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  347. return rc;
  348. }
  349. items = 0;
  350. val = le32_to_cpu(buf32[items++]);
  351. key.source_type = (u16)val;
  352. if (key.source_type != val) {
  353. printk(KERN_ERR "SELinux: avtab: truncated source type\n");
  354. return -EINVAL;
  355. }
  356. val = le32_to_cpu(buf32[items++]);
  357. key.target_type = (u16)val;
  358. if (key.target_type != val) {
  359. printk(KERN_ERR "SELinux: avtab: truncated target type\n");
  360. return -EINVAL;
  361. }
  362. val = le32_to_cpu(buf32[items++]);
  363. key.target_class = (u16)val;
  364. if (key.target_class != val) {
  365. printk(KERN_ERR "SELinux: avtab: truncated target class\n");
  366. return -EINVAL;
  367. }
  368. val = le32_to_cpu(buf32[items++]);
  369. enabled = (val & AVTAB_ENABLED_OLD) ? AVTAB_ENABLED : 0;
  370. if (!(val & (AVTAB_AV | AVTAB_TYPE))) {
  371. printk(KERN_ERR "SELinux: avtab: null entry\n");
  372. return -EINVAL;
  373. }
  374. if ((val & AVTAB_AV) &&
  375. (val & AVTAB_TYPE)) {
  376. printk(KERN_ERR "SELinux: avtab: entry has both access vectors and types\n");
  377. return -EINVAL;
  378. }
  379. for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
  380. if (val & spec_order[i]) {
  381. key.specified = spec_order[i] | enabled;
  382. datum.data = le32_to_cpu(buf32[items++]);
  383. rc = insertf(a, &key, &datum, p);
  384. if (rc)
  385. return rc;
  386. }
  387. }
  388. if (items != items2) {
  389. printk(KERN_ERR "SELinux: avtab: entry only had %d items, expected %d\n", items2, items);
  390. return -EINVAL;
  391. }
  392. return 0;
  393. }
  394. rc = next_entry(buf16, fp, sizeof(u16)*4);
  395. if (rc) {
  396. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  397. return rc;
  398. }
  399. items = 0;
  400. key.source_type = le16_to_cpu(buf16[items++]);
  401. key.target_type = le16_to_cpu(buf16[items++]);
  402. key.target_class = le16_to_cpu(buf16[items++]);
  403. key.specified = le16_to_cpu(buf16[items++]);
  404. if (!policydb_type_isvalid(pol, key.source_type) ||
  405. !policydb_type_isvalid(pol, key.target_type) ||
  406. !policydb_class_isvalid(pol, key.target_class)) {
  407. printk(KERN_ERR "SELinux: avtab: invalid type or class\n");
  408. return -EINVAL;
  409. }
  410. set = 0;
  411. for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
  412. if (key.specified & spec_order[i])
  413. set++;
  414. }
  415. if (!set || set > 1) {
  416. printk(KERN_ERR "SELinux: avtab: more than one specifier\n");
  417. return -EINVAL;
  418. }
  419. rc = next_entry(buf32, fp, sizeof(u32));
  420. if (rc) {
  421. printk(KERN_ERR "SELinux: avtab: truncated entry\n");
  422. return rc;
  423. }
  424. datum.data = le32_to_cpu(*buf32);
  425. if ((key.specified & AVTAB_TYPE) &&
  426. !policydb_type_isvalid(pol, datum.data)) {
  427. printk(KERN_ERR "SELinux: avtab: invalid type\n");
  428. return -EINVAL;
  429. }
  430. return insertf(a, &key, &datum, p);
  431. }
  432. static int avtab_insertf(struct avtab *a, struct avtab_key *k,
  433. struct avtab_datum *d, void *p)
  434. {
  435. return avtab_insert(a, k, d);
  436. }
  437. int avtab_read(struct avtab *a, void *fp, struct policydb *pol)
  438. {
  439. int rc;
  440. __le32 buf[1];
  441. u32 nel, i;
  442. rc = next_entry(buf, fp, sizeof(u32));
  443. if (rc < 0) {
  444. printk(KERN_ERR "SELinux: avtab: truncated table\n");
  445. goto bad;
  446. }
  447. nel = le32_to_cpu(buf[0]);
  448. if (!nel) {
  449. printk(KERN_ERR "SELinux: avtab: table is empty\n");
  450. rc = -EINVAL;
  451. goto bad;
  452. }
  453. rc = avtab_alloc(a, nel);
  454. if (rc)
  455. goto bad;
  456. for (i = 0; i < nel; i++) {
  457. rc = avtab_read_item(a, fp, pol, avtab_insertf, NULL);
  458. if (rc) {
  459. if (rc == -ENOMEM)
  460. printk(KERN_ERR "SELinux: avtab: out of memory\n");
  461. else if (rc == -EEXIST)
  462. printk(KERN_ERR "SELinux: avtab: duplicate entry\n");
  463. goto bad;
  464. }
  465. }
  466. rc = 0;
  467. out:
  468. return rc;
  469. bad:
  470. avtab_destroy(a);
  471. goto out;
  472. }
  473. int avtab_write_item(struct policydb *p, struct avtab_node *cur, void *fp)
  474. {
  475. __le16 buf16[4];
  476. __le32 buf32[1];
  477. int rc;
  478. buf16[0] = cpu_to_le16(cur->key.source_type);
  479. buf16[1] = cpu_to_le16(cur->key.target_type);
  480. buf16[2] = cpu_to_le16(cur->key.target_class);
  481. buf16[3] = cpu_to_le16(cur->key.specified);
  482. rc = put_entry(buf16, sizeof(u16), 4, fp);
  483. if (rc)
  484. return rc;
  485. buf32[0] = cpu_to_le32(cur->datum.data);
  486. rc = put_entry(buf32, sizeof(u32), 1, fp);
  487. if (rc)
  488. return rc;
  489. return 0;
  490. }
  491. int avtab_write(struct policydb *p, struct avtab *a, void *fp)
  492. {
  493. unsigned int i;
  494. int rc = 0;
  495. struct avtab_node *cur;
  496. __le32 buf[1];
  497. buf[0] = cpu_to_le32(a->nel);
  498. rc = put_entry(buf, sizeof(u32), 1, fp);
  499. if (rc)
  500. return rc;
  501. for (i = 0; i < a->nslot; i++) {
  502. for (cur = flex_array_get_ptr(a->htable, i); cur;
  503. cur = cur->next) {
  504. rc = avtab_write_item(p, cur, fp);
  505. if (rc)
  506. return rc;
  507. }
  508. }
  509. return rc;
  510. }
  511. void avtab_cache_init(void)
  512. {
  513. avtab_node_cachep = kmem_cache_create("avtab_node",
  514. sizeof(struct avtab_node),
  515. 0, SLAB_PANIC, NULL);
  516. }
  517. void avtab_cache_destroy(void)
  518. {
  519. kmem_cache_destroy(avtab_node_cachep);
  520. }