ima_asymmetric_keys.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2019 Microsoft Corporation
  4. *
  5. * Author: Lakshmi Ramasubramanian ([email protected])
  6. *
  7. * File: ima_asymmetric_keys.c
  8. * Defines an IMA hook to measure asymmetric keys on key
  9. * create or update.
  10. */
  11. #include <keys/asymmetric-type.h>
  12. #include <linux/user_namespace.h>
  13. #include <linux/ima.h>
  14. #include "ima.h"
  15. /**
  16. * ima_post_key_create_or_update - measure asymmetric keys
  17. * @keyring: keyring to which the key is linked to
  18. * @key: created or updated key
  19. * @payload: The data used to instantiate or update the key.
  20. * @payload_len: The length of @payload.
  21. * @flags: key flags
  22. * @create: flag indicating whether the key was created or updated
  23. *
  24. * Keys can only be measured, not appraised.
  25. * The payload data used to instantiate or update the key is measured.
  26. */
  27. void ima_post_key_create_or_update(struct key *keyring, struct key *key,
  28. const void *payload, size_t payload_len,
  29. unsigned long flags, bool create)
  30. {
  31. bool queued = false;
  32. /* Only asymmetric keys are handled by this hook. */
  33. if (key->type != &key_type_asymmetric)
  34. return;
  35. if (!payload || (payload_len == 0))
  36. return;
  37. if (ima_should_queue_key())
  38. queued = ima_queue_key(keyring, payload, payload_len);
  39. if (queued)
  40. return;
  41. /*
  42. * keyring->description points to the name of the keyring
  43. * (such as ".builtin_trusted_keys", ".ima", etc.) to
  44. * which the given key is linked to.
  45. *
  46. * The name of the keyring is passed in the "eventname"
  47. * parameter to process_buffer_measurement() and is set
  48. * in the "eventname" field in ima_event_data for
  49. * the key measurement IMA event.
  50. *
  51. * The name of the keyring is also passed in the "keyring"
  52. * parameter to process_buffer_measurement() to check
  53. * if the IMA policy is configured to measure a key linked
  54. * to the given keyring.
  55. */
  56. process_buffer_measurement(&init_user_ns, NULL, payload, payload_len,
  57. keyring->description, KEY_CHECK, 0,
  58. keyring->description, false, NULL, 0);
  59. }