net: ipv4: bound TCP reordering sysctl writes
Subsystems (3)
PATCH 1/1 net: ipv4: bound TCP reordering sysctl writes
In reply to a message not in the archiveFrom: Wyatt Feng <[email protected]>
Reject invalid net.ipv4.tcp_reordering values before they reach TCP socket state. The sysctl is stored as an int but copied into the u32 tp->reordering field for new sockets, so negative writes wrap to large values.
With tcp_mtu_probing=2, the wrapped value can overflow the tcp_mtu_probe() size calculation and drive the MTU probing path into an out-of-bounds read. Route tcp_reordering writes through proc_dointvec_minmax() and clamp them to the per-netns range [1, tcp_max_reordering].
Apply the matching cross-check to tcp_max_reordering so the existing invariant tcp_max_reordering >= tcp_reordering is preserved. This keeps the fix at the sysctl boundary and avoids changing the TCP fast path.
--- net/ipv4/sysctl_net_ipv4.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-)
net/ipv4/sysctl_net_ipv4.c +36 -2
@@ -215,6 +215,40 @@ static int ipv4_fwd_update_priority(const struct ctl_table *table, int write,return ret; } +static int proc_tcp_reordering (const struct ctl_table *table,int write, +void *buffer,size_t *lenp,loff_t *ppos) +{ +struct ctl_table tmp = { + .data = table->data, + .maxlen = table->maxlen, + .mode = table->mode, + .extra1 =SYSCTL_ONE , + }; +struct net *net; + + net = container_of(table->data, struct net, ipv4.sysctl_tcp_reordering); + tmp.extra2 = &net->ipv4.sysctl_tcp_max_reordering; + +return proc_dointvec_minmax (&tmp, write, buffer, lenp, ppos); +} + +static int proc_tcp_max_reordering (const struct ctl_table *table,int write, +void *buffer,size_t *lenp,loff_t *ppos) +{ +struct ctl_table tmp = { + .data = table->data, + .maxlen = table->maxlen, + .mode = table->mode, + }; +struct net *net; + + net = container_of(table->data, struct net, + ipv4.sysctl_tcp_max_reordering); + tmp.extra1 = &net->ipv4.sysctl_tcp_reordering; + +return proc_dointvec_minmax (&tmp, write, buffer, lenp, ppos); +} +static int proc_tcp_congestion_control (const struct ctl_table *ctl,int write,void *buffer,size_t *lenp,loff_t *ppos) {
@@ -1058,7 +1092,7 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_tcp_reordering, .maxlen = sizeof(int), .mode = 0644, - .proc_handler = proc_dointvec + .proc_handler = proc_tcp_reordering }, { .procname = "tcp_retries1",
@@ -1293,7 +1327,7 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_tcp_max_reordering, .maxlen = sizeof(int), .mode = 0644, - .proc_handler = proc_dointvec + .proc_handler = proc_tcp_max_reordering }, { .procname = "tcp_dsack",
Signature · 1 line
2.47.3
Quoted 95 lines · Ren Wei
From: Wyatt Feng <[email protected]>
Reject invalid
net.ipv4.tcp_reorderingvalues before they reach TCP socket state. The sysctl is stored as anintbut copied into theu32tp->reorderingfield for new sockets, so negative writes wrap to large values.With
tcp_mtu_probing=2, the wrapped value can overflow thetcp_mtu_probe()size calculation and drive the MTU probing path into an out-of-bounds read. Routetcp_reorderingwrites throughproc_dointvec_minmax()and clamp them to the per-netns range[1, tcp_max_reordering].Apply the matching cross-check to
tcp_max_reorderingso the existing invarianttcp_max_reordering >= tcp_reorderingis preserved. This keeps the fix at the sysctl boundary and avoids changing the TCP fast path.Fixes 91cc17c0e5e5 ("[TCP]: MTUprobe: receiver window & data available checks fixed")Reported-by Yuan Tan <[email protected]>Reported-by Zhengchuan Liang <[email protected]>Reported-by Xin Liu <[email protected]>Assisted-by Codex:GPT-5.4Signed-off-by Wyatt Feng <[email protected]>Signed-off-by Ren Wei <[email protected]>--- net/ipv4/sysctl_net_ipv4.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-)net/ipv4/sysctl_net_ipv4.c +36
-3@@ -215,6 +215,40 @@ static int ipv4_fwd_update_priority(const struct ctl_table *table, int write,return ret; } +static int proc_tcp_reordering (const struct ctl_table *table,int write, +void *buffer,size_t *lenp,loff_t *ppos) +{ +struct ctl_table tmp = { + .data = table->data, + .maxlen = table->maxlen, + .mode = table->mode, + .extra1 =SYSCTL_ONE , + }; +struct net *net; + + net = container_of(table->data, struct net, ipv4.sysctl_tcp_reordering); + tmp.extra2 = &net->ipv4.sysctl_tcp_max_reordering; + +return proc_dointvec_minmax (&tmp, write, buffer, lenp, ppos); +} + +static int proc_tcp_max_reordering (const struct ctl_table *table,int write, +void *buffer,size_t *lenp,loff_t *ppos) +{ +struct ctl_table tmp = { + .data = table->data, + .maxlen = table->maxlen, + .mode = table->mode, + }; +struct net *net; + + net = container_of(table->data, struct net, + ipv4.sysctl_tcp_max_reordering); + tmp.extra1 = &net->ipv4.sysctl_tcp_reordering; + +return proc_dointvec_minmax (&tmp, write, buffer, lenp, ppos); +} +static int proc_tcp_congestion_control (const struct ctl_table *ctl,int write,void *buffer,size_t *lenp,loff_t *ppos) {@@ -1058,7 +1092,7 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_tcp_reordering, .maxlen = sizeof(int), .mode = 0644, - .proc_handler = proc_dointvec + .proc_handler = proc_tcp_reordering }, { .procname = "tcp_retries1",@@ -1293,7 +1327,7 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_tcp_max_reordering, .maxlen = sizeof(int), .mode = 0644, - .proc_handler = proc_dointvec + .proc_handler = proc_tcp_max_reordering }, { .procname = "tcp_dsack", --
Quoted 2 lines
2.47.3
Thanks for the patch. I think we can do better.
Also I do not see a fix in tcp_mtu_probe()?
What about:
net/ipv4/sysctl_net_ipv4.c +0 -0
100644
net/ipv4/sysctl_net_ipv4.c +5 -1
@@ -1058,6 +1058,9 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_tcp_reordering, .maxlen = sizeof(int), .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ONE, + .extra2 = &init_net.ipv4.sysctl_tcp_max_reordering, .proc_handler = proc_dointvec }, {
@@ -1293,7 +1296,8 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_tcp_max_reordering, .maxlen = sizeof(int), .mode = 0644, - .proc_handler = proc_dointvec + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ONE, }, { .procname = "tcp_dsack",
@@ -1676,6 +1680,9 @@ static __net_init int
ipv4_sysctl_init_net(struct net *net)
*/
table[i].mode &= ~0222;
}
+ if (table[i].extra2 >= (void *)&init_net.ipv4 &&
+ table[i].extra2 < (void *)(&init_net.ipv4 + 1))
+ table[i].extra2 += (void *)net - (void *)&init_net;
}
}net/ipv4/tcp_output.c +0 -0
100644
net/ipv4/tcp_output.c +2 -2
@@ -2687,7 +2687,7 @@ static int tcp_mtu_probe(struct sock *sk)struct sk_buff *skb, *nskb, *next;struct net *net =sock_net (sk);int probe_size; -int size_needed; +u64 size_needed;int copy, len;int mss_now;int interval;
@@ -2711,7 +2711,7 @@ static int tcp_mtu_probe(struct sock *sk) mss_now =tcp_current_mss (sk); probe_size =tcp_mtu_to_mss (sk, (icsk->icsk_mtup.search_high + icsk->icsk_mtup.search_low) >>1 ); - size_needed = probe_size + (tp->reordering +1 ) * tp->mss_cache; + size_needed = probe_size + (tp->reordering +1 ) * (u64 )tp->mss_cache; interval = icsk->icsk_mtup.search_high - icsk->icsk_mtup.search_low;/* When misfortune happens, we are reprobing actively, * and then reprobe timer has expired. We stick with current
Quoted 15 lines · On Wed, 10 Jun 2026 10:04:06 -0700 Eric Dumazet <[email protected]> wrote:
Quoted 14 lines · Ren Wei
From: Wyatt Feng <[email protected]>
Reject invalid
net.ipv4.tcp_reorderingvalues before they reach TCP socket state. The sysctl is stored as anintbut copied into theu32tp->reorderingfield for new sockets, so negative writes wrap to large values.With
tcp_mtu_probing=2, the wrapped value can overflow thetcp_mtu_probe()size calculation and drive the MTU probing path into an out-of-bounds read. Routetcp_reorderingwrites throughproc_dointvec_minmax()and clamp them to the per-netns range[1, tcp_max_reordering].
...
Quoted 23 lines
Thanks for the patch. I think we can do better.
Also I do not see a fix in
tcp_mtu_probe()?What about:
net/ipv4/sysctl_net_ipv4.c +0
-0100644
net/ipv4/sysctl_net_ipv4.c +3
-0@@ -1058,6 +1058,9 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_tcp_reordering, .maxlen = sizeof(int), .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ONE, + .extra2 = &init_net.ipv4.sysctl_tcp_max_reordering, .proc_handler = proc_dointvec }, {
net/ipv4/sysctl_net_ipv4.c static struct ctl_table ipv4_net_table[] = { · line 1300
Quoted 6 lines
net/ipv4/sysctl_net_ipv4.c +2
-1@@ -1293,7 +1296,8 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_tcp_max_reordering, .maxlen = sizeof(int), .mode = 0644, - .proc_handler = proc_dointvec + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ONE,
Would it be reasonable to put in a 'sanity' upper bound here?
Since tcp_max_reordering can be lowered after tcp_reordering is set the bound set here isn't 'hard'.
So the same sanity limit for both may be enough?
I also found this 'gem':
void tcp_enter_loss(struct sock *sk)
{
...
u8 reordering; ... reordering = READ_ONCE(net->ipv4.sysctl_tcp_reordering);
Which makes be think that someone expected these values to be small.
I don't know what the sane bounds are for either sysctl or tp->reordering itself (which is u32).
-- David
Quoted 60 lines · David Laight
Quoted 15 lines · On Wed, 10 Jun 2026 10:04:06 -0700 Eric Dumazet <[email protected]> wrote:
Quoted 14 lines · Ren Wei
From: Wyatt Feng <[email protected]>
Reject invalid
net.ipv4.tcp_reorderingvalues before they reach TCP socket state. The sysctl is stored as anintbut copied into theu32tp->reorderingfield for new sockets, so negative writes wrap to large values.With
tcp_mtu_probing=2, the wrapped value can overflow thetcp_mtu_probe()size calculation and drive the MTU probing path into an out-of-bounds read. Routetcp_reorderingwrites throughproc_dointvec_minmax()and clamp them to the per-netns range[1, tcp_max_reordering]....
Quoted 23 lines
Thanks for the patch. I think we can do better.
Also I do not see a fix in
tcp_mtu_probe()?What about:
net/ipv4/sysctl_net_ipv4.c +0
-0100644
net/ipv4/sysctl_net_ipv4.c +3
-0@@ -1058,6 +1058,9 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_tcp_reordering, .maxlen = sizeof(int), .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ONE, + .extra2 = &init_net.ipv4.sysctl_tcp_max_reordering, .proc_handler = proc_dointvec }, {net/ipv4/sysctl_net_ipv4.c static struct ctl_table ipv4_net_table[] = { · line 1300
Quoted 6 lines
net/ipv4/sysctl_net_ipv4.c +2
-1@@ -1293,7 +1296,8 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_tcp_max_reordering, .maxlen = sizeof(int), .mode = 0644, - .proc_handler = proc_dointvec + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ONE,Would it be reasonable to put in a 'sanity' upper bound here?
Since
tcp_max_reorderingcan be lowered aftertcp_reorderingis set the bound set here isn't 'hard'.
So the same sanity limit for both may be enough?I also found this 'gem':
voidtcp_enter_loss(struct sock *sk)
{
...
Quoted 8 lines
u8 reordering; ... reordering = READ_ONCE(net->ipv4.sysctl_tcp_reordering);Which makes be think that someone expected these values to be small.
I don't know what the sane bounds are for either sysctl or
tp->reorderingitself (which is u32).
Kuniyuki, can you please send a fix ? You used u8 in your commit :/
Default value of this sysctl is 3, but it could potentially exceed 255.
Thanks.
commit 46778cd16e6a5ad1b2e3a91f6c057c907379418e
Author: Kuniyuki Iwashima <[email protected]> Date: Fri Jul 15 10:17:49 2022 -0700
tcp: Fix data-races around sysctl_tcp_reordering.
While reading sysctl_tcp_reordering, it can be changed concurrently. Thus, we need to add READ_ONCE() to its readers.
Quoted 43 lines · David Laight
Quoted 15 lines · On Wed, 10 Jun 2026 10:04:06 -0700 Eric Dumazet <[email protected]> wrote:
Quoted 14 lines · Ren Wei
From: Wyatt Feng <[email protected]>
Reject invalid
net.ipv4.tcp_reorderingvalues before they reach TCP socket state. The sysctl is stored as anintbut copied into theu32tp->reorderingfield for new sockets, so negative writes wrap to large values.With
tcp_mtu_probing=2, the wrapped value can overflow thetcp_mtu_probe()size calculation and drive the MTU probing path into an out-of-bounds read. Routetcp_reorderingwrites throughproc_dointvec_minmax()and clamp them to the per-netns range[1, tcp_max_reordering]....
Quoted 23 lines
Thanks for the patch. I think we can do better.
Also I do not see a fix in
tcp_mtu_probe()?What about:
net/ipv4/sysctl_net_ipv4.c +0
-0100644
net/ipv4/sysctl_net_ipv4.c +3
-0@@ -1058,6 +1058,9 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_tcp_reordering, .maxlen = sizeof(int), .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ONE, + .extra2 = &init_net.ipv4.sysctl_tcp_max_reordering, .proc_handler = proc_dointvec }, {@@ -1293,7 +1296,8 @@ static struct ctl_table ipv4_net_table[] = {
Quoted 8 lines
net/ipv4/sysctl_net_ipv4.c static struct ctl_table ipv4_net_table[] = { · line 1300
Quoted 6 lines
net/ipv4/sysctl_net_ipv4.c +2
-1.data = &init_net.ipv4.sysctl_tcp_max_reordering, .maxlen = sizeof(int), .mode = 0644, - .proc_handler = proc_dointvec + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ONE,Would it be reasonable to put in a 'sanity' upper bound here?
Probably, I am not even sure we use this much with RACK anyway.
Neal, what do you think?
Quoted 21 lines
Since
tcp_max_reorderingcan be lowered aftertcp_reorderingis set the bound set here isn't 'hard'.
So the same sanity limit for both may be enough?I also found this 'gem':
voidtcp_enter_loss(struct sock *sk)
{
...u8 reordering; ... reordering = READ_ONCE(net->ipv4.sysctl_tcp_reordering);Which makes be think that someone expected these values to be small.
I don't know what the sane bounds are for either sysctl or
tp->reorderingitself (which is u32).-- David
Kuniyuki Iwashima
reviewerQuoted 67 lines · Eric Dumazet
Quoted 64 lines · David Laight
Quoted 15 lines · On Wed, 10 Jun 2026 10:04:06 -0700 Eric Dumazet <[email protected]> wrote:
Quoted 14 lines · Ren Wei
From: Wyatt Feng <[email protected]>
Reject invalid
net.ipv4.tcp_reorderingvalues before they reach TCP socket state. The sysctl is stored as anintbut copied into theu32tp->reorderingfield for new sockets, so negative writes wrap to large values.With
tcp_mtu_probing=2, the wrapped value can overflow thetcp_mtu_probe()size calculation and drive the MTU probing path into an out-of-bounds read. Routetcp_reorderingwrites throughproc_dointvec_minmax()and clamp them to the per-netns range[1, tcp_max_reordering]....
Quoted 23 lines
Thanks for the patch. I think we can do better.
Also I do not see a fix in
tcp_mtu_probe()?What about:
net/ipv4/sysctl_net_ipv4.c +0
-0100644
net/ipv4/sysctl_net_ipv4.c +3
-0@@ -1058,6 +1058,9 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_tcp_reordering, .maxlen = sizeof(int), .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ONE, + .extra2 = &init_net.ipv4.sysctl_tcp_max_reordering, .proc_handler = proc_dointvec }, {net/ipv4/sysctl_net_ipv4.c static struct ctl_table ipv4_net_table[] = { · line 1300
Quoted 6 lines
net/ipv4/sysctl_net_ipv4.c +2
-1@@ -1293,7 +1296,8 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_tcp_max_reordering, .maxlen = sizeof(int), .mode = 0644, - .proc_handler = proc_dointvec + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ONE,Would it be reasonable to put in a 'sanity' upper bound here?
Since
tcp_max_reorderingcan be lowered aftertcp_reorderingis set the bound set here isn't 'hard'.
So the same sanity limit for both may be enough?I also found this 'gem':
voidtcp_enter_loss(struct sock *sk)
{
...u8 reordering; ... reordering = READ_ONCE(net->ipv4.sysctl_tcp_reordering);
Quoted 7 lines
Quoted 4 lines
Which makes be think that someone expected these values to be small.
I don't know what the sane bounds are for either sysctl or
tp->reorderingitself (which is u32).Kuniyuki, can you please send a fix ? You used u8 in your commit :/
Oh sorry, I'll change it to int.
Quoted 17 lines
Default value of this sysctl is 3, but it could potentially exceed 255.
Thanks.
commit
46778cd16e6a5ad1b2e3a91f6c057c907379418eAuthor: Kuniyuki Iwashima <[email protected]> Date: Fri Jul 15 10:17:49 2022 -0700tcp: Fix data-races around
sysctl_tcp_reordering.While reading
sysctl_tcp_reordering, it can be changed concurrently. Thus, we need to addREAD_ONCE()to its readers.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Kuniyuki Iwashima <[email protected]>Signed-off-by David S. Miller <[email protected]>