1 // SPDX-License-Identifier: GPL-2.0-only
3 * AppArmor security module
5 * This file contains AppArmor auditing functions
7 * Copyright (C) 1998-2008 Novell/SUSE
8 * Copyright 2009-2010 Canonical Ltd.
11 #include <linux/audit.h>
12 #include <linux/socket.h>
14 #include "include/apparmor.h"
15 #include "include/audit.h"
16 #include "include/policy.h"
17 #include "include/policy_ns.h"
18 #include "include/secid.h"
20 const char *const audit_mode_names[] = {
28 static const char *const aa_audit_type[] = {
39 static const char *const aa_class_names[] = {
77 * Currently AppArmor auditing is fed straight into the audit framework.
80 * netlink interface for complain mode
81 * user auditing, - send user auditing to netlink interface
82 * system control of whether user audit messages go to system log
86 * audit_pre() - core AppArmor function.
87 * @ab: audit buffer to fill (NOT NULL)
88 * @ca: audit structure containing data to audit (NOT NULL)
90 * Record common AppArmor audit data from @sa
92 static void audit_pre(struct audit_buffer *ab, void *ca)
94 struct common_audit_data *sa = ca;
96 if (aa_g_audit_header) {
97 audit_log_format(ab, "apparmor=\"%s\"",
98 aa_audit_type[aad(sa)->type]);
102 audit_log_format(ab, " operation=\"%s\"", aad(sa)->op);
106 audit_log_format(ab, " class=\"%s\"",
107 aad(sa)->class <= AA_CLASS_LAST ?
108 aa_class_names[aad(sa)->class] :
112 audit_log_format(ab, " info=\"%s\"", aad(sa)->info);
114 audit_log_format(ab, " error=%d", aad(sa)->error);
117 if (aad(sa)->label) {
118 struct aa_label *label = aad(sa)->label;
120 if (label_isprofile(label)) {
121 struct aa_profile *profile = labels_profile(label);
123 if (profile->ns != root_ns) {
124 audit_log_format(ab, " namespace=");
125 audit_log_untrustedstring(ab,
126 profile->ns->base.hname);
128 audit_log_format(ab, " profile=");
129 audit_log_untrustedstring(ab, profile->base.hname);
131 audit_log_format(ab, " label=");
132 aa_label_xaudit(ab, root_ns, label, FLAG_VIEW_SUBNS,
138 audit_log_format(ab, " name=");
139 audit_log_untrustedstring(ab, aad(sa)->name);
144 * aa_audit_msg - Log a message to the audit subsystem
145 * @sa: audit event structure (NOT NULL)
146 * @cb: optional callback fn for type specific fields (MAYBE NULL)
148 void aa_audit_msg(int type, struct common_audit_data *sa,
149 void (*cb) (struct audit_buffer *, void *))
151 aad(sa)->type = type;
152 common_lsm_audit(sa, audit_pre, cb);
156 * aa_audit - Log a profile based audit event to the audit subsystem
157 * @type: audit type for the message
158 * @profile: profile to check against (NOT NULL)
159 * @sa: audit event (NOT NULL)
160 * @cb: optional callback fn for type specific fields (MAYBE NULL)
162 * Handle default message switching based off of audit mode flags
164 * Returns: error on failure
166 int aa_audit(int type, struct aa_profile *profile, struct common_audit_data *sa,
167 void (*cb) (struct audit_buffer *, void *))
171 if (type == AUDIT_APPARMOR_AUTO) {
172 if (likely(!aad(sa)->error)) {
173 if (AUDIT_MODE(profile) != AUDIT_ALL)
175 type = AUDIT_APPARMOR_AUDIT;
176 } else if (COMPLAIN_MODE(profile))
177 type = AUDIT_APPARMOR_ALLOWED;
179 type = AUDIT_APPARMOR_DENIED;
181 if (AUDIT_MODE(profile) == AUDIT_QUIET ||
182 (type == AUDIT_APPARMOR_DENIED &&
183 AUDIT_MODE(profile) == AUDIT_QUIET_DENIED))
184 return aad(sa)->error;
186 if (KILL_MODE(profile) && type == AUDIT_APPARMOR_DENIED)
187 type = AUDIT_APPARMOR_KILL;
189 aad(sa)->label = &profile->label;
191 aa_audit_msg(type, sa, cb);
193 if (aad(sa)->type == AUDIT_APPARMOR_KILL)
194 (void)send_sig_info(SIGKILL, NULL,
195 sa->type == LSM_AUDIT_DATA_TASK && sa->u.tsk ?
196 sa->u.tsk : current);
198 if (aad(sa)->type == AUDIT_APPARMOR_ALLOWED)
199 return complain_error(aad(sa)->error);
201 return aad(sa)->error;
204 struct aa_audit_rule {
205 struct aa_label *label;
208 void aa_audit_rule_free(void *vrule)
210 struct aa_audit_rule *rule = vrule;
213 if (!IS_ERR(rule->label))
214 aa_put_label(rule->label);
219 int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
221 struct aa_audit_rule *rule;
224 case AUDIT_SUBJ_ROLE:
225 if (op != Audit_equal && op != Audit_not_equal)
232 rule = kzalloc(sizeof(struct aa_audit_rule), GFP_KERNEL);
237 /* Currently rules are treated as coming from the root ns */
238 rule->label = aa_label_parse(&root_ns->unconfined->label, rulestr,
239 GFP_KERNEL, true, false);
240 if (IS_ERR(rule->label)) {
241 int err = PTR_ERR(rule->label);
242 aa_audit_rule_free(rule);
250 int aa_audit_rule_known(struct audit_krule *rule)
254 for (i = 0; i < rule->field_count; i++) {
255 struct audit_field *f = &rule->fields[i];
258 case AUDIT_SUBJ_ROLE:
266 int aa_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule)
268 struct aa_audit_rule *rule = vrule;
269 struct aa_label *label;
272 label = aa_secid_to_label(sid);
277 if (aa_label_is_subset(label, rule->label))
281 case AUDIT_SUBJ_ROLE:
285 case Audit_not_equal: