1 // SPDX-License-Identifier: GPL-2.0
3 * Implementation of the extensible bitmap type.
5 * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
8 * Updated: Hewlett-Packard <paul@paul-moore.com>
10 * Added support to import/export the NetLabel category bitmap
12 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
15 * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com>
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 __ro_after_init;
31 int ebitmap_cmp(const struct ebitmap *e1, const struct ebitmap *e2)
33 const 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, const struct ebitmap *src)
55 struct ebitmap_node *new, *prev;
56 const struct ebitmap_node *n;
62 new = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
67 new->startbit = n->startbit;
68 memcpy(new->maps, n->maps, EBITMAP_SIZE / 8);
78 dst->highbit = src->highbit;
82 int ebitmap_and(struct ebitmap *dst, const struct ebitmap *e1, const struct ebitmap *e2)
84 struct ebitmap_node *n;
89 ebitmap_for_each_positive_bit(e1, n, bit) {
90 if (ebitmap_get_bit(e2, bit)) {
91 rc = ebitmap_set_bit(dst, bit, 1);
100 #ifdef CONFIG_NETLABEL
102 * ebitmap_netlbl_export - Export an ebitmap into a NetLabel category bitmap
103 * @ebmap: the ebitmap to export
104 * @catmap: the NetLabel category bitmap
107 * Export a SELinux extensibile bitmap into a NetLabel category bitmap.
108 * Returns zero on success, negative values on error.
111 int ebitmap_netlbl_export(struct ebitmap *ebmap,
112 struct netlbl_lsm_catmap **catmap)
114 struct ebitmap_node *e_iter = ebmap->node;
120 if (e_iter == NULL) {
126 netlbl_catmap_free(*catmap);
130 offset = e_iter->startbit;
131 for (iter = 0; iter < EBITMAP_UNIT_NUMS; iter++) {
132 e_map = e_iter->maps[iter];
134 rc = netlbl_catmap_setlong(catmap,
139 goto netlbl_export_failure;
141 offset += EBITMAP_UNIT_SIZE;
143 e_iter = e_iter->next;
148 netlbl_export_failure:
149 netlbl_catmap_free(*catmap);
154 * ebitmap_netlbl_import - Import a NetLabel category bitmap into an ebitmap
155 * @ebmap: the ebitmap to import
156 * @catmap: the NetLabel category bitmap
159 * Import a NetLabel category bitmap into a SELinux extensibile bitmap.
160 * Returns zero on success, negative values on error.
163 int ebitmap_netlbl_import(struct ebitmap *ebmap,
164 struct netlbl_lsm_catmap *catmap)
167 struct ebitmap_node *e_iter = NULL;
168 struct ebitmap_node *e_prev = NULL;
170 unsigned long bitmap;
173 rc = netlbl_catmap_getlong(catmap, &offset, &bitmap);
175 goto netlbl_import_failure;
176 if (offset == (u32)-1)
179 /* don't waste ebitmap space if the netlabel bitmap is empty */
181 offset += EBITMAP_UNIT_SIZE;
185 if (e_iter == NULL ||
186 offset >= e_iter->startbit + EBITMAP_SIZE) {
188 e_iter = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
190 goto netlbl_import_failure;
191 e_iter->startbit = offset - (offset % EBITMAP_SIZE);
193 ebmap->node = e_iter;
195 e_prev->next = e_iter;
196 ebmap->highbit = e_iter->startbit + EBITMAP_SIZE;
199 /* offset will always be aligned to an unsigned long */
200 idx = EBITMAP_NODE_INDEX(e_iter, offset);
201 e_iter->maps[idx] = bitmap;
204 offset += EBITMAP_UNIT_SIZE;
207 /* NOTE: we should never reach this return */
210 netlbl_import_failure:
211 ebitmap_destroy(ebmap);
214 #endif /* CONFIG_NETLABEL */
217 * Check to see if all the bits set in e2 are also set in e1. Optionally,
218 * if last_e2bit is non-zero, the highest set bit in e2 cannot exceed
221 int ebitmap_contains(const struct ebitmap *e1, const struct ebitmap *e2, u32 last_e2bit)
223 const struct ebitmap_node *n1, *n2;
226 if (e1->highbit < e2->highbit)
232 while (n1 && n2 && (n1->startbit <= n2->startbit)) {
233 if (n1->startbit < n2->startbit) {
237 for (i = EBITMAP_UNIT_NUMS - 1; (i >= 0) && !n2->maps[i]; )
238 i--; /* Skip trailing NULL map entries */
239 if (last_e2bit && (i >= 0)) {
240 u32 lastsetbit = n2->startbit + i * EBITMAP_UNIT_SIZE +
242 if (lastsetbit > last_e2bit)
247 if ((n1->maps[i] & n2->maps[i]) != n2->maps[i])
262 int ebitmap_get_bit(const struct ebitmap *e, unsigned long bit)
264 const struct ebitmap_node *n;
266 if (e->highbit < bit)
270 while (n && (n->startbit <= bit)) {
271 if ((n->startbit + EBITMAP_SIZE) > bit)
272 return ebitmap_node_get_bit(n, bit);
279 int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value)
281 struct ebitmap_node *n, *prev, *new;
285 while (n && n->startbit <= bit) {
286 if ((n->startbit + EBITMAP_SIZE) > bit) {
288 ebitmap_node_set_bit(n, bit);
292 ebitmap_node_clr_bit(n, bit);
294 s = find_first_bit(n->maps, EBITMAP_SIZE);
295 if (s < EBITMAP_SIZE)
298 /* drop this node from the bitmap */
301 * this was the highest map
305 e->highbit = prev->startbit
311 prev->next = n->next;
314 kmem_cache_free(ebitmap_node_cachep, n);
325 new = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
329 new->startbit = bit - (bit % EBITMAP_SIZE);
330 ebitmap_node_set_bit(new, bit);
333 /* this node will be the highest map within the bitmap */
334 e->highbit = new->startbit + EBITMAP_SIZE;
337 new->next = prev->next;
347 void ebitmap_destroy(struct ebitmap *e)
349 struct ebitmap_node *n, *temp;
358 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(const 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);