ebitmap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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_catmap **catmap)
  79. {
  80. struct ebitmap_node *e_iter = ebmap->node;
  81. unsigned long e_map;
  82. u32 offset;
  83. unsigned int iter;
  84. int rc;
  85. if (e_iter == NULL) {
  86. *catmap = NULL;
  87. return 0;
  88. }
  89. if (*catmap != NULL)
  90. netlbl_catmap_free(*catmap);
  91. *catmap = NULL;
  92. while (e_iter) {
  93. offset = e_iter->startbit;
  94. for (iter = 0; iter < EBITMAP_UNIT_NUMS; iter++) {
  95. e_map = e_iter->maps[iter];
  96. if (e_map != 0) {
  97. rc = netlbl_catmap_setlong(catmap,
  98. offset,
  99. e_map,
  100. GFP_ATOMIC);
  101. if (rc != 0)
  102. goto netlbl_export_failure;
  103. }
  104. offset += EBITMAP_UNIT_SIZE;
  105. }
  106. e_iter = e_iter->next;
  107. }
  108. return 0;
  109. netlbl_export_failure:
  110. netlbl_catmap_free(*catmap);
  111. return -ENOMEM;
  112. }
  113. /**
  114. * ebitmap_netlbl_import - Import a NetLabel category bitmap into an ebitmap
  115. * @ebmap: the ebitmap to import
  116. * @catmap: the NetLabel category bitmap
  117. *
  118. * Description:
  119. * Import a NetLabel category bitmap into a SELinux extensibile bitmap.
  120. * Returns zero on success, negative values on error.
  121. *
  122. */
  123. int ebitmap_netlbl_import(struct ebitmap *ebmap,
  124. struct netlbl_lsm_catmap *catmap)
  125. {
  126. int rc;
  127. struct ebitmap_node *e_iter = NULL;
  128. struct ebitmap_node *e_prev = NULL;
  129. u32 offset = 0, idx;
  130. unsigned long bitmap;
  131. for (;;) {
  132. rc = netlbl_catmap_getlong(catmap, &offset, &bitmap);
  133. if (rc < 0)
  134. goto netlbl_import_failure;
  135. if (offset == (u32)-1)
  136. return 0;
  137. /* don't waste ebitmap space if the netlabel bitmap is empty */
  138. if (bitmap == 0) {
  139. offset += EBITMAP_UNIT_SIZE;
  140. continue;
  141. }
  142. if (e_iter == NULL ||
  143. offset >= e_iter->startbit + EBITMAP_SIZE) {
  144. e_prev = e_iter;
  145. e_iter = kzalloc(sizeof(*e_iter), GFP_ATOMIC);
  146. if (e_iter == NULL)
  147. goto netlbl_import_failure;
  148. e_iter->startbit = offset & ~(EBITMAP_SIZE - 1);
  149. if (e_prev == NULL)
  150. ebmap->node = e_iter;
  151. else
  152. e_prev->next = e_iter;
  153. ebmap->highbit = e_iter->startbit + EBITMAP_SIZE;
  154. }
  155. /* offset will always be aligned to an unsigned long */
  156. idx = EBITMAP_NODE_INDEX(e_iter, offset);
  157. e_iter->maps[idx] = bitmap;
  158. /* next */
  159. offset += EBITMAP_UNIT_SIZE;
  160. }
  161. /* NOTE: we should never reach this return */
  162. return 0;
  163. netlbl_import_failure:
  164. ebitmap_destroy(ebmap);
  165. return -ENOMEM;
  166. }
  167. #endif /* CONFIG_NETLABEL */
  168. /*
  169. * Check to see if all the bits set in e2 are also set in e1. Optionally,
  170. * if last_e2bit is non-zero, the highest set bit in e2 cannot exceed
  171. * last_e2bit.
  172. */
  173. int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2, u32 last_e2bit)
  174. {
  175. struct ebitmap_node *n1, *n2;
  176. int i;
  177. if (e1->highbit < e2->highbit)
  178. return 0;
  179. n1 = e1->node;
  180. n2 = e2->node;
  181. while (n1 && n2 && (n1->startbit <= n2->startbit)) {
  182. if (n1->startbit < n2->startbit) {
  183. n1 = n1->next;
  184. continue;
  185. }
  186. for (i = EBITMAP_UNIT_NUMS - 1; (i >= 0) && !n2->maps[i]; )
  187. i--; /* Skip trailing NULL map entries */
  188. if (last_e2bit && (i >= 0)) {
  189. u32 lastsetbit = n2->startbit + i * EBITMAP_UNIT_SIZE +
  190. __fls(n2->maps[i]);
  191. if (lastsetbit > last_e2bit)
  192. return 0;
  193. }
  194. while (i >= 0) {
  195. if ((n1->maps[i] & n2->maps[i]) != n2->maps[i])
  196. return 0;
  197. i--;
  198. }
  199. n1 = n1->next;
  200. n2 = n2->next;
  201. }
  202. if (n2)
  203. return 0;
  204. return 1;
  205. }
  206. int ebitmap_get_bit(struct ebitmap *e, unsigned long bit)
  207. {
  208. struct ebitmap_node *n;
  209. if (e->highbit < bit)
  210. return 0;
  211. n = e->node;
  212. while (n && (n->startbit <= bit)) {
  213. if ((n->startbit + EBITMAP_SIZE) > bit)
  214. return ebitmap_node_get_bit(n, bit);
  215. n = n->next;
  216. }
  217. return 0;
  218. }
  219. int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value)
  220. {
  221. struct ebitmap_node *n, *prev, *new;
  222. prev = NULL;
  223. n = e->node;
  224. while (n && n->startbit <= bit) {
  225. if ((n->startbit + EBITMAP_SIZE) > bit) {
  226. if (value) {
  227. ebitmap_node_set_bit(n, bit);
  228. } else {
  229. unsigned int s;
  230. ebitmap_node_clr_bit(n, bit);
  231. s = find_first_bit(n->maps, EBITMAP_SIZE);
  232. if (s < EBITMAP_SIZE)
  233. return 0;
  234. /* drop this node from the bitmap */
  235. if (!n->next) {
  236. /*
  237. * this was the highest map
  238. * within the bitmap
  239. */
  240. if (prev)
  241. e->highbit = prev->startbit
  242. + EBITMAP_SIZE;
  243. else
  244. e->highbit = 0;
  245. }
  246. if (prev)
  247. prev->next = n->next;
  248. else
  249. e->node = n->next;
  250. kfree(n);
  251. }
  252. return 0;
  253. }
  254. prev = n;
  255. n = n->next;
  256. }
  257. if (!value)
  258. return 0;
  259. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  260. if (!new)
  261. return -ENOMEM;
  262. new->startbit = bit - (bit % EBITMAP_SIZE);
  263. ebitmap_node_set_bit(new, bit);
  264. if (!n)
  265. /* this node will be the highest map within the bitmap */
  266. e->highbit = new->startbit + EBITMAP_SIZE;
  267. if (prev) {
  268. new->next = prev->next;
  269. prev->next = new;
  270. } else {
  271. new->next = e->node;
  272. e->node = new;
  273. }
  274. return 0;
  275. }
  276. void ebitmap_destroy(struct ebitmap *e)
  277. {
  278. struct ebitmap_node *n, *temp;
  279. if (!e)
  280. return;
  281. n = e->node;
  282. while (n) {
  283. temp = n;
  284. n = n->next;
  285. kfree(temp);
  286. }
  287. e->highbit = 0;
  288. e->node = NULL;
  289. return;
  290. }
  291. int ebitmap_read(struct ebitmap *e, void *fp)
  292. {
  293. struct ebitmap_node *n = NULL;
  294. u32 mapunit, count, startbit, index;
  295. u64 map;
  296. __le32 buf[3];
  297. int rc, i;
  298. ebitmap_init(e);
  299. rc = next_entry(buf, fp, sizeof buf);
  300. if (rc < 0)
  301. goto out;
  302. mapunit = le32_to_cpu(buf[0]);
  303. e->highbit = le32_to_cpu(buf[1]);
  304. count = le32_to_cpu(buf[2]);
  305. if (mapunit != BITS_PER_U64) {
  306. printk(KERN_ERR "SELinux: ebitmap: map size %u does not "
  307. "match my size %Zd (high bit was %d)\n",
  308. mapunit, BITS_PER_U64, e->highbit);
  309. goto bad;
  310. }
  311. /* round up e->highbit */
  312. e->highbit += EBITMAP_SIZE - 1;
  313. e->highbit -= (e->highbit % EBITMAP_SIZE);
  314. if (!e->highbit) {
  315. e->node = NULL;
  316. goto ok;
  317. }
  318. for (i = 0; i < count; i++) {
  319. rc = next_entry(&startbit, fp, sizeof(u32));
  320. if (rc < 0) {
  321. printk(KERN_ERR "SELinux: ebitmap: truncated map\n");
  322. goto bad;
  323. }
  324. startbit = le32_to_cpu(startbit);
  325. if (startbit & (mapunit - 1)) {
  326. printk(KERN_ERR "SELinux: ebitmap start bit (%d) is "
  327. "not a multiple of the map unit size (%u)\n",
  328. startbit, mapunit);
  329. goto bad;
  330. }
  331. if (startbit > e->highbit - mapunit) {
  332. printk(KERN_ERR "SELinux: ebitmap start bit (%d) is "
  333. "beyond the end of the bitmap (%u)\n",
  334. startbit, (e->highbit - mapunit));
  335. goto bad;
  336. }
  337. if (!n || startbit >= n->startbit + EBITMAP_SIZE) {
  338. struct ebitmap_node *tmp;
  339. tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
  340. if (!tmp) {
  341. printk(KERN_ERR
  342. "SELinux: ebitmap: out of memory\n");
  343. rc = -ENOMEM;
  344. goto bad;
  345. }
  346. /* round down */
  347. tmp->startbit = startbit - (startbit % EBITMAP_SIZE);
  348. if (n)
  349. n->next = tmp;
  350. else
  351. e->node = tmp;
  352. n = tmp;
  353. } else if (startbit <= n->startbit) {
  354. printk(KERN_ERR "SELinux: ebitmap: start bit %d"
  355. " comes after start bit %d\n",
  356. startbit, n->startbit);
  357. goto bad;
  358. }
  359. rc = next_entry(&map, fp, sizeof(u64));
  360. if (rc < 0) {
  361. printk(KERN_ERR "SELinux: ebitmap: truncated map\n");
  362. goto bad;
  363. }
  364. map = le64_to_cpu(map);
  365. index = (startbit - n->startbit) / EBITMAP_UNIT_SIZE;
  366. while (map) {
  367. n->maps[index++] = map & (-1UL);
  368. map = EBITMAP_SHIFT_UNIT_SIZE(map);
  369. }
  370. }
  371. ok:
  372. rc = 0;
  373. out:
  374. return rc;
  375. bad:
  376. if (!rc)
  377. rc = -EINVAL;
  378. ebitmap_destroy(e);
  379. goto out;
  380. }
  381. int ebitmap_write(struct ebitmap *e, void *fp)
  382. {
  383. struct ebitmap_node *n;
  384. u32 count;
  385. __le32 buf[3];
  386. u64 map;
  387. int bit, last_bit, last_startbit, rc;
  388. buf[0] = cpu_to_le32(BITS_PER_U64);
  389. count = 0;
  390. last_bit = 0;
  391. last_startbit = -1;
  392. ebitmap_for_each_positive_bit(e, n, bit) {
  393. if (rounddown(bit, (int)BITS_PER_U64) > last_startbit) {
  394. count++;
  395. last_startbit = rounddown(bit, BITS_PER_U64);
  396. }
  397. last_bit = roundup(bit + 1, BITS_PER_U64);
  398. }
  399. buf[1] = cpu_to_le32(last_bit);
  400. buf[2] = cpu_to_le32(count);
  401. rc = put_entry(buf, sizeof(u32), 3, fp);
  402. if (rc)
  403. return rc;
  404. map = 0;
  405. last_startbit = INT_MIN;
  406. ebitmap_for_each_positive_bit(e, n, bit) {
  407. if (rounddown(bit, (int)BITS_PER_U64) > last_startbit) {
  408. __le64 buf64[1];
  409. /* this is the very first bit */
  410. if (!map) {
  411. last_startbit = rounddown(bit, BITS_PER_U64);
  412. map = (u64)1 << (bit - last_startbit);
  413. continue;
  414. }
  415. /* write the last node */
  416. buf[0] = cpu_to_le32(last_startbit);
  417. rc = put_entry(buf, sizeof(u32), 1, fp);
  418. if (rc)
  419. return rc;
  420. buf64[0] = cpu_to_le64(map);
  421. rc = put_entry(buf64, sizeof(u64), 1, fp);
  422. if (rc)
  423. return rc;
  424. /* set up for the next node */
  425. map = 0;
  426. last_startbit = rounddown(bit, BITS_PER_U64);
  427. }
  428. map |= (u64)1 << (bit - last_startbit);
  429. }
  430. /* write the last node */
  431. if (map) {
  432. __le64 buf64[1];
  433. /* write the last node */
  434. buf[0] = cpu_to_le32(last_startbit);
  435. rc = put_entry(buf, sizeof(u32), 1, fp);
  436. if (rc)
  437. return rc;
  438. buf64[0] = cpu_to_le64(map);
  439. rc = put_entry(buf64, sizeof(u64), 1, fp);
  440. if (rc)
  441. return rc;
  442. }
  443. return 0;
  444. }