Netdev List
 help / color / mirror / Atom feed
* [PATCH net v5 0/2] net/sched: act_gate: bound the cycle time and start time arithmetic
@ 2026-11-27 11:43 Jamal Hadi Salim
  2026-11-27 11:43 ` [PATCH net v5 1/2] " Jamal Hadi Salim
  2026-11-27 11:43 ` [PATCH net v5 2/2] selftests: tc-testing: act_gate: cover the cycle time and base time bounds Jamal Hadi Salim
  0 siblings, 2 replies; 3+ messages in thread
From: Jamal Hadi Salim @ 2026-11-27 11:43 UTC (permalink / raw)
  To: netdev
  Cc: Jamal Hadi Salim, Jiri Pirko, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman,
	Shuah Khan, linux-kselftest, Alexei Starovoitov, Victor Nogueira,
	stable, syzbot+0054fed3dc9085390f51, shaos

The cycle time and the base time are read from u64 attributes into a
signed ktime_t, and the cycle arithmetic built on them wraps when either
value is above S64_MAX.  With base_time = 1 and cycle_time = all ones the
timer is armed in the past on every expiry, so the softirq never finishes
and the installing task soft-locks the CPU.

Neither attribute has been bounded since the action was introduced, so one
request is enough to stall a CPU on any kernel that has it.

Patch 1 bounds both attributes at KTIME_MAX and computes the next
boundary with ktime_add_safe(), so an unrepresentable expiry saturates in
the far future.  Patch 2 covers the accepted upper bound and both refusals
in tc-testing.

The 10 us schedule-entry floor from the previous posting is a policy
change and moves to net-next; it follows once this series lands, and is not
part of this series.

Changes since v4:
- Split: only the regression fix and its tests are here; the entry
  interval floor and its test cases move to net-next.
- Conditions states the user-namespace route through a filter, which is
  the minimum privilege that reaches the code (reported on the list).
- The test cases match the kernel's own message on a libmnl build as
  well as the strerror text, use deterministic signed values instead of a
  2^63 decimal whose conversion is architecture dependent, and assert
  the stored value instead of only the index.

Jamal Hadi Salim (2):
  net/sched: act_gate: bound the cycle time and start time arithmetic
  selftests: tc-testing: act_gate: cover the cycle time and base time
    bounds

 net/sched/act_gate.c                          |  34 +++---
 .../tc-testing/tc-tests/actions/gate.json     | 108 ++++++++++++++++++
 2 files changed, 127 insertions(+), 15 deletions(-)

-- 
2.34.1


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

* [PATCH net v5 1/2] net/sched: act_gate: bound the cycle time and start time arithmetic
  2026-11-27 11:43 [PATCH net v5 0/2] net/sched: act_gate: bound the cycle time and start time arithmetic Jamal Hadi Salim
@ 2026-11-27 11:43 ` Jamal Hadi Salim
  2026-11-27 11:43 ` [PATCH net v5 2/2] selftests: tc-testing: act_gate: cover the cycle time and base time bounds Jamal Hadi Salim
  1 sibling, 0 replies; 3+ messages in thread
From: Jamal Hadi Salim @ 2026-11-27 11:43 UTC (permalink / raw)
  To: netdev
  Cc: Jamal Hadi Salim, Jiri Pirko, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman,
	Shuah Khan, linux-kselftest, Alexei Starovoitov, Victor Nogueira,
	stable, syzbot+0054fed3dc9085390f51, shaos

The issue:
TCA_GATE_BASE_TIME and TCA_GATE_CYCLE_TIME are u64 attributes read into a
signed ktime_t; a value above S64_MAX turns negative and the cycle
arithmetic wraps instead of saturating.

With base_time = 1 and cycle_time = 0xffffffffffffffff the start time
comes out as 0: the timer is armed in the past and every expiry recomputes
it, so the softirq never finishes.  Neither attribute has ever been
bounded, so one request stalls a CPU on any kernel that has the action.

Reported by syzbot and independently by shaos; the request below
reproduces the stall.

The Fix:
Cap base_time and cycle_time at KTIME_MAX in the policy.  Both keep NLA_U64
and 0 stays accepted (a 0 cycle time derives from the entries); the _EXT
attribute does not drive the timer and stays unbounded.

