2 * Implementation of the extensible bitmap type.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
7 * Updated: Hewlett-Packard <paul@paul-moore.com>
9 * Added support to import/export the NetLabel category bitmap
11 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
14 * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com>
15 * Applied standard bit operations to improve bitmap scanning.
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/errno.h>
21 #include <net/netlabel.h>
25 #define BITS_PER_U64 (sizeof(u64) * 8)
27 static struct kmem_cache *ebitmap_node_cachep;
29 int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2)
31 struct ebitmap_node *n1, *n2;
33 if (e1->highbit != e2->highbit)
39 (n1->startbit == n2->startbit) &&
40 !memcmp(n1->maps, n2->maps, EBITMAP_SIZE / 8)) {
51 int ebitmap_cpy(struct ebitmap *dst, struct ebitmap *src)
53 struct ebitmap_node *n, *new, *prev;
59 new = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
64 new->startbit = n->startbit;
65 memcpy(new->maps, n->maps, EBITMAP_SIZE / 8);
75 dst->highbit = src->highbit;
79 #ifdef CONFIG_NETLABEL
81 * ebitmap_netlbl_export - Export an ebitmap into a NetLabel category bitmap
82 * @ebmap: the ebitmap to export
83 * @catmap: the NetLabel category bitmap
86 * Export a SELinux extensibile bitmap into a NetLabel category bitmap.
87 * Returns zero on success, negative values on error.
90 int ebitmap_netlbl_export(struct ebitmap *ebmap,
91 struct netlbl_lsm_catmap **catmap)
93 struct ebitmap_node *e_iter = ebmap->node;
105 netlbl_catmap_free(*catmap);
109 offset = e_iter->startbit;
110 for (iter = 0; iter < EBITMAP_UNIT_NUMS; iter++) {
111 e_map = e_iter->maps[iter];
113 rc = netlbl_catmap_setlong(catmap,
118 goto netlbl_export_failure;
120 offset += EBITMAP_UNIT_SIZE;
122 e_iter = e_iter->next;
127 netlbl_export_failure:
128 netlbl_catmap_free(*catmap);
133 * ebitmap_netlbl_import - Import a NetLabel category bitmap into an ebitmap
134 * @ebmap: the ebitmap to import
135 * @catmap: the NetLabel category bitmap
138 * Import a NetLabel category bitmap into a SELinux extensibile bitmap.
139 * Returns zero on success, negative values on error.
142 int ebitmap_netlbl_import(struct ebitmap *ebmap,
143 struct netlbl_lsm_catmap *catmap)
146 struct ebitmap_node *e_iter = NULL;
147 struct ebitmap_node *e_prev = NULL;
149 unsigned long bitmap;
152 rc = netlbl_catmap_getlong(catmap, &offset, &bitmap);
154 goto netlbl_import_failure;
155 if (offset == (u32)-1)
158 /* don't waste ebitmap space if the netlabel bitmap is empty */
160 offset += EBITMAP_UNIT_SIZE;
164 if (e_iter == NULL ||
165 offset >= e_iter->startbit + EBITMAP_SIZE) {
167 e_iter = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
169 goto netlbl_import_failure;
170 e_iter->startbit = offset - (offset % EBITMAP_SIZE);
172 ebmap->node = e_iter;
174 e_prev->next = e_iter;
175 ebmap->highbit = e_iter->startbit + EBITMAP_SIZE;
178 /* offset will always be aligned to an unsigned long */
179 idx = EBITMAP_NODE_INDEX(e_iter, offset);
180 e_iter->maps[idx] = bitmap;
183 offset += EBITMAP_UNIT_SIZE;
186 /* NOTE: we should never reach this return */
189 netlbl_import_failure:
190 ebitmap_destroy(ebmap);
193 #endif /* CONFIG_NETLABEL */
196 * Check to see if all the bits set in e2 are also set in e1. Optionally,
197 * if last_e2bit is non-zero, the highest set bit in e2 cannot exceed
200 int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2, u32 last_e2bit)
202 struct ebitmap_node *n1, *n2;
205 if (e1->highbit < e2->highbit)
211 while (n1 && n2 && (n1->startbit <= n2->startbit)) {
212 if (n1->startbit < n2->startbit) {
216 for (i = EBITMAP_UNIT_NUMS - 1; (i >= 0) && !n2->maps[i]; )
217 i--; /* Skip trailing NULL map entries */
218 if (last_e2bit && (i >= 0)) {
219 u32 lastsetbit = n2->startbit + i * EBITMAP_UNIT_SIZE +
221 if (lastsetbit > last_e2bit)
226 if ((n1->maps[i] & n2->maps[i]) != n2->maps[i])
241 int ebitmap_get_bit(struct ebitmap *e, unsigned long bit)
243 struct ebitmap_node *n;
245 if (e->highbit < bit)
249 while (n && (n->startbit <= bit)) {
250 if ((n->startbit + EBITMAP_SIZE) > bit)
251 return ebitmap_node_get_bit(n, bit);
258 int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value)
260 struct ebitmap_node *n, *prev, *new;
264 while (n && n->startbit <= bit) {
265 if ((n->startbit + EBITMAP_SIZE) > bit) {
267 ebitmap_node_set_bit(n, bit);
271 ebitmap_node_clr_bit(n, bit);
273 s = find_first_bit(n->maps, EBITMAP_SIZE);
274 if (s < EBITMAP_SIZE)
277 /* drop this node from the bitmap */
280 * this was the highest map
284 e->highbit = prev->startbit
290 prev->next = n->next;
293 kmem_cache_free(ebitmap_node_cachep, n);
304 new = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
308 new->startbit = bit - (bit % EBITMAP_SIZE);
309 ebitmap_node_set_bit(new, bit);
312 /* this node will be the highest map within the bitmap */
313 e->highbit = new->startbit + EBITMAP_SIZE;
316 new->next = prev->next;
326 void ebitmap_destroy(struct ebitmap *e)
328 struct ebitmap_node *n, *temp;
337 kmem_cache_free(ebitmap_node_cachep, temp);
345 int ebitmap_read(struct ebitmap *e, void *fp)
347 struct ebitmap_node *n = NULL;
348 u32 mapunit, count, startbit, index;
355 rc = next_entry(buf, fp, sizeof buf);
359 mapunit = le32_to_cpu(buf[0]);
360 e->highbit = le32_to_cpu(buf[1]);
361 count = le32_to_cpu(buf[2]);
363 if (mapunit != BITS_PER_U64) {
364 printk(KERN_ERR "SELinux: ebitmap: map size %u does not "
365 "match my size %zd (high bit was %d)\n",
366 mapunit, BITS_PER_U64, e->highbit);
370 /* round up e->highbit */
371 e->highbit += EBITMAP_SIZE - 1;
372 e->highbit -= (e->highbit % EBITMAP_SIZE);
379 if (e->highbit && !count)
382 for (i = 0; i < count; i++) {
383 rc = next_entry(&startbit, fp, sizeof(u32));
385 printk(KERN_ERR "SELinux: ebitmap: truncated map\n");
388 startbit = le32_to_cpu(startbit);
390 if (startbit & (mapunit - 1)) {
391 printk(KERN_ERR "SELinux: ebitmap start bit (%d) is "
392 "not a multiple of the map unit size (%u)\n",
396 if (startbit > e->highbit - mapunit) {
397 printk(KERN_ERR "SELinux: ebitmap start bit (%d) is "
398 "beyond the end of the bitmap (%u)\n",
399 startbit, (e->highbit - mapunit));
403 if (!n || startbit >= n->startbit + EBITMAP_SIZE) {
404 struct ebitmap_node *tmp;
405 tmp = kmem_cache_zalloc(ebitmap_node_cachep, GFP_KERNEL);
408 "SELinux: ebitmap: out of memory\n");
413 tmp->startbit = startbit - (startbit % EBITMAP_SIZE);
419 } else if (startbit <= n->startbit) {
420 printk(KERN_ERR "SELinux: ebitmap: start bit %d"
421 " comes after start bit %d\n",
422 startbit, n->startbit);
426 rc = next_entry(&map, fp, sizeof(u64));
428 printk(KERN_ERR "SELinux: ebitmap: truncated map\n");
431 map = le64_to_cpu(map);
433 index = (startbit - n->startbit) / EBITMAP_UNIT_SIZE;
435 n->maps[index++] = map & (-1UL);
436 map = EBITMAP_SHIFT_UNIT_SIZE(map);
450 int ebitmap_write(struct ebitmap *e, void *fp)
452 struct ebitmap_node *n;
456 int bit, last_bit, last_startbit, rc;
458 buf[0] = cpu_to_le32(BITS_PER_U64);
463 ebitmap_for_each_positive_bit(e, n, bit) {
464 if (rounddown(bit, (int)BITS_PER_U64) > last_startbit) {
466 last_startbit = rounddown(bit, BITS_PER_U64);
468 last_bit = roundup(bit + 1, BITS_PER_U64);
470 buf[1] = cpu_to_le32(last_bit);
471 buf[2] = cpu_to_le32(count);
473 rc = put_entry(buf, sizeof(u32), 3, fp);
478 last_startbit = INT_MIN;
479 ebitmap_for_each_positive_bit(e, n, bit) {
480 if (rounddown(bit, (int)BITS_PER_U64) > last_startbit) {
483 /* this is the very first bit */
485 last_startbit = rounddown(bit, BITS_PER_U64);
486 map = (u64)1 << (bit - last_startbit);
490 /* write the last node */
491 buf[0] = cpu_to_le32(last_startbit);
492 rc = put_entry(buf, sizeof(u32), 1, fp);
496 buf64[0] = cpu_to_le64(map);
497 rc = put_entry(buf64, sizeof(u64), 1, fp);
501 /* set up for the next node */
503 last_startbit = rounddown(bit, BITS_PER_U64);
505 map |= (u64)1 << (bit - last_startbit);
507 /* write the last node */
511 /* write the last node */
512 buf[0] = cpu_to_le32(last_startbit);
513 rc = put_entry(buf, sizeof(u32), 1, fp);
517 buf64[0] = cpu_to_le64(map);
518 rc = put_entry(buf64, sizeof(u64), 1, fp);
525 void ebitmap_cache_init(void)
527 ebitmap_node_cachep = kmem_cache_create("ebitmap_node",
528 sizeof(struct ebitmap_node),
529 0, SLAB_PANIC, NULL);
532 void ebitmap_cache_destroy(void)
534 kmem_cache_destroy(ebitmap_node_cachep);