keyring.c 22 KB

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