keyring.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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_protected(keyring->payload.subscriptions,
  730. rwsem_is_locked(&keyring->sem));
  731. atomic_inc(&key->usage);
  732. /* there's a matching key we can displace or an empty slot in a newly
  733. * allocated list we can fill */
  734. if (nklist) {
  735. kdebug("replace %hu/%hu/%hu",
  736. nklist->delkey, nklist->nkeys, nklist->maxkeys);
  737. nklist->keys[nklist->delkey] = key;
  738. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  739. /* dispose of the old keyring list and, if there was one, the
  740. * displaced key */
  741. if (klist) {
  742. kdebug("dispose %hu/%hu/%hu",
  743. klist->delkey, klist->nkeys, klist->maxkeys);
  744. call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
  745. }
  746. } else {
  747. /* there's sufficient slack space to append directly */
  748. klist->keys[klist->nkeys] = key;
  749. smp_wmb();
  750. klist->nkeys++;
  751. }
  752. }
  753. /*
  754. * Finish linking a key into to a keyring.
  755. *
  756. * Must be called with __key_link_begin() having being called.
  757. */
  758. void __key_link_end(struct key *keyring, struct key_type *type,
  759. unsigned long prealloc)
  760. __releases(&keyring->sem)
  761. {
  762. BUG_ON(type == NULL);
  763. BUG_ON(type->name == NULL);
  764. kenter("%d,%s,%lx", keyring->serial, type->name, prealloc);
  765. if (type == &key_type_keyring)
  766. up_write(&keyring_serialise_link_sem);
  767. if (prealloc) {
  768. if (prealloc & KEY_LINK_FIXQUOTA)
  769. key_payload_reserve(keyring,
  770. keyring->datalen -
  771. KEYQUOTA_LINK_BYTES);
  772. kfree((struct keyring_list *)(prealloc & ~KEY_LINK_FIXQUOTA));
  773. }
  774. up_write(&keyring->sem);
  775. }
  776. /**
  777. * key_link - Link a key to a keyring
  778. * @keyring: The keyring to make the link in.
  779. * @key: The key to link to.
  780. *
  781. * Make a link in a keyring to a key, such that the keyring holds a reference
  782. * on that key and the key can potentially be found by searching that keyring.
  783. *
  784. * This function will write-lock the keyring's semaphore and will consume some
  785. * of the user's key data quota to hold the link.
  786. *
  787. * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
  788. * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
  789. * full, -EDQUOT if there is insufficient key data quota remaining to add
  790. * another link or -ENOMEM if there's insufficient memory.
  791. *
  792. * It is assumed that the caller has checked that it is permitted for a link to
  793. * be made (the keyring should have Write permission and the key Link
  794. * permission).
  795. */
  796. int key_link(struct key *keyring, struct key *key)
  797. {
  798. unsigned long prealloc;
  799. int ret;
  800. key_check(keyring);
  801. key_check(key);
  802. ret = __key_link_begin(keyring, key->type, key->description, &prealloc);
  803. if (ret == 0) {
  804. ret = __key_link_check_live_key(keyring, key);
  805. if (ret == 0)
  806. __key_link(keyring, key, &prealloc);
  807. __key_link_end(keyring, key->type, prealloc);
  808. }
  809. return ret;
  810. }
  811. EXPORT_SYMBOL(key_link);
  812. /**
  813. * key_unlink - Unlink the first link to a key from a keyring.
  814. * @keyring: The keyring to remove the link from.
  815. * @key: The key the link is to.
  816. *
  817. * Remove a link from a keyring to a key.
  818. *
  819. * This function will write-lock the keyring's semaphore.
  820. *
  821. * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
  822. * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
  823. * memory.
  824. *
  825. * It is assumed that the caller has checked that it is permitted for a link to
  826. * be removed (the keyring should have Write permission; no permissions are
  827. * required on the key).
  828. */
  829. int key_unlink(struct key *keyring, struct key *key)
  830. {
  831. struct keyring_list *klist, *nklist;
  832. int loop, ret;
  833. key_check(keyring);
  834. key_check(key);
  835. ret = -ENOTDIR;
  836. if (keyring->type != &key_type_keyring)
  837. goto error;
  838. down_write(&keyring->sem);
  839. klist = rcu_dereference_locked_keyring(keyring);
  840. if (klist) {
  841. /* search the keyring for the key */
  842. for (loop = 0; loop < klist->nkeys; loop++)
  843. if (klist->keys[loop] == key)
  844. goto key_is_present;
  845. }
  846. up_write(&keyring->sem);
  847. ret = -ENOENT;
  848. goto error;
  849. key_is_present:
  850. /* we need to copy the key list for RCU purposes */
  851. nklist = kmalloc(sizeof(*klist) +
  852. sizeof(struct key *) * klist->maxkeys,
  853. GFP_KERNEL);
  854. if (!nklist)
  855. goto nomem;
  856. nklist->maxkeys = klist->maxkeys;
  857. nklist->nkeys = klist->nkeys - 1;
  858. if (loop > 0)
  859. memcpy(&nklist->keys[0],
  860. &klist->keys[0],
  861. loop * sizeof(struct key *));
  862. if (loop < nklist->nkeys)
  863. memcpy(&nklist->keys[loop],
  864. &klist->keys[loop + 1],
  865. (nklist->nkeys - loop) * sizeof(struct key *));
  866. /* adjust the user's quota */
  867. key_payload_reserve(keyring,
  868. keyring->datalen - KEYQUOTA_LINK_BYTES);
  869. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  870. up_write(&keyring->sem);
  871. /* schedule for later cleanup */
  872. klist->delkey = loop;
  873. call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
  874. ret = 0;
  875. error:
  876. return ret;
  877. nomem:
  878. ret = -ENOMEM;
  879. up_write(&keyring->sem);
  880. goto error;
  881. }
  882. EXPORT_SYMBOL(key_unlink);
  883. /*
  884. * Dispose of a keyring list after the RCU grace period, releasing the keys it
  885. * links to.
  886. */
  887. static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
  888. {
  889. struct keyring_list *klist;
  890. int loop;
  891. klist = container_of(rcu, struct keyring_list, rcu);
  892. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  893. key_put(klist->keys[loop]);
  894. kfree(klist);
  895. }
  896. /**
  897. * keyring_clear - Clear a keyring
  898. * @keyring: The keyring to clear.
  899. *
  900. * Clear the contents of the specified keyring.
  901. *
  902. * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
  903. */
  904. int keyring_clear(struct key *keyring)
  905. {
  906. struct keyring_list *klist;
  907. int ret;
  908. ret = -ENOTDIR;
  909. if (keyring->type == &key_type_keyring) {
  910. /* detach the pointer block with the locks held */
  911. down_write(&keyring->sem);
  912. klist = rcu_dereference_locked_keyring(keyring);
  913. if (klist) {
  914. /* adjust the quota */
  915. key_payload_reserve(keyring,
  916. sizeof(struct keyring_list));
  917. rcu_assign_pointer(keyring->payload.subscriptions,
  918. NULL);
  919. }
  920. up_write(&keyring->sem);
  921. /* free the keys after the locks have been dropped */
  922. if (klist)
  923. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  924. ret = 0;
  925. }
  926. return ret;
  927. }
  928. EXPORT_SYMBOL(keyring_clear);
  929. /*
  930. * Dispose of the links from a revoked keyring.
  931. *
  932. * This is called with the key sem write-locked.
  933. */
  934. static void keyring_revoke(struct key *keyring)
  935. {
  936. struct keyring_list *klist;
  937. klist = rcu_dereference_locked_keyring(keyring);
  938. /* adjust the quota */
  939. key_payload_reserve(keyring, 0);
  940. if (klist) {
  941. rcu_assign_pointer(keyring->payload.subscriptions, NULL);
  942. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  943. }
  944. }
  945. /*
  946. * Determine whether a key is dead.
  947. */
  948. static bool key_is_dead(struct key *key, time_t limit)
  949. {
  950. return test_bit(KEY_FLAG_DEAD, &key->flags) ||
  951. (key->expiry > 0 && key->expiry <= limit);
  952. }
  953. /*
  954. * Collect garbage from the contents of a keyring, replacing the old list with
  955. * a new one with the pointers all shuffled down.
  956. *
  957. * Dead keys are classed as oned that are flagged as being dead or are revoked,
  958. * expired or negative keys that were revoked or expired before the specified
  959. * limit.
  960. */
  961. void keyring_gc(struct key *keyring, time_t limit)
  962. {
  963. struct keyring_list *klist, *new;
  964. struct key *key;
  965. int loop, keep, max;
  966. kenter("{%x,%s}", key_serial(keyring), keyring->description);
  967. down_write(&keyring->sem);
  968. klist = rcu_dereference_locked_keyring(keyring);
  969. if (!klist)
  970. goto no_klist;
  971. /* work out how many subscriptions we're keeping */
  972. keep = 0;
  973. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  974. if (!key_is_dead(klist->keys[loop], limit))
  975. keep++;
  976. if (keep == klist->nkeys)
  977. goto just_return;
  978. /* allocate a new keyring payload */
  979. max = roundup(keep, 4);
  980. new = kmalloc(sizeof(struct keyring_list) + max * sizeof(struct key *),
  981. GFP_KERNEL);
  982. if (!new)
  983. goto nomem;
  984. new->maxkeys = max;
  985. new->nkeys = 0;
  986. new->delkey = 0;
  987. /* install the live keys
  988. * - must take care as expired keys may be updated back to life
  989. */
  990. keep = 0;
  991. for (loop = klist->nkeys - 1; loop >= 0; loop--) {
  992. key = klist->keys[loop];
  993. if (!key_is_dead(key, limit)) {
  994. if (keep >= max)
  995. goto discard_new;
  996. new->keys[keep++] = key_get(key);
  997. }
  998. }
  999. new->nkeys = keep;
  1000. /* adjust the quota */
  1001. key_payload_reserve(keyring,
  1002. sizeof(struct keyring_list) +
  1003. KEYQUOTA_LINK_BYTES * keep);
  1004. if (keep == 0) {
  1005. rcu_assign_pointer(keyring->payload.subscriptions, NULL);
  1006. kfree(new);
  1007. } else {
  1008. rcu_assign_pointer(keyring->payload.subscriptions, new);
  1009. }
  1010. up_write(&keyring->sem);
  1011. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  1012. kleave(" [yes]");
  1013. return;
  1014. discard_new:
  1015. new->nkeys = keep;
  1016. keyring_clear_rcu_disposal(&new->rcu);
  1017. up_write(&keyring->sem);
  1018. kleave(" [discard]");
  1019. return;
  1020. just_return:
  1021. up_write(&keyring->sem);
  1022. kleave(" [no dead]");
  1023. return;
  1024. no_klist:
  1025. up_write(&keyring->sem);
  1026. kleave(" [no_klist]");
  1027. return;
  1028. nomem:
  1029. up_write(&keyring->sem);
  1030. kleave(" [oom]");
  1031. }