policy.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020-2024 Microsoft Corporation. All rights reserved.
  4. */
  5. #include <linux/errno.h>
  6. #include <linux/verification.h>
  7. #include "ipe.h"
  8. #include "eval.h"
  9. #include "fs.h"
  10. #include "policy.h"
  11. #include "policy_parser.h"
  12. #include "audit.h"
  13. /* lock for synchronizing writers across ipe policy */
  14. DEFINE_MUTEX(ipe_policy_lock);
  15. /**
  16. * ver_to_u64() - Convert an internal ipe_policy_version to a u64.
  17. * @p: Policy to extract the version from.
  18. *
  19. * Bits (LSB is index 0):
  20. * [48,32] -> Major
  21. * [32,16] -> Minor
  22. * [16, 0] -> Revision
  23. *
  24. * Return: u64 version of the embedded version structure.
  25. */
  26. static inline u64 ver_to_u64(const struct ipe_policy *const p)
  27. {
  28. u64 r;
  29. r = (((u64)p->parsed->version.major) << 32)
  30. | (((u64)p->parsed->version.minor) << 16)
  31. | ((u64)(p->parsed->version.rev));
  32. return r;
  33. }
  34. /**
  35. * ipe_free_policy() - Deallocate a given IPE policy.
  36. * @p: Supplies the policy to free.
  37. *
  38. * Safe to call on IS_ERR/NULL.
  39. */
  40. void ipe_free_policy(struct ipe_policy *p)
  41. {
  42. if (IS_ERR_OR_NULL(p))
  43. return;
  44. ipe_del_policyfs_node(p);
  45. ipe_free_parsed_policy(p->parsed);
  46. /*
  47. * p->text is allocated only when p->pkcs7 is not NULL
  48. * otherwise it points to the plaintext data inside the pkcs7
  49. */
  50. if (!p->pkcs7)
  51. kfree(p->text);
  52. kfree(p->pkcs7);
  53. kfree(p);
  54. }
  55. static int set_pkcs7_data(void *ctx, const void *data, size_t len,
  56. size_t asn1hdrlen __always_unused)
  57. {
  58. struct ipe_policy *p = ctx;
  59. p->text = (const char *)data;
  60. p->textlen = len;
  61. return 0;
  62. }
  63. /**
  64. * ipe_update_policy() - parse a new policy and replace old with it.
  65. * @root: Supplies a pointer to the securityfs inode saved the policy.
  66. * @text: Supplies a pointer to the plain text policy.
  67. * @textlen: Supplies the length of @text.
  68. * @pkcs7: Supplies a pointer to a buffer containing a pkcs7 message.
  69. * @pkcs7len: Supplies the length of @pkcs7len.
  70. *
  71. * @text/@textlen is mutually exclusive with @pkcs7/@pkcs7len - see
  72. * ipe_new_policy.
  73. *
  74. * Context: Requires root->i_rwsem to be held.
  75. * Return: %0 on success. If an error occurs, the function will return
  76. * the -errno.
  77. */
  78. int ipe_update_policy(struct inode *root, const char *text, size_t textlen,
  79. const char *pkcs7, size_t pkcs7len)
  80. {
  81. struct ipe_policy *old, *ap, *new = NULL;
  82. int rc = 0;
  83. old = (struct ipe_policy *)root->i_private;
  84. if (!old)
  85. return -ENOENT;
  86. new = ipe_new_policy(text, textlen, pkcs7, pkcs7len);
  87. if (IS_ERR(new))
  88. return PTR_ERR(new);
  89. if (strcmp(new->parsed->name, old->parsed->name)) {
  90. rc = -EINVAL;
  91. goto err;
  92. }
  93. if (ver_to_u64(old) >= ver_to_u64(new)) {
  94. rc = -ESTALE;
  95. goto err;
  96. }
  97. root->i_private = new;
  98. swap(new->policyfs, old->policyfs);
  99. ipe_audit_policy_load(new);
  100. mutex_lock(&ipe_policy_lock);
  101. ap = rcu_dereference_protected(ipe_active_policy,
  102. lockdep_is_held(&ipe_policy_lock));
  103. if (old == ap) {
  104. rcu_assign_pointer(ipe_active_policy, new);
  105. mutex_unlock(&ipe_policy_lock);
  106. ipe_audit_policy_activation(old, new);
  107. } else {
  108. mutex_unlock(&ipe_policy_lock);
  109. }
  110. synchronize_rcu();
  111. ipe_free_policy(old);
  112. return 0;
  113. err:
  114. ipe_free_policy(new);
  115. return rc;
  116. }
  117. /**
  118. * ipe_new_policy() - Allocate and parse an ipe_policy structure.
  119. *
  120. * @text: Supplies a pointer to the plain-text policy to parse.
  121. * @textlen: Supplies the length of @text.
  122. * @pkcs7: Supplies a pointer to a pkcs7-signed IPE policy.
  123. * @pkcs7len: Supplies the length of @pkcs7.
  124. *
  125. * @text/@textlen Should be NULL/0 if @pkcs7/@pkcs7len is set.
  126. *
  127. * Return:
  128. * * a pointer to the ipe_policy structure - Success
  129. * * %-EBADMSG - Policy is invalid
  130. * * %-ENOMEM - Out of memory (OOM)
  131. * * %-ERANGE - Policy version number overflow
  132. * * %-EINVAL - Policy version parsing error
  133. */
  134. struct ipe_policy *ipe_new_policy(const char *text, size_t textlen,
  135. const char *pkcs7, size_t pkcs7len)
  136. {
  137. struct ipe_policy *new = NULL;
  138. int rc = 0;
  139. new = kzalloc(sizeof(*new), GFP_KERNEL);
  140. if (!new)
  141. return ERR_PTR(-ENOMEM);
  142. if (!text) {
  143. new->pkcs7len = pkcs7len;
  144. new->pkcs7 = kmemdup(pkcs7, pkcs7len, GFP_KERNEL);
  145. if (!new->pkcs7) {
  146. rc = -ENOMEM;
  147. goto err;
  148. }
  149. rc = verify_pkcs7_signature(NULL, 0, new->pkcs7, pkcs7len,
  150. #ifdef CONFIG_IPE_POLICY_SIG_SECONDARY_KEYRING
  151. VERIFY_USE_SECONDARY_KEYRING,
  152. #else
  153. NULL,
  154. #endif
  155. VERIFYING_UNSPECIFIED_SIGNATURE,
  156. set_pkcs7_data, new);
  157. #ifdef CONFIG_IPE_POLICY_SIG_PLATFORM_KEYRING
  158. if (rc == -ENOKEY || rc == -EKEYREJECTED)
  159. rc = verify_pkcs7_signature(NULL, 0, new->pkcs7, pkcs7len,
  160. VERIFY_USE_PLATFORM_KEYRING,
  161. VERIFYING_UNSPECIFIED_SIGNATURE,
  162. set_pkcs7_data, new);
  163. #endif
  164. if (rc)
  165. goto err;
  166. } else {
  167. new->textlen = textlen;
  168. new->text = kstrdup(text, GFP_KERNEL);
  169. if (!new->text) {
  170. rc = -ENOMEM;
  171. goto err;
  172. }
  173. }
  174. rc = ipe_parse_policy(new);
  175. if (rc)
  176. goto err;
  177. return new;
  178. err:
  179. ipe_free_policy(new);
  180. return ERR_PTR(rc);
  181. }
  182. /**
  183. * ipe_set_active_pol() - Make @p the active policy.
  184. * @p: Supplies a pointer to the policy to make active.
  185. *
  186. * Context: Requires root->i_rwsem, which i_private has the policy, to be held.
  187. * Return:
  188. * * %0 - Success
  189. * * %-EINVAL - New active policy version is invalid
  190. */
  191. int ipe_set_active_pol(const struct ipe_policy *p)
  192. {
  193. struct ipe_policy *ap = NULL;
  194. mutex_lock(&ipe_policy_lock);
  195. ap = rcu_dereference_protected(ipe_active_policy,
  196. lockdep_is_held(&ipe_policy_lock));
  197. if (ap == p) {
  198. mutex_unlock(&ipe_policy_lock);
  199. return 0;
  200. }
  201. if (ap && ver_to_u64(ap) > ver_to_u64(p)) {
  202. mutex_unlock(&ipe_policy_lock);
  203. return -EINVAL;
  204. }
  205. rcu_assign_pointer(ipe_active_policy, p);
  206. ipe_audit_policy_activation(ap, p);
  207. mutex_unlock(&ipe_policy_lock);
  208. return 0;
  209. }