Skip to contentSkip to conversation

net/iucv: fix use-after-free of a severed iucv_path

PATCH Applied Bryam Vargas 4 participants 6 messages Jul 7 – Jul 24

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

PATCH 1/1 net/iucv: fix use-after-free of a severed iucv_path

af_iucv queues not-yet-received message notifications on iucv->message_q, each holding a raw pointer to the connection's iucv_path. When the peer severs the connection, iucv_sever_path() frees that path with iucv_path_free() but leaves the notifications queued. A later recvmsg() drains message_q via iucv_process_message_q() and hands the stale path to message_receive() -- a use-after-free of the freed iucv_path.

Drop the queued notifications when the path is severed; once the path is gone they can no longer be received. This also frees the notifications leaked when a socket is closed with messages still queued.

Fixes f0703c80e515 ("[AF_IUCV]: postpone receival of iucv-packets")
---
 net/iucv/af_iucv.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
net/iucv/af_iucv.c +14 -0
index fed240b453bd..2869a103f7fa 100644
@@ -337,6 +337,7 @@ static void iucv_sever_path(struct sock *sk, int with_user_data)
 	unsigned char user_data[16];
 	struct iucv_sock *iucv = iucv_sk(sk);
 	struct iucv_path *path = iucv->path;
+	struct sock_msg_q *p, *n;
 
 	/* Whoever resets the path pointer, must sever and free it. */
 	if (xchg(&iucv->path, NULL)) {
@@ -348,6 +349,19 @@ static void iucv_sever_path(struct sock *sk, int with_user_data)
 		} else
 			pr_iucv->path_sever(path, NULL);
 		iucv_path_free(path);
+
+		/*
+		 * Message notifications queued on message_q still reference
+		 * the now freed path; drop them, otherwise a later recvmsg()
+		 * would pass the freed iucv_path to message_receive() via
+		 * iucv_process_message_q().
+		 */
+		spin_lock_bh(&iucv->message_q.lock);
+		list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
+			list_del(&p->list);
+			kfree(p);
+		}
+		spin_unlock_bh(&iucv->message_q.lock);
 	}
 }
 

---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260707-b4-disp-783fedbb-39bc1bb9d4e0

Best regards,

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

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

Pre-existing issues:
- [High] Concurrent Use-After-Free of iucv_path due to incorrect ordering of queue cleanup and path freeing in iucv_sever_path(). --

--- Patch [1]: [PATCH net] net/iucv: fix use-after-free of a severed iucv_path --- commit 9139c973711b1b0860aa18a8e778aceb20d5b1dd
Author: Bryam Vargas <[email protected]>

net/iucv: fix use-after-free of a severed iucv_path

This commit drops queued message notifications in iucv->message_q when an IUCV path is severed. It acquires the message_q.lock and frees any remaining notifications that reference the severed path to prevent use-after-free during a later recvmsg().

Quoted 4 lines
net/iucv/af_iucv.c +0 -0
index fed240b453bd9..2869a103f7fa2 100644

[ ... ]

net/iucv/af_iucv.c static void iucv_sever_path(struct sock *sk, int with_user_data) · line 351

Quoted 4 lines
net/iucv/af_iucv.c +0 -0
@@ -348,6 +349,19 @@ static void iucv_sever_path(struct sock *sk, int with_user_data)
 		} else
 			pr_iucv->path_sever(path, NULL);
 		iucv_path_free(path);

[Severity: High]
This is a pre-existing issue, but does freeing the path before clearing the queue leave a race window for a concurrent receive?

If a concurrent recvmsg() is running, it could acquire the message_q.lock and pop a notification containing the freed path pointer before we acquire the lock here to clear the queue:

CPU1 in iucv_sever_path():

    iucv_path_free(path);
    // preempted or delayed before acquiring message_q.lock

CPU2 in iucv_sock_recvmsg():

    // pops notification from message_q
    iucv_process_message_q()
        iucv_process_message(sk, skb, p->path, &p->msg);
            __iucv_message_receive()
                parm->db.ippathid = path->pathid; // use-after-free read

