Skip to content

Commit 4d14429

Browse files
axboePaolo Abeni
authored andcommitted
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 a cmsg if u->recvmsg_inq is set. Additionally, mirror how TCP handles inquiry handling in that it should only be done for a successful return. This makes the logic for the two identical. Cc: stable@vger.kernel.org Fixes: df30285 ("af_unix: Introduce SO_INQ.") Reported-by: Julian Orth <ju.orth@gmail.com> Link: axboe/liburing#1509 Signed-off-by: Jens Axboe <axboe@kernel.dk> Reviewed-by: Willem de Bruijn <willemb@google.com> Link: https://patch.msgid.link/07adc0c2-2c3b-4d08-8af1-1c466a40b6a8@kernel.dk Signed-off-by: Paolo Abeni <pabeni@redhat.com>
1 parent 3387a7a commit 4d14429

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

net/unix/af_unix.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,6 +2904,7 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
29042904
unsigned int last_len;
29052905
struct unix_sock *u;
29062906
int copied = 0;
2907+
bool do_cmsg;
29072908
int err = 0;
29082909
long timeo;
29092910
int target;
@@ -2929,6 +2930,9 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
29292930

29302931
u = unix_sk(sk);
29312932

2933+
do_cmsg = READ_ONCE(u->recvmsg_inq);
2934+
if (do_cmsg)
2935+
msg->msg_get_inq = 1;
29322936
redo:
29332937
/* Lock the socket to prevent queue disordering
29342938
* while sleeps in memcpy_tomsg
@@ -3088,10 +3092,11 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
30883092
if (msg) {
30893093
scm_recv_unix(sock, msg, &scm, flags);
30903094

3091-
if (READ_ONCE(u->recvmsg_inq) || msg->msg_get_inq) {
3095+
if (msg->msg_get_inq && (copied ?: err) >= 0) {
30923096
msg->msg_inq = READ_ONCE(u->inq_len);
3093-
put_cmsg(msg, SOL_SOCKET, SCM_INQ,
3094-
sizeof(msg->msg_inq), &msg->msg_inq);
3097+
if (do_cmsg)
3098+
put_cmsg(msg, SOL_SOCKET, SCM_INQ,
3099+
sizeof(msg->msg_inq), &msg->msg_inq);
30953100
}
30963101
} else {
30973102
scm_destroy(&scm);

0 commit comments

Comments
 (0)