avtab.c 12.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
/*
 * Implementation of the access vector table type.
 *
 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
 */

/* Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
 *
9
 *	Added conditional policy language extensions
L
Linus Torvalds 已提交
10 11 12
 *
 * Copyright (C) 2003 Tresys Technology, LLC
 *	This program is free software; you can redistribute it and/or modify
13
 *	it under the terms of the GNU General Public License as published by
L
Linus Torvalds 已提交
14
 *	the Free Software Foundation, version 2.
15 16
 *
 * Updated: Yuichi Nakamura <ynakam@hitachisoft.jp>
17
 *	Tuned number of hash slots for avtab to reduce memory usage
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include "avtab.h"
#include "policydb.h"

26
static struct kmem_cache *avtab_node_cachep;
L
Linus Torvalds 已提交
27

28 29 30 31 32 33
static inline int avtab_hash(struct avtab_key *keyp, u16 mask)
{
	return ((keyp->target_class + (keyp->target_type << 2) +
		 (keyp->source_type << 9)) & mask);
}

L
Linus Torvalds 已提交
34 35
static struct avtab_node*
avtab_insert_node(struct avtab *h, int hvalue,
36
		  struct avtab_node *prev, struct avtab_node *cur,
L
Linus Torvalds 已提交
37 38
		  struct avtab_key *key, struct avtab_datum *datum)
{
39
	struct avtab_node *newnode;
40
	newnode = kmem_cache_zalloc(avtab_node_cachep, GFP_KERNEL);
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	if (newnode == NULL)
		return NULL;
	newnode->key = *key;
	newnode->datum = *datum;
	if (prev) {
		newnode->next = prev->next;
		prev->next = newnode;
	} else {
		newnode->next = h->htable[hvalue];
		h->htable[hvalue] = newnode;
	}

	h->nel++;
	return newnode;
}

static int avtab_insert(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum)
{
	int hvalue;
	struct avtab_node *prev, *cur, *newnode;
61
	u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
L
Linus Torvalds 已提交
62

63
	if (!h || !h->htable)
L
Linus Torvalds 已提交
64 65
		return -EINVAL;

66
	hvalue = avtab_hash(key, h->mask);
L
Linus Torvalds 已提交
67 68 69 70 71 72
	for (prev = NULL, cur = h->htable[hvalue];
	     cur;
	     prev = cur, cur = cur->next) {
		if (key->source_type == cur->key.source_type &&
		    key->target_type == cur->key.target_type &&
		    key->target_class == cur->key.target_class &&
73
		    (specified & cur->key.specified))
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86
			return -EEXIST;
		if (key->source_type < cur->key.source_type)
			break;
		if (key->source_type == cur->key.source_type &&
		    key->target_type < cur->key.target_type)
			break;
		if (key->source_type == cur->key.source_type &&
		    key->target_type == cur->key.target_type &&
		    key->target_class < cur->key.target_class)
			break;
	}

	newnode = avtab_insert_node(h, hvalue, prev, cur, key, datum);
87
	if (!newnode)
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96 97
		return -ENOMEM;

	return 0;
}

/* Unlike avtab_insert(), this function allow multiple insertions of the same
 * key/specified mask into the table, as needed by the conditional avtab.
 * It also returns a pointer to the node inserted.
 */
