blob: a06ffab38568809f5187970261888d6a9e673f18 [file] [log] [blame]
David Howells69664cf2008-04-29 01:01:31 -07001/* Keyring handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howells69664cf2008-04-29 01:01:31 -07003 * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/sched.h>
15#include <linux/slab.h>
David Howells29db9192005-10-30 15:02:44 -080016#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/seq_file.h>
18#include <linux/err.h>
David Howellse9e349b2008-11-14 10:39:13 +110019#include <keys/keyring-type.h>
Chihau Chau512ea3b2010-03-08 20:11:34 -030020#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "internal.h"
22
David Howellsf0641cb2010-04-30 14:32:18 +010023#define rcu_dereference_locked_keyring(keyring) \
24 (rcu_dereference_protected( \
25 (keyring)->payload.subscriptions, \
26 rwsem_is_locked((struct rw_semaphore *)&(keyring)->sem)))
27
David Howellsceb73c12011-01-25 16:34:28 +000028#define KEY_LINK_FIXQUOTA 1UL
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
David Howells973c9f42011-01-20 16:38:33 +000031 * When plumbing the depths of the key tree, this sets a hard limit
32 * set on how deep we're willing to go.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34#define KEYRING_SEARCH_MAX_DEPTH 6
35
36/*
David Howells973c9f42011-01-20 16:38:33 +000037 * We keep all named keyrings in a hash to speed looking them up.
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
39#define KEYRING_NAME_HASH_SIZE (1 << 5)
40
41static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE];
42static DEFINE_RWLOCK(keyring_name_lock);
43
44static inline unsigned keyring_hash(const char *desc)
45{
46 unsigned bucket = 0;
47
48 for (; *desc; desc++)
Justin P. Mattockc5b60b52010-04-21 00:02:11 -070049 bucket += (unsigned char)*desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 return bucket & (KEYRING_NAME_HASH_SIZE - 1);
52}
53
54/*
David Howells973c9f42011-01-20 16:38:33 +000055 * The keyring key type definition. Keyrings are simply keys of this type and
56 * can be treated as ordinary keys in addition to having their own special
57 * operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59static int keyring_instantiate(struct key *keyring,
60 const void *data, size_t datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int keyring_match(const struct key *keyring, const void *criterion);
David Howells31204ed2006-06-26 00:24:51 -070062static void keyring_revoke(struct key *keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void keyring_destroy(struct key *keyring);
64static void keyring_describe(const struct key *keyring, struct seq_file *m);
65static long keyring_read(const struct key *keyring,
66 char __user *buffer, size_t buflen);
67
68struct key_type key_type_keyring = {
69 .name = "keyring",
70 .def_datalen = sizeof(struct keyring_list),
71 .instantiate = keyring_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .match = keyring_match,
David Howells31204ed2006-06-26 00:24:51 -070073 .revoke = keyring_revoke,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 .destroy = keyring_destroy,
75 .describe = keyring_describe,
76 .read = keyring_read,
77};
David Howells73182262007-04-26 15:46:23 -070078EXPORT_SYMBOL(key_type_keyring);
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
David Howells973c9f42011-01-20 16:38:33 +000081 * Semaphore to serialise link/link calls to prevent two link calls in parallel
82 * introducing a cycle.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
Adrian Bunk1ae8f402006-01-06 00:11:25 -080084static DECLARE_RWSEM(keyring_serialise_link_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/*
David Howells973c9f42011-01-20 16:38:33 +000087 * Publish the name of a keyring so that it can be found by name (if it has
88 * one).
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
David Howells69664cf2008-04-29 01:01:31 -070090static void keyring_publish_name(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 int bucket;
93
94 if (keyring->description) {
95 bucket = keyring_hash(keyring->description);
96
97 write_lock(&keyring_name_lock);
98
99 if (!keyring_name_hash[bucket].next)
100 INIT_LIST_HEAD(&keyring_name_hash[bucket]);
101
102 list_add_tail(&keyring->type_data.link,
103 &keyring_name_hash[bucket]);
104
105 write_unlock(&keyring_name_lock);
106 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000107}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
David Howells973c9f42011-01-20 16:38:33 +0000110 * Initialise a keyring.
111 *
112 * Returns 0 on success, -EINVAL if given any data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114static int keyring_instantiate(struct key *keyring,
115 const void *data, size_t datalen)
116{
117 int ret;
118
119 ret = -EINVAL;
120 if (datalen == 0) {
121 /* make the keyring available by name if it has one */
122 keyring_publish_name(keyring);
123 ret = 0;
124 }
125
126 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000127}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/*
David Howells973c9f42011-01-20 16:38:33 +0000130 * Match keyrings on their name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
132static int keyring_match(const struct key *keyring, const void *description)
133{
134 return keyring->description &&
135 strcmp(keyring->description, description) == 0;
David Howellsa8b17ed2011-01-20 16:38:27 +0000136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
David Howells973c9f42011-01-20 16:38:33 +0000139 * Clean up a keyring when it is destroyed. Unpublish its name if it had one
140 * and dispose of its data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 */
142static void keyring_destroy(struct key *keyring)
143{
144 struct keyring_list *klist;
145 int loop;
146
147 if (keyring->description) {
148 write_lock(&keyring_name_lock);
David Howells94efe722005-08-04 13:07:07 -0700149
150 if (keyring->type_data.link.next != NULL &&
151 !list_empty(&keyring->type_data.link))
152 list_del(&keyring->type_data.link);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 write_unlock(&keyring_name_lock);
155 }
156
Paul E. McKenneye7b0a612010-02-22 17:04:56 -0800157 klist = rcu_dereference_check(keyring->payload.subscriptions,
158 rcu_read_lock_held() ||
159 atomic_read(&keyring->usage) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (klist) {
161 for (loop = klist->nkeys - 1; loop >= 0; loop--)
162 key_put(klist->keys[loop]);
163 kfree(klist);
164 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
David Howells973c9f42011-01-20 16:38:33 +0000168 * Describe a keyring for /proc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
170static void keyring_describe(const struct key *keyring, struct seq_file *m)
171{
172 struct keyring_list *klist;
173
wzt.wzt@gmail.comc8563472010-03-04 21:26:23 +0800174 if (keyring->description)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 seq_puts(m, keyring->description);
wzt.wzt@gmail.comc8563472010-03-04 21:26:23 +0800176 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 seq_puts(m, "[anon]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
David Howells78b72802011-03-11 17:57:23 +0000179 if (key_is_instantiated(keyring)) {
180 rcu_read_lock();
181 klist = rcu_dereference(keyring->payload.subscriptions);
182 if (klist)
183 seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
184 else
185 seq_puts(m, ": empty");
186 rcu_read_unlock();
187 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000188}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
David Howells973c9f42011-01-20 16:38:33 +0000191 * Read a list of key IDs from the keyring's contents in binary form
192 *
193 * The keyring's semaphore is read-locked by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
195static long keyring_read(const struct key *keyring,
196 char __user *buffer, size_t buflen)
197{
198 struct keyring_list *klist;
199 struct key *key;
200 size_t qty, tmp;
201 int loop, ret;
202
203 ret = 0;
David Howellsf0641cb2010-04-30 14:32:18 +0100204 klist = rcu_dereference_locked_keyring(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (klist) {
206 /* calculate how much data we could return */
207 qty = klist->nkeys * sizeof(key_serial_t);
208
209 if (buffer && buflen > 0) {
210 if (buflen > qty)
211 buflen = qty;
212
213 /* copy the IDs of the subscribed keys into the
214 * buffer */
215 ret = -EFAULT;
216
217 for (loop = 0; loop < klist->nkeys; loop++) {
218 key = klist->keys[loop];
219
220 tmp = sizeof(key_serial_t);
221 if (tmp > buflen)
222 tmp = buflen;
223
224 if (copy_to_user(buffer,
225 &key->serial,
226 tmp) != 0)
227 goto error;
228
229 buflen -= tmp;
230 if (buflen == 0)
231 break;
232 buffer += tmp;
233 }
234 }
235
236 ret = qty;
237 }
238
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700239error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/*
David Howells973c9f42011-01-20 16:38:33 +0000244 * Allocate a keyring and link into the destination keyring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
David Howellsd84f4f92008-11-14 10:39:23 +1100247 const struct cred *cred, unsigned long flags,
Michael LeMayd7200242006-06-22 14:47:17 -0700248 struct key *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct key *keyring;
251 int ret;
252
253 keyring = key_alloc(&key_type_keyring, description,
David Howellsd84f4f92008-11-14 10:39:23 +1100254 uid, gid, cred,
David Howells29db9192005-10-30 15:02:44 -0800255 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
David Howells7e047ef2006-06-26 00:24:50 -0700256 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (!IS_ERR(keyring)) {
David Howells3e301482005-06-23 22:00:56 -0700259 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (ret < 0) {
261 key_put(keyring);
262 keyring = ERR_PTR(ret);
263 }
264 }
265
266 return keyring;
David Howellsa8b17ed2011-01-20 16:38:27 +0000267}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
David Howells973c9f42011-01-20 16:38:33 +0000269/**
270 * keyring_search_aux - Search a keyring tree for a key matching some criteria
271 * @keyring_ref: A pointer to the keyring with possession indicator.
272 * @cred: The credentials to use for permissions checks.
273 * @type: The type of key to search for.
274 * @description: Parameter for @match.
275 * @match: Function to rule on whether or not a key is the one required.
David Howells78b72802011-03-11 17:57:23 +0000276 * @no_state_check: Don't check if a matching key is bad
David Howells973c9f42011-01-20 16:38:33 +0000277 *
278 * Search the supplied keyring tree for a key that matches the criteria given.
279 * The root keyring and any linked keyrings must grant Search permission to the
280 * caller to be searchable and keys can only be found if they too grant Search
281 * to the caller. The possession flag on the root keyring pointer controls use
282 * of the possessor bits in permissions checking of the entire tree. In
283 * addition, the LSM gets to forbid keyring searches and key matches.
284 *
285 * The search is performed as a breadth-then-depth search up to the prescribed
286 * limit (KEYRING_SEARCH_MAX_DEPTH).
287 *
288 * Keys are matched to the type provided and are then filtered by the match
289 * function, which is given the description to use in any way it sees fit. The
290 * match function may use any attributes of a key that it wishes to to
291 * determine the match. Normally the match function from the key type would be
292 * used.
293 *
294 * RCU is used to prevent the keyring key lists from disappearing without the
295 * need to take lots of locks.
296 *
297 * Returns a pointer to the found key and increments the key usage count if
298 * successful; -EAGAIN if no matching keys were found, or if expired or revoked
299 * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
300 * specified keyring wasn't a keyring.
301 *
302 * In the case of a successful return, the possession attribute from
303 * @keyring_ref is propagated to the returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
David Howells664cceb2005-09-28 17:03:15 +0100305key_ref_t keyring_search_aux(key_ref_t keyring_ref,
David Howellsd84f4f92008-11-14 10:39:23 +1100306 const struct cred *cred,
David Howells664cceb2005-09-28 17:03:15 +0100307 struct key_type *type,
308 const void *description,
David Howells78b72802011-03-11 17:57:23 +0000309 key_match_func_t match,
310 bool no_state_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct {
David Howells76d8aea2005-06-23 22:00:49 -0700313 struct keyring_list *keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int kix;
315 } stack[KEYRING_SEARCH_MAX_DEPTH];
316
317 struct keyring_list *keylist;
318 struct timespec now;
Kevin Coffmandceba992008-04-29 01:01:22 -0700319 unsigned long possessed, kflags;
David Howells664cceb2005-09-28 17:03:15 +0100320 struct key *keyring, *key;
321 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 long err;
David Howells76d8aea2005-06-23 22:00:49 -0700323 int sp, kix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
David Howells664cceb2005-09-28 17:03:15 +0100325 keyring = key_ref_to_ptr(keyring_ref);
326 possessed = is_key_possessed(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 key_check(keyring);
328
329 /* top keyring must have search permission to begin the search */
Chihau Chau512ea3b2010-03-08 20:11:34 -0300330 err = key_task_permission(keyring_ref, cred, KEY_SEARCH);
David Howells29db9192005-10-30 15:02:44 -0800331 if (err < 0) {
332 key_ref = ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 goto error;
David Howells29db9192005-10-30 15:02:44 -0800334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
David Howells664cceb2005-09-28 17:03:15 +0100336 key_ref = ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (keyring->type != &key_type_keyring)
338 goto error;
339
David Howells664cceb2005-09-28 17:03:15 +0100340 rcu_read_lock();
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 now = current_kernel_time();
343 err = -EAGAIN;
344 sp = 0;
345
Kevin Coffmandceba992008-04-29 01:01:22 -0700346 /* firstly we should check to see if this top-level keyring is what we
347 * are looking for */
348 key_ref = ERR_PTR(-EAGAIN);
349 kflags = keyring->flags;
350 if (keyring->type == type && match(keyring, description)) {
351 key = keyring;
David Howells78b72802011-03-11 17:57:23 +0000352 if (no_state_check)
353 goto found;
Kevin Coffmandceba992008-04-29 01:01:22 -0700354
355 /* check it isn't negative and hasn't expired or been
356 * revoked */
357 if (kflags & (1 << KEY_FLAG_REVOKED))
358 goto error_2;
359 if (key->expiry && now.tv_sec >= key->expiry)
360 goto error_2;
David Howellsfdd1b942011-03-07 15:06:09 +0000361 key_ref = ERR_PTR(key->type_data.reject_error);
Kevin Coffmandceba992008-04-29 01:01:22 -0700362 if (kflags & (1 << KEY_FLAG_NEGATIVE))
363 goto error_2;
364 goto found;
365 }
366
367 /* otherwise, the top keyring must not be revoked, expired, or
368 * negatively instantiated if we are to search it */
369 key_ref = ERR_PTR(-EAGAIN);
370 if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) ||
371 (keyring->expiry && now.tv_sec >= keyring->expiry))
372 goto error_2;
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* start processing a new keyring */
David Howells664cceb2005-09-28 17:03:15 +0100375descend:
David Howells76d8aea2005-06-23 22:00:49 -0700376 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 goto not_this_keyring;
378
David Howells76d8aea2005-06-23 22:00:49 -0700379 keylist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (!keylist)
381 goto not_this_keyring;
382
383 /* iterate through the keys in this keyring first */
384 for (kix = 0; kix < keylist->nkeys; kix++) {
385 key = keylist->keys[kix];
Kevin Coffmandceba992008-04-29 01:01:22 -0700386 kflags = key->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /* ignore keys not of this type */
389 if (key->type != type)
390 continue;
391
392 /* skip revoked keys and expired keys */
David Howells78b72802011-03-11 17:57:23 +0000393 if (!no_state_check) {
394 if (kflags & (1 << KEY_FLAG_REVOKED))
395 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
David Howells78b72802011-03-11 17:57:23 +0000397 if (key->expiry && now.tv_sec >= key->expiry)
398 continue;
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* keys that don't match */
402 if (!match(key, description))
403 continue;
404
405 /* key must have search permissions */
David Howells29db9192005-10-30 15:02:44 -0800406 if (key_task_permission(make_key_ref(key, possessed),
David Howellsd84f4f92008-11-14 10:39:23 +1100407 cred, KEY_SEARCH) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 continue;
409
David Howells78b72802011-03-11 17:57:23 +0000410 if (no_state_check)
411 goto found;
412
Kevin Coffmandceba992008-04-29 01:01:22 -0700413 /* we set a different error code if we pass a negative key */
414 if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
David Howellsfdd1b942011-03-07 15:06:09 +0000415 err = key->type_data.reject_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 continue;
417 }
418
419 goto found;
420 }
421
422 /* search through the keyrings nested in this one */
423 kix = 0;
David Howells664cceb2005-09-28 17:03:15 +0100424ascend:
David Howells76d8aea2005-06-23 22:00:49 -0700425 for (; kix < keylist->nkeys; kix++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 key = keylist->keys[kix];
427 if (key->type != &key_type_keyring)
David Howells76d8aea2005-06-23 22:00:49 -0700428 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* recursively search nested keyrings
431 * - only search keyrings for which we have search permission
432 */
433 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
David Howells76d8aea2005-06-23 22:00:49 -0700434 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
David Howells0f6ed7c2005-11-07 00:59:30 -0800436 if (key_task_permission(make_key_ref(key, possessed),
David Howellsd84f4f92008-11-14 10:39:23 +1100437 cred, KEY_SEARCH) < 0)
David Howells76d8aea2005-06-23 22:00:49 -0700438 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /* stack the current position */
David Howells76d8aea2005-06-23 22:00:49 -0700441 stack[sp].keylist = keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 stack[sp].kix = kix;
443 sp++;
444
445 /* begin again with the new keyring */
446 keyring = key;
447 goto descend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
450 /* the keyring we're looking at was disqualified or didn't contain a
451 * matching key */
David Howells664cceb2005-09-28 17:03:15 +0100452not_this_keyring:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (sp > 0) {
454 /* resume the processing of a keyring higher up in the tree */
455 sp--;
David Howells76d8aea2005-06-23 22:00:49 -0700456 keylist = stack[sp].keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 kix = stack[sp].kix + 1;
458 goto ascend;
459 }
460
David Howells664cceb2005-09-28 17:03:15 +0100461 key_ref = ERR_PTR(err);
462 goto error_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /* we found a viable match */
David Howells664cceb2005-09-28 17:03:15 +0100465found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 atomic_inc(&key->usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 key_check(key);
David Howells664cceb2005-09-28 17:03:15 +0100468 key_ref = make_key_ref(key, possessed);
469error_2:
David Howells76d8aea2005-06-23 22:00:49 -0700470 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100471error:
472 return key_ref;
David Howellsa8b17ed2011-01-20 16:38:27 +0000473}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
David Howells973c9f42011-01-20 16:38:33 +0000475/**
476 * keyring_search - Search the supplied keyring tree for a matching key
477 * @keyring: The root of the keyring tree to be searched.
478 * @type: The type of keyring we want to find.
479 * @description: The name of the keyring we want to find.
480 *
481 * As keyring_search_aux() above, but using the current task's credentials and
482 * type's default matching function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
David Howells664cceb2005-09-28 17:03:15 +0100484key_ref_t keyring_search(key_ref_t keyring,
485 struct key_type *type,
486 const char *description)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
David Howells3e301482005-06-23 22:00:56 -0700488 if (!type->match)
489 return ERR_PTR(-ENOKEY);
490
David Howellsd84f4f92008-11-14 10:39:23 +1100491 return keyring_search_aux(keyring, current->cred,
David Howells78b72802011-03-11 17:57:23 +0000492 type, description, type->match, false);
David Howellsa8b17ed2011-01-20 16:38:27 +0000493}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494EXPORT_SYMBOL(keyring_search);
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*
David Howells973c9f42011-01-20 16:38:33 +0000497 * Search the given keyring only (no recursion).
498 *
499 * The caller must guarantee that the keyring is a keyring and that the
500 * permission is granted to search the keyring as no check is made here.
501 *
502 * RCU is used to make it unnecessary to lock the keyring key list here.
503 *
504 * Returns a pointer to the found key with usage count incremented if
505 * successful and returns -ENOKEY if not found. Revoked keys and keys not
506 * providing the requested permission are skipped over.
507 *
508 * If successful, the possession indicator is propagated from the keyring ref
509 * to the returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
David Howells664cceb2005-09-28 17:03:15 +0100511key_ref_t __keyring_search_one(key_ref_t keyring_ref,
512 const struct key_type *ktype,
513 const char *description,
514 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 struct keyring_list *klist;
David Howells664cceb2005-09-28 17:03:15 +0100517 unsigned long possessed;
518 struct key *keyring, *key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 int loop;
520
David Howells664cceb2005-09-28 17:03:15 +0100521 keyring = key_ref_to_ptr(keyring_ref);
522 possessed = is_key_possessed(keyring_ref);
523
David Howells76d8aea2005-06-23 22:00:49 -0700524 rcu_read_lock();
525
526 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (klist) {
528 for (loop = 0; loop < klist->nkeys; loop++) {
529 key = klist->keys[loop];
530
531 if (key->type == ktype &&
David Howells3e301482005-06-23 22:00:56 -0700532 (!key->type->match ||
533 key->type->match(key, description)) &&
David Howells664cceb2005-09-28 17:03:15 +0100534 key_permission(make_key_ref(key, possessed),
David Howellsdb1d1d52005-12-01 00:51:18 -0800535 perm) == 0 &&
David Howells76d8aea2005-06-23 22:00:49 -0700536 !test_bit(KEY_FLAG_REVOKED, &key->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 )
538 goto found;
539 }
540 }
541
David Howells664cceb2005-09-28 17:03:15 +0100542 rcu_read_unlock();
543 return ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700545found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 atomic_inc(&key->usage);
David Howells76d8aea2005-06-23 22:00:49 -0700547 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100548 return make_key_ref(key, possessed);
David Howellsa8b17ed2011-01-20 16:38:27 +0000549}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/*
David Howells973c9f42011-01-20 16:38:33 +0000552 * Find a keyring with the specified name.
553 *
554 * All named keyrings in the current user namespace are searched, provided they
555 * grant Search permission directly to the caller (unless this check is
556 * skipped). Keyrings whose usage points have reached zero or who have been
557 * revoked are skipped.
558 *
559 * Returns a pointer to the keyring with the keyring's refcount having being
560 * incremented on success. -ENOKEY is returned if a key could not be found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 */
David Howells69664cf2008-04-29 01:01:31 -0700562struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct key *keyring;
565 int bucket;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (!name)
Toshiyuki Okajimacea7daa2010-04-30 14:32:13 +0100568 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 bucket = keyring_hash(name);
571
572 read_lock(&keyring_name_lock);
573
574 if (keyring_name_hash[bucket].next) {
575 /* search this hash bucket for a keyring with a matching name
576 * that's readable and that hasn't been revoked */
577 list_for_each_entry(keyring,
578 &keyring_name_hash[bucket],
579 type_data.link
580 ) {
Serge E. Hallyn2ea190d2009-02-26 18:27:55 -0600581 if (keyring->user->user_ns != current_user_ns())
582 continue;
583
David Howells76d8aea2005-06-23 22:00:49 -0700584 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 continue;
586
587 if (strcmp(keyring->description, name) != 0)
588 continue;
589
David Howells69664cf2008-04-29 01:01:31 -0700590 if (!skip_perm_check &&
591 key_permission(make_key_ref(keyring, 0),
David Howells0f6ed7c2005-11-07 00:59:30 -0800592 KEY_SEARCH) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 continue;
594
Toshiyuki Okajimacea7daa2010-04-30 14:32:13 +0100595 /* we've got a match but we might end up racing with
596 * key_cleanup() if the keyring is currently 'dead'
597 * (ie. it has a zero usage count) */
598 if (!atomic_inc_not_zero(&keyring->usage))
599 continue;
600 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602 }
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 keyring = ERR_PTR(-ENOKEY);
Toshiyuki Okajimacea7daa2010-04-30 14:32:13 +0100605out:
606 read_unlock(&keyring_name_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return keyring;
David Howellsa8b17ed2011-01-20 16:38:27 +0000608}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/*
David Howells973c9f42011-01-20 16:38:33 +0000611 * See if a cycle will will be created by inserting acyclic tree B in acyclic
612 * tree A at the topmost level (ie: as a direct child of A).
613 *
614 * Since we are adding B to A at the top level, checking for cycles should just
615 * be a matter of seeing if node A is somewhere in tree B.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
617static int keyring_detect_cycle(struct key *A, struct key *B)
618{
619 struct {
David Howells76d8aea2005-06-23 22:00:49 -0700620 struct keyring_list *keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int kix;
622 } stack[KEYRING_SEARCH_MAX_DEPTH];
623
624 struct keyring_list *keylist;
625 struct key *subtree, *key;
626 int sp, kix, ret;
627
David Howells76d8aea2005-06-23 22:00:49 -0700628 rcu_read_lock();
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 ret = -EDEADLK;
631 if (A == B)
David Howells76d8aea2005-06-23 22:00:49 -0700632 goto cycle_detected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 subtree = B;
635 sp = 0;
636
637 /* start processing a new keyring */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700638descend:
David Howells76d8aea2005-06-23 22:00:49 -0700639 if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto not_this_keyring;
641
David Howells76d8aea2005-06-23 22:00:49 -0700642 keylist = rcu_dereference(subtree->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!keylist)
644 goto not_this_keyring;
645 kix = 0;
646
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700647ascend:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* iterate through the remaining keys in this keyring */
649 for (; kix < keylist->nkeys; kix++) {
650 key = keylist->keys[kix];
651
652 if (key == A)
653 goto cycle_detected;
654
655 /* recursively check nested keyrings */
656 if (key->type == &key_type_keyring) {
657 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
658 goto too_deep;
659
660 /* stack the current position */
David Howells76d8aea2005-06-23 22:00:49 -0700661 stack[sp].keylist = keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 stack[sp].kix = kix;
663 sp++;
664
665 /* begin again with the new keyring */
666 subtree = key;
667 goto descend;
668 }
669 }
670
671 /* the keyring we're looking at was disqualified or didn't contain a
672 * matching key */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700673not_this_keyring:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (sp > 0) {
675 /* resume the checking of a keyring higher up in the tree */
676 sp--;
David Howells76d8aea2005-06-23 22:00:49 -0700677 keylist = stack[sp].keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 kix = stack[sp].kix + 1;
679 goto ascend;
680 }
681
682 ret = 0; /* no cycles detected */
683
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700684error:
David Howells76d8aea2005-06-23 22:00:49 -0700685 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return ret;
687
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700688too_deep:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 ret = -ELOOP;
David Howells76d8aea2005-06-23 22:00:49 -0700690 goto error;
691
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700692cycle_detected:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 ret = -EDEADLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto error;
David Howellsa8b17ed2011-01-20 16:38:27 +0000695}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
David Howells76d8aea2005-06-23 22:00:49 -0700697/*
David Howells973c9f42011-01-20 16:38:33 +0000698 * Dispose of a keyring list after the RCU grace period, freeing the unlinked
David Howellscab8eb52006-01-08 01:02:45 -0800699 * key
700 */
701static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
702{
703 struct keyring_list *klist =
704 container_of(rcu, struct keyring_list, rcu);
705
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700706 if (klist->delkey != USHRT_MAX)
David Howellsf70e2e02010-04-30 14:32:39 +0100707 key_put(klist->keys[klist->delkey]);
David Howellscab8eb52006-01-08 01:02:45 -0800708 kfree(klist);
David Howellsf70e2e02010-04-30 14:32:39 +0100709}
David Howellscab8eb52006-01-08 01:02:45 -0800710
David Howellscab8eb52006-01-08 01:02:45 -0800711/*
David Howells973c9f42011-01-20 16:38:33 +0000712 * Preallocate memory so that a key can be linked into to a keyring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
David Howellsf70e2e02010-04-30 14:32:39 +0100714int __key_link_begin(struct key *keyring, const struct key_type *type,
David Howellsceb73c12011-01-25 16:34:28 +0000715 const char *description, unsigned long *_prealloc)
David Howellsf70e2e02010-04-30 14:32:39 +0100716 __acquires(&keyring->sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct keyring_list *klist, *nklist;
David Howellsceb73c12011-01-25 16:34:28 +0000719 unsigned long prealloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 unsigned max;
721 size_t size;
David Howellscab8eb52006-01-08 01:02:45 -0800722 int loop, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
David Howellsf70e2e02010-04-30 14:32:39 +0100724 kenter("%d,%s,%s,", key_serial(keyring), type->name, description);
725
726 if (keyring->type != &key_type_keyring)
727 return -ENOTDIR;
728
729 down_write(&keyring->sem);
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 ret = -EKEYREVOKED;
David Howells76d8aea2005-06-23 22:00:49 -0700732 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
David Howellsf70e2e02010-04-30 14:32:39 +0100733 goto error_krsem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
David Howellsf70e2e02010-04-30 14:32:39 +0100735 /* serialise link/link calls to prevent parallel calls causing a cycle
736 * when linking two keyring in opposite orders */
737 if (type == &key_type_keyring)
David Howells553d6032010-04-30 14:32:28 +0100738 down_write(&keyring_serialise_link_sem);
739
David Howellsf70e2e02010-04-30 14:32:39 +0100740 klist = rcu_dereference_locked_keyring(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
David Howellscab8eb52006-01-08 01:02:45 -0800742 /* see if there's a matching key we can displace */
David Howellscab8eb52006-01-08 01:02:45 -0800743 if (klist && klist->nkeys > 0) {
David Howellscab8eb52006-01-08 01:02:45 -0800744 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
745 if (klist->keys[loop]->type == type &&
746 strcmp(klist->keys[loop]->description,
David Howellsf70e2e02010-04-30 14:32:39 +0100747 description) == 0
David Howellscab8eb52006-01-08 01:02:45 -0800748 ) {
David Howellsf70e2e02010-04-30 14:32:39 +0100749 /* found a match - we'll replace this one with
750 * the new key */
David Howellscab8eb52006-01-08 01:02:45 -0800751 size = sizeof(struct key *) * klist->maxkeys;
752 size += sizeof(*klist);
753 BUG_ON(size > PAGE_SIZE);
754
755 ret = -ENOMEM;
Eric Sesterhenn48ad5042006-12-06 20:33:47 -0800756 nklist = kmemdup(klist, size, GFP_KERNEL);
David Howellscab8eb52006-01-08 01:02:45 -0800757 if (!nklist)
David Howellsf70e2e02010-04-30 14:32:39 +0100758 goto error_sem;
David Howellscab8eb52006-01-08 01:02:45 -0800759
David Howellsf70e2e02010-04-30 14:32:39 +0100760 /* note replacement slot */
761 klist->delkey = nklist->delkey = loop;
David Howellsceb73c12011-01-25 16:34:28 +0000762 prealloc = (unsigned long)nklist;
David Howellscab8eb52006-01-08 01:02:45 -0800763 goto done;
764 }
765 }
766 }
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* check that we aren't going to overrun the user's quota */
769 ret = key_payload_reserve(keyring,
770 keyring->datalen + KEYQUOTA_LINK_BYTES);
771 if (ret < 0)
David Howellsf70e2e02010-04-30 14:32:39 +0100772 goto error_sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (klist && klist->nkeys < klist->maxkeys) {
David Howellsf70e2e02010-04-30 14:32:39 +0100775 /* there's sufficient slack space to append directly */
776 nklist = NULL;
David Howellsceb73c12011-01-25 16:34:28 +0000777 prealloc = KEY_LINK_FIXQUOTA;
Chihau Chau512ea3b2010-03-08 20:11:34 -0300778 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* grow the key list */
780 max = 4;
781 if (klist)
782 max += klist->maxkeys;
783
784 ret = -ENFILE;
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700785 if (max > USHRT_MAX - 1)
David Howellsf70e2e02010-04-30 14:32:39 +0100786 goto error_quota;
David Howellsa4014d82005-07-07 17:57:03 -0700787 size = sizeof(*klist) + sizeof(struct key *) * max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (size > PAGE_SIZE)
David Howellsf70e2e02010-04-30 14:32:39 +0100789 goto error_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 ret = -ENOMEM;
792 nklist = kmalloc(size, GFP_KERNEL);
793 if (!nklist)
David Howellsf70e2e02010-04-30 14:32:39 +0100794 goto error_quota;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
David Howellsf70e2e02010-04-30 14:32:39 +0100796 nklist->maxkeys = max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (klist) {
David Howellsf70e2e02010-04-30 14:32:39 +0100798 memcpy(nklist->keys, klist->keys,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 sizeof(struct key *) * klist->nkeys);
David Howellsf70e2e02010-04-30 14:32:39 +0100800 nklist->delkey = klist->nkeys;
801 nklist->nkeys = klist->nkeys + 1;
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700802 klist->delkey = USHRT_MAX;
David Howellsf70e2e02010-04-30 14:32:39 +0100803 } else {
804 nklist->nkeys = 1;
805 nklist->delkey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
808 /* add the key into the new space */
David Howellsf70e2e02010-04-30 14:32:39 +0100809 nklist->keys[nklist->delkey] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
David Howellsceb73c12011-01-25 16:34:28 +0000812 prealloc = (unsigned long)nklist | KEY_LINK_FIXQUOTA;
David Howellscab8eb52006-01-08 01:02:45 -0800813done:
David Howellsceb73c12011-01-25 16:34:28 +0000814 *_prealloc = prealloc;
David Howellsf70e2e02010-04-30 14:32:39 +0100815 kleave(" = 0");
816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
David Howellsf70e2e02010-04-30 14:32:39 +0100818error_quota:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* undo the quota changes */
820 key_payload_reserve(keyring,
821 keyring->datalen - KEYQUOTA_LINK_BYTES);
David Howellsf70e2e02010-04-30 14:32:39 +0100822error_sem:
823 if (type == &key_type_keyring)
824 up_write(&keyring_serialise_link_sem);
825error_krsem:
826 up_write(&keyring->sem);
827 kleave(" = %d", ret);
828 return ret;
829}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
David Howellsf70e2e02010-04-30 14:32:39 +0100831/*
David Howells973c9f42011-01-20 16:38:33 +0000832 * Check already instantiated keys aren't going to be a problem.
833 *
834 * The caller must have called __key_link_begin(). Don't need to call this for
835 * keys that were created since __key_link_begin() was called.
David Howellsf70e2e02010-04-30 14:32:39 +0100836 */
837int __key_link_check_live_key(struct key *keyring, struct key *key)
838{
839 if (key->type == &key_type_keyring)
840 /* check that we aren't going to create a cycle by linking one
841 * keyring to another */
842 return keyring_detect_cycle(keyring, key);
843 return 0;
844}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
David Howellsf70e2e02010-04-30 14:32:39 +0100846/*
David Howells973c9f42011-01-20 16:38:33 +0000847 * Link a key into to a keyring.
848 *
849 * Must be called with __key_link_begin() having being called. Discards any
850 * already extant link to matching key if there is one, so that each keyring
851 * holds at most one link to any given key of a particular type+description
852 * combination.
David Howellsf70e2e02010-04-30 14:32:39 +0100853 */
854void __key_link(struct key *keyring, struct key *key,
David Howellsceb73c12011-01-25 16:34:28 +0000855 unsigned long *_prealloc)
David Howellsf70e2e02010-04-30 14:32:39 +0100856{
857 struct keyring_list *klist, *nklist;
858
David Howellsceb73c12011-01-25 16:34:28 +0000859 nklist = (struct keyring_list *)(*_prealloc & ~KEY_LINK_FIXQUOTA);
860 *_prealloc = 0;
David Howellsf70e2e02010-04-30 14:32:39 +0100861
862 kenter("%d,%d,%p", keyring->serial, key->serial, nklist);
863
864 klist = rcu_dereference_protected(keyring->payload.subscriptions,
865 rwsem_is_locked(&keyring->sem));
866
867 atomic_inc(&key->usage);
868
869 /* there's a matching key we can displace or an empty slot in a newly
870 * allocated list we can fill */
871 if (nklist) {
872 kdebug("replace %hu/%hu/%hu",
873 nklist->delkey, nklist->nkeys, nklist->maxkeys);
874
875 nklist->keys[nklist->delkey] = key;
876
877 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
878
879 /* dispose of the old keyring list and, if there was one, the
880 * displaced key */
881 if (klist) {
882 kdebug("dispose %hu/%hu/%hu",
883 klist->delkey, klist->nkeys, klist->maxkeys);
884 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
885 }
886 } else {
887 /* there's sufficient slack space to append directly */
888 klist->keys[klist->nkeys] = key;
889 smp_wmb();
890 klist->nkeys++;
891 }
892}
893
894/*
David Howells973c9f42011-01-20 16:38:33 +0000895 * Finish linking a key into to a keyring.
896 *
897 * Must be called with __key_link_begin() having being called.
David Howellsf70e2e02010-04-30 14:32:39 +0100898 */
899void __key_link_end(struct key *keyring, struct key_type *type,
David Howellsceb73c12011-01-25 16:34:28 +0000900 unsigned long prealloc)
David Howellsf70e2e02010-04-30 14:32:39 +0100901 __releases(&keyring->sem)
902{
903 BUG_ON(type == NULL);
904 BUG_ON(type->name == NULL);
David Howellsceb73c12011-01-25 16:34:28 +0000905 kenter("%d,%s,%lx", keyring->serial, type->name, prealloc);
David Howellsf70e2e02010-04-30 14:32:39 +0100906
907 if (type == &key_type_keyring)
908 up_write(&keyring_serialise_link_sem);
909
910 if (prealloc) {
David Howellsceb73c12011-01-25 16:34:28 +0000911 if (prealloc & KEY_LINK_FIXQUOTA)
912 key_payload_reserve(keyring,
913 keyring->datalen -
914 KEYQUOTA_LINK_BYTES);
915 kfree((struct keyring_list *)(prealloc & ~KEY_LINK_FIXQUOTA));
David Howellsf70e2e02010-04-30 14:32:39 +0100916 }
917 up_write(&keyring->sem);
918}
919
David Howells973c9f42011-01-20 16:38:33 +0000920/**
921 * key_link - Link a key to a keyring
922 * @keyring: The keyring to make the link in.
923 * @key: The key to link to.
924 *
925 * Make a link in a keyring to a key, such that the keyring holds a reference
926 * on that key and the key can potentially be found by searching that keyring.
927 *
928 * This function will write-lock the keyring's semaphore and will consume some
929 * of the user's key data quota to hold the link.
930 *
931 * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
932 * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
933 * full, -EDQUOT if there is insufficient key data quota remaining to add
934 * another link or -ENOMEM if there's insufficient memory.
935 *
936 * It is assumed that the caller has checked that it is permitted for a link to
937 * be made (the keyring should have Write permission and the key Link
938 * permission).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
940int key_link(struct key *keyring, struct key *key)
941{
David Howellsceb73c12011-01-25 16:34:28 +0000942 unsigned long prealloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 int ret;
944
945 key_check(keyring);
946 key_check(key);
947
David Howellsf70e2e02010-04-30 14:32:39 +0100948 ret = __key_link_begin(keyring, key->type, key->description, &prealloc);
949 if (ret == 0) {
950 ret = __key_link_check_live_key(keyring, key);
951 if (ret == 0)
952 __key_link(keyring, key, &prealloc);
953 __key_link_end(keyring, key->type, prealloc);
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 return ret;
David Howellsf70e2e02010-04-30 14:32:39 +0100957}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958EXPORT_SYMBOL(key_link);
959
David Howells973c9f42011-01-20 16:38:33 +0000960/**
961 * key_unlink - Unlink the first link to a key from a keyring.
962 * @keyring: The keyring to remove the link from.
963 * @key: The key the link is to.
964 *
965 * Remove a link from a keyring to a key.
966 *
967 * This function will write-lock the keyring's semaphore.
968 *
969 * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
970 * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
971 * memory.
972 *
973 * It is assumed that the caller has checked that it is permitted for a link to
974 * be removed (the keyring should have Write permission; no permissions are
975 * required on the key).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 */
977int key_unlink(struct key *keyring, struct key *key)
978{
David Howells76d8aea2005-06-23 22:00:49 -0700979 struct keyring_list *klist, *nklist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int loop, ret;
981
982 key_check(keyring);
983 key_check(key);
984
985 ret = -ENOTDIR;
986 if (keyring->type != &key_type_keyring)
987 goto error;
988
989 down_write(&keyring->sem);
990
David Howellsf0641cb2010-04-30 14:32:18 +0100991 klist = rcu_dereference_locked_keyring(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (klist) {
993 /* search the keyring for the key */
994 for (loop = 0; loop < klist->nkeys; loop++)
995 if (klist->keys[loop] == key)
996 goto key_is_present;
997 }
998
999 up_write(&keyring->sem);
1000 ret = -ENOENT;
1001 goto error;
1002
David Howells76d8aea2005-06-23 22:00:49 -07001003key_is_present:
1004 /* we need to copy the key list for RCU purposes */
David Howellsa4014d82005-07-07 17:57:03 -07001005 nklist = kmalloc(sizeof(*klist) +
1006 sizeof(struct key *) * klist->maxkeys,
David Howells76d8aea2005-06-23 22:00:49 -07001007 GFP_KERNEL);
1008 if (!nklist)
1009 goto nomem;
1010 nklist->maxkeys = klist->maxkeys;
1011 nklist->nkeys = klist->nkeys - 1;
1012
1013 if (loop > 0)
1014 memcpy(&nklist->keys[0],
1015 &klist->keys[0],
David Howellsa4014d82005-07-07 17:57:03 -07001016 loop * sizeof(struct key *));
David Howells76d8aea2005-06-23 22:00:49 -07001017
1018 if (loop < nklist->nkeys)
1019 memcpy(&nklist->keys[loop],
1020 &klist->keys[loop + 1],
David Howellsa4014d82005-07-07 17:57:03 -07001021 (nklist->nkeys - loop) * sizeof(struct key *));
David Howells76d8aea2005-06-23 22:00:49 -07001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /* adjust the user's quota */
1024 key_payload_reserve(keyring,
1025 keyring->datalen - KEYQUOTA_LINK_BYTES);
1026
David Howells76d8aea2005-06-23 22:00:49 -07001027 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 up_write(&keyring->sem);
David Howells76d8aea2005-06-23 22:00:49 -07001030
1031 /* schedule for later cleanup */
1032 klist->delkey = loop;
1033 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 ret = 0;
1036
David Howells76d8aea2005-06-23 22:00:49 -07001037error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return ret;
David Howells76d8aea2005-06-23 22:00:49 -07001039nomem:
1040 ret = -ENOMEM;
1041 up_write(&keyring->sem);
1042 goto error;
David Howellsa8b17ed2011-01-20 16:38:27 +00001043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044EXPORT_SYMBOL(key_unlink);
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046/*
David Howells973c9f42011-01-20 16:38:33 +00001047 * Dispose of a keyring list after the RCU grace period, releasing the keys it
1048 * links to.
David Howells76d8aea2005-06-23 22:00:49 -07001049 */
1050static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
1051{
1052 struct keyring_list *klist;
1053 int loop;
1054
1055 klist = container_of(rcu, struct keyring_list, rcu);
1056
1057 for (loop = klist->nkeys - 1; loop >= 0; loop--)
1058 key_put(klist->keys[loop]);
1059
1060 kfree(klist);
David Howellsa8b17ed2011-01-20 16:38:27 +00001061}
David Howells76d8aea2005-06-23 22:00:49 -07001062
David Howells973c9f42011-01-20 16:38:33 +00001063/**
1064 * keyring_clear - Clear a keyring
1065 * @keyring: The keyring to clear.
1066 *
1067 * Clear the contents of the specified keyring.
1068 *
1069 * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 */
1071int keyring_clear(struct key *keyring)
1072{
1073 struct keyring_list *klist;
David Howells76d8aea2005-06-23 22:00:49 -07001074 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 ret = -ENOTDIR;
1077 if (keyring->type == &key_type_keyring) {
1078 /* detach the pointer block with the locks held */
1079 down_write(&keyring->sem);
1080
David Howellsf0641cb2010-04-30 14:32:18 +01001081 klist = rcu_dereference_locked_keyring(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (klist) {
1083 /* adjust the quota */
1084 key_payload_reserve(keyring,
1085 sizeof(struct keyring_list));
1086
David Howells76d8aea2005-06-23 22:00:49 -07001087 rcu_assign_pointer(keyring->payload.subscriptions,
1088 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
1091 up_write(&keyring->sem);
1092
1093 /* free the keys after the locks have been dropped */
David Howells76d8aea2005-06-23 22:00:49 -07001094 if (klist)
1095 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 ret = 0;
1098 }
1099
1100 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102EXPORT_SYMBOL(keyring_clear);
David Howells31204ed2006-06-26 00:24:51 -07001103
David Howells31204ed2006-06-26 00:24:51 -07001104/*
David Howells973c9f42011-01-20 16:38:33 +00001105 * Dispose of the links from a revoked keyring.
1106 *
1107 * This is called with the key sem write-locked.
David Howells31204ed2006-06-26 00:24:51 -07001108 */
1109static void keyring_revoke(struct key *keyring)
1110{
David Howellsf0641cb2010-04-30 14:32:18 +01001111 struct keyring_list *klist;
1112
1113 klist = rcu_dereference_locked_keyring(keyring);
David Howells31204ed2006-06-26 00:24:51 -07001114
1115 /* adjust the quota */
1116 key_payload_reserve(keyring, 0);
1117
1118 if (klist) {
1119 rcu_assign_pointer(keyring->payload.subscriptions, NULL);
1120 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1121 }
David Howellsa8b17ed2011-01-20 16:38:27 +00001122}
David Howells5d135442009-09-02 09:14:00 +01001123
1124/*
David Howells973c9f42011-01-20 16:38:33 +00001125 * Determine whether a key is dead.
David Howells5d135442009-09-02 09:14:00 +01001126 */
1127static bool key_is_dead(struct key *key, time_t limit)
1128{
1129 return test_bit(KEY_FLAG_DEAD, &key->flags) ||
1130 (key->expiry > 0 && key->expiry <= limit);
1131}
1132
1133/*
David Howells973c9f42011-01-20 16:38:33 +00001134 * Collect garbage from the contents of a keyring, replacing the old list with
1135 * a new one with the pointers all shuffled down.
1136 *
1137 * Dead keys are classed as oned that are flagged as being dead or are revoked,
1138 * expired or negative keys that were revoked or expired before the specified
1139 * limit.
David Howells5d135442009-09-02 09:14:00 +01001140 */
1141void keyring_gc(struct key *keyring, time_t limit)
1142{
1143 struct keyring_list *klist, *new;
1144 struct key *key;
1145 int loop, keep, max;
1146
David Howellsc08ef802009-09-14 17:26:13 +01001147 kenter("{%x,%s}", key_serial(keyring), keyring->description);
David Howells5d135442009-09-02 09:14:00 +01001148
1149 down_write(&keyring->sem);
1150
David Howellsf0641cb2010-04-30 14:32:18 +01001151 klist = rcu_dereference_locked_keyring(keyring);
David Howells5d135442009-09-02 09:14:00 +01001152 if (!klist)
David Howellsc08ef802009-09-14 17:26:13 +01001153 goto no_klist;
David Howells5d135442009-09-02 09:14:00 +01001154
1155 /* work out how many subscriptions we're keeping */
1156 keep = 0;
1157 for (loop = klist->nkeys - 1; loop >= 0; loop--)
David Howellsc08ef802009-09-14 17:26:13 +01001158 if (!key_is_dead(klist->keys[loop], limit))
David Howells5d135442009-09-02 09:14:00 +01001159 keep++;
1160
1161 if (keep == klist->nkeys)
1162 goto just_return;
1163
1164 /* allocate a new keyring payload */
1165 max = roundup(keep, 4);
1166 new = kmalloc(sizeof(struct keyring_list) + max * sizeof(struct key *),
1167 GFP_KERNEL);
1168 if (!new)
David Howellsc08ef802009-09-14 17:26:13 +01001169 goto nomem;
David Howells5d135442009-09-02 09:14:00 +01001170 new->maxkeys = max;
1171 new->nkeys = 0;
1172 new->delkey = 0;
1173
1174 /* install the live keys
1175 * - must take care as expired keys may be updated back to life
1176 */
1177 keep = 0;
1178 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
1179 key = klist->keys[loop];
1180 if (!key_is_dead(key, limit)) {
1181 if (keep >= max)
1182 goto discard_new;
1183 new->keys[keep++] = key_get(key);
1184 }
1185 }
1186 new->nkeys = keep;
1187
1188 /* adjust the quota */
1189 key_payload_reserve(keyring,
1190 sizeof(struct keyring_list) +
1191 KEYQUOTA_LINK_BYTES * keep);
1192
1193 if (keep == 0) {
1194 rcu_assign_pointer(keyring->payload.subscriptions, NULL);
1195 kfree(new);
1196 } else {
1197 rcu_assign_pointer(keyring->payload.subscriptions, new);
1198 }
1199
1200 up_write(&keyring->sem);
1201
1202 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1203 kleave(" [yes]");
1204 return;
1205
1206discard_new:
1207 new->nkeys = keep;
1208 keyring_clear_rcu_disposal(&new->rcu);
David Howellsc08ef802009-09-14 17:26:13 +01001209 up_write(&keyring->sem);
1210 kleave(" [discard]");
1211 return;
1212
David Howells5d135442009-09-02 09:14:00 +01001213just_return:
1214 up_write(&keyring->sem);
David Howellsc08ef802009-09-14 17:26:13 +01001215 kleave(" [no dead]");
1216 return;
1217
1218no_klist:
1219 up_write(&keyring->sem);
1220 kleave(" [no_klist]");
1221 return;
1222
1223nomem:
1224 up_write(&keyring->sem);
1225 kleave(" [oom]");
David Howells5d135442009-09-02 09:14:00 +01001226}