keyring.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. /* Keyring handling
  2. *
  3. * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/security.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/err.h>
  18. #include <keys/keyring-type.h>
  19. #include <linux/uaccess.h>
  20. #include "internal.h"
  21. #define rcu_dereference_locked_keyring(keyring) \
  22. (rcu_dereference_protected( \
  23. (keyring)->payload.subscriptions, \
  24. rwsem_is_locked((struct rw_semaphore *)&(keyring)->sem)))
  25. #define KEY_LINK_FIXQUOTA 1UL
  26. /*
  27. * When plumbing the depths of the key tree, this sets a hard limit
  28. * set on how deep we're willing to go.
  29. */
  30. #define KEYRING_SEARCH_MAX_DEPTH 6
  31. /*
  32. * We keep all named keyrings in a hash to speed looking them up.
  33. */
  34. #define KEYRING_NAME_HASH_SIZE (1 << 5)
  35. static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE];
  36. static DEFINE_RWLOCK(keyring_name_lock);
  37. static inline unsigned keyring_hash(const char *desc)
  38. {
  39. unsigned bucket = 0;
  40. for (; *desc; desc++)
  41. bucket += (unsigned char)*desc;
  42. return bucket & (KEYRING_NAME_HASH_SIZE - 1);
  43. }
  44. /*
  45. * The keyring key type definition. Keyrings are simply keys of this type and
  46. * can be treated as ordinary keys in addition to having their own special
  47. * operations.
  48. */
  49. static int keyring_instantiate(struct key *keyring,
  50. const void *data, size_t datalen);
  51. static int keyring_match(const struct key *keyring, const void *criterion);
  52. static void keyring_revoke(struct key *keyring);
  53. static void keyring_destroy(struct key *keyring);
  54. static void keyring_describe(const struct key *keyring, struct seq_file *m);
  55. static long keyring_read(const struct key *keyring,
  56. char __user *buffer, size_t buflen);
  57. struct key_type key_type_keyring = {
  58. .name = "keyring",
  59. .def_datalen = sizeof(struct keyring_list),
  60. .instantiate = keyring_instantiate,
  61. .match = keyring_match,
  62. .revoke = keyring_revoke,
  63. .destroy = keyring_destroy,
  64. .describe = keyring_describe,
  65. .read = keyring_read,
  66. };
  67. EXPORT_SYMBOL(key_type_keyring);
  68. /*
  69. * Semaphore to serialise link/link calls to prevent two link calls in parallel
  70. * introducing a cycle.
  71. */
  72. static DECLARE_RWSEM(keyring_serialise_link_sem);
  73. /*
  74. * Publish the name of a keyring so that it can be found by name (if it has
  75. * one).
  76. */
  77. static void keyring_publish_name(struct key *keyring)
  78. {
  79. int bucket;
  80. if (keyring->description) {
  81. bucket = keyring_hash(keyring->description);
  82. write_lock(&keyring_name_lock);
  83. if (!keyring_name_hash[bucket].next)
  84. INIT_LIST_HEAD(&keyring_name_hash[bucket]);
  85. list_add_tail(&keyring->type_data.link,
  86. &keyring_name_hash[bucket]);
  87. write_unlock(&keyring_name_lock);
  88. }
  89. }
  90. /*
  91. * Initialise a keyring.
  92. *
  93. * Returns 0 on success, -EINVAL if given any data.
  94. */
  95. static int keyring_instantiate(struct key *keyring,
  96. const void *data, size_t datalen)
  97. {
  98. int ret;
  99. ret = -EINVAL;
  100. if (datalen == 0) {
  101. /* make the keyring available by name if it has one */
  102. keyring_publish_name(keyring);
  103. ret = 0;
  104. }
  105. return ret;
  106. }
  107. /*
  108. * Match keyrings on their name
  109. */
  110. static int keyring_match(const struct key *keyring, const void *description)
  111. {
  112. return keyring->description &&
  113. strcmp(keyring->description, description) == 0;
  114. }
  115. /*
  116. * Clean up a keyring when it is destroyed. Unpublish its name if it had one
  117. * and dispose of its data.
  118. */
  119. static void keyring_destroy(struct key *keyring)
  120. {
  121. struct keyring_list *klist;
  122. int loop;
  123. if (keyring->description) {
  124. write_lock(&keyring_name_lock);
  125. if (keyring->type_data.link.next != NULL &&
  126. !list_empty(&keyring->type_data.link))
  127. list_del(&keyring->type_data.link);
  128. write_unlock(&keyring_name_lock);
  129. }
  130. klist = rcu_dereference_check(keyring->payload.subscriptions,
  131. rcu_read_lock_held() ||
  132. atomic_read(&keyring->usage) == 0);
  133. if (klist) {
  134. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  135. key_put(klist->keys[loop]);
  136. kfree(klist);
  137. }
  138. }
  139. /*
  140. * Describe a keyring for /proc.
  141. */
  142. static void keyring_describe(const struct key *keyring, struct seq_file *m)
  143. {
  144. struct keyring_list *klist;
  145. if (keyring->description)
  146. seq_puts(m, keyring->description);
  147. else
  148. seq_puts(m, "[anon]");
  149. if (key_is_instantiated(keyring)) {
  150. rcu_read_lock();
  151. klist = rcu_dereference(keyring->payload.subscriptions);
  152. if (klist)
  153. seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
  154. else
  155. seq_puts(m, ": empty");
  156. rcu_read_unlock();
  157. }
  158. }
  159. /*
  160. * Read a list of key IDs from the keyring's contents in binary form
  161. *
  162. * The keyring's semaphore is read-locked by the caller.
  163. */
  164. static long keyring_read(const struct key *keyring,
  165. char __user *buffer, size_t buflen)
  166. {
  167. struct keyring_list *klist;
  168. struct key *key;
  169. size_t qty, tmp;
  170. int loop, ret;
  171. ret = 0;
  172. klist = rcu_dereference_locked_keyring(keyring);
  173. if (klist) {
  174. /* calculate how much data we could return */
  175. qty = klist->nkeys * sizeof(key_serial_t);
  176. if (buffer && buflen > 0) {
  177. if (buflen > qty)
  178. buflen = qty;
  179. /* copy the IDs of the subscribed keys into the
  180. * buffer */
  181. ret = -EFAULT;
  182. for (loop = 0; loop < klist->nkeys; loop++) {
  183. key = klist->keys[loop];
  184. tmp = sizeof(key_serial_t);
  185. if (tmp > buflen)
  186. tmp = buflen;
  187. if (copy_to_user(buffer,
  188. &key->serial,
  189. tmp) != 0)
  190. goto error;
  191. buflen -= tmp;
  192. if (buflen == 0)
  193. break;
  194. buffer += tmp;
  195. }
  196. }
  197. ret = qty;
  198. }
  199. error:
  200. return ret;
  201. }
  202. /*
  203. * Allocate a keyring and link into the destination keyring.
  204. */
  205. struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
  206. const struct cred *cred, unsigned long flags,
  207. struct key *dest)
  208. {
  209. struct key *keyring;
  210. int ret;
  211. keyring = key_alloc(&key_type_keyring, description,
  212. uid, gid, cred,
  213. (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
  214. flags);
  215. if (!IS_ERR(keyring)) {
  216. ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
  217. if (ret < 0) {
  218. key_put(keyring);
  219. keyring = ERR_PTR(ret);
  220. }
  221. }
  222. return keyring;
  223. }
  224. /**
  225. * keyring_search_aux - Search a keyring tree for a key matching some criteria
  226. * @keyring_ref: A pointer to the keyring with possession indicator.
  227. * @cred: The credentials to use for permissions checks.
  228. * @type: The type of key to search for.
  229. * @description: Parameter for @match.
  230. * @match: Function to rule on whether or not a key is the one required.
  231. * @no_state_check: Don't check if a matching key is bad
  232. *
  233. * Search the supplied keyring tree for a key that matches the criteria given.
  234. * The root keyring and any linked keyrings must grant Search permission to the
  235. * caller to be searchable and keys can only be found if they too grant Search
  236. * to the caller. The possession flag on the root keyring pointer controls use
  237. * of the possessor bits in permissions checking of the entire tree. In
  238. * addition, the LSM gets to forbid keyring searches and key matches.
  239. *
  240. * The search is performed as a breadth-then-depth search up to the prescribed
  241. * limit (KEYRING_SEARCH_MAX_DEPTH).
  242. *
  243. * Keys are matched to the type provided and are then filtered by the match
  244. * function, which is given the description to use in any way it sees fit. The
  245. * match function may use any attributes of a key that it wishes to to
  246. * determine the match. Normally the match function from the key type would be
  247. * used.
  248. *
  249. * RCU is used to prevent the keyring key lists from disappearing without the
  250. * need to take lots of locks.
  251. *
  252. * Returns a pointer to the found key and increments the key usage count if
  253. * successful; -EAGAIN if no matching keys were found, or if expired or revoked
  254. * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
  255. * specified keyring wasn't a keyring.
  256. *
  257. * In the case of a successful return, the possession attribute from
  258. * @keyring_ref is propagated to the returned key reference.
  259. */
  260. key_ref_t keyring_search_aux(key_ref_t keyring_ref,
  261. const struct cred *cred,
  262. struct key_type *type,
  263. const void *description,
  264. key_match_func_t match,
  265. bool no_state_check)
  266. {
  267. struct {
  268. struct keyring_list *keylist;
  269. int kix;
  270. } stack[KEYRING_SEARCH_MAX_DEPTH];
  271. struct keyring_list *keylist;
  272. struct timespec now;
  273. unsigned long possessed, kflags;
  274. struct key *keyring, *key;
  275. key_ref_t key_ref;
  276. long err;
  277. int sp, kix;
  278. keyring = key_ref_to_ptr(keyring_ref);
  279. possessed = is_key_possessed(keyring_ref);
  280. key_check(keyring);
  281. /* top keyring must have search permission to begin the search */
  282. err = key_task_permission(keyring_ref, cred, KEY_SEARCH);
  283. if (err < 0) {
  284. key_ref = ERR_PTR(err);
  285. goto error;
  286. }
  287. key_ref = ERR_PTR(-ENOTDIR);
  288. if (keyring->type != &key_type_keyring)
  289. goto error;
  290. rcu_read_lock();
  291. now = current_kernel_time();
  292. err = -EAGAIN;
  293. sp = 0;
  294. /* firstly we should check to see if this top-level keyring is what we
  295. * are looking for */
  296. key_ref = ERR_PTR(-EAGAIN);
  297. kflags = keyring->flags;
  298. if (keyring->type == type && match(keyring, description)) {
  299. key = keyring;
  300. if (no_state_check)
  301. goto found;
  302. /* check it isn't negative and hasn't expired or been
  303. * revoked */
  304. if (kflags & (1 << KEY_FLAG_REVOKED))
  305. goto error_2;
  306. if (key->expiry && now.tv_sec >= key->expiry)
  307. goto error_2;
  308. key_ref = ERR_PTR(key->type_data.reject_error);
  309. if (kflags & (1 << KEY_FLAG_NEGATIVE))
  310. goto error_2;
  311. goto found;
  312. }
  313. /* otherwise, the top keyring must not be revoked, expired, or
  314. * negatively instantiated if we are to search it */
  315. key_ref = ERR_PTR(-EAGAIN);
  316. if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) ||
  317. (keyring->expiry && now.tv_sec >= keyring->expiry))
  318. goto error_2;
  319. /* start processing a new keyring */
  320. descend:
  321. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  322. goto not_this_keyring;
  323. keylist = rcu_dereference(keyring->payload.subscriptions);
  324. if (!keylist)
  325. goto not_this_keyring;
  326. /* iterate through the keys in this keyring first */
  327. for (kix = 0; kix < keylist->nkeys; kix++) {
  328. key = keylist->keys[kix];
  329. kflags = key->flags;
  330. /* ignore keys not of this type */
  331. if (key->type != type)
  332. continue;
  333. /* skip revoked keys and expired keys */
  334. if (!no_state_check) {
  335. if (kflags & (1 << KEY_FLAG_REVOKED))
  336. continue;
  337. if (key->expiry && now.tv_sec >= key->expiry)
  338. continue;
  339. }
  340. /* keys that don't match */
  341. if (!match(key, description))
  342. continue;
  343. /* key must have search permissions */
  344. if (key_task_permission(make_key_ref(key, possessed),
  345. cred, KEY_SEARCH) < 0)
  346. continue;
  347. if (no_state_check)
  348. goto found;
  349. /* we set a different error code if we pass a negative key */
  350. if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
  351. err = key->type_data.reject_error;
  352. continue;
  353. }
  354. goto found;
  355. }
  356. /* search through the keyrings nested in this one */
  357. kix = 0;
  358. ascend:
  359. for (; kix < keylist->nkeys; kix++) {
  360. key = keylist->keys[kix];
  361. if (key->type != &key_type_keyring)
  362. continue;
  363. /* recursively search nested keyrings
  364. * - only search keyrings for which we have search permission
  365. */
  366. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  367. continue;
  368. if (key_task_permission(make_key_ref(key, possessed),
  369. cred, KEY_SEARCH) < 0)
  370. continue;
  371. /* stack the current position */
  372. stack[sp].keylist = keylist;
  373. stack[sp].kix = kix;
  374. sp++;
  375. /* begin again with the new keyring */
  376. keyring = key;
  377. goto descend;
  378. }
  379. /* the keyring we're looking at was disqualified or didn't contain a
  380. * matching key */
  381. not_this_keyring:
  382. if (sp > 0) {
  383. /* resume the processing of a keyring higher up in the tree */
  384. sp--;
  385. keylist = stack[sp].keylist;
  386. kix = stack[sp].kix + 1;
  387. goto ascend;
  388. }
  389. key_ref = ERR_PTR(err);
  390. goto error_2;
  391. /* we found a viable match */
  392. found:
  393. atomic_inc(&key->usage);
  394. key_check(key);
  395. key_ref = make_key_ref(key, possessed);
  396. error_2:
  397. rcu_read_unlock();
  398. error:
  399. return key_ref;
  400. }
  401. /**
  402. * keyring_search - Search the supplied keyring tree for a matching key
  403. * @keyring: The root of the keyring tree to be searched.
  404. * @type: The type of keyring we want to find.
  405. * @description: The name of the keyring we want to find.
  406. *
  407. * As keyring_search_aux() above, but using the current task's credentials and
  408. * type's default matching function.
  409. */
  410. key_ref_t keyring_search(key_ref_t keyring,
  411. struct key_type *type,
  412. const char *description)
  413. {
  414. if (!type->match)
  415. return ERR_PTR(-ENOKEY);
  416. return keyring_search_aux(keyring, current->cred,
  417. type, description, type->match, false);
  418. }
  419. EXPORT_SYMBOL(keyring_search);
  420. /*
  421. * Search the given keyring only (no recursion).
  422. *
  423. * The caller must guarantee that the keyring is a keyring and that the
  424. * permission is granted to search the keyring as no check is made here.
  425. *
  426. * RCU is used to make it unnecessary to lock the keyring key list here.
  427. *
  428. * Returns a pointer to the found key with usage count incremented if
  429. * successful and returns -ENOKEY if not found. Revoked keys and keys not
  430. * providing the requested permission are skipped over.
  431. *
  432. * If successful, the possession indicator is propagated from the keyring ref
  433. * to the returned key reference.
  434. */
  435. key_ref_t __keyring_search_one(key_ref_t keyring_ref,
  436. const struct key_type *ktype,
  437. const char *description,
  438. key_perm_t perm)
  439. {
  440. struct keyring_list *klist;
  441. unsigned long possessed;
  442. struct key *keyring, *key;
  443. int loop;
  444. keyring = key_ref_to_ptr(keyring_ref);
  445. possessed = is_key_possessed(keyring_ref);
  446. rcu_read_lock();
  447. klist = rcu_dereference(keyring->payload.subscriptions);
  448. if (klist) {
  449. for (loop = 0; loop < klist->nkeys; loop++) {
  450. key = klist->keys[loop];
  451. if (key->type == ktype &&
  452. (!key->type->match ||
  453. key->type->match(key, description)) &&
  454. key_permission(make_key_ref(key, possessed),
  455. perm) == 0 &&
  456. !test_bit(KEY_FLAG_REVOKED, &key->flags)
  457. )
  458. goto found;
  459. }
  460. }
  461. rcu_read_unlock();
  462. return ERR_PTR(-ENOKEY);
  463. found:
  464. atomic_inc(&key->usage);
  465. rcu_read_unlock();
  466. return make_key_ref(key, possessed);
  467. }
  468. /*
  469. * Find a keyring with the specified name.
  470. *
  471. * All named keyrings in the current user namespace are searched, provided they
  472. * grant Search permission directly to the caller (unless this check is
  473. * skipped). Keyrings whose usage points have reached zero or who have been
  474. * revoked are skipped.
  475. *
  476. * Returns a pointer to the keyring with the keyring's refcount having being
  477. * incremented on success. -ENOKEY is returned if a key could not be found.
  478. */
  479. struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
  480. {
  481. struct key *keyring;
  482. int bucket;
  483. if (!name)
  484. return ERR_PTR(-EINVAL);
  485. bucket = keyring_hash(name);
  486. read_lock(&keyring_name_lock);
  487. if (keyring_name_hash[bucket].next) {
  488. /* search this hash bucket for a keyring with a matching name
  489. * that's readable and that hasn't been revoked */
  490. list_for_each_entry(keyring,
  491. &keyring_name_hash[bucket],
  492. type_data.link
  493. ) {
  494. if (keyring->user->user_ns != current_user_ns())
  495. continue;
  496. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  497. continue;
  498. if (strcmp(keyring->description, name) != 0)
  499. continue;
  500. if (!skip_perm_check &&
  501. key_permission(make_key_ref(keyring, 0),
  502. KEY_SEARCH) < 0)
  503. continue;
  504. /* we've got a match but we might end up racing with
  505. * key_cleanup() if the keyring is currently 'dead'
  506. * (ie. it has a zero usage count) */
  507. if (!atomic_inc_not_zero(&keyring->usage))
  508. continue;
  509. goto out;
  510. }
  511. }
  512. keyring = ERR_PTR(-ENOKEY);
  513. out:
  514. read_unlock(&keyring_name_lock);
  515. return keyring;
  516. }
  517. /*
  518. * See if a cycle will will be created by inserting acyclic tree B in acyclic
  519. * tree A at the topmost level (ie: as a direct child of A).
  520. *
  521. * Since we are adding B to A at the top level, checking for cycles should just
  522. * be a matter of seeing if node A is somewhere in tree B.
  523. */
  524. static int keyring_detect_cycle(struct key *A, struct key *B)
  525. {
  526. struct {
  527. struct keyring_list *keylist;
  528. int kix;
  529. } stack[KEYRING_SEARCH_MAX_DEPTH];
  530. struct keyring_list *keylist;
  531. struct key *subtree, *key;
  532. int sp, kix, ret;
  533. rcu_read_lock();
  534. ret = -EDEADLK;
  535. if (A == B)
  536. goto cycle_detected;
  537. subtree = B;
  538. sp = 0;
  539. /* start processing a new keyring */
  540. descend:
  541. if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
  542. goto not_this_keyring;
  543. keylist = rcu_dereference(subtree->payload.subscriptions);
  544. if (!keylist)
  545. goto not_this_keyring;
  546. kix = 0;
  547. ascend:
  548. /* iterate through the remaining keys in this keyring */
  549. for (; kix < keylist->nkeys; kix++) {
  550. key = keylist->keys[kix];
  551. if (key == A)
  552. goto cycle_detected;
  553. /* recursively check nested keyrings */
  554. if (key->type == &key_type_keyring) {
  555. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  556. goto too_deep;
  557. /* stack the current position */
  558. stack[sp].keylist = keylist;
  559. stack[sp].kix = kix;
  560. sp++;
  561. /* begin again with the new keyring */
  562. subtree = key;
  563. goto descend;
  564. }
  565. }
  566. /* the keyring we're looking at was disqualified or didn't contain a
  567. * matching key */
  568. not_this_keyring:
  569. if (sp > 0) {
  570. /* resume the checking of a keyring higher up in the tree */
  571. sp--;
  572. keylist = stack[sp].keylist;
  573. kix = stack[sp].kix + 1;
  574. goto ascend;
  575. }
  576. ret = 0; /* no cycles detected */
  577. error:
  578. rcu_read_unlock();
  579. return ret;
  580. too_deep:
  581. ret = -ELOOP;
  582. goto error;
  583. cycle_detected:
  584. ret = -EDEADLK;
  585. goto error;
  586. }
  587. /*
  588. * Dispose of a keyring list after the RCU grace period, freeing the unlinked
  589. * key
  590. */
  591. static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
  592. {
  593. struct keyring_list *klist =
  594. container_of(rcu, struct keyring_list, rcu);
  595. if (klist->delkey != USHRT_MAX)
  596. key_put(klist->keys[klist->delkey]);
  597. kfree(klist);
  598. }
  599. /*
  600. * Preallocate memory so that a key can be linked into to a keyring.
  601. */
  602. int __key_link_begin(struct key *keyring, const struct key_type *type,
  603. const char *description, unsigned long *_prealloc)
  604. __acquires(&keyring->sem)
  605. {
  606. struct keyring_list *klist, *nklist;
  607. unsigned long prealloc;
  608. unsigned max;
  609. size_t size;
  610. int loop, ret;
  611. kenter("%d,%s,%s,", key_serial(keyring), type->name, description);
  612. if (keyring->type != &key_type_keyring)
  613. return -ENOTDIR;
  614. down_write(&keyring->sem);
  615. ret = -EKEYREVOKED;
  616. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  617. goto error_krsem;
  618. /* serialise link/link calls to prevent parallel calls causing a cycle
  619. * when linking two keyring in opposite orders */
  620. if (type == &key_type_keyring)
  621. down_write(&keyring_serialise_link_sem);
  622. klist = rcu_dereference_locked_keyring(keyring);
  623. /* see if there's a matching key we can displace */
  624. if (klist && klist->nkeys > 0) {
  625. for (loop = klist->nkeys - 1; loop >= 0; loop--) {
  626. if (klist->keys[loop]->type == type &&
  627. strcmp(klist->keys[loop]->description,
  628. description) == 0
  629. ) {
  630. /* found a match - we'll replace this one with
  631. * the new key */
  632. size = sizeof(struct key *) * klist->maxkeys;
  633. size += sizeof(*klist);
  634. BUG_ON(size > PAGE_SIZE);
  635. ret = -ENOMEM;
  636. nklist = kmemdup(klist, size, GFP_KERNEL);
  637. if (!nklist)
  638. goto error_sem;
  639. /* note replacement slot */
  640. klist->delkey = nklist->delkey = loop;
  641. prealloc = (unsigned long)nklist;
  642. goto done;
  643. }
  644. }
  645. }
  646. /* check that we aren't going to overrun the user's quota */
  647. ret = key_payload_reserve(keyring,
  648. keyring->datalen + KEYQUOTA_LINK_BYTES);
  649. if (ret < 0)
  650. goto error_sem;
  651. if (klist && klist->nkeys < klist->maxkeys) {
  652. /* there's sufficient slack space to append directly */
  653. nklist = NULL;
  654. prealloc = KEY_LINK_FIXQUOTA;
  655. } else {
  656. /* grow the key list */
  657. max = 4;
  658. if (klist)
  659. max += klist->maxkeys;
  660. ret = -ENFILE;
  661. if (max > USHRT_MAX - 1)
  662. goto error_quota;
  663. size = sizeof(*klist) + sizeof(struct key *) * max;
  664. if (size > PAGE_SIZE)
  665. goto error_quota;
  666. ret = -ENOMEM;
  667. nklist = kmalloc(size, GFP_KERNEL);
  668. if (!nklist)
  669. goto error_quota;
  670. nklist->maxkeys = max;
  671. if (klist) {
  672. memcpy(nklist->keys, klist->keys,
  673. sizeof(struct key *) * klist->nkeys);
  674. nklist->delkey = klist->nkeys;
  675. nklist->nkeys = klist->nkeys + 1;
  676. klist->delkey = USHRT_MAX;
  677. } else {
  678. nklist->nkeys = 1;
  679. nklist->delkey = 0;
  680. }
  681. /* add the key into the new space */
  682. nklist->keys[nklist->delkey] = NULL;
  683. }
  684. prealloc = (unsigned long)nklist | KEY_LINK_FIXQUOTA;
  685. done:
  686. *_prealloc = prealloc;
  687. kleave(" = 0");
  688. return 0;
  689. error_quota:
  690. /* undo the quota changes */
  691. key_payload_reserve(keyring,
  692. keyring->datalen - KEYQUOTA_LINK_BYTES);
  693. error_sem:
  694. if (type == &key_type_keyring)
  695. up_write(&keyring_serialise_link_sem);
  696. error_krsem:
  697. up_write(&keyring->sem);
  698. kleave(" = %d", ret);
  699. return ret;
  700. }
  701. /*
  702. * Check already instantiated keys aren't going to be a problem.
  703. *
  704. * The caller must have called __key_link_begin(). Don't need to call this for
  705. * keys that were created since __key_link_begin() was called.
  706. */
  707. int __key_link_check_live_key(struct key *keyring, struct key *key)
  708. {
  709. if (key->type == &key_type_keyring)
  710. /* check that we aren't going to create a cycle by linking one
  711. * keyring to another */
  712. return keyring_detect_cycle(keyring, key);
  713. return 0;
  714. }
  715. /*
  716. * Link a key into to a keyring.
  717. *
  718. * Must be called with __key_link_begin() having being called. Discards any
  719. * already extant link to matching key if there is one, so that each keyring
  720. * holds at most one link to any given key of a particular type+description
  721. * combination.
  722. */
  723. void __key_link(struct key *keyring, struct key *key,
  724. unsigned long *_prealloc)
  725. {
  726. struct keyring_list *klist, *nklist;
  727. nklist = (struct keyring_list *)(*_prealloc & ~KEY_LINK_FIXQUOTA);
  728. *_prealloc = 0;
  729. kenter("%d,%d,%p", keyring->serial, key->serial, nklist);
  730. klist = rcu_dereference_protected(keyring->payload.subscriptions,
  731. rwsem_is_locked(&keyring->sem));
  732. atomic_inc(&key->usage);
  733. /* there's a matching key we can displace or an empty slot in a newly
  734. * allocated list we can fill */
  735. if (nklist) {
  736. kdebug("replace %hu/%hu/%hu",
  737. nklist->delkey, nklist->nkeys, nklist->maxkeys);
  738. nklist->keys[nklist->delkey] = key;
  739. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  740. /* dispose of the old keyring list and, if there was one, the
  741. * displaced key */
  742. if (klist) {
  743. kdebug("dispose %hu/%hu/%hu",
  744. klist->delkey, klist->nkeys, klist->maxkeys);
  745. call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
  746. }
  747. } else {
  748. /* there's sufficient slack space to append directly */
  749. klist->keys[klist->nkeys] = key;
  750. smp_wmb();
  751. klist->nkeys++;
  752. }
  753. }
  754. /*
  755. * Finish linking a key into to a keyring.
  756. *
  757. * Must be called with __key_link_begin() having being called.
  758. */
  759. void __key_link_end(struct key *keyring, struct key_type *type,
  760. unsigned long prealloc)
  761. __releases(&keyring->sem)
  762. {
  763. BUG_ON(type == NULL);
  764. BUG_ON(type->name == NULL);
  765. kenter("%d,%s,%lx", keyring->serial, type->name, prealloc);
  766. if (type == &key_type_keyring)
  767. up_write(&keyring_serialise_link_sem);
  768. if (prealloc) {
  769. if (prealloc & KEY_LINK_FIXQUOTA)
  770. key_payload_reserve(keyring,
  771. keyring->datalen -
  772. KEYQUOTA_LINK_BYTES);
  773. kfree((struct keyring_list *)(prealloc & ~KEY_LINK_FIXQUOTA));
  774. }
  775. up_write(&keyring->sem);
  776. }
  777. /**
  778. * key_link - Link a key to a keyring
  779. * @keyring: The keyring to make the link in.
  780. * @key: The key to link to.
  781. *
  782. * Make a link in a keyring to a key, such that the keyring holds a reference
  783. * on that key and the key can potentially be found by searching that keyring.
  784. *
  785. * This function will write-lock the keyring's semaphore and will consume some
  786. * of the user's key data quota to hold the link.
  787. *
  788. * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
  789. * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
  790. * full, -EDQUOT if there is insufficient key data quota remaining to add
  791. * another link or -ENOMEM if there's insufficient memory.
  792. *
  793. * It is assumed that the caller has checked that it is permitted for a link to
  794. * be made (the keyring should have Write permission and the key Link
  795. * permission).
  796. */
  797. int key_link(struct key *keyring, struct key *key)
  798. {
  799. unsigned long prealloc;
  800. int ret;
  801. key_check(keyring);
  802. key_check(key);
  803. ret = __key_link_begin(keyring, key->type, key->description, &prealloc);
  804. if (ret == 0) {
  805. ret = __key_link_check_live_key(keyring, key);
  806. if (ret == 0)
  807. __key_link(keyring, key, &prealloc);
  808. __key_link_end(keyring, key->type, prealloc);
  809. }
  810. return ret;
  811. }
  812. EXPORT_SYMBOL(key_link);
  813. /**
  814. * key_unlink - Unlink the first link to a key from a keyring.
  815. * @keyring: The keyring to remove the link from.
  816. * @key: The key the link is to.
  817. *
  818. * Remove a link from a keyring to a key.
  819. *
  820. * This function will write-lock the keyring's semaphore.
  821. *
  822. * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
  823. * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
  824. * memory.
  825. *
  826. * It is assumed that the caller has checked that it is permitted for a link to
  827. * be removed (the keyring should have Write permission; no permissions are
  828. * required on the key).
  829. */
  830. int key_unlink(struct key *keyring, struct key *key)
  831. {
  832. struct keyring_list *klist, *nklist;
  833. int loop, ret;
  834. key_check(keyring);
  835. key_check(key);
  836. ret = -ENOTDIR;
  837. if (keyring->type != &key_type_keyring)
  838. goto error;
  839. down_write(&keyring->sem);
  840. klist = rcu_dereference_locked_keyring(keyring);
  841. if (klist) {
  842. /* search the keyring for the key */
  843. for (loop = 0; loop < klist->nkeys; loop++)
  844. if (klist->keys[loop] == key)
  845. goto key_is_present;
  846. }
  847. up_write(&keyring->sem);
  848. ret = -ENOENT;
  849. goto error;
  850. key_is_present:
  851. /* we need to copy the key list for RCU purposes */
  852. nklist = kmalloc(sizeof(*klist) +
  853. sizeof(struct key *) * klist->maxkeys,
  854. GFP_KERNEL);
  855. if (!nklist)
  856. goto nomem;
  857. nklist->maxkeys = klist->maxkeys;
  858. nklist->nkeys = klist->nkeys - 1;
  859. if (loop > 0)
  860. memcpy(&nklist->keys[0],
  861. &klist->keys[0],
  862. loop * sizeof(struct key *));
  863. if (loop < nklist->nkeys)
  864. memcpy(&nklist->keys[loop],
  865. &klist->keys[loop + 1],
  866. (nklist->nkeys - loop) * sizeof(struct key *));
  867. /* adjust the user's quota */
  868. key_payload_reserve(keyring,
  869. keyring->datalen - KEYQUOTA_LINK_BYTES);
  870. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  871. up_write(&keyring->sem);
  872. /* schedule for later cleanup */
  873. klist->delkey = loop;
  874. call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
  875. ret = 0;
  876. error:
  877. return ret;
  878. nomem:
  879. ret = -ENOMEM;
  880. up_write(&keyring->sem);
  881. goto error;
  882. }
  883. EXPORT_SYMBOL(key_unlink);
  884. /*
  885. * Dispose of a keyring list after the RCU grace period, releasing the keys it
  886. * links to.
  887. */
  888. static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
  889. {
  890. struct keyring_list *klist;
  891. int loop;
  892. klist = container_of(rcu, struct keyring_list, rcu);
  893. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  894. key_put(klist->keys[loop]);
  895. kfree(klist);
  896. }
  897. /**
  898. * keyring_clear - Clear a keyring
  899. * @keyring: The keyring to clear.
  900. *
  901. * Clear the contents of the specified keyring.
  902. *
  903. * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
  904. */
  905. int keyring_clear(struct key *keyring)
  906. {
  907. struct keyring_list *klist;
  908. int ret;
  909. ret = -ENOTDIR;
  910. if (keyring->type == &key_type_keyring) {
  911. /* detach the pointer block with the locks held */
  912. down_write(&keyring->sem);
  913. klist = rcu_dereference_locked_keyring(keyring);
  914. if (klist) {
  915. /* adjust the quota */
  916. key_payload_reserve(keyring,
  917. sizeof(struct keyring_list));
  918. rcu_assign_pointer(keyring->payload.subscriptions,
  919. NULL);
  920. }
  921. up_write(&keyring->sem);
  922. /* free the keys after the locks have been dropped */
  923. if (klist)
  924. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  925. ret = 0;
  926. }
  927. return ret;
  928. }
  929. EXPORT_SYMBOL(keyring_clear);
  930. /*
  931. * Dispose of the links from a revoked keyring.
  932. *
  933. * This is called with the key sem write-locked.
  934. */
  935. static void keyring_revoke(struct key *keyring)
  936. {
  937. struct keyring_list *klist;
  938. klist = rcu_dereference_locked_keyring(keyring);
  939. /* adjust the quota */
  940. key_payload_reserve(keyring, 0);
  941. if (klist) {
  942. rcu_assign_pointer(keyring->payload.subscriptions, NULL);
  943. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  944. }
  945. }
  946. /*
  947. * Determine whether a key is dead.
  948. */
  949. static bool key_is_dead(struct key *key, time_t limit)
  950. {
  951. return test_bit(KEY_FLAG_DEAD, &key->flags) ||
  952. (key->expiry > 0 && key->expiry <= limit);
  953. }
  954. /*
  955. * Collect garbage from the contents of a keyring, replacing the old list with
  956. * a new one with the pointers all shuffled down.
  957. *
  958. * Dead keys are classed as oned that are flagged as being dead or are revoked,
  959. * expired or negative keys that were revoked or expired before the specified
  960. * limit.
  961. */
  962. void keyring_gc(struct key *keyring, time_t limit)
  963. {
  964. struct keyring_list *klist, *new;
  965. struct key *key;
  966. int loop, keep, max;
  967. kenter("{%x,%s}", key_serial(keyring), keyring->description);
  968. down_write(&keyring->sem);
  969. klist = rcu_dereference_locked_keyring(keyring);
  970. if (!klist)
  971. goto no_klist;
  972. /* work out how many subscriptions we're keeping */
  973. keep = 0;
  974. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  975. if (!key_is_dead(klist->keys[loop], limit))
  976. keep++;
  977. if (keep == klist->nkeys)
  978. goto just_return;
  979. /* allocate a new keyring payload */
  980. max = roundup(keep, 4);
  981. new = kmalloc(sizeof(struct keyring_list) + max * sizeof(struct key *),
  982. GFP_KERNEL);
  983. if (!new)
  984. goto nomem;
  985. new->maxkeys = max;
  986. new->nkeys = 0;
  987. new->delkey = 0;
  988. /* install the live keys
  989. * - must take care as expired keys may be updated back to life
  990. */
  991. keep = 0;
  992. for (loop = klist->nkeys - 1; loop >= 0; loop--) {
  993. key = klist->keys[loop];
  994. if (!key_is_dead(key, limit)) {
  995. if (keep >= max)
  996. goto discard_new;
  997. new->keys[keep++] = key_get(key);
  998. }
  999. }
  1000. new->nkeys = keep;
  1001. /* adjust the quota */
  1002. key_payload_reserve(keyring,
  1003. sizeof(struct keyring_list) +
  1004. KEYQUOTA_LINK_BYTES * keep);
  1005. if (keep == 0) {
  1006. rcu_assign_pointer(keyring->payload.subscriptions, NULL);
  1007. kfree(new);
  1008. } else {
  1009. rcu_assign_pointer(keyring->payload.subscriptions, new);
  1010. }
  1011. up_write(&keyring->sem);
  1012. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  1013. kleave(" [yes]");
  1014. return;
  1015. discard_new:
  1016. new->nkeys = keep;
  1017. keyring_clear_rcu_disposal(&new->rcu);
  1018. up_write(&keyring->sem);
  1019. kleave(" [discard]");
  1020. return;
  1021. just_return:
  1022. up_write(&keyring->sem);
  1023. kleave(" [no dead]");
  1024. return;
  1025. no_klist:
  1026. up_write(&keyring->sem);
  1027. kleave(" [no_klist]");
  1028. return;
  1029. nomem:
  1030. up_write(&keyring->sem);
  1031. kleave(" [oom]");
  1032. }