keyring.c 23 KB

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