All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jens Axboe <axboe@kernel.dk>
To: stable@vger.kernel.org
Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org
Subject: [PATCH 6.6.y 10/13] io_uring/net: restore the cmsg buffer on recvmsg retry
Date: Sat,  5 Dec 2026 23:12:30 +0000	[thread overview]
Message-ID: <20261205231233.4140988-11-axboe@kernel.dk> (raw)
In-Reply-To: <20261205231233.4140988-1-axboe@kernel.dk>

From: Chris Mason <mason@kernel.org>

Commit 437e79d6a2a7aba81d56c11198576a72574e6d52 upstream.

A single-shot IORING_OP_RECVMSG that is retried after -EAGAIN can report
the control messages of the failed attempt, and hide those that came
with the data. On an AF_UNIX stream socket with SO_PASSCRED, the caller
gets SCM_CREDENTIALS with pid, uid and gid 0, and never sees the
descriptors that an SCM_RIGHTS message installed.

io_recvmsg() passes the msghdr that was set up at prep to each attempt,
so a failed ->recvmsg() has to leave msg_control and msg_controllen as
they were. recvmsg(2) doesn't depend on that, since it copies the user's
msghdr for each call. unix_stream_read_generic() calls scm_recv_unix()
whatever its result is, with a zeroed scm_cookie if the queue was empty,
and put_cmsg() advances the control buffer. ____sys_recvmsg() then
counts the reported length from where its call started, but the caller
reads from the head of its buffer:

    msg_control = C, msg_controllen = 64

    first attempt, the queue is empty
      put_cmsg(SCM_CREDENTIALS {0, 0, 0}) at C
      msg_control = C + 32, msg_controllen = 32
      return -EAGAIN

    second attempt
      cmsg_ptr = C + 32
      put_cmsg(SCM_CREDENTIALS of the sender) at C + 32
      msg_control = C + 64, msg_controllen = 0
      put_user(msg_control - cmsg_ptr = 32, &umsg->msg_controllen)

The fix used here is to save the control buffer before each attempt, and
to restore it if the attempt fails with -EAGAIN and will be retried.

The tradeoff: with MSG_WAITALL, a retry whose control buffer the failed
attempts had used up went on after a short receive until the data buffer
was full. It now completes short, as every recvmsg with a control buffer
does since commit 78d0d2063bab ("io_uring/net: disable partial retries
for recvmsg with cmsg").

Fixes: e54937963fa2 ("net: remove cmsg restriction from io_uring based send/recvmsg calls")
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
[axboe: 6.6 retries through io_setup_async_msg(), which copies the
msghdr on the first -EAGAIN. Restore the control buffer before that
call]
---
 io_uring/net.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/io_uring/net.c b/io_uring/net.c
index 3705b50673eb..4f4a386d6a33 100644
--- a/io_uring/net.c
+++ b/io_uring/net.c
@@ -828,6 +828,8 @@ int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
 	int ret, min_ret = 0;
 	bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
 	bool mshot_finished = true;
+	void __user *control;
+	size_t controllen;
 
 	sock = sock_from_file(req->file);
 	if (unlikely(!sock))
@@ -876,6 +878,8 @@ int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
 
 	kmsg->msg.msg_get_inq = 1;
 	kmsg->msg.msg_inq = -1;
+	control = kmsg->msg.msg_control_user;
+	controllen = kmsg->msg.msg_controllen;
 	if (req->flags & REQ_F_APOLL_MULTISHOT) {
 		ret = io_recvmsg_multishot(sock, sr, kmsg, flags,
 					   &mshot_finished);
@@ -890,6 +894,12 @@ int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
 
 	if (ret < min_ret) {
 		if (ret == -EAGAIN && force_nonblock) {
+			/*
+			 * Don't depend on the control buffer being untouched, a
+			 * protocol may post a cmsg before it finds no data.
+			 */
+			kmsg->msg.msg_control_user = control;
+			kmsg->msg.msg_controllen = controllen;
 			ret = io_setup_async_msg(req, kmsg, issue_flags);
 			if (ret == -EAGAIN && (issue_flags & IO_URING_F_MULTISHOT)) {
 				io_kbuf_recycle(req, issue_flags);
-- 
2.55.0


  parent reply	other threads:[~2026-12-05 23:12 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 23:12 [PATCH 6.6.y 00/13] io_uring fixes for 6.6-stable Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 01/13] io_uring/net: don't charge the folio offset to truesize for managed frags Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 02/13] io_uring/net: don't copy msg_name on a recvmsg retry Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 03/13] io_uring/tctx: add separate lock for list of tctx's in ctx Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 04/13] io_uring: free the task context after a grace period Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 05/13] io_uring: free the ring " Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 06/13] io_uring/filetable: don't install O_PATH files Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 07/13] io_uring: make ctx->timeout_lock a raw spinlock Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 08/13] io_uring/timeout: flush timeouts outside of the timeout lock Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 09/13] io_uring/cancel: always walk links under timeout_lock Jens Axboe
2026-12-05 23:12 ` Jens Axboe [this message]
2026-12-05 23:12 ` [PATCH 6.6.y 11/13] io_uring_poll: kill the no longer necessary barrier after poll_wait() Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 12/13] io_uring: close poll activation race when enabling rings Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 13/13] io_uring/cancel: arm local work wakeups only for submitter Jens Axboe

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=20261205231233.4140988-11-axboe@kernel.dk \
    --to=axboe@kernel.dk \
    --cc=gregkh@linuxfoundation.org \
    --cc=io-uring@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /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.