keyring.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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 <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. static 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, kflags;
  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. /* firstly we should check to see if this top-level keyring is what we
  264. * are looking for */
  265. key_ref = ERR_PTR(-EAGAIN);
  266. kflags = keyring->flags;
  267. if (keyring->type == type && match(keyring, description)) {
  268. key = keyring;
  269. /* check it isn't negative and hasn't expired or been
  270. * revoked */
  271. if (kflags & (1 << KEY_FLAG_REVOKED))
  272. goto error_2;
  273. if (key->expiry && now.tv_sec >= key->expiry)
  274. goto error_2;
  275. key_ref = ERR_PTR(-ENOKEY);
  276. if (kflags & (1 << KEY_FLAG_NEGATIVE))
  277. goto error_2;
  278. goto found;
  279. }
  280. /* otherwise, the top keyring must not be revoked, expired, or
  281. * negatively instantiated if we are to search it */
  282. key_ref = ERR_PTR(-EAGAIN);
  283. if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) ||
  284. (keyring->expiry && now.tv_sec >= keyring->expiry))
  285. goto error_2;
  286. /* start processing a new keyring */
  287. descend:
  288. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  289. goto not_this_keyring;
  290. keylist = rcu_dereference(keyring->payload.subscriptions);
  291. if (!keylist)
  292. goto not_this_keyring;
  293. /* iterate through the keys in this keyring first */
  294. for (kix = 0; kix < keylist->nkeys; kix++) {
  295. key = keylist->keys[kix];
  296. kflags = key->flags;
  297. /* ignore keys not of this type */
  298. if (key->type != type)
  299. continue;
  300. /* skip revoked keys and expired keys */
  301. if (kflags & (1 << KEY_FLAG_REVOKED))
  302. continue;
  303. if (key->expiry && now.tv_sec >= key->expiry)
  304. continue;
  305. /* keys that don't match */
  306. if (!match(key, description))
  307. continue;
  308. /* key must have search permissions */
  309. if (key_task_permission(make_key_ref(key, possessed),
  310. context, KEY_SEARCH) < 0)
  311. continue;
  312. /* we set a different error code if we pass a negative key */
  313. if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
  314. err = -ENOKEY;
  315. continue;
  316. }
  317. goto found;
  318. }
  319. /* search through the keyrings nested in this one */
  320. kix = 0;
  321. ascend:
  322. for (; kix < keylist->nkeys; kix++) {
  323. key = keylist->keys[kix];
  324. if (key->type != &key_type_keyring)
  325. continue;
  326. /* recursively search nested keyrings
  327. * - only search keyrings for which we have search permission
  328. */
  329. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  330. continue;
  331. if (key_task_permission(make_key_ref(key, possessed),
  332. context, KEY_SEARCH) < 0)
  333. continue;
  334. /* stack the current position */
  335. stack[sp].keylist = keylist;
  336. stack[sp].kix = kix;
  337. sp++;
  338. /* begin again with the new keyring */
  339. keyring = key;
  340. goto descend;
  341. }
  342. /* the keyring we're looking at was disqualified or didn't contain a
  343. * matching key */
  344. not_this_keyring:
  345. if (sp > 0) {
  346. /* resume the processing of a keyring higher up in the tree */
  347. sp--;
  348. keylist = stack[sp].keylist;
  349. kix = stack[sp].kix + 1;
  350. goto ascend;
  351. }
  352. key_ref = ERR_PTR(err);
  353. goto error_2;
  354. /* we found a viable match */
  355. found:
  356. atomic_inc(&key->usage);
  357. key_check(key);
  358. key_ref = make_key_ref(key, possessed);
  359. error_2:
  360. rcu_read_unlock();
  361. error:
  362. return key_ref;
  363. } /* end keyring_search_aux() */
  364. /*****************************************************************************/
  365. /*
  366. * search the supplied keyring tree for a key that matches the criterion
  367. * - perform a breadth-then-depth search up to the prescribed limit
  368. * - we only find keys on which we have search permission
  369. * - we readlock the keyrings as we search down the tree
  370. * - we return -EAGAIN if we didn't find any matching key
  371. * - we return -ENOKEY if we only found negative matching keys
  372. */
  373. key_ref_t keyring_search(key_ref_t keyring,
  374. struct key_type *type,
  375. const char *description)
  376. {
  377. if (!type->match)
  378. return ERR_PTR(-ENOKEY);
  379. return keyring_search_aux(keyring, current,
  380. type, description, type->match);
  381. } /* end keyring_search() */
  382. EXPORT_SYMBOL(keyring_search);
  383. /*****************************************************************************/
  384. /*
  385. * search the given keyring only (no recursion)
  386. * - keyring must be locked by caller
  387. * - caller must guarantee that the keyring is a keyring
  388. */
  389. key_ref_t __keyring_search_one(key_ref_t keyring_ref,
  390. const struct key_type *ktype,
  391. const char *description,
  392. key_perm_t perm)
  393. {
  394. struct keyring_list *klist;
  395. unsigned long possessed;
  396. struct key *keyring, *key;
  397. int loop;
  398. keyring = key_ref_to_ptr(keyring_ref);
  399. possessed = is_key_possessed(keyring_ref);
  400. rcu_read_lock();
  401. klist = rcu_dereference(keyring->payload.subscriptions);
  402. if (klist) {
  403. for (loop = 0; loop < klist->nkeys; loop++) {
  404. key = klist->keys[loop];
  405. if (key->type == ktype &&
  406. (!key->type->match ||
  407. key->type->match(key, description)) &&
  408. key_permission(make_key_ref(key, possessed),
  409. perm) == 0 &&
  410. !test_bit(KEY_FLAG_REVOKED, &key->flags)
  411. )
  412. goto found;
  413. }
  414. }
  415. rcu_read_unlock();
  416. return ERR_PTR(-ENOKEY);
  417. found:
  418. atomic_inc(&key->usage);
  419. rcu_read_unlock();
  420. return make_key_ref(key, possessed);
  421. } /* end __keyring_search_one() */
  422. /*****************************************************************************/
  423. /*
  424. * find a keyring with the specified name
  425. * - all named keyrings are searched
  426. * - normally only finds keyrings with search permission for the current process
  427. */
  428. struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
  429. {
  430. struct key *keyring;
  431. int bucket;
  432. keyring = ERR_PTR(-EINVAL);
  433. if (!name)
  434. goto error;
  435. bucket = keyring_hash(name);
  436. read_lock(&keyring_name_lock);
  437. if (keyring_name_hash[bucket].next) {
  438. /* search this hash bucket for a keyring with a matching name
  439. * that's readable and that hasn't been revoked */
  440. list_for_each_entry(keyring,
  441. &keyring_name_hash[bucket],
  442. type_data.link
  443. ) {
  444. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  445. continue;
  446. if (strcmp(keyring->description, name) != 0)
  447. continue;
  448. if (!skip_perm_check &&
  449. key_permission(make_key_ref(keyring, 0),
  450. KEY_SEARCH) < 0)
  451. continue;
  452. /* we've got a match */
  453. atomic_inc(&keyring->usage);
  454. read_unlock(&keyring_name_lock);
  455. goto error;
  456. }
  457. }
  458. read_unlock(&keyring_name_lock);
  459. keyring = ERR_PTR(-ENOKEY);
  460. error:
  461. return keyring;
  462. } /* end find_keyring_by_name() */
  463. /*****************************************************************************/
  464. /*
  465. * see if a cycle will will be created by inserting acyclic tree B in acyclic
  466. * tree A at the topmost level (ie: as a direct child of A)
  467. * - since we are adding B to A at the top level, checking for cycles should
  468. * just be a matter of seeing if node A is somewhere in tree B
  469. */
  470. static int keyring_detect_cycle(struct key *A, struct key *B)
  471. {
  472. struct {
  473. struct keyring_list *keylist;
  474. int kix;
  475. } stack[KEYRING_SEARCH_MAX_DEPTH];
  476. struct keyring_list *keylist;
  477. struct key *subtree, *key;
  478. int sp, kix, ret;
  479. rcu_read_lock();
  480. ret = -EDEADLK;
  481. if (A == B)
  482. goto cycle_detected;
  483. subtree = B;
  484. sp = 0;
  485. /* start processing a new keyring */
  486. descend:
  487. if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
  488. goto not_this_keyring;
  489. keylist = rcu_dereference(subtree->payload.subscriptions);
  490. if (!keylist)
  491. goto not_this_keyring;
  492. kix = 0;
  493. ascend:
  494. /* iterate through the remaining keys in this keyring */
  495. for (; kix < keylist->nkeys; kix++) {
  496. key = keylist->keys[kix];
  497. if (key == A)
  498. goto cycle_detected;
  499. /* recursively check nested keyrings */
  500. if (key->type == &key_type_keyring) {
  501. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  502. goto too_deep;
  503. /* stack the current position */
  504. stack[sp].keylist = keylist;
  505. stack[sp].kix = kix;
  506. sp++;
  507. /* begin again with the new keyring */
  508. subtree = key;
  509. goto descend;
  510. }
  511. }
  512. /* the keyring we're looking at was disqualified or didn't contain a
  513. * matching key */
  514. not_this_keyring:
  515. if (sp > 0) {
  516. /* resume the checking of a keyring higher up in the tree */
  517. sp--;
  518. keylist = stack[sp].keylist;
  519. kix = stack[sp].kix + 1;
  520. goto ascend;
  521. }
  522. ret = 0; /* no cycles detected */
  523. error:
  524. rcu_read_unlock();
  525. return ret;
  526. too_deep:
  527. ret = -ELOOP;
  528. goto error;
  529. cycle_detected:
  530. ret = -EDEADLK;
  531. goto error;
  532. } /* end keyring_detect_cycle() */
  533. /*****************************************************************************/
  534. /*
  535. * dispose of a keyring list after the RCU grace period
  536. */
  537. static void keyring_link_rcu_disposal(struct rcu_head *rcu)
  538. {
  539. struct keyring_list *klist =
  540. container_of(rcu, struct keyring_list, rcu);
  541. kfree(klist);
  542. } /* end keyring_link_rcu_disposal() */
  543. /*****************************************************************************/
  544. /*
  545. * dispose of a keyring list after the RCU grace period, freeing the unlinked
  546. * key
  547. */
  548. static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
  549. {
  550. struct keyring_list *klist =
  551. container_of(rcu, struct keyring_list, rcu);
  552. key_put(klist->keys[klist->delkey]);
  553. kfree(klist);
  554. } /* end keyring_unlink_rcu_disposal() */
  555. /*****************************************************************************/
  556. /*
  557. * link a key into to a keyring
  558. * - must be called with the keyring's semaphore write-locked
  559. * - discard already extant link to matching key if there is one
  560. */
  561. int __key_link(struct key *keyring, struct key *key)
  562. {
  563. struct keyring_list *klist, *nklist;
  564. unsigned max;
  565. size_t size;
  566. int loop, ret;
  567. ret = -EKEYREVOKED;
  568. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  569. goto error;
  570. ret = -ENOTDIR;
  571. if (keyring->type != &key_type_keyring)
  572. goto error;
  573. /* serialise link/link calls to prevent parallel calls causing a
  574. * cycle when applied to two keyring in opposite orders */
  575. down_write(&keyring_serialise_link_sem);
  576. /* check that we aren't going to create a cycle adding one keyring to
  577. * another */
  578. if (key->type == &key_type_keyring) {
  579. ret = keyring_detect_cycle(keyring, key);
  580. if (ret < 0)
  581. goto error2;
  582. }
  583. /* see if there's a matching key we can displace */
  584. klist = keyring->payload.subscriptions;
  585. if (klist && klist->nkeys > 0) {
  586. struct key_type *type = key->type;
  587. for (loop = klist->nkeys - 1; loop >= 0; loop--) {
  588. if (klist->keys[loop]->type == type &&
  589. strcmp(klist->keys[loop]->description,
  590. key->description) == 0
  591. ) {
  592. /* found a match - replace with new key */
  593. size = sizeof(struct key *) * klist->maxkeys;
  594. size += sizeof(*klist);
  595. BUG_ON(size > PAGE_SIZE);
  596. ret = -ENOMEM;
  597. nklist = kmemdup(klist, size, GFP_KERNEL);
  598. if (!nklist)
  599. goto error2;
  600. /* replace matched key */
  601. atomic_inc(&key->usage);
  602. nklist->keys[loop] = key;
  603. rcu_assign_pointer(
  604. keyring->payload.subscriptions,
  605. nklist);
  606. /* dispose of the old keyring list and the
  607. * displaced key */
  608. klist->delkey = loop;
  609. call_rcu(&klist->rcu,
  610. keyring_unlink_rcu_disposal);
  611. goto done;
  612. }
  613. }
  614. }
  615. /* check that we aren't going to overrun the user's quota */
  616. ret = key_payload_reserve(keyring,
  617. keyring->datalen + KEYQUOTA_LINK_BYTES);
  618. if (ret < 0)
  619. goto error2;
  620. klist = keyring->payload.subscriptions;
  621. if (klist && klist->nkeys < klist->maxkeys) {
  622. /* there's sufficient slack space to add directly */
  623. atomic_inc(&key->usage);
  624. klist->keys[klist->nkeys] = key;
  625. smp_wmb();
  626. klist->nkeys++;
  627. smp_wmb();
  628. }
  629. else {
  630. /* grow the key list */
  631. max = 4;
  632. if (klist)
  633. max += klist->maxkeys;
  634. ret = -ENFILE;
  635. if (max > 65535)
  636. goto error3;
  637. size = sizeof(*klist) + sizeof(struct key *) * max;
  638. if (size > PAGE_SIZE)
  639. goto error3;
  640. ret = -ENOMEM;
  641. nklist = kmalloc(size, GFP_KERNEL);
  642. if (!nklist)
  643. goto error3;
  644. nklist->maxkeys = max;
  645. nklist->nkeys = 0;
  646. if (klist) {
  647. nklist->nkeys = klist->nkeys;
  648. memcpy(nklist->keys,
  649. klist->keys,
  650. sizeof(struct key *) * klist->nkeys);
  651. }
  652. /* add the key into the new space */
  653. atomic_inc(&key->usage);
  654. nklist->keys[nklist->nkeys++] = key;
  655. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  656. /* dispose of the old keyring list */
  657. if (klist)
  658. call_rcu(&klist->rcu, keyring_link_rcu_disposal);
  659. }
  660. done:
  661. ret = 0;
  662. error2:
  663. up_write(&keyring_serialise_link_sem);
  664. error:
  665. return ret;
  666. error3:
  667. /* undo the quota changes */
  668. key_payload_reserve(keyring,
  669. keyring->datalen - KEYQUOTA_LINK_BYTES);
  670. goto error2;
  671. } /* end __key_link() */
  672. /*****************************************************************************/
  673. /*
  674. * link a key to a keyring
  675. */
  676. int key_link(struct key *keyring, struct key *key)
  677. {
  678. int ret;
  679. key_check(keyring);
  680. key_check(key);
  681. down_write(&keyring->sem);
  682. ret = __key_link(keyring, key);
  683. up_write(&keyring->sem);
  684. return ret;
  685. } /* end key_link() */
  686. EXPORT_SYMBOL(key_link);
  687. /*****************************************************************************/
  688. /*
  689. * unlink the first link to a key from a keyring
  690. */
  691. int key_unlink(struct key *keyring, struct key *key)
  692. {
  693. struct keyring_list *klist, *nklist;
  694. int loop, ret;
  695. key_check(keyring);
  696. key_check(key);
  697. ret = -ENOTDIR;
  698. if (keyring->type != &key_type_keyring)
  699. goto error;
  700. down_write(&keyring->sem);
  701. klist = keyring->payload.subscriptions;
  702. if (klist) {
  703. /* search the keyring for the key */
  704. for (loop = 0; loop < klist->nkeys; loop++)
  705. if (klist->keys[loop] == key)
  706. goto key_is_present;
  707. }
  708. up_write(&keyring->sem);
  709. ret = -ENOENT;
  710. goto error;
  711. key_is_present:
  712. /* we need to copy the key list for RCU purposes */
  713. nklist = kmalloc(sizeof(*klist) +
  714. sizeof(struct key *) * klist->maxkeys,
  715. GFP_KERNEL);
  716. if (!nklist)
  717. goto nomem;
  718. nklist->maxkeys = klist->maxkeys;
  719. nklist->nkeys = klist->nkeys - 1;
  720. if (loop > 0)
  721. memcpy(&nklist->keys[0],
  722. &klist->keys[0],
  723. loop * sizeof(struct key *));
  724. if (loop < nklist->nkeys)
  725. memcpy(&nklist->keys[loop],
  726. &klist->keys[loop + 1],
  727. (nklist->nkeys - loop) * sizeof(struct key *));
  728. /* adjust the user's quota */
  729. key_payload_reserve(keyring,
  730. keyring->datalen - KEYQUOTA_LINK_BYTES);
  731. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  732. up_write(&keyring->sem);
  733. /* schedule for later cleanup */
  734. klist->delkey = loop;
  735. call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
  736. ret = 0;
  737. error:
  738. return ret;
  739. nomem:
  740. ret = -ENOMEM;
  741. up_write(&keyring->sem);
  742. goto error;
  743. } /* end key_unlink() */
  744. EXPORT_SYMBOL(key_unlink);
  745. /*****************************************************************************/
  746. /*
  747. * dispose of a keyring list after the RCU grace period, releasing the keys it
  748. * links to
  749. */
  750. static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
  751. {
  752. struct keyring_list *klist;
  753. int loop;
  754. klist = container_of(rcu, struct keyring_list, rcu);
  755. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  756. key_put(klist->keys[loop]);
  757. kfree(klist);
  758. } /* end keyring_clear_rcu_disposal() */
  759. /*****************************************************************************/
  760. /*
  761. * clear the specified process keyring
  762. * - implements keyctl(KEYCTL_CLEAR)
  763. */
  764. int keyring_clear(struct key *keyring)
  765. {
  766. struct keyring_list *klist;
  767. int ret;
  768. ret = -ENOTDIR;
  769. if (keyring->type == &key_type_keyring) {
  770. /* detach the pointer block with the locks held */
  771. down_write(&keyring->sem);
  772. klist = keyring->payload.subscriptions;
  773. if (klist) {
  774. /* adjust the quota */
  775. key_payload_reserve(keyring,
  776. sizeof(struct keyring_list));
  777. rcu_assign_pointer(keyring->payload.subscriptions,
  778. NULL);
  779. }
  780. up_write(&keyring->sem);
  781. /* free the keys after the locks have been dropped */
  782. if (klist)
  783. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  784. ret = 0;
  785. }
  786. return ret;
  787. } /* end keyring_clear() */
  788. EXPORT_SYMBOL(keyring_clear);
  789. /*****************************************************************************/
  790. /*
  791. * dispose of the links from a revoked keyring
  792. * - called with the key sem write-locked
  793. */
  794. static void keyring_revoke(struct key *keyring)
  795. {
  796. struct keyring_list *klist = keyring->payload.subscriptions;
  797. /* adjust the quota */
  798. key_payload_reserve(keyring, 0);
  799. if (klist) {
  800. rcu_assign_pointer(keyring->payload.subscriptions, NULL);
  801. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  802. }
  803. } /* end keyring_revoke() */