Skip to content

Commit e45aa21

Browse files
author
Al Viro
committed
audit rules ordering, part 2
Fix the actual rule listing; add per-type lists _not_ used for matching, with all exit,... sitting on one such list. Simplifies "do something for all rules" logics, while we are at it... Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 0590b93 commit e45aa21

3 files changed

Lines changed: 41 additions & 56 deletions

File tree

‎include/linux/audit.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ struct audit_krule {
373373
struct audit_watch *watch; /* associated watch */
374374
struct audit_tree *tree; /* associated watched tree */
375375
struct list_head rlist; /* entry in audit_{watch,tree}.rules list */
376+
struct list_head list; /* for AUDIT_LIST* purposes only */
376377
u64 prio;
377378
};
378379

‎kernel/audit_tree.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ static void kill_rules(struct audit_tree *tree)
450450
audit_log_end(ab);
451451
rule->tree = NULL;
452452
list_del_rcu(&entry->list);
453+
list_del(&entry->rule.list);
453454
call_rcu(&entry->rcu, audit_free_rule_rcu);
454455
}
455456
}

‎kernel/auditfilter.c‎

Lines changed: 39 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
8686
#error Fix audit_filter_list initialiser
8787
#endif
8888
};
89+
static struct list_head audit_rules_list[AUDIT_NR_FILTERS] = {
90+
LIST_HEAD_INIT(audit_rules_list[0]),
91+
LIST_HEAD_INIT(audit_rules_list[1]),
92+
LIST_HEAD_INIT(audit_rules_list[2]),
93+
LIST_HEAD_INIT(audit_rules_list[3]),
94+
LIST_HEAD_INIT(audit_rules_list[4]),
95+
LIST_HEAD_INIT(audit_rules_list[5]),
96+
};
8997

9098
DEFINE_MUTEX(audit_filter_mutex);
9199

@@ -1007,12 +1015,15 @@ static void audit_update_watch(struct audit_parent *parent,
10071015
list_del_rcu(&oentry->list);
10081016

10091017
nentry = audit_dupe_rule(&oentry->rule, nwatch);
1010-
if (IS_ERR(nentry))
1018+
if (IS_ERR(nentry)) {
1019+
list_del(&oentry->rule.list);
10111020
audit_panic("error updating watch, removing");
1012-
else {
1021+
} else {
10131022
int h = audit_hash_ino((u32)ino);
10141023
list_add(&nentry->rule.rlist, &nwatch->rules);
10151024
list_add_rcu(&nentry->list, &audit_inode_hash[h]);
1025+
list_replace(&oentry->rule.list,
1026+
&nentry->rule.list);
10161027
}
10171028

10181029
call_rcu(&oentry->rcu, audit_free_rule_rcu);
@@ -1077,6 +1088,7 @@ static void audit_remove_parent_watches(struct audit_parent *parent)
10771088
audit_log_end(ab);
10781089
}
10791090
list_del(&r->rlist);
1091+
list_del(&r->list);
10801092
list_del_rcu(&e->list);
10811093
call_rcu(&e->rcu, audit_free_rule_rcu);
10821094
}
@@ -1331,9 +1343,13 @@ static inline int audit_add_rule(struct audit_entry *entry,
13311343
}
13321344

