Skip to contentSkip to conversation

net/iucv: fix the recvmsg window update

PATCH Applied Bryam Vargas 5 participants 5 messages Aug 28 – Aug 31

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 net/iucv: fix the recvmsg window update

iucv_sock_recvmsg() sends the HiperSockets-only AF_IUCV_FLAG_WIN without testing the transport, so on a classic z/VM socket iucv_send_ctrl() sizes the skb through a NULL iucv->hs_dev. SO_MSGLIMIT accepts 1, so msglimit / 2 is zero and one recvmsg() on its own socket is enough for an unprivileged process to take a spurious disconnect.

It also calls iucv_send_ctrl() under spin_lock_bh(&message_q.lock), which allocates GFP_KERNEL inside a section the code treats as atomic. Sending outside that lock lets two recvmsg() reach afiucv_hs_send() at once, where msg_recv is sampled for the advertised window and subtracted after dev_queue_xmit() -- and sendmsg reaches that counter under lock_sock() while recvmsg holds no socket lock, so both can subtract the same value, the counter goes negative and the credit reaches the peer twice.

Test the transport, claim the credit with atomic_xchg() after the last error exit and hand it back if the transmit fails, and send once the lock is dropped.

Fixes 3881ac441f64 ("af_iucv: add HiperSockets transport")
Fixes 238965b71b96 ("net/af_iucv: build proper skbs for HiperTransport")

---
v3: squashed the three v2 patches into one, as Alexandra Winter asked -- they
are one subject (iucv->msg_recv handling), they had a stated dependency, and one patch is easier to backport. No code change from v2 1-3 combined.

The v2 1/3 changelog said the NULL read faults under relocate_lowcore. That is out: Alexandra booted with it and did not hit a page fault. I have no explanation to offer in its place: vmem.c:521-524 skips the first sizeof(struct lowcore) bytes from the identity mapping whether or not the lowcore was relocated. So the changelog now claims only the disconnect, which she reproduced.

Tested-by carried from v2: the code is identical to v2 1-3 applied in order.

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Not reproduced here: CONFIG_AFIUCV depends on S390 and I have no Z. The counter race was checked under LKMM -- the counter reaches -2 before the atomic_xchg() and cannot after -- and the tree builds clean for s390.

---
 net/iucv/af_iucv.c | 42 +++++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 17 deletions(-)
net/iucv/af_iucv.c +25 -17
index 4e5cc9da6e06..db261ecd19af 100644
@@ -210,12 +210,6 @@ static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
 	phs_hdr->flags = flags;
 	if (flags == AF_IUCV_FLAG_SYN)
 		phs_hdr->window = iucv->msglimit;
-	else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
-		confirm_recv = atomic_read(&iucv->msg_recv);
-		phs_hdr->window = confirm_recv;
-		if (confirm_recv)
-			phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
-	}
 	memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
 	memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
 	memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
@@ -250,13 +244,22 @@ static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
 	}
 	skb->protocol = cpu_to_be16(ETH_P_AF_IUCV);
 
+	/* Claim the receive credit here, not while building the header: every
+	 * way this frame can be dropped has now been ruled out, so the window
+	 * is zeroed only for as long as the transmit itself takes.
+	 */
+	if (flags == AF_IUCV_FLAG_WIN || !flags) {
+		confirm_recv = atomic_xchg(&iucv->msg_recv, 0);
+		phs_hdr->window = confirm_recv;
+		if (confirm_recv)
+			phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
+	}
+
 	atomic_inc(&iucv->skbs_in_xmit);
 	err = dev_queue_xmit(skb);
 	if (net_xmit_eval(err)) {
 		atomic_dec(&iucv->skbs_in_xmit);
-	} else {
-		atomic_sub(confirm_recv, &iucv->msg_recv);
-		WARN_ON(atomic_read(&iucv->msg_recv) < 0);
+		atomic_add(confirm_recv, &iucv->msg_recv);
 	}
 	return net_xmit_eval(err);
 
@@ -1241,6 +1244,7 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 	struct iucv_sock *iucv = iucv_sk(sk);
 	unsigned int copied, rlen;
 	struct sk_buff *skb, *rskb, *cskb;
+	bool send_win = false;
 	int err = 0;
 	u32 offset;
 
