crypto.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 (!apparmor_tfm)
  36. return 0;
  37. profile->hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
  38. if (!profile->hash)
  39. goto fail;
  40. desc.shash.tfm = apparmor_tfm;
  41. desc.shash.flags = 0;
  42. error = crypto_shash_init(&desc.shash);
  43. if (error)
  44. goto fail;
  45. error = crypto_shash_update(&desc.shash, (u8 *) &le32_version, 4);
  46. if (error)
  47. goto fail;
  48. error = crypto_shash_update(&desc.shash, (u8 *) start, len);
  49. if (error)
  50. goto fail;
  51. error = crypto_shash_final(&desc.shash, profile->hash);
  52. if (error)
  53. goto fail;
  54. return 0;
  55. fail:
  56. kfree(profile->hash);
  57. profile->hash = NULL;
  58. return error;
  59. }
  60. static int __init init_profile_hash(void)
  61. {
  62. struct crypto_shash *tfm;
  63. if (!apparmor_initialized)
  64. return 0;
  65. tfm = crypto_alloc_shash("sha1", 0, CRYPTO_ALG_ASYNC);
  66. if (IS_ERR(tfm)) {
  67. int error = PTR_ERR(tfm);
  68. AA_ERROR("failed to setup profile sha1 hashing: %d\n", error);
  69. return error;
  70. }
  71. apparmor_tfm = tfm;
  72. apparmor_hash_size = crypto_shash_digestsize(apparmor_tfm);
  73. aa_info_message("AppArmor sha1 policy hashing enabled");
  74. return 0;
  75. }
  76. late_initcall(init_profile_hash);