Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Paris | eb5df9a | 2008-04-18 17:38:28 -0400 | [diff] [blame] | 9 | * Added conditional policy language extensions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2003 Tresys Technology, LLC |
| 12 | * This program is free software; you can redistribute it and/or modify |
Eric Paris | eb5df9a | 2008-04-18 17:38:28 -0400 | [diff] [blame] | 13 | * it under the terms of the GNU General Public License as published by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * the Free Software Foundation, version 2. |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 15 | * |
| 16 | * Updated: Yuichi Nakamura <ynakam@hitachisoft.jp> |
Eric Paris | eb5df9a | 2008-04-18 17:38:28 -0400 | [diff] [blame] | 17 | * Tuned number of hash slots for avtab to reduce memory usage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "avtab.h" |
| 24 | #include "policydb.h" |
| 25 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 26 | static struct kmem_cache *avtab_node_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
John Brooks | 33ebc193 | 2015-03-24 16:54:17 -0400 | [diff] [blame^] | 28 | /* Based on MurmurHash3, written by Austin Appleby and placed in the |
| 29 | * public domain. |
| 30 | */ |
| 31 | static inline int avtab_hash(struct avtab_key *keyp, u32 mask) |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 32 | { |
John Brooks | 33ebc193 | 2015-03-24 16:54:17 -0400 | [diff] [blame^] | 33 | static const u32 c1 = 0xcc9e2d51; |
| 34 | static const u32 c2 = 0x1b873593; |
| 35 | static const u32 r1 = 15; |
| 36 | static const u32 r2 = 13; |
| 37 | static const u32 m = 5; |
| 38 | static const u32 n = 0xe6546b64; |
| 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 Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 65 | } |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static struct avtab_node* |
| 68 | avtab_insert_node(struct avtab *h, int hvalue, |
Eric Paris | eb5df9a | 2008-04-18 17:38:28 -0400 | [diff] [blame] | 69 | struct avtab_node *prev, struct avtab_node *cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | struct avtab_key *key, struct avtab_datum *datum) |
| 71 | { |
Eric Paris | eb5df9a | 2008-04-18 17:38:28 -0400 | [diff] [blame] | 72 | struct avtab_node *newnode; |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 73 | newnode = kmem_cache_zalloc(avtab_node_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | if (newnode == NULL) |
| 75 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | newnode->key = *key; |
| 77 | newnode->datum = *datum; |
| 78 | if (prev) { |
| 79 | newnode->next = prev->next; |
| 80 | prev->next = newnode; |
| 81 | } else { |
Stephen Smalley | ba39db6 | 2015-03-24 16:54:16 -0400 | [diff] [blame] | 82 | newnode->next = flex_array_get_ptr(h->htable, hvalue); |
| 83 | if (flex_array_put_ptr(h->htable, hvalue, newnode, |
| 84 | GFP_KERNEL|__GFP_ZERO)) { |
| 85 | kmem_cache_free(avtab_node_cachep, newnode); |
| 86 | return NULL; |
| 87 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | h->nel++; |
| 91 | return newnode; |
| 92 | } |
| 93 | |
| 94 | static 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 Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 98 | u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 100 | if (!h || !h->htable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | return -EINVAL; |
| 102 | |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 103 | hvalue = avtab_hash(key, h->mask); |
Stephen Smalley | ba39db6 | 2015-03-24 16:54:16 -0400 | [diff] [blame] | 104 | for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | 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 Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 110 | (specified & cur->key.specified)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | 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 Paris | eb5df9a | 2008-04-18 17:38:28 -0400 | [diff] [blame] | 124 | if (!newnode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | 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 | */ |
| 134 | struct avtab_node * |
Eric Paris | eb5df9a | 2008-04-18 17:38:28 -0400 | [diff] [blame] | 135 | avtab_insert_nonunique(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
| 137 | int hvalue; |
Vesa-Matti J Kari | 0c0e186 | 2008-07-21 02:50:20 +0300 | [diff] [blame] | 138 | struct avtab_node *prev, *cur; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 139 | u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 141 | if (!h || !h->htable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | return NULL; |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 143 | hvalue = avtab_hash(key, h->mask); |
Stephen Smalley | ba39db6 | 2015-03-24 16:54:16 -0400 | [diff] [blame] | 144 | for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | 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 Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 150 | (specified & cur->key.specified)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | 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 Kari | 0c0e186 | 2008-07-21 02:50:20 +0300 | [diff] [blame] | 162 | return avtab_insert_node(h, hvalue, prev, cur, key, datum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 165 | struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
| 167 | int hvalue; |
| 168 | struct avtab_node *cur; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 169 | u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 171 | if (!h || !h->htable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return NULL; |
| 173 | |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 174 | hvalue = avtab_hash(key, h->mask); |
Stephen Smalley | ba39db6 | 2015-03-24 16:54:16 -0400 | [diff] [blame] | 175 | for (cur = flex_array_get_ptr(h->htable, hvalue); cur; |
| 176 | cur = cur->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | if (key->source_type == cur->key.source_type && |
| 178 | key->target_type == cur->key.target_type && |
| 179 | key->target_class == cur->key.target_class && |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 180 | (specified & cur->key.specified)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | 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 | */ |
| 200 | struct avtab_node* |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 201 | avtab_search_node(struct avtab *h, struct avtab_key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 203 | int hvalue; |
| 204 | struct avtab_node *cur; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 205 | u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 207 | if (!h || !h->htable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return NULL; |
| 209 | |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 210 | hvalue = avtab_hash(key, h->mask); |
Stephen Smalley | ba39db6 | 2015-03-24 16:54:16 -0400 | [diff] [blame] | 211 | for (cur = flex_array_get_ptr(h->htable, hvalue); cur; |
| 212 | cur = cur->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | if (key->source_type == cur->key.source_type && |
| 214 | key->target_type == cur->key.target_type && |
| 215 | key->target_class == cur->key.target_class && |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 216 | (specified & cur->key.specified)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | 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 | |
| 232 | struct avtab_node* |
| 233 | avtab_search_node_next(struct avtab_node *node, int specified) |
| 234 | { |
| 235 | struct avtab_node *cur; |
| 236 | |
| 237 | if (!node) |
| 238 | return NULL; |
| 239 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 240 | specified &= ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | for (cur = node->next; cur; cur = cur->next) { |
| 242 | if (node->key.source_type == cur->key.source_type && |
| 243 | node->key.target_type == cur->key.target_type && |
| 244 | node->key.target_class == cur->key.target_class && |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 245 | (specified & cur->key.specified)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | 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 | |
| 261 | void 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 Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 269 | for (i = 0; i < h->nslot; i++) { |
Stephen Smalley | ba39db6 | 2015-03-24 16:54:16 -0400 | [diff] [blame] | 270 | cur = flex_array_get_ptr(h->htable, i); |
Vesa-Matti Kari | dbc74c6 | 2008-08-07 03:18:20 +0300 | [diff] [blame] | 271 | while (cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | temp = cur; |
| 273 | cur = cur->next; |
| 274 | kmem_cache_free(avtab_node_cachep, temp); |
| 275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
Stephen Smalley | ba39db6 | 2015-03-24 16:54:16 -0400 | [diff] [blame] | 277 | flex_array_free(h->htable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | h->htable = NULL; |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 279 | h->nslot = 0; |
| 280 | h->mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | int avtab_init(struct avtab *h) |
| 284 | { |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 285 | h->htable = NULL; |
| 286 | h->nel = 0; |
| 287 | return 0; |
| 288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 290 | int avtab_alloc(struct avtab *h, u32 nrules) |
| 291 | { |
John Brooks | 33ebc193 | 2015-03-24 16:54:17 -0400 | [diff] [blame^] | 292 | u32 mask = 0; |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 293 | 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 Paris | 00d85c8 | 2010-10-13 17:50:19 -0400 | [diff] [blame] | 307 | if (nslot > MAX_AVTAB_HASH_BUCKETS) |
| 308 | nslot = MAX_AVTAB_HASH_BUCKETS; |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 309 | mask = nslot - 1; |
| 310 | |
Stephen Smalley | ba39db6 | 2015-03-24 16:54:16 -0400 | [diff] [blame] | 311 | h->htable = flex_array_alloc(sizeof(struct avtab_node *), nslot, |
| 312 | GFP_KERNEL | __GFP_ZERO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | if (!h->htable) |
| 314 | return -ENOMEM; |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 315 | |
| 316 | avtab_alloc_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | h->nel = 0; |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 318 | h->nslot = nslot; |
| 319 | h->mask = mask; |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 320 | printk(KERN_DEBUG "SELinux: %d avtab hash slots, %d rules.\n", |
| 321 | h->nslot, nrules); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | void avtab_hash_eval(struct avtab *h, char *tag) |
| 326 | { |
| 327 | int i, chain_len, slots_used, max_chain_len; |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 328 | unsigned long long chain2_len_sum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | struct avtab_node *cur; |
| 330 | |
| 331 | slots_used = 0; |
| 332 | max_chain_len = 0; |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 333 | chain2_len_sum = 0; |
| 334 | for (i = 0; i < h->nslot; i++) { |
Stephen Smalley | ba39db6 | 2015-03-24 16:54:16 -0400 | [diff] [blame] | 335 | cur = flex_array_get_ptr(h->htable, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | 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 Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 346 | chain2_len_sum += chain_len * chain_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 350 | printk(KERN_DEBUG "SELinux: %s: %d entries and %d/%d buckets used, " |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 351 | "longest chain length %d sum of chain length^2 %llu\n", |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 352 | tag, h->nel, slots_used, h->nslot, max_chain_len, |
| 353 | chain2_len_sum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 356 | static 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 Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 365 | int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol, |
Eric Paris | eb5df9a | 2008-04-18 17:38:28 -0400 | [diff] [blame] | 366 | int (*insertf)(struct avtab *a, struct avtab_key *k, |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 367 | struct avtab_datum *d, void *p), |
| 368 | void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 370 | __le16 buf16[4]; |
| 371 | u16 enabled; |
| 372 | __le32 buf32[7]; |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 373 | u32 items, items2, val, vers = pol->policyvers; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 374 | struct avtab_key key; |
| 375 | struct avtab_datum datum; |
| 376 | int i, rc; |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 377 | unsigned set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 379 | memset(&key, 0, sizeof(struct avtab_key)); |
| 380 | memset(&datum, 0, sizeof(struct avtab_datum)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 382 | if (vers < POLICYDB_VERSION_AVTAB) { |
| 383 | rc = next_entry(buf32, fp, sizeof(u32)); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 384 | if (rc) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 385 | printk(KERN_ERR "SELinux: avtab: truncated entry\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 386 | return rc; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 387 | } |
| 388 | items2 = le32_to_cpu(buf32[0]); |
| 389 | if (items2 > ARRAY_SIZE(buf32)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 390 | printk(KERN_ERR "SELinux: avtab: entry overflow\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 391 | return -EINVAL; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 392 | |
| 393 | } |
| 394 | rc = next_entry(buf32, fp, sizeof(u32)*items2); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 395 | if (rc) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 396 | printk(KERN_ERR "SELinux: avtab: truncated entry\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 397 | return rc; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 398 | } |
| 399 | items = 0; |
| 400 | |
| 401 | val = le32_to_cpu(buf32[items++]); |
| 402 | key.source_type = (u16)val; |
| 403 | if (key.source_type != val) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 404 | printk(KERN_ERR "SELinux: avtab: truncated source type\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 405 | return -EINVAL; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 406 | } |
| 407 | val = le32_to_cpu(buf32[items++]); |
| 408 | key.target_type = (u16)val; |
| 409 | if (key.target_type != val) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 410 | printk(KERN_ERR "SELinux: avtab: truncated target type\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 411 | return -EINVAL; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 412 | } |
| 413 | val = le32_to_cpu(buf32[items++]); |
| 414 | key.target_class = (u16)val; |
| 415 | if (key.target_class != val) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 416 | printk(KERN_ERR "SELinux: avtab: truncated target class\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 417 | return -EINVAL; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 418 | } |
| 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 Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 424 | printk(KERN_ERR "SELinux: avtab: null entry\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 425 | return -EINVAL; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 426 | } |
| 427 | if ((val & AVTAB_AV) && |
| 428 | (val & AVTAB_TYPE)) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 429 | printk(KERN_ERR "SELinux: avtab: entry has both access vectors and types\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 430 | return -EINVAL; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Tobias Klauser | 32725ad | 2006-01-06 00:11:23 -0800 | [diff] [blame] | 433 | for (i = 0; i < ARRAY_SIZE(spec_order); i++) { |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 434 | if (val & spec_order[i]) { |
| 435 | key.specified = spec_order[i] | enabled; |
| 436 | datum.data = le32_to_cpu(buf32[items++]); |
| 437 | rc = insertf(a, &key, &datum, p); |
Eric Paris | eb5df9a | 2008-04-18 17:38:28 -0400 | [diff] [blame] | 438 | if (rc) |
| 439 | return rc; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | if (items != items2) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 444 | printk(KERN_ERR "SELinux: avtab: entry only had %d items, expected %d\n", items2, items); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 445 | return -EINVAL; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | rc = next_entry(buf16, fp, sizeof(u16)*4); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 451 | if (rc) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 452 | printk(KERN_ERR "SELinux: avtab: truncated entry\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 453 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | items = 0; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 457 | key.source_type = le16_to_cpu(buf16[items++]); |
| 458 | key.target_type = le16_to_cpu(buf16[items++]); |
| 459 | key.target_class = le16_to_cpu(buf16[items++]); |
| 460 | key.specified = le16_to_cpu(buf16[items++]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 462 | if (!policydb_type_isvalid(pol, key.source_type) || |
| 463 | !policydb_type_isvalid(pol, key.target_type) || |
| 464 | !policydb_class_isvalid(pol, key.target_class)) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 465 | printk(KERN_ERR "SELinux: avtab: invalid type or class\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 466 | return -EINVAL; |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 467 | } |
| 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 Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 475 | printk(KERN_ERR "SELinux: avtab: more than one specifier\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 476 | return -EINVAL; |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 477 | } |
| 478 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 479 | rc = next_entry(buf32, fp, sizeof(u32)); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 480 | if (rc) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 481 | printk(KERN_ERR "SELinux: avtab: truncated entry\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 482 | return rc; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 483 | } |
| 484 | datum.data = le32_to_cpu(*buf32); |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 485 | if ((key.specified & AVTAB_TYPE) && |
| 486 | !policydb_type_isvalid(pol, datum.data)) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 487 | printk(KERN_ERR "SELinux: avtab: invalid type\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 488 | return -EINVAL; |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 489 | } |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 490 | return insertf(a, &key, &datum, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 493 | static 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 Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 499 | int avtab_read(struct avtab *a, void *fp, struct policydb *pol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | { |
| 501 | int rc; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 502 | __le32 buf[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | u32 nel, i; |
| 504 | |
| 505 | |
| 506 | rc = next_entry(buf, fp, sizeof(u32)); |
| 507 | if (rc < 0) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 508 | printk(KERN_ERR "SELinux: avtab: truncated table\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | goto bad; |
| 510 | } |
| 511 | nel = le32_to_cpu(buf[0]); |
| 512 | if (!nel) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 513 | printk(KERN_ERR "SELinux: avtab: table is empty\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | rc = -EINVAL; |
| 515 | goto bad; |
| 516 | } |
Yuichi Nakamura | 3232c11 | 2007-08-24 11:55:11 +0900 | [diff] [blame] | 517 | |
| 518 | rc = avtab_alloc(a, nel); |
| 519 | if (rc) |
| 520 | goto bad; |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | for (i = 0; i < nel; i++) { |
Stephen Smalley | 45e5421 | 2007-11-07 10:08:00 -0500 | [diff] [blame] | 523 | rc = avtab_read_item(a, fp, pol, avtab_insertf, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | if (rc) { |
| 525 | if (rc == -ENOMEM) |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 526 | printk(KERN_ERR "SELinux: avtab: out of memory\n"); |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 527 | else if (rc == -EEXIST) |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 528 | printk(KERN_ERR "SELinux: avtab: duplicate entry\n"); |
Dan Carpenter | 9e0bd4c | 2010-06-12 20:50:35 +0200 | [diff] [blame] | 529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | goto bad; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | rc = 0; |
| 535 | out: |
| 536 | return rc; |
| 537 | |
| 538 | bad: |
| 539 | avtab_destroy(a); |
| 540 | goto out; |
| 541 | } |
| 542 | |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame] | 543 | int 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 | |
| 563 | int 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 Smalley | ba39db6 | 2015-03-24 16:54:16 -0400 | [diff] [blame] | 576 | for (cur = flex_array_get_ptr(a->htable, i); cur; |
| 577 | cur = cur->next) { |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame] | 578 | rc = avtab_write_item(p, cur, fp); |
| 579 | if (rc) |
| 580 | return rc; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | return rc; |
| 585 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | void avtab_cache_init(void) |
| 587 | { |
| 588 | avtab_node_cachep = kmem_cache_create("avtab_node", |
| 589 | sizeof(struct avtab_node), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 590 | 0, SLAB_PANIC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | void avtab_cache_destroy(void) |
| 594 | { |
Eric Paris | eb5df9a | 2008-04-18 17:38:28 -0400 | [diff] [blame] | 595 | kmem_cache_destroy(avtab_node_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |