2 * Implementation of the access vector table type.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
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>
26 static struct kmem_cache
*avtab_node_cachep
;
28 /* Based on MurmurHash3, written by Austin Appleby and placed in the
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;
42 #define mix(input) { \
45 v = (v << r1) | (v >> (32 - r1)); \
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
);
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
);
77 newnode
->datum
= *datum
;
79 newnode
->next
= prev
->next
;
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
);
94 static int avtab_insert(struct avtab
*h
, struct avtab_key
*key
, struct avtab_datum
*datum
)
97 struct avtab_node
*prev
, *cur
, *newnode
;
98 u16 specified
= key
->specified
& ~(AVTAB_ENABLED
|AVTAB_ENABLED_OLD
);
100 if (!h
|| !h
->htable
)
103 hvalue
= avtab_hash(key
, h
->mask
);
104 for (prev
= NULL
, cur
= flex_array_get_ptr(h
->htable
, hvalue
);
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
))
112 if (key
->source_type
< cur
->key
.source_type
)
114 if (key
->source_type
== cur
->key
.source_type
&&
115 key
->target_type
< cur
->key
.target_type
)
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
)
123 newnode
= avtab_insert_node(h
, hvalue
, prev
, cur
, key
, datum
);
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.
135 avtab_insert_nonunique(struct avtab
*h
, struct avtab_key
*key
, struct avtab_datum
*datum
)
138 struct avtab_node
*prev
, *cur
;
139 u16 specified
= key
->specified
& ~(AVTAB_ENABLED
|AVTAB_ENABLED_OLD
);
141 if (!h
|| !h
->htable
)
143 hvalue
= avtab_hash(key
, h
->mask
);
144 for (prev
= NULL
, cur
= flex_array_get_ptr(h
->htable
, hvalue
);
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
))
152 if (key
->source_type
< cur
->key
.source_type
)
154 if (key
->source_type
== cur
->key
.source_type
&&
155 key
->target_type
< cur
->key
.target_type
)
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
)
162 return avtab_insert_node(h
, hvalue
, prev
, cur
, key
, datum
);
165 struct avtab_datum
*avtab_search(struct avtab
*h
, struct avtab_key
*key
)
168 struct avtab_node
*cur
;
169 u16 specified
= key
->specified
& ~(AVTAB_ENABLED
|AVTAB_ENABLED_OLD
);
171 if (!h
|| !h
->htable
)
174 hvalue
= avtab_hash(key
, h
->mask
);
175 for (cur
= flex_array_get_ptr(h
->htable
, hvalue
); cur
;
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
))
183 if (key
->source_type
< cur
->key
.source_type
)
185 if (key
->source_type
== cur
->key
.source_type
&&
186 key
->target_type
< cur
->key
.target_type
)
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
)
197 /* This search function returns a node pointer, and can be used in
198 * conjunction with avtab_search_next_node()
201 avtab_search_node(struct avtab
*h
, struct avtab_key
*key
)
204 struct avtab_node
*cur
;
205 u16 specified
= key
->specified
& ~(AVTAB_ENABLED
|AVTAB_ENABLED_OLD
);
207 if (!h
|| !h
->htable
)
210 hvalue
= avtab_hash(key
, h
->mask
);
211 for (cur
= flex_array_get_ptr(h
->htable
, hvalue
); cur
;
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
))
219 if (key
->source_type
< cur
->key
.source_type
)
221 if (key
->source_type
== cur
->key
.source_type
&&
222 key
->target_type
< cur
->key
.target_type
)
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
)
233 avtab_search_node_next(struct avtab_node
*node
, int specified
)
235 struct avtab_node
*cur
;
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
))
248 if (node
->key
.source_type
< cur
->key
.source_type
)
250 if (node
->key
.source_type
== cur
->key
.source_type
&&
251 node
->key
.target_type
< cur
->key
.target_type
)
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
)
261 void avtab_destroy(struct avtab
*h
)
264 struct avtab_node
*cur
, *temp
;
266 if (!h
|| !h
->htable
)
269 for (i
= 0; i
< h
->nslot
; i
++) {
270 cur
= flex_array_get_ptr(h
->htable
, i
);
274 kmem_cache_free(avtab_node_cachep
, temp
);
277 flex_array_free(h
->htable
);
283 int avtab_init(struct avtab
*h
)
290 int avtab_alloc(struct avtab
*h
, u32 nrules
)
298 goto avtab_alloc_out
;
307 if (nslot
> MAX_AVTAB_HASH_BUCKETS
)
308 nslot
= MAX_AVTAB_HASH_BUCKETS
;
311 h
->htable
= flex_array_alloc(sizeof(struct avtab_node
*), nslot
,
312 GFP_KERNEL
| __GFP_ZERO
);
320 printk(KERN_DEBUG
"SELinux: %d avtab hash slots, %d rules.\n",
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
;
334 for (i
= 0; i
< h
->nslot
; i
++) {
335 cur
= flex_array_get_ptr(h
->htable
, i
);
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
,
356 static uint16_t spec_order
[] = {
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
),
373 u32 items
, items2
, val
, vers
= pol
->policyvers
;
374 struct avtab_key key
;
375 struct avtab_datum datum
;
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
));
385 printk(KERN_ERR
"SELinux: avtab: truncated entry\n");
388 items2
= le32_to_cpu(buf32
[0]);
389 if (items2
> ARRAY_SIZE(buf32
)) {
390 printk(KERN_ERR
"SELinux: avtab: entry overflow\n");
394 rc
= next_entry(buf32
, fp
, sizeof(u32
)*items2
);
396 printk(KERN_ERR
"SELinux: avtab: truncated entry\n");
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");
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");
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");
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");
427 if ((val
& AVTAB_AV
) &&
428 (val
& AVTAB_TYPE
)) {
429 printk(KERN_ERR
"SELinux: avtab: entry has both access vectors and types\n");
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
);
443 if (items
!= items2
) {
444 printk(KERN_ERR
"SELinux: avtab: entry only had %d items, expected %d\n", items2
, items
);
450 rc
= next_entry(buf16
, fp
, sizeof(u16
)*4);
452 printk(KERN_ERR
"SELinux: avtab: truncated entry\n");
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");
470 for (i
= 0; i
< ARRAY_SIZE(spec_order
); i
++) {
471 if (key
.specified
& spec_order
[i
])
474 if (!set
|| set
> 1) {
475 printk(KERN_ERR
"SELinux: avtab: more than one specifier\n");
479 rc
= next_entry(buf32
, fp
, sizeof(u32
));
481 printk(KERN_ERR
"SELinux: avtab: truncated entry\n");
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");
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
)
506 rc
= next_entry(buf
, fp
, sizeof(u32
));
508 printk(KERN_ERR
"SELinux: avtab: truncated table\n");
511 nel
= le32_to_cpu(buf
[0]);
513 printk(KERN_ERR
"SELinux: avtab: table is empty\n");
518 rc
= avtab_alloc(a
, nel
);
522 for (i
= 0; i
< nel
; i
++) {
523 rc
= avtab_read_item(a
, fp
, pol
, avtab_insertf
, NULL
);
526 printk(KERN_ERR
"SELinux: avtab: out of memory\n");
527 else if (rc
== -EEXIST
)
528 printk(KERN_ERR
"SELinux: avtab: duplicate entry\n");
543 int avtab_write_item(struct policydb
*p
, struct avtab_node
*cur
, void *fp
)
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
);
556 buf32
[0] = cpu_to_le32(cur
->datum
.data
);
557 rc
= put_entry(buf32
, sizeof(u32
), 1, fp
);
563 int avtab_write(struct policydb
*p
, struct avtab
*a
, void *fp
)
567 struct avtab_node
*cur
;
570 buf
[0] = cpu_to_le32(a
->nel
);
571 rc
= put_entry(buf
, sizeof(u32
), 1, fp
);
575 for (i
= 0; i
< a
->nslot
; i
++) {
576 for (cur
= flex_array_get_ptr(a
->htable
, i
); cur
;
578 rc
= avtab_write_item(p
, cur
, fp
);
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
);