keyring.c 37 KB

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