ebitmap.c 10.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * Implementation of the extensible bitmap type.
 *
 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
 */
V
Venkat Yekkirala 已提交
6
/*
7
 * Updated: Hewlett-Packard <paul@paul-moore.com>
V
Venkat Yekkirala 已提交
8
 *
9
 *      Added support to import/export the NetLabel category bitmap
V
Venkat Yekkirala 已提交
10 11 12
 *
 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
 */
13 14 15 16
/*
 * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com>
 *      Applied standard bit operations to improve bitmap scanning.
 */
V
Venkat Yekkirala 已提交
17

L
Linus Torvalds 已提交
18 19 20
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/errno.h>
21
#include <net/netlabel.h>
L
Linus Torvalds 已提交
22 23 24
#include "ebitmap.h"
#include "policydb.h"

25 26
#define BITS_PER_U64	(sizeof(u64) * 8)

L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34 35 36 37
int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2)
{
	struct ebitmap_node *n1, *n2;

	if (e1->highbit != e2->highbit)
		return 0;

	n1 = e1->node;
	n2 = e2->node;
	while (n1 && n2 &&
	       (n1->startbit == n2->startbit) &&
38
	       !memcmp(n1->maps, n2->maps, EBITMAP_SIZE / 8)) {
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
		n1 = n1->next;
		n2 = n2->next;
	}

	if (n1 || n2)
		return 0;

	return 1;
}

int ebitmap_cpy(struct ebitmap *dst, struct ebitmap *src)
{
	struct ebitmap_node *n, *new, *prev;

	ebitmap_init(dst);
	n = src->node;
	prev = NULL;
	while (n) {
J
James Morris 已提交
57
		new = kzalloc(sizeof(*new), GFP_ATOMIC);
L
Linus Torvalds 已提交
58 59 60 61 62
		if (!new) {
			ebitmap_destroy(dst);
			return -ENOMEM;
		}
		new->startbit = n->startbit;
63
		memcpy(new->maps, n->maps, EBITMAP_SIZE / 8);
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76
		new->next = NULL;
		if (prev)
			prev->next = new;
		else
			dst->node = new;
		prev = new;
		n = n->next;
	}

	dst->highbit = src->highbit;
	return 0;
}

77
#ifdef CONFIG_NETLABEL
V
Venkat Yekkirala 已提交
78
/**
79 80 81
 * ebitmap_netlbl_export - Export an ebitmap into a NetLabel category bitmap
 * @ebmap: the ebitmap to export
 * @catmap: the NetLabel category bitmap
V
Venkat Yekkirala 已提交
82 83
 *
 * Description:
84 85
 * Export a SELinux extensibile bitmap into a NetLabel category bitmap.
 * Returns zero on success, negative values on error.
V
Venkat Yekkirala 已提交
86 87
 *
 */
88
int ebitmap_netlbl_export(struct ebitmap *ebmap,
89
			  struct netlbl_lsm_catmap **catmap)
V
Venkat Yekkirala 已提交
90
{
91
	struct ebitmap_node *e_iter = ebmap->node;
92 93 94 95
	unsigned long e_map;
	u32 offset;
	unsigned int iter;
	int rc;
96 97 98

	if (e_iter == NULL) {
		*catmap = NULL;
99 100 101
		return 0;
	}

102
	if (*catmap != NULL)
103
		netlbl_catmap_free(*catmap);
104
	*catmap = NULL;
105

106
	while (e_iter) {
107 108 109 110
		offset = e_iter->startbit;
		for (iter = 0; iter < EBITMAP_UNIT_NUMS; iter++) {
			e_map = e_iter->maps[iter];
			if (e_map != 0) {
111 112 113 114
				rc = netlbl_catmap_setlong(catmap,
							   offset,
							   e_map,
							   GFP_ATOMIC);
115
				if (rc != 0)
116 117
					goto netlbl_export_failure;
			}
118
			offset += EBITMAP_UNIT_SIZE;
119
		}
120
		e_iter = e_iter->next;
121
	}
V
Venkat Yekkirala 已提交
122 123

	return 0;
124 125

netlbl_export_failure:
126
	netlbl_catmap_free(*catmap);
127
	return -ENOMEM;
V
Venkat Yekkirala 已提交
128 129 130
}

/**
131
 * ebitmap_netlbl_import - Import a NetLabel category bitmap into an ebitmap
132
 * @ebmap: the ebitmap to import
133
 * @catmap: the NetLabel category bitmap
V
Venkat Yekkirala 已提交
134 135
 *
 * Description:
136 137
 * Import a NetLabel category bitmap into a SELinux extensibile bitmap.
 * Returns zero on success, negative values on error.
V
Venkat Yekkirala 已提交
138 139
 *
 */
