crypto.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. char *aa_calc_hash(void *data, size_t len)
  27. {
  28. SHASH_DESC_ON_STACK(desc, apparmor_tfm);
  29. char *hash = NULL;
  30. int error = -ENOMEM;
  31. if (!apparmor_tfm)
  32. return NULL;
  33. hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
  34. if (!hash)
  35. goto fail;
  36. desc->tfm = apparmor_tfm;
  37. desc->flags = 0;
  38. error = crypto_shash_init(desc);
  39. if (error)
  40. goto fail;
  41. error = crypto_shash_update(desc, (u8 *) data, len);
  42. if (error)
  43. goto fail;
  44. error = crypto_shash_final(desc, hash);
  45. if (error)
  46. goto fail;
  47. return hash;
  48. fail:
  49. kfree(hash);
  50. return ERR_PTR(error);
  51. }
  52. int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
  53. size_t len)
  54. {
  55. SHASH_DESC_ON_STACK(desc, apparmor_tfm);
  56. int error = -ENOMEM;
  57. __le32 le32_version = cpu_to_le32(version);
  58. if (!aa_g_hash_policy)
  59. return 0;
  60. if (!apparmor_tfm)
  61. return 0;
  62. profile->hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
  63. if (!profile->hash)
  64. goto fail;
  65. desc->tfm = apparmor_tfm;
  66. desc->flags = 0;
  67. error = crypto_shash_init(desc);
  68. if (error)
  69. goto fail;
  70. error = crypto_shash_update(desc, (u8 *) &le32_version, 4);
  71. if (error)
  72. goto fail;
  73. error = crypto_shash_update(desc, (u8 *) start, len);
  74. if (error)
  75. goto fail;
  76. error = crypto_shash_final(desc, profile->hash);
  77. if (error)
  78. goto fail;
  79. return 0;
  80. fail:
  81. kfree(profile->hash);
  82. profile->hash = NULL;
  83. return error;
  84. }
  85. static int __init init_profile_hash(void)
  86. {
  87. struct crypto_shash *tfm;
  88. if (!apparmor_initialized)
  89. return 0;
  90. tfm = crypto_alloc_shash("sha1", 0, CRYPTO_ALG_ASYNC);
  91. if (IS_ERR(tfm)) {
  92. int error = PTR_ERR(tfm);
  93. AA_ERROR("failed to setup profile sha1 hashing: %d\n", error);
  94. return error;
  95. }
  96. apparmor_tfm = tfm;
  97. apparmor_hash_size = crypto_shash_digestsize(apparmor_tfm);
  98. aa_info_message("AppArmor sha1 policy hashing enabled");
  99. return 0;
  100. }
  101. late_initcall(init_profile_hash);