ebitmap.c 11 KB

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