From: Martin Schiller <ms@dev.tdt.de>
To: Martin Schiller <ms@dev.tdt.de>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Willem de Bruijn <willemb@google.com>,
Simon Horman <horms@kernel.org>, Lin Ma <linma@zju.edu.cn>,
Duoming Zhou <duoming@zju.edu.cn>,
Xiyu Yang <xiyuyang19@fudan.edu.cn>,
Xin Tan <tanxin.ctf@gmail.com>
Cc: linux-x25@vger.kernel.org, netdev@vger.kernel.org,
Nguyen Ngoc Thang <ngocthang2710.1999@gmail.com>,
Shuai Liu <847807867@qq.com>,
Krystian Kaniewski <krystianmkaniewski@gmail.com>,
syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com,
syzbot+9faa82ae2e94c5c7d2ef@syzkaller.appspotmail.com,
linux-kernel@vger.kernel.org
Subject: [PATCH net v2 2/2] net/x25: re-check x25->neighbour after re-acquiring the socket lock
Date: Fri, 27 Nov 2026 07:28:06 +0100 (CET) [thread overview]
Message-ID: <20261127-x25-kill-by-neigh-v2-2-c7cdcc9f1320@dev.tdt.de> (raw)
In-Reply-To: <20261127-x25-kill-by-neigh-v2-0-c7cdcc9f1320@dev.tdt.de>
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@gmail.com>
Closes: https://lore.kernel.org/all/02419845-bab8-4e36-9a98-6830761da1b9@gmail.com/
Link: https://lore.kernel.org/all/179121364205.434549.10310203820251738113@kernel.org/
Signed-off-by: Martin Schiller <ms@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 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
next prev parent reply other threads:[~2026-11-27 6:28 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 6:28 [PATCH net v2 0/2] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh() Martin Schiller
2026-11-27 6:28 ` [PATCH net v2 1/2] " Martin Schiller
2026-11-27 6:28 ` Martin Schiller [this message]
2026-11-27 6:33 ` [PATCH net v2 0/2] " netdev-bot+sinfo
2026-11-27 6:43 ` Martin Schiller
2026-11-27 11:48 ` Martin Schiller
2026-11-27 13:30 ` Krystian Kaniewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261127-x25-kill-by-neigh-v2-2-c7cdcc9f1320@dev.tdt.de \
--to=ms@dev.tdt.de \
--cc=847807867@qq.com \
--cc=davem@davemloft.net \
--cc=duoming@zju.edu.cn \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=krystianmkaniewski@gmail.com \
--cc=kuba@kernel.org \
--cc=linma@zju.edu.cn \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-x25@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=ngocthang2710.1999@gmail.com \
--cc=pabeni@redhat.com \
--cc=syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com \
--cc=syzbot+9faa82ae2e94c5c7d2ef@syzkaller.appspotmail.com \
--cc=tanxin.ctf@gmail.com \
--cc=willemb@google.com \
--cc=xiyuyang19@fudan.edu.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®