crypto.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor policy loading interface function definitions.
  5. *
  6. * Copyright 2013 Canonical Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation, version 2 of the
  11. * License.
  12. *
  13. * Fns to provide a checksum of policy that has been loaded this can be
  14. * compared to userspace policy compiles to check loaded policy is what
  15. * it should be.
  16. */
  17. #include <crypto/hash.h>
  18. #include "include/apparmor.h"
  19. #include "include/crypto.h"
  20. static unsigned int apparmor_hash_size;
  21. static struct crypto_shash *apparmor_tfm;
  22. unsigned int aa_hash_size(void)
  23. {
  24. return apparmor_hash_size;
  25. }
  26. int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
  27. size_t len)
  28. {
  29. struct {
  30. struct shash_desc shash;
  31. char ctx[crypto_shash_descsize(apparmor_tfm)];
  32. } desc;
  33. int error = -ENOMEM;
  34. u32 le32_version = cpu_to_le32(version);
  35. if (!aa_g_hash_policy)
  36. return 0;
  37. if (!apparmor_tfm)
  38. return 0;
  39. profile->hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
  40. if (!profile->hash)
  41. goto fail;
  42. desc.shash.tfm = apparmor_tfm;
  43. desc.shash.flags = 0;
  44. error = crypto_shash_init(&desc.shash);
  45. if (error)
  46. goto fail;
  47. error = crypto_shash_update(&desc.shash, (u8 *) &le32_version, 4);
  48. if (error)
  49. goto fail;
  50. error = crypto_shash_update(&desc.shash, (u8 *) start, len);
  51. if (error)
  52. goto fail;
  53. error = crypto_shash_final(&desc.shash, profile->hash);
  54. if (error)
  55. goto fail;
  56. return 0;
  57. fail:
  58. kfree(profile->hash);
  59. profile->hash = NULL;
  60. return error;
  61. }
  62. static int __init init_profile_hash(void)
  63. {
  64. struct crypto_shash *tfm;
  65. if (!apparmor_initialized)
  66. return 0;
  67. tfm = crypto_alloc_shash("sha1", 0, CRYPTO_ALG_ASYNC);
  68. if (IS_ERR(tfm)) {
  69. int error = PTR_ERR(tfm);
  70. AA_ERROR("failed to setup profile sha1 hashing: %d\n", error);
  71. return error;
  72. }
  73. apparmor_tfm = tfm;
  74. apparmor_hash_size = crypto_shash_digestsize(apparmor_tfm);
  75. aa_info_message("AppArmor sha1 policy hashing enabled");
  76. return 0;
  77. }
  78. late_initcall(init_profile_hash);