Skip to contentSkip to conversation

af_unix: don't post cmsg for SO_INQ unless explicitly asked for

PATCH In review Jens Axboe 1 participant 2 messages Dec 18, 2025

Subsystems (3)
lore
Share & tools
lore

Jens Axboe

author
#0 lore raw

PATCH 1/1 af_unix: don't post cmsg for SO_INQ unless explicitly asked for

A previous commit added SO_INQ support for AF_UNIX (SOCK_STREAM), but it posts a SCM_INQ cmsg even if just msg->msg_get_inq is set. This is incorrect, as ->msg_get_inq is just the caller asking for the remainder to be passed back in msg->msg_inq, it has nothing to do with cmsg. The original commit states that this is done to make sockets io_uring-friendly", but it's actually incorrect as io_uring doesn't use cmsg headers internally at all, and it's actively wrong as this means that cmsg's are always posted if someone does recvmsg via io_uring.

Fix that up by only posting cmsg if u->recvmsg_inq is set.

Fixes df30285b3670 ("af_unix: Introduce SO_INQ.")

---

net/unix/af_unix.c +7 -3
index 55cdebfa0da0..110d716087b5 100644
@@ -3086,12 +3086,16 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
 
 	mutex_unlock(&u->iolock);
 	if (msg) {
+		bool do_cmsg;
+
 		scm_recv_unix(sock, msg, &scm, flags);
 
-		if (READ_ONCE(u->recvmsg_inq) || msg->msg_get_inq) {
+		do_cmsg = READ_ONCE(u->recvmsg_inq);
+		if (do_cmsg || msg->msg_get_inq) {
 			msg->msg_inq = READ_ONCE(u->inq_len);
-			put_cmsg(msg, SOL_SOCKET, SCM_INQ,
-				 sizeof(msg->msg_inq), &msg->msg_inq);
+			if (do_cmsg)
+				put_cmsg(msg, SOL_SOCKET, SCM_INQ,
+					 sizeof(msg->msg_inq), &msg->msg_inq);
 		}
 	} else {
 		scm_destroy(&scm);
Signature · 1 line
Jens Axboe
author
#1 lore raw
Quoted 33 lines · Jens Axboe

A previous commit added SO_INQ support for AF_UNIX (SOCK_STREAM), but it posts a SCM_INQ cmsg even if just msg->msg_get_inq is set. This is incorrect, as ->msg_get_inq is just the caller asking for the remainder to be passed back in msg->msg_inq, it has nothing to do with cmsg. The original commit states that this is done to make sockets io_uring-friendly", but it's actually incorrect as io_uring doesn't use cmsg headers internally at all, and it's actively wrong as this means that cmsg's are always posted if someone does recvmsg via io_uring.

Fix that up by only posting cmsg if u->recvmsg_inq is set.

Fixes df30285b3670 ("af_unix: Introduce SO_INQ.")

---

net/unix/af_unix.c +3 -1
index 55cdebfa0da0..110d716087b5 100644
@@ -3086,12 +3086,16 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
 
 	mutex_unlock(&u->iolock);
 	if (msg) {
+		bool do_cmsg;
+
 		scm_recv_unix(sock, msg, &scm, flags);
 
-		if (READ_ONCE(u->recvmsg_inq) || msg->msg_get_inq) {
+		do_cmsg = READ_ONCE(u->recvmsg_inq);

net/unix/af_unix.c static int unix_stream_read_generic(struct unix_stream_read_state *state, · line 3101

Quoted 10 lines
net/unix/af_unix.c +4 -2
+		if (do_cmsg || msg->msg_get_inq) {
 			msg->msg_inq = READ_ONCE(u->inq_len);
-			put_cmsg(msg, SOL_SOCKET, SCM_INQ,
-				 sizeof(msg->msg_inq), &msg->msg_inq);
+			if (do_cmsg)
+				put_cmsg(msg, SOL_SOCKET, SCM_INQ,
+					 sizeof(msg->msg_inq), &msg->msg_inq);
 		}
 	} else {
 		scm_destroy(&scm);

Note, on top of this bug, I also believe the correct check here should be:

if ((do_cmsg || msg->msg_get_inq) && copied >= 0)

rather than always post a cmsg (or pass back inq data) if the socket read has failed.

Was going to post that patch separately, but can fold it into this one as well. Let me know.

Also note that this is commit is actively breaking some io_uring uses on streamed sockets, as you can now end up with multiple SCM_INQ cmsg postings per socket with retries. These were not requested. So would appreciate if we can get this one sorted out soonish and post for stable too. It affects 6.17 and newer.

Signature · 1 line
Jens Axboe