140
int ebitmap_netlbl_import(struct ebitmap *ebmap,
141
			  struct netlbl_lsm_catmap *catmap)
V
Venkat Yekkirala 已提交
142
{
143
	int rc;
144
	struct ebitmap_node *e_iter = NULL;
145 146 147 148 149
	struct ebitmap_node *e_prev = NULL;
	u32 offset = 0, idx;
	unsigned long bitmap;

	for (;;) {
150
		rc = netlbl_catmap_getlong(catmap, &offset, &bitmap);
151 152 153 154 155
		if (rc < 0)
			goto netlbl_import_failure;
		if (offset == (u32)-1)
			return 0;

156 157 158 159 160 161
		/* don't waste ebitmap space if the netlabel bitmap is empty */
		if (bitmap == 0) {
			offset += EBITMAP_UNIT_SIZE;
			continue;
		}

162 163 164 165 166 167 168 169 170 171 172 173
		if (e_iter == NULL ||
		    offset >= e_iter->startbit + EBITMAP_SIZE) {
			e_prev = e_iter;
			e_iter = kzalloc(sizeof(*e_iter), GFP_ATOMIC);
			if (e_iter == NULL)
				goto netlbl_import_failure;
			e_iter->startbit = offset & ~(EBITMAP_SIZE - 1);
			if (e_prev == NULL)
				ebmap->node = e_iter;
			else
				e_prev->next = e_iter;
			ebmap->highbit = e_iter->startbit + EBITMAP_SIZE;
174
		}
V
Venkat Yekkirala 已提交
175

176 177 178 179 180 181 182 183 184
		/* offset will always be aligned to an unsigned long */
		idx = EBITMAP_NODE_INDEX(e_iter, offset);
		e_iter->maps[idx] = bitmap;

		/* next */
		offset += EBITMAP_UNIT_SIZE;
	}

	/* NOTE: we should never reach this return */
V
Venkat Yekkirala 已提交
185
	return 0;
186 187 188 189

netlbl_import_failure:
	ebitmap_destroy(ebmap);
	return -ENOMEM;
V
Venkat Yekkirala 已提交
190
}
191
#endif /* CONFIG_NETLABEL */
V
Venkat Yekkirala 已提交
192

193 194 195 196 197 198
/*
 * Check to see if all the bits set in e2 are also set in e1. Optionally,
 * if last_e2bit is non-zero, the highest set bit in e2 cannot exceed
 * last_e2bit.
 */
int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2, u32 last_e2bit)
L
Linus Torvalds 已提交
199 200
{
	struct ebitmap_node *n1, *n2;
201
	int i;
L
Linus Torvalds 已提交
202 203 204 205 206 207

	if (e1->highbit < e2->highbit)
		return 0;

	n1 = e1->node;
	n2 = e2->node;
208

L
Linus Torvalds 已提交
209 210 211 212 213
	while (n1 && n2 && (n1->startbit <= n2->startbit)) {
		if (n1->startbit < n2->startbit) {
			n1 = n1->next;
			continue;
		}
214 215 216 217 218 219 220 221 222 223
		for (i = EBITMAP_UNIT_NUMS - 1; (i >= 0) && !n2->maps[i]; )
			i--;	/* Skip trailing NULL map entries */
		if (last_e2bit && (i >= 0)) {
			u32 lastsetbit = n2->startbit + i * EBITMAP_UNIT_SIZE +
					 __fls(n2->maps[i]);
			if (lastsetbit > last_e2bit)
				return 0;
		}

		while (i >= 0) {
224 225
			if ((n1->maps[i] & n2->maps[i]) != n2->maps[i])
				return 0;
226
			i--;
227
		}
L
Linus Torvalds 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

		n1 = n1->next;
		n2 = n2->next;
	}

	if (n2)
		return 0;

	return 1;
}

int ebitmap_get_bit(struct ebitmap *e, unsigned long bit)
{
	struct ebitmap_node *n;

	if (e->highbit < bit)
		return 0;

	n = e->node;
	while (n && (n->startbit <= bit)) {
248 249
		if ((n->startbit + EBITMAP_SIZE) > bit)
			return ebitmap_node_get_bit(n, bit);
L
Linus Torvalds 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262
		n = n->next;
	}

	return 0;
}

