ebitmap.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Implementation of the extensible bitmap type.
  3. *
  4. * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
  5. */
  6. /*
  7. * Updated: Hewlett-Packard <paul.moore@hp.com>
  8. *
  9. * Added ebitmap_export() and ebitmap_import()
  10. *
  11. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/errno.h>
  16. #include "ebitmap.h"
  17. #include "policydb.h"
  18. int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2)
  19. {
  20. struct ebitmap_node *n1, *n2;
  21. if (e1->highbit != e2->highbit)
  22. return 0;
  23. n1 = e1->node;
  24. n2 = e2->node;
  25. while (n1 && n2 &&
  26. (n1->startbit == n2->startbit) &&
  27. (n1->map == n2->map)) {
  28. n1 = n1->next;
  29. n2 = n2->next;
  30. }
  31. if (n1 || n2)
  32. return 0;
  33. return 1;
  34. }
  35. int ebitmap_cpy(struct ebitmap *dst, struct ebitmap *src)
  36. {
  37. struct ebitmap_node *n, *new, *prev;
  38. ebitmap_init(dst);
  39. n = src->node;
  40. prev = NULL;
  41. while (n) {
  42. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  43. if (!new) {
  44. ebitmap_destroy(dst);
  45. return -ENOMEM;
  46. }
  47. new->startbit = n->startbit;
  48. new->map = n->map;
  49. new->next = NULL;
  50. if (prev)
  51. prev->next = new;
  52. else
  53. dst->node = new;
  54. prev = new;
  55. n = n->next;
  56. }
  57. dst->highbit = src->highbit;
  58. return 0;
  59. }
  60. /**
  61. * ebitmap_export - Export an ebitmap to a unsigned char bitmap string
  62. * @src: the ebitmap to export
  63. * @dst: the resulting bitmap string
  64. * @dst_len: length of dst in bytes
  65. *
  66. * Description:
  67. * Allocate a buffer at least src->highbit bits long and export the extensible
  68. * bitmap into the buffer. The bitmap string will be in little endian format,
  69. * i.e. LSB first. The value returned in dst_len may not the true size of the
  70. * buffer as the length of the buffer is rounded up to a multiple of MAPTYPE.
  71. * The caller must free the buffer when finished. Returns zero on success,
  72. * negative values on failure.
  73. *
  74. */
  75. int ebitmap_export(const struct ebitmap *src,
  76. unsigned char **dst,
  77. size_t *dst_len)
  78. {
  79. size_t bitmap_len;
  80. unsigned char *bitmap;
  81. struct ebitmap_node *iter_node;
  82. MAPTYPE node_val;
  83. size_t bitmap_byte;
  84. unsigned char bitmask;
  85. if (src->highbit == 0) {
  86. *dst = NULL;
  87. *dst_len = 0;
  88. return 0;
  89. }
  90. bitmap_len = src->highbit / 8;
  91. if (src->highbit % 7)
  92. bitmap_len += 1;
  93. bitmap = kzalloc((bitmap_len & ~(sizeof(MAPTYPE) - 1)) +
  94. sizeof(MAPTYPE),
  95. GFP_ATOMIC);
  96. if (bitmap == NULL)
  97. return -ENOMEM;
  98. iter_node = src->node;
  99. do {
  100. bitmap_byte = iter_node->startbit / 8;
  101. bitmask = 0x80;
  102. node_val = iter_node->map;
  103. do {
  104. if (bitmask == 0) {
  105. bitmap_byte++;
  106. bitmask = 0x80;
  107. }
  108. if (node_val & (MAPTYPE)0x01)
  109. bitmap[bitmap_byte] |= bitmask;
  110. node_val >>= 1;
  111. bitmask >>= 1;
  112. } while (node_val > 0);
  113. iter_node = iter_node->next;
  114. } while (iter_node);
  115. *dst = bitmap;
  116. *dst_len = bitmap_len;
  117. return 0;
  118. }
  119. /**
  120. * ebitmap_import - Import an unsigned char bitmap string into an ebitmap
  121. * @src: the bitmap string
  122. * @src_len: the bitmap length in bytes
  123. * @dst: the empty ebitmap
  124. *
  125. * Description:
  126. * This function takes a little endian bitmap string in src and imports it into
  127. * the ebitmap pointed to by dst. Returns zero on success, negative values on
  128. * failure.
  129. *
  130. */
  131. int ebitmap_import(const unsigned char *src,
  132. size_t src_len,
  133. struct ebitmap *dst)
  134. {
  135. size_t src_off = 0;
  136. size_t node_limit;
  137. struct ebitmap_node *node_new;
  138. struct ebitmap_node *node_last = NULL;
  139. u32 i_byte;
  140. u32 i_bit;
  141. unsigned char src_byte;
  142. while (src_off < src_len) {
  143. if (src_len - src_off >= sizeof(MAPTYPE)) {
  144. if (*(MAPTYPE *)&src[src_off] == 0) {
  145. src_off += sizeof(MAPTYPE);
  146. continue;
  147. }
  148. node_limit = sizeof(MAPTYPE);
  149. } else {
  150. for (src_byte = 0, i_byte = src_off;
  151. i_byte < src_len && src_byte == 0;
  152. i_byte++)
  153. src_byte |= src[i_byte];
  154. if (src_byte == 0)
  155. break;
  156. node_limit = src_len - src_off;
  157. }
  158. node_new = kzalloc(sizeof(*node_new), GFP_ATOMIC);
  159. if (unlikely(node_new == NULL)) {
  160. ebitmap_destroy(dst);
  161. return -ENOMEM;
  162. }
  163. node_new->startbit = src_off * 8;
  164. for (i_byte = 0; i_byte < node_limit; i_byte++) {
  165. src_byte = src[src_off++];
  166. for (i_bit = i_byte * 8; src_byte != 0; i_bit++) {
  167. if (src_byte & 0x80)
  168. node_new->map |= MAPBIT << i_bit;
  169. src_byte <<= 1;
  170. }
  171. }
  172. if (node_last != NULL)
  173. node_last->next = node_new;
  174. else
  175. dst->node = node_new;
  176. node_last = node_new;
  177. }
  178. if (likely(node_last != NULL))
  179. dst->highbit = node_last->startbit + MAPSIZE;
  180. else
  181. ebitmap_init(dst);
  182. return 0;
  183. }
  184. int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2)
  185. {
  186. struct ebitmap_node *n1, *n2;
  187. if (e1->highbit < e2->highbit)
  188. return 0;
  189. n1 = e1->node;
  190. n2 = e2->node;
  191. while (n1 && n2 && (n1->startbit <= n2->startbit)) {
  192. if (n1->startbit < n2->startbit) {
  193. n1 = n1->next;
  194. continue;
  195. }
  196. if ((n1->map & n2->map) != n2->map)
  197. return 0;
  198. n1 = n1->next;
  199. n2 = n2->next;
  200. }
  201. if (n2)
  202. return 0;
  203. return 1;
  204. }
  205. int ebitmap_get_bit(struct ebitmap *e, unsigned long bit)
  206. {
  207. struct ebitmap_node *n;
  208. if (e->highbit < bit)
  209. return 0;
  210. n = e->node;
  211. while (n && (n->startbit <= bit)) {
  212. if ((n->startbit + MAPSIZE) > bit) {
  213. if (n->map & (MAPBIT << (bit - n->startbit)))
  214. return 1;
  215. else
  216. return 0;
  217. }
  218. n = n->next;
  219. }
  220. return 0;
  221. }
  222. int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value)
  223. {
  224. struct ebitmap_node *n, *prev, *new;
  225. prev = NULL;
  226. n = e->node;
  227. while (n && n->startbit <= bit) {
  228. if ((n->startbit + MAPSIZE) > bit) {
  229. if (value) {
  230. n->map |= (MAPBIT << (bit - n->startbit));
  231. } else {
  232. n->map &= ~(MAPBIT << (bit - n->startbit));
  233. if (!n->map) {
  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 + MAPSIZE;
  242. else
  243. e->highbit = 0;
  244. }
  245. if (prev)
  246. prev->next = n->next;
  247. else
  248. e->node = n->next;
  249. kfree(n);
  250. }
  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 & ~(MAPSIZE - 1);
  263. new->map = (MAPBIT << (bit - new->startbit));
  264. if (!n)
  265. /* this node will be the highest map within the bitmap */
  266. e->highbit = new->startbit + MAPSIZE;
  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. int rc;
  294. struct ebitmap_node *n, *l;
  295. __le32 buf[3];
  296. u32 mapsize, count, i;
  297. __le64 map;
  298. ebitmap_init(e);
  299. rc = next_entry(buf, fp, sizeof buf);
  300. if (rc < 0)
  301. goto out;
  302. mapsize = le32_to_cpu(buf[0]);
  303. e->highbit = le32_to_cpu(buf[1]);
  304. count = le32_to_cpu(buf[2]);
  305. if (mapsize != MAPSIZE) {
  306. printk(KERN_ERR "security: ebitmap: map size %u does not "
  307. "match my size %Zd (high bit was %d)\n", mapsize,
  308. MAPSIZE, e->highbit);
  309. goto bad;
  310. }
  311. if (!e->highbit) {
  312. e->node = NULL;
  313. goto ok;
  314. }
  315. if (e->highbit & (MAPSIZE - 1)) {
  316. printk(KERN_ERR "security: ebitmap: high bit (%d) is not a "
  317. "multiple of the map size (%Zd)\n", e->highbit, MAPSIZE);
  318. goto bad;
  319. }
  320. l = NULL;
  321. for (i = 0; i < count; i++) {
  322. rc = next_entry(buf, fp, sizeof(u32));
  323. if (rc < 0) {
  324. printk(KERN_ERR "security: ebitmap: truncated map\n");
  325. goto bad;
  326. }
  327. n = kzalloc(sizeof(*n), GFP_KERNEL);
  328. if (!n) {
  329. printk(KERN_ERR "security: ebitmap: out of memory\n");
  330. rc = -ENOMEM;
  331. goto bad;
  332. }
  333. n->startbit = le32_to_cpu(buf[0]);
  334. if (n->startbit & (MAPSIZE - 1)) {
  335. printk(KERN_ERR "security: ebitmap start bit (%d) is "
  336. "not a multiple of the map size (%Zd)\n",
  337. n->startbit, MAPSIZE);
  338. goto bad_free;
  339. }
  340. if (n->startbit > (e->highbit - MAPSIZE)) {
  341. printk(KERN_ERR "security: ebitmap start bit (%d) is "
  342. "beyond the end of the bitmap (%Zd)\n",
  343. n->startbit, (e->highbit - MAPSIZE));
  344. goto bad_free;
  345. }
  346. rc = next_entry(&map, fp, sizeof(u64));
  347. if (rc < 0) {
  348. printk(KERN_ERR "security: ebitmap: truncated map\n");
  349. goto bad_free;
  350. }
  351. n->map = le64_to_cpu(map);
  352. if (!n->map) {
  353. printk(KERN_ERR "security: ebitmap: null map in "
  354. "ebitmap (startbit %d)\n", n->startbit);
  355. goto bad_free;
  356. }
  357. if (l) {
  358. if (n->startbit <= l->startbit) {
  359. printk(KERN_ERR "security: ebitmap: start "
  360. "bit %d comes after start bit %d\n",
  361. n->startbit, l->startbit);
  362. goto bad_free;
  363. }
  364. l->next = n;
  365. } else
  366. e->node = n;
  367. l = n;
  368. }
  369. ok:
  370. rc = 0;
  371. out:
  372. return rc;
  373. bad_free:
  374. kfree(n);
  375. bad:
  376. if (!rc)
  377. rc = -EINVAL;
  378. ebitmap_destroy(e);
  379. goto out;
  380. }