Skip to content

Commit ee05917

Browse files
tammelakuba-moo
authored andcommitted
net/sched: tcindex: update imperfect hash filters respecting rcu
The imperfect hash area can be updated while packets are traversing, which will cause a use-after-free when 'tcf_exts_exec()' is called with the destroyed tcf_ext. CPU 0: CPU 1: tcindex_set_parms tcindex_classify tcindex_lookup tcindex_lookup tcf_exts_change tcf_exts_exec [UAF] Stop operating on the shared area directly, by using a local copy, and update the filter with 'rcu_replace_pointer()'. Delete the old filter version only after a rcu grace period elapsed. Fixes: 9b0d444 ("net: sched: avoid atomic swap in tcf_exts_change") Reported-by: valis <sec@valis.email> Suggested-by: valis <sec@valis.email> Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com> Signed-off-by: Pedro Tammela <pctammela@mojatatu.com> Link: https://lore.kernel.org/r/20230209143739.279867-1-pctammela@mojatatu.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent a122170 commit ee05917

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

net/sched/cls_tcindex.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/errno.h>
1313
#include <linux/slab.h>
1414
#include <linux/refcount.h>
15+
#include <linux/rcupdate.h>
1516
#include <net/act_api.h>
1617
#include <net/netlink.h>
1718
#include <net/pkt_cls.h>
@@ -339,6 +340,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
339340
struct tcf_result cr = {};
340341
int err, balloc = 0;
341342
struct tcf_exts e;
343+
bool update_h = false;
342344

343345
err = tcf_exts_init(&e, net, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
344346
if (err < 0)
@@ -456,10 +458,13 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
456458
}
457459
}
458460

459-
if (cp->perfect)
461+
if (cp->perfect) {
460462
r = cp->perfect + handle;
461-
else
462-
r = tcindex_lookup(cp, handle) ? : &new_filter_result;
463+
} else {
464+
/* imperfect area is updated in-place using rcu */
465+
update_h = !!tcindex_lookup(cp, handle);
466+
r = &new_filter_result;
467+
}
463468

464469
if (r == &new_filter_result) {
465470
f = kzalloc(sizeof(*f), GFP_KERNEL);
@@ -485,7 +490,28 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
485490

486491
rcu_assign_pointer(tp->root, cp);
487492

488-
if (r == &new_filter_result) {
493+
if (update_h) {
494+
struct tcindex_filter __rcu **fp;
495+
struct tcindex_filter *cf;
496+
497+
f->result.res = r->res;
498+
tcf_exts_change(&f->result.exts, &r->exts);
499+
500+
/* imperfect area bucket */
501+
fp = cp->h + (handle % cp->hash);
502+
503+
/* lookup the filter, guaranteed to exist */
504+
for (cf = rcu_dereference_bh_rtnl(*fp); cf;
505+
fp = &cf->next, cf = rcu_dereference_bh_rtnl(*fp))
506+
if (cf->key == handle)
507+
break;
508+
509+
f->next = cf->next;
510+
511+
cf = rcu_replace_pointer(*fp, f, 1);
512+
tcf_exts_get_net(&cf->result.exts);
513+
tcf_queue_work(&cf->rwork, tcindex_destroy_fexts_work);
514+
} else if (r == &new_filter_result) {
489515
struct tcindex_filter *nfp;
490516
struct tcindex_filter __rcu **fp;
491517

0 commit comments

Comments
 (0)