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