keyring.c 22 KB

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