crypto.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor policy loading interface function definitions.
  6. *
  7. * Copyright 2013 Canonical Ltd.
  8. *
  9. * Fns to provide a checksum of policy that has been loaded this can be
  10. * compared to userspace policy compiles to check loaded policy is what
  11. * it should be.
  12. */
  13. #include <crypto/hash.h>
  14. #include "include/apparmor.h"
  15. #include "include/crypto.h"
  16. static unsigned int apparmor_hash_size;
  17. static struct crypto_shash *apparmor_tfm;
  18. unsigned int aa_hash_size(void)
  19. {
  20. return apparmor_hash_size;
  21. }
  22. char *aa_calc_hash(void *data, size_t len)
  23. {
  24. SHASH_DESC_ON_STACK(desc, apparmor_tfm);
  25. char *hash = NULL;
  26. int error = -ENOMEM;
  27. if (!apparmor_tfm)
  28. return NULL;
  29. hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
  30. if (!hash)
  31. goto fail;
  32. desc->tfm = apparmor_tfm;
  33. error = crypto_shash_init(desc);
  34. if (error)
  35. goto fail;
  36. error = crypto_shash_update(desc, (u8 *) data, len);
  37. if (error)
  38. goto fail;
  39. error = crypto_shash_final(desc, hash);
  40. if (error)
  41. goto fail;
  42. return hash;
  43. fail:
  44. kfree(hash);
  45. return ERR_PTR(error);
  46. }
  47. int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
  48. size_t len)
  49. {
  50. SHASH_DESC_ON_STACK(desc, apparmor_tfm);
  51. int error = -ENOMEM;
  52. __le32 le32_version = cpu_to_le32(version);
  53. if (!aa_g_hash_policy)
  54. return 0;
  55. if (!apparmor_tfm)
  56. return 0;
  57. profile->hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
  58. if (!profile->hash)
  59. goto fail;
  60. desc->tfm = apparmor_tfm;
  61. error = crypto_shash_init(desc);
  62. if (error)
  63. goto fail;
  64. error = crypto_shash_update(desc, (u8 *) &le32_version, 4);
  65. if (error)
  66. goto fail;
  67. error = crypto_shash_update(desc, (u8 *) start, len);
  68. if (error)
  69. goto fail;
  70. error = crypto_shash_final(desc, profile->hash);
  71. if (error)
  72. goto fail;
  73. return 0;
  74. fail:
  75. kfree(profile->hash);
  76. profile->hash = NULL;
  77. return error;
  78. }
  79. static int __init init_profile_hash(void)
  80. {
  81. struct crypto_shash *tfm;
  82. if (!apparmor_initialized)
  83. return 0;
  84. tfm = crypto_alloc_shash("sha1", 0, 0);
  85. if (IS_ERR(tfm)) {
  86. int error = PTR_ERR(tfm);
  87. AA_ERROR("failed to setup profile sha1 hashing: %d\n", error);
  88. return error;
  89. }
  90. apparmor_tfm = tfm;
  91. apparmor_hash_size = crypto_shash_digestsize(apparmor_tfm);
  92. aa_info_message("AppArmor sha1 policy hashing enabled");
  93. return 0;
  94. }
  95. late_initcall(init_profile_hash);