keyring.c 37 KB

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