keyring.c 30 KB

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