1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020-2024 Microsoft Corporation. All rights reserved.
6 #include <linux/slab.h>
7 #include <linux/audit.h>
8 #include <linux/types.h>
9 #include <crypto/hash.h>
18 #define ACTSTR(x) ((x) == IPE_ACTION_ALLOW ? "ALLOW" : "DENY")
20 #define IPE_AUDIT_HASH_ALG "sha256"
22 #define AUDIT_POLICY_LOAD_FMT "policy_name=\"%s\" policy_version=%hu.%hu.%hu "\
23 "policy_digest=" IPE_AUDIT_HASH_ALG ":"
24 #define AUDIT_OLD_ACTIVE_POLICY_FMT "old_active_pol_name=\"%s\" "\
25 "old_active_pol_version=%hu.%hu.%hu "\
26 "old_policy_digest=" IPE_AUDIT_HASH_ALG ":"
27 #define AUDIT_OLD_ACTIVE_POLICY_NULL_FMT "old_active_pol_name=? "\
28 "old_active_pol_version=? "\
30 #define AUDIT_NEW_ACTIVE_POLICY_FMT "new_active_pol_name=\"%s\" "\
31 "new_active_pol_version=%hu.%hu.%hu "\
32 "new_policy_digest=" IPE_AUDIT_HASH_ALG ":"
34 static const char *const audit_op_names[__IPE_OP_MAX + 1] = {
45 static const char *const audit_hook_names[__IPE_HOOK_MAX] = {
53 static const char *const audit_prop_names[__IPE_PROP_MAX] = {
54 "boot_verified=FALSE",
57 "dmverity_signature=FALSE",
58 "dmverity_signature=TRUE",
60 "fsverity_signature=FALSE",
61 "fsverity_signature=TRUE",
65 * audit_dmv_roothash() - audit the roothash of a dmverity_roothash property.
66 * @ab: Supplies a pointer to the audit_buffer to append to.
67 * @rh: Supplies a pointer to the digest structure.
69 static void audit_dmv_roothash(struct audit_buffer *ab, const void *rh)
71 audit_log_format(ab, "%s", audit_prop_names[IPE_PROP_DMV_ROOTHASH]);
72 ipe_digest_audit(ab, rh);
76 * audit_fsv_digest() - audit the digest of a fsverity_digest property.
77 * @ab: Supplies a pointer to the audit_buffer to append to.
78 * @d: Supplies a pointer to the digest structure.
80 static void audit_fsv_digest(struct audit_buffer *ab, const void *d)
82 audit_log_format(ab, "%s", audit_prop_names[IPE_PROP_FSV_DIGEST]);
83 ipe_digest_audit(ab, d);
87 * audit_rule() - audit an IPE policy rule.
88 * @ab: Supplies a pointer to the audit_buffer to append to.
89 * @r: Supplies a pointer to the ipe_rule to approximate a string form for.
91 static void audit_rule(struct audit_buffer *ab, const struct ipe_rule *r)
93 const struct ipe_prop *ptr;
95 audit_log_format(ab, " rule=\"op=%s ", audit_op_names[r->op]);
97 list_for_each_entry(ptr, &r->props, next) {
99 case IPE_PROP_DMV_ROOTHASH:
100 audit_dmv_roothash(ab, ptr->value);
102 case IPE_PROP_FSV_DIGEST:
103 audit_fsv_digest(ab, ptr->value);
106 audit_log_format(ab, "%s", audit_prop_names[ptr->type]);
110 audit_log_format(ab, " ");
113 audit_log_format(ab, "action=%s\"", ACTSTR(r->action));
117 * ipe_audit_match() - Audit a rule match in a policy evaluation.
118 * @ctx: Supplies a pointer to the evaluation context that was used in the
120 * @match_type: Supplies the scope of the match: rule, operation default,
122 * @act: Supplies the IPE's evaluation decision, deny or allow.
123 * @r: Supplies a pointer to the rule that was matched, if possible.
125 void ipe_audit_match(const struct ipe_eval_ctx *const ctx,
126 enum ipe_match match_type,
127 enum ipe_action_type act, const struct ipe_rule *const r)
129 const char *op = audit_op_names[ctx->op];
130 char comm[sizeof(current->comm)];
131 struct audit_buffer *ab;
134 if (act != IPE_ACTION_DENY && !READ_ONCE(success_audit))
137 ab = audit_log_start(audit_context(), GFP_ATOMIC | __GFP_NOWARN,
142 audit_log_format(ab, "ipe_op=%s ipe_hook=%s enforcing=%d pid=%d comm=",
143 op, audit_hook_names[ctx->hook], READ_ONCE(enforce),
144 task_tgid_nr(current));
145 audit_log_untrustedstring(ab, get_task_comm(comm, current));
148 audit_log_d_path(ab, " path=", &ctx->file->f_path);
149 inode = file_inode(ctx->file);
151 audit_log_format(ab, " dev=");
152 audit_log_untrustedstring(ab, inode->i_sb->s_id);
153 audit_log_format(ab, " ino=%lu", inode->i_ino);
155 audit_log_format(ab, " dev=? ino=?");
158 audit_log_format(ab, " path=? dev=? ino=?");
161 if (match_type == IPE_MATCH_RULE)
163 else if (match_type == IPE_MATCH_TABLE)
164 audit_log_format(ab, " rule=\"DEFAULT op=%s action=%s\"", op,
167 audit_log_format(ab, " rule=\"DEFAULT action=%s\"",
174 * audit_policy() - Audit a policy's name, version and thumbprint to @ab.
175 * @ab: Supplies a pointer to the audit buffer to append to.
176 * @audit_format: Supplies a pointer to the audit format string
177 * @p: Supplies a pointer to the policy to audit.
179 static void audit_policy(struct audit_buffer *ab,
180 const char *audit_format,
181 const struct ipe_policy *const p)
183 SHASH_DESC_ON_STACK(desc, tfm);
184 struct crypto_shash *tfm;
187 tfm = crypto_alloc_shash(IPE_AUDIT_HASH_ALG, 0, 0);
193 digest = kzalloc(crypto_shash_digestsize(tfm), GFP_KERNEL);
197 if (crypto_shash_init(desc))
200 if (crypto_shash_update(desc, p->pkcs7, p->pkcs7len))
203 if (crypto_shash_final(desc, digest))
206 audit_log_format(ab, audit_format, p->parsed->name,
207 p->parsed->version.major, p->parsed->version.minor,
208 p->parsed->version.rev);
209 audit_log_n_hex(ab, digest, crypto_shash_digestsize(tfm));
213 crypto_free_shash(tfm);
217 * ipe_audit_policy_activation() - Audit a policy being activated.
218 * @op: Supplies a pointer to the previously activated policy to audit.
219 * @np: Supplies a pointer to the newly activated policy to audit.
221 void ipe_audit_policy_activation(const struct ipe_policy *const op,
222 const struct ipe_policy *const np)
224 struct audit_buffer *ab;
226 ab = audit_log_start(audit_context(), GFP_KERNEL,
227 AUDIT_IPE_CONFIG_CHANGE);
232 audit_policy(ab, AUDIT_OLD_ACTIVE_POLICY_FMT, op);
233 audit_log_format(ab, " ");
236 * old active policy can be NULL if there is no kernel
239 audit_log_format(ab, AUDIT_OLD_ACTIVE_POLICY_NULL_FMT);
240 audit_log_format(ab, " ");
242 audit_policy(ab, AUDIT_NEW_ACTIVE_POLICY_FMT, np);
243 audit_log_format(ab, " auid=%u ses=%u lsm=ipe res=1",
244 from_kuid(&init_user_ns, audit_get_loginuid(current)),
245 audit_get_sessionid(current));
251 * ipe_audit_policy_load() - Audit a policy being loaded into the kernel.
252 * @p: Supplies a pointer to the policy to audit.
254 void ipe_audit_policy_load(const struct ipe_policy *const p)
256 struct audit_buffer *ab;
258 ab = audit_log_start(audit_context(), GFP_KERNEL,
259 AUDIT_IPE_POLICY_LOAD);
263 audit_policy(ab, AUDIT_POLICY_LOAD_FMT, p);
264 audit_log_format(ab, " auid=%u ses=%u lsm=ipe res=1",
265 from_kuid(&init_user_ns, audit_get_loginuid(current)),
266 audit_get_sessionid(current));
272 * ipe_audit_enforce() - Audit a change in IPE's enforcement state.
273 * @new_enforce: The new value enforce to be set.
274 * @old_enforce: The old value currently in enforce.
276 void ipe_audit_enforce(bool new_enforce, bool old_enforce)
278 struct audit_buffer *ab;
280 ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS);
284 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
285 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
286 " enabled=1 old-enabled=1 lsm=ipe res=1",
287 new_enforce, old_enforce,
288 from_kuid(&init_user_ns, audit_get_loginuid(current)),
289 audit_get_sessionid(current));