struct avtab_node *
98
avtab_insert_nonunique(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum)
L
Linus Torvalds 已提交
99 100
{
	int hvalue;
101
	struct avtab_node *prev, *cur;
102
	u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
L
Linus Torvalds 已提交
103

104
	if (!h || !h->htable)
L
Linus Torvalds 已提交
105
		return NULL;
106
	hvalue = avtab_hash(key, h->mask);
L
Linus Torvalds 已提交
107 108 109 110 111 112
	for (prev = NULL, cur = h->htable[hvalue];
	     cur;
	     prev = cur, cur = cur->next) {
		if (key->source_type == cur->key.source_type &&
		    key->target_type == cur->key.target_type &&
		    key->target_class == cur->key.target_class &&
113
		    (specified & cur->key.specified))
L
Linus Torvalds 已提交
114 115 116 117 118 119 120 121 122 123 124
			break;
		if (key->source_type < cur->key.source_type)
			break;
		if (key->source_type == cur->key.source_type &&
		    key->target_type < cur->key.target_type)
			break;
		if (key->source_type == cur->key.source_type &&
		    key->target_type == cur->key.target_type &&
		    key->target_class < cur->key.target_class)
			break;
	}
125
	return avtab_insert_node(h, hvalue, prev, cur, key, datum);
L
Linus Torvalds 已提交
126 127
}

128
struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *key)
L
Linus Torvalds 已提交
129 130 131
{
	int hvalue;
	struct avtab_node *cur;
132
	u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
L
Linus Torvalds 已提交
133

134
	if (!h || !h->htable)
L
Linus Torvalds 已提交
135 136
		return NULL;

137
	hvalue = avtab_hash(key, h->mask);
L
Linus Torvalds 已提交
138 139 140 141
	for (cur = h->htable[hvalue]; cur; cur = cur->next) {
		if (key->source_type == cur->key.source_type &&
		    key->target_type == cur->key.target_type &&
		    key->target_class == cur->key.target_class &&
142
		    (specified & cur->key.specified))
L
Linus Torvalds 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
			return &cur->datum;

		if (key->source_type < cur->key.source_type)
			break;
		if (key->source_type == cur->key.source_type &&
		    key->target_type < cur->key.target_type)
			break;
		if (key->source_type == cur->key.source_type &&
		    key->target_type == cur->key.target_type &&
		    key->target_class < cur->key.target_class)
			break;
	}

	return NULL;
}

/* This search function returns a node pointer, and can be used in
 * conjunction with avtab_search_next_node()
 */
struct avtab_node*
163
avtab_search_node(struct avtab *h, struct avtab_key *key)
L
Linus Torvalds 已提交
164 165 166
{
	int hvalue;
	struct avtab_node *cur;
167
	u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
L
Linus Torvalds 已提交
168

169
	if (!h || !h->htable)
L
Linus Torvalds 已提交
170 171
		return NULL;

172
	hvalue = avtab_hash(key, h->mask);
L
Linus Torvalds 已提交
173 174 175 176
	for (cur = h->htable[hvalue]; cur; cur = cur->next) {
		if (key->source_type == cur->key.source_type &&
		    key->target_type == cur->key.target_type &&
		    key->target_class == cur->key.target_class &&
177
		    (specified & cur->key.specified))
L
Linus Torvalds 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
			return cur;

		if (key->source_type < cur->key.source_type)
			break;
		if (key->source_type == cur->key.source_type &&
		    key->target_type < cur->key.target_type)
			break;
		if (key->source_type == cur->key.source_type &&
		    key->target_type == cur->key.target_type &&
		    key->target_class < cur->key.target_class)
			break;
	}
	return NULL;
}

struct avtab_node*
avtab_search_node_next(struct avtab_node *node, int specified)
{
	struct avtab_node *cur;

	if (!node)
		return NULL;

201
	specified &= ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
L
Linus Torvalds 已提交
202 203 204 205
	for (cur = node->next; cur; cur = cur->next) {
		if (node->key.source_type == cur->key.source_type &&
		    node->key.target_type == cur->key.target_type &&
		    node->key.target_class == cur->key.target_class &&
206
		    (specified & cur->key.specified))
L
Linus Torvalds 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
			return cur;

		if (node->key.source_type < cur->key.source_type)
			break;
		if (node->key.source_type == cur->key.source_type &&
		    node->key.target_type < cur->key.target_type)
			break;
		if (node->key.source_type == cur->key.source_type &&
		    node->key.target_type == cur->key.target_type &&
		    node->key.target_class < cur->key.target_class)
			break;
	}
	return NULL;
}

