audit.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020-2024 Microsoft Corporation. All rights reserved.
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/audit.h>
  7. #include <linux/types.h>
  8. #include <crypto/hash.h>
  9. #include "ipe.h"
  10. #include "eval.h"
  11. #include "hooks.h"
  12. #include "policy.h"
  13. #include "audit.h"
  14. #include "digest.h"
  15. #define ACTSTR(x) ((x) == IPE_ACTION_ALLOW ? "ALLOW" : "DENY")
  16. #define IPE_AUDIT_HASH_ALG "sha256"
  17. #define AUDIT_POLICY_LOAD_FMT "policy_name=\"%s\" policy_version=%hu.%hu.%hu "\
  18. "policy_digest=" IPE_AUDIT_HASH_ALG ":"
  19. #define AUDIT_OLD_ACTIVE_POLICY_FMT "old_active_pol_name=\"%s\" "\
  20. "old_active_pol_version=%hu.%hu.%hu "\
  21. "old_policy_digest=" IPE_AUDIT_HASH_ALG ":"
  22. #define AUDIT_OLD_ACTIVE_POLICY_NULL_FMT "old_active_pol_name=? "\
  23. "old_active_pol_version=? "\
  24. "old_policy_digest=?"
  25. #define AUDIT_NEW_ACTIVE_POLICY_FMT "new_active_pol_name=\"%s\" "\
  26. "new_active_pol_version=%hu.%hu.%hu "\
  27. "new_policy_digest=" IPE_AUDIT_HASH_ALG ":"
  28. static const char *const audit_op_names[__IPE_OP_MAX + 1] = {
  29. "EXECUTE",
  30. "FIRMWARE",
  31. "KMODULE",
  32. "KEXEC_IMAGE",
  33. "KEXEC_INITRAMFS",
  34. "POLICY",
  35. "X509_CERT",
  36. "UNKNOWN",
  37. };
  38. static const char *const audit_hook_names[__IPE_HOOK_MAX] = {
  39. "BPRM_CHECK",
  40. "MMAP",
  41. "MPROTECT",
  42. "KERNEL_READ",
  43. "KERNEL_LOAD",
  44. };
  45. static const char *const audit_prop_names[__IPE_PROP_MAX] = {
  46. "boot_verified=FALSE",
  47. "boot_verified=TRUE",
  48. "dmverity_roothash=",
  49. "dmverity_signature=FALSE",
  50. "dmverity_signature=TRUE",
  51. "fsverity_digest=",
  52. "fsverity_signature=FALSE",
  53. "fsverity_signature=TRUE",
  54. };
  55. /**
  56. * audit_dmv_roothash() - audit the roothash of a dmverity_roothash property.
  57. * @ab: Supplies a pointer to the audit_buffer to append to.
  58. * @rh: Supplies a pointer to the digest structure.
  59. */
  60. static void audit_dmv_roothash(struct audit_buffer *ab, const void *rh)
  61. {
  62. audit_log_format(ab, "%s", audit_prop_names[IPE_PROP_DMV_ROOTHASH]);
  63. ipe_digest_audit(ab, rh);
  64. }
  65. /**
  66. * audit_fsv_digest() - audit the digest of a fsverity_digest property.
  67. * @ab: Supplies a pointer to the audit_buffer to append to.
  68. * @d: Supplies a pointer to the digest structure.
  69. */
  70. static void audit_fsv_digest(struct audit_buffer *ab, const void *d)
  71. {
  72. audit_log_format(ab, "%s", audit_prop_names[IPE_PROP_FSV_DIGEST]);
  73. ipe_digest_audit(ab, d);
  74. }
  75. /**
  76. * audit_rule() - audit an IPE policy rule.
  77. * @ab: Supplies a pointer to the audit_buffer to append to.
  78. * @r: Supplies a pointer to the ipe_rule to approximate a string form for.
  79. */
  80. static void audit_rule(struct audit_buffer *ab, const struct ipe_rule *r)
  81. {
  82. const struct ipe_prop *ptr;
  83. audit_log_format(ab, " rule=\"op=%s ", audit_op_names[r->op]);
  84. list_for_each_entry(ptr, &r->props, next) {
  85. switch (ptr->type) {
  86. case IPE_PROP_DMV_ROOTHASH:
  87. audit_dmv_roothash(ab, ptr->value);
  88. break;
  89. case IPE_PROP_FSV_DIGEST:
  90. audit_fsv_digest(ab, ptr->value);
  91. break;
  92. default:
  93. audit_log_format(ab, "%s", audit_prop_names[ptr->type]);
  94. break;
  95. }
  96. audit_log_format(ab, " ");
  97. }
  98. audit_log_format(ab, "action=%s\"", ACTSTR(r->action));
  99. }
  100. /**
  101. * ipe_audit_match() - Audit a rule match in a policy evaluation.
  102. * @ctx: Supplies a pointer to the evaluation context that was used in the
  103. * evaluation.
  104. * @match_type: Supplies the scope of the match: rule, operation default,
  105. * global default.
  106. * @act: Supplies the IPE's evaluation decision, deny or allow.
  107. * @r: Supplies a pointer to the rule that was matched, if possible.
  108. */
  109. void ipe_audit_match(const struct ipe_eval_ctx *const ctx,
  110. enum ipe_match match_type,
  111. enum ipe_action_type act, const struct ipe_rule *const r)
  112. {
  113. const char *op = audit_op_names[ctx->op];
  114. char comm[sizeof(current->comm)];
  115. struct audit_buffer *ab;
  116. struct inode *inode;
  117. if (act != IPE_ACTION_DENY && !READ_ONCE(success_audit))
  118. return;
  119. ab = audit_log_start(audit_context(), GFP_ATOMIC | __GFP_NOWARN,
  120. AUDIT_IPE_ACCESS);
  121. if (!ab)
  122. return;
  123. audit_log_format(ab, "ipe_op=%s ipe_hook=%s enforcing=%d pid=%d comm=",
  124. op, audit_hook_names[ctx->hook], READ_ONCE(enforce),
  125. task_tgid_nr(current));
  126. audit_log_untrustedstring(ab, get_task_comm(comm, current));
  127. if (ctx->file) {
  128. audit_log_d_path(ab, " path=", &ctx->file->f_path);
  129. inode = file_inode(ctx->file);
  130. if (inode) {
  131. audit_log_format(ab, " dev=");
  132. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  133. audit_log_format(ab, " ino=%lu", inode->i_ino);
  134. } else {
  135. audit_log_format(ab, " dev=? ino=?");
  136. }
  137. } else {
  138. audit_log_format(ab, " path=? dev=? ino=?");
  139. }
  140. if (match_type == IPE_MATCH_RULE)
  141. audit_rule(ab, r);
  142. else if (match_type == IPE_MATCH_TABLE)
  143. audit_log_format(ab, " rule=\"DEFAULT op=%s action=%s\"", op,
  144. ACTSTR(act));
  145. else
  146. audit_log_format(ab, " rule=\"DEFAULT action=%s\"",
  147. ACTSTR(act));
  148. audit_log_end(ab);
  149. }
  150. /**
  151. * audit_policy() - Audit a policy's name, version and thumbprint to @ab.
  152. * @ab: Supplies a pointer to the audit buffer to append to.
  153. * @audit_format: Supplies a pointer to the audit format string
  154. * @p: Supplies a pointer to the policy to audit.
  155. */
  156. static void audit_policy(struct audit_buffer *ab,
  157. const char *audit_format,
  158. const struct ipe_policy *const p)
  159. {
  160. SHASH_DESC_ON_STACK(desc, tfm);
  161. struct crypto_shash *tfm;
  162. u8 *digest = NULL;
  163. tfm = crypto_alloc_shash(IPE_AUDIT_HASH_ALG, 0, 0);
  164. if (IS_ERR(tfm))
  165. return;
  166. desc->tfm = tfm;
  167. digest = kzalloc(crypto_shash_digestsize(tfm), GFP_KERNEL);
  168. if (!digest)
  169. goto out;
  170. if (crypto_shash_init(desc))
  171. goto out;
  172. if (crypto_shash_update(desc, p->pkcs7, p->pkcs7len))
  173. goto out;
  174. if (crypto_shash_final(desc, digest))
  175. goto out;
  176. audit_log_format(ab, audit_format, p->parsed->name,
  177. p->parsed->version.major, p->parsed->version.minor,
  178. p->parsed->version.rev);
  179. audit_log_n_hex(ab, digest, crypto_shash_digestsize(tfm));
  180. out:
  181. kfree(digest);
  182. crypto_free_shash(tfm);
  183. }
  184. /**
  185. * ipe_audit_policy_activation() - Audit a policy being activated.
  186. * @op: Supplies a pointer to the previously activated policy to audit.
  187. * @np: Supplies a pointer to the newly activated policy to audit.
  188. */
  189. void ipe_audit_policy_activation(const struct ipe_policy *const op,
  190. const struct ipe_policy *const np)
  191. {
  192. struct audit_buffer *ab;
  193. ab = audit_log_start(audit_context(), GFP_KERNEL,
  194. AUDIT_IPE_CONFIG_CHANGE);
  195. if (!ab)
  196. return;
  197. if (op) {
  198. audit_policy(ab, AUDIT_OLD_ACTIVE_POLICY_FMT, op);
  199. audit_log_format(ab, " ");
  200. } else {
  201. /*
  202. * old active policy can be NULL if there is no kernel
  203. * built-in policy
  204. */
  205. audit_log_format(ab, AUDIT_OLD_ACTIVE_POLICY_NULL_FMT);
  206. audit_log_format(ab, " ");
  207. }
  208. audit_policy(ab, AUDIT_NEW_ACTIVE_POLICY_FMT, np);
  209. audit_log_format(ab, " auid=%u ses=%u lsm=ipe res=1",
  210. from_kuid(&init_user_ns, audit_get_loginuid(current)),
  211. audit_get_sessionid(current));
  212. audit_log_end(ab);
  213. }
  214. /**
  215. * ipe_audit_policy_load() - Audit a policy being loaded into the kernel.
  216. * @p: Supplies a pointer to the policy to audit.
  217. */
  218. void ipe_audit_policy_load(const struct ipe_policy *const p)
  219. {
  220. struct audit_buffer *ab;
  221. ab = audit_log_start(audit_context(), GFP_KERNEL,
  222. AUDIT_IPE_POLICY_LOAD);
  223. if (!ab)
  224. return;
  225. audit_policy(ab, AUDIT_POLICY_LOAD_FMT, p);
  226. audit_log_format(ab, " auid=%u ses=%u lsm=ipe res=1",
  227. from_kuid(&init_user_ns, audit_get_loginuid(current)),
  228. audit_get_sessionid(current));
  229. audit_log_end(ab);
  230. }
  231. /**
  232. * ipe_audit_enforce() - Audit a change in IPE's enforcement state.
  233. * @new_enforce: The new value enforce to be set.
  234. * @old_enforce: The old value currently in enforce.
  235. */
  236. void ipe_audit_enforce(bool new_enforce, bool old_enforce)
  237. {
  238. struct audit_buffer *ab;
  239. ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS);
  240. if (!ab)
  241. return;
  242. audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
  243. "enforcing=%d old_enforcing=%d auid=%u ses=%u"
  244. " enabled=1 old-enabled=1 lsm=ipe res=1",
  245. new_enforce, old_enforce,
  246. from_kuid(&init_user_ns, audit_get_loginuid(current)),
  247. audit_get_sessionid(current));
  248. audit_log_end(ab);
  249. }