Skip to contentSkip to conversation

net_sched: hold rtnl lock in tcindex_partial_destroy_work()

PATCH In review Cong Wang 2 participants 4 messages Mar 12, 2020 – Mar 15, 2020

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 net_sched: hold rtnl lock in tcindex_partial_destroy_work()

syzbot reported a use-after-free in tcindex_dump(). This is due to the lack of RTNL in the deferred rcu work. We queue this work with RTNL in tcindex_change(), later, tcindex_dump() is called:

        fh = tp->ops->get(tp, t->tcm_handle);
	...
        err = tp->ops->change(..., &fh, ...);
        tfilter_notify(..., fh, ...);

but there is nothing to serialize the pending tcindex_partial_destroy_work() with tcindex_dump().

Fix this by simply holding RTNL in tcindex_partial_destroy_work(), so that it won't be called until RTNL is released after tc_new_tfilter() is completed.

Reported-and-tested-by [email protected]
Fixes 3d210534cc93 ("net_sched: fix a race condition in tcindex_destroy()")
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>
---
 net/sched/cls_tcindex.c | 2 ++
 1 file changed, 2 insertions(+)
net/sched/cls_tcindex.c +2 -0
index 09b7dc5fe7e0..f2cb24b6f0cf 100644
@@ -261,8 +261,10 @@ static void tcindex_partial_destroy_work(struct work_struct *work)
 					      struct tcindex_data,
 					      rwork);
 
+	rtnl_lock();
 	kfree(p->perfect);
 	kfree(p);
+	rtnl_unlock();
 }
 
 static void tcindex_free_perfect_hash(struct tcindex_data *cp)
Signature · 1 line
2.21.1
author
#1 lore raw

PATCH 1/1 net_sched: keep alloc_hash updated after hash allocation

In commit 599be01ee567 ("net_sched: fix an OOB access in cls_tcindex") I moved cp->hash calculation before the first tcindex_alloc_perfect_hash(), but cp->alloc_hash is left untouched. This difference could lead to another out of bound access.

cp->alloc_hash should always be the size allocated, we should update it after this tcindex_alloc_perfect_hash().

Reported-and-tested-by [email protected]
Reported-and-tested-by [email protected]
Fixes 599be01ee567 ("net_sched: fix an OOB access in cls_tcindex")
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>
---
 net/sched/cls_tcindex.c | 1 +
 1 file changed, 1 insertion(+)
net/sched/cls_tcindex.c +1 -0
index f2cb24b6f0cf..9904299424a1 100644
@@ -359,6 +359,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 
 		if (tcindex_alloc_perfect_hash(net, cp) < 0)
 			goto errout;
+		cp->alloc_hash = cp->hash;
 		for (i = 0; i < min(cp->hash, p->hash); i++)
 			cp->perfect[i].res = p->perfect[i].res;
 		balloc = 1;
Signature · 1 line
2.21.1
maintainer
#2 lore raw

Re: [Patch net] net_sched: keep alloc_hash updated after hash allocation

From: Cong Wang <[email protected]>
Date: Wed, 11 Mar 2020 22:42:28 -0700

Quoted 8 lines

In commit 599be01ee567 ("net_sched: fix an OOB access in cls_tcindex") I moved cp->hash calculation before the first tcindex_alloc_perfect_hash(), but cp->alloc_hash is left untouched. This difference could lead to another out of bound access.

cp->alloc_hash should always be the size allocated, we should update it after this tcindex_alloc_perfect_hash().

Quoted 6 lines
Reported-and-tested-by [email protected]
Reported-and-tested-by [email protected]
Fixes 599be01ee567 ("net_sched: fix an OOB access in cls_tcindex")
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>

Also applied and queued up for -stable, thanks.

maintainer
#3 lore raw
In reply to Cong Wang

From: Cong Wang <[email protected]>
Date: Wed, 11 Mar 2020 22:42:27 -0700

Quoted 16 lines

syzbot reported a use-after-free in tcindex_dump(). This is due to the lack of RTNL in the deferred rcu work. We queue this work with RTNL in tcindex_change(), later, tcindex_dump() is called:

        fh = tp->ops->get(tp, t->tcm_handle);
	...
        err = tp->ops->change(..., &fh, ...);
        tfilter_notify(..., fh, ...);

but there is nothing to serialize the pending tcindex_partial_destroy_work() with tcindex_dump().

Fix this by simply holding RTNL in tcindex_partial_destroy_work(), so that it won't be called until RTNL is released after tc_new_tfilter() is completed.

Quoted 5 lines
Reported-and-tested-by [email protected]
Fixes 3d210534cc93 ("net_sched: fix a race condition in tcindex_destroy()")
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>

Applied and queued up for -stable, thanks Cong.