powerpc/kvm: Fix ppc64_defconfig + PPC_POWERNV=n build error
[linux/fpc-iii.git] / security / selinux / ss / avtab.c
blobb64f2772b030194d6ff3ca55e4cc86007ec9f193
1 /*
2 * Implementation of the access vector table type.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
5 */
7 /* Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
9 * Added conditional policy language extensions
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.
16 * Updated: Yuichi Nakamura <ynakam@hitachisoft.jp>
17 * Tuned number of hash slots for avtab to reduce memory usage
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/errno.h>
23 #include "avtab.h"
24 #include "policydb.h"
26 static struct kmem_cache *avtab_node_cachep;
28 /* Based on MurmurHash3, written by Austin Appleby and placed in the
29 * public domain.
31 static inline int avtab_hash(struct avtab_key *keyp, u32 mask)
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;
40 u32 hash = 0;
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; \
52 mix(keyp->target_class);
53 mix(keyp->target_type);
54 mix(keyp->source_type);
56 #undef mix
58 hash ^= hash >> 16;
59 hash *= 0x85ebca6b;
60 hash ^= hash >> 13;
61 hash *= 0xc2b2ae35;
62 hash ^= hash >> 16;
64 return hash & mask;
67 static struct avtab_node*
68 avtab_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)
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;
90 h->nel++;
91 return newnode;
94 static int avtab_insert(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum)
96 int hvalue;
97 struct avtab_node *prev, *cur, *newnode;
98 u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
100 if (!h || !h->htable)
101 return -EINVAL;
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;
123 newnode = avtab_insert_node(h, hvalue, prev, cur, key, datum);
124 if (!newnode)
125 return -ENOMEM;
127 return 0;
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.
134 struct avtab_node *
135 avtab_insert_nonunique(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum)
137 int hvalue;
138 struct avtab_node *prev, *cur;
139 u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
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;
162 return avtab_insert_node(h, hvalue, prev, cur, key, datum);
165 struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *key)
167 int hvalue;
168 struct avtab_node *cur;
169 u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
171 if (!h || !h->htable)
172 return NULL;
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;
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;
194 return NULL;
197 /* This search function returns a node pointer, and can be used in
198 * conjunction with avtab_search_next_node()
200 struct avtab_node*
201 avtab_search_node(struct avtab *h, struct avtab_key *key)
203 int hvalue;
204 struct avtab_node *cur;
205 u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
207 if (!h || !h->htable)
208 return NULL;
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;
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;
229 return NULL;
232 struct avtab_node*
233 avtab_search_node_next(struct avtab_node *node, int specified)
235 struct avtab_node *cur;
237 if (!node)
238 return NULL;
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;
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;
258 return NULL;
261 void avtab_destroy(struct avtab *h)
263 int i;
264 struct avtab_node *cur, *temp;
266 if (!h || !h->htable)
267 return;
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);
277 flex_array_free(h->htable);
278 h->htable = NULL;
279 h->nslot = 0;
280 h->mask = 0;
283 int avtab_init(struct avtab *h)
285 h->htable = NULL;
286 h->nel = 0;
287 return 0;
290 int avtab_alloc(struct avtab *h, u32 nrules)
292 u32 mask = 0;
293 u32 shift = 0;
294 u32 work = nrules;
295 u32 nslot = 0;
297 if (nrules == 0)
298 goto avtab_alloc_out;
300 while (work) {
301 work = work >> 1;
302 shift++;
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;
311 h->htable = flex_array_alloc(sizeof(struct avtab_node *), nslot,
312 GFP_KERNEL | __GFP_ZERO);
313 if (!h->htable)
314 return -ENOMEM;
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;
325 void avtab_hash_eval(struct avtab *h, char *tag)
327 int i, chain_len, slots_used, max_chain_len;
328 unsigned long long chain2_len_sum;
329 struct avtab_node *cur;
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;
344 if (chain_len > max_chain_len)
345 max_chain_len = chain_len;
346 chain2_len_sum += chain_len * chain_len;
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);
356 static uint16_t spec_order[] = {
357 AVTAB_ALLOWED,
358 AVTAB_AUDITDENY,
359 AVTAB_AUDITALLOW,
360 AVTAB_TRANSITION,
361 AVTAB_CHANGE,
362 AVTAB_MEMBER
365 int 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)
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;
379 memset(&key, 0, sizeof(struct avtab_key));
380 memset(&datum, 0, sizeof(struct avtab_datum));
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;
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;
394 rc = next_entry(buf32, fp, sizeof(u32)*items2);
395 if (rc) {
396 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
397 return rc;
399 items = 0;
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;
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;
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;
420 val = le32_to_cpu(buf32[items++]);
421 enabled = (val & AVTAB_ENABLED_OLD) ? AVTAB_ENABLED : 0;
423 if (!(val & (AVTAB_AV | AVTAB_TYPE))) {
424 printk(KERN_ERR "SELinux: avtab: null entry\n");
425 return -EINVAL;
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;
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;
443 if (items != items2) {
444 printk(KERN_ERR "SELinux: avtab: entry only had %d items, expected %d\n", items2, items);
445 return -EINVAL;
447 return 0;
450 rc = next_entry(buf16, fp, sizeof(u16)*4);
451 if (rc) {
452 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
453 return rc;
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++]);
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;
469 set = 0;
470 for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
471 if (key.specified & spec_order[i])
472 set++;
474 if (!set || set > 1) {
475 printk(KERN_ERR "SELinux: avtab: more than one specifier\n");
476 return -EINVAL;
479 rc = next_entry(buf32, fp, sizeof(u32));
480 if (rc) {
481 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
482 return rc;
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;
490 return insertf(a, &key, &datum, p);
493 static int avtab_insertf(struct avtab *a, struct avtab_key *k,
494 struct avtab_datum *d, void *p)
496 return avtab_insert(a, k, d);
499 int avtab_read(struct avtab *a, void *fp, struct policydb *pol)
501 int rc;
502 __le32 buf[1];
503 u32 nel, i;
506 rc = next_entry(buf, fp, sizeof(u32));
507 if (rc < 0) {
508 printk(KERN_ERR "SELinux: avtab: truncated table\n");
509 goto bad;
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;
518 rc = avtab_alloc(a, nel);
519 if (rc)
520 goto bad;
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");
530 goto bad;
534 rc = 0;
535 out:
536 return rc;
538 bad:
539 avtab_destroy(a);
540 goto out;
543 int avtab_write_item(struct policydb *p, struct avtab_node *cur, void *fp)
545 __le16 buf16[4];
546 __le32 buf32[1];
547 int rc;
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;
563 int avtab_write(struct policydb *p, struct avtab *a, void *fp)
565 unsigned int i;
566 int rc = 0;
567 struct avtab_node *cur;
568 __le32 buf[1];
570 buf[0] = cpu_to_le32(a->nel);
571 rc = put_entry(buf, sizeof(u32), 1, fp);
572 if (rc)
573 return rc;
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;
584 return rc;
586 void avtab_cache_init(void)
588 avtab_node_cachep = kmem_cache_create("avtab_node",
589 sizeof(struct avtab_node),
590 0, SLAB_PANIC, NULL);
593 void avtab_cache_destroy(void)
595 kmem_cache_destroy(avtab_node_cachep);