crypto.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <linux/crypto.h>
  18. #include "include/apparmor.h"
  19. #include "include/crypto.h"
  20. static unsigned int apparmor_hash_size;
  21. static struct crypto_hash *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 scatterlist sg[2];
  30. struct hash_desc desc = {
  31. .tfm = apparmor_tfm,
  32. .flags = 0
  33. };
  34. int error = -ENOMEM;
  35. u32 le32_version = cpu_to_le32(version);
  36. if (!apparmor_tfm)
  37. return 0;
  38. sg_init_table(sg, 2);
  39. sg_set_buf(&sg[0], &le32_version, 4);
  40. sg_set_buf(&sg[1], (u8 *) start, len);
  41. profile->hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
  42. if (!profile->hash)
  43. goto fail;
  44. error = crypto_hash_init(&desc);
  45. if (error)
  46. goto fail;
  47. error = crypto_hash_update(&desc, &sg[0], 4);
  48. if (error)
  49. goto fail;
  50. error = crypto_hash_update(&desc, &sg[1], len);
  51. if (error)
  52. goto fail;
  53. error = crypto_hash_final(&desc, 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_hash *tfm;
  65. if (!apparmor_initialized)
  66. return 0;
  67. tfm = crypto_alloc_hash("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_hash_digestsize(apparmor_tfm);
  75. aa_info_message("AppArmor sha1 policy hashing enabled");
  76. return 0;
  77. }
  78. late_initcall(init_profile_hash);