avtab.c 15 KB

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