Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Implementation of the SID table type. |
| 4 | * |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 5 | * Original author: Stephen Smalley, <sds@tycho.nsa.gov> |
| 6 | * Author: Ondrej Mosnacek, <omosnacek@gmail.com> |
| 7 | * |
| 8 | * Copyright (C) 2018 Red Hat, Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 10 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 12 | #include <linux/list.h> |
| 13 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/slab.h> |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 15 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/spinlock.h> |
Ondrej Mosnacek | 116f21b | 2019-08-14 15:33:20 +0200 | [diff] [blame] | 17 | #include <asm/barrier.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "flask.h" |
| 19 | #include "security.h" |
| 20 | #include "sidtab.h" |
| 21 | |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 22 | struct sidtab_str_cache { |
| 23 | struct rcu_head rcu_member; |
| 24 | struct list_head lru_member; |
| 25 | struct sidtab_entry *parent; |
| 26 | u32 len; |
| 27 | char str[]; |
| 28 | }; |
| 29 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 30 | #define index_to_sid(index) (index + SECINITSID_NUM + 1) |
| 31 | #define sid_to_index(sid) (sid - (SECINITSID_NUM + 1)) |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | int sidtab_init(struct sidtab *s) |
| 34 | { |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 35 | u32 i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 37 | memset(s->roots, 0, sizeof(s->roots)); |
| 38 | |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 39 | for (i = 0; i < SECINITSID_NUM; i++) |
| 40 | s->isids[i].set = 0; |
| 41 | |
Ondrej Mosnacek | 116f21b | 2019-08-14 15:33:20 +0200 | [diff] [blame] | 42 | s->count = 0; |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 43 | s->convert = NULL; |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 44 | hash_init(s->context_to_sid); |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 45 | |
James Morris | bdd581c | 2008-06-06 18:50:12 +1000 | [diff] [blame] | 46 | spin_lock_init(&s->lock); |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 47 | |
| 48 | #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0 |
| 49 | s->cache_free_slots = CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE; |
| 50 | INIT_LIST_HEAD(&s->cache_lru_list); |
| 51 | spin_lock_init(&s->cache_lock); |
| 52 | #endif |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 57 | static u32 context_to_sid(struct sidtab *s, struct context *context) |
| 58 | { |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 59 | struct sidtab_entry *entry; |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 60 | u32 sid = 0; |
| 61 | |
| 62 | rcu_read_lock(); |
| 63 | hash_for_each_possible_rcu(s->context_to_sid, entry, list, |
| 64 | context->hash) { |
| 65 | if (context_cmp(&entry->context, context)) { |
| 66 | sid = entry->sid; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | rcu_read_unlock(); |
| 71 | return sid; |
| 72 | } |
| 73 | |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 74 | int sidtab_set_initial(struct sidtab *s, u32 sid, struct context *context) |
| 75 | { |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 76 | struct sidtab_isid_entry *isid; |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 77 | int rc; |
| 78 | |
| 79 | if (sid == 0 || sid > SECINITSID_NUM) |
| 80 | return -EINVAL; |
| 81 | |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 82 | isid = &s->isids[sid - 1]; |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 83 | |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 84 | rc = context_cpy(&isid->entry.context, context); |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 85 | if (rc) |
| 86 | return rc; |
| 87 | |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 88 | #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0 |
| 89 | isid->entry.cache = NULL; |
| 90 | #endif |
| 91 | isid->set = 1; |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Multiple initial sids may map to the same context. Check that this |
| 95 | * context is not already represented in the context_to_sid hashtable |
| 96 | * to avoid duplicate entries and long linked lists upon hash |
| 97 | * collision. |
| 98 | */ |
| 99 | if (!context_to_sid(s, context)) { |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 100 | isid->entry.sid = sid; |
| 101 | hash_add(s->context_to_sid, &isid->entry.list, context->hash); |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 107 | int sidtab_hash_stats(struct sidtab *sidtab, char *page) |
| 108 | { |
| 109 | int i; |
| 110 | int chain_len = 0; |
| 111 | int slots_used = 0; |
| 112 | int entries = 0; |
| 113 | int max_chain_len = 0; |
| 114 | int cur_bucket = 0; |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 115 | struct sidtab_entry *entry; |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 116 | |
| 117 | rcu_read_lock(); |
| 118 | hash_for_each_rcu(sidtab->context_to_sid, i, entry, list) { |
| 119 | entries++; |
| 120 | if (i == cur_bucket) { |
| 121 | chain_len++; |
| 122 | if (chain_len == 1) |
| 123 | slots_used++; |
| 124 | } else { |
| 125 | cur_bucket = i; |
| 126 | if (chain_len > max_chain_len) |
| 127 | max_chain_len = chain_len; |
| 128 | chain_len = 0; |
| 129 | } |
| 130 | } |
| 131 | rcu_read_unlock(); |
| 132 | |
| 133 | if (chain_len > max_chain_len) |
| 134 | max_chain_len = chain_len; |
| 135 | |
| 136 | return scnprintf(page, PAGE_SIZE, "entries: %d\nbuckets used: %d/%d\n" |
| 137 | "longest chain: %d\n", entries, |
| 138 | slots_used, SIDTAB_HASH_BUCKETS, max_chain_len); |
| 139 | } |
| 140 | |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 141 | static u32 sidtab_level_from_count(u32 count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 143 | u32 capacity = SIDTAB_LEAF_ENTRIES; |
| 144 | u32 level = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 146 | while (count > capacity) { |
| 147 | capacity <<= SIDTAB_INNER_SHIFT; |
| 148 | ++level; |
| 149 | } |
| 150 | return level; |
| 151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 153 | static int sidtab_alloc_roots(struct sidtab *s, u32 level) |
| 154 | { |
| 155 | u32 l; |
| 156 | |
| 157 | if (!s->roots[0].ptr_leaf) { |
| 158 | s->roots[0].ptr_leaf = kzalloc(SIDTAB_NODE_ALLOC_SIZE, |
| 159 | GFP_ATOMIC); |
| 160 | if (!s->roots[0].ptr_leaf) |
| 161 | return -ENOMEM; |
| 162 | } |
| 163 | for (l = 1; l <= level; ++l) |
| 164 | if (!s->roots[l].ptr_inner) { |
| 165 | s->roots[l].ptr_inner = kzalloc(SIDTAB_NODE_ALLOC_SIZE, |
| 166 | GFP_ATOMIC); |
| 167 | if (!s->roots[l].ptr_inner) |
| 168 | return -ENOMEM; |
| 169 | s->roots[l].ptr_inner->entries[0] = s->roots[l - 1]; |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 174 | static struct sidtab_entry *sidtab_do_lookup(struct sidtab *s, u32 index, |
| 175 | int alloc) |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 176 | { |
| 177 | union sidtab_entry_inner *entry; |
| 178 | u32 level, capacity_shift, leaf_index = index / SIDTAB_LEAF_ENTRIES; |
| 179 | |
| 180 | /* find the level of the subtree we need */ |
| 181 | level = sidtab_level_from_count(index + 1); |
| 182 | capacity_shift = level * SIDTAB_INNER_SHIFT; |
| 183 | |
| 184 | /* allocate roots if needed */ |
| 185 | if (alloc && sidtab_alloc_roots(s, level) != 0) |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 186 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 188 | /* lookup inside the subtree */ |
| 189 | entry = &s->roots[level]; |
| 190 | while (level != 0) { |
| 191 | capacity_shift -= SIDTAB_INNER_SHIFT; |
| 192 | --level; |
| 193 | |
| 194 | entry = &entry->ptr_inner->entries[leaf_index >> capacity_shift]; |
| 195 | leaf_index &= ((u32)1 << capacity_shift) - 1; |
| 196 | |
| 197 | if (!entry->ptr_inner) { |
| 198 | if (alloc) |
| 199 | entry->ptr_inner = kzalloc(SIDTAB_NODE_ALLOC_SIZE, |
| 200 | GFP_ATOMIC); |
| 201 | if (!entry->ptr_inner) |
| 202 | return NULL; |
| 203 | } |
| 204 | } |
| 205 | if (!entry->ptr_leaf) { |
| 206 | if (alloc) |
| 207 | entry->ptr_leaf = kzalloc(SIDTAB_NODE_ALLOC_SIZE, |
| 208 | GFP_ATOMIC); |
| 209 | if (!entry->ptr_leaf) |
| 210 | return NULL; |
| 211 | } |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 212 | return &entry->ptr_leaf->entries[index % SIDTAB_LEAF_ENTRIES]; |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 215 | static struct sidtab_entry *sidtab_lookup(struct sidtab *s, u32 index) |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 216 | { |
Ondrej Mosnacek | 116f21b | 2019-08-14 15:33:20 +0200 | [diff] [blame] | 217 | /* read entries only after reading count */ |
| 218 | u32 count = smp_load_acquire(&s->count); |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 219 | |
| 220 | if (index >= count) |
| 221 | return NULL; |
| 222 | |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 223 | return sidtab_do_lookup(s, index, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 226 | static struct sidtab_entry *sidtab_lookup_initial(struct sidtab *s, u32 sid) |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 227 | { |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 228 | return s->isids[sid - 1].set ? &s->isids[sid - 1].entry : NULL; |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 231 | static struct sidtab_entry *sidtab_search_core(struct sidtab *s, u32 sid, |
| 232 | int force) |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 233 | { |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 234 | if (sid != 0) { |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 235 | struct sidtab_entry *entry; |
| 236 | |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 237 | if (sid > SECINITSID_NUM) |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 238 | entry = sidtab_lookup(s, sid_to_index(sid)); |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 239 | else |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 240 | entry = sidtab_lookup_initial(s, sid); |
| 241 | if (entry && (!entry->context.len || force)) |
| 242 | return entry; |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return sidtab_lookup_initial(s, SECINITSID_UNLABELED); |
| 246 | } |
| 247 | |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 248 | struct sidtab_entry *sidtab_search_entry(struct sidtab *s, u32 sid) |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 249 | { |
| 250 | return sidtab_search_core(s, sid, 0); |
| 251 | } |
| 252 | |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 253 | struct sidtab_entry *sidtab_search_entry_force(struct sidtab *s, u32 sid) |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 254 | { |
| 255 | return sidtab_search_core(s, sid, 1); |
| 256 | } |
| 257 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 258 | int sidtab_context_to_sid(struct sidtab *s, struct context *context, |
| 259 | u32 *sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | unsigned long flags; |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 262 | u32 count; |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 263 | struct sidtab_convert_params *convert; |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 264 | struct sidtab_entry *dst, *dst_convert; |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 265 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 267 | *sid = context_to_sid(s, context); |
| 268 | if (*sid) |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 269 | return 0; |
| 270 | |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 271 | /* lock-free search failed: lock, re-search, and insert if not found */ |
| 272 | spin_lock_irqsave(&s->lock, flags); |
| 273 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 274 | rc = 0; |
| 275 | *sid = context_to_sid(s, context); |
| 276 | if (*sid) |
| 277 | goto out_unlock; |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 278 | |
Ondrej Mosnacek | 433e3aa | 2020-04-08 11:08:08 +0200 | [diff] [blame^] | 279 | count = s->count; |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 280 | convert = s->convert; |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 281 | |
Ondrej Mosnacek | acbc372 | 2019-07-23 08:50:59 +0200 | [diff] [blame] | 282 | /* bail out if we already reached max entries */ |
| 283 | rc = -EOVERFLOW; |
| 284 | if (count >= SIDTAB_MAX) |
| 285 | goto out_unlock; |
| 286 | |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 287 | /* insert context into new entry */ |
| 288 | rc = -ENOMEM; |
| 289 | dst = sidtab_do_lookup(s, count, 1); |
| 290 | if (!dst) |
| 291 | goto out_unlock; |
| 292 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 293 | dst->sid = index_to_sid(count); |
| 294 | |
| 295 | rc = context_cpy(&dst->context, context); |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 296 | if (rc) |
| 297 | goto out_unlock; |
| 298 | |
| 299 | /* |
| 300 | * if we are building a new sidtab, we need to convert the context |
| 301 | * and insert it there as well |
| 302 | */ |
| 303 | if (convert) { |
| 304 | rc = -ENOMEM; |
| 305 | dst_convert = sidtab_do_lookup(convert->target, count, 1); |
| 306 | if (!dst_convert) { |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 307 | context_destroy(&dst->context); |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 308 | goto out_unlock; |
| 309 | } |
| 310 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 311 | rc = convert->func(context, &dst_convert->context, |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 312 | convert->args); |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 313 | if (rc) { |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 314 | context_destroy(&dst->context); |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 315 | goto out_unlock; |
| 316 | } |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 317 | dst_convert->sid = index_to_sid(count); |
Ondrej Mosnacek | 116f21b | 2019-08-14 15:33:20 +0200 | [diff] [blame] | 318 | convert->target->count = count + 1; |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 319 | |
| 320 | hash_add_rcu(convert->target->context_to_sid, |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 321 | &dst_convert->list, dst_convert->context.hash); |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | if (context->len) |
| 325 | pr_info("SELinux: Context %s is not valid (left unmapped).\n", |
| 326 | context->str); |
| 327 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 328 | *sid = index_to_sid(count); |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 329 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 330 | /* write entries before updating count */ |
Ondrej Mosnacek | 116f21b | 2019-08-14 15:33:20 +0200 | [diff] [blame] | 331 | smp_store_release(&s->count, count + 1); |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 332 | hash_add_rcu(s->context_to_sid, &dst->list, dst->context.hash); |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 333 | |
| 334 | rc = 0; |
| 335 | out_unlock: |
| 336 | spin_unlock_irqrestore(&s->lock, flags); |
| 337 | return rc; |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 338 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 340 | static void sidtab_convert_hashtable(struct sidtab *s, u32 count) |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 341 | { |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 342 | struct sidtab_entry *entry; |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 343 | u32 i; |
| 344 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 345 | for (i = 0; i < count; i++) { |
| 346 | entry = sidtab_do_lookup(s, i, 0); |
| 347 | entry->sid = index_to_sid(i); |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 348 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 349 | hash_add_rcu(s->context_to_sid, &entry->list, |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 350 | entry->context.hash); |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 351 | |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 355 | static int sidtab_convert_tree(union sidtab_entry_inner *edst, |
| 356 | union sidtab_entry_inner *esrc, |
| 357 | u32 *pos, u32 count, u32 level, |
| 358 | struct sidtab_convert_params *convert) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 360 | int rc; |
| 361 | u32 i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 363 | if (level != 0) { |
| 364 | if (!edst->ptr_inner) { |
| 365 | edst->ptr_inner = kzalloc(SIDTAB_NODE_ALLOC_SIZE, |
| 366 | GFP_KERNEL); |
| 367 | if (!edst->ptr_inner) |
| 368 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 370 | i = 0; |
| 371 | while (i < SIDTAB_INNER_ENTRIES && *pos < count) { |
| 372 | rc = sidtab_convert_tree(&edst->ptr_inner->entries[i], |
| 373 | &esrc->ptr_inner->entries[i], |
| 374 | pos, count, level - 1, |
| 375 | convert); |
| 376 | if (rc) |
| 377 | return rc; |
| 378 | i++; |
| 379 | } |
| 380 | } else { |
| 381 | if (!edst->ptr_leaf) { |
| 382 | edst->ptr_leaf = kzalloc(SIDTAB_NODE_ALLOC_SIZE, |
| 383 | GFP_KERNEL); |
| 384 | if (!edst->ptr_leaf) |
| 385 | return -ENOMEM; |
| 386 | } |
| 387 | i = 0; |
| 388 | while (i < SIDTAB_LEAF_ENTRIES && *pos < count) { |
| 389 | rc = convert->func(&esrc->ptr_leaf->entries[i].context, |
| 390 | &edst->ptr_leaf->entries[i].context, |
| 391 | convert->args); |
| 392 | if (rc) |
| 393 | return rc; |
| 394 | (*pos)++; |
| 395 | i++; |
| 396 | } |
| 397 | cond_resched(); |
| 398 | } |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | int sidtab_convert(struct sidtab *s, struct sidtab_convert_params *params) |
| 403 | { |
| 404 | unsigned long flags; |
| 405 | u32 count, level, pos; |
| 406 | int rc; |
| 407 | |
| 408 | spin_lock_irqsave(&s->lock, flags); |
| 409 | |
| 410 | /* concurrent policy loads are not allowed */ |
| 411 | if (s->convert) { |
| 412 | spin_unlock_irqrestore(&s->lock, flags); |
| 413 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Ondrej Mosnacek | 116f21b | 2019-08-14 15:33:20 +0200 | [diff] [blame] | 416 | count = s->count; |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 417 | level = sidtab_level_from_count(count); |
| 418 | |
| 419 | /* allocate last leaf in the new sidtab (to avoid race with |
| 420 | * live convert) |
| 421 | */ |
| 422 | rc = sidtab_do_lookup(params->target, count - 1, 1) ? 0 : -ENOMEM; |
| 423 | if (rc) { |
| 424 | spin_unlock_irqrestore(&s->lock, flags); |
| 425 | return rc; |
| 426 | } |
| 427 | |
| 428 | /* set count in case no new entries are added during conversion */ |
Ondrej Mosnacek | 116f21b | 2019-08-14 15:33:20 +0200 | [diff] [blame] | 429 | params->target->count = count; |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 430 | |
| 431 | /* enable live convert of new entries */ |
| 432 | s->convert = params; |
| 433 | |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 434 | /* we can safely convert the tree outside the lock */ |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 435 | spin_unlock_irqrestore(&s->lock, flags); |
| 436 | |
| 437 | pr_info("SELinux: Converting %u SID table entries...\n", count); |
| 438 | |
| 439 | /* convert all entries not covered by live convert */ |
| 440 | pos = 0; |
| 441 | rc = sidtab_convert_tree(¶ms->target->roots[level], |
| 442 | &s->roots[level], &pos, count, level, params); |
| 443 | if (rc) { |
| 444 | /* we need to keep the old table - disable live convert */ |
| 445 | spin_lock_irqsave(&s->lock, flags); |
| 446 | s->convert = NULL; |
| 447 | spin_unlock_irqrestore(&s->lock, flags); |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 448 | return rc; |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 449 | } |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 450 | /* |
| 451 | * The hashtable can also be modified in sidtab_context_to_sid() |
| 452 | * so we must re-acquire the lock here. |
| 453 | */ |
| 454 | spin_lock_irqsave(&s->lock, flags); |
| 455 | sidtab_convert_hashtable(params->target, count); |
| 456 | spin_unlock_irqrestore(&s->lock, flags); |
| 457 | |
| 458 | return 0; |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 459 | } |
| 460 | |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 461 | static void sidtab_destroy_entry(struct sidtab_entry *entry) |
| 462 | { |
| 463 | context_destroy(&entry->context); |
| 464 | #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0 |
| 465 | kfree(rcu_dereference_raw(entry->cache)); |
| 466 | #endif |
| 467 | } |
| 468 | |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 469 | static void sidtab_destroy_tree(union sidtab_entry_inner entry, u32 level) |
| 470 | { |
| 471 | u32 i; |
| 472 | |
| 473 | if (level != 0) { |
| 474 | struct sidtab_node_inner *node = entry.ptr_inner; |
| 475 | |
| 476 | if (!node) |
| 477 | return; |
| 478 | |
| 479 | for (i = 0; i < SIDTAB_INNER_ENTRIES; i++) |
| 480 | sidtab_destroy_tree(node->entries[i], level - 1); |
| 481 | kfree(node); |
| 482 | } else { |
| 483 | struct sidtab_node_leaf *node = entry.ptr_leaf; |
| 484 | |
| 485 | if (!node) |
| 486 | return; |
| 487 | |
| 488 | for (i = 0; i < SIDTAB_LEAF_ENTRIES; i++) |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 489 | sidtab_destroy_entry(&node->entries[i]); |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 490 | kfree(node); |
| 491 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | void sidtab_destroy(struct sidtab *s) |
| 495 | { |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 496 | u32 i, level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 498 | for (i = 0; i < SECINITSID_NUM; i++) |
| 499 | if (s->isids[i].set) |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 500 | sidtab_destroy_entry(&s->isids[i].entry); |
Ondrej Mosnacek | 24ed7fd | 2018-11-30 16:24:07 +0100 | [diff] [blame] | 501 | |
Ondrej Mosnacek | ee1a84f | 2018-11-30 16:24:08 +0100 | [diff] [blame] | 502 | level = SIDTAB_MAX_LEVEL; |
| 503 | while (level && !s->roots[level].ptr_inner) |
| 504 | --level; |
| 505 | |
| 506 | sidtab_destroy_tree(s->roots[level], level); |
Jeff Vander Stoep | 66f8e2f | 2019-11-22 10:33:06 +0100 | [diff] [blame] | 507 | /* |
| 508 | * The context_to_sid hashtable's objects are all shared |
| 509 | * with the isids array and context tree, and so don't need |
| 510 | * to be cleaned up here. |
| 511 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 513 | |
| 514 | #if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0 |
| 515 | |
| 516 | void sidtab_sid2str_put(struct sidtab *s, struct sidtab_entry *entry, |
| 517 | const char *str, u32 str_len) |
| 518 | { |
| 519 | struct sidtab_str_cache *cache, *victim = NULL; |
Ondrej Mosnacek | 39a706f | 2020-02-03 09:50:23 +0100 | [diff] [blame] | 520 | unsigned long flags; |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 521 | |
| 522 | /* do not cache invalid contexts */ |
| 523 | if (entry->context.len) |
| 524 | return; |
| 525 | |
Ondrej Mosnacek | 39a706f | 2020-02-03 09:50:23 +0100 | [diff] [blame] | 526 | spin_lock_irqsave(&s->cache_lock, flags); |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 527 | |
| 528 | cache = rcu_dereference_protected(entry->cache, |
| 529 | lockdep_is_held(&s->cache_lock)); |
| 530 | if (cache) { |
| 531 | /* entry in cache - just bump to the head of LRU list */ |
| 532 | list_move(&cache->lru_member, &s->cache_lru_list); |
| 533 | goto out_unlock; |
| 534 | } |
| 535 | |
| 536 | cache = kmalloc(sizeof(struct sidtab_str_cache) + str_len, GFP_ATOMIC); |
| 537 | if (!cache) |
| 538 | goto out_unlock; |
| 539 | |
| 540 | if (s->cache_free_slots == 0) { |
| 541 | /* pop a cache entry from the tail and free it */ |
| 542 | victim = container_of(s->cache_lru_list.prev, |
| 543 | struct sidtab_str_cache, lru_member); |
| 544 | list_del(&victim->lru_member); |
| 545 | rcu_assign_pointer(victim->parent->cache, NULL); |
| 546 | } else { |
| 547 | s->cache_free_slots--; |
| 548 | } |
| 549 | cache->parent = entry; |
| 550 | cache->len = str_len; |
| 551 | memcpy(cache->str, str, str_len); |
| 552 | list_add(&cache->lru_member, &s->cache_lru_list); |
| 553 | |
| 554 | rcu_assign_pointer(entry->cache, cache); |
| 555 | |
| 556 | out_unlock: |
Ondrej Mosnacek | 39a706f | 2020-02-03 09:50:23 +0100 | [diff] [blame] | 557 | spin_unlock_irqrestore(&s->cache_lock, flags); |
Ondrej Mosnacek | d97bd23 | 2019-11-26 14:57:00 +0100 | [diff] [blame] | 558 | kfree_rcu(victim, rcu_member); |
| 559 | } |
| 560 | |
| 561 | int sidtab_sid2str_get(struct sidtab *s, struct sidtab_entry *entry, |
| 562 | char **out, u32 *out_len) |
| 563 | { |
| 564 | struct sidtab_str_cache *cache; |
| 565 | int rc = 0; |
| 566 | |
| 567 | if (entry->context.len) |
| 568 | return -ENOENT; /* do not cache invalid contexts */ |
| 569 | |
| 570 | rcu_read_lock(); |
| 571 | |
| 572 | cache = rcu_dereference(entry->cache); |
| 573 | if (!cache) { |
| 574 | rc = -ENOENT; |
| 575 | } else { |
| 576 | *out_len = cache->len; |
| 577 | if (out) { |
| 578 | *out = kmemdup(cache->str, cache->len, GFP_ATOMIC); |
| 579 | if (!*out) |
| 580 | rc = -ENOMEM; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | rcu_read_unlock(); |
| 585 | |
| 586 | if (!rc && out) |
| 587 | sidtab_sid2str_put(s, entry, *out, *out_len); |
| 588 | return rc; |
| 589 | } |
| 590 | |
| 591 | #endif /* CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0 */ |