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 and reproduced all three in QEMU without modifying the kernel: a second thread owns the socket, stalled in a userfaultfd copy, while sendmsg() or recvmsg() waits with the lock dropped, and a CLEAR REQUEST from the peer goes to the backlog. Without this patch, KASAN reports a NULL pointer dereference in x25_sendmsg(), in x25_output() or, called from x25_check_rbuf(), in x25_write_internal(). With it, send() fails with ENOTCONN and recv() returns the data. Fixes: 4becb7ee5b3d ("net/x25: Fix x25_neigh refcnt leak when x25 disconnect") Reported-by: Krystian Kaniewski <krys…@gmail.com> Closes: https://lore.kernel.org/all/0241…@gmail.com/ Link: https://lore.kernel.org/all/1791…@kernel.org/ Tested-by: Krystian Kaniewski <krys…@gmail.com> Tested-by: Shuai Liu <8478…@qq.com> Reviewed-by: Shuai Liu <8478…@qq.com> Signed-off-by: Martin Schiller <m…@dev.tdt.de> --- 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 43c606a6bfa5a3cf69c905f230edf2c37a85e25d..1f9d5f7faffe26289f5723ade5d4d568a522fae6 100644 --- a/net/x25/af_x25.c +++ b/net/x25/af_x25.c @@ -1248,6 +1248,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); @@ -1433,7 +1439,9 @@ static int x25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, msg->msg_namelen = sizeof(*sx25); } - x25_check_rbuf_owned(sk); + /* The call may have been cleared while the socket was unlocked */ + if (x25->neighbour) + x25_check_rbuf_owned(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