void avtab_destroy(struct avtab *h)
{
	int i;
	struct avtab_node *cur, *temp;

	if (!h || !h->htable)
		return;

230
	for (i = 0; i < h->nslot; i++) {
L
Linus Torvalds 已提交
231 232 233 234 235 236 237 238
		cur = h->htable[i];
		while (cur != NULL) {
			temp = cur;
			cur = cur->next;
			kmem_cache_free(avtab_node_cachep, temp);
		}
		h->htable[i] = NULL;
	}
239
	kfree(h->htable);
L
Linus Torvalds 已提交
240
	h->htable = NULL;
241 242
	h->nslot = 0;
	h->mask = 0;
L
Linus Torvalds 已提交
243 244 245 246
}

int avtab_init(struct avtab *h)
{
247 248 249 250 251 252 253 254 255 256 257 258 259 260
	h->htable = NULL;
	h->nel = 0;
	return 0;
}

int avtab_alloc(struct avtab *h, u32 nrules)
{
	u16 mask = 0;
	u32 shift = 0;
	u32 work = nrules;
	u32 nslot = 0;

	if (nrules == 0)
		goto avtab_alloc_out;
L
Linus Torvalds 已提交
261

262 263 264 265 266 267 268 269 270 271 272 273
	while (work) {
		work  = work >> 1;
		shift++;
	}
	if (shift > 2)
		shift = shift - 2;
	nslot = 1 << shift;
	if (nslot > MAX_AVTAB_SIZE)
		nslot = MAX_AVTAB_SIZE;
	mask = nslot - 1;

	h->htable = kcalloc(nslot, sizeof(*(h->htable)), GFP_KERNEL);
L
Linus Torvalds 已提交
274 275
	if (!h->htable)
		return -ENOMEM;
276 277

 avtab_alloc_out:
L
Linus Torvalds 已提交
278
	h->nel = 0;
279 280
	h->nslot = nslot;
	h->mask = mask;
J
James Morris 已提交
281 282
	printk(KERN_DEBUG "SELinux: %d avtab hash slots, %d rules.\n",
	       h->nslot, nrules);
L
Linus Torvalds 已提交
283 284 285 286 287 288
	return 0;
}

void avtab_hash_eval(struct avtab *h, char *tag)
{
	int i, chain_len, slots_used, max_chain_len;
289
	unsigned long long chain2_len_sum;
L
Linus Torvalds 已提交
290 291 292 293
	struct avtab_node *cur;

	slots_used = 0;
	max_chain_len = 0;
294 295
	chain2_len_sum = 0;
	for (i = 0; i < h->nslot; i++) {
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303 304 305 306
		cur = h->htable[i];
		if (cur) {
			slots_used++;
			chain_len = 0;
			while (cur) {
				chain_len++;
				cur = cur->next;
			}

			if (chain_len > max_chain_len)
				max_chain_len = chain_len;
307
			chain2_len_sum += chain_len * chain_len;
L
Linus Torvalds 已提交
308 309 310
		}
	}

E
Eric Paris 已提交
311
	printk(KERN_DEBUG "SELinux: %s:  %d entries and %d/%d buckets used, "
312
	       "longest chain length %d sum of chain length^2 %llu\n",
313 314
	       tag, h->nel, slots_used, h->nslot, max_chain_len,
	       chain2_len_sum);
L
Linus Torvalds 已提交
315 316
}

317 318 319 320 321 322 323 324 325
static uint16_t spec_order[] = {
	AVTAB_ALLOWED,
	AVTAB_AUDITDENY,
	AVTAB_AUDITALLOW,
	AVTAB_TRANSITION,
	AVTAB_CHANGE,
	AVTAB_MEMBER
};

326
int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
327
		    int (*insertf)(struct avtab *a, struct avtab_key *k,
328 329
				   struct avtab_datum *d, void *p),
		    void *p)
