keyring.c 36 KB

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