xref: /openbmc/linux/security/keys/keyring.c (revision 0f44e4d976f96c6439da0d6717238efa4b91196e)
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 
65*0f44e4d9SDavid 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);
178f771fde8SDavid Howells 
179b2a4df20SDavid Howells 	for (;;) {
180b2a4df20SDavid Howells 		n = desc_len;
181b2a4df20SDavid Howells 		if (n <= 0)
182b2a4df20SDavid Howells 			break;
183b2a4df20SDavid Howells 		if (n > 4)
184b2a4df20SDavid Howells 			n = 4;
185b2a4df20SDavid Howells 		piece = 0;
186b2a4df20SDavid Howells 		memcpy(&piece, description, n);
187b2a4df20SDavid Howells 		description += n;
188b2a4df20SDavid Howells 		desc_len -= n;
189b2a4df20SDavid Howells 		acc = mult_64x32_and_fold(acc, piece);
190b2a4df20SDavid Howells 		acc = mult_64x32_and_fold(acc, 9207);
191b2a4df20SDavid Howells 	}
192b2a4df20SDavid Howells 
193b2a4df20SDavid Howells 	/* Fold the hash down to 32 bits if need be. */
194b2a4df20SDavid Howells 	hash = acc;
195b2a4df20SDavid Howells 	if (ASSOC_ARRAY_KEY_CHUNK_SIZE == 32)
196b2a4df20SDavid Howells 		hash ^= acc >> 32;
197b2a4df20SDavid Howells 
198b2a4df20SDavid Howells 	/* Squidge all the keyrings into a separate part of the tree to
199b2a4df20SDavid Howells 	 * ordinary keys by making sure the lowest level segment in the hash is
200b2a4df20SDavid Howells 	 * zero for keyrings and non-zero otherwise.
201b2a4df20SDavid Howells 	 */
202d54e58b7SDavid Howells 	if (index_key->type != &key_type_keyring && (hash & fan_mask) == 0)
203355ef8e1SDavid Howells 		hash |= (hash >> (ASSOC_ARRAY_KEY_CHUNK_SIZE - level_shift)) | 1;
204355ef8e1SDavid Howells 	else if (index_key->type == &key_type_keyring && (hash & fan_mask) != 0)
205355ef8e1SDavid Howells 		hash = (hash + (hash << level_shift)) & ~fan_mask;
206355ef8e1SDavid Howells 	index_key->hash = hash;
207355ef8e1SDavid Howells }
208355ef8e1SDavid Howells 
209355ef8e1SDavid Howells /*
210355ef8e1SDavid Howells  * Finalise an index key to include a part of the description actually in the
211355ef8e1SDavid Howells  * index key and to add in the hash too.
212355ef8e1SDavid Howells  */
213355ef8e1SDavid Howells void key_set_index_key(struct keyring_index_key *index_key)
214355ef8e1SDavid Howells {
215355ef8e1SDavid Howells 	size_t n = min_t(size_t, index_key->desc_len, sizeof(index_key->desc));
216355ef8e1SDavid Howells 	memcpy(index_key->desc, index_key->description, n);
217355ef8e1SDavid Howells 
218355ef8e1SDavid Howells 	hash_key_type_and_desc(index_key);
219b2a4df20SDavid Howells }
220b2a4df20SDavid Howells 
221b2a4df20SDavid Howells /*
222b2a4df20SDavid Howells  * Build the next index key chunk.
223b2a4df20SDavid Howells  *
224b2a4df20SDavid Howells  * We return it one word-sized chunk at a time.
225b2a4df20SDavid Howells  */
226b2a4df20SDavid Howells static unsigned long keyring_get_key_chunk(const void *data, int level)
227b2a4df20SDavid Howells {
228b2a4df20SDavid Howells 	const struct keyring_index_key *index_key = data;
229b2a4df20SDavid Howells 	unsigned long chunk = 0;
230f771fde8SDavid Howells 	const u8 *d;
231b2a4df20SDavid Howells 	int desc_len = index_key->desc_len, n = sizeof(chunk);
232b2a4df20SDavid Howells 
233b2a4df20SDavid Howells 	level /= ASSOC_ARRAY_KEY_CHUNK_SIZE;
234b2a4df20SDavid Howells 	switch (level) {
235b2a4df20SDavid Howells 	case 0:
236355ef8e1SDavid Howells 		return index_key->hash;
237b2a4df20SDavid Howells 	case 1:
238f771fde8SDavid Howells 		return index_key->x;
239b2a4df20SDavid Howells 	case 2:
240f771fde8SDavid Howells 		return (unsigned long)index_key->type;
241b2a4df20SDavid Howells 	default:
242f771fde8SDavid Howells 		level -= 3;
243f771fde8SDavid Howells 		if (desc_len <= sizeof(index_key->desc))
244b2a4df20SDavid Howells 			return 0;
245f771fde8SDavid Howells 
246f771fde8SDavid Howells 		d = index_key->description + sizeof(index_key->desc);
247f771fde8SDavid Howells 		d += level * sizeof(long);
248f771fde8SDavid Howells 		desc_len -= sizeof(index_key->desc);
249b2a4df20SDavid Howells 		if (desc_len > n)
250b2a4df20SDavid Howells 			desc_len = n;
251b2a4df20SDavid Howells 		do {
252b2a4df20SDavid Howells 			chunk <<= 8;
253f771fde8SDavid Howells 			chunk |= *d++;
254b2a4df20SDavid Howells 		} while (--desc_len > 0);
255b2a4df20SDavid Howells 		return chunk;
256b2a4df20SDavid Howells 	}
257b2a4df20SDavid Howells }
258b2a4df20SDavid Howells 
259b2a4df20SDavid Howells static unsigned long keyring_get_object_key_chunk(const void *object, int level)
260b2a4df20SDavid Howells {
261b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
262b2a4df20SDavid Howells 	return keyring_get_key_chunk(&key->index_key, level);
263b2a4df20SDavid Howells }
264b2a4df20SDavid Howells 
265b2a4df20SDavid Howells static bool keyring_compare_object(const void *object, const void *data)
266b2a4df20SDavid Howells {
267b2a4df20SDavid Howells 	const struct keyring_index_key *index_key = data;
268b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
269b2a4df20SDavid Howells 
270b2a4df20SDavid Howells 	return key->index_key.type == index_key->type &&
271b2a4df20SDavid Howells 		key->index_key.desc_len == index_key->desc_len &&
272b2a4df20SDavid Howells 		memcmp(key->index_key.description, index_key->description,
273b2a4df20SDavid Howells 		       index_key->desc_len) == 0;
274b2a4df20SDavid Howells }
275b2a4df20SDavid Howells 
276b2a4df20SDavid Howells /*
277b2a4df20SDavid Howells  * Compare the index keys of a pair of objects and determine the bit position
278b2a4df20SDavid Howells  * at which they differ - if they differ.
279b2a4df20SDavid Howells  */
28023fd78d7SDavid Howells static int keyring_diff_objects(const void *object, const void *data)
281b2a4df20SDavid Howells {
28223fd78d7SDavid Howells 	const struct key *key_a = keyring_ptr_to_key(object);
283b2a4df20SDavid Howells 	const struct keyring_index_key *a = &key_a->index_key;
28423fd78d7SDavid Howells 	const struct keyring_index_key *b = data;
285b2a4df20SDavid Howells 	unsigned long seg_a, seg_b;
286b2a4df20SDavid Howells 	int level, i;
287b2a4df20SDavid Howells 
288b2a4df20SDavid Howells 	level = 0;
289355ef8e1SDavid Howells 	seg_a = a->hash;
290355ef8e1SDavid Howells 	seg_b = b->hash;
291b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
292b2a4df20SDavid Howells 		goto differ;
293f771fde8SDavid Howells 	level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8;
294b2a4df20SDavid Howells 
295b2a4df20SDavid Howells 	/* The number of bits contributed by the hash is controlled by a
296b2a4df20SDavid Howells 	 * constant in the assoc_array headers.  Everything else thereafter we
297b2a4df20SDavid Howells 	 * can deal with as being machine word-size dependent.
298b2a4df20SDavid Howells 	 */
299f771fde8SDavid Howells 	seg_a = a->x;
300f771fde8SDavid Howells 	seg_b = b->x;
301b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
302b2a4df20SDavid Howells 		goto differ;
303f771fde8SDavid Howells 	level += sizeof(unsigned long);
304b2a4df20SDavid Howells 
305b2a4df20SDavid Howells 	/* The next bit may not work on big endian */
306b2a4df20SDavid Howells 	seg_a = (unsigned long)a->type;
307b2a4df20SDavid Howells 	seg_b = (unsigned long)b->type;
308b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
309b2a4df20SDavid Howells 		goto differ;
310b2a4df20SDavid Howells 	level += sizeof(unsigned long);
311b2a4df20SDavid Howells 
312f771fde8SDavid Howells 	i = sizeof(a->desc);
313f771fde8SDavid Howells 	if (a->desc_len <= i)
314f771fde8SDavid Howells 		goto same;
315b2a4df20SDavid Howells 
316b2a4df20SDavid Howells 	for (; i < a->desc_len; i++) {
317b2a4df20SDavid Howells 		seg_a = *(unsigned char *)(a->description + i);
318b2a4df20SDavid Howells 		seg_b = *(unsigned char *)(b->description + i);
319b2a4df20SDavid Howells 		if ((seg_a ^ seg_b) != 0)
320b2a4df20SDavid Howells 			goto differ_plus_i;
321b2a4df20SDavid Howells 	}
322b2a4df20SDavid Howells 
323b2a4df20SDavid Howells same:
324b2a4df20SDavid Howells 	return -1;
325b2a4df20SDavid Howells 
326b2a4df20SDavid Howells differ_plus_i:
327b2a4df20SDavid Howells 	level += i;
328b2a4df20SDavid Howells differ:
329b2a4df20SDavid Howells 	i = level * 8 + __ffs(seg_a ^ seg_b);
330b2a4df20SDavid Howells 	return i;
331b2a4df20SDavid Howells }
332b2a4df20SDavid Howells 
333b2a4df20SDavid Howells /*
334b2a4df20SDavid Howells  * Free an object after stripping the keyring flag off of the pointer.
335b2a4df20SDavid Howells  */
336b2a4df20SDavid Howells static void keyring_free_object(void *object)
337b2a4df20SDavid Howells {
338b2a4df20SDavid Howells 	key_put(keyring_ptr_to_key(object));
339b2a4df20SDavid Howells }
340b2a4df20SDavid Howells 
341b2a4df20SDavid Howells /*
342b2a4df20SDavid Howells  * Operations for keyring management by the index-tree routines.
343b2a4df20SDavid Howells  */
344b2a4df20SDavid Howells static const struct assoc_array_ops keyring_assoc_array_ops = {
345b2a4df20SDavid Howells 	.get_key_chunk		= keyring_get_key_chunk,
346b2a4df20SDavid Howells 	.get_object_key_chunk	= keyring_get_object_key_chunk,
347b2a4df20SDavid Howells 	.compare_object		= keyring_compare_object,
348b2a4df20SDavid Howells 	.diff_objects		= keyring_diff_objects,
349b2a4df20SDavid Howells 	.free_object		= keyring_free_object,
350b2a4df20SDavid Howells };
351b2a4df20SDavid Howells 
352b2a4df20SDavid Howells /*
353973c9f4fSDavid Howells  * Clean up a keyring when it is destroyed.  Unpublish its name if it had one
354973c9f4fSDavid Howells  * and dispose of its data.
355233e4735SDavid Howells  *
356233e4735SDavid Howells  * The garbage collector detects the final key_put(), removes the keyring from
357233e4735SDavid Howells  * the serial number tree and then does RCU synchronisation before coming here,
358233e4735SDavid Howells  * so we shouldn't need to worry about code poking around here with the RCU
359233e4735SDavid Howells  * readlock held by this time.
3601da177e4SLinus Torvalds  */
3611da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring)
3621da177e4SLinus Torvalds {
3631da177e4SLinus Torvalds 	if (keyring->description) {
3641da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
36594efe72fSDavid Howells 
366146aa8b1SDavid Howells 		if (keyring->name_link.next != NULL &&
367146aa8b1SDavid Howells 		    !list_empty(&keyring->name_link))
368146aa8b1SDavid Howells 			list_del(&keyring->name_link);
36994efe72fSDavid Howells 
3701da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds 
3732b6aa412SMat Martineau 	if (keyring->restrict_link) {
3742b6aa412SMat Martineau 		struct key_restriction *keyres = keyring->restrict_link;
3752b6aa412SMat Martineau 
3762b6aa412SMat Martineau 		key_put(keyres->key);
3772b6aa412SMat Martineau 		kfree(keyres);
3782b6aa412SMat Martineau 	}
3792b6aa412SMat Martineau 
380b2a4df20SDavid Howells 	assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops);
381a8b17ed0SDavid Howells }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds /*
384973c9f4fSDavid Howells  * Describe a keyring for /proc.
3851da177e4SLinus Torvalds  */
3861da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m)
3871da177e4SLinus Torvalds {
388c8563473Swzt.wzt@gmail.com 	if (keyring->description)
3891da177e4SLinus Torvalds 		seq_puts(m, keyring->description);
390c8563473Swzt.wzt@gmail.com 	else
3911da177e4SLinus Torvalds 		seq_puts(m, "[anon]");
3921da177e4SLinus Torvalds 
393363b02daSDavid Howells 	if (key_is_positive(keyring)) {
394b2a4df20SDavid Howells 		if (keyring->keys.nr_leaves_on_tree != 0)
395b2a4df20SDavid Howells 			seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree);
3961da177e4SLinus Torvalds 		else
3971da177e4SLinus Torvalds 			seq_puts(m, ": empty");
398a8b17ed0SDavid Howells 	}
39978b7280cSDavid Howells }
4001da177e4SLinus Torvalds 
401b2a4df20SDavid Howells struct keyring_read_iterator_context {
402e645016aSEric Biggers 	size_t			buflen;
403b2a4df20SDavid Howells 	size_t			count;
404b2a4df20SDavid Howells 	key_serial_t __user	*buffer;
405b2a4df20SDavid Howells };
406b2a4df20SDavid Howells 
407b2a4df20SDavid Howells static int keyring_read_iterator(const void *object, void *data)
408b2a4df20SDavid Howells {
409b2a4df20SDavid Howells 	struct keyring_read_iterator_context *ctx = data;
410b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
411b2a4df20SDavid Howells 	int ret;
412b2a4df20SDavid Howells 
413b2a4df20SDavid Howells 	kenter("{%s,%d},,{%zu/%zu}",
414e645016aSEric Biggers 	       key->type->name, key->serial, ctx->count, ctx->buflen);
415b2a4df20SDavid Howells 
416e645016aSEric Biggers 	if (ctx->count >= ctx->buflen)
417b2a4df20SDavid Howells 		return 1;
418b2a4df20SDavid Howells 
419b2a4df20SDavid Howells 	ret = put_user(key->serial, ctx->buffer);
420b2a4df20SDavid Howells 	if (ret < 0)
421b2a4df20SDavid Howells 		return ret;
422b2a4df20SDavid Howells 	ctx->buffer++;
423b2a4df20SDavid Howells 	ctx->count += sizeof(key->serial);
424b2a4df20SDavid Howells 	return 0;
425b2a4df20SDavid Howells }
426b2a4df20SDavid Howells 
4271da177e4SLinus Torvalds /*
428973c9f4fSDavid Howells  * Read a list of key IDs from the keyring's contents in binary form
429973c9f4fSDavid Howells  *
430b2a4df20SDavid Howells  * The keyring's semaphore is read-locked by the caller.  This prevents someone
431b2a4df20SDavid Howells  * from modifying it under us - which could cause us to read key IDs multiple
432b2a4df20SDavid Howells  * times.
4331da177e4SLinus Torvalds  */
4341da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
4351da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen)
4361da177e4SLinus Torvalds {
437b2a4df20SDavid Howells 	struct keyring_read_iterator_context ctx;
4383239b6f2SEric Biggers 	long ret;
4391da177e4SLinus Torvalds 
440b2a4df20SDavid Howells 	kenter("{%d},,%zu", key_serial(keyring), buflen);
4411da177e4SLinus Torvalds 
442b2a4df20SDavid Howells 	if (buflen & (sizeof(key_serial_t) - 1))
443b2a4df20SDavid Howells 		return -EINVAL;
4441da177e4SLinus Torvalds 
4453239b6f2SEric Biggers 	/* Copy as many key IDs as fit into the buffer */
4463239b6f2SEric Biggers 	if (buffer && buflen) {
447b2a4df20SDavid Howells 		ctx.buffer = (key_serial_t __user *)buffer;
448e645016aSEric Biggers 		ctx.buflen = buflen;
449b2a4df20SDavid Howells 		ctx.count = 0;
4503239b6f2SEric Biggers 		ret = assoc_array_iterate(&keyring->keys,
4513239b6f2SEric Biggers 					  keyring_read_iterator, &ctx);
452b2a4df20SDavid Howells 		if (ret < 0) {
4533239b6f2SEric Biggers 			kleave(" = %ld [iterate]", ret);
4541da177e4SLinus Torvalds 			return ret;
455a8b17ed0SDavid Howells 		}
4563239b6f2SEric Biggers 	}
4571da177e4SLinus Torvalds 
4583239b6f2SEric Biggers 	/* Return the size of the buffer needed */
4593239b6f2SEric Biggers 	ret = keyring->keys.nr_leaves_on_tree * sizeof(key_serial_t);
4603239b6f2SEric Biggers 	if (ret <= buflen)
4613239b6f2SEric Biggers 		kleave("= %ld [ok]", ret);
4623239b6f2SEric Biggers 	else
4633239b6f2SEric Biggers 		kleave("= %ld [buffer too small]", ret);
4643239b6f2SEric Biggers 	return ret;
465b2a4df20SDavid Howells }
466b2a4df20SDavid Howells 
4671da177e4SLinus Torvalds /*
468973c9f4fSDavid Howells  * Allocate a keyring and link into the destination keyring.
4691da177e4SLinus Torvalds  */
4709a56c2dbSEric W. Biederman struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
47196b5c8feSDavid Howells 			  const struct cred *cred, key_perm_t perm,
4725ac7eaceSDavid Howells 			  unsigned long flags,
4732b6aa412SMat Martineau 			  struct key_restriction *restrict_link,
4745ac7eaceSDavid Howells 			  struct key *dest)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds 	struct key *keyring;
4771da177e4SLinus Torvalds 	int ret;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	keyring = key_alloc(&key_type_keyring, description,
4805ac7eaceSDavid Howells 			    uid, gid, cred, perm, flags, restrict_link);
4811da177e4SLinus Torvalds 	if (!IS_ERR(keyring)) {
4823e30148cSDavid Howells 		ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
4831da177e4SLinus Torvalds 		if (ret < 0) {
4841da177e4SLinus Torvalds 			key_put(keyring);
4851da177e4SLinus Torvalds 			keyring = ERR_PTR(ret);
4861da177e4SLinus Torvalds 		}
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	return keyring;
490a8b17ed0SDavid Howells }
491f8aa23a5SDavid Howells EXPORT_SYMBOL(keyring_alloc);
4921da177e4SLinus Torvalds 
4935ac7eaceSDavid Howells /**
4945ac7eaceSDavid Howells  * restrict_link_reject - Give -EPERM to restrict link
4955ac7eaceSDavid Howells  * @keyring: The keyring being added to.
4965ac7eaceSDavid Howells  * @type: The type of key being added.
4975ac7eaceSDavid Howells  * @payload: The payload of the key intended to be added.
4989fd16537SDavid Howells  * @restriction_key: Keys providing additional data for evaluating restriction.
4995ac7eaceSDavid Howells  *
5005ac7eaceSDavid Howells  * Reject the addition of any links to a keyring.  It can be overridden by
5015ac7eaceSDavid Howells  * passing KEY_ALLOC_BYPASS_RESTRICTION to key_instantiate_and_link() when
5025ac7eaceSDavid Howells  * adding a key to a keyring.
5035ac7eaceSDavid Howells  *
5042b6aa412SMat Martineau  * This is meant to be stored in a key_restriction structure which is passed
5052b6aa412SMat Martineau  * in the restrict_link parameter to keyring_alloc().
5065ac7eaceSDavid Howells  */
5075ac7eaceSDavid Howells int restrict_link_reject(struct key *keyring,
5085ac7eaceSDavid Howells 			 const struct key_type *type,
509aaf66c88SMat Martineau 			 const union key_payload *payload,
510aaf66c88SMat Martineau 			 struct key *restriction_key)
5115ac7eaceSDavid Howells {
5125ac7eaceSDavid Howells 	return -EPERM;
5135ac7eaceSDavid Howells }
5145ac7eaceSDavid Howells 
515b2a4df20SDavid Howells /*
516c06cfb08SDavid Howells  * By default, we keys found by getting an exact match on their descriptions.
517c06cfb08SDavid Howells  */
5180c903ab6SDavid Howells bool key_default_cmp(const struct key *key,
519c06cfb08SDavid Howells 		     const struct key_match_data *match_data)
520c06cfb08SDavid Howells {
521c06cfb08SDavid Howells 	return strcmp(key->description, match_data->raw_data) == 0;
522c06cfb08SDavid Howells }
523c06cfb08SDavid Howells 
524c06cfb08SDavid Howells /*
525b2a4df20SDavid Howells  * Iteration function to consider each key found.
526b2a4df20SDavid Howells  */
527b2a4df20SDavid Howells static int keyring_search_iterator(const void *object, void *iterator_data)
528b2a4df20SDavid Howells {
529b2a4df20SDavid Howells 	struct keyring_search_context *ctx = iterator_data;
530b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
531363b02daSDavid Howells 	unsigned long kflags = READ_ONCE(key->flags);
532363b02daSDavid Howells 	short state = READ_ONCE(key->state);
533b2a4df20SDavid Howells 
534b2a4df20SDavid Howells 	kenter("{%d}", key->serial);
535b2a4df20SDavid Howells 
536b2a4df20SDavid Howells 	/* ignore keys not of this type */
537b2a4df20SDavid Howells 	if (key->type != ctx->index_key.type) {
538b2a4df20SDavid Howells 		kleave(" = 0 [!type]");
539b2a4df20SDavid Howells 		return 0;
540b2a4df20SDavid Howells 	}
541b2a4df20SDavid Howells 
542b2a4df20SDavid Howells 	/* skip invalidated, revoked and expired keys */
543b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
544074d5898SBaolin Wang 		time64_t expiry = READ_ONCE(key->expiry);
5459d6c8711SEric Biggers 
546b2a4df20SDavid Howells 		if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
547b2a4df20SDavid Howells 			      (1 << KEY_FLAG_REVOKED))) {
548b2a4df20SDavid Howells 			ctx->result = ERR_PTR(-EKEYREVOKED);
549b2a4df20SDavid Howells 			kleave(" = %d [invrev]", ctx->skipped_ret);
550b2a4df20SDavid Howells 			goto skipped;
551b2a4df20SDavid Howells 		}
552b2a4df20SDavid Howells 
553074d5898SBaolin Wang 		if (expiry && ctx->now >= expiry) {
5540b0a8415SDavid Howells 			if (!(ctx->flags & KEYRING_SEARCH_SKIP_EXPIRED))
555b2a4df20SDavid Howells 				ctx->result = ERR_PTR(-EKEYEXPIRED);
556b2a4df20SDavid Howells 			kleave(" = %d [expire]", ctx->skipped_ret);
557b2a4df20SDavid Howells 			goto skipped;
558b2a4df20SDavid Howells 		}
559b2a4df20SDavid Howells 	}
560b2a4df20SDavid Howells 
561b2a4df20SDavid Howells 	/* keys that don't match */
56246291959SDavid Howells 	if (!ctx->match_data.cmp(key, &ctx->match_data)) {
563b2a4df20SDavid Howells 		kleave(" = 0 [!match]");
564b2a4df20SDavid Howells 		return 0;
565b2a4df20SDavid Howells 	}
566b2a4df20SDavid Howells 
567b2a4df20SDavid Howells 	/* key must have search permissions */
568b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
569b2a4df20SDavid Howells 	    key_task_permission(make_key_ref(key, ctx->possessed),
570f5895943SDavid Howells 				ctx->cred, KEY_NEED_SEARCH) < 0) {
571b2a4df20SDavid Howells 		ctx->result = ERR_PTR(-EACCES);
572b2a4df20SDavid Howells 		kleave(" = %d [!perm]", ctx->skipped_ret);
573b2a4df20SDavid Howells 		goto skipped;
574b2a4df20SDavid Howells 	}
575b2a4df20SDavid Howells 
576b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
577b2a4df20SDavid Howells 		/* we set a different error code if we pass a negative key */
578363b02daSDavid Howells 		if (state < 0) {
579363b02daSDavid Howells 			ctx->result = ERR_PTR(state);
580b2a4df20SDavid Howells 			kleave(" = %d [neg]", ctx->skipped_ret);
581b2a4df20SDavid Howells 			goto skipped;
582b2a4df20SDavid Howells 		}
583b2a4df20SDavid Howells 	}
584b2a4df20SDavid Howells 
585b2a4df20SDavid Howells 	/* Found */
586b2a4df20SDavid Howells 	ctx->result = make_key_ref(key, ctx->possessed);
587b2a4df20SDavid Howells 	kleave(" = 1 [found]");
588b2a4df20SDavid Howells 	return 1;
589b2a4df20SDavid Howells 
590b2a4df20SDavid Howells skipped:
591b2a4df20SDavid Howells 	return ctx->skipped_ret;
592b2a4df20SDavid Howells }
593b2a4df20SDavid Howells 
594b2a4df20SDavid Howells /*
595b2a4df20SDavid Howells  * Search inside a keyring for a key.  We can search by walking to it
596b2a4df20SDavid Howells  * directly based on its index-key or we can iterate over the entire
597b2a4df20SDavid Howells  * tree looking for it, based on the match function.
598b2a4df20SDavid Howells  */
599b2a4df20SDavid Howells static int search_keyring(struct key *keyring, struct keyring_search_context *ctx)
600b2a4df20SDavid Howells {
60146291959SDavid Howells 	if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_DIRECT) {
602b2a4df20SDavid Howells 		const void *object;
603b2a4df20SDavid Howells 
604b2a4df20SDavid Howells 		object = assoc_array_find(&keyring->keys,
605b2a4df20SDavid Howells 					  &keyring_assoc_array_ops,
606b2a4df20SDavid Howells 					  &ctx->index_key);
607b2a4df20SDavid Howells 		return object ? ctx->iterator(object, ctx) : 0;
608b2a4df20SDavid Howells 	}
609b2a4df20SDavid Howells 	return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx);
610b2a4df20SDavid Howells }
611b2a4df20SDavid Howells 
612b2a4df20SDavid Howells /*
613b2a4df20SDavid Howells  * Search a tree of keyrings that point to other keyrings up to the maximum
614b2a4df20SDavid Howells  * depth.
615b2a4df20SDavid Howells  */
616b2a4df20SDavid Howells static bool search_nested_keyrings(struct key *keyring,
617b2a4df20SDavid Howells 				   struct keyring_search_context *ctx)
618b2a4df20SDavid Howells {
619b2a4df20SDavid Howells 	struct {
620b2a4df20SDavid Howells 		struct key *keyring;
621b2a4df20SDavid Howells 		struct assoc_array_node *node;
622b2a4df20SDavid Howells 		int slot;
623b2a4df20SDavid Howells 	} stack[KEYRING_SEARCH_MAX_DEPTH];
624b2a4df20SDavid Howells 
625b2a4df20SDavid Howells 	struct assoc_array_shortcut *shortcut;
626b2a4df20SDavid Howells 	struct assoc_array_node *node;
627b2a4df20SDavid Howells 	struct assoc_array_ptr *ptr;
628b2a4df20SDavid Howells 	struct key *key;
629b2a4df20SDavid Howells 	int sp = 0, slot;
630b2a4df20SDavid Howells 
631b2a4df20SDavid Howells 	kenter("{%d},{%s,%s}",
632b2a4df20SDavid Howells 	       keyring->serial,
633b2a4df20SDavid Howells 	       ctx->index_key.type->name,
634b2a4df20SDavid Howells 	       ctx->index_key.description);
635b2a4df20SDavid Howells 
636054f6180SDavid Howells #define STATE_CHECKS (KEYRING_SEARCH_NO_STATE_CHECK | KEYRING_SEARCH_DO_STATE_CHECK)
637054f6180SDavid Howells 	BUG_ON((ctx->flags & STATE_CHECKS) == 0 ||
638054f6180SDavid Howells 	       (ctx->flags & STATE_CHECKS) == STATE_CHECKS);
639054f6180SDavid Howells 
640f771fde8SDavid Howells 	if (ctx->index_key.description)
641f771fde8SDavid Howells 		key_set_index_key(&ctx->index_key);
642f771fde8SDavid Howells 
643b2a4df20SDavid Howells 	/* Check to see if this top-level keyring is what we are looking for
644b2a4df20SDavid Howells 	 * and whether it is valid or not.
645b2a4df20SDavid Howells 	 */
64646291959SDavid Howells 	if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_ITERATE ||
647b2a4df20SDavid Howells 	    keyring_compare_object(keyring, &ctx->index_key)) {
648b2a4df20SDavid Howells 		ctx->skipped_ret = 2;
649b2a4df20SDavid Howells 		switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) {
650b2a4df20SDavid Howells 		case 1:
651b2a4df20SDavid Howells 			goto found;
652b2a4df20SDavid Howells 		case 2:
653b2a4df20SDavid Howells 			return false;
654b2a4df20SDavid Howells 		default:
655b2a4df20SDavid Howells 			break;
656b2a4df20SDavid Howells 		}
657b2a4df20SDavid Howells 	}
658b2a4df20SDavid Howells 
659b2a4df20SDavid Howells 	ctx->skipped_ret = 0;
660b2a4df20SDavid Howells 
661b2a4df20SDavid Howells 	/* Start processing a new keyring */
662b2a4df20SDavid Howells descend_to_keyring:
663b2a4df20SDavid Howells 	kdebug("descend to %d", keyring->serial);
664b2a4df20SDavid Howells 	if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
665b2a4df20SDavid Howells 			      (1 << KEY_FLAG_REVOKED)))
666b2a4df20SDavid Howells 		goto not_this_keyring;
667b2a4df20SDavid Howells 
668b2a4df20SDavid Howells 	/* Search through the keys in this keyring before its searching its
669b2a4df20SDavid Howells 	 * subtrees.
670b2a4df20SDavid Howells 	 */
671b2a4df20SDavid Howells 	if (search_keyring(keyring, ctx))
672b2a4df20SDavid Howells 		goto found;
673b2a4df20SDavid Howells 
674b2a4df20SDavid Howells 	/* Then manually iterate through the keyrings nested in this one.
675b2a4df20SDavid Howells 	 *
676b2a4df20SDavid Howells 	 * Start from the root node of the index tree.  Because of the way the
677b2a4df20SDavid Howells 	 * hash function has been set up, keyrings cluster on the leftmost
678b2a4df20SDavid Howells 	 * branch of the root node (root slot 0) or in the root node itself.
679b2a4df20SDavid Howells 	 * Non-keyrings avoid the leftmost branch of the root entirely (root
680b2a4df20SDavid Howells 	 * slots 1-15).
681b2a4df20SDavid Howells 	 */
682dcf49dbcSDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_RECURSE))
683dcf49dbcSDavid Howells 		goto not_this_keyring;
684dcf49dbcSDavid Howells 
685381f20fcSDavidlohr Bueso 	ptr = READ_ONCE(keyring->keys.root);
686b2a4df20SDavid Howells 	if (!ptr)
687b2a4df20SDavid Howells 		goto not_this_keyring;
688b2a4df20SDavid Howells 
689b2a4df20SDavid Howells 	if (assoc_array_ptr_is_shortcut(ptr)) {
690b2a4df20SDavid Howells 		/* If the root is a shortcut, either the keyring only contains
691b2a4df20SDavid Howells 		 * keyring pointers (everything clusters behind root slot 0) or
692b2a4df20SDavid Howells 		 * doesn't contain any keyring pointers.
693b2a4df20SDavid Howells 		 */
694b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
695b2a4df20SDavid Howells 		if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0)
696b2a4df20SDavid Howells 			goto not_this_keyring;
697b2a4df20SDavid Howells 
698381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(shortcut->next_node);
699b2a4df20SDavid Howells 		node = assoc_array_ptr_to_node(ptr);
700b2a4df20SDavid Howells 		goto begin_node;
701b2a4df20SDavid Howells 	}
702b2a4df20SDavid Howells 
703b2a4df20SDavid Howells 	node = assoc_array_ptr_to_node(ptr);
704b2a4df20SDavid Howells 	ptr = node->slots[0];
705b2a4df20SDavid Howells 	if (!assoc_array_ptr_is_meta(ptr))
706b2a4df20SDavid Howells 		goto begin_node;
707b2a4df20SDavid Howells 
708b2a4df20SDavid Howells descend_to_node:
709b2a4df20SDavid Howells 	/* Descend to a more distal node in this keyring's content tree and go
710b2a4df20SDavid Howells 	 * through that.
711b2a4df20SDavid Howells 	 */
712b2a4df20SDavid Howells 	kdebug("descend");
713b2a4df20SDavid Howells 	if (assoc_array_ptr_is_shortcut(ptr)) {
714b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
715381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(shortcut->next_node);
716b2a4df20SDavid Howells 		BUG_ON(!assoc_array_ptr_is_node(ptr));
717b2a4df20SDavid Howells 	}
7189c5e45dfSDavid Howells 	node = assoc_array_ptr_to_node(ptr);
719b2a4df20SDavid Howells 
720b2a4df20SDavid Howells begin_node:
721b2a4df20SDavid Howells 	kdebug("begin_node");
722b2a4df20SDavid Howells 	slot = 0;
723b2a4df20SDavid Howells ascend_to_node:
724b2a4df20SDavid Howells 	/* Go through the slots in a node */
725b2a4df20SDavid Howells 	for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
726381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(node->slots[slot]);
727b2a4df20SDavid Howells 
728b2a4df20SDavid Howells 		if (assoc_array_ptr_is_meta(ptr) && node->back_pointer)
729b2a4df20SDavid Howells 			goto descend_to_node;
730b2a4df20SDavid Howells 
731b2a4df20SDavid Howells 		if (!keyring_ptr_is_keyring(ptr))
732b2a4df20SDavid Howells 			continue;
733b2a4df20SDavid Howells 
734b2a4df20SDavid Howells 		key = keyring_ptr_to_key(ptr);
735b2a4df20SDavid Howells 
736b2a4df20SDavid Howells 		if (sp >= KEYRING_SEARCH_MAX_DEPTH) {
737b2a4df20SDavid Howells 			if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) {
738b2a4df20SDavid Howells 				ctx->result = ERR_PTR(-ELOOP);
739b2a4df20SDavid Howells 				return false;
740b2a4df20SDavid Howells 			}
741b2a4df20SDavid Howells 			goto not_this_keyring;
742b2a4df20SDavid Howells 		}
743b2a4df20SDavid Howells 
744b2a4df20SDavid Howells 		/* Search a nested keyring */
745b2a4df20SDavid Howells 		if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
746b2a4df20SDavid Howells 		    key_task_permission(make_key_ref(key, ctx->possessed),
747f5895943SDavid Howells 					ctx->cred, KEY_NEED_SEARCH) < 0)
748b2a4df20SDavid Howells 			continue;
749b2a4df20SDavid Howells 
750b2a4df20SDavid Howells 		/* stack the current position */
751b2a4df20SDavid Howells 		stack[sp].keyring = keyring;
752b2a4df20SDavid Howells 		stack[sp].node = node;
753b2a4df20SDavid Howells 		stack[sp].slot = slot;
754b2a4df20SDavid Howells 		sp++;
755b2a4df20SDavid Howells 
756b2a4df20SDavid Howells 		/* begin again with the new keyring */
757b2a4df20SDavid Howells 		keyring = key;
758b2a4df20SDavid Howells 		goto descend_to_keyring;
759b2a4df20SDavid Howells 	}
760b2a4df20SDavid Howells 
761b2a4df20SDavid Howells 	/* We've dealt with all the slots in the current node, so now we need
762b2a4df20SDavid Howells 	 * to ascend to the parent and continue processing there.
763b2a4df20SDavid Howells 	 */
764381f20fcSDavidlohr Bueso 	ptr = READ_ONCE(node->back_pointer);
765b2a4df20SDavid Howells 	slot = node->parent_slot;
766b2a4df20SDavid Howells 
767b2a4df20SDavid Howells 	if (ptr && assoc_array_ptr_is_shortcut(ptr)) {
768b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
769381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(shortcut->back_pointer);
770b2a4df20SDavid Howells 		slot = shortcut->parent_slot;
771b2a4df20SDavid Howells 	}
772b2a4df20SDavid Howells 	if (!ptr)
773b2a4df20SDavid Howells 		goto not_this_keyring;
774b2a4df20SDavid Howells 	node = assoc_array_ptr_to_node(ptr);
775b2a4df20SDavid Howells 	slot++;
776b2a4df20SDavid Howells 
777b2a4df20SDavid Howells 	/* If we've ascended to the root (zero backpointer), we must have just
778b2a4df20SDavid Howells 	 * finished processing the leftmost branch rather than the root slots -
779b2a4df20SDavid Howells 	 * so there can't be any more keyrings for us to find.
780b2a4df20SDavid Howells 	 */
781b2a4df20SDavid Howells 	if (node->back_pointer) {
782b2a4df20SDavid Howells 		kdebug("ascend %d", slot);
783b2a4df20SDavid Howells 		goto ascend_to_node;
784b2a4df20SDavid Howells 	}
785b2a4df20SDavid Howells 
786b2a4df20SDavid Howells 	/* The keyring we're looking at was disqualified or didn't contain a
787b2a4df20SDavid Howells 	 * matching key.
788b2a4df20SDavid Howells 	 */
789b2a4df20SDavid Howells not_this_keyring:
790b2a4df20SDavid Howells 	kdebug("not_this_keyring %d", sp);
791b2a4df20SDavid Howells 	if (sp <= 0) {
792b2a4df20SDavid Howells 		kleave(" = false");
793b2a4df20SDavid Howells 		return false;
794b2a4df20SDavid Howells 	}
795b2a4df20SDavid Howells 
796b2a4df20SDavid Howells 	/* Resume the processing of a keyring higher up in the tree */
797b2a4df20SDavid Howells 	sp--;
798b2a4df20SDavid Howells 	keyring = stack[sp].keyring;
799b2a4df20SDavid Howells 	node = stack[sp].node;
800b2a4df20SDavid Howells 	slot = stack[sp].slot + 1;
801b2a4df20SDavid Howells 	kdebug("ascend to %d [%d]", keyring->serial, slot);
802b2a4df20SDavid Howells 	goto ascend_to_node;
803b2a4df20SDavid Howells 
804b2a4df20SDavid Howells 	/* We found a viable match */
805b2a4df20SDavid Howells found:
806b2a4df20SDavid Howells 	key = key_ref_to_ptr(ctx->result);
807b2a4df20SDavid Howells 	key_check(key);
808b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) {
809074d5898SBaolin Wang 		key->last_used_at = ctx->now;
810074d5898SBaolin Wang 		keyring->last_used_at = ctx->now;
811b2a4df20SDavid Howells 		while (sp > 0)
812074d5898SBaolin Wang 			stack[--sp].keyring->last_used_at = ctx->now;
813b2a4df20SDavid Howells 	}
814b2a4df20SDavid Howells 	kleave(" = true");
815b2a4df20SDavid Howells 	return true;
816b2a4df20SDavid Howells }
817b2a4df20SDavid Howells 
818973c9f4fSDavid Howells /**
819e59428f7SDavid Howells  * keyring_search_rcu - Search a keyring tree for a matching key under RCU
820973c9f4fSDavid Howells  * @keyring_ref: A pointer to the keyring with possession indicator.
8214bdf0bc3SDavid Howells  * @ctx: The keyring search context.
822973c9f4fSDavid Howells  *
823973c9f4fSDavid Howells  * Search the supplied keyring tree for a key that matches the criteria given.
824973c9f4fSDavid Howells  * The root keyring and any linked keyrings must grant Search permission to the
825973c9f4fSDavid Howells  * caller to be searchable and keys can only be found if they too grant Search
826973c9f4fSDavid Howells  * to the caller. The possession flag on the root keyring pointer controls use
827973c9f4fSDavid Howells  * of the possessor bits in permissions checking of the entire tree.  In
828973c9f4fSDavid Howells  * addition, the LSM gets to forbid keyring searches and key matches.
829973c9f4fSDavid Howells  *
830973c9f4fSDavid Howells  * The search is performed as a breadth-then-depth search up to the prescribed
831e59428f7SDavid Howells  * limit (KEYRING_SEARCH_MAX_DEPTH).  The caller must hold the RCU read lock to
832e59428f7SDavid Howells  * prevent keyrings from being destroyed or rearranged whilst they are being
833e59428f7SDavid Howells  * searched.
834973c9f4fSDavid Howells  *
835973c9f4fSDavid Howells  * Keys are matched to the type provided and are then filtered by the match
836973c9f4fSDavid Howells  * function, which is given the description to use in any way it sees fit.  The
837973c9f4fSDavid Howells  * match function may use any attributes of a key that it wishes to to
838973c9f4fSDavid Howells  * determine the match.  Normally the match function from the key type would be
839973c9f4fSDavid Howells  * used.
840973c9f4fSDavid Howells  *
841b2a4df20SDavid Howells  * RCU can be used to prevent the keyring key lists from disappearing without
842b2a4df20SDavid Howells  * the need to take lots of locks.
843973c9f4fSDavid Howells  *
844973c9f4fSDavid Howells  * Returns a pointer to the found key and increments the key usage count if
845973c9f4fSDavid Howells  * successful; -EAGAIN if no matching keys were found, or if expired or revoked
846973c9f4fSDavid Howells  * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
847973c9f4fSDavid Howells  * specified keyring wasn't a keyring.
848973c9f4fSDavid Howells  *
849973c9f4fSDavid Howells  * In the case of a successful return, the possession attribute from
850973c9f4fSDavid Howells  * @keyring_ref is propagated to the returned key reference.
8511da177e4SLinus Torvalds  */
852e59428f7SDavid Howells key_ref_t keyring_search_rcu(key_ref_t keyring_ref,
8534bdf0bc3SDavid Howells 			     struct keyring_search_context *ctx)
8541da177e4SLinus Torvalds {
85531d5a79dSDavid Howells 	struct key *keyring;
8561da177e4SLinus Torvalds 	long err;
857b2a4df20SDavid Howells 
858b2a4df20SDavid Howells 	ctx->iterator = keyring_search_iterator;
859b2a4df20SDavid Howells 	ctx->possessed = is_key_possessed(keyring_ref);
860b2a4df20SDavid Howells 	ctx->result = ERR_PTR(-EAGAIN);
8611da177e4SLinus Torvalds 
862664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
8631da177e4SLinus Torvalds 	key_check(keyring);
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
866b2a4df20SDavid Howells 		return ERR_PTR(-ENOTDIR);
867b2a4df20SDavid Howells 
868b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) {
869f5895943SDavid Howells 		err = key_task_permission(keyring_ref, ctx->cred, KEY_NEED_SEARCH);
870b2a4df20SDavid Howells 		if (err < 0)
871b2a4df20SDavid Howells 			return ERR_PTR(err);
872b2a4df20SDavid Howells 	}
8731da177e4SLinus Torvalds 
874074d5898SBaolin Wang 	ctx->now = ktime_get_real_seconds();
875b2a4df20SDavid Howells 	if (search_nested_keyrings(keyring, ctx))
876b2a4df20SDavid Howells 		__key_get(key_ref_to_ptr(ctx->result));
877b2a4df20SDavid Howells 	return ctx->result;
878a8b17ed0SDavid Howells }
8791da177e4SLinus Torvalds 
880973c9f4fSDavid Howells /**
881973c9f4fSDavid Howells  * keyring_search - Search the supplied keyring tree for a matching key
882973c9f4fSDavid Howells  * @keyring: The root of the keyring tree to be searched.
883973c9f4fSDavid Howells  * @type: The type of keyring we want to find.
884973c9f4fSDavid Howells  * @description: The name of the keyring we want to find.
885dcf49dbcSDavid Howells  * @recurse: True to search the children of @keyring also
886973c9f4fSDavid Howells  *
887e59428f7SDavid Howells  * As keyring_search_rcu() above, but using the current task's credentials and
888b2a4df20SDavid Howells  * type's default matching function and preferred search method.
8891da177e4SLinus Torvalds  */
890664cceb0SDavid Howells key_ref_t keyring_search(key_ref_t keyring,
8911da177e4SLinus Torvalds 			 struct key_type *type,
892dcf49dbcSDavid Howells 			 const char *description,
893dcf49dbcSDavid Howells 			 bool recurse)
8941da177e4SLinus Torvalds {
8954bdf0bc3SDavid Howells 	struct keyring_search_context ctx = {
8964bdf0bc3SDavid Howells 		.index_key.type		= type,
8974bdf0bc3SDavid Howells 		.index_key.description	= description,
898ede0fa98SEric Biggers 		.index_key.desc_len	= strlen(description),
8994bdf0bc3SDavid Howells 		.cred			= current_cred(),
900c06cfb08SDavid Howells 		.match_data.cmp		= key_default_cmp,
90146291959SDavid Howells 		.match_data.raw_data	= description,
90246291959SDavid Howells 		.match_data.lookup_type	= KEYRING_SEARCH_LOOKUP_DIRECT,
90346291959SDavid Howells 		.flags			= KEYRING_SEARCH_DO_STATE_CHECK,
9044bdf0bc3SDavid Howells 	};
90546291959SDavid Howells 	key_ref_t key;
90646291959SDavid Howells 	int ret;
9074bdf0bc3SDavid Howells 
908dcf49dbcSDavid Howells 	if (recurse)
909dcf49dbcSDavid Howells 		ctx.flags |= KEYRING_SEARCH_RECURSE;
91046291959SDavid Howells 	if (type->match_preparse) {
91146291959SDavid Howells 		ret = type->match_preparse(&ctx.match_data);
91246291959SDavid Howells 		if (ret < 0)
91346291959SDavid Howells 			return ERR_PTR(ret);
91446291959SDavid Howells 	}
91546291959SDavid Howells 
916e59428f7SDavid Howells 	rcu_read_lock();
917e59428f7SDavid Howells 	key = keyring_search_rcu(keyring, &ctx);
918e59428f7SDavid Howells 	rcu_read_unlock();
91946291959SDavid Howells 
92046291959SDavid Howells 	if (type->match_free)
92146291959SDavid Howells 		type->match_free(&ctx.match_data);
92246291959SDavid Howells 	return key;
923a8b17ed0SDavid Howells }
9241da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_search);
9251da177e4SLinus Torvalds 
9266563c91fSMat Martineau static struct key_restriction *keyring_restriction_alloc(
9276563c91fSMat Martineau 	key_restrict_link_func_t check)
9286563c91fSMat Martineau {
9296563c91fSMat Martineau 	struct key_restriction *keyres =
9306563c91fSMat Martineau 		kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
9316563c91fSMat Martineau 
9326563c91fSMat Martineau 	if (!keyres)
9336563c91fSMat Martineau 		return ERR_PTR(-ENOMEM);
9346563c91fSMat Martineau 
9356563c91fSMat Martineau 	keyres->check = check;
9366563c91fSMat Martineau 
9376563c91fSMat Martineau 	return keyres;
9386563c91fSMat Martineau }
9396563c91fSMat Martineau 
9406563c91fSMat Martineau /*
9416563c91fSMat Martineau  * Semaphore to serialise restriction setup to prevent reference count
9426563c91fSMat Martineau  * cycles through restriction key pointers.
9436563c91fSMat Martineau  */
9446563c91fSMat Martineau static DECLARE_RWSEM(keyring_serialise_restrict_sem);
9456563c91fSMat Martineau 
9466563c91fSMat Martineau /*
9476563c91fSMat Martineau  * Check for restriction cycles that would prevent keyring garbage collection.
9486563c91fSMat Martineau  * keyring_serialise_restrict_sem must be held.
9496563c91fSMat Martineau  */
9506563c91fSMat Martineau static bool keyring_detect_restriction_cycle(const struct key *dest_keyring,
9516563c91fSMat Martineau 					     struct key_restriction *keyres)
9526563c91fSMat Martineau {
9536563c91fSMat Martineau 	while (keyres && keyres->key &&
9546563c91fSMat Martineau 	       keyres->key->type == &key_type_keyring) {
9556563c91fSMat Martineau 		if (keyres->key == dest_keyring)
9566563c91fSMat Martineau 			return true;
9576563c91fSMat Martineau 
9586563c91fSMat Martineau 		keyres = keyres->key->restrict_link;
9596563c91fSMat Martineau 	}
9606563c91fSMat Martineau 
9616563c91fSMat Martineau 	return false;
9626563c91fSMat Martineau }
9636563c91fSMat Martineau 
9646563c91fSMat Martineau /**
9656563c91fSMat Martineau  * keyring_restrict - Look up and apply a restriction to a keyring
9669fd16537SDavid Howells  * @keyring_ref: The keyring to be restricted
9679fd16537SDavid Howells  * @type: The key type that will provide the restriction checker.
9686563c91fSMat Martineau  * @restriction: The restriction options to apply to the keyring
9699fd16537SDavid Howells  *
9709fd16537SDavid Howells  * Look up a keyring and apply a restriction to it.  The restriction is managed
9719fd16537SDavid Howells  * by the specific key type, but can be configured by the options specified in
9729fd16537SDavid Howells  * the restriction string.
9736563c91fSMat Martineau  */
9746563c91fSMat Martineau int keyring_restrict(key_ref_t keyring_ref, const char *type,
9756563c91fSMat Martineau 		     const char *restriction)
9766563c91fSMat Martineau {
9776563c91fSMat Martineau 	struct key *keyring;
9786563c91fSMat Martineau 	struct key_type *restrict_type = NULL;
9796563c91fSMat Martineau 	struct key_restriction *restrict_link;
9806563c91fSMat Martineau 	int ret = 0;
9816563c91fSMat Martineau 
9826563c91fSMat Martineau 	keyring = key_ref_to_ptr(keyring_ref);
9836563c91fSMat Martineau 	key_check(keyring);
9846563c91fSMat Martineau 
9856563c91fSMat Martineau 	if (keyring->type != &key_type_keyring)
9866563c91fSMat Martineau 		return -ENOTDIR;
9876563c91fSMat Martineau 
9886563c91fSMat Martineau 	if (!type) {
9896563c91fSMat Martineau 		restrict_link = keyring_restriction_alloc(restrict_link_reject);
9906563c91fSMat Martineau 	} else {
9916563c91fSMat Martineau 		restrict_type = key_type_lookup(type);
9926563c91fSMat Martineau 
9936563c91fSMat Martineau 		if (IS_ERR(restrict_type))
9946563c91fSMat Martineau 			return PTR_ERR(restrict_type);
9956563c91fSMat Martineau 
9966563c91fSMat Martineau 		if (!restrict_type->lookup_restriction) {
9976563c91fSMat Martineau 			ret = -ENOENT;
9986563c91fSMat Martineau 			goto error;
9996563c91fSMat Martineau 		}
10006563c91fSMat Martineau 
10016563c91fSMat Martineau 		restrict_link = restrict_type->lookup_restriction(restriction);
10026563c91fSMat Martineau 	}
10036563c91fSMat Martineau 
10046563c91fSMat Martineau 	if (IS_ERR(restrict_link)) {
10056563c91fSMat Martineau 		ret = PTR_ERR(restrict_link);
10066563c91fSMat Martineau 		goto error;
10076563c91fSMat Martineau 	}
10086563c91fSMat Martineau 
10096563c91fSMat Martineau 	down_write(&keyring->sem);
10106563c91fSMat Martineau 	down_write(&keyring_serialise_restrict_sem);
10116563c91fSMat Martineau 
10126563c91fSMat Martineau 	if (keyring->restrict_link)
10136563c91fSMat Martineau 		ret = -EEXIST;
10146563c91fSMat Martineau 	else if (keyring_detect_restriction_cycle(keyring, restrict_link))
10156563c91fSMat Martineau 		ret = -EDEADLK;
10166563c91fSMat Martineau 	else
10176563c91fSMat Martineau 		keyring->restrict_link = restrict_link;
10186563c91fSMat Martineau 
10196563c91fSMat Martineau 	up_write(&keyring_serialise_restrict_sem);
10206563c91fSMat Martineau 	up_write(&keyring->sem);
10216563c91fSMat Martineau 
10226563c91fSMat Martineau 	if (ret < 0) {
10236563c91fSMat Martineau 		key_put(restrict_link->key);
10246563c91fSMat Martineau 		kfree(restrict_link);
10256563c91fSMat Martineau 	}
10266563c91fSMat Martineau 
10276563c91fSMat Martineau error:
10286563c91fSMat Martineau 	if (restrict_type)
10296563c91fSMat Martineau 		key_type_put(restrict_type);
10306563c91fSMat Martineau 
10316563c91fSMat Martineau 	return ret;
10326563c91fSMat Martineau }
10336563c91fSMat Martineau EXPORT_SYMBOL(keyring_restrict);
10346563c91fSMat Martineau 
10351da177e4SLinus Torvalds /*
1036b2a4df20SDavid Howells  * Search the given keyring for a key that might be updated.
1037973c9f4fSDavid Howells  *
1038973c9f4fSDavid Howells  * The caller must guarantee that the keyring is a keyring and that the
1039b2a4df20SDavid Howells  * permission is granted to modify the keyring as no check is made here.  The
1040b2a4df20SDavid Howells  * caller must also hold a lock on the keyring semaphore.
1041973c9f4fSDavid Howells  *
1042973c9f4fSDavid Howells  * Returns a pointer to the found key with usage count incremented if
1043b2a4df20SDavid Howells  * successful and returns NULL if not found.  Revoked and invalidated keys are
1044b2a4df20SDavid Howells  * skipped over.
1045973c9f4fSDavid Howells  *
1046973c9f4fSDavid Howells  * If successful, the possession indicator is propagated from the keyring ref
1047973c9f4fSDavid Howells  * to the returned key reference.
10481da177e4SLinus Torvalds  */
1049b2a4df20SDavid Howells key_ref_t find_key_to_update(key_ref_t keyring_ref,
1050e57e8669SDavid Howells 			     const struct keyring_index_key *index_key)
10511da177e4SLinus Torvalds {
1052664cceb0SDavid Howells 	struct key *keyring, *key;
1053b2a4df20SDavid Howells 	const void *object;
10541da177e4SLinus Torvalds 
1055664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
1056664cceb0SDavid Howells 
1057b2a4df20SDavid Howells 	kenter("{%d},{%s,%s}",
1058b2a4df20SDavid Howells 	       keyring->serial, index_key->type->name, index_key->description);
105976d8aeabSDavid Howells 
1060b2a4df20SDavid Howells 	object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
1061b2a4df20SDavid Howells 				  index_key);
1062b2a4df20SDavid Howells 
1063b2a4df20SDavid Howells 	if (object)
10641da177e4SLinus Torvalds 		goto found;
10651da177e4SLinus Torvalds 
1066b2a4df20SDavid Howells 	kleave(" = NULL");
1067b2a4df20SDavid Howells 	return NULL;
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds found:
1070b2a4df20SDavid Howells 	key = keyring_ptr_to_key(object);
1071b2a4df20SDavid Howells 	if (key->flags & ((1 << KEY_FLAG_INVALIDATED) |
1072b2a4df20SDavid Howells 			  (1 << KEY_FLAG_REVOKED))) {
1073b2a4df20SDavid Howells 		kleave(" = NULL [x]");
1074b2a4df20SDavid Howells 		return NULL;
1075b2a4df20SDavid Howells 	}
1076ccc3e6d9SDavid Howells 	__key_get(key);
1077b2a4df20SDavid Howells 	kleave(" = {%d}", key->serial);
1078b2a4df20SDavid Howells 	return make_key_ref(key, is_key_possessed(keyring_ref));
1079a8b17ed0SDavid Howells }
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds /*
1082973c9f4fSDavid Howells  * Find a keyring with the specified name.
1083973c9f4fSDavid Howells  *
1084237bbd29SEric Biggers  * Only keyrings that have nonzero refcount, are not revoked, and are owned by a
1085237bbd29SEric Biggers  * user in the current user namespace are considered.  If @uid_keyring is %true,
1086237bbd29SEric Biggers  * the keyring additionally must have been allocated as a user or user session
1087237bbd29SEric Biggers  * keyring; otherwise, it must grant Search permission directly to the caller.
1088973c9f4fSDavid Howells  *
1089973c9f4fSDavid Howells  * Returns a pointer to the keyring with the keyring's refcount having being
1090973c9f4fSDavid Howells  * incremented on success.  -ENOKEY is returned if a key could not be found.
10911da177e4SLinus Torvalds  */
1092237bbd29SEric Biggers struct key *find_keyring_by_name(const char *name, bool uid_keyring)
10931da177e4SLinus Torvalds {
1094b206f281SDavid Howells 	struct user_namespace *ns = current_user_ns();
10951da177e4SLinus Torvalds 	struct key *keyring;
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 	if (!name)
1098cea7daa3SToshiyuki Okajima 		return ERR_PTR(-EINVAL);
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	read_lock(&keyring_name_lock);
11011da177e4SLinus Torvalds 
1102b206f281SDavid Howells 	/* Search this hash bucket for a keyring with a matching name that
1103b206f281SDavid Howells 	 * grants Search permission and that hasn't been revoked
1104b206f281SDavid Howells 	 */
1105b206f281SDavid Howells 	list_for_each_entry(keyring, &ns->keyring_name_list, name_link) {
1106b206f281SDavid Howells 		if (!kuid_has_mapping(ns, keyring->user->uid))
11072ea190d0SSerge E. Hallyn 			continue;
11082ea190d0SSerge E. Hallyn 
110976d8aeabSDavid Howells 		if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
11101da177e4SLinus Torvalds 			continue;
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 		if (strcmp(keyring->description, name) != 0)
11131da177e4SLinus Torvalds 			continue;
11141da177e4SLinus Torvalds 
1115237bbd29SEric Biggers 		if (uid_keyring) {
1116237bbd29SEric Biggers 			if (!test_bit(KEY_FLAG_UID_KEYRING,
1117237bbd29SEric Biggers 				      &keyring->flags))
1118237bbd29SEric Biggers 				continue;
1119237bbd29SEric Biggers 		} else {
1120237bbd29SEric Biggers 			if (key_permission(make_key_ref(keyring, 0),
1121f5895943SDavid Howells 					   KEY_NEED_SEARCH) < 0)
11221da177e4SLinus Torvalds 				continue;
1123237bbd29SEric Biggers 		}
11241da177e4SLinus Torvalds 
1125cea7daa3SToshiyuki Okajima 		/* we've got a match but we might end up racing with
1126cea7daa3SToshiyuki Okajima 		 * key_cleanup() if the keyring is currently 'dead'
1127cea7daa3SToshiyuki Okajima 		 * (ie. it has a zero usage count) */
1128fff29291SElena Reshetova 		if (!refcount_inc_not_zero(&keyring->usage))
1129cea7daa3SToshiyuki Okajima 			continue;
1130074d5898SBaolin Wang 		keyring->last_used_at = ktime_get_real_seconds();
1131cea7daa3SToshiyuki Okajima 		goto out;
11321da177e4SLinus Torvalds 	}
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	keyring = ERR_PTR(-ENOKEY);
1135cea7daa3SToshiyuki Okajima out:
1136cea7daa3SToshiyuki Okajima 	read_unlock(&keyring_name_lock);
11371da177e4SLinus Torvalds 	return keyring;
1138a8b17ed0SDavid Howells }
11391da177e4SLinus Torvalds 
1140b2a4df20SDavid Howells static int keyring_detect_cycle_iterator(const void *object,
1141b2a4df20SDavid Howells 					 void *iterator_data)
1142b2a4df20SDavid Howells {
1143b2a4df20SDavid Howells 	struct keyring_search_context *ctx = iterator_data;
1144b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
1145b2a4df20SDavid Howells 
1146b2a4df20SDavid Howells 	kenter("{%d}", key->serial);
1147b2a4df20SDavid Howells 
1148979e0d74SDavid Howells 	/* We might get a keyring with matching index-key that is nonetheless a
1149979e0d74SDavid Howells 	 * different keyring. */
115046291959SDavid Howells 	if (key != ctx->match_data.raw_data)
1151979e0d74SDavid Howells 		return 0;
1152979e0d74SDavid Howells 
1153b2a4df20SDavid Howells 	ctx->result = ERR_PTR(-EDEADLK);
1154b2a4df20SDavid Howells 	return 1;
1155b2a4df20SDavid Howells }
1156b2a4df20SDavid Howells 
11571da177e4SLinus Torvalds /*
1158973c9f4fSDavid Howells  * See if a cycle will will be created by inserting acyclic tree B in acyclic
1159973c9f4fSDavid Howells  * tree A at the topmost level (ie: as a direct child of A).
1160973c9f4fSDavid Howells  *
1161973c9f4fSDavid Howells  * Since we are adding B to A at the top level, checking for cycles should just
1162973c9f4fSDavid Howells  * be a matter of seeing if node A is somewhere in tree B.
11631da177e4SLinus Torvalds  */
11641da177e4SLinus Torvalds static int keyring_detect_cycle(struct key *A, struct key *B)
11651da177e4SLinus Torvalds {
1166b2a4df20SDavid Howells 	struct keyring_search_context ctx = {
1167b2a4df20SDavid Howells 		.index_key		= A->index_key,
116846291959SDavid Howells 		.match_data.raw_data	= A,
116946291959SDavid Howells 		.match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
1170b2a4df20SDavid Howells 		.iterator		= keyring_detect_cycle_iterator,
117146291959SDavid Howells 		.flags			= (KEYRING_SEARCH_NO_STATE_CHECK |
1172b2a4df20SDavid Howells 					   KEYRING_SEARCH_NO_UPDATE_TIME |
1173b2a4df20SDavid Howells 					   KEYRING_SEARCH_NO_CHECK_PERM |
1174dcf49dbcSDavid Howells 					   KEYRING_SEARCH_DETECT_TOO_DEEP |
1175dcf49dbcSDavid Howells 					   KEYRING_SEARCH_RECURSE),
1176b2a4df20SDavid Howells 	};
11771da177e4SLinus Torvalds 
117876d8aeabSDavid Howells 	rcu_read_lock();
1179b2a4df20SDavid Howells 	search_nested_keyrings(B, &ctx);
118076d8aeabSDavid Howells 	rcu_read_unlock();
1181b2a4df20SDavid Howells 	return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result);
1182f70e2e06SDavid Howells }
1183cab8eb59SDavid Howells 
1184cab8eb59SDavid Howells /*
1185df593ee2SDavid Howells  * Lock keyring for link.
1186df593ee2SDavid Howells  */
1187df593ee2SDavid Howells int __key_link_lock(struct key *keyring,
1188df593ee2SDavid Howells 		    const struct keyring_index_key *index_key)
1189df593ee2SDavid Howells 	__acquires(&keyring->sem)
1190df593ee2SDavid Howells 	__acquires(&keyring_serialise_link_lock)
1191df593ee2SDavid Howells {
1192df593ee2SDavid Howells 	if (keyring->type != &key_type_keyring)
1193df593ee2SDavid Howells 		return -ENOTDIR;
1194df593ee2SDavid Howells 
1195df593ee2SDavid Howells 	down_write(&keyring->sem);
1196df593ee2SDavid Howells 
1197df593ee2SDavid Howells 	/* Serialise link/link calls to prevent parallel calls causing a cycle
1198df593ee2SDavid Howells 	 * when linking two keyring in opposite orders.
1199df593ee2SDavid Howells 	 */
1200df593ee2SDavid Howells 	if (index_key->type == &key_type_keyring)
1201df593ee2SDavid Howells 		mutex_lock(&keyring_serialise_link_lock);
1202df593ee2SDavid Howells 
1203df593ee2SDavid Howells 	return 0;
1204df593ee2SDavid Howells }
1205df593ee2SDavid Howells 
1206df593ee2SDavid Howells /*
1207ed0ac5c7SDavid Howells  * Lock keyrings for move (link/unlink combination).
1208ed0ac5c7SDavid Howells  */
1209ed0ac5c7SDavid Howells int __key_move_lock(struct key *l_keyring, struct key *u_keyring,
1210ed0ac5c7SDavid Howells 		    const struct keyring_index_key *index_key)
1211ed0ac5c7SDavid Howells 	__acquires(&l_keyring->sem)
1212ed0ac5c7SDavid Howells 	__acquires(&u_keyring->sem)
1213ed0ac5c7SDavid Howells 	__acquires(&keyring_serialise_link_lock)
1214ed0ac5c7SDavid Howells {
1215ed0ac5c7SDavid Howells 	if (l_keyring->type != &key_type_keyring ||
1216ed0ac5c7SDavid Howells 	    u_keyring->type != &key_type_keyring)
1217ed0ac5c7SDavid Howells 		return -ENOTDIR;
1218ed0ac5c7SDavid Howells 
1219ed0ac5c7SDavid Howells 	/* We have to be very careful here to take the keyring locks in the
1220ed0ac5c7SDavid Howells 	 * right order, lest we open ourselves to deadlocking against another
1221ed0ac5c7SDavid Howells 	 * move operation.
1222ed0ac5c7SDavid Howells 	 */
1223ed0ac5c7SDavid Howells 	if (l_keyring < u_keyring) {
1224ed0ac5c7SDavid Howells 		down_write(&l_keyring->sem);
1225ed0ac5c7SDavid Howells 		down_write_nested(&u_keyring->sem, 1);
1226ed0ac5c7SDavid Howells 	} else {
1227ed0ac5c7SDavid Howells 		down_write(&u_keyring->sem);
1228ed0ac5c7SDavid Howells 		down_write_nested(&l_keyring->sem, 1);
1229ed0ac5c7SDavid Howells 	}
1230ed0ac5c7SDavid Howells 
1231ed0ac5c7SDavid Howells 	/* Serialise link/link calls to prevent parallel calls causing a cycle
1232ed0ac5c7SDavid Howells 	 * when linking two keyring in opposite orders.
1233ed0ac5c7SDavid Howells 	 */
1234ed0ac5c7SDavid Howells 	if (index_key->type == &key_type_keyring)
1235ed0ac5c7SDavid Howells 		mutex_lock(&keyring_serialise_link_lock);
1236ed0ac5c7SDavid Howells 
1237ed0ac5c7SDavid Howells 	return 0;
1238ed0ac5c7SDavid Howells }
1239ed0ac5c7SDavid Howells 
1240ed0ac5c7SDavid Howells /*
1241973c9f4fSDavid Howells  * Preallocate memory so that a key can be linked into to a keyring.
12421da177e4SLinus Torvalds  */
1243b2a4df20SDavid Howells int __key_link_begin(struct key *keyring,
1244b2a4df20SDavid Howells 		     const struct keyring_index_key *index_key,
1245b2a4df20SDavid Howells 		     struct assoc_array_edit **_edit)
12461da177e4SLinus Torvalds {
1247b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1248b2a4df20SDavid Howells 	int ret;
12491da177e4SLinus Torvalds 
125016feef43SDavid Howells 	kenter("%d,%s,%s,",
1251b2a4df20SDavid Howells 	       keyring->serial, index_key->type->name, index_key->description);
1252b2a4df20SDavid Howells 
1253b2a4df20SDavid Howells 	BUG_ON(index_key->desc_len == 0);
1254df593ee2SDavid Howells 	BUG_ON(*_edit != NULL);
1255f70e2e06SDavid Howells 
1256df593ee2SDavid Howells 	*_edit = NULL;
1257f70e2e06SDavid Howells 
12581da177e4SLinus Torvalds 	ret = -EKEYREVOKED;
125976d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1260df593ee2SDavid Howells 		goto error;
12611da177e4SLinus Torvalds 
1262b2a4df20SDavid Howells 	/* Create an edit script that will insert/replace the key in the
1263b2a4df20SDavid Howells 	 * keyring tree.
1264b2a4df20SDavid Howells 	 */
1265b2a4df20SDavid Howells 	edit = assoc_array_insert(&keyring->keys,
1266b2a4df20SDavid Howells 				  &keyring_assoc_array_ops,
1267b2a4df20SDavid Howells 				  index_key,
1268b2a4df20SDavid Howells 				  NULL);
1269b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1270b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1271df593ee2SDavid Howells 		goto error;
1272034faeb9SDavid Howells 	}
1273034faeb9SDavid Howells 
1274034faeb9SDavid Howells 	/* If we're not replacing a link in-place then we're going to need some
1275034faeb9SDavid Howells 	 * extra quota.
1276034faeb9SDavid Howells 	 */
1277034faeb9SDavid Howells 	if (!edit->dead_leaf) {
1278034faeb9SDavid Howells 		ret = key_payload_reserve(keyring,
1279034faeb9SDavid Howells 					  keyring->datalen + KEYQUOTA_LINK_BYTES);
1280034faeb9SDavid Howells 		if (ret < 0)
1281034faeb9SDavid Howells 			goto error_cancel;
12821da177e4SLinus Torvalds 	}
12831da177e4SLinus Torvalds 
1284b2a4df20SDavid Howells 	*_edit = edit;
1285f70e2e06SDavid Howells 	kleave(" = 0");
1286f70e2e06SDavid Howells 	return 0;
12871da177e4SLinus Torvalds 
1288034faeb9SDavid Howells error_cancel:
1289034faeb9SDavid Howells 	assoc_array_cancel_edit(edit);
1290df593ee2SDavid Howells error:
1291f70e2e06SDavid Howells 	kleave(" = %d", ret);
1292f70e2e06SDavid Howells 	return ret;
1293f70e2e06SDavid Howells }
12941da177e4SLinus Torvalds 
1295f70e2e06SDavid Howells /*
1296973c9f4fSDavid Howells  * Check already instantiated keys aren't going to be a problem.
1297973c9f4fSDavid Howells  *
1298973c9f4fSDavid Howells  * The caller must have called __key_link_begin(). Don't need to call this for
1299973c9f4fSDavid Howells  * keys that were created since __key_link_begin() was called.
1300f70e2e06SDavid Howells  */
1301f70e2e06SDavid Howells int __key_link_check_live_key(struct key *keyring, struct key *key)
1302f70e2e06SDavid Howells {
1303f70e2e06SDavid Howells 	if (key->type == &key_type_keyring)
1304f70e2e06SDavid Howells 		/* check that we aren't going to create a cycle by linking one
1305f70e2e06SDavid Howells 		 * keyring to another */
1306f70e2e06SDavid Howells 		return keyring_detect_cycle(keyring, key);
1307f70e2e06SDavid Howells 	return 0;
1308f70e2e06SDavid Howells }
13091da177e4SLinus Torvalds 
1310f70e2e06SDavid Howells /*
1311973c9f4fSDavid Howells  * Link a key into to a keyring.
1312973c9f4fSDavid Howells  *
1313973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.  Discards any
1314973c9f4fSDavid Howells  * already extant link to matching key if there is one, so that each keyring
1315973c9f4fSDavid Howells  * holds at most one link to any given key of a particular type+description
1316973c9f4fSDavid Howells  * combination.
1317f70e2e06SDavid Howells  */
1318b2a4df20SDavid Howells void __key_link(struct key *key, struct assoc_array_edit **_edit)
1319f70e2e06SDavid Howells {
1320ccc3e6d9SDavid Howells 	__key_get(key);
1321b2a4df20SDavid Howells 	assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key));
1322b2a4df20SDavid Howells 	assoc_array_apply_edit(*_edit);
1323b2a4df20SDavid Howells 	*_edit = NULL;
1324f70e2e06SDavid Howells }
1325f70e2e06SDavid Howells 
1326f70e2e06SDavid Howells /*
1327973c9f4fSDavid Howells  * Finish linking a key into to a keyring.
1328973c9f4fSDavid Howells  *
1329973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.
1330f70e2e06SDavid Howells  */
133116feef43SDavid Howells void __key_link_end(struct key *keyring,
133216feef43SDavid Howells 		    const struct keyring_index_key *index_key,
1333b2a4df20SDavid Howells 		    struct assoc_array_edit *edit)
1334f70e2e06SDavid Howells 	__releases(&keyring->sem)
13353be59f74SDavid Howells 	__releases(&keyring_serialise_link_lock)
1336f70e2e06SDavid Howells {
133716feef43SDavid Howells 	BUG_ON(index_key->type == NULL);
1338b2a4df20SDavid Howells 	kenter("%d,%s,", keyring->serial, index_key->type->name);
1339f70e2e06SDavid Howells 
1340ca4da5ddSColin Ian King 	if (edit) {
1341ca4da5ddSColin Ian King 		if (!edit->dead_leaf) {
1342f70e2e06SDavid Howells 			key_payload_reserve(keyring,
1343b2a4df20SDavid Howells 				keyring->datalen - KEYQUOTA_LINK_BYTES);
1344ca4da5ddSColin Ian King 		}
1345b2a4df20SDavid Howells 		assoc_array_cancel_edit(edit);
1346f70e2e06SDavid Howells 	}
1347f70e2e06SDavid Howells 	up_write(&keyring->sem);
1348df593ee2SDavid Howells 
1349df593ee2SDavid Howells 	if (index_key->type == &key_type_keyring)
1350df593ee2SDavid Howells 		mutex_unlock(&keyring_serialise_link_lock);
1351f70e2e06SDavid Howells }
1352f70e2e06SDavid Howells 
13535ac7eaceSDavid Howells /*
13545ac7eaceSDavid Howells  * Check addition of keys to restricted keyrings.
13555ac7eaceSDavid Howells  */
13565ac7eaceSDavid Howells static int __key_link_check_restriction(struct key *keyring, struct key *key)
13575ac7eaceSDavid Howells {
13582b6aa412SMat Martineau 	if (!keyring->restrict_link || !keyring->restrict_link->check)
13595ac7eaceSDavid Howells 		return 0;
13602b6aa412SMat Martineau 	return keyring->restrict_link->check(keyring, key->type, &key->payload,
13612b6aa412SMat Martineau 					     keyring->restrict_link->key);
13625ac7eaceSDavid Howells }
13635ac7eaceSDavid Howells 
1364973c9f4fSDavid Howells /**
1365973c9f4fSDavid Howells  * key_link - Link a key to a keyring
1366973c9f4fSDavid Howells  * @keyring: The keyring to make the link in.
1367973c9f4fSDavid Howells  * @key: The key to link to.
1368973c9f4fSDavid Howells  *
1369973c9f4fSDavid Howells  * Make a link in a keyring to a key, such that the keyring holds a reference
1370973c9f4fSDavid Howells  * on that key and the key can potentially be found by searching that keyring.
1371973c9f4fSDavid Howells  *
1372973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore and will consume some
1373973c9f4fSDavid Howells  * of the user's key data quota to hold the link.
1374973c9f4fSDavid Howells  *
1375973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
1376973c9f4fSDavid Howells  * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
1377973c9f4fSDavid Howells  * full, -EDQUOT if there is insufficient key data quota remaining to add
1378973c9f4fSDavid Howells  * another link or -ENOMEM if there's insufficient memory.
1379973c9f4fSDavid Howells  *
1380973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1381973c9f4fSDavid Howells  * be made (the keyring should have Write permission and the key Link
1382973c9f4fSDavid Howells  * permission).
13831da177e4SLinus Torvalds  */
13841da177e4SLinus Torvalds int key_link(struct key *keyring, struct key *key)
13851da177e4SLinus Torvalds {
1386df593ee2SDavid Howells 	struct assoc_array_edit *edit = NULL;
13871da177e4SLinus Torvalds 	int ret;
13881da177e4SLinus Torvalds 
1389fff29291SElena Reshetova 	kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage));
1390b2a4df20SDavid Howells 
13911da177e4SLinus Torvalds 	key_check(keyring);
13921da177e4SLinus Torvalds 	key_check(key);
13931da177e4SLinus Torvalds 
1394df593ee2SDavid Howells 	ret = __key_link_lock(keyring, &key->index_key);
1395df593ee2SDavid Howells 	if (ret < 0)
1396df593ee2SDavid Howells 		goto error;
1397df593ee2SDavid Howells 
1398b2a4df20SDavid Howells 	ret = __key_link_begin(keyring, &key->index_key, &edit);
1399df593ee2SDavid Howells 	if (ret < 0)
1400df593ee2SDavid Howells 		goto error_end;
1401df593ee2SDavid Howells 
1402fff29291SElena Reshetova 	kdebug("begun {%d,%d}", keyring->serial, refcount_read(&keyring->usage));
14035ac7eaceSDavid Howells 	ret = __key_link_check_restriction(keyring, key);
14045ac7eaceSDavid Howells 	if (ret == 0)
1405f70e2e06SDavid Howells 		ret = __key_link_check_live_key(keyring, key);
1406f70e2e06SDavid Howells 	if (ret == 0)
1407b2a4df20SDavid Howells 		__key_link(key, &edit);
14081da177e4SLinus Torvalds 
1409df593ee2SDavid Howells error_end:
1410df593ee2SDavid Howells 	__key_link_end(keyring, &key->index_key, edit);
1411df593ee2SDavid Howells error:
1412fff29291SElena Reshetova 	kleave(" = %d {%d,%d}", ret, keyring->serial, refcount_read(&keyring->usage));
14131da177e4SLinus Torvalds 	return ret;
1414f70e2e06SDavid Howells }
14151da177e4SLinus Torvalds EXPORT_SYMBOL(key_link);
14161da177e4SLinus Torvalds 
1417eb0f68cbSDavid Howells /*
1418eb0f68cbSDavid Howells  * Lock a keyring for unlink.
1419eb0f68cbSDavid Howells  */
1420eb0f68cbSDavid Howells static int __key_unlink_lock(struct key *keyring)
1421eb0f68cbSDavid Howells 	__acquires(&keyring->sem)
1422eb0f68cbSDavid Howells {
1423eb0f68cbSDavid Howells 	if (keyring->type != &key_type_keyring)
1424eb0f68cbSDavid Howells 		return -ENOTDIR;
1425eb0f68cbSDavid Howells 
1426eb0f68cbSDavid Howells 	down_write(&keyring->sem);
1427eb0f68cbSDavid Howells 	return 0;
1428eb0f68cbSDavid Howells }
1429eb0f68cbSDavid Howells 
1430eb0f68cbSDavid Howells /*
1431eb0f68cbSDavid Howells  * Begin the process of unlinking a key from a keyring.
1432eb0f68cbSDavid Howells  */
1433eb0f68cbSDavid Howells static int __key_unlink_begin(struct key *keyring, struct key *key,
1434eb0f68cbSDavid Howells 			      struct assoc_array_edit **_edit)
1435eb0f68cbSDavid Howells {
1436eb0f68cbSDavid Howells 	struct assoc_array_edit *edit;
1437eb0f68cbSDavid Howells 
1438eb0f68cbSDavid Howells 	BUG_ON(*_edit != NULL);
1439eb0f68cbSDavid Howells 
1440eb0f68cbSDavid Howells 	edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops,
1441eb0f68cbSDavid Howells 				  &key->index_key);
1442eb0f68cbSDavid Howells 	if (IS_ERR(edit))
1443eb0f68cbSDavid Howells 		return PTR_ERR(edit);
1444eb0f68cbSDavid Howells 
1445eb0f68cbSDavid Howells 	if (!edit)
1446eb0f68cbSDavid Howells 		return -ENOENT;
1447eb0f68cbSDavid Howells 
1448eb0f68cbSDavid Howells 	*_edit = edit;
1449eb0f68cbSDavid Howells 	return 0;
1450eb0f68cbSDavid Howells }
1451eb0f68cbSDavid Howells 
1452eb0f68cbSDavid Howells /*
1453eb0f68cbSDavid Howells  * Apply an unlink change.
1454eb0f68cbSDavid Howells  */
1455eb0f68cbSDavid Howells static void __key_unlink(struct key *keyring, struct key *key,
1456eb0f68cbSDavid Howells 			 struct assoc_array_edit **_edit)
1457eb0f68cbSDavid Howells {
1458eb0f68cbSDavid Howells 	assoc_array_apply_edit(*_edit);
1459eb0f68cbSDavid Howells 	*_edit = NULL;
1460eb0f68cbSDavid Howells 	key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES);
1461eb0f68cbSDavid Howells }
1462eb0f68cbSDavid Howells 
1463eb0f68cbSDavid Howells /*
1464eb0f68cbSDavid Howells  * Finish unlinking a key from to a keyring.
1465eb0f68cbSDavid Howells  */
1466eb0f68cbSDavid Howells static void __key_unlink_end(struct key *keyring,
1467eb0f68cbSDavid Howells 			     struct key *key,
1468eb0f68cbSDavid Howells 			     struct assoc_array_edit *edit)
1469eb0f68cbSDavid Howells 	__releases(&keyring->sem)
1470eb0f68cbSDavid Howells {
1471eb0f68cbSDavid Howells 	if (edit)
1472eb0f68cbSDavid Howells 		assoc_array_cancel_edit(edit);
1473eb0f68cbSDavid Howells 	up_write(&keyring->sem);
1474eb0f68cbSDavid Howells }
1475eb0f68cbSDavid Howells 
1476973c9f4fSDavid Howells /**
1477973c9f4fSDavid Howells  * key_unlink - Unlink the first link to a key from a keyring.
1478973c9f4fSDavid Howells  * @keyring: The keyring to remove the link from.
1479973c9f4fSDavid Howells  * @key: The key the link is to.
1480973c9f4fSDavid Howells  *
1481973c9f4fSDavid Howells  * Remove a link from a keyring to a key.
1482973c9f4fSDavid Howells  *
1483973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore.
1484973c9f4fSDavid Howells  *
1485973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
1486973c9f4fSDavid Howells  * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
1487973c9f4fSDavid Howells  * memory.
1488973c9f4fSDavid Howells  *
1489973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1490973c9f4fSDavid Howells  * be removed (the keyring should have Write permission; no permissions are
1491973c9f4fSDavid Howells  * required on the key).
14921da177e4SLinus Torvalds  */
14931da177e4SLinus Torvalds int key_unlink(struct key *keyring, struct key *key)
14941da177e4SLinus Torvalds {
1495eb0f68cbSDavid Howells 	struct assoc_array_edit *edit = NULL;
1496b2a4df20SDavid Howells 	int ret;
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	key_check(keyring);
14991da177e4SLinus Torvalds 	key_check(key);
15001da177e4SLinus Torvalds 
1501eb0f68cbSDavid Howells 	ret = __key_unlink_lock(keyring);
1502eb0f68cbSDavid Howells 	if (ret < 0)
1503eb0f68cbSDavid Howells 		return ret;
15041da177e4SLinus Torvalds 
1505eb0f68cbSDavid Howells 	ret = __key_unlink_begin(keyring, key, &edit);
1506eb0f68cbSDavid Howells 	if (ret == 0)
1507eb0f68cbSDavid Howells 		__key_unlink(keyring, key, &edit);
1508eb0f68cbSDavid Howells 	__key_unlink_end(keyring, key, edit);
1509b2a4df20SDavid Howells 	return ret;
1510a8b17ed0SDavid Howells }
15111da177e4SLinus Torvalds EXPORT_SYMBOL(key_unlink);
15121da177e4SLinus Torvalds 
1513973c9f4fSDavid Howells /**
1514ed0ac5c7SDavid Howells  * key_move - Move a key from one keyring to another
1515ed0ac5c7SDavid Howells  * @key: The key to move
1516ed0ac5c7SDavid Howells  * @from_keyring: The keyring to remove the link from.
1517ed0ac5c7SDavid Howells  * @to_keyring: The keyring to make the link in.
1518ed0ac5c7SDavid Howells  * @flags: Qualifying flags, such as KEYCTL_MOVE_EXCL.
1519ed0ac5c7SDavid Howells  *
1520ed0ac5c7SDavid Howells  * Make a link in @to_keyring to a key, such that the keyring holds a reference
1521ed0ac5c7SDavid Howells  * on that key and the key can potentially be found by searching that keyring
1522ed0ac5c7SDavid Howells  * whilst simultaneously removing a link to the key from @from_keyring.
1523ed0ac5c7SDavid Howells  *
1524ed0ac5c7SDavid Howells  * This function will write-lock both keyring's semaphores and will consume
1525ed0ac5c7SDavid Howells  * some of the user's key data quota to hold the link on @to_keyring.
1526ed0ac5c7SDavid Howells  *
1527ed0ac5c7SDavid Howells  * Returns 0 if successful, -ENOTDIR if either keyring isn't a keyring,
1528ed0ac5c7SDavid Howells  * -EKEYREVOKED if either keyring has been revoked, -ENFILE if the second
1529ed0ac5c7SDavid Howells  * keyring is full, -EDQUOT if there is insufficient key data quota remaining
1530ed0ac5c7SDavid Howells  * to add another link or -ENOMEM if there's insufficient memory.  If
1531ed0ac5c7SDavid Howells  * KEYCTL_MOVE_EXCL is set, then -EEXIST will be returned if there's already a
1532ed0ac5c7SDavid Howells  * matching key in @to_keyring.
1533ed0ac5c7SDavid Howells  *
1534ed0ac5c7SDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1535ed0ac5c7SDavid Howells  * be made (the keyring should have Write permission and the key Link
1536ed0ac5c7SDavid Howells  * permission).
1537ed0ac5c7SDavid Howells  */
1538ed0ac5c7SDavid Howells int key_move(struct key *key,
1539ed0ac5c7SDavid Howells 	     struct key *from_keyring,
1540ed0ac5c7SDavid Howells 	     struct key *to_keyring,
1541ed0ac5c7SDavid Howells 	     unsigned int flags)
1542ed0ac5c7SDavid Howells {
1543ed0ac5c7SDavid Howells 	struct assoc_array_edit *from_edit = NULL, *to_edit = NULL;
1544ed0ac5c7SDavid Howells 	int ret;
1545ed0ac5c7SDavid Howells 
1546ed0ac5c7SDavid Howells 	kenter("%d,%d,%d", key->serial, from_keyring->serial, to_keyring->serial);
1547ed0ac5c7SDavid Howells 
1548ed0ac5c7SDavid Howells 	if (from_keyring == to_keyring)
1549ed0ac5c7SDavid Howells 		return 0;
1550ed0ac5c7SDavid Howells 
1551ed0ac5c7SDavid Howells 	key_check(key);
1552ed0ac5c7SDavid Howells 	key_check(from_keyring);
1553ed0ac5c7SDavid Howells 	key_check(to_keyring);
1554ed0ac5c7SDavid Howells 
1555ed0ac5c7SDavid Howells 	ret = __key_move_lock(from_keyring, to_keyring, &key->index_key);
1556ed0ac5c7SDavid Howells 	if (ret < 0)
1557ed0ac5c7SDavid Howells 		goto out;
1558ed0ac5c7SDavid Howells 	ret = __key_unlink_begin(from_keyring, key, &from_edit);
1559ed0ac5c7SDavid Howells 	if (ret < 0)
1560ed0ac5c7SDavid Howells 		goto error;
1561ed0ac5c7SDavid Howells 	ret = __key_link_begin(to_keyring, &key->index_key, &to_edit);
1562ed0ac5c7SDavid Howells 	if (ret < 0)
1563ed0ac5c7SDavid Howells 		goto error;
1564ed0ac5c7SDavid Howells 
1565ed0ac5c7SDavid Howells 	ret = -EEXIST;
1566ed0ac5c7SDavid Howells 	if (to_edit->dead_leaf && (flags & KEYCTL_MOVE_EXCL))
1567ed0ac5c7SDavid Howells 		goto error;
1568ed0ac5c7SDavid Howells 
1569ed0ac5c7SDavid Howells 	ret = __key_link_check_restriction(to_keyring, key);
1570ed0ac5c7SDavid Howells 	if (ret < 0)
1571ed0ac5c7SDavid Howells 		goto error;
1572ed0ac5c7SDavid Howells 	ret = __key_link_check_live_key(to_keyring, key);
1573ed0ac5c7SDavid Howells 	if (ret < 0)
1574ed0ac5c7SDavid Howells 		goto error;
1575ed0ac5c7SDavid Howells 
1576ed0ac5c7SDavid Howells 	__key_unlink(from_keyring, key, &from_edit);
1577ed0ac5c7SDavid Howells 	__key_link(key, &to_edit);
1578ed0ac5c7SDavid Howells error:
1579ed0ac5c7SDavid Howells 	__key_link_end(to_keyring, &key->index_key, to_edit);
1580ed0ac5c7SDavid Howells 	__key_unlink_end(from_keyring, key, from_edit);
1581ed0ac5c7SDavid Howells out:
1582ed0ac5c7SDavid Howells 	kleave(" = %d", ret);
1583ed0ac5c7SDavid Howells 	return ret;
1584ed0ac5c7SDavid Howells }
1585ed0ac5c7SDavid Howells EXPORT_SYMBOL(key_move);
1586ed0ac5c7SDavid Howells 
1587ed0ac5c7SDavid Howells /**
1588973c9f4fSDavid Howells  * keyring_clear - Clear a keyring
1589973c9f4fSDavid Howells  * @keyring: The keyring to clear.
1590973c9f4fSDavid Howells  *
1591973c9f4fSDavid Howells  * Clear the contents of the specified keyring.
1592973c9f4fSDavid Howells  *
1593973c9f4fSDavid Howells  * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
15941da177e4SLinus Torvalds  */
15951da177e4SLinus Torvalds int keyring_clear(struct key *keyring)
15961da177e4SLinus Torvalds {
1597b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
159876d8aeabSDavid Howells 	int ret;
15991da177e4SLinus Torvalds 
1600b2a4df20SDavid Howells 	if (keyring->type != &key_type_keyring)
1601b2a4df20SDavid Howells 		return -ENOTDIR;
1602b2a4df20SDavid Howells 
16031da177e4SLinus Torvalds 	down_write(&keyring->sem);
16041da177e4SLinus Torvalds 
1605b2a4df20SDavid Howells 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1606b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1607b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1608b2a4df20SDavid Howells 	} else {
1609b2a4df20SDavid Howells 		if (edit)
1610b2a4df20SDavid Howells 			assoc_array_apply_edit(edit);
1611b2a4df20SDavid Howells 		key_payload_reserve(keyring, 0);
16121da177e4SLinus Torvalds 		ret = 0;
16131da177e4SLinus Torvalds 	}
16141da177e4SLinus Torvalds 
1615b2a4df20SDavid Howells 	up_write(&keyring->sem);
16161da177e4SLinus Torvalds 	return ret;
1617a8b17ed0SDavid Howells }
16181da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_clear);
161931204ed9SDavid Howells 
162031204ed9SDavid Howells /*
1621973c9f4fSDavid Howells  * Dispose of the links from a revoked keyring.
1622973c9f4fSDavid Howells  *
1623973c9f4fSDavid Howells  * This is called with the key sem write-locked.
162431204ed9SDavid Howells  */
162531204ed9SDavid Howells static void keyring_revoke(struct key *keyring)
162631204ed9SDavid Howells {
1627b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1628f0641cbaSDavid Howells 
1629b2a4df20SDavid Howells 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1630b2a4df20SDavid Howells 	if (!IS_ERR(edit)) {
1631b2a4df20SDavid Howells 		if (edit)
1632b2a4df20SDavid Howells 			assoc_array_apply_edit(edit);
163331204ed9SDavid Howells 		key_payload_reserve(keyring, 0);
163431204ed9SDavid Howells 	}
1635a8b17ed0SDavid Howells }
16365d135440SDavid Howells 
163762fe3182SDavid Howells static bool keyring_gc_select_iterator(void *object, void *iterator_data)
1638b2a4df20SDavid Howells {
1639b2a4df20SDavid Howells 	struct key *key = keyring_ptr_to_key(object);
1640074d5898SBaolin Wang 	time64_t *limit = iterator_data;
1641b2a4df20SDavid Howells 
1642b2a4df20SDavid Howells 	if (key_is_dead(key, *limit))
1643b2a4df20SDavid Howells 		return false;
1644b2a4df20SDavid Howells 	key_get(key);
1645b2a4df20SDavid Howells 	return true;
1646b2a4df20SDavid Howells }
1647b2a4df20SDavid Howells 
164862fe3182SDavid Howells static int keyring_gc_check_iterator(const void *object, void *iterator_data)
164962fe3182SDavid Howells {
165062fe3182SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
1651074d5898SBaolin Wang 	time64_t *limit = iterator_data;
165262fe3182SDavid Howells 
165362fe3182SDavid Howells 	key_check(key);
165462fe3182SDavid Howells 	return key_is_dead(key, *limit);
165562fe3182SDavid Howells }
165662fe3182SDavid Howells 
16575d135440SDavid Howells /*
165862fe3182SDavid Howells  * Garbage collect pointers from a keyring.
1659973c9f4fSDavid Howells  *
166062fe3182SDavid Howells  * Not called with any locks held.  The keyring's key struct will not be
166162fe3182SDavid Howells  * deallocated under us as only our caller may deallocate it.
16625d135440SDavid Howells  */
1663074d5898SBaolin Wang void keyring_gc(struct key *keyring, time64_t limit)
16645d135440SDavid Howells {
166562fe3182SDavid Howells 	int result;
16665d135440SDavid Howells 
166762fe3182SDavid Howells 	kenter("%x{%s}", keyring->serial, keyring->description ?: "");
166862fe3182SDavid Howells 
166962fe3182SDavid Howells 	if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
167062fe3182SDavid Howells 			      (1 << KEY_FLAG_REVOKED)))
167162fe3182SDavid Howells 		goto dont_gc;
167262fe3182SDavid Howells 
167362fe3182SDavid Howells 	/* scan the keyring looking for dead keys */
167462fe3182SDavid Howells 	rcu_read_lock();
167562fe3182SDavid Howells 	result = assoc_array_iterate(&keyring->keys,
167662fe3182SDavid Howells 				     keyring_gc_check_iterator, &limit);
167762fe3182SDavid Howells 	rcu_read_unlock();
167862fe3182SDavid Howells 	if (result == true)
167962fe3182SDavid Howells 		goto do_gc;
168062fe3182SDavid Howells 
168162fe3182SDavid Howells dont_gc:
168262fe3182SDavid Howells 	kleave(" [no gc]");
168362fe3182SDavid Howells 	return;
168462fe3182SDavid Howells 
168562fe3182SDavid Howells do_gc:
16865d135440SDavid Howells 	down_write(&keyring->sem);
1687b2a4df20SDavid Howells 	assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops,
168862fe3182SDavid Howells 		       keyring_gc_select_iterator, &limit);
16895d135440SDavid Howells 	up_write(&keyring->sem);
169062fe3182SDavid Howells 	kleave(" [gc]");
16915d135440SDavid Howells }
16922b6aa412SMat Martineau 
16932b6aa412SMat Martineau /*
16942b6aa412SMat Martineau  * Garbage collect restriction pointers from a keyring.
16952b6aa412SMat Martineau  *
16962b6aa412SMat Martineau  * Keyring restrictions are associated with a key type, and must be cleaned
16972b6aa412SMat Martineau  * up if the key type is unregistered. The restriction is altered to always
16982b6aa412SMat Martineau  * reject additional keys so a keyring cannot be opened up by unregistering
16992b6aa412SMat Martineau  * a key type.
17002b6aa412SMat Martineau  *
17012b6aa412SMat Martineau  * Not called with any keyring locks held. The keyring's key struct will not
17022b6aa412SMat Martineau  * be deallocated under us as only our caller may deallocate it.
17032b6aa412SMat Martineau  *
17042b6aa412SMat Martineau  * The caller is required to hold key_types_sem and dead_type->sem. This is
17052b6aa412SMat Martineau  * fulfilled by key_gc_keytype() holding the locks on behalf of
17062b6aa412SMat Martineau  * key_garbage_collector(), which it invokes on a workqueue.
17072b6aa412SMat Martineau  */
17082b6aa412SMat Martineau void keyring_restriction_gc(struct key *keyring, struct key_type *dead_type)
17092b6aa412SMat Martineau {
17102b6aa412SMat Martineau 	struct key_restriction *keyres;
17112b6aa412SMat Martineau 
17122b6aa412SMat Martineau 	kenter("%x{%s}", keyring->serial, keyring->description ?: "");
17132b6aa412SMat Martineau 
17142b6aa412SMat Martineau 	/*
17152b6aa412SMat Martineau 	 * keyring->restrict_link is only assigned at key allocation time
17162b6aa412SMat Martineau 	 * or with the key type locked, so the only values that could be
17172b6aa412SMat Martineau 	 * concurrently assigned to keyring->restrict_link are for key
17182b6aa412SMat Martineau 	 * types other than dead_type. Given this, it's ok to check
17192b6aa412SMat Martineau 	 * the key type before acquiring keyring->sem.
17202b6aa412SMat Martineau 	 */
17212b6aa412SMat Martineau 	if (!dead_type || !keyring->restrict_link ||
17222b6aa412SMat Martineau 	    keyring->restrict_link->keytype != dead_type) {
17232b6aa412SMat Martineau 		kleave(" [no restriction gc]");
17242b6aa412SMat Martineau 		return;
17252b6aa412SMat Martineau 	}
17262b6aa412SMat Martineau 
17272b6aa412SMat Martineau 	/* Lock the keyring to ensure that a link is not in progress */
17282b6aa412SMat Martineau 	down_write(&keyring->sem);
17292b6aa412SMat Martineau 
17302b6aa412SMat Martineau 	keyres = keyring->restrict_link;
17312b6aa412SMat Martineau 
17322b6aa412SMat Martineau 	keyres->check = restrict_link_reject;
17332b6aa412SMat Martineau 
17342b6aa412SMat Martineau 	key_put(keyres->key);
17352b6aa412SMat Martineau 	keyres->key = NULL;
17362b6aa412SMat Martineau 	keyres->keytype = NULL;
17372b6aa412SMat Martineau 
17382b6aa412SMat Martineau 	up_write(&keyring->sem);
17392b6aa412SMat Martineau 
17402b6aa412SMat Martineau 	kleave(" [restriction gc]");
17412b6aa412SMat Martineau }
1742