L
Linus Torvalds 已提交
330
{
331 332 333
	__le16 buf16[4];
	u16 enabled;
	__le32 buf32[7];
334
	u32 items, items2, val, vers = pol->policyvers;
335 336 337
	struct avtab_key key;
	struct avtab_datum datum;
	int i, rc;
338
	unsigned set;
339 340 341 342 343 344 345

	memset(&key, 0, sizeof(struct avtab_key));
	memset(&datum, 0, sizeof(struct avtab_datum));

	if (vers < POLICYDB_VERSION_AVTAB) {
		rc = next_entry(buf32, fp, sizeof(u32));
		if (rc < 0) {
J
James Morris 已提交
346
			printk(KERN_ERR "SELinux: avtab: truncated entry\n");
347 348 349 350
			return -1;
		}
		items2 = le32_to_cpu(buf32[0]);
		if (items2 > ARRAY_SIZE(buf32)) {
J
James Morris 已提交
351
			printk(KERN_ERR "SELinux: avtab: entry overflow\n");
352
			return -1;
L
Linus Torvalds 已提交
353

354 355 356
		}
		rc = next_entry(buf32, fp, sizeof(u32)*items2);
		if (rc < 0) {
J
James Morris 已提交
357
			printk(KERN_ERR "SELinux: avtab: truncated entry\n");
358 359 360
			return -1;
		}
		items = 0;
L
Linus Torvalds 已提交
361

362 363 364
		val = le32_to_cpu(buf32[items++]);
		key.source_type = (u16)val;
		if (key.source_type != val) {
E
Eric Paris 已提交
365
			printk(KERN_ERR "SELinux: avtab: truncated source type\n");
366 367 368 369 370
			return -1;
		}
		val = le32_to_cpu(buf32[items++]);
		key.target_type = (u16)val;
		if (key.target_type != val) {
E
Eric Paris 已提交
371
			printk(KERN_ERR "SELinux: avtab: truncated target type\n");
372 373 374 375 376
			return -1;
		}
		val = le32_to_cpu(buf32[items++]);
		key.target_class = (u16)val;
		if (key.target_class != val) {
E
Eric Paris 已提交
377
			printk(KERN_ERR "SELinux: avtab: truncated target class\n");
378 379 380 381 382 383 384
			return -1;
		}

		val = le32_to_cpu(buf32[items++]);
		enabled = (val & AVTAB_ENABLED_OLD) ? AVTAB_ENABLED : 0;

		if (!(val & (AVTAB_AV | AVTAB_TYPE))) {
E
Eric Paris 已提交
385
			printk(KERN_ERR "SELinux: avtab: null entry\n");
386 387 388 389
			return -1;
		}
		if ((val & AVTAB_AV) &&
		    (val & AVTAB_TYPE)) {
E
Eric Paris 已提交
390
			printk(KERN_ERR "SELinux: avtab: entry has both access vectors and types\n");
391 392 393
			return -1;
		}

394
		for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
395 396 397 398
			if (val & spec_order[i]) {
				key.specified = spec_order[i] | enabled;
				datum.data = le32_to_cpu(buf32[items++]);
				rc = insertf(a, &key, &datum, p);
399 400
				if (rc)
					return rc;
401 402 403 404
			}
		}

		if (items != items2) {
E
Eric Paris 已提交
405
			printk(KERN_ERR "SELinux: avtab: entry only had %d items, expected %d\n", items2, items);
406 407 408
			return -1;
		}
		return 0;
L
Linus Torvalds 已提交
409
	}
410 411

	rc = next_entry(buf16, fp, sizeof(u16)*4);
L
Linus Torvalds 已提交
412
	if (rc < 0) {
E
Eric Paris 已提交
413
		printk(KERN_ERR "SELinux: avtab: truncated entry\n");
414
		return -1;
L
Linus Torvalds 已提交
415
	}
416

L
Linus Torvalds 已提交
417
	items = 0;
