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