int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value)
{
	struct ebitmap_node *n, *prev, *new;

	prev = NULL;
	n = e->node;
	while (n && n->startbit <= bit) {
263
		if ((n->startbit + EBITMAP_SIZE) > bit) {
L
Linus Torvalds 已提交
264
			if (value) {
265
				ebitmap_node_set_bit(n, bit);
L
Linus Torvalds 已提交
266
			} else {
267 268 269 270 271 272 273 274 275 276 277 278 279 280
				unsigned int s;

				ebitmap_node_clr_bit(n, bit);

				s = find_first_bit(n->maps, EBITMAP_SIZE);
				if (s < EBITMAP_SIZE)
					return 0;

				/* drop this node from the bitmap */
				if (!n->next) {
					/*
					 * this was the highest map
					 * within the bitmap
					 */
L
Linus Torvalds 已提交
281
					if (prev)
282 283
						e->highbit = prev->startbit
							     + EBITMAP_SIZE;
L
Linus Torvalds 已提交
284
					else
285
						e->highbit = 0;
L
Linus Torvalds 已提交
286
				}
287 288 289 290 291
				if (prev)
					prev->next = n->next;
				else
					e->node = n->next;
				kfree(n);
L
Linus Torvalds 已提交
292 293 294 295 296 297 298 299 300 301
			}
			return 0;
		}
		prev = n;
		n = n->next;
	}

	if (!value)
		return 0;

J
James Morris 已提交
302
	new = kzalloc(sizeof(*new), GFP_ATOMIC);
L
Linus Torvalds 已提交
303 304 305
	if (!new)
		return -ENOMEM;

306 307
	new->startbit = bit - (bit % EBITMAP_SIZE);
	ebitmap_node_set_bit(new, bit);
L
Linus Torvalds 已提交
308 309 310

	if (!n)
		/* this node will be the highest map within the bitmap */
311
		e->highbit = new->startbit + EBITMAP_SIZE;
L
Linus Torvalds 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

	if (prev) {
		new->next = prev->next;
		prev->next = new;
	} else {
		new->next = e->node;
		e->node = new;
	}

	return 0;
}

void ebitmap_destroy(struct ebitmap *e)
{
	struct ebitmap_node *n, *temp;

	if (!e)
		return;

	n = e->node;
	while (n) {
		temp = n;
		n = n->next;
		kfree(temp);
	}

	e->highbit = 0;
	e->node = NULL;
	return;
}

