keyring.c 23 KB

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