418 419 420 421 422
	key.source_type = le16_to_cpu(buf16[items++]);
	key.target_type = le16_to_cpu(buf16[items++]);
	key.target_class = le16_to_cpu(buf16[items++]);
	key.specified = le16_to_cpu(buf16[items++]);

423 424 425
	if (!policydb_type_isvalid(pol, key.source_type) ||
	    !policydb_type_isvalid(pol, key.target_type) ||
	    !policydb_class_isvalid(pol, key.target_class)) {
E
Eric Paris 已提交
426
		printk(KERN_ERR "SELinux: avtab: invalid type or class\n");
427 428 429 430 431 432 433 434 435
		return -1;
	}

	set = 0;
	for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
		if (key.specified & spec_order[i])
			set++;
	}
	if (!set || set > 1) {
E
Eric Paris 已提交
436
		printk(KERN_ERR "SELinux:  avtab:  more than one specifier\n");
437 438 439
		return -1;
	}

440 441
	rc = next_entry(buf32, fp, sizeof(u32));
	if (rc < 0) {
E
Eric Paris 已提交
442
		printk(KERN_ERR "SELinux: avtab: truncated entry\n");
443
		return -1;
L
Linus Torvalds 已提交
444
	}
445
	datum.data = le32_to_cpu(*buf32);
446 447
	if ((key.specified & AVTAB_TYPE) &&
	    !policydb_type_isvalid(pol, datum.data)) {
E
Eric Paris 已提交
448
		printk(KERN_ERR "SELinux: avtab: invalid type\n");
449 450
		return -1;
	}
451 452
	return insertf(a, &key, &datum, p);
}
L
Linus Torvalds 已提交
453

454 455 456 457
static int avtab_insertf(struct avtab *a, struct avtab_key *k,
			 struct avtab_datum *d, void *p)
{
	return avtab_insert(a, k, d);
L
Linus Torvalds 已提交
458 459
}

460
int avtab_read(struct avtab *a, void *fp, struct policydb *pol)
L
Linus Torvalds 已提交
461 462
{
	int rc;
463
	__le32 buf[1];
L
Linus Torvalds 已提交
464 465 466 467 468
	u32 nel, i;


	rc = next_entry(buf, fp, sizeof(u32));
	if (rc < 0) {
J
James Morris 已提交
469
		printk(KERN_ERR "SELinux: avtab: truncated table\n");
L
Linus Torvalds 已提交
470 471 472 473
		goto bad;
	}
	nel = le32_to_cpu(buf[0]);
	if (!nel) {
J
James Morris 已提交
474
		printk(KERN_ERR "SELinux: avtab: table is empty\n");
L
Linus Torvalds 已提交
475 476 477
		rc = -EINVAL;
		goto bad;
	}
478 479 480 481 482

	rc = avtab_alloc(a, nel);
	if (rc)
		goto bad;

L
Linus Torvalds 已提交
483
	for (i = 0; i < nel; i++) {
484
		rc = avtab_read_item(a, fp, pol, avtab_insertf, NULL);
L
Linus Torvalds 已提交
485 486
		if (rc) {
			if (rc == -ENOMEM)
J
James Morris 已提交
487
				printk(KERN_ERR "SELinux: avtab: out of memory\n");
488
			else if (rc == -EEXIST)
J
James Morris 已提交
489
				printk(KERN_ERR "SELinux: avtab: duplicate entry\n");
490 491
			else
				rc = -EINVAL;
L
Linus Torvalds 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
			goto bad;
		}
	}

	rc = 0;
out:
	return rc;

bad:
	avtab_destroy(a);
	goto out;
}

void avtab_cache_init(void)
{
	avtab_node_cachep = kmem_cache_create("avtab_node",
					      sizeof(struct avtab_node),
509
					      0, SLAB_PANIC, NULL);
L
Linus Torvalds 已提交
510 511 512 513
}

void avtab_cache_destroy(void)
{
514
	kmem_cache_destroy(avtab_node_cachep);
L
Linus Torvalds 已提交
515
}