Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Implementation of the SID table type. |
| 3 | * |
Stephen Smalley | 7efbb60 | 2017-08-17 13:32:36 -0400 | [diff] [blame^] | 4 | * Author : Stephen Smalley, <sds@tycho.nsa.gov> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/spinlock.h> |
| 9 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include "flask.h" |
| 11 | #include "security.h" |
| 12 | #include "sidtab.h" |
| 13 | |
| 14 | #define SIDTAB_HASH(sid) \ |
| 15 | (sid & SIDTAB_HASH_MASK) |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | int sidtab_init(struct sidtab *s) |
| 18 | { |
| 19 | int i; |
| 20 | |
Markus Elfring | b380f78 | 2017-01-15 13:13:19 +0100 | [diff] [blame] | 21 | s->htable = kmalloc_array(SIDTAB_SIZE, sizeof(*s->htable), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | if (!s->htable) |
| 23 | return -ENOMEM; |
| 24 | for (i = 0; i < SIDTAB_SIZE; i++) |
| 25 | s->htable[i] = NULL; |
| 26 | s->nel = 0; |
| 27 | s->next_sid = 1; |
| 28 | s->shutdown = 0; |
James Morris | bdd581c | 2008-06-06 18:50:12 +1000 | [diff] [blame] | 29 | spin_lock_init(&s->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | int sidtab_insert(struct sidtab *s, u32 sid, struct context *context) |
| 34 | { |
Markus Elfring | 46be14d | 2017-04-04 11:33:53 +0200 | [diff] [blame] | 35 | int hvalue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | struct sidtab_node *prev, *cur, *newnode; |
| 37 | |
Markus Elfring | 46be14d | 2017-04-04 11:33:53 +0200 | [diff] [blame] | 38 | if (!s) |
| 39 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | hvalue = SIDTAB_HASH(sid); |
| 42 | prev = NULL; |
| 43 | cur = s->htable[hvalue]; |
Vesa-Matti Kari | dbc74c6 | 2008-08-07 03:18:20 +0300 | [diff] [blame] | 44 | while (cur && sid > cur->sid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | prev = cur; |
| 46 | cur = cur->next; |
| 47 | } |
| 48 | |
Markus Elfring | 46be14d | 2017-04-04 11:33:53 +0200 | [diff] [blame] | 49 | if (cur && sid == cur->sid) |
| 50 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | newnode = kmalloc(sizeof(*newnode), GFP_ATOMIC); |
Markus Elfring | 46be14d | 2017-04-04 11:33:53 +0200 | [diff] [blame] | 53 | if (!newnode) |
| 54 | return -ENOMEM; |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | newnode->sid = sid; |
| 57 | if (context_cpy(&newnode->context, context)) { |
| 58 | kfree(newnode); |
Markus Elfring | 46be14d | 2017-04-04 11:33:53 +0200 | [diff] [blame] | 59 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | if (prev) { |
| 63 | newnode->next = prev->next; |
| 64 | wmb(); |
| 65 | prev->next = newnode; |
| 66 | } else { |
| 67 | newnode->next = s->htable[hvalue]; |
| 68 | wmb(); |
| 69 | s->htable[hvalue] = newnode; |
| 70 | } |
| 71 | |
| 72 | s->nel++; |
| 73 | if (sid >= s->next_sid) |
| 74 | s->next_sid = sid + 1; |
Markus Elfring | 46be14d | 2017-04-04 11:33:53 +0200 | [diff] [blame] | 75 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 78 | static struct context *sidtab_search_core(struct sidtab *s, u32 sid, int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
| 80 | int hvalue; |
| 81 | struct sidtab_node *cur; |
| 82 | |
| 83 | if (!s) |
| 84 | return NULL; |
| 85 | |
| 86 | hvalue = SIDTAB_HASH(sid); |
| 87 | cur = s->htable[hvalue]; |
Vesa-Matti Kari | dbc74c6 | 2008-08-07 03:18:20 +0300 | [diff] [blame] | 88 | while (cur && sid > cur->sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | cur = cur->next; |
| 90 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 91 | if (force && cur && sid == cur->sid && cur->context.len) |
| 92 | return &cur->context; |
| 93 | |
Markus Elfring | 8ee4586 | 2017-01-15 13:30:20 +0100 | [diff] [blame] | 94 | if (!cur || sid != cur->sid || cur->context.len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | /* Remap invalid SIDs to the unlabeled SID. */ |
| 96 | sid = SECINITSID_UNLABELED; |
| 97 | hvalue = SIDTAB_HASH(sid); |
| 98 | cur = s->htable[hvalue]; |
Vesa-Matti Kari | dbc74c6 | 2008-08-07 03:18:20 +0300 | [diff] [blame] | 99 | while (cur && sid > cur->sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | cur = cur->next; |
| 101 | if (!cur || sid != cur->sid) |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | return &cur->context; |
| 106 | } |
| 107 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 108 | struct context *sidtab_search(struct sidtab *s, u32 sid) |
| 109 | { |
| 110 | return sidtab_search_core(s, sid, 0); |
| 111 | } |
| 112 | |
| 113 | struct context *sidtab_search_force(struct sidtab *s, u32 sid) |
| 114 | { |
| 115 | return sidtab_search_core(s, sid, 1); |
| 116 | } |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | int sidtab_map(struct sidtab *s, |
| 119 | int (*apply) (u32 sid, |
| 120 | struct context *context, |
| 121 | void *args), |
| 122 | void *args) |
| 123 | { |
| 124 | int i, rc = 0; |
| 125 | struct sidtab_node *cur; |
| 126 | |
| 127 | if (!s) |
| 128 | goto out; |
| 129 | |
| 130 | for (i = 0; i < SIDTAB_SIZE; i++) { |
| 131 | cur = s->htable[i]; |
Vesa-Matti Kari | dbc74c6 | 2008-08-07 03:18:20 +0300 | [diff] [blame] | 132 | while (cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | rc = apply(cur->sid, &cur->context, args); |
| 134 | if (rc) |
| 135 | goto out; |
| 136 | cur = cur->next; |
| 137 | } |
| 138 | } |
| 139 | out: |
| 140 | return rc; |
| 141 | } |
| 142 | |
Eric Paris | 73ff5fc0 | 2010-12-07 16:17:28 -0500 | [diff] [blame] | 143 | static void sidtab_update_cache(struct sidtab *s, struct sidtab_node *n, int loc) |
| 144 | { |
| 145 | BUG_ON(loc >= SIDTAB_CACHE_LEN); |
| 146 | |
| 147 | while (loc > 0) { |
| 148 | s->cache[loc] = s->cache[loc - 1]; |
| 149 | loc--; |
| 150 | } |
| 151 | s->cache[0] = n; |
| 152 | } |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | static inline u32 sidtab_search_context(struct sidtab *s, |
| 155 | struct context *context) |
| 156 | { |
| 157 | int i; |
| 158 | struct sidtab_node *cur; |
| 159 | |
| 160 | for (i = 0; i < SIDTAB_SIZE; i++) { |
| 161 | cur = s->htable[i]; |
Vesa-Matti Kari | dbc74c6 | 2008-08-07 03:18:20 +0300 | [diff] [blame] | 162 | while (cur) { |
Eric Paris | 73ff5fc0 | 2010-12-07 16:17:28 -0500 | [diff] [blame] | 163 | if (context_cmp(&cur->context, context)) { |
| 164 | sidtab_update_cache(s, cur, SIDTAB_CACHE_LEN - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | return cur->sid; |
Eric Paris | 73ff5fc0 | 2010-12-07 16:17:28 -0500 | [diff] [blame] | 166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | cur = cur->next; |
| 168 | } |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
Eric Paris | 73ff5fc0 | 2010-12-07 16:17:28 -0500 | [diff] [blame] | 173 | static inline u32 sidtab_search_cache(struct sidtab *s, struct context *context) |
| 174 | { |
| 175 | int i; |
| 176 | struct sidtab_node *node; |
| 177 | |
| 178 | for (i = 0; i < SIDTAB_CACHE_LEN; i++) { |
| 179 | node = s->cache[i]; |
| 180 | if (unlikely(!node)) |
| 181 | return 0; |
| 182 | if (context_cmp(&node->context, context)) { |
| 183 | sidtab_update_cache(s, node, i); |
| 184 | return node->sid; |
| 185 | } |
| 186 | } |
| 187 | return 0; |
| 188 | } |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | int sidtab_context_to_sid(struct sidtab *s, |
| 191 | struct context *context, |
| 192 | u32 *out_sid) |
| 193 | { |
| 194 | u32 sid; |
| 195 | int ret = 0; |
| 196 | unsigned long flags; |
| 197 | |
| 198 | *out_sid = SECSID_NULL; |
| 199 | |
Eric Paris | 73ff5fc0 | 2010-12-07 16:17:28 -0500 | [diff] [blame] | 200 | sid = sidtab_search_cache(s, context); |
| 201 | if (!sid) |
| 202 | sid = sidtab_search_context(s, context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | if (!sid) { |
James Morris | bdd581c | 2008-06-06 18:50:12 +1000 | [diff] [blame] | 204 | spin_lock_irqsave(&s->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | /* Rescan now that we hold the lock. */ |
| 206 | sid = sidtab_search_context(s, context); |
| 207 | if (sid) |
| 208 | goto unlock_out; |
| 209 | /* No SID exists for the context. Allocate a new one. */ |
| 210 | if (s->next_sid == UINT_MAX || s->shutdown) { |
| 211 | ret = -ENOMEM; |
| 212 | goto unlock_out; |
| 213 | } |
| 214 | sid = s->next_sid++; |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 215 | if (context->len) |
| 216 | printk(KERN_INFO |
| 217 | "SELinux: Context %s is not valid (left unmapped).\n", |
| 218 | context->str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | ret = sidtab_insert(s, sid, context); |
| 220 | if (ret) |
| 221 | s->next_sid--; |
| 222 | unlock_out: |
James Morris | bdd581c | 2008-06-06 18:50:12 +1000 | [diff] [blame] | 223 | spin_unlock_irqrestore(&s->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (ret) |
| 227 | return ret; |
| 228 | |
| 229 | *out_sid = sid; |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | void sidtab_hash_eval(struct sidtab *h, char *tag) |
| 234 | { |
| 235 | int i, chain_len, slots_used, max_chain_len; |
| 236 | struct sidtab_node *cur; |
| 237 | |
| 238 | slots_used = 0; |
| 239 | max_chain_len = 0; |
| 240 | for (i = 0; i < SIDTAB_SIZE; i++) { |
| 241 | cur = h->htable[i]; |
| 242 | if (cur) { |
| 243 | slots_used++; |
| 244 | chain_len = 0; |
| 245 | while (cur) { |
| 246 | chain_len++; |
| 247 | cur = cur->next; |
| 248 | } |
| 249 | |
| 250 | if (chain_len > max_chain_len) |
| 251 | max_chain_len = chain_len; |
| 252 | } |
| 253 | } |
| 254 | |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 255 | printk(KERN_DEBUG "%s: %d entries and %d/%d buckets used, longest " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | "chain length %d\n", tag, h->nel, slots_used, SIDTAB_SIZE, |
| 257 | max_chain_len); |
| 258 | } |
| 259 | |
| 260 | void sidtab_destroy(struct sidtab *s) |
| 261 | { |
| 262 | int i; |
| 263 | struct sidtab_node *cur, *temp; |
| 264 | |
| 265 | if (!s) |
| 266 | return; |
| 267 | |
| 268 | for (i = 0; i < SIDTAB_SIZE; i++) { |
| 269 | cur = s->htable[i]; |
Vesa-Matti Kari | dbc74c6 | 2008-08-07 03:18:20 +0300 | [diff] [blame] | 270 | while (cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | temp = cur; |
| 272 | cur = cur->next; |
| 273 | context_destroy(&temp->context); |
| 274 | kfree(temp); |
| 275 | } |
| 276 | s->htable[i] = NULL; |
| 277 | } |
| 278 | kfree(s->htable); |
| 279 | s->htable = NULL; |
| 280 | s->nel = 0; |
| 281 | s->next_sid = 1; |
| 282 | } |
| 283 | |
| 284 | void sidtab_set(struct sidtab *dst, struct sidtab *src) |
| 285 | { |
| 286 | unsigned long flags; |
Eric Paris | 73ff5fc0 | 2010-12-07 16:17:28 -0500 | [diff] [blame] | 287 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
James Morris | bdd581c | 2008-06-06 18:50:12 +1000 | [diff] [blame] | 289 | spin_lock_irqsave(&src->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | dst->htable = src->htable; |
| 291 | dst->nel = src->nel; |
| 292 | dst->next_sid = src->next_sid; |
| 293 | dst->shutdown = 0; |
Eric Paris | 73ff5fc0 | 2010-12-07 16:17:28 -0500 | [diff] [blame] | 294 | for (i = 0; i < SIDTAB_CACHE_LEN; i++) |
| 295 | dst->cache[i] = NULL; |
James Morris | bdd581c | 2008-06-06 18:50:12 +1000 | [diff] [blame] | 296 | spin_unlock_irqrestore(&src->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void sidtab_shutdown(struct sidtab *s) |
| 300 | { |
| 301 | unsigned long flags; |
| 302 | |
James Morris | bdd581c | 2008-06-06 18:50:12 +1000 | [diff] [blame] | 303 | spin_lock_irqsave(&s->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | s->shutdown = 1; |
James Morris | bdd581c | 2008-06-06 18:50:12 +1000 | [diff] [blame] | 305 | spin_unlock_irqrestore(&s->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |