All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@google.com>
To: Eric Dumazet <edumazet@kernel.org>
Cc: Neal Cardwell <ncardwell@google.com>,
	"David S . Miller" <davem@davemloft.net>,
	 Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>, Simon Horman <horms@kernel.org>,
	 Kuniyuki Iwashima <kuni1840@gmail.com>,
	netdev@vger.kernel.org
Subject: Re: [PATCH v1 net 1/8] tcp: Don't accept ICMP for SND.NXT in TCP_SYN_SENT.
Date: Tue, 17 Nov 2026 23:30:28 +0000	[thread overview]
Message-ID: <avzfabq_y7Y-5Kwi@google.com> (raw)
In-Reply-To: <CAL4WiioDbGpSGexQGczb84dc2wLy=qjhF38xQ_S1YaRZsM30bA@mail.gmail.com>

On Tue, 17 Nov 2026 09:35:41 +0100, Eric Dumazet wrote:
> Le mar. 17 nov. 2026 à 06:35, Kuniyuki Iwashima <kuniyu@google.com> a écrit :
> >
> > RFC 5927 states that the acceptable ICMP has seq in
> > SND.UNA =< SEG.SEQ < SND.NXT:
> >
> >   These implementations check that the TCP sequence number
> >   contained in the payload of the ICMP error message is within
> >   the range SND.UNA =< SEG.SEQ < SND.NXT.
> >
> > tcp_v4_err() and tcp_v6_err() accept packets if seq meets
> >
> >   between(seq, snd_una, tp->snd_nxt)
> >   -> tp->snd_nxt - snd_una >= seq - snd_una
> >
> > , which includes both ends.
> >
> > This is fine once connection is established because
> > tcp_acceptable_seq() uses SND_NXT for ACK and RST.
> >
> > However, we should not accept ICMP to SND_NXT for SYN_SENT.
> >
> > The minimal repro is:
> >
> >    --ip_version=ipv4
> >     0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
> >    +0 fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0
> >    +0 connect(3, ..., ...) = -1 EINPROGRESS (Operation now in progress)
> >    +0 > S 0:0(0) <...>
> >    +0 < icmp unreachable host_unreachable [1:1(0)]
> >    +0 %{assert tcpi_state == TCP_SYN_SENT}%
> >
> > Let's fix off-by-one.
> >
> > Note that we need to check seq != snd_una because tp->snd_nxt
> > is only advanced to tp->write_seq after tcp_transmit_skb().
> > Otherwise, ICMP for SYN could be dropped before tcp_connect()
> > advances tp->snd_nxt.
> 
> This hints at a separate issue.
> 
> An ICMP can only be processed in this window if it was generated
> locally and looped back while tcp_transmit_skb() is running
> (netfilter REJECT in OUTPUT for instance).
> 
> At this point connect() owns the socket, so we end up in :
> 
>         if (!sock_owned_by_user(sk))
>                 tcp_done_with_error(sk, err);
>         else
>                 WRITE_ONCE(sk->sk_err_soft, err);
> 
> The socket stays in TCP_SYN_SENT, SYN is retransmitted until
> tcp_write_err(), and a non blocking user gets no EPOLLERR.
> 
> I think the repro in patch 8 only passes because SO_ERROR falls back
> to sk_err_soft. Could you check tcpi_state there ?

Ah yes, just after checking SO_ERROR, sk->sk_state is still at
TCP_SYN_SENT and it transitions to TCP_CLOSE only at SYN retrans.

  --ip_version=ipv6

   0 `ip link set lo up`
  +0 `ip6tables -A OUTPUT -p tcp -j REJECT`
  +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 3
  +0 setsockopt(3, SOL_SOCKET, SO_BINDTODEVICE, "tun0", 4) = 0
  +0 connect(3, ..., ...) = -1 EINPROGRESS (Operation now in progress)
  +0 getsockopt(3, SOL_SOCKET, SO_ERROR, [ECONNREFUSED], [4]) = 0
  +0 %{ assert tcpi_state == TCP_SYN_SENT, tcpi_state }%
+1.2 %{ assert tcpi_state == TCP_CLOSE, tcpi_state }%


Maybe we could change TCP_MTU_REDUCED_DEFERRED to TCP_ICMP_DEFERRED
and have a new flag set for mtu etc in a hole in cold cache line ?


> 
> Also, given an attacker has to guess the ISN anyway, I am not sure
> this one is net material.

Fair, I'll post this to net-next later.


