xref: /openbmc/linux/security/keys/keyring.c (revision 5d19e20b534ff4c17dfba792f1f9e33e1378e3f9)
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  */
76*5d19e20bSDavid Howells static int keyring_preparse(struct key_preparsed_payload *prep);
77*5d19e20bSDavid 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,
89*5d19e20bSDavid Howells 	.preparse	= keyring_preparse,
90*5d19e20bSDavid Howells 	.free_preparse	= keyring_free_preparse,
911da177e4SLinus Torvalds 	.instantiate	= keyring_instantiate,
92b2a4df20SDavid Howells 	.match		= user_match,
9331204ed9SDavid Howells 	.revoke		= keyring_revoke,
941da177e4SLinus Torvalds 	.destroy	= keyring_destroy,
951da177e4SLinus Torvalds 	.describe	= keyring_describe,
961da177e4SLinus Torvalds 	.read		= keyring_read,
971da177e4SLinus Torvalds };
987318226eSDavid Howells EXPORT_SYMBOL(key_type_keyring);
997318226eSDavid Howells 
1001da177e4SLinus Torvalds /*
101973c9f4fSDavid Howells  * Semaphore to serialise link/link calls to prevent two link calls in parallel
102973c9f4fSDavid Howells  * introducing a cycle.
1031da177e4SLinus Torvalds  */
1041ae8f407SAdrian Bunk static DECLARE_RWSEM(keyring_serialise_link_sem);
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /*
107973c9f4fSDavid Howells  * Publish the name of a keyring so that it can be found by name (if it has
108973c9f4fSDavid Howells  * one).
1091da177e4SLinus Torvalds  */
11069664cf1SDavid Howells static void keyring_publish_name(struct key *keyring)
1111da177e4SLinus Torvalds {
1121da177e4SLinus Torvalds 	int bucket;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	if (keyring->description) {
1151da177e4SLinus Torvalds 		bucket = keyring_hash(keyring->description);
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 		if (!keyring_name_hash[bucket].next)
1201da177e4SLinus Torvalds 			INIT_LIST_HEAD(&keyring_name_hash[bucket]);
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 		list_add_tail(&keyring->type_data.link,
1231da177e4SLinus Torvalds 			      &keyring_name_hash[bucket]);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
1261da177e4SLinus Torvalds 	}
127a8b17ed0SDavid Howells }
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*
130*5d19e20bSDavid Howells  * Preparse a keyring payload
131*5d19e20bSDavid Howells  */
132*5d19e20bSDavid Howells static int keyring_preparse(struct key_preparsed_payload *prep)
133*5d19e20bSDavid Howells {
134*5d19e20bSDavid Howells 	return prep->datalen != 0 ? -EINVAL : 0;
135*5d19e20bSDavid Howells }
136*5d19e20bSDavid Howells 
137*5d19e20bSDavid Howells /*
138*5d19e20bSDavid Howells  * Free a preparse of a user defined key payload
139*5d19e20bSDavid Howells  */
140*5d19e20bSDavid Howells static void keyring_free_preparse(struct key_preparsed_payload *prep)
141*5d19e20bSDavid Howells {
142*5d19e20bSDavid Howells }
143*5d19e20bSDavid Howells 
144*5d19e20bSDavid Howells /*
145973c9f4fSDavid Howells  * Initialise a keyring.
146973c9f4fSDavid Howells  *
147973c9f4fSDavid Howells  * Returns 0 on success, -EINVAL if given any data.
1481da177e4SLinus Torvalds  */
1491da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
150cf7f601cSDavid Howells 			       struct key_preparsed_payload *prep)
1511da177e4SLinus Torvalds {
152b2a4df20SDavid Howells 	assoc_array_init(&keyring->keys);
1531da177e4SLinus Torvalds 	/* make the keyring available by name if it has one */
1541da177e4SLinus Torvalds 	keyring_publish_name(keyring);
155*5d19e20bSDavid Howells 	return 0;
156a8b17ed0SDavid Howells }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds /*
159b2a4df20SDavid Howells  * Multiply 64-bits by 32-bits to 96-bits and fold back to 64-bit.  Ideally we'd
160b2a4df20SDavid Howells  * fold the carry back too, but that requires inline asm.
1611da177e4SLinus Torvalds  */
162b2a4df20SDavid Howells static u64 mult_64x32_and_fold(u64 x, u32 y)
1631da177e4SLinus Torvalds {
164b2a4df20SDavid Howells 	u64 hi = (u64)(u32)(x >> 32) * y;
165b2a4df20SDavid Howells 	u64 lo = (u64)(u32)(x) * y;
166b2a4df20SDavid Howells 	return lo + ((u64)(u32)hi << 32) + (u32)(hi >> 32);
167a8b17ed0SDavid Howells }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds /*
170b2a4df20SDavid Howells  * Hash a key type and description.
171b2a4df20SDavid Howells  */
172b2a4df20SDavid Howells static unsigned long hash_key_type_and_desc(const struct keyring_index_key *index_key)
173b2a4df20SDavid Howells {
174b2a4df20SDavid Howells 	const unsigned level_shift = ASSOC_ARRAY_LEVEL_STEP;
175d54e58b7SDavid Howells 	const unsigned long fan_mask = ASSOC_ARRAY_FAN_MASK;
176b2a4df20SDavid Howells 	const char *description = index_key->description;
177b2a4df20SDavid Howells 	unsigned long hash, type;
178b2a4df20SDavid Howells 	u32 piece;
179b2a4df20SDavid Howells 	u64 acc;
180b2a4df20SDavid Howells 	int n, desc_len = index_key->desc_len;
181b2a4df20SDavid Howells 
182b2a4df20SDavid Howells 	type = (unsigned long)index_key->type;
183b2a4df20SDavid Howells 
184b2a4df20SDavid Howells 	acc = mult_64x32_and_fold(type, desc_len + 13);
185b2a4df20SDavid Howells 	acc = mult_64x32_and_fold(acc, 9207);
186b2a4df20SDavid Howells 	for (;;) {
187b2a4df20SDavid Howells 		n = desc_len;
188b2a4df20SDavid Howells 		if (n <= 0)
189b2a4df20SDavid Howells 			break;
190b2a4df20SDavid Howells 		if (n > 4)
191b2a4df20SDavid Howells 			n = 4;
192b2a4df20SDavid Howells 		piece = 0;
193b2a4df20SDavid Howells 		memcpy(&piece, description, n);
194b2a4df20SDavid Howells 		description += n;
195b2a4df20SDavid Howells 		desc_len -= n;
196b2a4df20SDavid Howells 		acc = mult_64x32_and_fold(acc, piece);
197b2a4df20SDavid Howells 		acc = mult_64x32_and_fold(acc, 9207);
198b2a4df20SDavid Howells 	}
199b2a4df20SDavid Howells 
200b2a4df20SDavid Howells 	/* Fold the hash down to 32 bits if need be. */
201b2a4df20SDavid Howells 	hash = acc;
202b2a4df20SDavid Howells 	if (ASSOC_ARRAY_KEY_CHUNK_SIZE == 32)
203b2a4df20SDavid Howells 		hash ^= acc >> 32;
204b2a4df20SDavid Howells 
205b2a4df20SDavid Howells 	/* Squidge all the keyrings into a separate part of the tree to
206b2a4df20SDavid Howells 	 * ordinary keys by making sure the lowest level segment in the hash is
207b2a4df20SDavid Howells 	 * zero for keyrings and non-zero otherwise.
208b2a4df20SDavid Howells 	 */
209d54e58b7SDavid Howells 	if (index_key->type != &key_type_keyring && (hash & fan_mask) == 0)
210b2a4df20SDavid Howells 		return hash | (hash >> (ASSOC_ARRAY_KEY_CHUNK_SIZE - level_shift)) | 1;
211d54e58b7SDavid Howells 	if (index_key->type == &key_type_keyring && (hash & fan_mask) != 0)
212d54e58b7SDavid Howells 		return (hash + (hash << level_shift)) & ~fan_mask;
213b2a4df20SDavid Howells 	return hash;
214b2a4df20SDavid Howells }
215b2a4df20SDavid Howells 
216b2a4df20SDavid Howells /*
217b2a4df20SDavid Howells  * Build the next index key chunk.
218b2a4df20SDavid Howells  *
219b2a4df20SDavid Howells  * On 32-bit systems the index key is laid out as:
220b2a4df20SDavid Howells  *
221b2a4df20SDavid Howells  *	0	4	5	9...
222b2a4df20SDavid Howells  *	hash	desclen	typeptr	desc[]
223b2a4df20SDavid Howells  *
224b2a4df20SDavid Howells  * On 64-bit systems:
225b2a4df20SDavid Howells  *
226b2a4df20SDavid Howells  *	0	8	9	17...
227b2a4df20SDavid Howells  *	hash	desclen	typeptr	desc[]
228b2a4df20SDavid Howells  *
229b2a4df20SDavid Howells  * We return it one word-sized chunk at a time.
230b2a4df20SDavid Howells  */
231b2a4df20SDavid Howells static unsigned long keyring_get_key_chunk(const void *data, int level)
232b2a4df20SDavid Howells {
233b2a4df20SDavid Howells 	const struct keyring_index_key *index_key = data;
234b2a4df20SDavid Howells 	unsigned long chunk = 0;
235b2a4df20SDavid Howells 	long offset = 0;
236b2a4df20SDavid Howells 	int desc_len = index_key->desc_len, n = sizeof(chunk);
237b2a4df20SDavid Howells 
238b2a4df20SDavid Howells 	level /= ASSOC_ARRAY_KEY_CHUNK_SIZE;
239b2a4df20SDavid Howells 	switch (level) {
240b2a4df20SDavid Howells 	case 0:
241b2a4df20SDavid Howells 		return hash_key_type_and_desc(index_key);
242b2a4df20SDavid Howells 	case 1:
243b2a4df20SDavid Howells 		return ((unsigned long)index_key->type << 8) | desc_len;
244b2a4df20SDavid Howells 	case 2:
245b2a4df20SDavid Howells 		if (desc_len == 0)
246b2a4df20SDavid Howells 			return (u8)((unsigned long)index_key->type >>
247b2a4df20SDavid Howells 				    (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8));
248b2a4df20SDavid Howells 		n--;
249b2a4df20SDavid Howells 		offset = 1;
250b2a4df20SDavid Howells 	default:
251b2a4df20SDavid Howells 		offset += sizeof(chunk) - 1;
252b2a4df20SDavid Howells 		offset += (level - 3) * sizeof(chunk);
253b2a4df20SDavid Howells 		if (offset >= desc_len)
254b2a4df20SDavid Howells 			return 0;
255b2a4df20SDavid Howells 		desc_len -= offset;
256b2a4df20SDavid Howells 		if (desc_len > n)
257b2a4df20SDavid Howells 			desc_len = n;
258b2a4df20SDavid Howells 		offset += desc_len;
259b2a4df20SDavid Howells 		do {
260b2a4df20SDavid Howells 			chunk <<= 8;
261b2a4df20SDavid Howells 			chunk |= ((u8*)index_key->description)[--offset];
262b2a4df20SDavid Howells 		} while (--desc_len > 0);
263b2a4df20SDavid Howells 
264b2a4df20SDavid Howells 		if (level == 2) {
265b2a4df20SDavid Howells 			chunk <<= 8;
266b2a4df20SDavid Howells 			chunk |= (u8)((unsigned long)index_key->type >>
267b2a4df20SDavid Howells 				      (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8));
268b2a4df20SDavid Howells 		}
269b2a4df20SDavid Howells 		return chunk;
270b2a4df20SDavid Howells 	}
271b2a4df20SDavid Howells }
272b2a4df20SDavid Howells 
273b2a4df20SDavid Howells static unsigned long keyring_get_object_key_chunk(const void *object, int level)
274b2a4df20SDavid Howells {
275b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
276b2a4df20SDavid Howells 	return keyring_get_key_chunk(&key->index_key, level);
277b2a4df20SDavid Howells }
278b2a4df20SDavid Howells 
279b2a4df20SDavid Howells static bool keyring_compare_object(const void *object, const void *data)
280b2a4df20SDavid Howells {
281b2a4df20SDavid Howells 	const struct keyring_index_key *index_key = data;
282b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
283b2a4df20SDavid Howells 
284b2a4df20SDavid Howells 	return key->index_key.type == index_key->type &&
285b2a4df20SDavid Howells 		key->index_key.desc_len == index_key->desc_len &&
286b2a4df20SDavid Howells 		memcmp(key->index_key.description, index_key->description,
287b2a4df20SDavid Howells 		       index_key->desc_len) == 0;
288b2a4df20SDavid Howells }
289b2a4df20SDavid Howells 
290b2a4df20SDavid Howells /*
291b2a4df20SDavid Howells  * Compare the index keys of a pair of objects and determine the bit position
292b2a4df20SDavid Howells  * at which they differ - if they differ.
293b2a4df20SDavid Howells  */
29423fd78d7SDavid Howells static int keyring_diff_objects(const void *object, const void *data)
295b2a4df20SDavid Howells {
29623fd78d7SDavid Howells 	const struct key *key_a = keyring_ptr_to_key(object);
297b2a4df20SDavid Howells 	const struct keyring_index_key *a = &key_a->index_key;
29823fd78d7SDavid Howells 	const struct keyring_index_key *b = data;
299b2a4df20SDavid Howells 	unsigned long seg_a, seg_b;
300b2a4df20SDavid Howells 	int level, i;
301b2a4df20SDavid Howells 
302b2a4df20SDavid Howells 	level = 0;
303b2a4df20SDavid Howells 	seg_a = hash_key_type_and_desc(a);
304b2a4df20SDavid Howells 	seg_b = hash_key_type_and_desc(b);
305b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
306b2a4df20SDavid Howells 		goto differ;
307b2a4df20SDavid Howells 
308b2a4df20SDavid Howells 	/* The number of bits contributed by the hash is controlled by a
309b2a4df20SDavid Howells 	 * constant in the assoc_array headers.  Everything else thereafter we
310b2a4df20SDavid Howells 	 * can deal with as being machine word-size dependent.
311b2a4df20SDavid Howells 	 */
312b2a4df20SDavid Howells 	level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8;
313b2a4df20SDavid Howells 	seg_a = a->desc_len;
314b2a4df20SDavid Howells 	seg_b = b->desc_len;
315b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
316b2a4df20SDavid Howells 		goto differ;
317b2a4df20SDavid Howells 
318b2a4df20SDavid Howells 	/* The next bit may not work on big endian */
319b2a4df20SDavid Howells 	level++;
320b2a4df20SDavid Howells 	seg_a = (unsigned long)a->type;
321b2a4df20SDavid Howells 	seg_b = (unsigned long)b->type;
322b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
323b2a4df20SDavid Howells 		goto differ;
324b2a4df20SDavid Howells 
325b2a4df20SDavid Howells 	level += sizeof(unsigned long);
326b2a4df20SDavid Howells 	if (a->desc_len == 0)
327b2a4df20SDavid Howells 		goto same;
328b2a4df20SDavid Howells 
329b2a4df20SDavid Howells 	i = 0;
330b2a4df20SDavid Howells 	if (((unsigned long)a->description | (unsigned long)b->description) &
331b2a4df20SDavid Howells 	    (sizeof(unsigned long) - 1)) {
332b2a4df20SDavid Howells 		do {
333b2a4df20SDavid Howells 			seg_a = *(unsigned long *)(a->description + i);
334b2a4df20SDavid Howells 			seg_b = *(unsigned long *)(b->description + i);
335b2a4df20SDavid Howells 			if ((seg_a ^ seg_b) != 0)
336b2a4df20SDavid Howells 				goto differ_plus_i;
337b2a4df20SDavid Howells 			i += sizeof(unsigned long);
338b2a4df20SDavid Howells 		} while (i < (a->desc_len & (sizeof(unsigned long) - 1)));
339b2a4df20SDavid Howells 	}
340b2a4df20SDavid Howells 
341b2a4df20SDavid Howells 	for (; i < a->desc_len; i++) {
342b2a4df20SDavid Howells 		seg_a = *(unsigned char *)(a->description + i);
343b2a4df20SDavid Howells 		seg_b = *(unsigned char *)(b->description + i);
344b2a4df20SDavid Howells 		if ((seg_a ^ seg_b) != 0)
345b2a4df20SDavid Howells 			goto differ_plus_i;
346b2a4df20SDavid Howells 	}
347b2a4df20SDavid Howells 
348b2a4df20SDavid Howells same:
349b2a4df20SDavid Howells 	return -1;
350b2a4df20SDavid Howells 
351b2a4df20SDavid Howells differ_plus_i:
352b2a4df20SDavid Howells 	level += i;
353b2a4df20SDavid Howells differ:
354b2a4df20SDavid Howells 	i = level * 8 + __ffs(seg_a ^ seg_b);
355b2a4df20SDavid Howells 	return i;
356b2a4df20SDavid Howells }
357b2a4df20SDavid Howells 
358b2a4df20SDavid Howells /*
359b2a4df20SDavid Howells  * Free an object after stripping the keyring flag off of the pointer.
360b2a4df20SDavid Howells  */
361b2a4df20SDavid Howells static void keyring_free_object(void *object)
362b2a4df20SDavid Howells {
363b2a4df20SDavid Howells 	key_put(keyring_ptr_to_key(object));
364b2a4df20SDavid Howells }
365b2a4df20SDavid Howells 
366b2a4df20SDavid Howells /*
367b2a4df20SDavid Howells  * Operations for keyring management by the index-tree routines.
368b2a4df20SDavid Howells  */
369b2a4df20SDavid Howells static const struct assoc_array_ops keyring_assoc_array_ops = {
370b2a4df20SDavid Howells 	.get_key_chunk		= keyring_get_key_chunk,
371b2a4df20SDavid Howells 	.get_object_key_chunk	= keyring_get_object_key_chunk,
372b2a4df20SDavid Howells 	.compare_object		= keyring_compare_object,
373b2a4df20SDavid Howells 	.diff_objects		= keyring_diff_objects,
374b2a4df20SDavid Howells 	.free_object		= keyring_free_object,
375b2a4df20SDavid Howells };
376b2a4df20SDavid Howells 
377b2a4df20SDavid Howells /*
378973c9f4fSDavid Howells  * Clean up a keyring when it is destroyed.  Unpublish its name if it had one
379973c9f4fSDavid Howells  * and dispose of its data.
380233e4735SDavid Howells  *
381233e4735SDavid Howells  * The garbage collector detects the final key_put(), removes the keyring from
382233e4735SDavid Howells  * the serial number tree and then does RCU synchronisation before coming here,
383233e4735SDavid Howells  * so we shouldn't need to worry about code poking around here with the RCU
384233e4735SDavid Howells  * readlock held by this time.
3851da177e4SLinus Torvalds  */
3861da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	if (keyring->description) {
3891da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
39094efe72fSDavid Howells 
39194efe72fSDavid Howells 		if (keyring->type_data.link.next != NULL &&
39294efe72fSDavid Howells 		    !list_empty(&keyring->type_data.link))
3931da177e4SLinus Torvalds 			list_del(&keyring->type_data.link);
39494efe72fSDavid Howells 
3951da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 
398b2a4df20SDavid Howells 	assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops);
399a8b17ed0SDavid Howells }
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds /*
402973c9f4fSDavid Howells  * Describe a keyring for /proc.
4031da177e4SLinus Torvalds  */
4041da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m)
4051da177e4SLinus Torvalds {
406c8563473Swzt.wzt@gmail.com 	if (keyring->description)
4071da177e4SLinus Torvalds 		seq_puts(m, keyring->description);
408c8563473Swzt.wzt@gmail.com 	else
4091da177e4SLinus Torvalds 		seq_puts(m, "[anon]");
4101da177e4SLinus Torvalds 
41178b7280cSDavid Howells 	if (key_is_instantiated(keyring)) {
412b2a4df20SDavid Howells 		if (keyring->keys.nr_leaves_on_tree != 0)
413b2a4df20SDavid Howells 			seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree);
4141da177e4SLinus Torvalds 		else
4151da177e4SLinus Torvalds 			seq_puts(m, ": empty");
416a8b17ed0SDavid Howells 	}
41778b7280cSDavid Howells }
4181da177e4SLinus Torvalds 
419b2a4df20SDavid Howells struct keyring_read_iterator_context {
420b2a4df20SDavid Howells 	size_t			qty;
421b2a4df20SDavid Howells 	size_t			count;
422b2a4df20SDavid Howells 	key_serial_t __user	*buffer;
423b2a4df20SDavid Howells };
424b2a4df20SDavid Howells 
425b2a4df20SDavid Howells static int keyring_read_iterator(const void *object, void *data)
426b2a4df20SDavid Howells {
427b2a4df20SDavid Howells 	struct keyring_read_iterator_context *ctx = data;
428b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
429b2a4df20SDavid Howells 	int ret;
430b2a4df20SDavid Howells 
431b2a4df20SDavid Howells 	kenter("{%s,%d},,{%zu/%zu}",
432b2a4df20SDavid Howells 	       key->type->name, key->serial, ctx->count, ctx->qty);
433b2a4df20SDavid Howells 
434b2a4df20SDavid Howells 	if (ctx->count >= ctx->qty)
435b2a4df20SDavid Howells 		return 1;
436b2a4df20SDavid Howells 
437b2a4df20SDavid Howells 	ret = put_user(key->serial, ctx->buffer);
438b2a4df20SDavid Howells 	if (ret < 0)
439b2a4df20SDavid Howells 		return ret;
440b2a4df20SDavid Howells 	ctx->buffer++;
441b2a4df20SDavid Howells 	ctx->count += sizeof(key->serial);
442b2a4df20SDavid Howells 	return 0;
443b2a4df20SDavid Howells }
444b2a4df20SDavid Howells 
4451da177e4SLinus Torvalds /*
446973c9f4fSDavid Howells  * Read a list of key IDs from the keyring's contents in binary form
447973c9f4fSDavid Howells  *
448b2a4df20SDavid Howells  * The keyring's semaphore is read-locked by the caller.  This prevents someone
449b2a4df20SDavid Howells  * from modifying it under us - which could cause us to read key IDs multiple
450b2a4df20SDavid Howells  * times.
4511da177e4SLinus Torvalds  */
4521da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
4531da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen)
4541da177e4SLinus Torvalds {
455b2a4df20SDavid Howells 	struct keyring_read_iterator_context ctx;
456b2a4df20SDavid Howells 	unsigned long nr_keys;
457b2a4df20SDavid Howells 	int ret;
4581da177e4SLinus Torvalds 
459b2a4df20SDavid Howells 	kenter("{%d},,%zu", key_serial(keyring), buflen);
4601da177e4SLinus Torvalds 
461b2a4df20SDavid Howells 	if (buflen & (sizeof(key_serial_t) - 1))
462b2a4df20SDavid Howells 		return -EINVAL;
4631da177e4SLinus Torvalds 
464b2a4df20SDavid Howells 	nr_keys = keyring->keys.nr_leaves_on_tree;
465b2a4df20SDavid Howells 	if (nr_keys == 0)
466b2a4df20SDavid Howells 		return 0;
4671da177e4SLinus Torvalds 
468b2a4df20SDavid Howells 	/* Calculate how much data we could return */
469b2a4df20SDavid Howells 	ctx.qty = nr_keys * sizeof(key_serial_t);
4701da177e4SLinus Torvalds 
471b2a4df20SDavid Howells 	if (!buffer || !buflen)
472b2a4df20SDavid Howells 		return ctx.qty;
4731da177e4SLinus Torvalds 
474b2a4df20SDavid Howells 	if (buflen > ctx.qty)
475b2a4df20SDavid Howells 		ctx.qty = buflen;
4761da177e4SLinus Torvalds 
477b2a4df20SDavid Howells 	/* Copy the IDs of the subscribed keys into the buffer */
478b2a4df20SDavid Howells 	ctx.buffer = (key_serial_t __user *)buffer;
479b2a4df20SDavid Howells 	ctx.count = 0;
480b2a4df20SDavid Howells 	ret = assoc_array_iterate(&keyring->keys, keyring_read_iterator, &ctx);
481b2a4df20SDavid Howells 	if (ret < 0) {
482b2a4df20SDavid Howells 		kleave(" = %d [iterate]", ret);
4831da177e4SLinus Torvalds 		return ret;
484a8b17ed0SDavid Howells 	}
4851da177e4SLinus Torvalds 
486b2a4df20SDavid Howells 	kleave(" = %zu [ok]", ctx.count);
487b2a4df20SDavid Howells 	return ctx.count;
488b2a4df20SDavid Howells }
489b2a4df20SDavid Howells 
4901da177e4SLinus Torvalds /*
491973c9f4fSDavid Howells  * Allocate a keyring and link into the destination keyring.
4921da177e4SLinus Torvalds  */
4939a56c2dbSEric W. Biederman struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
49496b5c8feSDavid Howells 			  const struct cred *cred, key_perm_t perm,
49596b5c8feSDavid Howells 			  unsigned long flags, struct key *dest)
4961da177e4SLinus Torvalds {
4971da177e4SLinus Torvalds 	struct key *keyring;
4981da177e4SLinus Torvalds 	int ret;
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	keyring = key_alloc(&key_type_keyring, description,
50196b5c8feSDavid Howells 			    uid, gid, cred, perm, flags);
5021da177e4SLinus Torvalds 	if (!IS_ERR(keyring)) {
5033e30148cSDavid Howells 		ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
5041da177e4SLinus Torvalds 		if (ret < 0) {
5051da177e4SLinus Torvalds 			key_put(keyring);
5061da177e4SLinus Torvalds 			keyring = ERR_PTR(ret);
5071da177e4SLinus Torvalds 		}
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	return keyring;
511a8b17ed0SDavid Howells }
512f8aa23a5SDavid Howells EXPORT_SYMBOL(keyring_alloc);
5131da177e4SLinus Torvalds 
514b2a4df20SDavid Howells /*
515b2a4df20SDavid Howells  * Iteration function to consider each key found.
516b2a4df20SDavid Howells  */
517b2a4df20SDavid Howells static int keyring_search_iterator(const void *object, void *iterator_data)
518b2a4df20SDavid Howells {
519b2a4df20SDavid Howells 	struct keyring_search_context *ctx = iterator_data;
520b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
521b2a4df20SDavid Howells 	unsigned long kflags = key->flags;
522b2a4df20SDavid Howells 
523b2a4df20SDavid Howells 	kenter("{%d}", key->serial);
524b2a4df20SDavid Howells 
525b2a4df20SDavid Howells 	/* ignore keys not of this type */
526b2a4df20SDavid Howells 	if (key->type != ctx->index_key.type) {
527b2a4df20SDavid Howells 		kleave(" = 0 [!type]");
528b2a4df20SDavid Howells 		return 0;
529b2a4df20SDavid Howells 	}
530b2a4df20SDavid Howells 
531b2a4df20SDavid Howells 	/* skip invalidated, revoked and expired keys */
532b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
533b2a4df20SDavid Howells 		if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
534b2a4df20SDavid Howells 			      (1 << KEY_FLAG_REVOKED))) {
535b2a4df20SDavid Howells 			ctx->result = ERR_PTR(-EKEYREVOKED);
536b2a4df20SDavid Howells 			kleave(" = %d [invrev]", ctx->skipped_ret);
537b2a4df20SDavid Howells 			goto skipped;
538b2a4df20SDavid Howells 		}
539b2a4df20SDavid Howells 
540b2a4df20SDavid Howells 		if (key->expiry && ctx->now.tv_sec >= key->expiry) {
541b2a4df20SDavid Howells 			ctx->result = ERR_PTR(-EKEYEXPIRED);
542b2a4df20SDavid Howells 			kleave(" = %d [expire]", ctx->skipped_ret);
543b2a4df20SDavid Howells 			goto skipped;
544b2a4df20SDavid Howells 		}
545b2a4df20SDavid Howells 	}
546b2a4df20SDavid Howells 
547b2a4df20SDavid Howells 	/* keys that don't match */
548b2a4df20SDavid Howells 	if (!ctx->match(key, ctx->match_data)) {
549b2a4df20SDavid Howells 		kleave(" = 0 [!match]");
550b2a4df20SDavid Howells 		return 0;
551b2a4df20SDavid Howells 	}
552b2a4df20SDavid Howells 
553b2a4df20SDavid Howells 	/* key must have search permissions */
554b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
555b2a4df20SDavid Howells 	    key_task_permission(make_key_ref(key, ctx->possessed),
556f5895943SDavid Howells 				ctx->cred, KEY_NEED_SEARCH) < 0) {
557b2a4df20SDavid Howells 		ctx->result = ERR_PTR(-EACCES);
558b2a4df20SDavid Howells 		kleave(" = %d [!perm]", ctx->skipped_ret);
559b2a4df20SDavid Howells 		goto skipped;
560b2a4df20SDavid Howells 	}
561b2a4df20SDavid Howells 
562b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
563b2a4df20SDavid Howells 		/* we set a different error code if we pass a negative key */
564b2a4df20SDavid Howells 		if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
56574792b00SDavid Howells 			smp_rmb();
566b2a4df20SDavid Howells 			ctx->result = ERR_PTR(key->type_data.reject_error);
567b2a4df20SDavid Howells 			kleave(" = %d [neg]", ctx->skipped_ret);
568b2a4df20SDavid Howells 			goto skipped;
569b2a4df20SDavid Howells 		}
570b2a4df20SDavid Howells 	}
571b2a4df20SDavid Howells 
572b2a4df20SDavid Howells 	/* Found */
573b2a4df20SDavid Howells 	ctx->result = make_key_ref(key, ctx->possessed);
574b2a4df20SDavid Howells 	kleave(" = 1 [found]");
575b2a4df20SDavid Howells 	return 1;
576b2a4df20SDavid Howells 
577b2a4df20SDavid Howells skipped:
578b2a4df20SDavid Howells 	return ctx->skipped_ret;
579b2a4df20SDavid Howells }
580b2a4df20SDavid Howells 
581b2a4df20SDavid Howells /*
582b2a4df20SDavid Howells  * Search inside a keyring for a key.  We can search by walking to it
583b2a4df20SDavid Howells  * directly based on its index-key or we can iterate over the entire
584b2a4df20SDavid Howells  * tree looking for it, based on the match function.
585b2a4df20SDavid Howells  */
586b2a4df20SDavid Howells static int search_keyring(struct key *keyring, struct keyring_search_context *ctx)
587b2a4df20SDavid Howells {
588b2a4df20SDavid Howells 	if ((ctx->flags & KEYRING_SEARCH_LOOKUP_TYPE) ==
589b2a4df20SDavid Howells 	    KEYRING_SEARCH_LOOKUP_DIRECT) {
590b2a4df20SDavid Howells 		const void *object;
591b2a4df20SDavid Howells 
592b2a4df20SDavid Howells 		object = assoc_array_find(&keyring->keys,
593b2a4df20SDavid Howells 					  &keyring_assoc_array_ops,
594b2a4df20SDavid Howells 					  &ctx->index_key);
595b2a4df20SDavid Howells 		return object ? ctx->iterator(object, ctx) : 0;
596b2a4df20SDavid Howells 	}
597b2a4df20SDavid Howells 	return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx);
598b2a4df20SDavid Howells }
599b2a4df20SDavid Howells 
600b2a4df20SDavid Howells /*
601b2a4df20SDavid Howells  * Search a tree of keyrings that point to other keyrings up to the maximum
602b2a4df20SDavid Howells  * depth.
603b2a4df20SDavid Howells  */
604b2a4df20SDavid Howells static bool search_nested_keyrings(struct key *keyring,
605b2a4df20SDavid Howells 				   struct keyring_search_context *ctx)
606b2a4df20SDavid Howells {
607b2a4df20SDavid Howells 	struct {
608b2a4df20SDavid Howells 		struct key *keyring;
609b2a4df20SDavid Howells 		struct assoc_array_node *node;
610b2a4df20SDavid Howells 		int slot;
611b2a4df20SDavid Howells 	} stack[KEYRING_SEARCH_MAX_DEPTH];
612b2a4df20SDavid Howells 
613b2a4df20SDavid Howells 	struct assoc_array_shortcut *shortcut;
614b2a4df20SDavid Howells 	struct assoc_array_node *node;
615b2a4df20SDavid Howells 	struct assoc_array_ptr *ptr;
616b2a4df20SDavid Howells 	struct key *key;
617b2a4df20SDavid Howells 	int sp = 0, slot;
618b2a4df20SDavid Howells 
619b2a4df20SDavid Howells 	kenter("{%d},{%s,%s}",
620b2a4df20SDavid Howells 	       keyring->serial,
621b2a4df20SDavid Howells 	       ctx->index_key.type->name,
622b2a4df20SDavid Howells 	       ctx->index_key.description);
623b2a4df20SDavid Howells 
624b2a4df20SDavid Howells 	if (ctx->index_key.description)
625b2a4df20SDavid Howells 		ctx->index_key.desc_len = strlen(ctx->index_key.description);
626b2a4df20SDavid Howells 
627b2a4df20SDavid Howells 	/* Check to see if this top-level keyring is what we are looking for
628b2a4df20SDavid Howells 	 * and whether it is valid or not.
629b2a4df20SDavid Howells 	 */
630b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_LOOKUP_ITERATE ||
631b2a4df20SDavid Howells 	    keyring_compare_object(keyring, &ctx->index_key)) {
632b2a4df20SDavid Howells 		ctx->skipped_ret = 2;
633b2a4df20SDavid Howells 		ctx->flags |= KEYRING_SEARCH_DO_STATE_CHECK;
634b2a4df20SDavid Howells 		switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) {
635b2a4df20SDavid Howells 		case 1:
636b2a4df20SDavid Howells 			goto found;
637b2a4df20SDavid Howells 		case 2:
638b2a4df20SDavid Howells 			return false;
639b2a4df20SDavid Howells 		default:
640b2a4df20SDavid Howells 			break;
641b2a4df20SDavid Howells 		}
642b2a4df20SDavid Howells 	}
643b2a4df20SDavid Howells 
644b2a4df20SDavid Howells 	ctx->skipped_ret = 0;
645b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_NO_STATE_CHECK)
646b2a4df20SDavid Howells 		ctx->flags &= ~KEYRING_SEARCH_DO_STATE_CHECK;
647b2a4df20SDavid Howells 
648b2a4df20SDavid Howells 	/* Start processing a new keyring */
649b2a4df20SDavid Howells descend_to_keyring:
650b2a4df20SDavid Howells 	kdebug("descend to %d", keyring->serial);
651b2a4df20SDavid Howells 	if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
652b2a4df20SDavid Howells 			      (1 << KEY_FLAG_REVOKED)))
653b2a4df20SDavid Howells 		goto not_this_keyring;
654b2a4df20SDavid Howells 
655b2a4df20SDavid Howells 	/* Search through the keys in this keyring before its searching its
656b2a4df20SDavid Howells 	 * subtrees.
657b2a4df20SDavid Howells 	 */
658b2a4df20SDavid Howells 	if (search_keyring(keyring, ctx))
659b2a4df20SDavid Howells 		goto found;
660b2a4df20SDavid Howells 
661b2a4df20SDavid Howells 	/* Then manually iterate through the keyrings nested in this one.
662b2a4df20SDavid Howells 	 *
663b2a4df20SDavid Howells 	 * Start from the root node of the index tree.  Because of the way the
664b2a4df20SDavid Howells 	 * hash function has been set up, keyrings cluster on the leftmost
665b2a4df20SDavid Howells 	 * branch of the root node (root slot 0) or in the root node itself.
666b2a4df20SDavid Howells 	 * Non-keyrings avoid the leftmost branch of the root entirely (root
667b2a4df20SDavid Howells 	 * slots 1-15).
668b2a4df20SDavid Howells 	 */
669b2a4df20SDavid Howells 	ptr = ACCESS_ONCE(keyring->keys.root);
670b2a4df20SDavid Howells 	if (!ptr)
671b2a4df20SDavid Howells 		goto not_this_keyring;
672b2a4df20SDavid Howells 
673b2a4df20SDavid Howells 	if (assoc_array_ptr_is_shortcut(ptr)) {
674b2a4df20SDavid Howells 		/* If the root is a shortcut, either the keyring only contains
675b2a4df20SDavid Howells 		 * keyring pointers (everything clusters behind root slot 0) or
676b2a4df20SDavid Howells 		 * doesn't contain any keyring pointers.
677b2a4df20SDavid Howells 		 */
678b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
679b2a4df20SDavid Howells 		smp_read_barrier_depends();
680b2a4df20SDavid Howells 		if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0)
681b2a4df20SDavid Howells 			goto not_this_keyring;
682b2a4df20SDavid Howells 
683b2a4df20SDavid Howells 		ptr = ACCESS_ONCE(shortcut->next_node);
684b2a4df20SDavid Howells 		node = assoc_array_ptr_to_node(ptr);
685b2a4df20SDavid Howells 		goto begin_node;
686b2a4df20SDavid Howells 	}
687b2a4df20SDavid Howells 
688b2a4df20SDavid Howells 	node = assoc_array_ptr_to_node(ptr);
689b2a4df20SDavid Howells 	smp_read_barrier_depends();
690b2a4df20SDavid Howells 
691b2a4df20SDavid Howells 	ptr = node->slots[0];
692b2a4df20SDavid Howells 	if (!assoc_array_ptr_is_meta(ptr))
693b2a4df20SDavid Howells 		goto begin_node;
694b2a4df20SDavid Howells 
695b2a4df20SDavid Howells descend_to_node:
696b2a4df20SDavid Howells 	/* Descend to a more distal node in this keyring's content tree and go
697b2a4df20SDavid Howells 	 * through that.
698b2a4df20SDavid Howells 	 */
699b2a4df20SDavid Howells 	kdebug("descend");
700b2a4df20SDavid Howells 	if (assoc_array_ptr_is_shortcut(ptr)) {
701b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
702b2a4df20SDavid Howells 		smp_read_barrier_depends();
703b2a4df20SDavid Howells 		ptr = ACCESS_ONCE(shortcut->next_node);
704b2a4df20SDavid Howells 		BUG_ON(!assoc_array_ptr_is_node(ptr));
705b2a4df20SDavid Howells 	}
7069c5e45dfSDavid Howells 	node = assoc_array_ptr_to_node(ptr);
707b2a4df20SDavid Howells 
708b2a4df20SDavid Howells begin_node:
709b2a4df20SDavid Howells 	kdebug("begin_node");
710b2a4df20SDavid Howells 	smp_read_barrier_depends();
711b2a4df20SDavid Howells 	slot = 0;
712b2a4df20SDavid Howells ascend_to_node:
713b2a4df20SDavid Howells 	/* Go through the slots in a node */
714b2a4df20SDavid Howells 	for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
715b2a4df20SDavid Howells 		ptr = ACCESS_ONCE(node->slots[slot]);
716b2a4df20SDavid Howells 
717b2a4df20SDavid Howells 		if (assoc_array_ptr_is_meta(ptr) && node->back_pointer)
718b2a4df20SDavid Howells 			goto descend_to_node;
719b2a4df20SDavid Howells 
720b2a4df20SDavid Howells 		if (!keyring_ptr_is_keyring(ptr))
721b2a4df20SDavid Howells 			continue;
722b2a4df20SDavid Howells 
723b2a4df20SDavid Howells 		key = keyring_ptr_to_key(ptr);
724b2a4df20SDavid Howells 
725b2a4df20SDavid Howells 		if (sp >= KEYRING_SEARCH_MAX_DEPTH) {
726b2a4df20SDavid Howells 			if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) {
727b2a4df20SDavid Howells 				ctx->result = ERR_PTR(-ELOOP);
728b2a4df20SDavid Howells 				return false;
729b2a4df20SDavid Howells 			}
730b2a4df20SDavid Howells 			goto not_this_keyring;
731b2a4df20SDavid Howells 		}
732b2a4df20SDavid Howells 
733b2a4df20SDavid Howells 		/* Search a nested keyring */
734b2a4df20SDavid Howells 		if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
735b2a4df20SDavid Howells 		    key_task_permission(make_key_ref(key, ctx->possessed),
736f5895943SDavid Howells 					ctx->cred, KEY_NEED_SEARCH) < 0)
737b2a4df20SDavid Howells 			continue;
738b2a4df20SDavid Howells 
739b2a4df20SDavid Howells 		/* stack the current position */
740b2a4df20SDavid Howells 		stack[sp].keyring = keyring;
741b2a4df20SDavid Howells 		stack[sp].node = node;
742b2a4df20SDavid Howells 		stack[sp].slot = slot;
743b2a4df20SDavid Howells 		sp++;
744b2a4df20SDavid Howells 
745b2a4df20SDavid Howells 		/* begin again with the new keyring */
746b2a4df20SDavid Howells 		keyring = key;
747b2a4df20SDavid Howells 		goto descend_to_keyring;
748b2a4df20SDavid Howells 	}
749b2a4df20SDavid Howells 
750b2a4df20SDavid Howells 	/* We've dealt with all the slots in the current node, so now we need
751b2a4df20SDavid Howells 	 * to ascend to the parent and continue processing there.
752b2a4df20SDavid Howells 	 */
753b2a4df20SDavid Howells 	ptr = ACCESS_ONCE(node->back_pointer);
754b2a4df20SDavid Howells 	slot = node->parent_slot;
755b2a4df20SDavid Howells 
756b2a4df20SDavid Howells 	if (ptr && assoc_array_ptr_is_shortcut(ptr)) {
757b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
758b2a4df20SDavid Howells 		smp_read_barrier_depends();
759b2a4df20SDavid Howells 		ptr = ACCESS_ONCE(shortcut->back_pointer);
760b2a4df20SDavid Howells 		slot = shortcut->parent_slot;
761b2a4df20SDavid Howells 	}
762b2a4df20SDavid Howells 	if (!ptr)
763b2a4df20SDavid Howells 		goto not_this_keyring;
764b2a4df20SDavid Howells 	node = assoc_array_ptr_to_node(ptr);
765b2a4df20SDavid Howells 	smp_read_barrier_depends();
766b2a4df20SDavid Howells 	slot++;
767b2a4df20SDavid Howells 
768b2a4df20SDavid Howells 	/* If we've ascended to the root (zero backpointer), we must have just
769b2a4df20SDavid Howells 	 * finished processing the leftmost branch rather than the root slots -
770b2a4df20SDavid Howells 	 * so there can't be any more keyrings for us to find.
771b2a4df20SDavid Howells 	 */
772b2a4df20SDavid Howells 	if (node->back_pointer) {
773b2a4df20SDavid Howells 		kdebug("ascend %d", slot);
774b2a4df20SDavid Howells 		goto ascend_to_node;
775b2a4df20SDavid Howells 	}
776b2a4df20SDavid Howells 
777b2a4df20SDavid Howells 	/* The keyring we're looking at was disqualified or didn't contain a
778b2a4df20SDavid Howells 	 * matching key.
779b2a4df20SDavid Howells 	 */
780b2a4df20SDavid Howells not_this_keyring:
781b2a4df20SDavid Howells 	kdebug("not_this_keyring %d", sp);
782b2a4df20SDavid Howells 	if (sp <= 0) {
783b2a4df20SDavid Howells 		kleave(" = false");
784b2a4df20SDavid Howells 		return false;
785b2a4df20SDavid Howells 	}
786b2a4df20SDavid Howells 
787b2a4df20SDavid Howells 	/* Resume the processing of a keyring higher up in the tree */
788b2a4df20SDavid Howells 	sp--;
789b2a4df20SDavid Howells 	keyring = stack[sp].keyring;
790b2a4df20SDavid Howells 	node = stack[sp].node;
791b2a4df20SDavid Howells 	slot = stack[sp].slot + 1;
792b2a4df20SDavid Howells 	kdebug("ascend to %d [%d]", keyring->serial, slot);
793b2a4df20SDavid Howells 	goto ascend_to_node;
794b2a4df20SDavid Howells 
795b2a4df20SDavid Howells 	/* We found a viable match */
796b2a4df20SDavid Howells found:
797b2a4df20SDavid Howells 	key = key_ref_to_ptr(ctx->result);
798b2a4df20SDavid Howells 	key_check(key);
799b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) {
800b2a4df20SDavid Howells 		key->last_used_at = ctx->now.tv_sec;
801b2a4df20SDavid Howells 		keyring->last_used_at = ctx->now.tv_sec;
802b2a4df20SDavid Howells 		while (sp > 0)
803b2a4df20SDavid Howells 			stack[--sp].keyring->last_used_at = ctx->now.tv_sec;
804b2a4df20SDavid Howells 	}
805b2a4df20SDavid Howells 	kleave(" = true");
806b2a4df20SDavid Howells 	return true;
807b2a4df20SDavid Howells }
808b2a4df20SDavid Howells 
809973c9f4fSDavid Howells /**
810973c9f4fSDavid Howells  * keyring_search_aux - Search a keyring tree for a key matching some criteria
811973c9f4fSDavid Howells  * @keyring_ref: A pointer to the keyring with possession indicator.
8124bdf0bc3SDavid Howells  * @ctx: The keyring search context.
813973c9f4fSDavid Howells  *
814973c9f4fSDavid Howells  * Search the supplied keyring tree for a key that matches the criteria given.
815973c9f4fSDavid Howells  * The root keyring and any linked keyrings must grant Search permission to the
816973c9f4fSDavid Howells  * caller to be searchable and keys can only be found if they too grant Search
817973c9f4fSDavid Howells  * to the caller. The possession flag on the root keyring pointer controls use
818973c9f4fSDavid Howells  * of the possessor bits in permissions checking of the entire tree.  In
819973c9f4fSDavid Howells  * addition, the LSM gets to forbid keyring searches and key matches.
820973c9f4fSDavid Howells  *
821973c9f4fSDavid Howells  * The search is performed as a breadth-then-depth search up to the prescribed
822973c9f4fSDavid Howells  * limit (KEYRING_SEARCH_MAX_DEPTH).
823973c9f4fSDavid Howells  *
824973c9f4fSDavid Howells  * Keys are matched to the type provided and are then filtered by the match
825973c9f4fSDavid Howells  * function, which is given the description to use in any way it sees fit.  The
826973c9f4fSDavid Howells  * match function may use any attributes of a key that it wishes to to
827973c9f4fSDavid Howells  * determine the match.  Normally the match function from the key type would be
828973c9f4fSDavid Howells  * used.
829973c9f4fSDavid Howells  *
830b2a4df20SDavid Howells  * RCU can be used to prevent the keyring key lists from disappearing without
831b2a4df20SDavid Howells  * the need to take lots of locks.
832973c9f4fSDavid Howells  *
833973c9f4fSDavid Howells  * Returns a pointer to the found key and increments the key usage count if
834973c9f4fSDavid Howells  * successful; -EAGAIN if no matching keys were found, or if expired or revoked
835973c9f4fSDavid Howells  * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
836973c9f4fSDavid Howells  * specified keyring wasn't a keyring.
837973c9f4fSDavid Howells  *
838973c9f4fSDavid Howells  * In the case of a successful return, the possession attribute from
839973c9f4fSDavid Howells  * @keyring_ref is propagated to the returned key reference.
8401da177e4SLinus Torvalds  */
841664cceb0SDavid Howells key_ref_t keyring_search_aux(key_ref_t keyring_ref,
8424bdf0bc3SDavid Howells 			     struct keyring_search_context *ctx)
8431da177e4SLinus Torvalds {
84431d5a79dSDavid Howells 	struct key *keyring;
8451da177e4SLinus Torvalds 	long err;
846b2a4df20SDavid Howells 
847b2a4df20SDavid Howells 	ctx->iterator = keyring_search_iterator;
848b2a4df20SDavid Howells 	ctx->possessed = is_key_possessed(keyring_ref);
849b2a4df20SDavid Howells 	ctx->result = ERR_PTR(-EAGAIN);
8501da177e4SLinus Torvalds 
851664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
8521da177e4SLinus Torvalds 	key_check(keyring);
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
855b2a4df20SDavid Howells 		return ERR_PTR(-ENOTDIR);
856b2a4df20SDavid Howells 
857b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) {
858f5895943SDavid Howells 		err = key_task_permission(keyring_ref, ctx->cred, KEY_NEED_SEARCH);
859b2a4df20SDavid Howells 		if (err < 0)
860b2a4df20SDavid Howells 			return ERR_PTR(err);
861b2a4df20SDavid Howells 	}
8621da177e4SLinus Torvalds 
863664cceb0SDavid Howells 	rcu_read_lock();
8644bdf0bc3SDavid Howells 	ctx->now = current_kernel_time();
865b2a4df20SDavid Howells 	if (search_nested_keyrings(keyring, ctx))
866b2a4df20SDavid Howells 		__key_get(key_ref_to_ptr(ctx->result));
86776d8aeabSDavid Howells 	rcu_read_unlock();
868b2a4df20SDavid Howells 	return ctx->result;
869a8b17ed0SDavid Howells }
8701da177e4SLinus Torvalds 
871973c9f4fSDavid Howells /**
872973c9f4fSDavid Howells  * keyring_search - Search the supplied keyring tree for a matching key
873973c9f4fSDavid Howells  * @keyring: The root of the keyring tree to be searched.
874973c9f4fSDavid Howells  * @type: The type of keyring we want to find.
875973c9f4fSDavid Howells  * @description: The name of the keyring we want to find.
876973c9f4fSDavid Howells  *
877973c9f4fSDavid Howells  * As keyring_search_aux() above, but using the current task's credentials and
878b2a4df20SDavid Howells  * type's default matching function and preferred search method.
8791da177e4SLinus Torvalds  */
880664cceb0SDavid Howells key_ref_t keyring_search(key_ref_t keyring,
8811da177e4SLinus Torvalds 			 struct key_type *type,
8821da177e4SLinus Torvalds 			 const char *description)
8831da177e4SLinus Torvalds {
8844bdf0bc3SDavid Howells 	struct keyring_search_context ctx = {
8854bdf0bc3SDavid Howells 		.index_key.type		= type,
8864bdf0bc3SDavid Howells 		.index_key.description	= description,
8874bdf0bc3SDavid Howells 		.cred			= current_cred(),
8884bdf0bc3SDavid Howells 		.match			= type->match,
8894bdf0bc3SDavid Howells 		.match_data		= description,
8904bdf0bc3SDavid Howells 		.flags			= (type->def_lookup_type |
8914bdf0bc3SDavid Howells 					   KEYRING_SEARCH_DO_STATE_CHECK),
8924bdf0bc3SDavid Howells 	};
8934bdf0bc3SDavid Howells 
8944bdf0bc3SDavid Howells 	if (!ctx.match)
8953e30148cSDavid Howells 		return ERR_PTR(-ENOKEY);
8963e30148cSDavid Howells 
8974bdf0bc3SDavid Howells 	return keyring_search_aux(keyring, &ctx);
898a8b17ed0SDavid Howells }
8991da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_search);
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds /*
902b2a4df20SDavid Howells  * Search the given keyring for a key that might be updated.
903973c9f4fSDavid Howells  *
904973c9f4fSDavid Howells  * The caller must guarantee that the keyring is a keyring and that the
905b2a4df20SDavid Howells  * permission is granted to modify the keyring as no check is made here.  The
906b2a4df20SDavid Howells  * caller must also hold a lock on the keyring semaphore.
907973c9f4fSDavid Howells  *
908973c9f4fSDavid Howells  * Returns a pointer to the found key with usage count incremented if
909b2a4df20SDavid Howells  * successful and returns NULL if not found.  Revoked and invalidated keys are
910b2a4df20SDavid Howells  * skipped over.
911973c9f4fSDavid Howells  *
912973c9f4fSDavid Howells  * If successful, the possession indicator is propagated from the keyring ref
913973c9f4fSDavid Howells  * to the returned key reference.
9141da177e4SLinus Torvalds  */
915b2a4df20SDavid Howells key_ref_t find_key_to_update(key_ref_t keyring_ref,
916e57e8669SDavid Howells 			     const struct keyring_index_key *index_key)
9171da177e4SLinus Torvalds {
918664cceb0SDavid Howells 	struct key *keyring, *key;
919b2a4df20SDavid Howells 	const void *object;
9201da177e4SLinus Torvalds 
921664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
922664cceb0SDavid Howells 
923b2a4df20SDavid Howells 	kenter("{%d},{%s,%s}",
924b2a4df20SDavid Howells 	       keyring->serial, index_key->type->name, index_key->description);
92576d8aeabSDavid Howells 
926b2a4df20SDavid Howells 	object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
927b2a4df20SDavid Howells 				  index_key);
928b2a4df20SDavid Howells 
929b2a4df20SDavid Howells 	if (object)
9301da177e4SLinus Torvalds 		goto found;
9311da177e4SLinus Torvalds 
932b2a4df20SDavid Howells 	kleave(" = NULL");
933b2a4df20SDavid Howells 	return NULL;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds found:
936b2a4df20SDavid Howells 	key = keyring_ptr_to_key(object);
937b2a4df20SDavid Howells 	if (key->flags & ((1 << KEY_FLAG_INVALIDATED) |
938b2a4df20SDavid Howells 			  (1 << KEY_FLAG_REVOKED))) {
939b2a4df20SDavid Howells 		kleave(" = NULL [x]");
940b2a4df20SDavid Howells 		return NULL;
941b2a4df20SDavid Howells 	}
942ccc3e6d9SDavid Howells 	__key_get(key);
943b2a4df20SDavid Howells 	kleave(" = {%d}", key->serial);
944b2a4df20SDavid Howells 	return make_key_ref(key, is_key_possessed(keyring_ref));
945a8b17ed0SDavid Howells }
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds /*
948973c9f4fSDavid Howells  * Find a keyring with the specified name.
949973c9f4fSDavid Howells  *
950973c9f4fSDavid Howells  * All named keyrings in the current user namespace are searched, provided they
951973c9f4fSDavid Howells  * grant Search permission directly to the caller (unless this check is
952973c9f4fSDavid Howells  * skipped).  Keyrings whose usage points have reached zero or who have been
953973c9f4fSDavid Howells  * revoked are skipped.
954973c9f4fSDavid Howells  *
955973c9f4fSDavid Howells  * Returns a pointer to the keyring with the keyring's refcount having being
956973c9f4fSDavid Howells  * incremented on success.  -ENOKEY is returned if a key could not be found.
9571da177e4SLinus Torvalds  */
95869664cf1SDavid Howells struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
9591da177e4SLinus Torvalds {
9601da177e4SLinus Torvalds 	struct key *keyring;
9611da177e4SLinus Torvalds 	int bucket;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	if (!name)
964cea7daa3SToshiyuki Okajima 		return ERR_PTR(-EINVAL);
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	bucket = keyring_hash(name);
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	read_lock(&keyring_name_lock);
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	if (keyring_name_hash[bucket].next) {
9711da177e4SLinus Torvalds 		/* search this hash bucket for a keyring with a matching name
9721da177e4SLinus Torvalds 		 * that's readable and that hasn't been revoked */
9731da177e4SLinus Torvalds 		list_for_each_entry(keyring,
9741da177e4SLinus Torvalds 				    &keyring_name_hash[bucket],
9751da177e4SLinus Torvalds 				    type_data.link
9761da177e4SLinus Torvalds 				    ) {
9779a56c2dbSEric W. Biederman 			if (!kuid_has_mapping(current_user_ns(), keyring->user->uid))
9782ea190d0SSerge E. Hallyn 				continue;
9792ea190d0SSerge E. Hallyn 
98076d8aeabSDavid Howells 			if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
9811da177e4SLinus Torvalds 				continue;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 			if (strcmp(keyring->description, name) != 0)
9841da177e4SLinus Torvalds 				continue;
9851da177e4SLinus Torvalds 
98669664cf1SDavid Howells 			if (!skip_perm_check &&
98769664cf1SDavid Howells 			    key_permission(make_key_ref(keyring, 0),
988f5895943SDavid Howells 					   KEY_NEED_SEARCH) < 0)
9891da177e4SLinus Torvalds 				continue;
9901da177e4SLinus Torvalds 
991cea7daa3SToshiyuki Okajima 			/* we've got a match but we might end up racing with
992cea7daa3SToshiyuki Okajima 			 * key_cleanup() if the keyring is currently 'dead'
993cea7daa3SToshiyuki Okajima 			 * (ie. it has a zero usage count) */
994cea7daa3SToshiyuki Okajima 			if (!atomic_inc_not_zero(&keyring->usage))
995cea7daa3SToshiyuki Okajima 				continue;
99631d5a79dSDavid Howells 			keyring->last_used_at = current_kernel_time().tv_sec;
997cea7daa3SToshiyuki Okajima 			goto out;
9981da177e4SLinus Torvalds 		}
9991da177e4SLinus Torvalds 	}
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	keyring = ERR_PTR(-ENOKEY);
1002cea7daa3SToshiyuki Okajima out:
1003cea7daa3SToshiyuki Okajima 	read_unlock(&keyring_name_lock);
10041da177e4SLinus Torvalds 	return keyring;
1005a8b17ed0SDavid Howells }
10061da177e4SLinus Torvalds 
1007b2a4df20SDavid Howells static int keyring_detect_cycle_iterator(const void *object,
1008b2a4df20SDavid Howells 					 void *iterator_data)
1009b2a4df20SDavid Howells {
1010b2a4df20SDavid Howells 	struct keyring_search_context *ctx = iterator_data;
1011b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
1012b2a4df20SDavid Howells 
1013b2a4df20SDavid Howells 	kenter("{%d}", key->serial);
1014b2a4df20SDavid Howells 
1015979e0d74SDavid Howells 	/* We might get a keyring with matching index-key that is nonetheless a
1016979e0d74SDavid Howells 	 * different keyring. */
1017979e0d74SDavid Howells 	if (key != ctx->match_data)
1018979e0d74SDavid Howells 		return 0;
1019979e0d74SDavid Howells 
1020b2a4df20SDavid Howells 	ctx->result = ERR_PTR(-EDEADLK);
1021b2a4df20SDavid Howells 	return 1;
1022b2a4df20SDavid Howells }
1023b2a4df20SDavid Howells 
10241da177e4SLinus Torvalds /*
1025973c9f4fSDavid Howells  * See if a cycle will will be created by inserting acyclic tree B in acyclic
1026973c9f4fSDavid Howells  * tree A at the topmost level (ie: as a direct child of A).
1027973c9f4fSDavid Howells  *
1028973c9f4fSDavid Howells  * Since we are adding B to A at the top level, checking for cycles should just
1029973c9f4fSDavid Howells  * be a matter of seeing if node A is somewhere in tree B.
10301da177e4SLinus Torvalds  */
10311da177e4SLinus Torvalds static int keyring_detect_cycle(struct key *A, struct key *B)
10321da177e4SLinus Torvalds {
1033b2a4df20SDavid Howells 	struct keyring_search_context ctx = {
1034b2a4df20SDavid Howells 		.index_key	= A->index_key,
1035b2a4df20SDavid Howells 		.match_data	= A,
1036b2a4df20SDavid Howells 		.iterator	= keyring_detect_cycle_iterator,
1037b2a4df20SDavid Howells 		.flags		= (KEYRING_SEARCH_LOOKUP_DIRECT |
1038b2a4df20SDavid Howells 				   KEYRING_SEARCH_NO_STATE_CHECK |
1039b2a4df20SDavid Howells 				   KEYRING_SEARCH_NO_UPDATE_TIME |
1040b2a4df20SDavid Howells 				   KEYRING_SEARCH_NO_CHECK_PERM |
1041b2a4df20SDavid Howells 				   KEYRING_SEARCH_DETECT_TOO_DEEP),
1042b2a4df20SDavid Howells 	};
10431da177e4SLinus Torvalds 
104476d8aeabSDavid Howells 	rcu_read_lock();
1045b2a4df20SDavid Howells 	search_nested_keyrings(B, &ctx);
104676d8aeabSDavid Howells 	rcu_read_unlock();
1047b2a4df20SDavid Howells 	return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result);
1048f70e2e06SDavid Howells }
1049cab8eb59SDavid Howells 
1050cab8eb59SDavid Howells /*
1051973c9f4fSDavid Howells  * Preallocate memory so that a key can be linked into to a keyring.
10521da177e4SLinus Torvalds  */
1053b2a4df20SDavid Howells int __key_link_begin(struct key *keyring,
1054b2a4df20SDavid Howells 		     const struct keyring_index_key *index_key,
1055b2a4df20SDavid Howells 		     struct assoc_array_edit **_edit)
1056f70e2e06SDavid Howells 	__acquires(&keyring->sem)
1057423b9788SDavid Howells 	__acquires(&keyring_serialise_link_sem)
10581da177e4SLinus Torvalds {
1059b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1060b2a4df20SDavid Howells 	int ret;
10611da177e4SLinus Torvalds 
106216feef43SDavid Howells 	kenter("%d,%s,%s,",
1063b2a4df20SDavid Howells 	       keyring->serial, index_key->type->name, index_key->description);
1064b2a4df20SDavid Howells 
1065b2a4df20SDavid Howells 	BUG_ON(index_key->desc_len == 0);
1066f70e2e06SDavid Howells 
1067f70e2e06SDavid Howells 	if (keyring->type != &key_type_keyring)
1068f70e2e06SDavid Howells 		return -ENOTDIR;
1069f70e2e06SDavid Howells 
1070f70e2e06SDavid Howells 	down_write(&keyring->sem);
1071f70e2e06SDavid Howells 
10721da177e4SLinus Torvalds 	ret = -EKEYREVOKED;
107376d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1074f70e2e06SDavid Howells 		goto error_krsem;
10751da177e4SLinus Torvalds 
1076f70e2e06SDavid Howells 	/* serialise link/link calls to prevent parallel calls causing a cycle
1077f70e2e06SDavid Howells 	 * when linking two keyring in opposite orders */
107816feef43SDavid Howells 	if (index_key->type == &key_type_keyring)
10791da177e4SLinus Torvalds 		down_write(&keyring_serialise_link_sem);
10801da177e4SLinus Torvalds 
1081b2a4df20SDavid Howells 	/* Create an edit script that will insert/replace the key in the
1082b2a4df20SDavid Howells 	 * keyring tree.
1083b2a4df20SDavid Howells 	 */
1084b2a4df20SDavid Howells 	edit = assoc_array_insert(&keyring->keys,
1085b2a4df20SDavid Howells 				  &keyring_assoc_array_ops,
1086b2a4df20SDavid Howells 				  index_key,
1087b2a4df20SDavid Howells 				  NULL);
1088b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1089b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1090034faeb9SDavid Howells 		goto error_sem;
1091034faeb9SDavid Howells 	}
1092034faeb9SDavid Howells 
1093034faeb9SDavid Howells 	/* If we're not replacing a link in-place then we're going to need some
1094034faeb9SDavid Howells 	 * extra quota.
1095034faeb9SDavid Howells 	 */
1096034faeb9SDavid Howells 	if (!edit->dead_leaf) {
1097034faeb9SDavid Howells 		ret = key_payload_reserve(keyring,
1098034faeb9SDavid Howells 					  keyring->datalen + KEYQUOTA_LINK_BYTES);
1099034faeb9SDavid Howells 		if (ret < 0)
1100034faeb9SDavid Howells 			goto error_cancel;
11011da177e4SLinus Torvalds 	}
11021da177e4SLinus Torvalds 
1103b2a4df20SDavid Howells 	*_edit = edit;
1104f70e2e06SDavid Howells 	kleave(" = 0");
1105f70e2e06SDavid Howells 	return 0;
11061da177e4SLinus Torvalds 
1107034faeb9SDavid Howells error_cancel:
1108034faeb9SDavid Howells 	assoc_array_cancel_edit(edit);
1109f70e2e06SDavid Howells error_sem:
111016feef43SDavid Howells 	if (index_key->type == &key_type_keyring)
1111f70e2e06SDavid Howells 		up_write(&keyring_serialise_link_sem);
1112f70e2e06SDavid Howells error_krsem:
1113f70e2e06SDavid Howells 	up_write(&keyring->sem);
1114f70e2e06SDavid Howells 	kleave(" = %d", ret);
1115f70e2e06SDavid Howells 	return ret;
1116f70e2e06SDavid Howells }
11171da177e4SLinus Torvalds 
1118f70e2e06SDavid Howells /*
1119973c9f4fSDavid Howells  * Check already instantiated keys aren't going to be a problem.
1120973c9f4fSDavid Howells  *
1121973c9f4fSDavid Howells  * The caller must have called __key_link_begin(). Don't need to call this for
1122973c9f4fSDavid Howells  * keys that were created since __key_link_begin() was called.
1123f70e2e06SDavid Howells  */
1124f70e2e06SDavid Howells int __key_link_check_live_key(struct key *keyring, struct key *key)
1125f70e2e06SDavid Howells {
1126f70e2e06SDavid Howells 	if (key->type == &key_type_keyring)
1127f70e2e06SDavid Howells 		/* check that we aren't going to create a cycle by linking one
1128f70e2e06SDavid Howells 		 * keyring to another */
1129f70e2e06SDavid Howells 		return keyring_detect_cycle(keyring, key);
1130f70e2e06SDavid Howells 	return 0;
1131f70e2e06SDavid Howells }
11321da177e4SLinus Torvalds 
1133f70e2e06SDavid Howells /*
1134973c9f4fSDavid Howells  * Link a key into to a keyring.
1135973c9f4fSDavid Howells  *
1136973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.  Discards any
1137973c9f4fSDavid Howells  * already extant link to matching key if there is one, so that each keyring
1138973c9f4fSDavid Howells  * holds at most one link to any given key of a particular type+description
1139973c9f4fSDavid Howells  * combination.
1140f70e2e06SDavid Howells  */
1141b2a4df20SDavid Howells void __key_link(struct key *key, struct assoc_array_edit **_edit)
1142f70e2e06SDavid Howells {
1143ccc3e6d9SDavid Howells 	__key_get(key);
1144b2a4df20SDavid Howells 	assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key));
1145b2a4df20SDavid Howells 	assoc_array_apply_edit(*_edit);
1146b2a4df20SDavid Howells 	*_edit = NULL;
1147f70e2e06SDavid Howells }
1148f70e2e06SDavid Howells 
1149f70e2e06SDavid Howells /*
1150973c9f4fSDavid Howells  * Finish linking a key into to a keyring.
1151973c9f4fSDavid Howells  *
1152973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.
1153f70e2e06SDavid Howells  */
115416feef43SDavid Howells void __key_link_end(struct key *keyring,
115516feef43SDavid Howells 		    const struct keyring_index_key *index_key,
1156b2a4df20SDavid Howells 		    struct assoc_array_edit *edit)
1157f70e2e06SDavid Howells 	__releases(&keyring->sem)
1158423b9788SDavid Howells 	__releases(&keyring_serialise_link_sem)
1159f70e2e06SDavid Howells {
116016feef43SDavid Howells 	BUG_ON(index_key->type == NULL);
1161b2a4df20SDavid Howells 	kenter("%d,%s,", keyring->serial, index_key->type->name);
1162f70e2e06SDavid Howells 
116316feef43SDavid Howells 	if (index_key->type == &key_type_keyring)
1164f70e2e06SDavid Howells 		up_write(&keyring_serialise_link_sem);
1165f70e2e06SDavid Howells 
1166034faeb9SDavid Howells 	if (edit && !edit->dead_leaf) {
1167f70e2e06SDavid Howells 		key_payload_reserve(keyring,
1168b2a4df20SDavid Howells 				    keyring->datalen - KEYQUOTA_LINK_BYTES);
1169b2a4df20SDavid Howells 		assoc_array_cancel_edit(edit);
1170f70e2e06SDavid Howells 	}
1171f70e2e06SDavid Howells 	up_write(&keyring->sem);
1172f70e2e06SDavid Howells }
1173f70e2e06SDavid Howells 
1174973c9f4fSDavid Howells /**
1175973c9f4fSDavid Howells  * key_link - Link a key to a keyring
1176973c9f4fSDavid Howells  * @keyring: The keyring to make the link in.
1177973c9f4fSDavid Howells  * @key: The key to link to.
1178973c9f4fSDavid Howells  *
1179973c9f4fSDavid Howells  * Make a link in a keyring to a key, such that the keyring holds a reference
1180973c9f4fSDavid Howells  * on that key and the key can potentially be found by searching that keyring.
1181973c9f4fSDavid Howells  *
1182973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore and will consume some
1183973c9f4fSDavid Howells  * of the user's key data quota to hold the link.
1184973c9f4fSDavid Howells  *
1185973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
1186973c9f4fSDavid Howells  * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
1187973c9f4fSDavid Howells  * full, -EDQUOT if there is insufficient key data quota remaining to add
1188973c9f4fSDavid Howells  * another link or -ENOMEM if there's insufficient memory.
1189973c9f4fSDavid Howells  *
1190973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1191973c9f4fSDavid Howells  * be made (the keyring should have Write permission and the key Link
1192973c9f4fSDavid Howells  * permission).
11931da177e4SLinus Torvalds  */
11941da177e4SLinus Torvalds int key_link(struct key *keyring, struct key *key)
11951da177e4SLinus Torvalds {
1196b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
11971da177e4SLinus Torvalds 	int ret;
11981da177e4SLinus Torvalds 
1199b2a4df20SDavid Howells 	kenter("{%d,%d}", keyring->serial, atomic_read(&keyring->usage));
1200b2a4df20SDavid Howells 
12011da177e4SLinus Torvalds 	key_check(keyring);
12021da177e4SLinus Torvalds 	key_check(key);
12031da177e4SLinus Torvalds 
1204008643b8SDavid Howells 	if (test_bit(KEY_FLAG_TRUSTED_ONLY, &keyring->flags) &&
1205008643b8SDavid Howells 	    !test_bit(KEY_FLAG_TRUSTED, &key->flags))
1206008643b8SDavid Howells 		return -EPERM;
1207008643b8SDavid Howells 
1208b2a4df20SDavid Howells 	ret = __key_link_begin(keyring, &key->index_key, &edit);
1209f70e2e06SDavid Howells 	if (ret == 0) {
1210b2a4df20SDavid Howells 		kdebug("begun {%d,%d}", keyring->serial, atomic_read(&keyring->usage));
1211f70e2e06SDavid Howells 		ret = __key_link_check_live_key(keyring, key);
1212f70e2e06SDavid Howells 		if (ret == 0)
1213b2a4df20SDavid Howells 			__key_link(key, &edit);
1214b2a4df20SDavid Howells 		__key_link_end(keyring, &key->index_key, edit);
1215f70e2e06SDavid Howells 	}
12161da177e4SLinus Torvalds 
1217b2a4df20SDavid Howells 	kleave(" = %d {%d,%d}", ret, keyring->serial, atomic_read(&keyring->usage));
12181da177e4SLinus Torvalds 	return ret;
1219f70e2e06SDavid Howells }
12201da177e4SLinus Torvalds EXPORT_SYMBOL(key_link);
12211da177e4SLinus Torvalds 
1222973c9f4fSDavid Howells /**
1223973c9f4fSDavid Howells  * key_unlink - Unlink the first link to a key from a keyring.
1224973c9f4fSDavid Howells  * @keyring: The keyring to remove the link from.
1225973c9f4fSDavid Howells  * @key: The key the link is to.
1226973c9f4fSDavid Howells  *
1227973c9f4fSDavid Howells  * Remove a link from a keyring to a key.
1228973c9f4fSDavid Howells  *
1229973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore.
1230973c9f4fSDavid Howells  *
1231973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
1232973c9f4fSDavid Howells  * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
1233973c9f4fSDavid Howells  * memory.
1234973c9f4fSDavid Howells  *
1235973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1236973c9f4fSDavid Howells  * be removed (the keyring should have Write permission; no permissions are
1237973c9f4fSDavid Howells  * required on the key).
12381da177e4SLinus Torvalds  */
12391da177e4SLinus Torvalds int key_unlink(struct key *keyring, struct key *key)
12401da177e4SLinus Torvalds {
1241b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1242b2a4df20SDavid Howells 	int ret;
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	key_check(keyring);
12451da177e4SLinus Torvalds 	key_check(key);
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
1248b2a4df20SDavid Howells 		return -ENOTDIR;
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	down_write(&keyring->sem);
12511da177e4SLinus Torvalds 
1252b2a4df20SDavid Howells 	edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops,
1253b2a4df20SDavid Howells 				  &key->index_key);
1254b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1255b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1256b2a4df20SDavid Howells 		goto error;
12571da177e4SLinus Torvalds 	}
12581da177e4SLinus Torvalds 	ret = -ENOENT;
1259b2a4df20SDavid Howells 	if (edit == NULL)
12601da177e4SLinus Torvalds 		goto error;
12611da177e4SLinus Torvalds 
1262b2a4df20SDavid Howells 	assoc_array_apply_edit(edit);
1263034faeb9SDavid Howells 	key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES);
12641da177e4SLinus Torvalds 	ret = 0;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds error:
126776d8aeabSDavid Howells 	up_write(&keyring->sem);
1268b2a4df20SDavid Howells 	return ret;
1269a8b17ed0SDavid Howells }
12701da177e4SLinus Torvalds EXPORT_SYMBOL(key_unlink);
12711da177e4SLinus Torvalds 
1272973c9f4fSDavid Howells /**
1273973c9f4fSDavid Howells  * keyring_clear - Clear a keyring
1274973c9f4fSDavid Howells  * @keyring: The keyring to clear.
1275973c9f4fSDavid Howells  *
1276973c9f4fSDavid Howells  * Clear the contents of the specified keyring.
1277973c9f4fSDavid Howells  *
1278973c9f4fSDavid Howells  * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
12791da177e4SLinus Torvalds  */
12801da177e4SLinus Torvalds int keyring_clear(struct key *keyring)
12811da177e4SLinus Torvalds {
1282b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
128376d8aeabSDavid Howells 	int ret;
12841da177e4SLinus Torvalds 
1285b2a4df20SDavid Howells 	if (keyring->type != &key_type_keyring)
1286b2a4df20SDavid Howells 		return -ENOTDIR;
1287b2a4df20SDavid Howells 
12881da177e4SLinus Torvalds 	down_write(&keyring->sem);
12891da177e4SLinus Torvalds 
1290b2a4df20SDavid Howells 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1291b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1292b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1293b2a4df20SDavid Howells 	} else {
1294b2a4df20SDavid Howells 		if (edit)
1295b2a4df20SDavid Howells 			assoc_array_apply_edit(edit);
1296b2a4df20SDavid Howells 		key_payload_reserve(keyring, 0);
12971da177e4SLinus Torvalds 		ret = 0;
12981da177e4SLinus Torvalds 	}
12991da177e4SLinus Torvalds 
1300b2a4df20SDavid Howells 	up_write(&keyring->sem);
13011da177e4SLinus Torvalds 	return ret;
1302a8b17ed0SDavid Howells }
13031da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_clear);
130431204ed9SDavid Howells 
130531204ed9SDavid Howells /*
1306973c9f4fSDavid Howells  * Dispose of the links from a revoked keyring.
1307973c9f4fSDavid Howells  *
1308973c9f4fSDavid Howells  * This is called with the key sem write-locked.
130931204ed9SDavid Howells  */
131031204ed9SDavid Howells static void keyring_revoke(struct key *keyring)
131131204ed9SDavid Howells {
1312b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1313f0641cbaSDavid Howells 
1314b2a4df20SDavid Howells 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1315b2a4df20SDavid Howells 	if (!IS_ERR(edit)) {
1316b2a4df20SDavid Howells 		if (edit)
1317b2a4df20SDavid Howells 			assoc_array_apply_edit(edit);
131831204ed9SDavid Howells 		key_payload_reserve(keyring, 0);
131931204ed9SDavid Howells 	}
1320a8b17ed0SDavid Howells }
13215d135440SDavid Howells 
132262fe3182SDavid Howells static bool keyring_gc_select_iterator(void *object, void *iterator_data)
1323b2a4df20SDavid Howells {
1324b2a4df20SDavid Howells 	struct key *key = keyring_ptr_to_key(object);
1325b2a4df20SDavid Howells 	time_t *limit = iterator_data;
1326b2a4df20SDavid Howells 
1327b2a4df20SDavid Howells 	if (key_is_dead(key, *limit))
1328b2a4df20SDavid Howells 		return false;
1329b2a4df20SDavid Howells 	key_get(key);
1330b2a4df20SDavid Howells 	return true;
1331b2a4df20SDavid Howells }
1332b2a4df20SDavid Howells 
133362fe3182SDavid Howells static int keyring_gc_check_iterator(const void *object, void *iterator_data)
133462fe3182SDavid Howells {
133562fe3182SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
133662fe3182SDavid Howells 	time_t *limit = iterator_data;
133762fe3182SDavid Howells 
133862fe3182SDavid Howells 	key_check(key);
133962fe3182SDavid Howells 	return key_is_dead(key, *limit);
134062fe3182SDavid Howells }
134162fe3182SDavid Howells 
13425d135440SDavid Howells /*
134362fe3182SDavid Howells  * Garbage collect pointers from a keyring.
1344973c9f4fSDavid Howells  *
134562fe3182SDavid Howells  * Not called with any locks held.  The keyring's key struct will not be
134662fe3182SDavid Howells  * deallocated under us as only our caller may deallocate it.
13475d135440SDavid Howells  */
13485d135440SDavid Howells void keyring_gc(struct key *keyring, time_t limit)
13495d135440SDavid Howells {
135062fe3182SDavid Howells 	int result;
13515d135440SDavid Howells 
135262fe3182SDavid Howells 	kenter("%x{%s}", keyring->serial, keyring->description ?: "");
135362fe3182SDavid Howells 
135462fe3182SDavid Howells 	if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
135562fe3182SDavid Howells 			      (1 << KEY_FLAG_REVOKED)))
135662fe3182SDavid Howells 		goto dont_gc;
135762fe3182SDavid Howells 
135862fe3182SDavid Howells 	/* scan the keyring looking for dead keys */
135962fe3182SDavid Howells 	rcu_read_lock();
136062fe3182SDavid Howells 	result = assoc_array_iterate(&keyring->keys,
136162fe3182SDavid Howells 				     keyring_gc_check_iterator, &limit);
136262fe3182SDavid Howells 	rcu_read_unlock();
136362fe3182SDavid Howells 	if (result == true)
136462fe3182SDavid Howells 		goto do_gc;
136562fe3182SDavid Howells 
136662fe3182SDavid Howells dont_gc:
136762fe3182SDavid Howells 	kleave(" [no gc]");
136862fe3182SDavid Howells 	return;
136962fe3182SDavid Howells 
137062fe3182SDavid Howells do_gc:
13715d135440SDavid Howells 	down_write(&keyring->sem);
1372b2a4df20SDavid Howells 	assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops,
137362fe3182SDavid Howells 		       keyring_gc_select_iterator, &limit);
13745d135440SDavid Howells 	up_write(&keyring->sem);
137562fe3182SDavid Howells 	kleave(" [gc]");
13765d135440SDavid Howells }
1377