[PATCH net v2 2/2] net/x25: re-check x25->neighbour after re-acquiring the socket lock
From: Martin Schiller
Date: Fri Nov 27 2026 - 01:28:34 EST
x25_sendmsg(), x25_output() and x25_recvmsg() release the socket lock
while they wait for memory or data, and use x25->neighbour once they
own the socket again. The call may have been cleared in the meantime:
x25_disconnect() drops the neighbour reference and sets x25->neighbour
to NULL, for example when a CLEAR REQUEST from the peer or a link
failure is processed in softirq context while the socket is not owned.
sock_alloc_send_skb() only notices this if the disconnect happened
before its error checks. A disconnect during the allocation, or before
lock_sock() returns, leads to a NULL pointer dereference when
x25_sendmsg() builds the packet header or in the fragmentation loop of
x25_output(). In x25_recvmsg(), x25_disconnect() leaves x25->condition
alone, so if the receiver was busy, x25_check_rbuf() sends an RR
through x25_write_internal(), which dereferences x25->neighbour too.
Re-check x25->neighbour after lock_sock(): fail with -ENOTCONN in
x25_sendmsg() and x25_output(), and skip x25_check_rbuf() in
x25_recvmsg(), which still returns the data it has dequeued. Fragments
already queued by x25_output() have been purged by x25_disconnect() at
that point.
The x25_sendmsg() and x25_output() cases were also pointed out by the
netdev AI review of an earlier version of the x25_kill_by_neigh() fix.
Krystian Kaniewski found the x25_recvmsg() case by code inspection.
Fixes: 4becb7ee5b3d ("net/x25: Fix x25_neigh refcnt leak when x25 disconnect")
Reported-by: Krystian Kaniewski <krystianmkaniewski@xxxxxxxxx>
Closes: https://lore.kernel.org/all/02419845-bab8-4e36-9a98-6830761da1b9@xxxxxxxxx/
Link: https://lore.kernel.org/all/179121364205.434549.10310203820251738113@xxxxxxxxxx/
Signed-off-by: Martin Schiller <ms@xxxxxxxxxx>
---
net/x25/af_x25.c | 10 +++++++++-
net/x25/x25_out.c | 6 ++++++
2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
index a5d3a86918a0f4f337af4566dde4b43a907d1b49..ce22fd657caa911ab79ba510df29ccd1185ce25d 100644
--- a/net/x25/af_x25.c
+++ b/net/x25/af_x25.c
@@ -1212,6 +1212,12 @@ static int x25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
lock_sock(sk);
if (!skb)
goto out;
+
+ /* The call may have been cleared while the socket was unlocked */
+ rc = -ENOTCONN;
+ if (!x25->neighbour)
+ goto out_kfree_skb;
+
X25_SKB_CB(skb)->flags = msg->msg_flags;
skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
@@ -1397,7 +1403,9 @@ static int x25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
msg->msg_namelen = sizeof(*sx25);
}
- x25_check_rbuf(sk);
+ /* The call may have been cleared while the socket was unlocked */
+ if (x25->neighbour)
+ x25_check_rbuf(sk);
rc = copied;
out_free_dgram:
skb_free_datagram(sk, skb);
diff --git a/net/x25/x25_out.c b/net/x25/x25_out.c
index f710d015fcc2ffd6a6922a0c29876bb248125666..89858a3fdf428b3eae5f9c8ba54fee82f73fd0d9 100644
--- a/net/x25/x25_out.c
+++ b/net/x25/x25_out.c
@@ -78,6 +78,12 @@ int x25_output(struct sock *sk, struct sk_buff *skb)
return err;
}
+ /* The call may have been cleared while unlocked */
+ if (!x25->neighbour) {
+ kfree_skb(skbn);
+ return -ENOTCONN;
+ }
+
skb_reserve(skbn, frontlen);
len = max_len > skb->len ? skb->len : max_len;
--
2.47.3