keyring.c 30 KB

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