iscsi,iser-target: Initiate termination only once
[linux/fpc-iii.git] / kernel / auditfilter.c
blob598c1dcf26dda85292831be2d700e3430909c239
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;
432 entry->rule.pflags |= AUDIT_LOGINUID_LEGACY;
435 err = audit_field_valid(entry, f);
436 if (err)
437 goto exit_free;
439 err = -EINVAL;
440 switch (f->type) {
441 case AUDIT_LOGINUID:
442 case AUDIT_UID:
443 case AUDIT_EUID:
444 case AUDIT_SUID:
445 case AUDIT_FSUID:
446 case AUDIT_OBJ_UID:
447 f->uid = make_kuid(current_user_ns(), f->val);
448 if (!uid_valid(f->uid))
449 goto exit_free;
450 break;
451 case AUDIT_GID:
452 case AUDIT_EGID:
453 case AUDIT_SGID:
454 case AUDIT_FSGID:
455 case AUDIT_OBJ_GID:
456 f->gid = make_kgid(current_user_ns(), f->val);
457 if (!gid_valid(f->gid))
458 goto exit_free;
459 break;
460 case AUDIT_ARCH:
461 entry->rule.arch_f = f;
462 break;
463 case AUDIT_SUBJ_USER:
464 case AUDIT_SUBJ_ROLE:
465 case AUDIT_SUBJ_TYPE:
466 case AUDIT_SUBJ_SEN:
467 case AUDIT_SUBJ_CLR:
468 case AUDIT_OBJ_USER:
469 case AUDIT_OBJ_ROLE:
470 case AUDIT_OBJ_TYPE:
471 case AUDIT_OBJ_LEV_LOW:
472 case AUDIT_OBJ_LEV_HIGH:
473 str = audit_unpack_string(&bufp, &remain, f->val);
474 if (IS_ERR(str))
475 goto exit_free;
476 entry->rule.buflen += f->val;
478 err = security_audit_rule_init(f->type, f->op, str,
479 (void **)&f->lsm_rule);
480 /* Keep currently invalid fields around in case they
481 * become valid after a policy reload. */
482 if (err == -EINVAL) {
483 printk(KERN_WARNING "audit rule for LSM "
484 "\'%s\' is invalid\n", str);
485 err = 0;
487 if (err) {
488 kfree(str);
489 goto exit_free;
490 } else
491 f->lsm_str = str;
492 break;
493 case AUDIT_WATCH:
494 str = audit_unpack_string(&bufp, &remain, f->val);
495 if (IS_ERR(str))
496 goto exit_free;
497 entry->rule.buflen += f->val;
499 err = audit_to_watch(&entry->rule, str, f->val, f->op);
500 if (err) {
501 kfree(str);
502 goto exit_free;
504 break;
505 case AUDIT_DIR:
506 str = audit_unpack_string(&bufp, &remain, f->val);
507 if (IS_ERR(str))
508 goto exit_free;
509 entry->rule.buflen += f->val;
511 err = audit_make_tree(&entry->rule, str, f->op);
512 kfree(str);
513 if (err)
514 goto exit_free;
515 break;
516 case AUDIT_INODE:
517 err = audit_to_inode(&entry->rule, f);
518 if (err)
519 goto exit_free;
520 break;
521 case AUDIT_FILTERKEY:
522 if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
523 goto exit_free;
524 str = audit_unpack_string(&bufp, &remain, f->val);
525 if (IS_ERR(str))
526 goto exit_free;
527 entry->rule.buflen += f->val;
528 entry->rule.filterkey = str;
529 break;
533 if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal)
534 entry->rule.inode_f = NULL;
536 exit_nofree:
537 return entry;
539 exit_free:
540 if (entry->rule.watch)
541 audit_put_watch(entry->rule.watch); /* matches initial get */
542 if (entry->rule.tree)
543 audit_put_tree(entry->rule.tree); /* that's the temporary one */
544 audit_free_rule(entry);
545 return ERR_PTR(err);
548 /* Pack a filter field's string representation into data block. */
549 static inline size_t audit_pack_string(void **bufp, const char *str)
551 size_t len = strlen(str);
553 memcpy(*bufp, str, len);
554 *bufp += len;
556 return len;
559 /* Translate kernel rule respresentation to struct audit_rule_data. */
560 static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
562 struct audit_rule_data *data;
563 void *bufp;
564 int i;
566 data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
567 if (unlikely(!data))
568 return NULL;
569 memset(data, 0, sizeof(*data));
571 data->flags = krule->flags | krule->listnr;
572 data->action = krule->action;
573 data->field_count = krule->field_count;
574 bufp = data->buf;
575 for (i = 0; i < data->field_count; i++) {
576 struct audit_field *f = &krule->fields[i];
578 data->fields[i] = f->type;
579 data->fieldflags[i] = audit_ops[f->op];
580 switch(f->type) {
581 case AUDIT_SUBJ_USER:
582 case AUDIT_SUBJ_ROLE:
583 case AUDIT_SUBJ_TYPE:
584 case AUDIT_SUBJ_SEN:
585 case AUDIT_SUBJ_CLR:
586 case AUDIT_OBJ_USER:
587 case AUDIT_OBJ_ROLE:
588 case AUDIT_OBJ_TYPE:
589 case AUDIT_OBJ_LEV_LOW:
590 case AUDIT_OBJ_LEV_HIGH:
591 data->buflen += data->values[i] =
592 audit_pack_string(&bufp, f->lsm_str);
593 break;
594 case AUDIT_WATCH:
595 data->buflen += data->values[i] =
596 audit_pack_string(&bufp,
597 audit_watch_path(krule->watch));
598 break;
599 case AUDIT_DIR:
600 data->buflen += data->values[i] =
601 audit_pack_string(&bufp,
602 audit_tree_path(krule->tree));
603 break;
604 case AUDIT_FILTERKEY:
605 data->buflen += data->values[i] =
606 audit_pack_string(&bufp, krule->filterkey);
607 break;
608 case AUDIT_LOGINUID_SET:
609 if (krule->pflags & AUDIT_LOGINUID_LEGACY && !f->val) {
610 data->fields[i] = AUDIT_LOGINUID;
611 data->values[i] = AUDIT_UID_UNSET;
612 break;
614 /* fallthrough if set */
615 default:
616 data->values[i] = f->val;
619 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
621 return data;
624 /* Compare two rules in kernel format. Considered success if rules
625 * don't match. */
626 static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
628 int i;
630 if (a->flags != b->flags ||
631 a->pflags != b->pflags ||
632 a->listnr != b->listnr ||
633 a->action != b->action ||
634 a->field_count != b->field_count)
635 return 1;
637 for (i = 0; i < a->field_count; i++) {
638 if (a->fields[i].type != b->fields[i].type ||
639 a->fields[i].op != b->fields[i].op)
640 return 1;
642 switch(a->fields[i].type) {
643 case AUDIT_SUBJ_USER:
644 case AUDIT_SUBJ_ROLE:
645 case AUDIT_SUBJ_TYPE:
646 case AUDIT_SUBJ_SEN:
647 case AUDIT_SUBJ_CLR:
648 case AUDIT_OBJ_USER:
649 case AUDIT_OBJ_ROLE:
650 case AUDIT_OBJ_TYPE:
651 case AUDIT_OBJ_LEV_LOW:
652 case AUDIT_OBJ_LEV_HIGH:
653 if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
654 return 1;
655 break;
656 case AUDIT_WATCH:
657 if (strcmp(audit_watch_path(a->watch),
658 audit_watch_path(b->watch)))
659 return 1;
660 break;
661 case AUDIT_DIR:
662 if (strcmp(audit_tree_path(a->tree),
663 audit_tree_path(b->tree)))
664 return 1;
665 break;
666 case AUDIT_FILTERKEY:
667 /* both filterkeys exist based on above type compare */
668 if (strcmp(a->filterkey, b->filterkey))
669 return 1;
670 break;
671 case AUDIT_UID:
672 case AUDIT_EUID:
673 case AUDIT_SUID:
674 case AUDIT_FSUID:
675 case AUDIT_LOGINUID:
676 case AUDIT_OBJ_UID:
677 if (!uid_eq(a->fields[i].uid, b->fields[i].uid))
678 return 1;
679 break;
680 case AUDIT_GID:
681 case AUDIT_EGID:
682 case AUDIT_SGID:
683 case AUDIT_FSGID:
684 case AUDIT_OBJ_GID:
685 if (!gid_eq(a->fields[i].gid, b->fields[i].gid))
686 return 1;
687 break;
688 default:
689 if (a->fields[i].val != b->fields[i].val)
690 return 1;
694 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
695 if (a->mask[i] != b->mask[i])
696 return 1;
698 return 0;
701 /* Duplicate LSM field information. The lsm_rule is opaque, so must be
702 * re-initialized. */
703 static inline int audit_dupe_lsm_field(struct audit_field *df,
704 struct audit_field *sf)
706 int ret = 0;
707 char *lsm_str;
709 /* our own copy of lsm_str */
710 lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
711 if (unlikely(!lsm_str))
712 return -ENOMEM;
713 df->lsm_str = lsm_str;
715 /* our own (refreshed) copy of lsm_rule */
716 ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
717 (void **)&df->lsm_rule);
718 /* Keep currently invalid fields around in case they
719 * become valid after a policy reload. */
720 if (ret == -EINVAL) {
721 printk(KERN_WARNING "audit rule for LSM \'%s\' is "
722 "invalid\n", df->lsm_str);
723 ret = 0;
726 return ret;
729 /* Duplicate an audit rule. This will be a deep copy with the exception
730 * of the watch - that pointer is carried over. The LSM specific fields
731 * will be updated in the copy. The point is to be able to replace the old
732 * rule with the new rule in the filterlist, then free the old rule.
733 * The rlist element is undefined; list manipulations are handled apart from
734 * the initial copy. */
735 struct audit_entry *audit_dupe_rule(struct audit_krule *old)
737 u32 fcount = old->field_count;
738 struct audit_entry *entry;
739 struct audit_krule *new;
740 char *fk;
741 int i, err = 0;
743 entry = audit_init_entry(fcount);
744 if (unlikely(!entry))
745 return ERR_PTR(-ENOMEM);
747 new = &entry->rule;
748 new->vers_ops = old->vers_ops;
749 new->flags = old->flags;
750 new->pflags = old->pflags;
751 new->listnr = old->listnr;
752 new->action = old->action;
753 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
754 new->mask[i] = old->mask[i];
755 new->prio = old->prio;
756 new->buflen = old->buflen;
757 new->inode_f = old->inode_f;
758 new->field_count = old->field_count;
761 * note that we are OK with not refcounting here; audit_match_tree()
762 * never dereferences tree and we can't get false positives there
763 * since we'd have to have rule gone from the list *and* removed
764 * before the chunks found by lookup had been allocated, i.e. before
765 * the beginning of list scan.
767 new->tree = old->tree;
768 memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
770 /* deep copy this information, updating the lsm_rule fields, because
771 * the originals will all be freed when the old rule is freed. */
772 for (i = 0; i < fcount; i++) {
773 switch (new->fields[i].type) {
774 case AUDIT_SUBJ_USER:
775 case AUDIT_SUBJ_ROLE:
776 case AUDIT_SUBJ_TYPE:
777 case AUDIT_SUBJ_SEN:
778 case AUDIT_SUBJ_CLR:
779 case AUDIT_OBJ_USER:
780 case AUDIT_OBJ_ROLE:
781 case AUDIT_OBJ_TYPE:
782 case AUDIT_OBJ_LEV_LOW:
783 case AUDIT_OBJ_LEV_HIGH:
784 err = audit_dupe_lsm_field(&new->fields[i],
785 &old->fields[i]);
786 break;
787 case AUDIT_FILTERKEY:
788 fk = kstrdup(old->filterkey, GFP_KERNEL);
789 if (unlikely(!fk))
790 err = -ENOMEM;
791 else
792 new->filterkey = fk;
794 if (err) {
795 audit_free_rule(entry);
796 return ERR_PTR(err);
800 if (old->watch) {
801 audit_get_watch(old->watch);
802 new->watch = old->watch;
805 return entry;
808 /* Find an existing audit rule.
809 * Caller must hold audit_filter_mutex to prevent stale rule data. */
810 static struct audit_entry *audit_find_rule(struct audit_entry *entry,
811 struct list_head **p)
813 struct audit_entry *e, *found = NULL;
814 struct list_head *list;
815 int h;
817 if (entry->rule.inode_f) {
818 h = audit_hash_ino(entry->rule.inode_f->val);
819 *p = list = &audit_inode_hash[h];
820 } else if (entry->rule.watch) {
821 /* we don't know the inode number, so must walk entire hash */
822 for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
823 list = &audit_inode_hash[h];
824 list_for_each_entry(e, list, list)
825 if (!audit_compare_rule(&entry->rule, &e->rule)) {
826 found = e;
827 goto out;
830 goto out;
831 } else {
832 *p = list = &audit_filter_list[entry->rule.listnr];
835 list_for_each_entry(e, list, list)
836 if (!audit_compare_rule(&entry->rule, &e->rule)) {
837 found = e;
838 goto out;
841 out:
842 return found;
845 static u64 prio_low = ~0ULL/2;
846 static u64 prio_high = ~0ULL/2 - 1;
848 /* Add rule to given filterlist if not a duplicate. */
849 static inline int audit_add_rule(struct audit_entry *entry)
851 struct audit_entry *e;
852 struct audit_watch *watch = entry->rule.watch;
853 struct audit_tree *tree = entry->rule.tree;
854 struct list_head *list;
855 int err;
856 #ifdef CONFIG_AUDITSYSCALL
857 int dont_count = 0;
859 /* If either of these, don't count towards total */
860 if (entry->rule.listnr == AUDIT_FILTER_USER ||
861 entry->rule.listnr == AUDIT_FILTER_TYPE)
862 dont_count = 1;
863 #endif
865 mutex_lock(&audit_filter_mutex);
866 e = audit_find_rule(entry, &list);
867 if (e) {
868 mutex_unlock(&audit_filter_mutex);
869 err = -EEXIST;
870 /* normally audit_add_tree_rule() will free it on failure */
871 if (tree)
872 audit_put_tree(tree);
873 goto error;
876 if (watch) {
877 /* audit_filter_mutex is dropped and re-taken during this call */
878 err = audit_add_watch(&entry->rule, &list);
879 if (err) {
880 mutex_unlock(&audit_filter_mutex);
882 * normally audit_add_tree_rule() will free it
883 * on failure
885 if (tree)
886 audit_put_tree(tree);
887 goto error;
890 if (tree) {
891 err = audit_add_tree_rule(&entry->rule);
892 if (err) {
893 mutex_unlock(&audit_filter_mutex);
894 goto error;
898 entry->rule.prio = ~0ULL;
899 if (entry->rule.listnr == AUDIT_FILTER_EXIT) {
900 if (entry->rule.flags & AUDIT_FILTER_PREPEND)
901 entry->rule.prio = ++prio_high;
902 else
903 entry->rule.prio = --prio_low;
906 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
907 list_add(&entry->rule.list,
908 &audit_rules_list[entry->rule.listnr]);
909 list_add_rcu(&entry->list, list);
910 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
911 } else {
912 list_add_tail(&entry->rule.list,
913 &audit_rules_list[entry->rule.listnr]);
914 list_add_tail_rcu(&entry->list, list);
916 #ifdef CONFIG_AUDITSYSCALL
917 if (!dont_count)
918 audit_n_rules++;
920 if (!audit_match_signal(entry))
921 audit_signals++;
922 #endif
923 mutex_unlock(&audit_filter_mutex);
925 return 0;
927 error:
928 if (watch)
929 audit_put_watch(watch); /* tmp watch, matches initial get */
930 return err;
933 /* Remove an existing rule from filterlist. */
934 static inline int audit_del_rule(struct audit_entry *entry)
936 struct audit_entry *e;
937 struct audit_watch *watch = entry->rule.watch;
938 struct audit_tree *tree = entry->rule.tree;
939 struct list_head *list;
940 int ret = 0;
941 #ifdef CONFIG_AUDITSYSCALL
942 int dont_count = 0;
944 /* If either of these, don't count towards total */
945 if (entry->rule.listnr == AUDIT_FILTER_USER ||
946 entry->rule.listnr == AUDIT_FILTER_TYPE)
947 dont_count = 1;
948 #endif
950 mutex_lock(&audit_filter_mutex);
951 e = audit_find_rule(entry, &list);
952 if (!e) {
953 mutex_unlock(&audit_filter_mutex);
954 ret = -ENOENT;
955 goto out;
958 if (e->rule.watch)
959 audit_remove_watch_rule(&e->rule);
961 if (e->rule.tree)
962 audit_remove_tree_rule(&e->rule);
964 list_del_rcu(&e->list);
965 list_del(&e->rule.list);
966 call_rcu(&e->rcu, audit_free_rule_rcu);
968 #ifdef CONFIG_AUDITSYSCALL
969 if (!dont_count)
970 audit_n_rules--;
972 if (!audit_match_signal(entry))
973 audit_signals--;
974 #endif
975 mutex_unlock(&audit_filter_mutex);
977 out:
978 if (watch)
979 audit_put_watch(watch); /* match initial get */
980 if (tree)
981 audit_put_tree(tree); /* that's the temporary one */
983 return ret;
986 /* List rules using struct audit_rule_data. */
987 static void audit_list_rules(__u32 portid, int seq, struct sk_buff_head *q)
989 struct sk_buff *skb;
990 struct audit_krule *r;
991 int i;
993 /* This is a blocking read, so use audit_filter_mutex instead of rcu
994 * iterator to sync with list writers. */
995 for (i=0; i<AUDIT_NR_FILTERS; i++) {
996 list_for_each_entry(r, &audit_rules_list[i], list) {
997 struct audit_rule_data *data;
999 data = audit_krule_to_data(r);
1000 if (unlikely(!data))
1001 break;
1002 skb = audit_make_reply(portid, seq, AUDIT_LIST_RULES,
1003 0, 1, data,
1004 sizeof(*data) + data->buflen);
1005 if (skb)
1006 skb_queue_tail(q, skb);
1007 kfree(data);
1010 skb = audit_make_reply(portid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
1011 if (skb)
1012 skb_queue_tail(q, skb);
1015 /* Log rule additions and removals */
1016 static void audit_log_rule_change(char *action, struct audit_krule *rule, int res)
1018 struct audit_buffer *ab;
1019 uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(current));
1020 unsigned int sessionid = audit_get_sessionid(current);
1022 if (!audit_enabled)
1023 return;
1025 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
1026 if (!ab)
1027 return;
1028 audit_log_format(ab, "auid=%u ses=%u" ,loginuid, sessionid);
1029 audit_log_task_context(ab);
1030 audit_log_format(ab, " op=");
1031 audit_log_string(ab, action);
1032 audit_log_key(ab, rule->filterkey);
1033 audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
1034 audit_log_end(ab);
1038 * audit_rule_change - apply all rules to the specified message type
1039 * @type: audit message type
1040 * @portid: target port id for netlink audit messages
1041 * @seq: netlink audit message sequence (serial) number
1042 * @data: payload data
1043 * @datasz: size of payload data
1045 int audit_rule_change(int type, __u32 portid, int seq, void *data,
1046 size_t datasz)
1048 int err = 0;
1049 struct audit_entry *entry;
1051 switch (type) {
1052 case AUDIT_ADD_RULE:
1053 entry = audit_data_to_entry(data, datasz);
1054 if (IS_ERR(entry))
1055 return PTR_ERR(entry);
1057 err = audit_add_rule(entry);
1058 audit_log_rule_change("add rule", &entry->rule, !err);
1059 if (err)
1060 audit_free_rule(entry);
1061 break;
1062 case AUDIT_DEL_RULE:
1063 entry = audit_data_to_entry(data, datasz);
1064 if (IS_ERR(entry))
1065 return PTR_ERR(entry);
1067 err = audit_del_rule(entry);
1068 audit_log_rule_change("remove rule", &entry->rule, !err);
1069 audit_free_rule(entry);
1070 break;
1071 default:
1072 return -EINVAL;
1075 return err;
1079 * audit_list_rules_send - list the audit rules
1080 * @request_skb: skb of request we are replying to (used to target the reply)
1081 * @seq: netlink audit message sequence (serial) number
1083 int audit_list_rules_send(struct sk_buff *request_skb, int seq)
1085 u32 portid = NETLINK_CB(request_skb).portid;
1086 struct net *net = sock_net(NETLINK_CB(request_skb).sk);
1087 struct task_struct *tsk;
1088 struct audit_netlink_list *dest;
1089 int err = 0;
1091 /* We can't just spew out the rules here because we might fill
1092 * the available socket buffer space and deadlock waiting for
1093 * auditctl to read from it... which isn't ever going to
1094 * happen if we're actually running in the context of auditctl
1095 * trying to _send_ the stuff */
1097 dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
1098 if (!dest)
1099 return -ENOMEM;
1100 dest->net = get_net(net);
1101 dest->portid = portid;
1102 skb_queue_head_init(&dest->q);
1104 mutex_lock(&audit_filter_mutex);
1105 audit_list_rules(portid, seq, &dest->q);
1106 mutex_unlock(&audit_filter_mutex);
1108 tsk = kthread_run(audit_send_list, dest, "audit_send_list");
1109 if (IS_ERR(tsk)) {
1110 skb_queue_purge(&dest->q);
1111 kfree(dest);
1112 err = PTR_ERR(tsk);
1115 return err;
1118 int audit_comparator(u32 left, u32 op, u32 right)
1120 switch (op) {
1121 case Audit_equal:
1122 return (left == right);
1123 case Audit_not_equal:
1124 return (left != right);
1125 case Audit_lt:
1126 return (left < right);
1127 case Audit_le:
1128 return (left <= right);
1129 case Audit_gt:
1130 return (left > right);
1131 case Audit_ge:
1132 return (left >= right);
1133 case Audit_bitmask:
1134 return (left & right);
1135 case Audit_bittest:
1136 return ((left & right) == right);
1137 default:
1138 BUG();
1139 return 0;
1143 int audit_uid_comparator(kuid_t left, u32 op, kuid_t right)
1145 switch (op) {
1146 case Audit_equal:
1147 return uid_eq(left, right);
1148 case Audit_not_equal:
1149 return !uid_eq(left, right);
1150 case Audit_lt:
1151 return uid_lt(left, right);
1152 case Audit_le:
1153 return uid_lte(left, right);
1154 case Audit_gt:
1155 return uid_gt(left, right);
1156 case Audit_ge:
1157 return uid_gte(left, right);
1158 case Audit_bitmask:
1159 case Audit_bittest:
1160 default:
1161 BUG();
1162 return 0;
1166 int audit_gid_comparator(kgid_t left, u32 op, kgid_t right)
1168 switch (op) {
1169 case Audit_equal:
1170 return gid_eq(left, right);
1171 case Audit_not_equal:
1172 return !gid_eq(left, right);
1173 case Audit_lt:
1174 return gid_lt(left, right);
1175 case Audit_le:
1176 return gid_lte(left, right);
1177 case Audit_gt:
1178 return gid_gt(left, right);
1179 case Audit_ge:
1180 return gid_gte(left, right);
1181 case Audit_bitmask:
1182 case Audit_bittest:
1183 default:
1184 BUG();
1185 return 0;
1190 * parent_len - find the length of the parent portion of a pathname
1191 * @path: pathname of which to determine length
1193 int parent_len(const char *path)
1195 int plen;
1196 const char *p;
1198 plen = strlen(path);
1200 if (plen == 0)
1201 return plen;
1203 /* disregard trailing slashes */
1204 p = path + plen - 1;
1205 while ((*p == '/') && (p > path))
1206 p--;
1208 /* walk backward until we find the next slash or hit beginning */
1209 while ((*p != '/') && (p > path))
1210 p--;
1212 /* did we find a slash? Then increment to include it in path */
1213 if (*p == '/')
1214 p++;
1216 return p - path;
1220 * audit_compare_dname_path - compare given dentry name with last component in
1221 * given path. Return of 0 indicates a match.
1222 * @dname: dentry name that we're comparing
1223 * @path: full pathname that we're comparing
1224 * @parentlen: length of the parent if known. Passing in AUDIT_NAME_FULL
1225 * here indicates that we must compute this value.
1227 int audit_compare_dname_path(const char *dname, const char *path, int parentlen)
1229 int dlen, pathlen;
1230 const char *p;
1232 dlen = strlen(dname);
1233 pathlen = strlen(path);
1234 if (pathlen < dlen)
1235 return 1;
1237 parentlen = parentlen == AUDIT_NAME_FULL ? parent_len(path) : parentlen;
1238 if (pathlen - parentlen != dlen)
1239 return 1;
1241 p = path + parentlen;
1243 return strncmp(p, dname, dlen);
1246 static int audit_filter_user_rules(struct audit_krule *rule, int type,
1247 enum audit_state *state)
1249 int i;
1251 for (i = 0; i < rule->field_count; i++) {
1252 struct audit_field *f = &rule->fields[i];
1253 int result = 0;
1254 u32 sid;
1256 switch (f->type) {
1257 case AUDIT_PID:
1258 result = audit_comparator(task_pid_vnr(current), f->op, f->val);
1259 break;
1260 case AUDIT_UID:
1261 result = audit_uid_comparator(current_uid(), f->op, f->uid);
1262 break;
1263 case AUDIT_GID:
1264 result = audit_gid_comparator(current_gid(), f->op, f->gid);
1265 break;
1266 case AUDIT_LOGINUID:
1267 result = audit_uid_comparator(audit_get_loginuid(current),
1268 f->op, f->uid);
1269 break;
1270 case AUDIT_LOGINUID_SET:
1271 result = audit_comparator(audit_loginuid_set(current),
1272 f->op, f->val);
1273 break;
1274 case AUDIT_MSGTYPE:
1275 result = audit_comparator(type, f->op, f->val);
1276 break;
1277 case AUDIT_SUBJ_USER:
1278 case AUDIT_SUBJ_ROLE:
1279 case AUDIT_SUBJ_TYPE:
1280 case AUDIT_SUBJ_SEN:
1281 case AUDIT_SUBJ_CLR:
1282 if (f->lsm_rule) {
1283 security_task_getsecid(current, &sid);
1284 result = security_audit_rule_match(sid,
1285 f->type,
1286 f->op,
1287 f->lsm_rule,
1288 NULL);
1290 break;
1293 if (!result)
1294 return 0;
1296 switch (rule->action) {
1297 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
1298 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
1300 return 1;
1303 int audit_filter_user(int type)
1305 enum audit_state state = AUDIT_DISABLED;
1306 struct audit_entry *e;
1307 int rc, ret;
1309 ret = 1; /* Audit by default */
1311 rcu_read_lock();
1312 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
1313 rc = audit_filter_user_rules(&e->rule, type, &state);
1314 if (rc) {
1315 if (rc > 0 && state == AUDIT_DISABLED)
1316 ret = 0;
1317 break;
1320 rcu_read_unlock();
1322 return ret;
1325 int audit_filter_type(int type)
1327 struct audit_entry *e;
1328 int result = 0;
1330 rcu_read_lock();
1331 if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
1332 goto unlock_and_return;
1334 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
1335 list) {
1336 int i;
1337 for (i = 0; i < e->rule.field_count; i++) {
1338 struct audit_field *f = &e->rule.fields[i];
1339 if (f->type == AUDIT_MSGTYPE) {
1340 result = audit_comparator(type, f->op, f->val);
1341 if (!result)
1342 break;
1345 if (result)
1346 goto unlock_and_return;
1348 unlock_and_return:
1349 rcu_read_unlock();
1350 return result;
1353 static int update_lsm_rule(struct audit_krule *r)
1355 struct audit_entry *entry = container_of(r, struct audit_entry, rule);
1356 struct audit_entry *nentry;
1357 int err = 0;
1359 if (!security_audit_rule_known(r))
1360 return 0;
1362 nentry = audit_dupe_rule(r);
1363 if (IS_ERR(nentry)) {
1364 /* save the first error encountered for the
1365 * return value */
1366 err = PTR_ERR(nentry);
1367 audit_panic("error updating LSM filters");
1368 if (r->watch)
1369 list_del(&r->rlist);
1370 list_del_rcu(&entry->list);
1371 list_del(&r->list);
1372 } else {
1373 if (r->watch || r->tree)
1374 list_replace_init(&r->rlist, &nentry->rule.rlist);
1375 list_replace_rcu(&entry->list, &nentry->list);
1376 list_replace(&r->list, &nentry->rule.list);
1378 call_rcu(&entry->rcu, audit_free_rule_rcu);
1380 return err;
1383 /* This function will re-initialize the lsm_rule field of all applicable rules.
1384 * It will traverse the filter lists serarching for rules that contain LSM
1385 * specific filter fields. When such a rule is found, it is copied, the
1386 * LSM field is re-initialized, and the old rule is replaced with the
1387 * updated rule. */
1388 int audit_update_lsm_rules(void)
1390 struct audit_krule *r, *n;
1391 int i, err = 0;
1393 /* audit_filter_mutex synchronizes the writers */
1394 mutex_lock(&audit_filter_mutex);
1396 for (i = 0; i < AUDIT_NR_FILTERS; i++) {
1397 list_for_each_entry_safe(r, n, &audit_rules_list[i], list) {
1398 int res = update_lsm_rule(r);
1399 if (!err)
1400 err = res;
1403 mutex_unlock(&audit_filter_mutex);
1405 return err;