Would it be safer to move iucv_path_free(path) after the spin_unlock_bh block that drops the queued notifications?

Quoted 15 lines
net/iucv/af_iucv.c +13 -0
+
+		/*
+		 * Message notifications queued on message_q still reference
+		 * the now freed path; drop them, otherwise a later recvmsg()
+		 * would pass the freed iucv_path to message_receive() via
+		 * iucv_process_message_q().
+		 */
+		spin_lock_bh(&iucv->message_q.lock);
+		list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
+			list_del(&p->list);
+			kfree(p);
+		}
+		spin_unlock_bh(&iucv->message_q.lock);
 	}
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Bryam Vargas
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

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

Quoted 10 lines · On Tue, 07 Jul 2026 02:00:54 -0500 you wrote:

From: Bryam Vargas <[email protected]>

af_iucv queues not-yet-received message notifications on iucv->message_q, each holding a raw pointer to the connection's iucv_path. When the peer severs the connection, iucv_sever_path() frees that path with iucv_path_free() but leaves the notifications queued. A later recvmsg() drains message_q via iucv_process_message_q() and hands the stale path to message_receive() -- a use-after-free of the freed iucv_path.

[...]

Here is the summary with links:
- [net] net/iucv: fix use-after-free of a severed iucv_path

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

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
maintainer
#3 lore raw
Quoted 5 lines · On 21.07.26 09:30, [email protected] wrote:

Hello:

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

-- Original Patch for reference: --

Quoted 44 lines

From: Bryam Vargas <[email protected]>

af_iucv queues not-yet-received message notifications on iucv->message_q, each holding a raw pointer to the connection's iucv_path. When the peer severs the connection, iucv_sever_path() frees that path with iucv_path_free() but leaves the notifications queued. A later recvmsg() drains message_q via iucv_process_message_q() and hands the stale path to message_receive() -- a use-after-free of the freed iucv_path.

Drop the queued notifications when the path is severed; once the path is gone they can no longer be received. This also frees the notifications leaked when a socket is closed with messages still queued.

Fixes f0703c80e515 ("[AF_IUCV]: postpone receival of iucv-packets")
---
 net/iucv/af_iucv.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
net/iucv/af_iucv.c +7 -0
index fed240b453bd..2869a103f7fa 100644
@@ -337,6 +337,7 @@ static void iucv_sever_path(struct sock *sk, int with_user_data)
 	unsigned char user_data[16];
 	struct iucv_sock *iucv = iucv_sk(sk);
 	struct iucv_path *path = iucv->path;
+	struct sock_msg_q *p, *n;
 
 	/* Whoever resets the path pointer, must sever and free it. */
 	if (xchg(&iucv->path, NULL)) {
@@ -348,6 +349,19 @@ static void iucv_sever_path(struct sock *sk, int with_user_data)
 		} else
 			pr_iucv->path_sever(path, NULL);
 		iucv_path_free(path);
+
+		/*
+		 * Message notifications queued on message_q still reference
+		 * the now freed path; drop them, otherwise a later recvmsg()
+		 * would pass the freed iucv_path to message_receive() via
+		 * iucv_process_message_q().

net/iucv/af_iucv.c static void iucv_sever_path(struct sock *sk, int with_user_data) · line 367

Quoted 10 lines
net/iucv/af_iucv.c +7 -0
+		 */
+		spin_lock_bh(&iucv->message_q.lock);
+		list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
+			list_del(&p->list);
+			kfree(p);
+		}
+		spin_unlock_bh(&iucv->message_q.lock);
 	}
 }
 

--- end of patch --

Quoted 6 lines

Here is the summary with links:
- [net] net/iucv: fix use-after-free of a severed iucv_path

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

You are awesome, thank you!

Ah, Paolo was faster than me.

@Bryam and for the records,
this should not be a use-after-free. After iucv_server_path it should not be possible to call iucv_process_message_q() anymore. I agree that it is a message leak, the pending messages indicators in iucv->message_q are not freed anywhere. I would have preferred to do that in iucv_sock_close() instead of iucv_sever_path() for symmetry with iucv_sock_alloc(), but this should work as well.

