keyring.c 23 KB

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