From: Kuniyuki Iwashima <kuniyu@google.com>
To: oss@malat.biz
Cc: davem@davemloft.net, ebiggers@google.com, kuba@kernel.org,
linux-kernel@vger.kernel.org, netdev@vger.kernel.org
Subject: Re: [PATCH] net: datagram: Drain queue before reporting EOF or ENOTCONN
Date: Tue, 28 Apr 2026 05:58:41 +0000 [thread overview]
Message-ID: <20260428055851.2204248-1-kuniyu@google.com> (raw)
In-Reply-To: <CANMuvJn15HHBrq8EEbhSfCk-Sn33WEEovRa-yVWF5huTKfoOYA@mail.gmail.com>
From: Petr Malat <oss@malat.biz>
Date: Mon, 27 Apr 2026 22:23:33 -0700
> If a packet is queued and RCV_SHUTDOWN flag is set after the function
> __skb_wait_for_more_packets() checked the queue, the function returns
> EOF, which is then propagated by __unix_dgram_recvmsg() and the user
> reads EOF although there is a message or messages still pending.
>
> The function should check if the queue is empty before returning EOF.
> As the same is true for disconnect and it's also reasonable for a pending
> signal, check in a common place before returning from the function.
>
> Signed-off-by: Petr Malat <oss@malat.biz>
> ---
> net/core/datagram.c | 38 +++++++++++++++++++++-----------------
> 1 file changed, 21 insertions(+), 17 deletions(-)
>
> diff --git a/net/core/datagram.c b/net/core/datagram.c
> index c285c6465923..5952950f7233 100644
> --- a/net/core/datagram.c
> +++ b/net/core/datagram.c
> @@ -98,40 +98,44 @@ int __skb_wait_for_more_packets(struct sock *sk,
> struct sk_buff_head *queue,
> /* Socket errors? */
> error = sock_error(sk);
> if (error)
> - goto out_err;
> + goto out;
>
> if (READ_ONCE(queue->prev) != skb)
> goto out;
>
> /* Socket shut down? */
> - if (sk->sk_shutdown & RCV_SHUTDOWN)
> - goto out_noerr;
> + if (sk->sk_shutdown & RCV_SHUTDOWN) {
> + error = 1;
> + goto check_queue;
We already have checked the same condition just above, and this
is a matter of timing.
Even after the duplicated check is evaluated to false, there is
a small chance that the concurrent sendmsg() enqueues a new skb.
Considering __skb_wait_for_more_packets() is called only when
the queue is empty, it's not worth another round when shutdown()ed.
> + }
>
> /* Sequenced packets can come disconnected.
> * If so we report the problem
> */
> - error = -ENOTCONN;
> if (connection_based(sk) &&
> - !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
> - goto out_err;
> + !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN)) {
> + error = -ENOTCONN;
Also, the queue is always empty if SOCK_SEQPACKET sk is at TCP_CLOSE.
> + goto check_queue;
> + }
>
> /* handle signals */
> - if (signal_pending(current))
> - goto interrupted;
> + if (signal_pending(current)) {
> + error = sock_intr_errno(*timeo_p);
> + goto check_queue;
and we don't want to delay signal if it arrived first.
> + }
>
> - error = 0;
> *timeo_p = schedule_timeout(*timeo_p);
> out:
> + *err = error < 0 ? error : 0;
> finish_wait(sk_sleep(sk), &wait);
> return error;
> -interrupted:
> - error = sock_intr_errno(*timeo_p);
> -out_err:
> - *err = error;
> - goto out;
> -out_noerr:
> - *err = 0;
> - error = 1;
> +check_queue:
> + /* A packet may have arrived between the initial queue check and any
> + * of the early-exit conditions above. Return 0 to let the caller
> + * drain the queue before acting on the shutdown / disconnect / signal.
> + */
> + if (READ_ONCE(queue->prev) != skb)
> + error = 0;
> goto out;
> }
> EXPORT_SYMBOL(__skb_wait_for_more_packets);
> --
> 2.47.3
>
next prev parent reply other threads:[~2026-04-28 5:58 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-28 5:23 [PATCH] net: datagram: Drain queue before reporting EOF or ENOTCONN Petr Malat
2026-04-28 5:58 ` Kuniyuki Iwashima [this message]
2026-04-28 6:49 ` Petr Malat
2026-04-28 19:45 ` 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=20260428055851.2204248-1-kuniyu@google.com \
--to=kuniyu@google.com \
--cc=davem@davemloft.net \
--cc=ebiggers@google.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=oss@malat.biz \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox