keyring.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. /* Keyring handling
  2. *
  3. * Copyright (C) 2004-2005, 2008, 2013 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 <keys/user-type.h>
  20. #include <linux/assoc_array_priv.h>
  21. #include <linux/uaccess.h>
  22. #include "internal.h"
  23. /*
  24. * When plumbing the depths of the key tree, this sets a hard limit
  25. * set on how deep we're willing to go.
  26. */
  27. #define KEYRING_SEARCH_MAX_DEPTH 6
  28. /*
  29. * We keep all named keyrings in a hash to speed looking them up.
  30. */
  31. #define KEYRING_NAME_HASH_SIZE (1 << 5)
  32. /*
  33. * We mark pointers we pass to the associative array with bit 1 set if
  34. * they're keyrings and clear otherwise.
  35. */
  36. #define KEYRING_PTR_SUBTYPE 0x2UL
  37. static inline bool keyring_ptr_is_keyring(const struct assoc_array_ptr *x)
  38. {
  39. return (unsigned long)x & KEYRING_PTR_SUBTYPE;
  40. }
  41. static inline struct key *keyring_ptr_to_key(const struct assoc_array_ptr *x)
  42. {
  43. void *object = assoc_array_ptr_to_leaf(x);
  44. return (struct key *)((unsigned long)object & ~KEYRING_PTR_SUBTYPE);
  45. }
  46. static inline void *keyring_key_to_ptr(struct key *key)
  47. {
  48. if (key->type == &key_type_keyring)
  49. return (void *)((unsigned long)key | KEYRING_PTR_SUBTYPE);
  50. return key;
  51. }
  52. static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE];
  53. static DEFINE_RWLOCK(keyring_name_lock);
  54. static inline unsigned keyring_hash(const char *desc)
  55. {
  56. unsigned bucket = 0;
  57. for (; *desc; desc++)
  58. bucket += (unsigned char)*desc;
  59. return bucket & (KEYRING_NAME_HASH_SIZE - 1);
  60. }
  61. /*
  62. * The keyring key type definition. Keyrings are simply keys of this type and
  63. * can be treated as ordinary keys in addition to having their own special
  64. * operations.
  65. */
  66. static int keyring_instantiate(struct key *keyring,
  67. struct key_preparsed_payload *prep);
  68. static void keyring_revoke(struct key *keyring);
  69. static void keyring_destroy(struct key *keyring);
  70. static void keyring_describe(const struct key *keyring, struct seq_file *m);
  71. static long keyring_read(const struct key *keyring,
  72. char __user *buffer, size_t buflen);
  73. struct key_type key_type_keyring = {
  74. .name = "keyring",
  75. .def_datalen = 0,
  76. .instantiate = keyring_instantiate,
  77. .match = user_match,
  78. .revoke = keyring_revoke,
  79. .destroy = keyring_destroy,
  80. .describe = keyring_describe,
  81. .read = keyring_read,
  82. };
  83. EXPORT_SYMBOL(key_type_keyring);
  84. /*
  85. * Semaphore to serialise link/link calls to prevent two link calls in parallel
  86. * introducing a cycle.
  87. */
  88. static DECLARE_RWSEM(keyring_serialise_link_sem);
  89. /*
  90. * Publish the name of a keyring so that it can be found by name (if it has
  91. * one).
  92. */
  93. static void keyring_publish_name(struct key *keyring)
  94. {
  95. int bucket;
  96. if (keyring->description) {
  97. bucket = keyring_hash(keyring->description);
  98. write_lock(&keyring_name_lock);
  99. if (!keyring_name_hash[bucket].next)
  100. INIT_LIST_HEAD(&keyring_name_hash[bucket]);
  101. list_add_tail(&keyring->type_data.link,
  102. &keyring_name_hash[bucket]);
  103. write_unlock(&keyring_name_lock);
  104. }
  105. }
  106. /*
  107. * Initialise a keyring.
  108. *
  109. * Returns 0 on success, -EINVAL if given any data.
  110. */
  111. static int keyring_instantiate(struct key *keyring,
  112. struct key_preparsed_payload *prep)
  113. {
  114. int ret;
  115. ret = -EINVAL;
  116. if (prep->datalen == 0) {
  117. assoc_array_init(&keyring->keys);
  118. /* make the keyring available by name if it has one */
  119. keyring_publish_name(keyring);
  120. ret = 0;
  121. }
  122. return ret;
  123. }
  124. /*
  125. * Multiply 64-bits by 32-bits to 96-bits and fold back to 64-bit. Ideally we'd
  126. * fold the carry back too, but that requires inline asm.
  127. */
  128. static u64 mult_64x32_and_fold(u64 x, u32 y)
  129. {
  130. u64 hi = (u64)(u32)(x >> 32) * y;
  131. u64 lo = (u64)(u32)(x) * y;
  132. return lo + ((u64)(u32)hi << 32) + (u32)(hi >> 32);
  133. }
  134. /*
  135. * Hash a key type and description.
  136. */
  137. static unsigned long hash_key_type_and_desc(const struct keyring_index_key *index_key)
  138. {
  139. const unsigned level_shift = ASSOC_ARRAY_LEVEL_STEP;
  140. const unsigned long level_mask = ASSOC_ARRAY_LEVEL_STEP_MASK;
  141. const char *description = index_key->description;
  142. unsigned long hash, type;
  143. u32 piece;
  144. u64 acc;
  145. int n, desc_len = index_key->desc_len;
  146. type = (unsigned long)index_key->type;
  147. acc = mult_64x32_and_fold(type, desc_len + 13);
  148. acc = mult_64x32_and_fold(acc, 9207);
  149. for (;;) {
  150. n = desc_len;
  151. if (n <= 0)
  152. break;
  153. if (n > 4)
  154. n = 4;
  155. piece = 0;
  156. memcpy(&piece, description, n);
  157. description += n;
  158. desc_len -= n;
  159. acc = mult_64x32_and_fold(acc, piece);
  160. acc = mult_64x32_and_fold(acc, 9207);
  161. }
  162. /* Fold the hash down to 32 bits if need be. */
  163. hash = acc;
  164. if (ASSOC_ARRAY_KEY_CHUNK_SIZE == 32)
  165. hash ^= acc >> 32;
  166. /* Squidge all the keyrings into a separate part of the tree to
  167. * ordinary keys by making sure the lowest level segment in the hash is
  168. * zero for keyrings and non-zero otherwise.
  169. */
  170. if (index_key->type != &key_type_keyring && (hash & level_mask) == 0)
  171. return hash | (hash >> (ASSOC_ARRAY_KEY_CHUNK_SIZE - level_shift)) | 1;
  172. if (index_key->type == &key_type_keyring && (hash & level_mask) != 0)
  173. return (hash + (hash << level_shift)) & ~level_mask;
  174. return hash;
  175. }
  176. /*
  177. * Build the next index key chunk.
  178. *
  179. * On 32-bit systems the index key is laid out as:
  180. *
  181. * 0 4 5 9...
  182. * hash desclen typeptr desc[]
  183. *
  184. * On 64-bit systems:
  185. *
  186. * 0 8 9 17...
  187. * hash desclen typeptr desc[]
  188. *
  189. * We return it one word-sized chunk at a time.
  190. */
  191. static unsigned long keyring_get_key_chunk(const void *data, int level)
  192. {
  193. const struct keyring_index_key *index_key = data;
  194. unsigned long chunk = 0;
  195. long offset = 0;
  196. int desc_len = index_key->desc_len, n = sizeof(chunk);
  197. level /= ASSOC_ARRAY_KEY_CHUNK_SIZE;
  198. switch (level) {
  199. case 0:
  200. return hash_key_type_and_desc(index_key);
  201. case 1:
  202. return ((unsigned long)index_key->type << 8) | desc_len;
  203. case 2:
  204. if (desc_len == 0)
  205. return (u8)((unsigned long)index_key->type >>
  206. (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8));
  207. n--;
  208. offset = 1;
  209. default:
  210. offset += sizeof(chunk) - 1;
  211. offset += (level - 3) * sizeof(chunk);
  212. if (offset >= desc_len)
  213. return 0;
  214. desc_len -= offset;
  215. if (desc_len > n)
  216. desc_len = n;
  217. offset += desc_len;
  218. do {
  219. chunk <<= 8;
  220. chunk |= ((u8*)index_key->description)[--offset];
  221. } while (--desc_len > 0);
  222. if (level == 2) {
  223. chunk <<= 8;
  224. chunk |= (u8)((unsigned long)index_key->type >>
  225. (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8));
  226. }
  227. return chunk;
  228. }
  229. }
  230. static unsigned long keyring_get_object_key_chunk(const void *object, int level)
  231. {
  232. const struct key *key = keyring_ptr_to_key(object);
  233. return keyring_get_key_chunk(&key->index_key, level);
  234. }
  235. static bool keyring_compare_object(const void *object, const void *data)
  236. {
  237. const struct keyring_index_key *index_key = data;
  238. const struct key *key = keyring_ptr_to_key(object);
  239. return key->index_key.type == index_key->type &&
  240. key->index_key.desc_len == index_key->desc_len &&
  241. memcmp(key->index_key.description, index_key->description,
  242. index_key->desc_len) == 0;
  243. }
  244. /*
  245. * Compare the index keys of a pair of objects and determine the bit position
  246. * at which they differ - if they differ.
  247. */
  248. static int keyring_diff_objects(const void *_a, const void *_b)
  249. {
  250. const struct key *key_a = keyring_ptr_to_key(_a);
  251. const struct key *key_b = keyring_ptr_to_key(_b);
  252. const struct keyring_index_key *a = &key_a->index_key;
  253. const struct keyring_index_key *b = &key_b->index_key;
  254. unsigned long seg_a, seg_b;
  255. int level, i;
  256. level = 0;
  257. seg_a = hash_key_type_and_desc(a);
  258. seg_b = hash_key_type_and_desc(b);
  259. if ((seg_a ^ seg_b) != 0)
  260. goto differ;
  261. /* The number of bits contributed by the hash is controlled by a
  262. * constant in the assoc_array headers. Everything else thereafter we
  263. * can deal with as being machine word-size dependent.
  264. */
  265. level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8;
  266. seg_a = a->desc_len;
  267. seg_b = b->desc_len;
  268. if ((seg_a ^ seg_b) != 0)
  269. goto differ;
  270. /* The next bit may not work on big endian */
  271. level++;
  272. seg_a = (unsigned long)a->type;
  273. seg_b = (unsigned long)b->type;
  274. if ((seg_a ^ seg_b) != 0)
  275. goto differ;
  276. level += sizeof(unsigned long);
  277. if (a->desc_len == 0)
  278. goto same;
  279. i = 0;
  280. if (((unsigned long)a->description | (unsigned long)b->description) &
  281. (sizeof(unsigned long) - 1)) {
  282. do {
  283. seg_a = *(unsigned long *)(a->description + i);
  284. seg_b = *(unsigned long *)(b->description + i);
  285. if ((seg_a ^ seg_b) != 0)
  286. goto differ_plus_i;
  287. i += sizeof(unsigned long);
  288. } while (i < (a->desc_len & (sizeof(unsigned long) - 1)));
  289. }
  290. for (; i < a->desc_len; i++) {
  291. seg_a = *(unsigned char *)(a->description + i);
  292. seg_b = *(unsigned char *)(b->description + i);
  293. if ((seg_a ^ seg_b) != 0)
  294. goto differ_plus_i;
  295. }
  296. same:
  297. return -1;
  298. differ_plus_i:
  299. level += i;
  300. differ:
  301. i = level * 8 + __ffs(seg_a ^ seg_b);
  302. return i;
  303. }
  304. /*
  305. * Free an object after stripping the keyring flag off of the pointer.
  306. */
  307. static void keyring_free_object(void *object)
  308. {
  309. key_put(keyring_ptr_to_key(object));
  310. }
  311. /*
  312. * Operations for keyring management by the index-tree routines.
  313. */
  314. static const struct assoc_array_ops keyring_assoc_array_ops = {
  315. .get_key_chunk = keyring_get_key_chunk,
  316. .get_object_key_chunk = keyring_get_object_key_chunk,
  317. .compare_object = keyring_compare_object,
  318. .diff_objects = keyring_diff_objects,
  319. .free_object = keyring_free_object,
  320. };
  321. /*
  322. * Clean up a keyring when it is destroyed. Unpublish its name if it had one
  323. * and dispose of its data.
  324. *
  325. * The garbage collector detects the final key_put(), removes the keyring from
  326. * the serial number tree and then does RCU synchronisation before coming here,
  327. * so we shouldn't need to worry about code poking around here with the RCU
  328. * readlock held by this time.
  329. */
  330. static void keyring_destroy(struct key *keyring)
  331. {
  332. if (keyring->description) {
  333. write_lock(&keyring_name_lock);
  334. if (keyring->type_data.link.next != NULL &&
  335. !list_empty(&keyring->type_data.link))
  336. list_del(&keyring->type_data.link);
  337. write_unlock(&keyring_name_lock);
  338. }
  339. assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops);
  340. }
  341. /*
  342. * Describe a keyring for /proc.
  343. */
  344. static void keyring_describe(const struct key *keyring, struct seq_file *m)
  345. {
  346. if (keyring->description)
  347. seq_puts(m, keyring->description);
  348. else
  349. seq_puts(m, "[anon]");
  350. if (key_is_instantiated(keyring)) {
  351. if (keyring->keys.nr_leaves_on_tree != 0)
  352. seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree);
  353. else
  354. seq_puts(m, ": empty");
  355. }
  356. }
  357. struct keyring_read_iterator_context {
  358. size_t qty;
  359. size_t count;
  360. key_serial_t __user *buffer;
  361. };
  362. static int keyring_read_iterator(const void *object, void *data)
  363. {
  364. struct keyring_read_iterator_context *ctx = data;
  365. const struct key *key = keyring_ptr_to_key(object);
  366. int ret;
  367. kenter("{%s,%d},,{%zu/%zu}",
  368. key->type->name, key->serial, ctx->count, ctx->qty);
  369. if (ctx->count >= ctx->qty)
  370. return 1;
  371. ret = put_user(key->serial, ctx->buffer);
  372. if (ret < 0)
  373. return ret;
  374. ctx->buffer++;
  375. ctx->count += sizeof(key->serial);
  376. return 0;
  377. }
  378. /*
  379. * Read a list of key IDs from the keyring's contents in binary form
  380. *
  381. * The keyring's semaphore is read-locked by the caller. This prevents someone
  382. * from modifying it under us - which could cause us to read key IDs multiple
  383. * times.
  384. */
  385. static long keyring_read(const struct key *keyring,
  386. char __user *buffer, size_t buflen)
  387. {
  388. struct keyring_read_iterator_context ctx;
  389. unsigned long nr_keys;
  390. int ret;
  391. kenter("{%d},,%zu", key_serial(keyring), buflen);
  392. if (buflen & (sizeof(key_serial_t) - 1))
  393. return -EINVAL;
  394. nr_keys = keyring->keys.nr_leaves_on_tree;
  395. if (nr_keys == 0)
  396. return 0;
  397. /* Calculate how much data we could return */
  398. ctx.qty = nr_keys * sizeof(key_serial_t);
  399. if (!buffer || !buflen)
  400. return ctx.qty;
  401. if (buflen > ctx.qty)
  402. ctx.qty = buflen;
  403. /* Copy the IDs of the subscribed keys into the buffer */
  404. ctx.buffer = (key_serial_t __user *)buffer;
  405. ctx.count = 0;
  406. ret = assoc_array_iterate(&keyring->keys, keyring_read_iterator, &ctx);
  407. if (ret < 0) {
  408. kleave(" = %d [iterate]", ret);
  409. return ret;
  410. }
  411. kleave(" = %zu [ok]", ctx.count);
  412. return ctx.count;
  413. }
  414. /*
  415. * Allocate a keyring and link into the destination keyring.
  416. */
  417. struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
  418. const struct cred *cred, key_perm_t perm,
  419. unsigned long flags, struct key *dest)
  420. {
  421. struct key *keyring;
  422. int ret;
  423. keyring = key_alloc(&key_type_keyring, description,
  424. uid, gid, cred, perm, flags);
  425. if (!IS_ERR(keyring)) {
  426. ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
  427. if (ret < 0) {
  428. key_put(keyring);
  429. keyring = ERR_PTR(ret);
  430. }
  431. }
  432. return keyring;
  433. }
  434. EXPORT_SYMBOL(keyring_alloc);
  435. /*
  436. * Iteration function to consider each key found.
  437. */
  438. static int keyring_search_iterator(const void *object, void *iterator_data)
  439. {
  440. struct keyring_search_context *ctx = iterator_data;
  441. const struct key *key = keyring_ptr_to_key(object);
  442. unsigned long kflags = key->flags;
  443. kenter("{%d}", key->serial);
  444. /* ignore keys not of this type */
  445. if (key->type != ctx->index_key.type) {
  446. kleave(" = 0 [!type]");
  447. return 0;
  448. }
  449. /* skip invalidated, revoked and expired keys */
  450. if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
  451. if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
  452. (1 << KEY_FLAG_REVOKED))) {
  453. ctx->result = ERR_PTR(-EKEYREVOKED);
  454. kleave(" = %d [invrev]", ctx->skipped_ret);
  455. goto skipped;
  456. }
  457. if (key->expiry && ctx->now.tv_sec >= key->expiry) {
  458. ctx->result = ERR_PTR(-EKEYEXPIRED);
  459. kleave(" = %d [expire]", ctx->skipped_ret);
  460. goto skipped;
  461. }
  462. }
  463. /* keys that don't match */
  464. if (!ctx->match(key, ctx->match_data)) {
  465. kleave(" = 0 [!match]");
  466. return 0;
  467. }
  468. /* key must have search permissions */
  469. if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
  470. key_task_permission(make_key_ref(key, ctx->possessed),
  471. ctx->cred, KEY_SEARCH) < 0) {
  472. ctx->result = ERR_PTR(-EACCES);
  473. kleave(" = %d [!perm]", ctx->skipped_ret);
  474. goto skipped;
  475. }
  476. if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
  477. /* we set a different error code if we pass a negative key */
  478. if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
  479. smp_rmb();
  480. ctx->result = ERR_PTR(key->type_data.reject_error);
  481. kleave(" = %d [neg]", ctx->skipped_ret);
  482. goto skipped;
  483. }
  484. }
  485. /* Found */
  486. ctx->result = make_key_ref(key, ctx->possessed);
  487. kleave(" = 1 [found]");
  488. return 1;
  489. skipped:
  490. return ctx->skipped_ret;
  491. }
  492. /*
  493. * Search inside a keyring for a key. We can search by walking to it
  494. * directly based on its index-key or we can iterate over the entire
  495. * tree looking for it, based on the match function.
  496. */
  497. static int search_keyring(struct key *keyring, struct keyring_search_context *ctx)
  498. {
  499. if ((ctx->flags & KEYRING_SEARCH_LOOKUP_TYPE) ==
  500. KEYRING_SEARCH_LOOKUP_DIRECT) {
  501. const void *object;
  502. object = assoc_array_find(&keyring->keys,
  503. &keyring_assoc_array_ops,
  504. &ctx->index_key);
  505. return object ? ctx->iterator(object, ctx) : 0;
  506. }
  507. return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx);
  508. }
  509. /*
  510. * Search a tree of keyrings that point to other keyrings up to the maximum
  511. * depth.
  512. */
  513. static bool search_nested_keyrings(struct key *keyring,
  514. struct keyring_search_context *ctx)
  515. {
  516. struct {
  517. struct key *keyring;
  518. struct assoc_array_node *node;
  519. int slot;
  520. } stack[KEYRING_SEARCH_MAX_DEPTH];
  521. struct assoc_array_shortcut *shortcut;
  522. struct assoc_array_node *node;
  523. struct assoc_array_ptr *ptr;
  524. struct key *key;
  525. int sp = 0, slot;
  526. kenter("{%d},{%s,%s}",
  527. keyring->serial,
  528. ctx->index_key.type->name,
  529. ctx->index_key.description);
  530. if (ctx->index_key.description)
  531. ctx->index_key.desc_len = strlen(ctx->index_key.description);
  532. /* Check to see if this top-level keyring is what we are looking for
  533. * and whether it is valid or not.
  534. */
  535. if (ctx->flags & KEYRING_SEARCH_LOOKUP_ITERATE ||
  536. keyring_compare_object(keyring, &ctx->index_key)) {
  537. ctx->skipped_ret = 2;
  538. ctx->flags |= KEYRING_SEARCH_DO_STATE_CHECK;
  539. switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) {
  540. case 1:
  541. goto found;
  542. case 2:
  543. return false;
  544. default:
  545. break;
  546. }
  547. }
  548. ctx->skipped_ret = 0;
  549. if (ctx->flags & KEYRING_SEARCH_NO_STATE_CHECK)
  550. ctx->flags &= ~KEYRING_SEARCH_DO_STATE_CHECK;
  551. /* Start processing a new keyring */
  552. descend_to_keyring:
  553. kdebug("descend to %d", keyring->serial);
  554. if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
  555. (1 << KEY_FLAG_REVOKED)))
  556. goto not_this_keyring;
  557. /* Search through the keys in this keyring before its searching its
  558. * subtrees.
  559. */
  560. if (search_keyring(keyring, ctx))
  561. goto found;
  562. /* Then manually iterate through the keyrings nested in this one.
  563. *
  564. * Start from the root node of the index tree. Because of the way the
  565. * hash function has been set up, keyrings cluster on the leftmost
  566. * branch of the root node (root slot 0) or in the root node itself.
  567. * Non-keyrings avoid the leftmost branch of the root entirely (root
  568. * slots 1-15).
  569. */
  570. ptr = ACCESS_ONCE(keyring->keys.root);
  571. if (!ptr)
  572. goto not_this_keyring;
  573. if (assoc_array_ptr_is_shortcut(ptr)) {
  574. /* If the root is a shortcut, either the keyring only contains
  575. * keyring pointers (everything clusters behind root slot 0) or
  576. * doesn't contain any keyring pointers.
  577. */
  578. shortcut = assoc_array_ptr_to_shortcut(ptr);
  579. smp_read_barrier_depends();
  580. if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0)
  581. goto not_this_keyring;
  582. ptr = ACCESS_ONCE(shortcut->next_node);
  583. node = assoc_array_ptr_to_node(ptr);
  584. goto begin_node;
  585. }
  586. node = assoc_array_ptr_to_node(ptr);
  587. smp_read_barrier_depends();
  588. ptr = node->slots[0];
  589. if (!assoc_array_ptr_is_meta(ptr))
  590. goto begin_node;
  591. descend_to_node:
  592. /* Descend to a more distal node in this keyring's content tree and go
  593. * through that.
  594. */
  595. kdebug("descend");
  596. if (assoc_array_ptr_is_shortcut(ptr)) {
  597. shortcut = assoc_array_ptr_to_shortcut(ptr);
  598. smp_read_barrier_depends();
  599. ptr = ACCESS_ONCE(shortcut->next_node);
  600. BUG_ON(!assoc_array_ptr_is_node(ptr));
  601. node = assoc_array_ptr_to_node(ptr);
  602. }
  603. begin_node:
  604. kdebug("begin_node");
  605. smp_read_barrier_depends();
  606. slot = 0;
  607. ascend_to_node:
  608. /* Go through the slots in a node */
  609. for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  610. ptr = ACCESS_ONCE(node->slots[slot]);
  611. if (assoc_array_ptr_is_meta(ptr) && node->back_pointer)
  612. goto descend_to_node;
  613. if (!keyring_ptr_is_keyring(ptr))
  614. continue;
  615. key = keyring_ptr_to_key(ptr);
  616. if (sp >= KEYRING_SEARCH_MAX_DEPTH) {
  617. if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) {
  618. ctx->result = ERR_PTR(-ELOOP);
  619. return false;
  620. }
  621. goto not_this_keyring;
  622. }
  623. /* Search a nested keyring */
  624. if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
  625. key_task_permission(make_key_ref(key, ctx->possessed),
  626. ctx->cred, KEY_SEARCH) < 0)
  627. continue;
  628. /* stack the current position */
  629. stack[sp].keyring = keyring;
  630. stack[sp].node = node;
  631. stack[sp].slot = slot;
  632. sp++;
  633. /* begin again with the new keyring */
  634. keyring = key;
  635. goto descend_to_keyring;
  636. }
  637. /* We've dealt with all the slots in the current node, so now we need
  638. * to ascend to the parent and continue processing there.
  639. */
  640. ptr = ACCESS_ONCE(node->back_pointer);
  641. slot = node->parent_slot;
  642. if (ptr && assoc_array_ptr_is_shortcut(ptr)) {
  643. shortcut = assoc_array_ptr_to_shortcut(ptr);
  644. smp_read_barrier_depends();
  645. ptr = ACCESS_ONCE(shortcut->back_pointer);
  646. slot = shortcut->parent_slot;
  647. }
  648. if (!ptr)
  649. goto not_this_keyring;
  650. node = assoc_array_ptr_to_node(ptr);
  651. smp_read_barrier_depends();
  652. slot++;
  653. /* If we've ascended to the root (zero backpointer), we must have just
  654. * finished processing the leftmost branch rather than the root slots -
  655. * so there can't be any more keyrings for us to find.
  656. */
  657. if (node->back_pointer) {
  658. kdebug("ascend %d", slot);
  659. goto ascend_to_node;
  660. }
  661. /* The keyring we're looking at was disqualified or didn't contain a
  662. * matching key.
  663. */
  664. not_this_keyring:
  665. kdebug("not_this_keyring %d", sp);
  666. if (sp <= 0) {
  667. kleave(" = false");
  668. return false;
  669. }
  670. /* Resume the processing of a keyring higher up in the tree */
  671. sp--;
  672. keyring = stack[sp].keyring;
  673. node = stack[sp].node;
  674. slot = stack[sp].slot + 1;
  675. kdebug("ascend to %d [%d]", keyring->serial, slot);
  676. goto ascend_to_node;
  677. /* We found a viable match */
  678. found:
  679. key = key_ref_to_ptr(ctx->result);
  680. key_check(key);
  681. if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) {
  682. key->last_used_at = ctx->now.tv_sec;
  683. keyring->last_used_at = ctx->now.tv_sec;
  684. while (sp > 0)
  685. stack[--sp].keyring->last_used_at = ctx->now.tv_sec;
  686. }
  687. kleave(" = true");
  688. return true;
  689. }
  690. /**
  691. * keyring_search_aux - Search a keyring tree for a key matching some criteria
  692. * @keyring_ref: A pointer to the keyring with possession indicator.
  693. * @ctx: The keyring search context.
  694. *
  695. * Search the supplied keyring tree for a key that matches the criteria given.
  696. * The root keyring and any linked keyrings must grant Search permission to the
  697. * caller to be searchable and keys can only be found if they too grant Search
  698. * to the caller. The possession flag on the root keyring pointer controls use
  699. * of the possessor bits in permissions checking of the entire tree. In
  700. * addition, the LSM gets to forbid keyring searches and key matches.
  701. *
  702. * The search is performed as a breadth-then-depth search up to the prescribed
  703. * limit (KEYRING_SEARCH_MAX_DEPTH).
  704. *
  705. * Keys are matched to the type provided and are then filtered by the match
  706. * function, which is given the description to use in any way it sees fit. The
  707. * match function may use any attributes of a key that it wishes to to
  708. * determine the match. Normally the match function from the key type would be
  709. * used.
  710. *
  711. * RCU can be used to prevent the keyring key lists from disappearing without
  712. * the need to take lots of locks.
  713. *
  714. * Returns a pointer to the found key and increments the key usage count if
  715. * successful; -EAGAIN if no matching keys were found, or if expired or revoked
  716. * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
  717. * specified keyring wasn't a keyring.
  718. *
  719. * In the case of a successful return, the possession attribute from
  720. * @keyring_ref is propagated to the returned key reference.
  721. */
  722. key_ref_t keyring_search_aux(key_ref_t keyring_ref,
  723. struct keyring_search_context *ctx)
  724. {
  725. struct key *keyring;
  726. long err;
  727. ctx->iterator = keyring_search_iterator;
  728. ctx->possessed = is_key_possessed(keyring_ref);
  729. ctx->result = ERR_PTR(-EAGAIN);
  730. keyring = key_ref_to_ptr(keyring_ref);
  731. key_check(keyring);
  732. if (keyring->type != &key_type_keyring)
  733. return ERR_PTR(-ENOTDIR);
  734. if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) {
  735. err = key_task_permission(keyring_ref, ctx->cred, KEY_SEARCH);
  736. if (err < 0)
  737. return ERR_PTR(err);
  738. }
  739. rcu_read_lock();
  740. ctx->now = current_kernel_time();
  741. if (search_nested_keyrings(keyring, ctx))
  742. __key_get(key_ref_to_ptr(ctx->result));
  743. rcu_read_unlock();
  744. return ctx->result;
  745. }
  746. /**
  747. * keyring_search - Search the supplied keyring tree for a matching key
  748. * @keyring: The root of the keyring tree to be searched.
  749. * @type: The type of keyring we want to find.
  750. * @description: The name of the keyring we want to find.
  751. *
  752. * As keyring_search_aux() above, but using the current task's credentials and
  753. * type's default matching function and preferred search method.
  754. */
  755. key_ref_t keyring_search(key_ref_t keyring,
  756. struct key_type *type,
  757. const char *description)
  758. {
  759. struct keyring_search_context ctx = {
  760. .index_key.type = type,
  761. .index_key.description = description,
  762. .cred = current_cred(),
  763. .match = type->match,
  764. .match_data = description,
  765. .flags = (type->def_lookup_type |
  766. KEYRING_SEARCH_DO_STATE_CHECK),
  767. };
  768. if (!ctx.match)
  769. return ERR_PTR(-ENOKEY);
  770. return keyring_search_aux(keyring, &ctx);
  771. }
  772. EXPORT_SYMBOL(keyring_search);
  773. /*
  774. * Search the given keyring for a key that might be updated.
  775. *
  776. * The caller must guarantee that the keyring is a keyring and that the
  777. * permission is granted to modify the keyring as no check is made here. The
  778. * caller must also hold a lock on the keyring semaphore.
  779. *
  780. * Returns a pointer to the found key with usage count incremented if
  781. * successful and returns NULL if not found. Revoked and invalidated keys are
  782. * skipped over.
  783. *
  784. * If successful, the possession indicator is propagated from the keyring ref
  785. * to the returned key reference.
  786. */
  787. key_ref_t find_key_to_update(key_ref_t keyring_ref,
  788. const struct keyring_index_key *index_key)
  789. {
  790. struct key *keyring, *key;
  791. const void *object;
  792. keyring = key_ref_to_ptr(keyring_ref);
  793. kenter("{%d},{%s,%s}",
  794. keyring->serial, index_key->type->name, index_key->description);
  795. object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
  796. index_key);
  797. if (object)
  798. goto found;
  799. kleave(" = NULL");
  800. return NULL;
  801. found:
  802. key = keyring_ptr_to_key(object);
  803. if (key->flags & ((1 << KEY_FLAG_INVALIDATED) |
  804. (1 << KEY_FLAG_REVOKED))) {
  805. kleave(" = NULL [x]");
  806. return NULL;
  807. }
  808. __key_get(key);
  809. kleave(" = {%d}", key->serial);
  810. return make_key_ref(key, is_key_possessed(keyring_ref));
  811. }
  812. /*
  813. * Find a keyring with the specified name.
  814. *
  815. * All named keyrings in the current user namespace are searched, provided they
  816. * grant Search permission directly to the caller (unless this check is
  817. * skipped). Keyrings whose usage points have reached zero or who have been
  818. * revoked are skipped.
  819. *
  820. * Returns a pointer to the keyring with the keyring's refcount having being
  821. * incremented on success. -ENOKEY is returned if a key could not be found.
  822. */
  823. struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
  824. {
  825. struct key *keyring;
  826. int bucket;
  827. if (!name)
  828. return ERR_PTR(-EINVAL);
  829. bucket = keyring_hash(name);
  830. read_lock(&keyring_name_lock);
  831. if (keyring_name_hash[bucket].next) {
  832. /* search this hash bucket for a keyring with a matching name
  833. * that's readable and that hasn't been revoked */
  834. list_for_each_entry(keyring,
  835. &keyring_name_hash[bucket],
  836. type_data.link
  837. ) {
  838. if (!kuid_has_mapping(current_user_ns(), keyring->user->uid))
  839. continue;
  840. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  841. continue;
  842. if (strcmp(keyring->description, name) != 0)
  843. continue;
  844. if (!skip_perm_check &&
  845. key_permission(make_key_ref(keyring, 0),
  846. KEY_SEARCH) < 0)
  847. continue;
  848. /* we've got a match but we might end up racing with
  849. * key_cleanup() if the keyring is currently 'dead'
  850. * (ie. it has a zero usage count) */
  851. if (!atomic_inc_not_zero(&keyring->usage))
  852. continue;
  853. keyring->last_used_at = current_kernel_time().tv_sec;
  854. goto out;
  855. }
  856. }
  857. keyring = ERR_PTR(-ENOKEY);
  858. out:
  859. read_unlock(&keyring_name_lock);
  860. return keyring;
  861. }
  862. static int keyring_detect_cycle_iterator(const void *object,
  863. void *iterator_data)
  864. {
  865. struct keyring_search_context *ctx = iterator_data;
  866. const struct key *key = keyring_ptr_to_key(object);
  867. kenter("{%d}", key->serial);
  868. BUG_ON(key != ctx->match_data);
  869. ctx->result = ERR_PTR(-EDEADLK);
  870. return 1;
  871. }
  872. /*
  873. * See if a cycle will will be created by inserting acyclic tree B in acyclic
  874. * tree A at the topmost level (ie: as a direct child of A).
  875. *
  876. * Since we are adding B to A at the top level, checking for cycles should just
  877. * be a matter of seeing if node A is somewhere in tree B.
  878. */
  879. static int keyring_detect_cycle(struct key *A, struct key *B)
  880. {
  881. struct keyring_search_context ctx = {
  882. .index_key = A->index_key,
  883. .match_data = A,
  884. .iterator = keyring_detect_cycle_iterator,
  885. .flags = (KEYRING_SEARCH_LOOKUP_DIRECT |
  886. KEYRING_SEARCH_NO_STATE_CHECK |
  887. KEYRING_SEARCH_NO_UPDATE_TIME |
  888. KEYRING_SEARCH_NO_CHECK_PERM |
  889. KEYRING_SEARCH_DETECT_TOO_DEEP),
  890. };
  891. rcu_read_lock();
  892. search_nested_keyrings(B, &ctx);
  893. rcu_read_unlock();
  894. return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result);
  895. }
  896. /*
  897. * Preallocate memory so that a key can be linked into to a keyring.
  898. */
  899. int __key_link_begin(struct key *keyring,
  900. const struct keyring_index_key *index_key,
  901. struct assoc_array_edit **_edit)
  902. __acquires(&keyring->sem)
  903. __acquires(&keyring_serialise_link_sem)
  904. {
  905. struct assoc_array_edit *edit;
  906. int ret;
  907. kenter("%d,%s,%s,",
  908. keyring->serial, index_key->type->name, index_key->description);
  909. BUG_ON(index_key->desc_len == 0);
  910. if (keyring->type != &key_type_keyring)
  911. return -ENOTDIR;
  912. down_write(&keyring->sem);
  913. ret = -EKEYREVOKED;
  914. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  915. goto error_krsem;
  916. /* serialise link/link calls to prevent parallel calls causing a cycle
  917. * when linking two keyring in opposite orders */
  918. if (index_key->type == &key_type_keyring)
  919. down_write(&keyring_serialise_link_sem);
  920. /* Create an edit script that will insert/replace the key in the
  921. * keyring tree.
  922. */
  923. edit = assoc_array_insert(&keyring->keys,
  924. &keyring_assoc_array_ops,
  925. index_key,
  926. NULL);
  927. if (IS_ERR(edit)) {
  928. ret = PTR_ERR(edit);
  929. goto error_sem;
  930. }
  931. /* If we're not replacing a link in-place then we're going to need some
  932. * extra quota.
  933. */
  934. if (!edit->dead_leaf) {
  935. ret = key_payload_reserve(keyring,
  936. keyring->datalen + KEYQUOTA_LINK_BYTES);
  937. if (ret < 0)
  938. goto error_cancel;
  939. }
  940. *_edit = edit;
  941. kleave(" = 0");
  942. return 0;
  943. error_cancel:
  944. assoc_array_cancel_edit(edit);
  945. error_sem:
  946. if (index_key->type == &key_type_keyring)
  947. up_write(&keyring_serialise_link_sem);
  948. error_krsem:
  949. up_write(&keyring->sem);
  950. kleave(" = %d", ret);
  951. return ret;
  952. }
  953. /*
  954. * Check already instantiated keys aren't going to be a problem.
  955. *
  956. * The caller must have called __key_link_begin(). Don't need to call this for
  957. * keys that were created since __key_link_begin() was called.
  958. */
  959. int __key_link_check_live_key(struct key *keyring, struct key *key)
  960. {
  961. if (key->type == &key_type_keyring)
  962. /* check that we aren't going to create a cycle by linking one
  963. * keyring to another */
  964. return keyring_detect_cycle(keyring, key);
  965. return 0;
  966. }
  967. /*
  968. * Link a key into to a keyring.
  969. *
  970. * Must be called with __key_link_begin() having being called. Discards any
  971. * already extant link to matching key if there is one, so that each keyring
  972. * holds at most one link to any given key of a particular type+description
  973. * combination.
  974. */
  975. void __key_link(struct key *key, struct assoc_array_edit **_edit)
  976. {
  977. __key_get(key);
  978. assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key));
  979. assoc_array_apply_edit(*_edit);
  980. *_edit = NULL;
  981. }
  982. /*
  983. * Finish linking a key into to a keyring.
  984. *
  985. * Must be called with __key_link_begin() having being called.
  986. */
  987. void __key_link_end(struct key *keyring,
  988. const struct keyring_index_key *index_key,
  989. struct assoc_array_edit *edit)
  990. __releases(&keyring->sem)
  991. __releases(&keyring_serialise_link_sem)
  992. {
  993. BUG_ON(index_key->type == NULL);
  994. kenter("%d,%s,", keyring->serial, index_key->type->name);
  995. if (index_key->type == &key_type_keyring)
  996. up_write(&keyring_serialise_link_sem);
  997. if (edit && !edit->dead_leaf) {
  998. key_payload_reserve(keyring,
  999. keyring->datalen - KEYQUOTA_LINK_BYTES);
  1000. assoc_array_cancel_edit(edit);
  1001. }
  1002. up_write(&keyring->sem);
  1003. }
  1004. /**
  1005. * key_link - Link a key to a keyring
  1006. * @keyring: The keyring to make the link in.
  1007. * @key: The key to link to.
  1008. *
  1009. * Make a link in a keyring to a key, such that the keyring holds a reference
  1010. * on that key and the key can potentially be found by searching that keyring.
  1011. *
  1012. * This function will write-lock the keyring's semaphore and will consume some
  1013. * of the user's key data quota to hold the link.
  1014. *
  1015. * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
  1016. * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
  1017. * full, -EDQUOT if there is insufficient key data quota remaining to add
  1018. * another link or -ENOMEM if there's insufficient memory.
  1019. *
  1020. * It is assumed that the caller has checked that it is permitted for a link to
  1021. * be made (the keyring should have Write permission and the key Link
  1022. * permission).
  1023. */
  1024. int key_link(struct key *keyring, struct key *key)
  1025. {
  1026. struct assoc_array_edit *edit;
  1027. int ret;
  1028. kenter("{%d,%d}", keyring->serial, atomic_read(&keyring->usage));
  1029. key_check(keyring);
  1030. key_check(key);
  1031. if (test_bit(KEY_FLAG_TRUSTED_ONLY, &keyring->flags) &&
  1032. !test_bit(KEY_FLAG_TRUSTED, &key->flags))
  1033. return -EPERM;
  1034. ret = __key_link_begin(keyring, &key->index_key, &edit);
  1035. if (ret == 0) {
  1036. kdebug("begun {%d,%d}", keyring->serial, atomic_read(&keyring->usage));
  1037. ret = __key_link_check_live_key(keyring, key);
  1038. if (ret == 0)
  1039. __key_link(key, &edit);
  1040. __key_link_end(keyring, &key->index_key, edit);
  1041. }
  1042. kleave(" = %d {%d,%d}", ret, keyring->serial, atomic_read(&keyring->usage));
  1043. return ret;
  1044. }
  1045. EXPORT_SYMBOL(key_link);
  1046. /**
  1047. * key_unlink - Unlink the first link to a key from a keyring.
  1048. * @keyring: The keyring to remove the link from.
  1049. * @key: The key the link is to.
  1050. *
  1051. * Remove a link from a keyring to a key.
  1052. *
  1053. * This function will write-lock the keyring's semaphore.
  1054. *
  1055. * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
  1056. * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
  1057. * memory.
  1058. *
  1059. * It is assumed that the caller has checked that it is permitted for a link to
  1060. * be removed (the keyring should have Write permission; no permissions are
  1061. * required on the key).
  1062. */
  1063. int key_unlink(struct key *keyring, struct key *key)
  1064. {
  1065. struct assoc_array_edit *edit;
  1066. int ret;
  1067. key_check(keyring);
  1068. key_check(key);
  1069. if (keyring->type != &key_type_keyring)
  1070. return -ENOTDIR;
  1071. down_write(&keyring->sem);
  1072. edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops,
  1073. &key->index_key);
  1074. if (IS_ERR(edit)) {
  1075. ret = PTR_ERR(edit);
  1076. goto error;
  1077. }
  1078. ret = -ENOENT;
  1079. if (edit == NULL)
  1080. goto error;
  1081. assoc_array_apply_edit(edit);
  1082. key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES);
  1083. ret = 0;
  1084. error:
  1085. up_write(&keyring->sem);
  1086. return ret;
  1087. }
  1088. EXPORT_SYMBOL(key_unlink);
  1089. /**
  1090. * keyring_clear - Clear a keyring
  1091. * @keyring: The keyring to clear.
  1092. *
  1093. * Clear the contents of the specified keyring.
  1094. *
  1095. * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
  1096. */
  1097. int keyring_clear(struct key *keyring)
  1098. {
  1099. struct assoc_array_edit *edit;
  1100. int ret;
  1101. if (keyring->type != &key_type_keyring)
  1102. return -ENOTDIR;
  1103. down_write(&keyring->sem);
  1104. edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
  1105. if (IS_ERR(edit)) {
  1106. ret = PTR_ERR(edit);
  1107. } else {
  1108. if (edit)
  1109. assoc_array_apply_edit(edit);
  1110. key_payload_reserve(keyring, 0);
  1111. ret = 0;
  1112. }
  1113. up_write(&keyring->sem);
  1114. return ret;
  1115. }
  1116. EXPORT_SYMBOL(keyring_clear);
  1117. /*
  1118. * Dispose of the links from a revoked keyring.
  1119. *
  1120. * This is called with the key sem write-locked.
  1121. */
  1122. static void keyring_revoke(struct key *keyring)
  1123. {
  1124. struct assoc_array_edit *edit;
  1125. edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
  1126. if (!IS_ERR(edit)) {
  1127. if (edit)
  1128. assoc_array_apply_edit(edit);
  1129. key_payload_reserve(keyring, 0);
  1130. }
  1131. }
  1132. static bool gc_iterator(void *object, void *iterator_data)
  1133. {
  1134. struct key *key = keyring_ptr_to_key(object);
  1135. time_t *limit = iterator_data;
  1136. if (key_is_dead(key, *limit))
  1137. return false;
  1138. key_get(key);
  1139. return true;
  1140. }
  1141. /*
  1142. * Collect garbage from the contents of a keyring, replacing the old list with
  1143. * a new one with the pointers all shuffled down.
  1144. *
  1145. * Dead keys are classed as oned that are flagged as being dead or are revoked,
  1146. * expired or negative keys that were revoked or expired before the specified
  1147. * limit.
  1148. */
  1149. void keyring_gc(struct key *keyring, time_t limit)
  1150. {
  1151. kenter("{%x,%s}", key_serial(keyring), keyring->description);
  1152. down_write(&keyring->sem);
  1153. assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops,
  1154. gc_iterator, &limit);
  1155. up_write(&keyring->sem);
  1156. kleave("");
  1157. }