]>
Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
fe7752ba DW |
2 | /* auditfilter.c -- filtering of audit events |
3 | * | |
4 | * Copyright 2003-2004 Red Hat, Inc. | |
5 | * Copyright 2005 Hewlett-Packard Development Company, L.P. | |
6 | * Copyright 2005 IBM Corporation | |
fe7752ba DW |
7 | */ |
8 | ||
f952d10f RGB |
9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
10 | ||
fe7752ba DW |
11 | #include <linux/kernel.h> |
12 | #include <linux/audit.h> | |
13 | #include <linux/kthread.h> | |
f368c07d AG |
14 | #include <linux/mutex.h> |
15 | #include <linux/fs.h> | |
16 | #include <linux/namei.h> | |
fe7752ba | 17 | #include <linux/netlink.h> |
f368c07d | 18 | #include <linux/sched.h> |
5a0e3ad6 | 19 | #include <linux/slab.h> |
2a862b32 | 20 | #include <linux/security.h> |
48095d99 | 21 | #include <net/net_namespace.h> |
6f285b19 | 22 | #include <net/sock.h> |
fe7752ba DW |
23 | #include "audit.h" |
24 | ||
f368c07d AG |
25 | /* |
26 | * Locking model: | |
27 | * | |
28 | * audit_filter_mutex: | |
725131ef SM |
29 | * Synchronizes writes and blocking reads of audit's filterlist |
30 | * data. Rcu is used to traverse the filterlist and access | |
31 | * contents of structs audit_entry, audit_watch and opaque | |
32 | * LSM rules during filtering. If modified, these structures | |
33 | * must be copied and replace their counterparts in the filterlist. | |
34 | * An audit_parent struct is not accessed during filtering, so may | |
35 | * be written directly provided audit_filter_mutex is held. | |
f368c07d AG |
36 | */ |
37 | ||
f368c07d | 38 | /* Audit filter lists, defined in <linux/audit.h> */ |
fe7752ba DW |
39 | struct list_head audit_filter_list[AUDIT_NR_FILTERS] = { |
40 | LIST_HEAD_INIT(audit_filter_list[0]), | |
41 | LIST_HEAD_INIT(audit_filter_list[1]), | |
42 | LIST_HEAD_INIT(audit_filter_list[2]), | |
43 | LIST_HEAD_INIT(audit_filter_list[3]), | |
44 | LIST_HEAD_INIT(audit_filter_list[4]), | |
45 | LIST_HEAD_INIT(audit_filter_list[5]), | |
42d5e376 RGB |
46 | LIST_HEAD_INIT(audit_filter_list[6]), |
47 | #if AUDIT_NR_FILTERS != 7 | |
fe7752ba DW |
48 | #error Fix audit_filter_list initialiser |
49 | #endif | |
50 | }; | |
e45aa212 AV |
51 | static struct list_head audit_rules_list[AUDIT_NR_FILTERS] = { |
52 | LIST_HEAD_INIT(audit_rules_list[0]), | |
53 | LIST_HEAD_INIT(audit_rules_list[1]), | |
54 | LIST_HEAD_INIT(audit_rules_list[2]), | |
55 | LIST_HEAD_INIT(audit_rules_list[3]), | |
56 | LIST_HEAD_INIT(audit_rules_list[4]), | |
57 | LIST_HEAD_INIT(audit_rules_list[5]), | |
42d5e376 | 58 | LIST_HEAD_INIT(audit_rules_list[6]), |
e45aa212 | 59 | }; |
fe7752ba | 60 | |
74c3cbe3 | 61 | DEFINE_MUTEX(audit_filter_mutex); |
f368c07d | 62 | |
219ca394 RGB |
63 | static void audit_free_lsm_field(struct audit_field *f) |
64 | { | |
65 | switch (f->type) { | |
66 | case AUDIT_SUBJ_USER: | |
67 | case AUDIT_SUBJ_ROLE: | |
68 | case AUDIT_SUBJ_TYPE: | |
69 | case AUDIT_SUBJ_SEN: | |
70 | case AUDIT_SUBJ_CLR: | |
71 | case AUDIT_OBJ_USER: | |
72 | case AUDIT_OBJ_ROLE: | |
73 | case AUDIT_OBJ_TYPE: | |
74 | case AUDIT_OBJ_LEV_LOW: | |
75 | case AUDIT_OBJ_LEV_HIGH: | |
76 | kfree(f->lsm_str); | |
f17b27a2 | 77 | security_audit_rule_free(f->lsm_rules); |
219ca394 RGB |
78 | } |
79 | } | |
80 | ||
93315ed6 | 81 | static inline void audit_free_rule(struct audit_entry *e) |
fe7752ba | 82 | { |
3dc7e315 | 83 | int i; |
c28bb7da | 84 | struct audit_krule *erule = &e->rule; |
ae7b8f41 | 85 | |
f368c07d | 86 | /* some rules don't have associated watches */ |
c28bb7da ZX |
87 | if (erule->watch) |
88 | audit_put_watch(erule->watch); | |
89 | if (erule->fields) | |
219ca394 RGB |
90 | for (i = 0; i < erule->field_count; i++) |
91 | audit_free_lsm_field(&erule->fields[i]); | |
c28bb7da ZX |
92 | kfree(erule->fields); |
93 | kfree(erule->filterkey); | |
93315ed6 AG |
94 | kfree(e); |
95 | } | |
96 | ||
74c3cbe3 | 97 | void audit_free_rule_rcu(struct rcu_head *head) |
93315ed6 AG |
98 | { |
99 | struct audit_entry *e = container_of(head, struct audit_entry, rcu); | |
100 | audit_free_rule(e); | |
101 | } | |
102 | ||
3dc7e315 DG |
103 | /* Initialize an audit filterlist entry. */ |
104 | static inline struct audit_entry *audit_init_entry(u32 field_count) | |
105 | { | |
106 | struct audit_entry *entry; | |
107 | struct audit_field *fields; | |
108 | ||
109 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); | |
110 | if (unlikely(!entry)) | |
111 | return NULL; | |
112 | ||
bab5e2d6 | 113 | fields = kcalloc(field_count, sizeof(*fields), GFP_KERNEL); |
3dc7e315 DG |
114 | if (unlikely(!fields)) { |
115 | kfree(entry); | |
116 | return NULL; | |
117 | } | |
118 | entry->rule.fields = fields; | |
119 | ||
120 | return entry; | |
121 | } | |
122 | ||
93315ed6 AG |
123 | /* Unpack a filter field's string representation from user-space |
124 | * buffer. */ | |
74c3cbe3 | 125 | char *audit_unpack_string(void **bufp, size_t *remain, size_t len) |
93315ed6 AG |
126 | { |
127 | char *str; | |
128 | ||
129 | if (!*bufp || (len == 0) || (len > *remain)) | |
130 | return ERR_PTR(-EINVAL); | |
131 | ||
132 | /* Of the currently implemented string fields, PATH_MAX | |
133 | * defines the longest valid length. | |
134 | */ | |
135 | if (len > PATH_MAX) | |
136 | return ERR_PTR(-ENAMETOOLONG); | |
137 | ||
138 | str = kmalloc(len + 1, GFP_KERNEL); | |
139 | if (unlikely(!str)) | |
140 | return ERR_PTR(-ENOMEM); | |
141 | ||
142 | memcpy(str, *bufp, len); | |
143 | str[len] = 0; | |
144 | *bufp += len; | |
145 | *remain -= len; | |
146 | ||
147 | return str; | |
148 | } | |
149 | ||
fd97646b | 150 | /* Translate an inode field to kernel representation. */ |
f368c07d AG |
151 | static inline int audit_to_inode(struct audit_krule *krule, |
152 | struct audit_field *f) | |
153 | { | |
154 | if (krule->listnr != AUDIT_FILTER_EXIT || | |
3639f170 | 155 | krule->inode_f || krule->watch || krule->tree || |
5af75d8d | 156 | (f->op != Audit_equal && f->op != Audit_not_equal)) |
f368c07d AG |
157 | return -EINVAL; |
158 | ||
159 | krule->inode_f = f; | |
160 | return 0; | |
161 | } | |
162 | ||
b915543b AV |
163 | static __u32 *classes[AUDIT_SYSCALL_CLASSES]; |
164 | ||
165 | int __init audit_register_class(int class, unsigned *list) | |
166 | { | |
bab5e2d6 | 167 | __u32 *p = kcalloc(AUDIT_BITMASK_SIZE, sizeof(__u32), GFP_KERNEL); |
b915543b AV |
168 | if (!p) |
169 | return -ENOMEM; | |
170 | while (*list != ~0U) { | |
171 | unsigned n = *list++; | |
172 | if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) { | |
173 | kfree(p); | |
174 | return -EINVAL; | |
175 | } | |
176 | p[AUDIT_WORD(n)] |= AUDIT_BIT(n); | |
177 | } | |
178 | if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) { | |
179 | kfree(p); | |
180 | return -EINVAL; | |
181 | } | |
182 | classes[class] = p; | |
183 | return 0; | |
184 | } | |
185 | ||
55669bfa AV |
186 | int audit_match_class(int class, unsigned syscall) |
187 | { | |
c926e4f4 | 188 | if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32)) |
55669bfa AV |
189 | return 0; |
190 | if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class])) | |
191 | return 0; | |
192 | return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall); | |
193 | } | |
194 | ||
327b9eeb | 195 | #ifdef CONFIG_AUDITSYSCALL |
e54dc243 AG |
196 | static inline int audit_match_class_bits(int class, u32 *mask) |
197 | { | |
198 | int i; | |
199 | ||
200 | if (classes[class]) { | |
201 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) | |
202 | if (mask[i] & classes[class][i]) | |
203 | return 0; | |
204 | } | |
205 | return 1; | |
206 | } | |
207 | ||
208 | static int audit_match_signal(struct audit_entry *entry) | |
209 | { | |
210 | struct audit_field *arch = entry->rule.arch_f; | |
211 | ||
212 | if (!arch) { | |
213 | /* When arch is unspecified, we must check both masks on biarch | |
214 | * as syscall number alone is ambiguous. */ | |
215 | return (audit_match_class_bits(AUDIT_CLASS_SIGNAL, | |
216 | entry->rule.mask) && | |
217 | audit_match_class_bits(AUDIT_CLASS_SIGNAL_32, | |
218 | entry->rule.mask)); | |
219 | } | |
220 | ||
221 | switch(audit_classify_arch(arch->val)) { | |
222 | case 0: /* native */ | |
223 | return (audit_match_class_bits(AUDIT_CLASS_SIGNAL, | |
224 | entry->rule.mask)); | |
225 | case 1: /* 32bit on biarch */ | |
226 | return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32, | |
227 | entry->rule.mask)); | |
228 | default: | |
229 | return 1; | |
230 | } | |
231 | } | |
327b9eeb | 232 | #endif |
e54dc243 | 233 | |
93315ed6 | 234 | /* Common user-space to kernel rule translation. */ |
56c4911a | 235 | static inline struct audit_entry *audit_to_entry_common(struct audit_rule_data *rule) |
93315ed6 AG |
236 | { |
237 | unsigned listnr; | |
238 | struct audit_entry *entry; | |
93315ed6 AG |
239 | int i, err; |
240 | ||
241 | err = -EINVAL; | |
242 | listnr = rule->flags & ~AUDIT_FILTER_PREPEND; | |
243 | switch(listnr) { | |
244 | default: | |
245 | goto exit_err; | |
93315ed6 AG |
246 | #ifdef CONFIG_AUDITSYSCALL |
247 | case AUDIT_FILTER_ENTRY: | |
5260ecc2 RGB |
248 | pr_err("AUDIT_FILTER_ENTRY is deprecated\n"); |
249 | goto exit_err; | |
93315ed6 AG |
250 | case AUDIT_FILTER_EXIT: |
251 | case AUDIT_FILTER_TASK: | |
252 | #endif | |
7ff68e53 | 253 | case AUDIT_FILTER_USER: |
d904ac03 | 254 | case AUDIT_FILTER_EXCLUDE: |
42d5e376 | 255 | case AUDIT_FILTER_FS: |
93315ed6 AG |
256 | ; |
257 | } | |
014149cc | 258 | if (unlikely(rule->action == AUDIT_POSSIBLE)) { |
f952d10f | 259 | pr_err("AUDIT_POSSIBLE is deprecated\n"); |
014149cc AV |
260 | goto exit_err; |
261 | } | |
262 | if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS) | |
93315ed6 AG |
263 | goto exit_err; |
264 | if (rule->field_count > AUDIT_MAX_FIELDS) | |
265 | goto exit_err; | |
266 | ||
267 | err = -ENOMEM; | |
3dc7e315 DG |
268 | entry = audit_init_entry(rule->field_count); |
269 | if (!entry) | |
93315ed6 | 270 | goto exit_err; |
93315ed6 AG |
271 | |
272 | entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND; | |
273 | entry->rule.listnr = listnr; | |
274 | entry->rule.action = rule->action; | |
275 | entry->rule.field_count = rule->field_count; | |
93315ed6 AG |
276 | |
277 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) | |
278 | entry->rule.mask[i] = rule->mask[i]; | |
279 | ||
b915543b AV |
280 | for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) { |
281 | int bit = AUDIT_BITMASK_SIZE * 32 - i - 1; | |
282 | __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)]; | |
283 | __u32 *class; | |
284 | ||
285 | if (!(*p & AUDIT_BIT(bit))) | |
286 | continue; | |
287 | *p &= ~AUDIT_BIT(bit); | |
288 | class = classes[i]; | |
289 | if (class) { | |
290 | int j; | |
291 | for (j = 0; j < AUDIT_BITMASK_SIZE; j++) | |
292 | entry->rule.mask[j] |= class[j]; | |
293 | } | |
294 | } | |
295 | ||
93315ed6 AG |
296 | return entry; |
297 | ||
298 | exit_err: | |
299 | return ERR_PTR(err); | |
300 | } | |
301 | ||
5af75d8d AV |
302 | static u32 audit_ops[] = |
303 | { | |
304 | [Audit_equal] = AUDIT_EQUAL, | |
305 | [Audit_not_equal] = AUDIT_NOT_EQUAL, | |
306 | [Audit_bitmask] = AUDIT_BIT_MASK, | |
307 | [Audit_bittest] = AUDIT_BIT_TEST, | |
308 | [Audit_lt] = AUDIT_LESS_THAN, | |
309 | [Audit_gt] = AUDIT_GREATER_THAN, | |
310 | [Audit_le] = AUDIT_LESS_THAN_OR_EQUAL, | |
311 | [Audit_ge] = AUDIT_GREATER_THAN_OR_EQUAL, | |
312 | }; | |
313 | ||
314 | static u32 audit_to_op(u32 op) | |
315 | { | |
316 | u32 n; | |
317 | for (n = Audit_equal; n < Audit_bad && audit_ops[n] != op; n++) | |
318 | ; | |
319 | return n; | |
320 | } | |
321 | ||
ab61d38e | 322 | /* check if an audit field is valid */ |
62062cf8 | 323 | static int audit_field_valid(struct audit_entry *entry, struct audit_field *f) |
93315ed6 | 324 | { |
ecc68904 | 325 | switch (f->type) { |
62062cf8 | 326 | case AUDIT_MSGTYPE: |
d904ac03 | 327 | if (entry->rule.listnr != AUDIT_FILTER_EXCLUDE && |
62062cf8 EP |
328 | entry->rule.listnr != AUDIT_FILTER_USER) |
329 | return -EINVAL; | |
330 | break; | |
42d5e376 RGB |
331 | case AUDIT_FSTYPE: |
332 | if (entry->rule.listnr != AUDIT_FILTER_FS) | |
333 | return -EINVAL; | |
334 | break; | |
335 | } | |
336 | ||
ecc68904 | 337 | switch (entry->rule.listnr) { |
42d5e376 RGB |
338 | case AUDIT_FILTER_FS: |
339 | switch(f->type) { | |
340 | case AUDIT_FSTYPE: | |
341 | case AUDIT_FILTERKEY: | |
342 | break; | |
343 | default: | |
344 | return -EINVAL; | |
345 | } | |
b7a84dea | 346 | } |
93315ed6 | 347 | |
ecc68904 RGB |
348 | /* Check for valid field type and op */ |
349 | switch (f->type) { | |
350 | case AUDIT_ARG0: | |
351 | case AUDIT_ARG1: | |
352 | case AUDIT_ARG2: | |
353 | case AUDIT_ARG3: | |
354 | case AUDIT_PERS: /* <uapi/linux/personality.h> */ | |
355 | case AUDIT_DEVMINOR: | |
356 | /* all ops are valid */ | |
357 | break; | |
ab61d38e EP |
358 | case AUDIT_UID: |
359 | case AUDIT_EUID: | |
360 | case AUDIT_SUID: | |
361 | case AUDIT_FSUID: | |
362 | case AUDIT_LOGINUID: | |
363 | case AUDIT_OBJ_UID: | |
364 | case AUDIT_GID: | |
365 | case AUDIT_EGID: | |
366 | case AUDIT_SGID: | |
367 | case AUDIT_FSGID: | |
368 | case AUDIT_OBJ_GID: | |
369 | case AUDIT_PID: | |
ab61d38e EP |
370 | case AUDIT_MSGTYPE: |
371 | case AUDIT_PPID: | |
372 | case AUDIT_DEVMAJOR: | |
ab61d38e EP |
373 | case AUDIT_EXIT: |
374 | case AUDIT_SUCCESS: | |
78122037 | 375 | case AUDIT_INODE: |
8fae4770 | 376 | case AUDIT_SESSIONID: |
ecc68904 RGB |
377 | case AUDIT_SUBJ_SEN: |
378 | case AUDIT_SUBJ_CLR: | |
379 | case AUDIT_OBJ_LEV_LOW: | |
380 | case AUDIT_OBJ_LEV_HIGH: | |
bf361231 | 381 | case AUDIT_SADDR_FAM: |
ab61d38e EP |
382 | /* bit ops are only useful on syscall args */ |
383 | if (f->op == Audit_bitmask || f->op == Audit_bittest) | |
384 | return -EINVAL; | |
385 | break; | |
ab61d38e EP |
386 | case AUDIT_SUBJ_USER: |
387 | case AUDIT_SUBJ_ROLE: | |
388 | case AUDIT_SUBJ_TYPE: | |
ab61d38e EP |
389 | case AUDIT_OBJ_USER: |
390 | case AUDIT_OBJ_ROLE: | |
391 | case AUDIT_OBJ_TYPE: | |
ab61d38e EP |
392 | case AUDIT_WATCH: |
393 | case AUDIT_DIR: | |
394 | case AUDIT_FILTERKEY: | |
780a7654 | 395 | case AUDIT_LOGINUID_SET: |
ab61d38e | 396 | case AUDIT_ARCH: |
42d5e376 | 397 | case AUDIT_FSTYPE: |
ecc68904 RGB |
398 | case AUDIT_PERM: |
399 | case AUDIT_FILETYPE: | |
400 | case AUDIT_FIELD_COMPARE: | |
401 | case AUDIT_EXE: | |
402 | /* only equal and not equal valid ops */ | |
ab61d38e EP |
403 | if (f->op != Audit_not_equal && f->op != Audit_equal) |
404 | return -EINVAL; | |
405 | break; | |
ecc68904 RGB |
406 | default: |
407 | /* field not recognized */ | |
408 | return -EINVAL; | |
409 | } | |
410 | ||
411 | /* Check for select valid field values */ | |
412 | switch (f->type) { | |
413 | case AUDIT_LOGINUID_SET: | |
414 | if ((f->val != 0) && (f->val != 1)) | |
415 | return -EINVAL; | |
416 | break; | |
ab61d38e EP |
417 | case AUDIT_PERM: |
418 | if (f->val & ~15) | |
419 | return -EINVAL; | |
420 | break; | |
421 | case AUDIT_FILETYPE: | |
422 | if (f->val & ~S_IFMT) | |
423 | return -EINVAL; | |
424 | break; | |
425 | case AUDIT_FIELD_COMPARE: | |
426 | if (f->val > AUDIT_MAX_FIELD_COMPARE) | |
427 | return -EINVAL; | |
428 | break; | |
bf361231 RGB |
429 | case AUDIT_SADDR_FAM: |
430 | if (f->val >= AF_MAX) | |
34d99af5 | 431 | return -EINVAL; |
34d99af5 | 432 | break; |
ecc68904 | 433 | default: |
34d99af5 | 434 | break; |
b7a84dea | 435 | } |
ecc68904 | 436 | |
62062cf8 | 437 | return 0; |
fe7752ba DW |
438 | } |
439 | ||
fd97646b | 440 | /* Translate struct audit_rule_data to kernel's rule representation. */ |
93315ed6 AG |
441 | static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data, |
442 | size_t datasz) | |
fe7752ba | 443 | { |
93315ed6 AG |
444 | int err = 0; |
445 | struct audit_entry *entry; | |
446 | void *bufp; | |
3dc7e315 | 447 | size_t remain = datasz - sizeof(struct audit_rule_data); |
fe7752ba | 448 | int i; |
3dc7e315 | 449 | char *str; |
34d99af5 | 450 | struct audit_fsnotify_mark *audit_mark; |
fe7752ba | 451 | |
56c4911a | 452 | entry = audit_to_entry_common(data); |
93315ed6 AG |
453 | if (IS_ERR(entry)) |
454 | goto exit_nofree; | |
fe7752ba | 455 | |
93315ed6 | 456 | bufp = data->buf; |
93315ed6 AG |
457 | for (i = 0; i < data->field_count; i++) { |
458 | struct audit_field *f = &entry->rule.fields[i]; | |
2ad3e17e | 459 | u32 f_val; |
93315ed6 AG |
460 | |
461 | err = -EINVAL; | |
5af75d8d AV |
462 | |
463 | f->op = audit_to_op(data->fieldflags[i]); | |
464 | if (f->op == Audit_bad) | |
93315ed6 AG |
465 | goto exit_free; |
466 | ||
93315ed6 | 467 | f->type = data->fields[i]; |
2ad3e17e | 468 | f_val = data->values[i]; |
62062cf8 | 469 | |
780a7654 | 470 | /* Support legacy tests for a valid loginuid */ |
2ad3e17e | 471 | if ((f->type == AUDIT_LOGINUID) && (f_val == AUDIT_UID_UNSET)) { |
780a7654 | 472 | f->type = AUDIT_LOGINUID_SET; |
2ad3e17e | 473 | f_val = 0; |
041d7b98 | 474 | entry->rule.pflags |= AUDIT_LOGINUID_LEGACY; |
f1dc4867 RGB |
475 | } |
476 | ||
62062cf8 EP |
477 | err = audit_field_valid(entry, f); |
478 | if (err) | |
479 | goto exit_free; | |
480 | ||
481 | err = -EINVAL; | |
ab61d38e | 482 | switch (f->type) { |
780a7654 | 483 | case AUDIT_LOGINUID: |
0a73dccc AV |
484 | case AUDIT_UID: |
485 | case AUDIT_EUID: | |
486 | case AUDIT_SUID: | |
487 | case AUDIT_FSUID: | |
ca57ec0f | 488 | case AUDIT_OBJ_UID: |
2ad3e17e | 489 | f->uid = make_kuid(current_user_ns(), f_val); |
ca57ec0f EB |
490 | if (!uid_valid(f->uid)) |
491 | goto exit_free; | |
492 | break; | |
0a73dccc AV |
493 | case AUDIT_GID: |
494 | case AUDIT_EGID: | |
495 | case AUDIT_SGID: | |
496 | case AUDIT_FSGID: | |
ca57ec0f | 497 | case AUDIT_OBJ_GID: |
2ad3e17e | 498 | f->gid = make_kgid(current_user_ns(), f_val); |
ca57ec0f EB |
499 | if (!gid_valid(f->gid)) |
500 | goto exit_free; | |
501 | break; | |
e54dc243 | 502 | case AUDIT_ARCH: |
2ad3e17e | 503 | f->val = f_val; |
e54dc243 AG |
504 | entry->rule.arch_f = f; |
505 | break; | |
3a6b9f85 DG |
506 | case AUDIT_SUBJ_USER: |
507 | case AUDIT_SUBJ_ROLE: | |
508 | case AUDIT_SUBJ_TYPE: | |
509 | case AUDIT_SUBJ_SEN: | |
510 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
511 | case AUDIT_OBJ_USER: |
512 | case AUDIT_OBJ_ROLE: | |
513 | case AUDIT_OBJ_TYPE: | |
514 | case AUDIT_OBJ_LEV_LOW: | |
515 | case AUDIT_OBJ_LEV_HIGH: | |
2ad3e17e PM |
516 | str = audit_unpack_string(&bufp, &remain, f_val); |
517 | if (IS_ERR(str)) { | |
518 | err = PTR_ERR(str); | |
3dc7e315 | 519 | goto exit_free; |
2ad3e17e PM |
520 | } |
521 | entry->rule.buflen += f_val; | |
f17b27a2 | 522 | f->lsm_isset = true; |
2ad3e17e | 523 | f->lsm_str = str; |
d7a96f3a | 524 | err = security_audit_rule_init(f->type, f->op, str, |
f17b27a2 | 525 | f->lsm_rules); |
3dc7e315 DG |
526 | /* Keep currently invalid fields around in case they |
527 | * become valid after a policy reload. */ | |
528 | if (err == -EINVAL) { | |
f952d10f RGB |
529 | pr_warn("audit rule for LSM \'%s\' is invalid\n", |
530 | str); | |
3dc7e315 | 531 | err = 0; |
2ad3e17e | 532 | } else if (err) |
3dc7e315 | 533 | goto exit_free; |
3dc7e315 | 534 | break; |
f368c07d | 535 | case AUDIT_WATCH: |
2ad3e17e PM |
536 | str = audit_unpack_string(&bufp, &remain, f_val); |
537 | if (IS_ERR(str)) { | |
538 | err = PTR_ERR(str); | |
f368c07d | 539 | goto exit_free; |
2ad3e17e PM |
540 | } |
541 | err = audit_to_watch(&entry->rule, str, f_val, f->op); | |
f368c07d AG |
542 | if (err) { |
543 | kfree(str); | |
544 | goto exit_free; | |
545 | } | |
2ad3e17e | 546 | entry->rule.buflen += f_val; |
f368c07d | 547 | break; |
74c3cbe3 | 548 | case AUDIT_DIR: |
2ad3e17e PM |
549 | str = audit_unpack_string(&bufp, &remain, f_val); |
550 | if (IS_ERR(str)) { | |
551 | err = PTR_ERR(str); | |
74c3cbe3 | 552 | goto exit_free; |
2ad3e17e | 553 | } |
74c3cbe3 AV |
554 | err = audit_make_tree(&entry->rule, str, f->op); |
555 | kfree(str); | |
556 | if (err) | |
557 | goto exit_free; | |
2ad3e17e | 558 | entry->rule.buflen += f_val; |
74c3cbe3 | 559 | break; |
f368c07d | 560 | case AUDIT_INODE: |
2ad3e17e | 561 | f->val = f_val; |
f368c07d AG |
562 | err = audit_to_inode(&entry->rule, f); |
563 | if (err) | |
564 | goto exit_free; | |
565 | break; | |
5adc8a6a | 566 | case AUDIT_FILTERKEY: |
2ad3e17e | 567 | if (entry->rule.filterkey || f_val > AUDIT_MAX_KEY_LEN) |
5adc8a6a | 568 | goto exit_free; |
2ad3e17e PM |
569 | str = audit_unpack_string(&bufp, &remain, f_val); |
570 | if (IS_ERR(str)) { | |
571 | err = PTR_ERR(str); | |
5adc8a6a | 572 | goto exit_free; |
2ad3e17e PM |
573 | } |
574 | entry->rule.buflen += f_val; | |
5adc8a6a AG |
575 | entry->rule.filterkey = str; |
576 | break; | |
34d99af5 | 577 | case AUDIT_EXE: |
2ad3e17e | 578 | if (entry->rule.exe || f_val > PATH_MAX) |
34d99af5 | 579 | goto exit_free; |
2ad3e17e | 580 | str = audit_unpack_string(&bufp, &remain, f_val); |
34d99af5 RGB |
581 | if (IS_ERR(str)) { |
582 | err = PTR_ERR(str); | |
583 | goto exit_free; | |
584 | } | |
2ad3e17e | 585 | audit_mark = audit_alloc_mark(&entry->rule, str, f_val); |
34d99af5 RGB |
586 | if (IS_ERR(audit_mark)) { |
587 | kfree(str); | |
588 | err = PTR_ERR(audit_mark); | |
589 | goto exit_free; | |
590 | } | |
2ad3e17e | 591 | entry->rule.buflen += f_val; |
34d99af5 RGB |
592 | entry->rule.exe = audit_mark; |
593 | break; | |
2ad3e17e PM |
594 | default: |
595 | f->val = f_val; | |
596 | break; | |
f368c07d AG |
597 | } |
598 | } | |
599 | ||
5af75d8d AV |
600 | if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal) |
601 | entry->rule.inode_f = NULL; | |
93315ed6 AG |
602 | |
603 | exit_nofree: | |
604 | return entry; | |
605 | ||
606 | exit_free: | |
373e0f34 CG |
607 | if (entry->rule.tree) |
608 | audit_put_tree(entry->rule.tree); /* that's the temporary one */ | |
34d99af5 RGB |
609 | if (entry->rule.exe) |
610 | audit_remove_mark(entry->rule.exe); /* that's the template one */ | |
93315ed6 AG |
611 | audit_free_rule(entry); |
612 | return ERR_PTR(err); | |
613 | } | |
614 | ||
615 | /* Pack a filter field's string representation into data block. */ | |
74c3cbe3 | 616 | static inline size_t audit_pack_string(void **bufp, const char *str) |
93315ed6 AG |
617 | { |
618 | size_t len = strlen(str); | |
619 | ||
620 | memcpy(*bufp, str, len); | |
621 | *bufp += len; | |
622 | ||
623 | return len; | |
624 | } | |
625 | ||
fd97646b | 626 | /* Translate kernel rule representation to struct audit_rule_data. */ |
93315ed6 AG |
627 | static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule) |
628 | { | |
629 | struct audit_rule_data *data; | |
630 | void *bufp; | |
631 | int i; | |
632 | ||
633 | data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL); | |
634 | if (unlikely(!data)) | |
0a3b483e | 635 | return NULL; |
93315ed6 AG |
636 | memset(data, 0, sizeof(*data)); |
637 | ||
638 | data->flags = krule->flags | krule->listnr; | |
639 | data->action = krule->action; | |
640 | data->field_count = krule->field_count; | |
641 | bufp = data->buf; | |
642 | for (i = 0; i < data->field_count; i++) { | |
643 | struct audit_field *f = &krule->fields[i]; | |
644 | ||
645 | data->fields[i] = f->type; | |
5af75d8d | 646 | data->fieldflags[i] = audit_ops[f->op]; |
93315ed6 | 647 | switch(f->type) { |
3a6b9f85 DG |
648 | case AUDIT_SUBJ_USER: |
649 | case AUDIT_SUBJ_ROLE: | |
650 | case AUDIT_SUBJ_TYPE: | |
651 | case AUDIT_SUBJ_SEN: | |
652 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
653 | case AUDIT_OBJ_USER: |
654 | case AUDIT_OBJ_ROLE: | |
655 | case AUDIT_OBJ_TYPE: | |
656 | case AUDIT_OBJ_LEV_LOW: | |
657 | case AUDIT_OBJ_LEV_HIGH: | |
3dc7e315 | 658 | data->buflen += data->values[i] = |
04305e4a | 659 | audit_pack_string(&bufp, f->lsm_str); |
3dc7e315 | 660 | break; |
f368c07d AG |
661 | case AUDIT_WATCH: |
662 | data->buflen += data->values[i] = | |
cfcad62c EP |
663 | audit_pack_string(&bufp, |
664 | audit_watch_path(krule->watch)); | |
f368c07d | 665 | break; |
74c3cbe3 AV |
666 | case AUDIT_DIR: |
667 | data->buflen += data->values[i] = | |
668 | audit_pack_string(&bufp, | |
669 | audit_tree_path(krule->tree)); | |
670 | break; | |
5adc8a6a AG |
671 | case AUDIT_FILTERKEY: |
672 | data->buflen += data->values[i] = | |
673 | audit_pack_string(&bufp, krule->filterkey); | |
674 | break; | |
34d99af5 RGB |
675 | case AUDIT_EXE: |
676 | data->buflen += data->values[i] = | |
677 | audit_pack_string(&bufp, audit_mark_path(krule->exe)); | |
678 | break; | |
041d7b98 RGB |
679 | case AUDIT_LOGINUID_SET: |
680 | if (krule->pflags & AUDIT_LOGINUID_LEGACY && !f->val) { | |
681 | data->fields[i] = AUDIT_LOGINUID; | |
682 | data->values[i] = AUDIT_UID_UNSET; | |
683 | break; | |
684 | } | |
df561f66 | 685 | fallthrough; /* if set */ |
93315ed6 AG |
686 | default: |
687 | data->values[i] = f->val; | |
688 | } | |
689 | } | |
690 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i]; | |
691 | ||
692 | return data; | |
693 | } | |
694 | ||
695 | /* Compare two rules in kernel format. Considered success if rules | |
696 | * don't match. */ | |
697 | static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b) | |
698 | { | |
699 | int i; | |
700 | ||
701 | if (a->flags != b->flags || | |
041d7b98 | 702 | a->pflags != b->pflags || |
93315ed6 AG |
703 | a->listnr != b->listnr || |
704 | a->action != b->action || | |
705 | a->field_count != b->field_count) | |
fe7752ba DW |
706 | return 1; |
707 | ||
708 | for (i = 0; i < a->field_count; i++) { | |
93315ed6 AG |
709 | if (a->fields[i].type != b->fields[i].type || |
710 | a->fields[i].op != b->fields[i].op) | |
fe7752ba | 711 | return 1; |
93315ed6 AG |
712 | |
713 | switch(a->fields[i].type) { | |
3a6b9f85 DG |
714 | case AUDIT_SUBJ_USER: |
715 | case AUDIT_SUBJ_ROLE: | |
716 | case AUDIT_SUBJ_TYPE: | |
717 | case AUDIT_SUBJ_SEN: | |
718 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
719 | case AUDIT_OBJ_USER: |
720 | case AUDIT_OBJ_ROLE: | |
721 | case AUDIT_OBJ_TYPE: | |
722 | case AUDIT_OBJ_LEV_LOW: | |
723 | case AUDIT_OBJ_LEV_HIGH: | |
04305e4a | 724 | if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str)) |
3dc7e315 DG |
725 | return 1; |
726 | break; | |
f368c07d | 727 | case AUDIT_WATCH: |
cfcad62c EP |
728 | if (strcmp(audit_watch_path(a->watch), |
729 | audit_watch_path(b->watch))) | |
f368c07d AG |
730 | return 1; |
731 | break; | |
74c3cbe3 AV |
732 | case AUDIT_DIR: |
733 | if (strcmp(audit_tree_path(a->tree), | |
734 | audit_tree_path(b->tree))) | |
735 | return 1; | |
736 | break; | |
5adc8a6a AG |
737 | case AUDIT_FILTERKEY: |
738 | /* both filterkeys exist based on above type compare */ | |
739 | if (strcmp(a->filterkey, b->filterkey)) | |
740 | return 1; | |
741 | break; | |
34d99af5 RGB |
742 | case AUDIT_EXE: |
743 | /* both paths exist based on above type compare */ | |
744 | if (strcmp(audit_mark_path(a->exe), | |
745 | audit_mark_path(b->exe))) | |
746 | return 1; | |
747 | break; | |
ca57ec0f EB |
748 | case AUDIT_UID: |
749 | case AUDIT_EUID: | |
750 | case AUDIT_SUID: | |
751 | case AUDIT_FSUID: | |
752 | case AUDIT_LOGINUID: | |
753 | case AUDIT_OBJ_UID: | |
754 | if (!uid_eq(a->fields[i].uid, b->fields[i].uid)) | |
755 | return 1; | |
756 | break; | |
757 | case AUDIT_GID: | |
758 | case AUDIT_EGID: | |
759 | case AUDIT_SGID: | |
760 | case AUDIT_FSGID: | |
761 | case AUDIT_OBJ_GID: | |
762 | if (!gid_eq(a->fields[i].gid, b->fields[i].gid)) | |
763 | return 1; | |
764 | break; | |
93315ed6 AG |
765 | default: |
766 | if (a->fields[i].val != b->fields[i].val) | |
767 | return 1; | |
768 | } | |
fe7752ba DW |
769 | } |
770 | ||
771 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) | |
772 | if (a->mask[i] != b->mask[i]) | |
773 | return 1; | |
774 | ||
775 | return 0; | |
776 | } | |
777 | ||
f17b27a2 | 778 | /* Duplicate LSM field information. The lsm_rules is opaque, so must be |
3dc7e315 | 779 | * re-initialized. */ |
d7a96f3a | 780 | static inline int audit_dupe_lsm_field(struct audit_field *df, |
3dc7e315 DG |
781 | struct audit_field *sf) |
782 | { | |
783 | int ret = 0; | |
04305e4a | 784 | char *lsm_str; |
3dc7e315 | 785 | |
04305e4a AD |
786 | /* our own copy of lsm_str */ |
787 | lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL); | |
788 | if (unlikely(!lsm_str)) | |
3e1fbd12 | 789 | return -ENOMEM; |
04305e4a | 790 | df->lsm_str = lsm_str; |
3dc7e315 | 791 | |
f17b27a2 | 792 | /* our own (refreshed) copy of lsm_rules */ |
04305e4a | 793 | ret = security_audit_rule_init(df->type, df->op, df->lsm_str, |
f17b27a2 | 794 | df->lsm_rules); |
3dc7e315 DG |
795 | /* Keep currently invalid fields around in case they |
796 | * become valid after a policy reload. */ | |
797 | if (ret == -EINVAL) { | |
f952d10f RGB |
798 | pr_warn("audit rule for LSM \'%s\' is invalid\n", |
799 | df->lsm_str); | |
3dc7e315 DG |
800 | ret = 0; |
801 | } | |
802 | ||
803 | return ret; | |
804 | } | |
805 | ||
806 | /* Duplicate an audit rule. This will be a deep copy with the exception | |
d7a96f3a | 807 | * of the watch - that pointer is carried over. The LSM specific fields |
3dc7e315 | 808 | * will be updated in the copy. The point is to be able to replace the old |
f368c07d AG |
809 | * rule with the new rule in the filterlist, then free the old rule. |
810 | * The rlist element is undefined; list manipulations are handled apart from | |
811 | * the initial copy. */ | |
ae7b8f41 | 812 | struct audit_entry *audit_dupe_rule(struct audit_krule *old) |
3dc7e315 DG |
813 | { |
814 | u32 fcount = old->field_count; | |
815 | struct audit_entry *entry; | |
816 | struct audit_krule *new; | |
5adc8a6a | 817 | char *fk; |
3dc7e315 DG |
818 | int i, err = 0; |
819 | ||
820 | entry = audit_init_entry(fcount); | |
821 | if (unlikely(!entry)) | |
822 | return ERR_PTR(-ENOMEM); | |
823 | ||
824 | new = &entry->rule; | |
3dc7e315 | 825 | new->flags = old->flags; |
041d7b98 | 826 | new->pflags = old->pflags; |
3dc7e315 DG |
827 | new->listnr = old->listnr; |
828 | new->action = old->action; | |
829 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) | |
830 | new->mask[i] = old->mask[i]; | |
0590b933 | 831 | new->prio = old->prio; |
3dc7e315 | 832 | new->buflen = old->buflen; |
f368c07d | 833 | new->inode_f = old->inode_f; |
3dc7e315 | 834 | new->field_count = old->field_count; |
ae7b8f41 | 835 | |
74c3cbe3 AV |
836 | /* |
837 | * note that we are OK with not refcounting here; audit_match_tree() | |
838 | * never dereferences tree and we can't get false positives there | |
839 | * since we'd have to have rule gone from the list *and* removed | |
840 | * before the chunks found by lookup had been allocated, i.e. before | |
841 | * the beginning of list scan. | |
842 | */ | |
843 | new->tree = old->tree; | |
3dc7e315 DG |
844 | memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount); |
845 | ||
f17b27a2 | 846 | /* deep copy this information, updating the lsm_rules fields, because |
3dc7e315 DG |
847 | * the originals will all be freed when the old rule is freed. */ |
848 | for (i = 0; i < fcount; i++) { | |
849 | switch (new->fields[i].type) { | |
3a6b9f85 DG |
850 | case AUDIT_SUBJ_USER: |
851 | case AUDIT_SUBJ_ROLE: | |
852 | case AUDIT_SUBJ_TYPE: | |
853 | case AUDIT_SUBJ_SEN: | |
854 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
855 | case AUDIT_OBJ_USER: |
856 | case AUDIT_OBJ_ROLE: | |
857 | case AUDIT_OBJ_TYPE: | |
858 | case AUDIT_OBJ_LEV_LOW: | |
859 | case AUDIT_OBJ_LEV_HIGH: | |
d7a96f3a | 860 | err = audit_dupe_lsm_field(&new->fields[i], |
3dc7e315 | 861 | &old->fields[i]); |
5adc8a6a AG |
862 | break; |
863 | case AUDIT_FILTERKEY: | |
864 | fk = kstrdup(old->filterkey, GFP_KERNEL); | |
865 | if (unlikely(!fk)) | |
866 | err = -ENOMEM; | |
867 | else | |
868 | new->filterkey = fk; | |
34d99af5 RGB |
869 | break; |
870 | case AUDIT_EXE: | |
871 | err = audit_dupe_exe(new, old); | |
872 | break; | |
3dc7e315 DG |
873 | } |
874 | if (err) { | |
34d99af5 RGB |
875 | if (new->exe) |
876 | audit_remove_mark(new->exe); | |
3dc7e315 DG |
877 | audit_free_rule(entry); |
878 | return ERR_PTR(err); | |
879 | } | |
880 | } | |
881 | ||
ae7b8f41 EP |
882 | if (old->watch) { |
883 | audit_get_watch(old->watch); | |
884 | new->watch = old->watch; | |
f368c07d AG |
885 | } |
886 | ||
3dc7e315 DG |
887 | return entry; |
888 | } | |
889 | ||
f368c07d AG |
890 | /* Find an existing audit rule. |
891 | * Caller must hold audit_filter_mutex to prevent stale rule data. */ | |
892 | static struct audit_entry *audit_find_rule(struct audit_entry *entry, | |
36c4f1b1 | 893 | struct list_head **p) |
f368c07d AG |
894 | { |
895 | struct audit_entry *e, *found = NULL; | |
36c4f1b1 | 896 | struct list_head *list; |
f368c07d AG |
897 | int h; |
898 | ||
36c4f1b1 AV |
899 | if (entry->rule.inode_f) { |
900 | h = audit_hash_ino(entry->rule.inode_f->val); | |
901 | *p = list = &audit_inode_hash[h]; | |
902 | } else if (entry->rule.watch) { | |
f368c07d AG |
903 | /* we don't know the inode number, so must walk entire hash */ |
904 | for (h = 0; h < AUDIT_INODE_BUCKETS; h++) { | |
905 | list = &audit_inode_hash[h]; | |
906 | list_for_each_entry(e, list, list) | |
907 | if (!audit_compare_rule(&entry->rule, &e->rule)) { | |
908 | found = e; | |
909 | goto out; | |
910 | } | |
911 | } | |
912 | goto out; | |
36c4f1b1 AV |
913 | } else { |
914 | *p = list = &audit_filter_list[entry->rule.listnr]; | |
f368c07d AG |
915 | } |
916 | ||
917 | list_for_each_entry(e, list, list) | |
918 | if (!audit_compare_rule(&entry->rule, &e->rule)) { | |
919 | found = e; | |
920 | goto out; | |
921 | } | |
922 | ||
923 | out: | |
924 | return found; | |
925 | } | |
926 | ||
0590b933 AV |
927 | static u64 prio_low = ~0ULL/2; |
928 | static u64 prio_high = ~0ULL/2 - 1; | |
929 | ||
f368c07d | 930 | /* Add rule to given filterlist if not a duplicate. */ |
36c4f1b1 | 931 | static inline int audit_add_rule(struct audit_entry *entry) |
fe7752ba | 932 | { |
93315ed6 | 933 | struct audit_entry *e; |
f368c07d | 934 | struct audit_watch *watch = entry->rule.watch; |
74c3cbe3 | 935 | struct audit_tree *tree = entry->rule.tree; |
36c4f1b1 | 936 | struct list_head *list; |
ae9d2fb4 | 937 | int err = 0; |
471a5c7c AV |
938 | #ifdef CONFIG_AUDITSYSCALL |
939 | int dont_count = 0; | |
940 | ||
42d5e376 RGB |
941 | /* If any of these, don't count towards total */ |
942 | switch(entry->rule.listnr) { | |
943 | case AUDIT_FILTER_USER: | |
d904ac03 | 944 | case AUDIT_FILTER_EXCLUDE: |
42d5e376 | 945 | case AUDIT_FILTER_FS: |
471a5c7c | 946 | dont_count = 1; |
42d5e376 | 947 | } |
471a5c7c | 948 | #endif |
f368c07d | 949 | |
f368c07d | 950 | mutex_lock(&audit_filter_mutex); |
36c4f1b1 | 951 | e = audit_find_rule(entry, &list); |
f368c07d | 952 | if (e) { |
35fe4d0b | 953 | mutex_unlock(&audit_filter_mutex); |
f368c07d | 954 | err = -EEXIST; |
74c3cbe3 AV |
955 | /* normally audit_add_tree_rule() will free it on failure */ |
956 | if (tree) | |
957 | audit_put_tree(tree); | |
f8259b26 | 958 | return err; |
f368c07d | 959 | } |
fe7752ba | 960 | |
f368c07d AG |
961 | if (watch) { |
962 | /* audit_filter_mutex is dropped and re-taken during this call */ | |
ae7b8f41 | 963 | err = audit_add_watch(&entry->rule, &list); |
f368c07d AG |
964 | if (err) { |
965 | mutex_unlock(&audit_filter_mutex); | |
2f992ee8 CG |
966 | /* |
967 | * normally audit_add_tree_rule() will free it | |
968 | * on failure | |
969 | */ | |
970 | if (tree) | |
971 | audit_put_tree(tree); | |
f8259b26 | 972 | return err; |
f368c07d | 973 | } |
fe7752ba | 974 | } |
74c3cbe3 AV |
975 | if (tree) { |
976 | err = audit_add_tree_rule(&entry->rule); | |
977 | if (err) { | |
978 | mutex_unlock(&audit_filter_mutex); | |
f8259b26 | 979 | return err; |
74c3cbe3 AV |
980 | } |
981 | } | |
fe7752ba | 982 | |
0590b933 AV |
983 | entry->rule.prio = ~0ULL; |
984 | if (entry->rule.listnr == AUDIT_FILTER_EXIT) { | |
985 | if (entry->rule.flags & AUDIT_FILTER_PREPEND) | |
986 | entry->rule.prio = ++prio_high; | |
987 | else | |
988 | entry->rule.prio = --prio_low; | |
989 | } | |
990 | ||
fe7752ba | 991 | if (entry->rule.flags & AUDIT_FILTER_PREPEND) { |
e45aa212 AV |
992 | list_add(&entry->rule.list, |
993 | &audit_rules_list[entry->rule.listnr]); | |
fe7752ba | 994 | list_add_rcu(&entry->list, list); |
6a2bceec | 995 | entry->rule.flags &= ~AUDIT_FILTER_PREPEND; |
fe7752ba | 996 | } else { |
e45aa212 AV |
997 | list_add_tail(&entry->rule.list, |
998 | &audit_rules_list[entry->rule.listnr]); | |
fe7752ba DW |
999 | list_add_tail_rcu(&entry->list, list); |
1000 | } | |
471a5c7c AV |
1001 | #ifdef CONFIG_AUDITSYSCALL |
1002 | if (!dont_count) | |
1003 | audit_n_rules++; | |
e54dc243 AG |
1004 | |
1005 | if (!audit_match_signal(entry)) | |
1006 | audit_signals++; | |
471a5c7c | 1007 | #endif |
f368c07d | 1008 | mutex_unlock(&audit_filter_mutex); |
fe7752ba | 1009 | |
f368c07d | 1010 | return err; |
fe7752ba DW |
1011 | } |
1012 | ||
f368c07d | 1013 | /* Remove an existing rule from filterlist. */ |
7f492942 | 1014 | int audit_del_rule(struct audit_entry *entry) |
fe7752ba DW |
1015 | { |
1016 | struct audit_entry *e; | |
74c3cbe3 | 1017 | struct audit_tree *tree = entry->rule.tree; |
36c4f1b1 | 1018 | struct list_head *list; |
36c4f1b1 | 1019 | int ret = 0; |
471a5c7c AV |
1020 | #ifdef CONFIG_AUDITSYSCALL |
1021 | int dont_count = 0; | |
1022 | ||
42d5e376 RGB |
1023 | /* If any of these, don't count towards total */ |
1024 | switch(entry->rule.listnr) { | |
1025 | case AUDIT_FILTER_USER: | |
d904ac03 | 1026 | case AUDIT_FILTER_EXCLUDE: |
42d5e376 | 1027 | case AUDIT_FILTER_FS: |
471a5c7c | 1028 | dont_count = 1; |
42d5e376 | 1029 | } |
471a5c7c | 1030 | #endif |
f368c07d | 1031 | |
f368c07d | 1032 | mutex_lock(&audit_filter_mutex); |
36c4f1b1 | 1033 | e = audit_find_rule(entry, &list); |
f368c07d | 1034 | if (!e) { |
f368c07d AG |
1035 | ret = -ENOENT; |
1036 | goto out; | |
1037 | } | |
1038 | ||
cfcad62c | 1039 | if (e->rule.watch) |
a05fb6cc | 1040 | audit_remove_watch_rule(&e->rule); |
f368c07d | 1041 | |
74c3cbe3 AV |
1042 | if (e->rule.tree) |
1043 | audit_remove_tree_rule(&e->rule); | |
1044 | ||
34d99af5 RGB |
1045 | if (e->rule.exe) |
1046 | audit_remove_mark_rule(&e->rule); | |
1047 | ||
471a5c7c AV |
1048 | #ifdef CONFIG_AUDITSYSCALL |
1049 | if (!dont_count) | |
1050 | audit_n_rules--; | |
e54dc243 AG |
1051 | |
1052 | if (!audit_match_signal(entry)) | |
1053 | audit_signals--; | |
471a5c7c | 1054 | #endif |
8c85fc9a RGB |
1055 | |
1056 | list_del_rcu(&e->list); | |
1057 | list_del(&e->rule.list); | |
1058 | call_rcu(&e->rcu, audit_free_rule_rcu); | |
f368c07d | 1059 | |
f368c07d | 1060 | out: |
8c85fc9a RGB |
1061 | mutex_unlock(&audit_filter_mutex); |
1062 | ||
74c3cbe3 AV |
1063 | if (tree) |
1064 | audit_put_tree(tree); /* that's the temporary one */ | |
f368c07d AG |
1065 | |
1066 | return ret; | |
fe7752ba DW |
1067 | } |
1068 | ||
93315ed6 | 1069 | /* List rules using struct audit_rule_data. */ |
45a0642b | 1070 | static void audit_list_rules(int seq, struct sk_buff_head *q) |
93315ed6 | 1071 | { |
9044e6bc | 1072 | struct sk_buff *skb; |
e45aa212 | 1073 | struct audit_krule *r; |
93315ed6 AG |
1074 | int i; |
1075 | ||
f368c07d AG |
1076 | /* This is a blocking read, so use audit_filter_mutex instead of rcu |
1077 | * iterator to sync with list writers. */ | |
93315ed6 | 1078 | for (i=0; i<AUDIT_NR_FILTERS; i++) { |
e45aa212 | 1079 | list_for_each_entry(r, &audit_rules_list[i], list) { |
f368c07d AG |
1080 | struct audit_rule_data *data; |
1081 | ||
e45aa212 | 1082 | data = audit_krule_to_data(r); |
f368c07d AG |
1083 | if (unlikely(!data)) |
1084 | break; | |
45a0642b PM |
1085 | skb = audit_make_reply(seq, AUDIT_LIST_RULES, 0, 1, |
1086 | data, | |
f9441639 | 1087 | sizeof(*data) + data->buflen); |
9044e6bc AV |
1088 | if (skb) |
1089 | skb_queue_tail(q, skb); | |
93315ed6 AG |
1090 | kfree(data); |
1091 | } | |
1092 | } | |
45a0642b | 1093 | skb = audit_make_reply(seq, AUDIT_LIST_RULES, 1, 1, NULL, 0); |
9044e6bc AV |
1094 | if (skb) |
1095 | skb_queue_tail(q, skb); | |
93315ed6 AG |
1096 | } |
1097 | ||
5adc8a6a | 1098 | /* Log rule additions and removals */ |
dc9eb698 | 1099 | static void audit_log_rule_change(char *action, struct audit_krule *rule, int res) |
5adc8a6a AG |
1100 | { |
1101 | struct audit_buffer *ab; | |
1102 | ||
1a6b9f23 EP |
1103 | if (!audit_enabled) |
1104 | return; | |
1105 | ||
626abcd1 | 1106 | ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_CONFIG_CHANGE); |
5adc8a6a AG |
1107 | if (!ab) |
1108 | return; | |
5c5b8d8b | 1109 | audit_log_session_info(ab); |
85ff5379 | 1110 | audit_log_task_context(ab, NULL); |
c1e8f06d | 1111 | audit_log_format(ab, " op=%s", action); |
9d960985 | 1112 | audit_log_key(ab, rule->filterkey); |
5adc8a6a AG |
1113 | audit_log_format(ab, " list=%d res=%d", rule->listnr, res); |
1114 | audit_log_end(ab); | |
1115 | } | |
1116 | ||
fe7752ba | 1117 | /** |
ce0d9f04 | 1118 | * audit_rule_change - apply all rules to the specified message type |
fe7752ba | 1119 | * @type: audit message type |
fe7752ba DW |
1120 | * @seq: netlink audit message sequence (serial) number |
1121 | * @data: payload data | |
93315ed6 | 1122 | * @datasz: size of payload data |
fe7752ba | 1123 | */ |
45a0642b | 1124 | int audit_rule_change(int type, int seq, void *data, size_t datasz) |
fe7752ba | 1125 | { |
93315ed6 AG |
1126 | int err = 0; |
1127 | struct audit_entry *entry; | |
fe7752ba DW |
1128 | |
1129 | switch (type) { | |
93315ed6 | 1130 | case AUDIT_ADD_RULE: |
70c4cf17 WW |
1131 | entry = audit_data_to_entry(data, datasz); |
1132 | if (IS_ERR(entry)) | |
1133 | return PTR_ERR(entry); | |
36c4f1b1 | 1134 | err = audit_add_rule(entry); |
e7df61f4 | 1135 | audit_log_rule_change("add_rule", &entry->rule, !err); |
fe7752ba | 1136 | break; |
93315ed6 | 1137 | case AUDIT_DEL_RULE: |
70c4cf17 WW |
1138 | entry = audit_data_to_entry(data, datasz); |
1139 | if (IS_ERR(entry)) | |
1140 | return PTR_ERR(entry); | |
36c4f1b1 | 1141 | err = audit_del_rule(entry); |
e7df61f4 | 1142 | audit_log_rule_change("remove_rule", &entry->rule, !err); |
fe7752ba DW |
1143 | break; |
1144 | default: | |
739c9503 | 1145 | WARN_ON(1); |
70c4cf17 | 1146 | return -EINVAL; |
fe7752ba DW |
1147 | } |
1148 | ||
34d99af5 RGB |
1149 | if (err || type == AUDIT_DEL_RULE) { |
1150 | if (entry->rule.exe) | |
1151 | audit_remove_mark(entry->rule.exe); | |
e85322d2 | 1152 | audit_free_rule(entry); |
34d99af5 | 1153 | } |
e85322d2 | 1154 | |
fe7752ba DW |
1155 | return err; |
1156 | } | |
1157 | ||
ce0d9f04 RGB |
1158 | /** |
1159 | * audit_list_rules_send - list the audit rules | |
d211f177 | 1160 | * @request_skb: skb of request we are replying to (used to target the reply) |
ce0d9f04 RGB |
1161 | * @seq: netlink audit message sequence (serial) number |
1162 | */ | |
6f285b19 | 1163 | int audit_list_rules_send(struct sk_buff *request_skb, int seq) |
ce0d9f04 RGB |
1164 | { |
1165 | struct task_struct *tsk; | |
1166 | struct audit_netlink_list *dest; | |
ce0d9f04 RGB |
1167 | |
1168 | /* We can't just spew out the rules here because we might fill | |
1169 | * the available socket buffer space and deadlock waiting for | |
1170 | * auditctl to read from it... which isn't ever going to | |
1171 | * happen if we're actually running in the context of auditctl | |
1172 | * trying to _send_ the stuff */ | |
1173 | ||
3054d067 | 1174 | dest = kmalloc(sizeof(*dest), GFP_KERNEL); |
ce0d9f04 RGB |
1175 | if (!dest) |
1176 | return -ENOMEM; | |
3054d067 PM |
1177 | dest->net = get_net(sock_net(NETLINK_CB(request_skb).sk)); |
1178 | dest->portid = NETLINK_CB(request_skb).portid; | |
ce0d9f04 RGB |
1179 | skb_queue_head_init(&dest->q); |
1180 | ||
1181 | mutex_lock(&audit_filter_mutex); | |
45a0642b | 1182 | audit_list_rules(seq, &dest->q); |
ce0d9f04 RGB |
1183 | mutex_unlock(&audit_filter_mutex); |
1184 | ||
3054d067 | 1185 | tsk = kthread_run(audit_send_list_thread, dest, "audit_send_list"); |
ce0d9f04 RGB |
1186 | if (IS_ERR(tsk)) { |
1187 | skb_queue_purge(&dest->q); | |
3054d067 | 1188 | put_net(dest->net); |
ce0d9f04 | 1189 | kfree(dest); |
3054d067 | 1190 | return PTR_ERR(tsk); |
ce0d9f04 RGB |
1191 | } |
1192 | ||
3054d067 | 1193 | return 0; |
ce0d9f04 RGB |
1194 | } |
1195 | ||
5af75d8d | 1196 | int audit_comparator(u32 left, u32 op, u32 right) |
fe7752ba DW |
1197 | { |
1198 | switch (op) { | |
5af75d8d | 1199 | case Audit_equal: |
fe7752ba | 1200 | return (left == right); |
5af75d8d | 1201 | case Audit_not_equal: |
fe7752ba | 1202 | return (left != right); |
5af75d8d | 1203 | case Audit_lt: |
fe7752ba | 1204 | return (left < right); |
5af75d8d | 1205 | case Audit_le: |
fe7752ba | 1206 | return (left <= right); |
5af75d8d | 1207 | case Audit_gt: |
fe7752ba | 1208 | return (left > right); |
5af75d8d | 1209 | case Audit_ge: |
fe7752ba | 1210 | return (left >= right); |
5af75d8d | 1211 | case Audit_bitmask: |
74f2345b | 1212 | return (left & right); |
5af75d8d | 1213 | case Audit_bittest: |
74f2345b | 1214 | return ((left & right) == right); |
5af75d8d | 1215 | default: |
5af75d8d | 1216 | return 0; |
fe7752ba DW |
1217 | } |
1218 | } | |
1219 | ||
ca57ec0f EB |
1220 | int audit_uid_comparator(kuid_t left, u32 op, kuid_t right) |
1221 | { | |
1222 | switch (op) { | |
1223 | case Audit_equal: | |
1224 | return uid_eq(left, right); | |
1225 | case Audit_not_equal: | |
1226 | return !uid_eq(left, right); | |
1227 | case Audit_lt: | |
1228 | return uid_lt(left, right); | |
1229 | case Audit_le: | |
1230 | return uid_lte(left, right); | |
1231 | case Audit_gt: | |
1232 | return uid_gt(left, right); | |
1233 | case Audit_ge: | |
1234 | return uid_gte(left, right); | |
1235 | case Audit_bitmask: | |
1236 | case Audit_bittest: | |
1237 | default: | |
ca57ec0f EB |
1238 | return 0; |
1239 | } | |
1240 | } | |
1241 | ||
1242 | int audit_gid_comparator(kgid_t left, u32 op, kgid_t right) | |
1243 | { | |
1244 | switch (op) { | |
1245 | case Audit_equal: | |
1246 | return gid_eq(left, right); | |
1247 | case Audit_not_equal: | |
1248 | return !gid_eq(left, right); | |
1249 | case Audit_lt: | |
1250 | return gid_lt(left, right); | |
1251 | case Audit_le: | |
1252 | return gid_lte(left, right); | |
1253 | case Audit_gt: | |
1254 | return gid_gt(left, right); | |
1255 | case Audit_ge: | |
1256 | return gid_gte(left, right); | |
1257 | case Audit_bitmask: | |
1258 | case Audit_bittest: | |
1259 | default: | |
ca57ec0f EB |
1260 | return 0; |
1261 | } | |
1262 | } | |
1263 | ||
bfcec708 JL |
1264 | /** |
1265 | * parent_len - find the length of the parent portion of a pathname | |
1266 | * @path: pathname of which to determine length | |
1267 | */ | |
1268 | int parent_len(const char *path) | |
1269 | { | |
1270 | int plen; | |
1271 | const char *p; | |
1272 | ||
1273 | plen = strlen(path); | |
1274 | ||
1275 | if (plen == 0) | |
1276 | return plen; | |
1277 | ||
1278 | /* disregard trailing slashes */ | |
1279 | p = path + plen - 1; | |
1280 | while ((*p == '/') && (p > path)) | |
1281 | p--; | |
1282 | ||
1283 | /* walk backward until we find the next slash or hit beginning */ | |
1284 | while ((*p != '/') && (p > path)) | |
1285 | p--; | |
1286 | ||
1287 | /* did we find a slash? Then increment to include it in path */ | |
1288 | if (*p == '/') | |
1289 | p++; | |
1290 | ||
1291 | return p - path; | |
1292 | } | |
1293 | ||
e3d6b07b JL |
1294 | /** |
1295 | * audit_compare_dname_path - compare given dentry name with last component in | |
1296 | * given path. Return of 0 indicates a match. | |
1297 | * @dname: dentry name that we're comparing | |
1298 | * @path: full pathname that we're comparing | |
1299 | * @parentlen: length of the parent if known. Passing in AUDIT_NAME_FULL | |
1300 | * here indicates that we must compute this value. | |
1301 | */ | |
795d673a | 1302 | int audit_compare_dname_path(const struct qstr *dname, const char *path, int parentlen) |
f368c07d | 1303 | { |
e3d6b07b | 1304 | int dlen, pathlen; |
f368c07d AG |
1305 | const char *p; |
1306 | ||
795d673a | 1307 | dlen = dname->len; |
29e9a346 EP |
1308 | pathlen = strlen(path); |
1309 | if (pathlen < dlen) | |
f368c07d AG |
1310 | return 1; |
1311 | ||
e3d6b07b | 1312 | parentlen = parentlen == AUDIT_NAME_FULL ? parent_len(path) : parentlen; |
29e9a346 | 1313 | if (pathlen - parentlen != dlen) |
f368c07d | 1314 | return 1; |
29e9a346 EP |
1315 | |
1316 | p = path + parentlen; | |
fe7752ba | 1317 | |
795d673a | 1318 | return strncmp(p, dname->name, dlen); |
f368c07d | 1319 | } |
fe7752ba | 1320 | |
86b2efbe | 1321 | int audit_filter(int msgtype, unsigned int listtype) |
fe7752ba DW |
1322 | { |
1323 | struct audit_entry *e; | |
86b2efbe | 1324 | int ret = 1; /* Audit by default */ |
9ce34218 | 1325 | |
fe7752ba | 1326 | rcu_read_lock(); |
86b2efbe RGB |
1327 | list_for_each_entry_rcu(e, &audit_filter_list[listtype], list) { |
1328 | int i, result = 0; | |
fe7752ba | 1329 | |
93315ed6 AG |
1330 | for (i = 0; i < e->rule.field_count; i++) { |
1331 | struct audit_field *f = &e->rule.fields[i]; | |
86b2efbe | 1332 | pid_t pid; |
eb182194 | 1333 | struct lsmblob blob; |
86b2efbe RGB |
1334 | |
1335 | switch (f->type) { | |
1336 | case AUDIT_PID: | |
1337 | pid = task_pid_nr(current); | |
1338 | result = audit_comparator(pid, f->op, f->val); | |
1339 | break; | |
1340 | case AUDIT_UID: | |
1341 | result = audit_uid_comparator(current_uid(), f->op, f->uid); | |
1342 | break; | |
1343 | case AUDIT_GID: | |
1344 | result = audit_gid_comparator(current_gid(), f->op, f->gid); | |
1345 | break; | |
1346 | case AUDIT_LOGINUID: | |
1347 | result = audit_uid_comparator(audit_get_loginuid(current), | |
1348 | f->op, f->uid); | |
1349 | break; | |
1350 | case AUDIT_LOGINUID_SET: | |
1351 | result = audit_comparator(audit_loginuid_set(current), | |
1352 | f->op, f->val); | |
1353 | break; | |
1354 | case AUDIT_MSGTYPE: | |
1355 | result = audit_comparator(msgtype, f->op, f->val); | |
1356 | break; | |
1357 | case AUDIT_SUBJ_USER: | |
1358 | case AUDIT_SUBJ_ROLE: | |
1359 | case AUDIT_SUBJ_TYPE: | |
1360 | case AUDIT_SUBJ_SEN: | |
1361 | case AUDIT_SUBJ_CLR: | |
f17b27a2 | 1362 | if (f->lsm_isset) { |
e209474e | 1363 | security_task_getsecid_subj(current, &blob); |
eb182194 CS |
1364 | result = security_audit_rule_match( |
1365 | &blob, f->type, f->op, | |
f17b27a2 | 1366 | f->lsm_rules); |
86b2efbe RGB |
1367 | } |
1368 | break; | |
29c1372d OM |
1369 | case AUDIT_EXE: |
1370 | result = audit_exe_compare(current, e->rule.exe); | |
1371 | if (f->op == Audit_not_equal) | |
1372 | result = !result; | |
1373 | break; | |
86b2efbe RGB |
1374 | default: |
1375 | goto unlock_and_return; | |
fe7752ba | 1376 | } |
86b2efbe RGB |
1377 | if (result < 0) /* error */ |
1378 | goto unlock_and_return; | |
1379 | if (!result) | |
1380 | break; | |
1381 | } | |
1382 | if (result > 0) { | |
d904ac03 | 1383 | if (e->rule.action == AUDIT_NEVER || listtype == AUDIT_FILTER_EXCLUDE) |
86b2efbe RGB |
1384 | ret = 0; |
1385 | break; | |
fe7752ba | 1386 | } |
fe7752ba DW |
1387 | } |
1388 | unlock_and_return: | |
1389 | rcu_read_unlock(); | |
86b2efbe | 1390 | return ret; |
fe7752ba | 1391 | } |
3dc7e315 | 1392 | |
f17b27a2 | 1393 | static int update_lsm_rules(struct audit_krule *r) |
1a9d0797 | 1394 | { |
e45aa212 | 1395 | struct audit_entry *entry = container_of(r, struct audit_entry, rule); |
1a9d0797 | 1396 | struct audit_entry *nentry; |
1a9d0797 AV |
1397 | int err = 0; |
1398 | ||
e45aa212 | 1399 | if (!security_audit_rule_known(r)) |
1a9d0797 AV |
1400 | return 0; |
1401 | ||
ae7b8f41 | 1402 | nentry = audit_dupe_rule(r); |
34d99af5 RGB |
1403 | if (entry->rule.exe) |
1404 | audit_remove_mark(entry->rule.exe); | |
1a9d0797 AV |
1405 | if (IS_ERR(nentry)) { |
1406 | /* save the first error encountered for the | |
1407 | * return value */ | |
1408 | err = PTR_ERR(nentry); | |
1409 | audit_panic("error updating LSM filters"); | |
ae7b8f41 | 1410 | if (r->watch) |
e45aa212 | 1411 | list_del(&r->rlist); |
1a9d0797 | 1412 | list_del_rcu(&entry->list); |
e45aa212 | 1413 | list_del(&r->list); |
1a9d0797 | 1414 | } else { |
ae7b8f41 | 1415 | if (r->watch || r->tree) |
e45aa212 | 1416 | list_replace_init(&r->rlist, &nentry->rule.rlist); |
1a9d0797 | 1417 | list_replace_rcu(&entry->list, &nentry->list); |
e45aa212 | 1418 | list_replace(&r->list, &nentry->rule.list); |
1a9d0797 AV |
1419 | } |
1420 | call_rcu(&entry->rcu, audit_free_rule_rcu); | |
1421 | ||
1422 | return err; | |
1423 | } | |
1424 | ||
f17b27a2 | 1425 | /* This function will re-initialize the lsm_rules field of all applicable rules. |
d7a96f3a | 1426 | * It will traverse the filter lists serarching for rules that contain LSM |
3dc7e315 | 1427 | * specific filter fields. When such a rule is found, it is copied, the |
d7a96f3a | 1428 | * LSM field is re-initialized, and the old rule is replaced with the |
3dc7e315 | 1429 | * updated rule. */ |
d7a96f3a | 1430 | int audit_update_lsm_rules(void) |
3dc7e315 | 1431 | { |
e45aa212 | 1432 | struct audit_krule *r, *n; |
3dc7e315 DG |
1433 | int i, err = 0; |
1434 | ||
f368c07d AG |
1435 | /* audit_filter_mutex synchronizes the writers */ |
1436 | mutex_lock(&audit_filter_mutex); | |
3dc7e315 DG |
1437 | |
1438 | for (i = 0; i < AUDIT_NR_FILTERS; i++) { | |
e45aa212 | 1439 | list_for_each_entry_safe(r, n, &audit_rules_list[i], list) { |
f17b27a2 | 1440 | int res = update_lsm_rules(r); |
1a9d0797 AV |
1441 | if (!err) |
1442 | err = res; | |
3dc7e315 DG |
1443 | } |
1444 | } | |
f368c07d | 1445 | mutex_unlock(&audit_filter_mutex); |
3dc7e315 DG |
1446 | |
1447 | return err; | |
1448 | } |