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 7.3.y 09/15] io_uring/net: restore the cmsg buffer on recvmsg retry
Date: Sat,  5 Dec 2026 23:12:59 +0000	[thread overview]
Message-ID: <20261205231305.4158103-10-axboe@kernel.dk> (raw)
In-Reply-To: <20261205231305.4158103-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>
---
 io_uring/net.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/io_uring/net.c b/io_uring/net.c
index 2e1d66504266..688db0f4af4d 100644
--- a/io_uring/net.c
+++ b/io_uring/net.c
@@ -1047,6 +1047,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;
 	int consumed = 0;
 	size_t len;
 
@@ -1084,6 +1086,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);
@@ -1108,6 +1112,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;
 			io_kbuf_recycle(req, sel.buf_list, issue_flags);
 			return IOU_RETRY;
 		}
-- 
2.55.0


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

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 23:12 [PATCH 7.3.y 00/15] io_uring fixes for 7.3-stable Jens Axboe
2026-12-05 23:12 ` [PATCH 7.3.y 01/15] io_uring/net: don't charge the folio offset to truesize for managed frags Jens Axboe
2026-12-05 23:12 ` [PATCH 7.3.y 02/15] io_uring/net: don't copy msg_name on a recvmsg retry Jens Axboe
2026-12-05 23:12 ` [PATCH 7.3.y 03/15] io_uring/kbuf: clear REQ_F_BL_NO_RECYCLE on buffer put Jens Axboe
2026-12-05 23:12 ` [PATCH 7.3.y 04/15] io_uring: free the ring after a grace period Jens Axboe
2026-12-05 23:12 ` [PATCH 7.3.y 05/15] io_uring/filetable: don't install O_PATH files Jens Axboe
2026-12-05 23:12 ` [PATCH 7.3.y 06/15] io_uring/timeout: hold timeout_lock for the link splice Jens Axboe
2026-12-05 23:12 ` [PATCH 7.3.y 07/15] io_uring/cancel: always walk links under timeout_lock Jens Axboe
2026-12-05 23:12 ` [PATCH 7.3.y 08/15] io_uring/net: don't count the buffers of a send bundle Jens Axboe
2026-12-05 23:12 ` Jens Axboe [this message]
2026-12-05 23:13 ` [PATCH 7.3.y 10/15] io_uring/openclose: allow an empty path with O_EMPTYPATH Jens Axboe
2026-12-05 23:13 ` [PATCH 7.3.y 11/15] io_uring/bpf-ops: validate loop_step before registration Jens Axboe
2026-12-05 23:13 ` [PATCH 7.3.y 12/15] io_uring: close poll activation race when enabling rings Jens Axboe
2026-12-05 23:13 ` [PATCH 7.3.y 13/15] io_uring/cancel: arm local work wakeups only for submitter Jens Axboe
2026-12-05 23:13 ` [PATCH 7.3.y 14/15] io_uring/zcrx: don't sync dma-bufs Jens Axboe
2026-12-05 23:13 ` [PATCH 7.3.y 15/15] io_uring: enforce task restrictions on blind MSG_RING 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=20261205231305.4158103-10-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.