int ebitmap_read(struct ebitmap *e, void *fp)
{
345 346 347
	struct ebitmap_node *n = NULL;
	u32 mapunit, count, startbit, index;
	u64 map;
348
	__le32 buf[3];
349
	int rc, i;
L
Linus Torvalds 已提交
350 351 352 353 354 355 356

	ebitmap_init(e);

	rc = next_entry(buf, fp, sizeof buf);
	if (rc < 0)
		goto out;

357
	mapunit = le32_to_cpu(buf[0]);
L
Linus Torvalds 已提交
358 359 360
	e->highbit = le32_to_cpu(buf[1]);
	count = le32_to_cpu(buf[2]);

361
	if (mapunit != BITS_PER_U64) {
J
James Morris 已提交
362
		printk(KERN_ERR "SELinux: ebitmap: map size %u does not "
363
		       "match my size %Zd (high bit was %d)\n",
364
		       mapunit, BITS_PER_U64, e->highbit);
L
Linus Torvalds 已提交
365 366
		goto bad;
	}
367 368 369 370 371

	/* round up e->highbit */
	e->highbit += EBITMAP_SIZE - 1;
	e->highbit -= (e->highbit % EBITMAP_SIZE);

L
Linus Torvalds 已提交
372 373 374 375
	if (!e->highbit) {
		e->node = NULL;
		goto ok;
	}
376

L
Linus Torvalds 已提交
377
	for (i = 0; i < count; i++) {
378
		rc = next_entry(&startbit, fp, sizeof(u32));
L
Linus Torvalds 已提交
379
		if (rc < 0) {
J
James Morris 已提交
380
			printk(KERN_ERR "SELinux: ebitmap: truncated map\n");
L
Linus Torvalds 已提交
381 382
			goto bad;
		}
383
		startbit = le32_to_cpu(startbit);
L
Linus Torvalds 已提交
384

385
		if (startbit & (mapunit - 1)) {
J
James Morris 已提交
386
			printk(KERN_ERR "SELinux: ebitmap start bit (%d) is "
387
			       "not a multiple of the map unit size (%u)\n",
388 389
			       startbit, mapunit);
			goto bad;
L
Linus Torvalds 已提交
390
		}
391
		if (startbit > e->highbit - mapunit) {
J
James Morris 已提交
392
			printk(KERN_ERR "SELinux: ebitmap start bit (%d) is "
393
			       "beyond the end of the bitmap (%u)\n",
394 395 396 397 398 399 400 401 402
			       startbit, (e->highbit - mapunit));
			goto bad;
		}

		if (!n || startbit >= n->startbit + EBITMAP_SIZE) {
			struct ebitmap_node *tmp;
			tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
			if (!tmp) {
				printk(KERN_ERR
J
James Morris 已提交
403
				       "SELinux: ebitmap: out of memory\n");
404 405 406 407 408
				rc = -ENOMEM;
				goto bad;
			}
			/* round down */
			tmp->startbit = startbit - (startbit % EBITMAP_SIZE);
409
			if (n)
410
				n->next = tmp;
411
			else
412 413 414
				e->node = tmp;
			n = tmp;
		} else if (startbit <= n->startbit) {
J
James Morris 已提交
415
			printk(KERN_ERR "SELinux: ebitmap: start bit %d"
416 417 418
			       " comes after start bit %d\n",
			       startbit, n->startbit);
			goto bad;
L
Linus Torvalds 已提交
419
		}
420

L
Linus Torvalds 已提交
421 422
		rc = next_entry(&map, fp, sizeof(u64));
		if (rc < 0) {
J
James Morris 已提交
423
			printk(KERN_ERR "SELinux: ebitmap: truncated map\n");
424
			goto bad;
L
Linus Torvalds 已提交
425
		}
426
		map = le64_to_cpu(map);
L
Linus Torvalds 已提交
427

428 429
		index = (startbit - n->startbit) / EBITMAP_UNIT_SIZE;
		while (map) {
430 431
			n->maps[index++] = map & (-1UL);
			map = EBITMAP_SHIFT_UNIT_SIZE(map);
L
Linus Torvalds 已提交
432 433 434 435 436 437 438 439 440 441 442 443
		}
	}
ok:
	rc = 0;
out:
	return rc;
bad:
	if (!rc)
		rc = -EINVAL;
	ebitmap_destroy(e);
	goto out;
}
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

int ebitmap_write(struct ebitmap *e, void *fp)
{
	struct ebitmap_node *n;
	u32 count;
	__le32 buf[3];
	u64 map;
	int bit, last_bit, last_startbit, rc;

	buf[0] = cpu_to_le32(BITS_PER_U64);

	count = 0;
	last_bit = 0;
	last_startbit = -1;
	ebitmap_for_each_positive_bit(e, n, bit) {
		if (rounddown(bit, (int)BITS_PER_U64) > last_startbit) {
			count++;
			last_startbit = rounddown(bit, BITS_PER_U64);
		}
		last_bit = roundup(bit + 1, BITS_PER_U64);
	}
	buf[1] = cpu_to_le32(last_bit);
	buf[2] = cpu_to_le32(count);

	rc = put_entry(buf, sizeof(u32), 3, fp);
	if (rc)
		return rc;

	map = 0;
	last_startbit = INT_MIN;
	ebitmap_for_each_positive_bit(e, n, bit) {
		if (rounddown(bit, (int)BITS_PER_U64) > last_startbit) {
			__le64 buf64[1];

			/* this is the very first bit */
			if (!map) {
				last_startbit = rounddown(bit, BITS_PER_U64);
				map = (u64)1 << (bit - last_startbit);
				continue;
			}

			/* write the last node */
			buf[0] = cpu_to_le32(last_startbit);
			rc = put_entry(buf, sizeof(u32), 1, fp);
			if (rc)
				return rc;

			buf64[0] = cpu_to_le64(map);
			rc = put_entry(buf64, sizeof(u64), 1, fp);
			if (rc)
				return rc;

			/* set up for the next node */
			map = 0;
			last_startbit = rounddown(bit, BITS_PER_U64);
		}
		map |= (u64)1 << (bit - last_startbit);
	}
	/* write the last node */
	if (map) {
		__le64 buf64[1];

		/* write the last node */
		buf[0] = cpu_to_le32(last_startbit);
		rc = put_entry(buf, sizeof(u32), 1, fp);
		if (rc)
			return rc;

		buf64[0] = cpu_to_le64(map);
		rc = put_entry(buf64, sizeof(u64), 1, fp);
		if (rc)
			return rc;
	}
	return 0;
}