tcp: Correct signedness in skb remaining space calculation
Subsystems (3)
PATCH 1/1 tcp: Correct signedness in skb remaining space calculation
The calculation for the remaining space, 'copy = size_goal - skb->len', was prone to an integer promotion bug that prevented copy from ever being negative.
The variable types involved are:
copy: ssize_t (long)size_goal: intskb->len: unsigned int
Due to C's type promotion rules, the signed size_goal is converted to an unsigned int to match skb->len before the subtraction. The result is an unsigned int.
When this unsigned int result is then assigned to the s64 copy variable, it is zero-extended, preserving its non-negative value. Consequently, copy is always >= 0.
The intended logic is that a negative copy value indicates that the tail skb lacks sufficient space for appending new data, which should trigger the allocation of a new skb. Because of this bug, the condition copy <= 0 was never met, causing the code to always append to the tail skb.
--- net/ipv4/tcp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/tcp.c +1 -1
@@ -1180,7 +1180,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) skb =tcp_write_queue_tail (sk);if (skb) - copy = size_goal - skb->len; + copy = size_goal - (ssize_t )skb->len;trace_tcp_sendmsg_locked (sk, msg, skb, size_goal);
Signature · 1 line
2.47.1
Quoted 19 lines · Jiayuan Chen
The calculation for the remaining space, 'copy =
size_goal-skb->len',was prone to an integer promotion bug that prevented copy from ever being
negative.
The variable types involved are:
copy:
ssize_t(long)
size_goal: int
skb->len: unsigned intDue to C's type promotion rules, the signed
size_goalis converted to anunsigned int to match
skb->lenbefore the subtraction. The result is an
Quoted 8 lines
unsigned int.
When this unsigned int result is then assigned to the s64 copy variable,
it is zero-extended, preserving its non-negative value. Consequently,
copy is always >= 0.
To better explain this problem, consider the following example:
'''
#include <sys/types.h>
#include <stdio.h>
int size_goal = 536;
unsigned int skblen = 1131;
void main() {
ssize_t copy = 0;
copy = size_goal - skblen;
printf("wrong: %zd\n", copy);
copy = size_goal - (ssize_t)skblen;
printf("correct: %zd\n", copy);
return;
}'''
Output:
'''
wrong: 4294966701
correct: -595
'''
Quoted 48 lines · Jiayuan Chen
Quoted 19 lines · Jiayuan Chen
The calculation for the remaining space, 'copy =
size_goal-skb->len',was prone to an integer promotion bug that prevented copy from ever being
negative.
The variable types involved are:
copy:
ssize_t(long)
size_goal: int
skb->len: unsigned intDue to C's type promotion rules, the signed
size_goalis converted to anunsigned int to match
skb->lenbefore the subtraction. The result is anQuoted 8 lines
unsigned int.
When this unsigned int result is then assigned to the s64 copy variable,
it is zero-extended, preserving its non-negative value. Consequently,
copy is always >= 0.
To better explain this problem, consider the following example:
'''
#include <sys/types.h>
#include <stdio.h>
intsize_goal= 536;
unsigned int skblen = 1131;void main() { ssize_t copy = 0; copy = size_goal - skblen; printf("wrong: %zd\n", copy); copy = size_goal - (ssize_t)skblen; printf("correct: %zd\n", copy); return; }
Quoted 6 lines
'''
Output:
'''
wrong: 4294966701
correct: -595
'''
Can you explain how one skb could have more bytes (skb->len) than size_goal ?
If we are under this condition, we already have a prior bug ?
Please describe how you caught this issue.
Quoted 57 lines · Eric Dumazet
Quoted 48 lines · Jiayuan Chen
Quoted 19 lines · Jiayuan Chen
The calculation for the remaining space, 'copy =
size_goal-skb->len',was prone to an integer promotion bug that prevented copy from ever being
negative.
The variable types involved are:
copy:
ssize_t(long)
size_goal: int
skb->len: unsigned intDue to C's type promotion rules, the signed
size_goalis converted to anunsigned int to match
skb->lenbefore the subtraction. The result is anQuoted 8 lines
unsigned int.
When this unsigned int result is then assigned to the s64 copy variable,
it is zero-extended, preserving its non-negative value. Consequently,
copy is always >= 0.
To better explain this problem, consider the following example:
'''
#include <sys/types.h>
#include <stdio.h>
intsize_goal= 536;
unsigned int skblen = 1131;void main() { ssize_t copy = 0; copy = size_goal - skblen; printf("wrong: %zd\n", copy); copy = size_goal - (ssize_t)skblen; printf("correct: %zd\n", copy); return; }Quoted 6 lines
'''
Output:
'''
wrong: 4294966701
correct: -595
'''
Quoted 5 lines
Can you explain how one skb could have more bytes (
skb->len) thansize_goal?If we are under this condition, we already have a prior bug ?
Please describe how you caught this issue.
Also, not sure why copy variable had to be changed from "int" to "ssize_t"
A nicer patch (without a cast) would be to make it an "int" again/
Quoted 104 lines · Eric Dumazet
Quoted 101 lines · Eric Dumazet
On Wed, Jul 2, 2025 at 6:42 AM Jiayuan Chen <[email protected]> wrote:
Quoted 46 lines · Jiayuan Chen
The calculation for the remaining space, 'copy =
size_goal-skb->len',was prone to an integer promotion bug that prevented copy from ever being
negative.
The variable types involved are:
copy:
ssize_t(long)
size_goal: int
skb->len: unsigned intDue to C's type promotion rules, the signed
size_goalis converted to anunsigned int to match
skb->lenbefore the subtraction. The result is anunsigned int.
When this unsigned int result is then assigned to the s64 copy variable,
Quoted 7 lines
it is zero-extended, preserving its non-negative value. Consequently,
copy is always >= 0.
To better explain this problem, consider the following example:
'''
#include <
sys/types.h>#include <
stdio.h>int
size_goal= 536;unsigned int skblen = 1131;
void
main(){
ssize_tcopy = 0;copy = size_goal - skblen;
printf("wrong: %zd\n", copy);copy = size_goal - (ssize_t)skblen;
printf("correct: %zd\n", copy);return;
}
'''
Output:
'''
wrong: 4294966701
correct: -595
'''
Can you explain how one skb could have more bytes (
skb->len) thansize_goal?
Quoted 9 lines
Quoted 4 lines
If we are under this condition, we already have a prior bug ?
Please describe how you caught this issue.
Also, not sure why copy variable had to be changed from "int" to "
ssize_t"A nicer patch (without a cast) would be to make it an "int" again/
I encountered this issue because I had tcp_repair enabled, which uses tcp_init_tso_segs to reset the MSS.
However, it seems that tcp_bound_to_half_wnd also dynamically adjusts the value to be smaller than the current size_goal.
Looking at the commit history, it's indeed unnecessary to define the copy variable as type ssize_t.
Quoted 116 lines · Jiayuan Chen
Quoted 111 lines · Eric Dumazet
Quoted 97 lines · Eric Dumazet
On Wed, Jul 2, 2025 at 6:42 AM Jiayuan Chen <[email protected]> wrote:
Quoted 46 lines · Jiayuan Chen
The calculation for the remaining space, 'copy =
size_goal-skb->len',was prone to an integer promotion bug that prevented copy from ever being
negative.
The variable types involved are:
copy:
ssize_t(long)
size_goal: int
skb->len: unsigned intDue to C's type promotion rules, the signed
size_goalis converted to anunsigned int to match
skb->lenbefore the subtraction. The result is anunsigned int.
When this unsigned int result is then assigned to the s64 copy variable,
Quoted 7 lines
it is zero-extended, preserving its non-negative value. Consequently,
copy is always >= 0.
To better explain this problem, consider the following example:
'''
#include <
sys/types.h>#include <
stdio.h>int
size_goal= 536;unsigned int skblen = 1131;
void
main(){
ssize_tcopy = 0;copy = size_goal - skblen;
printf("wrong: %zd\n", copy);copy = size_goal - (ssize_t)skblen;
printf("correct: %zd\n", copy);return;
}
'''
Output:
'''
wrong: 4294966701
correct: -595
Quoted 8 lines
'''
Can you explain how one skb could have more bytes (
skb->len) thansize_goal?If we are under this condition, we already have a prior bug ?
Please describe how you caught this issue.
Also, not sure why copy variable had to be changed from "int" to "
ssize_t"
Quoted 8 lines
Quoted 2 lines
A nicer patch (without a cast) would be to make it an "int" again/
I encountered this issue because I had
tcp_repairenabled, which usestcp_init_tso_segsto reset the MSS.
However, it seems thattcp_bound_to_half_wndalso dynamically adjusts the value to be smaller than the currentsize_goal.
Okay, and what was the end result ?
An skb has a limited amount of bytes that can be put into it (MAX_SKB_FRAGS * 32K) , and I can't see what are the effects of having an
"not optimally sized skb in socket write queue".
BTW if you have a tcp_repair test, I would love having it in the tools/testing/selftests/net :)
Thanks.
Quoted 2 lines
Looking at the commit history, it's indeed unnecessary to define the copy variable as type
ssize_t.
2025/7/2 23:34, "Eric Dumazet" <[email protected]> 写到:
Quoted 228 lines · Jiayuan Chen
Quoted 226 lines
July 2, 2025 at 22:02, "Eric Dumazet" <[email protected]> wrote:
Quoted 202 lines · Eric Dumazet
On Wed, Jul 2, 2025 at 6:42 AM Jiayuan Chen <[email protected]> wrote:
Quoted 100 lines · Jiayuan Chen
The calculation for the remaining space, 'copy =
size_goal-skb->len',was prone to an integer promotion bug that prevented copy from ever being
negative.
The variable types involved are:
copy:
ssize_t(long)
size_goal: int
skb->len: unsigned intDue to C's type promotion rules, the signed
size_goalis converted to anunsigned int to match
skb->lenbefore the subtraction. The result is anunsigned int.
When this unsigned int result is then assigned to the s64 copy variable,
it is zero-extended, preserving its non-negative value. Consequently,
Quoted 5 lines
copy is always >= 0.
To better explain this problem, consider the following example:
'''
#include <
sys/types.h>#include <
stdio.h>int
size_goal= 536;unsigned int skblen = 1131;
void
main(){
ssize_tcopy = 0;copy =
size_goal- skblen;
printf("wrong: %zd\n", copy);copy =
size_goal- (ssize_t)skblen;
printf("correct: %zd\n", copy);return;
}
'''
Output:
'''
wrong: 4294966701
correct: -595
'''
Can you explain how one skb could have more bytes (
skb->len) thansize_goal?Quoted 7 lines
If we are under this condition, we already have a prior bug ?
Please describe how you caught this issue.
Also, not sure why copy variable had to be changed from "int" to "
ssize_t"A nicer patch (without a cast) would be to make it an "int" again/
I encountered this issue because I had
tcp_repairenabled, which uses
tcp_init_tso_segsto reset the MSS.However, it seems that
tcp_bound_to_half_wndalso dynamically adjusts
Quoted 9 lines
Quoted 2 lines
the value to be smaller than the current
size_goal.Okay, and what was the end result ?
An skb has a limited amount of bytes that can be put into it
(
MAX_SKB_FRAGS* 32K) , and I can't see what are the effects of having
Hi Eric,
I'm working with a reproducer generated by syzkaller [1], and its core logic is roughly as follows:
'''setsockopt(fd, TCP_REPAIR, 1)
connect(fd);setsockopt(fd, TCP_REPAIR, -1)
send(fd, small);sendmmsg(fd, buffer_2G);
'''
First, because TCP_REPAIR is enabled, the send() operation leaves the skb at the tail of the write_queue. Subsequently, sendmmsg is called to send 2GB of data.
Due to TCP_REPAIR, the size_goal is reduced, which can cause the copy variable to become negative. However, because of integer promotion bug mentioned in the previous email, this negative value is misinterpreted as a large positive number. Ultimately, copy becomes a huge value, approaching the int32 limit. This, in turn, causes sk->sk_forward_alloc to overflow, which is the exact issue reported by syzkaller.
On a related note, even without using TCP_REPAIR, the tcp_bound_to_half_wnd() function can also reduce size_goal on its own. Therefore, my understanding is that under extreme conditions, we might still encounter an overflow in sk->sk_forward_alloc.
So, I think we have good reason to change copy to an int.
2025/7/3 20:03, "Jiayuan Chen" <[email protected]> 写到:
Quoted 4 lines
Hi Eric,
I'm working with a reproducer generated by syzkaller [1], and its core
https://syzkaller.appspot.com/bug?extid=de6565462ab540f50e47
(sorry losting the link...)
Quoted 25 lines · Jiayuan Chen
Hi Eric,
I'm working with a reproducer generated by syzkaller [1], and its core logic is roughly as follows:
'''
setsockopt(fd, TCP_REPAIR, 1)
connect(fd);setsockopt(fd, TCP_REPAIR, -1)
send(fd, small);sendmmsg(fd, buffer_2G);
'''First, because
TCP_REPAIRis enabled, thesend()operation leaves the skb at the tail of thewrite_queue. Subsequently, sendmmsg is called to send 2GB of data.Due to
TCP_REPAIR, thesize_goalis reduced, which can cause the copy variable to become negative. However, because of integer promotion bug mentioned in the previous email, this negative value is misinterpreted as a large positive number. Ultimately, copy becomes a huge value, approaching the int32 limit. This, in turn, causessk->sk_forward_allocto overflow, which is the exact issue reported by syzkaller.
Quoted 6 lines
On a related note, even without using
TCP_REPAIR, thetcp_bound_to_half_wnd()function can also reducesize_goalon its own. Therefore, my understanding is that under extreme conditions, we might still encounter an overflow insk->sk_forward_alloc.So, I think we have good reason to change copy to an int.
Ok, I wish you had stated you were working on a syzbot report from the very beginning.
Why hiding ?
Please send a V2 of the patch.