xref: /linux/security/apparmor/audit.c (revision 97dd3f51bf0cb111a2c1cdef7ecca48e31223cb7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AppArmor security module
4  *
5  * This file contains AppArmor auditing functions
6  *
7  * Copyright (C) 1998-2008 Novell/SUSE
8  * Copyright 2009-2010 Canonical Ltd.
9  */
10 
11 #include <linux/audit.h>
12 #include <linux/socket.h>
13 
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"
19 
20 const char *const audit_mode_names[] = {
21 	"normal",
22 	"quiet_denied",
23 	"quiet.allowed",
24 	"quiet",
25 	"noquiet",
26 	"all"
27 };
28 
29 static const char *const aa_audit_type[] = {
30 	"AUDIT",
31 	"ALLOWED",
32 	"DENIED",
33 	"HINT",
34 	"STATUS",
35 	"ERROR",
36 	"KILLED",
37 	"AUTO"
38 };
39 
40 static const char *const aa_class_names[] = {
41 	"none",
42 	"unknown",
43 	"file",
44 	"cap",
45 	"net",
46 	"rlimits",
47 	"domain",
48 	"mount",
49 	"unknown",
50 	"ptrace",
51 	"signal",
52 	"xmatch",
53 	"unknown",
54 	"unknown",
55 	"net",
56 	"netv9",
57 	"label",
58 	"posix_mqueue",
59 	"io_uring",
60 	"module",
61 	"lsm",
62 	"namespace",
63 	"io_uring",
64 	"unknown",
65 	"unknown",
66 	"unknown",
67 	"unknown",
68 	"unknown",
69 	"unknown",
70 	"unknown",
71 	"netv9_packet",
72 	"X",
73 	"dbus",
74 };
75 
76 
77 /*
78  * Currently AppArmor auditing is fed straight into the audit framework.
79  *
80  * TODO:
81  * netlink interface for complain mode
82  * user auditing, - send user auditing to netlink interface
83  * system control of whether user audit messages go to system log
84  */
85 
86 /**
87  * audit_pre() - core AppArmor function.
88  * @ab: audit buffer to fill (NOT NULL)
89  * @va: audit structure containing data to audit (NOT NULL)
90  *
91  * Record common AppArmor audit data from @va
92  */
93 static void audit_pre(struct audit_buffer *ab, void *va)
94 {
95 	struct apparmor_audit_data *ad = aad_of_va(va);
96 
97 	if (aa_g_audit_header) {
98 		audit_log_format(ab, "apparmor=\"%s\"",
99 				 aa_audit_type[ad->type]);
100 	}
101 
102 	if (ad->op)
103 		audit_log_format(ab, " operation=\"%s\"", ad->op);
104 
105 	if (ad->class)
106 		audit_log_format(ab, " class=\"%s\"",
107 				 ad->class <= AA_CLASS_LAST ?
108 				 aa_class_names[ad->class] :
109 				 "unknown");
110 
111 	if (ad->info) {
112 		audit_log_format(ab, " info=\"%s\"", ad->info);
113 		if (ad->error)
114 			audit_log_format(ab, " error=%d", ad->error);
115 	}
116 
117 	if (ad->subj_label) {
118 		struct aa_label *label = ad->subj_label;
119 
120 		if (label_isprofile(label)) {
121 			struct aa_profile *profile = labels_profile(label);
122 
123 			if (profile->ns != root_ns) {
124 				audit_log_format(ab, " namespace=");
125 				audit_log_untrustedstring(ab,
126 						       profile->ns->base.hname);
127 			}
128 			audit_log_format(ab, " profile=");
129 			audit_log_untrustedstring(ab, profile->base.hname);
130 		} else {
131 			audit_log_format(ab, " label=");
132 			aa_label_xaudit(ab, root_ns, label, FLAG_VIEW_SUBNS,
133 					GFP_ATOMIC);
134 		}
135 	}
136 
137 	if (ad->name) {
138 		audit_log_format(ab, " name=");
139 		audit_log_untrustedstring(ab, ad->name);
140 	}
141 }
142 
143 /**
144  * aa_audit_msg - Log a message to the audit subsystem
145  * @type: audit type for the message
146  * @ad: audit event structure (NOT NULL)
147  * @cb: optional callback fn for type specific fields (MAYBE NULL)
148  */
149 void aa_audit_msg(int type, struct apparmor_audit_data *ad,
150 		  void (*cb) (struct audit_buffer *, void *))
151 {
152 	ad->type = type;
153 	common_lsm_audit(&ad->common, audit_pre, cb);
154 }
155 
156 /**
157  * aa_audit - Log a profile based audit event to the audit subsystem
158  * @type: audit type for the message
159  * @profile: profile to check against (NOT NULL)
160  * @ad: audit event (NOT NULL)
161  * @cb: optional callback fn for type specific fields (MAYBE NULL)
162  *
163  * Handle default message switching based off of audit mode flags
164  *
165  * Returns: error on failure
166  */
167 int aa_audit(int type, struct aa_profile *profile,
168 	     struct apparmor_audit_data *ad,
169 	     void (*cb) (struct audit_buffer *, void *))
170 {
171 	AA_BUG(!profile);
172 
173 	if (type == AUDIT_APPARMOR_AUTO) {
174 		if (likely(!ad->error)) {
175 			if (AUDIT_MODE(profile) != AUDIT_ALL)
176 				return 0;
177 			type = AUDIT_APPARMOR_AUDIT;
178 		} else if (COMPLAIN_MODE(profile))
179 			type = AUDIT_APPARMOR_ALLOWED;
180 		else
181 			type = AUDIT_APPARMOR_DENIED;
182 	}
183 	if (AUDIT_MODE(profile) == AUDIT_QUIET ||
184 	    (type == AUDIT_APPARMOR_DENIED &&
185 	     AUDIT_MODE(profile) == AUDIT_QUIET_DENIED))
186 		return ad->error;
187 
188 	if (KILL_MODE(profile) && type == AUDIT_APPARMOR_DENIED)
189 		type = AUDIT_APPARMOR_KILL;
190 
191 	ad->subj_label = &profile->label;
192 
193 	aa_audit_msg(type, ad, cb);
194 
195 	if (ad->type == AUDIT_APPARMOR_KILL)
196 		send_sig_info(profile->signal, SEND_SIG_NOINFO,
197 			ad->common.type == LSM_AUDIT_DATA_TASK &&
198 			ad->common.u.tsk ? ad->common.u.tsk : current);
199 
200 	if (ad->type == AUDIT_APPARMOR_ALLOWED)
201 		return complain_error(ad->error);
202 
203 	return ad->error;
204 }
205 
206 struct aa_audit_rule {
207 	struct aa_label *label;
208 };
209 
210 void aa_audit_rule_free(void *vrule)
211 {
212 	struct aa_audit_rule *rule = vrule;
213 
214 	if (rule) {
215 		if (!IS_ERR(rule->label))
216 			aa_put_label(rule->label);
217 		kfree(rule);
218 	}
219 }
220 
221 int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule, gfp_t gfp)
222 {
223 	struct aa_audit_rule *rule;
224 
225 	switch (field) {
226 	case AUDIT_SUBJ_ROLE:
227 		if (op != Audit_equal && op != Audit_not_equal)
228 			return -EINVAL;
229 		break;
230 	default:
231 		return -EINVAL;
232 	}
233 
234 	rule = kzalloc_obj(struct aa_audit_rule, gfp);
235 
236 	if (!rule)
237 		return -ENOMEM;
238 
239 	/* Currently rules are treated as coming from the root ns */
240 	rule->label = aa_label_parse(&root_ns->unconfined->label, rulestr,
241 				     gfp, true, false);
242 	if (IS_ERR(rule->label)) {
243 		int err = PTR_ERR(rule->label);
244 		aa_audit_rule_free(rule);
245 		return err;
246 	}
247 
248 	*vrule = rule;
249 	return 0;
250 }
251 
252 int aa_audit_rule_known(struct audit_krule *rule)
253 {
254 	int i;
255 
256 	for (i = 0; i < rule->field_count; i++) {
257 		struct audit_field *f = &rule->fields[i];
258 
259 		switch (f->type) {
260 		case AUDIT_SUBJ_ROLE:
261 			return 1;
262 		}
263 	}
264 
265 	return 0;
266 }
267 
268 int aa_audit_rule_match(struct lsm_prop *prop, u32 field, u32 op, void *vrule)
269 {
270 	struct aa_audit_rule *rule = vrule;
271 	struct aa_label *label;
272 	int found = 0;
273 
274 	label = prop->apparmor.label;
275 
276 	if (!label)
277 		return -ENOENT;
278 
279 	if (aa_label_is_subset(label, rule->label))
280 		found = 1;
281 
282 	switch (field) {
283 	case AUDIT_SUBJ_ROLE:
284 		switch (op) {
285 		case Audit_equal:
286 			return found;
287 		case Audit_not_equal:
288 			return !found;
289 		}
290 	}
291 	return 0;
292 }
293