11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds * Implementation of the access vector table type.
31da177e4SLinus Torvalds *
40fe53224SStephen Smalley * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
51da177e4SLinus Torvalds */
61da177e4SLinus Torvalds
71da177e4SLinus Torvalds /* Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Added conditional policy language extensions
101da177e4SLinus Torvalds *
111da177e4SLinus Torvalds * Copyright (C) 2003 Tresys Technology, LLC
121da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify
131da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by
141da177e4SLinus Torvalds * the Free Software Foundation, version 2.
153232c110SYuichi Nakamura *
163232c110SYuichi Nakamura * Updated: Yuichi Nakamura <ynakam@hitachisoft.jp>
173232c110SYuichi Nakamura * Tuned number of hash slots for avtab to reduce memory usage
181da177e4SLinus Torvalds */
191da177e4SLinus Torvalds
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/slab.h>
221da177e4SLinus Torvalds #include <linux/errno.h>
231da177e4SLinus Torvalds #include "avtab.h"
241da177e4SLinus Torvalds #include "policydb.h"
251da177e4SLinus Torvalds
26cd2bb4cbSOndrej Mosnacek static struct kmem_cache *avtab_node_cachep __ro_after_init;
27cd2bb4cbSOndrej Mosnacek static struct kmem_cache *avtab_xperms_cachep __ro_after_init;
281da177e4SLinus Torvalds
2933ebc193SJohn Brooks /* Based on MurmurHash3, written by Austin Appleby and placed in the
3033ebc193SJohn Brooks * public domain.
3133ebc193SJohn Brooks */
avtab_hash(const struct avtab_key * keyp,u32 mask)32*df9d4749SChristian Göttsche static inline u32 avtab_hash(const struct avtab_key *keyp, u32 mask)
333232c110SYuichi Nakamura {
3433ebc193SJohn Brooks static const u32 c1 = 0xcc9e2d51;
3533ebc193SJohn Brooks static const u32 c2 = 0x1b873593;
3633ebc193SJohn Brooks static const u32 r1 = 15;
3733ebc193SJohn Brooks static const u32 r2 = 13;
3833ebc193SJohn Brooks static const u32 m = 5;
3933ebc193SJohn Brooks static const u32 n = 0xe6546b64;
4033ebc193SJohn Brooks
4133ebc193SJohn Brooks u32 hash = 0;
4233ebc193SJohn Brooks
431d4e8036SChristian Göttsche #define mix(input) do { \
4433ebc193SJohn Brooks u32 v = input; \
4533ebc193SJohn Brooks v *= c1; \
4633ebc193SJohn Brooks v = (v << r1) | (v >> (32 - r1)); \
4733ebc193SJohn Brooks v *= c2; \
4833ebc193SJohn Brooks hash ^= v; \
4933ebc193SJohn Brooks hash = (hash << r2) | (hash >> (32 - r2)); \
5033ebc193SJohn Brooks hash = hash * m + n; \
511d4e8036SChristian Göttsche } while (0)
5233ebc193SJohn Brooks
5333ebc193SJohn Brooks mix(keyp->target_class);
5433ebc193SJohn Brooks mix(keyp->target_type);
5533ebc193SJohn Brooks mix(keyp->source_type);
5633ebc193SJohn Brooks
5733ebc193SJohn Brooks #undef mix
5833ebc193SJohn Brooks
5933ebc193SJohn Brooks hash ^= hash >> 16;
6033ebc193SJohn Brooks hash *= 0x85ebca6b;
6133ebc193SJohn Brooks hash ^= hash >> 13;
6233ebc193SJohn Brooks hash *= 0xc2b2ae35;
6333ebc193SJohn Brooks hash ^= hash >> 16;
6433ebc193SJohn Brooks
6533ebc193SJohn Brooks return hash & mask;
663232c110SYuichi Nakamura }
673232c110SYuichi Nakamura
681da177e4SLinus Torvalds static struct avtab_node*
avtab_insert_node(struct avtab * h,u32 hvalue,struct avtab_node * prev,const struct avtab_key * key,const struct avtab_datum * datum)69*df9d4749SChristian Göttsche avtab_insert_node(struct avtab *h, u32 hvalue,
70056945a9SChristian Göttsche struct avtab_node *prev,
71e1cce3a3SOndrej Mosnacek const struct avtab_key *key, const struct avtab_datum *datum)
721da177e4SLinus Torvalds {
731da177e4SLinus Torvalds struct avtab_node *newnode;
74fa1aa143SJeff Vander Stoep struct avtab_extended_perms *xperms;
75c3762229SRobert P. J. Day newnode = kmem_cache_zalloc(avtab_node_cachep, GFP_KERNEL);
761da177e4SLinus Torvalds if (newnode == NULL)
771da177e4SLinus Torvalds return NULL;
781da177e4SLinus Torvalds newnode->key = *key;
79fa1aa143SJeff Vander Stoep
80fa1aa143SJeff Vander Stoep if (key->specified & AVTAB_XPERMS) {
81fa1aa143SJeff Vander Stoep xperms = kmem_cache_zalloc(avtab_xperms_cachep, GFP_KERNEL);
82fa1aa143SJeff Vander Stoep if (xperms == NULL) {
83fa1aa143SJeff Vander Stoep kmem_cache_free(avtab_node_cachep, newnode);
84fa1aa143SJeff Vander Stoep return NULL;
85fa1aa143SJeff Vander Stoep }
86fa1aa143SJeff Vander Stoep *xperms = *(datum->u.xperms);
87fa1aa143SJeff Vander Stoep newnode->datum.u.xperms = xperms;
88fa1aa143SJeff Vander Stoep } else {
89fa1aa143SJeff Vander Stoep newnode->datum.u.data = datum->u.data;
90fa1aa143SJeff Vander Stoep }
91fa1aa143SJeff Vander Stoep
921da177e4SLinus Torvalds if (prev) {
931da177e4SLinus Torvalds newnode->next = prev->next;
941da177e4SLinus Torvalds prev->next = newnode;
951da177e4SLinus Torvalds } else {
96acdf52d9SKent Overstreet struct avtab_node **n = &h->htable[hvalue];
97acdf52d9SKent Overstreet
98acdf52d9SKent Overstreet newnode->next = *n;
99acdf52d9SKent Overstreet *n = newnode;
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds
1021da177e4SLinus Torvalds h->nel++;
1031da177e4SLinus Torvalds return newnode;
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds
avtab_insert(struct avtab * h,const struct avtab_key * key,const struct avtab_datum * datum)106e1cce3a3SOndrej Mosnacek static int avtab_insert(struct avtab *h, const struct avtab_key *key,
107e1cce3a3SOndrej Mosnacek const struct avtab_datum *datum)
1081da177e4SLinus Torvalds {
109*df9d4749SChristian Göttsche u32 hvalue;
1101da177e4SLinus Torvalds struct avtab_node *prev, *cur, *newnode;
111782ebb99SStephen Smalley u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
1121da177e4SLinus Torvalds
113f785c541SChristian Göttsche if (!h || !h->nslot || h->nel == U32_MAX)
1141da177e4SLinus Torvalds return -EINVAL;
1151da177e4SLinus Torvalds
1163232c110SYuichi Nakamura hvalue = avtab_hash(key, h->mask);
117acdf52d9SKent Overstreet for (prev = NULL, cur = h->htable[hvalue];
1181da177e4SLinus Torvalds cur;
1191da177e4SLinus Torvalds prev = cur, cur = cur->next) {
1201da177e4SLinus Torvalds if (key->source_type == cur->key.source_type &&
1211da177e4SLinus Torvalds key->target_type == cur->key.target_type &&
1221da177e4SLinus Torvalds key->target_class == cur->key.target_class &&
123fa1aa143SJeff Vander Stoep (specified & cur->key.specified)) {
124fa1aa143SJeff Vander Stoep /* extended perms may not be unique */
125fa1aa143SJeff Vander Stoep if (specified & AVTAB_XPERMS)
126fa1aa143SJeff Vander Stoep break;
1271da177e4SLinus Torvalds return -EEXIST;
128fa1aa143SJeff Vander Stoep }
1291da177e4SLinus Torvalds if (key->source_type < cur->key.source_type)
1301da177e4SLinus Torvalds break;
1311da177e4SLinus Torvalds if (key->source_type == cur->key.source_type &&
1321da177e4SLinus Torvalds key->target_type < cur->key.target_type)
1331da177e4SLinus Torvalds break;
1341da177e4SLinus Torvalds if (key->source_type == cur->key.source_type &&
1351da177e4SLinus Torvalds key->target_type == cur->key.target_type &&
1361da177e4SLinus Torvalds key->target_class < cur->key.target_class)
1371da177e4SLinus Torvalds break;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds
140056945a9SChristian Göttsche newnode = avtab_insert_node(h, hvalue, prev, key, datum);
1411da177e4SLinus Torvalds if (!newnode)
1421da177e4SLinus Torvalds return -ENOMEM;
1431da177e4SLinus Torvalds
1441da177e4SLinus Torvalds return 0;
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds
1471da177e4SLinus Torvalds /* Unlike avtab_insert(), this function allow multiple insertions of the same
1481da177e4SLinus Torvalds * key/specified mask into the table, as needed by the conditional avtab.
1491da177e4SLinus Torvalds * It also returns a pointer to the node inserted.
1501da177e4SLinus Torvalds */
avtab_insert_nonunique(struct avtab * h,const struct avtab_key * key,const struct avtab_datum * datum)151e1cce3a3SOndrej Mosnacek struct avtab_node *avtab_insert_nonunique(struct avtab *h,
152e1cce3a3SOndrej Mosnacek const struct avtab_key *key,
153e1cce3a3SOndrej Mosnacek const struct avtab_datum *datum)
1541da177e4SLinus Torvalds {
155*df9d4749SChristian Göttsche u32 hvalue;
1560c0e186fSVesa-Matti J Kari struct avtab_node *prev, *cur;
157782ebb99SStephen Smalley u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
1581da177e4SLinus Torvalds
159f785c541SChristian Göttsche if (!h || !h->nslot || h->nel == U32_MAX)
1601da177e4SLinus Torvalds return NULL;
1613232c110SYuichi Nakamura hvalue = avtab_hash(key, h->mask);
162acdf52d9SKent Overstreet for (prev = NULL, cur = h->htable[hvalue];
1631da177e4SLinus Torvalds cur;
1641da177e4SLinus Torvalds prev = cur, cur = cur->next) {
1651da177e4SLinus Torvalds if (key->source_type == cur->key.source_type &&
1661da177e4SLinus Torvalds key->target_type == cur->key.target_type &&
1671da177e4SLinus Torvalds key->target_class == cur->key.target_class &&
168782ebb99SStephen Smalley (specified & cur->key.specified))
1691da177e4SLinus Torvalds break;
1701da177e4SLinus Torvalds if (key->source_type < cur->key.source_type)
1711da177e4SLinus Torvalds break;
1721da177e4SLinus Torvalds if (key->source_type == cur->key.source_type &&
1731da177e4SLinus Torvalds key->target_type < cur->key.target_type)
1741da177e4SLinus Torvalds break;
1751da177e4SLinus Torvalds if (key->source_type == cur->key.source_type &&
1761da177e4SLinus Torvalds key->target_type == cur->key.target_type &&
1771da177e4SLinus Torvalds key->target_class < cur->key.target_class)
1781da177e4SLinus Torvalds break;
1791da177e4SLinus Torvalds }
180056945a9SChristian Göttsche return avtab_insert_node(h, hvalue, prev, key, datum);
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds
1831da177e4SLinus Torvalds /* This search function returns a node pointer, and can be used in
1841da177e4SLinus Torvalds * conjunction with avtab_search_next_node()
1851da177e4SLinus Torvalds */
avtab_search_node(struct avtab * h,const struct avtab_key * key)186e1cce3a3SOndrej Mosnacek struct avtab_node *avtab_search_node(struct avtab *h,
187e1cce3a3SOndrej Mosnacek const struct avtab_key *key)
1881da177e4SLinus Torvalds {
189*df9d4749SChristian Göttsche u32 hvalue;
1901da177e4SLinus Torvalds struct avtab_node *cur;
191782ebb99SStephen Smalley u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
1921da177e4SLinus Torvalds
193442dc00fSOndrej Mosnacek if (!h || !h->nslot)
1941da177e4SLinus Torvalds return NULL;
1951da177e4SLinus Torvalds
1963232c110SYuichi Nakamura hvalue = avtab_hash(key, h->mask);
197acdf52d9SKent Overstreet for (cur = h->htable[hvalue]; cur;
198ba39db6eSStephen Smalley cur = cur->next) {
1991da177e4SLinus Torvalds if (key->source_type == cur->key.source_type &&
2001da177e4SLinus Torvalds key->target_type == cur->key.target_type &&
2011da177e4SLinus Torvalds key->target_class == cur->key.target_class &&
202782ebb99SStephen Smalley (specified & cur->key.specified))
2031da177e4SLinus Torvalds return cur;
2041da177e4SLinus Torvalds
2051da177e4SLinus Torvalds if (key->source_type < cur->key.source_type)
2061da177e4SLinus Torvalds break;
2071da177e4SLinus Torvalds if (key->source_type == cur->key.source_type &&
2081da177e4SLinus Torvalds key->target_type < cur->key.target_type)
2091da177e4SLinus Torvalds break;
2101da177e4SLinus Torvalds if (key->source_type == cur->key.source_type &&
2111da177e4SLinus Torvalds key->target_type == cur->key.target_type &&
2121da177e4SLinus Torvalds key->target_class < cur->key.target_class)
2131da177e4SLinus Torvalds break;
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds return NULL;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds
2181da177e4SLinus Torvalds struct avtab_node*
avtab_search_node_next(struct avtab_node * node,u16 specified)2197128578cSChristian Göttsche avtab_search_node_next(struct avtab_node *node, u16 specified)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds struct avtab_node *cur;
2221da177e4SLinus Torvalds
2231da177e4SLinus Torvalds if (!node)
2241da177e4SLinus Torvalds return NULL;
2251da177e4SLinus Torvalds
226782ebb99SStephen Smalley specified &= ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
2271da177e4SLinus Torvalds for (cur = node->next; cur; cur = cur->next) {
2281da177e4SLinus Torvalds if (node->key.source_type == cur->key.source_type &&
2291da177e4SLinus Torvalds node->key.target_type == cur->key.target_type &&
2301da177e4SLinus Torvalds node->key.target_class == cur->key.target_class &&
231782ebb99SStephen Smalley (specified & cur->key.specified))
2321da177e4SLinus Torvalds return cur;
2331da177e4SLinus Torvalds
2341da177e4SLinus Torvalds if (node->key.source_type < cur->key.source_type)
2351da177e4SLinus Torvalds break;
2361da177e4SLinus Torvalds if (node->key.source_type == cur->key.source_type &&
2371da177e4SLinus Torvalds node->key.target_type < cur->key.target_type)
2381da177e4SLinus Torvalds break;
2391da177e4SLinus Torvalds if (node->key.source_type == cur->key.source_type &&
2401da177e4SLinus Torvalds node->key.target_type == cur->key.target_type &&
2411da177e4SLinus Torvalds node->key.target_class < cur->key.target_class)
2421da177e4SLinus Torvalds break;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds return NULL;
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds
avtab_destroy(struct avtab * h)2471da177e4SLinus Torvalds void avtab_destroy(struct avtab *h)
2481da177e4SLinus Torvalds {
249*df9d4749SChristian Göttsche u32 i;
2501da177e4SLinus Torvalds struct avtab_node *cur, *temp;
2511da177e4SLinus Torvalds
252acdf52d9SKent Overstreet if (!h)
2531da177e4SLinus Torvalds return;
2541da177e4SLinus Torvalds
2553232c110SYuichi Nakamura for (i = 0; i < h->nslot; i++) {
256acdf52d9SKent Overstreet cur = h->htable[i];
257dbc74c65SVesa-Matti Kari while (cur) {
2581da177e4SLinus Torvalds temp = cur;
2591da177e4SLinus Torvalds cur = cur->next;
260fa1aa143SJeff Vander Stoep if (temp->key.specified & AVTAB_XPERMS)
261fa1aa143SJeff Vander Stoep kmem_cache_free(avtab_xperms_cachep,
262fa1aa143SJeff Vander Stoep temp->datum.u.xperms);
2631da177e4SLinus Torvalds kmem_cache_free(avtab_node_cachep, temp);
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds }
266acdf52d9SKent Overstreet kvfree(h->htable);
2671da177e4SLinus Torvalds h->htable = NULL;
268442dc00fSOndrej Mosnacek h->nel = 0;
2693232c110SYuichi Nakamura h->nslot = 0;
2703232c110SYuichi Nakamura h->mask = 0;
2711da177e4SLinus Torvalds }
2721da177e4SLinus Torvalds
avtab_init(struct avtab * h)2735e729e11SPaul Moore void avtab_init(struct avtab *h)
2741da177e4SLinus Torvalds {
2753232c110SYuichi Nakamura h->htable = NULL;
2763232c110SYuichi Nakamura h->nel = 0;
277442dc00fSOndrej Mosnacek h->nslot = 0;
278442dc00fSOndrej Mosnacek h->mask = 0;
2793232c110SYuichi Nakamura }
2801da177e4SLinus Torvalds
avtab_alloc_common(struct avtab * h,u32 nslot)281d8f5f0eaSOndrej Mosnacek static int avtab_alloc_common(struct avtab *h, u32 nslot)
2823232c110SYuichi Nakamura {
283d8f5f0eaSOndrej Mosnacek if (!nslot)
284d8f5f0eaSOndrej Mosnacek return 0;
2853232c110SYuichi Nakamura
286acdf52d9SKent Overstreet h->htable = kvcalloc(nslot, sizeof(void *), GFP_KERNEL);
2871da177e4SLinus Torvalds if (!h->htable)
2881da177e4SLinus Torvalds return -ENOMEM;
2893232c110SYuichi Nakamura
2903232c110SYuichi Nakamura h->nslot = nslot;
291442dc00fSOndrej Mosnacek h->mask = nslot - 1;
2921da177e4SLinus Torvalds return 0;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds
avtab_alloc(struct avtab * h,u32 nrules)295d8f5f0eaSOndrej Mosnacek int avtab_alloc(struct avtab *h, u32 nrules)
296c7c556f1SStephen Smalley {
297d8f5f0eaSOndrej Mosnacek int rc;
298d8f5f0eaSOndrej Mosnacek u32 nslot = 0;
299c7c556f1SStephen Smalley
300d8f5f0eaSOndrej Mosnacek if (nrules != 0) {
301d8f5f0eaSOndrej Mosnacek u32 shift = 1;
302d8f5f0eaSOndrej Mosnacek u32 work = nrules >> 3;
303d8f5f0eaSOndrej Mosnacek while (work) {
304d8f5f0eaSOndrej Mosnacek work >>= 1;
305d8f5f0eaSOndrej Mosnacek shift++;
306c7c556f1SStephen Smalley }
307d8f5f0eaSOndrej Mosnacek nslot = 1 << shift;
308d8f5f0eaSOndrej Mosnacek if (nslot > MAX_AVTAB_HASH_BUCKETS)
309d8f5f0eaSOndrej Mosnacek nslot = MAX_AVTAB_HASH_BUCKETS;
310c7c556f1SStephen Smalley
311d8f5f0eaSOndrej Mosnacek rc = avtab_alloc_common(h, nslot);
312d8f5f0eaSOndrej Mosnacek if (rc)
313d8f5f0eaSOndrej Mosnacek return rc;
314c7c556f1SStephen Smalley }
315c7c556f1SStephen Smalley
316d8f5f0eaSOndrej Mosnacek pr_debug("SELinux: %d avtab hash slots, %d rules.\n", nslot, nrules);
317c7c556f1SStephen Smalley return 0;
318d8f5f0eaSOndrej Mosnacek }
319d8f5f0eaSOndrej Mosnacek
avtab_alloc_dup(struct avtab * new,const struct avtab * orig)320d8f5f0eaSOndrej Mosnacek int avtab_alloc_dup(struct avtab *new, const struct avtab *orig)
321d8f5f0eaSOndrej Mosnacek {
322d8f5f0eaSOndrej Mosnacek return avtab_alloc_common(new, orig->nslot);
323c7c556f1SStephen Smalley }
324c7c556f1SStephen Smalley
325f01dd590SChristian Göttsche #ifdef CONFIG_SECURITY_SELINUX_DEBUG
avtab_hash_eval(struct avtab * h,const char * tag)3264595ae8cSChristian Göttsche void avtab_hash_eval(struct avtab *h, const char *tag)
3271da177e4SLinus Torvalds {
328*df9d4749SChristian Göttsche u32 i, chain_len, slots_used, max_chain_len;
3293232c110SYuichi Nakamura unsigned long long chain2_len_sum;
3301da177e4SLinus Torvalds struct avtab_node *cur;
3311da177e4SLinus Torvalds
3321da177e4SLinus Torvalds slots_used = 0;
3331da177e4SLinus Torvalds max_chain_len = 0;
3343232c110SYuichi Nakamura chain2_len_sum = 0;
3353232c110SYuichi Nakamura for (i = 0; i < h->nslot; i++) {
336acdf52d9SKent Overstreet cur = h->htable[i];
3371da177e4SLinus Torvalds if (cur) {
3381da177e4SLinus Torvalds slots_used++;
3391da177e4SLinus Torvalds chain_len = 0;
3401da177e4SLinus Torvalds while (cur) {
3411da177e4SLinus Torvalds chain_len++;
3421da177e4SLinus Torvalds cur = cur->next;
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds
3451da177e4SLinus Torvalds if (chain_len > max_chain_len)
3461da177e4SLinus Torvalds max_chain_len = chain_len;
347*df9d4749SChristian Göttsche chain2_len_sum += (unsigned long long)chain_len * chain_len;
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds
351c87a7e75Speter enderborg pr_debug("SELinux: %s: %d entries and %d/%d buckets used, "
352f5269710SEric Paris "longest chain length %d sum of chain length^2 %llu\n",
3533232c110SYuichi Nakamura tag, h->nel, slots_used, h->nslot, max_chain_len,
3543232c110SYuichi Nakamura chain2_len_sum);
3551da177e4SLinus Torvalds }
356f01dd590SChristian Göttsche #endif /* CONFIG_SECURITY_SELINUX_DEBUG */
3571da177e4SLinus Torvalds
358ded34574SChristian Göttsche static const uint16_t spec_order[] = {
359782ebb99SStephen Smalley AVTAB_ALLOWED,
360782ebb99SStephen Smalley AVTAB_AUDITDENY,
361782ebb99SStephen Smalley AVTAB_AUDITALLOW,
362782ebb99SStephen Smalley AVTAB_TRANSITION,
363782ebb99SStephen Smalley AVTAB_CHANGE,
364fa1aa143SJeff Vander Stoep AVTAB_MEMBER,
365fa1aa143SJeff Vander Stoep AVTAB_XPERMS_ALLOWED,
366fa1aa143SJeff Vander Stoep AVTAB_XPERMS_AUDITALLOW,
367fa1aa143SJeff Vander Stoep AVTAB_XPERMS_DONTAUDIT
368782ebb99SStephen Smalley };
369782ebb99SStephen Smalley
avtab_read_item(struct avtab * a,void * fp,struct policydb * pol,int (* insertf)(struct avtab * a,const struct avtab_key * k,const struct avtab_datum * d,void * p),void * p)37045e5421eSStephen Smalley int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
371e1cce3a3SOndrej Mosnacek int (*insertf)(struct avtab *a, const struct avtab_key *k,
372e1cce3a3SOndrej Mosnacek const struct avtab_datum *d, void *p),
373782ebb99SStephen Smalley void *p)
3741da177e4SLinus Torvalds {
375b5bf6c55SAlexey Dobriyan __le16 buf16[4];
376b5bf6c55SAlexey Dobriyan u16 enabled;
377*df9d4749SChristian Göttsche u32 items, items2, val, i;
378782ebb99SStephen Smalley struct avtab_key key;
379782ebb99SStephen Smalley struct avtab_datum datum;
380fa1aa143SJeff Vander Stoep struct avtab_extended_perms xperms;
381fa1aa143SJeff Vander Stoep __le32 buf32[ARRAY_SIZE(xperms.perms.p)];
382*df9d4749SChristian Göttsche int rc;
383*df9d4749SChristian Göttsche unsigned int set, vers = pol->policyvers;
3841da177e4SLinus Torvalds
385782ebb99SStephen Smalley memset(&key, 0, sizeof(struct avtab_key));
386782ebb99SStephen Smalley memset(&datum, 0, sizeof(struct avtab_datum));
3871da177e4SLinus Torvalds
388782ebb99SStephen Smalley if (vers < POLICYDB_VERSION_AVTAB) {
389782ebb99SStephen Smalley rc = next_entry(buf32, fp, sizeof(u32));
3909e0bd4cbSDan Carpenter if (rc) {
391c87a7e75Speter enderborg pr_err("SELinux: avtab: truncated entry\n");
3929e0bd4cbSDan Carpenter return rc;
3931da177e4SLinus Torvalds }
394782ebb99SStephen Smalley items2 = le32_to_cpu(buf32[0]);
395782ebb99SStephen Smalley if (items2 > ARRAY_SIZE(buf32)) {
396c87a7e75Speter enderborg pr_err("SELinux: avtab: entry overflow\n");
3979e0bd4cbSDan Carpenter return -EINVAL;
398782ebb99SStephen Smalley
3991da177e4SLinus Torvalds }
400782ebb99SStephen Smalley rc = next_entry(buf32, fp, sizeof(u32)*items2);
4019e0bd4cbSDan Carpenter if (rc) {
402c87a7e75Speter enderborg pr_err("SELinux: avtab: truncated entry\n");
4039e0bd4cbSDan Carpenter return rc;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds items = 0;
4061da177e4SLinus Torvalds
407782ebb99SStephen Smalley val = le32_to_cpu(buf32[items++]);
408782ebb99SStephen Smalley key.source_type = (u16)val;
409782ebb99SStephen Smalley if (key.source_type != val) {
410c87a7e75Speter enderborg pr_err("SELinux: avtab: truncated source type\n");
4119e0bd4cbSDan Carpenter return -EINVAL;
412782ebb99SStephen Smalley }
413782ebb99SStephen Smalley val = le32_to_cpu(buf32[items++]);
414782ebb99SStephen Smalley key.target_type = (u16)val;
415782ebb99SStephen Smalley if (key.target_type != val) {
416c87a7e75Speter enderborg pr_err("SELinux: avtab: truncated target type\n");
4179e0bd4cbSDan Carpenter return -EINVAL;
418782ebb99SStephen Smalley }
419782ebb99SStephen Smalley val = le32_to_cpu(buf32[items++]);
420782ebb99SStephen Smalley key.target_class = (u16)val;
421782ebb99SStephen Smalley if (key.target_class != val) {
422c87a7e75Speter enderborg pr_err("SELinux: avtab: truncated target class\n");
4239e0bd4cbSDan Carpenter return -EINVAL;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds
426782ebb99SStephen Smalley val = le32_to_cpu(buf32[items++]);
427782ebb99SStephen Smalley enabled = (val & AVTAB_ENABLED_OLD) ? AVTAB_ENABLED : 0;
428782ebb99SStephen Smalley
429782ebb99SStephen Smalley if (!(val & (AVTAB_AV | AVTAB_TYPE))) {
430c87a7e75Speter enderborg pr_err("SELinux: avtab: null entry\n");
4319e0bd4cbSDan Carpenter return -EINVAL;
432782ebb99SStephen Smalley }
433782ebb99SStephen Smalley if ((val & AVTAB_AV) &&
434782ebb99SStephen Smalley (val & AVTAB_TYPE)) {
435c87a7e75Speter enderborg pr_err("SELinux: avtab: entry has both access vectors and types\n");
4369e0bd4cbSDan Carpenter return -EINVAL;
437782ebb99SStephen Smalley }
438fa1aa143SJeff Vander Stoep if (val & AVTAB_XPERMS) {
439c87a7e75Speter enderborg pr_err("SELinux: avtab: entry has extended permissions\n");
440fa1aa143SJeff Vander Stoep return -EINVAL;
441fa1aa143SJeff Vander Stoep }
442782ebb99SStephen Smalley
44332725ad8STobias Klauser for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
444782ebb99SStephen Smalley if (val & spec_order[i]) {
445782ebb99SStephen Smalley key.specified = spec_order[i] | enabled;
446fa1aa143SJeff Vander Stoep datum.u.data = le32_to_cpu(buf32[items++]);
447782ebb99SStephen Smalley rc = insertf(a, &key, &datum, p);
448eb5df9a7SEric Paris if (rc)
449eb5df9a7SEric Paris return rc;
450782ebb99SStephen Smalley }
451782ebb99SStephen Smalley }
452782ebb99SStephen Smalley
453782ebb99SStephen Smalley if (items != items2) {
454c87a7e75Speter enderborg pr_err("SELinux: avtab: entry only had %d items, expected %d\n",
455c87a7e75Speter enderborg items2, items);
4569e0bd4cbSDan Carpenter return -EINVAL;
457782ebb99SStephen Smalley }
458782ebb99SStephen Smalley return 0;
459782ebb99SStephen Smalley }
460782ebb99SStephen Smalley
461782ebb99SStephen Smalley rc = next_entry(buf16, fp, sizeof(u16)*4);
4629e0bd4cbSDan Carpenter if (rc) {
463c87a7e75Speter enderborg pr_err("SELinux: avtab: truncated entry\n");
4649e0bd4cbSDan Carpenter return rc;
465782ebb99SStephen Smalley }
466782ebb99SStephen Smalley
467782ebb99SStephen Smalley items = 0;
468782ebb99SStephen Smalley key.source_type = le16_to_cpu(buf16[items++]);
469782ebb99SStephen Smalley key.target_type = le16_to_cpu(buf16[items++]);
470782ebb99SStephen Smalley key.target_class = le16_to_cpu(buf16[items++]);
471782ebb99SStephen Smalley key.specified = le16_to_cpu(buf16[items++]);
472782ebb99SStephen Smalley
47345e5421eSStephen Smalley if (!policydb_type_isvalid(pol, key.source_type) ||
47445e5421eSStephen Smalley !policydb_type_isvalid(pol, key.target_type) ||
47545e5421eSStephen Smalley !policydb_class_isvalid(pol, key.target_class)) {
476c87a7e75Speter enderborg pr_err("SELinux: avtab: invalid type or class\n");
4779e0bd4cbSDan Carpenter return -EINVAL;
47845e5421eSStephen Smalley }
47945e5421eSStephen Smalley
48045e5421eSStephen Smalley set = 0;
48145e5421eSStephen Smalley for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
48245e5421eSStephen Smalley if (key.specified & spec_order[i])
48345e5421eSStephen Smalley set++;
48445e5421eSStephen Smalley }
48545e5421eSStephen Smalley if (!set || set > 1) {
486c87a7e75Speter enderborg pr_err("SELinux: avtab: more than one specifier\n");
4879e0bd4cbSDan Carpenter return -EINVAL;
48845e5421eSStephen Smalley }
48945e5421eSStephen Smalley
490fa1aa143SJeff Vander Stoep if ((vers < POLICYDB_VERSION_XPERMS_IOCTL) &&
491fa1aa143SJeff Vander Stoep (key.specified & AVTAB_XPERMS)) {
492c87a7e75Speter enderborg pr_err("SELinux: avtab: policy version %u does not "
493fa1aa143SJeff Vander Stoep "support extended permissions rules and one "
494fa1aa143SJeff Vander Stoep "was specified\n", vers);
495fa1aa143SJeff Vander Stoep return -EINVAL;
496fa1aa143SJeff Vander Stoep } else if (key.specified & AVTAB_XPERMS) {
497fa1aa143SJeff Vander Stoep memset(&xperms, 0, sizeof(struct avtab_extended_perms));
498fa1aa143SJeff Vander Stoep rc = next_entry(&xperms.specified, fp, sizeof(u8));
499fa1aa143SJeff Vander Stoep if (rc) {
500c87a7e75Speter enderborg pr_err("SELinux: avtab: truncated entry\n");
501fa1aa143SJeff Vander Stoep return rc;
502fa1aa143SJeff Vander Stoep }
503fa1aa143SJeff Vander Stoep rc = next_entry(&xperms.driver, fp, sizeof(u8));
504fa1aa143SJeff Vander Stoep if (rc) {
505c87a7e75Speter enderborg pr_err("SELinux: avtab: truncated entry\n");
506fa1aa143SJeff Vander Stoep return rc;
507fa1aa143SJeff Vander Stoep }
508fa1aa143SJeff Vander Stoep rc = next_entry(buf32, fp, sizeof(u32)*ARRAY_SIZE(xperms.perms.p));
509fa1aa143SJeff Vander Stoep if (rc) {
510c87a7e75Speter enderborg pr_err("SELinux: avtab: truncated entry\n");
511fa1aa143SJeff Vander Stoep return rc;
512fa1aa143SJeff Vander Stoep }
513fa1aa143SJeff Vander Stoep for (i = 0; i < ARRAY_SIZE(xperms.perms.p); i++)
514fa1aa143SJeff Vander Stoep xperms.perms.p[i] = le32_to_cpu(buf32[i]);
515fa1aa143SJeff Vander Stoep datum.u.xperms = &xperms;
516fa1aa143SJeff Vander Stoep } else {
517782ebb99SStephen Smalley rc = next_entry(buf32, fp, sizeof(u32));
5189e0bd4cbSDan Carpenter if (rc) {
519c87a7e75Speter enderborg pr_err("SELinux: avtab: truncated entry\n");
5209e0bd4cbSDan Carpenter return rc;
521782ebb99SStephen Smalley }
522fa1aa143SJeff Vander Stoep datum.u.data = le32_to_cpu(*buf32);
523fa1aa143SJeff Vander Stoep }
52445e5421eSStephen Smalley if ((key.specified & AVTAB_TYPE) &&
525fa1aa143SJeff Vander Stoep !policydb_type_isvalid(pol, datum.u.data)) {
526c87a7e75Speter enderborg pr_err("SELinux: avtab: invalid type\n");
5279e0bd4cbSDan Carpenter return -EINVAL;
52845e5421eSStephen Smalley }
529782ebb99SStephen Smalley return insertf(a, &key, &datum, p);
530782ebb99SStephen Smalley }
531782ebb99SStephen Smalley
avtab_insertf(struct avtab * a,const struct avtab_key * k,const struct avtab_datum * d,void * p)532e1cce3a3SOndrej Mosnacek static int avtab_insertf(struct avtab *a, const struct avtab_key *k,
533e1cce3a3SOndrej Mosnacek const struct avtab_datum *d, void *p)
534782ebb99SStephen Smalley {
535782ebb99SStephen Smalley return avtab_insert(a, k, d);
536782ebb99SStephen Smalley }
537782ebb99SStephen Smalley
avtab_read(struct avtab * a,void * fp,struct policydb * pol)53845e5421eSStephen Smalley int avtab_read(struct avtab *a, void *fp, struct policydb *pol)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds int rc;
541b5bf6c55SAlexey Dobriyan __le32 buf[1];
5421da177e4SLinus Torvalds u32 nel, i;
5431da177e4SLinus Torvalds
5441da177e4SLinus Torvalds
5451da177e4SLinus Torvalds rc = next_entry(buf, fp, sizeof(u32));
5461da177e4SLinus Torvalds if (rc < 0) {
547c87a7e75Speter enderborg pr_err("SELinux: avtab: truncated table\n");
5481da177e4SLinus Torvalds goto bad;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds nel = le32_to_cpu(buf[0]);
5511da177e4SLinus Torvalds if (!nel) {
552c87a7e75Speter enderborg pr_err("SELinux: avtab: table is empty\n");
5531da177e4SLinus Torvalds rc = -EINVAL;
5541da177e4SLinus Torvalds goto bad;
5551da177e4SLinus Torvalds }
5563232c110SYuichi Nakamura
5573232c110SYuichi Nakamura rc = avtab_alloc(a, nel);
5583232c110SYuichi Nakamura if (rc)
5593232c110SYuichi Nakamura goto bad;
5603232c110SYuichi Nakamura
5611da177e4SLinus Torvalds for (i = 0; i < nel; i++) {
56245e5421eSStephen Smalley rc = avtab_read_item(a, fp, pol, avtab_insertf, NULL);
5631da177e4SLinus Torvalds if (rc) {
5641da177e4SLinus Torvalds if (rc == -ENOMEM)
565c87a7e75Speter enderborg pr_err("SELinux: avtab: out of memory\n");
566782ebb99SStephen Smalley else if (rc == -EEXIST)
567c87a7e75Speter enderborg pr_err("SELinux: avtab: duplicate entry\n");
5689e0bd4cbSDan Carpenter
5691da177e4SLinus Torvalds goto bad;
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds
5731da177e4SLinus Torvalds rc = 0;
5741da177e4SLinus Torvalds out:
5751da177e4SLinus Torvalds return rc;
5761da177e4SLinus Torvalds
5771da177e4SLinus Torvalds bad:
5781da177e4SLinus Torvalds avtab_destroy(a);
5791da177e4SLinus Torvalds goto out;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds
avtab_write_item(struct policydb * p,const struct avtab_node * cur,void * fp)582e1cce3a3SOndrej Mosnacek int avtab_write_item(struct policydb *p, const struct avtab_node *cur, void *fp)
583cee74f47SEric Paris {
584cee74f47SEric Paris __le16 buf16[4];
585fa1aa143SJeff Vander Stoep __le32 buf32[ARRAY_SIZE(cur->datum.u.xperms->perms.p)];
586cee74f47SEric Paris int rc;
587fa1aa143SJeff Vander Stoep unsigned int i;
588cee74f47SEric Paris
589cee74f47SEric Paris buf16[0] = cpu_to_le16(cur->key.source_type);
590cee74f47SEric Paris buf16[1] = cpu_to_le16(cur->key.target_type);
591cee74f47SEric Paris buf16[2] = cpu_to_le16(cur->key.target_class);
592cee74f47SEric Paris buf16[3] = cpu_to_le16(cur->key.specified);
593cee74f47SEric Paris rc = put_entry(buf16, sizeof(u16), 4, fp);
594cee74f47SEric Paris if (rc)
595cee74f47SEric Paris return rc;
596fa1aa143SJeff Vander Stoep
597fa1aa143SJeff Vander Stoep if (cur->key.specified & AVTAB_XPERMS) {
598fa1aa143SJeff Vander Stoep rc = put_entry(&cur->datum.u.xperms->specified, sizeof(u8), 1, fp);
599fa1aa143SJeff Vander Stoep if (rc)
600fa1aa143SJeff Vander Stoep return rc;
601fa1aa143SJeff Vander Stoep rc = put_entry(&cur->datum.u.xperms->driver, sizeof(u8), 1, fp);
602fa1aa143SJeff Vander Stoep if (rc)
603fa1aa143SJeff Vander Stoep return rc;
604fa1aa143SJeff Vander Stoep for (i = 0; i < ARRAY_SIZE(cur->datum.u.xperms->perms.p); i++)
605fa1aa143SJeff Vander Stoep buf32[i] = cpu_to_le32(cur->datum.u.xperms->perms.p[i]);
606fa1aa143SJeff Vander Stoep rc = put_entry(buf32, sizeof(u32),
607fa1aa143SJeff Vander Stoep ARRAY_SIZE(cur->datum.u.xperms->perms.p), fp);
608fa1aa143SJeff Vander Stoep } else {
609fa1aa143SJeff Vander Stoep buf32[0] = cpu_to_le32(cur->datum.u.data);
610cee74f47SEric Paris rc = put_entry(buf32, sizeof(u32), 1, fp);
611fa1aa143SJeff Vander Stoep }
612cee74f47SEric Paris if (rc)
613cee74f47SEric Paris return rc;
614cee74f47SEric Paris return 0;
615cee74f47SEric Paris }
616cee74f47SEric Paris
avtab_write(struct policydb * p,struct avtab * a,void * fp)617cee74f47SEric Paris int avtab_write(struct policydb *p, struct avtab *a, void *fp)
618cee74f47SEric Paris {
619*df9d4749SChristian Göttsche u32 i;
620cee74f47SEric Paris int rc = 0;
621cee74f47SEric Paris struct avtab_node *cur;
622cee74f47SEric Paris __le32 buf[1];
623cee74f47SEric Paris
624cee74f47SEric Paris buf[0] = cpu_to_le32(a->nel);
625cee74f47SEric Paris rc = put_entry(buf, sizeof(u32), 1, fp);
626cee74f47SEric Paris if (rc)
627cee74f47SEric Paris return rc;
628cee74f47SEric Paris
629cee74f47SEric Paris for (i = 0; i < a->nslot; i++) {
630acdf52d9SKent Overstreet for (cur = a->htable[i]; cur;
631ba39db6eSStephen Smalley cur = cur->next) {
632cee74f47SEric Paris rc = avtab_write_item(p, cur, fp);
633cee74f47SEric Paris if (rc)
634cee74f47SEric Paris return rc;
635cee74f47SEric Paris }
636cee74f47SEric Paris }
637cee74f47SEric Paris
638cee74f47SEric Paris return rc;
639cee74f47SEric Paris }
640aa8e712cSStephen Smalley
avtab_cache_init(void)641aa8e712cSStephen Smalley void __init avtab_cache_init(void)
6421da177e4SLinus Torvalds {
6431da177e4SLinus Torvalds avtab_node_cachep = kmem_cache_create("avtab_node",
6441da177e4SLinus Torvalds sizeof(struct avtab_node),
64520c2df83SPaul Mundt 0, SLAB_PANIC, NULL);
646fa1aa143SJeff Vander Stoep avtab_xperms_cachep = kmem_cache_create("avtab_extended_perms",
647fa1aa143SJeff Vander Stoep sizeof(struct avtab_extended_perms),
648fa1aa143SJeff Vander Stoep 0, SLAB_PANIC, NULL);
6491da177e4SLinus Torvalds }
650