Having said that, as we discussed in [1] the socket locking in af_iucv wrt receive path has deficiencies, and we will follow up anyhow.

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

#4 lore raw

To: Alexandra Winter <[email protected]>
Cc: Thorsten Winkler <[email protected]>,
Hidayath Khan <[email protected]>,
Heiko Carstens <[email protected]>,
Vasily Gorbik <[email protected]>,
Alexander Gordeev <[email protected]>,
Paolo Abeni <[email protected]>,
"David S. Miller" <[email protected]>,
Eric Dumazet <[email protected]>,
Jakub Kicinski <[email protected]>,
[email protected],
[email protected],
[email protected]

Alexandra,

Quoted 2 lines

After iucv_sever_path it should not be possible to call iucv_process_message_q() anymore.

For the close path, yes -- iucv_sock_close() ends at IUCV_CLOSED/SOCK_ZAPPED and recvmsg() is out, so message_q just leaks. The peer-sever path is
different: iucv_callback_connrej() severs and then sets IUCV_DISCONN, leaving the socket open. recvmsg()'s early return needs message_q.list empty; with a saved entry it falls through, and if a datagram is still on sk_receive_queue it reaches iucv_process_message_q() and hands the freed path to message_receive() -- pathid is the read.

It isn't a tight free-vs-use race. The core tasklet runs message_pending then path_severed in arrival order under iucv_table_lock, so the entry is saved with the path live and the sever frees it in the same pass; neither connrej nor close drains message_q or purges sk_receive_queue. The stale entry then sits in message_q.list until the next qualifying recvmsg -- deferred use, not a nanosecond window. A peer that floods past rcvbuf (which is exactly what spills into message_q) and then severs before the slow reader catches up owns the ordering; and where a gap did need stretching, it's a lock-free interval a blocked reader holds open, so I wouldn't read "narrow on HW" as a bound.

Quoted 1 line

I agree that it is a message leak [...] not freed anywhere.

Right, same missing drain. On close it's a leak; on connrej the socket stays readable, so it's a use-after-free on the next recvmsg(). So: a leak from your side, a reachable UAF from mine, gated to s390/z-VM and its timing. I verified the pointer lifetime with KASAN (mocked transport) and CBMC, not on z/VM hardware -- the measured HW trigger is yours to confirm, I'm not claiming it.

Quoted 2 lines

I would have preferred to do that in iucv_sock_close() [...] but this should work as well.

Draining at sever covers both callers in one spot; close-side for symmetry with iucv_sock_alloc() is fine too, no objection to moving it.

I'll fold the reachability point into the receive-path locking rework and send that RFC to netdev and linux-s390, as you asked.

Thanks,
Bryam

maintainer
#5 lore raw
Quoted 4 lines · Bryam Vargas

Alexandra,

Quoted 2 lines

After iucv_sever_path it should not be possible to call iucv_process_message_q() anymore.

Quoted 8 lines

For the close path, yes -- iucv_sock_close() ends at IUCV_CLOSED/SOCK_ZAPPED and recvmsg() is out, so message_q just leaks. The peer-sever path is
different: iucv_callback_connrej() severs and then sets IUCV_DISCONN, leaving the socket open. recvmsg()'s early return needs message_q.list empty; with a saved entry it falls through, and if a datagram is still on sk_receive_queue it reaches iucv_process_message_q() and hands the freed path to message_receive() -- pathid is the read.

Thank you very much for the explanation. I overlooked that one. I think this was a design bug in f0703c80e515 ("[AF_IUCV]: postpone receival of iucv-packets"). It doesn't make sense to check the message_q if IUCV_DISCONN.
I'm taking notes for future cleanups.

[...]

Quoted 4 lines

I'll fold the reachability point into the receive-path locking rework and send that RFC to netdev and linux-s390, as you asked.

Looking forward to that, thank you.

Quoted 2 lines

Thanks,
Bryam