avtab.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Implementation of the access vector table type.
  4. *
  5. * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
  6. */
  7. /* Updated: Frank Mayer <mayerf@tresys.com> and
  8. * Karl MacMillan <kmacmillan@tresys.com>
  9. * Added conditional policy language extensions
  10. * Copyright (C) 2003 Tresys Technology, LLC
  11. *
  12. * Updated: Yuichi Nakamura <ynakam@hitachisoft.jp>
  13. * Tuned number of hash slots for avtab to reduce memory usage
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/errno.h>
  19. #include "avtab.h"
  20. #include "policydb.h"
  21. static struct kmem_cache *avtab_node_cachep __ro_after_init;
  22. static struct kmem_cache *avtab_xperms_cachep __ro_after_init;
  23. /* Based on MurmurHash3, written by Austin Appleby and placed in the
  24. * public domain.
  25. */
  26. static inline u32 avtab_hash(const struct avtab_key *keyp, u32 mask)
  27. {
  28. static const u32 c1 = 0xcc9e2d51;
  29. static const u32 c2 = 0x1b873593;
  30. static const u32 r1 = 15;
  31. static const u32 r2 = 13;
  32. static const u32 m = 5;
  33. static const u32 n = 0xe6546b64;
  34. u32 hash = 0;
  35. #define mix(input) \
  36. do { \
  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. } while (0)
  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 *avtab_insert_node(struct avtab *h,
  57. struct avtab_node **dst,
  58. const struct avtab_key *key,
  59. const struct avtab_datum *datum)
  60. {
  61. struct avtab_node *newnode;
  62. struct avtab_extended_perms *xperms;
  63. newnode = kmem_cache_zalloc(avtab_node_cachep, GFP_KERNEL);
  64. if (newnode == NULL)
  65. return NULL;
  66. newnode->key = *key;
  67. if (key->specified & AVTAB_XPERMS) {
  68. xperms = kmem_cache_zalloc(avtab_xperms_cachep, GFP_KERNEL);
  69. if (xperms == NULL) {
  70. kmem_cache_free(avtab_node_cachep, newnode);
  71. return NULL;
  72. }
  73. *xperms = *(datum->u.xperms);
  74. newnode->datum.u.xperms = xperms;
  75. } else {
  76. newnode->datum.u.data = datum->u.data;
  77. }
  78. newnode->next = *dst;
  79. *dst = newnode;
  80. h->nel++;
  81. return newnode;
  82. }
  83. static int avtab_node_cmp(const struct avtab_key *key1,
  84. const struct avtab_key *key2)
  85. {
  86. u16 specified = key1->specified & ~(AVTAB_ENABLED | AVTAB_ENABLED_OLD);
  87. if (key1->source_type == key2->source_type &&
  88. key1->target_type == key2->target_type &&
  89. key1->target_class == key2->target_class &&
  90. (specified & key2->specified))
  91. return 0;
  92. if (key1->source_type < key2->source_type)
  93. return -1;
  94. if (key1->source_type == key2->source_type &&
  95. key1->target_type < key2->target_type)
  96. return -1;
  97. if (key1->source_type == key2->source_type &&
  98. key1->target_type == key2->target_type &&
  99. key1->target_class < key2->target_class)
  100. return -1;
  101. return 1;
  102. }
  103. static int avtab_insert(struct avtab *h, const struct avtab_key *key,
  104. const struct avtab_datum *datum)
  105. {
  106. u32 hvalue;
  107. struct avtab_node *prev, *cur, *newnode;
  108. int cmp;
  109. if (!h || !h->nslot || h->nel == U32_MAX)
  110. return -EINVAL;
  111. hvalue = avtab_hash(key, h->mask);
  112. for (prev = NULL, cur = h->htable[hvalue]; cur;
  113. prev = cur, cur = cur->next) {
  114. cmp = avtab_node_cmp(key, &cur->key);
  115. /* extended perms may not be unique */
  116. if (cmp == 0 && !(key->specified & AVTAB_XPERMS))
  117. return -EEXIST;
  118. if (cmp <= 0)
  119. break;
  120. }
  121. newnode = avtab_insert_node(h, prev ? &prev->next : &h->htable[hvalue],
  122. key, datum);
  123. if (!newnode)
  124. return -ENOMEM;
  125. return 0;
  126. }
  127. /* Unlike avtab_insert(), this function allow multiple insertions of the same
  128. * key/specified mask into the table, as needed by the conditional avtab.
  129. * It also returns a pointer to the node inserted.
  130. */
  131. struct avtab_node *avtab_insert_nonunique(struct avtab *h,
  132. const struct avtab_key *key,
  133. const struct avtab_datum *datum)
  134. {
  135. u32 hvalue;
  136. struct avtab_node *prev, *cur;
  137. int cmp;
  138. if (!h || !h->nslot || h->nel == U32_MAX)
  139. return NULL;
  140. hvalue = avtab_hash(key, h->mask);
  141. for (prev = NULL, cur = h->htable[hvalue]; cur;
  142. prev = cur, cur = cur->next) {
  143. cmp = avtab_node_cmp(key, &cur->key);
  144. if (cmp <= 0)
  145. break;
  146. }
  147. return avtab_insert_node(h, prev ? &prev->next : &h->htable[hvalue],
  148. key, datum);
  149. }
  150. /* This search function returns a node pointer, and can be used in
  151. * conjunction with avtab_search_next_node()
  152. */
  153. struct avtab_node *avtab_search_node(struct avtab *h,
  154. const struct avtab_key *key)
  155. {
  156. u32 hvalue;
  157. struct avtab_node *cur;
  158. int cmp;
  159. if (!h || !h->nslot)
  160. return NULL;
  161. hvalue = avtab_hash(key, h->mask);
  162. for (cur = h->htable[hvalue]; cur; cur = cur->next) {
  163. cmp = avtab_node_cmp(key, &cur->key);
  164. if (cmp == 0)
  165. return cur;
  166. if (cmp < 0)
  167. break;
  168. }
  169. return NULL;
  170. }
  171. struct avtab_node *avtab_search_node_next(struct avtab_node *node,
  172. u16 specified)
  173. {
  174. struct avtab_key tmp_key;
  175. struct avtab_node *cur;
  176. int cmp;
  177. if (!node)
  178. return NULL;
  179. tmp_key = node->key;
  180. tmp_key.specified = specified;
  181. for (cur = node->next; cur; cur = cur->next) {
  182. cmp = avtab_node_cmp(&tmp_key, &cur->key);
  183. if (cmp == 0)
  184. return cur;
  185. if (cmp < 0)
  186. break;
  187. }
  188. return NULL;
  189. }
  190. void avtab_destroy(struct avtab *h)
  191. {
  192. u32 i;
  193. struct avtab_node *cur, *temp;
  194. if (!h)
  195. return;
  196. for (i = 0; i < h->nslot; i++) {
  197. cur = h->htable[i];
  198. while (cur) {
  199. temp = cur;
  200. cur = cur->next;
  201. if (temp->key.specified & AVTAB_XPERMS)
  202. kmem_cache_free(avtab_xperms_cachep,
  203. temp->datum.u.xperms);
  204. kmem_cache_free(avtab_node_cachep, temp);
  205. }
  206. }
  207. kvfree(h->htable);
  208. h->htable = NULL;
  209. h->nel = 0;
  210. h->nslot = 0;
  211. h->mask = 0;
  212. }
  213. void avtab_init(struct avtab *h)
  214. {
  215. h->htable = NULL;
  216. h->nel = 0;
  217. h->nslot = 0;
  218. h->mask = 0;
  219. }
  220. static int avtab_alloc_common(struct avtab *h, u32 nslot)
  221. {
  222. if (!nslot)
  223. return 0;
  224. h->htable = kvcalloc(nslot, sizeof(void *), GFP_KERNEL);
  225. if (!h->htable)
  226. return -ENOMEM;
  227. h->nslot = nslot;
  228. h->mask = nslot - 1;
  229. return 0;
  230. }
  231. int avtab_alloc(struct avtab *h, u32 nrules)
  232. {
  233. int rc;
  234. u32 nslot = 0;
  235. if (nrules != 0) {
  236. nslot = nrules > 3 ? rounddown_pow_of_two(nrules / 2) : 2;
  237. if (nslot > MAX_AVTAB_HASH_BUCKETS)
  238. nslot = MAX_AVTAB_HASH_BUCKETS;
  239. rc = avtab_alloc_common(h, nslot);
  240. if (rc)
  241. return rc;
  242. }
  243. pr_debug("SELinux: %d avtab hash slots, %d rules.\n", nslot, nrules);
  244. return 0;
  245. }
  246. int avtab_alloc_dup(struct avtab *new, const struct avtab *orig)
  247. {
  248. return avtab_alloc_common(new, orig->nslot);
  249. }
  250. #ifdef CONFIG_SECURITY_SELINUX_DEBUG
  251. void avtab_hash_eval(struct avtab *h, const char *tag)
  252. {
  253. u32 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 +=
  271. (unsigned long long)chain_len * chain_len;
  272. }
  273. }
  274. pr_debug("SELinux: %s: %d entries and %d/%d buckets used, "
  275. "longest chain length %d, sum of chain length^2 %llu\n",
  276. tag, h->nel, slots_used, h->nslot, max_chain_len,
  277. chain2_len_sum);
  278. }
  279. #endif /* CONFIG_SECURITY_SELINUX_DEBUG */
  280. /* clang-format off */
  281. static const uint16_t spec_order[] = {
  282. AVTAB_ALLOWED,
  283. AVTAB_AUDITDENY,
  284. AVTAB_AUDITALLOW,
  285. AVTAB_TRANSITION,
  286. AVTAB_CHANGE,
  287. AVTAB_MEMBER,
  288. AVTAB_XPERMS_ALLOWED,
  289. AVTAB_XPERMS_AUDITALLOW,
  290. AVTAB_XPERMS_DONTAUDIT
  291. };
  292. /* clang-format on */
  293. int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
  294. int (*insertf)(struct avtab *a, const struct avtab_key *k,
  295. const struct avtab_datum *d, void *p),
  296. void *p)
  297. {
  298. __le16 buf16[4];
  299. u16 enabled;
  300. u32 items, items2, val, i;
  301. struct avtab_key key;
  302. struct avtab_datum datum;
  303. struct avtab_extended_perms xperms;
  304. __le32 buf32[ARRAY_SIZE(xperms.perms.p)];
  305. int rc;
  306. unsigned int set, vers = pol->policyvers;
  307. memset(&key, 0, sizeof(struct avtab_key));
  308. memset(&datum, 0, sizeof(struct avtab_datum));
  309. if (vers < POLICYDB_VERSION_AVTAB) {
  310. rc = next_entry(buf32, fp, sizeof(u32));
  311. if (rc) {
  312. pr_err("SELinux: avtab: truncated entry\n");
  313. return rc;
  314. }
  315. items2 = le32_to_cpu(buf32[0]);
  316. if (items2 > ARRAY_SIZE(buf32)) {
  317. pr_err("SELinux: avtab: entry overflow\n");
  318. return -EINVAL;
  319. }
  320. rc = next_entry(buf32, fp, sizeof(u32) * items2);
  321. if (rc) {
  322. pr_err("SELinux: avtab: truncated entry\n");
  323. return rc;
  324. }
  325. items = 0;
  326. val = le32_to_cpu(buf32[items++]);
  327. key.source_type = (u16)val;
  328. if (key.source_type != val) {
  329. pr_err("SELinux: avtab: truncated source type\n");
  330. return -EINVAL;
  331. }
  332. val = le32_to_cpu(buf32[items++]);
  333. key.target_type = (u16)val;
  334. if (key.target_type != val) {
  335. pr_err("SELinux: avtab: truncated target type\n");
  336. return -EINVAL;
  337. }
  338. val = le32_to_cpu(buf32[items++]);
  339. key.target_class = (u16)val;
  340. if (key.target_class != val) {
  341. pr_err("SELinux: avtab: truncated target class\n");
  342. return -EINVAL;
  343. }
  344. val = le32_to_cpu(buf32[items++]);
  345. enabled = (val & AVTAB_ENABLED_OLD) ? AVTAB_ENABLED : 0;
  346. if (!(val & (AVTAB_AV | AVTAB_TYPE))) {
  347. pr_err("SELinux: avtab: null entry\n");
  348. return -EINVAL;
  349. }
  350. if ((val & AVTAB_AV) && (val & AVTAB_TYPE)) {
  351. pr_err("SELinux: avtab: entry has both access vectors and types\n");
  352. return -EINVAL;
  353. }
  354. if (val & AVTAB_XPERMS) {
  355. pr_err("SELinux: avtab: entry has extended permissions\n");
  356. return -EINVAL;
  357. }
  358. for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
  359. if (val & spec_order[i]) {
  360. key.specified = spec_order[i] | enabled;
  361. datum.u.data = le32_to_cpu(buf32[items++]);
  362. rc = insertf(a, &key, &datum, p);
  363. if (rc)
  364. return rc;
  365. }
  366. }
  367. if (items != items2) {
  368. pr_err("SELinux: avtab: entry only had %d items, expected %d\n",
  369. items2, items);
  370. return -EINVAL;
  371. }
  372. return 0;
  373. }
  374. rc = next_entry(buf16, fp, sizeof(u16) * 4);
  375. if (rc) {
  376. pr_err("SELinux: avtab: truncated entry\n");
  377. return rc;
  378. }
  379. items = 0;
  380. key.source_type = le16_to_cpu(buf16[items++]);
  381. key.target_type = le16_to_cpu(buf16[items++]);
  382. key.target_class = le16_to_cpu(buf16[items++]);
  383. key.specified = le16_to_cpu(buf16[items++]);
  384. if (!policydb_type_isvalid(pol, key.source_type) ||
  385. !policydb_type_isvalid(pol, key.target_type) ||
  386. !policydb_class_isvalid(pol, key.target_class)) {
  387. pr_err("SELinux: avtab: invalid type or class\n");
  388. return -EINVAL;
  389. }
  390. set = hweight16(key.specified & (AVTAB_XPERMS | AVTAB_TYPE | AVTAB_AV));
  391. if (!set || set > 1) {
  392. pr_err("SELinux: avtab: more than one specifier\n");
  393. return -EINVAL;
  394. }
  395. if ((vers < POLICYDB_VERSION_XPERMS_IOCTL) &&
  396. (key.specified & AVTAB_XPERMS)) {
  397. pr_err("SELinux: avtab: policy version %u does not "
  398. "support extended permissions rules and one "
  399. "was specified\n",
  400. vers);
  401. return -EINVAL;
  402. } else if (key.specified & AVTAB_XPERMS) {
  403. memset(&xperms, 0, sizeof(struct avtab_extended_perms));
  404. rc = next_entry(&xperms.specified, fp, sizeof(u8));
  405. if (rc) {
  406. pr_err("SELinux: avtab: truncated entry\n");
  407. return rc;
  408. }
  409. rc = next_entry(&xperms.driver, fp, sizeof(u8));
  410. if (rc) {
  411. pr_err("SELinux: avtab: truncated entry\n");
  412. return rc;
  413. }
  414. rc = next_entry(buf32, fp,
  415. sizeof(u32) * ARRAY_SIZE(xperms.perms.p));
  416. if (rc) {
  417. pr_err("SELinux: avtab: truncated entry\n");
  418. return rc;
  419. }
  420. for (i = 0; i < ARRAY_SIZE(xperms.perms.p); i++)
  421. xperms.perms.p[i] = le32_to_cpu(buf32[i]);
  422. datum.u.xperms = &xperms;
  423. } else {
  424. rc = next_entry(buf32, fp, sizeof(u32));
  425. if (rc) {
  426. pr_err("SELinux: avtab: truncated entry\n");
  427. return rc;
  428. }
  429. datum.u.data = le32_to_cpu(*buf32);
  430. }
  431. if ((key.specified & AVTAB_TYPE) &&
  432. !policydb_type_isvalid(pol, datum.u.data)) {
  433. pr_err("SELinux: avtab: invalid type\n");
  434. return -EINVAL;
  435. }
  436. return insertf(a, &key, &datum, p);
  437. }
  438. static int avtab_insertf(struct avtab *a, const struct avtab_key *k,
  439. const struct avtab_datum *d, void *p)
  440. {
  441. return avtab_insert(a, k, d);
  442. }
  443. int avtab_read(struct avtab *a, void *fp, struct policydb *pol)
  444. {
  445. int rc;
  446. __le32 buf[1];
  447. u32 nel, i;
  448. rc = next_entry(buf, fp, sizeof(u32));
  449. if (rc < 0) {
  450. pr_err("SELinux: avtab: truncated table\n");
  451. goto bad;
  452. }
  453. nel = le32_to_cpu(buf[0]);
  454. if (!nel) {
  455. pr_err("SELinux: avtab: table is empty\n");
  456. rc = -EINVAL;
  457. goto bad;
  458. }
  459. rc = avtab_alloc(a, nel);
  460. if (rc)
  461. goto bad;
  462. for (i = 0; i < nel; i++) {
  463. rc = avtab_read_item(a, fp, pol, avtab_insertf, NULL);
  464. if (rc) {
  465. if (rc == -ENOMEM)
  466. pr_err("SELinux: avtab: out of memory\n");
  467. else if (rc == -EEXIST)
  468. pr_err("SELinux: avtab: duplicate entry\n");
  469. goto bad;
  470. }
  471. }
  472. rc = 0;
  473. out:
  474. return rc;
  475. bad:
  476. avtab_destroy(a);
  477. goto out;
  478. }
  479. int avtab_write_item(struct policydb *p, const struct avtab_node *cur, void *fp)
  480. {
  481. __le16 buf16[4];
  482. __le32 buf32[ARRAY_SIZE(cur->datum.u.xperms->perms.p)];
  483. int rc;
  484. unsigned int i;
  485. buf16[0] = cpu_to_le16(cur->key.source_type);
  486. buf16[1] = cpu_to_le16(cur->key.target_type);
  487. buf16[2] = cpu_to_le16(cur->key.target_class);
  488. buf16[3] = cpu_to_le16(cur->key.specified);
  489. rc = put_entry(buf16, sizeof(u16), 4, fp);
  490. if (rc)
  491. return rc;
  492. if (cur->key.specified & AVTAB_XPERMS) {
  493. rc = put_entry(&cur->datum.u.xperms->specified, sizeof(u8), 1,
  494. fp);
  495. if (rc)
  496. return rc;
  497. rc = put_entry(&cur->datum.u.xperms->driver, sizeof(u8), 1, fp);
  498. if (rc)
  499. return rc;
  500. for (i = 0; i < ARRAY_SIZE(cur->datum.u.xperms->perms.p); i++)
  501. buf32[i] = cpu_to_le32(cur->datum.u.xperms->perms.p[i]);
  502. rc = put_entry(buf32, sizeof(u32),
  503. ARRAY_SIZE(cur->datum.u.xperms->perms.p), fp);
  504. } else {
  505. buf32[0] = cpu_to_le32(cur->datum.u.data);
  506. rc = put_entry(buf32, sizeof(u32), 1, fp);
  507. }
  508. if (rc)
  509. return rc;
  510. return 0;
  511. }
  512. int avtab_write(struct policydb *p, struct avtab *a, void *fp)
  513. {
  514. u32 i;
  515. int rc = 0;
  516. struct avtab_node *cur;
  517. __le32 buf[1];
  518. buf[0] = cpu_to_le32(a->nel);
  519. rc = put_entry(buf, sizeof(u32), 1, fp);
  520. if (rc)
  521. return rc;
  522. for (i = 0; i < a->nslot; i++) {
  523. for (cur = a->htable[i]; cur; cur = cur->next) {
  524. rc = avtab_write_item(p, cur, fp);
  525. if (rc)
  526. return rc;
  527. }
  528. }
  529. return rc;
  530. }
  531. void __init avtab_cache_init(void)
  532. {
  533. avtab_node_cachep = KMEM_CACHE(avtab_node, SLAB_PANIC);
  534. avtab_xperms_cachep = KMEM_CACHE(avtab_extended_perms, SLAB_PANIC);
  535. }