ebitmap.c 12 KB

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