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 * @va: audit structure containing data to audit (NOT NULL)
90 * Record common AppArmor audit data from @va
92 static void audit_pre(struct audit_buffer *ab, void *va)
94 struct apparmor_audit_data *ad = aad_of_va(va);
96 if (aa_g_audit_header) {
97 audit_log_format(ab, "apparmor=\"%s\"",
98 aa_audit_type[ad->type]);
102 audit_log_format(ab, " operation=\"%s\"", ad->op);
105 audit_log_format(ab, " class=\"%s\"",
106 ad->class <= AA_CLASS_LAST ?
107 aa_class_names[ad->class] :
111 audit_log_format(ab, " info=\"%s\"", ad->info);
113 audit_log_format(ab, " error=%d", ad->error);
116 if (ad->subj_label) {
117 struct aa_label *label = ad->subj_label;
119 if (label_isprofile(label)) {
120 struct aa_profile *profile = labels_profile(label);
122 if (profile->ns != root_ns) {
123 audit_log_format(ab, " namespace=");
124 audit_log_untrustedstring(ab,
125 profile->ns->base.hname);
127 audit_log_format(ab, " profile=");
128 audit_log_untrustedstring(ab, profile->base.hname);
130 audit_log_format(ab, " label=");
131 aa_label_xaudit(ab, root_ns, label, FLAG_VIEW_SUBNS,
137 audit_log_format(ab, " name=");
138 audit_log_untrustedstring(ab, ad->name);
143 * aa_audit_msg - Log a message to the audit subsystem
144 * @type: audit type for the message
145 * @ad: audit event structure (NOT NULL)
146 * @cb: optional callback fn for type specific fields (MAYBE NULL)
148 void aa_audit_msg(int type, struct apparmor_audit_data *ad,
149 void (*cb) (struct audit_buffer *, void *))
152 common_lsm_audit(&ad->common, 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 * @ad: 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,
167 struct apparmor_audit_data *ad,
168 void (*cb) (struct audit_buffer *, void *))
172 if (type == AUDIT_APPARMOR_AUTO) {
173 if (likely(!ad->error)) {
174 if (AUDIT_MODE(profile) != AUDIT_ALL)
176 type = AUDIT_APPARMOR_AUDIT;
177 } else if (COMPLAIN_MODE(profile))
178 type = AUDIT_APPARMOR_ALLOWED;
180 type = AUDIT_APPARMOR_DENIED;
182 if (AUDIT_MODE(profile) == AUDIT_QUIET ||
183 (type == AUDIT_APPARMOR_DENIED &&
184 AUDIT_MODE(profile) == AUDIT_QUIET_DENIED))
187 if (KILL_MODE(profile) && type == AUDIT_APPARMOR_DENIED)
188 type = AUDIT_APPARMOR_KILL;
190 ad->subj_label = &profile->label;
192 aa_audit_msg(type, ad, cb);
194 if (ad->type == AUDIT_APPARMOR_KILL)
195 (void)send_sig_info(SIGKILL, NULL,
196 ad->common.type == LSM_AUDIT_DATA_TASK &&
197 ad->common.u.tsk ? ad->common.u.tsk : current);
199 if (ad->type == AUDIT_APPARMOR_ALLOWED)
200 return complain_error(ad->error);
205 struct aa_audit_rule {
206 struct aa_label *label;
209 void aa_audit_rule_free(void *vrule)
211 struct aa_audit_rule *rule = vrule;
214 if (!IS_ERR(rule->label))
215 aa_put_label(rule->label);
220 int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule, gfp_t gfp)
222 struct aa_audit_rule *rule;
225 case AUDIT_SUBJ_ROLE:
226 if (op != Audit_equal && op != Audit_not_equal)
233 rule = kzalloc(sizeof(struct aa_audit_rule), gfp);
238 /* Currently rules are treated as coming from the root ns */
239 rule->label = aa_label_parse(&root_ns->unconfined->label, rulestr,
241 if (IS_ERR(rule->label)) {
242 int err = PTR_ERR(rule->label);
243 aa_audit_rule_free(rule);
251 int aa_audit_rule_known(struct audit_krule *rule)
255 for (i = 0; i < rule->field_count; i++) {
256 struct audit_field *f = &rule->fields[i];
259 case AUDIT_SUBJ_ROLE:
267 int aa_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule)
269 struct aa_audit_rule *rule = vrule;
270 struct aa_label *label;
273 label = aa_secid_to_label(sid);
278 if (aa_label_is_subset(label, rule->label))
282 case AUDIT_SUBJ_ROLE:
286 case Audit_not_equal: