ebitmap.c 11 KB

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