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