David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 1 | /* Keyring handling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 3 | * Copyright (C) 2004-2005, 2008, 2013 Red Hat, Inc. All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
Paul Gortmaker | 876979c | 2018-12-09 15:36:29 -0500 | [diff] [blame] | 12 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/slab.h> |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 16 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/seq_file.h> |
| 18 | #include <linux/err.h> |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 19 | #include <linux/user_namespace.h> |
David Howells | e9e349b | 2008-11-14 10:39:13 +1100 | [diff] [blame] | 20 | #include <keys/keyring-type.h> |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 21 | #include <keys/user-type.h> |
| 22 | #include <linux/assoc_array_priv.h> |
Chihau Chau | 512ea3b | 2010-03-08 20:11:34 -0300 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "internal.h" |
| 25 | |
| 26 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 27 | * When plumbing the depths of the key tree, this sets a hard limit |
| 28 | * set on how deep we're willing to go. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | */ |
| 30 | #define KEYRING_SEARCH_MAX_DEPTH 6 |
| 31 | |
| 32 | /* |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 33 | * We mark pointers we pass to the associative array with bit 1 set if |
| 34 | * they're keyrings and clear otherwise. |
| 35 | */ |
| 36 | #define KEYRING_PTR_SUBTYPE 0x2UL |
| 37 | |
| 38 | static inline bool keyring_ptr_is_keyring(const struct assoc_array_ptr *x) |
| 39 | { |
| 40 | return (unsigned long)x & KEYRING_PTR_SUBTYPE; |
| 41 | } |
| 42 | static inline struct key *keyring_ptr_to_key(const struct assoc_array_ptr *x) |
| 43 | { |
| 44 | void *object = assoc_array_ptr_to_leaf(x); |
| 45 | return (struct key *)((unsigned long)object & ~KEYRING_PTR_SUBTYPE); |
| 46 | } |
| 47 | static inline void *keyring_key_to_ptr(struct key *key) |
| 48 | { |
| 49 | if (key->type == &key_type_keyring) |
| 50 | return (void *)((unsigned long)key | KEYRING_PTR_SUBTYPE); |
| 51 | return key; |
| 52 | } |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | static DEFINE_RWLOCK(keyring_name_lock); |
| 55 | |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 56 | /* |
| 57 | * Clean up the bits of user_namespace that belong to us. |
| 58 | */ |
| 59 | void key_free_user_ns(struct user_namespace *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 61 | write_lock(&keyring_name_lock); |
| 62 | list_del_init(&ns->keyring_name_list); |
| 63 | write_unlock(&keyring_name_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
David Howells | 0f44e4d | 2019-06-26 21:02:32 +0100 | [diff] [blame^] | 65 | key_put(ns->user_keyring_register); |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 66 | #ifdef CONFIG_PERSISTENT_KEYRINGS |
| 67 | key_put(ns->persistent_keyring_register); |
| 68 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 72 | * The keyring key type definition. Keyrings are simply keys of this type and |
| 73 | * can be treated as ordinary keys in addition to having their own special |
| 74 | * operations. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | */ |
David Howells | 5d19e20 | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 76 | static int keyring_preparse(struct key_preparsed_payload *prep); |
| 77 | static void keyring_free_preparse(struct key_preparsed_payload *prep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | static int keyring_instantiate(struct key *keyring, |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 79 | struct key_preparsed_payload *prep); |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 80 | static void keyring_revoke(struct key *keyring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | static void keyring_destroy(struct key *keyring); |
| 82 | static void keyring_describe(const struct key *keyring, struct seq_file *m); |
| 83 | static long keyring_read(const struct key *keyring, |
| 84 | char __user *buffer, size_t buflen); |
| 85 | |
| 86 | struct key_type key_type_keyring = { |
| 87 | .name = "keyring", |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 88 | .def_datalen = 0, |
David Howells | 5d19e20 | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 89 | .preparse = keyring_preparse, |
| 90 | .free_preparse = keyring_free_preparse, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | .instantiate = keyring_instantiate, |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 92 | .revoke = keyring_revoke, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | .destroy = keyring_destroy, |
| 94 | .describe = keyring_describe, |
| 95 | .read = keyring_read, |
| 96 | }; |
David Howells | 7318226 | 2007-04-26 15:46:23 -0700 | [diff] [blame] | 97 | EXPORT_SYMBOL(key_type_keyring); |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 100 | * Semaphore to serialise link/link calls to prevent two link calls in parallel |
| 101 | * introducing a cycle. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | */ |
David Howells | 3be59f7 | 2019-05-30 11:40:24 +0100 | [diff] [blame] | 103 | static DEFINE_MUTEX(keyring_serialise_link_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 106 | * Publish the name of a keyring so that it can be found by name (if it has |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 107 | * one and it doesn't begin with a dot). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | */ |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 109 | static void keyring_publish_name(struct key *keyring) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 111 | struct user_namespace *ns = current_user_ns(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 113 | if (keyring->description && |
| 114 | keyring->description[0] && |
| 115 | keyring->description[0] != '.') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | write_lock(&keyring_name_lock); |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 117 | list_add_tail(&keyring->name_link, &ns->keyring_name_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | write_unlock(&keyring_name_lock); |
| 119 | } |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* |
David Howells | 5d19e20 | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 123 | * Preparse a keyring payload |
| 124 | */ |
| 125 | static int keyring_preparse(struct key_preparsed_payload *prep) |
| 126 | { |
| 127 | return prep->datalen != 0 ? -EINVAL : 0; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Free a preparse of a user defined key payload |
| 132 | */ |
| 133 | static void keyring_free_preparse(struct key_preparsed_payload *prep) |
| 134 | { |
| 135 | } |
| 136 | |
| 137 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 138 | * Initialise a keyring. |
| 139 | * |
| 140 | * Returns 0 on success, -EINVAL if given any data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | */ |
| 142 | static int keyring_instantiate(struct key *keyring, |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 143 | struct key_preparsed_payload *prep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
David Howells | 5d19e20 | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 145 | assoc_array_init(&keyring->keys); |
| 146 | /* make the keyring available by name if it has one */ |
| 147 | keyring_publish_name(keyring); |
| 148 | return 0; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | /* |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 152 | * Multiply 64-bits by 32-bits to 96-bits and fold back to 64-bit. Ideally we'd |
| 153 | * fold the carry back too, but that requires inline asm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | */ |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 155 | static u64 mult_64x32_and_fold(u64 x, u32 y) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 157 | u64 hi = (u64)(u32)(x >> 32) * y; |
| 158 | u64 lo = (u64)(u32)(x) * y; |
| 159 | return lo + ((u64)(u32)hi << 32) + (u32)(hi >> 32); |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | /* |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 163 | * Hash a key type and description. |
| 164 | */ |
David Howells | 355ef8e | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 165 | static void hash_key_type_and_desc(struct keyring_index_key *index_key) |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 166 | { |
| 167 | const unsigned level_shift = ASSOC_ARRAY_LEVEL_STEP; |
David Howells | d54e58b | 2013-12-02 11:24:18 +0000 | [diff] [blame] | 168 | const unsigned long fan_mask = ASSOC_ARRAY_FAN_MASK; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 169 | const char *description = index_key->description; |
| 170 | unsigned long hash, type; |
| 171 | u32 piece; |
| 172 | u64 acc; |
| 173 | int n, desc_len = index_key->desc_len; |
| 174 | |
| 175 | type = (unsigned long)index_key->type; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 176 | acc = mult_64x32_and_fold(type, desc_len + 13); |
| 177 | acc = mult_64x32_and_fold(acc, 9207); |
David Howells | f771fde | 2019-06-26 21:02:31 +0100 | [diff] [blame] | 178 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 179 | for (;;) { |
| 180 | n = desc_len; |
| 181 | if (n <= 0) |
| 182 | break; |
| 183 | if (n > 4) |
| 184 | n = 4; |
| 185 | piece = 0; |
| 186 | memcpy(&piece, description, n); |
| 187 | description += n; |
| 188 | desc_len -= n; |
| 189 | acc = mult_64x32_and_fold(acc, piece); |
| 190 | acc = mult_64x32_and_fold(acc, 9207); |
| 191 | } |
| 192 | |
| 193 | /* Fold the hash down to 32 bits if need be. */ |
| 194 | hash = acc; |
| 195 | if (ASSOC_ARRAY_KEY_CHUNK_SIZE == 32) |
| 196 | hash ^= acc >> 32; |
| 197 | |
| 198 | /* Squidge all the keyrings into a separate part of the tree to |
| 199 | * ordinary keys by making sure the lowest level segment in the hash is |
| 200 | * zero for keyrings and non-zero otherwise. |
| 201 | */ |
David Howells | d54e58b | 2013-12-02 11:24:18 +0000 | [diff] [blame] | 202 | if (index_key->type != &key_type_keyring && (hash & fan_mask) == 0) |
David Howells | 355ef8e | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 203 | hash |= (hash >> (ASSOC_ARRAY_KEY_CHUNK_SIZE - level_shift)) | 1; |
| 204 | else if (index_key->type == &key_type_keyring && (hash & fan_mask) != 0) |
| 205 | hash = (hash + (hash << level_shift)) & ~fan_mask; |
| 206 | index_key->hash = hash; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Finalise an index key to include a part of the description actually in the |
| 211 | * index key and to add in the hash too. |
| 212 | */ |
| 213 | void key_set_index_key(struct keyring_index_key *index_key) |
| 214 | { |
| 215 | size_t n = min_t(size_t, index_key->desc_len, sizeof(index_key->desc)); |
| 216 | memcpy(index_key->desc, index_key->description, n); |
| 217 | |
| 218 | hash_key_type_and_desc(index_key); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Build the next index key chunk. |
| 223 | * |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 224 | * We return it one word-sized chunk at a time. |
| 225 | */ |
| 226 | static unsigned long keyring_get_key_chunk(const void *data, int level) |
| 227 | { |
| 228 | const struct keyring_index_key *index_key = data; |
| 229 | unsigned long chunk = 0; |
David Howells | f771fde | 2019-06-26 21:02:31 +0100 | [diff] [blame] | 230 | const u8 *d; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 231 | int desc_len = index_key->desc_len, n = sizeof(chunk); |
| 232 | |
| 233 | level /= ASSOC_ARRAY_KEY_CHUNK_SIZE; |
| 234 | switch (level) { |
| 235 | case 0: |
David Howells | 355ef8e | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 236 | return index_key->hash; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 237 | case 1: |
David Howells | f771fde | 2019-06-26 21:02:31 +0100 | [diff] [blame] | 238 | return index_key->x; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 239 | case 2: |
David Howells | f771fde | 2019-06-26 21:02:31 +0100 | [diff] [blame] | 240 | return (unsigned long)index_key->type; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 241 | default: |
David Howells | f771fde | 2019-06-26 21:02:31 +0100 | [diff] [blame] | 242 | level -= 3; |
| 243 | if (desc_len <= sizeof(index_key->desc)) |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 244 | return 0; |
David Howells | f771fde | 2019-06-26 21:02:31 +0100 | [diff] [blame] | 245 | |
| 246 | d = index_key->description + sizeof(index_key->desc); |
| 247 | d += level * sizeof(long); |
| 248 | desc_len -= sizeof(index_key->desc); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 249 | if (desc_len > n) |
| 250 | desc_len = n; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 251 | do { |
| 252 | chunk <<= 8; |
David Howells | f771fde | 2019-06-26 21:02:31 +0100 | [diff] [blame] | 253 | chunk |= *d++; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 254 | } while (--desc_len > 0); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 255 | return chunk; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | static unsigned long keyring_get_object_key_chunk(const void *object, int level) |
| 260 | { |
| 261 | const struct key *key = keyring_ptr_to_key(object); |
| 262 | return keyring_get_key_chunk(&key->index_key, level); |
| 263 | } |
| 264 | |
| 265 | static bool keyring_compare_object(const void *object, const void *data) |
| 266 | { |
| 267 | const struct keyring_index_key *index_key = data; |
| 268 | const struct key *key = keyring_ptr_to_key(object); |
| 269 | |
| 270 | return key->index_key.type == index_key->type && |
| 271 | key->index_key.desc_len == index_key->desc_len && |
| 272 | memcmp(key->index_key.description, index_key->description, |
| 273 | index_key->desc_len) == 0; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Compare the index keys of a pair of objects and determine the bit position |
| 278 | * at which they differ - if they differ. |
| 279 | */ |
David Howells | 23fd78d | 2013-12-02 11:24:18 +0000 | [diff] [blame] | 280 | static int keyring_diff_objects(const void *object, const void *data) |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 281 | { |
David Howells | 23fd78d | 2013-12-02 11:24:18 +0000 | [diff] [blame] | 282 | const struct key *key_a = keyring_ptr_to_key(object); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 283 | const struct keyring_index_key *a = &key_a->index_key; |
David Howells | 23fd78d | 2013-12-02 11:24:18 +0000 | [diff] [blame] | 284 | const struct keyring_index_key *b = data; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 285 | unsigned long seg_a, seg_b; |
| 286 | int level, i; |
| 287 | |
| 288 | level = 0; |
David Howells | 355ef8e | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 289 | seg_a = a->hash; |
| 290 | seg_b = b->hash; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 291 | if ((seg_a ^ seg_b) != 0) |
| 292 | goto differ; |
David Howells | f771fde | 2019-06-26 21:02:31 +0100 | [diff] [blame] | 293 | level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 294 | |
| 295 | /* The number of bits contributed by the hash is controlled by a |
| 296 | * constant in the assoc_array headers. Everything else thereafter we |
| 297 | * can deal with as being machine word-size dependent. |
| 298 | */ |
David Howells | f771fde | 2019-06-26 21:02:31 +0100 | [diff] [blame] | 299 | seg_a = a->x; |
| 300 | seg_b = b->x; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 301 | if ((seg_a ^ seg_b) != 0) |
| 302 | goto differ; |
David Howells | f771fde | 2019-06-26 21:02:31 +0100 | [diff] [blame] | 303 | level += sizeof(unsigned long); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 304 | |
| 305 | /* The next bit may not work on big endian */ |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 306 | seg_a = (unsigned long)a->type; |
| 307 | seg_b = (unsigned long)b->type; |
| 308 | if ((seg_a ^ seg_b) != 0) |
| 309 | goto differ; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 310 | level += sizeof(unsigned long); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 311 | |
David Howells | f771fde | 2019-06-26 21:02:31 +0100 | [diff] [blame] | 312 | i = sizeof(a->desc); |
| 313 | if (a->desc_len <= i) |
| 314 | goto same; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 315 | |
| 316 | for (; i < a->desc_len; i++) { |
| 317 | seg_a = *(unsigned char *)(a->description + i); |
| 318 | seg_b = *(unsigned char *)(b->description + i); |
| 319 | if ((seg_a ^ seg_b) != 0) |
| 320 | goto differ_plus_i; |
| 321 | } |
| 322 | |
| 323 | same: |
| 324 | return -1; |
| 325 | |
| 326 | differ_plus_i: |
| 327 | level += i; |
| 328 | differ: |
| 329 | i = level * 8 + __ffs(seg_a ^ seg_b); |
| 330 | return i; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Free an object after stripping the keyring flag off of the pointer. |
| 335 | */ |
| 336 | static void keyring_free_object(void *object) |
| 337 | { |
| 338 | key_put(keyring_ptr_to_key(object)); |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Operations for keyring management by the index-tree routines. |
| 343 | */ |
| 344 | static const struct assoc_array_ops keyring_assoc_array_ops = { |
| 345 | .get_key_chunk = keyring_get_key_chunk, |
| 346 | .get_object_key_chunk = keyring_get_object_key_chunk, |
| 347 | .compare_object = keyring_compare_object, |
| 348 | .diff_objects = keyring_diff_objects, |
| 349 | .free_object = keyring_free_object, |
| 350 | }; |
| 351 | |
| 352 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 353 | * Clean up a keyring when it is destroyed. Unpublish its name if it had one |
| 354 | * and dispose of its data. |
David Howells | 233e473 | 2012-05-11 10:56:56 +0100 | [diff] [blame] | 355 | * |
| 356 | * The garbage collector detects the final key_put(), removes the keyring from |
| 357 | * the serial number tree and then does RCU synchronisation before coming here, |
| 358 | * so we shouldn't need to worry about code poking around here with the RCU |
| 359 | * readlock held by this time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | */ |
| 361 | static void keyring_destroy(struct key *keyring) |
| 362 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | if (keyring->description) { |
| 364 | write_lock(&keyring_name_lock); |
David Howells | 94efe72 | 2005-08-04 13:07:07 -0700 | [diff] [blame] | 365 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 366 | if (keyring->name_link.next != NULL && |
| 367 | !list_empty(&keyring->name_link)) |
| 368 | list_del(&keyring->name_link); |
David Howells | 94efe72 | 2005-08-04 13:07:07 -0700 | [diff] [blame] | 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | write_unlock(&keyring_name_lock); |
| 371 | } |
| 372 | |
Mat Martineau | 2b6aa41 | 2016-08-31 16:05:43 -0700 | [diff] [blame] | 373 | if (keyring->restrict_link) { |
| 374 | struct key_restriction *keyres = keyring->restrict_link; |
| 375 | |
| 376 | key_put(keyres->key); |
| 377 | kfree(keyres); |
| 378 | } |
| 379 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 380 | assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops); |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 381 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 384 | * Describe a keyring for /proc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | */ |
| 386 | static void keyring_describe(const struct key *keyring, struct seq_file *m) |
| 387 | { |
wzt.wzt@gmail.com | c856347 | 2010-03-04 21:26:23 +0800 | [diff] [blame] | 388 | if (keyring->description) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | seq_puts(m, keyring->description); |
wzt.wzt@gmail.com | c856347 | 2010-03-04 21:26:23 +0800 | [diff] [blame] | 390 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | seq_puts(m, "[anon]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
David Howells | 363b02d | 2017-10-04 16:43:25 +0100 | [diff] [blame] | 393 | if (key_is_positive(keyring)) { |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 394 | if (keyring->keys.nr_leaves_on_tree != 0) |
| 395 | seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree); |
David Howells | 78b7280 | 2011-03-11 17:57:23 +0000 | [diff] [blame] | 396 | else |
| 397 | seq_puts(m, ": empty"); |
David Howells | 78b7280 | 2011-03-11 17:57:23 +0000 | [diff] [blame] | 398 | } |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 401 | struct keyring_read_iterator_context { |
Eric Biggers | e645016 | 2017-09-18 11:36:45 -0700 | [diff] [blame] | 402 | size_t buflen; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 403 | size_t count; |
| 404 | key_serial_t __user *buffer; |
| 405 | }; |
| 406 | |
| 407 | static int keyring_read_iterator(const void *object, void *data) |
| 408 | { |
| 409 | struct keyring_read_iterator_context *ctx = data; |
| 410 | const struct key *key = keyring_ptr_to_key(object); |
| 411 | int ret; |
| 412 | |
| 413 | kenter("{%s,%d},,{%zu/%zu}", |
Eric Biggers | e645016 | 2017-09-18 11:36:45 -0700 | [diff] [blame] | 414 | key->type->name, key->serial, ctx->count, ctx->buflen); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 415 | |
Eric Biggers | e645016 | 2017-09-18 11:36:45 -0700 | [diff] [blame] | 416 | if (ctx->count >= ctx->buflen) |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 417 | return 1; |
| 418 | |
| 419 | ret = put_user(key->serial, ctx->buffer); |
| 420 | if (ret < 0) |
| 421 | return ret; |
| 422 | ctx->buffer++; |
| 423 | ctx->count += sizeof(key->serial); |
| 424 | return 0; |
| 425 | } |
| 426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 428 | * Read a list of key IDs from the keyring's contents in binary form |
| 429 | * |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 430 | * The keyring's semaphore is read-locked by the caller. This prevents someone |
| 431 | * from modifying it under us - which could cause us to read key IDs multiple |
| 432 | * times. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | */ |
| 434 | static long keyring_read(const struct key *keyring, |
| 435 | char __user *buffer, size_t buflen) |
| 436 | { |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 437 | struct keyring_read_iterator_context ctx; |
Eric Biggers | 3239b6f | 2017-11-02 00:47:03 +0000 | [diff] [blame] | 438 | long ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 440 | kenter("{%d},,%zu", key_serial(keyring), buflen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 442 | if (buflen & (sizeof(key_serial_t) - 1)) |
| 443 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Eric Biggers | 3239b6f | 2017-11-02 00:47:03 +0000 | [diff] [blame] | 445 | /* Copy as many key IDs as fit into the buffer */ |
| 446 | if (buffer && buflen) { |
| 447 | ctx.buffer = (key_serial_t __user *)buffer; |
| 448 | ctx.buflen = buflen; |
| 449 | ctx.count = 0; |
| 450 | ret = assoc_array_iterate(&keyring->keys, |
| 451 | keyring_read_iterator, &ctx); |
| 452 | if (ret < 0) { |
| 453 | kleave(" = %ld [iterate]", ret); |
| 454 | return ret; |
| 455 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Eric Biggers | 3239b6f | 2017-11-02 00:47:03 +0000 | [diff] [blame] | 458 | /* Return the size of the buffer needed */ |
| 459 | ret = keyring->keys.nr_leaves_on_tree * sizeof(key_serial_t); |
| 460 | if (ret <= buflen) |
| 461 | kleave("= %ld [ok]", ret); |
| 462 | else |
| 463 | kleave("= %ld [buffer too small]", ret); |
| 464 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 468 | * Allocate a keyring and link into the destination keyring. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | */ |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 470 | struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid, |
David Howells | 96b5c8f | 2012-10-02 19:24:56 +0100 | [diff] [blame] | 471 | const struct cred *cred, key_perm_t perm, |
David Howells | 5ac7eac | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 472 | unsigned long flags, |
Mat Martineau | 2b6aa41 | 2016-08-31 16:05:43 -0700 | [diff] [blame] | 473 | struct key_restriction *restrict_link, |
David Howells | 5ac7eac | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 474 | struct key *dest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
| 476 | struct key *keyring; |
| 477 | int ret; |
| 478 | |
| 479 | keyring = key_alloc(&key_type_keyring, description, |
David Howells | 5ac7eac | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 480 | uid, gid, cred, perm, flags, restrict_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (!IS_ERR(keyring)) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 482 | ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | if (ret < 0) { |
| 484 | key_put(keyring); |
| 485 | keyring = ERR_PTR(ret); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return keyring; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 490 | } |
David Howells | f8aa23a | 2012-10-02 19:24:56 +0100 | [diff] [blame] | 491 | EXPORT_SYMBOL(keyring_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
David Howells | 5ac7eac | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 493 | /** |
David Howells | 5ac7eac | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 494 | * restrict_link_reject - Give -EPERM to restrict link |
| 495 | * @keyring: The keyring being added to. |
| 496 | * @type: The type of key being added. |
David Howells | 5ac7eac | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 497 | * @payload: The payload of the key intended to be added. |
David Howells | 9fd1653 | 2019-05-22 13:30:56 +0100 | [diff] [blame] | 498 | * @restriction_key: Keys providing additional data for evaluating restriction. |
David Howells | 5ac7eac | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 499 | * |
| 500 | * Reject the addition of any links to a keyring. It can be overridden by |
| 501 | * passing KEY_ALLOC_BYPASS_RESTRICTION to key_instantiate_and_link() when |
| 502 | * adding a key to a keyring. |
| 503 | * |
Mat Martineau | 2b6aa41 | 2016-08-31 16:05:43 -0700 | [diff] [blame] | 504 | * This is meant to be stored in a key_restriction structure which is passed |
| 505 | * in the restrict_link parameter to keyring_alloc(). |
David Howells | 5ac7eac | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 506 | */ |
| 507 | int restrict_link_reject(struct key *keyring, |
| 508 | const struct key_type *type, |
Mat Martineau | aaf66c8 | 2016-08-30 11:33:13 -0700 | [diff] [blame] | 509 | const union key_payload *payload, |
| 510 | struct key *restriction_key) |
David Howells | 5ac7eac | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 511 | { |
| 512 | return -EPERM; |
| 513 | } |
| 514 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 515 | /* |
David Howells | c06cfb0 | 2014-09-16 17:36:06 +0100 | [diff] [blame] | 516 | * By default, we keys found by getting an exact match on their descriptions. |
| 517 | */ |
David Howells | 0c903ab | 2014-09-16 17:36:08 +0100 | [diff] [blame] | 518 | bool key_default_cmp(const struct key *key, |
| 519 | const struct key_match_data *match_data) |
David Howells | c06cfb0 | 2014-09-16 17:36:06 +0100 | [diff] [blame] | 520 | { |
| 521 | return strcmp(key->description, match_data->raw_data) == 0; |
| 522 | } |
| 523 | |
| 524 | /* |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 525 | * Iteration function to consider each key found. |
| 526 | */ |
| 527 | static int keyring_search_iterator(const void *object, void *iterator_data) |
| 528 | { |
| 529 | struct keyring_search_context *ctx = iterator_data; |
| 530 | const struct key *key = keyring_ptr_to_key(object); |
David Howells | 363b02d | 2017-10-04 16:43:25 +0100 | [diff] [blame] | 531 | unsigned long kflags = READ_ONCE(key->flags); |
| 532 | short state = READ_ONCE(key->state); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 533 | |
| 534 | kenter("{%d}", key->serial); |
| 535 | |
| 536 | /* ignore keys not of this type */ |
| 537 | if (key->type != ctx->index_key.type) { |
| 538 | kleave(" = 0 [!type]"); |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | /* skip invalidated, revoked and expired keys */ |
| 543 | if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) { |
Baolin Wang | 074d589 | 2017-11-15 16:38:45 +0000 | [diff] [blame] | 544 | time64_t expiry = READ_ONCE(key->expiry); |
Eric Biggers | 9d6c871 | 2017-09-27 12:50:45 -0700 | [diff] [blame] | 545 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 546 | if (kflags & ((1 << KEY_FLAG_INVALIDATED) | |
| 547 | (1 << KEY_FLAG_REVOKED))) { |
| 548 | ctx->result = ERR_PTR(-EKEYREVOKED); |
| 549 | kleave(" = %d [invrev]", ctx->skipped_ret); |
| 550 | goto skipped; |
| 551 | } |
| 552 | |
Baolin Wang | 074d589 | 2017-11-15 16:38:45 +0000 | [diff] [blame] | 553 | if (expiry && ctx->now >= expiry) { |
David Howells | 0b0a841 | 2014-12-01 22:52:53 +0000 | [diff] [blame] | 554 | if (!(ctx->flags & KEYRING_SEARCH_SKIP_EXPIRED)) |
| 555 | ctx->result = ERR_PTR(-EKEYEXPIRED); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 556 | kleave(" = %d [expire]", ctx->skipped_ret); |
| 557 | goto skipped; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /* keys that don't match */ |
David Howells | 4629195 | 2014-09-16 17:36:02 +0100 | [diff] [blame] | 562 | if (!ctx->match_data.cmp(key, &ctx->match_data)) { |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 563 | kleave(" = 0 [!match]"); |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /* key must have search permissions */ |
| 568 | if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) && |
| 569 | key_task_permission(make_key_ref(key, ctx->possessed), |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 570 | ctx->cred, KEY_NEED_SEARCH) < 0) { |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 571 | ctx->result = ERR_PTR(-EACCES); |
| 572 | kleave(" = %d [!perm]", ctx->skipped_ret); |
| 573 | goto skipped; |
| 574 | } |
| 575 | |
| 576 | if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) { |
| 577 | /* we set a different error code if we pass a negative key */ |
David Howells | 363b02d | 2017-10-04 16:43:25 +0100 | [diff] [blame] | 578 | if (state < 0) { |
| 579 | ctx->result = ERR_PTR(state); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 580 | kleave(" = %d [neg]", ctx->skipped_ret); |
| 581 | goto skipped; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /* Found */ |
| 586 | ctx->result = make_key_ref(key, ctx->possessed); |
| 587 | kleave(" = 1 [found]"); |
| 588 | return 1; |
| 589 | |
| 590 | skipped: |
| 591 | return ctx->skipped_ret; |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Search inside a keyring for a key. We can search by walking to it |
| 596 | * directly based on its index-key or we can iterate over the entire |
| 597 | * tree looking for it, based on the match function. |
| 598 | */ |
| 599 | static int search_keyring(struct key *keyring, struct keyring_search_context *ctx) |
| 600 | { |
David Howells | 4629195 | 2014-09-16 17:36:02 +0100 | [diff] [blame] | 601 | if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_DIRECT) { |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 602 | const void *object; |
| 603 | |
| 604 | object = assoc_array_find(&keyring->keys, |
| 605 | &keyring_assoc_array_ops, |
| 606 | &ctx->index_key); |
| 607 | return object ? ctx->iterator(object, ctx) : 0; |
| 608 | } |
| 609 | return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx); |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * Search a tree of keyrings that point to other keyrings up to the maximum |
| 614 | * depth. |
| 615 | */ |
| 616 | static bool search_nested_keyrings(struct key *keyring, |
| 617 | struct keyring_search_context *ctx) |
| 618 | { |
| 619 | struct { |
| 620 | struct key *keyring; |
| 621 | struct assoc_array_node *node; |
| 622 | int slot; |
| 623 | } stack[KEYRING_SEARCH_MAX_DEPTH]; |
| 624 | |
| 625 | struct assoc_array_shortcut *shortcut; |
| 626 | struct assoc_array_node *node; |
| 627 | struct assoc_array_ptr *ptr; |
| 628 | struct key *key; |
| 629 | int sp = 0, slot; |
| 630 | |
| 631 | kenter("{%d},{%s,%s}", |
| 632 | keyring->serial, |
| 633 | ctx->index_key.type->name, |
| 634 | ctx->index_key.description); |
| 635 | |
David Howells | 054f618 | 2014-12-01 22:52:50 +0000 | [diff] [blame] | 636 | #define STATE_CHECKS (KEYRING_SEARCH_NO_STATE_CHECK | KEYRING_SEARCH_DO_STATE_CHECK) |
| 637 | BUG_ON((ctx->flags & STATE_CHECKS) == 0 || |
| 638 | (ctx->flags & STATE_CHECKS) == STATE_CHECKS); |
| 639 | |
David Howells | f771fde | 2019-06-26 21:02:31 +0100 | [diff] [blame] | 640 | if (ctx->index_key.description) |
| 641 | key_set_index_key(&ctx->index_key); |
| 642 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 643 | /* Check to see if this top-level keyring is what we are looking for |
| 644 | * and whether it is valid or not. |
| 645 | */ |
David Howells | 4629195 | 2014-09-16 17:36:02 +0100 | [diff] [blame] | 646 | if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_ITERATE || |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 647 | keyring_compare_object(keyring, &ctx->index_key)) { |
| 648 | ctx->skipped_ret = 2; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 649 | switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) { |
| 650 | case 1: |
| 651 | goto found; |
| 652 | case 2: |
| 653 | return false; |
| 654 | default: |
| 655 | break; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | ctx->skipped_ret = 0; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 660 | |
| 661 | /* Start processing a new keyring */ |
| 662 | descend_to_keyring: |
| 663 | kdebug("descend to %d", keyring->serial); |
| 664 | if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) | |
| 665 | (1 << KEY_FLAG_REVOKED))) |
| 666 | goto not_this_keyring; |
| 667 | |
| 668 | /* Search through the keys in this keyring before its searching its |
| 669 | * subtrees. |
| 670 | */ |
| 671 | if (search_keyring(keyring, ctx)) |
| 672 | goto found; |
| 673 | |
| 674 | /* Then manually iterate through the keyrings nested in this one. |
| 675 | * |
| 676 | * Start from the root node of the index tree. Because of the way the |
| 677 | * hash function has been set up, keyrings cluster on the leftmost |
| 678 | * branch of the root node (root slot 0) or in the root node itself. |
| 679 | * Non-keyrings avoid the leftmost branch of the root entirely (root |
| 680 | * slots 1-15). |
| 681 | */ |
David Howells | dcf49db | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 682 | if (!(ctx->flags & KEYRING_SEARCH_RECURSE)) |
| 683 | goto not_this_keyring; |
| 684 | |
Davidlohr Bueso | 381f20f | 2017-06-08 14:47:34 +0100 | [diff] [blame] | 685 | ptr = READ_ONCE(keyring->keys.root); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 686 | if (!ptr) |
| 687 | goto not_this_keyring; |
| 688 | |
| 689 | if (assoc_array_ptr_is_shortcut(ptr)) { |
| 690 | /* If the root is a shortcut, either the keyring only contains |
| 691 | * keyring pointers (everything clusters behind root slot 0) or |
| 692 | * doesn't contain any keyring pointers. |
| 693 | */ |
| 694 | shortcut = assoc_array_ptr_to_shortcut(ptr); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 695 | if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0) |
| 696 | goto not_this_keyring; |
| 697 | |
Davidlohr Bueso | 381f20f | 2017-06-08 14:47:34 +0100 | [diff] [blame] | 698 | ptr = READ_ONCE(shortcut->next_node); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 699 | node = assoc_array_ptr_to_node(ptr); |
| 700 | goto begin_node; |
| 701 | } |
| 702 | |
| 703 | node = assoc_array_ptr_to_node(ptr); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 704 | ptr = node->slots[0]; |
| 705 | if (!assoc_array_ptr_is_meta(ptr)) |
| 706 | goto begin_node; |
| 707 | |
| 708 | descend_to_node: |
| 709 | /* Descend to a more distal node in this keyring's content tree and go |
| 710 | * through that. |
| 711 | */ |
| 712 | kdebug("descend"); |
| 713 | if (assoc_array_ptr_is_shortcut(ptr)) { |
| 714 | shortcut = assoc_array_ptr_to_shortcut(ptr); |
Davidlohr Bueso | 381f20f | 2017-06-08 14:47:34 +0100 | [diff] [blame] | 715 | ptr = READ_ONCE(shortcut->next_node); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 716 | BUG_ON(!assoc_array_ptr_is_node(ptr)); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 717 | } |
David Howells | 9c5e45d | 2013-12-02 11:24:19 +0000 | [diff] [blame] | 718 | node = assoc_array_ptr_to_node(ptr); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 719 | |
| 720 | begin_node: |
| 721 | kdebug("begin_node"); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 722 | slot = 0; |
| 723 | ascend_to_node: |
| 724 | /* Go through the slots in a node */ |
| 725 | for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) { |
Davidlohr Bueso | 381f20f | 2017-06-08 14:47:34 +0100 | [diff] [blame] | 726 | ptr = READ_ONCE(node->slots[slot]); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 727 | |
| 728 | if (assoc_array_ptr_is_meta(ptr) && node->back_pointer) |
| 729 | goto descend_to_node; |
| 730 | |
| 731 | if (!keyring_ptr_is_keyring(ptr)) |
| 732 | continue; |
| 733 | |
| 734 | key = keyring_ptr_to_key(ptr); |
| 735 | |
| 736 | if (sp >= KEYRING_SEARCH_MAX_DEPTH) { |
| 737 | if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) { |
| 738 | ctx->result = ERR_PTR(-ELOOP); |
| 739 | return false; |
| 740 | } |
| 741 | goto not_this_keyring; |
| 742 | } |
| 743 | |
| 744 | /* Search a nested keyring */ |
| 745 | if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) && |
| 746 | key_task_permission(make_key_ref(key, ctx->possessed), |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 747 | ctx->cred, KEY_NEED_SEARCH) < 0) |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 748 | continue; |
| 749 | |
| 750 | /* stack the current position */ |
| 751 | stack[sp].keyring = keyring; |
| 752 | stack[sp].node = node; |
| 753 | stack[sp].slot = slot; |
| 754 | sp++; |
| 755 | |
| 756 | /* begin again with the new keyring */ |
| 757 | keyring = key; |
| 758 | goto descend_to_keyring; |
| 759 | } |
| 760 | |
| 761 | /* We've dealt with all the slots in the current node, so now we need |
| 762 | * to ascend to the parent and continue processing there. |
| 763 | */ |
Davidlohr Bueso | 381f20f | 2017-06-08 14:47:34 +0100 | [diff] [blame] | 764 | ptr = READ_ONCE(node->back_pointer); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 765 | slot = node->parent_slot; |
| 766 | |
| 767 | if (ptr && assoc_array_ptr_is_shortcut(ptr)) { |
| 768 | shortcut = assoc_array_ptr_to_shortcut(ptr); |
Davidlohr Bueso | 381f20f | 2017-06-08 14:47:34 +0100 | [diff] [blame] | 769 | ptr = READ_ONCE(shortcut->back_pointer); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 770 | slot = shortcut->parent_slot; |
| 771 | } |
| 772 | if (!ptr) |
| 773 | goto not_this_keyring; |
| 774 | node = assoc_array_ptr_to_node(ptr); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 775 | slot++; |
| 776 | |
| 777 | /* If we've ascended to the root (zero backpointer), we must have just |
| 778 | * finished processing the leftmost branch rather than the root slots - |
| 779 | * so there can't be any more keyrings for us to find. |
| 780 | */ |
| 781 | if (node->back_pointer) { |
| 782 | kdebug("ascend %d", slot); |
| 783 | goto ascend_to_node; |
| 784 | } |
| 785 | |
| 786 | /* The keyring we're looking at was disqualified or didn't contain a |
| 787 | * matching key. |
| 788 | */ |
| 789 | not_this_keyring: |
| 790 | kdebug("not_this_keyring %d", sp); |
| 791 | if (sp <= 0) { |
| 792 | kleave(" = false"); |
| 793 | return false; |
| 794 | } |
| 795 | |
| 796 | /* Resume the processing of a keyring higher up in the tree */ |
| 797 | sp--; |
| 798 | keyring = stack[sp].keyring; |
| 799 | node = stack[sp].node; |
| 800 | slot = stack[sp].slot + 1; |
| 801 | kdebug("ascend to %d [%d]", keyring->serial, slot); |
| 802 | goto ascend_to_node; |
| 803 | |
| 804 | /* We found a viable match */ |
| 805 | found: |
| 806 | key = key_ref_to_ptr(ctx->result); |
| 807 | key_check(key); |
| 808 | if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) { |
Baolin Wang | 074d589 | 2017-11-15 16:38:45 +0000 | [diff] [blame] | 809 | key->last_used_at = ctx->now; |
| 810 | keyring->last_used_at = ctx->now; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 811 | while (sp > 0) |
Baolin Wang | 074d589 | 2017-11-15 16:38:45 +0000 | [diff] [blame] | 812 | stack[--sp].keyring->last_used_at = ctx->now; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 813 | } |
| 814 | kleave(" = true"); |
| 815 | return true; |
| 816 | } |
| 817 | |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 818 | /** |
David Howells | e59428f | 2019-06-19 16:10:15 +0100 | [diff] [blame] | 819 | * keyring_search_rcu - Search a keyring tree for a matching key under RCU |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 820 | * @keyring_ref: A pointer to the keyring with possession indicator. |
David Howells | 4bdf0bc | 2013-09-24 10:35:15 +0100 | [diff] [blame] | 821 | * @ctx: The keyring search context. |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 822 | * |
| 823 | * Search the supplied keyring tree for a key that matches the criteria given. |
| 824 | * The root keyring and any linked keyrings must grant Search permission to the |
| 825 | * caller to be searchable and keys can only be found if they too grant Search |
| 826 | * to the caller. The possession flag on the root keyring pointer controls use |
| 827 | * of the possessor bits in permissions checking of the entire tree. In |
| 828 | * addition, the LSM gets to forbid keyring searches and key matches. |
| 829 | * |
| 830 | * The search is performed as a breadth-then-depth search up to the prescribed |
David Howells | e59428f | 2019-06-19 16:10:15 +0100 | [diff] [blame] | 831 | * limit (KEYRING_SEARCH_MAX_DEPTH). The caller must hold the RCU read lock to |
| 832 | * prevent keyrings from being destroyed or rearranged whilst they are being |
| 833 | * searched. |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 834 | * |
| 835 | * Keys are matched to the type provided and are then filtered by the match |
| 836 | * function, which is given the description to use in any way it sees fit. The |
| 837 | * match function may use any attributes of a key that it wishes to to |
| 838 | * determine the match. Normally the match function from the key type would be |
| 839 | * used. |
| 840 | * |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 841 | * RCU can be used to prevent the keyring key lists from disappearing without |
| 842 | * the need to take lots of locks. |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 843 | * |
| 844 | * Returns a pointer to the found key and increments the key usage count if |
| 845 | * successful; -EAGAIN if no matching keys were found, or if expired or revoked |
| 846 | * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the |
| 847 | * specified keyring wasn't a keyring. |
| 848 | * |
| 849 | * In the case of a successful return, the possession attribute from |
| 850 | * @keyring_ref is propagated to the returned key reference. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | */ |
David Howells | e59428f | 2019-06-19 16:10:15 +0100 | [diff] [blame] | 852 | key_ref_t keyring_search_rcu(key_ref_t keyring_ref, |
David Howells | 4bdf0bc | 2013-09-24 10:35:15 +0100 | [diff] [blame] | 853 | struct keyring_search_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | { |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 855 | struct key *keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | long err; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 857 | |
| 858 | ctx->iterator = keyring_search_iterator; |
| 859 | ctx->possessed = is_key_possessed(keyring_ref); |
| 860 | ctx->result = ERR_PTR(-EAGAIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 862 | keyring = key_ref_to_ptr(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | key_check(keyring); |
| 864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | if (keyring->type != &key_type_keyring) |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 866 | return ERR_PTR(-ENOTDIR); |
| 867 | |
| 868 | if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) { |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 869 | err = key_task_permission(keyring_ref, ctx->cred, KEY_NEED_SEARCH); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 870 | if (err < 0) |
| 871 | return ERR_PTR(err); |
| 872 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | |
Baolin Wang | 074d589 | 2017-11-15 16:38:45 +0000 | [diff] [blame] | 874 | ctx->now = ktime_get_real_seconds(); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 875 | if (search_nested_keyrings(keyring, ctx)) |
| 876 | __key_get(key_ref_to_ptr(ctx->result)); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 877 | return ctx->result; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 878 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 880 | /** |
| 881 | * keyring_search - Search the supplied keyring tree for a matching key |
| 882 | * @keyring: The root of the keyring tree to be searched. |
| 883 | * @type: The type of keyring we want to find. |
| 884 | * @description: The name of the keyring we want to find. |
David Howells | dcf49db | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 885 | * @recurse: True to search the children of @keyring also |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 886 | * |
David Howells | e59428f | 2019-06-19 16:10:15 +0100 | [diff] [blame] | 887 | * As keyring_search_rcu() above, but using the current task's credentials and |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 888 | * type's default matching function and preferred search method. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 890 | key_ref_t keyring_search(key_ref_t keyring, |
| 891 | struct key_type *type, |
David Howells | dcf49db | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 892 | const char *description, |
| 893 | bool recurse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | { |
David Howells | 4bdf0bc | 2013-09-24 10:35:15 +0100 | [diff] [blame] | 895 | struct keyring_search_context ctx = { |
| 896 | .index_key.type = type, |
| 897 | .index_key.description = description, |
Eric Biggers | ede0fa98 | 2019-02-22 15:36:18 +0000 | [diff] [blame] | 898 | .index_key.desc_len = strlen(description), |
David Howells | 4bdf0bc | 2013-09-24 10:35:15 +0100 | [diff] [blame] | 899 | .cred = current_cred(), |
David Howells | c06cfb0 | 2014-09-16 17:36:06 +0100 | [diff] [blame] | 900 | .match_data.cmp = key_default_cmp, |
David Howells | 4629195 | 2014-09-16 17:36:02 +0100 | [diff] [blame] | 901 | .match_data.raw_data = description, |
| 902 | .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, |
| 903 | .flags = KEYRING_SEARCH_DO_STATE_CHECK, |
David Howells | 4bdf0bc | 2013-09-24 10:35:15 +0100 | [diff] [blame] | 904 | }; |
David Howells | 4629195 | 2014-09-16 17:36:02 +0100 | [diff] [blame] | 905 | key_ref_t key; |
| 906 | int ret; |
David Howells | 4bdf0bc | 2013-09-24 10:35:15 +0100 | [diff] [blame] | 907 | |
David Howells | dcf49db | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 908 | if (recurse) |
| 909 | ctx.flags |= KEYRING_SEARCH_RECURSE; |
David Howells | 4629195 | 2014-09-16 17:36:02 +0100 | [diff] [blame] | 910 | if (type->match_preparse) { |
| 911 | ret = type->match_preparse(&ctx.match_data); |
| 912 | if (ret < 0) |
| 913 | return ERR_PTR(ret); |
| 914 | } |
| 915 | |
David Howells | e59428f | 2019-06-19 16:10:15 +0100 | [diff] [blame] | 916 | rcu_read_lock(); |
| 917 | key = keyring_search_rcu(keyring, &ctx); |
| 918 | rcu_read_unlock(); |
David Howells | 4629195 | 2014-09-16 17:36:02 +0100 | [diff] [blame] | 919 | |
| 920 | if (type->match_free) |
| 921 | type->match_free(&ctx.match_data); |
| 922 | return key; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 923 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | EXPORT_SYMBOL(keyring_search); |
| 925 | |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 926 | static struct key_restriction *keyring_restriction_alloc( |
| 927 | key_restrict_link_func_t check) |
| 928 | { |
| 929 | struct key_restriction *keyres = |
| 930 | kzalloc(sizeof(struct key_restriction), GFP_KERNEL); |
| 931 | |
| 932 | if (!keyres) |
| 933 | return ERR_PTR(-ENOMEM); |
| 934 | |
| 935 | keyres->check = check; |
| 936 | |
| 937 | return keyres; |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * Semaphore to serialise restriction setup to prevent reference count |
| 942 | * cycles through restriction key pointers. |
| 943 | */ |
| 944 | static DECLARE_RWSEM(keyring_serialise_restrict_sem); |
| 945 | |
| 946 | /* |
| 947 | * Check for restriction cycles that would prevent keyring garbage collection. |
| 948 | * keyring_serialise_restrict_sem must be held. |
| 949 | */ |
| 950 | static bool keyring_detect_restriction_cycle(const struct key *dest_keyring, |
| 951 | struct key_restriction *keyres) |
| 952 | { |
| 953 | while (keyres && keyres->key && |
| 954 | keyres->key->type == &key_type_keyring) { |
| 955 | if (keyres->key == dest_keyring) |
| 956 | return true; |
| 957 | |
| 958 | keyres = keyres->key->restrict_link; |
| 959 | } |
| 960 | |
| 961 | return false; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * keyring_restrict - Look up and apply a restriction to a keyring |
David Howells | 9fd1653 | 2019-05-22 13:30:56 +0100 | [diff] [blame] | 966 | * @keyring_ref: The keyring to be restricted |
| 967 | * @type: The key type that will provide the restriction checker. |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 968 | * @restriction: The restriction options to apply to the keyring |
David Howells | 9fd1653 | 2019-05-22 13:30:56 +0100 | [diff] [blame] | 969 | * |
| 970 | * Look up a keyring and apply a restriction to it. The restriction is managed |
| 971 | * by the specific key type, but can be configured by the options specified in |
| 972 | * the restriction string. |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 973 | */ |
| 974 | int keyring_restrict(key_ref_t keyring_ref, const char *type, |
| 975 | const char *restriction) |
| 976 | { |
| 977 | struct key *keyring; |
| 978 | struct key_type *restrict_type = NULL; |
| 979 | struct key_restriction *restrict_link; |
| 980 | int ret = 0; |
| 981 | |
| 982 | keyring = key_ref_to_ptr(keyring_ref); |
| 983 | key_check(keyring); |
| 984 | |
| 985 | if (keyring->type != &key_type_keyring) |
| 986 | return -ENOTDIR; |
| 987 | |
| 988 | if (!type) { |
| 989 | restrict_link = keyring_restriction_alloc(restrict_link_reject); |
| 990 | } else { |
| 991 | restrict_type = key_type_lookup(type); |
| 992 | |
| 993 | if (IS_ERR(restrict_type)) |
| 994 | return PTR_ERR(restrict_type); |
| 995 | |
| 996 | if (!restrict_type->lookup_restriction) { |
| 997 | ret = -ENOENT; |
| 998 | goto error; |
| 999 | } |
| 1000 | |
| 1001 | restrict_link = restrict_type->lookup_restriction(restriction); |
| 1002 | } |
| 1003 | |
| 1004 | if (IS_ERR(restrict_link)) { |
| 1005 | ret = PTR_ERR(restrict_link); |
| 1006 | goto error; |
| 1007 | } |
| 1008 | |
| 1009 | down_write(&keyring->sem); |
| 1010 | down_write(&keyring_serialise_restrict_sem); |
| 1011 | |
| 1012 | if (keyring->restrict_link) |
| 1013 | ret = -EEXIST; |
| 1014 | else if (keyring_detect_restriction_cycle(keyring, restrict_link)) |
| 1015 | ret = -EDEADLK; |
| 1016 | else |
| 1017 | keyring->restrict_link = restrict_link; |
| 1018 | |
| 1019 | up_write(&keyring_serialise_restrict_sem); |
| 1020 | up_write(&keyring->sem); |
| 1021 | |
| 1022 | if (ret < 0) { |
| 1023 | key_put(restrict_link->key); |
| 1024 | kfree(restrict_link); |
| 1025 | } |
| 1026 | |
| 1027 | error: |
| 1028 | if (restrict_type) |
| 1029 | key_type_put(restrict_type); |
| 1030 | |
| 1031 | return ret; |
| 1032 | } |
| 1033 | EXPORT_SYMBOL(keyring_restrict); |
| 1034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | /* |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1036 | * Search the given keyring for a key that might be updated. |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1037 | * |
| 1038 | * The caller must guarantee that the keyring is a keyring and that the |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1039 | * permission is granted to modify the keyring as no check is made here. The |
| 1040 | * caller must also hold a lock on the keyring semaphore. |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1041 | * |
| 1042 | * Returns a pointer to the found key with usage count incremented if |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1043 | * successful and returns NULL if not found. Revoked and invalidated keys are |
| 1044 | * skipped over. |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1045 | * |
| 1046 | * If successful, the possession indicator is propagated from the keyring ref |
| 1047 | * to the returned key reference. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | */ |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1049 | key_ref_t find_key_to_update(key_ref_t keyring_ref, |
| 1050 | const struct keyring_index_key *index_key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 1052 | struct key *keyring, *key; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1053 | const void *object; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 1055 | keyring = key_ref_to_ptr(keyring_ref); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 1056 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1057 | kenter("{%d},{%s,%s}", |
| 1058 | keyring->serial, index_key->type->name, index_key->description); |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 1059 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1060 | object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops, |
| 1061 | index_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1063 | if (object) |
| 1064 | goto found; |
| 1065 | |
| 1066 | kleave(" = NULL"); |
| 1067 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 1069 | found: |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1070 | key = keyring_ptr_to_key(object); |
| 1071 | if (key->flags & ((1 << KEY_FLAG_INVALIDATED) | |
| 1072 | (1 << KEY_FLAG_REVOKED))) { |
| 1073 | kleave(" = NULL [x]"); |
| 1074 | return NULL; |
| 1075 | } |
David Howells | ccc3e6d | 2013-09-24 10:35:16 +0100 | [diff] [blame] | 1076 | __key_get(key); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1077 | kleave(" = {%d}", key->serial); |
| 1078 | return make_key_ref(key, is_key_possessed(keyring_ref)); |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1079 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1082 | * Find a keyring with the specified name. |
| 1083 | * |
Eric Biggers | 237bbd2 | 2017-09-18 11:37:03 -0700 | [diff] [blame] | 1084 | * Only keyrings that have nonzero refcount, are not revoked, and are owned by a |
| 1085 | * user in the current user namespace are considered. If @uid_keyring is %true, |
| 1086 | * the keyring additionally must have been allocated as a user or user session |
| 1087 | * keyring; otherwise, it must grant Search permission directly to the caller. |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1088 | * |
| 1089 | * Returns a pointer to the keyring with the keyring's refcount having being |
| 1090 | * incremented on success. -ENOKEY is returned if a key could not be found. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | */ |
Eric Biggers | 237bbd2 | 2017-09-18 11:37:03 -0700 | [diff] [blame] | 1092 | struct key *find_keyring_by_name(const char *name, bool uid_keyring) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | { |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 1094 | struct user_namespace *ns = current_user_ns(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | struct key *keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | if (!name) |
Toshiyuki Okajima | cea7daa | 2010-04-30 14:32:13 +0100 | [diff] [blame] | 1098 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | read_lock(&keyring_name_lock); |
| 1101 | |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 1102 | /* Search this hash bucket for a keyring with a matching name that |
| 1103 | * grants Search permission and that hasn't been revoked |
| 1104 | */ |
| 1105 | list_for_each_entry(keyring, &ns->keyring_name_list, name_link) { |
| 1106 | if (!kuid_has_mapping(ns, keyring->user->uid)) |
| 1107 | continue; |
Serge E. Hallyn | 2ea190d | 2009-02-26 18:27:55 -0600 | [diff] [blame] | 1108 | |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 1109 | if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) |
| 1110 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 1112 | if (strcmp(keyring->description, name) != 0) |
| 1113 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 1115 | if (uid_keyring) { |
| 1116 | if (!test_bit(KEY_FLAG_UID_KEYRING, |
| 1117 | &keyring->flags)) |
Toshiyuki Okajima | cea7daa | 2010-04-30 14:32:13 +0100 | [diff] [blame] | 1118 | continue; |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 1119 | } else { |
| 1120 | if (key_permission(make_key_ref(keyring, 0), |
| 1121 | KEY_NEED_SEARCH) < 0) |
| 1122 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | } |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 1124 | |
| 1125 | /* we've got a match but we might end up racing with |
| 1126 | * key_cleanup() if the keyring is currently 'dead' |
| 1127 | * (ie. it has a zero usage count) */ |
| 1128 | if (!refcount_inc_not_zero(&keyring->usage)) |
| 1129 | continue; |
| 1130 | keyring->last_used_at = ktime_get_real_seconds(); |
| 1131 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | keyring = ERR_PTR(-ENOKEY); |
Toshiyuki Okajima | cea7daa | 2010-04-30 14:32:13 +0100 | [diff] [blame] | 1135 | out: |
| 1136 | read_unlock(&keyring_name_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | return keyring; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1140 | static int keyring_detect_cycle_iterator(const void *object, |
| 1141 | void *iterator_data) |
| 1142 | { |
| 1143 | struct keyring_search_context *ctx = iterator_data; |
| 1144 | const struct key *key = keyring_ptr_to_key(object); |
| 1145 | |
| 1146 | kenter("{%d}", key->serial); |
| 1147 | |
David Howells | 979e0d7 | 2014-03-09 08:21:58 +0000 | [diff] [blame] | 1148 | /* We might get a keyring with matching index-key that is nonetheless a |
| 1149 | * different keyring. */ |
David Howells | 4629195 | 2014-09-16 17:36:02 +0100 | [diff] [blame] | 1150 | if (key != ctx->match_data.raw_data) |
David Howells | 979e0d7 | 2014-03-09 08:21:58 +0000 | [diff] [blame] | 1151 | return 0; |
| 1152 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1153 | ctx->result = ERR_PTR(-EDEADLK); |
| 1154 | return 1; |
| 1155 | } |
| 1156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1158 | * See if a cycle will will be created by inserting acyclic tree B in acyclic |
| 1159 | * tree A at the topmost level (ie: as a direct child of A). |
| 1160 | * |
| 1161 | * Since we are adding B to A at the top level, checking for cycles should just |
| 1162 | * be a matter of seeing if node A is somewhere in tree B. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | */ |
| 1164 | static int keyring_detect_cycle(struct key *A, struct key *B) |
| 1165 | { |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1166 | struct keyring_search_context ctx = { |
David Howells | 4629195 | 2014-09-16 17:36:02 +0100 | [diff] [blame] | 1167 | .index_key = A->index_key, |
| 1168 | .match_data.raw_data = A, |
| 1169 | .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, |
| 1170 | .iterator = keyring_detect_cycle_iterator, |
| 1171 | .flags = (KEYRING_SEARCH_NO_STATE_CHECK | |
| 1172 | KEYRING_SEARCH_NO_UPDATE_TIME | |
| 1173 | KEYRING_SEARCH_NO_CHECK_PERM | |
David Howells | dcf49db | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 1174 | KEYRING_SEARCH_DETECT_TOO_DEEP | |
| 1175 | KEYRING_SEARCH_RECURSE), |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1176 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 1178 | rcu_read_lock(); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1179 | search_nested_keyrings(B, &ctx); |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 1180 | rcu_read_unlock(); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1181 | return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result); |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1182 | } |
David Howells | cab8eb5 | 2006-01-08 01:02:45 -0800 | [diff] [blame] | 1183 | |
David Howells | cab8eb5 | 2006-01-08 01:02:45 -0800 | [diff] [blame] | 1184 | /* |
David Howells | df593ee | 2019-05-30 11:37:39 +0100 | [diff] [blame] | 1185 | * Lock keyring for link. |
| 1186 | */ |
| 1187 | int __key_link_lock(struct key *keyring, |
| 1188 | const struct keyring_index_key *index_key) |
| 1189 | __acquires(&keyring->sem) |
| 1190 | __acquires(&keyring_serialise_link_lock) |
| 1191 | { |
| 1192 | if (keyring->type != &key_type_keyring) |
| 1193 | return -ENOTDIR; |
| 1194 | |
| 1195 | down_write(&keyring->sem); |
| 1196 | |
| 1197 | /* Serialise link/link calls to prevent parallel calls causing a cycle |
| 1198 | * when linking two keyring in opposite orders. |
| 1199 | */ |
| 1200 | if (index_key->type == &key_type_keyring) |
| 1201 | mutex_lock(&keyring_serialise_link_lock); |
| 1202 | |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
| 1206 | /* |
David Howells | ed0ac5c | 2019-05-20 21:51:50 +0100 | [diff] [blame] | 1207 | * Lock keyrings for move (link/unlink combination). |
| 1208 | */ |
| 1209 | int __key_move_lock(struct key *l_keyring, struct key *u_keyring, |
| 1210 | const struct keyring_index_key *index_key) |
| 1211 | __acquires(&l_keyring->sem) |
| 1212 | __acquires(&u_keyring->sem) |
| 1213 | __acquires(&keyring_serialise_link_lock) |
| 1214 | { |
| 1215 | if (l_keyring->type != &key_type_keyring || |
| 1216 | u_keyring->type != &key_type_keyring) |
| 1217 | return -ENOTDIR; |
| 1218 | |
| 1219 | /* We have to be very careful here to take the keyring locks in the |
| 1220 | * right order, lest we open ourselves to deadlocking against another |
| 1221 | * move operation. |
| 1222 | */ |
| 1223 | if (l_keyring < u_keyring) { |
| 1224 | down_write(&l_keyring->sem); |
| 1225 | down_write_nested(&u_keyring->sem, 1); |
| 1226 | } else { |
| 1227 | down_write(&u_keyring->sem); |
| 1228 | down_write_nested(&l_keyring->sem, 1); |
| 1229 | } |
| 1230 | |
| 1231 | /* Serialise link/link calls to prevent parallel calls causing a cycle |
| 1232 | * when linking two keyring in opposite orders. |
| 1233 | */ |
| 1234 | if (index_key->type == &key_type_keyring) |
| 1235 | mutex_lock(&keyring_serialise_link_lock); |
| 1236 | |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
| 1240 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1241 | * Preallocate memory so that a key can be linked into to a keyring. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | */ |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1243 | int __key_link_begin(struct key *keyring, |
| 1244 | const struct keyring_index_key *index_key, |
| 1245 | struct assoc_array_edit **_edit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | { |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1247 | struct assoc_array_edit *edit; |
| 1248 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | |
David Howells | 16feef4 | 2013-09-24 10:35:15 +0100 | [diff] [blame] | 1250 | kenter("%d,%s,%s,", |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1251 | keyring->serial, index_key->type->name, index_key->description); |
| 1252 | |
| 1253 | BUG_ON(index_key->desc_len == 0); |
David Howells | df593ee | 2019-05-30 11:37:39 +0100 | [diff] [blame] | 1254 | BUG_ON(*_edit != NULL); |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1255 | |
David Howells | df593ee | 2019-05-30 11:37:39 +0100 | [diff] [blame] | 1256 | *_edit = NULL; |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | ret = -EKEYREVOKED; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 1259 | if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) |
David Howells | df593ee | 2019-05-30 11:37:39 +0100 | [diff] [blame] | 1260 | goto error; |
David Howells | 553d603 | 2010-04-30 14:32:28 +0100 | [diff] [blame] | 1261 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1262 | /* Create an edit script that will insert/replace the key in the |
| 1263 | * keyring tree. |
| 1264 | */ |
| 1265 | edit = assoc_array_insert(&keyring->keys, |
| 1266 | &keyring_assoc_array_ops, |
| 1267 | index_key, |
| 1268 | NULL); |
| 1269 | if (IS_ERR(edit)) { |
| 1270 | ret = PTR_ERR(edit); |
David Howells | df593ee | 2019-05-30 11:37:39 +0100 | [diff] [blame] | 1271 | goto error; |
David Howells | 034faeb | 2013-10-30 11:15:24 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | /* If we're not replacing a link in-place then we're going to need some |
| 1275 | * extra quota. |
| 1276 | */ |
| 1277 | if (!edit->dead_leaf) { |
| 1278 | ret = key_payload_reserve(keyring, |
| 1279 | keyring->datalen + KEYQUOTA_LINK_BYTES); |
| 1280 | if (ret < 0) |
| 1281 | goto error_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1284 | *_edit = edit; |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1285 | kleave(" = 0"); |
| 1286 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
David Howells | 034faeb | 2013-10-30 11:15:24 +0000 | [diff] [blame] | 1288 | error_cancel: |
| 1289 | assoc_array_cancel_edit(edit); |
David Howells | df593ee | 2019-05-30 11:37:39 +0100 | [diff] [blame] | 1290 | error: |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1291 | kleave(" = %d", ret); |
| 1292 | return ret; |
| 1293 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1295 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1296 | * Check already instantiated keys aren't going to be a problem. |
| 1297 | * |
| 1298 | * The caller must have called __key_link_begin(). Don't need to call this for |
| 1299 | * keys that were created since __key_link_begin() was called. |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1300 | */ |
| 1301 | int __key_link_check_live_key(struct key *keyring, struct key *key) |
| 1302 | { |
| 1303 | if (key->type == &key_type_keyring) |
| 1304 | /* check that we aren't going to create a cycle by linking one |
| 1305 | * keyring to another */ |
| 1306 | return keyring_detect_cycle(keyring, key); |
| 1307 | return 0; |
| 1308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1310 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1311 | * Link a key into to a keyring. |
| 1312 | * |
| 1313 | * Must be called with __key_link_begin() having being called. Discards any |
| 1314 | * already extant link to matching key if there is one, so that each keyring |
| 1315 | * holds at most one link to any given key of a particular type+description |
| 1316 | * combination. |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1317 | */ |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1318 | void __key_link(struct key *key, struct assoc_array_edit **_edit) |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1319 | { |
David Howells | ccc3e6d | 2013-09-24 10:35:16 +0100 | [diff] [blame] | 1320 | __key_get(key); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1321 | assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key)); |
| 1322 | assoc_array_apply_edit(*_edit); |
| 1323 | *_edit = NULL; |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1327 | * Finish linking a key into to a keyring. |
| 1328 | * |
| 1329 | * Must be called with __key_link_begin() having being called. |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1330 | */ |
David Howells | 16feef4 | 2013-09-24 10:35:15 +0100 | [diff] [blame] | 1331 | void __key_link_end(struct key *keyring, |
| 1332 | const struct keyring_index_key *index_key, |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1333 | struct assoc_array_edit *edit) |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1334 | __releases(&keyring->sem) |
David Howells | 3be59f7 | 2019-05-30 11:40:24 +0100 | [diff] [blame] | 1335 | __releases(&keyring_serialise_link_lock) |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1336 | { |
David Howells | 16feef4 | 2013-09-24 10:35:15 +0100 | [diff] [blame] | 1337 | BUG_ON(index_key->type == NULL); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1338 | kenter("%d,%s,", keyring->serial, index_key->type->name); |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1339 | |
Colin Ian King | ca4da5d | 2015-07-27 15:23:43 +0100 | [diff] [blame] | 1340 | if (edit) { |
| 1341 | if (!edit->dead_leaf) { |
| 1342 | key_payload_reserve(keyring, |
| 1343 | keyring->datalen - KEYQUOTA_LINK_BYTES); |
| 1344 | } |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1345 | assoc_array_cancel_edit(edit); |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1346 | } |
| 1347 | up_write(&keyring->sem); |
David Howells | df593ee | 2019-05-30 11:37:39 +0100 | [diff] [blame] | 1348 | |
| 1349 | if (index_key->type == &key_type_keyring) |
| 1350 | mutex_unlock(&keyring_serialise_link_lock); |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1351 | } |
| 1352 | |
David Howells | 5ac7eac | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 1353 | /* |
| 1354 | * Check addition of keys to restricted keyrings. |
| 1355 | */ |
| 1356 | static int __key_link_check_restriction(struct key *keyring, struct key *key) |
| 1357 | { |
Mat Martineau | 2b6aa41 | 2016-08-31 16:05:43 -0700 | [diff] [blame] | 1358 | if (!keyring->restrict_link || !keyring->restrict_link->check) |
David Howells | 5ac7eac | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 1359 | return 0; |
Mat Martineau | 2b6aa41 | 2016-08-31 16:05:43 -0700 | [diff] [blame] | 1360 | return keyring->restrict_link->check(keyring, key->type, &key->payload, |
| 1361 | keyring->restrict_link->key); |
David Howells | 5ac7eac | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 1362 | } |
| 1363 | |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1364 | /** |
| 1365 | * key_link - Link a key to a keyring |
| 1366 | * @keyring: The keyring to make the link in. |
| 1367 | * @key: The key to link to. |
| 1368 | * |
| 1369 | * Make a link in a keyring to a key, such that the keyring holds a reference |
| 1370 | * on that key and the key can potentially be found by searching that keyring. |
| 1371 | * |
| 1372 | * This function will write-lock the keyring's semaphore and will consume some |
| 1373 | * of the user's key data quota to hold the link. |
| 1374 | * |
| 1375 | * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, |
| 1376 | * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is |
| 1377 | * full, -EDQUOT if there is insufficient key data quota remaining to add |
| 1378 | * another link or -ENOMEM if there's insufficient memory. |
| 1379 | * |
| 1380 | * It is assumed that the caller has checked that it is permitted for a link to |
| 1381 | * be made (the keyring should have Write permission and the key Link |
| 1382 | * permission). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | */ |
| 1384 | int key_link(struct key *keyring, struct key *key) |
| 1385 | { |
David Howells | df593ee | 2019-05-30 11:37:39 +0100 | [diff] [blame] | 1386 | struct assoc_array_edit *edit = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | int ret; |
| 1388 | |
Elena Reshetova | fff2929 | 2017-03-31 15:20:48 +0300 | [diff] [blame] | 1389 | kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage)); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | key_check(keyring); |
| 1392 | key_check(key); |
| 1393 | |
David Howells | df593ee | 2019-05-30 11:37:39 +0100 | [diff] [blame] | 1394 | ret = __key_link_lock(keyring, &key->index_key); |
| 1395 | if (ret < 0) |
| 1396 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | |
David Howells | df593ee | 2019-05-30 11:37:39 +0100 | [diff] [blame] | 1398 | ret = __key_link_begin(keyring, &key->index_key, &edit); |
| 1399 | if (ret < 0) |
| 1400 | goto error_end; |
| 1401 | |
| 1402 | kdebug("begun {%d,%d}", keyring->serial, refcount_read(&keyring->usage)); |
| 1403 | ret = __key_link_check_restriction(keyring, key); |
| 1404 | if (ret == 0) |
| 1405 | ret = __key_link_check_live_key(keyring, key); |
| 1406 | if (ret == 0) |
| 1407 | __key_link(key, &edit); |
| 1408 | |
| 1409 | error_end: |
| 1410 | __key_link_end(keyring, &key->index_key, edit); |
| 1411 | error: |
Elena Reshetova | fff2929 | 2017-03-31 15:20:48 +0300 | [diff] [blame] | 1412 | kleave(" = %d {%d,%d}", ret, keyring->serial, refcount_read(&keyring->usage)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | return ret; |
David Howells | f70e2e0 | 2010-04-30 14:32:39 +0100 | [diff] [blame] | 1414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | EXPORT_SYMBOL(key_link); |
| 1416 | |
David Howells | eb0f68c | 2019-05-30 14:19:20 +0100 | [diff] [blame] | 1417 | /* |
| 1418 | * Lock a keyring for unlink. |
| 1419 | */ |
| 1420 | static int __key_unlink_lock(struct key *keyring) |
| 1421 | __acquires(&keyring->sem) |
| 1422 | { |
| 1423 | if (keyring->type != &key_type_keyring) |
| 1424 | return -ENOTDIR; |
| 1425 | |
| 1426 | down_write(&keyring->sem); |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | /* |
| 1431 | * Begin the process of unlinking a key from a keyring. |
| 1432 | */ |
| 1433 | static int __key_unlink_begin(struct key *keyring, struct key *key, |
| 1434 | struct assoc_array_edit **_edit) |
| 1435 | { |
| 1436 | struct assoc_array_edit *edit; |
| 1437 | |
| 1438 | BUG_ON(*_edit != NULL); |
| 1439 | |
| 1440 | edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops, |
| 1441 | &key->index_key); |
| 1442 | if (IS_ERR(edit)) |
| 1443 | return PTR_ERR(edit); |
| 1444 | |
| 1445 | if (!edit) |
| 1446 | return -ENOENT; |
| 1447 | |
| 1448 | *_edit = edit; |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
| 1452 | /* |
| 1453 | * Apply an unlink change. |
| 1454 | */ |
| 1455 | static void __key_unlink(struct key *keyring, struct key *key, |
| 1456 | struct assoc_array_edit **_edit) |
| 1457 | { |
| 1458 | assoc_array_apply_edit(*_edit); |
| 1459 | *_edit = NULL; |
| 1460 | key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES); |
| 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | * Finish unlinking a key from to a keyring. |
| 1465 | */ |
| 1466 | static void __key_unlink_end(struct key *keyring, |
| 1467 | struct key *key, |
| 1468 | struct assoc_array_edit *edit) |
| 1469 | __releases(&keyring->sem) |
| 1470 | { |
| 1471 | if (edit) |
| 1472 | assoc_array_cancel_edit(edit); |
| 1473 | up_write(&keyring->sem); |
| 1474 | } |
| 1475 | |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1476 | /** |
| 1477 | * key_unlink - Unlink the first link to a key from a keyring. |
| 1478 | * @keyring: The keyring to remove the link from. |
| 1479 | * @key: The key the link is to. |
| 1480 | * |
| 1481 | * Remove a link from a keyring to a key. |
| 1482 | * |
| 1483 | * This function will write-lock the keyring's semaphore. |
| 1484 | * |
| 1485 | * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if |
| 1486 | * the key isn't linked to by the keyring or -ENOMEM if there's insufficient |
| 1487 | * memory. |
| 1488 | * |
| 1489 | * It is assumed that the caller has checked that it is permitted for a link to |
| 1490 | * be removed (the keyring should have Write permission; no permissions are |
| 1491 | * required on the key). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | */ |
| 1493 | int key_unlink(struct key *keyring, struct key *key) |
| 1494 | { |
David Howells | eb0f68c | 2019-05-30 14:19:20 +0100 | [diff] [blame] | 1495 | struct assoc_array_edit *edit = NULL; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1496 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | |
| 1498 | key_check(keyring); |
| 1499 | key_check(key); |
| 1500 | |
David Howells | eb0f68c | 2019-05-30 14:19:20 +0100 | [diff] [blame] | 1501 | ret = __key_unlink_lock(keyring); |
| 1502 | if (ret < 0) |
| 1503 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | |
David Howells | eb0f68c | 2019-05-30 14:19:20 +0100 | [diff] [blame] | 1505 | ret = __key_unlink_begin(keyring, key, &edit); |
| 1506 | if (ret == 0) |
| 1507 | __key_unlink(keyring, key, &edit); |
| 1508 | __key_unlink_end(keyring, key, edit); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1509 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | EXPORT_SYMBOL(key_unlink); |
| 1512 | |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1513 | /** |
David Howells | ed0ac5c | 2019-05-20 21:51:50 +0100 | [diff] [blame] | 1514 | * key_move - Move a key from one keyring to another |
| 1515 | * @key: The key to move |
| 1516 | * @from_keyring: The keyring to remove the link from. |
| 1517 | * @to_keyring: The keyring to make the link in. |
| 1518 | * @flags: Qualifying flags, such as KEYCTL_MOVE_EXCL. |
| 1519 | * |
| 1520 | * Make a link in @to_keyring to a key, such that the keyring holds a reference |
| 1521 | * on that key and the key can potentially be found by searching that keyring |
| 1522 | * whilst simultaneously removing a link to the key from @from_keyring. |
| 1523 | * |
| 1524 | * This function will write-lock both keyring's semaphores and will consume |
| 1525 | * some of the user's key data quota to hold the link on @to_keyring. |
| 1526 | * |
| 1527 | * Returns 0 if successful, -ENOTDIR if either keyring isn't a keyring, |
| 1528 | * -EKEYREVOKED if either keyring has been revoked, -ENFILE if the second |
| 1529 | * keyring is full, -EDQUOT if there is insufficient key data quota remaining |
| 1530 | * to add another link or -ENOMEM if there's insufficient memory. If |
| 1531 | * KEYCTL_MOVE_EXCL is set, then -EEXIST will be returned if there's already a |
| 1532 | * matching key in @to_keyring. |
| 1533 | * |
| 1534 | * It is assumed that the caller has checked that it is permitted for a link to |
| 1535 | * be made (the keyring should have Write permission and the key Link |
| 1536 | * permission). |
| 1537 | */ |
| 1538 | int key_move(struct key *key, |
| 1539 | struct key *from_keyring, |
| 1540 | struct key *to_keyring, |
| 1541 | unsigned int flags) |
| 1542 | { |
| 1543 | struct assoc_array_edit *from_edit = NULL, *to_edit = NULL; |
| 1544 | int ret; |
| 1545 | |
| 1546 | kenter("%d,%d,%d", key->serial, from_keyring->serial, to_keyring->serial); |
| 1547 | |
| 1548 | if (from_keyring == to_keyring) |
| 1549 | return 0; |
| 1550 | |
| 1551 | key_check(key); |
| 1552 | key_check(from_keyring); |
| 1553 | key_check(to_keyring); |
| 1554 | |
| 1555 | ret = __key_move_lock(from_keyring, to_keyring, &key->index_key); |
| 1556 | if (ret < 0) |
| 1557 | goto out; |
| 1558 | ret = __key_unlink_begin(from_keyring, key, &from_edit); |
| 1559 | if (ret < 0) |
| 1560 | goto error; |
| 1561 | ret = __key_link_begin(to_keyring, &key->index_key, &to_edit); |
| 1562 | if (ret < 0) |
| 1563 | goto error; |
| 1564 | |
| 1565 | ret = -EEXIST; |
| 1566 | if (to_edit->dead_leaf && (flags & KEYCTL_MOVE_EXCL)) |
| 1567 | goto error; |
| 1568 | |
| 1569 | ret = __key_link_check_restriction(to_keyring, key); |
| 1570 | if (ret < 0) |
| 1571 | goto error; |
| 1572 | ret = __key_link_check_live_key(to_keyring, key); |
| 1573 | if (ret < 0) |
| 1574 | goto error; |
| 1575 | |
| 1576 | __key_unlink(from_keyring, key, &from_edit); |
| 1577 | __key_link(key, &to_edit); |
| 1578 | error: |
| 1579 | __key_link_end(to_keyring, &key->index_key, to_edit); |
| 1580 | __key_unlink_end(from_keyring, key, from_edit); |
| 1581 | out: |
| 1582 | kleave(" = %d", ret); |
| 1583 | return ret; |
| 1584 | } |
| 1585 | EXPORT_SYMBOL(key_move); |
| 1586 | |
| 1587 | /** |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1588 | * keyring_clear - Clear a keyring |
| 1589 | * @keyring: The keyring to clear. |
| 1590 | * |
| 1591 | * Clear the contents of the specified keyring. |
| 1592 | * |
| 1593 | * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | */ |
| 1595 | int keyring_clear(struct key *keyring) |
| 1596 | { |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1597 | struct assoc_array_edit *edit; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 1598 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1600 | if (keyring->type != &key_type_keyring) |
| 1601 | return -ENOTDIR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1603 | down_write(&keyring->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1605 | edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops); |
| 1606 | if (IS_ERR(edit)) { |
| 1607 | ret = PTR_ERR(edit); |
| 1608 | } else { |
| 1609 | if (edit) |
| 1610 | assoc_array_apply_edit(edit); |
| 1611 | key_payload_reserve(keyring, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | ret = 0; |
| 1613 | } |
| 1614 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1615 | up_write(&keyring->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | EXPORT_SYMBOL(keyring_clear); |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 1619 | |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 1620 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1621 | * Dispose of the links from a revoked keyring. |
| 1622 | * |
| 1623 | * This is called with the key sem write-locked. |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 1624 | */ |
| 1625 | static void keyring_revoke(struct key *keyring) |
| 1626 | { |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1627 | struct assoc_array_edit *edit; |
David Howells | f0641cb | 2010-04-30 14:32:18 +0100 | [diff] [blame] | 1628 | |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1629 | edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops); |
| 1630 | if (!IS_ERR(edit)) { |
| 1631 | if (edit) |
| 1632 | assoc_array_apply_edit(edit); |
| 1633 | key_payload_reserve(keyring, 0); |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 1634 | } |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1635 | } |
David Howells | 5d13544 | 2009-09-02 09:14:00 +0100 | [diff] [blame] | 1636 | |
David Howells | 62fe318 | 2013-11-14 13:02:31 +0000 | [diff] [blame] | 1637 | static bool keyring_gc_select_iterator(void *object, void *iterator_data) |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1638 | { |
| 1639 | struct key *key = keyring_ptr_to_key(object); |
Baolin Wang | 074d589 | 2017-11-15 16:38:45 +0000 | [diff] [blame] | 1640 | time64_t *limit = iterator_data; |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1641 | |
| 1642 | if (key_is_dead(key, *limit)) |
| 1643 | return false; |
| 1644 | key_get(key); |
| 1645 | return true; |
| 1646 | } |
| 1647 | |
David Howells | 62fe318 | 2013-11-14 13:02:31 +0000 | [diff] [blame] | 1648 | static int keyring_gc_check_iterator(const void *object, void *iterator_data) |
| 1649 | { |
| 1650 | const struct key *key = keyring_ptr_to_key(object); |
Baolin Wang | 074d589 | 2017-11-15 16:38:45 +0000 | [diff] [blame] | 1651 | time64_t *limit = iterator_data; |
David Howells | 62fe318 | 2013-11-14 13:02:31 +0000 | [diff] [blame] | 1652 | |
| 1653 | key_check(key); |
| 1654 | return key_is_dead(key, *limit); |
| 1655 | } |
| 1656 | |
David Howells | 5d13544 | 2009-09-02 09:14:00 +0100 | [diff] [blame] | 1657 | /* |
David Howells | 62fe318 | 2013-11-14 13:02:31 +0000 | [diff] [blame] | 1658 | * Garbage collect pointers from a keyring. |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1659 | * |
David Howells | 62fe318 | 2013-11-14 13:02:31 +0000 | [diff] [blame] | 1660 | * Not called with any locks held. The keyring's key struct will not be |
| 1661 | * deallocated under us as only our caller may deallocate it. |
David Howells | 5d13544 | 2009-09-02 09:14:00 +0100 | [diff] [blame] | 1662 | */ |
Baolin Wang | 074d589 | 2017-11-15 16:38:45 +0000 | [diff] [blame] | 1663 | void keyring_gc(struct key *keyring, time64_t limit) |
David Howells | 5d13544 | 2009-09-02 09:14:00 +0100 | [diff] [blame] | 1664 | { |
David Howells | 62fe318 | 2013-11-14 13:02:31 +0000 | [diff] [blame] | 1665 | int result; |
David Howells | 5d13544 | 2009-09-02 09:14:00 +0100 | [diff] [blame] | 1666 | |
David Howells | 62fe318 | 2013-11-14 13:02:31 +0000 | [diff] [blame] | 1667 | kenter("%x{%s}", keyring->serial, keyring->description ?: ""); |
| 1668 | |
| 1669 | if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) | |
| 1670 | (1 << KEY_FLAG_REVOKED))) |
| 1671 | goto dont_gc; |
| 1672 | |
| 1673 | /* scan the keyring looking for dead keys */ |
| 1674 | rcu_read_lock(); |
| 1675 | result = assoc_array_iterate(&keyring->keys, |
| 1676 | keyring_gc_check_iterator, &limit); |
| 1677 | rcu_read_unlock(); |
| 1678 | if (result == true) |
| 1679 | goto do_gc; |
| 1680 | |
| 1681 | dont_gc: |
| 1682 | kleave(" [no gc]"); |
| 1683 | return; |
| 1684 | |
| 1685 | do_gc: |
David Howells | 5d13544 | 2009-09-02 09:14:00 +0100 | [diff] [blame] | 1686 | down_write(&keyring->sem); |
David Howells | b2a4df2 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1687 | assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops, |
David Howells | 62fe318 | 2013-11-14 13:02:31 +0000 | [diff] [blame] | 1688 | keyring_gc_select_iterator, &limit); |
David Howells | 5d13544 | 2009-09-02 09:14:00 +0100 | [diff] [blame] | 1689 | up_write(&keyring->sem); |
David Howells | 62fe318 | 2013-11-14 13:02:31 +0000 | [diff] [blame] | 1690 | kleave(" [gc]"); |
David Howells | 5d13544 | 2009-09-02 09:14:00 +0100 | [diff] [blame] | 1691 | } |
Mat Martineau | 2b6aa41 | 2016-08-31 16:05:43 -0700 | [diff] [blame] | 1692 | |
| 1693 | /* |
| 1694 | * Garbage collect restriction pointers from a keyring. |
| 1695 | * |
| 1696 | * Keyring restrictions are associated with a key type, and must be cleaned |
| 1697 | * up if the key type is unregistered. The restriction is altered to always |
| 1698 | * reject additional keys so a keyring cannot be opened up by unregistering |
| 1699 | * a key type. |
| 1700 | * |
| 1701 | * Not called with any keyring locks held. The keyring's key struct will not |
| 1702 | * be deallocated under us as only our caller may deallocate it. |
| 1703 | * |
| 1704 | * The caller is required to hold key_types_sem and dead_type->sem. This is |
| 1705 | * fulfilled by key_gc_keytype() holding the locks on behalf of |
| 1706 | * key_garbage_collector(), which it invokes on a workqueue. |
| 1707 | */ |
| 1708 | void keyring_restriction_gc(struct key *keyring, struct key_type *dead_type) |
| 1709 | { |
| 1710 | struct key_restriction *keyres; |
| 1711 | |
| 1712 | kenter("%x{%s}", keyring->serial, keyring->description ?: ""); |
| 1713 | |
| 1714 | /* |
| 1715 | * keyring->restrict_link is only assigned at key allocation time |
| 1716 | * or with the key type locked, so the only values that could be |
| 1717 | * concurrently assigned to keyring->restrict_link are for key |
| 1718 | * types other than dead_type. Given this, it's ok to check |
| 1719 | * the key type before acquiring keyring->sem. |
| 1720 | */ |
| 1721 | if (!dead_type || !keyring->restrict_link || |
| 1722 | keyring->restrict_link->keytype != dead_type) { |
| 1723 | kleave(" [no restriction gc]"); |
| 1724 | return; |
| 1725 | } |
| 1726 | |
| 1727 | /* Lock the keyring to ensure that a link is not in progress */ |
| 1728 | down_write(&keyring->sem); |
| 1729 | |
| 1730 | keyres = keyring->restrict_link; |
| 1731 | |
| 1732 | keyres->check = restrict_link_reject; |
| 1733 | |
| 1734 | key_put(keyres->key); |
| 1735 | keyres->key = NULL; |
| 1736 | keyres->keytype = NULL; |
| 1737 | |
| 1738 | up_write(&keyring->sem); |
| 1739 | |
| 1740 | kleave(" [restriction gc]"); |
| 1741 | } |