Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH net 1/2 repost3] net/sched: act_api: reject duplicate actions in a batch
@ 2026-10-12 18:09 Jamal Hadi Salim
  2026-10-12 18:09 ` [PATCH net 2/2 repost3] selftests: tc-testing: add duplicate action delete batch test Jamal Hadi Salim
  2026-10-20  3:00 ` [PATCH net 1/2 repost3] net/sched: act_api: reject duplicate actions in a batch patchwork-bot+netdevbpf
  0 siblings, 2 replies; 3+ messages in thread
From: Jamal Hadi Salim @ 2026-10-12 18:09 UTC (permalink / raw)
  To: netdev
  Cc: Jamal Hadi Salim, stable, Victor Nogueira, Jiri Pirko,
	David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Vlad Buslov, Marcelo Ricardo Leitner, Shuah Khan,
	linux-kselftest, sashiko-bot

tca_action_gd() builds actions[] with one tcf_action_get_1() per nested
TCA_ACT_TAB entry.  Each successful lookup takes its own reference to the
resolved action, so repeated TCA_ACT_INDEX entries in one request yield
the same pointer in multiple slots.

For RTM_DELACTION, tcf_action_delete() then consumes two references per
slot: one in tcf_action_put() and one in tcf_idr_delete_index().  A
duplicate therefore drives the refcount to zero mid-walk -- the delete
frees the action and removes its IDR slot -- and the next slot calls
tcf_action_put() on the freed action:

  refcount_t: underflow; use-after-free.
  WARNING: lib/refcount.c:87 at refcount_dec_not_one
   refcount_dec_and_mutex_lock
   __tcf_action_put
   tca_action_gd

Three duplicate entries are enough.  Reject a repeated action while a
delete batch is built, dropping the reference the extra lookup took, and
return -EINVAL.  RTM_GETACTION balances its own references and keeps
accepting duplicate entries.

Conditions to recreate the bug:
  tc actions add action gact index 100
  tc actions delete action gact index 100 action gact index 100 \
                     action gact index 100

Fixes: 16af6067392c ("net: sched: implement reference counted action release")
Reported-by: Sashiko (gemini) <sashiko-bot@kernel.org>
Link: https://sashiko.dev/#/patchset/20260824153903.4143642-1-victor@mojatatu.com
Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
---
 net/sched/act_api.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/net/sched/act_api.c b/net/sched/act_api.c
index e45a63be397c..a9323c42a69a 100644
--- a/net/sched/act_api.c
+++ b/net/sched/act_api.c
@@ -2026,7 +2026,7 @@ static int
 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
 	      u32 portid, int event, struct netlink_ext_ack *extack)
 {
-	int i, ret;
+	int i, j, ret;
 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
 	struct tc_action *act;
 	size_t attr_size = 0;
@@ -2051,6 +2051,25 @@ tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
 			ret = PTR_ERR(act);
 			goto err;
 		}
+
+		/* A delete consumes two references per slot (tcf_action_put()
+		 * and tcf_idr_delete_index()) but each entry takes one, so a
+		 * repeated action would hit zero mid-walk.  GET balances its
+		 * own references and keeps accepting duplicates.
+		 */
+		if (event == RTM_DELACTION) {
+			for (j = 0; j < i - 1; j++) {
+				if (actions[j] != act)
+					continue;
+
+				tcf_action_put(act);
+				NL_SET_ERR_MSG(extack,
+					       "Duplicate TC action in a delete batch");
+				ret = -EINVAL;
+				goto err;
+			}
+		}
+
 		attr_size += tcf_action_fill_size(act);
 		actions[i - 1] = act;
 	}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH net 2/2 repost3] selftests: tc-testing: add duplicate action delete batch test
  2026-10-12 18:09 [PATCH net 1/2 repost3] net/sched: act_api: reject duplicate actions in a batch Jamal Hadi Salim
@ 2026-10-12 18:09 ` Jamal Hadi Salim
  2026-10-20  3:00 ` [PATCH net 1/2 repost3] net/sched: act_api: reject duplicate actions in a batch patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: Jamal Hadi Salim @ 2026-10-12 18:09 UTC (permalink / raw)
  To: netdev
  Cc: Jamal Hadi Salim, Victor Nogueira, Jiri Pirko, David S . Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Vlad Buslov, Marcelo Ricardo Leitner, Shuah Khan, linux-kselftest,
	sashiko-bot