@@ -1331,16 +1335,20 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 		if (skb_queue_empty(&iucv->backlog_skb_q)) {
 			if (!list_empty(&iucv->message_q.list))
 				iucv_process_message_q(sk);
-			if (atomic_read(&iucv->msg_recv) >=
-							iucv->msglimit / 2) {
-				err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
-				if (err) {
-					sk->sk_state = IUCV_DISCONN;
-					sk->sk_state_change(sk);
-				}
-			}
+			if (iucv->transport == AF_IUCV_TRANS_HIPER &&
+			    atomic_read(&iucv->msg_recv) >=
+							iucv->msglimit / 2)
+				send_win = true;
 		}
 		spin_unlock_bh(&iucv->message_q.lock);
+
+		if (send_win) {
+			err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
+			if (err) {
+				sk->sk_state = IUCV_DISCONN;
+				sk->sk_state_change(sk);
+			}
+		}
 	}
 
 done:

---
base-commit: 1b78070aaef63512688aebfbc82365ef9d6660f1
change-id: 20260828-b4-disp-33fac0ed-f37c51985da1

Best regards,

Signature · 1 line
Bryam Vargas <[email protected]>
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Lockless modification of sk->sk_shutdown and sk->sk_state leads to data races and lost state updates.
- [High] Silent data truncation for SOCK_STREAM over HiperSockets when message size exceeds MTU. --

commit 9ec78889adee8ffda3665e319fc36af7e52f542f
Author: Bryam Vargas <[email protected]>

net/iucv: fix the recvmsg window update

This commit attempts to fix a spurious disconnect in HiperSockets by properly checking the transport before sending an AF_IUCV_FLAG_WIN message. It also addresses a counter race by claiming the window credit after ruling out error exits.

net/iucv/af_iucv.c static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock, · line 245

Quoted 7 lines
net/iucv/af_iucv.c +0 -0
index 4e5cc9da6e068..db261ecd19af1 100644
@@ -250,13 +244,22 @@ static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
 	}
 	skb->protocol = cpu_to_be16(ETH_P_AF_IUCV);

[Severity: High]
This is a pre-existing issue, but just above this point in afiucv_hs_send(), large buffers are silently truncated:

	if (skb->len > skb->dev->mtu) {
		if (sock->sk_type == SOCK_SEQPACKET) {
			err = -EMSGSIZE;
			goto err_free;
		}
		err = pskb_trim(skb, skb->dev->mtu);
		if (err)
			goto err_free;
	}

If pskb_trim() succeeds for a SOCK_STREAM socket, afiucv_hs_send() returns success. Does iucv_sock_sendmsg() then return the full original requested length to user-space instead of the truncated length, masking the truncation and causing silent data loss?

Quoted 13 lines
net/iucv/af_iucv.c +11 -0
+	/* Claim the receive credit here, not while building the header: every
+	 * way this frame can be dropped has now been ruled out, so the window
+	 * is zeroed only for as long as the transmit itself takes.
+	 */
+	if (flags == AF_IUCV_FLAG_WIN || !flags) {
+		confirm_recv = atomic_xchg(&iucv->msg_recv, 0);
+		phs_hdr->window = confirm_recv;
+		if (confirm_recv)
+			phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
+	}
+
 	atomic_inc(&iucv->skbs_in_xmit);
 	err = dev_queue_xmit(skb);

[ ... ]

Quoted 11 lines
net/iucv/af_iucv.c +0 -7
@@ -1331,16 +1335,20 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 		if (skb_queue_empty(&iucv->backlog_skb_q)) {
 			if (!list_empty(&iucv->message_q.list))
 				iucv_process_message_q(sk);
-			if (atomic_read(&iucv->msg_recv) >=
-							iucv->msglimit / 2) {
-				err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
-				if (err) {
-					sk->sk_state = IUCV_DISCONN;
-					sk->sk_state_change(sk);
-				}

net/iucv/af_iucv.c static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg, · line 1346

Quoted 10 lines
net/iucv/af_iucv.c +7 -1
-			}
+			if (iucv->transport == AF_IUCV_TRANS_HIPER &&
+			    atomic_read(&iucv->msg_recv) >=
+							iucv->msglimit / 2)
+				send_win = true;
 		}
 		spin_unlock_bh(&iucv->message_q.lock);
+
+		if (send_win) {
+			err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);

[Severity: High]
This is a pre-existing issue, but does calling iucv_send_ctrl() here without holding lock_sock() cause data races?

In net/iucv/af_iucv.c:iucv_send_ctrl():

	if (sk->sk_shutdown & SEND_SHUTDOWN) {
		/* controlling flags should be sent anyway */
		shutdown = sk->sk_shutdown;
		sk->sk_shutdown &= RCV_SHUTDOWN;
	}
