keyring.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /* keyring.c: keyring handling
  2. *
  3. * Copyright (C) 2004 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. list_del(&keyring->type_data.link);
  166. write_unlock(&keyring_name_lock);
  167. }
  168. klist = rcu_dereference(keyring->payload.subscriptions);
  169. if (klist) {
  170. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  171. key_put(klist->keys[loop]);
  172. kfree(klist);
  173. }
  174. } /* end keyring_destroy() */
  175. /*****************************************************************************/
  176. /*
  177. * describe the keyring
  178. */
  179. static void keyring_describe(const struct key *keyring, struct seq_file *m)
  180. {
  181. struct keyring_list *klist;
  182. if (keyring->description) {
  183. seq_puts(m, keyring->description);
  184. }
  185. else {
  186. seq_puts(m, "[anon]");
  187. }
  188. rcu_read_lock();
  189. klist = rcu_dereference(keyring->payload.subscriptions);
  190. if (klist)
  191. seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
  192. else
  193. seq_puts(m, ": empty");
  194. rcu_read_unlock();
  195. } /* end keyring_describe() */
  196. /*****************************************************************************/
  197. /*
  198. * read a list of key IDs from the keyring's contents
  199. * - the keyring's semaphore is read-locked
  200. */
  201. static long keyring_read(const struct key *keyring,
  202. char __user *buffer, size_t buflen)
  203. {
  204. struct keyring_list *klist;
  205. struct key *key;
  206. size_t qty, tmp;
  207. int loop, ret;
  208. ret = 0;
  209. klist = rcu_dereference(keyring->payload.subscriptions);
  210. if (klist) {
  211. /* calculate how much data we could return */
  212. qty = klist->nkeys * sizeof(key_serial_t);
  213. if (buffer && buflen > 0) {
  214. if (buflen > qty)
  215. buflen = qty;
  216. /* copy the IDs of the subscribed keys into the
  217. * buffer */
  218. ret = -EFAULT;
  219. for (loop = 0; loop < klist->nkeys; loop++) {
  220. key = klist->keys[loop];
  221. tmp = sizeof(key_serial_t);
  222. if (tmp > buflen)
  223. tmp = buflen;
  224. if (copy_to_user(buffer,
  225. &key->serial,
  226. tmp) != 0)
  227. goto error;
  228. buflen -= tmp;
  229. if (buflen == 0)
  230. break;
  231. buffer += tmp;
  232. }
  233. }
  234. ret = qty;
  235. }
  236. error:
  237. return ret;
  238. } /* end keyring_read() */
  239. /*****************************************************************************/
  240. /*
  241. * allocate a keyring and link into the destination keyring
  242. */
  243. struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
  244. int not_in_quota, struct key *dest)
  245. {
  246. struct key *keyring;
  247. int ret;
  248. keyring = key_alloc(&key_type_keyring, description,
  249. uid, gid, KEY_USR_ALL, not_in_quota);
  250. if (!IS_ERR(keyring)) {
  251. ret = key_instantiate_and_link(keyring, NULL, 0, dest);
  252. if (ret < 0) {
  253. key_put(keyring);
  254. keyring = ERR_PTR(ret);
  255. }
  256. }
  257. return keyring;
  258. } /* end keyring_alloc() */
  259. /*****************************************************************************/
  260. /*
  261. * search the supplied keyring tree for a key that matches the criterion
  262. * - perform a breadth-then-depth search up to the prescribed limit
  263. * - we only find keys on which we have search permission
  264. * - we use the supplied match function to see if the description (or other
  265. * feature of interest) matches
  266. * - we readlock the keyrings as we search down the tree
  267. * - we return -EAGAIN if we didn't find any matching key
  268. * - we return -ENOKEY if we only found negative matching keys
  269. */
  270. struct key *keyring_search_aux(struct key *keyring,
  271. struct key_type *type,
  272. const void *description,
  273. key_match_func_t match)
  274. {
  275. struct {
  276. struct keyring_list *keylist;
  277. int kix;
  278. } stack[KEYRING_SEARCH_MAX_DEPTH];
  279. struct keyring_list *keylist;
  280. struct timespec now;
  281. struct key *key;
  282. long err;
  283. int sp, kix;
  284. key_check(keyring);
  285. rcu_read_lock();
  286. /* top keyring must have search permission to begin the search */
  287. key = ERR_PTR(-EACCES);
  288. if (!key_permission(keyring, KEY_SEARCH))
  289. goto error;
  290. key = ERR_PTR(-ENOTDIR);
  291. if (keyring->type != &key_type_keyring)
  292. goto error;
  293. now = current_kernel_time();
  294. err = -EAGAIN;
  295. sp = 0;
  296. /* start processing a new keyring */
  297. descend:
  298. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  299. goto not_this_keyring;
  300. keylist = rcu_dereference(keyring->payload.subscriptions);
  301. if (!keylist)
  302. goto not_this_keyring;
  303. /* iterate through the keys in this keyring first */
  304. for (kix = 0; kix < keylist->nkeys; kix++) {
  305. key = keylist->keys[kix];
  306. /* ignore keys not of this type */
  307. if (key->type != type)
  308. continue;
  309. /* skip revoked keys and expired keys */
  310. if (test_bit(KEY_FLAG_REVOKED, &key->flags))
  311. continue;
  312. if (key->expiry && now.tv_sec >= key->expiry)
  313. continue;
  314. /* keys that don't match */
  315. if (!match(key, description))
  316. continue;
  317. /* key must have search permissions */
  318. if (!key_permission(key, KEY_SEARCH))
  319. continue;
  320. /* we set a different error code if we find a negative key */
  321. if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
  322. err = -ENOKEY;
  323. continue;
  324. }
  325. goto found;
  326. }
  327. /* search through the keyrings nested in this one */
  328. kix = 0;
  329. ascend:
  330. for (; kix < keylist->nkeys; kix++) {
  331. key = keylist->keys[kix];
  332. if (key->type != &key_type_keyring)
  333. continue;
  334. /* recursively search nested keyrings
  335. * - only search keyrings for which we have search permission
  336. */
  337. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  338. continue;
  339. if (!key_permission(key, KEY_SEARCH))
  340. continue;
  341. /* stack the current position */
  342. stack[sp].keylist = keylist;
  343. stack[sp].kix = kix;
  344. sp++;
  345. /* begin again with the new keyring */
  346. keyring = key;
  347. goto descend;
  348. }
  349. /* the keyring we're looking at was disqualified or didn't contain a
  350. * matching key */
  351. not_this_keyring:
  352. if (sp > 0) {
  353. /* resume the processing of a keyring higher up in the tree */
  354. sp--;
  355. keylist = stack[sp].keylist;
  356. kix = stack[sp].kix + 1;
  357. goto ascend;
  358. }
  359. key = ERR_PTR(err);
  360. goto error;
  361. /* we found a viable match */
  362. found:
  363. atomic_inc(&key->usage);
  364. key_check(key);
  365. error:
  366. rcu_read_unlock();
  367. return key;
  368. } /* end keyring_search_aux() */
  369. /*****************************************************************************/
  370. /*
  371. * search the supplied keyring tree for a key that matches the criterion
  372. * - perform a breadth-then-depth search up to the prescribed limit
  373. * - we only find keys on which we have search permission
  374. * - we readlock the keyrings as we search down the tree
  375. * - we return -EAGAIN if we didn't find any matching key
  376. * - we return -ENOKEY if we only found negative matching keys
  377. */
  378. struct key *keyring_search(struct key *keyring,
  379. struct key_type *type,
  380. const char *description)
  381. {
  382. return keyring_search_aux(keyring, type, description, type->match);
  383. } /* end keyring_search() */
  384. EXPORT_SYMBOL(keyring_search);
  385. /*****************************************************************************/
  386. /*
  387. * search the given keyring only (no recursion)
  388. * - keyring must be locked by caller
  389. */
  390. struct key *__keyring_search_one(struct key *keyring,
  391. const struct key_type *ktype,
  392. const char *description,
  393. key_perm_t perm)
  394. {
  395. struct keyring_list *klist;
  396. struct key *key;
  397. int loop;
  398. rcu_read_lock();
  399. klist = rcu_dereference(keyring->payload.subscriptions);
  400. if (klist) {
  401. for (loop = 0; loop < klist->nkeys; loop++) {
  402. key = klist->keys[loop];
  403. if (key->type == ktype &&
  404. key->type->match(key, description) &&
  405. key_permission(key, perm) &&
  406. !test_bit(KEY_FLAG_REVOKED, &key->flags)
  407. )
  408. goto found;
  409. }
  410. }
  411. key = ERR_PTR(-ENOKEY);
  412. goto error;
  413. found:
  414. atomic_inc(&key->usage);
  415. error:
  416. rcu_read_unlock();
  417. return key;
  418. } /* end __keyring_search_one() */
  419. /*****************************************************************************/
  420. /*
  421. * find a keyring with the specified name
  422. * - all named keyrings are searched
  423. * - only find keyrings with search permission for the process
  424. * - only find keyrings with a serial number greater than the one specified
  425. */
  426. struct key *find_keyring_by_name(const char *name, key_serial_t bound)
  427. {
  428. struct key *keyring;
  429. int bucket;
  430. keyring = ERR_PTR(-EINVAL);
  431. if (!name)
  432. goto error;
  433. bucket = keyring_hash(name);
  434. read_lock(&keyring_name_lock);
  435. if (keyring_name_hash[bucket].next) {
  436. /* search this hash bucket for a keyring with a matching name
  437. * that's readable and that hasn't been revoked */
  438. list_for_each_entry(keyring,
  439. &keyring_name_hash[bucket],
  440. type_data.link
  441. ) {
  442. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  443. continue;
  444. if (strcmp(keyring->description, name) != 0)
  445. continue;
  446. if (!key_permission(keyring, KEY_SEARCH))
  447. continue;
  448. /* found a potential candidate, but we still need to
  449. * check the serial number */
  450. if (keyring->serial <= bound)
  451. continue;
  452. /* we've got a match */
  453. atomic_inc(&keyring->usage);
  454. read_unlock(&keyring_name_lock);
  455. goto error;
  456. }
  457. }
  458. read_unlock(&keyring_name_lock);
  459. keyring = ERR_PTR(-ENOKEY);
  460. error:
  461. return keyring;
  462. } /* end find_keyring_by_name() */
  463. /*****************************************************************************/
  464. /*
  465. * see if a cycle will will be created by inserting acyclic tree B in acyclic
  466. * tree A at the topmost level (ie: as a direct child of A)
  467. * - since we are adding B to A at the top level, checking for cycles should
  468. * just be a matter of seeing if node A is somewhere in tree B
  469. */
  470. static int keyring_detect_cycle(struct key *A, struct key *B)
  471. {
  472. struct {
  473. struct keyring_list *keylist;
  474. int kix;
  475. } stack[KEYRING_SEARCH_MAX_DEPTH];
  476. struct keyring_list *keylist;
  477. struct key *subtree, *key;
  478. int sp, kix, ret;
  479. rcu_read_lock();
  480. ret = -EDEADLK;
  481. if (A == B)
  482. goto cycle_detected;
  483. subtree = B;
  484. sp = 0;
  485. /* start processing a new keyring */
  486. descend:
  487. if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
  488. goto not_this_keyring;
  489. keylist = rcu_dereference(subtree->payload.subscriptions);
  490. if (!keylist)
  491. goto not_this_keyring;
  492. kix = 0;
  493. ascend:
  494. /* iterate through the remaining keys in this keyring */
  495. for (; kix < keylist->nkeys; kix++) {
  496. key = keylist->keys[kix];
  497. if (key == A)
  498. goto cycle_detected;
  499. /* recursively check nested keyrings */
  500. if (key->type == &key_type_keyring) {
  501. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  502. goto too_deep;
  503. /* stack the current position */
  504. stack[sp].keylist = keylist;
  505. stack[sp].kix = kix;
  506. sp++;
  507. /* begin again with the new keyring */
  508. subtree = key;
  509. goto descend;
  510. }
  511. }
  512. /* the keyring we're looking at was disqualified or didn't contain a
  513. * matching key */
  514. not_this_keyring:
  515. if (sp > 0) {
  516. /* resume the checking of a keyring higher up in the tree */
  517. sp--;
  518. keylist = stack[sp].keylist;
  519. kix = stack[sp].kix + 1;
  520. goto ascend;
  521. }
  522. ret = 0; /* no cycles detected */
  523. error:
  524. rcu_read_unlock();
  525. return ret;
  526. too_deep:
  527. ret = -ELOOP;
  528. goto error;
  529. cycle_detected:
  530. ret = -EDEADLK;
  531. goto error;
  532. } /* end keyring_detect_cycle() */
  533. /*****************************************************************************/
  534. /*
  535. * dispose of a keyring list after the RCU grace period
  536. */
  537. static void keyring_link_rcu_disposal(struct rcu_head *rcu)
  538. {
  539. struct keyring_list *klist =
  540. container_of(rcu, struct keyring_list, rcu);
  541. kfree(klist);
  542. } /* end keyring_link_rcu_disposal() */
  543. /*****************************************************************************/
  544. /*
  545. * link a key into to a keyring
  546. * - must be called with the keyring's semaphore write-locked
  547. */
  548. int __key_link(struct key *keyring, struct key *key)
  549. {
  550. struct keyring_list *klist, *nklist;
  551. unsigned max;
  552. size_t size;
  553. int ret;
  554. ret = -EKEYREVOKED;
  555. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  556. goto error;
  557. ret = -ENOTDIR;
  558. if (keyring->type != &key_type_keyring)
  559. goto error;
  560. /* serialise link/link calls to prevent parallel calls causing a
  561. * cycle when applied to two keyring in opposite orders */
  562. down_write(&keyring_serialise_link_sem);
  563. /* check that we aren't going to create a cycle adding one keyring to
  564. * another */
  565. if (key->type == &key_type_keyring) {
  566. ret = keyring_detect_cycle(keyring, key);
  567. if (ret < 0)
  568. goto error2;
  569. }
  570. /* check that we aren't going to overrun the user's quota */
  571. ret = key_payload_reserve(keyring,
  572. keyring->datalen + KEYQUOTA_LINK_BYTES);
  573. if (ret < 0)
  574. goto error2;
  575. klist = keyring->payload.subscriptions;
  576. if (klist && klist->nkeys < klist->maxkeys) {
  577. /* there's sufficient slack space to add directly */
  578. atomic_inc(&key->usage);
  579. klist->keys[klist->nkeys] = key;
  580. smp_wmb();
  581. klist->nkeys++;
  582. smp_wmb();
  583. ret = 0;
  584. }
  585. else {
  586. /* grow the key list */
  587. max = 4;
  588. if (klist)
  589. max += klist->maxkeys;
  590. ret = -ENFILE;
  591. if (max > 65535)
  592. goto error3;
  593. size = sizeof(*klist) + sizeof(*key) * max;
  594. if (size > PAGE_SIZE)
  595. goto error3;
  596. ret = -ENOMEM;
  597. nklist = kmalloc(size, GFP_KERNEL);
  598. if (!nklist)
  599. goto error3;
  600. nklist->maxkeys = max;
  601. nklist->nkeys = 0;
  602. if (klist) {
  603. nklist->nkeys = klist->nkeys;
  604. memcpy(nklist->keys,
  605. klist->keys,
  606. sizeof(struct key *) * klist->nkeys);
  607. }
  608. /* add the key into the new space */
  609. atomic_inc(&key->usage);
  610. nklist->keys[nklist->nkeys++] = key;
  611. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  612. /* dispose of the old keyring list */
  613. if (klist)
  614. call_rcu(&klist->rcu, keyring_link_rcu_disposal);
  615. ret = 0;
  616. }
  617. error2:
  618. up_write(&keyring_serialise_link_sem);
  619. error:
  620. return ret;
  621. error3:
  622. /* undo the quota changes */
  623. key_payload_reserve(keyring,
  624. keyring->datalen - KEYQUOTA_LINK_BYTES);
  625. goto error2;
  626. } /* end __key_link() */
  627. /*****************************************************************************/
  628. /*
  629. * link a key to a keyring
  630. */
  631. int key_link(struct key *keyring, struct key *key)
  632. {
  633. int ret;
  634. key_check(keyring);
  635. key_check(key);
  636. down_write(&keyring->sem);
  637. ret = __key_link(keyring, key);
  638. up_write(&keyring->sem);
  639. return ret;
  640. } /* end key_link() */
  641. EXPORT_SYMBOL(key_link);
  642. /*****************************************************************************/
  643. /*
  644. * dispose of a keyring list after the RCU grace period, freeing the unlinked
  645. * key
  646. */
  647. static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
  648. {
  649. struct keyring_list *klist =
  650. container_of(rcu, struct keyring_list, rcu);
  651. key_put(klist->keys[klist->delkey]);
  652. kfree(klist);
  653. } /* end keyring_unlink_rcu_disposal() */
  654. /*****************************************************************************/
  655. /*
  656. * unlink the first link to a key from a keyring
  657. */
  658. int key_unlink(struct key *keyring, struct key *key)
  659. {
  660. struct keyring_list *klist, *nklist;
  661. int loop, ret;
  662. key_check(keyring);
  663. key_check(key);
  664. ret = -ENOTDIR;
  665. if (keyring->type != &key_type_keyring)
  666. goto error;
  667. down_write(&keyring->sem);
  668. klist = keyring->payload.subscriptions;
  669. if (klist) {
  670. /* search the keyring for the key */
  671. for (loop = 0; loop < klist->nkeys; loop++)
  672. if (klist->keys[loop] == key)
  673. goto key_is_present;
  674. }
  675. up_write(&keyring->sem);
  676. ret = -ENOENT;
  677. goto error;
  678. key_is_present:
  679. /* we need to copy the key list for RCU purposes */
  680. nklist = kmalloc(sizeof(*klist) + sizeof(*key) * klist->maxkeys,
  681. GFP_KERNEL);
  682. if (!nklist)
  683. goto nomem;
  684. nklist->maxkeys = klist->maxkeys;
  685. nklist->nkeys = klist->nkeys - 1;
  686. if (loop > 0)
  687. memcpy(&nklist->keys[0],
  688. &klist->keys[0],
  689. loop * sizeof(klist->keys[0]));
  690. if (loop < nklist->nkeys)
  691. memcpy(&nklist->keys[loop],
  692. &klist->keys[loop + 1],
  693. (nklist->nkeys - loop) * sizeof(klist->keys[0]));
  694. /* adjust the user's quota */
  695. key_payload_reserve(keyring,
  696. keyring->datalen - KEYQUOTA_LINK_BYTES);
  697. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  698. up_write(&keyring->sem);
  699. /* schedule for later cleanup */
  700. klist->delkey = loop;
  701. call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
  702. ret = 0;
  703. error:
  704. return ret;
  705. nomem:
  706. ret = -ENOMEM;
  707. up_write(&keyring->sem);
  708. goto error;
  709. } /* end key_unlink() */
  710. EXPORT_SYMBOL(key_unlink);
  711. /*****************************************************************************/
  712. /*
  713. * dispose of a keyring list after the RCU grace period, releasing the keys it
  714. * links to
  715. */
  716. static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
  717. {
  718. struct keyring_list *klist;
  719. int loop;
  720. klist = container_of(rcu, struct keyring_list, rcu);
  721. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  722. key_put(klist->keys[loop]);
  723. kfree(klist);
  724. } /* end keyring_clear_rcu_disposal() */
  725. /*****************************************************************************/
  726. /*
  727. * clear the specified process keyring
  728. * - implements keyctl(KEYCTL_CLEAR)
  729. */
  730. int keyring_clear(struct key *keyring)
  731. {
  732. struct keyring_list *klist;
  733. int ret;
  734. ret = -ENOTDIR;
  735. if (keyring->type == &key_type_keyring) {
  736. /* detach the pointer block with the locks held */
  737. down_write(&keyring->sem);
  738. klist = keyring->payload.subscriptions;
  739. if (klist) {
  740. /* adjust the quota */
  741. key_payload_reserve(keyring,
  742. sizeof(struct keyring_list));
  743. rcu_assign_pointer(keyring->payload.subscriptions,
  744. NULL);
  745. }
  746. up_write(&keyring->sem);
  747. /* free the keys after the locks have been dropped */
  748. if (klist)
  749. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  750. ret = 0;
  751. }
  752. return ret;
  753. } /* end keyring_clear() */
  754. EXPORT_SYMBOL(keyring_clear);