keyring.c 23 KB

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