blob: 70f0c313c888bd7dd88963c61b2c5f5258d454e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* keyring.c: keyring handling
2 *
David Howells3e301482005-06-23 22:00:56 -07003 * Copyright (C) 2004-5 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>
19#include <asm/uaccess.h>
20#include "internal.h"
21
22/*
23 * when plumbing the depths of the key tree, this sets a hard limit set on how
24 * deep we're willing to go
25 */
26#define KEYRING_SEARCH_MAX_DEPTH 6
27
28/*
29 * we keep all named keyrings in a hash to speed looking them up
30 */
31#define KEYRING_NAME_HASH_SIZE (1 << 5)
32
33static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE];
34static DEFINE_RWLOCK(keyring_name_lock);
35
36static inline unsigned keyring_hash(const char *desc)
37{
38 unsigned bucket = 0;
39
40 for (; *desc; desc++)
41 bucket += (unsigned char) *desc;
42
43 return bucket & (KEYRING_NAME_HASH_SIZE - 1);
44}
45
46/*
47 * the keyring type definition
48 */
49static int keyring_instantiate(struct key *keyring,
50 const void *data, size_t datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int keyring_match(const struct key *keyring, const void *criterion);
David Howells31204ed2006-06-26 00:24:51 -070052static void keyring_revoke(struct key *keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static void keyring_destroy(struct key *keyring);
54static void keyring_describe(const struct key *keyring, struct seq_file *m);
55static long keyring_read(const struct key *keyring,
56 char __user *buffer, size_t buflen);
57
58struct key_type key_type_keyring = {
59 .name = "keyring",
60 .def_datalen = sizeof(struct keyring_list),
61 .instantiate = keyring_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .match = keyring_match,
David Howells31204ed2006-06-26 00:24:51 -070063 .revoke = keyring_revoke,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .destroy = keyring_destroy,
65 .describe = keyring_describe,
66 .read = keyring_read,
67};
68
David Howells73182262007-04-26 15:46:23 -070069EXPORT_SYMBOL(key_type_keyring);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * semaphore to serialise link/link calls to prevent two link calls in parallel
73 * introducing a cycle
74 */
Adrian Bunk1ae8f402006-01-06 00:11:25 -080075static DECLARE_RWSEM(keyring_serialise_link_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*****************************************************************************/
78/*
79 * publish the name of a keyring so that it can be found by name (if it has
80 * one)
81 */
82void keyring_publish_name(struct key *keyring)
83{
84 int bucket;
85
86 if (keyring->description) {
87 bucket = keyring_hash(keyring->description);
88
89 write_lock(&keyring_name_lock);
90
91 if (!keyring_name_hash[bucket].next)
92 INIT_LIST_HEAD(&keyring_name_hash[bucket]);
93
94 list_add_tail(&keyring->type_data.link,
95 &keyring_name_hash[bucket]);
96
97 write_unlock(&keyring_name_lock);
98 }
99
100} /* end keyring_publish_name() */
101
102/*****************************************************************************/
103/*
104 * initialise a keyring
105 * - we object if we were given any data
106 */
107static int keyring_instantiate(struct key *keyring,
108 const void *data, size_t datalen)
109{
110 int ret;
111
112 ret = -EINVAL;
113 if (datalen == 0) {
114 /* make the keyring available by name if it has one */
115 keyring_publish_name(keyring);
116 ret = 0;
117 }
118
119 return ret;
120
121} /* end keyring_instantiate() */
122
123/*****************************************************************************/
124/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * match keyrings on their name
126 */
127static int keyring_match(const struct key *keyring, const void *description)
128{
129 return keyring->description &&
130 strcmp(keyring->description, description) == 0;
131
132} /* end keyring_match() */
133
134/*****************************************************************************/
135/*
136 * dispose of the data dangling from the corpse of a keyring
137 */
138static void keyring_destroy(struct key *keyring)
139{
140 struct keyring_list *klist;
141 int loop;
142
143 if (keyring->description) {
144 write_lock(&keyring_name_lock);
David Howells94efe722005-08-04 13:07:07 -0700145
146 if (keyring->type_data.link.next != NULL &&
147 !list_empty(&keyring->type_data.link))
148 list_del(&keyring->type_data.link);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 write_unlock(&keyring_name_lock);
151 }
152
David Howells76d8aea2005-06-23 22:00:49 -0700153 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (klist) {
155 for (loop = klist->nkeys - 1; loop >= 0; loop--)
156 key_put(klist->keys[loop]);
157 kfree(klist);
158 }
159
160} /* end keyring_destroy() */
161
162/*****************************************************************************/
163/*
164 * describe the keyring
165 */
166static void keyring_describe(const struct key *keyring, struct seq_file *m)
167{
168 struct keyring_list *klist;
169
170 if (keyring->description) {
171 seq_puts(m, keyring->description);
172 }
173 else {
174 seq_puts(m, "[anon]");
175 }
176
David Howells76d8aea2005-06-23 22:00:49 -0700177 rcu_read_lock();
178 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (klist)
180 seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
181 else
182 seq_puts(m, ": empty");
David Howells76d8aea2005-06-23 22:00:49 -0700183 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185} /* end keyring_describe() */
186
187/*****************************************************************************/
188/*
189 * read a list of key IDs from the keyring's contents
David Howells76d8aea2005-06-23 22:00:49 -0700190 * - the keyring's semaphore is read-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
192static long keyring_read(const struct key *keyring,
193 char __user *buffer, size_t buflen)
194{
195 struct keyring_list *klist;
196 struct key *key;
197 size_t qty, tmp;
198 int loop, ret;
199
200 ret = 0;
David Howells76d8aea2005-06-23 22:00:49 -0700201 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 if (klist) {
204 /* calculate how much data we could return */
205 qty = klist->nkeys * sizeof(key_serial_t);
206
207 if (buffer && buflen > 0) {
208 if (buflen > qty)
209 buflen = qty;
210
211 /* copy the IDs of the subscribed keys into the
212 * buffer */
213 ret = -EFAULT;
214
215 for (loop = 0; loop < klist->nkeys; loop++) {
216 key = klist->keys[loop];
217
218 tmp = sizeof(key_serial_t);
219 if (tmp > buflen)
220 tmp = buflen;
221
222 if (copy_to_user(buffer,
223 &key->serial,
224 tmp) != 0)
225 goto error;
226
227 buflen -= tmp;
228 if (buflen == 0)
229 break;
230 buffer += tmp;
231 }
232 }
233
234 ret = qty;
235 }
236
237 error:
238 return ret;
239
240} /* end keyring_read() */
241
242/*****************************************************************************/
243/*
244 * allocate a keyring and link into the destination keyring
245 */
246struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
David Howells7e047ef2006-06-26 00:24:50 -0700247 struct task_struct *ctx, 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,
Michael LeMayd7200242006-06-22 14:47:17 -0700254 uid, gid, ctx,
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;
267
268} /* end keyring_alloc() */
269
270/*****************************************************************************/
271/*
272 * search the supplied keyring tree for a key that matches the criterion
273 * - perform a breadth-then-depth search up to the prescribed limit
274 * - we only find keys on which we have search permission
275 * - we use the supplied match function to see if the description (or other
276 * feature of interest) matches
David Howells3e301482005-06-23 22:00:56 -0700277 * - we rely on RCU to prevent the keyring lists from disappearing on us
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * - we return -EAGAIN if we didn't find any matching key
279 * - we return -ENOKEY if we only found negative matching keys
David Howells664cceb2005-09-28 17:03:15 +0100280 * - we propagate the possession attribute from the keyring ref to the key ref
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
David Howells664cceb2005-09-28 17:03:15 +0100282key_ref_t keyring_search_aux(key_ref_t keyring_ref,
283 struct task_struct *context,
284 struct key_type *type,
285 const void *description,
286 key_match_func_t match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 struct {
David Howells76d8aea2005-06-23 22:00:49 -0700289 struct keyring_list *keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 int kix;
291 } stack[KEYRING_SEARCH_MAX_DEPTH];
292
293 struct keyring_list *keylist;
294 struct timespec now;
Kevin Coffmandceba992008-04-29 01:01:22 -0700295 unsigned long possessed, kflags;
David Howells664cceb2005-09-28 17:03:15 +0100296 struct key *keyring, *key;
297 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 long err;
David Howells76d8aea2005-06-23 22:00:49 -0700299 int sp, kix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
David Howells664cceb2005-09-28 17:03:15 +0100301 keyring = key_ref_to_ptr(keyring_ref);
302 possessed = is_key_possessed(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 key_check(keyring);
304
305 /* top keyring must have search permission to begin the search */
David Howells29db9192005-10-30 15:02:44 -0800306 err = key_task_permission(keyring_ref, context, KEY_SEARCH);
307 if (err < 0) {
308 key_ref = ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto error;
David Howells29db9192005-10-30 15:02:44 -0800310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
David Howells664cceb2005-09-28 17:03:15 +0100312 key_ref = ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (keyring->type != &key_type_keyring)
314 goto error;
315
David Howells664cceb2005-09-28 17:03:15 +0100316 rcu_read_lock();
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 now = current_kernel_time();
319 err = -EAGAIN;
320 sp = 0;
321
Kevin Coffmandceba992008-04-29 01:01:22 -0700322 /* firstly we should check to see if this top-level keyring is what we
323 * are looking for */
324 key_ref = ERR_PTR(-EAGAIN);
325 kflags = keyring->flags;
326 if (keyring->type == type && match(keyring, description)) {
327 key = keyring;
328
329 /* check it isn't negative and hasn't expired or been
330 * revoked */
331 if (kflags & (1 << KEY_FLAG_REVOKED))
332 goto error_2;
333 if (key->expiry && now.tv_sec >= key->expiry)
334 goto error_2;
335 key_ref = ERR_PTR(-ENOKEY);
336 if (kflags & (1 << KEY_FLAG_NEGATIVE))
337 goto error_2;
338 goto found;
339 }
340
341 /* otherwise, the top keyring must not be revoked, expired, or
342 * negatively instantiated if we are to search it */
343 key_ref = ERR_PTR(-EAGAIN);
344 if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) ||
345 (keyring->expiry && now.tv_sec >= keyring->expiry))
346 goto error_2;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /* start processing a new keyring */
David Howells664cceb2005-09-28 17:03:15 +0100349descend:
David Howells76d8aea2005-06-23 22:00:49 -0700350 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 goto not_this_keyring;
352
David Howells76d8aea2005-06-23 22:00:49 -0700353 keylist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!keylist)
355 goto not_this_keyring;
356
357 /* iterate through the keys in this keyring first */
358 for (kix = 0; kix < keylist->nkeys; kix++) {
359 key = keylist->keys[kix];
Kevin Coffmandceba992008-04-29 01:01:22 -0700360 kflags = key->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /* ignore keys not of this type */
363 if (key->type != type)
364 continue;
365
366 /* skip revoked keys and expired keys */
Kevin Coffmandceba992008-04-29 01:01:22 -0700367 if (kflags & (1 << KEY_FLAG_REVOKED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 continue;
369
370 if (key->expiry && now.tv_sec >= key->expiry)
371 continue;
372
373 /* keys that don't match */
374 if (!match(key, description))
375 continue;
376
377 /* key must have search permissions */
David Howells29db9192005-10-30 15:02:44 -0800378 if (key_task_permission(make_key_ref(key, possessed),
379 context, KEY_SEARCH) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 continue;
381
Kevin Coffmandceba992008-04-29 01:01:22 -0700382 /* we set a different error code if we pass a negative key */
383 if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 err = -ENOKEY;
385 continue;
386 }
387
388 goto found;
389 }
390
391 /* search through the keyrings nested in this one */
392 kix = 0;
David Howells664cceb2005-09-28 17:03:15 +0100393ascend:
David Howells76d8aea2005-06-23 22:00:49 -0700394 for (; kix < keylist->nkeys; kix++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 key = keylist->keys[kix];
396 if (key->type != &key_type_keyring)
David Howells76d8aea2005-06-23 22:00:49 -0700397 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /* recursively search nested keyrings
400 * - only search keyrings for which we have search permission
401 */
402 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
David Howells76d8aea2005-06-23 22:00:49 -0700403 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
David Howells0f6ed7c2005-11-07 00:59:30 -0800405 if (key_task_permission(make_key_ref(key, possessed),
406 context, KEY_SEARCH) < 0)
David Howells76d8aea2005-06-23 22:00:49 -0700407 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* stack the current position */
David Howells76d8aea2005-06-23 22:00:49 -0700410 stack[sp].keylist = keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 stack[sp].kix = kix;
412 sp++;
413
414 /* begin again with the new keyring */
415 keyring = key;
416 goto descend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
419 /* the keyring we're looking at was disqualified or didn't contain a
420 * matching key */
David Howells664cceb2005-09-28 17:03:15 +0100421not_this_keyring:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (sp > 0) {
423 /* resume the processing of a keyring higher up in the tree */
424 sp--;
David Howells76d8aea2005-06-23 22:00:49 -0700425 keylist = stack[sp].keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 kix = stack[sp].kix + 1;
427 goto ascend;
428 }
429
David Howells664cceb2005-09-28 17:03:15 +0100430 key_ref = ERR_PTR(err);
431 goto error_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /* we found a viable match */
David Howells664cceb2005-09-28 17:03:15 +0100434found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 atomic_inc(&key->usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 key_check(key);
David Howells664cceb2005-09-28 17:03:15 +0100437 key_ref = make_key_ref(key, possessed);
438error_2:
David Howells76d8aea2005-06-23 22:00:49 -0700439 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100440error:
441 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443} /* end keyring_search_aux() */
444
445/*****************************************************************************/
446/*
447 * search the supplied keyring tree for a key that matches the criterion
448 * - perform a breadth-then-depth search up to the prescribed limit
449 * - we only find keys on which we have search permission
450 * - we readlock the keyrings as we search down the tree
451 * - we return -EAGAIN if we didn't find any matching key
452 * - we return -ENOKEY if we only found negative matching keys
453 */
David Howells664cceb2005-09-28 17:03:15 +0100454key_ref_t keyring_search(key_ref_t keyring,
455 struct key_type *type,
456 const char *description)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
David Howells3e301482005-06-23 22:00:56 -0700458 if (!type->match)
459 return ERR_PTR(-ENOKEY);
460
461 return keyring_search_aux(keyring, current,
462 type, description, type->match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464} /* end keyring_search() */
465
466EXPORT_SYMBOL(keyring_search);
467
468/*****************************************************************************/
469/*
470 * search the given keyring only (no recursion)
471 * - keyring must be locked by caller
David Howellsc3a9d652006-04-10 15:15:21 +0100472 * - caller must guarantee that the keyring is a keyring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
David Howells664cceb2005-09-28 17:03:15 +0100474key_ref_t __keyring_search_one(key_ref_t keyring_ref,
475 const struct key_type *ktype,
476 const char *description,
477 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct keyring_list *klist;
David Howells664cceb2005-09-28 17:03:15 +0100480 unsigned long possessed;
481 struct key *keyring, *key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int loop;
483
David Howells664cceb2005-09-28 17:03:15 +0100484 keyring = key_ref_to_ptr(keyring_ref);
485 possessed = is_key_possessed(keyring_ref);
486
David Howells76d8aea2005-06-23 22:00:49 -0700487 rcu_read_lock();
488
489 klist = rcu_dereference(keyring->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (klist) {
491 for (loop = 0; loop < klist->nkeys; loop++) {
492 key = klist->keys[loop];
493
494 if (key->type == ktype &&
David Howells3e301482005-06-23 22:00:56 -0700495 (!key->type->match ||
496 key->type->match(key, description)) &&
David Howells664cceb2005-09-28 17:03:15 +0100497 key_permission(make_key_ref(key, possessed),
David Howellsdb1d1d52005-12-01 00:51:18 -0800498 perm) == 0 &&
David Howells76d8aea2005-06-23 22:00:49 -0700499 !test_bit(KEY_FLAG_REVOKED, &key->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 )
501 goto found;
502 }
503 }
504
David Howells664cceb2005-09-28 17:03:15 +0100505 rcu_read_unlock();
506 return ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 found:
509 atomic_inc(&key->usage);
David Howells76d8aea2005-06-23 22:00:49 -0700510 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100511 return make_key_ref(key, possessed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513} /* end __keyring_search_one() */
514
515/*****************************************************************************/
516/*
517 * find a keyring with the specified name
518 * - all named keyrings are searched
519 * - only find keyrings with search permission for the process
520 * - only find keyrings with a serial number greater than the one specified
521 */
522struct key *find_keyring_by_name(const char *name, key_serial_t bound)
523{
524 struct key *keyring;
525 int bucket;
526
527 keyring = ERR_PTR(-EINVAL);
528 if (!name)
529 goto error;
530
531 bucket = keyring_hash(name);
532
533 read_lock(&keyring_name_lock);
534
535 if (keyring_name_hash[bucket].next) {
536 /* search this hash bucket for a keyring with a matching name
537 * that's readable and that hasn't been revoked */
538 list_for_each_entry(keyring,
539 &keyring_name_hash[bucket],
540 type_data.link
541 ) {
David Howells76d8aea2005-06-23 22:00:49 -0700542 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 continue;
544
545 if (strcmp(keyring->description, name) != 0)
546 continue;
547
David Howells0f6ed7c2005-11-07 00:59:30 -0800548 if (key_permission(make_key_ref(keyring, 0),
549 KEY_SEARCH) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 continue;
551
552 /* found a potential candidate, but we still need to
553 * check the serial number */
554 if (keyring->serial <= bound)
555 continue;
556
557 /* we've got a match */
558 atomic_inc(&keyring->usage);
559 read_unlock(&keyring_name_lock);
560 goto error;
561 }
562 }
563
564 read_unlock(&keyring_name_lock);
565 keyring = ERR_PTR(-ENOKEY);
566
567 error:
568 return keyring;
569
570} /* end find_keyring_by_name() */
571
572/*****************************************************************************/
573/*
574 * see if a cycle will will be created by inserting acyclic tree B in acyclic
575 * tree A at the topmost level (ie: as a direct child of A)
576 * - since we are adding B to A at the top level, checking for cycles should
577 * just be a matter of seeing if node A is somewhere in tree B
578 */
579static int keyring_detect_cycle(struct key *A, struct key *B)
580{
581 struct {
David Howells76d8aea2005-06-23 22:00:49 -0700582 struct keyring_list *keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 int kix;
584 } stack[KEYRING_SEARCH_MAX_DEPTH];
585
586 struct keyring_list *keylist;
587 struct key *subtree, *key;
588 int sp, kix, ret;
589
David Howells76d8aea2005-06-23 22:00:49 -0700590 rcu_read_lock();
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 ret = -EDEADLK;
593 if (A == B)
David Howells76d8aea2005-06-23 22:00:49 -0700594 goto cycle_detected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 subtree = B;
597 sp = 0;
598
599 /* start processing a new keyring */
600 descend:
David Howells76d8aea2005-06-23 22:00:49 -0700601 if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 goto not_this_keyring;
603
David Howells76d8aea2005-06-23 22:00:49 -0700604 keylist = rcu_dereference(subtree->payload.subscriptions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (!keylist)
606 goto not_this_keyring;
607 kix = 0;
608
609 ascend:
610 /* iterate through the remaining keys in this keyring */
611 for (; kix < keylist->nkeys; kix++) {
612 key = keylist->keys[kix];
613
614 if (key == A)
615 goto cycle_detected;
616
617 /* recursively check nested keyrings */
618 if (key->type == &key_type_keyring) {
619 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
620 goto too_deep;
621
622 /* stack the current position */
David Howells76d8aea2005-06-23 22:00:49 -0700623 stack[sp].keylist = keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 stack[sp].kix = kix;
625 sp++;
626
627 /* begin again with the new keyring */
628 subtree = key;
629 goto descend;
630 }
631 }
632
633 /* the keyring we're looking at was disqualified or didn't contain a
634 * matching key */
635 not_this_keyring:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (sp > 0) {
637 /* resume the checking of a keyring higher up in the tree */
638 sp--;
David Howells76d8aea2005-06-23 22:00:49 -0700639 keylist = stack[sp].keylist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 kix = stack[sp].kix + 1;
641 goto ascend;
642 }
643
644 ret = 0; /* no cycles detected */
645
646 error:
David Howells76d8aea2005-06-23 22:00:49 -0700647 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return ret;
649
650 too_deep:
651 ret = -ELOOP;
David Howells76d8aea2005-06-23 22:00:49 -0700652 goto error;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 cycle_detected:
655 ret = -EDEADLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 goto error;
657
658} /* end keyring_detect_cycle() */
659
660/*****************************************************************************/
661/*
David Howells76d8aea2005-06-23 22:00:49 -0700662 * dispose of a keyring list after the RCU grace period
663 */
664static void keyring_link_rcu_disposal(struct rcu_head *rcu)
665{
666 struct keyring_list *klist =
667 container_of(rcu, struct keyring_list, rcu);
668
669 kfree(klist);
670
671} /* end keyring_link_rcu_disposal() */
672
673/*****************************************************************************/
674/*
David Howellscab8eb52006-01-08 01:02:45 -0800675 * dispose of a keyring list after the RCU grace period, freeing the unlinked
676 * key
677 */
678static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
679{
680 struct keyring_list *klist =
681 container_of(rcu, struct keyring_list, rcu);
682
683 key_put(klist->keys[klist->delkey]);
684 kfree(klist);
685
686} /* end keyring_unlink_rcu_disposal() */
687
688/*****************************************************************************/
689/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 * link a key into to a keyring
David Howells76d8aea2005-06-23 22:00:49 -0700691 * - must be called with the keyring's semaphore write-locked
David Howellscab8eb52006-01-08 01:02:45 -0800692 * - discard already extant link to matching key if there is one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
694int __key_link(struct key *keyring, struct key *key)
695{
696 struct keyring_list *klist, *nklist;
697 unsigned max;
698 size_t size;
David Howellscab8eb52006-01-08 01:02:45 -0800699 int loop, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 ret = -EKEYREVOKED;
David Howells76d8aea2005-06-23 22:00:49 -0700702 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goto error;
704
705 ret = -ENOTDIR;
706 if (keyring->type != &key_type_keyring)
707 goto error;
708
709 /* serialise link/link calls to prevent parallel calls causing a
710 * cycle when applied to two keyring in opposite orders */
711 down_write(&keyring_serialise_link_sem);
712
713 /* check that we aren't going to create a cycle adding one keyring to
714 * another */
715 if (key->type == &key_type_keyring) {
716 ret = keyring_detect_cycle(keyring, key);
717 if (ret < 0)
718 goto error2;
719 }
720
David Howellscab8eb52006-01-08 01:02:45 -0800721 /* see if there's a matching key we can displace */
722 klist = keyring->payload.subscriptions;
723
724 if (klist && klist->nkeys > 0) {
725 struct key_type *type = key->type;
726
727 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
728 if (klist->keys[loop]->type == type &&
729 strcmp(klist->keys[loop]->description,
730 key->description) == 0
731 ) {
732 /* found a match - replace with new key */
733 size = sizeof(struct key *) * klist->maxkeys;
734 size += sizeof(*klist);
735 BUG_ON(size > PAGE_SIZE);
736
737 ret = -ENOMEM;
Eric Sesterhenn48ad5042006-12-06 20:33:47 -0800738 nklist = kmemdup(klist, size, GFP_KERNEL);
David Howellscab8eb52006-01-08 01:02:45 -0800739 if (!nklist)
740 goto error2;
741
David Howellscab8eb52006-01-08 01:02:45 -0800742 /* replace matched key */
743 atomic_inc(&key->usage);
744 nklist->keys[loop] = key;
745
746 rcu_assign_pointer(
747 keyring->payload.subscriptions,
748 nklist);
749
750 /* dispose of the old keyring list and the
751 * displaced key */
752 klist->delkey = loop;
753 call_rcu(&klist->rcu,
754 keyring_unlink_rcu_disposal);
755
756 goto done;
757 }
758 }
759 }
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* check that we aren't going to overrun the user's quota */
762 ret = key_payload_reserve(keyring,
763 keyring->datalen + KEYQUOTA_LINK_BYTES);
764 if (ret < 0)
765 goto error2;
766
767 klist = keyring->payload.subscriptions;
768
769 if (klist && klist->nkeys < klist->maxkeys) {
770 /* there's sufficient slack space to add directly */
771 atomic_inc(&key->usage);
772
David Howells76d8aea2005-06-23 22:00:49 -0700773 klist->keys[klist->nkeys] = key;
774 smp_wmb();
775 klist->nkeys++;
776 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778 else {
779 /* grow the key list */
780 max = 4;
781 if (klist)
782 max += klist->maxkeys;
783
784 ret = -ENFILE;
David Howells76d8aea2005-06-23 22:00:49 -0700785 if (max > 65535)
786 goto error3;
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)
789 goto error3;
790
791 ret = -ENOMEM;
792 nklist = kmalloc(size, GFP_KERNEL);
793 if (!nklist)
794 goto error3;
795 nklist->maxkeys = max;
796 nklist->nkeys = 0;
797
798 if (klist) {
799 nklist->nkeys = klist->nkeys;
800 memcpy(nklist->keys,
801 klist->keys,
802 sizeof(struct key *) * klist->nkeys);
803 }
804
805 /* add the key into the new space */
806 atomic_inc(&key->usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 nklist->keys[nklist->nkeys++] = key;
David Howells76d8aea2005-06-23 22:00:49 -0700808
809 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* dispose of the old keyring list */
David Howells76d8aea2005-06-23 22:00:49 -0700812 if (klist)
813 call_rcu(&klist->rcu, keyring_link_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
David Howellscab8eb52006-01-08 01:02:45 -0800816done:
817 ret = 0;
818error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 up_write(&keyring_serialise_link_sem);
David Howellscab8eb52006-01-08 01:02:45 -0800820error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return ret;
822
David Howellscab8eb52006-01-08 01:02:45 -0800823error3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /* undo the quota changes */
825 key_payload_reserve(keyring,
826 keyring->datalen - KEYQUOTA_LINK_BYTES);
827 goto error2;
828
829} /* end __key_link() */
830
831/*****************************************************************************/
832/*
833 * link a key to a keyring
834 */
835int key_link(struct key *keyring, struct key *key)
836{
837 int ret;
838
839 key_check(keyring);
840 key_check(key);
841
842 down_write(&keyring->sem);
843 ret = __key_link(keyring, key);
844 up_write(&keyring->sem);
845
846 return ret;
847
848} /* end key_link() */
849
850EXPORT_SYMBOL(key_link);
851
852/*****************************************************************************/
853/*
854 * unlink the first link to a key from a keyring
855 */
856int key_unlink(struct key *keyring, struct key *key)
857{
David Howells76d8aea2005-06-23 22:00:49 -0700858 struct keyring_list *klist, *nklist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 int loop, ret;
860
861 key_check(keyring);
862 key_check(key);
863
864 ret = -ENOTDIR;
865 if (keyring->type != &key_type_keyring)
866 goto error;
867
868 down_write(&keyring->sem);
869
870 klist = keyring->payload.subscriptions;
871 if (klist) {
872 /* search the keyring for the key */
873 for (loop = 0; loop < klist->nkeys; loop++)
874 if (klist->keys[loop] == key)
875 goto key_is_present;
876 }
877
878 up_write(&keyring->sem);
879 ret = -ENOENT;
880 goto error;
881
David Howells76d8aea2005-06-23 22:00:49 -0700882key_is_present:
883 /* we need to copy the key list for RCU purposes */
David Howellsa4014d82005-07-07 17:57:03 -0700884 nklist = kmalloc(sizeof(*klist) +
885 sizeof(struct key *) * klist->maxkeys,
David Howells76d8aea2005-06-23 22:00:49 -0700886 GFP_KERNEL);
887 if (!nklist)
888 goto nomem;
889 nklist->maxkeys = klist->maxkeys;
890 nklist->nkeys = klist->nkeys - 1;
891
892 if (loop > 0)
893 memcpy(&nklist->keys[0],
894 &klist->keys[0],
David Howellsa4014d82005-07-07 17:57:03 -0700895 loop * sizeof(struct key *));
David Howells76d8aea2005-06-23 22:00:49 -0700896
897 if (loop < nklist->nkeys)
898 memcpy(&nklist->keys[loop],
899 &klist->keys[loop + 1],
David Howellsa4014d82005-07-07 17:57:03 -0700900 (nklist->nkeys - loop) * sizeof(struct key *));
David Howells76d8aea2005-06-23 22:00:49 -0700901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* adjust the user's quota */
903 key_payload_reserve(keyring,
904 keyring->datalen - KEYQUOTA_LINK_BYTES);
905
David Howells76d8aea2005-06-23 22:00:49 -0700906 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 up_write(&keyring->sem);
David Howells76d8aea2005-06-23 22:00:49 -0700909
910 /* schedule for later cleanup */
911 klist->delkey = loop;
912 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 ret = 0;
915
David Howells76d8aea2005-06-23 22:00:49 -0700916error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return ret;
David Howells76d8aea2005-06-23 22:00:49 -0700918nomem:
919 ret = -ENOMEM;
920 up_write(&keyring->sem);
921 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923} /* end key_unlink() */
924
925EXPORT_SYMBOL(key_unlink);
926
927/*****************************************************************************/
928/*
David Howells76d8aea2005-06-23 22:00:49 -0700929 * dispose of a keyring list after the RCU grace period, releasing the keys it
930 * links to
931 */
932static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
933{
934 struct keyring_list *klist;
935 int loop;
936
937 klist = container_of(rcu, struct keyring_list, rcu);
938
939 for (loop = klist->nkeys - 1; loop >= 0; loop--)
940 key_put(klist->keys[loop]);
941
942 kfree(klist);
943
944} /* end keyring_clear_rcu_disposal() */
945
946/*****************************************************************************/
947/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * clear the specified process keyring
949 * - implements keyctl(KEYCTL_CLEAR)
950 */
951int keyring_clear(struct key *keyring)
952{
953 struct keyring_list *klist;
David Howells76d8aea2005-06-23 22:00:49 -0700954 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 ret = -ENOTDIR;
957 if (keyring->type == &key_type_keyring) {
958 /* detach the pointer block with the locks held */
959 down_write(&keyring->sem);
960
961 klist = keyring->payload.subscriptions;
962 if (klist) {
963 /* adjust the quota */
964 key_payload_reserve(keyring,
965 sizeof(struct keyring_list));
966
David Howells76d8aea2005-06-23 22:00:49 -0700967 rcu_assign_pointer(keyring->payload.subscriptions,
968 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970
971 up_write(&keyring->sem);
972
973 /* free the keys after the locks have been dropped */
David Howells76d8aea2005-06-23 22:00:49 -0700974 if (klist)
975 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 ret = 0;
978 }
979
980 return ret;
981
982} /* end keyring_clear() */
983
984EXPORT_SYMBOL(keyring_clear);
David Howells31204ed2006-06-26 00:24:51 -0700985
986/*****************************************************************************/
987/*
988 * dispose of the links from a revoked keyring
989 * - called with the key sem write-locked
990 */
991static void keyring_revoke(struct key *keyring)
992{
993 struct keyring_list *klist = keyring->payload.subscriptions;
994
995 /* adjust the quota */
996 key_payload_reserve(keyring, 0);
997
998 if (klist) {
999 rcu_assign_pointer(keyring->payload.subscriptions, NULL);
1000 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1001 }
1002
1003} /* end keyring_revoke() */