ebitmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Implementation of the extensible bitmap type.
  4. *
  5. * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
  6. */
  7. /*
  8. * Updated: Hewlett-Packard <paul@paul-moore.com>
  9. * Added support to import/export the NetLabel category bitmap
  10. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
  11. *
  12. * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com>
  13. * Applied standard bit operations to improve bitmap scanning.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/errno.h>
  18. #include <linux/jhash.h>
  19. #include <net/netlabel.h>
  20. #include "ebitmap.h"
  21. #include "policydb.h"
  22. #define BITS_PER_U64 ((u32)(sizeof(u64) * 8))
  23. static struct kmem_cache *ebitmap_node_cachep __ro_after_init;
  24. int ebitmap_cmp(const struct ebitmap *e1, const struct ebitmap *e2)
  25. {
  26. const struct ebitmap_node *n1, *n2;
  27. if (e1->highbit != e2->highbit)
  28. return 0;
  29. n1 = e1->node;
  30. n2 = e2->node;
  31. while (n1 && n2 && (n1->startbit == n2->startbit) &&
  32. !memcmp(n1->maps, n2->maps, EBITMAP_SIZE / 8)) {
  33. n1 = n1->next;
  34. n2 = n2->next;
  35. }
  36. if (n1 || n2)
  37. return 0;
  38. return 1;
  39. }
  40. int ebitmap_cpy(struct ebitmap *dst, const struct ebitmap *src)
  41. {
  42. struct ebitmap_node *new, *prev;
  43. const struct ebitmap_node *n;
  44. ebitmap_init(dst);
  45. n = src->node;
  46. prev = NULL;
  47. while (n) {
  48. new = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
  49. if (!new) {
  50. ebitmap_destroy(dst);
  51. return -ENOMEM;
  52. }
  53. new->startbit = n->startbit;
  54. memcpy(new->maps, n->maps, EBITMAP_SIZE / 8);
  55. new->next = NULL;
  56. if (prev)
  57. prev->next = new;
  58. else
  59. dst->node = new;
  60. prev = new;
  61. n = n->next;
  62. }
  63. dst->highbit = src->highbit;
  64. return 0;
  65. }
  66. int ebitmap_and(struct ebitmap *dst, const struct ebitmap *e1,
  67. const struct ebitmap *e2)
  68. {
  69. struct ebitmap_node *n;
  70. u32 bit;
  71. int rc;
  72. ebitmap_init(dst);
  73. ebitmap_for_each_positive_bit(e1, n, bit)
  74. {
  75. if (ebitmap_get_bit(e2, bit)) {
  76. rc = ebitmap_set_bit(dst, bit, 1);
  77. if (rc < 0)
  78. return rc;
  79. }
  80. }
  81. return 0;
  82. }
  83. #ifdef CONFIG_NETLABEL
  84. /**
  85. * ebitmap_netlbl_export - Export an ebitmap into a NetLabel category bitmap
  86. * @ebmap: the ebitmap to export
  87. * @catmap: the NetLabel category bitmap
  88. *
  89. * Description:
  90. * Export a SELinux extensibile bitmap into a NetLabel category bitmap.
  91. * Returns zero on success, negative values on error.
  92. *
  93. */
  94. int ebitmap_netlbl_export(struct ebitmap *ebmap,
  95. struct netlbl_lsm_catmap **catmap)
  96. {
  97. struct ebitmap_node *e_iter = ebmap->node;
  98. unsigned long e_map;
  99. u32 offset;
  100. unsigned int iter;
  101. int rc;
  102. if (e_iter == NULL) {
  103. *catmap = NULL;
  104. return 0;
  105. }
  106. if (*catmap != NULL)
  107. netlbl_catmap_free(*catmap);
  108. *catmap = NULL;
  109. while (e_iter) {
  110. offset = e_iter->startbit;
  111. for (iter = 0; iter < EBITMAP_UNIT_NUMS; iter++) {
  112. e_map = e_iter->maps[iter];
  113. if (e_map != 0) {
  114. rc = netlbl_catmap_setlong(catmap, offset,
  115. e_map, GFP_ATOMIC);
  116. if (rc != 0)
  117. goto netlbl_export_failure;
  118. }
  119. offset += EBITMAP_UNIT_SIZE;
  120. }
  121. e_iter = e_iter->next;
  122. }
  123. return 0;
  124. netlbl_export_failure:
  125. netlbl_catmap_free(*catmap);
  126. return -ENOMEM;
  127. }
  128. /**
  129. * ebitmap_netlbl_import - Import a NetLabel category bitmap into an ebitmap
  130. * @ebmap: the ebitmap to import
  131. * @catmap: the NetLabel category bitmap
  132. *
  133. * Description:
  134. * Import a NetLabel category bitmap into a SELinux extensibile bitmap.
  135. * Returns zero on success, negative values on error.
  136. *
  137. */
  138. int ebitmap_netlbl_import(struct ebitmap *ebmap,
  139. struct netlbl_lsm_catmap *catmap)
  140. {
  141. int rc;
  142. struct ebitmap_node *e_iter = NULL;
  143. struct ebitmap_node *e_prev = NULL;
  144. u32 offset = 0, idx;
  145. unsigned long bitmap;
  146. for (;;) {
  147. rc = netlbl_catmap_getlong(catmap, &offset, &bitmap);
  148. if (rc < 0)
  149. goto netlbl_import_failure;
  150. if (offset == (u32)-1)
  151. return 0;
  152. /* don't waste ebitmap space if the netlabel bitmap is empty */
  153. if (bitmap == 0) {
  154. offset += EBITMAP_UNIT_SIZE;
  155. continue;
  156. }
  157. if (e_iter == NULL ||
  158. offset >= e_iter->startbit + EBITMAP_SIZE) {
  159. e_prev = e_iter;
  160. e_iter = kmem_cache_zalloc(ebitmap_node_cachep,
  161. GFP_ATOMIC);
  162. if (e_iter == NULL)
  163. goto netlbl_import_failure;
  164. e_iter->startbit = offset - (offset % EBITMAP_SIZE);
  165. if (e_prev == NULL)
  166. ebmap->node = e_iter;
  167. else
  168. e_prev->next = e_iter;
  169. ebmap->highbit = e_iter->startbit + EBITMAP_SIZE;
  170. }
  171. /* offset will always be aligned to an unsigned long */
  172. idx = EBITMAP_NODE_INDEX(e_iter, offset);
  173. e_iter->maps[idx] = bitmap;
  174. /* next */
  175. offset += EBITMAP_UNIT_SIZE;
  176. }
  177. /* NOTE: we should never reach this return */
  178. return 0;
  179. netlbl_import_failure:
  180. ebitmap_destroy(ebmap);
  181. return -ENOMEM;
  182. }
  183. #endif /* CONFIG_NETLABEL */
  184. /*
  185. * Check to see if all the bits set in e2 are also set in e1. Optionally,
  186. * if last_e2bit is non-zero, the highest set bit in e2 cannot exceed
  187. * last_e2bit.
  188. */
  189. int ebitmap_contains(const struct ebitmap *e1, const struct ebitmap *e2,
  190. u32 last_e2bit)
  191. {
  192. const struct ebitmap_node *n1, *n2;
  193. int i;
  194. if (e1->highbit < e2->highbit)
  195. return 0;
  196. n1 = e1->node;
  197. n2 = e2->node;
  198. while (n1 && n2 && (n1->startbit <= n2->startbit)) {
  199. if (n1->startbit < n2->startbit) {
  200. n1 = n1->next;
  201. continue;
  202. }
  203. for (i = EBITMAP_UNIT_NUMS - 1; (i >= 0) && !n2->maps[i];)
  204. i--; /* Skip trailing NULL map entries */
  205. if (last_e2bit && (i >= 0)) {
  206. u32 lastsetbit = n2->startbit + i * EBITMAP_UNIT_SIZE +
  207. __fls(n2->maps[i]);
  208. if (lastsetbit > last_e2bit)
  209. return 0;
  210. }
  211. while (i >= 0) {
  212. if ((n1->maps[i] & n2->maps[i]) != n2->maps[i])
  213. return 0;
  214. i--;
  215. }
  216. n1 = n1->next;
  217. n2 = n2->next;
  218. }
  219. if (n2)
  220. return 0;
  221. return 1;
  222. }
  223. int ebitmap_get_bit(const struct ebitmap *e, u32 bit)
  224. {
  225. const struct ebitmap_node *n;
  226. if (e->highbit < bit)
  227. return 0;
  228. n = e->node;
  229. while (n && (n->startbit <= bit)) {
  230. if ((n->startbit + EBITMAP_SIZE) > bit)
  231. return ebitmap_node_get_bit(n, bit);
  232. n = n->next;
  233. }
  234. return 0;
  235. }
  236. int ebitmap_set_bit(struct ebitmap *e, u32 bit, int value)
  237. {
  238. struct ebitmap_node *n, *prev, *new;
  239. prev = NULL;
  240. n = e->node;
  241. while (n && n->startbit <= bit) {
  242. if ((n->startbit + EBITMAP_SIZE) > bit) {
  243. if (value) {
  244. ebitmap_node_set_bit(n, bit);
  245. } else {
  246. u32 s;
  247. ebitmap_node_clr_bit(n, bit);
  248. s = find_first_bit(n->maps, EBITMAP_SIZE);
  249. if (s < EBITMAP_SIZE)
  250. return 0;
  251. /* drop this node from the bitmap */
  252. if (!n->next) {
  253. /*
  254. * this was the highest map
  255. * within the bitmap
  256. */
  257. if (prev)
  258. e->highbit = prev->startbit +
  259. EBITMAP_SIZE;
  260. else
  261. e->highbit = 0;
  262. }
  263. if (prev)
  264. prev->next = n->next;
  265. else
  266. e->node = n->next;
  267. kmem_cache_free(ebitmap_node_cachep, n);
  268. }
  269. return 0;
  270. }
  271. prev = n;
  272. n = n->next;
  273. }
  274. if (!value)
  275. return 0;
  276. new = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
  277. if (!new)
  278. return -ENOMEM;
  279. new->startbit = bit - (bit % EBITMAP_SIZE);
  280. ebitmap_node_set_bit(new, bit);
  281. if (!n)
  282. /* this node will be the highest map within the bitmap */
  283. e->highbit = new->startbit + EBITMAP_SIZE;
  284. if (prev) {
  285. new->next = prev->next;
  286. prev->next = new;
  287. } else {
  288. new->next = e->node;
  289. e->node = new;
  290. }
  291. return 0;
  292. }
  293. void ebitmap_destroy(struct ebitmap *e)
  294. {
  295. struct ebitmap_node *n, *temp;
  296. if (!e)
  297. return;
  298. n = e->node;
  299. while (n) {
  300. temp = n;
  301. n = n->next;
  302. kmem_cache_free(ebitmap_node_cachep, temp);
  303. }
  304. e->highbit = 0;
  305. e->node = NULL;
  306. }
  307. int ebitmap_read(struct ebitmap *e, void *fp)
  308. {
  309. struct ebitmap_node *n = NULL;
  310. u32 mapunit, count, startbit, index, i;
  311. __le32 ebitmap_start;
  312. u64 map;
  313. __le64 mapbits;
  314. __le32 buf[3];
  315. int rc;
  316. ebitmap_init(e);
  317. rc = next_entry(buf, fp, sizeof buf);
  318. if (rc < 0)
  319. goto out;
  320. mapunit = le32_to_cpu(buf[0]);
  321. e->highbit = le32_to_cpu(buf[1]);
  322. count = le32_to_cpu(buf[2]);
  323. if (mapunit != BITS_PER_U64) {
  324. pr_err("SELinux: ebitmap: map size %u does not "
  325. "match my size %u (high bit was %u)\n",
  326. mapunit, BITS_PER_U64, e->highbit);
  327. goto bad;
  328. }
  329. /* round up e->highbit */
  330. e->highbit += EBITMAP_SIZE - 1;
  331. e->highbit -= (e->highbit % EBITMAP_SIZE);
  332. if (!e->highbit) {
  333. e->node = NULL;
  334. goto ok;
  335. }
  336. if (e->highbit && !count)
  337. goto bad;
  338. for (i = 0; i < count; i++) {
  339. rc = next_entry(&ebitmap_start, fp, sizeof(u32));
  340. if (rc < 0) {
  341. pr_err("SELinux: ebitmap: truncated map\n");
  342. goto bad;
  343. }
  344. startbit = le32_to_cpu(ebitmap_start);
  345. if (startbit & (mapunit - 1)) {
  346. pr_err("SELinux: ebitmap start bit (%u) is "
  347. "not a multiple of the map unit size (%u)\n",
  348. startbit, mapunit);
  349. goto bad;
  350. }
  351. if (startbit > e->highbit - mapunit) {
  352. pr_err("SELinux: ebitmap start bit (%u) is "
  353. "beyond the end of the bitmap (%u)\n",
  354. startbit, (e->highbit - mapunit));
  355. goto bad;
  356. }
  357. if (!n || startbit >= n->startbit + EBITMAP_SIZE) {
  358. struct ebitmap_node *tmp;
  359. tmp = kmem_cache_zalloc(ebitmap_node_cachep,
  360. GFP_KERNEL);
  361. if (!tmp) {
  362. pr_err("SELinux: ebitmap: out of memory\n");
  363. rc = -ENOMEM;
  364. goto bad;
  365. }
  366. /* round down */
  367. tmp->startbit = startbit - (startbit % EBITMAP_SIZE);
  368. if (n)
  369. n->next = tmp;
  370. else
  371. e->node = tmp;
  372. n = tmp;
  373. } else if (startbit <= n->startbit) {
  374. pr_err("SELinux: ebitmap: start bit %u"
  375. " comes after start bit %u\n",
  376. startbit, n->startbit);
  377. goto bad;
  378. }
  379. rc = next_entry(&mapbits, fp, sizeof(u64));
  380. if (rc < 0) {
  381. pr_err("SELinux: ebitmap: truncated map\n");
  382. goto bad;
  383. }
  384. map = le64_to_cpu(mapbits);
  385. if (!map) {
  386. pr_err("SELinux: ebitmap: empty map\n");
  387. goto bad;
  388. }
  389. index = (startbit - n->startbit) / EBITMAP_UNIT_SIZE;
  390. while (map) {
  391. n->maps[index++] = map & (-1UL);
  392. map = EBITMAP_SHIFT_UNIT_SIZE(map);
  393. }
  394. }
  395. if (n && n->startbit + EBITMAP_SIZE != e->highbit) {
  396. pr_err("SELinux: ebitmap: high bit %u is not equal to the expected value %zu\n",
  397. e->highbit, n->startbit + EBITMAP_SIZE);
  398. goto bad;
  399. }
  400. ok:
  401. rc = 0;
  402. out:
  403. return rc;
  404. bad:
  405. if (!rc)
  406. rc = -EINVAL;
  407. ebitmap_destroy(e);
  408. goto out;
  409. }
  410. int ebitmap_write(const struct ebitmap *e, void *fp)
  411. {
  412. struct ebitmap_node *n;
  413. u32 bit, count, last_bit, last_startbit;
  414. __le32 buf[3];
  415. u64 map;
  416. int rc;
  417. buf[0] = cpu_to_le32(BITS_PER_U64);
  418. count = 0;
  419. last_bit = 0;
  420. last_startbit = U32_MAX;
  421. ebitmap_for_each_positive_bit(e, n, bit)
  422. {
  423. if (last_startbit == U32_MAX ||
  424. rounddown(bit, BITS_PER_U64) > last_startbit) {
  425. count++;
  426. last_startbit = rounddown(bit, BITS_PER_U64);
  427. }
  428. last_bit = roundup(bit + 1, BITS_PER_U64);
  429. }
  430. buf[1] = cpu_to_le32(last_bit);
  431. buf[2] = cpu_to_le32(count);
  432. rc = put_entry(buf, sizeof(u32), 3, fp);
  433. if (rc)
  434. return rc;
  435. map = 0;
  436. last_startbit = U32_MAX;
  437. ebitmap_for_each_positive_bit(e, n, bit)
  438. {
  439. if (last_startbit == U32_MAX ||
  440. rounddown(bit, BITS_PER_U64) > last_startbit) {
  441. __le64 buf64[1];
  442. /* this is the very first bit */
  443. if (!map) {
  444. last_startbit = rounddown(bit, BITS_PER_U64);
  445. map = (u64)1 << (bit - last_startbit);
  446. continue;
  447. }
  448. /* write the last node */
  449. buf[0] = cpu_to_le32(last_startbit);
  450. rc = put_entry(buf, sizeof(u32), 1, fp);
  451. if (rc)
  452. return rc;
  453. buf64[0] = cpu_to_le64(map);
  454. rc = put_entry(buf64, sizeof(u64), 1, fp);
  455. if (rc)
  456. return rc;
  457. /* set up for the next node */
  458. map = 0;
  459. last_startbit = rounddown(bit, BITS_PER_U64);
  460. }
  461. map |= (u64)1 << (bit - last_startbit);
  462. }
  463. /* write the last node */
  464. if (map) {
  465. __le64 buf64[1];
  466. /* write the last node */
  467. buf[0] = cpu_to_le32(last_startbit);
  468. rc = put_entry(buf, sizeof(u32), 1, fp);
  469. if (rc)
  470. return rc;
  471. buf64[0] = cpu_to_le64(map);
  472. rc = put_entry(buf64, sizeof(u64), 1, fp);
  473. if (rc)
  474. return rc;
  475. }
  476. return 0;
  477. }
  478. u32 ebitmap_hash(const struct ebitmap *e, u32 hash)
  479. {
  480. struct ebitmap_node *node;
  481. /* need to change hash even if ebitmap is empty */
  482. hash = jhash_1word(e->highbit, hash);
  483. for (node = e->node; node; node = node->next) {
  484. hash = jhash_1word(node->startbit, hash);
  485. hash = jhash(node->maps, sizeof(node->maps), hash);
  486. }
  487. return hash;
  488. }
  489. void __init ebitmap_cache_init(void)
  490. {
  491. ebitmap_node_cachep = KMEM_CACHE(ebitmap_node, SLAB_PANIC);
  492. }