xref: /openbmc/linux/security/keys/keyring.c (revision 3b6e4de05e9ee2e2f94e4a3fe14d945e2418d9a8)
169664cf1SDavid Howells /* Keyring handling
21da177e4SLinus Torvalds  *
3b2a4df20SDavid Howells  * Copyright (C) 2004-2005, 2008, 2013 Red Hat, Inc. All Rights Reserved.
41da177e4SLinus Torvalds  * Written by David Howells (dhowells@redhat.com)
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or
71da177e4SLinus Torvalds  * modify it under the terms of the GNU General Public License
81da177e4SLinus Torvalds  * as published by the Free Software Foundation; either version
91da177e4SLinus Torvalds  * 2 of the License, or (at your option) any later version.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
12876979c9SPaul Gortmaker #include <linux/export.h>
131da177e4SLinus Torvalds #include <linux/init.h>
141da177e4SLinus Torvalds #include <linux/sched.h>
151da177e4SLinus Torvalds #include <linux/slab.h>
1629db9190SDavid Howells #include <linux/security.h>
171da177e4SLinus Torvalds #include <linux/seq_file.h>
181da177e4SLinus Torvalds #include <linux/err.h>
19b206f281SDavid Howells #include <linux/user_namespace.h>
20e9e349b0SDavid Howells #include <keys/keyring-type.h>
21b2a4df20SDavid Howells #include <keys/user-type.h>
22b2a4df20SDavid Howells #include <linux/assoc_array_priv.h>
23512ea3bcSChihau Chau #include <linux/uaccess.h>
241da177e4SLinus Torvalds #include "internal.h"
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds /*
27973c9f4fSDavid Howells  * When plumbing the depths of the key tree, this sets a hard limit
28973c9f4fSDavid Howells  * set on how deep we're willing to go.
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds #define KEYRING_SEARCH_MAX_DEPTH 6
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds /*
33b2a4df20SDavid Howells  * We mark pointers we pass to the associative array with bit 1 set if
34b2a4df20SDavid Howells  * they're keyrings and clear otherwise.
35b2a4df20SDavid Howells  */
36b2a4df20SDavid Howells #define KEYRING_PTR_SUBTYPE	0x2UL
37b2a4df20SDavid Howells 
38b2a4df20SDavid Howells static inline bool keyring_ptr_is_keyring(const struct assoc_array_ptr *x)
39b2a4df20SDavid Howells {
40b2a4df20SDavid Howells 	return (unsigned long)x & KEYRING_PTR_SUBTYPE;
41b2a4df20SDavid Howells }
42b2a4df20SDavid Howells static inline struct key *keyring_ptr_to_key(const struct assoc_array_ptr *x)
43b2a4df20SDavid Howells {
44b2a4df20SDavid Howells 	void *object = assoc_array_ptr_to_leaf(x);
45b2a4df20SDavid Howells 	return (struct key *)((unsigned long)object & ~KEYRING_PTR_SUBTYPE);
46b2a4df20SDavid Howells }
47b2a4df20SDavid Howells static inline void *keyring_key_to_ptr(struct key *key)
48b2a4df20SDavid Howells {
49b2a4df20SDavid Howells 	if (key->type == &key_type_keyring)
50b2a4df20SDavid Howells 		return (void *)((unsigned long)key | KEYRING_PTR_SUBTYPE);
51b2a4df20SDavid Howells 	return key;
52b2a4df20SDavid Howells }
53b2a4df20SDavid Howells 
541da177e4SLinus Torvalds static DEFINE_RWLOCK(keyring_name_lock);
551da177e4SLinus Torvalds 
56b206f281SDavid Howells /*
57b206f281SDavid Howells  * Clean up the bits of user_namespace that belong to us.
58b206f281SDavid Howells  */
59b206f281SDavid Howells void key_free_user_ns(struct user_namespace *ns)
601da177e4SLinus Torvalds {
61b206f281SDavid Howells 	write_lock(&keyring_name_lock);
62b206f281SDavid Howells 	list_del_init(&ns->keyring_name_list);
63b206f281SDavid Howells 	write_unlock(&keyring_name_lock);
641da177e4SLinus Torvalds 
650f44e4d9SDavid Howells 	key_put(ns->user_keyring_register);
66b206f281SDavid Howells #ifdef CONFIG_PERSISTENT_KEYRINGS
67b206f281SDavid Howells 	key_put(ns->persistent_keyring_register);
68b206f281SDavid Howells #endif
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /*
72973c9f4fSDavid Howells  * The keyring key type definition.  Keyrings are simply keys of this type and
73973c9f4fSDavid Howells  * can be treated as ordinary keys in addition to having their own special
74973c9f4fSDavid Howells  * operations.
751da177e4SLinus Torvalds  */
765d19e20bSDavid Howells static int keyring_preparse(struct key_preparsed_payload *prep);
775d19e20bSDavid Howells static void keyring_free_preparse(struct key_preparsed_payload *prep);
781da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
79cf7f601cSDavid Howells 			       struct key_preparsed_payload *prep);
8031204ed9SDavid Howells static void keyring_revoke(struct key *keyring);
811da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring);
821da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m);
831da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
841da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen);
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds struct key_type key_type_keyring = {
871da177e4SLinus Torvalds 	.name		= "keyring",
88b2a4df20SDavid Howells 	.def_datalen	= 0,
895d19e20bSDavid Howells 	.preparse	= keyring_preparse,
905d19e20bSDavid Howells 	.free_preparse	= keyring_free_preparse,
911da177e4SLinus Torvalds 	.instantiate	= keyring_instantiate,
9231204ed9SDavid Howells 	.revoke		= keyring_revoke,
931da177e4SLinus Torvalds 	.destroy	= keyring_destroy,
941da177e4SLinus Torvalds 	.describe	= keyring_describe,
951da177e4SLinus Torvalds 	.read		= keyring_read,
961da177e4SLinus Torvalds };
977318226eSDavid Howells EXPORT_SYMBOL(key_type_keyring);
987318226eSDavid Howells 
991da177e4SLinus Torvalds /*
100973c9f4fSDavid Howells  * Semaphore to serialise link/link calls to prevent two link calls in parallel
101973c9f4fSDavid Howells  * introducing a cycle.
1021da177e4SLinus Torvalds  */
1033be59f74SDavid Howells static DEFINE_MUTEX(keyring_serialise_link_lock);
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /*
106973c9f4fSDavid Howells  * Publish the name of a keyring so that it can be found by name (if it has
107b206f281SDavid Howells  * one and it doesn't begin with a dot).
1081da177e4SLinus Torvalds  */
10969664cf1SDavid Howells static void keyring_publish_name(struct key *keyring)
1101da177e4SLinus Torvalds {
111b206f281SDavid Howells 	struct user_namespace *ns = current_user_ns();
1121da177e4SLinus Torvalds 
113b206f281SDavid Howells 	if (keyring->description &&
114b206f281SDavid Howells 	    keyring->description[0] &&
115b206f281SDavid Howells 	    keyring->description[0] != '.') {
1161da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
117b206f281SDavid Howells 		list_add_tail(&keyring->name_link, &ns->keyring_name_list);
1181da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
1191da177e4SLinus Torvalds 	}
120a8b17ed0SDavid Howells }
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /*
1235d19e20bSDavid Howells  * Preparse a keyring payload
1245d19e20bSDavid Howells  */
1255d19e20bSDavid Howells static int keyring_preparse(struct key_preparsed_payload *prep)
1265d19e20bSDavid Howells {
1275d19e20bSDavid Howells 	return prep->datalen != 0 ? -EINVAL : 0;
1285d19e20bSDavid Howells }
1295d19e20bSDavid Howells 
1305d19e20bSDavid Howells /*
1315d19e20bSDavid Howells  * Free a preparse of a user defined key payload
1325d19e20bSDavid Howells  */
1335d19e20bSDavid Howells static void keyring_free_preparse(struct key_preparsed_payload *prep)
1345d19e20bSDavid Howells {
1355d19e20bSDavid Howells }
1365d19e20bSDavid Howells 
1375d19e20bSDavid Howells /*
138973c9f4fSDavid Howells  * Initialise a keyring.
139973c9f4fSDavid Howells  *
140973c9f4fSDavid Howells  * Returns 0 on success, -EINVAL if given any data.
1411da177e4SLinus Torvalds  */
1421da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
143cf7f601cSDavid Howells 			       struct key_preparsed_payload *prep)
1441da177e4SLinus Torvalds {
145b2a4df20SDavid Howells 	assoc_array_init(&keyring->keys);
1461da177e4SLinus Torvalds 	/* make the keyring available by name if it has one */
1471da177e4SLinus Torvalds 	keyring_publish_name(keyring);
1485d19e20bSDavid Howells 	return 0;
149a8b17ed0SDavid Howells }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds /*
152b2a4df20SDavid Howells  * Multiply 64-bits by 32-bits to 96-bits and fold back to 64-bit.  Ideally we'd
153b2a4df20SDavid Howells  * fold the carry back too, but that requires inline asm.
1541da177e4SLinus Torvalds  */
155b2a4df20SDavid Howells static u64 mult_64x32_and_fold(u64 x, u32 y)
1561da177e4SLinus Torvalds {
157b2a4df20SDavid Howells 	u64 hi = (u64)(u32)(x >> 32) * y;
158b2a4df20SDavid Howells 	u64 lo = (u64)(u32)(x) * y;
159b2a4df20SDavid Howells 	return lo + ((u64)(u32)hi << 32) + (u32)(hi >> 32);
160a8b17ed0SDavid Howells }
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds /*
163b2a4df20SDavid Howells  * Hash a key type and description.
164b2a4df20SDavid Howells  */
165355ef8e1SDavid Howells static void hash_key_type_and_desc(struct keyring_index_key *index_key)
166b2a4df20SDavid Howells {
167b2a4df20SDavid Howells 	const unsigned level_shift = ASSOC_ARRAY_LEVEL_STEP;
168d54e58b7SDavid Howells 	const unsigned long fan_mask = ASSOC_ARRAY_FAN_MASK;
169b2a4df20SDavid Howells 	const char *description = index_key->description;
170b2a4df20SDavid Howells 	unsigned long hash, type;
171b2a4df20SDavid Howells 	u32 piece;
172b2a4df20SDavid Howells 	u64 acc;
173b2a4df20SDavid Howells 	int n, desc_len = index_key->desc_len;
174b2a4df20SDavid Howells 
175b2a4df20SDavid Howells 	type = (unsigned long)index_key->type;
176b2a4df20SDavid Howells 	acc = mult_64x32_and_fold(type, desc_len + 13);
177b2a4df20SDavid Howells 	acc = mult_64x32_and_fold(acc, 9207);
178*3b6e4de0SDavid Howells 	piece = (unsigned long)index_key->domain_tag;
179*3b6e4de0SDavid Howells 	acc = mult_64x32_and_fold(acc, piece);
180*3b6e4de0SDavid Howells 	acc = mult_64x32_and_fold(acc, 9207);
181f771fde8SDavid Howells 
182b2a4df20SDavid Howells 	for (;;) {
183b2a4df20SDavid Howells 		n = desc_len;
184b2a4df20SDavid Howells 		if (n <= 0)
185b2a4df20SDavid Howells 			break;
186b2a4df20SDavid Howells 		if (n > 4)
187b2a4df20SDavid Howells 			n = 4;
188b2a4df20SDavid Howells 		piece = 0;
189b2a4df20SDavid Howells 		memcpy(&piece, description, n);
190b2a4df20SDavid Howells 		description += n;
191b2a4df20SDavid Howells 		desc_len -= n;
192b2a4df20SDavid Howells 		acc = mult_64x32_and_fold(acc, piece);
193b2a4df20SDavid Howells 		acc = mult_64x32_and_fold(acc, 9207);
194b2a4df20SDavid Howells 	}
195b2a4df20SDavid Howells 
196b2a4df20SDavid Howells 	/* Fold the hash down to 32 bits if need be. */
197b2a4df20SDavid Howells 	hash = acc;
198b2a4df20SDavid Howells 	if (ASSOC_ARRAY_KEY_CHUNK_SIZE == 32)
199b2a4df20SDavid Howells 		hash ^= acc >> 32;
200b2a4df20SDavid Howells 
201b2a4df20SDavid Howells 	/* Squidge all the keyrings into a separate part of the tree to
202b2a4df20SDavid Howells 	 * ordinary keys by making sure the lowest level segment in the hash is
203b2a4df20SDavid Howells 	 * zero for keyrings and non-zero otherwise.
204b2a4df20SDavid Howells 	 */
205d54e58b7SDavid Howells 	if (index_key->type != &key_type_keyring && (hash & fan_mask) == 0)
206355ef8e1SDavid Howells 		hash |= (hash >> (ASSOC_ARRAY_KEY_CHUNK_SIZE - level_shift)) | 1;
207355ef8e1SDavid Howells 	else if (index_key->type == &key_type_keyring && (hash & fan_mask) != 0)
208355ef8e1SDavid Howells 		hash = (hash + (hash << level_shift)) & ~fan_mask;
209355ef8e1SDavid Howells 	index_key->hash = hash;
210355ef8e1SDavid Howells }
211355ef8e1SDavid Howells 
212355ef8e1SDavid Howells /*
213355ef8e1SDavid Howells  * Finalise an index key to include a part of the description actually in the
214*3b6e4de0SDavid Howells  * index key, to set the domain tag and to calculate the hash.
215355ef8e1SDavid Howells  */
216355ef8e1SDavid Howells void key_set_index_key(struct keyring_index_key *index_key)
217355ef8e1SDavid Howells {
218*3b6e4de0SDavid Howells 	static struct key_tag default_domain_tag = { .usage = REFCOUNT_INIT(1), };
219355ef8e1SDavid Howells 	size_t n = min_t(size_t, index_key->desc_len, sizeof(index_key->desc));
220*3b6e4de0SDavid Howells 
221355ef8e1SDavid Howells 	memcpy(index_key->desc, index_key->description, n);
222355ef8e1SDavid Howells 
223*3b6e4de0SDavid Howells 	index_key->domain_tag = &default_domain_tag;
224355ef8e1SDavid Howells 	hash_key_type_and_desc(index_key);
225b2a4df20SDavid Howells }
226b2a4df20SDavid Howells 
227*3b6e4de0SDavid Howells /**
228*3b6e4de0SDavid Howells  * key_put_tag - Release a ref on a tag.
229*3b6e4de0SDavid Howells  * @tag: The tag to release.
230*3b6e4de0SDavid Howells  *
231*3b6e4de0SDavid Howells  * This releases a reference the given tag and returns true if that ref was the
232*3b6e4de0SDavid Howells  * last one.
233*3b6e4de0SDavid Howells  */
234*3b6e4de0SDavid Howells bool key_put_tag(struct key_tag *tag)
235*3b6e4de0SDavid Howells {
236*3b6e4de0SDavid Howells 	if (refcount_dec_and_test(&tag->usage)) {
237*3b6e4de0SDavid Howells 		kfree_rcu(tag, rcu);
238*3b6e4de0SDavid Howells 		return true;
239*3b6e4de0SDavid Howells 	}
240*3b6e4de0SDavid Howells 
241*3b6e4de0SDavid Howells 	return false;
242*3b6e4de0SDavid Howells }
243*3b6e4de0SDavid Howells 
244b2a4df20SDavid Howells /*
245b2a4df20SDavid Howells  * Build the next index key chunk.
246b2a4df20SDavid Howells  *
247b2a4df20SDavid Howells  * We return it one word-sized chunk at a time.
248b2a4df20SDavid Howells  */
249b2a4df20SDavid Howells static unsigned long keyring_get_key_chunk(const void *data, int level)
250b2a4df20SDavid Howells {
251b2a4df20SDavid Howells 	const struct keyring_index_key *index_key = data;
252b2a4df20SDavid Howells 	unsigned long chunk = 0;
253f771fde8SDavid Howells 	const u8 *d;
254b2a4df20SDavid Howells 	int desc_len = index_key->desc_len, n = sizeof(chunk);
255b2a4df20SDavid Howells 
256b2a4df20SDavid Howells 	level /= ASSOC_ARRAY_KEY_CHUNK_SIZE;
257b2a4df20SDavid Howells 	switch (level) {
258b2a4df20SDavid Howells 	case 0:
259355ef8e1SDavid Howells 		return index_key->hash;
260b2a4df20SDavid Howells 	case 1:
261f771fde8SDavid Howells 		return index_key->x;
262b2a4df20SDavid Howells 	case 2:
263f771fde8SDavid Howells 		return (unsigned long)index_key->type;
264*3b6e4de0SDavid Howells 	case 3:
265*3b6e4de0SDavid Howells 		return (unsigned long)index_key->domain_tag;
266b2a4df20SDavid Howells 	default:
267*3b6e4de0SDavid Howells 		level -= 4;
268f771fde8SDavid Howells 		if (desc_len <= sizeof(index_key->desc))
269b2a4df20SDavid Howells 			return 0;
270f771fde8SDavid Howells 
271f771fde8SDavid Howells 		d = index_key->description + sizeof(index_key->desc);
272f771fde8SDavid Howells 		d += level * sizeof(long);
273f771fde8SDavid Howells 		desc_len -= sizeof(index_key->desc);
274b2a4df20SDavid Howells 		if (desc_len > n)
275b2a4df20SDavid Howells 			desc_len = n;
276b2a4df20SDavid Howells 		do {
277b2a4df20SDavid Howells 			chunk <<= 8;
278f771fde8SDavid Howells 			chunk |= *d++;
279b2a4df20SDavid Howells 		} while (--desc_len > 0);
280b2a4df20SDavid Howells 		return chunk;
281b2a4df20SDavid Howells 	}
282b2a4df20SDavid Howells }
283b2a4df20SDavid Howells 
284b2a4df20SDavid Howells static unsigned long keyring_get_object_key_chunk(const void *object, int level)
285b2a4df20SDavid Howells {
286b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
287b2a4df20SDavid Howells 	return keyring_get_key_chunk(&key->index_key, level);
288b2a4df20SDavid Howells }
289b2a4df20SDavid Howells 
290b2a4df20SDavid Howells static bool keyring_compare_object(const void *object, const void *data)
291b2a4df20SDavid Howells {
292b2a4df20SDavid Howells 	const struct keyring_index_key *index_key = data;
293b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
294b2a4df20SDavid Howells 
295b2a4df20SDavid Howells 	return key->index_key.type == index_key->type &&
296*3b6e4de0SDavid Howells 		key->index_key.domain_tag == index_key->domain_tag &&
297b2a4df20SDavid Howells 		key->index_key.desc_len == index_key->desc_len &&
298b2a4df20SDavid Howells 		memcmp(key->index_key.description, index_key->description,
299b2a4df20SDavid Howells 		       index_key->desc_len) == 0;
300b2a4df20SDavid Howells }
301b2a4df20SDavid Howells 
302b2a4df20SDavid Howells /*
303b2a4df20SDavid Howells  * Compare the index keys of a pair of objects and determine the bit position
304b2a4df20SDavid Howells  * at which they differ - if they differ.
305b2a4df20SDavid Howells  */
30623fd78d7SDavid Howells static int keyring_diff_objects(const void *object, const void *data)
307b2a4df20SDavid Howells {
30823fd78d7SDavid Howells 	const struct key *key_a = keyring_ptr_to_key(object);
309b2a4df20SDavid Howells 	const struct keyring_index_key *a = &key_a->index_key;
31023fd78d7SDavid Howells 	const struct keyring_index_key *b = data;
311b2a4df20SDavid Howells 	unsigned long seg_a, seg_b;
312b2a4df20SDavid Howells 	int level, i;
313b2a4df20SDavid Howells 
314b2a4df20SDavid Howells 	level = 0;
315355ef8e1SDavid Howells 	seg_a = a->hash;
316355ef8e1SDavid Howells 	seg_b = b->hash;
317b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
318b2a4df20SDavid Howells 		goto differ;
319f771fde8SDavid Howells 	level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8;
320b2a4df20SDavid Howells 
321b2a4df20SDavid Howells 	/* The number of bits contributed by the hash is controlled by a
322b2a4df20SDavid Howells 	 * constant in the assoc_array headers.  Everything else thereafter we
323b2a4df20SDavid Howells 	 * can deal with as being machine word-size dependent.
324b2a4df20SDavid Howells 	 */
325f771fde8SDavid Howells 	seg_a = a->x;
326f771fde8SDavid Howells 	seg_b = b->x;
327b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
328b2a4df20SDavid Howells 		goto differ;
329f771fde8SDavid Howells 	level += sizeof(unsigned long);
330b2a4df20SDavid Howells 
331b2a4df20SDavid Howells 	/* The next bit may not work on big endian */
332b2a4df20SDavid Howells 	seg_a = (unsigned long)a->type;
333b2a4df20SDavid Howells 	seg_b = (unsigned long)b->type;
334b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
335b2a4df20SDavid Howells 		goto differ;
336b2a4df20SDavid Howells 	level += sizeof(unsigned long);
337b2a4df20SDavid Howells 
338*3b6e4de0SDavid Howells 	seg_a = (unsigned long)a->domain_tag;
339*3b6e4de0SDavid Howells 	seg_b = (unsigned long)b->domain_tag;
340*3b6e4de0SDavid Howells 	if ((seg_a ^ seg_b) != 0)
341*3b6e4de0SDavid Howells 		goto differ;
342*3b6e4de0SDavid Howells 	level += sizeof(unsigned long);
343*3b6e4de0SDavid Howells 
344f771fde8SDavid Howells 	i = sizeof(a->desc);
345f771fde8SDavid Howells 	if (a->desc_len <= i)
346f771fde8SDavid Howells 		goto same;
347b2a4df20SDavid Howells 
348b2a4df20SDavid Howells 	for (; i < a->desc_len; i++) {
349b2a4df20SDavid Howells 		seg_a = *(unsigned char *)(a->description + i);
350b2a4df20SDavid Howells 		seg_b = *(unsigned char *)(b->description + i);
351b2a4df20SDavid Howells 		if ((seg_a ^ seg_b) != 0)
352b2a4df20SDavid Howells 			goto differ_plus_i;
353b2a4df20SDavid Howells 	}
354b2a4df20SDavid Howells 
355b2a4df20SDavid Howells same:
356b2a4df20SDavid Howells 	return -1;
357b2a4df20SDavid Howells 
358b2a4df20SDavid Howells differ_plus_i:
359b2a4df20SDavid Howells 	level += i;
360b2a4df20SDavid Howells differ:
361b2a4df20SDavid Howells 	i = level * 8 + __ffs(seg_a ^ seg_b);
362b2a4df20SDavid Howells 	return i;
363b2a4df20SDavid Howells }
364b2a4df20SDavid Howells 
365b2a4df20SDavid Howells /*
366b2a4df20SDavid Howells  * Free an object after stripping the keyring flag off of the pointer.
367b2a4df20SDavid Howells  */
368b2a4df20SDavid Howells static void keyring_free_object(void *object)
369b2a4df20SDavid Howells {
370b2a4df20SDavid Howells 	key_put(keyring_ptr_to_key(object));
371b2a4df20SDavid Howells }
372b2a4df20SDavid Howells 
373b2a4df20SDavid Howells /*
374b2a4df20SDavid Howells  * Operations for keyring management by the index-tree routines.
375b2a4df20SDavid Howells  */
376b2a4df20SDavid Howells static const struct assoc_array_ops keyring_assoc_array_ops = {
377b2a4df20SDavid Howells 	.get_key_chunk		= keyring_get_key_chunk,
378b2a4df20SDavid Howells 	.get_object_key_chunk	= keyring_get_object_key_chunk,
379b2a4df20SDavid Howells 	.compare_object		= keyring_compare_object,
380b2a4df20SDavid Howells 	.diff_objects		= keyring_diff_objects,
381b2a4df20SDavid Howells 	.free_object		= keyring_free_object,
382b2a4df20SDavid Howells };
383b2a4df20SDavid Howells 
384b2a4df20SDavid Howells /*
385973c9f4fSDavid Howells  * Clean up a keyring when it is destroyed.  Unpublish its name if it had one
386973c9f4fSDavid Howells  * and dispose of its data.
387233e4735SDavid Howells  *
388233e4735SDavid Howells  * The garbage collector detects the final key_put(), removes the keyring from
389233e4735SDavid Howells  * the serial number tree and then does RCU synchronisation before coming here,
390233e4735SDavid Howells  * so we shouldn't need to worry about code poking around here with the RCU
391233e4735SDavid Howells  * readlock held by this time.
3921da177e4SLinus Torvalds  */
3931da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	if (keyring->description) {
3961da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
39794efe72fSDavid Howells 
398146aa8b1SDavid Howells 		if (keyring->name_link.next != NULL &&
399146aa8b1SDavid Howells 		    !list_empty(&keyring->name_link))
400146aa8b1SDavid Howells 			list_del(&keyring->name_link);
40194efe72fSDavid Howells 
4021da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
4031da177e4SLinus Torvalds 	}
4041da177e4SLinus Torvalds 
4052b6aa412SMat Martineau 	if (keyring->restrict_link) {
4062b6aa412SMat Martineau 		struct key_restriction *keyres = keyring->restrict_link;
4072b6aa412SMat Martineau 
4082b6aa412SMat Martineau 		key_put(keyres->key);
4092b6aa412SMat Martineau 		kfree(keyres);
4102b6aa412SMat Martineau 	}
4112b6aa412SMat Martineau 
412b2a4df20SDavid Howells 	assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops);
413a8b17ed0SDavid Howells }
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds /*
416973c9f4fSDavid Howells  * Describe a keyring for /proc.
4171da177e4SLinus Torvalds  */
4181da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m)
4191da177e4SLinus Torvalds {
420c8563473Swzt.wzt@gmail.com 	if (keyring->description)
4211da177e4SLinus Torvalds 		seq_puts(m, keyring->description);
422c8563473Swzt.wzt@gmail.com 	else
4231da177e4SLinus Torvalds 		seq_puts(m, "[anon]");
4241da177e4SLinus Torvalds 
425363b02daSDavid Howells 	if (key_is_positive(keyring)) {
426b2a4df20SDavid Howells 		if (keyring->keys.nr_leaves_on_tree != 0)
427b2a4df20SDavid Howells 			seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree);
4281da177e4SLinus Torvalds 		else
4291da177e4SLinus Torvalds 			seq_puts(m, ": empty");
430a8b17ed0SDavid Howells 	}
43178b7280cSDavid Howells }
4321da177e4SLinus Torvalds 
433b2a4df20SDavid Howells struct keyring_read_iterator_context {
434e645016aSEric Biggers 	size_t			buflen;
435b2a4df20SDavid Howells 	size_t			count;
436b2a4df20SDavid Howells 	key_serial_t __user	*buffer;
437b2a4df20SDavid Howells };
438b2a4df20SDavid Howells 
439b2a4df20SDavid Howells static int keyring_read_iterator(const void *object, void *data)
440b2a4df20SDavid Howells {
441b2a4df20SDavid Howells 	struct keyring_read_iterator_context *ctx = data;
442b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
443b2a4df20SDavid Howells 	int ret;
444b2a4df20SDavid Howells 
445b2a4df20SDavid Howells 	kenter("{%s,%d},,{%zu/%zu}",
446e645016aSEric Biggers 	       key->type->name, key->serial, ctx->count, ctx->buflen);
447b2a4df20SDavid Howells 
448e645016aSEric Biggers 	if (ctx->count >= ctx->buflen)
449b2a4df20SDavid Howells 		return 1;
450b2a4df20SDavid Howells 
451b2a4df20SDavid Howells 	ret = put_user(key->serial, ctx->buffer);
452b2a4df20SDavid Howells 	if (ret < 0)
453b2a4df20SDavid Howells 		return ret;
454b2a4df20SDavid Howells 	ctx->buffer++;
455b2a4df20SDavid Howells 	ctx->count += sizeof(key->serial);
456b2a4df20SDavid Howells 	return 0;
457b2a4df20SDavid Howells }
458b2a4df20SDavid Howells 
4591da177e4SLinus Torvalds /*
460973c9f4fSDavid Howells  * Read a list of key IDs from the keyring's contents in binary form
461973c9f4fSDavid Howells  *
462b2a4df20SDavid Howells  * The keyring's semaphore is read-locked by the caller.  This prevents someone
463b2a4df20SDavid Howells  * from modifying it under us - which could cause us to read key IDs multiple
464b2a4df20SDavid Howells  * times.
4651da177e4SLinus Torvalds  */
4661da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
4671da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen)
4681da177e4SLinus Torvalds {
469b2a4df20SDavid Howells 	struct keyring_read_iterator_context ctx;
4703239b6f2SEric Biggers 	long ret;
4711da177e4SLinus Torvalds 
472b2a4df20SDavid Howells 	kenter("{%d},,%zu", key_serial(keyring), buflen);
4731da177e4SLinus Torvalds 
474b2a4df20SDavid Howells 	if (buflen & (sizeof(key_serial_t) - 1))
475b2a4df20SDavid Howells 		return -EINVAL;
4761da177e4SLinus Torvalds 
4773239b6f2SEric Biggers 	/* Copy as many key IDs as fit into the buffer */
4783239b6f2SEric Biggers 	if (buffer && buflen) {
479b2a4df20SDavid Howells 		ctx.buffer = (key_serial_t __user *)buffer;
480e645016aSEric Biggers 		ctx.buflen = buflen;
481b2a4df20SDavid Howells 		ctx.count = 0;
4823239b6f2SEric Biggers 		ret = assoc_array_iterate(&keyring->keys,
4833239b6f2SEric Biggers 					  keyring_read_iterator, &ctx);
484b2a4df20SDavid Howells 		if (ret < 0) {
4853239b6f2SEric Biggers 			kleave(" = %ld [iterate]", ret);
4861da177e4SLinus Torvalds 			return ret;
487a8b17ed0SDavid Howells 		}
4883239b6f2SEric Biggers 	}
4891da177e4SLinus Torvalds 
4903239b6f2SEric Biggers 	/* Return the size of the buffer needed */
4913239b6f2SEric Biggers 	ret = keyring->keys.nr_leaves_on_tree * sizeof(key_serial_t);
4923239b6f2SEric Biggers 	if (ret <= buflen)
4933239b6f2SEric Biggers 		kleave("= %ld [ok]", ret);
4943239b6f2SEric Biggers 	else
4953239b6f2SEric Biggers 		kleave("= %ld [buffer too small]", ret);
4963239b6f2SEric Biggers 	return ret;
497b2a4df20SDavid Howells }
498b2a4df20SDavid Howells 
4991da177e4SLinus Torvalds /*
500973c9f4fSDavid Howells  * Allocate a keyring and link into the destination keyring.
5011da177e4SLinus Torvalds  */
5029a56c2dbSEric W. Biederman struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
50396b5c8feSDavid Howells 			  const struct cred *cred, key_perm_t perm,
5045ac7eaceSDavid Howells 			  unsigned long flags,
5052b6aa412SMat Martineau 			  struct key_restriction *restrict_link,
5065ac7eaceSDavid Howells 			  struct key *dest)
5071da177e4SLinus Torvalds {
5081da177e4SLinus Torvalds 	struct key *keyring;
5091da177e4SLinus Torvalds 	int ret;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	keyring = key_alloc(&key_type_keyring, description,
5125ac7eaceSDavid Howells 			    uid, gid, cred, perm, flags, restrict_link);
5131da177e4SLinus Torvalds 	if (!IS_ERR(keyring)) {
5143e30148cSDavid Howells 		ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
5151da177e4SLinus Torvalds 		if (ret < 0) {
5161da177e4SLinus Torvalds 			key_put(keyring);
5171da177e4SLinus Torvalds 			keyring = ERR_PTR(ret);
5181da177e4SLinus Torvalds 		}
5191da177e4SLinus Torvalds 	}
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	return keyring;
522a8b17ed0SDavid Howells }
523f8aa23a5SDavid Howells EXPORT_SYMBOL(keyring_alloc);
5241da177e4SLinus Torvalds 
5255ac7eaceSDavid Howells /**
5265ac7eaceSDavid Howells  * restrict_link_reject - Give -EPERM to restrict link
5275ac7eaceSDavid Howells  * @keyring: The keyring being added to.
5285ac7eaceSDavid Howells  * @type: The type of key being added.
5295ac7eaceSDavid Howells  * @payload: The payload of the key intended to be added.
5309fd16537SDavid Howells  * @restriction_key: Keys providing additional data for evaluating restriction.
5315ac7eaceSDavid Howells  *
5325ac7eaceSDavid Howells  * Reject the addition of any links to a keyring.  It can be overridden by
5335ac7eaceSDavid Howells  * passing KEY_ALLOC_BYPASS_RESTRICTION to key_instantiate_and_link() when
5345ac7eaceSDavid Howells  * adding a key to a keyring.
5355ac7eaceSDavid Howells  *
5362b6aa412SMat Martineau  * This is meant to be stored in a key_restriction structure which is passed
5372b6aa412SMat Martineau  * in the restrict_link parameter to keyring_alloc().
5385ac7eaceSDavid Howells  */
5395ac7eaceSDavid Howells int restrict_link_reject(struct key *keyring,
5405ac7eaceSDavid Howells 			 const struct key_type *type,
541aaf66c88SMat Martineau 			 const union key_payload *payload,
542aaf66c88SMat Martineau 			 struct key *restriction_key)
5435ac7eaceSDavid Howells {
5445ac7eaceSDavid Howells 	return -EPERM;
5455ac7eaceSDavid Howells }
5465ac7eaceSDavid Howells 
547b2a4df20SDavid Howells /*
548c06cfb08SDavid Howells  * By default, we keys found by getting an exact match on their descriptions.
549c06cfb08SDavid Howells  */
5500c903ab6SDavid Howells bool key_default_cmp(const struct key *key,
551c06cfb08SDavid Howells 		     const struct key_match_data *match_data)
552c06cfb08SDavid Howells {
553c06cfb08SDavid Howells 	return strcmp(key->description, match_data->raw_data) == 0;
554c06cfb08SDavid Howells }
555c06cfb08SDavid Howells 
556c06cfb08SDavid Howells /*
557b2a4df20SDavid Howells  * Iteration function to consider each key found.
558b2a4df20SDavid Howells  */
559b2a4df20SDavid Howells static int keyring_search_iterator(const void *object, void *iterator_data)
560b2a4df20SDavid Howells {
561b2a4df20SDavid Howells 	struct keyring_search_context *ctx = iterator_data;
562b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
563363b02daSDavid Howells 	unsigned long kflags = READ_ONCE(key->flags);
564363b02daSDavid Howells 	short state = READ_ONCE(key->state);
565b2a4df20SDavid Howells 
566b2a4df20SDavid Howells 	kenter("{%d}", key->serial);
567b2a4df20SDavid Howells 
568b2a4df20SDavid Howells 	/* ignore keys not of this type */
569b2a4df20SDavid Howells 	if (key->type != ctx->index_key.type) {
570b2a4df20SDavid Howells 		kleave(" = 0 [!type]");
571b2a4df20SDavid Howells 		return 0;
572b2a4df20SDavid Howells 	}
573b2a4df20SDavid Howells 
574b2a4df20SDavid Howells 	/* skip invalidated, revoked and expired keys */
575b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
576074d5898SBaolin Wang 		time64_t expiry = READ_ONCE(key->expiry);
5779d6c8711SEric Biggers 
578b2a4df20SDavid Howells 		if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
579b2a4df20SDavid Howells 			      (1 << KEY_FLAG_REVOKED))) {
580b2a4df20SDavid Howells 			ctx->result = ERR_PTR(-EKEYREVOKED);
581b2a4df20SDavid Howells 			kleave(" = %d [invrev]", ctx->skipped_ret);
582b2a4df20SDavid Howells 			goto skipped;
583b2a4df20SDavid Howells 		}
584b2a4df20SDavid Howells 
585074d5898SBaolin Wang 		if (expiry && ctx->now >= expiry) {
5860b0a8415SDavid Howells 			if (!(ctx->flags & KEYRING_SEARCH_SKIP_EXPIRED))
587b2a4df20SDavid Howells 				ctx->result = ERR_PTR(-EKEYEXPIRED);
588b2a4df20SDavid Howells 			kleave(" = %d [expire]", ctx->skipped_ret);
589b2a4df20SDavid Howells 			goto skipped;
590b2a4df20SDavid Howells 		}
591b2a4df20SDavid Howells 	}
592b2a4df20SDavid Howells 
593b2a4df20SDavid Howells 	/* keys that don't match */
59446291959SDavid Howells 	if (!ctx->match_data.cmp(key, &ctx->match_data)) {
595b2a4df20SDavid Howells 		kleave(" = 0 [!match]");
596b2a4df20SDavid Howells 		return 0;
597b2a4df20SDavid Howells 	}
598b2a4df20SDavid Howells 
599b2a4df20SDavid Howells 	/* key must have search permissions */
600b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
601b2a4df20SDavid Howells 	    key_task_permission(make_key_ref(key, ctx->possessed),
602f5895943SDavid Howells 				ctx->cred, KEY_NEED_SEARCH) < 0) {
603b2a4df20SDavid Howells 		ctx->result = ERR_PTR(-EACCES);
604b2a4df20SDavid Howells 		kleave(" = %d [!perm]", ctx->skipped_ret);
605b2a4df20SDavid Howells 		goto skipped;
606b2a4df20SDavid Howells 	}
607b2a4df20SDavid Howells 
608b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
609b2a4df20SDavid Howells 		/* we set a different error code if we pass a negative key */
610363b02daSDavid Howells 		if (state < 0) {
611363b02daSDavid Howells 			ctx->result = ERR_PTR(state);
612b2a4df20SDavid Howells 			kleave(" = %d [neg]", ctx->skipped_ret);
613b2a4df20SDavid Howells 			goto skipped;
614b2a4df20SDavid Howells 		}
615b2a4df20SDavid Howells 	}
616b2a4df20SDavid Howells 
617b2a4df20SDavid Howells 	/* Found */
618b2a4df20SDavid Howells 	ctx->result = make_key_ref(key, ctx->possessed);
619b2a4df20SDavid Howells 	kleave(" = 1 [found]");
620b2a4df20SDavid Howells 	return 1;
621b2a4df20SDavid Howells 
622b2a4df20SDavid Howells skipped:
623b2a4df20SDavid Howells 	return ctx->skipped_ret;
624b2a4df20SDavid Howells }
625b2a4df20SDavid Howells 
626b2a4df20SDavid Howells /*
627b2a4df20SDavid Howells  * Search inside a keyring for a key.  We can search by walking to it
628b2a4df20SDavid Howells  * directly based on its index-key or we can iterate over the entire
629b2a4df20SDavid Howells  * tree looking for it, based on the match function.
630b2a4df20SDavid Howells  */
631b2a4df20SDavid Howells static int search_keyring(struct key *keyring, struct keyring_search_context *ctx)
632b2a4df20SDavid Howells {
63346291959SDavid Howells 	if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_DIRECT) {
634b2a4df20SDavid Howells 		const void *object;
635b2a4df20SDavid Howells 
636b2a4df20SDavid Howells 		object = assoc_array_find(&keyring->keys,
637b2a4df20SDavid Howells 					  &keyring_assoc_array_ops,
638b2a4df20SDavid Howells 					  &ctx->index_key);
639b2a4df20SDavid Howells 		return object ? ctx->iterator(object, ctx) : 0;
640b2a4df20SDavid Howells 	}
641b2a4df20SDavid Howells 	return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx);
642b2a4df20SDavid Howells }
643b2a4df20SDavid Howells 
644b2a4df20SDavid Howells /*
645b2a4df20SDavid Howells  * Search a tree of keyrings that point to other keyrings up to the maximum
646b2a4df20SDavid Howells  * depth.
647b2a4df20SDavid Howells  */
648b2a4df20SDavid Howells static bool search_nested_keyrings(struct key *keyring,
649b2a4df20SDavid Howells 				   struct keyring_search_context *ctx)
650b2a4df20SDavid Howells {
651b2a4df20SDavid Howells 	struct {
652b2a4df20SDavid Howells 		struct key *keyring;
653b2a4df20SDavid Howells 		struct assoc_array_node *node;
654b2a4df20SDavid Howells 		int slot;
655b2a4df20SDavid Howells 	} stack[KEYRING_SEARCH_MAX_DEPTH];
656b2a4df20SDavid Howells 
657b2a4df20SDavid Howells 	struct assoc_array_shortcut *shortcut;
658b2a4df20SDavid Howells 	struct assoc_array_node *node;
659b2a4df20SDavid Howells 	struct assoc_array_ptr *ptr;
660b2a4df20SDavid Howells 	struct key *key;
661b2a4df20SDavid Howells 	int sp = 0, slot;
662b2a4df20SDavid Howells 
663b2a4df20SDavid Howells 	kenter("{%d},{%s,%s}",
664b2a4df20SDavid Howells 	       keyring->serial,
665b2a4df20SDavid Howells 	       ctx->index_key.type->name,
666b2a4df20SDavid Howells 	       ctx->index_key.description);
667b2a4df20SDavid Howells 
668054f6180SDavid Howells #define STATE_CHECKS (KEYRING_SEARCH_NO_STATE_CHECK | KEYRING_SEARCH_DO_STATE_CHECK)
669054f6180SDavid Howells 	BUG_ON((ctx->flags & STATE_CHECKS) == 0 ||
670054f6180SDavid Howells 	       (ctx->flags & STATE_CHECKS) == STATE_CHECKS);
671054f6180SDavid Howells 
672f771fde8SDavid Howells 	if (ctx->index_key.description)
673f771fde8SDavid Howells 		key_set_index_key(&ctx->index_key);
674f771fde8SDavid Howells 
675b2a4df20SDavid Howells 	/* Check to see if this top-level keyring is what we are looking for
676b2a4df20SDavid Howells 	 * and whether it is valid or not.
677b2a4df20SDavid Howells 	 */
67846291959SDavid Howells 	if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_ITERATE ||
679b2a4df20SDavid Howells 	    keyring_compare_object(keyring, &ctx->index_key)) {
680b2a4df20SDavid Howells 		ctx->skipped_ret = 2;
681b2a4df20SDavid Howells 		switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) {
682b2a4df20SDavid Howells 		case 1:
683b2a4df20SDavid Howells 			goto found;
684b2a4df20SDavid Howells 		case 2:
685b2a4df20SDavid Howells 			return false;
686b2a4df20SDavid Howells 		default:
687b2a4df20SDavid Howells 			break;
688b2a4df20SDavid Howells 		}
689b2a4df20SDavid Howells 	}
690b2a4df20SDavid Howells 
691b2a4df20SDavid Howells 	ctx->skipped_ret = 0;
692b2a4df20SDavid Howells 
693b2a4df20SDavid Howells 	/* Start processing a new keyring */
694b2a4df20SDavid Howells descend_to_keyring:
695b2a4df20SDavid Howells 	kdebug("descend to %d", keyring->serial);
696b2a4df20SDavid Howells 	if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
697b2a4df20SDavid Howells 			      (1 << KEY_FLAG_REVOKED)))
698b2a4df20SDavid Howells 		goto not_this_keyring;
699b2a4df20SDavid Howells 
700b2a4df20SDavid Howells 	/* Search through the keys in this keyring before its searching its
701b2a4df20SDavid Howells 	 * subtrees.
702b2a4df20SDavid Howells 	 */
703b2a4df20SDavid Howells 	if (search_keyring(keyring, ctx))
704b2a4df20SDavid Howells 		goto found;
705b2a4df20SDavid Howells 
706b2a4df20SDavid Howells 	/* Then manually iterate through the keyrings nested in this one.
707b2a4df20SDavid Howells 	 *
708b2a4df20SDavid Howells 	 * Start from the root node of the index tree.  Because of the way the
709b2a4df20SDavid Howells 	 * hash function has been set up, keyrings cluster on the leftmost
710b2a4df20SDavid Howells 	 * branch of the root node (root slot 0) or in the root node itself.
711b2a4df20SDavid Howells 	 * Non-keyrings avoid the leftmost branch of the root entirely (root
712b2a4df20SDavid Howells 	 * slots 1-15).
713b2a4df20SDavid Howells 	 */
714dcf49dbcSDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_RECURSE))
715dcf49dbcSDavid Howells 		goto not_this_keyring;
716dcf49dbcSDavid Howells 
717381f20fcSDavidlohr Bueso 	ptr = READ_ONCE(keyring->keys.root);
718b2a4df20SDavid Howells 	if (!ptr)
719b2a4df20SDavid Howells 		goto not_this_keyring;
720b2a4df20SDavid Howells 
721b2a4df20SDavid Howells 	if (assoc_array_ptr_is_shortcut(ptr)) {
722b2a4df20SDavid Howells 		/* If the root is a shortcut, either the keyring only contains
723b2a4df20SDavid Howells 		 * keyring pointers (everything clusters behind root slot 0) or
724b2a4df20SDavid Howells 		 * doesn't contain any keyring pointers.
725b2a4df20SDavid Howells 		 */
726b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
727b2a4df20SDavid Howells 		if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0)
728b2a4df20SDavid Howells 			goto not_this_keyring;
729b2a4df20SDavid Howells 
730381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(shortcut->next_node);
731b2a4df20SDavid Howells 		node = assoc_array_ptr_to_node(ptr);
732b2a4df20SDavid Howells 		goto begin_node;
733b2a4df20SDavid Howells 	}
734b2a4df20SDavid Howells 
735b2a4df20SDavid Howells 	node = assoc_array_ptr_to_node(ptr);
736b2a4df20SDavid Howells 	ptr = node->slots[0];
737b2a4df20SDavid Howells 	if (!assoc_array_ptr_is_meta(ptr))
738b2a4df20SDavid Howells 		goto begin_node;
739b2a4df20SDavid Howells 
740b2a4df20SDavid Howells descend_to_node:
741b2a4df20SDavid Howells 	/* Descend to a more distal node in this keyring's content tree and go
742b2a4df20SDavid Howells 	 * through that.
743b2a4df20SDavid Howells 	 */
744b2a4df20SDavid Howells 	kdebug("descend");
745b2a4df20SDavid Howells 	if (assoc_array_ptr_is_shortcut(ptr)) {
746b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
747381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(shortcut->next_node);
748b2a4df20SDavid Howells 		BUG_ON(!assoc_array_ptr_is_node(ptr));
749b2a4df20SDavid Howells 	}
7509c5e45dfSDavid Howells 	node = assoc_array_ptr_to_node(ptr);
751b2a4df20SDavid Howells 
752b2a4df20SDavid Howells begin_node:
753b2a4df20SDavid Howells 	kdebug("begin_node");
754b2a4df20SDavid Howells 	slot = 0;
755b2a4df20SDavid Howells ascend_to_node:
756b2a4df20SDavid Howells 	/* Go through the slots in a node */
757b2a4df20SDavid Howells 	for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
758381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(node->slots[slot]);
759b2a4df20SDavid Howells 
760b2a4df20SDavid Howells 		if (assoc_array_ptr_is_meta(ptr) && node->back_pointer)
761b2a4df20SDavid Howells 			goto descend_to_node;
762b2a4df20SDavid Howells 
763b2a4df20SDavid Howells 		if (!keyring_ptr_is_keyring(ptr))
764b2a4df20SDavid Howells 			continue;
765b2a4df20SDavid Howells 
766b2a4df20SDavid Howells 		key = keyring_ptr_to_key(ptr);
767b2a4df20SDavid Howells 
768b2a4df20SDavid Howells 		if (sp >= KEYRING_SEARCH_MAX_DEPTH) {
769b2a4df20SDavid Howells 			if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) {
770b2a4df20SDavid Howells 				ctx->result = ERR_PTR(-ELOOP);
771b2a4df20SDavid Howells 				return false;
772b2a4df20SDavid Howells 			}
773b2a4df20SDavid Howells 			goto not_this_keyring;
774b2a4df20SDavid Howells 		}
775b2a4df20SDavid Howells 
776b2a4df20SDavid Howells 		/* Search a nested keyring */
777b2a4df20SDavid Howells 		if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
778b2a4df20SDavid Howells 		    key_task_permission(make_key_ref(key, ctx->possessed),
779f5895943SDavid Howells 					ctx->cred, KEY_NEED_SEARCH) < 0)
780b2a4df20SDavid Howells 			continue;
781b2a4df20SDavid Howells 
782b2a4df20SDavid Howells 		/* stack the current position */
783b2a4df20SDavid Howells 		stack[sp].keyring = keyring;
784b2a4df20SDavid Howells 		stack[sp].node = node;
785b2a4df20SDavid Howells 		stack[sp].slot = slot;
786b2a4df20SDavid Howells 		sp++;
787b2a4df20SDavid Howells 
788b2a4df20SDavid Howells 		/* begin again with the new keyring */
789b2a4df20SDavid Howells 		keyring = key;
790b2a4df20SDavid Howells 		goto descend_to_keyring;
791b2a4df20SDavid Howells 	}
792b2a4df20SDavid Howells 
793b2a4df20SDavid Howells 	/* We've dealt with all the slots in the current node, so now we need
794b2a4df20SDavid Howells 	 * to ascend to the parent and continue processing there.
795b2a4df20SDavid Howells 	 */
796381f20fcSDavidlohr Bueso 	ptr = READ_ONCE(node->back_pointer);
797b2a4df20SDavid Howells 	slot = node->parent_slot;
798b2a4df20SDavid Howells 
799b2a4df20SDavid Howells 	if (ptr && assoc_array_ptr_is_shortcut(ptr)) {
800b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
801381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(shortcut->back_pointer);
802b2a4df20SDavid Howells 		slot = shortcut->parent_slot;
803b2a4df20SDavid Howells 	}
804b2a4df20SDavid Howells 	if (!ptr)
805b2a4df20SDavid Howells 		goto not_this_keyring;
806b2a4df20SDavid Howells 	node = assoc_array_ptr_to_node(ptr);
807b2a4df20SDavid Howells 	slot++;
808b2a4df20SDavid Howells 
809b2a4df20SDavid Howells 	/* If we've ascended to the root (zero backpointer), we must have just
810b2a4df20SDavid Howells 	 * finished processing the leftmost branch rather than the root slots -
811b2a4df20SDavid Howells 	 * so there can't be any more keyrings for us to find.
812b2a4df20SDavid Howells 	 */
813b2a4df20SDavid Howells 	if (node->back_pointer) {
814b2a4df20SDavid Howells 		kdebug("ascend %d", slot);
815b2a4df20SDavid Howells 		goto ascend_to_node;
816b2a4df20SDavid Howells 	}
817b2a4df20SDavid Howells 
818b2a4df20SDavid Howells 	/* The keyring we're looking at was disqualified or didn't contain a
819b2a4df20SDavid Howells 	 * matching key.
820b2a4df20SDavid Howells 	 */
821b2a4df20SDavid Howells not_this_keyring:
822b2a4df20SDavid Howells 	kdebug("not_this_keyring %d", sp);
823b2a4df20SDavid Howells 	if (sp <= 0) {
824b2a4df20SDavid Howells 		kleave(" = false");
825b2a4df20SDavid Howells 		return false;
826b2a4df20SDavid Howells 	}
827b2a4df20SDavid Howells 
828b2a4df20SDavid Howells 	/* Resume the processing of a keyring higher up in the tree */
829b2a4df20SDavid Howells 	sp--;
830b2a4df20SDavid Howells 	keyring = stack[sp].keyring;
831b2a4df20SDavid Howells 	node = stack[sp].node;
832b2a4df20SDavid Howells 	slot = stack[sp].slot + 1;
833b2a4df20SDavid Howells 	kdebug("ascend to %d [%d]", keyring->serial, slot);
834b2a4df20SDavid Howells 	goto ascend_to_node;
835b2a4df20SDavid Howells 
836b2a4df20SDavid Howells 	/* We found a viable match */
837b2a4df20SDavid Howells found:
838b2a4df20SDavid Howells 	key = key_ref_to_ptr(ctx->result);
839b2a4df20SDavid Howells 	key_check(key);
840b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) {
841074d5898SBaolin Wang 		key->last_used_at = ctx->now;
842074d5898SBaolin Wang 		keyring->last_used_at = ctx->now;
843b2a4df20SDavid Howells 		while (sp > 0)
844074d5898SBaolin Wang 			stack[--sp].keyring->last_used_at = ctx->now;
845b2a4df20SDavid Howells 	}
846b2a4df20SDavid Howells 	kleave(" = true");
847b2a4df20SDavid Howells 	return true;
848b2a4df20SDavid Howells }
849b2a4df20SDavid Howells 
850973c9f4fSDavid Howells /**
851e59428f7SDavid Howells  * keyring_search_rcu - Search a keyring tree for a matching key under RCU
852973c9f4fSDavid Howells  * @keyring_ref: A pointer to the keyring with possession indicator.
8534bdf0bc3SDavid Howells  * @ctx: The keyring search context.
854973c9f4fSDavid Howells  *
855973c9f4fSDavid Howells  * Search the supplied keyring tree for a key that matches the criteria given.
856973c9f4fSDavid Howells  * The root keyring and any linked keyrings must grant Search permission to the
857973c9f4fSDavid Howells  * caller to be searchable and keys can only be found if they too grant Search
858973c9f4fSDavid Howells  * to the caller. The possession flag on the root keyring pointer controls use
859973c9f4fSDavid Howells  * of the possessor bits in permissions checking of the entire tree.  In
860973c9f4fSDavid Howells  * addition, the LSM gets to forbid keyring searches and key matches.
861973c9f4fSDavid Howells  *
862973c9f4fSDavid Howells  * The search is performed as a breadth-then-depth search up to the prescribed
863e59428f7SDavid Howells  * limit (KEYRING_SEARCH_MAX_DEPTH).  The caller must hold the RCU read lock to
864e59428f7SDavid Howells  * prevent keyrings from being destroyed or rearranged whilst they are being
865e59428f7SDavid Howells  * searched.
866973c9f4fSDavid Howells  *
867973c9f4fSDavid Howells  * Keys are matched to the type provided and are then filtered by the match
868973c9f4fSDavid Howells  * function, which is given the description to use in any way it sees fit.  The
869973c9f4fSDavid Howells  * match function may use any attributes of a key that it wishes to to
870973c9f4fSDavid Howells  * determine the match.  Normally the match function from the key type would be
871973c9f4fSDavid Howells  * used.
872973c9f4fSDavid Howells  *
873b2a4df20SDavid Howells  * RCU can be used to prevent the keyring key lists from disappearing without
874b2a4df20SDavid Howells  * the need to take lots of locks.
875973c9f4fSDavid Howells  *
876973c9f4fSDavid Howells  * Returns a pointer to the found key and increments the key usage count if
877973c9f4fSDavid Howells  * successful; -EAGAIN if no matching keys were found, or if expired or revoked
878973c9f4fSDavid Howells  * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
879973c9f4fSDavid Howells  * specified keyring wasn't a keyring.
880973c9f4fSDavid Howells  *
881973c9f4fSDavid Howells  * In the case of a successful return, the possession attribute from
882973c9f4fSDavid Howells  * @keyring_ref is propagated to the returned key reference.
8831da177e4SLinus Torvalds  */
884e59428f7SDavid Howells key_ref_t keyring_search_rcu(key_ref_t keyring_ref,
8854bdf0bc3SDavid Howells 			     struct keyring_search_context *ctx)
8861da177e4SLinus Torvalds {
88731d5a79dSDavid Howells 	struct key *keyring;
8881da177e4SLinus Torvalds 	long err;
889b2a4df20SDavid Howells 
890b2a4df20SDavid Howells 	ctx->iterator = keyring_search_iterator;
891b2a4df20SDavid Howells 	ctx->possessed = is_key_possessed(keyring_ref);
892b2a4df20SDavid Howells 	ctx->result = ERR_PTR(-EAGAIN);
8931da177e4SLinus Torvalds 
894664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
8951da177e4SLinus Torvalds 	key_check(keyring);
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
898b2a4df20SDavid Howells 		return ERR_PTR(-ENOTDIR);
899b2a4df20SDavid Howells 
900b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) {
901f5895943SDavid Howells 		err = key_task_permission(keyring_ref, ctx->cred, KEY_NEED_SEARCH);
902b2a4df20SDavid Howells 		if (err < 0)
903b2a4df20SDavid Howells 			return ERR_PTR(err);
904b2a4df20SDavid Howells 	}
9051da177e4SLinus Torvalds 
906074d5898SBaolin Wang 	ctx->now = ktime_get_real_seconds();
907b2a4df20SDavid Howells 	if (search_nested_keyrings(keyring, ctx))
908b2a4df20SDavid Howells 		__key_get(key_ref_to_ptr(ctx->result));
909b2a4df20SDavid Howells 	return ctx->result;
910a8b17ed0SDavid Howells }
9111da177e4SLinus Torvalds 
912973c9f4fSDavid Howells /**
913973c9f4fSDavid Howells  * keyring_search - Search the supplied keyring tree for a matching key
914973c9f4fSDavid Howells  * @keyring: The root of the keyring tree to be searched.
915973c9f4fSDavid Howells  * @type: The type of keyring we want to find.
916973c9f4fSDavid Howells  * @description: The name of the keyring we want to find.
917dcf49dbcSDavid Howells  * @recurse: True to search the children of @keyring also
918973c9f4fSDavid Howells  *
919e59428f7SDavid Howells  * As keyring_search_rcu() above, but using the current task's credentials and
920b2a4df20SDavid Howells  * type's default matching function and preferred search method.
9211da177e4SLinus Torvalds  */
922664cceb0SDavid Howells key_ref_t keyring_search(key_ref_t keyring,
9231da177e4SLinus Torvalds 			 struct key_type *type,
924dcf49dbcSDavid Howells 			 const char *description,
925dcf49dbcSDavid Howells 			 bool recurse)
9261da177e4SLinus Torvalds {
9274bdf0bc3SDavid Howells 	struct keyring_search_context ctx = {
9284bdf0bc3SDavid Howells 		.index_key.type		= type,
9294bdf0bc3SDavid Howells 		.index_key.description	= description,
930ede0fa98SEric Biggers 		.index_key.desc_len	= strlen(description),
9314bdf0bc3SDavid Howells 		.cred			= current_cred(),
932c06cfb08SDavid Howells 		.match_data.cmp		= key_default_cmp,
93346291959SDavid Howells 		.match_data.raw_data	= description,
93446291959SDavid Howells 		.match_data.lookup_type	= KEYRING_SEARCH_LOOKUP_DIRECT,
93546291959SDavid Howells 		.flags			= KEYRING_SEARCH_DO_STATE_CHECK,
9364bdf0bc3SDavid Howells 	};
93746291959SDavid Howells 	key_ref_t key;
93846291959SDavid Howells 	int ret;
9394bdf0bc3SDavid Howells 
940dcf49dbcSDavid Howells 	if (recurse)
941dcf49dbcSDavid Howells 		ctx.flags |= KEYRING_SEARCH_RECURSE;
94246291959SDavid Howells 	if (type->match_preparse) {
94346291959SDavid Howells 		ret = type->match_preparse(&ctx.match_data);
94446291959SDavid Howells 		if (ret < 0)
94546291959SDavid Howells 			return ERR_PTR(ret);
94646291959SDavid Howells 	}
94746291959SDavid Howells 
948e59428f7SDavid Howells 	rcu_read_lock();
949e59428f7SDavid Howells 	key = keyring_search_rcu(keyring, &ctx);
950e59428f7SDavid Howells 	rcu_read_unlock();
95146291959SDavid Howells 
95246291959SDavid Howells 	if (type->match_free)
95346291959SDavid Howells 		type->match_free(&ctx.match_data);
95446291959SDavid Howells 	return key;
955a8b17ed0SDavid Howells }
9561da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_search);
9571da177e4SLinus Torvalds 
9586563c91fSMat Martineau static struct key_restriction *keyring_restriction_alloc(
9596563c91fSMat Martineau 	key_restrict_link_func_t check)
9606563c91fSMat Martineau {
9616563c91fSMat Martineau 	struct key_restriction *keyres =
9626563c91fSMat Martineau 		kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
9636563c91fSMat Martineau 
9646563c91fSMat Martineau 	if (!keyres)
9656563c91fSMat Martineau 		return ERR_PTR(-ENOMEM);
9666563c91fSMat Martineau 
9676563c91fSMat Martineau 	keyres->check = check;
9686563c91fSMat Martineau 
9696563c91fSMat Martineau 	return keyres;
9706563c91fSMat Martineau }
9716563c91fSMat Martineau 
9726563c91fSMat Martineau /*
9736563c91fSMat Martineau  * Semaphore to serialise restriction setup to prevent reference count
9746563c91fSMat Martineau  * cycles through restriction key pointers.
9756563c91fSMat Martineau  */
9766563c91fSMat Martineau static DECLARE_RWSEM(keyring_serialise_restrict_sem);
9776563c91fSMat Martineau 
9786563c91fSMat Martineau /*
9796563c91fSMat Martineau  * Check for restriction cycles that would prevent keyring garbage collection.
9806563c91fSMat Martineau  * keyring_serialise_restrict_sem must be held.
9816563c91fSMat Martineau  */
9826563c91fSMat Martineau static bool keyring_detect_restriction_cycle(const struct key *dest_keyring,
9836563c91fSMat Martineau 					     struct key_restriction *keyres)
9846563c91fSMat Martineau {
9856563c91fSMat Martineau 	while (keyres && keyres->key &&
9866563c91fSMat Martineau 	       keyres->key->type == &key_type_keyring) {
9876563c91fSMat Martineau 		if (keyres->key == dest_keyring)
9886563c91fSMat Martineau 			return true;
9896563c91fSMat Martineau 
9906563c91fSMat Martineau 		keyres = keyres->key->restrict_link;
9916563c91fSMat Martineau 	}
9926563c91fSMat Martineau 
9936563c91fSMat Martineau 	return false;
9946563c91fSMat Martineau }
9956563c91fSMat Martineau 
9966563c91fSMat Martineau /**
9976563c91fSMat Martineau  * keyring_restrict - Look up and apply a restriction to a keyring
9989fd16537SDavid Howells  * @keyring_ref: The keyring to be restricted
9999fd16537SDavid Howells  * @type: The key type that will provide the restriction checker.
10006563c91fSMat Martineau  * @restriction: The restriction options to apply to the keyring
10019fd16537SDavid Howells  *
10029fd16537SDavid Howells  * Look up a keyring and apply a restriction to it.  The restriction is managed
10039fd16537SDavid Howells  * by the specific key type, but can be configured by the options specified in
10049fd16537SDavid Howells  * the restriction string.
10056563c91fSMat Martineau  */
10066563c91fSMat Martineau int keyring_restrict(key_ref_t keyring_ref, const char *type,
10076563c91fSMat Martineau 		     const char *restriction)
10086563c91fSMat Martineau {
10096563c91fSMat Martineau 	struct key *keyring;
10106563c91fSMat Martineau 	struct key_type *restrict_type = NULL;
10116563c91fSMat Martineau 	struct key_restriction *restrict_link;
10126563c91fSMat Martineau 	int ret = 0;
10136563c91fSMat Martineau 
10146563c91fSMat Martineau 	keyring = key_ref_to_ptr(keyring_ref);
10156563c91fSMat Martineau 	key_check(keyring);
10166563c91fSMat Martineau 
10176563c91fSMat Martineau 	if (keyring->type != &key_type_keyring)
10186563c91fSMat Martineau 		return -ENOTDIR;
10196563c91fSMat Martineau 
10206563c91fSMat Martineau 	if (!type) {
10216563c91fSMat Martineau 		restrict_link = keyring_restriction_alloc(restrict_link_reject);
10226563c91fSMat Martineau 	} else {
10236563c91fSMat Martineau 		restrict_type = key_type_lookup(type);
10246563c91fSMat Martineau 
10256563c91fSMat Martineau 		if (IS_ERR(restrict_type))
10266563c91fSMat Martineau 			return PTR_ERR(restrict_type);
10276563c91fSMat Martineau 
10286563c91fSMat Martineau 		if (!restrict_type->lookup_restriction) {
10296563c91fSMat Martineau 			ret = -ENOENT;
10306563c91fSMat Martineau 			goto error;
10316563c91fSMat Martineau 		}
10326563c91fSMat Martineau 
10336563c91fSMat Martineau 		restrict_link = restrict_type->lookup_restriction(restriction);
10346563c91fSMat Martineau 	}
10356563c91fSMat Martineau 
10366563c91fSMat Martineau 	if (IS_ERR(restrict_link)) {
10376563c91fSMat Martineau 		ret = PTR_ERR(restrict_link);
10386563c91fSMat Martineau 		goto error;
10396563c91fSMat Martineau 	}
10406563c91fSMat Martineau 
10416563c91fSMat Martineau 	down_write(&keyring->sem);
10426563c91fSMat Martineau 	down_write(&keyring_serialise_restrict_sem);
10436563c91fSMat Martineau 
10446563c91fSMat Martineau 	if (keyring->restrict_link)
10456563c91fSMat Martineau 		ret = -EEXIST;
10466563c91fSMat Martineau 	else if (keyring_detect_restriction_cycle(keyring, restrict_link))
10476563c91fSMat Martineau 		ret = -EDEADLK;
10486563c91fSMat Martineau 	else
10496563c91fSMat Martineau 		keyring->restrict_link = restrict_link;
10506563c91fSMat Martineau 
10516563c91fSMat Martineau 	up_write(&keyring_serialise_restrict_sem);
10526563c91fSMat Martineau 	up_write(&keyring->sem);
10536563c91fSMat Martineau 
10546563c91fSMat Martineau 	if (ret < 0) {
10556563c91fSMat Martineau 		key_put(restrict_link->key);
10566563c91fSMat Martineau 		kfree(restrict_link);
10576563c91fSMat Martineau 	}
10586563c91fSMat Martineau 
10596563c91fSMat Martineau error:
10606563c91fSMat Martineau 	if (restrict_type)
10616563c91fSMat Martineau 		key_type_put(restrict_type);
10626563c91fSMat Martineau 
10636563c91fSMat Martineau 	return ret;
10646563c91fSMat Martineau }
10656563c91fSMat Martineau EXPORT_SYMBOL(keyring_restrict);
10666563c91fSMat Martineau 
10671da177e4SLinus Torvalds /*
1068b2a4df20SDavid Howells  * Search the given keyring for a key that might be updated.
1069973c9f4fSDavid Howells  *
1070973c9f4fSDavid Howells  * The caller must guarantee that the keyring is a keyring and that the
1071b2a4df20SDavid Howells  * permission is granted to modify the keyring as no check is made here.  The
1072b2a4df20SDavid Howells  * caller must also hold a lock on the keyring semaphore.
1073973c9f4fSDavid Howells  *
1074973c9f4fSDavid Howells  * Returns a pointer to the found key with usage count incremented if
1075b2a4df20SDavid Howells  * successful and returns NULL if not found.  Revoked and invalidated keys are
1076b2a4df20SDavid Howells  * skipped over.
1077973c9f4fSDavid Howells  *
1078973c9f4fSDavid Howells  * If successful, the possession indicator is propagated from the keyring ref
1079973c9f4fSDavid Howells  * to the returned key reference.
10801da177e4SLinus Torvalds  */
1081b2a4df20SDavid Howells key_ref_t find_key_to_update(key_ref_t keyring_ref,
1082e57e8669SDavid Howells 			     const struct keyring_index_key *index_key)
10831da177e4SLinus Torvalds {
1084664cceb0SDavid Howells 	struct key *keyring, *key;
1085b2a4df20SDavid Howells 	const void *object;
10861da177e4SLinus Torvalds 
1087664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
1088664cceb0SDavid Howells 
1089b2a4df20SDavid Howells 	kenter("{%d},{%s,%s}",
1090b2a4df20SDavid Howells 	       keyring->serial, index_key->type->name, index_key->description);
109176d8aeabSDavid Howells 
1092b2a4df20SDavid Howells 	object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
1093b2a4df20SDavid Howells 				  index_key);
1094b2a4df20SDavid Howells 
1095b2a4df20SDavid Howells 	if (object)
10961da177e4SLinus Torvalds 		goto found;
10971da177e4SLinus Torvalds 
1098b2a4df20SDavid Howells 	kleave(" = NULL");
1099b2a4df20SDavid Howells 	return NULL;
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds found:
1102b2a4df20SDavid Howells 	key = keyring_ptr_to_key(object);
1103b2a4df20SDavid Howells 	if (key->flags & ((1 << KEY_FLAG_INVALIDATED) |
1104b2a4df20SDavid Howells 			  (1 << KEY_FLAG_REVOKED))) {
1105b2a4df20SDavid Howells 		kleave(" = NULL [x]");
1106b2a4df20SDavid Howells 		return NULL;
1107b2a4df20SDavid Howells 	}
1108ccc3e6d9SDavid Howells 	__key_get(key);
1109b2a4df20SDavid Howells 	kleave(" = {%d}", key->serial);
1110b2a4df20SDavid Howells 	return make_key_ref(key, is_key_possessed(keyring_ref));
1111a8b17ed0SDavid Howells }
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds /*
1114973c9f4fSDavid Howells  * Find a keyring with the specified name.
1115973c9f4fSDavid Howells  *
1116237bbd29SEric Biggers  * Only keyrings that have nonzero refcount, are not revoked, and are owned by a
1117237bbd29SEric Biggers  * user in the current user namespace are considered.  If @uid_keyring is %true,
1118237bbd29SEric Biggers  * the keyring additionally must have been allocated as a user or user session
1119237bbd29SEric Biggers  * keyring; otherwise, it must grant Search permission directly to the caller.
1120973c9f4fSDavid Howells  *
1121973c9f4fSDavid Howells  * Returns a pointer to the keyring with the keyring's refcount having being
1122973c9f4fSDavid Howells  * incremented on success.  -ENOKEY is returned if a key could not be found.
11231da177e4SLinus Torvalds  */
1124237bbd29SEric Biggers struct key *find_keyring_by_name(const char *name, bool uid_keyring)
11251da177e4SLinus Torvalds {
1126b206f281SDavid Howells 	struct user_namespace *ns = current_user_ns();
11271da177e4SLinus Torvalds 	struct key *keyring;
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	if (!name)
1130cea7daa3SToshiyuki Okajima 		return ERR_PTR(-EINVAL);
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	read_lock(&keyring_name_lock);
11331da177e4SLinus Torvalds 
1134b206f281SDavid Howells 	/* Search this hash bucket for a keyring with a matching name that
1135b206f281SDavid Howells 	 * grants Search permission and that hasn't been revoked
1136b206f281SDavid Howells 	 */
1137b206f281SDavid Howells 	list_for_each_entry(keyring, &ns->keyring_name_list, name_link) {
1138b206f281SDavid Howells 		if (!kuid_has_mapping(ns, keyring->user->uid))
11392ea190d0SSerge E. Hallyn 			continue;
11402ea190d0SSerge E. Hallyn 
114176d8aeabSDavid Howells 		if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
11421da177e4SLinus Torvalds 			continue;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 		if (strcmp(keyring->description, name) != 0)
11451da177e4SLinus Torvalds 			continue;
11461da177e4SLinus Torvalds 
1147237bbd29SEric Biggers 		if (uid_keyring) {
1148237bbd29SEric Biggers 			if (!test_bit(KEY_FLAG_UID_KEYRING,
1149237bbd29SEric Biggers 				      &keyring->flags))
1150237bbd29SEric Biggers 				continue;
1151237bbd29SEric Biggers 		} else {
1152237bbd29SEric Biggers 			if (key_permission(make_key_ref(keyring, 0),
1153f5895943SDavid Howells 					   KEY_NEED_SEARCH) < 0)
11541da177e4SLinus Torvalds 				continue;
1155237bbd29SEric Biggers 		}
11561da177e4SLinus Torvalds 
1157cea7daa3SToshiyuki Okajima 		/* we've got a match but we might end up racing with
1158cea7daa3SToshiyuki Okajima 		 * key_cleanup() if the keyring is currently 'dead'
1159cea7daa3SToshiyuki Okajima 		 * (ie. it has a zero usage count) */
1160fff29291SElena Reshetova 		if (!refcount_inc_not_zero(&keyring->usage))
1161cea7daa3SToshiyuki Okajima 			continue;
1162074d5898SBaolin Wang 		keyring->last_used_at = ktime_get_real_seconds();
1163cea7daa3SToshiyuki Okajima 		goto out;
11641da177e4SLinus Torvalds 	}
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 	keyring = ERR_PTR(-ENOKEY);
1167cea7daa3SToshiyuki Okajima out:
1168cea7daa3SToshiyuki Okajima 	read_unlock(&keyring_name_lock);
11691da177e4SLinus Torvalds 	return keyring;
1170a8b17ed0SDavid Howells }
11711da177e4SLinus Torvalds 
1172b2a4df20SDavid Howells static int keyring_detect_cycle_iterator(const void *object,
1173b2a4df20SDavid Howells 					 void *iterator_data)
1174b2a4df20SDavid Howells {
1175b2a4df20SDavid Howells 	struct keyring_search_context *ctx = iterator_data;
1176b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
1177b2a4df20SDavid Howells 
1178b2a4df20SDavid Howells 	kenter("{%d}", key->serial);
1179b2a4df20SDavid Howells 
1180979e0d74SDavid Howells 	/* We might get a keyring with matching index-key that is nonetheless a
1181979e0d74SDavid Howells 	 * different keyring. */
118246291959SDavid Howells 	if (key != ctx->match_data.raw_data)
1183979e0d74SDavid Howells 		return 0;
1184979e0d74SDavid Howells 
1185b2a4df20SDavid Howells 	ctx->result = ERR_PTR(-EDEADLK);
1186b2a4df20SDavid Howells 	return 1;
1187b2a4df20SDavid Howells }
1188b2a4df20SDavid Howells 
11891da177e4SLinus Torvalds /*
1190973c9f4fSDavid Howells  * See if a cycle will will be created by inserting acyclic tree B in acyclic
1191973c9f4fSDavid Howells  * tree A at the topmost level (ie: as a direct child of A).
1192973c9f4fSDavid Howells  *
1193973c9f4fSDavid Howells  * Since we are adding B to A at the top level, checking for cycles should just
1194973c9f4fSDavid Howells  * be a matter of seeing if node A is somewhere in tree B.
11951da177e4SLinus Torvalds  */
11961da177e4SLinus Torvalds static int keyring_detect_cycle(struct key *A, struct key *B)
11971da177e4SLinus Torvalds {
1198b2a4df20SDavid Howells 	struct keyring_search_context ctx = {
1199b2a4df20SDavid Howells 		.index_key		= A->index_key,
120046291959SDavid Howells 		.match_data.raw_data	= A,
120146291959SDavid Howells 		.match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
1202b2a4df20SDavid Howells 		.iterator		= keyring_detect_cycle_iterator,
120346291959SDavid Howells 		.flags			= (KEYRING_SEARCH_NO_STATE_CHECK |
1204b2a4df20SDavid Howells 					   KEYRING_SEARCH_NO_UPDATE_TIME |
1205b2a4df20SDavid Howells 					   KEYRING_SEARCH_NO_CHECK_PERM |
1206dcf49dbcSDavid Howells 					   KEYRING_SEARCH_DETECT_TOO_DEEP |
1207dcf49dbcSDavid Howells 					   KEYRING_SEARCH_RECURSE),
1208b2a4df20SDavid Howells 	};
12091da177e4SLinus Torvalds 
121076d8aeabSDavid Howells 	rcu_read_lock();
1211b2a4df20SDavid Howells 	search_nested_keyrings(B, &ctx);
121276d8aeabSDavid Howells 	rcu_read_unlock();
1213b2a4df20SDavid Howells 	return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result);
1214f70e2e06SDavid Howells }
1215cab8eb59SDavid Howells 
1216cab8eb59SDavid Howells /*
1217df593ee2SDavid Howells  * Lock keyring for link.
1218df593ee2SDavid Howells  */
1219df593ee2SDavid Howells int __key_link_lock(struct key *keyring,
1220df593ee2SDavid Howells 		    const struct keyring_index_key *index_key)
1221df593ee2SDavid Howells 	__acquires(&keyring->sem)
1222df593ee2SDavid Howells 	__acquires(&keyring_serialise_link_lock)
1223df593ee2SDavid Howells {
1224df593ee2SDavid Howells 	if (keyring->type != &key_type_keyring)
1225df593ee2SDavid Howells 		return -ENOTDIR;
1226df593ee2SDavid Howells 
1227df593ee2SDavid Howells 	down_write(&keyring->sem);
1228df593ee2SDavid Howells 
1229df593ee2SDavid Howells 	/* Serialise link/link calls to prevent parallel calls causing a cycle
1230df593ee2SDavid Howells 	 * when linking two keyring in opposite orders.
1231df593ee2SDavid Howells 	 */
1232df593ee2SDavid Howells 	if (index_key->type == &key_type_keyring)
1233df593ee2SDavid Howells 		mutex_lock(&keyring_serialise_link_lock);
1234df593ee2SDavid Howells 
1235df593ee2SDavid Howells 	return 0;
1236df593ee2SDavid Howells }
1237df593ee2SDavid Howells 
1238df593ee2SDavid Howells /*
1239ed0ac5c7SDavid Howells  * Lock keyrings for move (link/unlink combination).
1240ed0ac5c7SDavid Howells  */
1241ed0ac5c7SDavid Howells int __key_move_lock(struct key *l_keyring, struct key *u_keyring,
1242ed0ac5c7SDavid Howells 		    const struct keyring_index_key *index_key)
1243ed0ac5c7SDavid Howells 	__acquires(&l_keyring->sem)
1244ed0ac5c7SDavid Howells 	__acquires(&u_keyring->sem)
1245ed0ac5c7SDavid Howells 	__acquires(&keyring_serialise_link_lock)
1246ed0ac5c7SDavid Howells {
1247ed0ac5c7SDavid Howells 	if (l_keyring->type != &key_type_keyring ||
1248ed0ac5c7SDavid Howells 	    u_keyring->type != &key_type_keyring)
1249ed0ac5c7SDavid Howells 		return -ENOTDIR;
1250ed0ac5c7SDavid Howells 
1251ed0ac5c7SDavid Howells 	/* We have to be very careful here to take the keyring locks in the
1252ed0ac5c7SDavid Howells 	 * right order, lest we open ourselves to deadlocking against another
1253ed0ac5c7SDavid Howells 	 * move operation.
1254ed0ac5c7SDavid Howells 	 */
1255ed0ac5c7SDavid Howells 	if (l_keyring < u_keyring) {
1256ed0ac5c7SDavid Howells 		down_write(&l_keyring->sem);
1257ed0ac5c7SDavid Howells 		down_write_nested(&u_keyring->sem, 1);
1258ed0ac5c7SDavid Howells 	} else {
1259ed0ac5c7SDavid Howells 		down_write(&u_keyring->sem);
1260ed0ac5c7SDavid Howells 		down_write_nested(&l_keyring->sem, 1);
1261ed0ac5c7SDavid Howells 	}
1262ed0ac5c7SDavid Howells 
1263ed0ac5c7SDavid Howells 	/* Serialise link/link calls to prevent parallel calls causing a cycle
1264ed0ac5c7SDavid Howells 	 * when linking two keyring in opposite orders.
1265ed0ac5c7SDavid Howells 	 */
1266ed0ac5c7SDavid Howells 	if (index_key->type == &key_type_keyring)
1267ed0ac5c7SDavid Howells 		mutex_lock(&keyring_serialise_link_lock);
1268ed0ac5c7SDavid Howells 
1269ed0ac5c7SDavid Howells 	return 0;
1270ed0ac5c7SDavid Howells }
1271ed0ac5c7SDavid Howells 
1272ed0ac5c7SDavid Howells /*
1273973c9f4fSDavid Howells  * Preallocate memory so that a key can be linked into to a keyring.
12741da177e4SLinus Torvalds  */
1275b2a4df20SDavid Howells int __key_link_begin(struct key *keyring,
1276b2a4df20SDavid Howells 		     const struct keyring_index_key *index_key,
1277b2a4df20SDavid Howells 		     struct assoc_array_edit **_edit)
12781da177e4SLinus Torvalds {
1279b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1280b2a4df20SDavid Howells 	int ret;
12811da177e4SLinus Torvalds 
128216feef43SDavid Howells 	kenter("%d,%s,%s,",
1283b2a4df20SDavid Howells 	       keyring->serial, index_key->type->name, index_key->description);
1284b2a4df20SDavid Howells 
1285b2a4df20SDavid Howells 	BUG_ON(index_key->desc_len == 0);
1286df593ee2SDavid Howells 	BUG_ON(*_edit != NULL);
1287f70e2e06SDavid Howells 
1288df593ee2SDavid Howells 	*_edit = NULL;
1289f70e2e06SDavid Howells 
12901da177e4SLinus Torvalds 	ret = -EKEYREVOKED;
129176d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1292df593ee2SDavid Howells 		goto error;
12931da177e4SLinus Torvalds 
1294b2a4df20SDavid Howells 	/* Create an edit script that will insert/replace the key in the
1295b2a4df20SDavid Howells 	 * keyring tree.
1296b2a4df20SDavid Howells 	 */
1297b2a4df20SDavid Howells 	edit = assoc_array_insert(&keyring->keys,
1298b2a4df20SDavid Howells 				  &keyring_assoc_array_ops,
1299b2a4df20SDavid Howells 				  index_key,
1300b2a4df20SDavid Howells 				  NULL);
1301b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1302b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1303df593ee2SDavid Howells 		goto error;
1304034faeb9SDavid Howells 	}
1305034faeb9SDavid Howells 
1306034faeb9SDavid Howells 	/* If we're not replacing a link in-place then we're going to need some
1307034faeb9SDavid Howells 	 * extra quota.
1308034faeb9SDavid Howells 	 */
1309034faeb9SDavid Howells 	if (!edit->dead_leaf) {
1310034faeb9SDavid Howells 		ret = key_payload_reserve(keyring,
1311034faeb9SDavid Howells 					  keyring->datalen + KEYQUOTA_LINK_BYTES);
1312034faeb9SDavid Howells 		if (ret < 0)
1313034faeb9SDavid Howells 			goto error_cancel;
13141da177e4SLinus Torvalds 	}
13151da177e4SLinus Torvalds 
1316b2a4df20SDavid Howells 	*_edit = edit;
1317f70e2e06SDavid Howells 	kleave(" = 0");
1318f70e2e06SDavid Howells 	return 0;
13191da177e4SLinus Torvalds 
1320034faeb9SDavid Howells error_cancel:
1321034faeb9SDavid Howells 	assoc_array_cancel_edit(edit);
1322df593ee2SDavid Howells error:
1323f70e2e06SDavid Howells 	kleave(" = %d", ret);
1324f70e2e06SDavid Howells 	return ret;
1325f70e2e06SDavid Howells }
13261da177e4SLinus Torvalds 
1327f70e2e06SDavid Howells /*
1328973c9f4fSDavid Howells  * Check already instantiated keys aren't going to be a problem.
1329973c9f4fSDavid Howells  *
1330973c9f4fSDavid Howells  * The caller must have called __key_link_begin(). Don't need to call this for
1331973c9f4fSDavid Howells  * keys that were created since __key_link_begin() was called.
1332f70e2e06SDavid Howells  */
1333f70e2e06SDavid Howells int __key_link_check_live_key(struct key *keyring, struct key *key)
1334f70e2e06SDavid Howells {
1335f70e2e06SDavid Howells 	if (key->type == &key_type_keyring)
1336f70e2e06SDavid Howells 		/* check that we aren't going to create a cycle by linking one
1337f70e2e06SDavid Howells 		 * keyring to another */
1338f70e2e06SDavid Howells 		return keyring_detect_cycle(keyring, key);
1339f70e2e06SDavid Howells 	return 0;
1340f70e2e06SDavid Howells }
13411da177e4SLinus Torvalds 
1342f70e2e06SDavid Howells /*
1343973c9f4fSDavid Howells  * Link a key into to a keyring.
1344973c9f4fSDavid Howells  *
1345973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.  Discards any
1346973c9f4fSDavid Howells  * already extant link to matching key if there is one, so that each keyring
1347973c9f4fSDavid Howells  * holds at most one link to any given key of a particular type+description
1348973c9f4fSDavid Howells  * combination.
1349f70e2e06SDavid Howells  */
1350b2a4df20SDavid Howells void __key_link(struct key *key, struct assoc_array_edit **_edit)
1351f70e2e06SDavid Howells {
1352ccc3e6d9SDavid Howells 	__key_get(key);
1353b2a4df20SDavid Howells 	assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key));
1354b2a4df20SDavid Howells 	assoc_array_apply_edit(*_edit);
1355b2a4df20SDavid Howells 	*_edit = NULL;
1356f70e2e06SDavid Howells }
1357f70e2e06SDavid Howells 
1358f70e2e06SDavid Howells /*
1359973c9f4fSDavid Howells  * Finish linking a key into to a keyring.
1360973c9f4fSDavid Howells  *
1361973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.
1362f70e2e06SDavid Howells  */
136316feef43SDavid Howells void __key_link_end(struct key *keyring,
136416feef43SDavid Howells 		    const struct keyring_index_key *index_key,
1365b2a4df20SDavid Howells 		    struct assoc_array_edit *edit)
1366f70e2e06SDavid Howells 	__releases(&keyring->sem)
13673be59f74SDavid Howells 	__releases(&keyring_serialise_link_lock)
1368f70e2e06SDavid Howells {
136916feef43SDavid Howells 	BUG_ON(index_key->type == NULL);
1370b2a4df20SDavid Howells 	kenter("%d,%s,", keyring->serial, index_key->type->name);
1371f70e2e06SDavid Howells 
1372ca4da5ddSColin Ian King 	if (edit) {
1373ca4da5ddSColin Ian King 		if (!edit->dead_leaf) {
1374f70e2e06SDavid Howells 			key_payload_reserve(keyring,
1375b2a4df20SDavid Howells 				keyring->datalen - KEYQUOTA_LINK_BYTES);
1376ca4da5ddSColin Ian King 		}
1377b2a4df20SDavid Howells 		assoc_array_cancel_edit(edit);
1378f70e2e06SDavid Howells 	}
1379f70e2e06SDavid Howells 	up_write(&keyring->sem);
1380df593ee2SDavid Howells 
1381df593ee2SDavid Howells 	if (index_key->type == &key_type_keyring)
1382df593ee2SDavid Howells 		mutex_unlock(&keyring_serialise_link_lock);
1383f70e2e06SDavid Howells }
1384f70e2e06SDavid Howells 
13855ac7eaceSDavid Howells /*
13865ac7eaceSDavid Howells  * Check addition of keys to restricted keyrings.
13875ac7eaceSDavid Howells  */
13885ac7eaceSDavid Howells static int __key_link_check_restriction(struct key *keyring, struct key *key)
13895ac7eaceSDavid Howells {
13902b6aa412SMat Martineau 	if (!keyring->restrict_link || !keyring->restrict_link->check)
13915ac7eaceSDavid Howells 		return 0;
13922b6aa412SMat Martineau 	return keyring->restrict_link->check(keyring, key->type, &key->payload,
13932b6aa412SMat Martineau 					     keyring->restrict_link->key);
13945ac7eaceSDavid Howells }
13955ac7eaceSDavid Howells 
1396973c9f4fSDavid Howells /**
1397973c9f4fSDavid Howells  * key_link - Link a key to a keyring
1398973c9f4fSDavid Howells  * @keyring: The keyring to make the link in.
1399973c9f4fSDavid Howells  * @key: The key to link to.
1400973c9f4fSDavid Howells  *
1401973c9f4fSDavid Howells  * Make a link in a keyring to a key, such that the keyring holds a reference
1402973c9f4fSDavid Howells  * on that key and the key can potentially be found by searching that keyring.
1403973c9f4fSDavid Howells  *
1404973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore and will consume some
1405973c9f4fSDavid Howells  * of the user's key data quota to hold the link.
1406973c9f4fSDavid Howells  *
1407973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
1408973c9f4fSDavid Howells  * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
1409973c9f4fSDavid Howells  * full, -EDQUOT if there is insufficient key data quota remaining to add
1410973c9f4fSDavid Howells  * another link or -ENOMEM if there's insufficient memory.
1411973c9f4fSDavid Howells  *
1412973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1413973c9f4fSDavid Howells  * be made (the keyring should have Write permission and the key Link
1414973c9f4fSDavid Howells  * permission).
14151da177e4SLinus Torvalds  */
14161da177e4SLinus Torvalds int key_link(struct key *keyring, struct key *key)
14171da177e4SLinus Torvalds {
1418df593ee2SDavid Howells 	struct assoc_array_edit *edit = NULL;
14191da177e4SLinus Torvalds 	int ret;
14201da177e4SLinus Torvalds 
1421fff29291SElena Reshetova 	kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage));
1422b2a4df20SDavid Howells 
14231da177e4SLinus Torvalds 	key_check(keyring);
14241da177e4SLinus Torvalds 	key_check(key);
14251da177e4SLinus Torvalds 
1426df593ee2SDavid Howells 	ret = __key_link_lock(keyring, &key->index_key);
1427df593ee2SDavid Howells 	if (ret < 0)
1428df593ee2SDavid Howells 		goto error;
1429df593ee2SDavid Howells 
1430b2a4df20SDavid Howells 	ret = __key_link_begin(keyring, &key->index_key, &edit);
1431df593ee2SDavid Howells 	if (ret < 0)
1432df593ee2SDavid Howells 		goto error_end;
1433df593ee2SDavid Howells 
1434fff29291SElena Reshetova 	kdebug("begun {%d,%d}", keyring->serial, refcount_read(&keyring->usage));
14355ac7eaceSDavid Howells 	ret = __key_link_check_restriction(keyring, key);
14365ac7eaceSDavid Howells 	if (ret == 0)
1437f70e2e06SDavid Howells 		ret = __key_link_check_live_key(keyring, key);
1438f70e2e06SDavid Howells 	if (ret == 0)
1439b2a4df20SDavid Howells 		__key_link(key, &edit);
14401da177e4SLinus Torvalds 
1441df593ee2SDavid Howells error_end:
1442df593ee2SDavid Howells 	__key_link_end(keyring, &key->index_key, edit);
1443df593ee2SDavid Howells error:
1444fff29291SElena Reshetova 	kleave(" = %d {%d,%d}", ret, keyring->serial, refcount_read(&keyring->usage));
14451da177e4SLinus Torvalds 	return ret;
1446f70e2e06SDavid Howells }
14471da177e4SLinus Torvalds EXPORT_SYMBOL(key_link);
14481da177e4SLinus Torvalds 
1449eb0f68cbSDavid Howells /*
1450eb0f68cbSDavid Howells  * Lock a keyring for unlink.
1451eb0f68cbSDavid Howells  */
1452eb0f68cbSDavid Howells static int __key_unlink_lock(struct key *keyring)
1453eb0f68cbSDavid Howells 	__acquires(&keyring->sem)
1454eb0f68cbSDavid Howells {
1455eb0f68cbSDavid Howells 	if (keyring->type != &key_type_keyring)
1456eb0f68cbSDavid Howells 		return -ENOTDIR;
1457eb0f68cbSDavid Howells 
1458eb0f68cbSDavid Howells 	down_write(&keyring->sem);
1459eb0f68cbSDavid Howells 	return 0;
1460eb0f68cbSDavid Howells }
1461eb0f68cbSDavid Howells 
1462eb0f68cbSDavid Howells /*
1463eb0f68cbSDavid Howells  * Begin the process of unlinking a key from a keyring.
1464eb0f68cbSDavid Howells  */
1465eb0f68cbSDavid Howells static int __key_unlink_begin(struct key *keyring, struct key *key,
1466eb0f68cbSDavid Howells 			      struct assoc_array_edit **_edit)
1467eb0f68cbSDavid Howells {
1468eb0f68cbSDavid Howells 	struct assoc_array_edit *edit;
1469eb0f68cbSDavid Howells 
1470eb0f68cbSDavid Howells 	BUG_ON(*_edit != NULL);
1471eb0f68cbSDavid Howells 
1472eb0f68cbSDavid Howells 	edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops,
1473eb0f68cbSDavid Howells 				  &key->index_key);
1474eb0f68cbSDavid Howells 	if (IS_ERR(edit))
1475eb0f68cbSDavid Howells 		return PTR_ERR(edit);
1476eb0f68cbSDavid Howells 
1477eb0f68cbSDavid Howells 	if (!edit)
1478eb0f68cbSDavid Howells 		return -ENOENT;
1479eb0f68cbSDavid Howells 
1480eb0f68cbSDavid Howells 	*_edit = edit;
1481eb0f68cbSDavid Howells 	return 0;
1482eb0f68cbSDavid Howells }
1483eb0f68cbSDavid Howells 
1484eb0f68cbSDavid Howells /*
1485eb0f68cbSDavid Howells  * Apply an unlink change.
1486eb0f68cbSDavid Howells  */
1487eb0f68cbSDavid Howells static void __key_unlink(struct key *keyring, struct key *key,
1488eb0f68cbSDavid Howells 			 struct assoc_array_edit **_edit)
1489eb0f68cbSDavid Howells {
1490eb0f68cbSDavid Howells 	assoc_array_apply_edit(*_edit);
1491eb0f68cbSDavid Howells 	*_edit = NULL;
1492eb0f68cbSDavid Howells 	key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES);
1493eb0f68cbSDavid Howells }
1494eb0f68cbSDavid Howells 
1495eb0f68cbSDavid Howells /*
1496eb0f68cbSDavid Howells  * Finish unlinking a key from to a keyring.
1497eb0f68cbSDavid Howells  */
1498eb0f68cbSDavid Howells static void __key_unlink_end(struct key *keyring,
1499eb0f68cbSDavid Howells 			     struct key *key,
1500eb0f68cbSDavid Howells 			     struct assoc_array_edit *edit)
1501eb0f68cbSDavid Howells 	__releases(&keyring->sem)
1502eb0f68cbSDavid Howells {
1503eb0f68cbSDavid Howells 	if (edit)
1504eb0f68cbSDavid Howells 		assoc_array_cancel_edit(edit);
1505eb0f68cbSDavid Howells 	up_write(&keyring->sem);
1506eb0f68cbSDavid Howells }
1507eb0f68cbSDavid Howells 
1508973c9f4fSDavid Howells /**
1509973c9f4fSDavid Howells  * key_unlink - Unlink the first link to a key from a keyring.
1510973c9f4fSDavid Howells  * @keyring: The keyring to remove the link from.
1511973c9f4fSDavid Howells  * @key: The key the link is to.
1512973c9f4fSDavid Howells  *
1513973c9f4fSDavid Howells  * Remove a link from a keyring to a key.
1514973c9f4fSDavid Howells  *
1515973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore.
1516973c9f4fSDavid Howells  *
1517973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
1518973c9f4fSDavid Howells  * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
1519973c9f4fSDavid Howells  * memory.
1520973c9f4fSDavid Howells  *
1521973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1522973c9f4fSDavid Howells  * be removed (the keyring should have Write permission; no permissions are
1523973c9f4fSDavid Howells  * required on the key).
15241da177e4SLinus Torvalds  */
15251da177e4SLinus Torvalds int key_unlink(struct key *keyring, struct key *key)
15261da177e4SLinus Torvalds {
1527eb0f68cbSDavid Howells 	struct assoc_array_edit *edit = NULL;
1528b2a4df20SDavid Howells 	int ret;
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds 	key_check(keyring);
15311da177e4SLinus Torvalds 	key_check(key);
15321da177e4SLinus Torvalds 
1533eb0f68cbSDavid Howells 	ret = __key_unlink_lock(keyring);
1534eb0f68cbSDavid Howells 	if (ret < 0)
1535eb0f68cbSDavid Howells 		return ret;
15361da177e4SLinus Torvalds 
1537eb0f68cbSDavid Howells 	ret = __key_unlink_begin(keyring, key, &edit);
1538eb0f68cbSDavid Howells 	if (ret == 0)
1539eb0f68cbSDavid Howells 		__key_unlink(keyring, key, &edit);
1540eb0f68cbSDavid Howells 	__key_unlink_end(keyring, key, edit);
1541b2a4df20SDavid Howells 	return ret;
1542a8b17ed0SDavid Howells }
15431da177e4SLinus Torvalds EXPORT_SYMBOL(key_unlink);
15441da177e4SLinus Torvalds 
1545973c9f4fSDavid Howells /**
1546ed0ac5c7SDavid Howells  * key_move - Move a key from one keyring to another
1547ed0ac5c7SDavid Howells  * @key: The key to move
1548ed0ac5c7SDavid Howells  * @from_keyring: The keyring to remove the link from.
1549ed0ac5c7SDavid Howells  * @to_keyring: The keyring to make the link in.
1550ed0ac5c7SDavid Howells  * @flags: Qualifying flags, such as KEYCTL_MOVE_EXCL.
1551ed0ac5c7SDavid Howells  *
1552ed0ac5c7SDavid Howells  * Make a link in @to_keyring to a key, such that the keyring holds a reference
1553ed0ac5c7SDavid Howells  * on that key and the key can potentially be found by searching that keyring
1554ed0ac5c7SDavid Howells  * whilst simultaneously removing a link to the key from @from_keyring.
1555ed0ac5c7SDavid Howells  *
1556ed0ac5c7SDavid Howells  * This function will write-lock both keyring's semaphores and will consume
1557ed0ac5c7SDavid Howells  * some of the user's key data quota to hold the link on @to_keyring.
1558ed0ac5c7SDavid Howells  *
1559ed0ac5c7SDavid Howells  * Returns 0 if successful, -ENOTDIR if either keyring isn't a keyring,
1560ed0ac5c7SDavid Howells  * -EKEYREVOKED if either keyring has been revoked, -ENFILE if the second
1561ed0ac5c7SDavid Howells  * keyring is full, -EDQUOT if there is insufficient key data quota remaining
1562ed0ac5c7SDavid Howells  * to add another link or -ENOMEM if there's insufficient memory.  If
1563ed0ac5c7SDavid Howells  * KEYCTL_MOVE_EXCL is set, then -EEXIST will be returned if there's already a
1564ed0ac5c7SDavid Howells  * matching key in @to_keyring.
1565ed0ac5c7SDavid Howells  *
1566ed0ac5c7SDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1567ed0ac5c7SDavid Howells  * be made (the keyring should have Write permission and the key Link
1568ed0ac5c7SDavid Howells  * permission).
1569ed0ac5c7SDavid Howells  */
1570ed0ac5c7SDavid Howells int key_move(struct key *key,
1571ed0ac5c7SDavid Howells 	     struct key *from_keyring,
1572ed0ac5c7SDavid Howells 	     struct key *to_keyring,
1573ed0ac5c7SDavid Howells 	     unsigned int flags)
1574ed0ac5c7SDavid Howells {
1575ed0ac5c7SDavid Howells 	struct assoc_array_edit *from_edit = NULL, *to_edit = NULL;
1576ed0ac5c7SDavid Howells 	int ret;
1577ed0ac5c7SDavid Howells 
1578ed0ac5c7SDavid Howells 	kenter("%d,%d,%d", key->serial, from_keyring->serial, to_keyring->serial);
1579ed0ac5c7SDavid Howells 
1580ed0ac5c7SDavid Howells 	if (from_keyring == to_keyring)
1581ed0ac5c7SDavid Howells 		return 0;
1582ed0ac5c7SDavid Howells 
1583ed0ac5c7SDavid Howells 	key_check(key);
1584ed0ac5c7SDavid Howells 	key_check(from_keyring);
1585ed0ac5c7SDavid Howells 	key_check(to_keyring);
1586ed0ac5c7SDavid Howells 
1587ed0ac5c7SDavid Howells 	ret = __key_move_lock(from_keyring, to_keyring, &key->index_key);
1588ed0ac5c7SDavid Howells 	if (ret < 0)
1589ed0ac5c7SDavid Howells 		goto out;
1590ed0ac5c7SDavid Howells 	ret = __key_unlink_begin(from_keyring, key, &from_edit);
1591ed0ac5c7SDavid Howells 	if (ret < 0)
1592ed0ac5c7SDavid Howells 		goto error;
1593ed0ac5c7SDavid Howells 	ret = __key_link_begin(to_keyring, &key->index_key, &to_edit);
1594ed0ac5c7SDavid Howells 	if (ret < 0)
1595ed0ac5c7SDavid Howells 		goto error;
1596ed0ac5c7SDavid Howells 
1597ed0ac5c7SDavid Howells 	ret = -EEXIST;
1598ed0ac5c7SDavid Howells 	if (to_edit->dead_leaf && (flags & KEYCTL_MOVE_EXCL))
1599ed0ac5c7SDavid Howells 		goto error;
1600ed0ac5c7SDavid Howells 
1601ed0ac5c7SDavid Howells 	ret = __key_link_check_restriction(to_keyring, key);
1602ed0ac5c7SDavid Howells 	if (ret < 0)
1603ed0ac5c7SDavid Howells 		goto error;
1604ed0ac5c7SDavid Howells 	ret = __key_link_check_live_key(to_keyring, key);
1605ed0ac5c7SDavid Howells 	if (ret < 0)
1606ed0ac5c7SDavid Howells 		goto error;
1607ed0ac5c7SDavid Howells 
1608ed0ac5c7SDavid Howells 	__key_unlink(from_keyring, key, &from_edit);
1609ed0ac5c7SDavid Howells 	__key_link(key, &to_edit);
1610ed0ac5c7SDavid Howells error:
1611ed0ac5c7SDavid Howells 	__key_link_end(to_keyring, &key->index_key, to_edit);
1612ed0ac5c7SDavid Howells 	__key_unlink_end(from_keyring, key, from_edit);
1613ed0ac5c7SDavid Howells out:
1614ed0ac5c7SDavid Howells 	kleave(" = %d", ret);
1615ed0ac5c7SDavid Howells 	return ret;
1616ed0ac5c7SDavid Howells }
1617ed0ac5c7SDavid Howells EXPORT_SYMBOL(key_move);
1618ed0ac5c7SDavid Howells 
1619ed0ac5c7SDavid Howells /**
1620973c9f4fSDavid Howells  * keyring_clear - Clear a keyring
1621973c9f4fSDavid Howells  * @keyring: The keyring to clear.
1622973c9f4fSDavid Howells  *
1623973c9f4fSDavid Howells  * Clear the contents of the specified keyring.
1624973c9f4fSDavid Howells  *
1625973c9f4fSDavid Howells  * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
16261da177e4SLinus Torvalds  */
16271da177e4SLinus Torvalds int keyring_clear(struct key *keyring)
16281da177e4SLinus Torvalds {
1629b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
163076d8aeabSDavid Howells 	int ret;
16311da177e4SLinus Torvalds 
1632b2a4df20SDavid Howells 	if (keyring->type != &key_type_keyring)
1633b2a4df20SDavid Howells 		return -ENOTDIR;
1634b2a4df20SDavid Howells 
16351da177e4SLinus Torvalds 	down_write(&keyring->sem);
16361da177e4SLinus Torvalds 
1637b2a4df20SDavid Howells 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1638b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1639b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1640b2a4df20SDavid Howells 	} else {
1641b2a4df20SDavid Howells 		if (edit)
1642b2a4df20SDavid Howells 			assoc_array_apply_edit(edit);
1643b2a4df20SDavid Howells 		key_payload_reserve(keyring, 0);
16441da177e4SLinus Torvalds 		ret = 0;
16451da177e4SLinus Torvalds 	}
16461da177e4SLinus Torvalds 
1647b2a4df20SDavid Howells 	up_write(&keyring->sem);
16481da177e4SLinus Torvalds 	return ret;
1649a8b17ed0SDavid Howells }
16501da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_clear);
165131204ed9SDavid Howells 
165231204ed9SDavid Howells /*
1653973c9f4fSDavid Howells  * Dispose of the links from a revoked keyring.
1654973c9f4fSDavid Howells  *
1655973c9f4fSDavid Howells  * This is called with the key sem write-locked.
165631204ed9SDavid Howells  */
165731204ed9SDavid Howells static void keyring_revoke(struct key *keyring)
165831204ed9SDavid Howells {
1659b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1660f0641cbaSDavid Howells 
1661b2a4df20SDavid Howells 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1662b2a4df20SDavid Howells 	if (!IS_ERR(edit)) {
1663b2a4df20SDavid Howells 		if (edit)
1664b2a4df20SDavid Howells 			assoc_array_apply_edit(edit);
166531204ed9SDavid Howells 		key_payload_reserve(keyring, 0);
166631204ed9SDavid Howells 	}
1667a8b17ed0SDavid Howells }
16685d135440SDavid Howells 
166962fe3182SDavid Howells static bool keyring_gc_select_iterator(void *object, void *iterator_data)
1670b2a4df20SDavid Howells {
1671b2a4df20SDavid Howells 	struct key *key = keyring_ptr_to_key(object);
1672074d5898SBaolin Wang 	time64_t *limit = iterator_data;
1673b2a4df20SDavid Howells 
1674b2a4df20SDavid Howells 	if (key_is_dead(key, *limit))
1675b2a4df20SDavid Howells 		return false;
1676b2a4df20SDavid Howells 	key_get(key);
1677b2a4df20SDavid Howells 	return true;
1678b2a4df20SDavid Howells }
1679b2a4df20SDavid Howells 
168062fe3182SDavid Howells static int keyring_gc_check_iterator(const void *object, void *iterator_data)
168162fe3182SDavid Howells {
168262fe3182SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
1683074d5898SBaolin Wang 	time64_t *limit = iterator_data;
168462fe3182SDavid Howells 
168562fe3182SDavid Howells 	key_check(key);
168662fe3182SDavid Howells 	return key_is_dead(key, *limit);
168762fe3182SDavid Howells }
168862fe3182SDavid Howells 
16895d135440SDavid Howells /*
169062fe3182SDavid Howells  * Garbage collect pointers from a keyring.
1691973c9f4fSDavid Howells  *
169262fe3182SDavid Howells  * Not called with any locks held.  The keyring's key struct will not be
169362fe3182SDavid Howells  * deallocated under us as only our caller may deallocate it.
16945d135440SDavid Howells  */
1695074d5898SBaolin Wang void keyring_gc(struct key *keyring, time64_t limit)
16965d135440SDavid Howells {
169762fe3182SDavid Howells 	int result;
16985d135440SDavid Howells 
169962fe3182SDavid Howells 	kenter("%x{%s}", keyring->serial, keyring->description ?: "");
170062fe3182SDavid Howells 
170162fe3182SDavid Howells 	if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
170262fe3182SDavid Howells 			      (1 << KEY_FLAG_REVOKED)))
170362fe3182SDavid Howells 		goto dont_gc;
170462fe3182SDavid Howells 
170562fe3182SDavid Howells 	/* scan the keyring looking for dead keys */
170662fe3182SDavid Howells 	rcu_read_lock();
170762fe3182SDavid Howells 	result = assoc_array_iterate(&keyring->keys,
170862fe3182SDavid Howells 				     keyring_gc_check_iterator, &limit);
170962fe3182SDavid Howells 	rcu_read_unlock();
171062fe3182SDavid Howells 	if (result == true)
171162fe3182SDavid Howells 		goto do_gc;
171262fe3182SDavid Howells 
171362fe3182SDavid Howells dont_gc:
171462fe3182SDavid Howells 	kleave(" [no gc]");
171562fe3182SDavid Howells 	return;
171662fe3182SDavid Howells 
171762fe3182SDavid Howells do_gc:
17185d135440SDavid Howells 	down_write(&keyring->sem);
1719b2a4df20SDavid Howells 	assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops,
172062fe3182SDavid Howells 		       keyring_gc_select_iterator, &limit);
17215d135440SDavid Howells 	up_write(&keyring->sem);
172262fe3182SDavid Howells 	kleave(" [gc]");
17235d135440SDavid Howells }
17242b6aa412SMat Martineau 
17252b6aa412SMat Martineau /*
17262b6aa412SMat Martineau  * Garbage collect restriction pointers from a keyring.
17272b6aa412SMat Martineau  *
17282b6aa412SMat Martineau  * Keyring restrictions are associated with a key type, and must be cleaned
17292b6aa412SMat Martineau  * up if the key type is unregistered. The restriction is altered to always
17302b6aa412SMat Martineau  * reject additional keys so a keyring cannot be opened up by unregistering
17312b6aa412SMat Martineau  * a key type.
17322b6aa412SMat Martineau  *
17332b6aa412SMat Martineau  * Not called with any keyring locks held. The keyring's key struct will not
17342b6aa412SMat Martineau  * be deallocated under us as only our caller may deallocate it.
17352b6aa412SMat Martineau  *
17362b6aa412SMat Martineau  * The caller is required to hold key_types_sem and dead_type->sem. This is
17372b6aa412SMat Martineau  * fulfilled by key_gc_keytype() holding the locks on behalf of
17382b6aa412SMat Martineau  * key_garbage_collector(), which it invokes on a workqueue.
17392b6aa412SMat Martineau  */
17402b6aa412SMat Martineau void keyring_restriction_gc(struct key *keyring, struct key_type *dead_type)
17412b6aa412SMat Martineau {
17422b6aa412SMat Martineau 	struct key_restriction *keyres;
17432b6aa412SMat Martineau 
17442b6aa412SMat Martineau 	kenter("%x{%s}", keyring->serial, keyring->description ?: "");
17452b6aa412SMat Martineau 
17462b6aa412SMat Martineau 	/*
17472b6aa412SMat Martineau 	 * keyring->restrict_link is only assigned at key allocation time
17482b6aa412SMat Martineau 	 * or with the key type locked, so the only values that could be
17492b6aa412SMat Martineau 	 * concurrently assigned to keyring->restrict_link are for key
17502b6aa412SMat Martineau 	 * types other than dead_type. Given this, it's ok to check
17512b6aa412SMat Martineau 	 * the key type before acquiring keyring->sem.
17522b6aa412SMat Martineau 	 */
17532b6aa412SMat Martineau 	if (!dead_type || !keyring->restrict_link ||
17542b6aa412SMat Martineau 	    keyring->restrict_link->keytype != dead_type) {
17552b6aa412SMat Martineau 		kleave(" [no restriction gc]");
17562b6aa412SMat Martineau 		return;
17572b6aa412SMat Martineau 	}
17582b6aa412SMat Martineau 
17592b6aa412SMat Martineau 	/* Lock the keyring to ensure that a link is not in progress */
17602b6aa412SMat Martineau 	down_write(&keyring->sem);
17612b6aa412SMat Martineau 
17622b6aa412SMat Martineau 	keyres = keyring->restrict_link;
17632b6aa412SMat Martineau 
17642b6aa412SMat Martineau 	keyres->check = restrict_link_reject;
17652b6aa412SMat Martineau 
17662b6aa412SMat Martineau 	key_put(keyres->key);
17672b6aa412SMat Martineau 	keyres->key = NULL;
17682b6aa412SMat Martineau 	keyres->keytype = NULL;
17692b6aa412SMat Martineau 
17702b6aa412SMat Martineau 	up_write(&keyring->sem);
17712b6aa412SMat Martineau 
17722b6aa412SMat Martineau 	kleave(" [restriction gc]");
17732b6aa412SMat Martineau }
1774