random32: assign to network folks in MAINTAINERS
[linux/fpc-iii.git] / kernel / auditfilter.c
blob92062fd6cc8cec4deff933c04848782bfdcded11
1 /* auditfilter.c -- filtering of audit events
3 * Copyright 2003-2004 Red Hat, Inc.
4 * Copyright 2005 Hewlett-Packard Development Company, L.P.
5 * Copyright 2005 IBM Corporation
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.
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.
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
22 #include <linux/kernel.h>
23 #include <linux/audit.h>
24 #include <linux/kthread.h>
25 #include <linux/mutex.h>
26 #include <linux/fs.h>
27 #include <linux/namei.h>
28 #include <linux/netlink.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/security.h>
32 #include <net/net_namespace.h>
33 #include <net/sock.h>
34 #include "audit.h"
37 * Locking model:
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
43 * LSM rules during filtering. If modified, these structures
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.
49 /* Audit filter lists, defined in <linux/audit.h> */
50 struct 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
61 static 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]),
70 DEFINE_MUTEX(audit_filter_mutex);
72 static inline void audit_free_rule(struct audit_entry *e)
74 int i;
75 struct audit_krule *erule = &e->rule;
77 /* some rules don't have associated watches */
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];
83 kfree(f->lsm_str);
84 security_audit_rule_free(f->lsm_rule);
86 kfree(erule->fields);
87 kfree(erule->filterkey);
88 kfree(e);
91 void audit_free_rule_rcu(struct rcu_head *head)
93 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
94 audit_free_rule(e);
97 /* Initialize an audit filterlist entry. */
98 static inline struct audit_entry *audit_init_entry(u32 field_count)
100 struct audit_entry *entry;
101 struct audit_field *fields;
103 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
104 if (unlikely(!entry))
105 return NULL;
107 fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
108 if (unlikely(!fields)) {
109 kfree(entry);
110 return NULL;
112 entry->rule.fields = fields;
114 return entry;
117 /* Unpack a filter field's string representation from user-space
118 * buffer. */
119 char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
121 char *str;
123 if (!*bufp || (len == 0) || (len > *remain))
124 return ERR_PTR(-EINVAL);
126 /* Of the currently implemented string fields, PATH_MAX
127 * defines the longest valid length.
129 if (len > PATH_MAX)
130 return ERR_PTR(-ENAMETOOLONG);
132 str = kmalloc(len + 1, GFP_KERNEL);
133 if (unlikely(!str))
134 return ERR_PTR(-ENOMEM);
136 memcpy(str, *bufp, len);
137 str[len] = 0;
138 *bufp += len;
139 *remain -= len;
141 return str;
144 /* Translate an inode field to kernel respresentation. */
145 static inline int audit_to_inode(struct audit_krule *krule,
146 struct audit_field *f)
148 if (krule->listnr != AUDIT_FILTER_EXIT ||
149 krule->watch || krule->inode_f || krule->tree ||
150 (f->op != Audit_equal && f->op != Audit_not_equal))
151 return -EINVAL;
153 krule->inode_f = f;
154 return 0;
157 static __u32 *classes[AUDIT_SYSCALL_CLASSES];
159 int __init audit_register_class(int class, unsigned *list)
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;
170 p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
172 if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
173 kfree(p);
174 return -EINVAL;
176 classes[class] = p;
177 return 0;
180 int audit_match_class(int class, unsigned syscall)
182 if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32))
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);
189 #ifdef CONFIG_AUDITSYSCALL
190 static inline int audit_match_class_bits(int class, u32 *mask)
192 int i;
194 if (classes[class]) {
195 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
196 if (mask[i] & classes[class][i])
197 return 0;
199 return 1;
202 static int audit_match_signal(struct audit_entry *entry)
204 struct audit_field *arch = entry->rule.arch_f;
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));
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;
226 #endif
228 /* Common user-space to kernel rule translation. */
229 static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
231 unsigned listnr;
232 struct audit_entry *entry;
233 int i, err;
235 err = -EINVAL;
236 listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
237 switch(listnr) {
238 default:
239 goto exit_err;
240 #ifdef CONFIG_AUDITSYSCALL
241 case AUDIT_FILTER_ENTRY:
242 if (rule->action == AUDIT_ALWAYS)
243 goto exit_err;
244 case AUDIT_FILTER_EXIT:
245 case AUDIT_FILTER_TASK:
246 #endif
247 case AUDIT_FILTER_USER:
248 case AUDIT_FILTER_TYPE:
251 if (unlikely(rule->action == AUDIT_POSSIBLE)) {
252 printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
253 goto exit_err;
255 if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
256 goto exit_err;
257 if (rule->field_count > AUDIT_MAX_FIELDS)
258 goto exit_err;
260 err = -ENOMEM;
261 entry = audit_init_entry(rule->field_count);
262 if (!entry)
263 goto exit_err;
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;
270 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
271 entry->rule.mask[i] = rule->mask[i];
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;
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];
289 return entry;
291 exit_err:
292 return ERR_PTR(err);
295 static u32 audit_ops[] =
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,
307 static u32 audit_to_op(u32 op)
309 u32 n;
310 for (n = Audit_equal; n < Audit_bad && audit_ops[n] != op; n++)
312 return n;
315 /* check if an audit field is valid */
316 static int audit_field_valid(struct audit_entry *entry, struct audit_field *f)
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;
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:
348 case AUDIT_INODE:
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;
371 case AUDIT_LOGINUID_SET:
372 if ((f->val != 0) && (f->val != 1))
373 return -EINVAL;
374 /* FALL THROUGH */
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;
392 return 0;
395 /* Translate struct audit_rule_data to kernel's rule respresentation. */
396 static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
397 size_t datasz)
399 int err = 0;
400 struct audit_entry *entry;
401 void *bufp;
402 size_t remain = datasz - sizeof(struct audit_rule_data);
403 int i;
404 char *str;
406 entry = audit_to_entry_common((struct audit_rule *)data);
407 if (IS_ERR(entry))
408 goto exit_nofree;
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];
415 err = -EINVAL;
417 f->op = audit_to_op(data->fieldflags[i]);
418 if (f->op == Audit_bad)
419 goto exit_free;
421 f->type = data->fields[i];
422 f->val = data->values[i];
423 f->uid = INVALID_UID;
424 f->gid = INVALID_GID;
425 f->lsm_str = NULL;
426 f->lsm_rule = NULL;
428 /* Support legacy tests for a valid loginuid */
429 if ((f->type == AUDIT_LOGINUID) && (f->val == AUDIT_UID_UNSET)) {
430 f->type = AUDIT_LOGINUID_SET;
431 f->val = 0;
434 err = audit_field_valid(entry, f);
435 if (err)
436 goto exit_free;
438 err = -EINVAL;
439 switch (f->type) {
440 case AUDIT_LOGINUID:
441 case AUDIT_UID:
442 case AUDIT_EUID:
443 case AUDIT_SUID:
444 case AUDIT_FSUID:
445 case AUDIT_OBJ_UID:
446 f->uid = make_kuid(current_user_ns(), f->val);
447 if (!uid_valid(f->uid))
448 goto exit_free;
449 break;
450 case AUDIT_GID:
451 case AUDIT_EGID:
452 case AUDIT_SGID:
453 case AUDIT_FSGID:
454 case AUDIT_OBJ_GID:
455 f->gid = make_kgid(current_user_ns(), f->val);
456 if (!gid_valid(f->gid))
457 goto exit_free;
458 break;
459 case AUDIT_ARCH:
460 entry->rule.arch_f = f;
461 break;
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:
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:
472 str = audit_unpack_string(&bufp, &remain, f->val);
473 if (IS_ERR(str))
474 goto exit_free;
475 entry->rule.buflen += f->val;
477 err = security_audit_rule_init(f->type, f->op, str,
478 (void **)&f->lsm_rule);
479 /* Keep currently invalid fields around in case they
480 * become valid after a policy reload. */
481 if (err == -EINVAL) {
482 printk(KERN_WARNING "audit rule for LSM "
483 "\'%s\' is invalid\n", str);
484 err = 0;
486 if (err) {
487 kfree(str);
488 goto exit_free;
489 } else
490 f->lsm_str = str;
491 break;
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;
498 err = audit_to_watch(&entry->rule, str, f->val, f->op);
499 if (err) {
500 kfree(str);
501 goto exit_free;
503 break;
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;
510 err = audit_make_tree(&entry->rule, str, f->op);
511 kfree(str);
512 if (err)
513 goto exit_free;
514 break;
515 case AUDIT_INODE:
516 err = audit_to_inode(&entry->rule, f);
517 if (err)
518 goto exit_free;
519 break;
520 case AUDIT_FILTERKEY:
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;
532 if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal)
533 entry->rule.inode_f = NULL;
535 exit_nofree:
536 return entry;
538 exit_free:
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 */
543 audit_free_rule(entry);
544 return ERR_PTR(err);
547 /* Pack a filter field's string representation into data block. */
548 static inline size_t audit_pack_string(void **bufp, const char *str)
550 size_t len = strlen(str);
552 memcpy(*bufp, str, len);
553 *bufp += len;
555 return len;
558 /* Translate kernel rule respresentation to struct audit_rule_data. */
559 static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
561 struct audit_rule_data *data;
562 void *bufp;
563 int i;
565 data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
566 if (unlikely(!data))
567 return NULL;
568 memset(data, 0, sizeof(*data));
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];
577 data->fields[i] = f->type;
578 data->fieldflags[i] = audit_ops[f->op];
579 switch(f->type) {
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:
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:
590 data->buflen += data->values[i] =
591 audit_pack_string(&bufp, f->lsm_str);
592 break;
593 case AUDIT_WATCH:
594 data->buflen += data->values[i] =
595 audit_pack_string(&bufp,
596 audit_watch_path(krule->watch));
597 break;
598 case AUDIT_DIR:
599 data->buflen += data->values[i] =
600 audit_pack_string(&bufp,
601 audit_tree_path(krule->tree));
602 break;
603 case AUDIT_FILTERKEY:
604 data->buflen += data->values[i] =
605 audit_pack_string(&bufp, krule->filterkey);
606 break;
607 default:
608 data->values[i] = f->val;
611 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
613 return data;
616 /* Compare two rules in kernel format. Considered success if rules
617 * don't match. */
618 static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
620 int i;
622 if (a->flags != b->flags ||
623 a->listnr != b->listnr ||
624 a->action != b->action ||
625 a->field_count != b->field_count)
626 return 1;
628 for (i = 0; i < a->field_count; i++) {
629 if (a->fields[i].type != b->fields[i].type ||
630 a->fields[i].op != b->fields[i].op)
631 return 1;
633 switch(a->fields[i].type) {
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:
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:
644 if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
645 return 1;
646 break;
647 case AUDIT_WATCH:
648 if (strcmp(audit_watch_path(a->watch),
649 audit_watch_path(b->watch)))
650 return 1;
651 break;
652 case AUDIT_DIR:
653 if (strcmp(audit_tree_path(a->tree),
654 audit_tree_path(b->tree)))
655 return 1;
656 break;
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;
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;
679 default:
680 if (a->fields[i].val != b->fields[i].val)
681 return 1;
685 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
686 if (a->mask[i] != b->mask[i])
687 return 1;
689 return 0;
692 /* Duplicate LSM field information. The lsm_rule is opaque, so must be
693 * re-initialized. */
694 static inline int audit_dupe_lsm_field(struct audit_field *df,
695 struct audit_field *sf)
697 int ret = 0;
698 char *lsm_str;
700 /* our own copy of lsm_str */
701 lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
702 if (unlikely(!lsm_str))
703 return -ENOMEM;
704 df->lsm_str = lsm_str;
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);
709 /* Keep currently invalid fields around in case they
710 * become valid after a policy reload. */
711 if (ret == -EINVAL) {
712 printk(KERN_WARNING "audit rule for LSM \'%s\' is "
713 "invalid\n", df->lsm_str);
714 ret = 0;
717 return ret;
720 /* Duplicate an audit rule. This will be a deep copy with the exception
721 * of the watch - that pointer is carried over. The LSM specific fields
722 * will be updated in the copy. The point is to be able to replace the old
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. */
726 struct audit_entry *audit_dupe_rule(struct audit_krule *old)
728 u32 fcount = old->field_count;
729 struct audit_entry *entry;
730 struct audit_krule *new;
731 char *fk;
732 int i, err = 0;
734 entry = audit_init_entry(fcount);
735 if (unlikely(!entry))
736 return ERR_PTR(-ENOMEM);
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];
745 new->prio = old->prio;
746 new->buflen = old->buflen;
747 new->inode_f = old->inode_f;
748 new->field_count = old->field_count;
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.
757 new->tree = old->tree;
758 memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
760 /* deep copy this information, updating the lsm_rule fields, because
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) {
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:
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:
774 err = audit_dupe_lsm_field(&new->fields[i],
775 &old->fields[i]);
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;
784 if (err) {
785 audit_free_rule(entry);
786 return ERR_PTR(err);
790 if (old->watch) {
791 audit_get_watch(old->watch);
792 new->watch = old->watch;
795 return entry;
798 /* Find an existing audit rule.
799 * Caller must hold audit_filter_mutex to prevent stale rule data. */
800 static struct audit_entry *audit_find_rule(struct audit_entry *entry,
801 struct list_head **p)
803 struct audit_entry *e, *found = NULL;
804 struct list_head *list;
805 int h;
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) {
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;
820 goto out;
821 } else {
822 *p = list = &audit_filter_list[entry->rule.listnr];
825 list_for_each_entry(e, list, list)
826 if (!audit_compare_rule(&entry->rule, &e->rule)) {
827 found = e;
828 goto out;
831 out:
832 return found;
835 static u64 prio_low = ~0ULL/2;
836 static u64 prio_high = ~0ULL/2 - 1;
838 /* Add rule to given filterlist if not a duplicate. */
839 static inline int audit_add_rule(struct audit_entry *entry)
841 struct audit_entry *e;
842 struct audit_watch *watch = entry->rule.watch;
843 struct audit_tree *tree = entry->rule.tree;
844 struct list_head *list;
845 int err;
846 #ifdef CONFIG_AUDITSYSCALL
847 int dont_count = 0;
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
855 mutex_lock(&audit_filter_mutex);
856 e = audit_find_rule(entry, &list);
857 if (e) {
858 mutex_unlock(&audit_filter_mutex);
859 err = -EEXIST;
860 /* normally audit_add_tree_rule() will free it on failure */
861 if (tree)
862 audit_put_tree(tree);
863 goto error;
866 if (watch) {
867 /* audit_filter_mutex is dropped and re-taken during this call */
868 err = audit_add_watch(&entry->rule, &list);
869 if (err) {
870 mutex_unlock(&audit_filter_mutex);
872 * normally audit_add_tree_rule() will free it
873 * on failure
875 if (tree)
876 audit_put_tree(tree);
877 goto error;
880 if (tree) {
881 err = audit_add_tree_rule(&entry->rule);
882 if (err) {
883 mutex_unlock(&audit_filter_mutex);
884 goto error;
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;
896 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
897 list_add(&entry->rule.list,
898 &audit_rules_list[entry->rule.listnr]);
899 list_add_rcu(&entry->list, list);
900 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
901 } else {
902 list_add_tail(&entry->rule.list,
903 &audit_rules_list[entry->rule.listnr]);
904 list_add_tail_rcu(&entry->list, list);
906 #ifdef CONFIG_AUDITSYSCALL
907 if (!dont_count)
908 audit_n_rules++;
910 if (!audit_match_signal(entry))
911 audit_signals++;
912 #endif
913 mutex_unlock(&audit_filter_mutex);
915 return 0;
917 error:
918 if (watch)
919 audit_put_watch(watch); /* tmp watch, matches initial get */
920 return err;
923 /* Remove an existing rule from filterlist. */
924 static inline int audit_del_rule(struct audit_entry *entry)
926 struct audit_entry *e;
927 struct audit_watch *watch = entry->rule.watch;
928 struct audit_tree *tree = entry->rule.tree;
929 struct list_head *list;
930 int ret = 0;
931 #ifdef CONFIG_AUDITSYSCALL
932 int dont_count = 0;
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
940 mutex_lock(&audit_filter_mutex);
941 e = audit_find_rule(entry, &list);
942 if (!e) {
943 mutex_unlock(&audit_filter_mutex);
944 ret = -ENOENT;
945 goto out;
948 if (e->rule.watch)
949 audit_remove_watch_rule(&e->rule);
951 if (e->rule.tree)
952 audit_remove_tree_rule(&e->rule);
954 list_del_rcu(&e->list);
955 list_del(&e->rule.list);
956 call_rcu(&e->rcu, audit_free_rule_rcu);
958 #ifdef CONFIG_AUDITSYSCALL
959 if (!dont_count)
960 audit_n_rules--;
962 if (!audit_match_signal(entry))
963 audit_signals--;
964 #endif
965 mutex_unlock(&audit_filter_mutex);
967 out:
968 if (watch)
969 audit_put_watch(watch); /* match initial get */
970 if (tree)
971 audit_put_tree(tree); /* that's the temporary one */
973 return ret;
976 /* List rules using struct audit_rule_data. */
977 static void audit_list_rules(__u32 portid, int seq, struct sk_buff_head *q)
979 struct sk_buff *skb;
980 struct audit_krule *r;
981 int i;
983 /* This is a blocking read, so use audit_filter_mutex instead of rcu
984 * iterator to sync with list writers. */
985 for (i=0; i<AUDIT_NR_FILTERS; i++) {
986 list_for_each_entry(r, &audit_rules_list[i], list) {
987 struct audit_rule_data *data;
989 data = audit_krule_to_data(r);
990 if (unlikely(!data))
991 break;
992 skb = audit_make_reply(portid, seq, AUDIT_LIST_RULES,
993 0, 1, data,
994 sizeof(*data) + data->buflen);
995 if (skb)
996 skb_queue_tail(q, skb);
997 kfree(data);
1000 skb = audit_make_reply(portid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
1001 if (skb)
1002 skb_queue_tail(q, skb);
1005 /* Log rule additions and removals */
1006 static void audit_log_rule_change(char *action, struct audit_krule *rule, int res)
1008 struct audit_buffer *ab;
1009 uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(current));
1010 unsigned int sessionid = audit_get_sessionid(current);
1012 if (!audit_enabled)
1013 return;
1015 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
1016 if (!ab)
1017 return;
1018 audit_log_format(ab, "auid=%u ses=%u" ,loginuid, sessionid);
1019 audit_log_task_context(ab);
1020 audit_log_format(ab, " op=");
1021 audit_log_string(ab, action);
1022 audit_log_key(ab, rule->filterkey);
1023 audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
1024 audit_log_end(ab);
1028 * audit_rule_change - apply all rules to the specified message type
1029 * @type: audit message type
1030 * @portid: target port id for netlink audit messages
1031 * @seq: netlink audit message sequence (serial) number
1032 * @data: payload data
1033 * @datasz: size of payload data
1035 int audit_rule_change(int type, __u32 portid, int seq, void *data,
1036 size_t datasz)
1038 int err = 0;
1039 struct audit_entry *entry;
1041 switch (type) {
1042 case AUDIT_ADD_RULE:
1043 entry = audit_data_to_entry(data, datasz);
1044 if (IS_ERR(entry))
1045 return PTR_ERR(entry);
1047 err = audit_add_rule(entry);
1048 audit_log_rule_change("add rule", &entry->rule, !err);
1049 if (err)
1050 audit_free_rule(entry);
1051 break;
1052 case AUDIT_DEL_RULE:
1053 entry = audit_data_to_entry(data, datasz);
1054 if (IS_ERR(entry))
1055 return PTR_ERR(entry);
1057 err = audit_del_rule(entry);
1058 audit_log_rule_change("remove rule", &entry->rule, !err);
1059 audit_free_rule(entry);
1060 break;
1061 default:
1062 return -EINVAL;
1065 return err;
1069 * audit_list_rules_send - list the audit rules
1070 * @request_skb: skb of request we are replying to (used to target the reply)
1071 * @seq: netlink audit message sequence (serial) number
1073 int audit_list_rules_send(struct sk_buff *request_skb, int seq)
1075 u32 portid = NETLINK_CB(request_skb).portid;
1076 struct net *net = sock_net(NETLINK_CB(request_skb).sk);
1077 struct task_struct *tsk;
1078 struct audit_netlink_list *dest;
1079 int err = 0;
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 */
1087 dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
1088 if (!dest)
1089 return -ENOMEM;
1090 dest->net = get_net(net);
1091 dest->portid = portid;
1092 skb_queue_head_init(&dest->q);
1094 mutex_lock(&audit_filter_mutex);
1095 audit_list_rules(portid, seq, &dest->q);
1096 mutex_unlock(&audit_filter_mutex);
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);
1105 return err;
1108 int audit_comparator(u32 left, u32 op, u32 right)
1110 switch (op) {
1111 case Audit_equal:
1112 return (left == right);
1113 case Audit_not_equal:
1114 return (left != right);
1115 case Audit_lt:
1116 return (left < right);
1117 case Audit_le:
1118 return (left <= right);
1119 case Audit_gt:
1120 return (left > right);
1121 case Audit_ge:
1122 return (left >= right);
1123 case Audit_bitmask:
1124 return (left & right);
1125 case Audit_bittest:
1126 return ((left & right) == right);
1127 default:
1128 BUG();
1129 return 0;
1133 int audit_uid_comparator(kuid_t left, u32 op, kuid_t right)
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;
1156 int audit_gid_comparator(kgid_t left, u32 op, kgid_t right)
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;
1180 * parent_len - find the length of the parent portion of a pathname
1181 * @path: pathname of which to determine length
1183 int parent_len(const char *path)
1185 int plen;
1186 const char *p;
1188 plen = strlen(path);
1190 if (plen == 0)
1191 return plen;
1193 /* disregard trailing slashes */
1194 p = path + plen - 1;
1195 while ((*p == '/') && (p > path))
1196 p--;
1198 /* walk backward until we find the next slash or hit beginning */
1199 while ((*p != '/') && (p > path))
1200 p--;
1202 /* did we find a slash? Then increment to include it in path */
1203 if (*p == '/')
1204 p++;
1206 return p - path;
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.
1217 int audit_compare_dname_path(const char *dname, const char *path, int parentlen)
1219 int dlen, pathlen;
1220 const char *p;
1222 dlen = strlen(dname);
1223 pathlen = strlen(path);
1224 if (pathlen < dlen)
1225 return 1;
1227 parentlen = parentlen == AUDIT_NAME_FULL ? parent_len(path) : parentlen;
1228 if (pathlen - parentlen != dlen)
1229 return 1;
1231 p = path + parentlen;
1233 return strncmp(p, dname, dlen);
1236 static int audit_filter_user_rules(struct audit_krule *rule, int type,
1237 enum audit_state *state)
1239 int i;
1241 for (i = 0; i < rule->field_count; i++) {
1242 struct audit_field *f = &rule->fields[i];
1243 int result = 0;
1244 u32 sid;
1246 switch (f->type) {
1247 case AUDIT_PID:
1248 result = audit_comparator(task_pid_vnr(current), f->op, f->val);
1249 break;
1250 case AUDIT_UID:
1251 result = audit_uid_comparator(current_uid(), f->op, f->uid);
1252 break;
1253 case AUDIT_GID:
1254 result = audit_gid_comparator(current_gid(), f->op, f->gid);
1255 break;
1256 case AUDIT_LOGINUID:
1257 result = audit_uid_comparator(audit_get_loginuid(current),
1258 f->op, f->uid);
1259 break;
1260 case AUDIT_LOGINUID_SET:
1261 result = audit_comparator(audit_loginuid_set(current),
1262 f->op, f->val);
1263 break;
1264 case AUDIT_MSGTYPE:
1265 result = audit_comparator(type, f->op, f->val);
1266 break;
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:
1272 if (f->lsm_rule) {
1273 security_task_getsecid(current, &sid);
1274 result = security_audit_rule_match(sid,
1275 f->type,
1276 f->op,
1277 f->lsm_rule,
1278 NULL);
1280 break;
1283 if (!result)
1284 return 0;
1286 switch (rule->action) {
1287 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
1288 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
1290 return 1;
1293 int audit_filter_user(int type)
1295 enum audit_state state = AUDIT_DISABLED;
1296 struct audit_entry *e;
1297 int rc, ret;
1299 ret = 1; /* Audit by default */
1301 rcu_read_lock();
1302 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
1303 rc = audit_filter_user_rules(&e->rule, type, &state);
1304 if (rc) {
1305 if (rc > 0 && state == AUDIT_DISABLED)
1306 ret = 0;
1307 break;
1310 rcu_read_unlock();
1312 return ret;
1315 int audit_filter_type(int type)
1317 struct audit_entry *e;
1318 int result = 0;
1320 rcu_read_lock();
1321 if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
1322 goto unlock_and_return;
1324 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
1325 list) {
1326 int i;
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);
1331 if (!result)
1332 break;
1335 if (result)
1336 goto unlock_and_return;
1338 unlock_and_return:
1339 rcu_read_unlock();
1340 return result;
1343 static int update_lsm_rule(struct audit_krule *r)
1345 struct audit_entry *entry = container_of(r, struct audit_entry, rule);
1346 struct audit_entry *nentry;
1347 int err = 0;
1349 if (!security_audit_rule_known(r))
1350 return 0;
1352 nentry = audit_dupe_rule(r);
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");
1358 if (r->watch)
1359 list_del(&r->rlist);
1360 list_del_rcu(&entry->list);
1361 list_del(&r->list);
1362 } else {
1363 if (r->watch || r->tree)
1364 list_replace_init(&r->rlist, &nentry->rule.rlist);
1365 list_replace_rcu(&entry->list, &nentry->list);
1366 list_replace(&r->list, &nentry->rule.list);
1368 call_rcu(&entry->rcu, audit_free_rule_rcu);
1370 return err;
1373 /* This function will re-initialize the lsm_rule field of all applicable rules.
1374 * It will traverse the filter lists serarching for rules that contain LSM
1375 * specific filter fields. When such a rule is found, it is copied, the
1376 * LSM field is re-initialized, and the old rule is replaced with the
1377 * updated rule. */
1378 int audit_update_lsm_rules(void)
1380 struct audit_krule *r, *n;
1381 int i, err = 0;
1383 /* audit_filter_mutex synchronizes the writers */
1384 mutex_lock(&audit_filter_mutex);
1386 for (i = 0; i < AUDIT_NR_FILTERS; i++) {
1387 list_for_each_entry_safe(r, n, &audit_rules_list[i], list) {
1388 int res = update_lsm_rule(r);
1389 if (!err)
1390 err = res;
1393 mutex_unlock(&audit_filter_mutex);
1395 return err;