> 
> >
> > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> > Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
> > ---
> >  net/ipv4/tcp_ipv4.c | 9 ++++++---
> >  net/ipv6/tcp_ipv6.c | 9 ++++++---
> >  2 files changed, 12 insertions(+), 6 deletions(-)
> >
> > diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
> > index 2fc92a7884fc..7d58f522068d 100644
> > --- a/net/ipv4/tcp_ipv4.c
> > +++ b/net/ipv4/tcp_ipv4.c
> > @@ -499,8 +499,8 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)
> >         const int type = icmp_hdr(skb)->type;
> >         const int code = icmp_hdr(skb)->code;
> >         struct request_sock *fastopen;
> > +       u32 seq, snd_una, snd_nxt;
> >         struct tcp_sock *tp;
> > -       u32 seq, snd_una;
> >         struct sock *sk;
> >         int err;
> >
> > @@ -555,9 +555,12 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)
> >         tp = tcp_sk(sk);
> >         /* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
> >         fastopen = rcu_dereference(tp->fastopen_rsk);
> > -       snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una;
> > +       snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : READ_ONCE(tp->snd_una);
> > +       snd_nxt = READ_ONCE(tp->snd_nxt);
> >         if (sk->sk_state != TCP_LISTEN &&
> > -           !between(seq, snd_una, tp->snd_nxt)) {
> > +           (!between(seq, snd_una, snd_nxt) ||
> > +            (sk->sk_state == TCP_SYN_SENT &&
> > +             seq != snd_una && seq == snd_nxt))) {
> 
> 
> In TCP_SYN_SENT the only sequence we ever put on the wire is the ISN,
> including the Fast Open case (SYN+data is sent with seq == ISN, and the
> RTO retransmits the bare SYN).
> 
> So this could simply be :
> 
>         if (sk->sk_state != TCP_LISTEN &&
>             (!between(seq, snd_una, snd_nxt) ||
>              (sk->sk_state == TCP_SYN_SENT && seq != snd_una))) {
> 
> This mirrors the (seq != tcp_rsk(req)->snt_isn) test in tcp_req_err(),
> and does not need the special case for the window where snd_nxt has
> not been advanced yet.

Looks much better, I'll use it.

Thank you !


> 
> >                 __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
> >                 goto out;
> >         }
> > diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
> > index 5c7ed38f1f56..8691d220cf94 100644
> > --- a/net/ipv6/tcp_ipv6.c
> > +++ b/net/ipv6/tcp_ipv6.c
> > @@ -410,9 +410,9 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
> >         const struct tcphdr *th = (struct tcphdr *)(skb->data+offset);
> >         struct net *net = dev_net_rcu(skb->dev);
> >         struct request_sock *fastopen;
> > +       __u32 seq, snd_una, snd_nxt;
> >         struct ipv6_pinfo *np;
> >         struct tcp_sock *tp;
> > -       __u32 seq, snd_una;
> >         struct sock *sk;
> >         bool fatal;
> >         int err;
> > @@ -463,9 +463,12 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
> >         tp = tcp_sk(sk);
> >         /* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
> >         fastopen = rcu_dereference(tp->fastopen_rsk);
> > -       snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una;
> > +       snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : READ_ONCE(tp->snd_una);
> > +       snd_nxt = READ_ONCE(tp->snd_nxt);
> >         if (sk->sk_state != TCP_LISTEN &&
> > -           !between(seq, snd_una, tp->snd_nxt)) {
> > +           (!between(seq, snd_una, snd_nxt) ||
> > +            (sk->sk_state == TCP_SYN_SENT &&
> > +             seq != snd_una && seq == snd_nxt))) {
> >                 __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
> >                 goto out;
> >         }
> > --
> > 2.56.0.679.gc527c65306-goog
> >

  reply	other threads:[~2026-11-17 23:30 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  5:35 [PATCH v1 net 0/8] tcp: Misc fixes for tcp_v[46]_err() Kuniyuki Iwashima
2026-11-17  5:35 ` [PATCH v1 net 1/8] tcp: Don't accept ICMP for SND.NXT in TCP_SYN_SENT Kuniyuki Iwashima
2026-11-17  8:35   ` Eric Dumazet
2026-11-17 23:30     ` Kuniyuki Iwashima [this message]
2026-11-17  5:35 ` [PATCH v1 net 2/8] tcp: Don't use inner IP TTL for IP_MINTTL in tcp_v4_err() Kuniyuki Iwashima
2026-11-17  8:42   ` Eric Dumazet
2026-11-17 23:35     ` Kuniyuki Iwashima
2026-11-17  5:35 ` [PATCH v1 net 3/8] tcp: Don't bypass GTSM check for reqsk Kuniyuki Iwashima
2026-11-17  8:45   ` Eric Dumazet
2026-11-17 23:42     ` Kuniyuki Iwashima
2026-11-17  5:35 ` [PATCH v1 net 4/8] tcp: Fix reqsk abort condition on ICMP errors Kuniyuki Iwashima
2026-11-17  8:49   ` Eric Dumazet
2026-11-17 23:44     ` Kuniyuki Iwashima
2026-11-17  5:35 ` [PATCH v1 net 5/8] tcp: Check ip_sk_accept_pmtu() in tcp_v4_err() Kuniyuki Iwashima
2026-11-17  8:54   ` Eric Dumazet
2026-11-17  5:35 ` [PATCH v1 net 6/8] selftest: net: packetdrill: Add ICMP and ICMPv6 tests Kuniyuki Iwashima
2026-11-17  8:58   ` Eric Dumazet
2026-11-17 23:45     ` Kuniyuki Iwashima
2026-11-17  5:35 ` [PATCH v1 net 7/8] tcp: Pass inet_sdif() to __inet_lookup_established() in tcp_v4_err() Kuniyuki Iwashima
2026-11-17  9:01   ` Eric Dumazet
2026-11-17  5:35 ` [PATCH v1 net 8/8] tcp: Pass inet6_iif() to __inet6_lookup_established() in tcp_v6_err() Kuniyuki Iwashima
2026-11-17  9:03   ` Eric Dumazet
2026-11-17 23:56     ` Kuniyuki Iwashima

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=avzfabq_y7Y-5Kwi@google.com \
    --to=kuniyu@google.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuni1840@gmail.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.