blob: 31345376aa9b7ff78be9412c7b07866660bb9cad [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;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -040027static struct kmem_cache *avtab_xperms_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
John Brooks33ebc1932015-03-24 16:54:17 -040029/* Based on MurmurHash3, written by Austin Appleby and placed in the
30 * public domain.
31 */
32static inline int avtab_hash(struct avtab_key *keyp, u32 mask)
Yuichi Nakamura3232c112007-08-24 11:55:11 +090033{
John Brooks33ebc1932015-03-24 16:54:17 -040034 static const u32 c1 = 0xcc9e2d51;
35 static const u32 c2 = 0x1b873593;
36 static const u32 r1 = 15;
37 static const u32 r2 = 13;
38 static const u32 m = 5;
39 static const u32 n = 0xe6546b64;
40
41 u32 hash = 0;
42
43#define mix(input) { \
44 u32 v = input; \
45 v *= c1; \
46 v = (v << r1) | (v >> (32 - r1)); \
47 v *= c2; \
48 hash ^= v; \
49 hash = (hash << r2) | (hash >> (32 - r2)); \
50 hash = hash * m + n; \
51}
52
53 mix(keyp->target_class);
54 mix(keyp->target_type);
55 mix(keyp->source_type);
56
57#undef mix
58
59 hash ^= hash >> 16;
60 hash *= 0x85ebca6b;
61 hash ^= hash >> 13;
62 hash *= 0xc2b2ae35;
63 hash ^= hash >> 16;
64
65 return hash & mask;
Yuichi Nakamura3232c112007-08-24 11:55:11 +090066}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static struct avtab_node*
69avtab_insert_node(struct avtab *h, int hvalue,
Eric Pariseb5df9a2008-04-18 17:38:28 -040070 struct avtab_node *prev, struct avtab_node *cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 struct avtab_key *key, struct avtab_datum *datum)
72{
Eric Pariseb5df9a2008-04-18 17:38:28 -040073 struct avtab_node *newnode;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -040074 struct avtab_extended_perms *xperms;
Robert P. J. Dayc3762222007-02-10 01:45:03 -080075 newnode = kmem_cache_zalloc(avtab_node_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (newnode == NULL)
77 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 newnode->key = *key;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -040079
80 if (key->specified & AVTAB_XPERMS) {
81 xperms = kmem_cache_zalloc(avtab_xperms_cachep, GFP_KERNEL);
82 if (xperms == NULL) {
83 kmem_cache_free(avtab_node_cachep, newnode);
84 return NULL;
85 }
86 *xperms = *(datum->u.xperms);
87 newnode->datum.u.xperms = xperms;
88 } else {
89 newnode->datum.u.data = datum->u.data;
90 }
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (prev) {
93 newnode->next = prev->next;
94 prev->next = newnode;
95 } else {
Stephen Smalleyba39db62015-03-24 16:54:16 -040096 newnode->next = flex_array_get_ptr(h->htable, hvalue);
97 if (flex_array_put_ptr(h->htable, hvalue, newnode,
98 GFP_KERNEL|__GFP_ZERO)) {
99 kmem_cache_free(avtab_node_cachep, newnode);
100 return NULL;
101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103
104 h->nel++;
105 return newnode;
106}
107
108static int avtab_insert(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum)
109{
110 int hvalue;
111 struct avtab_node *prev, *cur, *newnode;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700112 u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900114 if (!h || !h->htable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return -EINVAL;
116
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900117 hvalue = avtab_hash(key, h->mask);
Stephen Smalleyba39db62015-03-24 16:54:16 -0400118 for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 cur;
120 prev = cur, cur = cur->next) {
121 if (key->source_type == cur->key.source_type &&
122 key->target_type == cur->key.target_type &&
123 key->target_class == cur->key.target_class &&
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400124 (specified & cur->key.specified)) {
125 /* extended perms may not be unique */
126 if (specified & AVTAB_XPERMS)
127 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return -EEXIST;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (key->source_type < cur->key.source_type)
131 break;
132 if (key->source_type == cur->key.source_type &&
133 key->target_type < cur->key.target_type)
134 break;
135 if (key->source_type == cur->key.source_type &&
136 key->target_type == cur->key.target_type &&
137 key->target_class < cur->key.target_class)
138 break;
139 }
140
141 newnode = avtab_insert_node(h, hvalue, prev, cur, key, datum);
Eric Pariseb5df9a2008-04-18 17:38:28 -0400142 if (!newnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return -ENOMEM;
144
145 return 0;
146}
147
148/* Unlike avtab_insert(), this function allow multiple insertions of the same
149 * key/specified mask into the table, as needed by the conditional avtab.
150 * It also returns a pointer to the node inserted.
151 */
152struct avtab_node *
Eric Pariseb5df9a2008-04-18 17:38:28 -0400153avtab_insert_nonunique(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 int hvalue;
Vesa-Matti J Kari0c0e1862008-07-21 02:50:20 +0300156 struct avtab_node *prev, *cur;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700157 u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900159 if (!h || !h->htable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return NULL;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900161 hvalue = avtab_hash(key, h->mask);
Stephen Smalleyba39db62015-03-24 16:54:16 -0400162 for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 cur;
164 prev = cur, cur = cur->next) {
165 if (key->source_type == cur->key.source_type &&
166 key->target_type == cur->key.target_type &&
167 key->target_class == cur->key.target_class &&
Stephen Smalley782ebb92005-09-03 15:55:16 -0700168 (specified & cur->key.specified))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
170 if (key->source_type < cur->key.source_type)
171 break;
172 if (key->source_type == cur->key.source_type &&
173 key->target_type < cur->key.target_type)
174 break;
175 if (key->source_type == cur->key.source_type &&
176 key->target_type == cur->key.target_type &&
177 key->target_class < cur->key.target_class)
178 break;
179 }
Vesa-Matti J Kari0c0e1862008-07-21 02:50:20 +0300180 return avtab_insert_node(h, hvalue, prev, cur, key, datum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Stephen Smalley782ebb92005-09-03 15:55:16 -0700183struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int hvalue;
186 struct avtab_node *cur;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700187 u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900189 if (!h || !h->htable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return NULL;
191
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900192 hvalue = avtab_hash(key, h->mask);
Stephen Smalleyba39db62015-03-24 16:54:16 -0400193 for (cur = flex_array_get_ptr(h->htable, hvalue); cur;
194 cur = cur->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (key->source_type == cur->key.source_type &&
196 key->target_type == cur->key.target_type &&
197 key->target_class == cur->key.target_class &&
Stephen Smalley782ebb92005-09-03 15:55:16 -0700198 (specified & cur->key.specified))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return &cur->datum;
200
201 if (key->source_type < cur->key.source_type)
202 break;
203 if (key->source_type == cur->key.source_type &&
204 key->target_type < cur->key.target_type)
205 break;
206 if (key->source_type == cur->key.source_type &&
207 key->target_type == cur->key.target_type &&
208 key->target_class < cur->key.target_class)
209 break;
210 }
211
212 return NULL;
213}
214
215/* This search function returns a node pointer, and can be used in
216 * conjunction with avtab_search_next_node()
217 */
218struct avtab_node*
Stephen Smalley782ebb92005-09-03 15:55:16 -0700219avtab_search_node(struct avtab *h, struct avtab_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 int hvalue;
222 struct avtab_node *cur;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700223 u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900225 if (!h || !h->htable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return NULL;
227
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900228 hvalue = avtab_hash(key, h->mask);
Stephen Smalleyba39db62015-03-24 16:54:16 -0400229 for (cur = flex_array_get_ptr(h->htable, hvalue); cur;
230 cur = cur->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (key->source_type == cur->key.source_type &&
232 key->target_type == cur->key.target_type &&
233 key->target_class == cur->key.target_class &&
Stephen Smalley782ebb92005-09-03 15:55:16 -0700234 (specified & cur->key.specified))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return cur;
236
237 if (key->source_type < cur->key.source_type)
238 break;
239 if (key->source_type == cur->key.source_type &&
240 key->target_type < cur->key.target_type)
241 break;
242 if (key->source_type == cur->key.source_type &&
243 key->target_type == cur->key.target_type &&
244 key->target_class < cur->key.target_class)
245 break;
246 }
247 return NULL;
248}
249
250struct avtab_node*
251avtab_search_node_next(struct avtab_node *node, int specified)
252{
253 struct avtab_node *cur;
254
255 if (!node)
256 return NULL;
257
Stephen Smalley782ebb92005-09-03 15:55:16 -0700258 specified &= ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 for (cur = node->next; cur; cur = cur->next) {
260 if (node->key.source_type == cur->key.source_type &&
261 node->key.target_type == cur->key.target_type &&
262 node->key.target_class == cur->key.target_class &&
Stephen Smalley782ebb92005-09-03 15:55:16 -0700263 (specified & cur->key.specified))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return cur;
265
266 if (node->key.source_type < cur->key.source_type)
267 break;
268 if (node->key.source_type == cur->key.source_type &&
269 node->key.target_type < cur->key.target_type)
270 break;
271 if (node->key.source_type == cur->key.source_type &&
272 node->key.target_type == cur->key.target_type &&
273 node->key.target_class < cur->key.target_class)
274 break;
275 }
276 return NULL;
277}
278
279void avtab_destroy(struct avtab *h)
280{
281 int i;
282 struct avtab_node *cur, *temp;
283
284 if (!h || !h->htable)
285 return;
286
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900287 for (i = 0; i < h->nslot; i++) {
Stephen Smalleyba39db62015-03-24 16:54:16 -0400288 cur = flex_array_get_ptr(h->htable, i);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300289 while (cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 temp = cur;
291 cur = cur->next;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400292 if (temp->key.specified & AVTAB_XPERMS)
293 kmem_cache_free(avtab_xperms_cachep,
294 temp->datum.u.xperms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 kmem_cache_free(avtab_node_cachep, temp);
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Stephen Smalleyba39db62015-03-24 16:54:16 -0400298 flex_array_free(h->htable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 h->htable = NULL;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900300 h->nslot = 0;
301 h->mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304int avtab_init(struct avtab *h)
305{
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900306 h->htable = NULL;
307 h->nel = 0;
308 return 0;
309}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900311int avtab_alloc(struct avtab *h, u32 nrules)
312{
John Brooks33ebc1932015-03-24 16:54:17 -0400313 u32 mask = 0;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900314 u32 shift = 0;
315 u32 work = nrules;
316 u32 nslot = 0;
317
318 if (nrules == 0)
319 goto avtab_alloc_out;
320
321 while (work) {
322 work = work >> 1;
323 shift++;
324 }
325 if (shift > 2)
326 shift = shift - 2;
327 nslot = 1 << shift;
Eric Paris00d85c82010-10-13 17:50:19 -0400328 if (nslot > MAX_AVTAB_HASH_BUCKETS)
329 nslot = MAX_AVTAB_HASH_BUCKETS;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900330 mask = nslot - 1;
331
Stephen Smalleyba39db62015-03-24 16:54:16 -0400332 h->htable = flex_array_alloc(sizeof(struct avtab_node *), nslot,
333 GFP_KERNEL | __GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (!h->htable)
335 return -ENOMEM;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900336
337 avtab_alloc_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 h->nel = 0;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900339 h->nslot = nslot;
340 h->mask = mask;
James Morris454d9722008-02-26 20:42:02 +1100341 printk(KERN_DEBUG "SELinux: %d avtab hash slots, %d rules.\n",
342 h->nslot, nrules);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344}
345
346void avtab_hash_eval(struct avtab *h, char *tag)
347{
348 int i, chain_len, slots_used, max_chain_len;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900349 unsigned long long chain2_len_sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct avtab_node *cur;
351
352 slots_used = 0;
353 max_chain_len = 0;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900354 chain2_len_sum = 0;
355 for (i = 0; i < h->nslot; i++) {
Stephen Smalleyba39db62015-03-24 16:54:16 -0400356 cur = flex_array_get_ptr(h->htable, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (cur) {
358 slots_used++;
359 chain_len = 0;
360 while (cur) {
361 chain_len++;
362 cur = cur->next;
363 }
364
365 if (chain_len > max_chain_len)
366 max_chain_len = chain_len;
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900367 chain2_len_sum += chain_len * chain_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 }
370
Eric Paris744ba352008-04-17 11:52:44 -0400371 printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, "
Eric Parisf5269712008-05-14 11:27:45 -0400372 "longest chain length %d sum of chain length^2 %llu\n",
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900373 tag, h->nel, slots_used, h->nslot, max_chain_len,
374 chain2_len_sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Jeff Vander Stoep099f0062015-10-22 09:30:40 -0700377/*
378 * extended permissions compatibility. Make ToT Android kernels compatible
379 * with Android M releases
380 */
381#define AVTAB_OPTYPE_ALLOWED 0x1000
382#define AVTAB_OPTYPE_AUDITALLOW 0x2000
383#define AVTAB_OPTYPE_DONTAUDIT 0x4000
384#define AVTAB_OPTYPE (AVTAB_OPTYPE_ALLOWED | \
385 AVTAB_OPTYPE_AUDITALLOW | \
386 AVTAB_OPTYPE_DONTAUDIT)
387#define AVTAB_XPERMS_OPTYPE 4
388
389#define avtab_xperms_to_optype(x) (x << AVTAB_XPERMS_OPTYPE)
390#define avtab_optype_to_xperms(x) (x >> AVTAB_XPERMS_OPTYPE)
391
392static unsigned int avtab_android_m_compat;
393
394static void avtab_android_m_compat_set(void)
395{
396 if (!avtab_android_m_compat) {
397 pr_info("SELinux: Android master kernel running Android"
398 " M policy in compatibility mode.\n");
399 avtab_android_m_compat = 1;
400 }
401}
402
Stephen Smalley782ebb92005-09-03 15:55:16 -0700403static uint16_t spec_order[] = {
404 AVTAB_ALLOWED,
405 AVTAB_AUDITDENY,
406 AVTAB_AUDITALLOW,
407 AVTAB_TRANSITION,
408 AVTAB_CHANGE,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400409 AVTAB_MEMBER,
410 AVTAB_XPERMS_ALLOWED,
411 AVTAB_XPERMS_AUDITALLOW,
412 AVTAB_XPERMS_DONTAUDIT
Stephen Smalley782ebb92005-09-03 15:55:16 -0700413};
414
Stephen Smalley45e54212007-11-07 10:08:00 -0500415int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
Eric Pariseb5df9a2008-04-18 17:38:28 -0400416 int (*insertf)(struct avtab *a, struct avtab_key *k,
Stephen Smalley782ebb92005-09-03 15:55:16 -0700417 struct avtab_datum *d, void *p),
418 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700420 __le16 buf16[4];
421 u16 enabled;
Stephen Smalley45e54212007-11-07 10:08:00 -0500422 u32 items, items2, val, vers = pol->policyvers;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700423 struct avtab_key key;
424 struct avtab_datum datum;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400425 struct avtab_extended_perms xperms;
426 __le32 buf32[ARRAY_SIZE(xperms.perms.p)];
Jeff Vander Stoep099f0062015-10-22 09:30:40 -0700427 unsigned int android_m_compat_optype = 0;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700428 int i, rc;
Stephen Smalley45e54212007-11-07 10:08:00 -0500429 unsigned set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Stephen Smalley782ebb92005-09-03 15:55:16 -0700431 memset(&key, 0, sizeof(struct avtab_key));
432 memset(&datum, 0, sizeof(struct avtab_datum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Stephen Smalley782ebb92005-09-03 15:55:16 -0700434 if (vers < POLICYDB_VERSION_AVTAB) {
435 rc = next_entry(buf32, fp, sizeof(u32));
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200436 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100437 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200438 return rc;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700439 }
440 items2 = le32_to_cpu(buf32[0]);
441 if (items2 > ARRAY_SIZE(buf32)) {
James Morris454d9722008-02-26 20:42:02 +1100442 printk(KERN_ERR "SELinux: avtab: entry overflow\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200443 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700444
445 }
446 rc = next_entry(buf32, fp, sizeof(u32)*items2);
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200447 if (rc) {
James Morris454d9722008-02-26 20:42:02 +1100448 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200449 return rc;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700450 }
451 items = 0;
452
453 val = le32_to_cpu(buf32[items++]);
454 key.source_type = (u16)val;
455 if (key.source_type != val) {
Eric Paris744ba352008-04-17 11:52:44 -0400456 printk(KERN_ERR "SELinux: avtab: truncated source type\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200457 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700458 }
459 val = le32_to_cpu(buf32[items++]);
460 key.target_type = (u16)val;
461 if (key.target_type != val) {
Eric Paris744ba352008-04-17 11:52:44 -0400462 printk(KERN_ERR "SELinux: avtab: truncated target type\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200463 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700464 }
465 val = le32_to_cpu(buf32[items++]);
466 key.target_class = (u16)val;
467 if (key.target_class != val) {
Eric Paris744ba352008-04-17 11:52:44 -0400468 printk(KERN_ERR "SELinux: avtab: truncated target class\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200469 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700470 }
471
472 val = le32_to_cpu(buf32[items++]);
473 enabled = (val & AVTAB_ENABLED_OLD) ? AVTAB_ENABLED : 0;
474
475 if (!(val & (AVTAB_AV | AVTAB_TYPE))) {
Eric Paris744ba352008-04-17 11:52:44 -0400476 printk(KERN_ERR "SELinux: avtab: null entry\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200477 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700478 }
479 if ((val & AVTAB_AV) &&
480 (val & AVTAB_TYPE)) {
Eric Paris744ba352008-04-17 11:52:44 -0400481 printk(KERN_ERR "SELinux: avtab: entry has both access vectors and types\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200482 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700483 }
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400484 if (val & AVTAB_XPERMS) {
485 printk(KERN_ERR "SELinux: avtab: entry has extended permissions\n");
486 return -EINVAL;
487 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700488
Tobias Klauser32725ad2006-01-06 00:11:23 -0800489 for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700490 if (val & spec_order[i]) {
491 key.specified = spec_order[i] | enabled;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400492 datum.u.data = le32_to_cpu(buf32[items++]);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700493 rc = insertf(a, &key, &datum, p);
Eric Pariseb5df9a2008-04-18 17:38:28 -0400494 if (rc)
495 return rc;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700496 }
497 }
498
499 if (items != items2) {
Eric Paris744ba352008-04-17 11:52:44 -0400500 printk(KERN_ERR "SELinux: avtab: entry only had %d items, expected %d\n", items2, items);
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200501 return -EINVAL;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700502 }
503 return 0;
504 }
505
506 rc = next_entry(buf16, fp, sizeof(u16)*4);
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200507 if (rc) {
Eric Paris744ba352008-04-17 11:52:44 -0400508 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200509 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 items = 0;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700513 key.source_type = le16_to_cpu(buf16[items++]);
514 key.target_type = le16_to_cpu(buf16[items++]);
515 key.target_class = le16_to_cpu(buf16[items++]);
516 key.specified = le16_to_cpu(buf16[items++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Jeff Vander Stoep099f0062015-10-22 09:30:40 -0700518 if ((key.specified & AVTAB_OPTYPE) &&
519 (vers == POLICYDB_VERSION_XPERMS_IOCTL)) {
520 key.specified = avtab_optype_to_xperms(key.specified);
521 android_m_compat_optype = 1;
522 avtab_android_m_compat_set();
523 }
524
Stephen Smalley45e54212007-11-07 10:08:00 -0500525 if (!policydb_type_isvalid(pol, key.source_type) ||
526 !policydb_type_isvalid(pol, key.target_type) ||
527 !policydb_class_isvalid(pol, key.target_class)) {
Eric Paris744ba352008-04-17 11:52:44 -0400528 printk(KERN_ERR "SELinux: avtab: invalid type or class\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200529 return -EINVAL;
Stephen Smalley45e54212007-11-07 10:08:00 -0500530 }
531
532 set = 0;
533 for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
534 if (key.specified & spec_order[i])
535 set++;
536 }
537 if (!set || set > 1) {
Eric Paris744ba352008-04-17 11:52:44 -0400538 printk(KERN_ERR "SELinux: avtab: more than one specifier\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200539 return -EINVAL;
Stephen Smalley45e54212007-11-07 10:08:00 -0500540 }
541
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400542 if ((vers < POLICYDB_VERSION_XPERMS_IOCTL) &&
543 (key.specified & AVTAB_XPERMS)) {
544 printk(KERN_ERR "SELinux: avtab: policy version %u does not "
545 "support extended permissions rules and one "
546 "was specified\n", vers);
547 return -EINVAL;
548 } else if (key.specified & AVTAB_XPERMS) {
549 memset(&xperms, 0, sizeof(struct avtab_extended_perms));
550 rc = next_entry(&xperms.specified, fp, sizeof(u8));
551 if (rc) {
552 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
553 return rc;
554 }
Jeff Vander Stoep099f0062015-10-22 09:30:40 -0700555 if (avtab_android_m_compat ||
556 ((xperms.specified != AVTAB_XPERMS_IOCTLFUNCTION) &&
557 (xperms.specified != AVTAB_XPERMS_IOCTLDRIVER) &&
558 (vers == POLICYDB_VERSION_XPERMS_IOCTL))) {
559 xperms.driver = xperms.specified;
560 if (android_m_compat_optype)
561 xperms.specified = AVTAB_XPERMS_IOCTLDRIVER;
562 else
563 xperms.specified = AVTAB_XPERMS_IOCTLFUNCTION;
564 avtab_android_m_compat_set();
565 } else {
566 rc = next_entry(&xperms.driver, fp, sizeof(u8));
567 if (rc) {
568 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
569 return rc;
570 }
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400571 }
572 rc = next_entry(buf32, fp, sizeof(u32)*ARRAY_SIZE(xperms.perms.p));
573 if (rc) {
574 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
575 return rc;
576 }
577 for (i = 0; i < ARRAY_SIZE(xperms.perms.p); i++)
578 xperms.perms.p[i] = le32_to_cpu(buf32[i]);
579 datum.u.xperms = &xperms;
580 } else {
581 rc = next_entry(buf32, fp, sizeof(u32));
582 if (rc) {
583 printk(KERN_ERR "SELinux: avtab: truncated entry\n");
584 return rc;
585 }
586 datum.u.data = le32_to_cpu(*buf32);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700587 }
Stephen Smalley45e54212007-11-07 10:08:00 -0500588 if ((key.specified & AVTAB_TYPE) &&
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400589 !policydb_type_isvalid(pol, datum.u.data)) {
Eric Paris744ba352008-04-17 11:52:44 -0400590 printk(KERN_ERR "SELinux: avtab: invalid type\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200591 return -EINVAL;
Stephen Smalley45e54212007-11-07 10:08:00 -0500592 }
Stephen Smalley782ebb92005-09-03 15:55:16 -0700593 return insertf(a, &key, &datum, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Stephen Smalley782ebb92005-09-03 15:55:16 -0700596static int avtab_insertf(struct avtab *a, struct avtab_key *k,
597 struct avtab_datum *d, void *p)
598{
599 return avtab_insert(a, k, d);
600}
601
Stephen Smalley45e54212007-11-07 10:08:00 -0500602int avtab_read(struct avtab *a, void *fp, struct policydb *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 int rc;
Alexey Dobriyanb5bf6c52005-09-03 15:55:17 -0700605 __le32 buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 u32 nel, i;
607
608
609 rc = next_entry(buf, fp, sizeof(u32));
610 if (rc < 0) {
James Morris454d9722008-02-26 20:42:02 +1100611 printk(KERN_ERR "SELinux: avtab: truncated table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 goto bad;
613 }
614 nel = le32_to_cpu(buf[0]);
615 if (!nel) {
James Morris454d9722008-02-26 20:42:02 +1100616 printk(KERN_ERR "SELinux: avtab: table is empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 rc = -EINVAL;
618 goto bad;
619 }
Yuichi Nakamura3232c112007-08-24 11:55:11 +0900620
621 rc = avtab_alloc(a, nel);
622 if (rc)
623 goto bad;
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 for (i = 0; i < nel; i++) {
Stephen Smalley45e54212007-11-07 10:08:00 -0500626 rc = avtab_read_item(a, fp, pol, avtab_insertf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (rc) {
628 if (rc == -ENOMEM)
James Morris454d9722008-02-26 20:42:02 +1100629 printk(KERN_ERR "SELinux: avtab: out of memory\n");
Stephen Smalley782ebb92005-09-03 15:55:16 -0700630 else if (rc == -EEXIST)
James Morris454d9722008-02-26 20:42:02 +1100631 printk(KERN_ERR "SELinux: avtab: duplicate entry\n");
Dan Carpenter9e0bd4c2010-06-12 20:50:35 +0200632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 goto bad;
634 }
635 }
636
637 rc = 0;
638out:
639 return rc;
640
641bad:
642 avtab_destroy(a);
643 goto out;
644}
645
Eric Pariscee74f42010-10-13 17:50:25 -0400646int avtab_write_item(struct policydb *p, struct avtab_node *cur, void *fp)
647{
648 __le16 buf16[4];
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400649 __le32 buf32[ARRAY_SIZE(cur->datum.u.xperms->perms.p)];
Eric Pariscee74f42010-10-13 17:50:25 -0400650 int rc;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400651 unsigned int i;
Eric Pariscee74f42010-10-13 17:50:25 -0400652
653 buf16[0] = cpu_to_le16(cur->key.source_type);
654 buf16[1] = cpu_to_le16(cur->key.target_type);
655 buf16[2] = cpu_to_le16(cur->key.target_class);
Jeff Vander Stoep099f0062015-10-22 09:30:40 -0700656 if (avtab_android_m_compat && (cur->key.specified & AVTAB_XPERMS) &&
657 (cur->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER))
658 buf16[3] = cpu_to_le16(avtab_xperms_to_optype(cur->key.specified));
659 else
660 buf16[3] = cpu_to_le16(cur->key.specified);
Eric Pariscee74f42010-10-13 17:50:25 -0400661 rc = put_entry(buf16, sizeof(u16), 4, fp);
662 if (rc)
663 return rc;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400664
665 if (cur->key.specified & AVTAB_XPERMS) {
Jeff Vander Stoep099f0062015-10-22 09:30:40 -0700666 if (avtab_android_m_compat == 0) {
667 rc = put_entry(&cur->datum.u.xperms->specified,
668 sizeof(u8), 1, fp);
669 if (rc)
670 return rc;
671 }
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400672 rc = put_entry(&cur->datum.u.xperms->driver, sizeof(u8), 1, fp);
673 if (rc)
674 return rc;
675 for (i = 0; i < ARRAY_SIZE(cur->datum.u.xperms->perms.p); i++)
676 buf32[i] = cpu_to_le32(cur->datum.u.xperms->perms.p[i]);
677 rc = put_entry(buf32, sizeof(u32),
678 ARRAY_SIZE(cur->datum.u.xperms->perms.p), fp);
679 } else {
680 buf32[0] = cpu_to_le32(cur->datum.u.data);
681 rc = put_entry(buf32, sizeof(u32), 1, fp);
682 }
Eric Pariscee74f42010-10-13 17:50:25 -0400683 if (rc)
684 return rc;
685 return 0;
686}
687
688int avtab_write(struct policydb *p, struct avtab *a, void *fp)
689{
690 unsigned int i;
691 int rc = 0;
692 struct avtab_node *cur;
693 __le32 buf[1];
694
695 buf[0] = cpu_to_le32(a->nel);
696 rc = put_entry(buf, sizeof(u32), 1, fp);
697 if (rc)
698 return rc;
699
700 for (i = 0; i < a->nslot; i++) {
Stephen Smalleyba39db62015-03-24 16:54:16 -0400701 for (cur = flex_array_get_ptr(a->htable, i); cur;
702 cur = cur->next) {
Eric Pariscee74f42010-10-13 17:50:25 -0400703 rc = avtab_write_item(p, cur, fp);
704 if (rc)
705 return rc;
706 }
707 }
708
709 return rc;
710}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711void avtab_cache_init(void)
712{
713 avtab_node_cachep = kmem_cache_create("avtab_node",
714 sizeof(struct avtab_node),
Paul Mundt20c2df82007-07-20 10:11:58 +0900715 0, SLAB_PANIC, NULL);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400716 avtab_xperms_cachep = kmem_cache_create("avtab_extended_perms",
717 sizeof(struct avtab_extended_perms),
718 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721void avtab_cache_destroy(void)
722{
Eric Pariseb5df9a2008-04-18 17:38:28 -0400723 kmem_cache_destroy(avtab_node_cachep);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400724 kmem_cache_destroy(avtab_xperms_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}