keyring.c 22 KB

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