Work out the part of the cycle still ahead of now and add it with
ktime_add_safe(), so an unrepresentable expiry saturates instead of
wrapping into the past.  gate_timer_func() reuses that helper, stores the
boundary it computes, and restarts the walk at the first entry.

Conditions to recreate the bug:
CONFIG_NET_ACT_GATE=y.  The action can also be created from a filter,
where the only capability check is netns-scoped, so a user namespace of
its own is enough:

  unshare -Urn
  ip link set lo up
  tc qdisc add dev lo clsact
  tc filter add dev lo ingress matchall action gate base-time 1 \
      cycle-time -1 sched-entry close 1000000

iproute2 parses the attribute as a signed 64-bit value, so -1 reaches the
kernel as 0xffffffffffffffff.  The raw RTM_NEWACTION path also tests
netlink_capable() against the init ns; the reproducer writes those values.

The send never returns and the CPU stays in the timer softirq.  With
softlockup_panic=1 and watchdog_thresh=5 the kernel reports

  watchdog: BUG: soft lockup - CPU#0 stuck for 13s! [poc:832]

with tcf_gate_init() on the stack; on the fixed kernel both requests are
refused.

Fixes: a51c328df310 ("net: qos: introduce a gate control flow action")
Reported-by: syzbot+0054fed3dc9085390f51@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=0054fed3dc9085390f51
Reported-by: shaos <shawnq1@163.com>
Link: https://lore.kernel.org/netdev/20261022144732.61188-1-shawnq1@163.com/
Link: https://lore.kernel.org/netdev/20260827191547.4004973-2-kuba@kernel.org/
Link: https://lore.kernel.org/netdev/20260827121751.33dac8aa@kernel.org/
Link: https://lore.kernel.org/netdev/DN37NBGPJJG9.2DSVDCKS3UYTV@gmail.com/
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Cc: stable@vger.kernel.org
Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
---
v4 -> v5:
- Split: this posting carries the regression fix and its tests; the 10 us
  entry-interval floor and its cases moved to net-next.
- Conditions: a filter reaches the same code with CAP_NET_ADMIN in a user
  namespace of its own; the earlier text claimed the initial user namespace
  was required (reported on the list).
- Changelog: KTIME_MAX equals S64_MAX, so the bound is the same value the
  earlier policy carried; name what removed the wrapped start time, the
  base_time bound and ktime_add_safe() (reported by Sashiko).
---
 net/sched/act_gate.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/net/sched/act_gate.c b/net/sched/act_gate.c
