ebitmap.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 support to import/export the NetLabel category bitmap
  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 <net/netlabel.h>
  17. #include "ebitmap.h"
  18. #include "policydb.h"
  19. int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2)
  20. {
  21. struct ebitmap_node *n1, *n2;
  22. if (e1->highbit != e2->highbit)
  23. return 0;
  24. n1 = e1->node;
  25. n2 = e2->node;
  26. while (n1 && n2 &&
  27. (n1->startbit == n2->startbit) &&
  28. (n1->map == n2->map)) {
  29. n1 = n1->next;
  30. n2 = n2->next;
  31. }
  32. if (n1 || n2)
  33. return 0;
  34. return 1;
  35. }
  36. int ebitmap_cpy(struct ebitmap *dst, struct ebitmap *src)
  37. {
  38. struct ebitmap_node *n, *new, *prev;
  39. ebitmap_init(dst);
  40. n = src->node;
  41. prev = NULL;
  42. while (n) {
  43. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  44. if (!new) {
  45. ebitmap_destroy(dst);
  46. return -ENOMEM;
  47. }
  48. new->startbit = n->startbit;
  49. new->map = n->map;
  50. new->next = NULL;
  51. if (prev)
  52. prev->next = new;
  53. else
  54. dst->node = new;
  55. prev = new;
  56. n = n->next;
  57. }
  58. dst->highbit = src->highbit;
  59. return 0;
  60. }
  61. #ifdef CONFIG_NETLABEL
  62. /**
  63. * ebitmap_netlbl_export - Export an ebitmap into a NetLabel category bitmap
  64. * @ebmap: the ebitmap to export
  65. * @catmap: the NetLabel category bitmap
  66. *
  67. * Description:
  68. * Export a SELinux extensibile bitmap into a NetLabel category bitmap.
  69. * Returns zero on success, negative values on error.
  70. *
  71. */
  72. int ebitmap_netlbl_export(struct ebitmap *ebmap,
  73. struct netlbl_lsm_secattr_catmap **catmap)
  74. {
  75. struct ebitmap_node *e_iter = ebmap->node;
  76. struct netlbl_lsm_secattr_catmap *c_iter;
  77. u32 cmap_idx;
  78. /* This function is a much simpler because SELinux's MAPTYPE happens
  79. * to be the same as NetLabel's NETLBL_CATMAP_MAPTYPE, if MAPTYPE is
  80. * changed from a u64 this function will most likely need to be changed
  81. * as well. It's not ideal but I think the tradeoff in terms of
  82. * neatness and speed is worth it. */
  83. if (e_iter == NULL) {
  84. *catmap = NULL;
  85. return 0;
  86. }
  87. c_iter = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
  88. if (c_iter == NULL)
  89. return -ENOMEM;
  90. *catmap = c_iter;
  91. c_iter->startbit = e_iter->startbit & ~(NETLBL_CATMAP_SIZE - 1);
  92. while (e_iter != NULL) {
  93. if (e_iter->startbit >=
  94. (c_iter->startbit + NETLBL_CATMAP_SIZE)) {
  95. c_iter->next = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
  96. if (c_iter->next == NULL)
  97. goto netlbl_export_failure;
  98. c_iter = c_iter->next;
  99. c_iter->startbit = e_iter->startbit &
  100. ~(NETLBL_CATMAP_SIZE - 1);
  101. }
  102. cmap_idx = (e_iter->startbit - c_iter->startbit) /
  103. NETLBL_CATMAP_MAPSIZE;
  104. c_iter->bitmap[cmap_idx] = e_iter->map;
  105. e_iter = e_iter->next;
  106. }
  107. return 0;
  108. netlbl_export_failure:
  109. netlbl_secattr_catmap_free(*catmap);
  110. return -ENOMEM;
  111. }
  112. /**
  113. * ebitmap_netlbl_import - Import a NetLabel category bitmap into an ebitmap
  114. * @ebmap: the ebitmap to export
  115. * @catmap: the NetLabel category bitmap
  116. *
  117. * Description:
  118. * Import a NetLabel category bitmap into a SELinux extensibile bitmap.
  119. * Returns zero on success, negative values on error.
  120. *
  121. */
  122. int ebitmap_netlbl_import(struct ebitmap *ebmap,
  123. struct netlbl_lsm_secattr_catmap *catmap)
  124. {
  125. struct ebitmap_node *e_iter = NULL;
  126. struct ebitmap_node *emap_prev = NULL;
  127. struct netlbl_lsm_secattr_catmap *c_iter = catmap;
  128. u32 c_idx;
  129. /* This function is a much simpler because SELinux's MAPTYPE happens
  130. * to be the same as NetLabel's NETLBL_CATMAP_MAPTYPE, if MAPTYPE is
  131. * changed from a u64 this function will most likely need to be changed
  132. * as well. It's not ideal but I think the tradeoff in terms of
  133. * neatness and speed is worth it. */
  134. do {
  135. for (c_idx = 0; c_idx < NETLBL_CATMAP_MAPCNT; c_idx++) {
  136. if (c_iter->bitmap[c_idx] == 0)
  137. continue;
  138. e_iter = kzalloc(sizeof(*e_iter), GFP_ATOMIC);
  139. if (e_iter == NULL)
  140. goto netlbl_import_failure;
  141. if (emap_prev == NULL)
  142. ebmap->node = e_iter;
  143. else
  144. emap_prev->next = e_iter;
  145. emap_prev = e_iter;
  146. e_iter->startbit = c_iter->startbit +
  147. NETLBL_CATMAP_MAPSIZE * c_idx;
  148. e_iter->map = c_iter->bitmap[c_idx];
  149. }
  150. c_iter = c_iter->next;
  151. } while (c_iter != NULL);
  152. if (e_iter != NULL)
  153. ebmap->highbit = e_iter->startbit + MAPSIZE;
  154. else
  155. ebitmap_destroy(ebmap);
  156. return 0;
  157. netlbl_import_failure:
  158. ebitmap_destroy(ebmap);
  159. return -ENOMEM;
  160. }
  161. #endif /* CONFIG_NETLABEL */
  162. int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2)
  163. {
  164. struct ebitmap_node *n1, *n2;
  165. if (e1->highbit < e2->highbit)
  166. return 0;
  167. n1 = e1->node;
  168. n2 = e2->node;
  169. while (n1 && n2 && (n1->startbit <= n2->startbit)) {
  170. if (n1->startbit < n2->startbit) {
  171. n1 = n1->next;
  172. continue;
  173. }
  174. if ((n1->map & n2->map) != n2->map)
  175. return 0;
  176. n1 = n1->next;
  177. n2 = n2->next;
  178. }
  179. if (n2)
  180. return 0;
  181. return 1;
  182. }
  183. int ebitmap_get_bit(struct ebitmap *e, unsigned long bit)
  184. {
  185. struct ebitmap_node *n;
  186. if (e->highbit < bit)
  187. return 0;
  188. n = e->node;
  189. while (n && (n->startbit <= bit)) {
  190. if ((n->startbit + MAPSIZE) > bit) {
  191. if (n->map & (MAPBIT << (bit - n->startbit)))
  192. return 1;
  193. else
  194. return 0;
  195. }
  196. n = n->next;
  197. }
  198. return 0;
  199. }
  200. int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value)
  201. {
  202. struct ebitmap_node *n, *prev, *new;
  203. prev = NULL;
  204. n = e->node;
  205. while (n && n->startbit <= bit) {
  206. if ((n->startbit + MAPSIZE) > bit) {
  207. if (value) {
  208. n->map |= (MAPBIT << (bit - n->startbit));
  209. } else {
  210. n->map &= ~(MAPBIT << (bit - n->startbit));
  211. if (!n->map) {
  212. /* drop this node from the bitmap */
  213. if (!n->next) {
  214. /*
  215. * this was the highest map
  216. * within the bitmap
  217. */
  218. if (prev)
  219. e->highbit = prev->startbit + MAPSIZE;
  220. else
  221. e->highbit = 0;
  222. }
  223. if (prev)
  224. prev->next = n->next;
  225. else
  226. e->node = n->next;
  227. kfree(n);
  228. }
  229. }
  230. return 0;
  231. }
  232. prev = n;
  233. n = n->next;
  234. }
  235. if (!value)
  236. return 0;
  237. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  238. if (!new)
  239. return -ENOMEM;
  240. new->startbit = bit & ~(MAPSIZE - 1);
  241. new->map = (MAPBIT << (bit - new->startbit));
  242. if (!n)
  243. /* this node will be the highest map within the bitmap */
  244. e->highbit = new->startbit + MAPSIZE;
  245. if (prev) {
  246. new->next = prev->next;
  247. prev->next = new;
  248. } else {
  249. new->next = e->node;
  250. e->node = new;
  251. }
  252. return 0;
  253. }
  254. void ebitmap_destroy(struct ebitmap *e)
  255. {
  256. struct ebitmap_node *n, *temp;
  257. if (!e)
  258. return;
  259. n = e->node;
  260. while (n) {
  261. temp = n;
  262. n = n->next;
  263. kfree(temp);
  264. }
  265. e->highbit = 0;
  266. e->node = NULL;
  267. return;
  268. }
  269. int ebitmap_read(struct ebitmap *e, void *fp)
  270. {
  271. int rc;
  272. struct ebitmap_node *n, *l;
  273. __le32 buf[3];
  274. u32 mapsize, count, i;
  275. __le64 map;
  276. ebitmap_init(e);
  277. rc = next_entry(buf, fp, sizeof buf);
  278. if (rc < 0)
  279. goto out;
  280. mapsize = le32_to_cpu(buf[0]);
  281. e->highbit = le32_to_cpu(buf[1]);
  282. count = le32_to_cpu(buf[2]);
  283. if (mapsize != MAPSIZE) {
  284. printk(KERN_ERR "security: ebitmap: map size %u does not "
  285. "match my size %Zd (high bit was %d)\n", mapsize,
  286. MAPSIZE, e->highbit);
  287. goto bad;
  288. }
  289. if (!e->highbit) {
  290. e->node = NULL;
  291. goto ok;
  292. }
  293. if (e->highbit & (MAPSIZE - 1)) {
  294. printk(KERN_ERR "security: ebitmap: high bit (%d) is not a "
  295. "multiple of the map size (%Zd)\n", e->highbit, MAPSIZE);
  296. goto bad;
  297. }
  298. l = NULL;
  299. for (i = 0; i < count; i++) {
  300. rc = next_entry(buf, fp, sizeof(u32));
  301. if (rc < 0) {
  302. printk(KERN_ERR "security: ebitmap: truncated map\n");
  303. goto bad;
  304. }
  305. n = kzalloc(sizeof(*n), GFP_KERNEL);
  306. if (!n) {
  307. printk(KERN_ERR "security: ebitmap: out of memory\n");
  308. rc = -ENOMEM;
  309. goto bad;
  310. }
  311. n->startbit = le32_to_cpu(buf[0]);
  312. if (n->startbit & (MAPSIZE - 1)) {
  313. printk(KERN_ERR "security: ebitmap start bit (%d) is "
  314. "not a multiple of the map size (%Zd)\n",
  315. n->startbit, MAPSIZE);
  316. goto bad_free;
  317. }
  318. if (n->startbit > (e->highbit - MAPSIZE)) {
  319. printk(KERN_ERR "security: ebitmap start bit (%d) is "
  320. "beyond the end of the bitmap (%Zd)\n",
  321. n->startbit, (e->highbit - MAPSIZE));
  322. goto bad_free;
  323. }
  324. rc = next_entry(&map, fp, sizeof(u64));
  325. if (rc < 0) {
  326. printk(KERN_ERR "security: ebitmap: truncated map\n");
  327. goto bad_free;
  328. }
  329. n->map = le64_to_cpu(map);
  330. if (!n->map) {
  331. printk(KERN_ERR "security: ebitmap: null map in "
  332. "ebitmap (startbit %d)\n", n->startbit);
  333. goto bad_free;
  334. }
  335. if (l) {
  336. if (n->startbit <= l->startbit) {
  337. printk(KERN_ERR "security: ebitmap: start "
  338. "bit %d comes after start bit %d\n",
  339. n->startbit, l->startbit);
  340. goto bad_free;
  341. }
  342. l->next = n;
  343. } else
  344. e->node = n;
  345. l = n;
  346. }
  347. ok:
  348. rc = 0;
  349. out:
  350. return rc;
  351. bad_free:
  352. kfree(n);
  353. bad:
  354. if (!rc)
  355. rc = -EINVAL;
  356. ebitmap_destroy(e);
  357. goto out;
  358. }