keyring.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. * find a keyring with the specified name
  396. * - all named keyrings are searched
  397. * - only find keyrings with search permission for the process
  398. * - only find keyrings with a serial number greater than the one specified
  399. */
  400. struct key *find_keyring_by_name(const char *name, key_serial_t bound)
  401. {
  402. struct key *keyring;
  403. int bucket;
  404. keyring = ERR_PTR(-EINVAL);
  405. if (!name)
  406. goto error;
  407. bucket = keyring_hash(name);
  408. read_lock(&keyring_name_lock);
  409. if (keyring_name_hash[bucket].next) {
  410. /* search this hash bucket for a keyring with a matching name
  411. * that's readable and that hasn't been revoked */
  412. list_for_each_entry(keyring,
  413. &keyring_name_hash[bucket],
  414. type_data.link
  415. ) {
  416. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  417. continue;
  418. if (strcmp(keyring->description, name) != 0)
  419. continue;
  420. if (key_permission(make_key_ref(keyring, 0),
  421. KEY_SEARCH) < 0)
  422. continue;
  423. /* found a potential candidate, but we still need to
  424. * check the serial number */
  425. if (keyring->serial <= bound)
  426. continue;
  427. /* we've got a match */
  428. atomic_inc(&keyring->usage);
  429. read_unlock(&keyring_name_lock);
  430. goto error;
  431. }
  432. }
  433. read_unlock(&keyring_name_lock);
  434. keyring = ERR_PTR(-ENOKEY);
  435. error:
  436. return keyring;
  437. } /* end find_keyring_by_name() */
  438. /*****************************************************************************/
  439. /*
  440. * see if a cycle will will be created by inserting acyclic tree B in acyclic
  441. * tree A at the topmost level (ie: as a direct child of A)
  442. * - since we are adding B to A at the top level, checking for cycles should
  443. * just be a matter of seeing if node A is somewhere in tree B
  444. */
  445. static int keyring_detect_cycle(struct key *A, struct key *B)
  446. {
  447. struct {
  448. struct keyring_list *keylist;
  449. int kix;
  450. } stack[KEYRING_SEARCH_MAX_DEPTH];
  451. struct keyring_list *keylist;
  452. struct key *subtree, *key;
  453. int sp, kix, ret;
  454. rcu_read_lock();
  455. ret = -EDEADLK;
  456. if (A == B)
  457. goto cycle_detected;
  458. subtree = B;
  459. sp = 0;
  460. /* start processing a new keyring */
  461. descend:
  462. if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
  463. goto not_this_keyring;
  464. keylist = rcu_dereference(subtree->payload.subscriptions);
  465. if (!keylist)
  466. goto not_this_keyring;
  467. kix = 0;
  468. ascend:
  469. /* iterate through the remaining keys in this keyring */
  470. for (; kix < keylist->nkeys; kix++) {
  471. key = keylist->keys[kix];
  472. if (key == A)
  473. goto cycle_detected;
  474. /* recursively check nested keyrings */
  475. if (key->type == &key_type_keyring) {
  476. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  477. goto too_deep;
  478. /* stack the current position */
  479. stack[sp].keylist = keylist;
  480. stack[sp].kix = kix;
  481. sp++;
  482. /* begin again with the new keyring */
  483. subtree = key;
  484. goto descend;
  485. }
  486. }
  487. /* the keyring we're looking at was disqualified or didn't contain a
  488. * matching key */
  489. not_this_keyring:
  490. if (sp > 0) {
  491. /* resume the checking of a keyring higher up in the tree */
  492. sp--;
  493. keylist = stack[sp].keylist;
  494. kix = stack[sp].kix + 1;
  495. goto ascend;
  496. }
  497. ret = 0; /* no cycles detected */
  498. error:
  499. rcu_read_unlock();
  500. return ret;
  501. too_deep:
  502. ret = -ELOOP;
  503. goto error;
  504. cycle_detected:
  505. ret = -EDEADLK;
  506. goto error;
  507. } /* end keyring_detect_cycle() */
  508. /*****************************************************************************/
  509. /*
  510. * dispose of a keyring list after the RCU grace period
  511. */
  512. static void keyring_link_rcu_disposal(struct rcu_head *rcu)
  513. {
  514. struct keyring_list *klist =
  515. container_of(rcu, struct keyring_list, rcu);
  516. kfree(klist);
  517. } /* end keyring_link_rcu_disposal() */
  518. /*****************************************************************************/
  519. /*
  520. * dispose of a keyring list after the RCU grace period, freeing the unlinked
  521. * key
  522. */
  523. static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
  524. {
  525. struct keyring_list *klist =
  526. container_of(rcu, struct keyring_list, rcu);
  527. key_put(klist->keys[klist->delkey]);
  528. kfree(klist);
  529. } /* end keyring_unlink_rcu_disposal() */
  530. /*****************************************************************************/
  531. /*
  532. * link a key into to a keyring
  533. * - must be called with the keyring's semaphore write-locked
  534. * - discard already extant link to matching key if there is one
  535. */
  536. int __key_link(struct key *keyring, struct key *key)
  537. {
  538. struct keyring_list *klist, *nklist;
  539. unsigned max;
  540. size_t size;
  541. int loop, ret;
  542. ret = -EKEYREVOKED;
  543. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  544. goto error;
  545. ret = -ENOTDIR;
  546. if (keyring->type != &key_type_keyring)
  547. goto error;
  548. /* serialise link/link calls to prevent parallel calls causing a
  549. * cycle when applied to two keyring in opposite orders */
  550. down_write(&keyring_serialise_link_sem);
  551. /* check that we aren't going to create a cycle adding one keyring to
  552. * another */
  553. if (key->type == &key_type_keyring) {
  554. ret = keyring_detect_cycle(keyring, key);
  555. if (ret < 0)
  556. goto error2;
  557. }
  558. /* see if there's a matching key we can displace */
  559. klist = keyring->payload.subscriptions;
  560. if (klist && klist->nkeys > 0) {
  561. struct key_type *type = key->type;
  562. for (loop = klist->nkeys - 1; loop >= 0; loop--) {
  563. if (klist->keys[loop]->type == type &&
  564. strcmp(klist->keys[loop]->description,
  565. key->description) == 0
  566. ) {
  567. /* found a match - replace with new key */
  568. size = sizeof(struct key *) * klist->maxkeys;
  569. size += sizeof(*klist);
  570. BUG_ON(size > PAGE_SIZE);
  571. ret = -ENOMEM;
  572. nklist = kmalloc(size, GFP_KERNEL);
  573. if (!nklist)
  574. goto error2;
  575. memcpy(nklist, klist, size);
  576. /* replace matched key */
  577. atomic_inc(&key->usage);
  578. nklist->keys[loop] = key;
  579. rcu_assign_pointer(
  580. keyring->payload.subscriptions,
  581. nklist);
  582. /* dispose of the old keyring list and the
  583. * displaced key */
  584. klist->delkey = loop;
  585. call_rcu(&klist->rcu,
  586. keyring_unlink_rcu_disposal);
  587. goto done;
  588. }
  589. }
  590. }
  591. /* check that we aren't going to overrun the user's quota */
  592. ret = key_payload_reserve(keyring,
  593. keyring->datalen + KEYQUOTA_LINK_BYTES);
  594. if (ret < 0)
  595. goto error2;
  596. klist = keyring->payload.subscriptions;
  597. if (klist && klist->nkeys < klist->maxkeys) {
  598. /* there's sufficient slack space to add directly */
  599. atomic_inc(&key->usage);
  600. klist->keys[klist->nkeys] = key;
  601. smp_wmb();
  602. klist->nkeys++;
  603. smp_wmb();
  604. }
  605. else {
  606. /* grow the key list */
  607. max = 4;
  608. if (klist)
  609. max += klist->maxkeys;
  610. ret = -ENFILE;
  611. if (max > 65535)
  612. goto error3;
  613. size = sizeof(*klist) + sizeof(struct key *) * max;
  614. if (size > PAGE_SIZE)
  615. goto error3;
  616. ret = -ENOMEM;
  617. nklist = kmalloc(size, GFP_KERNEL);
  618. if (!nklist)
  619. goto error3;
  620. nklist->maxkeys = max;
  621. nklist->nkeys = 0;
  622. if (klist) {
  623. nklist->nkeys = klist->nkeys;
  624. memcpy(nklist->keys,
  625. klist->keys,
  626. sizeof(struct key *) * klist->nkeys);
  627. }
  628. /* add the key into the new space */
  629. atomic_inc(&key->usage);
  630. nklist->keys[nklist->nkeys++] = key;
  631. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  632. /* dispose of the old keyring list */
  633. if (klist)
  634. call_rcu(&klist->rcu, keyring_link_rcu_disposal);
  635. }
  636. done:
  637. ret = 0;
  638. error2:
  639. up_write(&keyring_serialise_link_sem);
  640. error:
  641. return ret;
  642. error3:
  643. /* undo the quota changes */
  644. key_payload_reserve(keyring,
  645. keyring->datalen - KEYQUOTA_LINK_BYTES);
  646. goto error2;
  647. } /* end __key_link() */
  648. /*****************************************************************************/
  649. /*
  650. * link a key to a keyring
  651. */
  652. int key_link(struct key *keyring, struct key *key)
  653. {
  654. int ret;
  655. key_check(keyring);
  656. key_check(key);
  657. down_write(&keyring->sem);
  658. ret = __key_link(keyring, key);
  659. up_write(&keyring->sem);
  660. return ret;
  661. } /* end key_link() */
  662. EXPORT_SYMBOL(key_link);
  663. /*****************************************************************************/
  664. /*
  665. * unlink the first link to a key from a keyring
  666. */
  667. int key_unlink(struct key *keyring, struct key *key)
  668. {
  669. struct keyring_list *klist, *nklist;
  670. int loop, ret;
  671. key_check(keyring);
  672. key_check(key);
  673. ret = -ENOTDIR;
  674. if (keyring->type != &key_type_keyring)
  675. goto error;
  676. down_write(&keyring->sem);
  677. klist = keyring->payload.subscriptions;
  678. if (klist) {
  679. /* search the keyring for the key */
  680. for (loop = 0; loop < klist->nkeys; loop++)
  681. if (klist->keys[loop] == key)
  682. goto key_is_present;
  683. }
  684. up_write(&keyring->sem);
  685. ret = -ENOENT;
  686. goto error;
  687. key_is_present:
  688. /* we need to copy the key list for RCU purposes */
  689. nklist = kmalloc(sizeof(*klist) +
  690. sizeof(struct key *) * klist->maxkeys,
  691. GFP_KERNEL);
  692. if (!nklist)
  693. goto nomem;
  694. nklist->maxkeys = klist->maxkeys;
  695. nklist->nkeys = klist->nkeys - 1;
  696. if (loop > 0)
  697. memcpy(&nklist->keys[0],
  698. &klist->keys[0],
  699. loop * sizeof(struct key *));
  700. if (loop < nklist->nkeys)
  701. memcpy(&nklist->keys[loop],
  702. &klist->keys[loop + 1],
  703. (nklist->nkeys - loop) * sizeof(struct key *));
  704. /* adjust the user's quota */
  705. key_payload_reserve(keyring,
  706. keyring->datalen - KEYQUOTA_LINK_BYTES);
  707. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  708. up_write(&keyring->sem);
  709. /* schedule for later cleanup */
  710. klist->delkey = loop;
  711. call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
  712. ret = 0;
  713. error:
  714. return ret;
  715. nomem:
  716. ret = -ENOMEM;
  717. up_write(&keyring->sem);
  718. goto error;
  719. } /* end key_unlink() */
  720. EXPORT_SYMBOL(key_unlink);
  721. /*****************************************************************************/
  722. /*
  723. * dispose of a keyring list after the RCU grace period, releasing the keys it
  724. * links to
  725. */
  726. static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
  727. {
  728. struct keyring_list *klist;
  729. int loop;
  730. klist = container_of(rcu, struct keyring_list, rcu);
  731. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  732. key_put(klist->keys[loop]);
  733. kfree(klist);
  734. } /* end keyring_clear_rcu_disposal() */
  735. /*****************************************************************************/
  736. /*
  737. * clear the specified process keyring
  738. * - implements keyctl(KEYCTL_CLEAR)
  739. */
  740. int keyring_clear(struct key *keyring)
  741. {
  742. struct keyring_list *klist;
  743. int ret;
  744. ret = -ENOTDIR;
  745. if (keyring->type == &key_type_keyring) {
  746. /* detach the pointer block with the locks held */
  747. down_write(&keyring->sem);
  748. klist = keyring->payload.subscriptions;
  749. if (klist) {
  750. /* adjust the quota */
  751. key_payload_reserve(keyring,
  752. sizeof(struct keyring_list));
  753. rcu_assign_pointer(keyring->payload.subscriptions,
  754. NULL);
  755. }
  756. up_write(&keyring->sem);
  757. /* free the keys after the locks have been dropped */
  758. if (klist)
  759. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  760. ret = 0;
  761. }
  762. return ret;
  763. } /* end keyring_clear() */
  764. EXPORT_SYMBOL(keyring_clear);