13331345
if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
1346+
list_add(&entry->rule.list,
1347+
&audit_rules_list[entry->rule.listnr]);
13341348
list_add_rcu(&entry->list, list);
13351349
entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
13361350
} else {
1351+
list_add_tail(&entry->rule.list,
1352+
&audit_rules_list[entry->rule.listnr]);
13371353
list_add_tail_rcu(&entry->list, list);
13381354
}
13391355
#ifdef CONFIG_AUDITSYSCALL
@@ -1415,6 +1431,7 @@ static inline int audit_del_rule(struct audit_entry *entry,
14151431
audit_remove_tree_rule(&e->rule);
14161432

14171433
list_del_rcu(&e->list);
1434+
list_del(&e->rule.list);
14181435
call_rcu(&e->rcu, audit_free_rule_rcu);
14191436

14201437
#ifdef CONFIG_AUDITSYSCALL
@@ -1443,30 +1460,16 @@ static inline int audit_del_rule(struct audit_entry *entry,
14431460
static void audit_list(int pid, int seq, struct sk_buff_head *q)
14441461
{
14451462
struct sk_buff *skb;
1446-
struct audit_entry *entry;
1463+
struct audit_krule *r;
14471464
int i;
14481465

14491466
/* This is a blocking read, so use audit_filter_mutex instead of rcu
14501467
* iterator to sync with list writers. */
14511468
for (i=0; i<AUDIT_NR_FILTERS; i++) {
1452-
list_for_each_entry(entry, &audit_filter_list[i], list) {
1453-
struct audit_rule *rule;
1454-
1455-
rule = audit_krule_to_rule(&entry->rule);
1456-
if (unlikely(!rule))
1457-
break;
1458-
skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
1459-
rule, sizeof(*rule));
1460-
if (skb)
1461-
skb_queue_tail(q, skb);
1462-
kfree(rule);
1463-
}
1464-
}
1465-
for (i = 0; i < AUDIT_INODE_BUCKETS; i++) {
1466-
list_for_each_entry(entry, &audit_inode_hash[i], list) {
1469+
list_for_each_entry(r, &audit_rules_list[i], list) {
14671470
struct audit_rule *rule;
14681471

1469-
rule = audit_krule_to_rule(&entry->rule);
1472+
rule = audit_krule_to_rule(r);
14701473
if (unlikely(!rule))
14711474
break;
14721475
skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
@@ -1485,30 +1488,16 @@ static void audit_list(int pid, int seq, struct sk_buff_head *q)
14851488
static void audit_list_rules(int pid, int seq, struct sk_buff_head *q)
14861489
{
14871490
struct sk_buff *skb;
1488-
struct audit_entry *e;
1491+
struct audit_krule *r;
14891492
int i;
14901493

14911494
/* This is a blocking read, so use audit_filter_mutex instead of rcu
14921495
* iterator to sync with list writers. */
14931496
for (i=0; i<AUDIT_NR_FILTERS; i++) {
1494-
list_for_each_entry(e, &audit_filter_list[i], list) {
1495-
struct audit_rule_data *data;
1496-
1497-
data = audit_krule_to_data(&e->rule);
1498-
if (unlikely(!data))
1499-
break;
1500-
skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
1501-
data, sizeof(*data) + data->buflen);
1502-
if (skb)
1503-
skb_queue_tail(q, skb);
1504-
kfree(data);
1505-
}
1506-
}
1507-
for (i=0; i< AUDIT_INODE_BUCKETS; i++) {
1508-
list_for_each_entry(e, &audit_inode_hash[i], list) {
1497+
list_for_each_entry(r, &audit_rules_list[i], list) {
15091498
struct audit_rule_data *data;
15101499

1511-
data = audit_krule_to_data(&e->rule);
1500+
data = audit_krule_to_data(r);
15121501
if (unlikely(!data))
15131502
break;
15141503
skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
@@ -1789,35 +1778,37 @@ int audit_filter_type(int type)
17891778
return result;
17901779
}
17911780

1792-
static int update_lsm_rule(struct audit_entry *entry)
1781+
static int update_lsm_rule(struct audit_krule *r)
17931782
{
1783+
struct audit_entry *entry = container_of(r, struct audit_entry, rule);
17941784
struct audit_entry *nentry;
17951785
struct audit_watch *watch;
17961786
struct audit_tree *tree;
17971787
int err = 0;
17981788

1799-
if (!security_audit_rule_known(&entry->rule))
1789+
if (!security_audit_rule_known(r))
18001790
return 0;
18011791

1802-
watch = entry->rule.watch;
1803-
tree = entry->rule.tree;
1804-
nentry = audit_dupe_rule(&entry->rule, watch);
1792+
watch = r->watch;
1793+
tree = r->tree;
1794+
nentry = audit_dupe_rule(r, watch);
18051795
if (IS_ERR(nentry)) {
18061796
/* save the first error encountered for the
18071797
* return value */
18081798
err = PTR_ERR(nentry);
18091799
audit_panic("error updating LSM filters");
18101800
if (watch)
1811-
list_del(&entry->rule.rlist);
1801+
list_del(&r->rlist);
18121802
list_del_rcu(&entry->list);
1803+
list_del(&r->list);
18131804
} else {
18141805
if (watch) {
18151806
list_add(&nentry->rule.rlist, &watch->rules);
1816-
list_del(&entry->rule.rlist);
1807+
list_del(&r->rlist);
18171808
} else if (tree)
1818-
list_replace_init(&entry->rule.rlist,
1819-
&nentry->rule.rlist);
1809+
list_replace_init(&r->rlist, &nentry->rule.rlist);
18201810
list_replace_rcu(&entry->list, &nentry->list);
1811+
list_replace(&r->list, &nentry->rule.list);
18211812
}
18221813
call_rcu(&entry->rcu, audit_free_rule_rcu);
18231814

@@ -1831,27 +1822,19 @@ static int update_lsm_rule(struct audit_entry *entry)
18311822
* updated rule. */
18321823
int audit_update_lsm_rules(void)
18331824
{
1834-
struct audit_entry *e, *n;
1825+
struct audit_krule *r, *n;
18351826
int i, err = 0;
18361827

18371828
/* audit_filter_mutex synchronizes the writers */
18381829
mutex_lock(&audit_filter_mutex);
18391830

18401831
for (i = 0; i < AUDIT_NR_FILTERS; i++) {
1841-
list_for_each_entry_safe(e, n, &audit_filter_list[i], list) {
1842-
int res = update_lsm_rule(e);
1843-
if (!err)
1844-
err = res;
1845-
}
1846-
}
1847-
for (i=0; i< AUDIT_INODE_BUCKETS; i++) {
1848-
list_for_each_entry_safe(e, n, &audit_inode_hash[i], list) {
1849-
int res = update_lsm_rule(e);
1832+
list_for_each_entry_safe(r, n, &audit_rules_list[i], list) {
1833+
int res = update_lsm_rule(r);
18501834
if (!err)
18511835
err = res;
18521836
}
18531837
}
1854-
18551838
mutex_unlock(&audit_filter_mutex);
18561839

18571840
return err;

0 commit comments

Comments
 (0)