Skip to contentSkip to conversation

Fix SO_INQ for af_unix

PATCH Superseded Jens Axboe 2 participants 7 messages Dec 18, 2025

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

cover Fix SO_INQ for af_unix

Hi,

We ran into an issue with the recently added SO_INQ support for unix/stream sockets. First patch fixes the unconditional posting of cmsg for io_uring cases, which it should not do, and the second patch fixes the condition for when to post an SO_INQ cmsg in general (which is only for the non-error case).

Please review and apply if you're happy with them, these patches fix a regression introduced in 6.17 and newer kernels and hence are marked for stable as well.

 net/unix/af_unix.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
Signature · 1 line
Jens Axboe
author maintainer
#1 lore raw

PATCH 1/2 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.

---
 net/unix/af_unix.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
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
2.51.0
author maintainer
#6 lore raw

PATCH 2/2 af_unix: only post SO_INQ cmsg for non-error case

In reply to Jens Axboe

As is done for TCP sockets, don't post an SCM_INQ cmsg for an error case. Only post them for the non-error case, which is when unix_stream_read_generic will return >= 0.

---
 net/unix/af_unix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/unix/af_unix.c +1 -1
index 110d716087b5..72dc5d5bcac8 100644
@@ -3091,7 +3091,7 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
 		scm_recv_unix(sock, msg, &scm, flags);
 
 		do_cmsg = READ_ONCE(u->recvmsg_inq);
-		if (do_cmsg || msg->msg_get_inq) {
+		if ((do_cmsg || msg->msg_get_inq) && (copied ?: err) >= 0) {
 			msg->msg_inq = READ_ONCE(u->inq_len);
 			if (do_cmsg)
 				put_cmsg(msg, SOL_SOCKET, SCM_INQ,
Signature · 1 line
2.51.0
#2 lore raw

Re: [PATCH 1/2] af_unix: don't post cmsg for SO_INQ unless explicitly asked for

In reply to Jens Axboe
Quoted 32 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.

---
 net/unix/af_unix.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
net/unix/af_unix.c +2 -0
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);

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

Quoted 10 lines
net/unix/af_unix.c +5 -3
 
-		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);

Is it intentional that msg_inq is set also if msg_get_inq is not set, but do_cmsg is?

It just seems a bit surprising behavior.

That is an entangling of two separate things.
- msg_get_inq sets msg_inq, and
- cmsg_flags & TCP_CMSG_INQ inserts TCP_CM_INQ cmsg

The original TCP patch also entangles them, but in another way. The cmsg is written only if msg_get_inq is requested.

- if (cmsg_flags && ret >= 0) {
+ if ((cmsg_flags || msg->msg_get_inq) && ret >= 0) {
if (cmsg_flags & TCP_CMSG_TS)
tcp_recv_timestamp(msg, sk, &tss);
- if (cmsg_flags & TCP_CMSG_INQ) {
- inq = tcp_inq_hint(sk);
- put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
+ if (msg->msg_get_inq) {
+ msg->msg_inq = tcp_inq_hint(sk);
+ if (cmsg_flags & TCP_CMSG_INQ)
+ put_cmsg(msg, SOL_TCP, TCP_CM_INQ,
+ sizeof(msg->msg_inq), &msg->msg_inq);

With this patch the two are still not entirely consistent.

Quoted 6 lines
net/unix/af_unix.c +0 -0
 		}
 	} else {
 		scm_destroy(&scm);
Signature · 1 line
2.51.0
author maintainer
#3 lore raw

Re: [PATCH 1/2] af_unix: don't post cmsg for SO_INQ unless explicitly asked for

Quoted 34 lines · Willem de Bruijn
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.

---
 net/unix/af_unix.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
net/unix/af_unix.c +2 -0
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);
 
Quoted 12 lines

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

Quoted 9 lines
net/unix/af_unix.c +5 -3
-		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);

Is it intentional that msg_inq is set also if msg_get_inq is not set, but do_cmsg is?

It doesn't really matter, what matters is the actual cmsg posting be guarded. The msg_inq should only be used for a successful return anyway, I think we're better off reading it unconditionally than having multiple branches.

Not really important, if you prefer to keep them consistent, that's fine with me too.

Quoted 9 lines

It just seems a bit surprising behavior.

That is an entangling of two separate things.
- msg_get_inq sets msg_inq, and
- cmsg_flags & TCP_CMSG_INQ inserts TCP_CM_INQ cmsg

The original TCP patch also entangles them, but in another way. The cmsg is written only if msg_get_inq is requested.

The cmsg is written iff TCP_CMSG_INQ is set, not if ->msg_get_inq is the only thing set. That part is important.

But yes, both need the data left.

Signature · 1 line
Jens Axboe
#4 lore raw

Re: [PATCH 1/2] af_unix: don't post cmsg for SO_INQ unless explicitly asked for

Quoted 63 lines · Jens Axboe
Quoted 34 lines · Willem de Bruijn
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.

---
 net/unix/af_unix.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
net/unix/af_unix.c +2 -0
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);
 
Quoted 12 lines

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

Quoted 9 lines
net/unix/af_unix.c +5 -3
-		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);

