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.
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/errno.h>
25 #define AVTAB_HASH(keyp) \
26 ((keyp->target_class + \
27 (keyp->target_type << 2) + \
28 (keyp->source_type << 9)) & \
31 static kmem_cache_t *avtab_node_cachep;
33 static struct avtab_node*
34 avtab_insert_node(struct avtab *h, int hvalue, struct avtab_node * prev, struct avtab_node * cur,
35 struct avtab_key *key, struct avtab_datum *datum)
37 struct avtab_node * newnode;
38 newnode = kmem_cache_alloc(avtab_node_cachep, SLAB_KERNEL);
41 memset(newnode, 0, sizeof(struct avtab_node));
43 newnode->datum = *datum;
45 newnode->next = prev->next;
48 newnode->next = h->htable[hvalue];
49 h->htable[hvalue] = newnode;
56 int avtab_insert(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum)
59 struct avtab_node *prev, *cur, *newnode;
64 hvalue = AVTAB_HASH(key);
65 for (prev = NULL, cur = h->htable[hvalue];
67 prev = cur, cur = cur->next) {
68 if (key->source_type == cur->key.source_type &&
69 key->target_type == cur->key.target_type &&
70 key->target_class == cur->key.target_class &&
71 (datum->specified & cur->datum.specified))
73 if (key->source_type < cur->key.source_type)
75 if (key->source_type == cur->key.source_type &&
76 key->target_type < cur->key.target_type)
78 if (key->source_type == cur->key.source_type &&
79 key->target_type == cur->key.target_type &&
80 key->target_class < cur->key.target_class)
84 newnode = avtab_insert_node(h, hvalue, prev, cur, key, datum);
91 /* Unlike avtab_insert(), this function allow multiple insertions of the same
92 * key/specified mask into the table, as needed by the conditional avtab.
93 * It also returns a pointer to the node inserted.
96 avtab_insert_nonunique(struct avtab * h, struct avtab_key * key, struct avtab_datum * datum)
99 struct avtab_node *prev, *cur, *newnode;
103 hvalue = AVTAB_HASH(key);
104 for (prev = NULL, cur = 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 (datum->specified & cur->datum.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)
122 newnode = avtab_insert_node(h, hvalue, prev, cur, key, datum);
127 struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *key, int specified)
130 struct avtab_node *cur;
135 hvalue = AVTAB_HASH(key);
136 for (cur = h->htable[hvalue]; cur; cur = cur->next) {
137 if (key->source_type == cur->key.source_type &&
138 key->target_type == cur->key.target_type &&
139 key->target_class == cur->key.target_class &&
140 (specified & cur->datum.specified))
143 if (key->source_type < cur->key.source_type)
145 if (key->source_type == cur->key.source_type &&
146 key->target_type < cur->key.target_type)
148 if (key->source_type == cur->key.source_type &&
149 key->target_type == cur->key.target_type &&
150 key->target_class < cur->key.target_class)
157 /* This search function returns a node pointer, and can be used in
158 * conjunction with avtab_search_next_node()
161 avtab_search_node(struct avtab *h, struct avtab_key *key, int specified)
164 struct avtab_node *cur;
169 hvalue = AVTAB_HASH(key);
170 for (cur = h->htable[hvalue]; cur; cur = cur->next) {
171 if (key->source_type == cur->key.source_type &&
172 key->target_type == cur->key.target_type &&
173 key->target_class == cur->key.target_class &&
174 (specified & cur->datum.specified))
177 if (key->source_type < cur->key.source_type)
179 if (key->source_type == cur->key.source_type &&
180 key->target_type < cur->key.target_type)
182 if (key->source_type == cur->key.source_type &&
183 key->target_type == cur->key.target_type &&
184 key->target_class < cur->key.target_class)
191 avtab_search_node_next(struct avtab_node *node, int specified)
193 struct avtab_node *cur;
198 for (cur = node->next; cur; cur = cur->next) {
199 if (node->key.source_type == cur->key.source_type &&
200 node->key.target_type == cur->key.target_type &&
201 node->key.target_class == cur->key.target_class &&
202 (specified & cur->datum.specified))
205 if (node->key.source_type < cur->key.source_type)
207 if (node->key.source_type == cur->key.source_type &&
208 node->key.target_type < cur->key.target_type)
210 if (node->key.source_type == cur->key.source_type &&
211 node->key.target_type == cur->key.target_type &&
212 node->key.target_class < cur->key.target_class)
218 void avtab_destroy(struct avtab *h)
221 struct avtab_node *cur, *temp;
223 if (!h || !h->htable)
226 for (i = 0; i < AVTAB_SIZE; i++) {
228 while (cur != NULL) {
231 kmem_cache_free(avtab_node_cachep, temp);
240 int avtab_map(struct avtab *h,
241 int (*apply) (struct avtab_key *k,
242 struct avtab_datum *d,
247 struct avtab_node *cur;
252 for (i = 0; i < AVTAB_SIZE; i++) {
254 while (cur != NULL) {
255 ret = apply(&cur->key, &cur->datum, args);
264 int avtab_init(struct avtab *h)
268 h->htable = vmalloc(sizeof(*(h->htable)) * AVTAB_SIZE);
271 for (i = 0; i < AVTAB_SIZE; i++)
277 void avtab_hash_eval(struct avtab *h, char *tag)
279 int i, chain_len, slots_used, max_chain_len;
280 struct avtab_node *cur;
284 for (i = 0; i < AVTAB_SIZE; i++) {
294 if (chain_len > max_chain_len)
295 max_chain_len = chain_len;
299 printk(KERN_INFO "%s: %d entries and %d/%d buckets used, longest "
300 "chain length %d\n", tag, h->nel, slots_used, AVTAB_SIZE,
304 int avtab_read_item(void *fp, struct avtab_datum *avdatum, struct avtab_key *avkey)
310 memset(avkey, 0, sizeof(struct avtab_key));
311 memset(avdatum, 0, sizeof(struct avtab_datum));
313 rc = next_entry(buf, fp, sizeof(u32));
315 printk(KERN_ERR "security: avtab: truncated entry\n");
318 items2 = le32_to_cpu(buf[0]);
319 if (items2 > ARRAY_SIZE(buf)) {
320 printk(KERN_ERR "security: avtab: entry overflow\n");
323 rc = next_entry(buf, fp, sizeof(u32)*items2);
325 printk(KERN_ERR "security: avtab: truncated entry\n");
329 avkey->source_type = le32_to_cpu(buf[items++]);
330 avkey->target_type = le32_to_cpu(buf[items++]);
331 avkey->target_class = le32_to_cpu(buf[items++]);
332 avdatum->specified = le32_to_cpu(buf[items++]);
333 if (!(avdatum->specified & (AVTAB_AV | AVTAB_TYPE))) {
334 printk(KERN_ERR "security: avtab: null entry\n");
337 if ((avdatum->specified & AVTAB_AV) &&
338 (avdatum->specified & AVTAB_TYPE)) {
339 printk(KERN_ERR "security: avtab: entry has both access vectors and types\n");
342 if (avdatum->specified & AVTAB_AV) {
343 if (avdatum->specified & AVTAB_ALLOWED)
344 avtab_allowed(avdatum) = le32_to_cpu(buf[items++]);
345 if (avdatum->specified & AVTAB_AUDITDENY)
346 avtab_auditdeny(avdatum) = le32_to_cpu(buf[items++]);
347 if (avdatum->specified & AVTAB_AUDITALLOW)
348 avtab_auditallow(avdatum) = le32_to_cpu(buf[items++]);
350 if (avdatum->specified & AVTAB_TRANSITION)
351 avtab_transition(avdatum) = le32_to_cpu(buf[items++]);
352 if (avdatum->specified & AVTAB_CHANGE)
353 avtab_change(avdatum) = le32_to_cpu(buf[items++]);
354 if (avdatum->specified & AVTAB_MEMBER)
355 avtab_member(avdatum) = le32_to_cpu(buf[items++]);
357 if (items != items2) {
358 printk(KERN_ERR "security: avtab: entry only had %d items, expected %d\n",
368 int avtab_read(struct avtab *a, void *fp, u32 config)
371 struct avtab_key avkey;
372 struct avtab_datum avdatum;
377 rc = next_entry(buf, fp, sizeof(u32));
379 printk(KERN_ERR "security: avtab: truncated table\n");
382 nel = le32_to_cpu(buf[0]);
384 printk(KERN_ERR "security: avtab: table is empty\n");
388 for (i = 0; i < nel; i++) {
389 if (avtab_read_item(fp, &avdatum, &avkey)) {
393 rc = avtab_insert(a, &avkey, &avdatum);
396 printk(KERN_ERR "security: avtab: out of memory\n");
398 printk(KERN_ERR "security: avtab: duplicate entry\n");
412 void avtab_cache_init(void)
414 avtab_node_cachep = kmem_cache_create("avtab_node",
415 sizeof(struct avtab_node),
416 0, SLAB_PANIC, NULL, NULL);
419 void avtab_cache_destroy(void)
421 kmem_cache_destroy (avtab_node_cachep);