ebitmap.c 10 KB

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