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