keyring.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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. ctx->result = ERR_PTR(key->type_data.reject_error);
  480. kleave(" = %d [neg]", ctx->skipped_ret);
  481. goto skipped;
  482. }
  483. }
  484. /* Found */
  485. ctx->result = make_key_ref(key, ctx->possessed);
  486. kleave(" = 1 [found]");
  487. return 1;
  488. skipped:
  489. return ctx->skipped_ret;
  490. }
  491. /*
  492. * Search inside a keyring for a key. We can search by walking to it
  493. * directly based on its index-key or we can iterate over the entire
  494. * tree looking for it, based on the match function.
  495. */
  496. static int search_keyring(struct key *keyring, struct keyring_search_context *ctx)
  497. {
  498. if ((ctx->flags & KEYRING_SEARCH_LOOKUP_TYPE) ==
  499. KEYRING_SEARCH_LOOKUP_DIRECT) {
  500. const void *object;
  501. object = assoc_array_find(&keyring->keys,
  502. &keyring_assoc_array_ops,
  503. &ctx->index_key);
  504. return object ? ctx->iterator(object, ctx) : 0;
  505. }
  506. return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx);
  507. }
  508. /*
  509. * Search a tree of keyrings that point to other keyrings up to the maximum
  510. * depth.
  511. */
  512. static bool search_nested_keyrings(struct key *keyring,
  513. struct keyring_search_context *ctx)
  514. {
  515. struct {
  516. struct key *keyring;
  517. struct assoc_array_node *node;
  518. int slot;
  519. } stack[KEYRING_SEARCH_MAX_DEPTH];
  520. struct assoc_array_shortcut *shortcut;
  521. struct assoc_array_node *node;
  522. struct assoc_array_ptr *ptr;
  523. struct key *key;
  524. int sp = 0, slot;
  525. kenter("{%d},{%s,%s}",
  526. keyring->serial,
  527. ctx->index_key.type->name,
  528. ctx->index_key.description);
  529. if (ctx->index_key.description)
  530. ctx->index_key.desc_len = strlen(ctx->index_key.description);
  531. /* Check to see if this top-level keyring is what we are looking for
  532. * and whether it is valid or not.
  533. */
  534. if (ctx->flags & KEYRING_SEARCH_LOOKUP_ITERATE ||
  535. keyring_compare_object(keyring, &ctx->index_key)) {
  536. ctx->skipped_ret = 2;
  537. ctx->flags |= KEYRING_SEARCH_DO_STATE_CHECK;
  538. switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) {
  539. case 1:
  540. goto found;
  541. case 2:
  542. return false;
  543. default:
  544. break;
  545. }
  546. }
  547. ctx->skipped_ret = 0;
  548. if (ctx->flags & KEYRING_SEARCH_NO_STATE_CHECK)
  549. ctx->flags &= ~KEYRING_SEARCH_DO_STATE_CHECK;
  550. /* Start processing a new keyring */
  551. descend_to_keyring:
  552. kdebug("descend to %d", keyring->serial);
  553. if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
  554. (1 << KEY_FLAG_REVOKED)))
  555. goto not_this_keyring;
  556. /* Search through the keys in this keyring before its searching its
  557. * subtrees.
  558. */
  559. if (search_keyring(keyring, ctx))
  560. goto found;
  561. /* Then manually iterate through the keyrings nested in this one.
  562. *
  563. * Start from the root node of the index tree. Because of the way the
  564. * hash function has been set up, keyrings cluster on the leftmost
  565. * branch of the root node (root slot 0) or in the root node itself.
  566. * Non-keyrings avoid the leftmost branch of the root entirely (root
  567. * slots 1-15).
  568. */
  569. ptr = ACCESS_ONCE(keyring->keys.root);
  570. if (!ptr)
  571. goto not_this_keyring;
  572. if (assoc_array_ptr_is_shortcut(ptr)) {
  573. /* If the root is a shortcut, either the keyring only contains
  574. * keyring pointers (everything clusters behind root slot 0) or
  575. * doesn't contain any keyring pointers.
  576. */
  577. shortcut = assoc_array_ptr_to_shortcut(ptr);
  578. smp_read_barrier_depends();
  579. if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0)
  580. goto not_this_keyring;
  581. ptr = ACCESS_ONCE(shortcut->next_node);
  582. node = assoc_array_ptr_to_node(ptr);
  583. goto begin_node;
  584. }
  585. node = assoc_array_ptr_to_node(ptr);
  586. smp_read_barrier_depends();
  587. ptr = node->slots[0];
  588. if (!assoc_array_ptr_is_meta(ptr))
  589. goto begin_node;
  590. descend_to_node:
  591. /* Descend to a more distal node in this keyring's content tree and go
  592. * through that.
  593. */
  594. kdebug("descend");
  595. if (assoc_array_ptr_is_shortcut(ptr)) {
  596. shortcut = assoc_array_ptr_to_shortcut(ptr);
  597. smp_read_barrier_depends();
  598. ptr = ACCESS_ONCE(shortcut->next_node);
  599. BUG_ON(!assoc_array_ptr_is_node(ptr));
  600. node = assoc_array_ptr_to_node(ptr);
  601. }
  602. begin_node:
  603. kdebug("begin_node");
  604. smp_read_barrier_depends();
  605. slot = 0;
  606. ascend_to_node:
  607. /* Go through the slots in a node */
  608. for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
  609. ptr = ACCESS_ONCE(node->slots[slot]);
  610. if (assoc_array_ptr_is_meta(ptr) && node->back_pointer)
  611. goto descend_to_node;
  612. if (!keyring_ptr_is_keyring(ptr))
  613. continue;
  614. key = keyring_ptr_to_key(ptr);
  615. if (sp >= KEYRING_SEARCH_MAX_DEPTH) {
  616. if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) {
  617. ctx->result = ERR_PTR(-ELOOP);
  618. return false;
  619. }
  620. goto not_this_keyring;
  621. }
  622. /* Search a nested keyring */
  623. if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
  624. key_task_permission(make_key_ref(key, ctx->possessed),
  625. ctx->cred, KEY_SEARCH) < 0)
  626. continue;
  627. /* stack the current position */
  628. stack[sp].keyring = keyring;
  629. stack[sp].node = node;
  630. stack[sp].slot = slot;
  631. sp++;
  632. /* begin again with the new keyring */
  633. keyring = key;
  634. goto descend_to_keyring;
  635. }
  636. /* We've dealt with all the slots in the current node, so now we need
  637. * to ascend to the parent and continue processing there.
  638. */
  639. ptr = ACCESS_ONCE(node->back_pointer);
  640. slot = node->parent_slot;
  641. if (ptr && assoc_array_ptr_is_shortcut(ptr)) {
  642. shortcut = assoc_array_ptr_to_shortcut(ptr);
  643. smp_read_barrier_depends();
  644. ptr = ACCESS_ONCE(shortcut->back_pointer);
  645. slot = shortcut->parent_slot;
  646. }
  647. if (!ptr)
  648. goto not_this_keyring;
  649. node = assoc_array_ptr_to_node(ptr);
  650. smp_read_barrier_depends();
  651. slot++;
  652. /* If we've ascended to the root (zero backpointer), we must have just
  653. * finished processing the leftmost branch rather than the root slots -
  654. * so there can't be any more keyrings for us to find.
  655. */
  656. if (node->back_pointer) {
  657. kdebug("ascend %d", slot);
  658. goto ascend_to_node;
  659. }
  660. /* The keyring we're looking at was disqualified or didn't contain a
  661. * matching key.
  662. */
  663. not_this_keyring:
  664. kdebug("not_this_keyring %d", sp);
  665. if (sp <= 0) {
  666. kleave(" = false");
  667. return false;
  668. }
  669. /* Resume the processing of a keyring higher up in the tree */
  670. sp--;
  671. keyring = stack[sp].keyring;
  672. node = stack[sp].node;
  673. slot = stack[sp].slot + 1;
  674. kdebug("ascend to %d [%d]", keyring->serial, slot);
  675. goto ascend_to_node;
  676. /* We found a viable match */
  677. found:
  678. key = key_ref_to_ptr(ctx->result);
  679. key_check(key);
  680. if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) {
  681. key->last_used_at = ctx->now.tv_sec;
  682. keyring->last_used_at = ctx->now.tv_sec;
  683. while (sp > 0)
  684. stack[--sp].keyring->last_used_at = ctx->now.tv_sec;
  685. }
  686. kleave(" = true");
  687. return true;
  688. }
  689. /**
  690. * keyring_search_aux - Search a keyring tree for a key matching some criteria
  691. * @keyring_ref: A pointer to the keyring with possession indicator.
  692. * @ctx: The keyring search context.
  693. *
  694. * Search the supplied keyring tree for a key that matches the criteria given.
  695. * The root keyring and any linked keyrings must grant Search permission to the
  696. * caller to be searchable and keys can only be found if they too grant Search
  697. * to the caller. The possession flag on the root keyring pointer controls use
  698. * of the possessor bits in permissions checking of the entire tree. In
  699. * addition, the LSM gets to forbid keyring searches and key matches.
  700. *
  701. * The search is performed as a breadth-then-depth search up to the prescribed
  702. * limit (KEYRING_SEARCH_MAX_DEPTH).
  703. *
  704. * Keys are matched to the type provided and are then filtered by the match
  705. * function, which is given the description to use in any way it sees fit. The
  706. * match function may use any attributes of a key that it wishes to to
  707. * determine the match. Normally the match function from the key type would be
  708. * used.
  709. *
  710. * RCU can be used to prevent the keyring key lists from disappearing without
  711. * the need to take lots of locks.
  712. *
  713. * Returns a pointer to the found key and increments the key usage count if
  714. * successful; -EAGAIN if no matching keys were found, or if expired or revoked
  715. * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
  716. * specified keyring wasn't a keyring.
  717. *
  718. * In the case of a successful return, the possession attribute from
  719. * @keyring_ref is propagated to the returned key reference.
  720. */
  721. key_ref_t keyring_search_aux(key_ref_t keyring_ref,
  722. struct keyring_search_context *ctx)
  723. {
  724. struct key *keyring;
  725. long err;
  726. ctx->iterator = keyring_search_iterator;
  727. ctx->possessed = is_key_possessed(keyring_ref);
  728. ctx->result = ERR_PTR(-EAGAIN);
  729. keyring = key_ref_to_ptr(keyring_ref);
  730. key_check(keyring);
  731. if (keyring->type != &key_type_keyring)
  732. return ERR_PTR(-ENOTDIR);
  733. if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) {
  734. err = key_task_permission(keyring_ref, ctx->cred, KEY_SEARCH);
  735. if (err < 0)
  736. return ERR_PTR(err);
  737. }
  738. rcu_read_lock();
  739. ctx->now = current_kernel_time();
  740. if (search_nested_keyrings(keyring, ctx))
  741. __key_get(key_ref_to_ptr(ctx->result));
  742. rcu_read_unlock();
  743. return ctx->result;
  744. }
  745. /**
  746. * keyring_search - Search the supplied keyring tree for a matching key
  747. * @keyring: The root of the keyring tree to be searched.
  748. * @type: The type of keyring we want to find.
  749. * @description: The name of the keyring we want to find.
  750. *
  751. * As keyring_search_aux() above, but using the current task's credentials and
  752. * type's default matching function and preferred search method.
  753. */
  754. key_ref_t keyring_search(key_ref_t keyring,
  755. struct key_type *type,
  756. const char *description)
  757. {
  758. struct keyring_search_context ctx = {
  759. .index_key.type = type,
  760. .index_key.description = description,
  761. .cred = current_cred(),
  762. .match = type->match,
  763. .match_data = description,
  764. .flags = (type->def_lookup_type |
  765. KEYRING_SEARCH_DO_STATE_CHECK),
  766. };
  767. if (!ctx.match)
  768. return ERR_PTR(-ENOKEY);
  769. return keyring_search_aux(keyring, &ctx);
  770. }
  771. EXPORT_SYMBOL(keyring_search);
  772. /*
  773. * Search the given keyring for a key that might be updated.
  774. *
  775. * The caller must guarantee that the keyring is a keyring and that the
  776. * permission is granted to modify the keyring as no check is made here. The
  777. * caller must also hold a lock on the keyring semaphore.
  778. *
  779. * Returns a pointer to the found key with usage count incremented if
  780. * successful and returns NULL if not found. Revoked and invalidated keys are
  781. * skipped over.
  782. *
  783. * If successful, the possession indicator is propagated from the keyring ref
  784. * to the returned key reference.
  785. */
  786. key_ref_t find_key_to_update(key_ref_t keyring_ref,
  787. const struct keyring_index_key *index_key)
  788. {
  789. struct key *keyring, *key;
  790. const void *object;
  791. keyring = key_ref_to_ptr(keyring_ref);
  792. kenter("{%d},{%s,%s}",
  793. keyring->serial, index_key->type->name, index_key->description);
  794. object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
  795. index_key);
  796. if (object)
  797. goto found;
  798. kleave(" = NULL");
  799. return NULL;
  800. found:
  801. key = keyring_ptr_to_key(object);
  802. if (key->flags & ((1 << KEY_FLAG_INVALIDATED) |
  803. (1 << KEY_FLAG_REVOKED))) {
  804. kleave(" = NULL [x]");
  805. return NULL;
  806. }
  807. __key_get(key);
  808. kleave(" = {%d}", key->serial);
  809. return make_key_ref(key, is_key_possessed(keyring_ref));
  810. }
  811. /*
  812. * Find a keyring with the specified name.
  813. *
  814. * All named keyrings in the current user namespace are searched, provided they
  815. * grant Search permission directly to the caller (unless this check is
  816. * skipped). Keyrings whose usage points have reached zero or who have been
  817. * revoked are skipped.
  818. *
  819. * Returns a pointer to the keyring with the keyring's refcount having being
  820. * incremented on success. -ENOKEY is returned if a key could not be found.
  821. */
  822. struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
  823. {
  824. struct key *keyring;
  825. int bucket;
  826. if (!name)
  827. return ERR_PTR(-EINVAL);
  828. bucket = keyring_hash(name);
  829. read_lock(&keyring_name_lock);
  830. if (keyring_name_hash[bucket].next) {
  831. /* search this hash bucket for a keyring with a matching name
  832. * that's readable and that hasn't been revoked */
  833. list_for_each_entry(keyring,
  834. &keyring_name_hash[bucket],
  835. type_data.link
  836. ) {
  837. if (!kuid_has_mapping(current_user_ns(), keyring->user->uid))
  838. continue;
  839. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  840. continue;
  841. if (strcmp(keyring->description, name) != 0)
  842. continue;
  843. if (!skip_perm_check &&
  844. key_permission(make_key_ref(keyring, 0),
  845. KEY_SEARCH) < 0)
  846. continue;
  847. /* we've got a match but we might end up racing with
  848. * key_cleanup() if the keyring is currently 'dead'
  849. * (ie. it has a zero usage count) */
  850. if (!atomic_inc_not_zero(&keyring->usage))
  851. continue;
  852. keyring->last_used_at = current_kernel_time().tv_sec;
  853. goto out;
  854. }
  855. }
  856. keyring = ERR_PTR(-ENOKEY);
  857. out:
  858. read_unlock(&keyring_name_lock);
  859. return keyring;
  860. }
  861. static int keyring_detect_cycle_iterator(const void *object,
  862. void *iterator_data)
  863. {
  864. struct keyring_search_context *ctx = iterator_data;
  865. const struct key *key = keyring_ptr_to_key(object);
  866. kenter("{%d}", key->serial);
  867. BUG_ON(key != ctx->match_data);
  868. ctx->result = ERR_PTR(-EDEADLK);
  869. return 1;
  870. }
  871. /*
  872. * See if a cycle will will be created by inserting acyclic tree B in acyclic
  873. * tree A at the topmost level (ie: as a direct child of A).
  874. *
  875. * Since we are adding B to A at the top level, checking for cycles should just
  876. * be a matter of seeing if node A is somewhere in tree B.
  877. */
  878. static int keyring_detect_cycle(struct key *A, struct key *B)
  879. {
  880. struct keyring_search_context ctx = {
  881. .index_key = A->index_key,
  882. .match_data = A,
  883. .iterator = keyring_detect_cycle_iterator,
  884. .flags = (KEYRING_SEARCH_LOOKUP_DIRECT |
  885. KEYRING_SEARCH_NO_STATE_CHECK |
  886. KEYRING_SEARCH_NO_UPDATE_TIME |
  887. KEYRING_SEARCH_NO_CHECK_PERM |
  888. KEYRING_SEARCH_DETECT_TOO_DEEP),
  889. };
  890. rcu_read_lock();
  891. search_nested_keyrings(B, &ctx);
  892. rcu_read_unlock();
  893. return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result);
  894. }
  895. /*
  896. * Preallocate memory so that a key can be linked into to a keyring.
  897. */
  898. int __key_link_begin(struct key *keyring,
  899. const struct keyring_index_key *index_key,
  900. struct assoc_array_edit **_edit)
  901. __acquires(&keyring->sem)
  902. __acquires(&keyring_serialise_link_sem)
  903. {
  904. struct assoc_array_edit *edit;
  905. int ret;
  906. kenter("%d,%s,%s,",
  907. keyring->serial, index_key->type->name, index_key->description);
  908. BUG_ON(index_key->desc_len == 0);
  909. if (keyring->type != &key_type_keyring)
  910. return -ENOTDIR;
  911. down_write(&keyring->sem);
  912. ret = -EKEYREVOKED;
  913. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  914. goto error_krsem;
  915. /* serialise link/link calls to prevent parallel calls causing a cycle
  916. * when linking two keyring in opposite orders */
  917. if (index_key->type == &key_type_keyring)
  918. down_write(&keyring_serialise_link_sem);
  919. /* check that we aren't going to overrun the user's quota */
  920. ret = key_payload_reserve(keyring,
  921. keyring->datalen + KEYQUOTA_LINK_BYTES);
  922. if (ret < 0)
  923. goto error_sem;
  924. /* Create an edit script that will insert/replace the key in the
  925. * keyring tree.
  926. */
  927. edit = assoc_array_insert(&keyring->keys,
  928. &keyring_assoc_array_ops,
  929. index_key,
  930. NULL);
  931. if (IS_ERR(edit)) {
  932. ret = PTR_ERR(edit);
  933. goto error_quota;
  934. }
  935. *_edit = edit;
  936. kleave(" = 0");
  937. return 0;
  938. error_quota:
  939. /* undo the quota changes */
  940. key_payload_reserve(keyring,
  941. keyring->datalen - KEYQUOTA_LINK_BYTES);
  942. error_sem:
  943. if (index_key->type == &key_type_keyring)
  944. up_write(&keyring_serialise_link_sem);
  945. error_krsem:
  946. up_write(&keyring->sem);
  947. kleave(" = %d", ret);
  948. return ret;
  949. }
  950. /*
  951. * Check already instantiated keys aren't going to be a problem.
  952. *
  953. * The caller must have called __key_link_begin(). Don't need to call this for
  954. * keys that were created since __key_link_begin() was called.
  955. */
  956. int __key_link_check_live_key(struct key *keyring, struct key *key)
  957. {
  958. if (key->type == &key_type_keyring)
  959. /* check that we aren't going to create a cycle by linking one
  960. * keyring to another */
  961. return keyring_detect_cycle(keyring, key);
  962. return 0;
  963. }
  964. /*
  965. * Link a key into to a keyring.
  966. *
  967. * Must be called with __key_link_begin() having being called. Discards any
  968. * already extant link to matching key if there is one, so that each keyring
  969. * holds at most one link to any given key of a particular type+description
  970. * combination.
  971. */
  972. void __key_link(struct key *key, struct assoc_array_edit **_edit)
  973. {
  974. __key_get(key);
  975. assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key));
  976. assoc_array_apply_edit(*_edit);
  977. *_edit = NULL;
  978. }
  979. /*
  980. * Finish linking a key into to a keyring.
  981. *
  982. * Must be called with __key_link_begin() having being called.
  983. */
  984. void __key_link_end(struct key *keyring,
  985. const struct keyring_index_key *index_key,
  986. struct assoc_array_edit *edit)
  987. __releases(&keyring->sem)
  988. __releases(&keyring_serialise_link_sem)
  989. {
  990. BUG_ON(index_key->type == NULL);
  991. kenter("%d,%s,", keyring->serial, index_key->type->name);
  992. if (index_key->type == &key_type_keyring)
  993. up_write(&keyring_serialise_link_sem);
  994. if (edit) {
  995. key_payload_reserve(keyring,
  996. keyring->datalen - KEYQUOTA_LINK_BYTES);
  997. assoc_array_cancel_edit(edit);
  998. }
  999. up_write(&keyring->sem);
  1000. }
  1001. /**
  1002. * key_link - Link a key to a keyring
  1003. * @keyring: The keyring to make the link in.
  1004. * @key: The key to link to.
  1005. *
  1006. * Make a link in a keyring to a key, such that the keyring holds a reference
  1007. * on that key and the key can potentially be found by searching that keyring.
  1008. *
  1009. * This function will write-lock the keyring's semaphore and will consume some
  1010. * of the user's key data quota to hold the link.
  1011. *
  1012. * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
  1013. * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
  1014. * full, -EDQUOT if there is insufficient key data quota remaining to add
  1015. * another link or -ENOMEM if there's insufficient memory.
  1016. *
  1017. * It is assumed that the caller has checked that it is permitted for a link to
  1018. * be made (the keyring should have Write permission and the key Link
  1019. * permission).
  1020. */
  1021. int key_link(struct key *keyring, struct key *key)
  1022. {
  1023. struct assoc_array_edit *edit;
  1024. int ret;
  1025. kenter("{%d,%d}", keyring->serial, atomic_read(&keyring->usage));
  1026. key_check(keyring);
  1027. key_check(key);
  1028. ret = __key_link_begin(keyring, &key->index_key, &edit);
  1029. if (ret == 0) {
  1030. kdebug("begun {%d,%d}", keyring->serial, atomic_read(&keyring->usage));
  1031. ret = __key_link_check_live_key(keyring, key);
  1032. if (ret == 0)
  1033. __key_link(key, &edit);
  1034. __key_link_end(keyring, &key->index_key, edit);
  1035. }
  1036. kleave(" = %d {%d,%d}", ret, keyring->serial, atomic_read(&keyring->usage));
  1037. return ret;
  1038. }
  1039. EXPORT_SYMBOL(key_link);
  1040. /**
  1041. * key_unlink - Unlink the first link to a key from a keyring.
  1042. * @keyring: The keyring to remove the link from.
  1043. * @key: The key the link is to.
  1044. *
  1045. * Remove a link from a keyring to a key.
  1046. *
  1047. * This function will write-lock the keyring's semaphore.
  1048. *
  1049. * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
  1050. * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
  1051. * memory.
  1052. *
  1053. * It is assumed that the caller has checked that it is permitted for a link to
  1054. * be removed (the keyring should have Write permission; no permissions are
  1055. * required on the key).
  1056. */
  1057. int key_unlink(struct key *keyring, struct key *key)
  1058. {
  1059. struct assoc_array_edit *edit;
  1060. int ret;
  1061. key_check(keyring);
  1062. key_check(key);
  1063. if (keyring->type != &key_type_keyring)
  1064. return -ENOTDIR;
  1065. down_write(&keyring->sem);
  1066. edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops,
  1067. &key->index_key);
  1068. if (IS_ERR(edit)) {
  1069. ret = PTR_ERR(edit);
  1070. goto error;
  1071. }
  1072. ret = -ENOENT;
  1073. if (edit == NULL)
  1074. goto error;
  1075. assoc_array_apply_edit(edit);
  1076. ret = 0;
  1077. error:
  1078. up_write(&keyring->sem);
  1079. return ret;
  1080. }
  1081. EXPORT_SYMBOL(key_unlink);
  1082. /**
  1083. * keyring_clear - Clear a keyring
  1084. * @keyring: The keyring to clear.
  1085. *
  1086. * Clear the contents of the specified keyring.
  1087. *
  1088. * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
  1089. */
  1090. int keyring_clear(struct key *keyring)
  1091. {
  1092. struct assoc_array_edit *edit;
  1093. int ret;
  1094. if (keyring->type != &key_type_keyring)
  1095. return -ENOTDIR;
  1096. down_write(&keyring->sem);
  1097. edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
  1098. if (IS_ERR(edit)) {
  1099. ret = PTR_ERR(edit);
  1100. } else {
  1101. if (edit)
  1102. assoc_array_apply_edit(edit);
  1103. key_payload_reserve(keyring, 0);
  1104. ret = 0;
  1105. }
  1106. up_write(&keyring->sem);
  1107. return ret;
  1108. }
  1109. EXPORT_SYMBOL(keyring_clear);
  1110. /*
  1111. * Dispose of the links from a revoked keyring.
  1112. *
  1113. * This is called with the key sem write-locked.
  1114. */
  1115. static void keyring_revoke(struct key *keyring)
  1116. {
  1117. struct assoc_array_edit *edit;
  1118. edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
  1119. if (!IS_ERR(edit)) {
  1120. if (edit)
  1121. assoc_array_apply_edit(edit);
  1122. key_payload_reserve(keyring, 0);
  1123. }
  1124. }
  1125. static bool gc_iterator(void *object, void *iterator_data)
  1126. {
  1127. struct key *key = keyring_ptr_to_key(object);
  1128. time_t *limit = iterator_data;
  1129. if (key_is_dead(key, *limit))
  1130. return false;
  1131. key_get(key);
  1132. return true;
  1133. }
  1134. /*
  1135. * Collect garbage from the contents of a keyring, replacing the old list with
  1136. * a new one with the pointers all shuffled down.
  1137. *
  1138. * Dead keys are classed as oned that are flagged as being dead or are revoked,
  1139. * expired or negative keys that were revoked or expired before the specified
  1140. * limit.
  1141. */
  1142. void keyring_gc(struct key *keyring, time_t limit)
  1143. {
  1144. kenter("{%x,%s}", key_serial(keyring), keyring->description);
  1145. down_write(&keyring->sem);
  1146. assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops,
  1147. gc_iterator, &limit);
  1148. up_write(&keyring->sem);
  1149. kleave("");
  1150. }