1/*
2 * Implementation of the access vector table type.
3 *
4 * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
5 */
6
7/* Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
8 *
9 *	Added conditional policy language extensions
10 *
11 * Copyright (C) 2003 Tresys Technology, LLC
12 *	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
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
18 */
19
20#include <linux/kernel.h>
21#include <linux/slab.h>
22#include <linux/errno.h>
23#include "avtab.h"
24#include "policydb.h"
25
26static struct kmem_cache *avtab_node_cachep __ro_after_init;
27static struct kmem_cache *avtab_xperms_cachep __ro_after_init;
28
29/* Based on MurmurHash3, written by Austin Appleby and placed in the
30 * public domain.
31 */
32static inline u32 avtab_hash(const struct avtab_key *keyp, u32 mask)
33{
34	static const u32 c1 = 0xcc9e2d51;
35	static const u32 c2 = 0x1b873593;
36	static const u32 r1 = 15;
37	static const u32 r2 = 13;
38	static const u32 m  = 5;
39	static const u32 n  = 0xe6546b64;
40
41	u32 hash = 0;
42
43#define mix(input) do { \
44		u32 v = input; \
45		v *= c1; \
46		v = (v << r1) | (v >> (32 - r1)); \
47		v *= c2; \
48		hash ^= v; \
49		hash = (hash << r2) | (hash >> (32 - r2)); \
50		hash = hash * m + n; \
51	} while (0)
52
53	mix(keyp->target_class);
54	mix(keyp->target_type);
55	mix(keyp->source_type);
56
57#undef mix
58
59	hash ^= hash >> 16;
60	hash *= 0x85ebca6b;
61	hash ^= hash >> 13;
62	hash *= 0xc2b2ae35;
63	hash ^= hash >> 16;
64
65	return hash & mask;
66}
67
68static struct avtab_node*
69avtab_insert_node(struct avtab *h, u32 hvalue,
70		  struct avtab_node *prev,
71		  const struct avtab_key *key, const struct avtab_datum *datum)
72{
73	struct avtab_node *newnode;
74	struct avtab_extended_perms *xperms;
75	newnode = kmem_cache_zalloc(avtab_node_cachep, GFP_KERNEL);
76	if (newnode == NULL)
77		return NULL;
78	newnode->key = *key;
79
80	if (key->specified & AVTAB_XPERMS) {
81		xperms = kmem_cache_zalloc(avtab_xperms_cachep, GFP_KERNEL);
82		if (xperms == NULL) {
83			kmem_cache_free(avtab_node_cachep, newnode);
84			return NULL;
85		}
86		*xperms = *(datum->u.xperms);
87		newnode->datum.u.xperms = xperms;
88	} else {
89		newnode->datum.u.data = datum->u.data;
90	}
91
92	if (prev) {
93		newnode->next = prev->next;
94		prev->next = newnode;
95	} else {
96		struct avtab_node **n = &h->htable[hvalue];
97
98		newnode->next = *n;
99		*n = newnode;
100	}
101
102	h->nel++;
103	return newnode;
104}
105
106static int avtab_insert(struct avtab *h, const struct avtab_key *key,
107			const struct avtab_datum *datum)
108{
109	u32 hvalue;
110	struct avtab_node *prev, *cur, *newnode;
111	u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
112
113	if (!h || !h->nslot || h->nel == U32_MAX)
114		return -EINVAL;
115
116	hvalue = avtab_hash(key, h->mask);
117	for (prev = NULL, cur = h->htable[hvalue];
118	     cur;
119	     prev = cur, cur = cur->next) {
120		if (key->source_type == cur->key.source_type &&
121		    key->target_type == cur->key.target_type &&
122		    key->target_class == cur->key.target_class &&
123		    (specified & cur->key.specified)) {
124			/* extended perms may not be unique */
125			if (specified & AVTAB_XPERMS)
126				break;
127			return -EEXIST;
128		}
129		if (key->source_type < cur->key.source_type)
130			break;
131		if (key->source_type == cur->key.source_type &&
132		    key->target_type < cur->key.target_type)
133			break;
134		if (key->source_type == cur->key.source_type &&
135		    key->target_type == cur->key.target_type &&
136		    key->target_class < cur->key.target_class)
137			break;
138	}
139
140	newnode = avtab_insert_node(h, hvalue, prev, key, datum);
141	if (!newnode)
142		return -ENOMEM;
143
144	return 0;
145}
146
147/* Unlike avtab_insert(), this function allow multiple insertions of the same
148 * key/specified mask into the table, as needed by the conditional avtab.
149 * It also returns a pointer to the node inserted.
150 */
151struct avtab_node *avtab_insert_nonunique(struct avtab *h,
152					  const struct avtab_key *key,
153					  const struct avtab_datum *datum)
154{
155	u32 hvalue;
156	struct avtab_node *prev, *cur;
157	u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
158
159	if (!h || !h->nslot || h->nel == U32_MAX)
160		return NULL;
161	hvalue = avtab_hash(key, h->mask);
162	for (prev = NULL, cur = h->htable[hvalue];
163	     cur;
164	     prev = cur, cur = cur->next) {
165		if (key->source_type == cur->key.source_type &&
166		    key->target_type == cur->key.target_type &&
167		    key->target_class == cur->key.target_class &&
168		    (specified & cur->key.specified))
169			break;
170		if (key->source_type < cur->key.source_type)
171			break;
172		if (key->source_type == cur->key.source_type &&
173		    key->target_type < cur->key.target_type)
174			break;
175		if (key->source_type == cur->key.source_type &&
176		    key->target_type == cur->key.target_type &&
177		    key->target_class < cur->key.target_class)
178			break;
179	}
180	return avtab_insert_node(h, hvalue, prev, key, datum);
181}
182
183/* This search function returns a node pointer, and can be used in
184 * conjunction with avtab_search_next_node()
185 */
186struct avtab_node *avtab_search_node(struct avtab *h,
187				     const struct avtab_key *key)
188{
189	u32 hvalue;
190	struct avtab_node *cur;
191	u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
192
193	if (!h || !h->nslot)
194		return NULL;
195
196	hvalue = avtab_hash(key, h->mask);
197	for (cur = h->htable[hvalue]; cur;
198	     cur = cur->next) {
199		if (key->source_type == cur->key.source_type &&
200		    key->target_type == cur->key.target_type &&
201		    key->target_class == cur->key.target_class &&
202		    (specified & cur->key.specified))
203			return cur;
204
205		if (key->source_type < cur->key.source_type)
206			break;
207		if (key->source_type == cur->key.source_type &&
208		    key->target_type < cur->key.target_type)
209			break;
210		if (key->source_type == cur->key.source_type &&
211		    key->target_type == cur->key.target_type &&
212		    key->target_class < cur->key.target_class)
213			break;
214	}
215	return NULL;
216}
217
218struct avtab_node*
219avtab_search_node_next(struct avtab_node *node, u16 specified)
220{
221	struct avtab_node *cur;
222
223	if (!node)
224		return NULL;
225
226	specified &= ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
227	for (cur = node->next; cur; cur = cur->next) {
228		if (node->key.source_type == cur->key.source_type &&
229		    node->key.target_type == cur->key.target_type &&
230		    node->key.target_class == cur->key.target_class &&
231		    (specified & cur->key.specified))
232			return cur;
233
234		if (node->key.source_type < cur->key.source_type)
235			break;
236		if (node->key.source_type == cur->key.source_type &&
237		    node->key.target_type < cur->key.target_type)
238			break;
239		if (node->key.source_type == cur->key.source_type &&
240		    node->key.target_type == cur->key.target_type &&
241		    node->key.target_class < cur->key.target_class)
242			break;
243	}
244	return NULL;
245}
246
247void avtab_destroy(struct avtab *h)
248{
249	u32 i;
250	struct avtab_node *cur, *temp;
251
252	if (!h)
253		return;
254
255	for (i = 0; i < h->nslot; i++) {
256		cur = h->htable[i];
257		while (cur) {
258			temp = cur;
259			cur = cur->next;
260			if (temp->key.specified & AVTAB_XPERMS)
261				kmem_cache_free(avtab_xperms_cachep,
262						temp->datum.u.xperms);
263			kmem_cache_free(avtab_node_cachep, temp);
264		}
265	}
266	kvfree(h->htable);
267	h->htable = NULL;
268	h->nel = 0;
269	h->nslot = 0;
270	h->mask = 0;
271}
272
273void avtab_init(struct avtab *h)
274{
275	h->htable = NULL;
276	h->nel = 0;
277	h->nslot = 0;
278	h->mask = 0;
279}
280
281static int avtab_alloc_common(struct avtab *h, u32 nslot)
282{
283	if (!nslot)
284		return 0;
285
286	h->htable = kvcalloc(nslot, sizeof(void *), GFP_KERNEL);
287	if (!h->htable)
288		return -ENOMEM;
289
290	h->nslot = nslot;
291	h->mask = nslot - 1;
292	return 0;
293}
294
295int avtab_alloc(struct avtab *h, u32 nrules)
296{
297	int rc;
298	u32 nslot = 0;
299
300	if (nrules != 0) {
301		u32 shift = 1;
302		u32 work = nrules >> 3;
303		while (work) {
304			work >>= 1;
305			shift++;
306		}
307		nslot = 1 << shift;
308		if (nslot > MAX_AVTAB_HASH_BUCKETS)
309			nslot = MAX_AVTAB_HASH_BUCKETS;
310
311		rc = avtab_alloc_common(h, nslot);
312		if (rc)
313			return rc;
314	}
315
316	pr_debug("SELinux: %d avtab hash slots, %d rules.\n", nslot, nrules);
317	return 0;
318}
319
320int avtab_alloc_dup(struct avtab *new, const struct avtab *orig)
321{
322	return avtab_alloc_common(new, orig->nslot);
323}
324
325#ifdef CONFIG_SECURITY_SELINUX_DEBUG
326void avtab_hash_eval(struct avtab *h, const char *tag)
327{
328	u32 i, chain_len, slots_used, max_chain_len;
329	unsigned long long chain2_len_sum;
330	struct avtab_node *cur;
331
332	slots_used = 0;
333	max_chain_len = 0;
334	chain2_len_sum = 0;
335	for (i = 0; i < h->nslot; i++) {
336		cur = h->htable[i];
337		if (cur) {
338			slots_used++;
339			chain_len = 0;
340			while (cur) {
341				chain_len++;
342				cur = cur->next;
343			}
344
345			if (chain_len > max_chain_len)
346				max_chain_len = chain_len;
347			chain2_len_sum += (unsigned long long)chain_len * chain_len;
348		}
349	}
350
351	pr_debug("SELinux: %s:  %d entries and %d/%d buckets used, "
352	       "longest chain length %d sum of chain length^2 %llu\n",
353	       tag, h->nel, slots_used, h->nslot, max_chain_len,
354	       chain2_len_sum);
355}
356#endif /* CONFIG_SECURITY_SELINUX_DEBUG */
357
358static const uint16_t spec_order[] = {
359	AVTAB_ALLOWED,
360	AVTAB_AUDITDENY,
361	AVTAB_AUDITALLOW,
362	AVTAB_TRANSITION,
363	AVTAB_CHANGE,
364	AVTAB_MEMBER,
365	AVTAB_XPERMS_ALLOWED,
366	AVTAB_XPERMS_AUDITALLOW,
367	AVTAB_XPERMS_DONTAUDIT
368};
369
370int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
371		    int (*insertf)(struct avtab *a, const struct avtab_key *k,
372				   const struct avtab_datum *d, void *p),
373		    void *p)
374{
375	__le16 buf16[4];
376	u16 enabled;
377	u32 items, items2, val, i;
378	struct avtab_key key;
379	struct avtab_datum datum;
380	struct avtab_extended_perms xperms;
381	__le32 buf32[ARRAY_SIZE(xperms.perms.p)];
382	int rc;
383	unsigned int set, vers = pol->policyvers;
384
385	memset(&key, 0, sizeof(struct avtab_key));
386	memset(&datum, 0, sizeof(struct avtab_datum));
387
388	if (vers < POLICYDB_VERSION_AVTAB) {
389		rc = next_entry(buf32, fp, sizeof(u32));
390		if (rc) {
391			pr_err("SELinux: avtab: truncated entry\n");
392			return rc;
393		}
394		items2 = le32_to_cpu(buf32[0]);
395		if (items2 > ARRAY_SIZE(buf32)) {
396			pr_err("SELinux: avtab: entry overflow\n");
397			return -EINVAL;
398
399		}
400		rc = next_entry(buf32, fp, sizeof(u32)*items2);
401		if (rc) {
402			pr_err("SELinux: avtab: truncated entry\n");
403			return rc;
404		}
405		items = 0;
406
407		val = le32_to_cpu(buf32[items++]);
408		key.source_type = (u16)val;
409		if (key.source_type != val) {
410			pr_err("SELinux: avtab: truncated source type\n");
411			return -EINVAL;
412		}
413		val = le32_to_cpu(buf32[items++]);
414		key.target_type = (u16)val;
415		if (key.target_type != val) {
416			pr_err("SELinux: avtab: truncated target type\n");
417			return -EINVAL;
418		}
419		val = le32_to_cpu(buf32[items++]);
420		key.target_class = (u16)val;
421		if (key.target_class != val) {
422			pr_err("SELinux: avtab: truncated target class\n");
423			return -EINVAL;
424		}
425
426		val = le32_to_cpu(buf32[items++]);
427		enabled = (val & AVTAB_ENABLED_OLD) ? AVTAB_ENABLED : 0;
428
429		if (!(val & (AVTAB_AV | AVTAB_TYPE))) {
430			pr_err("SELinux: avtab: null entry\n");
431			return -EINVAL;
432		}
433		if ((val & AVTAB_AV) &&
434		    (val & AVTAB_TYPE)) {
435			pr_err("SELinux: avtab: entry has both access vectors and types\n");
436			return -EINVAL;
437		}
438		if (val & AVTAB_XPERMS) {
439			pr_err("SELinux: avtab: entry has extended permissions\n");
440			return -EINVAL;
441		}
442
443		for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
444			if (val & spec_order[i]) {
445				key.specified = spec_order[i] | enabled;
446				datum.u.data = le32_to_cpu(buf32[items++]);
447				rc = insertf(a, &key, &datum, p);
448				if (rc)
449					return rc;
450			}
451		}
452
453		if (items != items2) {
454			pr_err("SELinux: avtab: entry only had %d items, expected %d\n",
455			       items2, items);
456			return -EINVAL;
457		}
458		return 0;
459	}
460
461	rc = next_entry(buf16, fp, sizeof(u16)*4);
462	if (rc) {
463		pr_err("SELinux: avtab: truncated entry\n");
464		return rc;
465	}
466
467	items = 0;
468	key.source_type = le16_to_cpu(buf16[items++]);
469	key.target_type = le16_to_cpu(buf16[items++]);
470	key.target_class = le16_to_cpu(buf16[items++]);
471	key.specified = le16_to_cpu(buf16[items++]);
472
473	if (!policydb_type_isvalid(pol, key.source_type) ||
474	    !policydb_type_isvalid(pol, key.target_type) ||
475	    !policydb_class_isvalid(pol, key.target_class)) {
476		pr_err("SELinux: avtab: invalid type or class\n");
477		return -EINVAL;
478	}
479
480	set = 0;
481	for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
482		if (key.specified & spec_order[i])
483			set++;
484	}
485	if (!set || set > 1) {
486		pr_err("SELinux:  avtab:  more than one specifier\n");
487		return -EINVAL;
488	}
489
490	if ((vers < POLICYDB_VERSION_XPERMS_IOCTL) &&
491			(key.specified & AVTAB_XPERMS)) {
492		pr_err("SELinux:  avtab:  policy version %u does not "
493				"support extended permissions rules and one "
494				"was specified\n", vers);
495		return -EINVAL;
496	} else if (key.specified & AVTAB_XPERMS) {
497		memset(&xperms, 0, sizeof(struct avtab_extended_perms));
498		rc = next_entry(&xperms.specified, fp, sizeof(u8));
499		if (rc) {
500			pr_err("SELinux: avtab: truncated entry\n");
501			return rc;
502		}
503		rc = next_entry(&xperms.driver, fp, sizeof(u8));
504		if (rc) {
505			pr_err("SELinux: avtab: truncated entry\n");
506			return rc;
507		}
508		rc = next_entry(buf32, fp, sizeof(u32)*ARRAY_SIZE(xperms.perms.p));
509		if (rc) {
510			pr_err("SELinux: avtab: truncated entry\n");
511			return rc;
512		}
513		for (i = 0; i < ARRAY_SIZE(xperms.perms.p); i++)
514			xperms.perms.p[i] = le32_to_cpu(buf32[i]);
515		datum.u.xperms = &xperms;
516	} else {
517		rc = next_entry(buf32, fp, sizeof(u32));
518		if (rc) {
519			pr_err("SELinux: avtab: truncated entry\n");
520			return rc;
521		}
522		datum.u.data = le32_to_cpu(*buf32);
523	}
524	if ((key.specified & AVTAB_TYPE) &&
525	    !policydb_type_isvalid(pol, datum.u.data)) {
526		pr_err("SELinux: avtab: invalid type\n");
527		return -EINVAL;
528	}
529	return insertf(a, &key, &datum, p);
530}
531
532static int avtab_insertf(struct avtab *a, const struct avtab_key *k,
533			 const struct avtab_datum *d, void *p)
534{
535	return avtab_insert(a, k, d);
536}
537
538int avtab_read(struct avtab *a, void *fp, struct policydb *pol)
539{
540	int rc;
541	__le32 buf[1];
542	u32 nel, i;
543
544
545	rc = next_entry(buf, fp, sizeof(u32));
546	if (rc < 0) {
547		pr_err("SELinux: avtab: truncated table\n");
548		goto bad;
549	}
550	nel = le32_to_cpu(buf[0]);
551	if (!nel) {
552		pr_err("SELinux: avtab: table is empty\n");
553		rc = -EINVAL;
554		goto bad;
555	}
556
557	rc = avtab_alloc(a, nel);
558	if (rc)
559		goto bad;
560
561	for (i = 0; i < nel; i++) {
562		rc = avtab_read_item(a, fp, pol, avtab_insertf, NULL);
563		if (rc) {
564			if (rc == -ENOMEM)
565				pr_err("SELinux: avtab: out of memory\n");
566			else if (rc == -EEXIST)
567				pr_err("SELinux: avtab: duplicate entry\n");
568
569			goto bad;
570		}
571	}
572
573	rc = 0;
574out:
575	return rc;
576
577bad:
578	avtab_destroy(a);
579	goto out;
580}
581
582int avtab_write_item(struct policydb *p, const struct avtab_node *cur, void *fp)
583{
584	__le16 buf16[4];
585	__le32 buf32[ARRAY_SIZE(cur->datum.u.xperms->perms.p)];
586	int rc;
587	unsigned int i;
588
589	buf16[0] = cpu_to_le16(cur->key.source_type);
590	buf16[1] = cpu_to_le16(cur->key.target_type);
591	buf16[2] = cpu_to_le16(cur->key.target_class);
592	buf16[3] = cpu_to_le16(cur->key.specified);
593	rc = put_entry(buf16, sizeof(u16), 4, fp);
594	if (rc)
595		return rc;
596
597	if (cur->key.specified & AVTAB_XPERMS) {
598		rc = put_entry(&cur->datum.u.xperms->specified, sizeof(u8), 1, fp);
599		if (rc)
600			return rc;
601		rc = put_entry(&cur->datum.u.xperms->driver, sizeof(u8), 1, fp);
602		if (rc)
603			return rc;
604		for (i = 0; i < ARRAY_SIZE(cur->datum.u.xperms->perms.p); i++)
605			buf32[i] = cpu_to_le32(cur->datum.u.xperms->perms.p[i]);
606		rc = put_entry(buf32, sizeof(u32),
607				ARRAY_SIZE(cur->datum.u.xperms->perms.p), fp);
608	} else {
609		buf32[0] = cpu_to_le32(cur->datum.u.data);
610		rc = put_entry(buf32, sizeof(u32), 1, fp);
611	}
612	if (rc)
613		return rc;
614	return 0;
615}
616
617int avtab_write(struct policydb *p, struct avtab *a, void *fp)
618{
619	u32 i;
620	int rc = 0;
621	struct avtab_node *cur;
622	__le32 buf[1];
623
624	buf[0] = cpu_to_le32(a->nel);
625	rc = put_entry(buf, sizeof(u32), 1, fp);
626	if (rc)
627		return rc;
628
629	for (i = 0; i < a->nslot; i++) {
630		for (cur = a->htable[i]; cur;
631		     cur = cur->next) {
632			rc = avtab_write_item(p, cur, fp);
633			if (rc)
634				return rc;
635		}
636	}
637
638	return rc;
639}
640
641void __init avtab_cache_init(void)
642{
643	avtab_node_cachep = kmem_cache_create("avtab_node",
644					      sizeof(struct avtab_node),
645					      0, SLAB_PANIC, NULL);
646	avtab_xperms_cachep = kmem_cache_create("avtab_extended_perms",
647						sizeof(struct avtab_extended_perms),
648						0, SLAB_PANIC, NULL);
649}
650