[ ... ]
	if (shutdown)
		sk->sk_shutdown = shutdown;

Could this lockless temporary modification overwrite concurrent updates to sk_shutdown (e.g., losing a RCV_SHUTDOWN flag set by shutdown()), leading to an application hanging indefinitely in recvmsg?

net/iucv/af_iucv.c static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg, · line 1348

Quoted 2 lines
net/iucv/af_iucv.c +2 -0
+			if (err) {
+				sk->sk_state = IUCV_DISCONN;

[Severity: High]
This is also a pre-existing issue, but is it safe to update sk_state locklessly here? This appears to violate core socket locking rules, potentially corrupting the state machine.

Quoted 6 lines
net/iucv/af_iucv.c +3 -0
+				sk->sk_state_change(sk);
+			}
+		}
 	}
 
 done:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v3 1/1
#2 lore raw
In reply to Bryam Vargas
Quoted 16 lines · On 28.08.26 21:07, Bryam Vargas via B4 Relay wrote:

From: Bryam Vargas <[email protected]>

iucv_sock_recvmsg() sends the HiperSockets-only AF_IUCV_FLAG_WIN without testing the transport, so on a classic z/VM socket iucv_send_ctrl() sizes the skb through a NULL iucv->hs_dev. SO_MSGLIMIT accepts 1, so msglimit / 2 is zero and one recvmsg() on its own socket is enough for an unprivileged process to take a spurious disconnect.

It also calls iucv_send_ctrl() under spin_lock_bh(&message_q.lock), which allocates GFP_KERNEL inside a section the code treats as atomic. Sending outside that lock lets two recvmsg() reach afiucv_hs_send() at once, where msg_recv is sampled for the advertised window and subtracted after dev_queue_xmit() -- and sendmsg reaches that counter under lock_sock() while recvmsg holds no socket lock, so both can subtract the same value, the counter goes negative and the credit reaches the peer twice.

Quoted 5 lines

Test the transport, claim the credit with atomic_xchg() after the last error exit and hand it back if the transmit fails, and send once the lock is dropped.

Fixes 3881ac441f64 ("af_iucv: add HiperSockets transport")

^^^ This is the older commit and should be used as Fixes-Tag
(alloc under spin lock and HiperSockets send in a z/VM scenario)> Fixes: 238965b71b96 ("net/af_iucv: build proper skbs for HiperTransport") ^^^
This is a newer commit and add the skb build consequence described above.
I think it needs to be drpped as Fixes tag.

I don't think you can have 2 Fixes tags, can you?

Quoted 18 lines

---
v3: squashed the three v2 patches into one, as Alexandra Winter asked -- they
are one subject (iucv->msg_recv handling), they had a stated dependency, and one patch is easier to backport. No code change from v2 1-3 combined.

The v2 1/3 changelog said the NULL read faults under relocate_lowcore. That is out: Alexandra booted with it and did not hit a page fault. I have no explanation to offer in its place: vmem.c:521-524 skips the first sizeof(struct lowcore) bytes from the identity mapping whether or not the lowcore was relocated. So the changelog now claims only the disconnect, which she reproduced.

Tested-by carried from v2: the code is identical to v2 1-3 applied in order.

Quoted 7 lines

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Not reproduced here: CONFIG_AFIUCV depends on S390 and I have no Z. The counter race was checked under LKMM -- the counter reaches -2 before the atomic_xchg() and cannot after -- and the tree builds clean for s390.
---

netdev maintainers, feel free to remove the second Fixes tag.

(Re-tested on s390)

Quoted 1 line
maintainer
#3 lore raw
Quoted 1 line · On Mon, 31 Aug 2026 17:08:54 +0200 Alexandra Winter wrote:

netdev maintainers, feel free to remove the second Fixes tag.

It's okay to have more than 1 FWIW

#4 lore raw
In reply to Bryam Vargas
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Fri, 28 Aug 2026 14:07:47 -0500 you wrote:

From: Bryam Vargas <[email protected]>

iucv_sock_recvmsg() sends the HiperSockets-only AF_IUCV_FLAG_WIN without testing the transport, so on a classic z/VM socket iucv_send_ctrl() sizes the skb through a NULL iucv->hs_dev. SO_MSGLIMIT accepts 1, so msglimit / 2 is zero and one recvmsg() on its own socket is enough for an unprivileged process to take a spurious disconnect.

[...]

Here is the summary with links:
- [net,v3] net/iucv: fix the recvmsg window update

    https://git.kernel.org/netdev/net/c/fa5acd038ea6

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html