All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jiayuan Chen" <jiayuan.chen@linux.dev>
To: "Eric Dumazet" <edumazet@google.com>
Cc: netdev@vger.kernel.org, mrpre@163.com,
	"Neal Cardwell" <ncardwell@google.com>,
	"Kuniyuki Iwashima" <kuniyu@google.com>,
	"David S. Miller" <davem@davemloft.net>,
	"David Ahern" <dsahern@kernel.org>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Simon Horman" <horms@kernel.org>,
	"David Howells" <dhowells@redhat.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH net-next v1] tcp: Correct signedness in skb remaining space calculation
Date: Thu, 03 Jul 2025 12:03:33 +0000	[thread overview]
Message-ID: <6724e69057445ab66d70f0b28c115e2d8fb5543b@linux.dev> (raw)
In-Reply-To: <CANn89iL=GR5iHXUQ6Jor_rjkn91vuL5w8DCrxwJRQGSO7zmQ-w@mail.gmail.com>

2025/7/2 23:34, "Eric Dumazet" <edumazet@google.com> 写到:



> 
> On Wed, Jul 2, 2025 at 8:28 AM Jiayuan Chen <jiayuan.chen@linux.dev> wrote:
> 
> > 
> > July 2, 2025 at 22:02, "Eric Dumazet" <edumazet@google.com> wrote:
> > 
> >  On Wed, Jul 2, 2025 at 6:59 AM Eric Dumazet <edumazet@google.com> wrote:
> > 
> >  >
> > 
> >  > On Wed, Jul 2, 2025 at 6:42 AM Jiayuan Chen <jiayuan.chen@linux.dev> wrote:
> > 
> >  >
> > 
> >  > July 2, 2025 at 19:00, "Jiayuan Chen" <jiayuan.chen@linux.dev> wrote:
> > 
> >  >
> > 
> >  > >
> > 
> >  >
> > 
> >  > > 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 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.
> > 
> >  >
> > 
> >  > >
> > 
> >  >
> > 
> >  > 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
> > 
> >  >
> > 
> >  > '''
> > 
> >  >
> > 
> >  > 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.
> > 
> >  >
> > 
> >  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.
> > 
> 
> 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.

  reply	other threads:[~2025-07-03 12:03 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-02 11:00 [PATCH net-next v1] tcp: Correct signedness in skb remaining space calculation Jiayuan Chen
2025-07-02 13:41 ` Jiayuan Chen
2025-07-02 13:59   ` Eric Dumazet
2025-07-02 14:02     ` Eric Dumazet
2025-07-02 15:27       ` Jiayuan Chen
2025-07-02 15:34         ` Eric Dumazet
2025-07-03 12:03           ` Jiayuan Chen [this message]
2025-07-03 12:06             ` Jiayuan Chen
2025-07-03 12:33             ` Eric Dumazet

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=6724e69057445ab66d70f0b28c115e2d8fb5543b@linux.dev \
    --to=jiayuan.chen@linux.dev \
    --cc=davem@davemloft.net \
    --cc=dhowells@redhat.com \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mrpre@163.com \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.