1 // SPDX-License-Identifier: GPL-2.0
3 * Implementation of the extensible bitmap type.
10 * Added support to import/export the NetLabel category bitmap
12 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
16 * Applied standard bit operations to improve bitmap scanning.
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/errno.h>
22 #include <linux/jhash.h>
23 #include <net/netlabel.h>
27 #define BITS_PER_U64 (sizeof(u64) * 8)
29 static struct kmem_cache *ebitmap_node_cachep;
31 int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2)
33 struct ebitmap_node *n1, *n2;
35 if (e1->highbit != e2->highbit)
41 (n1->startbit == n2->startbit) &&
42 !memcmp(n1->maps, n2->maps, EBITMAP_SIZE / 8)) {
53 int ebitmap_cpy(struct ebitmap *dst, struct ebitmap *src)
55 struct ebitmap_node *n, *new, *prev;
61 new = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
66 new->startbit = n->startbit;
67 memcpy(new->maps, n->maps, EBITMAP_SIZE / 8);
77 dst->highbit = src->highbit;
81 int ebitmap_and(struct ebitmap *dst, struct ebitmap *e1, struct ebitmap *e2)
83 struct ebitmap_node *n;
88 ebitmap_for_each_positive_bit(e1, n, bit) {
89 if (ebitmap_get_bit(e2, bit)) {
90 rc = ebitmap_set_bit(dst, bit, 1);
99 #ifdef CONFIG_NETLABEL
101 * ebitmap_netlbl_export - Export an ebitmap into a NetLabel category bitmap
102 * @ebmap: the ebitmap to export
103 * @catmap: the NetLabel category bitmap
106 * Export a SELinux extensibile bitmap into a NetLabel category bitmap.
107 * Returns zero on success, negative values on error.
110 int ebitmap_netlbl_export(struct ebitmap *ebmap,
111 struct netlbl_lsm_catmap **catmap)
113 struct ebitmap_node *e_iter = ebmap->node;
119 if (e_iter == NULL) {
125 netlbl_catmap_free(*catmap);
129 offset = e_iter->startbit;
130 for (iter = 0; iter < EBITMAP_UNIT_NUMS; iter++) {
131 e_map = e_iter->maps[iter];
133 rc = netlbl_catmap_setlong(catmap,
138 goto netlbl_export_failure;
140 offset += EBITMAP_UNIT_SIZE;
142 e_iter = e_iter->next;
147 netlbl_export_failure:
148 netlbl_catmap_free(*catmap);
153 * ebitmap_netlbl_import - Import a NetLabel category bitmap into an ebitmap
154 * @ebmap: the ebitmap to import
155 * @catmap: the NetLabel category bitmap
158 * Import a NetLabel category bitmap into a SELinux extensibile bitmap.
159 * Returns zero on success, negative values on error.
162 int ebitmap_netlbl_import(struct ebitmap *ebmap,
163 struct netlbl_lsm_catmap *catmap)
166 struct ebitmap_node *e_iter = NULL;
167 struct ebitmap_node *e_prev = NULL;
169 unsigned long bitmap;
172 rc = netlbl_catmap_getlong(catmap, &offset, &bitmap);
174 goto netlbl_import_failure;
175 if (offset == (u32)-1)
178 /* don't waste ebitmap space if the netlabel bitmap is empty */
180 offset += EBITMAP_UNIT_SIZE;
184 if (e_iter == NULL ||
185 offset >= e_iter->startbit + EBITMAP_SIZE) {
187 e_iter = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
189 goto netlbl_import_failure;
190 e_iter->startbit = offset - (offset % EBITMAP_SIZE);
192 ebmap->node = e_iter;
194 e_prev->next = e_iter;
195 ebmap->highbit = e_iter->startbit + EBITMAP_SIZE;
198 /* offset will always be aligned to an unsigned long */
199 idx = EBITMAP_NODE_INDEX(e_iter, offset);
200 e_iter->maps[idx] = bitmap;
203 offset += EBITMAP_UNIT_SIZE;
206 /* NOTE: we should never reach this return */
209 netlbl_import_failure:
210 ebitmap_destroy(ebmap);
213 #endif /* CONFIG_NETLABEL */
216 * Check to see if all the bits set in e2 are also set in e1. Optionally,
217 * if last_e2bit is non-zero, the highest set bit in e2 cannot exceed
220 int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2, u32 last_e2bit)
222 struct ebitmap_node *n1, *n2;
225 if (e1->highbit < e2->highbit)
231 while (n1 && n2 && (n1->startbit <= n2->startbit)) {
232 if (n1->startbit < n2->startbit) {
236 for (i = EBITMAP_UNIT_NUMS - 1; (i >= 0) && !n2->maps[i]; )
237 i--; /* Skip trailing NULL map entries */
238 if (last_e2bit && (i >= 0)) {
239 u32 lastsetbit = n2->startbit + i * EBITMAP_UNIT_SIZE +
241 if (lastsetbit > last_e2bit)
246 if ((n1->maps[i] & n2->maps[i]) != n2->maps[i])
261 int ebitmap_get_bit(struct ebitmap *e, unsigned long bit)
263 struct ebitmap_node *n;
265 if (e->highbit < bit)
269 while (n && (n->startbit <= bit)) {
270 if ((n->startbit + EBITMAP_SIZE) > bit)
271 return ebitmap_node_get_bit(n, bit);
278 int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value)
280 struct ebitmap_node *n, *prev, *new;
284 while (n && n->startbit <= bit) {
285 if ((n->startbit + EBITMAP_SIZE) > bit) {
287 ebitmap_node_set_bit(n, bit);
291 ebitmap_node_clr_bit(n, bit);
293 s = find_first_bit(n->maps, EBITMAP_SIZE);
294 if (s < EBITMAP_SIZE)
297 /* drop this node from the bitmap */
300 * this was the highest map
304 e->highbit = prev->startbit
310 prev->next = n->next;
313 kmem_cache_free(ebitmap_node_cachep, n);
324 new = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
328 new->startbit = bit - (bit % EBITMAP_SIZE);
329 ebitmap_node_set_bit(new, bit);
332 /* this node will be the highest map within the bitmap */
333 e->highbit = new->startbit + EBITMAP_SIZE;
336 new->next = prev->next;
346 void ebitmap_destroy(struct ebitmap *e)
348 struct ebitmap_node *n, *temp;
357 kmem_cache_free(ebitmap_node_cachep, temp);
365 int ebitmap_read(struct ebitmap *e, void *fp)
367 struct ebitmap_node *n = NULL;
368 u32 mapunit, count, startbit, index;
369 __le32 ebitmap_start;
377 rc = next_entry(buf, fp, sizeof buf);
381 mapunit = le32_to_cpu(buf[0]);
382 e->highbit = le32_to_cpu(buf[1]);
383 count = le32_to_cpu(buf[2]);
385 if (mapunit != BITS_PER_U64) {
386 pr_err("SELinux: ebitmap: map size %u does not "
387 "match my size %zd (high bit was %d)\n",
388 mapunit, BITS_PER_U64, e->highbit);
392 /* round up e->highbit */
393 e->highbit += EBITMAP_SIZE - 1;
394 e->highbit -= (e->highbit % EBITMAP_SIZE);
401 if (e->highbit && !count)
404 for (i = 0; i < count; i++) {
405 rc = next_entry(&ebitmap_start, fp, sizeof(u32));
407 pr_err("SELinux: ebitmap: truncated map\n");
410 startbit = le32_to_cpu(ebitmap_start);
412 if (startbit & (mapunit - 1)) {
413 pr_err("SELinux: ebitmap start bit (%d) is "
414 "not a multiple of the map unit size (%u)\n",
418 if (startbit > e->highbit - mapunit) {
419 pr_err("SELinux: ebitmap start bit (%d) is "
420 "beyond the end of the bitmap (%u)\n",
421 startbit, (e->highbit - mapunit));
425 if (!n || startbit >= n->startbit + EBITMAP_SIZE) {
426 struct ebitmap_node *tmp;
427 tmp = kmem_cache_zalloc(ebitmap_node_cachep, GFP_KERNEL);
429 pr_err("SELinux: ebitmap: out of memory\n");
434 tmp->startbit = startbit - (startbit % EBITMAP_SIZE);
440 } else if (startbit <= n->startbit) {
441 pr_err("SELinux: ebitmap: start bit %d"
442 " comes after start bit %d\n",
443 startbit, n->startbit);
447 rc = next_entry(&mapbits, fp, sizeof(u64));
449 pr_err("SELinux: ebitmap: truncated map\n");
452 map = le64_to_cpu(mapbits);
454 index = (startbit - n->startbit) / EBITMAP_UNIT_SIZE;
456 n->maps[index++] = map & (-1UL);
457 map = EBITMAP_SHIFT_UNIT_SIZE(map);
471 int ebitmap_write(struct ebitmap *e, void *fp)
473 struct ebitmap_node *n;
477 int bit, last_bit, last_startbit, rc;
479 buf[0] = cpu_to_le32(BITS_PER_U64);
484 ebitmap_for_each_positive_bit(e, n, bit) {
485 if (rounddown(bit, (int)BITS_PER_U64) > last_startbit) {
487 last_startbit = rounddown(bit, BITS_PER_U64);
489 last_bit = roundup(bit + 1, BITS_PER_U64);
491 buf[1] = cpu_to_le32(last_bit);
492 buf[2] = cpu_to_le32(count);
494 rc = put_entry(buf, sizeof(u32), 3, fp);
499 last_startbit = INT_MIN;
500 ebitmap_for_each_positive_bit(e, n, bit) {
501 if (rounddown(bit, (int)BITS_PER_U64) > last_startbit) {
504 /* this is the very first bit */
506 last_startbit = rounddown(bit, BITS_PER_U64);
507 map = (u64)1 << (bit - last_startbit);
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);
522 /* set up for the next node */
524 last_startbit = rounddown(bit, BITS_PER_U64);
526 map |= (u64)1 << (bit - last_startbit);
528 /* write the last node */
532 /* write the last node */
533 buf[0] = cpu_to_le32(last_startbit);
534 rc = put_entry(buf, sizeof(u32), 1, fp);
538 buf64[0] = cpu_to_le64(map);
539 rc = put_entry(buf64, sizeof(u64), 1, fp);
546 u32 ebitmap_hash(const struct ebitmap *e, u32 hash)
548 struct ebitmap_node *node;
550 /* need to change hash even if ebitmap is empty */
551 hash = jhash_1word(e->highbit, hash);
552 for (node = e->node; node; node = node->next) {
553 hash = jhash_1word(node->startbit, hash);
554 hash = jhash(node->maps, sizeof(node->maps), hash);
559 void __init ebitmap_cache_init(void)
561 ebitmap_node_cachep = kmem_cache_create("ebitmap_node",
562 sizeof(struct ebitmap_node),
563 0, SLAB_PANIC, NULL);