blob: 3663e516858393c8c764ccc931c944787307ae11 [file] [log] [blame]
David Howells69664cf2008-04-29 01:01:31 -07001/* Keyring handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howellsb2a4df22013-09-24 10:35:18 +01003 * Copyright (C) 2004-2005, 2008, 2013 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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 Gortmaker876979c2018-12-09 15:36:29 -050012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
14#include <linux/sched.h>
15#include <linux/slab.h>
David Howells29db9192005-10-30 15:02:44 -080016#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/seq_file.h>
18#include <linux/err.h>
David Howellsb206f282019-06-26 21:02:32 +010019#include <linux/user_namespace.h>
David Howellse9e349b2008-11-14 10:39:13 +110020#include <keys/keyring-type.h>
David Howellsb2a4df22013-09-24 10:35:18 +010021#include <keys/user-type.h>
22#include <linux/assoc_array_priv.h>
Chihau Chau512ea3b2010-03-08 20:11:34 -030023#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "internal.h"
25
26/*
David Howells973c9f42011-01-20 16:38:33 +000027 * When plumbing the depths of the key tree, this sets a hard limit
28 * set on how deep we're willing to go.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30#define KEYRING_SEARCH_MAX_DEPTH 6
31
32/*
David Howellsb2a4df22013-09-24 10:35:18 +010033 * 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
38static inline bool keyring_ptr_is_keyring(const struct assoc_array_ptr *x)
39{
40 return (unsigned long)x & KEYRING_PTR_SUBTYPE;
41}
42static 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}
47static 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 Torvalds1da177e2005-04-16 15:20:36 -070054static DEFINE_RWLOCK(keyring_name_lock);
55
David Howellsb206f282019-06-26 21:02:32 +010056/*
57 * Clean up the bits of user_namespace that belong to us.
58 */
59void key_free_user_ns(struct user_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
David Howellsb206f282019-06-26 21:02:32 +010061 write_lock(&keyring_name_lock);
62 list_del_init(&ns->keyring_name_list);
63 write_unlock(&keyring_name_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
David Howells0f44e4d2019-06-26 21:02:32 +010065 key_put(ns->user_keyring_register);
David Howellsb206f282019-06-26 21:02:32 +010066#ifdef CONFIG_PERSISTENT_KEYRINGS
67 key_put(ns->persistent_keyring_register);
68#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71/*
David Howells973c9f42011-01-20 16:38:33 +000072 * 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 Torvalds1da177e2005-04-16 15:20:36 -070075 */
David Howells5d19e202014-07-18 18:56:36 +010076static int keyring_preparse(struct key_preparsed_payload *prep);
77static void keyring_free_preparse(struct key_preparsed_payload *prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int keyring_instantiate(struct key *keyring,
David Howellscf7f6012012-09-13 13:06:29 +010079 struct key_preparsed_payload *prep);
David Howells31204ed2006-06-26 00:24:51 -070080static void keyring_revoke(struct key *keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static void keyring_destroy(struct key *keyring);
82static void keyring_describe(const struct key *keyring, struct seq_file *m);
83static long keyring_read(const struct key *keyring,
84 char __user *buffer, size_t buflen);
85
86struct key_type key_type_keyring = {
87 .name = "keyring",
David Howellsb2a4df22013-09-24 10:35:18 +010088 .def_datalen = 0,
David Howells5d19e202014-07-18 18:56:36 +010089 .preparse = keyring_preparse,
90 .free_preparse = keyring_free_preparse,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .instantiate = keyring_instantiate,
David Howells31204ed2006-06-26 00:24:51 -070092 .revoke = keyring_revoke,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .destroy = keyring_destroy,
94 .describe = keyring_describe,
95 .read = keyring_read,
96};
David Howells73182262007-04-26 15:46:23 -070097EXPORT_SYMBOL(key_type_keyring);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
David Howells973c9f42011-01-20 16:38:33 +0000100 * Semaphore to serialise link/link calls to prevent two link calls in parallel
101 * introducing a cycle.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
David Howells3be59f72019-05-30 11:40:24 +0100103static DEFINE_MUTEX(keyring_serialise_link_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
David Howells973c9f42011-01-20 16:38:33 +0000106 * Publish the name of a keyring so that it can be found by name (if it has
David Howellsb206f282019-06-26 21:02:32 +0100107 * one and it doesn't begin with a dot).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
David Howells69664cf2008-04-29 01:01:31 -0700109static void keyring_publish_name(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
David Howellsb206f282019-06-26 21:02:32 +0100111 struct user_namespace *ns = current_user_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
David Howellsb206f282019-06-26 21:02:32 +0100113 if (keyring->description &&
114 keyring->description[0] &&
115 keyring->description[0] != '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 write_lock(&keyring_name_lock);
David Howellsb206f282019-06-26 21:02:32 +0100117 list_add_tail(&keyring->name_link, &ns->keyring_name_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 write_unlock(&keyring_name_lock);
119 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/*
David Howells5d19e202014-07-18 18:56:36 +0100123 * Preparse a keyring payload
124 */
125static 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 */
133static void keyring_free_preparse(struct key_preparsed_payload *prep)
134{
135}
136
137/*
David Howells973c9f42011-01-20 16:38:33 +0000138 * Initialise a keyring.
139 *
140 * Returns 0 on success, -EINVAL if given any data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 */
142static int keyring_instantiate(struct key *keyring,
David Howellscf7f6012012-09-13 13:06:29 +0100143 struct key_preparsed_payload *prep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
David Howells5d19e202014-07-18 18:56:36 +0100145 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 Howellsa8b17ed2011-01-20 16:38:27 +0000149}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
David Howellsb2a4df22013-09-24 10:35:18 +0100152 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700154 */
David Howellsb2a4df22013-09-24 10:35:18 +0100155static u64 mult_64x32_and_fold(u64 x, u32 y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
David Howellsb2a4df22013-09-24 10:35:18 +0100157 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 Howellsa8b17ed2011-01-20 16:38:27 +0000160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/*
David Howellsb2a4df22013-09-24 10:35:18 +0100163 * Hash a key type and description.
164 */
David Howells355ef8e2019-06-26 21:02:32 +0100165static void hash_key_type_and_desc(struct keyring_index_key *index_key)
David Howellsb2a4df22013-09-24 10:35:18 +0100166{
167 const unsigned level_shift = ASSOC_ARRAY_LEVEL_STEP;
David Howellsd54e58b2013-12-02 11:24:18 +0000168 const unsigned long fan_mask = ASSOC_ARRAY_FAN_MASK;
David Howellsb2a4df22013-09-24 10:35:18 +0100169 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 Howellsb2a4df22013-09-24 10:35:18 +0100176 acc = mult_64x32_and_fold(type, desc_len + 13);
177 acc = mult_64x32_and_fold(acc, 9207);
David Howellsf771fde2019-06-26 21:02:31 +0100178
David Howellsb2a4df22013-09-24 10:35:18 +0100179 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 Howellsd54e58b2013-12-02 11:24:18 +0000202 if (index_key->type != &key_type_keyring && (hash & fan_mask) == 0)
David Howells355ef8e2019-06-26 21:02:32 +0100203 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 */
213void 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 Howellsb2a4df22013-09-24 10:35:18 +0100219}
220
221/*
222 * Build the next index key chunk.
223 *
David Howellsb2a4df22013-09-24 10:35:18 +0100224 * We return it one word-sized chunk at a time.
225 */
226static 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 Howellsf771fde2019-06-26 21:02:31 +0100230 const u8 *d;
David Howellsb2a4df22013-09-24 10:35:18 +0100231 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 Howells355ef8e2019-06-26 21:02:32 +0100236 return index_key->hash;
David Howellsb2a4df22013-09-24 10:35:18 +0100237 case 1:
David Howellsf771fde2019-06-26 21:02:31 +0100238 return index_key->x;
David Howellsb2a4df22013-09-24 10:35:18 +0100239 case 2:
David Howellsf771fde2019-06-26 21:02:31 +0100240 return (unsigned long)index_key->type;
David Howellsb2a4df22013-09-24 10:35:18 +0100241 default:
David Howellsf771fde2019-06-26 21:02:31 +0100242 level -= 3;
243 if (desc_len <= sizeof(index_key->desc))
David Howellsb2a4df22013-09-24 10:35:18 +0100244 return 0;
David Howellsf771fde2019-06-26 21:02:31 +0100245
246 d = index_key->description + sizeof(index_key->desc);
247 d += level * sizeof(long);
248 desc_len -= sizeof(index_key->desc);
David Howellsb2a4df22013-09-24 10:35:18 +0100249 if (desc_len > n)
250 desc_len = n;
David Howellsb2a4df22013-09-24 10:35:18 +0100251 do {
252 chunk <<= 8;
David Howellsf771fde2019-06-26 21:02:31 +0100253 chunk |= *d++;
David Howellsb2a4df22013-09-24 10:35:18 +0100254 } while (--desc_len > 0);
David Howellsb2a4df22013-09-24 10:35:18 +0100255 return chunk;
256 }
257}
258
259static 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
265static 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 Howells23fd78d2013-12-02 11:24:18 +0000280static int keyring_diff_objects(const void *object, const void *data)
David Howellsb2a4df22013-09-24 10:35:18 +0100281{
David Howells23fd78d2013-12-02 11:24:18 +0000282 const struct key *key_a = keyring_ptr_to_key(object);
David Howellsb2a4df22013-09-24 10:35:18 +0100283 const struct keyring_index_key *a = &key_a->index_key;
David Howells23fd78d2013-12-02 11:24:18 +0000284 const struct keyring_index_key *b = data;
David Howellsb2a4df22013-09-24 10:35:18 +0100285 unsigned long seg_a, seg_b;
286 int level, i;
287
288 level = 0;
David Howells355ef8e2019-06-26 21:02:32 +0100289 seg_a = a->hash;
290 seg_b = b->hash;
David Howellsb2a4df22013-09-24 10:35:18 +0100291 if ((seg_a ^ seg_b) != 0)
292 goto differ;
David Howellsf771fde2019-06-26 21:02:31 +0100293 level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8;
David Howellsb2a4df22013-09-24 10:35:18 +0100294
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 Howellsf771fde2019-06-26 21:02:31 +0100299 seg_a = a->x;
300 seg_b = b->x;
David Howellsb2a4df22013-09-24 10:35:18 +0100301 if ((seg_a ^ seg_b) != 0)
302 goto differ;
David Howellsf771fde2019-06-26 21:02:31 +0100303 level += sizeof(unsigned long);
David Howellsb2a4df22013-09-24 10:35:18 +0100304
305 /* The next bit may not work on big endian */
David Howellsb2a4df22013-09-24 10:35:18 +0100306 seg_a = (unsigned long)a->type;
307 seg_b = (unsigned long)b->type;
308 if ((seg_a ^ seg_b) != 0)
309 goto differ;
David Howellsb2a4df22013-09-24 10:35:18 +0100310 level += sizeof(unsigned long);
David Howellsb2a4df22013-09-24 10:35:18 +0100311
David Howellsf771fde2019-06-26 21:02:31 +0100312 i = sizeof(a->desc);
313 if (a->desc_len <= i)
314 goto same;
David Howellsb2a4df22013-09-24 10:35:18 +0100315
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
323same:
324 return -1;
325
326differ_plus_i:
327 level += i;
328differ:
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 */
336static 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 */
344static 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 Howells973c9f42011-01-20 16:38:33 +0000353 * Clean up a keyring when it is destroyed. Unpublish its name if it had one
354 * and dispose of its data.
David Howells233e4732012-05-11 10:56:56 +0100355 *
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 Torvalds1da177e2005-04-16 15:20:36 -0700360 */
361static void keyring_destroy(struct key *keyring)
362{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (keyring->description) {
364 write_lock(&keyring_name_lock);
David Howells94efe722005-08-04 13:07:07 -0700365
David Howells146aa8b2015-10-21 14:04:48 +0100366 if (keyring->name_link.next != NULL &&
367 !list_empty(&keyring->name_link))
368 list_del(&keyring->name_link);
David Howells94efe722005-08-04 13:07:07 -0700369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 write_unlock(&keyring_name_lock);
371 }
372
Mat Martineau2b6aa412016-08-31 16:05:43 -0700373 if (keyring->restrict_link) {
374 struct key_restriction *keyres = keyring->restrict_link;
375
376 key_put(keyres->key);
377 kfree(keyres);
378 }
379
David Howellsb2a4df22013-09-24 10:35:18 +0100380 assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops);
David Howellsa8b17ed2011-01-20 16:38:27 +0000381}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/*
David Howells973c9f42011-01-20 16:38:33 +0000384 * Describe a keyring for /proc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 */
386static void keyring_describe(const struct key *keyring, struct seq_file *m)
387{
wzt.wzt@gmail.comc8563472010-03-04 21:26:23 +0800388 if (keyring->description)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 seq_puts(m, keyring->description);
wzt.wzt@gmail.comc8563472010-03-04 21:26:23 +0800390 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 seq_puts(m, "[anon]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
David Howells363b02d2017-10-04 16:43:25 +0100393 if (key_is_positive(keyring)) {
David Howellsb2a4df22013-09-24 10:35:18 +0100394 if (keyring->keys.nr_leaves_on_tree != 0)
395 seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree);
David Howells78b72802011-03-11 17:57:23 +0000396 else
397 seq_puts(m, ": empty");
David Howells78b72802011-03-11 17:57:23 +0000398 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000399}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David Howellsb2a4df22013-09-24 10:35:18 +0100401struct keyring_read_iterator_context {
Eric Biggerse6450162017-09-18 11:36:45 -0700402 size_t buflen;
David Howellsb2a4df22013-09-24 10:35:18 +0100403 size_t count;
404 key_serial_t __user *buffer;
405};
406
407static 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 Biggerse6450162017-09-18 11:36:45 -0700414 key->type->name, key->serial, ctx->count, ctx->buflen);
David Howellsb2a4df22013-09-24 10:35:18 +0100415
Eric Biggerse6450162017-09-18 11:36:45 -0700416 if (ctx->count >= ctx->buflen)
David Howellsb2a4df22013-09-24 10:35:18 +0100417 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 Torvalds1da177e2005-04-16 15:20:36 -0700427/*
David Howells973c9f42011-01-20 16:38:33 +0000428 * Read a list of key IDs from the keyring's contents in binary form
429 *
David Howellsb2a4df22013-09-24 10:35:18 +0100430 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700433 */
434static long keyring_read(const struct key *keyring,
435 char __user *buffer, size_t buflen)
436{
David Howellsb2a4df22013-09-24 10:35:18 +0100437 struct keyring_read_iterator_context ctx;
Eric Biggers3239b6f2017-11-02 00:47:03 +0000438 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
David Howellsb2a4df22013-09-24 10:35:18 +0100440 kenter("{%d},,%zu", key_serial(keyring), buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
David Howellsb2a4df22013-09-24 10:35:18 +0100442 if (buflen & (sizeof(key_serial_t) - 1))
443 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Eric Biggers3239b6f2017-11-02 00:47:03 +0000445 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
Eric Biggers3239b6f2017-11-02 00:47:03 +0000458 /* 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 Howellsa8b17ed2011-01-20 16:38:27 +0000465}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/*
David Howells973c9f42011-01-20 16:38:33 +0000468 * Allocate a keyring and link into the destination keyring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800470struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
David Howells96b5c8f2012-10-02 19:24:56 +0100471 const struct cred *cred, key_perm_t perm,
David Howells5ac7eac2016-04-06 16:14:24 +0100472 unsigned long flags,
Mat Martineau2b6aa412016-08-31 16:05:43 -0700473 struct key_restriction *restrict_link,
David Howells5ac7eac2016-04-06 16:14:24 +0100474 struct key *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct key *keyring;
477 int ret;
478
479 keyring = key_alloc(&key_type_keyring, description,
David Howells5ac7eac2016-04-06 16:14:24 +0100480 uid, gid, cred, perm, flags, restrict_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (!IS_ERR(keyring)) {
David Howells3e301482005-06-23 22:00:56 -0700482 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (ret < 0) {
484 key_put(keyring);
485 keyring = ERR_PTR(ret);
486 }
487 }
488
489 return keyring;
David Howellsa8b17ed2011-01-20 16:38:27 +0000490}
David Howellsf8aa23a2012-10-02 19:24:56 +0100491EXPORT_SYMBOL(keyring_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
David Howells5ac7eac2016-04-06 16:14:24 +0100493/**
David Howells5ac7eac2016-04-06 16:14:24 +0100494 * restrict_link_reject - Give -EPERM to restrict link
495 * @keyring: The keyring being added to.
496 * @type: The type of key being added.
David Howells5ac7eac2016-04-06 16:14:24 +0100497 * @payload: The payload of the key intended to be added.
David Howells9fd16532019-05-22 13:30:56 +0100498 * @restriction_key: Keys providing additional data for evaluating restriction.
David Howells5ac7eac2016-04-06 16:14:24 +0100499 *
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 Martineau2b6aa412016-08-31 16:05:43 -0700504 * This is meant to be stored in a key_restriction structure which is passed
505 * in the restrict_link parameter to keyring_alloc().
David Howells5ac7eac2016-04-06 16:14:24 +0100506 */
507int restrict_link_reject(struct key *keyring,
508 const struct key_type *type,
Mat Martineauaaf66c82016-08-30 11:33:13 -0700509 const union key_payload *payload,
510 struct key *restriction_key)
David Howells5ac7eac2016-04-06 16:14:24 +0100511{
512 return -EPERM;
513}
514
David Howellsb2a4df22013-09-24 10:35:18 +0100515/*
David Howellsc06cfb02014-09-16 17:36:06 +0100516 * By default, we keys found by getting an exact match on their descriptions.
517 */
David Howells0c903ab2014-09-16 17:36:08 +0100518bool key_default_cmp(const struct key *key,
519 const struct key_match_data *match_data)
David Howellsc06cfb02014-09-16 17:36:06 +0100520{
521 return strcmp(key->description, match_data->raw_data) == 0;
522}
523
524/*
David Howellsb2a4df22013-09-24 10:35:18 +0100525 * Iteration function to consider each key found.
526 */
527static 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 Howells363b02d2017-10-04 16:43:25 +0100531 unsigned long kflags = READ_ONCE(key->flags);
532 short state = READ_ONCE(key->state);
David Howellsb2a4df22013-09-24 10:35:18 +0100533
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 Wang074d5892017-11-15 16:38:45 +0000544 time64_t expiry = READ_ONCE(key->expiry);
Eric Biggers9d6c8712017-09-27 12:50:45 -0700545
David Howellsb2a4df22013-09-24 10:35:18 +0100546 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 Wang074d5892017-11-15 16:38:45 +0000553 if (expiry && ctx->now >= expiry) {
David Howells0b0a8412014-12-01 22:52:53 +0000554 if (!(ctx->flags & KEYRING_SEARCH_SKIP_EXPIRED))
555 ctx->result = ERR_PTR(-EKEYEXPIRED);
David Howellsb2a4df22013-09-24 10:35:18 +0100556 kleave(" = %d [expire]", ctx->skipped_ret);
557 goto skipped;
558 }
559 }
560
561 /* keys that don't match */
David Howells46291952014-09-16 17:36:02 +0100562 if (!ctx->match_data.cmp(key, &ctx->match_data)) {
David Howellsb2a4df22013-09-24 10:35:18 +0100563 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 Howellsf5895942014-03-14 17:44:49 +0000570 ctx->cred, KEY_NEED_SEARCH) < 0) {
David Howellsb2a4df22013-09-24 10:35:18 +0100571 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 Howells363b02d2017-10-04 16:43:25 +0100578 if (state < 0) {
579 ctx->result = ERR_PTR(state);
David Howellsb2a4df22013-09-24 10:35:18 +0100580 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
590skipped:
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 */
599static int search_keyring(struct key *keyring, struct keyring_search_context *ctx)
600{
David Howells46291952014-09-16 17:36:02 +0100601 if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_DIRECT) {
David Howellsb2a4df22013-09-24 10:35:18 +0100602 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 */
616static 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 Howells054f6182014-12-01 22:52:50 +0000636#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 Howellsf771fde2019-06-26 21:02:31 +0100640 if (ctx->index_key.description)
641 key_set_index_key(&ctx->index_key);
642
David Howellsb2a4df22013-09-24 10:35:18 +0100643 /* Check to see if this top-level keyring is what we are looking for
644 * and whether it is valid or not.
645 */
David Howells46291952014-09-16 17:36:02 +0100646 if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_ITERATE ||
David Howellsb2a4df22013-09-24 10:35:18 +0100647 keyring_compare_object(keyring, &ctx->index_key)) {
648 ctx->skipped_ret = 2;
David Howellsb2a4df22013-09-24 10:35:18 +0100649 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 Howellsb2a4df22013-09-24 10:35:18 +0100660
661 /* Start processing a new keyring */
662descend_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 Howellsdcf49db2019-06-26 21:02:32 +0100682 if (!(ctx->flags & KEYRING_SEARCH_RECURSE))
683 goto not_this_keyring;
684
Davidlohr Bueso381f20f2017-06-08 14:47:34 +0100685 ptr = READ_ONCE(keyring->keys.root);
David Howellsb2a4df22013-09-24 10:35:18 +0100686 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 Howellsb2a4df22013-09-24 10:35:18 +0100695 if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0)
696 goto not_this_keyring;
697
Davidlohr Bueso381f20f2017-06-08 14:47:34 +0100698 ptr = READ_ONCE(shortcut->next_node);
David Howellsb2a4df22013-09-24 10:35:18 +0100699 node = assoc_array_ptr_to_node(ptr);
700 goto begin_node;
701 }
702
703 node = assoc_array_ptr_to_node(ptr);
David Howellsb2a4df22013-09-24 10:35:18 +0100704 ptr = node->slots[0];
705 if (!assoc_array_ptr_is_meta(ptr))
706 goto begin_node;
707
708descend_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 Bueso381f20f2017-06-08 14:47:34 +0100715 ptr = READ_ONCE(shortcut->next_node);
David Howellsb2a4df22013-09-24 10:35:18 +0100716 BUG_ON(!assoc_array_ptr_is_node(ptr));
David Howellsb2a4df22013-09-24 10:35:18 +0100717 }
David Howells9c5e45d2013-12-02 11:24:19 +0000718 node = assoc_array_ptr_to_node(ptr);
David Howellsb2a4df22013-09-24 10:35:18 +0100719
720begin_node:
721 kdebug("begin_node");
David Howellsb2a4df22013-09-24 10:35:18 +0100722 slot = 0;
723ascend_to_node:
724 /* Go through the slots in a node */
725 for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
Davidlohr Bueso381f20f2017-06-08 14:47:34 +0100726 ptr = READ_ONCE(node->slots[slot]);
David Howellsb2a4df22013-09-24 10:35:18 +0100727
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 Howellsf5895942014-03-14 17:44:49 +0000747 ctx->cred, KEY_NEED_SEARCH) < 0)
David Howellsb2a4df22013-09-24 10:35:18 +0100748 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 Bueso381f20f2017-06-08 14:47:34 +0100764 ptr = READ_ONCE(node->back_pointer);
David Howellsb2a4df22013-09-24 10:35:18 +0100765 slot = node->parent_slot;
766
767 if (ptr && assoc_array_ptr_is_shortcut(ptr)) {
768 shortcut = assoc_array_ptr_to_shortcut(ptr);
Davidlohr Bueso381f20f2017-06-08 14:47:34 +0100769 ptr = READ_ONCE(shortcut->back_pointer);
David Howellsb2a4df22013-09-24 10:35:18 +0100770 slot = shortcut->parent_slot;
771 }
772 if (!ptr)
773 goto not_this_keyring;
774 node = assoc_array_ptr_to_node(ptr);
David Howellsb2a4df22013-09-24 10:35:18 +0100775 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 */
789not_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 */
805found:
806 key = key_ref_to_ptr(ctx->result);
807 key_check(key);
808 if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) {
Baolin Wang074d5892017-11-15 16:38:45 +0000809 key->last_used_at = ctx->now;
810 keyring->last_used_at = ctx->now;
David Howellsb2a4df22013-09-24 10:35:18 +0100811 while (sp > 0)
Baolin Wang074d5892017-11-15 16:38:45 +0000812 stack[--sp].keyring->last_used_at = ctx->now;
David Howellsb2a4df22013-09-24 10:35:18 +0100813 }
814 kleave(" = true");
815 return true;
816}
817
David Howells973c9f42011-01-20 16:38:33 +0000818/**
David Howellse59428f2019-06-19 16:10:15 +0100819 * keyring_search_rcu - Search a keyring tree for a matching key under RCU
David Howells973c9f42011-01-20 16:38:33 +0000820 * @keyring_ref: A pointer to the keyring with possession indicator.
David Howells4bdf0bc2013-09-24 10:35:15 +0100821 * @ctx: The keyring search context.
David Howells973c9f42011-01-20 16:38:33 +0000822 *
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 Howellse59428f2019-06-19 16:10:15 +0100831 * 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 Howells973c9f42011-01-20 16:38:33 +0000834 *
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 Howellsb2a4df22013-09-24 10:35:18 +0100841 * RCU can be used to prevent the keyring key lists from disappearing without
842 * the need to take lots of locks.
David Howells973c9f42011-01-20 16:38:33 +0000843 *
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 Torvalds1da177e2005-04-16 15:20:36 -0700851 */
David Howellse59428f2019-06-19 16:10:15 +0100852key_ref_t keyring_search_rcu(key_ref_t keyring_ref,
David Howells4bdf0bc2013-09-24 10:35:15 +0100853 struct keyring_search_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
David Howellsb2a4df22013-09-24 10:35:18 +0100855 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 long err;
David Howellsb2a4df22013-09-24 10:35:18 +0100857
858 ctx->iterator = keyring_search_iterator;
859 ctx->possessed = is_key_possessed(keyring_ref);
860 ctx->result = ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
David Howells664cceb2005-09-28 17:03:15 +0100862 keyring = key_ref_to_ptr(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 key_check(keyring);
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (keyring->type != &key_type_keyring)
David Howellsb2a4df22013-09-24 10:35:18 +0100866 return ERR_PTR(-ENOTDIR);
867
868 if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) {
David Howellsf5895942014-03-14 17:44:49 +0000869 err = key_task_permission(keyring_ref, ctx->cred, KEY_NEED_SEARCH);
David Howellsb2a4df22013-09-24 10:35:18 +0100870 if (err < 0)
871 return ERR_PTR(err);
872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Baolin Wang074d5892017-11-15 16:38:45 +0000874 ctx->now = ktime_get_real_seconds();
David Howellsb2a4df22013-09-24 10:35:18 +0100875 if (search_nested_keyrings(keyring, ctx))
876 __key_get(key_ref_to_ptr(ctx->result));
David Howellsb2a4df22013-09-24 10:35:18 +0100877 return ctx->result;
David Howellsa8b17ed2011-01-20 16:38:27 +0000878}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
David Howells973c9f42011-01-20 16:38:33 +0000880/**
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 Howellsdcf49db2019-06-26 21:02:32 +0100885 * @recurse: True to search the children of @keyring also
David Howells973c9f42011-01-20 16:38:33 +0000886 *
David Howellse59428f2019-06-19 16:10:15 +0100887 * As keyring_search_rcu() above, but using the current task's credentials and
David Howellsb2a4df22013-09-24 10:35:18 +0100888 * type's default matching function and preferred search method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 */
David Howells664cceb2005-09-28 17:03:15 +0100890key_ref_t keyring_search(key_ref_t keyring,
891 struct key_type *type,
David Howellsdcf49db2019-06-26 21:02:32 +0100892 const char *description,
893 bool recurse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
David Howells4bdf0bc2013-09-24 10:35:15 +0100895 struct keyring_search_context ctx = {
896 .index_key.type = type,
897 .index_key.description = description,
Eric Biggersede0fa982019-02-22 15:36:18 +0000898 .index_key.desc_len = strlen(description),
David Howells4bdf0bc2013-09-24 10:35:15 +0100899 .cred = current_cred(),
David Howellsc06cfb02014-09-16 17:36:06 +0100900 .match_data.cmp = key_default_cmp,
David Howells46291952014-09-16 17:36:02 +0100901 .match_data.raw_data = description,
902 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
903 .flags = KEYRING_SEARCH_DO_STATE_CHECK,
David Howells4bdf0bc2013-09-24 10:35:15 +0100904 };
David Howells46291952014-09-16 17:36:02 +0100905 key_ref_t key;
906 int ret;
David Howells4bdf0bc2013-09-24 10:35:15 +0100907
David Howellsdcf49db2019-06-26 21:02:32 +0100908 if (recurse)
909 ctx.flags |= KEYRING_SEARCH_RECURSE;
David Howells46291952014-09-16 17:36:02 +0100910 if (type->match_preparse) {
911 ret = type->match_preparse(&ctx.match_data);
912 if (ret < 0)
913 return ERR_PTR(ret);
914 }
915
David Howellse59428f2019-06-19 16:10:15 +0100916 rcu_read_lock();
917 key = keyring_search_rcu(keyring, &ctx);
918 rcu_read_unlock();
David Howells46291952014-09-16 17:36:02 +0100919
920 if (type->match_free)
921 type->match_free(&ctx.match_data);
922 return key;
David Howellsa8b17ed2011-01-20 16:38:27 +0000923}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924EXPORT_SYMBOL(keyring_search);
925
Mat Martineau6563c912017-03-01 16:44:09 -0800926static 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 */
944static 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 */
950static 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 Howells9fd16532019-05-22 13:30:56 +0100966 * @keyring_ref: The keyring to be restricted
967 * @type: The key type that will provide the restriction checker.
Mat Martineau6563c912017-03-01 16:44:09 -0800968 * @restriction: The restriction options to apply to the keyring
David Howells9fd16532019-05-22 13:30:56 +0100969 *
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 Martineau6563c912017-03-01 16:44:09 -0800973 */
974int 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
1027error:
1028 if (restrict_type)
1029 key_type_put(restrict_type);
1030
1031 return ret;
1032}
1033EXPORT_SYMBOL(keyring_restrict);
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/*
David Howellsb2a4df22013-09-24 10:35:18 +01001036 * Search the given keyring for a key that might be updated.
David Howells973c9f42011-01-20 16:38:33 +00001037 *
1038 * The caller must guarantee that the keyring is a keyring and that the
David Howellsb2a4df22013-09-24 10:35:18 +01001039 * 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 Howells973c9f42011-01-20 16:38:33 +00001041 *
1042 * Returns a pointer to the found key with usage count incremented if
David Howellsb2a4df22013-09-24 10:35:18 +01001043 * successful and returns NULL if not found. Revoked and invalidated keys are
1044 * skipped over.
David Howells973c9f42011-01-20 16:38:33 +00001045 *
1046 * If successful, the possession indicator is propagated from the keyring ref
1047 * to the returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
David Howellsb2a4df22013-09-24 10:35:18 +01001049key_ref_t find_key_to_update(key_ref_t keyring_ref,
1050 const struct keyring_index_key *index_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
David Howells664cceb2005-09-28 17:03:15 +01001052 struct key *keyring, *key;
David Howellsb2a4df22013-09-24 10:35:18 +01001053 const void *object;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
David Howells664cceb2005-09-28 17:03:15 +01001055 keyring = key_ref_to_ptr(keyring_ref);
David Howells664cceb2005-09-28 17:03:15 +01001056
David Howellsb2a4df22013-09-24 10:35:18 +01001057 kenter("{%d},{%s,%s}",
1058 keyring->serial, index_key->type->name, index_key->description);
David Howells76d8aea2005-06-23 22:00:49 -07001059
David Howellsb2a4df22013-09-24 10:35:18 +01001060 object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
1061 index_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
David Howellsb2a4df22013-09-24 10:35:18 +01001063 if (object)
1064 goto found;
1065
1066 kleave(" = NULL");
1067 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Justin P. Mattockc5b60b52010-04-21 00:02:11 -07001069found:
David Howellsb2a4df22013-09-24 10:35:18 +01001070 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 Howellsccc3e6d2013-09-24 10:35:16 +01001076 __key_get(key);
David Howellsb2a4df22013-09-24 10:35:18 +01001077 kleave(" = {%d}", key->serial);
1078 return make_key_ref(key, is_key_possessed(keyring_ref));
David Howellsa8b17ed2011-01-20 16:38:27 +00001079}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081/*
David Howells973c9f42011-01-20 16:38:33 +00001082 * Find a keyring with the specified name.
1083 *
Eric Biggers237bbd22017-09-18 11:37:03 -07001084 * 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 Howells973c9f42011-01-20 16:38:33 +00001088 *
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 Torvalds1da177e2005-04-16 15:20:36 -07001091 */
Eric Biggers237bbd22017-09-18 11:37:03 -07001092struct key *find_keyring_by_name(const char *name, bool uid_keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
David Howellsb206f282019-06-26 21:02:32 +01001094 struct user_namespace *ns = current_user_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (!name)
Toshiyuki Okajimacea7daa2010-04-30 14:32:13 +01001098 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 read_lock(&keyring_name_lock);
1101
David Howellsb206f282019-06-26 21:02:32 +01001102 /* 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. Hallyn2ea190d2009-02-26 18:27:55 -06001108
David Howellsb206f282019-06-26 21:02:32 +01001109 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1110 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
David Howellsb206f282019-06-26 21:02:32 +01001112 if (strcmp(keyring->description, name) != 0)
1113 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
David Howellsb206f282019-06-26 21:02:32 +01001115 if (uid_keyring) {
1116 if (!test_bit(KEY_FLAG_UID_KEYRING,
1117 &keyring->flags))
Toshiyuki Okajimacea7daa2010-04-30 14:32:13 +01001118 continue;
David Howellsb206f282019-06-26 21:02:32 +01001119 } else {
1120 if (key_permission(make_key_ref(keyring, 0),
1121 KEY_NEED_SEARCH) < 0)
1122 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
David Howellsb206f282019-06-26 21:02:32 +01001124
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 Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 keyring = ERR_PTR(-ENOKEY);
Toshiyuki Okajimacea7daa2010-04-30 14:32:13 +01001135out:
1136 read_unlock(&keyring_name_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return keyring;
David Howellsa8b17ed2011-01-20 16:38:27 +00001138}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
David Howellsb2a4df22013-09-24 10:35:18 +01001140static 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 Howells979e0d72014-03-09 08:21:58 +00001148 /* We might get a keyring with matching index-key that is nonetheless a
1149 * different keyring. */
David Howells46291952014-09-16 17:36:02 +01001150 if (key != ctx->match_data.raw_data)
David Howells979e0d72014-03-09 08:21:58 +00001151 return 0;
1152
David Howellsb2a4df22013-09-24 10:35:18 +01001153 ctx->result = ERR_PTR(-EDEADLK);
1154 return 1;
1155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157/*
David Howells973c9f42011-01-20 16:38:33 +00001158 * 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 Torvalds1da177e2005-04-16 15:20:36 -07001163 */
1164static int keyring_detect_cycle(struct key *A, struct key *B)
1165{
David Howellsb2a4df22013-09-24 10:35:18 +01001166 struct keyring_search_context ctx = {
David Howells46291952014-09-16 17:36:02 +01001167 .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 Howellsdcf49db2019-06-26 21:02:32 +01001174 KEYRING_SEARCH_DETECT_TOO_DEEP |
1175 KEYRING_SEARCH_RECURSE),
David Howellsb2a4df22013-09-24 10:35:18 +01001176 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
David Howells76d8aea2005-06-23 22:00:49 -07001178 rcu_read_lock();
David Howellsb2a4df22013-09-24 10:35:18 +01001179 search_nested_keyrings(B, &ctx);
David Howells76d8aea2005-06-23 22:00:49 -07001180 rcu_read_unlock();
David Howellsb2a4df22013-09-24 10:35:18 +01001181 return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result);
David Howellsf70e2e02010-04-30 14:32:39 +01001182}
David Howellscab8eb52006-01-08 01:02:45 -08001183
David Howellscab8eb52006-01-08 01:02:45 -08001184/*
David Howellsdf593ee2019-05-30 11:37:39 +01001185 * Lock keyring for link.
1186 */
1187int __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 Howellsed0ac5c2019-05-20 21:51:50 +01001207 * Lock keyrings for move (link/unlink combination).
1208 */
1209int __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 Howells973c9f42011-01-20 16:38:33 +00001241 * Preallocate memory so that a key can be linked into to a keyring.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
David Howellsb2a4df22013-09-24 10:35:18 +01001243int __key_link_begin(struct key *keyring,
1244 const struct keyring_index_key *index_key,
1245 struct assoc_array_edit **_edit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
David Howellsb2a4df22013-09-24 10:35:18 +01001247 struct assoc_array_edit *edit;
1248 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
David Howells16feef42013-09-24 10:35:15 +01001250 kenter("%d,%s,%s,",
David Howellsb2a4df22013-09-24 10:35:18 +01001251 keyring->serial, index_key->type->name, index_key->description);
1252
1253 BUG_ON(index_key->desc_len == 0);
David Howellsdf593ee2019-05-30 11:37:39 +01001254 BUG_ON(*_edit != NULL);
David Howellsf70e2e02010-04-30 14:32:39 +01001255
David Howellsdf593ee2019-05-30 11:37:39 +01001256 *_edit = NULL;
David Howellsf70e2e02010-04-30 14:32:39 +01001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 ret = -EKEYREVOKED;
David Howells76d8aea2005-06-23 22:00:49 -07001259 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
David Howellsdf593ee2019-05-30 11:37:39 +01001260 goto error;
David Howells553d6032010-04-30 14:32:28 +01001261
David Howellsb2a4df22013-09-24 10:35:18 +01001262 /* 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 Howellsdf593ee2019-05-30 11:37:39 +01001271 goto error;
David Howells034faeb2013-10-30 11:15:24 +00001272 }
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 Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
David Howellsb2a4df22013-09-24 10:35:18 +01001284 *_edit = edit;
David Howellsf70e2e02010-04-30 14:32:39 +01001285 kleave(" = 0");
1286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
David Howells034faeb2013-10-30 11:15:24 +00001288error_cancel:
1289 assoc_array_cancel_edit(edit);
David Howellsdf593ee2019-05-30 11:37:39 +01001290error:
David Howellsf70e2e02010-04-30 14:32:39 +01001291 kleave(" = %d", ret);
1292 return ret;
1293}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
David Howellsf70e2e02010-04-30 14:32:39 +01001295/*
David Howells973c9f42011-01-20 16:38:33 +00001296 * 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 Howellsf70e2e02010-04-30 14:32:39 +01001300 */
1301int __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 Torvalds1da177e2005-04-16 15:20:36 -07001309
David Howellsf70e2e02010-04-30 14:32:39 +01001310/*
David Howells973c9f42011-01-20 16:38:33 +00001311 * 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 Howellsf70e2e02010-04-30 14:32:39 +01001317 */
David Howellsb2a4df22013-09-24 10:35:18 +01001318void __key_link(struct key *key, struct assoc_array_edit **_edit)
David Howellsf70e2e02010-04-30 14:32:39 +01001319{
David Howellsccc3e6d2013-09-24 10:35:16 +01001320 __key_get(key);
David Howellsb2a4df22013-09-24 10:35:18 +01001321 assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key));
1322 assoc_array_apply_edit(*_edit);
1323 *_edit = NULL;
David Howellsf70e2e02010-04-30 14:32:39 +01001324}
1325
1326/*
David Howells973c9f42011-01-20 16:38:33 +00001327 * Finish linking a key into to a keyring.
1328 *
1329 * Must be called with __key_link_begin() having being called.
David Howellsf70e2e02010-04-30 14:32:39 +01001330 */
David Howells16feef42013-09-24 10:35:15 +01001331void __key_link_end(struct key *keyring,
1332 const struct keyring_index_key *index_key,
David Howellsb2a4df22013-09-24 10:35:18 +01001333 struct assoc_array_edit *edit)
David Howellsf70e2e02010-04-30 14:32:39 +01001334 __releases(&keyring->sem)
David Howells3be59f72019-05-30 11:40:24 +01001335 __releases(&keyring_serialise_link_lock)
David Howellsf70e2e02010-04-30 14:32:39 +01001336{
David Howells16feef42013-09-24 10:35:15 +01001337 BUG_ON(index_key->type == NULL);
David Howellsb2a4df22013-09-24 10:35:18 +01001338 kenter("%d,%s,", keyring->serial, index_key->type->name);
David Howellsf70e2e02010-04-30 14:32:39 +01001339
Colin Ian Kingca4da5d2015-07-27 15:23:43 +01001340 if (edit) {
1341 if (!edit->dead_leaf) {
1342 key_payload_reserve(keyring,
1343 keyring->datalen - KEYQUOTA_LINK_BYTES);
1344 }
David Howellsb2a4df22013-09-24 10:35:18 +01001345 assoc_array_cancel_edit(edit);
David Howellsf70e2e02010-04-30 14:32:39 +01001346 }
1347 up_write(&keyring->sem);
David Howellsdf593ee2019-05-30 11:37:39 +01001348
1349 if (index_key->type == &key_type_keyring)
1350 mutex_unlock(&keyring_serialise_link_lock);
David Howellsf70e2e02010-04-30 14:32:39 +01001351}
1352
David Howells5ac7eac2016-04-06 16:14:24 +01001353/*
1354 * Check addition of keys to restricted keyrings.
1355 */
1356static int __key_link_check_restriction(struct key *keyring, struct key *key)
1357{
Mat Martineau2b6aa412016-08-31 16:05:43 -07001358 if (!keyring->restrict_link || !keyring->restrict_link->check)
David Howells5ac7eac2016-04-06 16:14:24 +01001359 return 0;
Mat Martineau2b6aa412016-08-31 16:05:43 -07001360 return keyring->restrict_link->check(keyring, key->type, &key->payload,
1361 keyring->restrict_link->key);
David Howells5ac7eac2016-04-06 16:14:24 +01001362}
1363
David Howells973c9f42011-01-20 16:38:33 +00001364/**
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 Torvalds1da177e2005-04-16 15:20:36 -07001383 */
1384int key_link(struct key *keyring, struct key *key)
1385{
David Howellsdf593ee2019-05-30 11:37:39 +01001386 struct assoc_array_edit *edit = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 int ret;
1388
Elena Reshetovafff29292017-03-31 15:20:48 +03001389 kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage));
David Howellsb2a4df22013-09-24 10:35:18 +01001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 key_check(keyring);
1392 key_check(key);
1393
David Howellsdf593ee2019-05-30 11:37:39 +01001394 ret = __key_link_lock(keyring, &key->index_key);
1395 if (ret < 0)
1396 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
David Howellsdf593ee2019-05-30 11:37:39 +01001398 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
1409error_end:
1410 __key_link_end(keyring, &key->index_key, edit);
1411error:
Elena Reshetovafff29292017-03-31 15:20:48 +03001412 kleave(" = %d {%d,%d}", ret, keyring->serial, refcount_read(&keyring->usage));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return ret;
David Howellsf70e2e02010-04-30 14:32:39 +01001414}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415EXPORT_SYMBOL(key_link);
1416
David Howellseb0f68c2019-05-30 14:19:20 +01001417/*
1418 * Lock a keyring for unlink.
1419 */
1420static 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 */
1433static 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 */
1455static 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 */
1466static 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 Howells973c9f42011-01-20 16:38:33 +00001476/**
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 Torvalds1da177e2005-04-16 15:20:36 -07001492 */
1493int key_unlink(struct key *keyring, struct key *key)
1494{
David Howellseb0f68c2019-05-30 14:19:20 +01001495 struct assoc_array_edit *edit = NULL;
David Howellsb2a4df22013-09-24 10:35:18 +01001496 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 key_check(keyring);
1499 key_check(key);
1500
David Howellseb0f68c2019-05-30 14:19:20 +01001501 ret = __key_unlink_lock(keyring);
1502 if (ret < 0)
1503 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
David Howellseb0f68c2019-05-30 14:19:20 +01001505 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 Howellsb2a4df22013-09-24 10:35:18 +01001509 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001510}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511EXPORT_SYMBOL(key_unlink);
1512
David Howells973c9f42011-01-20 16:38:33 +00001513/**
David Howellsed0ac5c2019-05-20 21:51:50 +01001514 * 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 */
1538int 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);
1578error:
1579 __key_link_end(to_keyring, &key->index_key, to_edit);
1580 __key_unlink_end(from_keyring, key, from_edit);
1581out:
1582 kleave(" = %d", ret);
1583 return ret;
1584}
1585EXPORT_SYMBOL(key_move);
1586
1587/**
David Howells973c9f42011-01-20 16:38:33 +00001588 * 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 Torvalds1da177e2005-04-16 15:20:36 -07001594 */
1595int keyring_clear(struct key *keyring)
1596{
David Howellsb2a4df22013-09-24 10:35:18 +01001597 struct assoc_array_edit *edit;
David Howells76d8aea2005-06-23 22:00:49 -07001598 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
David Howellsb2a4df22013-09-24 10:35:18 +01001600 if (keyring->type != &key_type_keyring)
1601 return -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
David Howellsb2a4df22013-09-24 10:35:18 +01001603 down_write(&keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
David Howellsb2a4df22013-09-24 10:35:18 +01001605 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 Torvalds1da177e2005-04-16 15:20:36 -07001612 ret = 0;
1613 }
1614
David Howellsb2a4df22013-09-24 10:35:18 +01001615 up_write(&keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001617}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618EXPORT_SYMBOL(keyring_clear);
David Howells31204ed2006-06-26 00:24:51 -07001619
David Howells31204ed2006-06-26 00:24:51 -07001620/*
David Howells973c9f42011-01-20 16:38:33 +00001621 * Dispose of the links from a revoked keyring.
1622 *
1623 * This is called with the key sem write-locked.
David Howells31204ed2006-06-26 00:24:51 -07001624 */
1625static void keyring_revoke(struct key *keyring)
1626{
David Howellsb2a4df22013-09-24 10:35:18 +01001627 struct assoc_array_edit *edit;
David Howellsf0641cb2010-04-30 14:32:18 +01001628
David Howellsb2a4df22013-09-24 10:35:18 +01001629 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 Howells31204ed2006-06-26 00:24:51 -07001634 }
David Howellsa8b17ed2011-01-20 16:38:27 +00001635}
David Howells5d135442009-09-02 09:14:00 +01001636
David Howells62fe3182013-11-14 13:02:31 +00001637static bool keyring_gc_select_iterator(void *object, void *iterator_data)
David Howellsb2a4df22013-09-24 10:35:18 +01001638{
1639 struct key *key = keyring_ptr_to_key(object);
Baolin Wang074d5892017-11-15 16:38:45 +00001640 time64_t *limit = iterator_data;
David Howellsb2a4df22013-09-24 10:35:18 +01001641
1642 if (key_is_dead(key, *limit))
1643 return false;
1644 key_get(key);
1645 return true;
1646}
1647
David Howells62fe3182013-11-14 13:02:31 +00001648static int keyring_gc_check_iterator(const void *object, void *iterator_data)
1649{
1650 const struct key *key = keyring_ptr_to_key(object);
Baolin Wang074d5892017-11-15 16:38:45 +00001651 time64_t *limit = iterator_data;
David Howells62fe3182013-11-14 13:02:31 +00001652
1653 key_check(key);
1654 return key_is_dead(key, *limit);
1655}
1656
David Howells5d135442009-09-02 09:14:00 +01001657/*
David Howells62fe3182013-11-14 13:02:31 +00001658 * Garbage collect pointers from a keyring.
David Howells973c9f42011-01-20 16:38:33 +00001659 *
David Howells62fe3182013-11-14 13:02:31 +00001660 * 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 Howells5d135442009-09-02 09:14:00 +01001662 */
Baolin Wang074d5892017-11-15 16:38:45 +00001663void keyring_gc(struct key *keyring, time64_t limit)
David Howells5d135442009-09-02 09:14:00 +01001664{
David Howells62fe3182013-11-14 13:02:31 +00001665 int result;
David Howells5d135442009-09-02 09:14:00 +01001666
David Howells62fe3182013-11-14 13:02:31 +00001667 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
1681dont_gc:
1682 kleave(" [no gc]");
1683 return;
1684
1685do_gc:
David Howells5d135442009-09-02 09:14:00 +01001686 down_write(&keyring->sem);
David Howellsb2a4df22013-09-24 10:35:18 +01001687 assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops,
David Howells62fe3182013-11-14 13:02:31 +00001688 keyring_gc_select_iterator, &limit);
David Howells5d135442009-09-02 09:14:00 +01001689 up_write(&keyring->sem);
David Howells62fe3182013-11-14 13:02:31 +00001690 kleave(" [gc]");
David Howells5d135442009-09-02 09:14:00 +01001691}
Mat Martineau2b6aa412016-08-31 16:05:43 -07001692
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 */
1708void 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}