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