tca_action_gd() takes one reference per nested TCA_ACT_TAB entry while
tcf_action_delete() consumes two per slot, so three duplicate
TCA_ACT_INDEX entries in one RTM_DELACTION free the action mid-walk and
the next slot dereferences it.  Assert a duplicate batch is rejected and
the action survives.

Conditions to recreate the bug:
  tc actions add action pass index 1
  tc actions delete action gact index 1 action gact index 1 \
                     action gact index 1


Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
---
 .../tc-tests/actions/batch-delete.json        | 33 +++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/tools/testing/selftests/tc-testing/tc-tests/actions/batch-delete.json b/tools/testing/selftests/tc-testing/tc-tests/actions/batch-delete.json
index ef7ca4a6775b..7620e927ae2c 100644
--- a/tools/testing/selftests/tc-testing/tc-tests/actions/batch-delete.json
+++ b/tools/testing/selftests/tc-testing/tc-tests/actions/batch-delete.json
@@ -111,5 +111,38 @@
                 255
             ]
         ]
+    },
+    {
+        "id": "8135",
+        "name": "Reject duplicate action in a delete batch",
+        "category": [
+            "actions",
+            "gact"
+        ],
+        "plugins": {
+            "requires": "nsPlugin"
+        },
+        "setup": [
+            [
+                "$TC actions flush action gact",
+                0,
+                1,
+                255
+            ],
+            "$TC actions add action pass index 1"
+        ],
+        "cmdUnderTest": "$TC actions del action gact index 1 action gact index 1 action gact index 1",
+        "expExitCode": "255",
+        "verifyCmd": "$TC actions ls action gact",
+        "matchPattern": "total acts 1\\b.*index 1 ref 1 bind 0\\b",
+        "matchCount": "1",
+        "teardown": [
+            [
+                "$TC actions flush action gact",
+                0,
+                1,
+                255
+            ]
+        ]
     }
 ]
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net 1/2 repost3] net/sched: act_api: reject duplicate actions in a batch
  2026-10-12 18:09 [PATCH net 1/2 repost3] net/sched: act_api: reject duplicate actions in a batch Jamal Hadi Salim
  2026-10-12 18:09 ` [PATCH net 2/2 repost3] selftests: tc-testing: add duplicate action delete batch test Jamal Hadi Salim
@ 2026-10-20  3:00 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-10-20  3:00 UTC (permalink / raw)
  To: Jamal Hadi Salim
  Cc: netdev, stable, victor, jiri, davem, edumazet, kuba, pabeni,
	horms, vladbu, marcelo.leitner, shuah, linux-kselftest,
	sashiko-bot

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Mon, 12 Oct 2026 14:09:25 -0400 you wrote:
> tca_action_gd() builds actions[] with one tcf_action_get_1() per nested
> TCA_ACT_TAB entry.  Each successful lookup takes its own reference to the
> resolved action, so repeated TCA_ACT_INDEX entries in one request yield
> the same pointer in multiple slots.
> 
> For RTM_DELACTION, tcf_action_delete() then consumes two references per
> slot: one in tcf_action_put() and one in tcf_idr_delete_index().  A
> duplicate therefore drives the refcount to zero mid-walk -- the delete
> frees the action and removes its IDR slot -- and the next slot calls
> tcf_action_put() on the freed action:
> 
> [...]

Here is the summary with links:
  - [net,1/2,repost3] net/sched: act_api: reject duplicate actions in a batch
    https://git.kernel.org/netdev/net/c/f67b3ed1308d
  - [net,2/2,repost3] selftests: tc-testing: add duplicate action delete batch test
    https://git.kernel.org/netdev/net/c/044ad3c90d9d

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-10-20  3:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-12 18:09 [PATCH net 1/2 repost3] net/sched: act_api: reject duplicate actions in a batch Jamal Hadi Salim
2026-10-12 18:09 ` [PATCH net 2/2 repost3] selftests: tc-testing: add duplicate action delete batch test Jamal Hadi Salim
2026-10-20  3:00 ` [PATCH net 1/2 repost3] net/sched: act_api: reject duplicate actions in a batch patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox