xref: /openbmc/linux/security/keys/keyring.c (revision e645016abc803dafc75e4b8f6e4118f088900ffb)
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 
121da177e4SLinus Torvalds #include <linux/module.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>
19e9e349b0SDavid Howells #include <keys/keyring-type.h>
20b2a4df20SDavid Howells #include <keys/user-type.h>
21b2a4df20SDavid Howells #include <linux/assoc_array_priv.h>
22512ea3bcSChihau Chau #include <linux/uaccess.h>
231da177e4SLinus Torvalds #include "internal.h"
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /*
26973c9f4fSDavid Howells  * When plumbing the depths of the key tree, this sets a hard limit
27973c9f4fSDavid Howells  * set on how deep we're willing to go.
281da177e4SLinus Torvalds  */
291da177e4SLinus Torvalds #define KEYRING_SEARCH_MAX_DEPTH 6
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds /*
32973c9f4fSDavid Howells  * We keep all named keyrings in a hash to speed looking them up.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds #define KEYRING_NAME_HASH_SIZE	(1 << 5)
351da177e4SLinus Torvalds 
36b2a4df20SDavid Howells /*
37b2a4df20SDavid Howells  * We mark pointers we pass to the associative array with bit 1 set if
38b2a4df20SDavid Howells  * they're keyrings and clear otherwise.
39b2a4df20SDavid Howells  */
40b2a4df20SDavid Howells #define KEYRING_PTR_SUBTYPE	0x2UL
41b2a4df20SDavid Howells 
42b2a4df20SDavid Howells static inline bool keyring_ptr_is_keyring(const struct assoc_array_ptr *x)
43b2a4df20SDavid Howells {
44b2a4df20SDavid Howells 	return (unsigned long)x & KEYRING_PTR_SUBTYPE;
45b2a4df20SDavid Howells }
46b2a4df20SDavid Howells static inline struct key *keyring_ptr_to_key(const struct assoc_array_ptr *x)
47b2a4df20SDavid Howells {
48b2a4df20SDavid Howells 	void *object = assoc_array_ptr_to_leaf(x);
49b2a4df20SDavid Howells 	return (struct key *)((unsigned long)object & ~KEYRING_PTR_SUBTYPE);
50b2a4df20SDavid Howells }
51b2a4df20SDavid Howells static inline void *keyring_key_to_ptr(struct key *key)
52b2a4df20SDavid Howells {
53b2a4df20SDavid Howells 	if (key->type == &key_type_keyring)
54b2a4df20SDavid Howells 		return (void *)((unsigned long)key | KEYRING_PTR_SUBTYPE);
55b2a4df20SDavid Howells 	return key;
56b2a4df20SDavid Howells }
57b2a4df20SDavid Howells 
581da177e4SLinus Torvalds static struct list_head	keyring_name_hash[KEYRING_NAME_HASH_SIZE];
591da177e4SLinus Torvalds static DEFINE_RWLOCK(keyring_name_lock);
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static inline unsigned keyring_hash(const char *desc)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	unsigned bucket = 0;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	for (; *desc; desc++)
661da177e4SLinus Torvalds 		bucket += (unsigned char)*desc;
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	return bucket & (KEYRING_NAME_HASH_SIZE - 1);
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  */
1031ae8f407SAdrian Bunk static DECLARE_RWSEM(keyring_serialise_link_sem);
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /*
106973c9f4fSDavid Howells  * Publish the name of a keyring so that it can be found by name (if it has
107973c9f4fSDavid Howells  * one).
1081da177e4SLinus Torvalds  */
10969664cf1SDavid Howells static void keyring_publish_name(struct key *keyring)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	int bucket;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	if (keyring->description) {
1141da177e4SLinus Torvalds 		bucket = keyring_hash(keyring->description);
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 		if (!keyring_name_hash[bucket].next)
1191da177e4SLinus Torvalds 			INIT_LIST_HEAD(&keyring_name_hash[bucket]);
1201da177e4SLinus Torvalds 
121146aa8b1SDavid Howells 		list_add_tail(&keyring->name_link,
1221da177e4SLinus Torvalds 			      &keyring_name_hash[bucket]);
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
1251da177e4SLinus Torvalds 	}
126a8b17ed0SDavid Howells }
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /*
1295d19e20bSDavid Howells  * Preparse a keyring payload
1305d19e20bSDavid Howells  */
1315d19e20bSDavid Howells static int keyring_preparse(struct key_preparsed_payload *prep)
1325d19e20bSDavid Howells {
1335d19e20bSDavid Howells 	return prep->datalen != 0 ? -EINVAL : 0;
1345d19e20bSDavid Howells }
1355d19e20bSDavid Howells 
1365d19e20bSDavid Howells /*
1375d19e20bSDavid Howells  * Free a preparse of a user defined key payload
1385d19e20bSDavid Howells  */
1395d19e20bSDavid Howells static void keyring_free_preparse(struct key_preparsed_payload *prep)
1405d19e20bSDavid Howells {
1415d19e20bSDavid Howells }
1425d19e20bSDavid Howells 
1435d19e20bSDavid Howells /*
144973c9f4fSDavid Howells  * Initialise a keyring.
145973c9f4fSDavid Howells  *
146973c9f4fSDavid Howells  * Returns 0 on success, -EINVAL if given any data.
1471da177e4SLinus Torvalds  */
1481da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
149cf7f601cSDavid Howells 			       struct key_preparsed_payload *prep)
1501da177e4SLinus Torvalds {
151b2a4df20SDavid Howells 	assoc_array_init(&keyring->keys);
1521da177e4SLinus Torvalds 	/* make the keyring available by name if it has one */
1531da177e4SLinus Torvalds 	keyring_publish_name(keyring);
1545d19e20bSDavid Howells 	return 0;
155a8b17ed0SDavid Howells }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /*
158b2a4df20SDavid Howells  * Multiply 64-bits by 32-bits to 96-bits and fold back to 64-bit.  Ideally we'd
159b2a4df20SDavid Howells  * fold the carry back too, but that requires inline asm.
1601da177e4SLinus Torvalds  */
161b2a4df20SDavid Howells static u64 mult_64x32_and_fold(u64 x, u32 y)
1621da177e4SLinus Torvalds {
163b2a4df20SDavid Howells 	u64 hi = (u64)(u32)(x >> 32) * y;
164b2a4df20SDavid Howells 	u64 lo = (u64)(u32)(x) * y;
165b2a4df20SDavid Howells 	return lo + ((u64)(u32)hi << 32) + (u32)(hi >> 32);
166a8b17ed0SDavid Howells }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds /*
169b2a4df20SDavid Howells  * Hash a key type and description.
170b2a4df20SDavid Howells  */
171b2a4df20SDavid Howells static unsigned long hash_key_type_and_desc(const struct keyring_index_key *index_key)
172b2a4df20SDavid Howells {
173b2a4df20SDavid Howells 	const unsigned level_shift = ASSOC_ARRAY_LEVEL_STEP;
174d54e58b7SDavid Howells 	const unsigned long fan_mask = ASSOC_ARRAY_FAN_MASK;
175b2a4df20SDavid Howells 	const char *description = index_key->description;
176b2a4df20SDavid Howells 	unsigned long hash, type;
177b2a4df20SDavid Howells 	u32 piece;
178b2a4df20SDavid Howells 	u64 acc;
179b2a4df20SDavid Howells 	int n, desc_len = index_key->desc_len;
180b2a4df20SDavid Howells 
181b2a4df20SDavid Howells 	type = (unsigned long)index_key->type;
182b2a4df20SDavid Howells 
183b2a4df20SDavid Howells 	acc = mult_64x32_and_fold(type, desc_len + 13);
184b2a4df20SDavid Howells 	acc = mult_64x32_and_fold(acc, 9207);
185b2a4df20SDavid Howells 	for (;;) {
186b2a4df20SDavid Howells 		n = desc_len;
187b2a4df20SDavid Howells 		if (n <= 0)
188b2a4df20SDavid Howells 			break;
189b2a4df20SDavid Howells 		if (n > 4)
190b2a4df20SDavid Howells 			n = 4;
191b2a4df20SDavid Howells 		piece = 0;
192b2a4df20SDavid Howells 		memcpy(&piece, description, n);
193b2a4df20SDavid Howells 		description += n;
194b2a4df20SDavid Howells 		desc_len -= n;
195b2a4df20SDavid Howells 		acc = mult_64x32_and_fold(acc, piece);
196b2a4df20SDavid Howells 		acc = mult_64x32_and_fold(acc, 9207);
197b2a4df20SDavid Howells 	}
198b2a4df20SDavid Howells 
199b2a4df20SDavid Howells 	/* Fold the hash down to 32 bits if need be. */
200b2a4df20SDavid Howells 	hash = acc;
201b2a4df20SDavid Howells 	if (ASSOC_ARRAY_KEY_CHUNK_SIZE == 32)
202b2a4df20SDavid Howells 		hash ^= acc >> 32;
203b2a4df20SDavid Howells 
204b2a4df20SDavid Howells 	/* Squidge all the keyrings into a separate part of the tree to
205b2a4df20SDavid Howells 	 * ordinary keys by making sure the lowest level segment in the hash is
206b2a4df20SDavid Howells 	 * zero for keyrings and non-zero otherwise.
207b2a4df20SDavid Howells 	 */
208d54e58b7SDavid Howells 	if (index_key->type != &key_type_keyring && (hash & fan_mask) == 0)
209b2a4df20SDavid Howells 		return hash | (hash >> (ASSOC_ARRAY_KEY_CHUNK_SIZE - level_shift)) | 1;
210d54e58b7SDavid Howells 	if (index_key->type == &key_type_keyring && (hash & fan_mask) != 0)
211d54e58b7SDavid Howells 		return (hash + (hash << level_shift)) & ~fan_mask;
212b2a4df20SDavid Howells 	return hash;
213b2a4df20SDavid Howells }
214b2a4df20SDavid Howells 
215b2a4df20SDavid Howells /*
216b2a4df20SDavid Howells  * Build the next index key chunk.
217b2a4df20SDavid Howells  *
218b2a4df20SDavid Howells  * On 32-bit systems the index key is laid out as:
219b2a4df20SDavid Howells  *
220b2a4df20SDavid Howells  *	0	4	5	9...
221b2a4df20SDavid Howells  *	hash	desclen	typeptr	desc[]
222b2a4df20SDavid Howells  *
223b2a4df20SDavid Howells  * On 64-bit systems:
224b2a4df20SDavid Howells  *
225b2a4df20SDavid Howells  *	0	8	9	17...
226b2a4df20SDavid Howells  *	hash	desclen	typeptr	desc[]
227b2a4df20SDavid Howells  *
228b2a4df20SDavid Howells  * We return it one word-sized chunk at a time.
229b2a4df20SDavid Howells  */
230b2a4df20SDavid Howells static unsigned long keyring_get_key_chunk(const void *data, int level)
231b2a4df20SDavid Howells {
232b2a4df20SDavid Howells 	const struct keyring_index_key *index_key = data;
233b2a4df20SDavid Howells 	unsigned long chunk = 0;
234b2a4df20SDavid Howells 	long offset = 0;
235b2a4df20SDavid Howells 	int desc_len = index_key->desc_len, n = sizeof(chunk);
236b2a4df20SDavid Howells 
237b2a4df20SDavid Howells 	level /= ASSOC_ARRAY_KEY_CHUNK_SIZE;
238b2a4df20SDavid Howells 	switch (level) {
239b2a4df20SDavid Howells 	case 0:
240b2a4df20SDavid Howells 		return hash_key_type_and_desc(index_key);
241b2a4df20SDavid Howells 	case 1:
242b2a4df20SDavid Howells 		return ((unsigned long)index_key->type << 8) | desc_len;
243b2a4df20SDavid Howells 	case 2:
244b2a4df20SDavid Howells 		if (desc_len == 0)
245b2a4df20SDavid Howells 			return (u8)((unsigned long)index_key->type >>
246b2a4df20SDavid Howells 				    (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8));
247b2a4df20SDavid Howells 		n--;
248b2a4df20SDavid Howells 		offset = 1;
249b2a4df20SDavid Howells 	default:
250b2a4df20SDavid Howells 		offset += sizeof(chunk) - 1;
251b2a4df20SDavid Howells 		offset += (level - 3) * sizeof(chunk);
252b2a4df20SDavid Howells 		if (offset >= desc_len)
253b2a4df20SDavid Howells 			return 0;
254b2a4df20SDavid Howells 		desc_len -= offset;
255b2a4df20SDavid Howells 		if (desc_len > n)
256b2a4df20SDavid Howells 			desc_len = n;
257b2a4df20SDavid Howells 		offset += desc_len;
258b2a4df20SDavid Howells 		do {
259b2a4df20SDavid Howells 			chunk <<= 8;
260b2a4df20SDavid Howells 			chunk |= ((u8*)index_key->description)[--offset];
261b2a4df20SDavid Howells 		} while (--desc_len > 0);
262b2a4df20SDavid Howells 
263b2a4df20SDavid Howells 		if (level == 2) {
264b2a4df20SDavid Howells 			chunk <<= 8;
265b2a4df20SDavid Howells 			chunk |= (u8)((unsigned long)index_key->type >>
266b2a4df20SDavid Howells 				      (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8));
267b2a4df20SDavid Howells 		}
268b2a4df20SDavid Howells 		return chunk;
269b2a4df20SDavid Howells 	}
270b2a4df20SDavid Howells }
271b2a4df20SDavid Howells 
272b2a4df20SDavid Howells static unsigned long keyring_get_object_key_chunk(const void *object, int level)
273b2a4df20SDavid Howells {
274b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
275b2a4df20SDavid Howells 	return keyring_get_key_chunk(&key->index_key, level);
276b2a4df20SDavid Howells }
277b2a4df20SDavid Howells 
278b2a4df20SDavid Howells static bool keyring_compare_object(const void *object, const void *data)
279b2a4df20SDavid Howells {
280b2a4df20SDavid Howells 	const struct keyring_index_key *index_key = data;
281b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
282b2a4df20SDavid Howells 
283b2a4df20SDavid Howells 	return key->index_key.type == index_key->type &&
284b2a4df20SDavid Howells 		key->index_key.desc_len == index_key->desc_len &&
285b2a4df20SDavid Howells 		memcmp(key->index_key.description, index_key->description,
286b2a4df20SDavid Howells 		       index_key->desc_len) == 0;
287b2a4df20SDavid Howells }
288b2a4df20SDavid Howells 
289b2a4df20SDavid Howells /*
290b2a4df20SDavid Howells  * Compare the index keys of a pair of objects and determine the bit position
291b2a4df20SDavid Howells  * at which they differ - if they differ.
292b2a4df20SDavid Howells  */
29323fd78d7SDavid Howells static int keyring_diff_objects(const void *object, const void *data)
294b2a4df20SDavid Howells {
29523fd78d7SDavid Howells 	const struct key *key_a = keyring_ptr_to_key(object);
296b2a4df20SDavid Howells 	const struct keyring_index_key *a = &key_a->index_key;
29723fd78d7SDavid Howells 	const struct keyring_index_key *b = data;
298b2a4df20SDavid Howells 	unsigned long seg_a, seg_b;
299b2a4df20SDavid Howells 	int level, i;
300b2a4df20SDavid Howells 
301b2a4df20SDavid Howells 	level = 0;
302b2a4df20SDavid Howells 	seg_a = hash_key_type_and_desc(a);
303b2a4df20SDavid Howells 	seg_b = hash_key_type_and_desc(b);
304b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
305b2a4df20SDavid Howells 		goto differ;
306b2a4df20SDavid Howells 
307b2a4df20SDavid Howells 	/* The number of bits contributed by the hash is controlled by a
308b2a4df20SDavid Howells 	 * constant in the assoc_array headers.  Everything else thereafter we
309b2a4df20SDavid Howells 	 * can deal with as being machine word-size dependent.
310b2a4df20SDavid Howells 	 */
311b2a4df20SDavid Howells 	level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8;
312b2a4df20SDavid Howells 	seg_a = a->desc_len;
313b2a4df20SDavid Howells 	seg_b = b->desc_len;
314b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
315b2a4df20SDavid Howells 		goto differ;
316b2a4df20SDavid Howells 
317b2a4df20SDavid Howells 	/* The next bit may not work on big endian */
318b2a4df20SDavid Howells 	level++;
319b2a4df20SDavid Howells 	seg_a = (unsigned long)a->type;
320b2a4df20SDavid Howells 	seg_b = (unsigned long)b->type;
321b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
322b2a4df20SDavid Howells 		goto differ;
323b2a4df20SDavid Howells 
324b2a4df20SDavid Howells 	level += sizeof(unsigned long);
325b2a4df20SDavid Howells 	if (a->desc_len == 0)
326b2a4df20SDavid Howells 		goto same;
327b2a4df20SDavid Howells 
328b2a4df20SDavid Howells 	i = 0;
329b2a4df20SDavid Howells 	if (((unsigned long)a->description | (unsigned long)b->description) &
330b2a4df20SDavid Howells 	    (sizeof(unsigned long) - 1)) {
331b2a4df20SDavid Howells 		do {
332b2a4df20SDavid Howells 			seg_a = *(unsigned long *)(a->description + i);
333b2a4df20SDavid Howells 			seg_b = *(unsigned long *)(b->description + i);
334b2a4df20SDavid Howells 			if ((seg_a ^ seg_b) != 0)
335b2a4df20SDavid Howells 				goto differ_plus_i;
336b2a4df20SDavid Howells 			i += sizeof(unsigned long);
337b2a4df20SDavid Howells 		} while (i < (a->desc_len & (sizeof(unsigned long) - 1)));
338b2a4df20SDavid Howells 	}
339b2a4df20SDavid Howells 
340b2a4df20SDavid Howells 	for (; i < a->desc_len; i++) {
341b2a4df20SDavid Howells 		seg_a = *(unsigned char *)(a->description + i);
342b2a4df20SDavid Howells 		seg_b = *(unsigned char *)(b->description + i);
343b2a4df20SDavid Howells 		if ((seg_a ^ seg_b) != 0)
344b2a4df20SDavid Howells 			goto differ_plus_i;
345b2a4df20SDavid Howells 	}
346b2a4df20SDavid Howells 
347b2a4df20SDavid Howells same:
348b2a4df20SDavid Howells 	return -1;
349b2a4df20SDavid Howells 
350b2a4df20SDavid Howells differ_plus_i:
351b2a4df20SDavid Howells 	level += i;
352b2a4df20SDavid Howells differ:
353b2a4df20SDavid Howells 	i = level * 8 + __ffs(seg_a ^ seg_b);
354b2a4df20SDavid Howells 	return i;
355b2a4df20SDavid Howells }
356b2a4df20SDavid Howells 
357b2a4df20SDavid Howells /*
358b2a4df20SDavid Howells  * Free an object after stripping the keyring flag off of the pointer.
359b2a4df20SDavid Howells  */
360b2a4df20SDavid Howells static void keyring_free_object(void *object)
361b2a4df20SDavid Howells {
362b2a4df20SDavid Howells 	key_put(keyring_ptr_to_key(object));
363b2a4df20SDavid Howells }
364b2a4df20SDavid Howells 
365b2a4df20SDavid Howells /*
366b2a4df20SDavid Howells  * Operations for keyring management by the index-tree routines.
367b2a4df20SDavid Howells  */
368b2a4df20SDavid Howells static const struct assoc_array_ops keyring_assoc_array_ops = {
369b2a4df20SDavid Howells 	.get_key_chunk		= keyring_get_key_chunk,
370b2a4df20SDavid Howells 	.get_object_key_chunk	= keyring_get_object_key_chunk,
371b2a4df20SDavid Howells 	.compare_object		= keyring_compare_object,
372b2a4df20SDavid Howells 	.diff_objects		= keyring_diff_objects,
373b2a4df20SDavid Howells 	.free_object		= keyring_free_object,
374b2a4df20SDavid Howells };
375b2a4df20SDavid Howells 
376b2a4df20SDavid Howells /*
377973c9f4fSDavid Howells  * Clean up a keyring when it is destroyed.  Unpublish its name if it had one
378973c9f4fSDavid Howells  * and dispose of its data.
379233e4735SDavid Howells  *
380233e4735SDavid Howells  * The garbage collector detects the final key_put(), removes the keyring from
381233e4735SDavid Howells  * the serial number tree and then does RCU synchronisation before coming here,
382233e4735SDavid Howells  * so we shouldn't need to worry about code poking around here with the RCU
383233e4735SDavid Howells  * readlock held by this time.
3841da177e4SLinus Torvalds  */
3851da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring)
3861da177e4SLinus Torvalds {
3871da177e4SLinus Torvalds 	if (keyring->description) {
3881da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
38994efe72fSDavid Howells 
390146aa8b1SDavid Howells 		if (keyring->name_link.next != NULL &&
391146aa8b1SDavid Howells 		    !list_empty(&keyring->name_link))
392146aa8b1SDavid Howells 			list_del(&keyring->name_link);
39394efe72fSDavid Howells 
3941da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 
3972b6aa412SMat Martineau 	if (keyring->restrict_link) {
3982b6aa412SMat Martineau 		struct key_restriction *keyres = keyring->restrict_link;
3992b6aa412SMat Martineau 
4002b6aa412SMat Martineau 		key_put(keyres->key);
4012b6aa412SMat Martineau 		kfree(keyres);
4022b6aa412SMat Martineau 	}
4032b6aa412SMat Martineau 
404b2a4df20SDavid Howells 	assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops);
405a8b17ed0SDavid Howells }
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds /*
408973c9f4fSDavid Howells  * Describe a keyring for /proc.
4091da177e4SLinus Torvalds  */
4101da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m)
4111da177e4SLinus Torvalds {
412c8563473Swzt.wzt@gmail.com 	if (keyring->description)
4131da177e4SLinus Torvalds 		seq_puts(m, keyring->description);
414c8563473Swzt.wzt@gmail.com 	else
4151da177e4SLinus Torvalds 		seq_puts(m, "[anon]");
4161da177e4SLinus Torvalds 
41778b7280cSDavid Howells 	if (key_is_instantiated(keyring)) {
418b2a4df20SDavid Howells 		if (keyring->keys.nr_leaves_on_tree != 0)
419b2a4df20SDavid Howells 			seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree);
4201da177e4SLinus Torvalds 		else
4211da177e4SLinus Torvalds 			seq_puts(m, ": empty");
422a8b17ed0SDavid Howells 	}
42378b7280cSDavid Howells }
4241da177e4SLinus Torvalds 
425b2a4df20SDavid Howells struct keyring_read_iterator_context {
426*e645016aSEric Biggers 	size_t			buflen;
427b2a4df20SDavid Howells 	size_t			count;
428b2a4df20SDavid Howells 	key_serial_t __user	*buffer;
429b2a4df20SDavid Howells };
430b2a4df20SDavid Howells 
431b2a4df20SDavid Howells static int keyring_read_iterator(const void *object, void *data)
432b2a4df20SDavid Howells {
433b2a4df20SDavid Howells 	struct keyring_read_iterator_context *ctx = data;
434b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
435b2a4df20SDavid Howells 	int ret;
436b2a4df20SDavid Howells 
437b2a4df20SDavid Howells 	kenter("{%s,%d},,{%zu/%zu}",
438*e645016aSEric Biggers 	       key->type->name, key->serial, ctx->count, ctx->buflen);
439b2a4df20SDavid Howells 
440*e645016aSEric Biggers 	if (ctx->count >= ctx->buflen)
441b2a4df20SDavid Howells 		return 1;
442b2a4df20SDavid Howells 
443b2a4df20SDavid Howells 	ret = put_user(key->serial, ctx->buffer);
444b2a4df20SDavid Howells 	if (ret < 0)
445b2a4df20SDavid Howells 		return ret;
446b2a4df20SDavid Howells 	ctx->buffer++;
447b2a4df20SDavid Howells 	ctx->count += sizeof(key->serial);
448b2a4df20SDavid Howells 	return 0;
449b2a4df20SDavid Howells }
450b2a4df20SDavid Howells 
4511da177e4SLinus Torvalds /*
452973c9f4fSDavid Howells  * Read a list of key IDs from the keyring's contents in binary form
453973c9f4fSDavid Howells  *
454b2a4df20SDavid Howells  * The keyring's semaphore is read-locked by the caller.  This prevents someone
455b2a4df20SDavid Howells  * from modifying it under us - which could cause us to read key IDs multiple
456b2a4df20SDavid Howells  * times.
4571da177e4SLinus Torvalds  */
4581da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
4591da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen)
4601da177e4SLinus Torvalds {
461b2a4df20SDavid Howells 	struct keyring_read_iterator_context ctx;
462b2a4df20SDavid Howells 	unsigned long nr_keys;
463b2a4df20SDavid Howells 	int ret;
4641da177e4SLinus Torvalds 
465b2a4df20SDavid Howells 	kenter("{%d},,%zu", key_serial(keyring), buflen);
4661da177e4SLinus Torvalds 
467b2a4df20SDavid Howells 	if (buflen & (sizeof(key_serial_t) - 1))
468b2a4df20SDavid Howells 		return -EINVAL;
4691da177e4SLinus Torvalds 
470b2a4df20SDavid Howells 	nr_keys = keyring->keys.nr_leaves_on_tree;
471b2a4df20SDavid Howells 	if (nr_keys == 0)
472b2a4df20SDavid Howells 		return 0;
4731da177e4SLinus Torvalds 
474b2a4df20SDavid Howells 	/* Calculate how much data we could return */
475b2a4df20SDavid Howells 	if (!buffer || !buflen)
476*e645016aSEric Biggers 		return nr_keys * sizeof(key_serial_t);
4771da177e4SLinus Torvalds 
478b2a4df20SDavid Howells 	/* Copy the IDs of the subscribed keys into the buffer */
479b2a4df20SDavid Howells 	ctx.buffer = (key_serial_t __user *)buffer;
480*e645016aSEric Biggers 	ctx.buflen = buflen;
481b2a4df20SDavid Howells 	ctx.count = 0;
482b2a4df20SDavid Howells 	ret = assoc_array_iterate(&keyring->keys, keyring_read_iterator, &ctx);
483b2a4df20SDavid Howells 	if (ret < 0) {
484b2a4df20SDavid Howells 		kleave(" = %d [iterate]", ret);
4851da177e4SLinus Torvalds 		return ret;
486a8b17ed0SDavid Howells 	}
4871da177e4SLinus Torvalds 
488b2a4df20SDavid Howells 	kleave(" = %zu [ok]", ctx.count);
489b2a4df20SDavid Howells 	return ctx.count;
490b2a4df20SDavid Howells }
491b2a4df20SDavid Howells 
4921da177e4SLinus Torvalds /*
493973c9f4fSDavid Howells  * Allocate a keyring and link into the destination keyring.
4941da177e4SLinus Torvalds  */
4959a56c2dbSEric W. Biederman struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
49696b5c8feSDavid Howells 			  const struct cred *cred, key_perm_t perm,
4975ac7eaceSDavid Howells 			  unsigned long flags,
4982b6aa412SMat Martineau 			  struct key_restriction *restrict_link,
4995ac7eaceSDavid Howells 			  struct key *dest)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	struct key *keyring;
5021da177e4SLinus Torvalds 	int ret;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	keyring = key_alloc(&key_type_keyring, description,
5055ac7eaceSDavid Howells 			    uid, gid, cred, perm, flags, restrict_link);
5061da177e4SLinus Torvalds 	if (!IS_ERR(keyring)) {
5073e30148cSDavid Howells 		ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
5081da177e4SLinus Torvalds 		if (ret < 0) {
5091da177e4SLinus Torvalds 			key_put(keyring);
5101da177e4SLinus Torvalds 			keyring = ERR_PTR(ret);
5111da177e4SLinus Torvalds 		}
5121da177e4SLinus Torvalds 	}
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	return keyring;
515a8b17ed0SDavid Howells }
516f8aa23a5SDavid Howells EXPORT_SYMBOL(keyring_alloc);
5171da177e4SLinus Torvalds 
5185ac7eaceSDavid Howells /**
5195ac7eaceSDavid Howells  * restrict_link_reject - Give -EPERM to restrict link
5205ac7eaceSDavid Howells  * @keyring: The keyring being added to.
5215ac7eaceSDavid Howells  * @type: The type of key being added.
5225ac7eaceSDavid Howells  * @payload: The payload of the key intended to be added.
523aaf66c88SMat Martineau  * @data: Additional data for evaluating restriction.
5245ac7eaceSDavid Howells  *
5255ac7eaceSDavid Howells  * Reject the addition of any links to a keyring.  It can be overridden by
5265ac7eaceSDavid Howells  * passing KEY_ALLOC_BYPASS_RESTRICTION to key_instantiate_and_link() when
5275ac7eaceSDavid Howells  * adding a key to a keyring.
5285ac7eaceSDavid Howells  *
5292b6aa412SMat Martineau  * This is meant to be stored in a key_restriction structure which is passed
5302b6aa412SMat Martineau  * in the restrict_link parameter to keyring_alloc().
5315ac7eaceSDavid Howells  */
5325ac7eaceSDavid Howells int restrict_link_reject(struct key *keyring,
5335ac7eaceSDavid Howells 			 const struct key_type *type,
534aaf66c88SMat Martineau 			 const union key_payload *payload,
535aaf66c88SMat Martineau 			 struct key *restriction_key)
5365ac7eaceSDavid Howells {
5375ac7eaceSDavid Howells 	return -EPERM;
5385ac7eaceSDavid Howells }
5395ac7eaceSDavid Howells 
540b2a4df20SDavid Howells /*
541c06cfb08SDavid Howells  * By default, we keys found by getting an exact match on their descriptions.
542c06cfb08SDavid Howells  */
5430c903ab6SDavid Howells bool key_default_cmp(const struct key *key,
544c06cfb08SDavid Howells 		     const struct key_match_data *match_data)
545c06cfb08SDavid Howells {
546c06cfb08SDavid Howells 	return strcmp(key->description, match_data->raw_data) == 0;
547c06cfb08SDavid Howells }
548c06cfb08SDavid Howells 
549c06cfb08SDavid Howells /*
550b2a4df20SDavid Howells  * Iteration function to consider each key found.
551b2a4df20SDavid Howells  */
552b2a4df20SDavid Howells static int keyring_search_iterator(const void *object, void *iterator_data)
553b2a4df20SDavid Howells {
554b2a4df20SDavid Howells 	struct keyring_search_context *ctx = iterator_data;
555b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
556b2a4df20SDavid Howells 	unsigned long kflags = key->flags;
557b2a4df20SDavid Howells 
558b2a4df20SDavid Howells 	kenter("{%d}", key->serial);
559b2a4df20SDavid Howells 
560b2a4df20SDavid Howells 	/* ignore keys not of this type */
561b2a4df20SDavid Howells 	if (key->type != ctx->index_key.type) {
562b2a4df20SDavid Howells 		kleave(" = 0 [!type]");
563b2a4df20SDavid Howells 		return 0;
564b2a4df20SDavid Howells 	}
565b2a4df20SDavid Howells 
566b2a4df20SDavid Howells 	/* skip invalidated, revoked and expired keys */
567b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
568b2a4df20SDavid Howells 		if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
569b2a4df20SDavid Howells 			      (1 << KEY_FLAG_REVOKED))) {
570b2a4df20SDavid Howells 			ctx->result = ERR_PTR(-EKEYREVOKED);
571b2a4df20SDavid Howells 			kleave(" = %d [invrev]", ctx->skipped_ret);
572b2a4df20SDavid Howells 			goto skipped;
573b2a4df20SDavid Howells 		}
574b2a4df20SDavid Howells 
575b2a4df20SDavid Howells 		if (key->expiry && ctx->now.tv_sec >= key->expiry) {
5760b0a8415SDavid Howells 			if (!(ctx->flags & KEYRING_SEARCH_SKIP_EXPIRED))
577b2a4df20SDavid Howells 				ctx->result = ERR_PTR(-EKEYEXPIRED);
578b2a4df20SDavid Howells 			kleave(" = %d [expire]", ctx->skipped_ret);
579b2a4df20SDavid Howells 			goto skipped;
580b2a4df20SDavid Howells 		}
581b2a4df20SDavid Howells 	}
582b2a4df20SDavid Howells 
583b2a4df20SDavid Howells 	/* keys that don't match */
58446291959SDavid Howells 	if (!ctx->match_data.cmp(key, &ctx->match_data)) {
585b2a4df20SDavid Howells 		kleave(" = 0 [!match]");
586b2a4df20SDavid Howells 		return 0;
587b2a4df20SDavid Howells 	}
588b2a4df20SDavid Howells 
589b2a4df20SDavid Howells 	/* key must have search permissions */
590b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
591b2a4df20SDavid Howells 	    key_task_permission(make_key_ref(key, ctx->possessed),
592f5895943SDavid Howells 				ctx->cred, KEY_NEED_SEARCH) < 0) {
593b2a4df20SDavid Howells 		ctx->result = ERR_PTR(-EACCES);
594b2a4df20SDavid Howells 		kleave(" = %d [!perm]", ctx->skipped_ret);
595b2a4df20SDavid Howells 		goto skipped;
596b2a4df20SDavid Howells 	}
597b2a4df20SDavid Howells 
598b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
599b2a4df20SDavid Howells 		/* we set a different error code if we pass a negative key */
600b2a4df20SDavid Howells 		if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
60174792b00SDavid Howells 			smp_rmb();
602146aa8b1SDavid Howells 			ctx->result = ERR_PTR(key->reject_error);
603b2a4df20SDavid Howells 			kleave(" = %d [neg]", ctx->skipped_ret);
604b2a4df20SDavid Howells 			goto skipped;
605b2a4df20SDavid Howells 		}
606b2a4df20SDavid Howells 	}
607b2a4df20SDavid Howells 
608b2a4df20SDavid Howells 	/* Found */
609b2a4df20SDavid Howells 	ctx->result = make_key_ref(key, ctx->possessed);
610b2a4df20SDavid Howells 	kleave(" = 1 [found]");
611b2a4df20SDavid Howells 	return 1;
612b2a4df20SDavid Howells 
613b2a4df20SDavid Howells skipped:
614b2a4df20SDavid Howells 	return ctx->skipped_ret;
615b2a4df20SDavid Howells }
616b2a4df20SDavid Howells 
617b2a4df20SDavid Howells /*
618b2a4df20SDavid Howells  * Search inside a keyring for a key.  We can search by walking to it
619b2a4df20SDavid Howells  * directly based on its index-key or we can iterate over the entire
620b2a4df20SDavid Howells  * tree looking for it, based on the match function.
621b2a4df20SDavid Howells  */
622b2a4df20SDavid Howells static int search_keyring(struct key *keyring, struct keyring_search_context *ctx)
623b2a4df20SDavid Howells {
62446291959SDavid Howells 	if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_DIRECT) {
625b2a4df20SDavid Howells 		const void *object;
626b2a4df20SDavid Howells 
627b2a4df20SDavid Howells 		object = assoc_array_find(&keyring->keys,
628b2a4df20SDavid Howells 					  &keyring_assoc_array_ops,
629b2a4df20SDavid Howells 					  &ctx->index_key);
630b2a4df20SDavid Howells 		return object ? ctx->iterator(object, ctx) : 0;
631b2a4df20SDavid Howells 	}
632b2a4df20SDavid Howells 	return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx);
633b2a4df20SDavid Howells }
634b2a4df20SDavid Howells 
635b2a4df20SDavid Howells /*
636b2a4df20SDavid Howells  * Search a tree of keyrings that point to other keyrings up to the maximum
637b2a4df20SDavid Howells  * depth.
638b2a4df20SDavid Howells  */
639b2a4df20SDavid Howells static bool search_nested_keyrings(struct key *keyring,
640b2a4df20SDavid Howells 				   struct keyring_search_context *ctx)
641b2a4df20SDavid Howells {
642b2a4df20SDavid Howells 	struct {
643b2a4df20SDavid Howells 		struct key *keyring;
644b2a4df20SDavid Howells 		struct assoc_array_node *node;
645b2a4df20SDavid Howells 		int slot;
646b2a4df20SDavid Howells 	} stack[KEYRING_SEARCH_MAX_DEPTH];
647b2a4df20SDavid Howells 
648b2a4df20SDavid Howells 	struct assoc_array_shortcut *shortcut;
649b2a4df20SDavid Howells 	struct assoc_array_node *node;
650b2a4df20SDavid Howells 	struct assoc_array_ptr *ptr;
651b2a4df20SDavid Howells 	struct key *key;
652b2a4df20SDavid Howells 	int sp = 0, slot;
653b2a4df20SDavid Howells 
654b2a4df20SDavid Howells 	kenter("{%d},{%s,%s}",
655b2a4df20SDavid Howells 	       keyring->serial,
656b2a4df20SDavid Howells 	       ctx->index_key.type->name,
657b2a4df20SDavid Howells 	       ctx->index_key.description);
658b2a4df20SDavid Howells 
659054f6180SDavid Howells #define STATE_CHECKS (KEYRING_SEARCH_NO_STATE_CHECK | KEYRING_SEARCH_DO_STATE_CHECK)
660054f6180SDavid Howells 	BUG_ON((ctx->flags & STATE_CHECKS) == 0 ||
661054f6180SDavid Howells 	       (ctx->flags & STATE_CHECKS) == STATE_CHECKS);
662054f6180SDavid Howells 
663b2a4df20SDavid Howells 	if (ctx->index_key.description)
664b2a4df20SDavid Howells 		ctx->index_key.desc_len = strlen(ctx->index_key.description);
665b2a4df20SDavid Howells 
666b2a4df20SDavid Howells 	/* Check to see if this top-level keyring is what we are looking for
667b2a4df20SDavid Howells 	 * and whether it is valid or not.
668b2a4df20SDavid Howells 	 */
66946291959SDavid Howells 	if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_ITERATE ||
670b2a4df20SDavid Howells 	    keyring_compare_object(keyring, &ctx->index_key)) {
671b2a4df20SDavid Howells 		ctx->skipped_ret = 2;
672b2a4df20SDavid Howells 		switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) {
673b2a4df20SDavid Howells 		case 1:
674b2a4df20SDavid Howells 			goto found;
675b2a4df20SDavid Howells 		case 2:
676b2a4df20SDavid Howells 			return false;
677b2a4df20SDavid Howells 		default:
678b2a4df20SDavid Howells 			break;
679b2a4df20SDavid Howells 		}
680b2a4df20SDavid Howells 	}
681b2a4df20SDavid Howells 
682b2a4df20SDavid Howells 	ctx->skipped_ret = 0;
683b2a4df20SDavid Howells 
684b2a4df20SDavid Howells 	/* Start processing a new keyring */
685b2a4df20SDavid Howells descend_to_keyring:
686b2a4df20SDavid Howells 	kdebug("descend to %d", keyring->serial);
687b2a4df20SDavid Howells 	if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
688b2a4df20SDavid Howells 			      (1 << KEY_FLAG_REVOKED)))
689b2a4df20SDavid Howells 		goto not_this_keyring;
690b2a4df20SDavid Howells 
691b2a4df20SDavid Howells 	/* Search through the keys in this keyring before its searching its
692b2a4df20SDavid Howells 	 * subtrees.
693b2a4df20SDavid Howells 	 */
694b2a4df20SDavid Howells 	if (search_keyring(keyring, ctx))
695b2a4df20SDavid Howells 		goto found;
696b2a4df20SDavid Howells 
697b2a4df20SDavid Howells 	/* Then manually iterate through the keyrings nested in this one.
698b2a4df20SDavid Howells 	 *
699b2a4df20SDavid Howells 	 * Start from the root node of the index tree.  Because of the way the
700b2a4df20SDavid Howells 	 * hash function has been set up, keyrings cluster on the leftmost
701b2a4df20SDavid Howells 	 * branch of the root node (root slot 0) or in the root node itself.
702b2a4df20SDavid Howells 	 * Non-keyrings avoid the leftmost branch of the root entirely (root
703b2a4df20SDavid Howells 	 * slots 1-15).
704b2a4df20SDavid Howells 	 */
705381f20fcSDavidlohr Bueso 	ptr = READ_ONCE(keyring->keys.root);
706b2a4df20SDavid Howells 	if (!ptr)
707b2a4df20SDavid Howells 		goto not_this_keyring;
708b2a4df20SDavid Howells 
709b2a4df20SDavid Howells 	if (assoc_array_ptr_is_shortcut(ptr)) {
710b2a4df20SDavid Howells 		/* If the root is a shortcut, either the keyring only contains
711b2a4df20SDavid Howells 		 * keyring pointers (everything clusters behind root slot 0) or
712b2a4df20SDavid Howells 		 * doesn't contain any keyring pointers.
713b2a4df20SDavid Howells 		 */
714b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
715b2a4df20SDavid Howells 		smp_read_barrier_depends();
716b2a4df20SDavid Howells 		if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0)
717b2a4df20SDavid Howells 			goto not_this_keyring;
718b2a4df20SDavid Howells 
719381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(shortcut->next_node);
720b2a4df20SDavid Howells 		node = assoc_array_ptr_to_node(ptr);
721b2a4df20SDavid Howells 		goto begin_node;
722b2a4df20SDavid Howells 	}
723b2a4df20SDavid Howells 
724b2a4df20SDavid Howells 	node = assoc_array_ptr_to_node(ptr);
725b2a4df20SDavid Howells 	smp_read_barrier_depends();
726b2a4df20SDavid Howells 
727b2a4df20SDavid Howells 	ptr = node->slots[0];
728b2a4df20SDavid Howells 	if (!assoc_array_ptr_is_meta(ptr))
729b2a4df20SDavid Howells 		goto begin_node;
730b2a4df20SDavid Howells 
731b2a4df20SDavid Howells descend_to_node:
732b2a4df20SDavid Howells 	/* Descend to a more distal node in this keyring's content tree and go
733b2a4df20SDavid Howells 	 * through that.
734b2a4df20SDavid Howells 	 */
735b2a4df20SDavid Howells 	kdebug("descend");
736b2a4df20SDavid Howells 	if (assoc_array_ptr_is_shortcut(ptr)) {
737b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
738b2a4df20SDavid Howells 		smp_read_barrier_depends();
739381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(shortcut->next_node);
740b2a4df20SDavid Howells 		BUG_ON(!assoc_array_ptr_is_node(ptr));
741b2a4df20SDavid Howells 	}
7429c5e45dfSDavid Howells 	node = assoc_array_ptr_to_node(ptr);
743b2a4df20SDavid Howells 
744b2a4df20SDavid Howells begin_node:
745b2a4df20SDavid Howells 	kdebug("begin_node");
746b2a4df20SDavid Howells 	smp_read_barrier_depends();
747b2a4df20SDavid Howells 	slot = 0;
748b2a4df20SDavid Howells ascend_to_node:
749b2a4df20SDavid Howells 	/* Go through the slots in a node */
750b2a4df20SDavid Howells 	for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
751381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(node->slots[slot]);
752b2a4df20SDavid Howells 
753b2a4df20SDavid Howells 		if (assoc_array_ptr_is_meta(ptr) && node->back_pointer)
754b2a4df20SDavid Howells 			goto descend_to_node;
755b2a4df20SDavid Howells 
756b2a4df20SDavid Howells 		if (!keyring_ptr_is_keyring(ptr))
757b2a4df20SDavid Howells 			continue;
758b2a4df20SDavid Howells 
759b2a4df20SDavid Howells 		key = keyring_ptr_to_key(ptr);
760b2a4df20SDavid Howells 
761b2a4df20SDavid Howells 		if (sp >= KEYRING_SEARCH_MAX_DEPTH) {
762b2a4df20SDavid Howells 			if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) {
763b2a4df20SDavid Howells 				ctx->result = ERR_PTR(-ELOOP);
764b2a4df20SDavid Howells 				return false;
765b2a4df20SDavid Howells 			}
766b2a4df20SDavid Howells 			goto not_this_keyring;
767b2a4df20SDavid Howells 		}
768b2a4df20SDavid Howells 
769b2a4df20SDavid Howells 		/* Search a nested keyring */
770b2a4df20SDavid Howells 		if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
771b2a4df20SDavid Howells 		    key_task_permission(make_key_ref(key, ctx->possessed),
772f5895943SDavid Howells 					ctx->cred, KEY_NEED_SEARCH) < 0)
773b2a4df20SDavid Howells 			continue;
774b2a4df20SDavid Howells 
775b2a4df20SDavid Howells 		/* stack the current position */
776b2a4df20SDavid Howells 		stack[sp].keyring = keyring;
777b2a4df20SDavid Howells 		stack[sp].node = node;
778b2a4df20SDavid Howells 		stack[sp].slot = slot;
779b2a4df20SDavid Howells 		sp++;
780b2a4df20SDavid Howells 
781b2a4df20SDavid Howells 		/* begin again with the new keyring */
782b2a4df20SDavid Howells 		keyring = key;
783b2a4df20SDavid Howells 		goto descend_to_keyring;
784b2a4df20SDavid Howells 	}
785b2a4df20SDavid Howells 
786b2a4df20SDavid Howells 	/* We've dealt with all the slots in the current node, so now we need
787b2a4df20SDavid Howells 	 * to ascend to the parent and continue processing there.
788b2a4df20SDavid Howells 	 */
789381f20fcSDavidlohr Bueso 	ptr = READ_ONCE(node->back_pointer);
790b2a4df20SDavid Howells 	slot = node->parent_slot;
791b2a4df20SDavid Howells 
792b2a4df20SDavid Howells 	if (ptr && assoc_array_ptr_is_shortcut(ptr)) {
793b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
794b2a4df20SDavid Howells 		smp_read_barrier_depends();
795381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(shortcut->back_pointer);
796b2a4df20SDavid Howells 		slot = shortcut->parent_slot;
797b2a4df20SDavid Howells 	}
798b2a4df20SDavid Howells 	if (!ptr)
799b2a4df20SDavid Howells 		goto not_this_keyring;
800b2a4df20SDavid Howells 	node = assoc_array_ptr_to_node(ptr);
801b2a4df20SDavid Howells 	smp_read_barrier_depends();
802b2a4df20SDavid Howells 	slot++;
803b2a4df20SDavid Howells 
804b2a4df20SDavid Howells 	/* If we've ascended to the root (zero backpointer), we must have just
805b2a4df20SDavid Howells 	 * finished processing the leftmost branch rather than the root slots -
806b2a4df20SDavid Howells 	 * so there can't be any more keyrings for us to find.
807b2a4df20SDavid Howells 	 */
808b2a4df20SDavid Howells 	if (node->back_pointer) {
809b2a4df20SDavid Howells 		kdebug("ascend %d", slot);
810b2a4df20SDavid Howells 		goto ascend_to_node;
811b2a4df20SDavid Howells 	}
812b2a4df20SDavid Howells 
813b2a4df20SDavid Howells 	/* The keyring we're looking at was disqualified or didn't contain a
814b2a4df20SDavid Howells 	 * matching key.
815b2a4df20SDavid Howells 	 */
816b2a4df20SDavid Howells not_this_keyring:
817b2a4df20SDavid Howells 	kdebug("not_this_keyring %d", sp);
818b2a4df20SDavid Howells 	if (sp <= 0) {
819b2a4df20SDavid Howells 		kleave(" = false");
820b2a4df20SDavid Howells 		return false;
821b2a4df20SDavid Howells 	}
822b2a4df20SDavid Howells 
823b2a4df20SDavid Howells 	/* Resume the processing of a keyring higher up in the tree */
824b2a4df20SDavid Howells 	sp--;
825b2a4df20SDavid Howells 	keyring = stack[sp].keyring;
826b2a4df20SDavid Howells 	node = stack[sp].node;
827b2a4df20SDavid Howells 	slot = stack[sp].slot + 1;
828b2a4df20SDavid Howells 	kdebug("ascend to %d [%d]", keyring->serial, slot);
829b2a4df20SDavid Howells 	goto ascend_to_node;
830b2a4df20SDavid Howells 
831b2a4df20SDavid Howells 	/* We found a viable match */
832b2a4df20SDavid Howells found:
833b2a4df20SDavid Howells 	key = key_ref_to_ptr(ctx->result);
834b2a4df20SDavid Howells 	key_check(key);
835b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) {
836b2a4df20SDavid Howells 		key->last_used_at = ctx->now.tv_sec;
837b2a4df20SDavid Howells 		keyring->last_used_at = ctx->now.tv_sec;
838b2a4df20SDavid Howells 		while (sp > 0)
839b2a4df20SDavid Howells 			stack[--sp].keyring->last_used_at = ctx->now.tv_sec;
840b2a4df20SDavid Howells 	}
841b2a4df20SDavid Howells 	kleave(" = true");
842b2a4df20SDavid Howells 	return true;
843b2a4df20SDavid Howells }
844b2a4df20SDavid Howells 
845973c9f4fSDavid Howells /**
846973c9f4fSDavid Howells  * keyring_search_aux - Search a keyring tree for a key matching some criteria
847973c9f4fSDavid Howells  * @keyring_ref: A pointer to the keyring with possession indicator.
8484bdf0bc3SDavid Howells  * @ctx: The keyring search context.
849973c9f4fSDavid Howells  *
850973c9f4fSDavid Howells  * Search the supplied keyring tree for a key that matches the criteria given.
851973c9f4fSDavid Howells  * The root keyring and any linked keyrings must grant Search permission to the
852973c9f4fSDavid Howells  * caller to be searchable and keys can only be found if they too grant Search
853973c9f4fSDavid Howells  * to the caller. The possession flag on the root keyring pointer controls use
854973c9f4fSDavid Howells  * of the possessor bits in permissions checking of the entire tree.  In
855973c9f4fSDavid Howells  * addition, the LSM gets to forbid keyring searches and key matches.
856973c9f4fSDavid Howells  *
857973c9f4fSDavid Howells  * The search is performed as a breadth-then-depth search up to the prescribed
858973c9f4fSDavid Howells  * limit (KEYRING_SEARCH_MAX_DEPTH).
859973c9f4fSDavid Howells  *
860973c9f4fSDavid Howells  * Keys are matched to the type provided and are then filtered by the match
861973c9f4fSDavid Howells  * function, which is given the description to use in any way it sees fit.  The
862973c9f4fSDavid Howells  * match function may use any attributes of a key that it wishes to to
863973c9f4fSDavid Howells  * determine the match.  Normally the match function from the key type would be
864973c9f4fSDavid Howells  * used.
865973c9f4fSDavid Howells  *
866b2a4df20SDavid Howells  * RCU can be used to prevent the keyring key lists from disappearing without
867b2a4df20SDavid Howells  * the need to take lots of locks.
868973c9f4fSDavid Howells  *
869973c9f4fSDavid Howells  * Returns a pointer to the found key and increments the key usage count if
870973c9f4fSDavid Howells  * successful; -EAGAIN if no matching keys were found, or if expired or revoked
871973c9f4fSDavid Howells  * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
872973c9f4fSDavid Howells  * specified keyring wasn't a keyring.
873973c9f4fSDavid Howells  *
874973c9f4fSDavid Howells  * In the case of a successful return, the possession attribute from
875973c9f4fSDavid Howells  * @keyring_ref is propagated to the returned key reference.
8761da177e4SLinus Torvalds  */
877664cceb0SDavid Howells key_ref_t keyring_search_aux(key_ref_t keyring_ref,
8784bdf0bc3SDavid Howells 			     struct keyring_search_context *ctx)
8791da177e4SLinus Torvalds {
88031d5a79dSDavid Howells 	struct key *keyring;
8811da177e4SLinus Torvalds 	long err;
882b2a4df20SDavid Howells 
883b2a4df20SDavid Howells 	ctx->iterator = keyring_search_iterator;
884b2a4df20SDavid Howells 	ctx->possessed = is_key_possessed(keyring_ref);
885b2a4df20SDavid Howells 	ctx->result = ERR_PTR(-EAGAIN);
8861da177e4SLinus Torvalds 
887664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
8881da177e4SLinus Torvalds 	key_check(keyring);
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
891b2a4df20SDavid Howells 		return ERR_PTR(-ENOTDIR);
892b2a4df20SDavid Howells 
893b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) {
894f5895943SDavid Howells 		err = key_task_permission(keyring_ref, ctx->cred, KEY_NEED_SEARCH);
895b2a4df20SDavid Howells 		if (err < 0)
896b2a4df20SDavid Howells 			return ERR_PTR(err);
897b2a4df20SDavid Howells 	}
8981da177e4SLinus Torvalds 
899664cceb0SDavid Howells 	rcu_read_lock();
9004bdf0bc3SDavid Howells 	ctx->now = current_kernel_time();
901b2a4df20SDavid Howells 	if (search_nested_keyrings(keyring, ctx))
902b2a4df20SDavid Howells 		__key_get(key_ref_to_ptr(ctx->result));
90376d8aeabSDavid Howells 	rcu_read_unlock();
904b2a4df20SDavid Howells 	return ctx->result;
905a8b17ed0SDavid Howells }
9061da177e4SLinus Torvalds 
907973c9f4fSDavid Howells /**
908973c9f4fSDavid Howells  * keyring_search - Search the supplied keyring tree for a matching key
909973c9f4fSDavid Howells  * @keyring: The root of the keyring tree to be searched.
910973c9f4fSDavid Howells  * @type: The type of keyring we want to find.
911973c9f4fSDavid Howells  * @description: The name of the keyring we want to find.
912973c9f4fSDavid Howells  *
913973c9f4fSDavid Howells  * As keyring_search_aux() above, but using the current task's credentials and
914b2a4df20SDavid Howells  * type's default matching function and preferred search method.
9151da177e4SLinus Torvalds  */
916664cceb0SDavid Howells key_ref_t keyring_search(key_ref_t keyring,
9171da177e4SLinus Torvalds 			 struct key_type *type,
9181da177e4SLinus Torvalds 			 const char *description)
9191da177e4SLinus Torvalds {
9204bdf0bc3SDavid Howells 	struct keyring_search_context ctx = {
9214bdf0bc3SDavid Howells 		.index_key.type		= type,
9224bdf0bc3SDavid Howells 		.index_key.description	= description,
9234bdf0bc3SDavid Howells 		.cred			= current_cred(),
924c06cfb08SDavid Howells 		.match_data.cmp		= key_default_cmp,
92546291959SDavid Howells 		.match_data.raw_data	= description,
92646291959SDavid Howells 		.match_data.lookup_type	= KEYRING_SEARCH_LOOKUP_DIRECT,
92746291959SDavid Howells 		.flags			= KEYRING_SEARCH_DO_STATE_CHECK,
9284bdf0bc3SDavid Howells 	};
92946291959SDavid Howells 	key_ref_t key;
93046291959SDavid Howells 	int ret;
9314bdf0bc3SDavid Howells 
93246291959SDavid Howells 	if (type->match_preparse) {
93346291959SDavid Howells 		ret = type->match_preparse(&ctx.match_data);
93446291959SDavid Howells 		if (ret < 0)
93546291959SDavid Howells 			return ERR_PTR(ret);
93646291959SDavid Howells 	}
93746291959SDavid Howells 
93846291959SDavid Howells 	key = keyring_search_aux(keyring, &ctx);
93946291959SDavid Howells 
94046291959SDavid Howells 	if (type->match_free)
94146291959SDavid Howells 		type->match_free(&ctx.match_data);
94246291959SDavid Howells 	return key;
943a8b17ed0SDavid Howells }
9441da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_search);
9451da177e4SLinus Torvalds 
9466563c91fSMat Martineau static struct key_restriction *keyring_restriction_alloc(
9476563c91fSMat Martineau 	key_restrict_link_func_t check)
9486563c91fSMat Martineau {
9496563c91fSMat Martineau 	struct key_restriction *keyres =
9506563c91fSMat Martineau 		kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
9516563c91fSMat Martineau 
9526563c91fSMat Martineau 	if (!keyres)
9536563c91fSMat Martineau 		return ERR_PTR(-ENOMEM);
9546563c91fSMat Martineau 
9556563c91fSMat Martineau 	keyres->check = check;
9566563c91fSMat Martineau 
9576563c91fSMat Martineau 	return keyres;
9586563c91fSMat Martineau }
9596563c91fSMat Martineau 
9606563c91fSMat Martineau /*
9616563c91fSMat Martineau  * Semaphore to serialise restriction setup to prevent reference count
9626563c91fSMat Martineau  * cycles through restriction key pointers.
9636563c91fSMat Martineau  */
9646563c91fSMat Martineau static DECLARE_RWSEM(keyring_serialise_restrict_sem);
9656563c91fSMat Martineau 
9666563c91fSMat Martineau /*
9676563c91fSMat Martineau  * Check for restriction cycles that would prevent keyring garbage collection.
9686563c91fSMat Martineau  * keyring_serialise_restrict_sem must be held.
9696563c91fSMat Martineau  */
9706563c91fSMat Martineau static bool keyring_detect_restriction_cycle(const struct key *dest_keyring,
9716563c91fSMat Martineau 					     struct key_restriction *keyres)
9726563c91fSMat Martineau {
9736563c91fSMat Martineau 	while (keyres && keyres->key &&
9746563c91fSMat Martineau 	       keyres->key->type == &key_type_keyring) {
9756563c91fSMat Martineau 		if (keyres->key == dest_keyring)
9766563c91fSMat Martineau 			return true;
9776563c91fSMat Martineau 
9786563c91fSMat Martineau 		keyres = keyres->key->restrict_link;
9796563c91fSMat Martineau 	}
9806563c91fSMat Martineau 
9816563c91fSMat Martineau 	return false;
9826563c91fSMat Martineau }
9836563c91fSMat Martineau 
9846563c91fSMat Martineau /**
9856563c91fSMat Martineau  * keyring_restrict - Look up and apply a restriction to a keyring
9866563c91fSMat Martineau  *
9876563c91fSMat Martineau  * @keyring: The keyring to be restricted
9886563c91fSMat Martineau  * @restriction: The restriction options to apply to the keyring
9896563c91fSMat Martineau  */
9906563c91fSMat Martineau int keyring_restrict(key_ref_t keyring_ref, const char *type,
9916563c91fSMat Martineau 		     const char *restriction)
9926563c91fSMat Martineau {
9936563c91fSMat Martineau 	struct key *keyring;
9946563c91fSMat Martineau 	struct key_type *restrict_type = NULL;
9956563c91fSMat Martineau 	struct key_restriction *restrict_link;
9966563c91fSMat Martineau 	int ret = 0;
9976563c91fSMat Martineau 
9986563c91fSMat Martineau 	keyring = key_ref_to_ptr(keyring_ref);
9996563c91fSMat Martineau 	key_check(keyring);
10006563c91fSMat Martineau 
10016563c91fSMat Martineau 	if (keyring->type != &key_type_keyring)
10026563c91fSMat Martineau 		return -ENOTDIR;
10036563c91fSMat Martineau 
10046563c91fSMat Martineau 	if (!type) {
10056563c91fSMat Martineau 		restrict_link = keyring_restriction_alloc(restrict_link_reject);
10066563c91fSMat Martineau 	} else {
10076563c91fSMat Martineau 		restrict_type = key_type_lookup(type);
10086563c91fSMat Martineau 
10096563c91fSMat Martineau 		if (IS_ERR(restrict_type))
10106563c91fSMat Martineau 			return PTR_ERR(restrict_type);
10116563c91fSMat Martineau 
10126563c91fSMat Martineau 		if (!restrict_type->lookup_restriction) {
10136563c91fSMat Martineau 			ret = -ENOENT;
10146563c91fSMat Martineau 			goto error;
10156563c91fSMat Martineau 		}
10166563c91fSMat Martineau 
10176563c91fSMat Martineau 		restrict_link = restrict_type->lookup_restriction(restriction);
10186563c91fSMat Martineau 	}
10196563c91fSMat Martineau 
10206563c91fSMat Martineau 	if (IS_ERR(restrict_link)) {
10216563c91fSMat Martineau 		ret = PTR_ERR(restrict_link);
10226563c91fSMat Martineau 		goto error;
10236563c91fSMat Martineau 	}
10246563c91fSMat Martineau 
10256563c91fSMat Martineau 	down_write(&keyring->sem);
10266563c91fSMat Martineau 	down_write(&keyring_serialise_restrict_sem);
10276563c91fSMat Martineau 
10286563c91fSMat Martineau 	if (keyring->restrict_link)
10296563c91fSMat Martineau 		ret = -EEXIST;
10306563c91fSMat Martineau 	else if (keyring_detect_restriction_cycle(keyring, restrict_link))
10316563c91fSMat Martineau 		ret = -EDEADLK;
10326563c91fSMat Martineau 	else
10336563c91fSMat Martineau 		keyring->restrict_link = restrict_link;
10346563c91fSMat Martineau 
10356563c91fSMat Martineau 	up_write(&keyring_serialise_restrict_sem);
10366563c91fSMat Martineau 	up_write(&keyring->sem);
10376563c91fSMat Martineau 
10386563c91fSMat Martineau 	if (ret < 0) {
10396563c91fSMat Martineau 		key_put(restrict_link->key);
10406563c91fSMat Martineau 		kfree(restrict_link);
10416563c91fSMat Martineau 	}
10426563c91fSMat Martineau 
10436563c91fSMat Martineau error:
10446563c91fSMat Martineau 	if (restrict_type)
10456563c91fSMat Martineau 		key_type_put(restrict_type);
10466563c91fSMat Martineau 
10476563c91fSMat Martineau 	return ret;
10486563c91fSMat Martineau }
10496563c91fSMat Martineau EXPORT_SYMBOL(keyring_restrict);
10506563c91fSMat Martineau 
10511da177e4SLinus Torvalds /*
1052b2a4df20SDavid Howells  * Search the given keyring for a key that might be updated.
1053973c9f4fSDavid Howells  *
1054973c9f4fSDavid Howells  * The caller must guarantee that the keyring is a keyring and that the
1055b2a4df20SDavid Howells  * permission is granted to modify the keyring as no check is made here.  The
1056b2a4df20SDavid Howells  * caller must also hold a lock on the keyring semaphore.
1057973c9f4fSDavid Howells  *
1058973c9f4fSDavid Howells  * Returns a pointer to the found key with usage count incremented if
1059b2a4df20SDavid Howells  * successful and returns NULL if not found.  Revoked and invalidated keys are
1060b2a4df20SDavid Howells  * skipped over.
1061973c9f4fSDavid Howells  *
1062973c9f4fSDavid Howells  * If successful, the possession indicator is propagated from the keyring ref
1063973c9f4fSDavid Howells  * to the returned key reference.
10641da177e4SLinus Torvalds  */
1065b2a4df20SDavid Howells key_ref_t find_key_to_update(key_ref_t keyring_ref,
1066e57e8669SDavid Howells 			     const struct keyring_index_key *index_key)
10671da177e4SLinus Torvalds {
1068664cceb0SDavid Howells 	struct key *keyring, *key;
1069b2a4df20SDavid Howells 	const void *object;
10701da177e4SLinus Torvalds 
1071664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
1072664cceb0SDavid Howells 
1073b2a4df20SDavid Howells 	kenter("{%d},{%s,%s}",
1074b2a4df20SDavid Howells 	       keyring->serial, index_key->type->name, index_key->description);
107576d8aeabSDavid Howells 
1076b2a4df20SDavid Howells 	object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
1077b2a4df20SDavid Howells 				  index_key);
1078b2a4df20SDavid Howells 
1079b2a4df20SDavid Howells 	if (object)
10801da177e4SLinus Torvalds 		goto found;
10811da177e4SLinus Torvalds 
1082b2a4df20SDavid Howells 	kleave(" = NULL");
1083b2a4df20SDavid Howells 	return NULL;
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds found:
1086b2a4df20SDavid Howells 	key = keyring_ptr_to_key(object);
1087b2a4df20SDavid Howells 	if (key->flags & ((1 << KEY_FLAG_INVALIDATED) |
1088b2a4df20SDavid Howells 			  (1 << KEY_FLAG_REVOKED))) {
1089b2a4df20SDavid Howells 		kleave(" = NULL [x]");
1090b2a4df20SDavid Howells 		return NULL;
1091b2a4df20SDavid Howells 	}
1092ccc3e6d9SDavid Howells 	__key_get(key);
1093b2a4df20SDavid Howells 	kleave(" = {%d}", key->serial);
1094b2a4df20SDavid Howells 	return make_key_ref(key, is_key_possessed(keyring_ref));
1095a8b17ed0SDavid Howells }
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds /*
1098973c9f4fSDavid Howells  * Find a keyring with the specified name.
1099973c9f4fSDavid Howells  *
1100973c9f4fSDavid Howells  * All named keyrings in the current user namespace are searched, provided they
1101973c9f4fSDavid Howells  * grant Search permission directly to the caller (unless this check is
1102973c9f4fSDavid Howells  * skipped).  Keyrings whose usage points have reached zero or who have been
1103973c9f4fSDavid Howells  * revoked are skipped.
1104973c9f4fSDavid Howells  *
1105973c9f4fSDavid Howells  * Returns a pointer to the keyring with the keyring's refcount having being
1106973c9f4fSDavid Howells  * incremented on success.  -ENOKEY is returned if a key could not be found.
11071da177e4SLinus Torvalds  */
110869664cf1SDavid Howells struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
11091da177e4SLinus Torvalds {
11101da177e4SLinus Torvalds 	struct key *keyring;
11111da177e4SLinus Torvalds 	int bucket;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	if (!name)
1114cea7daa3SToshiyuki Okajima 		return ERR_PTR(-EINVAL);
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	bucket = keyring_hash(name);
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	read_lock(&keyring_name_lock);
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	if (keyring_name_hash[bucket].next) {
11211da177e4SLinus Torvalds 		/* search this hash bucket for a keyring with a matching name
11221da177e4SLinus Torvalds 		 * that's readable and that hasn't been revoked */
11231da177e4SLinus Torvalds 		list_for_each_entry(keyring,
11241da177e4SLinus Torvalds 				    &keyring_name_hash[bucket],
1125146aa8b1SDavid Howells 				    name_link
11261da177e4SLinus Torvalds 				    ) {
11279a56c2dbSEric W. Biederman 			if (!kuid_has_mapping(current_user_ns(), keyring->user->uid))
11282ea190d0SSerge E. Hallyn 				continue;
11292ea190d0SSerge E. Hallyn 
113076d8aeabSDavid Howells 			if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
11311da177e4SLinus Torvalds 				continue;
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 			if (strcmp(keyring->description, name) != 0)
11341da177e4SLinus Torvalds 				continue;
11351da177e4SLinus Torvalds 
113669664cf1SDavid Howells 			if (!skip_perm_check &&
113769664cf1SDavid Howells 			    key_permission(make_key_ref(keyring, 0),
1138f5895943SDavid Howells 					   KEY_NEED_SEARCH) < 0)
11391da177e4SLinus Torvalds 				continue;
11401da177e4SLinus Torvalds 
1141cea7daa3SToshiyuki Okajima 			/* we've got a match but we might end up racing with
1142cea7daa3SToshiyuki Okajima 			 * key_cleanup() if the keyring is currently 'dead'
1143cea7daa3SToshiyuki Okajima 			 * (ie. it has a zero usage count) */
1144fff29291SElena Reshetova 			if (!refcount_inc_not_zero(&keyring->usage))
1145cea7daa3SToshiyuki Okajima 				continue;
114631d5a79dSDavid Howells 			keyring->last_used_at = current_kernel_time().tv_sec;
1147cea7daa3SToshiyuki Okajima 			goto out;
11481da177e4SLinus Torvalds 		}
11491da177e4SLinus Torvalds 	}
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	keyring = ERR_PTR(-ENOKEY);
1152cea7daa3SToshiyuki Okajima out:
1153cea7daa3SToshiyuki Okajima 	read_unlock(&keyring_name_lock);
11541da177e4SLinus Torvalds 	return keyring;
1155a8b17ed0SDavid Howells }
11561da177e4SLinus Torvalds 
1157b2a4df20SDavid Howells static int keyring_detect_cycle_iterator(const void *object,
1158b2a4df20SDavid Howells 					 void *iterator_data)
1159b2a4df20SDavid Howells {
1160b2a4df20SDavid Howells 	struct keyring_search_context *ctx = iterator_data;
1161b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
1162b2a4df20SDavid Howells 
1163b2a4df20SDavid Howells 	kenter("{%d}", key->serial);
1164b2a4df20SDavid Howells 
1165979e0d74SDavid Howells 	/* We might get a keyring with matching index-key that is nonetheless a
1166979e0d74SDavid Howells 	 * different keyring. */
116746291959SDavid Howells 	if (key != ctx->match_data.raw_data)
1168979e0d74SDavid Howells 		return 0;
1169979e0d74SDavid Howells 
1170b2a4df20SDavid Howells 	ctx->result = ERR_PTR(-EDEADLK);
1171b2a4df20SDavid Howells 	return 1;
1172b2a4df20SDavid Howells }
1173b2a4df20SDavid Howells 
11741da177e4SLinus Torvalds /*
1175973c9f4fSDavid Howells  * See if a cycle will will be created by inserting acyclic tree B in acyclic
1176973c9f4fSDavid Howells  * tree A at the topmost level (ie: as a direct child of A).
1177973c9f4fSDavid Howells  *
1178973c9f4fSDavid Howells  * Since we are adding B to A at the top level, checking for cycles should just
1179973c9f4fSDavid Howells  * be a matter of seeing if node A is somewhere in tree B.
11801da177e4SLinus Torvalds  */
11811da177e4SLinus Torvalds static int keyring_detect_cycle(struct key *A, struct key *B)
11821da177e4SLinus Torvalds {
1183b2a4df20SDavid Howells 	struct keyring_search_context ctx = {
1184b2a4df20SDavid Howells 		.index_key		= A->index_key,
118546291959SDavid Howells 		.match_data.raw_data	= A,
118646291959SDavid Howells 		.match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
1187b2a4df20SDavid Howells 		.iterator		= keyring_detect_cycle_iterator,
118846291959SDavid Howells 		.flags			= (KEYRING_SEARCH_NO_STATE_CHECK |
1189b2a4df20SDavid Howells 					   KEYRING_SEARCH_NO_UPDATE_TIME |
1190b2a4df20SDavid Howells 					   KEYRING_SEARCH_NO_CHECK_PERM |
1191b2a4df20SDavid Howells 					   KEYRING_SEARCH_DETECT_TOO_DEEP),
1192b2a4df20SDavid Howells 	};
11931da177e4SLinus Torvalds 
119476d8aeabSDavid Howells 	rcu_read_lock();
1195b2a4df20SDavid Howells 	search_nested_keyrings(B, &ctx);
119676d8aeabSDavid Howells 	rcu_read_unlock();
1197b2a4df20SDavid Howells 	return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result);
1198f70e2e06SDavid Howells }
1199cab8eb59SDavid Howells 
1200cab8eb59SDavid Howells /*
1201973c9f4fSDavid Howells  * Preallocate memory so that a key can be linked into to a keyring.
12021da177e4SLinus Torvalds  */
1203b2a4df20SDavid Howells int __key_link_begin(struct key *keyring,
1204b2a4df20SDavid Howells 		     const struct keyring_index_key *index_key,
1205b2a4df20SDavid Howells 		     struct assoc_array_edit **_edit)
1206f70e2e06SDavid Howells 	__acquires(&keyring->sem)
1207423b9788SDavid Howells 	__acquires(&keyring_serialise_link_sem)
12081da177e4SLinus Torvalds {
1209b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1210b2a4df20SDavid Howells 	int ret;
12111da177e4SLinus Torvalds 
121216feef43SDavid Howells 	kenter("%d,%s,%s,",
1213b2a4df20SDavid Howells 	       keyring->serial, index_key->type->name, index_key->description);
1214b2a4df20SDavid Howells 
1215b2a4df20SDavid Howells 	BUG_ON(index_key->desc_len == 0);
1216f70e2e06SDavid Howells 
1217f70e2e06SDavid Howells 	if (keyring->type != &key_type_keyring)
1218f70e2e06SDavid Howells 		return -ENOTDIR;
1219f70e2e06SDavid Howells 
1220f70e2e06SDavid Howells 	down_write(&keyring->sem);
1221f70e2e06SDavid Howells 
12221da177e4SLinus Torvalds 	ret = -EKEYREVOKED;
122376d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1224f70e2e06SDavid Howells 		goto error_krsem;
12251da177e4SLinus Torvalds 
1226f70e2e06SDavid Howells 	/* serialise link/link calls to prevent parallel calls causing a cycle
1227f70e2e06SDavid Howells 	 * when linking two keyring in opposite orders */
122816feef43SDavid Howells 	if (index_key->type == &key_type_keyring)
12291da177e4SLinus Torvalds 		down_write(&keyring_serialise_link_sem);
12301da177e4SLinus Torvalds 
1231b2a4df20SDavid Howells 	/* Create an edit script that will insert/replace the key in the
1232b2a4df20SDavid Howells 	 * keyring tree.
1233b2a4df20SDavid Howells 	 */
1234b2a4df20SDavid Howells 	edit = assoc_array_insert(&keyring->keys,
1235b2a4df20SDavid Howells 				  &keyring_assoc_array_ops,
1236b2a4df20SDavid Howells 				  index_key,
1237b2a4df20SDavid Howells 				  NULL);
1238b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1239b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1240034faeb9SDavid Howells 		goto error_sem;
1241034faeb9SDavid Howells 	}
1242034faeb9SDavid Howells 
1243034faeb9SDavid Howells 	/* If we're not replacing a link in-place then we're going to need some
1244034faeb9SDavid Howells 	 * extra quota.
1245034faeb9SDavid Howells 	 */
1246034faeb9SDavid Howells 	if (!edit->dead_leaf) {
1247034faeb9SDavid Howells 		ret = key_payload_reserve(keyring,
1248034faeb9SDavid Howells 					  keyring->datalen + KEYQUOTA_LINK_BYTES);
1249034faeb9SDavid Howells 		if (ret < 0)
1250034faeb9SDavid Howells 			goto error_cancel;
12511da177e4SLinus Torvalds 	}
12521da177e4SLinus Torvalds 
1253b2a4df20SDavid Howells 	*_edit = edit;
1254f70e2e06SDavid Howells 	kleave(" = 0");
1255f70e2e06SDavid Howells 	return 0;
12561da177e4SLinus Torvalds 
1257034faeb9SDavid Howells error_cancel:
1258034faeb9SDavid Howells 	assoc_array_cancel_edit(edit);
1259f70e2e06SDavid Howells error_sem:
126016feef43SDavid Howells 	if (index_key->type == &key_type_keyring)
1261f70e2e06SDavid Howells 		up_write(&keyring_serialise_link_sem);
1262f70e2e06SDavid Howells error_krsem:
1263f70e2e06SDavid Howells 	up_write(&keyring->sem);
1264f70e2e06SDavid Howells 	kleave(" = %d", ret);
1265f70e2e06SDavid Howells 	return ret;
1266f70e2e06SDavid Howells }
12671da177e4SLinus Torvalds 
1268f70e2e06SDavid Howells /*
1269973c9f4fSDavid Howells  * Check already instantiated keys aren't going to be a problem.
1270973c9f4fSDavid Howells  *
1271973c9f4fSDavid Howells  * The caller must have called __key_link_begin(). Don't need to call this for
1272973c9f4fSDavid Howells  * keys that were created since __key_link_begin() was called.
1273f70e2e06SDavid Howells  */
1274f70e2e06SDavid Howells int __key_link_check_live_key(struct key *keyring, struct key *key)
1275f70e2e06SDavid Howells {
1276f70e2e06SDavid Howells 	if (key->type == &key_type_keyring)
1277f70e2e06SDavid Howells 		/* check that we aren't going to create a cycle by linking one
1278f70e2e06SDavid Howells 		 * keyring to another */
1279f70e2e06SDavid Howells 		return keyring_detect_cycle(keyring, key);
1280f70e2e06SDavid Howells 	return 0;
1281f70e2e06SDavid Howells }
12821da177e4SLinus Torvalds 
1283f70e2e06SDavid Howells /*
1284973c9f4fSDavid Howells  * Link a key into to a keyring.
1285973c9f4fSDavid Howells  *
1286973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.  Discards any
1287973c9f4fSDavid Howells  * already extant link to matching key if there is one, so that each keyring
1288973c9f4fSDavid Howells  * holds at most one link to any given key of a particular type+description
1289973c9f4fSDavid Howells  * combination.
1290f70e2e06SDavid Howells  */
1291b2a4df20SDavid Howells void __key_link(struct key *key, struct assoc_array_edit **_edit)
1292f70e2e06SDavid Howells {
1293ccc3e6d9SDavid Howells 	__key_get(key);
1294b2a4df20SDavid Howells 	assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key));
1295b2a4df20SDavid Howells 	assoc_array_apply_edit(*_edit);
1296b2a4df20SDavid Howells 	*_edit = NULL;
1297f70e2e06SDavid Howells }
1298f70e2e06SDavid Howells 
1299f70e2e06SDavid Howells /*
1300973c9f4fSDavid Howells  * Finish linking a key into to a keyring.
1301973c9f4fSDavid Howells  *
1302973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.
1303f70e2e06SDavid Howells  */
130416feef43SDavid Howells void __key_link_end(struct key *keyring,
130516feef43SDavid Howells 		    const struct keyring_index_key *index_key,
1306b2a4df20SDavid Howells 		    struct assoc_array_edit *edit)
1307f70e2e06SDavid Howells 	__releases(&keyring->sem)
1308423b9788SDavid Howells 	__releases(&keyring_serialise_link_sem)
1309f70e2e06SDavid Howells {
131016feef43SDavid Howells 	BUG_ON(index_key->type == NULL);
1311b2a4df20SDavid Howells 	kenter("%d,%s,", keyring->serial, index_key->type->name);
1312f70e2e06SDavid Howells 
131316feef43SDavid Howells 	if (index_key->type == &key_type_keyring)
1314f70e2e06SDavid Howells 		up_write(&keyring_serialise_link_sem);
1315f70e2e06SDavid Howells 
1316ca4da5ddSColin Ian King 	if (edit) {
1317ca4da5ddSColin Ian King 		if (!edit->dead_leaf) {
1318f70e2e06SDavid Howells 			key_payload_reserve(keyring,
1319b2a4df20SDavid Howells 				keyring->datalen - KEYQUOTA_LINK_BYTES);
1320ca4da5ddSColin Ian King 		}
1321b2a4df20SDavid Howells 		assoc_array_cancel_edit(edit);
1322f70e2e06SDavid Howells 	}
1323f70e2e06SDavid Howells 	up_write(&keyring->sem);
1324f70e2e06SDavid Howells }
1325f70e2e06SDavid Howells 
13265ac7eaceSDavid Howells /*
13275ac7eaceSDavid Howells  * Check addition of keys to restricted keyrings.
13285ac7eaceSDavid Howells  */
13295ac7eaceSDavid Howells static int __key_link_check_restriction(struct key *keyring, struct key *key)
13305ac7eaceSDavid Howells {
13312b6aa412SMat Martineau 	if (!keyring->restrict_link || !keyring->restrict_link->check)
13325ac7eaceSDavid Howells 		return 0;
13332b6aa412SMat Martineau 	return keyring->restrict_link->check(keyring, key->type, &key->payload,
13342b6aa412SMat Martineau 					     keyring->restrict_link->key);
13355ac7eaceSDavid Howells }
13365ac7eaceSDavid Howells 
1337973c9f4fSDavid Howells /**
1338973c9f4fSDavid Howells  * key_link - Link a key to a keyring
1339973c9f4fSDavid Howells  * @keyring: The keyring to make the link in.
1340973c9f4fSDavid Howells  * @key: The key to link to.
1341973c9f4fSDavid Howells  *
1342973c9f4fSDavid Howells  * Make a link in a keyring to a key, such that the keyring holds a reference
1343973c9f4fSDavid Howells  * on that key and the key can potentially be found by searching that keyring.
1344973c9f4fSDavid Howells  *
1345973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore and will consume some
1346973c9f4fSDavid Howells  * of the user's key data quota to hold the link.
1347973c9f4fSDavid Howells  *
1348973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
1349973c9f4fSDavid Howells  * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
1350973c9f4fSDavid Howells  * full, -EDQUOT if there is insufficient key data quota remaining to add
1351973c9f4fSDavid Howells  * another link or -ENOMEM if there's insufficient memory.
1352973c9f4fSDavid Howells  *
1353973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1354973c9f4fSDavid Howells  * be made (the keyring should have Write permission and the key Link
1355973c9f4fSDavid Howells  * permission).
13561da177e4SLinus Torvalds  */
13571da177e4SLinus Torvalds int key_link(struct key *keyring, struct key *key)
13581da177e4SLinus Torvalds {
1359b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
13601da177e4SLinus Torvalds 	int ret;
13611da177e4SLinus Torvalds 
1362fff29291SElena Reshetova 	kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage));
1363b2a4df20SDavid Howells 
13641da177e4SLinus Torvalds 	key_check(keyring);
13651da177e4SLinus Torvalds 	key_check(key);
13661da177e4SLinus Torvalds 
1367b2a4df20SDavid Howells 	ret = __key_link_begin(keyring, &key->index_key, &edit);
1368f70e2e06SDavid Howells 	if (ret == 0) {
1369fff29291SElena Reshetova 		kdebug("begun {%d,%d}", keyring->serial, refcount_read(&keyring->usage));
13705ac7eaceSDavid Howells 		ret = __key_link_check_restriction(keyring, key);
13715ac7eaceSDavid Howells 		if (ret == 0)
1372f70e2e06SDavid Howells 			ret = __key_link_check_live_key(keyring, key);
1373f70e2e06SDavid Howells 		if (ret == 0)
1374b2a4df20SDavid Howells 			__key_link(key, &edit);
1375b2a4df20SDavid Howells 		__key_link_end(keyring, &key->index_key, edit);
1376f70e2e06SDavid Howells 	}
13771da177e4SLinus Torvalds 
1378fff29291SElena Reshetova 	kleave(" = %d {%d,%d}", ret, keyring->serial, refcount_read(&keyring->usage));
13791da177e4SLinus Torvalds 	return ret;
1380f70e2e06SDavid Howells }
13811da177e4SLinus Torvalds EXPORT_SYMBOL(key_link);
13821da177e4SLinus Torvalds 
1383973c9f4fSDavid Howells /**
1384973c9f4fSDavid Howells  * key_unlink - Unlink the first link to a key from a keyring.
1385973c9f4fSDavid Howells  * @keyring: The keyring to remove the link from.
1386973c9f4fSDavid Howells  * @key: The key the link is to.
1387973c9f4fSDavid Howells  *
1388973c9f4fSDavid Howells  * Remove a link from a keyring to a key.
1389973c9f4fSDavid Howells  *
1390973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore.
1391973c9f4fSDavid Howells  *
1392973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
1393973c9f4fSDavid Howells  * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
1394973c9f4fSDavid Howells  * memory.
1395973c9f4fSDavid Howells  *
1396973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1397973c9f4fSDavid Howells  * be removed (the keyring should have Write permission; no permissions are
1398973c9f4fSDavid Howells  * required on the key).
13991da177e4SLinus Torvalds  */
14001da177e4SLinus Torvalds int key_unlink(struct key *keyring, struct key *key)
14011da177e4SLinus Torvalds {
1402b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1403b2a4df20SDavid Howells 	int ret;
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	key_check(keyring);
14061da177e4SLinus Torvalds 	key_check(key);
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
1409b2a4df20SDavid Howells 		return -ENOTDIR;
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds 	down_write(&keyring->sem);
14121da177e4SLinus Torvalds 
1413b2a4df20SDavid Howells 	edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops,
1414b2a4df20SDavid Howells 				  &key->index_key);
1415b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1416b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1417b2a4df20SDavid Howells 		goto error;
14181da177e4SLinus Torvalds 	}
14191da177e4SLinus Torvalds 	ret = -ENOENT;
1420b2a4df20SDavid Howells 	if (edit == NULL)
14211da177e4SLinus Torvalds 		goto error;
14221da177e4SLinus Torvalds 
1423b2a4df20SDavid Howells 	assoc_array_apply_edit(edit);
1424034faeb9SDavid Howells 	key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES);
14251da177e4SLinus Torvalds 	ret = 0;
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds error:
142876d8aeabSDavid Howells 	up_write(&keyring->sem);
1429b2a4df20SDavid Howells 	return ret;
1430a8b17ed0SDavid Howells }
14311da177e4SLinus Torvalds EXPORT_SYMBOL(key_unlink);
14321da177e4SLinus Torvalds 
1433973c9f4fSDavid Howells /**
1434973c9f4fSDavid Howells  * keyring_clear - Clear a keyring
1435973c9f4fSDavid Howells  * @keyring: The keyring to clear.
1436973c9f4fSDavid Howells  *
1437973c9f4fSDavid Howells  * Clear the contents of the specified keyring.
1438973c9f4fSDavid Howells  *
1439973c9f4fSDavid Howells  * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
14401da177e4SLinus Torvalds  */
14411da177e4SLinus Torvalds int keyring_clear(struct key *keyring)
14421da177e4SLinus Torvalds {
1443b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
144476d8aeabSDavid Howells 	int ret;
14451da177e4SLinus Torvalds 
1446b2a4df20SDavid Howells 	if (keyring->type != &key_type_keyring)
1447b2a4df20SDavid Howells 		return -ENOTDIR;
1448b2a4df20SDavid Howells 
14491da177e4SLinus Torvalds 	down_write(&keyring->sem);
14501da177e4SLinus Torvalds 
1451b2a4df20SDavid Howells 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1452b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1453b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1454b2a4df20SDavid Howells 	} else {
1455b2a4df20SDavid Howells 		if (edit)
1456b2a4df20SDavid Howells 			assoc_array_apply_edit(edit);
1457b2a4df20SDavid Howells 		key_payload_reserve(keyring, 0);
14581da177e4SLinus Torvalds 		ret = 0;
14591da177e4SLinus Torvalds 	}
14601da177e4SLinus Torvalds 
1461b2a4df20SDavid Howells 	up_write(&keyring->sem);
14621da177e4SLinus Torvalds 	return ret;
1463a8b17ed0SDavid Howells }
14641da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_clear);
146531204ed9SDavid Howells 
146631204ed9SDavid Howells /*
1467973c9f4fSDavid Howells  * Dispose of the links from a revoked keyring.
1468973c9f4fSDavid Howells  *
1469973c9f4fSDavid Howells  * This is called with the key sem write-locked.
147031204ed9SDavid Howells  */
147131204ed9SDavid Howells static void keyring_revoke(struct key *keyring)
147231204ed9SDavid Howells {
1473b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1474f0641cbaSDavid Howells 
1475b2a4df20SDavid Howells 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1476b2a4df20SDavid Howells 	if (!IS_ERR(edit)) {
1477b2a4df20SDavid Howells 		if (edit)
1478b2a4df20SDavid Howells 			assoc_array_apply_edit(edit);
147931204ed9SDavid Howells 		key_payload_reserve(keyring, 0);
148031204ed9SDavid Howells 	}
1481a8b17ed0SDavid Howells }
14825d135440SDavid Howells 
148362fe3182SDavid Howells static bool keyring_gc_select_iterator(void *object, void *iterator_data)
1484b2a4df20SDavid Howells {
1485b2a4df20SDavid Howells 	struct key *key = keyring_ptr_to_key(object);
1486b2a4df20SDavid Howells 	time_t *limit = iterator_data;
1487b2a4df20SDavid Howells 
1488b2a4df20SDavid Howells 	if (key_is_dead(key, *limit))
1489b2a4df20SDavid Howells 		return false;
1490b2a4df20SDavid Howells 	key_get(key);
1491b2a4df20SDavid Howells 	return true;
1492b2a4df20SDavid Howells }
1493b2a4df20SDavid Howells 
149462fe3182SDavid Howells static int keyring_gc_check_iterator(const void *object, void *iterator_data)
149562fe3182SDavid Howells {
149662fe3182SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
149762fe3182SDavid Howells 	time_t *limit = iterator_data;
149862fe3182SDavid Howells 
149962fe3182SDavid Howells 	key_check(key);
150062fe3182SDavid Howells 	return key_is_dead(key, *limit);
150162fe3182SDavid Howells }
150262fe3182SDavid Howells 
15035d135440SDavid Howells /*
150462fe3182SDavid Howells  * Garbage collect pointers from a keyring.
1505973c9f4fSDavid Howells  *
150662fe3182SDavid Howells  * Not called with any locks held.  The keyring's key struct will not be
150762fe3182SDavid Howells  * deallocated under us as only our caller may deallocate it.
15085d135440SDavid Howells  */
15095d135440SDavid Howells void keyring_gc(struct key *keyring, time_t limit)
15105d135440SDavid Howells {
151162fe3182SDavid Howells 	int result;
15125d135440SDavid Howells 
151362fe3182SDavid Howells 	kenter("%x{%s}", keyring->serial, keyring->description ?: "");
151462fe3182SDavid Howells 
151562fe3182SDavid Howells 	if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
151662fe3182SDavid Howells 			      (1 << KEY_FLAG_REVOKED)))
151762fe3182SDavid Howells 		goto dont_gc;
151862fe3182SDavid Howells 
151962fe3182SDavid Howells 	/* scan the keyring looking for dead keys */
152062fe3182SDavid Howells 	rcu_read_lock();
152162fe3182SDavid Howells 	result = assoc_array_iterate(&keyring->keys,
152262fe3182SDavid Howells 				     keyring_gc_check_iterator, &limit);
152362fe3182SDavid Howells 	rcu_read_unlock();
152462fe3182SDavid Howells 	if (result == true)
152562fe3182SDavid Howells 		goto do_gc;
152662fe3182SDavid Howells 
152762fe3182SDavid Howells dont_gc:
152862fe3182SDavid Howells 	kleave(" [no gc]");
152962fe3182SDavid Howells 	return;
153062fe3182SDavid Howells 
153162fe3182SDavid Howells do_gc:
15325d135440SDavid Howells 	down_write(&keyring->sem);
1533b2a4df20SDavid Howells 	assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops,
153462fe3182SDavid Howells 		       keyring_gc_select_iterator, &limit);
15355d135440SDavid Howells 	up_write(&keyring->sem);
153662fe3182SDavid Howells 	kleave(" [gc]");
15375d135440SDavid Howells }
15382b6aa412SMat Martineau 
15392b6aa412SMat Martineau /*
15402b6aa412SMat Martineau  * Garbage collect restriction pointers from a keyring.
15412b6aa412SMat Martineau  *
15422b6aa412SMat Martineau  * Keyring restrictions are associated with a key type, and must be cleaned
15432b6aa412SMat Martineau  * up if the key type is unregistered. The restriction is altered to always
15442b6aa412SMat Martineau  * reject additional keys so a keyring cannot be opened up by unregistering
15452b6aa412SMat Martineau  * a key type.
15462b6aa412SMat Martineau  *
15472b6aa412SMat Martineau  * Not called with any keyring locks held. The keyring's key struct will not
15482b6aa412SMat Martineau  * be deallocated under us as only our caller may deallocate it.
15492b6aa412SMat Martineau  *
15502b6aa412SMat Martineau  * The caller is required to hold key_types_sem and dead_type->sem. This is
15512b6aa412SMat Martineau  * fulfilled by key_gc_keytype() holding the locks on behalf of
15522b6aa412SMat Martineau  * key_garbage_collector(), which it invokes on a workqueue.
15532b6aa412SMat Martineau  */
15542b6aa412SMat Martineau void keyring_restriction_gc(struct key *keyring, struct key_type *dead_type)
15552b6aa412SMat Martineau {
15562b6aa412SMat Martineau 	struct key_restriction *keyres;
15572b6aa412SMat Martineau 
15582b6aa412SMat Martineau 	kenter("%x{%s}", keyring->serial, keyring->description ?: "");
15592b6aa412SMat Martineau 
15602b6aa412SMat Martineau 	/*
15612b6aa412SMat Martineau 	 * keyring->restrict_link is only assigned at key allocation time
15622b6aa412SMat Martineau 	 * or with the key type locked, so the only values that could be
15632b6aa412SMat Martineau 	 * concurrently assigned to keyring->restrict_link are for key
15642b6aa412SMat Martineau 	 * types other than dead_type. Given this, it's ok to check
15652b6aa412SMat Martineau 	 * the key type before acquiring keyring->sem.
15662b6aa412SMat Martineau 	 */
15672b6aa412SMat Martineau 	if (!dead_type || !keyring->restrict_link ||
15682b6aa412SMat Martineau 	    keyring->restrict_link->keytype != dead_type) {
15692b6aa412SMat Martineau 		kleave(" [no restriction gc]");
15702b6aa412SMat Martineau 		return;
15712b6aa412SMat Martineau 	}
15722b6aa412SMat Martineau 
15732b6aa412SMat Martineau 	/* Lock the keyring to ensure that a link is not in progress */
15742b6aa412SMat Martineau 	down_write(&keyring->sem);
15752b6aa412SMat Martineau 
15762b6aa412SMat Martineau 	keyres = keyring->restrict_link;
15772b6aa412SMat Martineau 
15782b6aa412SMat Martineau 	keyres->check = restrict_link_reject;
15792b6aa412SMat Martineau 
15802b6aa412SMat Martineau 	key_put(keyres->key);
15812b6aa412SMat Martineau 	keyres->key = NULL;
15822b6aa412SMat Martineau 	keyres->keytype = NULL;
15832b6aa412SMat Martineau 
15842b6aa412SMat Martineau 	up_write(&keyring->sem);
15852b6aa412SMat Martineau 
15862b6aa412SMat Martineau 	kleave(" [restriction gc]");
15872b6aa412SMat Martineau }
1588