index 711cf2443540..4835f6543901 100644
--- a/net/sched/act_gate.c
+++ b/net/sched/act_gate.c
@@ -45,8 +45,8 @@ static void gate_get_start_time(struct tcf_gate *gact,
 				const struct tcf_gate_params *param,
 				ktime_t *start)
 {
-	ktime_t now, base, cycle;
-	u64 n;
+	ktime_t now, base;
+	u64 cycle, rem;
 
 	base = ns_to_ktime(param->tcfg_basetime);
 	now = gate_get_time(gact);
@@ -58,8 +58,8 @@ static void gate_get_start_time(struct tcf_gate *gact,
 
 	cycle = param->tcfg_cycletime;
 
-	n = div64_u64(ktime_sub_ns(now, base), cycle);
-	*start = ktime_add_ns(base, (n + 1) * cycle);
+	div64_u64_rem(ktime_sub_ns(now, base), cycle, &rem);
+	*start = ktime_add_safe(now, ns_to_ktime(cycle - rem));
 }
 
 static void gate_start_timer(struct tcf_gate *gact, ktime_t start)
@@ -94,8 +94,8 @@ static enum hrtimer_restart gate_timer_func(struct hrtimer *timer)
 	gact->current_entry_octets = 0;
 	gact->current_max_octets = next->maxoctets;
 
-	gact->current_close_time = ktime_add_ns(gact->current_close_time,
-						next->interval);
+	gact->current_close_time = ktime_add_safe(gact->current_close_time,
+						  next->interval);
 
 	close_time = gact->current_close_time;
 
@@ -108,13 +108,10 @@ static enum hrtimer_restart gate_timer_func(struct hrtimer *timer)
 	now = gate_get_time(gact);
 
 	if (ktime_after(now, close_time)) {
-		ktime_t cycle, base;
-		u64 n;
-
-		cycle = p->tcfg_cycletime;
-		base = ns_to_ktime(p->tcfg_basetime);
-		n = div64_u64(ktime_sub_ns(now, base), cycle);
-		close_time = ktime_add_ns(base, (n + 1) * cycle);
+		gate_get_start_time(gact, p, &close_time);
+		gact->current_close_time = close_time;
+		next = list_first_entry(&p->entries,
+					struct tcfg_gate_entry, list);
 	}
 
 	gact->next_entry = next;
@@ -173,13 +170,20 @@ static const struct nla_policy entry_policy[TCA_GATE_ENTRY_MAX + 1] = {
 	[TCA_GATE_ENTRY_MAX_OCTETS]	= { .type = NLA_S32 },
 };
 
+static const struct netlink_range_validation gate_time_range = {
+	.min = 0,
+	.max = KTIME_MAX,
+};
+
 static const struct nla_policy gate_policy[TCA_GATE_MAX + 1] = {
 	[TCA_GATE_PARMS]		=
 		NLA_POLICY_EXACT_LEN(sizeof(struct tc_gate)),
 	[TCA_GATE_PRIORITY]		= { .type = NLA_S32 },
 	[TCA_GATE_ENTRY_LIST]		= { .type = NLA_NESTED },
-	[TCA_GATE_BASE_TIME]		= { .type = NLA_U64 },
-	[TCA_GATE_CYCLE_TIME]		= { .type = NLA_U64 },
+	[TCA_GATE_BASE_TIME]		=
+		NLA_POLICY_FULL_RANGE(NLA_U64, &gate_time_range),
+	[TCA_GATE_CYCLE_TIME]		=
+		NLA_POLICY_FULL_RANGE(NLA_U64, &gate_time_range),
 	[TCA_GATE_CYCLE_TIME_EXT]	= { .type = NLA_U64 },
 	[TCA_GATE_FLAGS]		= { .type = NLA_U32 },
 	[TCA_GATE_CLOCKID]		= { .type = NLA_S32 },
-- 
2.34.1


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

* [PATCH net v5 2/2] selftests: tc-testing: act_gate: cover the cycle time and base time bounds
  2026-11-27 11:43 [PATCH net v5 0/2] net/sched: act_gate: bound the cycle time and start time arithmetic Jamal Hadi Salim
  2026-11-27 11:43 ` [PATCH net v5 1/2] " Jamal Hadi Salim
@ 2026-11-27 11:43 ` Jamal Hadi Salim
  1 sibling, 0 replies; 3+ messages in thread
From: Jamal Hadi Salim @ 2026-11-27 11:43 UTC (permalink / raw)
  To: netdev
  Cc: Jamal Hadi Salim, Jiri Pirko, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman,
	Shuah Khan, linux-kselftest, Alexei Starovoitov, Victor Nogueira,
	syzbot+0054fed3dc9085390f51, shaos

The tests:
The gate action now caps the base time and the cycle time at KTIME_MAX.
Cover the accepted upper bound and both refusals.

The accepted values:
A base time and a cycle time of exactly KTIME_MAX install, and the
read-back must show the value that was installed rather than only the
index.

The refusals:
-1 reaches the kernel as the largest u64, above KTIME_MAX, so it is
refused as out of range.  The negative cycle time is tested against a
base time that is itself accepted, so the case pins the cycle bound
instead of whichever bound is checked first.

Context note:
tc does not print the same error line on every build: without libmnl it
prints the strerror text, with libmnl the kernel's own message.  The
cases accept either.

The values are signed decimals on purpose: iproute2 falls back to
strtod() above S64_MAX and the conversion is undefined, so such an input
is refused on x86 and installs on the architectures that saturate it.

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

diff --git a/tools/testing/selftests/tc-testing/tc-tests/actions/gate.json b/tools/testing/selftests/tc-testing/tc-tests/actions/gate.json
index db645c22ad7b..e5450557751d 100644
--- a/tools/testing/selftests/tc-testing/tc-tests/actions/gate.json
+++ b/tools/testing/selftests/tc-testing/tc-tests/actions/gate.json
@@ -347,5 +347,113 @@
         "teardown": [
             "$TC action flush action gate"
         ]
+    },
+    {
+        "id": "7d1f",
+        "name": "Add gate action with the maximum base time",
+        "category": [
+            "actions",
+            "gate"
+        ],
+        "plugins": {
+            "requires": "nsPlugin"
+        },
+        "setup": [
+            [
+                "$TC actions flush action gate",
+                0,
+                1,
+                255
+            ]
+        ],
+        "cmdUnderTest": "$TC action add action gate base-time 9223372036854775807 sched-entry close 1000000ns index 201",
+        "expExitCode": "0",
+        "verifyCmd": "$TC -j action get action gate index 201",
+        "matchPattern": "\"base_time\":9223372036854775807",
+        "matchCount": "1",
+        "teardown": [
+            "$TC actions flush action gate"
+        ]
+    },
+    {
+        "id": "9f10",
+        "name": "Add gate action with the maximum cycle time",
+        "category": [
+            "actions",
+            "gate"
+        ],
+        "plugins": {
+            "requires": "nsPlugin"
+        },
+        "setup": [
+            [
+                "$TC actions flush action gate",
+                0,
+                1,
+                255
+            ]
+        ],
+        "cmdUnderTest": "$TC action add action gate cycle-time 9223372036854775807 sched-entry close 1000000ns index 301",
+        "expExitCode": "0",
+        "verifyCmd": "$TC -j action get action gate index 301",
+        "matchPattern": "\"cycle_time\":9223372036854775807",
+        "matchCount": "1",
+        "teardown": [
+            "$TC actions flush action gate"
+        ]
+    },
+    {
+        "id": "5c47",
+        "name": "Reject gate action with a negative base time",
+        "category": [
+            "actions",
+            "gate"
+        ],
+        "plugins": {
+            "requires": "nsPlugin"
+        },
+        "setup": [
+            [
+                "$TC actions flush action gate",
+                0,
+                1,
+                255
+            ]
+        ],
+        "cmdUnderTest": "$TC action add action gate base-time -1 sched-entry close 1000000ns index 401",
+        "expExitCode": "255",
+        "verifyCmd": "$TC action add action gate base-time -1 sched-entry close 1000000ns index 401 2>&1",
+        "matchPattern": "integer out of range|RTNETLINK answers: Numerical result out of range",
+        "matchCount": "1",
+        "teardown": [
+            "$TC actions flush action gate"
+        ]
+    },
+    {
+        "id": "b3c3",
+        "name": "Reject gate action with a negative cycle time",
+        "category": [
+            "actions",
+            "gate"
+        ],
+        "plugins": {
+            "requires": "nsPlugin"
+        },
+        "setup": [
+            [
+                "$TC actions flush action gate",
+                0,
+                1,
+                255
+            ]
+        ],
+        "cmdUnderTest": "$TC action add action gate base-time 9223372036854775807 cycle-time -1 sched-entry close 1000000ns index 402",
+        "expExitCode": "255",
+        "verifyCmd": "$TC action add action gate base-time 9223372036854775807 cycle-time -1 sched-entry close 1000000ns index 402 2>&1",
+        "matchPattern": "integer out of range|RTNETLINK answers: Numerical result out of range",
+        "matchCount": "1",
+        "teardown": [
+            "$TC actions flush action gate"
+        ]
     }
 ]
-- 
2.34.1


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

end of thread, other threads:[~2026-11-27 11:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 11:43 [PATCH net v5 0/2] net/sched: act_gate: bound the cycle time and start time arithmetic Jamal Hadi Salim
2026-11-27 11:43 ` [PATCH net v5 1/2] " Jamal Hadi Salim
2026-11-27 11:43 ` [PATCH net v5 2/2] selftests: tc-testing: act_gate: cover the cycle time and base time bounds Jamal Hadi Salim

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