blob: b64f2772b030194d6ff3ca55e4cc86007ec9f193 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Eric Pariseb5df9a2008-04-18 17:38:28 -04009 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Copyright (C) 2003 Tresys Technology, LLC
12 * This program is free software; you can redistribute it and/or modify
Eric Pariseb5df9a2008-04-18 17:38:28 -040013 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * the Free Software Foundation, version 2.
Yuichi Nakamura3232c112007-08-24 11:55:11 +090015 *
16 * Updated: Yuichi Nakamura <ynakam@hitachisoft.jp>
Eric Pariseb5df9a2008-04-18 17:38:28 -040017 * Tuned number of hash slots for avtab to reduce memory usage
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
20#include <linux/kernel.h>
21#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "avtab.h"
24#include "policydb.h"
25
Christoph Lametere18b8902006-12-06 20:33:20 -080026static struct kmem_cache *avtab_node_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
John Brooks33ebc1932015-03-24 16:54:17 -040028/* 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)
Yuichi Nakamura3232c112007-08-24 11:55:11 +090032{
John Brooks33ebc1932015-03-24 16:54:17 -040033 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;
Yuichi Nakamura3232c112007-08-24 11:55:11 +090065}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static struct avtab_node*
68avtab_insert_node(struct avtab *h, int hvalue,
Eric Pariseb5df9a2008-04-18 17:38:28 -040069 struct avtab_node *prev, struct avtab_node *cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 struct avtab_key *key, struct avtab_datum *datum)
71{
Eric Pariseb5df9a2008-04-18 17:38:28 -040072 struct avtab_node *newnode;
Robert P. J. Dayc3762222007-02-10 01:45:03 -080073 newnode = kmem_cache_zalloc(avtab_node_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (newnode == NULL)
75 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 newnode->key = *key;
77 newnode->datum = *datum;
78 if (prev) {
79 newnode->next = prev->next;
80 prev->next = newnode;
81 } else {
Stephen Smalleyba39db62015-03-24 16:54:16 -040082 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
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;
Stephen Smalley782ebb92005-09-03 15:55:16 -070098 u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900100 if (!h || !h->htable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return -EINVAL;
102
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900103 hvalue = avtab_hash(key, h->mask);
Stephen Smalleyba39db62015-03-24 16:54:16 -0400104 for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 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 &&
Stephen Smalley782ebb92005-09-03 15:55:16 -0700110 (specified & cur->key.specified))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 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);
Eric Pariseb5df9a2008-04-18 17:38:28 -0400124 if (!newnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 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 *
Eric Pariseb5df9a2008-04-18 17:38:28 -0400135avtab_insert_nonunique(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 int hvalue;
Vesa-Matti J Kari0c0e1862008-07-21 02:50:20 +0300138 struct avtab_node *prev, *cur;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700139 u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900141 if (!h || !h->htable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return NULL;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900143 hvalue = avtab_hash(key, h->mask);
Stephen Smalleyba39db62015-03-24 16:54:16 -0400144 for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 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 &&
Stephen Smalley782ebb92005-09-03 15:55:16 -0700150 (specified & cur->key.specified))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 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 }
Vesa-Matti J Kari0c0e1862008-07-21 02:50:20 +0300162 return avtab_insert_node(h, hvalue, prev, cur, key, datum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Stephen Smalley782ebb92005-09-03 15:55:16 -0700165struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 int hvalue;
168 struct avtab_node *cur;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700169 u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900171 if (!h || !h->htable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return NULL;
173
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900174 hvalue = avtab_hash(key, h->mask);
Stephen Smalleyba39db62015-03-24 16:54:16 -0400175 for (cur = flex_array_get_ptr(h->htable, hvalue); cur;
176 cur = cur->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (key->source_type == cur->key.source_type &&
178 key->target_type == cur->key.target_type &&
179 key->target_class == cur->key.target_class &&
Stephen Smalley782ebb92005-09-03 15:55:16 -0700180 (specified & cur->key.specified))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 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*
Stephen Smalley782ebb92005-09-03 15:55:16 -0700201avtab_search_node(struct avtab *h, struct avtab_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int hvalue;
204 struct avtab_node *cur;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700205 u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900207 if (!h || !h->htable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return NULL;
209
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900210 hvalue = avtab_hash(key, h->mask);
Stephen Smalleyba39db62015-03-24 16:54:16 -0400211 for (cur = flex_array_get_ptr(h->htable, hvalue); cur;
212 cur = cur->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (key->source_type == cur->key.source_type &&
214 key->target_type == cur->key.target_type &&
215 key->target_class == cur->key.target_class &&
Stephen Smalley782ebb92005-09-03 15:55:16 -0700216 (specified & cur->key.specified))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 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
Stephen Smalley782ebb92005-09-03 15:55:16 -0700240 specified &= ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 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 &&
Stephen Smalley782ebb92005-09-03 15:55:16 -0700245 (specified & cur->key.specified))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 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
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900269 for (i = 0; i < h->nslot; i++) {
Stephen Smalleyba39db62015-03-24 16:54:16 -0400270 cur = flex_array_get_ptr(h->htable, i);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300271 while (cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 temp = cur;
273 cur = cur->next;
274 kmem_cache_free(avtab_node_cachep, temp);
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Stephen Smalleyba39db62015-03-24 16:54:16 -0400277 flex_array_free(h->htable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 h->htable = NULL;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900279 h->nslot = 0;
280 h->mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283int avtab_init(struct avtab *h)
284{
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900285 h->htable = NULL;
286 h->nel = 0;
287 return 0;
288}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900290int avtab_alloc(struct avtab *h, u32 nrules)
291{
John Brooks33ebc1932015-03-24 16:54:17 -0400292 u32 mask = 0;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900293 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;
Eric Paris00d85c82010-10-13 17:50:19 -0400307 if (nslot > MAX_AVTAB_HASH_BUCKETS)
308 nslot = MAX_AVTAB_HASH_BUCKETS;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900309 mask = nslot - 1;
310
Stephen Smalleyba39db62015-03-24 16:54:16 -0400311 h->htable = flex_array_alloc(sizeof(struct avtab_node *), nslot,
312 GFP_KERNEL | __GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (!h->htable)
314 return -ENOMEM;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900315
316 avtab_alloc_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 h->nel = 0;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900318 h->nslot = nslot;
319 h->mask = mask;
James Morris454d9722008-02-26 20:42:02 +1100320 printk(KERN_DEBUG "SELinux: %d avtab hash slots, %d rules.\n",
321 h->nslot, nrules);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return 0;
323}
324
325void avtab_hash_eval(struct avtab *h, char *tag)
326{
327 int i, chain_len, slots_used, max_chain_len;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900328 unsigned long long chain2_len_sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct avtab_node *cur;
330
331 slots_used = 0;
332 max_chain_len = 0;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900333 chain2_len_sum = 0;
334 for (i = 0; i < h->nslot; i++) {
Stephen Smalleyba39db62015-03-24 16:54:16 -0400335 cur = flex_array_get_ptr(h->htable, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 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;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900346 chain2_len_sum += chain_len * chain_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 }
349
Eric Paris744ba352008-04-17 11:52:44 -0400350 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
Eric Parisf5269712008-05-14 11:27:45 -0400351 "longest chain length %d sum of chain length^2 %llu\n",
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900352 tag, h->nel, slots_used, h->nslot, max_chain_len,
353 chain2_len_sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Stephen Smalley782ebb92005-09-03 15:55:16 -0700356static uint16_t spec_order[] = {
357 AVTAB_ALLOWED,
358 AVTAB_AUDITDENY,
359 AVTAB_AUDITALLOW,
360 AVTAB_TRANSITION,
361 AVTAB_CHANGE,
362 AVTAB_MEMBER
363};
364
Stephen Smalley45e54212007-11-07 10:08:00 -0500365int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
Eric Pariseb5df9a2008-04-18 17:38:28 -0400366 int (*insertf)(struct avtab *a, struct avtab_key *k,
Stephen Smalley782ebb92005-09-03 15:55:16 -0700367 struct avtab_datum *d, void *p),
368 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700370 __le16 buf16[4];
371 u16 enabled;
372 __le32 buf32[7];
Stephen Smalley45e54212007-11-07 10:08:00 -0500373 u32 items, items2, val, vers = pol->policyvers;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700374 struct avtab_key key;
375 struct avtab_datum datum;
376 int i, rc;
Stephen Smalley45e54212007-11-07 10:08:00 -0500377 unsigned set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Stephen Smalley782ebb92005-09-03 15:55:16 -0700379 memset(&key, 0, sizeof(struct avtab_key));
380 memset(&datum, 0, sizeof(struct avtab_datum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Stephen Smalley782ebb92005-09-03 15:55:16 -0700382 if (vers < POLICYDB_VERSION_AVTAB) {
383 rc = next_entry(buf32, fp, sizeof(u32));
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200384 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100385 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200386 return rc;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700387 }
388 items2 = le32_to_cpu(buf32[0]);
389 if (items2 > ARRAY_SIZE(buf32)) {
James Morris454d9722008-02-26 20:42:02 +1100390 printk(KERN_ERR "SELinux: avtab: entry overflow\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200391 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700392
393 }
394 rc = next_entry(buf32, fp, sizeof(u32)*items2);
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200395 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100396 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200397 return rc;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700398 }
399 items = 0;
400
401 val = le32_to_cpu(buf32[items++]);
402 key.source_type = (u16)val;
403 if (key.source_type != val) {
Eric Paris744ba352008-04-17 11:52:44 -0400404 printk(KERN_ERR "SELinux: avtab: truncated source type\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200405 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700406 }
407 val = le32_to_cpu(buf32[items++]);
408 key.target_type = (u16)val;
409 if (key.target_type != val) {
Eric Paris744ba352008-04-17 11:52:44 -0400410 printk(KERN_ERR "SELinux: avtab: truncated target type\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200411 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700412 }
413 val = le32_to_cpu(buf32[items++]);
414 key.target_class = (u16)val;
415 if (key.target_class != val) {
Eric Paris744ba352008-04-17 11:52:44 -0400416 printk(KERN_ERR "SELinux: avtab: truncated target class\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200417 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700418 }
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))) {
Eric Paris744ba352008-04-17 11:52:44 -0400424 printk(KERN_ERR "SELinux: avtab: null entry\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200425 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700426 }
427 if ((val & AVTAB_AV) &&
428 (val & AVTAB_TYPE)) {
Eric Paris744ba352008-04-17 11:52:44 -0400429 printk(KERN_ERR "SELinux: avtab: entry has both access vectors and types\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200430 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700431 }
432
Tobias Klauser32725ad2006-01-06 00:11:23 -0800433 for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700434 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);
Eric Pariseb5df9a2008-04-18 17:38:28 -0400438 if (rc)
439 return rc;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700440 }
441 }
442
443 if (items != items2) {
Eric Paris744ba352008-04-17 11:52:44 -0400444 printk(KERN_ERR "SELinux: avtab: entry only had %d items, expected %d\n", items2, items);
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200445 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700446 }
447 return 0;
448 }
449
450 rc = next_entry(buf16, fp, sizeof(u16)*4);
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200451 if (rc) {
Eric Paris744ba352008-04-17 11:52:44 -0400452 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200453 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 items = 0;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700457 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++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Stephen Smalley45e54212007-11-07 10:08:00 -0500462 if (!policydb_type_isvalid(pol, key.source_type) ||
463 !policydb_type_isvalid(pol, key.target_type) ||
464 !policydb_class_isvalid(pol, key.target_class)) {
Eric Paris744ba352008-04-17 11:52:44 -0400465 printk(KERN_ERR "SELinux: avtab: invalid type or class\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200466 return -EINVAL;
Stephen Smalley45e54212007-11-07 10:08:00 -0500467 }
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) {
Eric Paris744ba352008-04-17 11:52:44 -0400475 printk(KERN_ERR "SELinux: avtab: more than one specifier\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200476 return -EINVAL;
Stephen Smalley45e54212007-11-07 10:08:00 -0500477 }
478
Stephen Smalley782ebb92005-09-03 15:55:16 -0700479 rc = next_entry(buf32, fp, sizeof(u32));
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200480 if (rc) {
Eric Paris744ba352008-04-17 11:52:44 -0400481 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200482 return rc;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700483 }
484 datum.data = le32_to_cpu(*buf32);
Stephen Smalley45e54212007-11-07 10:08:00 -0500485 if ((key.specified & AVTAB_TYPE) &&
486 !policydb_type_isvalid(pol, datum.data)) {
Eric Paris744ba352008-04-17 11:52:44 -0400487 printk(KERN_ERR "SELinux: avtab: invalid type\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200488 return -EINVAL;
Stephen Smalley45e54212007-11-07 10:08:00 -0500489 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700490 return insertf(a, &key, &datum, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Stephen Smalley782ebb92005-09-03 15:55:16 -0700493static 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
Stephen Smalley45e54212007-11-07 10:08:00 -0500499int avtab_read(struct avtab *a, void *fp, struct policydb *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700502 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 u32 nel, i;
504
505
506 rc = next_entry(buf, fp, sizeof(u32));
507 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100508 printk(KERN_ERR "SELinux: avtab: truncated table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 goto bad;
510 }
511 nel = le32_to_cpu(buf[0]);
512 if (!nel) {
James Morris454d9722008-02-26 20:42:02 +1100513 printk(KERN_ERR "SELinux: avtab: table is empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 rc = -EINVAL;
515 goto bad;
516 }
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900517
518 rc = avtab_alloc(a, nel);
519 if (rc)
520 goto bad;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 for (i = 0; i < nel; i++) {
Stephen Smalley45e54212007-11-07 10:08:00 -0500523 rc = avtab_read_item(a, fp, pol, avtab_insertf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (rc) {
525 if (rc == -ENOMEM)
James Morris454d9722008-02-26 20:42:02 +1100526 printk(KERN_ERR "SELinux: avtab: out of memory\n");
Stephen Smalley782ebb92005-09-03 15:55:16 -0700527 else if (rc == -EEXIST)
James Morris454d9722008-02-26 20:42:02 +1100528 printk(KERN_ERR "SELinux: avtab: duplicate entry\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 goto bad;
531 }
532 }
533
534 rc = 0;
535out:
536 return rc;
537
538bad:
539 avtab_destroy(a);
540 goto out;
541}
542
Eric Pariscee74f42010-10-13 17:50:25 -0400543int 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++) {
Stephen Smalleyba39db62015-03-24 16:54:16 -0400576 for (cur = flex_array_get_ptr(a->htable, i); cur;
577 cur = cur->next) {
Eric Pariscee74f42010-10-13 17:50:25 -0400578 rc = avtab_write_item(p, cur, fp);
579 if (rc)
580 return rc;
581 }
582 }
583
584 return rc;
585}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586void avtab_cache_init(void)
587{
588 avtab_node_cachep = kmem_cache_create("avtab_node",
589 sizeof(struct avtab_node),
Paul Mundt20c2df82007-07-20 10:11:58 +0900590 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593void avtab_cache_destroy(void)
594{
Eric Pariseb5df9a2008-04-18 17:38:28 -0400595 kmem_cache_destroy(avtab_node_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}