Article View: linux.kernel.netdev
Article #21597Re: [PATCH net] nfc: llcp: fix double put of nfc_dev on interrupted connect
From: netdev-bot+sashiko
Date: Tue, 24 Nov 2026 08:29
Date: Tue, 24 Nov 2026 08:29
301 lines
18576 bytes
18576 bytes
Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider.
Critical: 0 · High: 3 · Medium: 2 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The fix is incomplete.
- [High] The patch adds an nfc_dev refcount leak.
- [High] The new early return in llcp_sock_connect() (socket on
local->sockets, state not CONNECTED, ret = -ERESTARTSYS/-EINTR/-ENXIO)…
- [Medium] The commit message gets the failure mechanism wrong and makes
false claims about ownership.
- [Medium] The ownership heuristic is also fooled by bind() followed by
connect().
- [Low] The comment added in llcp_sock_connect() says the 'dev/local
references now belong to the socket.
--
nfc: llcp: fix double put of nfc_dev on interrupted connect
This patch changes the interrupted-wait error path in llcp_sock_connect().
When the socket is found linked on local->sockets, the nfc_dev and local
references taken by connect() are no longer unwound. A new helper,
llcp_sock_on_local_sockets(), treats list membership as meaning that the
CC handler has claimed the socket.
> While the socket waits for the peer, nfc_llcp_recv_cc() can already
> have claimed it: the CC handler moves the socket from
> connecting_sockets to local->sockets and the reference now belongs to
> the socket. It is dropped exactly once, by nfc_llcp_recv_disc() or by
> llcp_sock_destruct().
[Severity: Medium]
Is this description of the failure complete? A CC frame on its own does
not seem able to make sock_wait_state() return an error.
nfc_llcp_recv_cc() sets LLCP_CONNECTED under lock_sock(). After
schedule_timeout(), the loop takes the lock again and checks the state
before it reaches signal_pending():
net/nfc/llcp_sock.c:sock_wait_state() {
while (sk->sk_state != state) {
...
if (signal_pending(current)) {
err = sock_intr_errno(timeo);
break;
}
release_sock(sk);
timeo = schedule_timeout(timeo);
lock_sock(sk);
...
}
}
So the double put seems to need a CC followed by something that drops
the CONNECTED-state dev reference and takes the socket out of
LLCP_CONNECTED, such as nfc_llcp_recv_disc() or nfc_llcp_socket_release().
The "dropped exactly once" list also leaves out two functions:
nfc_llcp_socket_release() calls nfc_put_device() for CONNECTED sockets.
nfc_llcp_recv_dm() closes a claimed socket without dropping the
reference.
> Check whether the CC handler already claimed the socket - it is the
> only path that links a connecting socket into local->sockets - and
> leave the references with the socket in that case.
Is "the only path" accurate? llcp_sock_bind() also links sk into
local->sockets and sets LLCP_BOUND, and llcp_sock_connect() does not
reject LLCP_BOUND.
The commit message also doesn't explain the new "if (sk->sk_state ==
LLCP_CONNECTED) ret = 0;" branch. sock_wait_state() only returns an
error while CONNECTED if sk_err was already set. Which case is that
branch meant to handle?
> diff --git a/net/nfc/llcp_sock.c b/net/nfc/llcp_sock.c
> index 1e5ee4bcde684..22b2f65e984a4 100644
> --- a/net/nfc/llcp_sock.c
> +++ b/net/nfc/llcp_sock.c
> @@ -683,6 +683,36 @@ static int llcp_sock_release(struct socket *sock)
> return err;
> }
>
> +/**
> + * llcp_sock_on_local_sockets() - check if a socket was claimed by a CC
> + * @local: the LLCP local the socket connected on
> + * @sk: the socket to look up
> + *
> + * nfc_llcp_recv_cc() moves a connecting socket to local->sockets when the
> + * peer answers with a CC frame. Only that path links a connecting socket
> + * into local->sockets, so membership doubles as "the peer claimed the
> + * socket". The caller must hold lock_sock().
[Severity: Medium]
Can bind() followed by connect() make this helper return true when no
CC has arrived?
llcp_sock_bind():
nfc_llcp_sock_link(&local->sockets, sk);
...
sk->sk_state = LLCP_BOUND;
llcp_sock_connect() only rejects LLCP_CONNECTED and LLCP_CONNECTING, so
a bound socket goes on to:
nfc_llcp_sock_link(&local->connecting_sockets, sk);
This links the same sk_node a second time. That hlist double link is
older than this patch. But because local->sockets still reaches sk_node,
llcp_sock_on_local_sockets() finds sk.
If a signal interrupts sock_wait_state() before any CC arrives, the new
code takes goto out. It returns -ERESTARTSYS or -EINTR with the socket
still in LLCP_CONNECTING.
The nfc_dev reference taken by connect() is then never dropped, because
llcp_sock_destruct() only puts it for LLCP_CONNECTED. connect() also
overwrote llcp_sock->local, so the local reference taken by bind() leaks.
Before this patch, the error path unwound both references taken by
connect().
> + *
> + * Return: true if @sk is linked in local->sockets.
> + */
[ ... ]
> @@ -781,9 +811,27 @@ static int llcp_sock_connect(struct socket *sock, struct sockaddr_unsized *_addr
>
> ret = sock_wait_state(sk, LLCP_CONNECTED,
> sock_sndtimeo(sk, flags & O_NONBLOCK));
> - if (ret && ret != -EINPROGRESS)
> + if (ret) {
> + if (ret == -EINPROGRESS)
> + goto out;
> +
> + /* If the CC frame was processed while we slept, recv_cc()
> + * moved the socket to local->sockets and the dev/local
> + * references now belong to the socket. They are dropped by
> + * nfc_llcp_recv_disc() or llcp_sock_destruct(); unwinding
> + * them on this error path would put the nfc_dev reference
> + * obtained above a second time.
> + */
[Severity: Low]
Does this comment correctly say who releases these references?
nfc_llcp_recv_disc() only drops the dev reference, and never the local
kref:
if (sk->sk_state == LLCP_CONNECTED) {
nfc_put_device(local->dev);
...
The local reference is only released through
llcp_sock_destruct()->nfc_llcp_sock_free()->nfc_llcp_local_put().
llcp_sock_destruct() drops the dev reference only when sk_state is
LLCP_CONNECTED.
nfc_llcp_socket_release() also drops the dev reference for CONNECTED
sockets, and the comment doesn't mention it.
> + if (llcp_sock_on_local_sockets(local, sk)) {
> + if (sk->sk_state == LLCP_CONNECTED)
> + ret = 0;
> + goto out;
> + }
[Severity: High]
Does this leak the nfc_dev reference if the peer sends a CC and then a
DM?
Say rx_work handles a CC and then a DM whose reason is not
LLCP_DM_NOBOUND or LLCP_DM_REJ, before the connecting thread gets
lock_sock() back. nfc_llcp_recv_dm() takes the default branch and finds
the socket on local->sockets with nfc_llcp_sock_get(). It then does:
sk->sk_err = ENXIO;
sk->sk_state = LLCP_CLOSED;
sk->sk_state_change(sk);
It doesn't call nfc_put_device(), and it leaves sk on local->sockets.
sock_wait_state() then returns -ENXIO from sock_error().
llcp_sock_on_local_sockets() returns true and the state isn't
CONNECTED, so we goto out and skip put_dev. Later, llcp_sock_destruct()
skips the put as well:
if (sk->sk_state == LLCP_CONNECTED)
nfc_put_device(llcp_sock->dev);
Before this patch, this sequence went to sock_unlink and the reference
was dropped once. Does the nfc_dev now stay pinned for good?
[Severity: High]
What state is the socket in when this path returns an error with
sk_state not CONNECTED?
The socket stays linked on local->sockets in LLCP_CLOSED. It still holds
llcp_sock->local, the reserved ssap and the kmemdup'd service_name.
For example, take a CC followed by a DISC with non-zero SAPs.
nfc_llcp_recv_disc() puts dev and sets LLCP_CLOSED, but leaves sk on
local->sockets. A signal then makes sock_wait_state() return
sock_intr_errno(timeo). With the default sk_sndtimeo of
MAX_SCHEDULE_TIMEOUT, that is -ERESTARTSYS.
Now suppose the syscall is restarted or userspace retries. The entry
checks in llcp_sock_connect() only reject LLCP_CONNECTED and
LLCP_CONNECTING, so the second call goes ahead. It overwrites
llcp_sock->local, dev, ssap, reserved_ssap and service_name, which leaks
the old local reference, the ssap reservation and the service_name
buffer. It then does:
nfc_llcp_sock_link(&local->connecting_sockets, sk);
sk_add_node() calls hlist_add_head() on sk_node without any check, and
sk_node is still linked in local->sockets. Can this corrupt both
local->sockets and connecting_sockets, and lead to a use-after-free on
later list walks?
Before this patch, this error path went through sock_unlink, so a
restarted connect() started from a clean socket. A similar double link
already exists if userspace reconnects after a normal connect plus
DISC. Here, though, a remote peer and an ordinary signal create the
state, and the kernel's own syscall restart triggers the corruption.
> +
> goto sock_unlink;
> + }
[Severity: High]
Can the double put still happen through nfc_llcp_socket_release()?
That function takes sockets off local->sockets with
nfc_llcp_sock_list_pop(), which only holds the list write lock, not
lock_sock():
nfc_llcp_sock_list_pop()
write_lock(&l->lock);
sk = sk_head(&l->head);
if (sk) {
sock_hold(sk);
sk_del_node_init(sk);
}
Then, for each socket it pops:
if (sk->sk_state == LLCP_CONNECTED)
nfc_put_device(llcp_sock->dev);
...
if (err)
sk->sk_err = err;
sk->sk_state = LLCP_CLOSED;
So holding lock_sock() in llcp_sock_connect() doesn't keep list
membership stable. That also contradicts "The caller must hold
lock_sock()" in the new kernel-doc.
There are two ways to get here while connect() sleeps after the CC was
handled:
rx_work: nfc_llcp_recv_disc() with dsap == 0 and ssap == 0
nfc_dep_link_down()
nfc_llcp_mac_is_down()
nfc_llcp_socket_release(local, true, 0)
nfc_llcp_unregister_device()
local_cleanup()
nfc_llcp_socket_release(local, false, ENXIO)
Either way, the socket's dev reference is dropped and sk is no longer on
any list. In the unregister case, sock_error() returns -ENXIO and no
signal is needed. In the DISC(0,0) case, the state is CLOSED, so the
wait continues until a signal returns -ERESTARTSYS or -EINTR.
llcp_sock_on_local_sockets() then returns false, we goto sock_unlink,
and put_dev calls nfc_put_device(dev) a second time. Isn't that the same
kref underflow and nfc_dev use-after-free that the commit message
describes?
Would tracking ownership of the references explicitly work better here
than inferring it from list membership?
>
> +out:
> release_sock(sk);
>
> return ret;
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120081354.4098856-1-bsdhenrymartin%40gmail.com
Technical Details
Message-ID:
<179550898290.3135.6710394263567960134@kernel.org>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!3.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370104-linux-kernel-netdev=dewy.ruReferences:
<20261120081354.4098856-1-bsdhenrymartin@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!3.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370104-linux-kernel-netdev=dewy.ru From: netdev-bot+sashiko@kernel.org Newsgroups: linux.kernel.netdev Subject: Re: [PATCH net] nfc: llcp: fix double put of nfc_dev on interrupted connect Date: Tue, 24 Nov 2026 08:29:42 +0000 Organization: Usenet Network Message-ID: <179550898290.3135.6710394263567960134@kernel.org> References: <20261120081354.4098856-1-bsdhenrymartin@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="4088601"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:pRqGWgxu/LSXOVQMlbvT9qVXm0s= sha256:pl7SdyzlMX7M9ECM6U0lWRbNOSxXjjoKW8aHVKVZFsw= sha1:PkY1Zop1tlWkCwFSc9+787EDe1I= sha256:3deE6L4HcFssHUetnhH53oqQOjkTCoFZ3zd7mX9qC6g= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 8D0C4125DE0; Tue, 24 Nov 2026 11:30:04 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.401 X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id KOjOLgSAb_k6; Tue, 24 Nov 2026 11:30:00 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-370104-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114]) by mail.dewy.ru (Postfix) with ESMTPS id C8259125DD6; Tue, 24 Nov 2026 11:29:49 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by tor.lore.kernel.org (Postfix) with ESMTP id 7750634601; Tue, 24 Nov 2026 08:29:48 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795508985; b=yPEaIl7cgkgyRPVnzt1QE+eJoBxVwDciwUa13tgWDZKWKJ+7U1CcLsqeWEP0uNQ43UPa vcVuoWUdX6Uw3p9Ft+/g5GOOxn6doGYNMjQn7sl6KU8mKifByl2UPjP3R4DazuAiqL53l a539wjuhp8Qtw9Mo4P3SQOxoHyEc0C3dloFWXfn9XTC+SpeN1tGcCHCnjdFS4h2aUVtmI sK7WhsqLzyeOT+FcoK2v0TKVjeY512hXGHlkvwbZI3snvTU23mkU/0iTl1YsuKjf8XlzK Hb0CnyAQckCvnhixpoFFf9v+33sj26SI0w0DYaDdqhzALQHX0PWSlmirUsvzC9kcVzQ== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795508985; h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc: Date:Message-ID:In-Reply-To:References:X-sashiko-severity: Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id: List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner: Precedence:MIME-Version; bh=7CedIZqW8uK1+Z6TUaHzLF+svnPdCe1eL2kyZtyrFhM=; b=sOlsv9H88M8RBeR+ihzh08vT3i5wYLzGq0d+ct3LvozzFJGwooWWa1XG2eyuFD5t/yE5 96e36YHc8ChsG7up5pUcGGqhoUQraIfGLhlVhol52xF8xwrex9xaj5WOSXorabHzLhmWh B0vzfwFz9NT8JhQEWbXcGDrAth7apFRmnIZUws746U9UbUJl1yD5+qMfnhvHiCioouNmC yntZCT+FCbe2ngSamwDFOE/az8hWxzMXdbk8s0P7Kpi9g8ZenjuAU2c7i+8xwrFvixPvS HpKCGWWaed189b9zhiNmGc9y6JIZdBj0SysYqwyXFaBjBOeBX59PDSRHVq04Fy3qusw== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Yv4B1IO8; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6883B606A330; Tue, 24 Nov 2026 08:29:45 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Yv4B1IO8 X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795508984; b=MXUwo0mCXm26UDce5AyX/FQNmg9SPoTL3oFyNm0omAtcW6ljvd95VcLAw7HGe/ew+ql3 KhjdZMsmlHk8Tjlnd4Pcgrw3lVqDz5Mx3r1uUUy6qXyuAYzIQ8fC43Pidg/8ajag557N/ Gtl1gCJHBwizzJVgpgOZytZS5QmkTLZaW6YWvigoHLBsdiNi8vjnHICq/El9krxnl5ryA lj04Cr7dyL/gTbSTQm7paTcx/hnbXwCqwnIMjBZNSx/IV3gYRsM2waoRgyimcw1joTCpW /bkbRvGXV604HfoW4pTwmiZiiGVtnk9kstl7rXWrcmzU2icvzJ7XgMTIpzff61wNN8g== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795508984; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=7CedIZqW8uK1+Z6TUaHzLF+svnPdCe1eL2kyZtyrFhM=; b=r5VOyLPHYULUtcwyAlOgjoXWzNr9DPoxeWK9drrNQN+md4NDKqAHf3CiyGoRmoFwhJz0 K7AlZV34epYPr1qEKpwO2Tuyxc6CbSpiw7CRSvHf7XnV+w0y0Sd4+unKY4zB/0gM54wIL j6+3zY82ZQ5kEzlUC67Hh/chjq2Hbl6Jc0I7kRbkN/0NmpF+8ociN32277j9S1z4sc821 KHiGs9Dez4LzBV76knKHI0FcDQnUqaHj3BSIo5EaPkJ/gkKpZjf/mzrM+ILdwINqQe/1E pp1FzXqr6uaRB3+aVzO+W86MRZfmMCUMS1D+9PF7ZIdyVjG1H/JpGmllLQW2RpCIMMw== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Yv4B1IO8; arc=none smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 84288606A31E; Tue, 24 Nov 2026 08:29:44 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 580EF1F000C4; Tue, 24 Nov 2026 08:29:43 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795508983; bh=7CedIZqW8uK1+Z6TUaHzLF+svnPdCe1eL2kyZtyrFhM=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=Yv4B1IO8X+uV5oGuT//cJjpUkkEKqNgiRFkdPjU2IsqEtGzTBaGCcRdA9YlB4d0sZ MJeFwEBxqS11LLlV7G80+VQKaZbmwy1JZ5vlh7q+MyeLrVZK8/xZd6G/wRlthn7a0Z fbU+dRRdGQKxLa6hyBMmvanLXLRis9ECu8nXK8vhH+WEyZCHOQF1NATBNqtNKOuo+F QN33vB0rDcYqzrH6H6N7O3Y2uCO5r6dU40oM2ISFaHMsS9m5nLOun0/biCFN2It3KQ /YG10f3+TylA3V9xy57eH+f09QLWK8EwhXMBT13acd26oSdE3Io+nI782Bsn86SgIF zulAcPUEHyJUw== X-Cc: david@ixit.cz, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, oe-linux-nfc@lists.linux.dev, netdev@vger.kernel.org, stable@vger.kernel.org X-In-Reply-To: <20261120081354.4098856-1-bsdhenrymartin@gmail.com> X-sashiko-severity: High X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list