blob: e814d2109f8ef170ea60043fa83b1f37c3e35de1 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/uaccess.h>
21#include "internal.h"
22
23/*
24 * when plumbing the depths of the key tree, this sets a hard limit set on how
25 * deep we're willing to go
26 */
27#define KEYRING_SEARCH_MAX_DEPTH 6
28
29/*
30 * we keep all named keyrings in a hash to speed looking them up
31 */
32#define KEYRING_NAME_HASH_SIZE (1 << 5)
33
34static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE];
35static DEFINE_RWLOCK(keyring_name_lock);
36
37static inline unsigned keyring_hash(const char *desc)
38{
39 unsigned bucket = 0;
40
41 for (; *desc; desc++)
42 bucket += (unsigned char) *desc;
43
44 return bucket & (KEYRING_NAME_HASH_SIZE - 1);
45}
46
47/*
48 * the keyring type definition
49 */
50static int keyring_instantiate(struct key *keyring,
51 const void *data, size_t datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int keyring_match(const struct key *keyring, const void *criterion);
David Howells31204ed2006-06-26 00:24:51 -070053static void keyring_revoke(struct key *keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static void keyring_destroy(struct key *keyring);
55static void keyring_describe(const struct key *keyring, struct seq_file *m);
56static long keyring_read(const struct key *keyring,
57 char __user *buffer, size_t buflen);
58
59struct key_type key_type_keyring = {
60 .name = "keyring",
61 .def_datalen = sizeof(struct keyring_list),
62 .instantiate = keyring_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .match = keyring_match,
David Howells31204ed2006-06-26 00:24:51 -070064 .revoke = keyring_revoke,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .destroy = keyring_destroy,
66 .describe = keyring_describe,
67 .read = keyring_read,
68};
69
David Howells73182262007-04-26 15:46:23 -070070EXPORT_SYMBOL(key_type_keyring);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
73 * semaphore to serialise link/link calls to prevent two link calls in parallel
74 * introducing a cycle
75 */
Adrian Bunk1ae8f402006-01-06 00:11:25 -080076static DECLARE_RWSEM(keyring_serialise_link_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*****************************************************************************/
79/*
80 * publish the name of a keyring so that it can be found by name (if it has
81 * one)
82 */
David Howells69664cf2008-04-29 01:01:31 -070083static void keyring_publish_name(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 int bucket;
86
87 if (keyring->description) {
88 bucket = keyring_hash(keyring->description);
89
90 write_lock(&keyring_name_lock);
91
92 if (!keyring_name_hash[bucket].next)
93 INIT_LIST_HEAD(&keyring_name_hash[bucket]);
94
95 list_add_tail(&keyring->type_data.link,
96 &keyring_name_hash[bucket]);
97
98 write_unlock(&keyring_name_lock);
99 }
100
101} /* end keyring_publish_name() */
102
103/*****************************************************************************/
104/*
105 * initialise a keyring
106 * - we object if we were given any data
107 */
108static int keyring_instantiate(struct key *keyring,
109 const void *data, size_t datalen)
110{
111 int ret;
112
113 ret = -EINVAL;
114 if (datalen == 0) {
115 /* make the keyring available by name if it has one */
116 keyring_publish_name(keyring);
117 ret = 0;
118 }
119
120 return ret;
121
122} /* end keyring_instantiate() */
123
124/*****************************************************************************/
125/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * match keyrings on their name
127 */
128static int keyring_match(const struct key *keyring, const void *description)
129{
130 return keyring->description &&
131 strcmp(keyring->description, description) == 0;
132
133} /* end keyring_match() */
134
135/*****************************************************************************/
136/*
137 * dispose of the data dangling from the corpse of a keyring
138 */
139static void keyring_destroy(struct key *keyring)
140{
141 struct keyring_list *klist;
142 int loop;
143
144 if (keyring->description) {
145 write_lock(&keyring_name_lock);
David Howells94efe722005-08-04 13:07:07 -0700146
147 if (keyring->type_data.link.next != NULL &&
148 !list_empty(&keyring->type_data.link))
149 list_del(&keyring->type_data.link);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 write_unlock(&keyring_name_lock);
152 }
153
Paul E. McKenneye7b0a612010-02-22 17:04:56 -0800154 klist = rcu_dereference_check(keyring->payload.subscriptions,
155 rcu_read_lock_held() ||
156 atomic_read(&keyring->usage) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (klist) {
158 for (loop = klist->nkeys - 1; loop >= 0; loop--)
159 key_put(klist->keys[loop]);
160 kfree(klist);
161 }
162
163} /* end keyring_destroy() */
164
165/*****************************************************************************/
166/*
167 * describe the keyring
168 */
169static void keyring_describe(const struct key *keyring, struct seq_file *m)
170{
171 struct keyring_list *klist;
172
173 if (keyring->description) {
174 seq_puts(m, keyring->description);
175 }
176 else {
177 seq_puts(m, "[anon]");
178 }
179
David Howells76d8aea2005-06-23 22:00:49 -0700180 rcu_read_lock();
181 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (klist)
183 seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
184 else
185 seq_puts(m, ": empty");
David Howells76d8aea2005-06-23 22:00:49 -0700186 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188} /* end keyring_describe() */
189
190/*****************************************************************************/
191/*
192 * read a list of key IDs from the keyring's contents
David Howells76d8aea2005-06-23 22:00:49 -0700193 * - the keyring's semaphore is read-locked
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 Howells76d8aea2005-06-23 22:00:49 -0700204 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (klist) {
207 /* calculate how much data we could return */
208 qty = klist->nkeys * sizeof(key_serial_t);
209
210 if (buffer && buflen > 0) {
211 if (buflen > qty)
212 buflen = qty;
213
214 /* copy the IDs of the subscribed keys into the
215 * buffer */
216 ret = -EFAULT;
217
218 for (loop = 0; loop < klist->nkeys; loop++) {
219 key = klist->keys[loop];
220
221 tmp = sizeof(key_serial_t);
222 if (tmp > buflen)
223 tmp = buflen;
224
225 if (copy_to_user(buffer,
226 &key->serial,
227 tmp) != 0)
228 goto error;
229
230 buflen -= tmp;
231 if (buflen == 0)
232 break;
233 buffer += tmp;
234 }
235 }
236
237 ret = qty;
238 }
239
240 error:
241 return ret;
242
243} /* end keyring_read() */
244
245/*****************************************************************************/
246/*
247 * allocate a keyring and link into the destination keyring
248 */
249struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
David Howellsd84f4f92008-11-14 10:39:23 +1100250 const struct cred *cred, unsigned long flags,
Michael LeMayd7200242006-06-22 14:47:17 -0700251 struct key *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 struct key *keyring;
254 int ret;
255
256 keyring = key_alloc(&key_type_keyring, description,
David Howellsd84f4f92008-11-14 10:39:23 +1100257 uid, gid, cred,
David Howells29db9192005-10-30 15:02:44 -0800258 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
David Howells7e047ef2006-06-26 00:24:50 -0700259 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 if (!IS_ERR(keyring)) {
David Howells3e301482005-06-23 22:00:56 -0700262 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (ret < 0) {
264 key_put(keyring);
265 keyring = ERR_PTR(ret);
266 }
267 }
268
269 return keyring;
270
271} /* end keyring_alloc() */
272
273/*****************************************************************************/
274/*
275 * search the supplied keyring tree for a key that matches the criterion
276 * - perform a breadth-then-depth search up to the prescribed limit
277 * - we only find keys on which we have search permission
278 * - we use the supplied match function to see if the description (or other
279 * feature of interest) matches
David Howells3e301482005-06-23 22:00:56 -0700280 * - we rely on RCU to prevent the keyring lists from disappearing on us
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * - we return -EAGAIN if we didn't find any matching key
282 * - we return -ENOKEY if we only found negative matching keys
David Howells664cceb2005-09-28 17:03:15 +0100283 * - we propagate the possession attribute from the keyring ref to the key ref
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
David Howells664cceb2005-09-28 17:03:15 +0100285key_ref_t keyring_search_aux(key_ref_t keyring_ref,
David Howellsd84f4f92008-11-14 10:39:23 +1100286 const struct cred *cred,
David Howells664cceb2005-09-28 17:03:15 +0100287 struct key_type *type,
288 const void *description,
289 key_match_func_t match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct {
David Howells76d8aea2005-06-23 22:00:49 -0700292 struct keyring_list *keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int kix;
294 } stack[KEYRING_SEARCH_MAX_DEPTH];
295
296 struct keyring_list *keylist;
297 struct timespec now;
Kevin Coffmandceba992008-04-29 01:01:22 -0700298 unsigned long possessed, kflags;
David Howells664cceb2005-09-28 17:03:15 +0100299 struct key *keyring, *key;
300 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 long err;
David Howells76d8aea2005-06-23 22:00:49 -0700302 int sp, kix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
David Howells664cceb2005-09-28 17:03:15 +0100304 keyring = key_ref_to_ptr(keyring_ref);
305 possessed = is_key_possessed(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 key_check(keyring);
307
308 /* top keyring must have search permission to begin the search */
David Howellsd84f4f92008-11-14 10:39:23 +1100309 err = key_task_permission(keyring_ref, cred, KEY_SEARCH);
David Howells29db9192005-10-30 15:02:44 -0800310 if (err < 0) {
311 key_ref = ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 goto error;
David Howells29db9192005-10-30 15:02:44 -0800313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
David Howells664cceb2005-09-28 17:03:15 +0100315 key_ref = ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (keyring->type != &key_type_keyring)
317 goto error;
318
David Howells664cceb2005-09-28 17:03:15 +0100319 rcu_read_lock();
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 now = current_kernel_time();
322 err = -EAGAIN;
323 sp = 0;
324
Kevin Coffmandceba992008-04-29 01:01:22 -0700325 /* firstly we should check to see if this top-level keyring is what we
326 * are looking for */
327 key_ref = ERR_PTR(-EAGAIN);
328 kflags = keyring->flags;
329 if (keyring->type == type && match(keyring, description)) {
330 key = keyring;
331
332 /* check it isn't negative and hasn't expired or been
333 * revoked */
334 if (kflags & (1 << KEY_FLAG_REVOKED))
335 goto error_2;
336 if (key->expiry && now.tv_sec >= key->expiry)
337 goto error_2;
338 key_ref = ERR_PTR(-ENOKEY);
339 if (kflags & (1 << KEY_FLAG_NEGATIVE))
340 goto error_2;
341 goto found;
342 }
343
344 /* otherwise, the top keyring must not be revoked, expired, or
345 * negatively instantiated if we are to search it */
346 key_ref = ERR_PTR(-EAGAIN);
347 if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) ||
348 (keyring->expiry && now.tv_sec >= keyring->expiry))
349 goto error_2;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* start processing a new keyring */
David Howells664cceb2005-09-28 17:03:15 +0100352descend:
David Howells76d8aea2005-06-23 22:00:49 -0700353 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 goto not_this_keyring;
355
David Howells76d8aea2005-06-23 22:00:49 -0700356 keylist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!keylist)
358 goto not_this_keyring;
359
360 /* iterate through the keys in this keyring first */
361 for (kix = 0; kix < keylist->nkeys; kix++) {
362 key = keylist->keys[kix];
Kevin Coffmandceba992008-04-29 01:01:22 -0700363 kflags = key->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /* ignore keys not of this type */
366 if (key->type != type)
367 continue;
368
369 /* skip revoked keys and expired keys */
Kevin Coffmandceba992008-04-29 01:01:22 -0700370 if (kflags & (1 << KEY_FLAG_REVOKED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 continue;
372
373 if (key->expiry && now.tv_sec >= key->expiry)
374 continue;
375
376 /* keys that don't match */
377 if (!match(key, description))
378 continue;
379
380 /* key must have search permissions */
David Howells29db9192005-10-30 15:02:44 -0800381 if (key_task_permission(make_key_ref(key, possessed),
David Howellsd84f4f92008-11-14 10:39:23 +1100382 cred, KEY_SEARCH) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 continue;
384
Kevin Coffmandceba992008-04-29 01:01:22 -0700385 /* we set a different error code if we pass a negative key */
386 if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 err = -ENOKEY;
388 continue;
389 }
390
391 goto found;
392 }
393
394 /* search through the keyrings nested in this one */
395 kix = 0;
David Howells664cceb2005-09-28 17:03:15 +0100396ascend:
David Howells76d8aea2005-06-23 22:00:49 -0700397 for (; kix < keylist->nkeys; kix++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 key = keylist->keys[kix];
399 if (key->type != &key_type_keyring)
David Howells76d8aea2005-06-23 22:00:49 -0700400 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /* recursively search nested keyrings
403 * - only search keyrings for which we have search permission
404 */
405 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
David Howells76d8aea2005-06-23 22:00:49 -0700406 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
David Howells0f6ed7c2005-11-07 00:59:30 -0800408 if (key_task_permission(make_key_ref(key, possessed),
David Howellsd84f4f92008-11-14 10:39:23 +1100409 cred, KEY_SEARCH) < 0)
David Howells76d8aea2005-06-23 22:00:49 -0700410 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* stack the current position */
David Howells76d8aea2005-06-23 22:00:49 -0700413 stack[sp].keylist = keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 stack[sp].kix = kix;
415 sp++;
416
417 /* begin again with the new keyring */
418 keyring = key;
419 goto descend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 /* the keyring we're looking at was disqualified or didn't contain a
423 * matching key */
David Howells664cceb2005-09-28 17:03:15 +0100424not_this_keyring:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (sp > 0) {
426 /* resume the processing of a keyring higher up in the tree */
427 sp--;
David Howells76d8aea2005-06-23 22:00:49 -0700428 keylist = stack[sp].keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 kix = stack[sp].kix + 1;
430 goto ascend;
431 }
432
David Howells664cceb2005-09-28 17:03:15 +0100433 key_ref = ERR_PTR(err);
434 goto error_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /* we found a viable match */
David Howells664cceb2005-09-28 17:03:15 +0100437found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 atomic_inc(&key->usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 key_check(key);
David Howells664cceb2005-09-28 17:03:15 +0100440 key_ref = make_key_ref(key, possessed);
441error_2:
David Howells76d8aea2005-06-23 22:00:49 -0700442 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100443error:
444 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446} /* end keyring_search_aux() */
447
448/*****************************************************************************/
449/*
450 * search the supplied keyring tree for a key that matches the criterion
451 * - perform a breadth-then-depth search up to the prescribed limit
452 * - we only find keys on which we have search permission
453 * - we readlock the keyrings as we search down the tree
454 * - we return -EAGAIN if we didn't find any matching key
455 * - we return -ENOKEY if we only found negative matching keys
456 */
David Howells664cceb2005-09-28 17:03:15 +0100457key_ref_t keyring_search(key_ref_t keyring,
458 struct key_type *type,
459 const char *description)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
David Howells3e301482005-06-23 22:00:56 -0700461 if (!type->match)
462 return ERR_PTR(-ENOKEY);
463
David Howellsd84f4f92008-11-14 10:39:23 +1100464 return keyring_search_aux(keyring, current->cred,
David Howells3e301482005-06-23 22:00:56 -0700465 type, description, type->match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467} /* end keyring_search() */
468
469EXPORT_SYMBOL(keyring_search);
470
471/*****************************************************************************/
472/*
473 * search the given keyring only (no recursion)
474 * - keyring must be locked by caller
David Howellsc3a9d652006-04-10 15:15:21 +0100475 * - caller must guarantee that the keyring is a keyring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 */
David Howells664cceb2005-09-28 17:03:15 +0100477key_ref_t __keyring_search_one(key_ref_t keyring_ref,
478 const struct key_type *ktype,
479 const char *description,
480 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct keyring_list *klist;
David Howells664cceb2005-09-28 17:03:15 +0100483 unsigned long possessed;
484 struct key *keyring, *key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int loop;
486
David Howells664cceb2005-09-28 17:03:15 +0100487 keyring = key_ref_to_ptr(keyring_ref);
488 possessed = is_key_possessed(keyring_ref);
489
David Howells76d8aea2005-06-23 22:00:49 -0700490 rcu_read_lock();
491
492 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (klist) {
494 for (loop = 0; loop < klist->nkeys; loop++) {
495 key = klist->keys[loop];
496
497 if (key->type == ktype &&
David Howells3e301482005-06-23 22:00:56 -0700498 (!key->type->match ||
499 key->type->match(key, description)) &&
David Howells664cceb2005-09-28 17:03:15 +0100500 key_permission(make_key_ref(key, possessed),
David Howellsdb1d1d52005-12-01 00:51:18 -0800501 perm) == 0 &&
David Howells76d8aea2005-06-23 22:00:49 -0700502 !test_bit(KEY_FLAG_REVOKED, &key->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 )
504 goto found;
505 }
506 }
507
David Howells664cceb2005-09-28 17:03:15 +0100508 rcu_read_unlock();
509 return ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 found:
512 atomic_inc(&key->usage);
David Howells76d8aea2005-06-23 22:00:49 -0700513 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100514 return make_key_ref(key, possessed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516} /* end __keyring_search_one() */
517
518/*****************************************************************************/
519/*
520 * find a keyring with the specified name
521 * - all named keyrings are searched
David Howells69664cf2008-04-29 01:01:31 -0700522 * - normally only finds keyrings with search permission for the current process
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
David Howells69664cf2008-04-29 01:01:31 -0700524struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 struct key *keyring;
527 int bucket;
528
529 keyring = ERR_PTR(-EINVAL);
530 if (!name)
531 goto error;
532
533 bucket = keyring_hash(name);
534
535 read_lock(&keyring_name_lock);
536
537 if (keyring_name_hash[bucket].next) {
538 /* search this hash bucket for a keyring with a matching name
539 * that's readable and that hasn't been revoked */
540 list_for_each_entry(keyring,
541 &keyring_name_hash[bucket],
542 type_data.link
543 ) {
Serge E. Hallyn2ea190d2009-02-26 18:27:55 -0600544 if (keyring->user->user_ns != current_user_ns())
545 continue;
546
David Howells76d8aea2005-06-23 22:00:49 -0700547 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 continue;
549
550 if (strcmp(keyring->description, name) != 0)
551 continue;
552
David Howells69664cf2008-04-29 01:01:31 -0700553 if (!skip_perm_check &&
554 key_permission(make_key_ref(keyring, 0),
David Howells0f6ed7c2005-11-07 00:59:30 -0800555 KEY_SEARCH) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 continue;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* we've got a match */
559 atomic_inc(&keyring->usage);
560 read_unlock(&keyring_name_lock);
561 goto error;
562 }
563 }
564
565 read_unlock(&keyring_name_lock);
566 keyring = ERR_PTR(-ENOKEY);
567
568 error:
569 return keyring;
570
571} /* end find_keyring_by_name() */
572
573/*****************************************************************************/
574/*
575 * see if a cycle will will be created by inserting acyclic tree B in acyclic
576 * tree A at the topmost level (ie: as a direct child of A)
577 * - since we are adding B to A at the top level, checking for cycles should
578 * just be a matter of seeing if node A is somewhere in tree B
579 */
580static int keyring_detect_cycle(struct key *A, struct key *B)
581{
582 struct {
David Howells76d8aea2005-06-23 22:00:49 -0700583 struct keyring_list *keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int kix;
585 } stack[KEYRING_SEARCH_MAX_DEPTH];
586
587 struct keyring_list *keylist;
588 struct key *subtree, *key;
589 int sp, kix, ret;
590
David Howells76d8aea2005-06-23 22:00:49 -0700591 rcu_read_lock();
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 ret = -EDEADLK;
594 if (A == B)
David Howells76d8aea2005-06-23 22:00:49 -0700595 goto cycle_detected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 subtree = B;
598 sp = 0;
599
600 /* start processing a new keyring */
601 descend:
David Howells76d8aea2005-06-23 22:00:49 -0700602 if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 goto not_this_keyring;
604
David Howells76d8aea2005-06-23 22:00:49 -0700605 keylist = rcu_dereference(subtree->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (!keylist)
607 goto not_this_keyring;
608 kix = 0;
609
610 ascend:
611 /* iterate through the remaining keys in this keyring */
612 for (; kix < keylist->nkeys; kix++) {
613 key = keylist->keys[kix];
614
615 if (key == A)
616 goto cycle_detected;
617
618 /* recursively check nested keyrings */
619 if (key->type == &key_type_keyring) {
620 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
621 goto too_deep;
622
623 /* stack the current position */
David Howells76d8aea2005-06-23 22:00:49 -0700624 stack[sp].keylist = keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 stack[sp].kix = kix;
626 sp++;
627
628 /* begin again with the new keyring */
629 subtree = key;
630 goto descend;
631 }
632 }
633
634 /* the keyring we're looking at was disqualified or didn't contain a
635 * matching key */
636 not_this_keyring:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (sp > 0) {
638 /* resume the checking of a keyring higher up in the tree */
639 sp--;
David Howells76d8aea2005-06-23 22:00:49 -0700640 keylist = stack[sp].keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 kix = stack[sp].kix + 1;
642 goto ascend;
643 }
644
645 ret = 0; /* no cycles detected */
646
647 error:
David Howells76d8aea2005-06-23 22:00:49 -0700648 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return ret;
650
651 too_deep:
652 ret = -ELOOP;
David Howells76d8aea2005-06-23 22:00:49 -0700653 goto error;
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 cycle_detected:
656 ret = -EDEADLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto error;
658
659} /* end keyring_detect_cycle() */
660
661/*****************************************************************************/
662/*
David Howells76d8aea2005-06-23 22:00:49 -0700663 * dispose of a keyring list after the RCU grace period
664 */
665static void keyring_link_rcu_disposal(struct rcu_head *rcu)
666{
667 struct keyring_list *klist =
668 container_of(rcu, struct keyring_list, rcu);
669
670 kfree(klist);
671
672} /* end keyring_link_rcu_disposal() */
673
674/*****************************************************************************/
675/*
David Howellscab8eb52006-01-08 01:02:45 -0800676 * dispose of a keyring list after the RCU grace period, freeing the unlinked
677 * key
678 */
679static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
680{
681 struct keyring_list *klist =
682 container_of(rcu, struct keyring_list, rcu);
683
684 key_put(klist->keys[klist->delkey]);
685 kfree(klist);
686
687} /* end keyring_unlink_rcu_disposal() */
688
689/*****************************************************************************/
690/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * link a key into to a keyring
David Howells76d8aea2005-06-23 22:00:49 -0700692 * - must be called with the keyring's semaphore write-locked
David Howellscab8eb52006-01-08 01:02:45 -0800693 * - discard already extant link to matching key if there is one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
695int __key_link(struct key *keyring, struct key *key)
696{
697 struct keyring_list *klist, *nklist;
698 unsigned max;
699 size_t size;
David Howellscab8eb52006-01-08 01:02:45 -0800700 int loop, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 ret = -EKEYREVOKED;
David Howells76d8aea2005-06-23 22:00:49 -0700703 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 goto error;
705
706 ret = -ENOTDIR;
707 if (keyring->type != &key_type_keyring)
708 goto error;
709
710 /* serialise link/link calls to prevent parallel calls causing a
711 * cycle when applied to two keyring in opposite orders */
712 down_write(&keyring_serialise_link_sem);
713
714 /* check that we aren't going to create a cycle adding one keyring to
715 * another */
716 if (key->type == &key_type_keyring) {
717 ret = keyring_detect_cycle(keyring, key);
718 if (ret < 0)
719 goto error2;
720 }
721
David Howellscab8eb52006-01-08 01:02:45 -0800722 /* see if there's a matching key we can displace */
723 klist = keyring->payload.subscriptions;
724
725 if (klist && klist->nkeys > 0) {
726 struct key_type *type = key->type;
727
728 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
729 if (klist->keys[loop]->type == type &&
730 strcmp(klist->keys[loop]->description,
731 key->description) == 0
732 ) {
733 /* found a match - replace with new key */
734 size = sizeof(struct key *) * klist->maxkeys;
735 size += sizeof(*klist);
736 BUG_ON(size > PAGE_SIZE);
737
738 ret = -ENOMEM;
Eric Sesterhenn48ad5042006-12-06 20:33:47 -0800739 nklist = kmemdup(klist, size, GFP_KERNEL);
David Howellscab8eb52006-01-08 01:02:45 -0800740 if (!nklist)
741 goto error2;
742
David Howellscab8eb52006-01-08 01:02:45 -0800743 /* replace matched key */
744 atomic_inc(&key->usage);
745 nklist->keys[loop] = key;
746
747 rcu_assign_pointer(
748 keyring->payload.subscriptions,
749 nklist);
750
751 /* dispose of the old keyring list and the
752 * displaced key */
753 klist->delkey = loop;
754 call_rcu(&klist->rcu,
755 keyring_unlink_rcu_disposal);
756
757 goto done;
758 }
759 }
760 }
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* check that we aren't going to overrun the user's quota */
763 ret = key_payload_reserve(keyring,
764 keyring->datalen + KEYQUOTA_LINK_BYTES);
765 if (ret < 0)
766 goto error2;
767
768 klist = keyring->payload.subscriptions;
769
770 if (klist && klist->nkeys < klist->maxkeys) {
771 /* there's sufficient slack space to add directly */
772 atomic_inc(&key->usage);
773
David Howells76d8aea2005-06-23 22:00:49 -0700774 klist->keys[klist->nkeys] = key;
775 smp_wmb();
776 klist->nkeys++;
777 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779 else {
780 /* grow the key list */
781 max = 4;
782 if (klist)
783 max += klist->maxkeys;
784
785 ret = -ENFILE;
David Howells76d8aea2005-06-23 22:00:49 -0700786 if (max > 65535)
787 goto error3;
David Howellsa4014d82005-07-07 17:57:03 -0700788 size = sizeof(*klist) + sizeof(struct key *) * max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (size > PAGE_SIZE)
790 goto error3;
791
792 ret = -ENOMEM;
793 nklist = kmalloc(size, GFP_KERNEL);
794 if (!nklist)
795 goto error3;
796 nklist->maxkeys = max;
797 nklist->nkeys = 0;
798
799 if (klist) {
800 nklist->nkeys = klist->nkeys;
801 memcpy(nklist->keys,
802 klist->keys,
803 sizeof(struct key *) * klist->nkeys);
804 }
805
806 /* add the key into the new space */
807 atomic_inc(&key->usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 nklist->keys[nklist->nkeys++] = key;
David Howells76d8aea2005-06-23 22:00:49 -0700809
810 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* dispose of the old keyring list */
David Howells76d8aea2005-06-23 22:00:49 -0700813 if (klist)
814 call_rcu(&klist->rcu, keyring_link_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
David Howellscab8eb52006-01-08 01:02:45 -0800817done:
818 ret = 0;
819error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 up_write(&keyring_serialise_link_sem);
David Howellscab8eb52006-01-08 01:02:45 -0800821error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return ret;
823
David Howellscab8eb52006-01-08 01:02:45 -0800824error3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /* undo the quota changes */
826 key_payload_reserve(keyring,
827 keyring->datalen - KEYQUOTA_LINK_BYTES);
828 goto error2;
829
830} /* end __key_link() */
831
832/*****************************************************************************/
833/*
834 * link a key to a keyring
835 */
836int key_link(struct key *keyring, struct key *key)
837{
838 int ret;
839
840 key_check(keyring);
841 key_check(key);
842
843 down_write(&keyring->sem);
844 ret = __key_link(keyring, key);
845 up_write(&keyring->sem);
846
847 return ret;
848
849} /* end key_link() */
850
851EXPORT_SYMBOL(key_link);
852
853/*****************************************************************************/
854/*
855 * unlink the first link to a key from a keyring
856 */
857int key_unlink(struct key *keyring, struct key *key)
858{
David Howells76d8aea2005-06-23 22:00:49 -0700859 struct keyring_list *klist, *nklist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 int loop, ret;
861
862 key_check(keyring);
863 key_check(key);
864
865 ret = -ENOTDIR;
866 if (keyring->type != &key_type_keyring)
867 goto error;
868
869 down_write(&keyring->sem);
870
871 klist = keyring->payload.subscriptions;
872 if (klist) {
873 /* search the keyring for the key */
874 for (loop = 0; loop < klist->nkeys; loop++)
875 if (klist->keys[loop] == key)
876 goto key_is_present;
877 }
878
879 up_write(&keyring->sem);
880 ret = -ENOENT;
881 goto error;
882
David Howells76d8aea2005-06-23 22:00:49 -0700883key_is_present:
884 /* we need to copy the key list for RCU purposes */
David Howellsa4014d82005-07-07 17:57:03 -0700885 nklist = kmalloc(sizeof(*klist) +
886 sizeof(struct key *) * klist->maxkeys,
David Howells76d8aea2005-06-23 22:00:49 -0700887 GFP_KERNEL);
888 if (!nklist)
889 goto nomem;
890 nklist->maxkeys = klist->maxkeys;
891 nklist->nkeys = klist->nkeys - 1;
892
893 if (loop > 0)
894 memcpy(&nklist->keys[0],
895 &klist->keys[0],
David Howellsa4014d82005-07-07 17:57:03 -0700896 loop * sizeof(struct key *));
David Howells76d8aea2005-06-23 22:00:49 -0700897
898 if (loop < nklist->nkeys)
899 memcpy(&nklist->keys[loop],
900 &klist->keys[loop + 1],
David Howellsa4014d82005-07-07 17:57:03 -0700901 (nklist->nkeys - loop) * sizeof(struct key *));
David Howells76d8aea2005-06-23 22:00:49 -0700902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /* adjust the user's quota */
904 key_payload_reserve(keyring,
905 keyring->datalen - KEYQUOTA_LINK_BYTES);
906
David Howells76d8aea2005-06-23 22:00:49 -0700907 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 up_write(&keyring->sem);
David Howells76d8aea2005-06-23 22:00:49 -0700910
911 /* schedule for later cleanup */
912 klist->delkey = loop;
913 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 ret = 0;
916
David Howells76d8aea2005-06-23 22:00:49 -0700917error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return ret;
David Howells76d8aea2005-06-23 22:00:49 -0700919nomem:
920 ret = -ENOMEM;
921 up_write(&keyring->sem);
922 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924} /* end key_unlink() */
925
926EXPORT_SYMBOL(key_unlink);
927
928/*****************************************************************************/
929/*
David Howells76d8aea2005-06-23 22:00:49 -0700930 * dispose of a keyring list after the RCU grace period, releasing the keys it
931 * links to
932 */
933static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
934{
935 struct keyring_list *klist;
936 int loop;
937
938 klist = container_of(rcu, struct keyring_list, rcu);
939
940 for (loop = klist->nkeys - 1; loop >= 0; loop--)
941 key_put(klist->keys[loop]);
942
943 kfree(klist);
944
945} /* end keyring_clear_rcu_disposal() */
946
947/*****************************************************************************/
948/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * clear the specified process keyring
950 * - implements keyctl(KEYCTL_CLEAR)
951 */
952int keyring_clear(struct key *keyring)
953{
954 struct keyring_list *klist;
David Howells76d8aea2005-06-23 22:00:49 -0700955 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 ret = -ENOTDIR;
958 if (keyring->type == &key_type_keyring) {
959 /* detach the pointer block with the locks held */
960 down_write(&keyring->sem);
961
962 klist = keyring->payload.subscriptions;
963 if (klist) {
964 /* adjust the quota */
965 key_payload_reserve(keyring,
966 sizeof(struct keyring_list));
967
David Howells76d8aea2005-06-23 22:00:49 -0700968 rcu_assign_pointer(keyring->payload.subscriptions,
969 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971
972 up_write(&keyring->sem);
973
974 /* free the keys after the locks have been dropped */
David Howells76d8aea2005-06-23 22:00:49 -0700975 if (klist)
976 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 ret = 0;
979 }
980
981 return ret;
982
983} /* end keyring_clear() */
984
985EXPORT_SYMBOL(keyring_clear);
David Howells31204ed2006-06-26 00:24:51 -0700986
987/*****************************************************************************/
988/*
989 * dispose of the links from a revoked keyring
990 * - called with the key sem write-locked
991 */
992static void keyring_revoke(struct key *keyring)
993{
994 struct keyring_list *klist = keyring->payload.subscriptions;
995
996 /* adjust the quota */
997 key_payload_reserve(keyring, 0);
998
999 if (klist) {
1000 rcu_assign_pointer(keyring->payload.subscriptions, NULL);
1001 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1002 }
1003
1004} /* end keyring_revoke() */
David Howells5d135442009-09-02 09:14:00 +01001005
1006/*
1007 * Determine whether a key is dead
1008 */
1009static bool key_is_dead(struct key *key, time_t limit)
1010{
1011 return test_bit(KEY_FLAG_DEAD, &key->flags) ||
1012 (key->expiry > 0 && key->expiry <= limit);
1013}
1014
1015/*
1016 * Collect garbage from the contents of a keyring
1017 */
1018void keyring_gc(struct key *keyring, time_t limit)
1019{
1020 struct keyring_list *klist, *new;
1021 struct key *key;
1022 int loop, keep, max;
1023
David Howellsc08ef802009-09-14 17:26:13 +01001024 kenter("{%x,%s}", key_serial(keyring), keyring->description);
David Howells5d135442009-09-02 09:14:00 +01001025
1026 down_write(&keyring->sem);
1027
1028 klist = keyring->payload.subscriptions;
1029 if (!klist)
David Howellsc08ef802009-09-14 17:26:13 +01001030 goto no_klist;
David Howells5d135442009-09-02 09:14:00 +01001031
1032 /* work out how many subscriptions we're keeping */
1033 keep = 0;
1034 for (loop = klist->nkeys - 1; loop >= 0; loop--)
David Howellsc08ef802009-09-14 17:26:13 +01001035 if (!key_is_dead(klist->keys[loop], limit))
David Howells5d135442009-09-02 09:14:00 +01001036 keep++;
1037
1038 if (keep == klist->nkeys)
1039 goto just_return;
1040
1041 /* allocate a new keyring payload */
1042 max = roundup(keep, 4);
1043 new = kmalloc(sizeof(struct keyring_list) + max * sizeof(struct key *),
1044 GFP_KERNEL);
1045 if (!new)
David Howellsc08ef802009-09-14 17:26:13 +01001046 goto nomem;
David Howells5d135442009-09-02 09:14:00 +01001047 new->maxkeys = max;
1048 new->nkeys = 0;
1049 new->delkey = 0;
1050
1051 /* install the live keys
1052 * - must take care as expired keys may be updated back to life
1053 */
1054 keep = 0;
1055 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
1056 key = klist->keys[loop];
1057 if (!key_is_dead(key, limit)) {
1058 if (keep >= max)
1059 goto discard_new;
1060 new->keys[keep++] = key_get(key);
1061 }
1062 }
1063 new->nkeys = keep;
1064
1065 /* adjust the quota */
1066 key_payload_reserve(keyring,
1067 sizeof(struct keyring_list) +
1068 KEYQUOTA_LINK_BYTES * keep);
1069
1070 if (keep == 0) {
1071 rcu_assign_pointer(keyring->payload.subscriptions, NULL);
1072 kfree(new);
1073 } else {
1074 rcu_assign_pointer(keyring->payload.subscriptions, new);
1075 }
1076
1077 up_write(&keyring->sem);
1078
1079 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1080 kleave(" [yes]");
1081 return;
1082
1083discard_new:
1084 new->nkeys = keep;
1085 keyring_clear_rcu_disposal(&new->rcu);
David Howellsc08ef802009-09-14 17:26:13 +01001086 up_write(&keyring->sem);
1087 kleave(" [discard]");
1088 return;
1089
David Howells5d135442009-09-02 09:14:00 +01001090just_return:
1091 up_write(&keyring->sem);
David Howellsc08ef802009-09-14 17:26:13 +01001092 kleave(" [no dead]");
1093 return;
1094
1095no_klist:
1096 up_write(&keyring->sem);
1097 kleave(" [no_klist]");
1098 return;
1099
1100nomem:
1101 up_write(&keyring->sem);
1102 kleave(" [oom]");
David Howells5d135442009-09-02 09:14:00 +01001103}