keyring.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /* keyring.c: keyring handling
  2. *
  3. * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
  4. * 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. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/err.h>
  17. #include <asm/uaccess.h>
  18. #include "internal.h"
  19. /*
  20. * when plumbing the depths of the key tree, this sets a hard limit set on how
  21. * deep we're willing to go
  22. */
  23. #define KEYRING_SEARCH_MAX_DEPTH 6
  24. /*
  25. * we keep all named keyrings in a hash to speed looking them up
  26. */
  27. #define KEYRING_NAME_HASH_SIZE (1 << 5)
  28. static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE];
  29. static DEFINE_RWLOCK(keyring_name_lock);
  30. static inline unsigned keyring_hash(const char *desc)
  31. {
  32. unsigned bucket = 0;
  33. for (; *desc; desc++)
  34. bucket += (unsigned char) *desc;
  35. return bucket & (KEYRING_NAME_HASH_SIZE - 1);
  36. }
  37. /*
  38. * the keyring type definition
  39. */
  40. static int keyring_instantiate(struct key *keyring,
  41. const void *data, size_t datalen);
  42. static int keyring_duplicate(struct key *keyring, const struct key *source);
  43. static int keyring_match(const struct key *keyring, const void *criterion);
  44. static void keyring_destroy(struct key *keyring);
  45. static void keyring_describe(const struct key *keyring, struct seq_file *m);
  46. static long keyring_read(const struct key *keyring,
  47. char __user *buffer, size_t buflen);
  48. struct key_type key_type_keyring = {
  49. .name = "keyring",
  50. .def_datalen = sizeof(struct keyring_list),
  51. .instantiate = keyring_instantiate,
  52. .duplicate = keyring_duplicate,
  53. .match = keyring_match,
  54. .destroy = keyring_destroy,
  55. .describe = keyring_describe,
  56. .read = keyring_read,
  57. };
  58. /*
  59. * semaphore to serialise link/link calls to prevent two link calls in parallel
  60. * introducing a cycle
  61. */
  62. DECLARE_RWSEM(keyring_serialise_link_sem);
  63. /*****************************************************************************/
  64. /*
  65. * publish the name of a keyring so that it can be found by name (if it has
  66. * one)
  67. */
  68. void keyring_publish_name(struct key *keyring)
  69. {
  70. int bucket;
  71. if (keyring->description) {
  72. bucket = keyring_hash(keyring->description);
  73. write_lock(&keyring_name_lock);
  74. if (!keyring_name_hash[bucket].next)
  75. INIT_LIST_HEAD(&keyring_name_hash[bucket]);
  76. list_add_tail(&keyring->type_data.link,
  77. &keyring_name_hash[bucket]);
  78. write_unlock(&keyring_name_lock);
  79. }
  80. } /* end keyring_publish_name() */
  81. /*****************************************************************************/
  82. /*
  83. * initialise a keyring
  84. * - we object if we were given any data
  85. */
  86. static int keyring_instantiate(struct key *keyring,
  87. const void *data, size_t datalen)
  88. {
  89. int ret;
  90. ret = -EINVAL;
  91. if (datalen == 0) {
  92. /* make the keyring available by name if it has one */
  93. keyring_publish_name(keyring);
  94. ret = 0;
  95. }
  96. return ret;
  97. } /* end keyring_instantiate() */
  98. /*****************************************************************************/
  99. /*
  100. * duplicate the list of subscribed keys from a source keyring into this one
  101. */
  102. static int keyring_duplicate(struct key *keyring, const struct key *source)
  103. {
  104. struct keyring_list *sklist, *klist;
  105. unsigned max;
  106. size_t size;
  107. int loop, ret;
  108. const unsigned limit =
  109. (PAGE_SIZE - sizeof(*klist)) / sizeof(struct key *);
  110. ret = 0;
  111. /* find out how many keys are currently linked */
  112. rcu_read_lock();
  113. sklist = rcu_dereference(source->payload.subscriptions);
  114. max = 0;
  115. if (sklist)
  116. max = sklist->nkeys;
  117. rcu_read_unlock();
  118. /* allocate a new payload and stuff load with key links */
  119. if (max > 0) {
  120. BUG_ON(max > limit);
  121. max = (max + 3) & ~3;
  122. if (max > limit)
  123. max = limit;
  124. ret = -ENOMEM;
  125. size = sizeof(*klist) + sizeof(struct key *) * max;
  126. klist = kmalloc(size, GFP_KERNEL);
  127. if (!klist)
  128. goto error;
  129. /* set links */
  130. rcu_read_lock();
  131. sklist = rcu_dereference(source->payload.subscriptions);
  132. klist->maxkeys = max;
  133. klist->nkeys = sklist->nkeys;
  134. memcpy(klist->keys,
  135. sklist->keys,
  136. sklist->nkeys * sizeof(struct key *));
  137. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  138. atomic_inc(&klist->keys[loop]->usage);
  139. rcu_read_unlock();
  140. rcu_assign_pointer(keyring->payload.subscriptions, klist);
  141. ret = 0;
  142. }
  143. error:
  144. return ret;
  145. } /* end keyring_duplicate() */
  146. /*****************************************************************************/
  147. /*
  148. * match keyrings on their name
  149. */
  150. static int keyring_match(const struct key *keyring, const void *description)
  151. {
  152. return keyring->description &&
  153. strcmp(keyring->description, description) == 0;
  154. } /* end keyring_match() */
  155. /*****************************************************************************/
  156. /*
  157. * dispose of the data dangling from the corpse of a keyring
  158. */
  159. static void keyring_destroy(struct key *keyring)
  160. {
  161. struct keyring_list *klist;
  162. int loop;
  163. if (keyring->description) {
  164. write_lock(&keyring_name_lock);
  165. if (keyring->type_data.link.next != NULL &&
  166. !list_empty(&keyring->type_data.link))
  167. list_del(&keyring->type_data.link);
  168. write_unlock(&keyring_name_lock);
  169. }
  170. klist = rcu_dereference(keyring->payload.subscriptions);
  171. if (klist) {
  172. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  173. key_put(klist->keys[loop]);
  174. kfree(klist);
  175. }
  176. } /* end keyring_destroy() */
  177. /*****************************************************************************/
  178. /*
  179. * describe the keyring
  180. */
  181. static void keyring_describe(const struct key *keyring, struct seq_file *m)
  182. {
  183. struct keyring_list *klist;
  184. if (keyring->description) {
  185. seq_puts(m, keyring->description);
  186. }
  187. else {
  188. seq_puts(m, "[anon]");
  189. }
  190. rcu_read_lock();
  191. klist = rcu_dereference(keyring->payload.subscriptions);
  192. if (klist)
  193. seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
  194. else
  195. seq_puts(m, ": empty");
  196. rcu_read_unlock();
  197. } /* end keyring_describe() */
  198. /*****************************************************************************/
  199. /*
  200. * read a list of key IDs from the keyring's contents
  201. * - the keyring's semaphore is read-locked
  202. */
  203. static long keyring_read(const struct key *keyring,
  204. char __user *buffer, size_t buflen)
  205. {
  206. struct keyring_list *klist;
  207. struct key *key;
  208. size_t qty, tmp;
  209. int loop, ret;
  210. ret = 0;
  211. klist = rcu_dereference(keyring->payload.subscriptions);
  212. if (klist) {
  213. /* calculate how much data we could return */
  214. qty = klist->nkeys * sizeof(key_serial_t);
  215. if (buffer && buflen > 0) {
  216. if (buflen > qty)
  217. buflen = qty;
  218. /* copy the IDs of the subscribed keys into the
  219. * buffer */
  220. ret = -EFAULT;
  221. for (loop = 0; loop < klist->nkeys; loop++) {
  222. key = klist->keys[loop];
  223. tmp = sizeof(key_serial_t);
  224. if (tmp > buflen)
  225. tmp = buflen;
  226. if (copy_to_user(buffer,
  227. &key->serial,
  228. tmp) != 0)
  229. goto error;
  230. buflen -= tmp;
  231. if (buflen == 0)
  232. break;
  233. buffer += tmp;
  234. }
  235. }
  236. ret = qty;
  237. }
  238. error:
  239. return ret;
  240. } /* end keyring_read() */
  241. /*****************************************************************************/
  242. /*
  243. * allocate a keyring and link into the destination keyring
  244. */
  245. struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
  246. int not_in_quota, struct key *dest)
  247. {
  248. struct key *keyring;
  249. int ret;
  250. keyring = key_alloc(&key_type_keyring, description,
  251. uid, gid, KEY_USR_ALL, not_in_quota);
  252. if (!IS_ERR(keyring)) {
  253. ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
  254. if (ret < 0) {
  255. key_put(keyring);
  256. keyring = ERR_PTR(ret);
  257. }
  258. }
  259. return keyring;
  260. } /* end keyring_alloc() */
  261. /*****************************************************************************/
  262. /*
  263. * search the supplied keyring tree for a key that matches the criterion
  264. * - perform a breadth-then-depth search up to the prescribed limit
  265. * - we only find keys on which we have search permission
  266. * - we use the supplied match function to see if the description (or other
  267. * feature of interest) matches
  268. * - we rely on RCU to prevent the keyring lists from disappearing on us
  269. * - we return -EAGAIN if we didn't find any matching key
  270. * - we return -ENOKEY if we only found negative matching keys
  271. */
  272. struct key *keyring_search_aux(struct key *keyring,
  273. struct task_struct *context,
  274. struct key_type *type,
  275. const void *description,
  276. key_match_func_t match)
  277. {
  278. struct {
  279. struct keyring_list *keylist;
  280. int kix;
  281. } stack[KEYRING_SEARCH_MAX_DEPTH];
  282. struct keyring_list *keylist;
  283. struct timespec now;
  284. struct key *key;
  285. long err;
  286. int sp, kix;
  287. key_check(keyring);
  288. rcu_read_lock();
  289. /* top keyring must have search permission to begin the search */
  290. key = ERR_PTR(-EACCES);
  291. if (!key_task_permission(keyring, context, KEY_SEARCH))
  292. goto error;
  293. key = ERR_PTR(-ENOTDIR);
  294. if (keyring->type != &key_type_keyring)
  295. goto error;
  296. now = current_kernel_time();
  297. err = -EAGAIN;
  298. sp = 0;
  299. /* start processing a new keyring */
  300. descend:
  301. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  302. goto not_this_keyring;
  303. keylist = rcu_dereference(keyring->payload.subscriptions);
  304. if (!keylist)
  305. goto not_this_keyring;
  306. /* iterate through the keys in this keyring first */
  307. for (kix = 0; kix < keylist->nkeys; kix++) {
  308. key = keylist->keys[kix];
  309. /* ignore keys not of this type */
  310. if (key->type != type)
  311. continue;
  312. /* skip revoked keys and expired keys */
  313. if (test_bit(KEY_FLAG_REVOKED, &key->flags))
  314. continue;
  315. if (key->expiry && now.tv_sec >= key->expiry)
  316. continue;
  317. /* keys that don't match */
  318. if (!match(key, description))
  319. continue;
  320. /* key must have search permissions */
  321. if (!key_task_permission(key, context, KEY_SEARCH))
  322. continue;
  323. /* we set a different error code if we find a negative key */
  324. if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
  325. err = -ENOKEY;
  326. continue;
  327. }
  328. goto found;
  329. }
  330. /* search through the keyrings nested in this one */
  331. kix = 0;
  332. ascend:
  333. for (; kix < keylist->nkeys; kix++) {
  334. key = keylist->keys[kix];
  335. if (key->type != &key_type_keyring)
  336. continue;
  337. /* recursively search nested keyrings
  338. * - only search keyrings for which we have search permission
  339. */
  340. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  341. continue;
  342. if (!key_task_permission(key, context, KEY_SEARCH))
  343. continue;
  344. /* stack the current position */
  345. stack[sp].keylist = keylist;
  346. stack[sp].kix = kix;
  347. sp++;
  348. /* begin again with the new keyring */
  349. keyring = key;
  350. goto descend;
  351. }
  352. /* the keyring we're looking at was disqualified or didn't contain a
  353. * matching key */
  354. not_this_keyring:
  355. if (sp > 0) {
  356. /* resume the processing of a keyring higher up in the tree */
  357. sp--;
  358. keylist = stack[sp].keylist;
  359. kix = stack[sp].kix + 1;
  360. goto ascend;
  361. }
  362. key = ERR_PTR(err);
  363. goto error;
  364. /* we found a viable match */
  365. found:
  366. atomic_inc(&key->usage);
  367. key_check(key);
  368. error:
  369. rcu_read_unlock();
  370. return key;
  371. } /* end keyring_search_aux() */
  372. /*****************************************************************************/
  373. /*
  374. * search the supplied keyring tree for a key that matches the criterion
  375. * - perform a breadth-then-depth search up to the prescribed limit
  376. * - we only find keys on which we have search permission
  377. * - we readlock the keyrings as we search down the tree
  378. * - we return -EAGAIN if we didn't find any matching key
  379. * - we return -ENOKEY if we only found negative matching keys
  380. */
  381. struct key *keyring_search(struct key *keyring,
  382. struct key_type *type,
  383. const char *description)
  384. {
  385. if (!type->match)
  386. return ERR_PTR(-ENOKEY);
  387. return keyring_search_aux(keyring, current,
  388. type, description, type->match);
  389. } /* end keyring_search() */
  390. EXPORT_SYMBOL(keyring_search);
  391. /*****************************************************************************/
  392. /*
  393. * search the given keyring only (no recursion)
  394. * - keyring must be locked by caller
  395. */
  396. struct key *__keyring_search_one(struct key *keyring,
  397. const struct key_type *ktype,
  398. const char *description,
  399. key_perm_t perm)
  400. {
  401. struct keyring_list *klist;
  402. struct key *key;
  403. int loop;
  404. rcu_read_lock();
  405. klist = rcu_dereference(keyring->payload.subscriptions);
  406. if (klist) {
  407. for (loop = 0; loop < klist->nkeys; loop++) {
  408. key = klist->keys[loop];
  409. if (key->type == ktype &&
  410. (!key->type->match ||
  411. key->type->match(key, description)) &&
  412. key_permission(key, perm) &&
  413. !test_bit(KEY_FLAG_REVOKED, &key->flags)
  414. )
  415. goto found;
  416. }
  417. }
  418. key = ERR_PTR(-ENOKEY);
  419. goto error;
  420. found:
  421. atomic_inc(&key->usage);
  422. error:
  423. rcu_read_unlock();
  424. return key;
  425. } /* end __keyring_search_one() */
  426. /*****************************************************************************/
  427. /*
  428. * search for an instantiation authorisation key matching a target key
  429. * - the RCU read lock must be held by the caller
  430. * - a target_id of zero specifies any valid token
  431. */
  432. struct key *keyring_search_instkey(struct key *keyring,
  433. key_serial_t target_id)
  434. {
  435. struct request_key_auth *rka;
  436. struct keyring_list *klist;
  437. struct key *instkey;
  438. int loop;
  439. klist = rcu_dereference(keyring->payload.subscriptions);
  440. if (klist) {
  441. for (loop = 0; loop < klist->nkeys; loop++) {
  442. instkey = klist->keys[loop];
  443. if (instkey->type != &key_type_request_key_auth)
  444. continue;
  445. rka = instkey->payload.data;
  446. if (target_id && rka->target_key->serial != target_id)
  447. continue;
  448. /* the auth key is revoked during instantiation */
  449. if (!test_bit(KEY_FLAG_REVOKED, &instkey->flags))
  450. goto found;
  451. instkey = ERR_PTR(-EKEYREVOKED);
  452. goto error;
  453. }
  454. }
  455. instkey = ERR_PTR(-EACCES);
  456. goto error;
  457. found:
  458. atomic_inc(&instkey->usage);
  459. error:
  460. return instkey;
  461. } /* end keyring_search_instkey() */
  462. /*****************************************************************************/
  463. /*
  464. * find a keyring with the specified name
  465. * - all named keyrings are searched
  466. * - only find keyrings with search permission for the process
  467. * - only find keyrings with a serial number greater than the one specified
  468. */
  469. struct key *find_keyring_by_name(const char *name, key_serial_t bound)
  470. {
  471. struct key *keyring;
  472. int bucket;
  473. keyring = ERR_PTR(-EINVAL);
  474. if (!name)
  475. goto error;
  476. bucket = keyring_hash(name);
  477. read_lock(&keyring_name_lock);
  478. if (keyring_name_hash[bucket].next) {
  479. /* search this hash bucket for a keyring with a matching name
  480. * that's readable and that hasn't been revoked */
  481. list_for_each_entry(keyring,
  482. &keyring_name_hash[bucket],
  483. type_data.link
  484. ) {
  485. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  486. continue;
  487. if (strcmp(keyring->description, name) != 0)
  488. continue;
  489. if (!key_permission(keyring, KEY_SEARCH))
  490. continue;
  491. /* found a potential candidate, but we still need to
  492. * check the serial number */
  493. if (keyring->serial <= bound)
  494. continue;
  495. /* we've got a match */
  496. atomic_inc(&keyring->usage);
  497. read_unlock(&keyring_name_lock);
  498. goto error;
  499. }
  500. }
  501. read_unlock(&keyring_name_lock);
  502. keyring = ERR_PTR(-ENOKEY);
  503. error:
  504. return keyring;
  505. } /* end find_keyring_by_name() */
  506. /*****************************************************************************/
  507. /*
  508. * see if a cycle will will be created by inserting acyclic tree B in acyclic
  509. * tree A at the topmost level (ie: as a direct child of A)
  510. * - since we are adding B to A at the top level, checking for cycles should
  511. * just be a matter of seeing if node A is somewhere in tree B
  512. */
  513. static int keyring_detect_cycle(struct key *A, struct key *B)
  514. {
  515. struct {
  516. struct keyring_list *keylist;
  517. int kix;
  518. } stack[KEYRING_SEARCH_MAX_DEPTH];
  519. struct keyring_list *keylist;
  520. struct key *subtree, *key;
  521. int sp, kix, ret;
  522. rcu_read_lock();
  523. ret = -EDEADLK;
  524. if (A == B)
  525. goto cycle_detected;
  526. subtree = B;
  527. sp = 0;
  528. /* start processing a new keyring */
  529. descend:
  530. if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
  531. goto not_this_keyring;
  532. keylist = rcu_dereference(subtree->payload.subscriptions);
  533. if (!keylist)
  534. goto not_this_keyring;
  535. kix = 0;
  536. ascend:
  537. /* iterate through the remaining keys in this keyring */
  538. for (; kix < keylist->nkeys; kix++) {
  539. key = keylist->keys[kix];
  540. if (key == A)
  541. goto cycle_detected;
  542. /* recursively check nested keyrings */
  543. if (key->type == &key_type_keyring) {
  544. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  545. goto too_deep;
  546. /* stack the current position */
  547. stack[sp].keylist = keylist;
  548. stack[sp].kix = kix;
  549. sp++;
  550. /* begin again with the new keyring */
  551. subtree = key;
  552. goto descend;
  553. }
  554. }
  555. /* the keyring we're looking at was disqualified or didn't contain a
  556. * matching key */
  557. not_this_keyring:
  558. if (sp > 0) {
  559. /* resume the checking of a keyring higher up in the tree */
  560. sp--;
  561. keylist = stack[sp].keylist;
  562. kix = stack[sp].kix + 1;
  563. goto ascend;
  564. }
  565. ret = 0; /* no cycles detected */
  566. error:
  567. rcu_read_unlock();
  568. return ret;
  569. too_deep:
  570. ret = -ELOOP;
  571. goto error;
  572. cycle_detected:
  573. ret = -EDEADLK;
  574. goto error;
  575. } /* end keyring_detect_cycle() */
  576. /*****************************************************************************/
  577. /*
  578. * dispose of a keyring list after the RCU grace period
  579. */
  580. static void keyring_link_rcu_disposal(struct rcu_head *rcu)
  581. {
  582. struct keyring_list *klist =
  583. container_of(rcu, struct keyring_list, rcu);
  584. kfree(klist);
  585. } /* end keyring_link_rcu_disposal() */
  586. /*****************************************************************************/
  587. /*
  588. * link a key into to a keyring
  589. * - must be called with the keyring's semaphore write-locked
  590. */
  591. int __key_link(struct key *keyring, struct key *key)
  592. {
  593. struct keyring_list *klist, *nklist;
  594. unsigned max;
  595. size_t size;
  596. int ret;
  597. ret = -EKEYREVOKED;
  598. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  599. goto error;
  600. ret = -ENOTDIR;
  601. if (keyring->type != &key_type_keyring)
  602. goto error;
  603. /* serialise link/link calls to prevent parallel calls causing a
  604. * cycle when applied to two keyring in opposite orders */
  605. down_write(&keyring_serialise_link_sem);
  606. /* check that we aren't going to create a cycle adding one keyring to
  607. * another */
  608. if (key->type == &key_type_keyring) {
  609. ret = keyring_detect_cycle(keyring, key);
  610. if (ret < 0)
  611. goto error2;
  612. }
  613. /* check that we aren't going to overrun the user's quota */
  614. ret = key_payload_reserve(keyring,
  615. keyring->datalen + KEYQUOTA_LINK_BYTES);
  616. if (ret < 0)
  617. goto error2;
  618. klist = keyring->payload.subscriptions;
  619. if (klist && klist->nkeys < klist->maxkeys) {
  620. /* there's sufficient slack space to add directly */
  621. atomic_inc(&key->usage);
  622. klist->keys[klist->nkeys] = key;
  623. smp_wmb();
  624. klist->nkeys++;
  625. smp_wmb();
  626. ret = 0;
  627. }
  628. else {
  629. /* grow the key list */
  630. max = 4;
  631. if (klist)
  632. max += klist->maxkeys;
  633. ret = -ENFILE;
  634. if (max > 65535)
  635. goto error3;
  636. size = sizeof(*klist) + sizeof(struct key *) * max;
  637. if (size > PAGE_SIZE)
  638. goto error3;
  639. ret = -ENOMEM;
  640. nklist = kmalloc(size, GFP_KERNEL);
  641. if (!nklist)
  642. goto error3;
  643. nklist->maxkeys = max;
  644. nklist->nkeys = 0;
  645. if (klist) {
  646. nklist->nkeys = klist->nkeys;
  647. memcpy(nklist->keys,
  648. klist->keys,
  649. sizeof(struct key *) * klist->nkeys);
  650. }
  651. /* add the key into the new space */
  652. atomic_inc(&key->usage);
  653. nklist->keys[nklist->nkeys++] = key;
  654. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  655. /* dispose of the old keyring list */
  656. if (klist)
  657. call_rcu(&klist->rcu, keyring_link_rcu_disposal);
  658. ret = 0;
  659. }
  660. error2:
  661. up_write(&keyring_serialise_link_sem);
  662. error:
  663. return ret;
  664. error3:
  665. /* undo the quota changes */
  666. key_payload_reserve(keyring,
  667. keyring->datalen - KEYQUOTA_LINK_BYTES);
  668. goto error2;
  669. } /* end __key_link() */
  670. /*****************************************************************************/
  671. /*
  672. * link a key to a keyring
  673. */
  674. int key_link(struct key *keyring, struct key *key)
  675. {
  676. int ret;
  677. key_check(keyring);
  678. key_check(key);
  679. down_write(&keyring->sem);
  680. ret = __key_link(keyring, key);
  681. up_write(&keyring->sem);
  682. return ret;
  683. } /* end key_link() */
  684. EXPORT_SYMBOL(key_link);
  685. /*****************************************************************************/
  686. /*
  687. * dispose of a keyring list after the RCU grace period, freeing the unlinked
  688. * key
  689. */
  690. static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
  691. {
  692. struct keyring_list *klist =
  693. container_of(rcu, struct keyring_list, rcu);
  694. key_put(klist->keys[klist->delkey]);
  695. kfree(klist);
  696. } /* end keyring_unlink_rcu_disposal() */
  697. /*****************************************************************************/
  698. /*
  699. * unlink the first link to a key from a keyring
  700. */
  701. int key_unlink(struct key *keyring, struct key *key)
  702. {
  703. struct keyring_list *klist, *nklist;
  704. int loop, ret;
  705. key_check(keyring);
  706. key_check(key);
  707. ret = -ENOTDIR;
  708. if (keyring->type != &key_type_keyring)
  709. goto error;
  710. down_write(&keyring->sem);
  711. klist = keyring->payload.subscriptions;
  712. if (klist) {
  713. /* search the keyring for the key */
  714. for (loop = 0; loop < klist->nkeys; loop++)
  715. if (klist->keys[loop] == key)
  716. goto key_is_present;
  717. }
  718. up_write(&keyring->sem);
  719. ret = -ENOENT;
  720. goto error;
  721. key_is_present:
  722. /* we need to copy the key list for RCU purposes */
  723. nklist = kmalloc(sizeof(*klist) +
  724. sizeof(struct key *) * klist->maxkeys,
  725. GFP_KERNEL);
  726. if (!nklist)
  727. goto nomem;
  728. nklist->maxkeys = klist->maxkeys;
  729. nklist->nkeys = klist->nkeys - 1;
  730. if (loop > 0)
  731. memcpy(&nklist->keys[0],
  732. &klist->keys[0],
  733. loop * sizeof(struct key *));
  734. if (loop < nklist->nkeys)
  735. memcpy(&nklist->keys[loop],
  736. &klist->keys[loop + 1],
  737. (nklist->nkeys - loop) * sizeof(struct key *));
  738. /* adjust the user's quota */
  739. key_payload_reserve(keyring,
  740. keyring->datalen - KEYQUOTA_LINK_BYTES);
  741. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  742. up_write(&keyring->sem);
  743. /* schedule for later cleanup */
  744. klist->delkey = loop;
  745. call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
  746. ret = 0;
  747. error:
  748. return ret;
  749. nomem:
  750. ret = -ENOMEM;
  751. up_write(&keyring->sem);
  752. goto error;
  753. } /* end key_unlink() */
  754. EXPORT_SYMBOL(key_unlink);
  755. /*****************************************************************************/
  756. /*
  757. * dispose of a keyring list after the RCU grace period, releasing the keys it
  758. * links to
  759. */
  760. static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
  761. {
  762. struct keyring_list *klist;
  763. int loop;
  764. klist = container_of(rcu, struct keyring_list, rcu);
  765. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  766. key_put(klist->keys[loop]);
  767. kfree(klist);
  768. } /* end keyring_clear_rcu_disposal() */
  769. /*****************************************************************************/
  770. /*
  771. * clear the specified process keyring
  772. * - implements keyctl(KEYCTL_CLEAR)
  773. */
  774. int keyring_clear(struct key *keyring)
  775. {
  776. struct keyring_list *klist;
  777. int ret;
  778. ret = -ENOTDIR;
  779. if (keyring->type == &key_type_keyring) {
  780. /* detach the pointer block with the locks held */
  781. down_write(&keyring->sem);
  782. klist = keyring->payload.subscriptions;
  783. if (klist) {
  784. /* adjust the quota */
  785. key_payload_reserve(keyring,
  786. sizeof(struct keyring_list));
  787. rcu_assign_pointer(keyring->payload.subscriptions,
  788. NULL);
  789. }
  790. up_write(&keyring->sem);
  791. /* free the keys after the locks have been dropped */
  792. if (klist)
  793. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  794. ret = 0;
  795. }
  796. return ret;
  797. } /* end keyring_clear() */
  798. EXPORT_SYMBOL(keyring_clear);