From: Bryam Vargas <hexl…@proton.me> recvmsg() takes no socket lock, on either transport: - Two threads reading one socket get its data out of order. - A read that finds the peer's SHT sets RCV_SHUTDOWN with a plain read-modify-write of sk_shutdown, and can lose the SEND_SHUTDOWN that a shutdown(SHUT_WR) set meanwhile. - A MSG_PEEK read leaves its skb on the receive queue and holds a reference to it. When a second reader consumes the skb meanwhile, that reference is the last one and skb_unref() frees nothing: the skb and its receive memory leak, and iucv_sock_destruct() warns. All three need only a second thread at the wrong moment. No memory-safety report is known from any of them. Take lock_sock() in recvmsg(). It sleeps for data without the lock, in sk_wait_event(), so a reader with nothing to read keeps no other call on the socket waiting. sendmsg() now waits for send memory without the socket lock as well: recvmsg() needs that lock, and over z/VM the memory comes back only when the peer reads, which may wait for our own reader. That wait is one more point at which shutdown(SHUT_WR) can find the send memory used up: iucv_send_ctrl() then has no room for the SHT and the peer reads no EOF, as before this patch whenever the memory is used up when shutdown() runs. The writer itself returns -EPIPE: iucv_send_ctrl() does not clear SEND_SHUTDOWN, so the wait cannot miss it. Fixes: eac3731bd04c ("[S390]: Add AF_IUCV socket support") Suggested-by: Alexandra Winter <wint…@linux.ibm.com> Link: https://lore.kernel.org/all/2026…@proton.me/ Cc: stab…@vger.kernel.org # 6.18.x: aa5ec571b93f: net/iucv: Add lock nesting annotation in iucv_sock_close() Cc: stab…@vger.kernel.org # 6.18.x Signed-off-by: Bryam Vargas <hexl…@proton.me> --- v5: no change of its own. recvmsg() counts the frames it reads against msglimit again, as on net: 1/10 has no field for the window. v4: no code change, was 3/7. Two changes to the commit message, both for what 2/10 does under this patch: - the second item was the sk_shutdown bit lost around a window update. That is 2/10's now; what is left here is recvmsg()'s own write of RCV_SHUTDOWN - the last paragraph says what the writer gets when shutdown(SHUT_WR) runs during its memory wait. v3's note had that wait missing the wake-up, by inspection (AI review of v3 3/7, link below) v3: new as a patch of its own, the half of v2's lock patch that does not touch the softirq side. Without 6/10 the softirq still changes sk_state under a task that owns the socket, as on net. v4: https://lore.kernel.org/netdev/2026…@proton.me/ v3: https://lore.kernel.org/netdev/2026…@proton.me/ AI review of v3 3/7: https://lore.kernel.org/all/sash…@kernel.org/ In the emulated setup: two readers on one socket under MTTCG, three runs of 838 messages each. On net, on the kernel with 1/10 and 2/10 and on the one with 1/10 to 3/10, every run has reads that go backwards, 12 to 25 of them; on the one with 1/10 to 4/10 and with the series there are none. The second item of the commit message is by inspection. The test that loses the bit on net (notes of 2/10) loses none with 2/10 or with the series, and I have no test where the peer's SHT is read while shutdown(SHUT_WR) runs. Repro of the MSG_PEEK item: one process is parked in the copy of a recv(MSG_PEEK), its buffer a userfaultfd page, and a second one reads the same message. On net and on the kernels with 1/10, with 1/10 and 2/10 and with 1/10 to 3/10, the second read returns the byte while the first is still parked; once both are back, the socket has receive memory charged and nothing to read, and iucv_sock_destruct() takes its WARN_ON() at close. From 4/10 on the second reader waits for the first, then reads the byte, and nothing stays charged. With the two reads one after the other every kernel is clean. sendmsg()'s memory wait: with it left under the socket lock, a non-blocking read is held up for as long as a writer waits for send memory, 6209 ms in the test. shutdown(SHUT_WR) during that wait: a writer blocks with the send memory full and SO_SNDTIMEO 8 s, and shutdown(SHUT_WR) is called 200 ms later. On net and on the kernels with 1/10, with 1/10 and 2/10 and with 1/10 to 3/10, the shutdown() waits for the socket lock until the writer's timeout, 8022 to 8297 ms, and the writer returns -EAGAIN. With 1/10 to 4/10 and with the series, shutdown() returns at once and the writer returns -EPIPE, 201 to 203 ms after it blocked. The case where the wake-up could be missed is under 2/10. The prerequisite on the stable line, aa5ec571b93f, brings __iucv_sock_close(), which recvmsg() now calls with the lock held where it used to close the socket on a peer that overran its window. Hidayath's 19 KCSAN reports of recvmsg() against shutdown() on net-next are what this patch should end; the READ_ONCE() in the receive wait against shutdown()'s plain write stays reportable until 7/10. --- net/iucv/af_iucv.c | 94 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 78 insertions(+), 16 deletions(-) diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c index c955ecd0c522..9c4bac738e67 100644 --- a/net/iucv/af_iucv.c +++ b/net/iucv/af_iucv.c @@ -1039,7 +1039,17 @@ static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg, } } skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear, - noblock, &err, 0); + 1, &err, 0); + if (!skb && err == -EAGAIN && !noblock) { + /* Wait without the socket lock: recvmsg() takes it, and over + * z/VM the memory comes back only when the peer reads, which + * may wait for our own reader. + */ + release_sock(sk); + skb = sock_alloc_send_pskb(sk, headroom + linear, + len - linear, 0, &err, 0); + lock_sock(sk); + } if (!skb) goto out; if (headroom) @@ -1266,6 +1276,54 @@ static void iucv_process_message_q(struct sock *sk) } } +/** + * iucv_sock_recv_skb() - get the next skb for recvmsg(), waiting for one + * @sk: the socket, locked by the caller + * @flags: recvmsg() flags; MSG_PEEK leaves the skb queued and returns + * it with a reference + * @err: set when no skb is returned + * + * skb_recv_datagram() would sleep with the socket lock held. This sleeps in + * sk_wait_event(), which drops it. + * + * Context: Process context, socket lock held; sleeps with it released. + * Return: the skb, or NULL with @err set. At RCV_SHUTDOWN the caller + * returns EOF whatever @err is. + */ +static struct sk_buff *iucv_sock_recv_skb(struct sock *sk, int flags, int *err) +{ + long timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); + struct sk_buff_head *queue = &sk->sk_receive_queue; + DEFINE_WAIT_FUNC(wait, woken_wake_function); + struct sk_buff *skb; + + for (;;) { + skb = skb_recv_datagram(sk, flags | MSG_DONTWAIT, err); + if (skb || *err != -EAGAIN) + break; + if (sk->sk_shutdown & RCV_SHUTDOWN) + break; + if (!timeo) + break; + if (sk->sk_state != IUCV_CONNECTED) { + *err = -ENOTCONN; + break; + } + if (signal_pending(current)) { + *err = sock_intr_errno(timeo); + break; + } + add_wait_queue(sk_sleep(sk), &wait); + sk_wait_event(sk, &timeo, + !skb_queue_empty_lockless(queue) || + READ_ONCE(sk->sk_err) || + (READ_ONCE(sk->sk_shutdown) & RCV_SHUTDOWN) || + sk->sk_state != IUCV_CONNECTED, &wait); + remove_wait_queue(sk_sleep(sk), &wait); + } + return skb; +} + static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, int flags) { @@ -1277,24 +1335,25 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg, int err = 0; u32 offset; + lock_sock(sk); if ((sk->sk_state == IUCV_DISCONN) && skb_queue_empty(&iucv->backlog_skb_q) && skb_queue_empty(&sk->sk_receive_queue) && - list_empty(&iucv->message_q.list)) - return 0; + list_empty(&iucv->message_q.list)) { + copied = 0; + goto unlock; + } - if (flags & (MSG_OOB)) - return -EOPNOTSUPP; + if (flags & (MSG_OOB)) { + err = -EOPNOTSUPP; + goto err_unlock; + } - /* receive/dequeue next skb: - * the function understands MSG_PEEK and, thus, does not dequeue skb - * only refcount is increased. - */ - skb = skb_recv_datagram(sk, flags, &err); + skb = iucv_sock_recv_skb(sk, flags, &err); if (!skb) { if (sk->sk_shutdown & RCV_SHUTDOWN) - return 0; - return err; + err = 0; + goto err_unlock; } offset = IUCV_SKB_CB(skb)->offset; @@ -1343,8 +1402,9 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg, atomic_inc(&iucv->msg_recv); if (atomic_read(&iucv->msg_recv) > iucv->msglimit) { WARN_ON(1); - iucv_sock_close(sk); - return -EFAULT; + __iucv_sock_close(sk); + err = -EFAULT; + goto err_unlock; } } @@ -1386,7 +1446,8 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg, copied = rlen; if (flags & MSG_PEEK) skb_unref(skb); - +unlock: + release_sock(sk); return copied; err_out: @@ -1394,7 +1455,8 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg, skb_queue_head(&sk->sk_receive_queue, skb); else skb_unref(skb); - +err_unlock: + release_sock(sk); return err; }
-- 2.56.1