Skip to contentSkip to conversation

net/sched: cls_flow: avoid integer overflow in perturb_period

PATCH Applied Wei Tao 2 participants 2 messages Nov 13 – Nov 19

Subsystems (3)
lore
Share & tools
lore

Wei Tao

author
#0 lore raw

PATCH v3 1/1 net/sched: cls_flow: avoid integer overflow in perturb_period

flow_change() calculates perturb_period by directly multiplying nla_get_u32(tb[TCA_FLOW_PERTURB]) by HZ, without any overflow protection. Since perturb_period is a u32, the multiplication overflows when the value from netlink exceeds UINT_MAX / HZ. The result can be an unexpectedly short period or even zero.

  # tc qdisc add dev lo clsact
  # tc filter add dev lo ingress protocol ip pref 1 handle 1 flow \
        hash keys src perturb 42949673 baseclass 1:10

With HZ=100, 42949673*100=4294967300, but UINT_MAX is 4294967295, so the product wraps to 4 jiffies.

Both the filter-modify and filter-create paths in flow_change() are affected.

Since perturb_period is ultimately fed to mod_timer(), and calc_wheel_index() internally casts delta to signed long for comparison, values greater than INT_MAX / HZ must be rejected. This keeps the relative timeout within the signed-long range on 32-bit systems in addition to preventing the u32 multiplication from wrapping.

Declare the limit in flow_policy with NLA_POLICY_FULL_RANGE(), so the netlink parser validates the attribute instead of open-coding the check in both paths, and pass extack to nla_parse_nested_deprecated() so userspace is told which attribute is out of range.

The missing check was noticed while reviewing commit 7ca52541c05c ("net_sched: sch_sfq: reject invalid perturb period"). An LLM-assisted analysis of the same pattern then showed that cls_flow performs the same unchecked seconds-to-jiffies conversion in both its create and change paths, and needs the same range check.

Tested on 7.4.0-rc1+ (aarch64, CONFIG_HZ=100):

  # tc qdisc add dev lo clsact
  # tc filter add dev lo ingress protocol ip pref 1 handle 1 flow \
        hash keys src perturb 42949673 baseclass 1:10
  RTNETLINK answers: Numerical result out of range
  # tc filter add dev lo ingress protocol ip pref 1 handle 1 flow \
        hash keys src perturb 100 baseclass 1:10
  # tc filter show dev lo ingress | cut -d' ' -f9-
  handle 0x1 hash keys src baseclass 1:10 perturb 100sec
  # tc filter change dev lo ingress protocol ip pref 1 handle 1 flow \
        hash keys src perturb 42949673 baseclass 1:10
  RTNETLINK answers: Numerical result out of range
  # tc filter show dev lo ingress | cut -d' ' -f9-
  handle 0x1 hash keys src baseclass 1:10 perturb 100sec

---
v3:
- declare the limit in flow_policy with NLA_POLICY_FULL_RANGE() and pass extack to nla_parse_nested_deprecated(), as suggested in review
- update the tested transcript for the policy-based rejection
v2: https://lore.kernel.org/netdev/[email protected]/
- drop the Fixes and Cc: stable tags: this is hardening
- describe how the issue was found and add the tested commands
v1: https://lore.kernel.org/netdev/[email protected]/

 net/sched/cls_flow.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
net/sched/cls_flow.c +7 -2
index 2ef449706638..2e1825f09ea1 100644
@@ -357,6 +357,10 @@ static void flow_perturbation(struct timer_list *t)
 		mod_timer(&f->perturb_timer, jiffies + f->perturb_period);
 }
 
+static const struct netlink_range_validation flow_perturb_range = {
+	.max = INT_MAX / HZ,
+};
+
 static const struct nla_policy flow_policy[TCA_FLOW_MAX + 1] = {
 	[TCA_FLOW_KEYS]		= { .type = NLA_U32 },
 	[TCA_FLOW_MODE]		= { .type = NLA_U32 },
@@ -370,7 +374,8 @@ static const struct nla_policy flow_policy[TCA_FLOW_MAX + 1] = {
 	[TCA_FLOW_ACT]		= { .type = NLA_NESTED },
 	[TCA_FLOW_POLICE]	= { .type = NLA_NESTED },
 	[TCA_FLOW_EMATCHES]	= { .type = NLA_NESTED },
-	[TCA_FLOW_PERTURB]	= { .type = NLA_U32 },
+	[TCA_FLOW_PERTURB]	= NLA_POLICY_FULL_RANGE(NLA_U32,
+							&flow_perturb_range),
 };
 
 static void __flow_destroy_filter(struct flow_filter *f)
@@ -413,7 +418,7 @@ static int flow_change(struct net *net, struct sk_buff *in_skb,
 		return -EINVAL;
 
 	err = nla_parse_nested_deprecated(tb, TCA_FLOW_MAX, opt, flow_policy,
-					  NULL);
+					  extack);
 	if (err < 0)
 		return err;
 

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.25.1
#1 lore raw
This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Quoted 11 lines · On Fri, 13 Nov 2026 10:48:02 +0800 you wrote:

flow_change() calculates perturb_period by directly multiplying nla_get_u32(tb[TCA_FLOW_PERTURB]) by HZ, without any overflow protection. Since perturb_period is a u32, the multiplication overflows when the value from netlink exceeds UINT_MAX / HZ. The result can be an unexpectedly short period or even zero.

  # tc qdisc add dev lo clsact
  # tc filter add dev lo ingress protocol ip pref 1 handle 1 flow \
        hash keys src perturb 42949673 baseclass 1:10

[...]

Here is the summary with links:
- [net-next,v3] net/sched: cls_flow: avoid integer overflow in perturb_period

    https://git.kernel.org/netdev/net-next/c/042ca0604cbb

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html