xref: /openbmc/linux/security/keys/keyring.c (revision b2a4df200d570b2c33a57e1ebfa5896e4bc81b69)
169664cf1SDavid Howells /* Keyring handling
21da177e4SLinus Torvalds  *
3*b2a4df20SDavid 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>
20*b2a4df20SDavid Howells #include <keys/user-type.h>
21*b2a4df20SDavid 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 
36*b2a4df20SDavid Howells /*
37*b2a4df20SDavid Howells  * We mark pointers we pass to the associative array with bit 1 set if
38*b2a4df20SDavid Howells  * they're keyrings and clear otherwise.
39*b2a4df20SDavid Howells  */
40*b2a4df20SDavid Howells #define KEYRING_PTR_SUBTYPE	0x2UL
41*b2a4df20SDavid Howells 
42*b2a4df20SDavid Howells static inline bool keyring_ptr_is_keyring(const struct assoc_array_ptr *x)
43*b2a4df20SDavid Howells {
44*b2a4df20SDavid Howells 	return (unsigned long)x & KEYRING_PTR_SUBTYPE;
45*b2a4df20SDavid Howells }
46*b2a4df20SDavid Howells static inline struct key *keyring_ptr_to_key(const struct assoc_array_ptr *x)
47*b2a4df20SDavid Howells {
48*b2a4df20SDavid Howells 	void *object = assoc_array_ptr_to_leaf(x);
49*b2a4df20SDavid Howells 	return (struct key *)((unsigned long)object & ~KEYRING_PTR_SUBTYPE);
50*b2a4df20SDavid Howells }
51*b2a4df20SDavid Howells static inline void *keyring_key_to_ptr(struct key *key)
52*b2a4df20SDavid Howells {
53*b2a4df20SDavid Howells 	if (key->type == &key_type_keyring)
54*b2a4df20SDavid Howells 		return (void *)((unsigned long)key | KEYRING_PTR_SUBTYPE);
55*b2a4df20SDavid Howells 	return key;
56*b2a4df20SDavid Howells }
57*b2a4df20SDavid 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  */
761da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
77cf7f601cSDavid Howells 			       struct key_preparsed_payload *prep);
7831204ed9SDavid Howells static void keyring_revoke(struct key *keyring);
791da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring);
801da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m);
811da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
821da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds struct key_type key_type_keyring = {
851da177e4SLinus Torvalds 	.name		= "keyring",
86*b2a4df20SDavid Howells 	.def_datalen	= 0,
871da177e4SLinus Torvalds 	.instantiate	= keyring_instantiate,
88*b2a4df20SDavid Howells 	.match		= user_match,
8931204ed9SDavid Howells 	.revoke		= keyring_revoke,
901da177e4SLinus Torvalds 	.destroy	= keyring_destroy,
911da177e4SLinus Torvalds 	.describe	= keyring_describe,
921da177e4SLinus Torvalds 	.read		= keyring_read,
931da177e4SLinus Torvalds };
947318226eSDavid Howells EXPORT_SYMBOL(key_type_keyring);
957318226eSDavid Howells 
961da177e4SLinus Torvalds /*
97973c9f4fSDavid Howells  * Semaphore to serialise link/link calls to prevent two link calls in parallel
98973c9f4fSDavid Howells  * introducing a cycle.
991da177e4SLinus Torvalds  */
1001ae8f407SAdrian Bunk static DECLARE_RWSEM(keyring_serialise_link_sem);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds /*
103973c9f4fSDavid Howells  * Publish the name of a keyring so that it can be found by name (if it has
104973c9f4fSDavid Howells  * one).
1051da177e4SLinus Torvalds  */
10669664cf1SDavid Howells static void keyring_publish_name(struct key *keyring)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	int bucket;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	if (keyring->description) {
1111da177e4SLinus Torvalds 		bucket = keyring_hash(keyring->description);
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 		if (!keyring_name_hash[bucket].next)
1161da177e4SLinus Torvalds 			INIT_LIST_HEAD(&keyring_name_hash[bucket]);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 		list_add_tail(&keyring->type_data.link,
1191da177e4SLinus Torvalds 			      &keyring_name_hash[bucket]);
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
1221da177e4SLinus Torvalds 	}
123a8b17ed0SDavid Howells }
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /*
126973c9f4fSDavid Howells  * Initialise a keyring.
127973c9f4fSDavid Howells  *
128973c9f4fSDavid Howells  * Returns 0 on success, -EINVAL if given any data.
1291da177e4SLinus Torvalds  */
1301da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
131cf7f601cSDavid Howells 			       struct key_preparsed_payload *prep)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	int ret;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	ret = -EINVAL;
136cf7f601cSDavid Howells 	if (prep->datalen == 0) {
137*b2a4df20SDavid Howells 		assoc_array_init(&keyring->keys);
1381da177e4SLinus Torvalds 		/* make the keyring available by name if it has one */
1391da177e4SLinus Torvalds 		keyring_publish_name(keyring);
1401da177e4SLinus Torvalds 		ret = 0;
1411da177e4SLinus Torvalds 	}
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	return ret;
144a8b17ed0SDavid Howells }
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds /*
147*b2a4df20SDavid Howells  * Multiply 64-bits by 32-bits to 96-bits and fold back to 64-bit.  Ideally we'd
148*b2a4df20SDavid Howells  * fold the carry back too, but that requires inline asm.
1491da177e4SLinus Torvalds  */
150*b2a4df20SDavid Howells static u64 mult_64x32_and_fold(u64 x, u32 y)
1511da177e4SLinus Torvalds {
152*b2a4df20SDavid Howells 	u64 hi = (u64)(u32)(x >> 32) * y;
153*b2a4df20SDavid Howells 	u64 lo = (u64)(u32)(x) * y;
154*b2a4df20SDavid Howells 	return lo + ((u64)(u32)hi << 32) + (u32)(hi >> 32);
155a8b17ed0SDavid Howells }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /*
158*b2a4df20SDavid Howells  * Hash a key type and description.
159*b2a4df20SDavid Howells  */
160*b2a4df20SDavid Howells static unsigned long hash_key_type_and_desc(const struct keyring_index_key *index_key)
161*b2a4df20SDavid Howells {
162*b2a4df20SDavid Howells 	const unsigned level_shift = ASSOC_ARRAY_LEVEL_STEP;
163*b2a4df20SDavid Howells 	const unsigned long level_mask = ASSOC_ARRAY_LEVEL_STEP_MASK;
164*b2a4df20SDavid Howells 	const char *description = index_key->description;
165*b2a4df20SDavid Howells 	unsigned long hash, type;
166*b2a4df20SDavid Howells 	u32 piece;
167*b2a4df20SDavid Howells 	u64 acc;
168*b2a4df20SDavid Howells 	int n, desc_len = index_key->desc_len;
169*b2a4df20SDavid Howells 
170*b2a4df20SDavid Howells 	type = (unsigned long)index_key->type;
171*b2a4df20SDavid Howells 
172*b2a4df20SDavid Howells 	acc = mult_64x32_and_fold(type, desc_len + 13);
173*b2a4df20SDavid Howells 	acc = mult_64x32_and_fold(acc, 9207);
174*b2a4df20SDavid Howells 	for (;;) {
175*b2a4df20SDavid Howells 		n = desc_len;
176*b2a4df20SDavid Howells 		if (n <= 0)
177*b2a4df20SDavid Howells 			break;
178*b2a4df20SDavid Howells 		if (n > 4)
179*b2a4df20SDavid Howells 			n = 4;
180*b2a4df20SDavid Howells 		piece = 0;
181*b2a4df20SDavid Howells 		memcpy(&piece, description, n);
182*b2a4df20SDavid Howells 		description += n;
183*b2a4df20SDavid Howells 		desc_len -= n;
184*b2a4df20SDavid Howells 		acc = mult_64x32_and_fold(acc, piece);
185*b2a4df20SDavid Howells 		acc = mult_64x32_and_fold(acc, 9207);
186*b2a4df20SDavid Howells 	}
187*b2a4df20SDavid Howells 
188*b2a4df20SDavid Howells 	/* Fold the hash down to 32 bits if need be. */
189*b2a4df20SDavid Howells 	hash = acc;
190*b2a4df20SDavid Howells 	if (ASSOC_ARRAY_KEY_CHUNK_SIZE == 32)
191*b2a4df20SDavid Howells 		hash ^= acc >> 32;
192*b2a4df20SDavid Howells 
193*b2a4df20SDavid Howells 	/* Squidge all the keyrings into a separate part of the tree to
194*b2a4df20SDavid Howells 	 * ordinary keys by making sure the lowest level segment in the hash is
195*b2a4df20SDavid Howells 	 * zero for keyrings and non-zero otherwise.
196*b2a4df20SDavid Howells 	 */
197*b2a4df20SDavid Howells 	if (index_key->type != &key_type_keyring && (hash & level_mask) == 0)
198*b2a4df20SDavid Howells 		return hash | (hash >> (ASSOC_ARRAY_KEY_CHUNK_SIZE - level_shift)) | 1;
199*b2a4df20SDavid Howells 	if (index_key->type == &key_type_keyring && (hash & level_mask) != 0)
200*b2a4df20SDavid Howells 		return (hash + (hash << level_shift)) & ~level_mask;
201*b2a4df20SDavid Howells 	return hash;
202*b2a4df20SDavid Howells }
203*b2a4df20SDavid Howells 
204*b2a4df20SDavid Howells /*
205*b2a4df20SDavid Howells  * Build the next index key chunk.
206*b2a4df20SDavid Howells  *
207*b2a4df20SDavid Howells  * On 32-bit systems the index key is laid out as:
208*b2a4df20SDavid Howells  *
209*b2a4df20SDavid Howells  *	0	4	5	9...
210*b2a4df20SDavid Howells  *	hash	desclen	typeptr	desc[]
211*b2a4df20SDavid Howells  *
212*b2a4df20SDavid Howells  * On 64-bit systems:
213*b2a4df20SDavid Howells  *
214*b2a4df20SDavid Howells  *	0	8	9	17...
215*b2a4df20SDavid Howells  *	hash	desclen	typeptr	desc[]
216*b2a4df20SDavid Howells  *
217*b2a4df20SDavid Howells  * We return it one word-sized chunk at a time.
218*b2a4df20SDavid Howells  */
219*b2a4df20SDavid Howells static unsigned long keyring_get_key_chunk(const void *data, int level)
220*b2a4df20SDavid Howells {
221*b2a4df20SDavid Howells 	const struct keyring_index_key *index_key = data;
222*b2a4df20SDavid Howells 	unsigned long chunk = 0;
223*b2a4df20SDavid Howells 	long offset = 0;
224*b2a4df20SDavid Howells 	int desc_len = index_key->desc_len, n = sizeof(chunk);
225*b2a4df20SDavid Howells 
226*b2a4df20SDavid Howells 	level /= ASSOC_ARRAY_KEY_CHUNK_SIZE;
227*b2a4df20SDavid Howells 	switch (level) {
228*b2a4df20SDavid Howells 	case 0:
229*b2a4df20SDavid Howells 		return hash_key_type_and_desc(index_key);
230*b2a4df20SDavid Howells 	case 1:
231*b2a4df20SDavid Howells 		return ((unsigned long)index_key->type << 8) | desc_len;
232*b2a4df20SDavid Howells 	case 2:
233*b2a4df20SDavid Howells 		if (desc_len == 0)
234*b2a4df20SDavid Howells 			return (u8)((unsigned long)index_key->type >>
235*b2a4df20SDavid Howells 				    (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8));
236*b2a4df20SDavid Howells 		n--;
237*b2a4df20SDavid Howells 		offset = 1;
238*b2a4df20SDavid Howells 	default:
239*b2a4df20SDavid Howells 		offset += sizeof(chunk) - 1;
240*b2a4df20SDavid Howells 		offset += (level - 3) * sizeof(chunk);
241*b2a4df20SDavid Howells 		if (offset >= desc_len)
242*b2a4df20SDavid Howells 			return 0;
243*b2a4df20SDavid Howells 		desc_len -= offset;
244*b2a4df20SDavid Howells 		if (desc_len > n)
245*b2a4df20SDavid Howells 			desc_len = n;
246*b2a4df20SDavid Howells 		offset += desc_len;
247*b2a4df20SDavid Howells 		do {
248*b2a4df20SDavid Howells 			chunk <<= 8;
249*b2a4df20SDavid Howells 			chunk |= ((u8*)index_key->description)[--offset];
250*b2a4df20SDavid Howells 		} while (--desc_len > 0);
251*b2a4df20SDavid Howells 
252*b2a4df20SDavid Howells 		if (level == 2) {
253*b2a4df20SDavid Howells 			chunk <<= 8;
254*b2a4df20SDavid Howells 			chunk |= (u8)((unsigned long)index_key->type >>
255*b2a4df20SDavid Howells 				      (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8));
256*b2a4df20SDavid Howells 		}
257*b2a4df20SDavid Howells 		return chunk;
258*b2a4df20SDavid Howells 	}
259*b2a4df20SDavid Howells }
260*b2a4df20SDavid Howells 
261*b2a4df20SDavid Howells static unsigned long keyring_get_object_key_chunk(const void *object, int level)
262*b2a4df20SDavid Howells {
263*b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
264*b2a4df20SDavid Howells 	return keyring_get_key_chunk(&key->index_key, level);
265*b2a4df20SDavid Howells }
266*b2a4df20SDavid Howells 
267*b2a4df20SDavid Howells static bool keyring_compare_object(const void *object, const void *data)
268*b2a4df20SDavid Howells {
269*b2a4df20SDavid Howells 	const struct keyring_index_key *index_key = data;
270*b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
271*b2a4df20SDavid Howells 
272*b2a4df20SDavid Howells 	return key->index_key.type == index_key->type &&
273*b2a4df20SDavid Howells 		key->index_key.desc_len == index_key->desc_len &&
274*b2a4df20SDavid Howells 		memcmp(key->index_key.description, index_key->description,
275*b2a4df20SDavid Howells 		       index_key->desc_len) == 0;
276*b2a4df20SDavid Howells }
277*b2a4df20SDavid Howells 
278*b2a4df20SDavid Howells /*
279*b2a4df20SDavid Howells  * Compare the index keys of a pair of objects and determine the bit position
280*b2a4df20SDavid Howells  * at which they differ - if they differ.
281*b2a4df20SDavid Howells  */
282*b2a4df20SDavid Howells static int keyring_diff_objects(const void *_a, const void *_b)
283*b2a4df20SDavid Howells {
284*b2a4df20SDavid Howells 	const struct key *key_a = keyring_ptr_to_key(_a);
285*b2a4df20SDavid Howells 	const struct key *key_b = keyring_ptr_to_key(_b);
286*b2a4df20SDavid Howells 	const struct keyring_index_key *a = &key_a->index_key;
287*b2a4df20SDavid Howells 	const struct keyring_index_key *b = &key_b->index_key;
288*b2a4df20SDavid Howells 	unsigned long seg_a, seg_b;
289*b2a4df20SDavid Howells 	int level, i;
290*b2a4df20SDavid Howells 
291*b2a4df20SDavid Howells 	level = 0;
292*b2a4df20SDavid Howells 	seg_a = hash_key_type_and_desc(a);
293*b2a4df20SDavid Howells 	seg_b = hash_key_type_and_desc(b);
294*b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
295*b2a4df20SDavid Howells 		goto differ;
296*b2a4df20SDavid Howells 
297*b2a4df20SDavid Howells 	/* The number of bits contributed by the hash is controlled by a
298*b2a4df20SDavid Howells 	 * constant in the assoc_array headers.  Everything else thereafter we
299*b2a4df20SDavid Howells 	 * can deal with as being machine word-size dependent.
300*b2a4df20SDavid Howells 	 */
301*b2a4df20SDavid Howells 	level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8;
302*b2a4df20SDavid Howells 	seg_a = a->desc_len;
303*b2a4df20SDavid Howells 	seg_b = b->desc_len;
304*b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
305*b2a4df20SDavid Howells 		goto differ;
306*b2a4df20SDavid Howells 
307*b2a4df20SDavid Howells 	/* The next bit may not work on big endian */
308*b2a4df20SDavid Howells 	level++;
309*b2a4df20SDavid Howells 	seg_a = (unsigned long)a->type;
310*b2a4df20SDavid Howells 	seg_b = (unsigned long)b->type;
311*b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
312*b2a4df20SDavid Howells 		goto differ;
313*b2a4df20SDavid Howells 
314*b2a4df20SDavid Howells 	level += sizeof(unsigned long);
315*b2a4df20SDavid Howells 	if (a->desc_len == 0)
316*b2a4df20SDavid Howells 		goto same;
317*b2a4df20SDavid Howells 
318*b2a4df20SDavid Howells 	i = 0;
319*b2a4df20SDavid Howells 	if (((unsigned long)a->description | (unsigned long)b->description) &
320*b2a4df20SDavid Howells 	    (sizeof(unsigned long) - 1)) {
321*b2a4df20SDavid Howells 		do {
322*b2a4df20SDavid Howells 			seg_a = *(unsigned long *)(a->description + i);
323*b2a4df20SDavid Howells 			seg_b = *(unsigned long *)(b->description + i);
324*b2a4df20SDavid Howells 			if ((seg_a ^ seg_b) != 0)
325*b2a4df20SDavid Howells 				goto differ_plus_i;
326*b2a4df20SDavid Howells 			i += sizeof(unsigned long);
327*b2a4df20SDavid Howells 		} while (i < (a->desc_len & (sizeof(unsigned long) - 1)));
328*b2a4df20SDavid Howells 	}
329*b2a4df20SDavid Howells 
330*b2a4df20SDavid Howells 	for (; i < a->desc_len; i++) {
331*b2a4df20SDavid Howells 		seg_a = *(unsigned char *)(a->description + i);
332*b2a4df20SDavid Howells 		seg_b = *(unsigned char *)(b->description + i);
333*b2a4df20SDavid Howells 		if ((seg_a ^ seg_b) != 0)
334*b2a4df20SDavid Howells 			goto differ_plus_i;
335*b2a4df20SDavid Howells 	}
336*b2a4df20SDavid Howells 
337*b2a4df20SDavid Howells same:
338*b2a4df20SDavid Howells 	return -1;
339*b2a4df20SDavid Howells 
340*b2a4df20SDavid Howells differ_plus_i:
341*b2a4df20SDavid Howells 	level += i;
342*b2a4df20SDavid Howells differ:
343*b2a4df20SDavid Howells 	i = level * 8 + __ffs(seg_a ^ seg_b);
344*b2a4df20SDavid Howells 	return i;
345*b2a4df20SDavid Howells }
346*b2a4df20SDavid Howells 
347*b2a4df20SDavid Howells /*
348*b2a4df20SDavid Howells  * Free an object after stripping the keyring flag off of the pointer.
349*b2a4df20SDavid Howells  */
350*b2a4df20SDavid Howells static void keyring_free_object(void *object)
351*b2a4df20SDavid Howells {
352*b2a4df20SDavid Howells 	key_put(keyring_ptr_to_key(object));
353*b2a4df20SDavid Howells }
354*b2a4df20SDavid Howells 
355*b2a4df20SDavid Howells /*
356*b2a4df20SDavid Howells  * Operations for keyring management by the index-tree routines.
357*b2a4df20SDavid Howells  */
358*b2a4df20SDavid Howells static const struct assoc_array_ops keyring_assoc_array_ops = {
359*b2a4df20SDavid Howells 	.get_key_chunk		= keyring_get_key_chunk,
360*b2a4df20SDavid Howells 	.get_object_key_chunk	= keyring_get_object_key_chunk,
361*b2a4df20SDavid Howells 	.compare_object		= keyring_compare_object,
362*b2a4df20SDavid Howells 	.diff_objects		= keyring_diff_objects,
363*b2a4df20SDavid Howells 	.free_object		= keyring_free_object,
364*b2a4df20SDavid Howells };
365*b2a4df20SDavid Howells 
366*b2a4df20SDavid Howells /*
367973c9f4fSDavid Howells  * Clean up a keyring when it is destroyed.  Unpublish its name if it had one
368973c9f4fSDavid Howells  * and dispose of its data.
369233e4735SDavid Howells  *
370233e4735SDavid Howells  * The garbage collector detects the final key_put(), removes the keyring from
371233e4735SDavid Howells  * the serial number tree and then does RCU synchronisation before coming here,
372233e4735SDavid Howells  * so we shouldn't need to worry about code poking around here with the RCU
373233e4735SDavid Howells  * readlock held by this time.
3741da177e4SLinus Torvalds  */
3751da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	if (keyring->description) {
3781da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
37994efe72fSDavid Howells 
38094efe72fSDavid Howells 		if (keyring->type_data.link.next != NULL &&
38194efe72fSDavid Howells 		    !list_empty(&keyring->type_data.link))
3821da177e4SLinus Torvalds 			list_del(&keyring->type_data.link);
38394efe72fSDavid Howells 
3841da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 
387*b2a4df20SDavid Howells 	assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops);
388a8b17ed0SDavid Howells }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /*
391973c9f4fSDavid Howells  * Describe a keyring for /proc.
3921da177e4SLinus Torvalds  */
3931da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m)
3941da177e4SLinus Torvalds {
395c8563473Swzt.wzt@gmail.com 	if (keyring->description)
3961da177e4SLinus Torvalds 		seq_puts(m, keyring->description);
397c8563473Swzt.wzt@gmail.com 	else
3981da177e4SLinus Torvalds 		seq_puts(m, "[anon]");
3991da177e4SLinus Torvalds 
40078b7280cSDavid Howells 	if (key_is_instantiated(keyring)) {
401*b2a4df20SDavid Howells 		if (keyring->keys.nr_leaves_on_tree != 0)
402*b2a4df20SDavid Howells 			seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree);
4031da177e4SLinus Torvalds 		else
4041da177e4SLinus Torvalds 			seq_puts(m, ": empty");
405a8b17ed0SDavid Howells 	}
40678b7280cSDavid Howells }
4071da177e4SLinus Torvalds 
408*b2a4df20SDavid Howells struct keyring_read_iterator_context {
409*b2a4df20SDavid Howells 	size_t			qty;
410*b2a4df20SDavid Howells 	size_t			count;
411*b2a4df20SDavid Howells 	key_serial_t __user	*buffer;
412*b2a4df20SDavid Howells };
413*b2a4df20SDavid Howells 
414*b2a4df20SDavid Howells static int keyring_read_iterator(const void *object, void *data)
415*b2a4df20SDavid Howells {
416*b2a4df20SDavid Howells 	struct keyring_read_iterator_context *ctx = data;
417*b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
418*b2a4df20SDavid Howells 	int ret;
419*b2a4df20SDavid Howells 
420*b2a4df20SDavid Howells 	kenter("{%s,%d},,{%zu/%zu}",
421*b2a4df20SDavid Howells 	       key->type->name, key->serial, ctx->count, ctx->qty);
422*b2a4df20SDavid Howells 
423*b2a4df20SDavid Howells 	if (ctx->count >= ctx->qty)
424*b2a4df20SDavid Howells 		return 1;
425*b2a4df20SDavid Howells 
426*b2a4df20SDavid Howells 	ret = put_user(key->serial, ctx->buffer);
427*b2a4df20SDavid Howells 	if (ret < 0)
428*b2a4df20SDavid Howells 		return ret;
429*b2a4df20SDavid Howells 	ctx->buffer++;
430*b2a4df20SDavid Howells 	ctx->count += sizeof(key->serial);
431*b2a4df20SDavid Howells 	return 0;
432*b2a4df20SDavid Howells }
433*b2a4df20SDavid Howells 
4341da177e4SLinus Torvalds /*
435973c9f4fSDavid Howells  * Read a list of key IDs from the keyring's contents in binary form
436973c9f4fSDavid Howells  *
437*b2a4df20SDavid Howells  * The keyring's semaphore is read-locked by the caller.  This prevents someone
438*b2a4df20SDavid Howells  * from modifying it under us - which could cause us to read key IDs multiple
439*b2a4df20SDavid Howells  * times.
4401da177e4SLinus Torvalds  */
4411da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
4421da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen)
4431da177e4SLinus Torvalds {
444*b2a4df20SDavid Howells 	struct keyring_read_iterator_context ctx;
445*b2a4df20SDavid Howells 	unsigned long nr_keys;
446*b2a4df20SDavid Howells 	int ret;
4471da177e4SLinus Torvalds 
448*b2a4df20SDavid Howells 	kenter("{%d},,%zu", key_serial(keyring), buflen);
4491da177e4SLinus Torvalds 
450*b2a4df20SDavid Howells 	if (buflen & (sizeof(key_serial_t) - 1))
451*b2a4df20SDavid Howells 		return -EINVAL;
4521da177e4SLinus Torvalds 
453*b2a4df20SDavid Howells 	nr_keys = keyring->keys.nr_leaves_on_tree;
454*b2a4df20SDavid Howells 	if (nr_keys == 0)
455*b2a4df20SDavid Howells 		return 0;
4561da177e4SLinus Torvalds 
457*b2a4df20SDavid Howells 	/* Calculate how much data we could return */
458*b2a4df20SDavid Howells 	ctx.qty = nr_keys * sizeof(key_serial_t);
4591da177e4SLinus Torvalds 
460*b2a4df20SDavid Howells 	if (!buffer || !buflen)
461*b2a4df20SDavid Howells 		return ctx.qty;
4621da177e4SLinus Torvalds 
463*b2a4df20SDavid Howells 	if (buflen > ctx.qty)
464*b2a4df20SDavid Howells 		ctx.qty = buflen;
4651da177e4SLinus Torvalds 
466*b2a4df20SDavid Howells 	/* Copy the IDs of the subscribed keys into the buffer */
467*b2a4df20SDavid Howells 	ctx.buffer = (key_serial_t __user *)buffer;
468*b2a4df20SDavid Howells 	ctx.count = 0;
469*b2a4df20SDavid Howells 	ret = assoc_array_iterate(&keyring->keys, keyring_read_iterator, &ctx);
470*b2a4df20SDavid Howells 	if (ret < 0) {
471*b2a4df20SDavid Howells 		kleave(" = %d [iterate]", ret);
4721da177e4SLinus Torvalds 		return ret;
473a8b17ed0SDavid Howells 	}
4741da177e4SLinus Torvalds 
475*b2a4df20SDavid Howells 	kleave(" = %zu [ok]", ctx.count);
476*b2a4df20SDavid Howells 	return ctx.count;
477*b2a4df20SDavid Howells }
478*b2a4df20SDavid Howells 
4791da177e4SLinus Torvalds /*
480973c9f4fSDavid Howells  * Allocate a keyring and link into the destination keyring.
4811da177e4SLinus Torvalds  */
4829a56c2dbSEric W. Biederman struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
48396b5c8feSDavid Howells 			  const struct cred *cred, key_perm_t perm,
48496b5c8feSDavid Howells 			  unsigned long flags, struct key *dest)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	struct key *keyring;
4871da177e4SLinus Torvalds 	int ret;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	keyring = key_alloc(&key_type_keyring, description,
49096b5c8feSDavid Howells 			    uid, gid, cred, perm, flags);
4911da177e4SLinus Torvalds 	if (!IS_ERR(keyring)) {
4923e30148cSDavid Howells 		ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
4931da177e4SLinus Torvalds 		if (ret < 0) {
4941da177e4SLinus Torvalds 			key_put(keyring);
4951da177e4SLinus Torvalds 			keyring = ERR_PTR(ret);
4961da177e4SLinus Torvalds 		}
4971da177e4SLinus Torvalds 	}
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	return keyring;
500a8b17ed0SDavid Howells }
501f8aa23a5SDavid Howells EXPORT_SYMBOL(keyring_alloc);
5021da177e4SLinus Torvalds 
503*b2a4df20SDavid Howells /*
504*b2a4df20SDavid Howells  * Iteration function to consider each key found.
505*b2a4df20SDavid Howells  */
506*b2a4df20SDavid Howells static int keyring_search_iterator(const void *object, void *iterator_data)
507*b2a4df20SDavid Howells {
508*b2a4df20SDavid Howells 	struct keyring_search_context *ctx = iterator_data;
509*b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
510*b2a4df20SDavid Howells 	unsigned long kflags = key->flags;
511*b2a4df20SDavid Howells 
512*b2a4df20SDavid Howells 	kenter("{%d}", key->serial);
513*b2a4df20SDavid Howells 
514*b2a4df20SDavid Howells 	/* ignore keys not of this type */
515*b2a4df20SDavid Howells 	if (key->type != ctx->index_key.type) {
516*b2a4df20SDavid Howells 		kleave(" = 0 [!type]");
517*b2a4df20SDavid Howells 		return 0;
518*b2a4df20SDavid Howells 	}
519*b2a4df20SDavid Howells 
520*b2a4df20SDavid Howells 	/* skip invalidated, revoked and expired keys */
521*b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
522*b2a4df20SDavid Howells 		if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
523*b2a4df20SDavid Howells 			      (1 << KEY_FLAG_REVOKED))) {
524*b2a4df20SDavid Howells 			ctx->result = ERR_PTR(-EKEYREVOKED);
525*b2a4df20SDavid Howells 			kleave(" = %d [invrev]", ctx->skipped_ret);
526*b2a4df20SDavid Howells 			goto skipped;
527*b2a4df20SDavid Howells 		}
528*b2a4df20SDavid Howells 
529*b2a4df20SDavid Howells 		if (key->expiry && ctx->now.tv_sec >= key->expiry) {
530*b2a4df20SDavid Howells 			ctx->result = ERR_PTR(-EKEYEXPIRED);
531*b2a4df20SDavid Howells 			kleave(" = %d [expire]", ctx->skipped_ret);
532*b2a4df20SDavid Howells 			goto skipped;
533*b2a4df20SDavid Howells 		}
534*b2a4df20SDavid Howells 	}
535*b2a4df20SDavid Howells 
536*b2a4df20SDavid Howells 	/* keys that don't match */
537*b2a4df20SDavid Howells 	if (!ctx->match(key, ctx->match_data)) {
538*b2a4df20SDavid Howells 		kleave(" = 0 [!match]");
539*b2a4df20SDavid Howells 		return 0;
540*b2a4df20SDavid Howells 	}
541*b2a4df20SDavid Howells 
542*b2a4df20SDavid Howells 	/* key must have search permissions */
543*b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
544*b2a4df20SDavid Howells 	    key_task_permission(make_key_ref(key, ctx->possessed),
545*b2a4df20SDavid Howells 				ctx->cred, KEY_SEARCH) < 0) {
546*b2a4df20SDavid Howells 		ctx->result = ERR_PTR(-EACCES);
547*b2a4df20SDavid Howells 		kleave(" = %d [!perm]", ctx->skipped_ret);
548*b2a4df20SDavid Howells 		goto skipped;
549*b2a4df20SDavid Howells 	}
550*b2a4df20SDavid Howells 
551*b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
552*b2a4df20SDavid Howells 		/* we set a different error code if we pass a negative key */
553*b2a4df20SDavid Howells 		if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
554*b2a4df20SDavid Howells 			ctx->result = ERR_PTR(key->type_data.reject_error);
555*b2a4df20SDavid Howells 			kleave(" = %d [neg]", ctx->skipped_ret);
556*b2a4df20SDavid Howells 			goto skipped;
557*b2a4df20SDavid Howells 		}
558*b2a4df20SDavid Howells 	}
559*b2a4df20SDavid Howells 
560*b2a4df20SDavid Howells 	/* Found */
561*b2a4df20SDavid Howells 	ctx->result = make_key_ref(key, ctx->possessed);
562*b2a4df20SDavid Howells 	kleave(" = 1 [found]");
563*b2a4df20SDavid Howells 	return 1;
564*b2a4df20SDavid Howells 
565*b2a4df20SDavid Howells skipped:
566*b2a4df20SDavid Howells 	return ctx->skipped_ret;
567*b2a4df20SDavid Howells }
568*b2a4df20SDavid Howells 
569*b2a4df20SDavid Howells /*
570*b2a4df20SDavid Howells  * Search inside a keyring for a key.  We can search by walking to it
571*b2a4df20SDavid Howells  * directly based on its index-key or we can iterate over the entire
572*b2a4df20SDavid Howells  * tree looking for it, based on the match function.
573*b2a4df20SDavid Howells  */
574*b2a4df20SDavid Howells static int search_keyring(struct key *keyring, struct keyring_search_context *ctx)
575*b2a4df20SDavid Howells {
576*b2a4df20SDavid Howells 	if ((ctx->flags & KEYRING_SEARCH_LOOKUP_TYPE) ==
577*b2a4df20SDavid Howells 	    KEYRING_SEARCH_LOOKUP_DIRECT) {
578*b2a4df20SDavid Howells 		const void *object;
579*b2a4df20SDavid Howells 
580*b2a4df20SDavid Howells 		object = assoc_array_find(&keyring->keys,
581*b2a4df20SDavid Howells 					  &keyring_assoc_array_ops,
582*b2a4df20SDavid Howells 					  &ctx->index_key);
583*b2a4df20SDavid Howells 		return object ? ctx->iterator(object, ctx) : 0;
584*b2a4df20SDavid Howells 	}
585*b2a4df20SDavid Howells 	return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx);
586*b2a4df20SDavid Howells }
587*b2a4df20SDavid Howells 
588*b2a4df20SDavid Howells /*
589*b2a4df20SDavid Howells  * Search a tree of keyrings that point to other keyrings up to the maximum
590*b2a4df20SDavid Howells  * depth.
591*b2a4df20SDavid Howells  */
592*b2a4df20SDavid Howells static bool search_nested_keyrings(struct key *keyring,
593*b2a4df20SDavid Howells 				   struct keyring_search_context *ctx)
594*b2a4df20SDavid Howells {
595*b2a4df20SDavid Howells 	struct {
596*b2a4df20SDavid Howells 		struct key *keyring;
597*b2a4df20SDavid Howells 		struct assoc_array_node *node;
598*b2a4df20SDavid Howells 		int slot;
599*b2a4df20SDavid Howells 	} stack[KEYRING_SEARCH_MAX_DEPTH];
600*b2a4df20SDavid Howells 
601*b2a4df20SDavid Howells 	struct assoc_array_shortcut *shortcut;
602*b2a4df20SDavid Howells 	struct assoc_array_node *node;
603*b2a4df20SDavid Howells 	struct assoc_array_ptr *ptr;
604*b2a4df20SDavid Howells 	struct key *key;
605*b2a4df20SDavid Howells 	int sp = 0, slot;
606*b2a4df20SDavid Howells 
607*b2a4df20SDavid Howells 	kenter("{%d},{%s,%s}",
608*b2a4df20SDavid Howells 	       keyring->serial,
609*b2a4df20SDavid Howells 	       ctx->index_key.type->name,
610*b2a4df20SDavid Howells 	       ctx->index_key.description);
611*b2a4df20SDavid Howells 
612*b2a4df20SDavid Howells 	if (ctx->index_key.description)
613*b2a4df20SDavid Howells 		ctx->index_key.desc_len = strlen(ctx->index_key.description);
614*b2a4df20SDavid Howells 
615*b2a4df20SDavid Howells 	/* Check to see if this top-level keyring is what we are looking for
616*b2a4df20SDavid Howells 	 * and whether it is valid or not.
617*b2a4df20SDavid Howells 	 */
618*b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_LOOKUP_ITERATE ||
619*b2a4df20SDavid Howells 	    keyring_compare_object(keyring, &ctx->index_key)) {
620*b2a4df20SDavid Howells 		ctx->skipped_ret = 2;
621*b2a4df20SDavid Howells 		ctx->flags |= KEYRING_SEARCH_DO_STATE_CHECK;
622*b2a4df20SDavid Howells 		switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) {
623*b2a4df20SDavid Howells 		case 1:
624*b2a4df20SDavid Howells 			goto found;
625*b2a4df20SDavid Howells 		case 2:
626*b2a4df20SDavid Howells 			return false;
627*b2a4df20SDavid Howells 		default:
628*b2a4df20SDavid Howells 			break;
629*b2a4df20SDavid Howells 		}
630*b2a4df20SDavid Howells 	}
631*b2a4df20SDavid Howells 
632*b2a4df20SDavid Howells 	ctx->skipped_ret = 0;
633*b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_NO_STATE_CHECK)
634*b2a4df20SDavid Howells 		ctx->flags &= ~KEYRING_SEARCH_DO_STATE_CHECK;
635*b2a4df20SDavid Howells 
636*b2a4df20SDavid Howells 	/* Start processing a new keyring */
637*b2a4df20SDavid Howells descend_to_keyring:
638*b2a4df20SDavid Howells 	kdebug("descend to %d", keyring->serial);
639*b2a4df20SDavid Howells 	if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
640*b2a4df20SDavid Howells 			      (1 << KEY_FLAG_REVOKED)))
641*b2a4df20SDavid Howells 		goto not_this_keyring;
642*b2a4df20SDavid Howells 
643*b2a4df20SDavid Howells 	/* Search through the keys in this keyring before its searching its
644*b2a4df20SDavid Howells 	 * subtrees.
645*b2a4df20SDavid Howells 	 */
646*b2a4df20SDavid Howells 	if (search_keyring(keyring, ctx))
647*b2a4df20SDavid Howells 		goto found;
648*b2a4df20SDavid Howells 
649*b2a4df20SDavid Howells 	/* Then manually iterate through the keyrings nested in this one.
650*b2a4df20SDavid Howells 	 *
651*b2a4df20SDavid Howells 	 * Start from the root node of the index tree.  Because of the way the
652*b2a4df20SDavid Howells 	 * hash function has been set up, keyrings cluster on the leftmost
653*b2a4df20SDavid Howells 	 * branch of the root node (root slot 0) or in the root node itself.
654*b2a4df20SDavid Howells 	 * Non-keyrings avoid the leftmost branch of the root entirely (root
655*b2a4df20SDavid Howells 	 * slots 1-15).
656*b2a4df20SDavid Howells 	 */
657*b2a4df20SDavid Howells 	ptr = ACCESS_ONCE(keyring->keys.root);
658*b2a4df20SDavid Howells 	if (!ptr)
659*b2a4df20SDavid Howells 		goto not_this_keyring;
660*b2a4df20SDavid Howells 
661*b2a4df20SDavid Howells 	if (assoc_array_ptr_is_shortcut(ptr)) {
662*b2a4df20SDavid Howells 		/* If the root is a shortcut, either the keyring only contains
663*b2a4df20SDavid Howells 		 * keyring pointers (everything clusters behind root slot 0) or
664*b2a4df20SDavid Howells 		 * doesn't contain any keyring pointers.
665*b2a4df20SDavid Howells 		 */
666*b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
667*b2a4df20SDavid Howells 		smp_read_barrier_depends();
668*b2a4df20SDavid Howells 		if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0)
669*b2a4df20SDavid Howells 			goto not_this_keyring;
670*b2a4df20SDavid Howells 
671*b2a4df20SDavid Howells 		ptr = ACCESS_ONCE(shortcut->next_node);
672*b2a4df20SDavid Howells 		node = assoc_array_ptr_to_node(ptr);
673*b2a4df20SDavid Howells 		goto begin_node;
674*b2a4df20SDavid Howells 	}
675*b2a4df20SDavid Howells 
676*b2a4df20SDavid Howells 	node = assoc_array_ptr_to_node(ptr);
677*b2a4df20SDavid Howells 	smp_read_barrier_depends();
678*b2a4df20SDavid Howells 
679*b2a4df20SDavid Howells 	ptr = node->slots[0];
680*b2a4df20SDavid Howells 	if (!assoc_array_ptr_is_meta(ptr))
681*b2a4df20SDavid Howells 		goto begin_node;
682*b2a4df20SDavid Howells 
683*b2a4df20SDavid Howells descend_to_node:
684*b2a4df20SDavid Howells 	/* Descend to a more distal node in this keyring's content tree and go
685*b2a4df20SDavid Howells 	 * through that.
686*b2a4df20SDavid Howells 	 */
687*b2a4df20SDavid Howells 	kdebug("descend");
688*b2a4df20SDavid Howells 	if (assoc_array_ptr_is_shortcut(ptr)) {
689*b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
690*b2a4df20SDavid Howells 		smp_read_barrier_depends();
691*b2a4df20SDavid Howells 		ptr = ACCESS_ONCE(shortcut->next_node);
692*b2a4df20SDavid Howells 		BUG_ON(!assoc_array_ptr_is_node(ptr));
693*b2a4df20SDavid Howells 		node = assoc_array_ptr_to_node(ptr);
694*b2a4df20SDavid Howells 	}
695*b2a4df20SDavid Howells 
696*b2a4df20SDavid Howells begin_node:
697*b2a4df20SDavid Howells 	kdebug("begin_node");
698*b2a4df20SDavid Howells 	smp_read_barrier_depends();
699*b2a4df20SDavid Howells 	slot = 0;
700*b2a4df20SDavid Howells ascend_to_node:
701*b2a4df20SDavid Howells 	/* Go through the slots in a node */
702*b2a4df20SDavid Howells 	for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
703*b2a4df20SDavid Howells 		ptr = ACCESS_ONCE(node->slots[slot]);
704*b2a4df20SDavid Howells 
705*b2a4df20SDavid Howells 		if (assoc_array_ptr_is_meta(ptr) && node->back_pointer)
706*b2a4df20SDavid Howells 			goto descend_to_node;
707*b2a4df20SDavid Howells 
708*b2a4df20SDavid Howells 		if (!keyring_ptr_is_keyring(ptr))
709*b2a4df20SDavid Howells 			continue;
710*b2a4df20SDavid Howells 
711*b2a4df20SDavid Howells 		key = keyring_ptr_to_key(ptr);
712*b2a4df20SDavid Howells 
713*b2a4df20SDavid Howells 		if (sp >= KEYRING_SEARCH_MAX_DEPTH) {
714*b2a4df20SDavid Howells 			if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) {
715*b2a4df20SDavid Howells 				ctx->result = ERR_PTR(-ELOOP);
716*b2a4df20SDavid Howells 				return false;
717*b2a4df20SDavid Howells 			}
718*b2a4df20SDavid Howells 			goto not_this_keyring;
719*b2a4df20SDavid Howells 		}
720*b2a4df20SDavid Howells 
721*b2a4df20SDavid Howells 		/* Search a nested keyring */
722*b2a4df20SDavid Howells 		if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
723*b2a4df20SDavid Howells 		    key_task_permission(make_key_ref(key, ctx->possessed),
724*b2a4df20SDavid Howells 					ctx->cred, KEY_SEARCH) < 0)
725*b2a4df20SDavid Howells 			continue;
726*b2a4df20SDavid Howells 
727*b2a4df20SDavid Howells 		/* stack the current position */
728*b2a4df20SDavid Howells 		stack[sp].keyring = keyring;
729*b2a4df20SDavid Howells 		stack[sp].node = node;
730*b2a4df20SDavid Howells 		stack[sp].slot = slot;
731*b2a4df20SDavid Howells 		sp++;
732*b2a4df20SDavid Howells 
733*b2a4df20SDavid Howells 		/* begin again with the new keyring */
734*b2a4df20SDavid Howells 		keyring = key;
735*b2a4df20SDavid Howells 		goto descend_to_keyring;
736*b2a4df20SDavid Howells 	}
737*b2a4df20SDavid Howells 
738*b2a4df20SDavid Howells 	/* We've dealt with all the slots in the current node, so now we need
739*b2a4df20SDavid Howells 	 * to ascend to the parent and continue processing there.
740*b2a4df20SDavid Howells 	 */
741*b2a4df20SDavid Howells 	ptr = ACCESS_ONCE(node->back_pointer);
742*b2a4df20SDavid Howells 	slot = node->parent_slot;
743*b2a4df20SDavid Howells 
744*b2a4df20SDavid Howells 	if (ptr && assoc_array_ptr_is_shortcut(ptr)) {
745*b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
746*b2a4df20SDavid Howells 		smp_read_barrier_depends();
747*b2a4df20SDavid Howells 		ptr = ACCESS_ONCE(shortcut->back_pointer);
748*b2a4df20SDavid Howells 		slot = shortcut->parent_slot;
749*b2a4df20SDavid Howells 	}
750*b2a4df20SDavid Howells 	if (!ptr)
751*b2a4df20SDavid Howells 		goto not_this_keyring;
752*b2a4df20SDavid Howells 	node = assoc_array_ptr_to_node(ptr);
753*b2a4df20SDavid Howells 	smp_read_barrier_depends();
754*b2a4df20SDavid Howells 	slot++;
755*b2a4df20SDavid Howells 
756*b2a4df20SDavid Howells 	/* If we've ascended to the root (zero backpointer), we must have just
757*b2a4df20SDavid Howells 	 * finished processing the leftmost branch rather than the root slots -
758*b2a4df20SDavid Howells 	 * so there can't be any more keyrings for us to find.
759*b2a4df20SDavid Howells 	 */
760*b2a4df20SDavid Howells 	if (node->back_pointer) {
761*b2a4df20SDavid Howells 		kdebug("ascend %d", slot);
762*b2a4df20SDavid Howells 		goto ascend_to_node;
763*b2a4df20SDavid Howells 	}
764*b2a4df20SDavid Howells 
765*b2a4df20SDavid Howells 	/* The keyring we're looking at was disqualified or didn't contain a
766*b2a4df20SDavid Howells 	 * matching key.
767*b2a4df20SDavid Howells 	 */
768*b2a4df20SDavid Howells not_this_keyring:
769*b2a4df20SDavid Howells 	kdebug("not_this_keyring %d", sp);
770*b2a4df20SDavid Howells 	if (sp <= 0) {
771*b2a4df20SDavid Howells 		kleave(" = false");
772*b2a4df20SDavid Howells 		return false;
773*b2a4df20SDavid Howells 	}
774*b2a4df20SDavid Howells 
775*b2a4df20SDavid Howells 	/* Resume the processing of a keyring higher up in the tree */
776*b2a4df20SDavid Howells 	sp--;
777*b2a4df20SDavid Howells 	keyring = stack[sp].keyring;
778*b2a4df20SDavid Howells 	node = stack[sp].node;
779*b2a4df20SDavid Howells 	slot = stack[sp].slot + 1;
780*b2a4df20SDavid Howells 	kdebug("ascend to %d [%d]", keyring->serial, slot);
781*b2a4df20SDavid Howells 	goto ascend_to_node;
782*b2a4df20SDavid Howells 
783*b2a4df20SDavid Howells 	/* We found a viable match */
784*b2a4df20SDavid Howells found:
785*b2a4df20SDavid Howells 	key = key_ref_to_ptr(ctx->result);
786*b2a4df20SDavid Howells 	key_check(key);
787*b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) {
788*b2a4df20SDavid Howells 		key->last_used_at = ctx->now.tv_sec;
789*b2a4df20SDavid Howells 		keyring->last_used_at = ctx->now.tv_sec;
790*b2a4df20SDavid Howells 		while (sp > 0)
791*b2a4df20SDavid Howells 			stack[--sp].keyring->last_used_at = ctx->now.tv_sec;
792*b2a4df20SDavid Howells 	}
793*b2a4df20SDavid Howells 	kleave(" = true");
794*b2a4df20SDavid Howells 	return true;
795*b2a4df20SDavid Howells }
796*b2a4df20SDavid Howells 
797973c9f4fSDavid Howells /**
798973c9f4fSDavid Howells  * keyring_search_aux - Search a keyring tree for a key matching some criteria
799973c9f4fSDavid Howells  * @keyring_ref: A pointer to the keyring with possession indicator.
8004bdf0bc3SDavid Howells  * @ctx: The keyring search context.
801973c9f4fSDavid Howells  *
802973c9f4fSDavid Howells  * Search the supplied keyring tree for a key that matches the criteria given.
803973c9f4fSDavid Howells  * The root keyring and any linked keyrings must grant Search permission to the
804973c9f4fSDavid Howells  * caller to be searchable and keys can only be found if they too grant Search
805973c9f4fSDavid Howells  * to the caller. The possession flag on the root keyring pointer controls use
806973c9f4fSDavid Howells  * of the possessor bits in permissions checking of the entire tree.  In
807973c9f4fSDavid Howells  * addition, the LSM gets to forbid keyring searches and key matches.
808973c9f4fSDavid Howells  *
809973c9f4fSDavid Howells  * The search is performed as a breadth-then-depth search up to the prescribed
810973c9f4fSDavid Howells  * limit (KEYRING_SEARCH_MAX_DEPTH).
811973c9f4fSDavid Howells  *
812973c9f4fSDavid Howells  * Keys are matched to the type provided and are then filtered by the match
813973c9f4fSDavid Howells  * function, which is given the description to use in any way it sees fit.  The
814973c9f4fSDavid Howells  * match function may use any attributes of a key that it wishes to to
815973c9f4fSDavid Howells  * determine the match.  Normally the match function from the key type would be
816973c9f4fSDavid Howells  * used.
817973c9f4fSDavid Howells  *
818*b2a4df20SDavid Howells  * RCU can be used to prevent the keyring key lists from disappearing without
819*b2a4df20SDavid Howells  * the need to take lots of locks.
820973c9f4fSDavid Howells  *
821973c9f4fSDavid Howells  * Returns a pointer to the found key and increments the key usage count if
822973c9f4fSDavid Howells  * successful; -EAGAIN if no matching keys were found, or if expired or revoked
823973c9f4fSDavid Howells  * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
824973c9f4fSDavid Howells  * specified keyring wasn't a keyring.
825973c9f4fSDavid Howells  *
826973c9f4fSDavid Howells  * In the case of a successful return, the possession attribute from
827973c9f4fSDavid Howells  * @keyring_ref is propagated to the returned key reference.
8281da177e4SLinus Torvalds  */
829664cceb0SDavid Howells key_ref_t keyring_search_aux(key_ref_t keyring_ref,
8304bdf0bc3SDavid Howells 			     struct keyring_search_context *ctx)
8311da177e4SLinus Torvalds {
83231d5a79dSDavid Howells 	struct key *keyring;
8331da177e4SLinus Torvalds 	long err;
834*b2a4df20SDavid Howells 
835*b2a4df20SDavid Howells 	ctx->iterator = keyring_search_iterator;
836*b2a4df20SDavid Howells 	ctx->possessed = is_key_possessed(keyring_ref);
837*b2a4df20SDavid Howells 	ctx->result = ERR_PTR(-EAGAIN);
8381da177e4SLinus Torvalds 
839664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
8401da177e4SLinus Torvalds 	key_check(keyring);
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
843*b2a4df20SDavid Howells 		return ERR_PTR(-ENOTDIR);
844*b2a4df20SDavid Howells 
845*b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) {
846*b2a4df20SDavid Howells 		err = key_task_permission(keyring_ref, ctx->cred, KEY_SEARCH);
847*b2a4df20SDavid Howells 		if (err < 0)
848*b2a4df20SDavid Howells 			return ERR_PTR(err);
849*b2a4df20SDavid Howells 	}
8501da177e4SLinus Torvalds 
851664cceb0SDavid Howells 	rcu_read_lock();
8524bdf0bc3SDavid Howells 	ctx->now = current_kernel_time();
853*b2a4df20SDavid Howells 	if (search_nested_keyrings(keyring, ctx))
854*b2a4df20SDavid Howells 		__key_get(key_ref_to_ptr(ctx->result));
85576d8aeabSDavid Howells 	rcu_read_unlock();
856*b2a4df20SDavid Howells 	return ctx->result;
857a8b17ed0SDavid Howells }
8581da177e4SLinus Torvalds 
859973c9f4fSDavid Howells /**
860973c9f4fSDavid Howells  * keyring_search - Search the supplied keyring tree for a matching key
861973c9f4fSDavid Howells  * @keyring: The root of the keyring tree to be searched.
862973c9f4fSDavid Howells  * @type: The type of keyring we want to find.
863973c9f4fSDavid Howells  * @description: The name of the keyring we want to find.
864973c9f4fSDavid Howells  *
865973c9f4fSDavid Howells  * As keyring_search_aux() above, but using the current task's credentials and
866*b2a4df20SDavid Howells  * type's default matching function and preferred search method.
8671da177e4SLinus Torvalds  */
868664cceb0SDavid Howells key_ref_t keyring_search(key_ref_t keyring,
8691da177e4SLinus Torvalds 			 struct key_type *type,
8701da177e4SLinus Torvalds 			 const char *description)
8711da177e4SLinus Torvalds {
8724bdf0bc3SDavid Howells 	struct keyring_search_context ctx = {
8734bdf0bc3SDavid Howells 		.index_key.type		= type,
8744bdf0bc3SDavid Howells 		.index_key.description	= description,
8754bdf0bc3SDavid Howells 		.cred			= current_cred(),
8764bdf0bc3SDavid Howells 		.match			= type->match,
8774bdf0bc3SDavid Howells 		.match_data		= description,
8784bdf0bc3SDavid Howells 		.flags			= (type->def_lookup_type |
8794bdf0bc3SDavid Howells 					   KEYRING_SEARCH_DO_STATE_CHECK),
8804bdf0bc3SDavid Howells 	};
8814bdf0bc3SDavid Howells 
8824bdf0bc3SDavid Howells 	if (!ctx.match)
8833e30148cSDavid Howells 		return ERR_PTR(-ENOKEY);
8843e30148cSDavid Howells 
8854bdf0bc3SDavid Howells 	return keyring_search_aux(keyring, &ctx);
886a8b17ed0SDavid Howells }
8871da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_search);
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds /*
890*b2a4df20SDavid Howells  * Search the given keyring for a key that might be updated.
891973c9f4fSDavid Howells  *
892973c9f4fSDavid Howells  * The caller must guarantee that the keyring is a keyring and that the
893*b2a4df20SDavid Howells  * permission is granted to modify the keyring as no check is made here.  The
894*b2a4df20SDavid Howells  * caller must also hold a lock on the keyring semaphore.
895973c9f4fSDavid Howells  *
896973c9f4fSDavid Howells  * Returns a pointer to the found key with usage count incremented if
897*b2a4df20SDavid Howells  * successful and returns NULL if not found.  Revoked and invalidated keys are
898*b2a4df20SDavid Howells  * skipped over.
899973c9f4fSDavid Howells  *
900973c9f4fSDavid Howells  * If successful, the possession indicator is propagated from the keyring ref
901973c9f4fSDavid Howells  * to the returned key reference.
9021da177e4SLinus Torvalds  */
903*b2a4df20SDavid Howells key_ref_t find_key_to_update(key_ref_t keyring_ref,
904e57e8669SDavid Howells 			     const struct keyring_index_key *index_key)
9051da177e4SLinus Torvalds {
906664cceb0SDavid Howells 	struct key *keyring, *key;
907*b2a4df20SDavid Howells 	const void *object;
9081da177e4SLinus Torvalds 
909664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
910664cceb0SDavid Howells 
911*b2a4df20SDavid Howells 	kenter("{%d},{%s,%s}",
912*b2a4df20SDavid Howells 	       keyring->serial, index_key->type->name, index_key->description);
91376d8aeabSDavid Howells 
914*b2a4df20SDavid Howells 	object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
915*b2a4df20SDavid Howells 				  index_key);
916*b2a4df20SDavid Howells 
917*b2a4df20SDavid Howells 	if (object)
9181da177e4SLinus Torvalds 		goto found;
9191da177e4SLinus Torvalds 
920*b2a4df20SDavid Howells 	kleave(" = NULL");
921*b2a4df20SDavid Howells 	return NULL;
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds found:
924*b2a4df20SDavid Howells 	key = keyring_ptr_to_key(object);
925*b2a4df20SDavid Howells 	if (key->flags & ((1 << KEY_FLAG_INVALIDATED) |
926*b2a4df20SDavid Howells 			  (1 << KEY_FLAG_REVOKED))) {
927*b2a4df20SDavid Howells 		kleave(" = NULL [x]");
928*b2a4df20SDavid Howells 		return NULL;
929*b2a4df20SDavid Howells 	}
930ccc3e6d9SDavid Howells 	__key_get(key);
931*b2a4df20SDavid Howells 	kleave(" = {%d}", key->serial);
932*b2a4df20SDavid Howells 	return make_key_ref(key, is_key_possessed(keyring_ref));
933a8b17ed0SDavid Howells }
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds /*
936973c9f4fSDavid Howells  * Find a keyring with the specified name.
937973c9f4fSDavid Howells  *
938973c9f4fSDavid Howells  * All named keyrings in the current user namespace are searched, provided they
939973c9f4fSDavid Howells  * grant Search permission directly to the caller (unless this check is
940973c9f4fSDavid Howells  * skipped).  Keyrings whose usage points have reached zero or who have been
941973c9f4fSDavid Howells  * revoked are skipped.
942973c9f4fSDavid Howells  *
943973c9f4fSDavid Howells  * Returns a pointer to the keyring with the keyring's refcount having being
944973c9f4fSDavid Howells  * incremented on success.  -ENOKEY is returned if a key could not be found.
9451da177e4SLinus Torvalds  */
94669664cf1SDavid Howells struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
9471da177e4SLinus Torvalds {
9481da177e4SLinus Torvalds 	struct key *keyring;
9491da177e4SLinus Torvalds 	int bucket;
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	if (!name)
952cea7daa3SToshiyuki Okajima 		return ERR_PTR(-EINVAL);
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	bucket = keyring_hash(name);
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	read_lock(&keyring_name_lock);
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	if (keyring_name_hash[bucket].next) {
9591da177e4SLinus Torvalds 		/* search this hash bucket for a keyring with a matching name
9601da177e4SLinus Torvalds 		 * that's readable and that hasn't been revoked */
9611da177e4SLinus Torvalds 		list_for_each_entry(keyring,
9621da177e4SLinus Torvalds 				    &keyring_name_hash[bucket],
9631da177e4SLinus Torvalds 				    type_data.link
9641da177e4SLinus Torvalds 				    ) {
9659a56c2dbSEric W. Biederman 			if (!kuid_has_mapping(current_user_ns(), keyring->user->uid))
9662ea190d0SSerge E. Hallyn 				continue;
9672ea190d0SSerge E. Hallyn 
96876d8aeabSDavid Howells 			if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
9691da177e4SLinus Torvalds 				continue;
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 			if (strcmp(keyring->description, name) != 0)
9721da177e4SLinus Torvalds 				continue;
9731da177e4SLinus Torvalds 
97469664cf1SDavid Howells 			if (!skip_perm_check &&
97569664cf1SDavid Howells 			    key_permission(make_key_ref(keyring, 0),
97629db9190SDavid Howells 					   KEY_SEARCH) < 0)
9771da177e4SLinus Torvalds 				continue;
9781da177e4SLinus Torvalds 
979cea7daa3SToshiyuki Okajima 			/* we've got a match but we might end up racing with
980cea7daa3SToshiyuki Okajima 			 * key_cleanup() if the keyring is currently 'dead'
981cea7daa3SToshiyuki Okajima 			 * (ie. it has a zero usage count) */
982cea7daa3SToshiyuki Okajima 			if (!atomic_inc_not_zero(&keyring->usage))
983cea7daa3SToshiyuki Okajima 				continue;
98431d5a79dSDavid Howells 			keyring->last_used_at = current_kernel_time().tv_sec;
985cea7daa3SToshiyuki Okajima 			goto out;
9861da177e4SLinus Torvalds 		}
9871da177e4SLinus Torvalds 	}
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	keyring = ERR_PTR(-ENOKEY);
990cea7daa3SToshiyuki Okajima out:
991cea7daa3SToshiyuki Okajima 	read_unlock(&keyring_name_lock);
9921da177e4SLinus Torvalds 	return keyring;
993a8b17ed0SDavid Howells }
9941da177e4SLinus Torvalds 
995*b2a4df20SDavid Howells static int keyring_detect_cycle_iterator(const void *object,
996*b2a4df20SDavid Howells 					 void *iterator_data)
997*b2a4df20SDavid Howells {
998*b2a4df20SDavid Howells 	struct keyring_search_context *ctx = iterator_data;
999*b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
1000*b2a4df20SDavid Howells 
1001*b2a4df20SDavid Howells 	kenter("{%d}", key->serial);
1002*b2a4df20SDavid Howells 
1003*b2a4df20SDavid Howells 	BUG_ON(key != ctx->match_data);
1004*b2a4df20SDavid Howells 	ctx->result = ERR_PTR(-EDEADLK);
1005*b2a4df20SDavid Howells 	return 1;
1006*b2a4df20SDavid Howells }
1007*b2a4df20SDavid Howells 
10081da177e4SLinus Torvalds /*
1009973c9f4fSDavid Howells  * See if a cycle will will be created by inserting acyclic tree B in acyclic
1010973c9f4fSDavid Howells  * tree A at the topmost level (ie: as a direct child of A).
1011973c9f4fSDavid Howells  *
1012973c9f4fSDavid Howells  * Since we are adding B to A at the top level, checking for cycles should just
1013973c9f4fSDavid Howells  * be a matter of seeing if node A is somewhere in tree B.
10141da177e4SLinus Torvalds  */
10151da177e4SLinus Torvalds static int keyring_detect_cycle(struct key *A, struct key *B)
10161da177e4SLinus Torvalds {
1017*b2a4df20SDavid Howells 	struct keyring_search_context ctx = {
1018*b2a4df20SDavid Howells 		.index_key	= A->index_key,
1019*b2a4df20SDavid Howells 		.match_data	= A,
1020*b2a4df20SDavid Howells 		.iterator	= keyring_detect_cycle_iterator,
1021*b2a4df20SDavid Howells 		.flags		= (KEYRING_SEARCH_LOOKUP_DIRECT |
1022*b2a4df20SDavid Howells 				   KEYRING_SEARCH_NO_STATE_CHECK |
1023*b2a4df20SDavid Howells 				   KEYRING_SEARCH_NO_UPDATE_TIME |
1024*b2a4df20SDavid Howells 				   KEYRING_SEARCH_NO_CHECK_PERM |
1025*b2a4df20SDavid Howells 				   KEYRING_SEARCH_DETECT_TOO_DEEP),
1026*b2a4df20SDavid Howells 	};
10271da177e4SLinus Torvalds 
102876d8aeabSDavid Howells 	rcu_read_lock();
1029*b2a4df20SDavid Howells 	search_nested_keyrings(B, &ctx);
103076d8aeabSDavid Howells 	rcu_read_unlock();
1031*b2a4df20SDavid Howells 	return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result);
1032f70e2e06SDavid Howells }
1033cab8eb59SDavid Howells 
1034cab8eb59SDavid Howells /*
1035973c9f4fSDavid Howells  * Preallocate memory so that a key can be linked into to a keyring.
10361da177e4SLinus Torvalds  */
1037*b2a4df20SDavid Howells int __key_link_begin(struct key *keyring,
1038*b2a4df20SDavid Howells 		     const struct keyring_index_key *index_key,
1039*b2a4df20SDavid Howells 		     struct assoc_array_edit **_edit)
1040f70e2e06SDavid Howells 	__acquires(&keyring->sem)
1041423b9788SDavid Howells 	__acquires(&keyring_serialise_link_sem)
10421da177e4SLinus Torvalds {
1043*b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1044*b2a4df20SDavid Howells 	int ret;
10451da177e4SLinus Torvalds 
104616feef43SDavid Howells 	kenter("%d,%s,%s,",
1047*b2a4df20SDavid Howells 	       keyring->serial, index_key->type->name, index_key->description);
1048*b2a4df20SDavid Howells 
1049*b2a4df20SDavid Howells 	BUG_ON(index_key->desc_len == 0);
1050f70e2e06SDavid Howells 
1051f70e2e06SDavid Howells 	if (keyring->type != &key_type_keyring)
1052f70e2e06SDavid Howells 		return -ENOTDIR;
1053f70e2e06SDavid Howells 
1054f70e2e06SDavid Howells 	down_write(&keyring->sem);
1055f70e2e06SDavid Howells 
10561da177e4SLinus Torvalds 	ret = -EKEYREVOKED;
105776d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1058f70e2e06SDavid Howells 		goto error_krsem;
10591da177e4SLinus Torvalds 
1060f70e2e06SDavid Howells 	/* serialise link/link calls to prevent parallel calls causing a cycle
1061f70e2e06SDavid Howells 	 * when linking two keyring in opposite orders */
106216feef43SDavid Howells 	if (index_key->type == &key_type_keyring)
10631da177e4SLinus Torvalds 		down_write(&keyring_serialise_link_sem);
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	/* check that we aren't going to overrun the user's quota */
10661da177e4SLinus Torvalds 	ret = key_payload_reserve(keyring,
10671da177e4SLinus Torvalds 				  keyring->datalen + KEYQUOTA_LINK_BYTES);
10681da177e4SLinus Torvalds 	if (ret < 0)
1069f70e2e06SDavid Howells 		goto error_sem;
10701da177e4SLinus Torvalds 
1071*b2a4df20SDavid Howells 	/* Create an edit script that will insert/replace the key in the
1072*b2a4df20SDavid Howells 	 * keyring tree.
1073*b2a4df20SDavid Howells 	 */
1074*b2a4df20SDavid Howells 	edit = assoc_array_insert(&keyring->keys,
1075*b2a4df20SDavid Howells 				  &keyring_assoc_array_ops,
1076*b2a4df20SDavid Howells 				  index_key,
1077*b2a4df20SDavid Howells 				  NULL);
1078*b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1079*b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1080f70e2e06SDavid Howells 		goto error_quota;
10811da177e4SLinus Torvalds 	}
10821da177e4SLinus Torvalds 
1083*b2a4df20SDavid Howells 	*_edit = edit;
1084f70e2e06SDavid Howells 	kleave(" = 0");
1085f70e2e06SDavid Howells 	return 0;
10861da177e4SLinus Torvalds 
1087f70e2e06SDavid Howells error_quota:
10881da177e4SLinus Torvalds 	/* undo the quota changes */
10891da177e4SLinus Torvalds 	key_payload_reserve(keyring,
10901da177e4SLinus Torvalds 			    keyring->datalen - KEYQUOTA_LINK_BYTES);
1091f70e2e06SDavid Howells error_sem:
109216feef43SDavid Howells 	if (index_key->type == &key_type_keyring)
1093f70e2e06SDavid Howells 		up_write(&keyring_serialise_link_sem);
1094f70e2e06SDavid Howells error_krsem:
1095f70e2e06SDavid Howells 	up_write(&keyring->sem);
1096f70e2e06SDavid Howells 	kleave(" = %d", ret);
1097f70e2e06SDavid Howells 	return ret;
1098f70e2e06SDavid Howells }
10991da177e4SLinus Torvalds 
1100f70e2e06SDavid Howells /*
1101973c9f4fSDavid Howells  * Check already instantiated keys aren't going to be a problem.
1102973c9f4fSDavid Howells  *
1103973c9f4fSDavid Howells  * The caller must have called __key_link_begin(). Don't need to call this for
1104973c9f4fSDavid Howells  * keys that were created since __key_link_begin() was called.
1105f70e2e06SDavid Howells  */
1106f70e2e06SDavid Howells int __key_link_check_live_key(struct key *keyring, struct key *key)
1107f70e2e06SDavid Howells {
1108f70e2e06SDavid Howells 	if (key->type == &key_type_keyring)
1109f70e2e06SDavid Howells 		/* check that we aren't going to create a cycle by linking one
1110f70e2e06SDavid Howells 		 * keyring to another */
1111f70e2e06SDavid Howells 		return keyring_detect_cycle(keyring, key);
1112f70e2e06SDavid Howells 	return 0;
1113f70e2e06SDavid Howells }
11141da177e4SLinus Torvalds 
1115f70e2e06SDavid Howells /*
1116973c9f4fSDavid Howells  * Link a key into to a keyring.
1117973c9f4fSDavid Howells  *
1118973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.  Discards any
1119973c9f4fSDavid Howells  * already extant link to matching key if there is one, so that each keyring
1120973c9f4fSDavid Howells  * holds at most one link to any given key of a particular type+description
1121973c9f4fSDavid Howells  * combination.
1122f70e2e06SDavid Howells  */
1123*b2a4df20SDavid Howells void __key_link(struct key *key, struct assoc_array_edit **_edit)
1124f70e2e06SDavid Howells {
1125ccc3e6d9SDavid Howells 	__key_get(key);
1126*b2a4df20SDavid Howells 	assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key));
1127*b2a4df20SDavid Howells 	assoc_array_apply_edit(*_edit);
1128*b2a4df20SDavid Howells 	*_edit = NULL;
1129f70e2e06SDavid Howells }
1130f70e2e06SDavid Howells 
1131f70e2e06SDavid Howells /*
1132973c9f4fSDavid Howells  * Finish linking a key into to a keyring.
1133973c9f4fSDavid Howells  *
1134973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.
1135f70e2e06SDavid Howells  */
113616feef43SDavid Howells void __key_link_end(struct key *keyring,
113716feef43SDavid Howells 		    const struct keyring_index_key *index_key,
1138*b2a4df20SDavid Howells 		    struct assoc_array_edit *edit)
1139f70e2e06SDavid Howells 	__releases(&keyring->sem)
1140423b9788SDavid Howells 	__releases(&keyring_serialise_link_sem)
1141f70e2e06SDavid Howells {
114216feef43SDavid Howells 	BUG_ON(index_key->type == NULL);
1143*b2a4df20SDavid Howells 	kenter("%d,%s,", keyring->serial, index_key->type->name);
1144f70e2e06SDavid Howells 
114516feef43SDavid Howells 	if (index_key->type == &key_type_keyring)
1146f70e2e06SDavid Howells 		up_write(&keyring_serialise_link_sem);
1147f70e2e06SDavid Howells 
1148*b2a4df20SDavid Howells 	if (edit) {
1149f70e2e06SDavid Howells 		key_payload_reserve(keyring,
1150*b2a4df20SDavid Howells 				    keyring->datalen - KEYQUOTA_LINK_BYTES);
1151*b2a4df20SDavid Howells 		assoc_array_cancel_edit(edit);
1152f70e2e06SDavid Howells 	}
1153f70e2e06SDavid Howells 	up_write(&keyring->sem);
1154f70e2e06SDavid Howells }
1155f70e2e06SDavid Howells 
1156973c9f4fSDavid Howells /**
1157973c9f4fSDavid Howells  * key_link - Link a key to a keyring
1158973c9f4fSDavid Howells  * @keyring: The keyring to make the link in.
1159973c9f4fSDavid Howells  * @key: The key to link to.
1160973c9f4fSDavid Howells  *
1161973c9f4fSDavid Howells  * Make a link in a keyring to a key, such that the keyring holds a reference
1162973c9f4fSDavid Howells  * on that key and the key can potentially be found by searching that keyring.
1163973c9f4fSDavid Howells  *
1164973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore and will consume some
1165973c9f4fSDavid Howells  * of the user's key data quota to hold the link.
1166973c9f4fSDavid Howells  *
1167973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
1168973c9f4fSDavid Howells  * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
1169973c9f4fSDavid Howells  * full, -EDQUOT if there is insufficient key data quota remaining to add
1170973c9f4fSDavid Howells  * another link or -ENOMEM if there's insufficient memory.
1171973c9f4fSDavid Howells  *
1172973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1173973c9f4fSDavid Howells  * be made (the keyring should have Write permission and the key Link
1174973c9f4fSDavid Howells  * permission).
11751da177e4SLinus Torvalds  */
11761da177e4SLinus Torvalds int key_link(struct key *keyring, struct key *key)
11771da177e4SLinus Torvalds {
1178*b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
11791da177e4SLinus Torvalds 	int ret;
11801da177e4SLinus Torvalds 
1181*b2a4df20SDavid Howells 	kenter("{%d,%d}", keyring->serial, atomic_read(&keyring->usage));
1182*b2a4df20SDavid Howells 
11831da177e4SLinus Torvalds 	key_check(keyring);
11841da177e4SLinus Torvalds 	key_check(key);
11851da177e4SLinus Torvalds 
1186*b2a4df20SDavid Howells 	ret = __key_link_begin(keyring, &key->index_key, &edit);
1187f70e2e06SDavid Howells 	if (ret == 0) {
1188*b2a4df20SDavid Howells 		kdebug("begun {%d,%d}", keyring->serial, atomic_read(&keyring->usage));
1189f70e2e06SDavid Howells 		ret = __key_link_check_live_key(keyring, key);
1190f70e2e06SDavid Howells 		if (ret == 0)
1191*b2a4df20SDavid Howells 			__key_link(key, &edit);
1192*b2a4df20SDavid Howells 		__key_link_end(keyring, &key->index_key, edit);
1193f70e2e06SDavid Howells 	}
11941da177e4SLinus Torvalds 
1195*b2a4df20SDavid Howells 	kleave(" = %d {%d,%d}", ret, keyring->serial, atomic_read(&keyring->usage));
11961da177e4SLinus Torvalds 	return ret;
1197f70e2e06SDavid Howells }
11981da177e4SLinus Torvalds EXPORT_SYMBOL(key_link);
11991da177e4SLinus Torvalds 
1200973c9f4fSDavid Howells /**
1201973c9f4fSDavid Howells  * key_unlink - Unlink the first link to a key from a keyring.
1202973c9f4fSDavid Howells  * @keyring: The keyring to remove the link from.
1203973c9f4fSDavid Howells  * @key: The key the link is to.
1204973c9f4fSDavid Howells  *
1205973c9f4fSDavid Howells  * Remove a link from a keyring to a key.
1206973c9f4fSDavid Howells  *
1207973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore.
1208973c9f4fSDavid Howells  *
1209973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
1210973c9f4fSDavid Howells  * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
1211973c9f4fSDavid Howells  * memory.
1212973c9f4fSDavid Howells  *
1213973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1214973c9f4fSDavid Howells  * be removed (the keyring should have Write permission; no permissions are
1215973c9f4fSDavid Howells  * required on the key).
12161da177e4SLinus Torvalds  */
12171da177e4SLinus Torvalds int key_unlink(struct key *keyring, struct key *key)
12181da177e4SLinus Torvalds {
1219*b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1220*b2a4df20SDavid Howells 	int ret;
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	key_check(keyring);
12231da177e4SLinus Torvalds 	key_check(key);
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
1226*b2a4df20SDavid Howells 		return -ENOTDIR;
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	down_write(&keyring->sem);
12291da177e4SLinus Torvalds 
1230*b2a4df20SDavid Howells 	edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops,
1231*b2a4df20SDavid Howells 				  &key->index_key);
1232*b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1233*b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1234*b2a4df20SDavid Howells 		goto error;
12351da177e4SLinus Torvalds 	}
12361da177e4SLinus Torvalds 	ret = -ENOENT;
1237*b2a4df20SDavid Howells 	if (edit == NULL)
12381da177e4SLinus Torvalds 		goto error;
12391da177e4SLinus Torvalds 
1240*b2a4df20SDavid Howells 	assoc_array_apply_edit(edit);
12411da177e4SLinus Torvalds 	ret = 0;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds error:
124476d8aeabSDavid Howells 	up_write(&keyring->sem);
1245*b2a4df20SDavid Howells 	return ret;
1246a8b17ed0SDavid Howells }
12471da177e4SLinus Torvalds EXPORT_SYMBOL(key_unlink);
12481da177e4SLinus Torvalds 
1249973c9f4fSDavid Howells /**
1250973c9f4fSDavid Howells  * keyring_clear - Clear a keyring
1251973c9f4fSDavid Howells  * @keyring: The keyring to clear.
1252973c9f4fSDavid Howells  *
1253973c9f4fSDavid Howells  * Clear the contents of the specified keyring.
1254973c9f4fSDavid Howells  *
1255973c9f4fSDavid Howells  * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
12561da177e4SLinus Torvalds  */
12571da177e4SLinus Torvalds int keyring_clear(struct key *keyring)
12581da177e4SLinus Torvalds {
1259*b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
126076d8aeabSDavid Howells 	int ret;
12611da177e4SLinus Torvalds 
1262*b2a4df20SDavid Howells 	if (keyring->type != &key_type_keyring)
1263*b2a4df20SDavid Howells 		return -ENOTDIR;
1264*b2a4df20SDavid Howells 
12651da177e4SLinus Torvalds 	down_write(&keyring->sem);
12661da177e4SLinus Torvalds 
1267*b2a4df20SDavid Howells 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1268*b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1269*b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1270*b2a4df20SDavid Howells 	} else {
1271*b2a4df20SDavid Howells 		if (edit)
1272*b2a4df20SDavid Howells 			assoc_array_apply_edit(edit);
1273*b2a4df20SDavid Howells 		key_payload_reserve(keyring, 0);
12741da177e4SLinus Torvalds 		ret = 0;
12751da177e4SLinus Torvalds 	}
12761da177e4SLinus Torvalds 
1277*b2a4df20SDavid Howells 	up_write(&keyring->sem);
12781da177e4SLinus Torvalds 	return ret;
1279a8b17ed0SDavid Howells }
12801da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_clear);
128131204ed9SDavid Howells 
128231204ed9SDavid Howells /*
1283973c9f4fSDavid Howells  * Dispose of the links from a revoked keyring.
1284973c9f4fSDavid Howells  *
1285973c9f4fSDavid Howells  * This is called with the key sem write-locked.
128631204ed9SDavid Howells  */
128731204ed9SDavid Howells static void keyring_revoke(struct key *keyring)
128831204ed9SDavid Howells {
1289*b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1290f0641cbaSDavid Howells 
1291*b2a4df20SDavid Howells 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1292*b2a4df20SDavid Howells 	if (!IS_ERR(edit)) {
1293*b2a4df20SDavid Howells 		if (edit)
1294*b2a4df20SDavid Howells 			assoc_array_apply_edit(edit);
129531204ed9SDavid Howells 		key_payload_reserve(keyring, 0);
129631204ed9SDavid Howells 	}
1297a8b17ed0SDavid Howells }
12985d135440SDavid Howells 
1299*b2a4df20SDavid Howells static bool gc_iterator(void *object, void *iterator_data)
1300*b2a4df20SDavid Howells {
1301*b2a4df20SDavid Howells 	struct key *key = keyring_ptr_to_key(object);
1302*b2a4df20SDavid Howells 	time_t *limit = iterator_data;
1303*b2a4df20SDavid Howells 
1304*b2a4df20SDavid Howells 	if (key_is_dead(key, *limit))
1305*b2a4df20SDavid Howells 		return false;
1306*b2a4df20SDavid Howells 	key_get(key);
1307*b2a4df20SDavid Howells 	return true;
1308*b2a4df20SDavid Howells }
1309*b2a4df20SDavid Howells 
13105d135440SDavid Howells /*
1311973c9f4fSDavid Howells  * Collect garbage from the contents of a keyring, replacing the old list with
1312973c9f4fSDavid Howells  * a new one with the pointers all shuffled down.
1313973c9f4fSDavid Howells  *
1314973c9f4fSDavid Howells  * Dead keys are classed as oned that are flagged as being dead or are revoked,
1315973c9f4fSDavid Howells  * expired or negative keys that were revoked or expired before the specified
1316973c9f4fSDavid Howells  * limit.
13175d135440SDavid Howells  */
13185d135440SDavid Howells void keyring_gc(struct key *keyring, time_t limit)
13195d135440SDavid Howells {
1320c08ef808SDavid Howells 	kenter("{%x,%s}", key_serial(keyring), keyring->description);
13215d135440SDavid Howells 
13225d135440SDavid Howells 	down_write(&keyring->sem);
1323*b2a4df20SDavid Howells 	assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops,
1324*b2a4df20SDavid Howells 		       gc_iterator, &limit);
13255d135440SDavid Howells 	up_write(&keyring->sem);
13265d135440SDavid Howells 
1327*b2a4df20SDavid Howells 	kleave("");
13285d135440SDavid Howells }
1329