keyring.c 24 KB

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