ebitmap.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. bitmap_len = src->highbit / 8;
  86. if (src->highbit % 7)
  87. bitmap_len += 1;
  88. if (bitmap_len == 0)
  89. return -EINVAL;
  90. bitmap = kzalloc((bitmap_len & ~(sizeof(MAPTYPE) - 1)) +
  91. sizeof(MAPTYPE),
  92. GFP_ATOMIC);
  93. if (bitmap == NULL)
  94. return -ENOMEM;
  95. iter_node = src->node;
  96. do {
  97. bitmap_byte = iter_node->startbit / 8;
  98. bitmask = 0x80;
  99. node_val = iter_node->map;
  100. do {
  101. if (bitmask == 0) {
  102. bitmap_byte++;
  103. bitmask = 0x80;
  104. }
  105. if (node_val & (MAPTYPE)0x01)
  106. bitmap[bitmap_byte] |= bitmask;
  107. node_val >>= 1;
  108. bitmask >>= 1;
  109. } while (node_val > 0);
  110. iter_node = iter_node->next;
  111. } while (iter_node);
  112. *dst = bitmap;
  113. *dst_len = bitmap_len;
  114. return 0;
  115. }
  116. /**
  117. * ebitmap_import - Import an unsigned char bitmap string into an ebitmap
  118. * @src: the bitmap string
  119. * @src_len: the bitmap length in bytes
  120. * @dst: the empty ebitmap
  121. *
  122. * Description:
  123. * This function takes a little endian bitmap string in src and imports it into
  124. * the ebitmap pointed to by dst. Returns zero on success, negative values on
  125. * failure.
  126. *
  127. */
  128. int ebitmap_import(const unsigned char *src,
  129. size_t src_len,
  130. struct ebitmap *dst)
  131. {
  132. size_t src_off = 0;
  133. size_t node_limit;
  134. struct ebitmap_node *node_new;
  135. struct ebitmap_node *node_last = NULL;
  136. u32 i_byte;
  137. u32 i_bit;
  138. unsigned char src_byte;
  139. while (src_off < src_len) {
  140. if (src_len - src_off >= sizeof(MAPTYPE)) {
  141. if (*(MAPTYPE *)&src[src_off] == 0) {
  142. src_off += sizeof(MAPTYPE);
  143. continue;
  144. }
  145. node_limit = sizeof(MAPTYPE);
  146. } else {
  147. for (src_byte = 0, i_byte = src_off;
  148. i_byte < src_len && src_byte == 0;
  149. i_byte++)
  150. src_byte |= src[i_byte];
  151. if (src_byte == 0)
  152. break;
  153. node_limit = src_len - src_off;
  154. }
  155. node_new = kzalloc(sizeof(*node_new), GFP_ATOMIC);
  156. if (unlikely(node_new == NULL)) {
  157. ebitmap_destroy(dst);
  158. return -ENOMEM;
  159. }
  160. node_new->startbit = src_off * 8;
  161. for (i_byte = 0; i_byte < node_limit; i_byte++) {
  162. src_byte = src[src_off++];
  163. for (i_bit = i_byte * 8; src_byte != 0; i_bit++) {
  164. if (src_byte & 0x80)
  165. node_new->map |= MAPBIT << i_bit;
  166. src_byte <<= 1;
  167. }
  168. }
  169. if (node_last != NULL)
  170. node_last->next = node_new;
  171. else
  172. dst->node = node_new;
  173. node_last = node_new;
  174. }
  175. if (likely(node_last != NULL))
  176. dst->highbit = node_last->startbit + MAPSIZE;
  177. else
  178. ebitmap_init(dst);
  179. return 0;
  180. }
  181. int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2)
  182. {
  183. struct ebitmap_node *n1, *n2;
  184. if (e1->highbit < e2->highbit)
  185. return 0;
  186. n1 = e1->node;
  187. n2 = e2->node;
  188. while (n1 && n2 && (n1->startbit <= n2->startbit)) {
  189. if (n1->startbit < n2->startbit) {
  190. n1 = n1->next;
  191. continue;
  192. }
  193. if ((n1->map & n2->map) != n2->map)
  194. return 0;
  195. n1 = n1->next;
  196. n2 = n2->next;
  197. }
  198. if (n2)
  199. return 0;
  200. return 1;
  201. }
  202. int ebitmap_get_bit(struct ebitmap *e, unsigned long bit)
  203. {
  204. struct ebitmap_node *n;
  205. if (e->highbit < bit)
  206. return 0;
  207. n = e->node;
  208. while (n && (n->startbit <= bit)) {
  209. if ((n->startbit + MAPSIZE) > bit) {
  210. if (n->map & (MAPBIT << (bit - n->startbit)))
  211. return 1;
  212. else
  213. return 0;
  214. }
  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 + MAPSIZE) > bit) {
  226. if (value) {
  227. n->map |= (MAPBIT << (bit - n->startbit));
  228. } else {
  229. n->map &= ~(MAPBIT << (bit - n->startbit));
  230. if (!n->map) {
  231. /* drop this node from the bitmap */
  232. if (!n->next) {
  233. /*
  234. * this was the highest map
  235. * within the bitmap
  236. */
  237. if (prev)
  238. e->highbit = prev->startbit + MAPSIZE;
  239. else
  240. e->highbit = 0;
  241. }
  242. if (prev)
  243. prev->next = n->next;
  244. else
  245. e->node = n->next;
  246. kfree(n);
  247. }
  248. }
  249. return 0;
  250. }
  251. prev = n;
  252. n = n->next;
  253. }
  254. if (!value)
  255. return 0;
  256. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  257. if (!new)
  258. return -ENOMEM;
  259. new->startbit = bit & ~(MAPSIZE - 1);
  260. new->map = (MAPBIT << (bit - new->startbit));
  261. if (!n)
  262. /* this node will be the highest map within the bitmap */
  263. e->highbit = new->startbit + MAPSIZE;
  264. if (prev) {
  265. new->next = prev->next;
  266. prev->next = new;
  267. } else {
  268. new->next = e->node;
  269. e->node = new;
  270. }
  271. return 0;
  272. }
  273. void ebitmap_destroy(struct ebitmap *e)
  274. {
  275. struct ebitmap_node *n, *temp;
  276. if (!e)
  277. return;
  278. n = e->node;
  279. while (n) {
  280. temp = n;
  281. n = n->next;
  282. kfree(temp);
  283. }
  284. e->highbit = 0;
  285. e->node = NULL;
  286. return;
  287. }
  288. int ebitmap_read(struct ebitmap *e, void *fp)
  289. {
  290. int rc;
  291. struct ebitmap_node *n, *l;
  292. __le32 buf[3];
  293. u32 mapsize, count, i;
  294. __le64 map;
  295. ebitmap_init(e);
  296. rc = next_entry(buf, fp, sizeof buf);
  297. if (rc < 0)
  298. goto out;
  299. mapsize = le32_to_cpu(buf[0]);
  300. e->highbit = le32_to_cpu(buf[1]);
  301. count = le32_to_cpu(buf[2]);
  302. if (mapsize != MAPSIZE) {
  303. printk(KERN_ERR "security: ebitmap: map size %u does not "
  304. "match my size %Zd (high bit was %d)\n", mapsize,
  305. MAPSIZE, e->highbit);
  306. goto bad;
  307. }
  308. if (!e->highbit) {
  309. e->node = NULL;
  310. goto ok;
  311. }
  312. if (e->highbit & (MAPSIZE - 1)) {
  313. printk(KERN_ERR "security: ebitmap: high bit (%d) is not a "
  314. "multiple of the map size (%Zd)\n", e->highbit, MAPSIZE);
  315. goto bad;
  316. }
  317. l = NULL;
  318. for (i = 0; i < count; i++) {
  319. rc = next_entry(buf, fp, sizeof(u32));
  320. if (rc < 0) {
  321. printk(KERN_ERR "security: ebitmap: truncated map\n");
  322. goto bad;
  323. }
  324. n = kzalloc(sizeof(*n), GFP_KERNEL);
  325. if (!n) {
  326. printk(KERN_ERR "security: ebitmap: out of memory\n");
  327. rc = -ENOMEM;
  328. goto bad;
  329. }
  330. n->startbit = le32_to_cpu(buf[0]);
  331. if (n->startbit & (MAPSIZE - 1)) {
  332. printk(KERN_ERR "security: ebitmap start bit (%d) is "
  333. "not a multiple of the map size (%Zd)\n",
  334. n->startbit, MAPSIZE);
  335. goto bad_free;
  336. }
  337. if (n->startbit > (e->highbit - MAPSIZE)) {
  338. printk(KERN_ERR "security: ebitmap start bit (%d) is "
  339. "beyond the end of the bitmap (%Zd)\n",
  340. n->startbit, (e->highbit - MAPSIZE));
  341. goto bad_free;
  342. }
  343. rc = next_entry(&map, fp, sizeof(u64));
  344. if (rc < 0) {
  345. printk(KERN_ERR "security: ebitmap: truncated map\n");
  346. goto bad_free;
  347. }
  348. n->map = le64_to_cpu(map);
  349. if (!n->map) {
  350. printk(KERN_ERR "security: ebitmap: null map in "
  351. "ebitmap (startbit %d)\n", n->startbit);
  352. goto bad_free;
  353. }
  354. if (l) {
  355. if (n->startbit <= l->startbit) {
  356. printk(KERN_ERR "security: ebitmap: start "
  357. "bit %d comes after start bit %d\n",
  358. n->startbit, l->startbit);
  359. goto bad_free;
  360. }
  361. l->next = n;
  362. } else
  363. e->node = n;
  364. l = n;
  365. }
  366. ok:
  367. rc = 0;
  368. out:
  369. return rc;
  370. bad_free:
  371. kfree(n);
  372. bad:
  373. if (!rc)
  374. rc = -EINVAL;
  375. ebitmap_destroy(e);
  376. goto out;
  377. }