Skip to contentSkip to conversation

net/sched: act_api: use mutex in tcf_idr_check_alloc

PATCH In review Kyle Zeng 5 participants 9 messages May 26 – May 31

Subsystems (3)
lore
Share & tools
lore

Kyle Zeng

author
#0 lore raw

PATCH 1/1 net/sched: act_api: use mutex in tcf_idr_check_alloc

Currently, the NEWTFILTER path uses RCU to guard action idr accesses while the DELTFILTER path uses mutex to guard action accesses. This inconsistency leads to a race condition scenario, which can lead to erroneous operations on refcount, eventually leading to use-after-free situation.
In this patch, we revert the introduction of RCU back to mutex in the NEWFILTER path, which is consistent with the DELFILTER path, avoiding the race condition.

Fixes 4b55e86736d5 ("net/sched: act_api: rely on rcu in tcf_idr_check_alloc")
---
 net/sched/act_api.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
net/sched/act_api.c +5 -7
index 332fd9695e54..63841c3e4310 100644
@@ -874,36 +874,34 @@ int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
 	u32 max;
 
 	if (*index) {
-		rcu_read_lock();
+		mutex_lock(&idrinfo->lock);
 		p = idr_find(&idrinfo->action_idr, *index);
 
 		if (IS_ERR(p)) {
 			/* This means that another process allocated
 			 * index but did not assign the pointer yet.
 			 */
-			rcu_read_unlock();
+			mutex_unlock(&idrinfo->lock);
 			return -EAGAIN;
 		}
 
 		if (!p) {
 			/* Empty slot, try to allocate it */
 			max = *index;
-			rcu_read_unlock();
+			mutex_unlock(&idrinfo->lock);
 			goto new;
 		}
 
 		if (!refcount_inc_not_zero(&p->tcfa_refcnt)) {
 			/* Action was deleted in parallel */
-			rcu_read_unlock();
+			mutex_unlock(&idrinfo->lock);
 			return -EAGAIN;
 		}
 
 		if (bind)
 			atomic_inc(&p->tcfa_bindcnt);
 		*a = p;
-
-		rcu_read_unlock();
-
+		mutex_unlock(&idrinfo->lock);
 		return 1;
 	} else {
 		/* Find a slot */
Signature · 1 line
2.54.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Kyle Zeng

Currently, the NEWTFILTER path uses RCU to guard action idr accesses while the DELTFILTER path uses mutex to guard action accesses. This inconsistency leads to a race condition scenario, which can lead to erroneous operations on refcount, eventually leading to use-after-free situation.
In this patch, we revert the introduction of RCU back to mutex in the NEWFILTER path, which is consistent with the DELFILTER path, avoiding the race condition.

Quoted 2 lines
Fixes 4b55e86736d5 ("net/sched: act_api: rely on rcu in tcf_idr_check_alloc")

cheers,
jamal

Quoted 53 lines
---
 net/sched/act_api.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
net/sched/act_api.c +5 -8
index 332fd9695e54..63841c3e4310 100644
@@ -874,36 +874,34 @@ int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
        u32 max;
 
        if (*index) {
-               rcu_read_lock();
+               mutex_lock(&idrinfo->lock);
                p = idr_find(&idrinfo->action_idr, *index);
 
                if (IS_ERR(p)) {
                        /* This means that another process allocated
                         * index but did not assign the pointer yet.
                         */
-                       rcu_read_unlock();
+                       mutex_unlock(&idrinfo->lock);
                        return -EAGAIN;
                }
 
                if (!p) {
                        /* Empty slot, try to allocate it */
                        max = *index;
-                       rcu_read_unlock();
+                       mutex_unlock(&idrinfo->lock);
                        goto new;
                }
 
                if (!refcount_inc_not_zero(&p->tcfa_refcnt)) {
                        /* Action was deleted in parallel */
-                       rcu_read_unlock();
+                       mutex_unlock(&idrinfo->lock);
                        return -EAGAIN;
                }
 
                if (bind)
                        atomic_inc(&p->tcfa_bindcnt);
                *a = p;
-
-               rcu_read_unlock();
-
+               mutex_unlock(&idrinfo->lock);
                return 1;
        } else {
                /* Find a slot */
--

2.54.0

Reviewed-by on v1 1/1
#2 lore raw
In reply to Kyle Zeng
Quoted 11 lines · Kyle Zeng

Currently, the NEWTFILTER path uses RCU to guard action idr accesses while the DELTFILTER path uses mutex to guard action accesses. This inconsistency leads to a race condition scenario, which can lead to erroneous operations on refcount, eventually leading to use-after-free situation.
In this patch, we revert the introduction of RCU back to mutex in the NEWFILTER path, which is consistent with the DELFILTER path, avoiding the race condition.

Fixes 4b55e86736d5 ("net/sched: act_api: rely on rcu in tcf_idr_check_alloc")
Quoted 50 lines
---
  net/sched/act_api.c | 12 +++++-------
  1 file changed, 5 insertions(+), 7 deletions(-)
net/sched/act_api.c +5 -7
index 332fd9695e54..63841c3e4310 100644
@@ -874,36 +874,34 @@ int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
  	u32 max;
 
  	if (*index) {
-		rcu_read_lock();
+		mutex_lock(&idrinfo->lock);
  		p = idr_find(&idrinfo->action_idr, *index);
 
  		if (IS_ERR(p)) {
  			/* This means that another process allocated
  			 * index but did not assign the pointer yet.
  			 */
-			rcu_read_unlock();
+			mutex_unlock(&idrinfo->lock);
  			return -EAGAIN;
  		}
 
  		if (!p) {
  			/* Empty slot, try to allocate it */
  			max = *index;
-			rcu_read_unlock();
+			mutex_unlock(&idrinfo->lock);
  			goto new;
  		}
 
  		if (!refcount_inc_not_zero(&p->tcfa_refcnt)) {
  			/* Action was deleted in parallel */
-			rcu_read_unlock();
+			mutex_unlock(&idrinfo->lock);
  			return -EAGAIN;
  		}
 
  		if (bind)
  			atomic_inc(&p->tcfa_bindcnt);
  		*a = p;
-
-		rcu_read_unlock();
-
+		mutex_unlock(&idrinfo->lock);
  		return 1;
  	} else {
  		/* Find a slot */
Reviewed-by on v1 1/1
#3 lore raw
In reply to Kyle Zeng
Quoted 11 lines · Kyle Zeng

Currently, the NEWTFILTER path uses RCU to guard action idr accesses while the DELTFILTER path uses mutex to guard action accesses. This inconsistency leads to a race condition scenario, which can lead to erroneous operations on refcount, eventually leading to use-after-free situation.
In this patch, we revert the introduction of RCU back to mutex in the NEWFILTER path, which is consistent with the DELFILTER path, avoiding the race condition.

Fixes 4b55e86736d5 ("net/sched: act_api: rely on rcu in tcf_idr_check_alloc")
maintainer
#4 lore raw
In reply to Kyle Zeng
Quoted 8 lines · On Tue, 26 May 2026 15:08:47 -0700 Kyle Zeng wrote:

Currently, the NEWTFILTER path uses RCU to guard action idr accesses while the DELTFILTER path uses mutex to guard action accesses. This inconsistency leads to a race condition scenario, which can lead to erroneous operations on refcount, eventually leading to use-after-free situation.
In this patch, we revert the introduction of RCU back to mutex in the NEWFILTER path, which is consistent with the DELFILTER path, avoiding the race condition.

The commit message is quite inadequate here. Looks like a run-of-the-mill UAF so you should explain the flow / race that leads to it properly.

Doing some extra digging with Jamal off-list we can't find the reason why normal RCU protection wouldn't work here so maybe hold off reposting until you hear from Jamal.

maintainer
#5 lore raw
Quoted 11 lines · Jakub Kicinski
Quoted 8 lines · On Tue, 26 May 2026 15:08:47 -0700 Kyle Zeng wrote:

Currently, the NEWTFILTER path uses RCU to guard action idr accesses while the DELTFILTER path uses mutex to guard action accesses. This inconsistency leads to a race condition scenario, which can lead to erroneous operations on refcount, eventually leading to use-after-free situation.
In this patch, we revert the introduction of RCU back to mutex in the NEWFILTER path, which is consistent with the DELFILTER path, avoiding the race condition.

Quoted 7 lines

The commit message is quite inadequate here. Looks like a run-of-the-mill UAF so you should explain the flow / race that leads to it properly.

Doing some extra digging with Jamal off-list we can't find the reason why normal RCU protection wouldn't work here so maybe hold off reposting until you hear from Jamal.

Kyle, can you try the attached patch?

cheers,
jamal

author
#6 lore raw

I ran the PoC against Jamal's patch (with inserted mdelay calls) multiple times. And I confirm the issue does not reproduce with the new patch.

Best,
Kyle

Quoted 25 lines · Jamal Hadi Salim
Quoted 11 lines · Jakub Kicinski
Quoted 8 lines · On Tue, 26 May 2026 15:08:47 -0700 Kyle Zeng wrote:

Currently, the NEWTFILTER path uses RCU to guard action idr accesses while the DELTFILTER path uses mutex to guard action accesses. This inconsistency leads to a race condition scenario, which can lead to erroneous operations on refcount, eventually leading to use-after-free situation.
In this patch, we revert the introduction of RCU back to mutex in the NEWFILTER path, which is consistent with the DELFILTER path, avoiding the race condition.

Quoted 7 lines

The commit message is quite inadequate here. Looks like a run-of-the-mill UAF so you should explain the flow / race that leads to it properly.

Doing some extra digging with Jamal off-list we can't find the reason why normal RCU protection wouldn't work here so maybe hold off reposting until you hear from Jamal.

Kyle, can you try the attached patch?

cheers,
jamal

maintainer
#7 lore raw
Quoted 5 lines · Kyle Zeng

I ran the PoC against Jamal's patch (with inserted mdelay calls) multiple times. And I confirm the issue does not reproduce with the new patch.

Thanks Kyle. I will send the patch shortly.

cheers,
jamal

Quoted 29 lines

Best,
Kyle

Quoted 25 lines · Jamal Hadi Salim
Quoted 11 lines · Jakub Kicinski
Quoted 8 lines · On Tue, 26 May 2026 15:08:47 -0700 Kyle Zeng wrote:

Currently, the NEWTFILTER path uses RCU to guard action idr accesses while the DELTFILTER path uses mutex to guard action accesses. This inconsistency leads to a race condition scenario, which can lead to erroneous operations on refcount, eventually leading to use-after-free situation.
In this patch, we revert the introduction of RCU back to mutex in the NEWFILTER path, which is consistent with the DELFILTER path, avoiding the race condition.

Quoted 7 lines

The commit message is quite inadequate here. Looks like a run-of-the-mill UAF so you should explain the flow / race that leads to it properly.

Doing some extra digging with Jamal off-list we can't find the reason why normal RCU protection wouldn't work here so maybe hold off reposting until you hear from Jamal.

Kyle, can you try the attached patch?

cheers,
jamal

maintainer
#8 lore raw
In reply to Kyle Zeng
Quoted 5 lines · Kyle Zeng

I ran the PoC against Jamal's patch (with inserted mdelay calls) multiple times. And I confirm the issue does not reproduce with the new patch.

Can you test v2 and put your tested-by if it passes. Sorry, there were issues caught by syzbot.

cheers,
jamal

Quoted 29 lines

Best,
Kyle

Quoted 25 lines · Jamal Hadi Salim
Quoted 11 lines · Jakub Kicinski
Quoted 8 lines · On Tue, 26 May 2026 15:08:47 -0700 Kyle Zeng wrote:

Currently, the NEWTFILTER path uses RCU to guard action idr accesses while the DELTFILTER path uses mutex to guard action accesses. This inconsistency leads to a race condition scenario, which can lead to erroneous operations on refcount, eventually leading to use-after-free situation.
In this patch, we revert the introduction of RCU back to mutex in the NEWFILTER path, which is consistent with the DELFILTER path, avoiding the race condition.

Quoted 7 lines

The commit message is quite inadequate here. Looks like a run-of-the-mill UAF so you should explain the flow / race that leads to it properly.

Doing some extra digging with Jamal off-list we can't find the reason why normal RCU protection wouldn't work here so maybe hold off reposting until you hear from Jamal.

Kyle, can you try the attached patch?

cheers,
jamal