Is it intentional that msg_inq is set also if msg_get_inq is not set, but do_cmsg is?

It doesn't really matter, what matters is the actual cmsg posting be guarded. The msg_inq should only be used for a successful return anyway, I think we're better off reading it unconditionally than having multiple branches.

Not really important, if you prefer to keep them consistent, that's fine with me too.

Quoted 7 lines

It just seems a bit surprising behavior.

That is an entangling of two separate things.
- msg_get_inq sets msg_inq, and
- cmsg_flags & TCP_CMSG_INQ inserts TCP_CM_INQ cmsg

Quoted 7 lines
Quoted 2 lines

The original TCP patch also entangles them, but in another way. The cmsg is written only if msg_get_inq is requested.

The cmsg is written iff TCP_CMSG_INQ is set, not if ->msg_get_inq is the only thing set. That part is important.

But yes, both need the data left.

I see, writing msg_inq if not requested is benign indeed. The inverse is not true.

Ok. I do think it would be good to have the protocols consistent. Simpler to reason about the behavior and intent long term.

Quoted 2 lines
Signature · 1 line
Jens Axboe
author maintainer
#5 lore raw

Re: [PATCH 1/2] af_unix: don't post cmsg for SO_INQ unless explicitly asked for

Quoted 70 lines · Willem de Bruijn
Quoted 69 lines · Jens Axboe
Quoted 34 lines · Willem de Bruijn
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.

---
 net/unix/af_unix.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
net/unix/af_unix.c +2 -0
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);
 
Quoted 12 lines

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

Quoted 9 lines
net/unix/af_unix.c +5 -3
-		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);

Is it intentional that msg_inq is set also if msg_get_inq is not set, but do_cmsg is?

It doesn't really matter, what matters is the actual cmsg posting be guarded. The msg_inq should only be used for a successful return anyway, I think we're better off reading it unconditionally than having multiple branches.

Not really important, if you prefer to keep them consistent, that's fine with me too.

Quoted 9 lines

It just seems a bit surprising behavior.

That is an entangling of two separate things.
- msg_get_inq sets msg_inq, and
- cmsg_flags & TCP_CMSG_INQ inserts TCP_CM_INQ cmsg

The original TCP patch also entangles them, but in another way. The cmsg is written only if msg_get_inq is requested.

The cmsg is written iff TCP_CMSG_INQ is set, not if ->msg_get_inq is the only thing set. That part is important.

Quoted 7 lines
Quoted 1 line

But yes, both need the data left.

I see, writing msg_inq if not requested is benign indeed. The inverse is not true.

Ok. I do think it would be good to have the protocols consistent. Simpler to reason about the behavior and intent long term.

Sure, I can do that. Would you prefer patch 1 and 2 folded as well, or keep them separate? If we're mirroring the logic, seems like 1 patch would be better.

Signature · 1 line
Jens Axboe