Netdev List
 help / color / mirror / Atom feed
From: Jamal Hadi Salim <jhs@mojatatu.com>
To: netdev@vger.kernel.org
Cc: Jamal Hadi Salim <jhs@mojatatu.com>,
	Jiri Pirko <jiri@resnulli.us>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>,
	Alexei Starovoitov <alexei.starovoitov@gmail.com>,
	Victor Nogueira <victor@mojatatu.com>, Po Liu <Po.Liu@nxp.com>,
	stable@vger.kernel.org,
	syzbot+0054fed3dc9085390f51@syzkaller.appspotmail.com,
	shaos <shawnq1@163.com>
Subject: [PATCH net v4 1/3] net/sched: act_gate: bound the cycle time and start time arithmetic
Date: Tue, 24 Nov 2026 09:12:08 -0500	[thread overview]
Message-ID: <ACT-WEXM.v4.r0.20261124074514-1@mojatatu.com> (raw)
In-Reply-To: <ACT-WEXM.v4.r0.20261124074514@mojatatu.com>

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

With base_time = 1 and cycle_time = 0xffffffffffffffff the computed start
time comes out as 0 instead of the next cycle boundary, so the timer is
armed in the past.  Every expiry recomputes that same past time, so the
softirq never finishes and the installing task spins inside
_raw_spin_unlock_bh() until the watchdog reports a soft lockup.

Reported by syzbot and independently by shaos; the request below
reproduces the stall in our test environment.

The Fix:
Cap base_time and cycle_time at KTIME_MAX in the policy.  Both keep
NLA_U64 and 0 stays accepted, because a 0 cycle time means "derive it
from the entry intervals"; TCA_GATE_CYCLE_TIME_EXT does not drive the
timer and is deliberately left unbounded.

Work out the part of the cycle still ahead of now and add it with
ktime_add_safe(), so an unrepresentable expiry saturates in the far
future 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: the next expiry measures from the boundary that was armed.

Conditions to recreate the bug:
CONFIG_NET_ACT_GATE=y, and CAP_NET_ADMIN in the initial user namespace:
tc_ctl_action() tests netlink_capable(), which checks init_user_ns, so a
user namespace of its own does not help.  iproute2 parses cycle-time as
__s64 then through strtod(), silently rewriting 0xffffffffffffffff to
0x8000000000000000, so tc cannot produce the input.  Build the action as
a raw RTM_NEWACTION message instead: index 1, TCA_GATE_BASE_TIME = 1,
TCA_GATE_CYCLE_TIME = 0xffffffffffffffff, and one schedule entry of
1000000 ns.

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() and _raw_spin_unlock_bh() on the stack, then
reboots.  The same message now fails with -ERANGE.

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 <alexei.starovoitov@gmail.com>
Cc: stable@vger.kernel.org
Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
---
v3 -> v4:
- Bound base_time as well as cycle_time, and bound both at KTIME_MAX
rather than S64_MAX: bounding one attribute's value does not bound the
start time the timer is armed with, and the v3 range still computes a
negative ktime_t for some accepted pairs.
  (Sashiko review of v3, forwarded by Jakub Kicinski)
- Keep NLA_U64 for both attributes and carry the range in the policy,
instead of changing the type to NLA_S64, and drop the unrelated
blank-line hunk.
  (Jakub Kicinski)
- Saturate the start-time computation, use the same helper from
gate_timer_func(), store the boundary it computes and restart the walk
at the first entry.  Bounding the inputs alone does not stop the old
expression from wrapping, and a boundary recomputed from a later now is
not the boundary the armed timer was measured from.  The shape is the one
Alexei Starovoitov sketched on the syzbot stall thread.
- Add a 10 us floor on the schedule entry interval, derived from the
measured cost of one expiry.  The tdc coverage pins both sides of that
boundary, the refusals, and the largest accepted base time and cycle
time.
v2 -> v3: reject an out-of-range cycletime through the netlink policy
instead of a runtime -EINVAL test.
v1 -> v2: reject an out-of-range cycletime with -EINVAL instead of
clamping it.
---
 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.43.0


  reply	other threads:[~2026-11-24 14:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 14:12 [PATCH net v4 0/3] net/sched: act_gate: bound the cycle time and refuse schedule entries shorter than 10 us Jamal Hadi Salim
2026-11-24 14:12 ` Jamal Hadi Salim [this message]
2026-11-24 14:12 ` [PATCH net v4 2/3] net/sched: act_gate: " Jamal Hadi Salim
2026-11-24 14:12 ` [PATCH net v4 3/3] selftests: tc-testing: act_gate: cover the new cycle time and interval limits Jamal Hadi Salim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ACT-WEXM.v4.r0.20261124074514-1@mojatatu.com \
    --to=jhs@mojatatu.com \
    --cc=Po.Liu@nxp.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shawnq1@163.com \
    --cc=stable@vger.kernel.org \
    --cc=syzbot+0054fed3dc9085390f51@syzkaller.appspotmail.com \
    --cc=victor@mojatatu.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox