Netdev List
 help / color / mirror / Atom feed
From: David Heidelberg <david@ixit.cz>
To: Shuangpeng Bai <shuangpeng.kernel@gmail.com>,
	oe-linux-nfc@lists.linux.dev
Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	pabeni@redhat.com, horms@kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] nfc: llcp: Fix nfc_dev refcount leak in connect
Date: Sun, 19 Jul 2026 15:44:45 +0200	[thread overview]
Message-ID: <1c2cd3d9-7d97-482c-aced-63bc93faf430@ixit.cz> (raw)
In-Reply-To: <20260707183518.1888697-1-shuangpeng.kernel@gmail.com>

On 07/07/2026 20:35, Shuangpeng Bai wrote:
> llcp_sock_connect() takes a reference to the NFC device with
> nfc_get_device() and stores it in llcp_sock->dev before moving the
> socket to LLCP_CONNECTING.
> 
> If such a pending connection is released before reaching
> LLCP_CONNECTED, llcp_sock_destruct() does not drop that device
> reference because it only handles connected sockets. This leaks the
> nfc_dev reference acquired during connect.
> 
> Drop the device reference for LLCP_CONNECTING sockets as well.
> 
> Signed-off-by: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
> ---
>   net/nfc/llcp_sock.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 

Hello Shuangpeng,

could you check the Sashiko review, if you can address the issue found within 
the patch (the existing code issues are optional to address)?

--- Patch [1]: [PATCH] nfc: llcp: Fix nfc_dev refcount leak in connect ---
commit 84e092575501f812e707c62695cef1ec7ff9d8a6
Author: Shuangpeng Bai <shuangpeng.kernel@gmail.com>

nfc: llcp: Fix nfc_dev refcount leak in connect

llcp_sock_connect() takes a reference to the NFC device with
nfc_get_device() and stores it in llcp_sock->dev before moving the
socket to LLCP_CONNECTING. If such a pending connection is released
before reaching LLCP_CONNECTED, llcp_sock_destruct() does not drop that
device reference because it only handles connected sockets. This leaks the
nfc_dev reference acquired during connect. Drop the device reference for
LLCP_CONNECTING sockets as well.

 > diff --git a/net/nfc/llcp_sock.c b/net/nfc/llcp_sock.c
 > index feab29fc62f44..44639ccd438e4 100644
 > --- a/net/nfc/llcp_sock.c
 > +++ b/net/nfc/llcp_sock.c
 > @@ -960,7 +960,8 @@ static void llcp_sock_destruct(struct sock *sk)
 >
 >  	pr_debug("%p\n", sk);
 >
 > -	if (sk->sk_state == LLCP_CONNECTED)
 > +	if (sk->sk_state == LLCP_CONNECTED ||
 > +	    sk->sk_state == LLCP_CONNECTING)

This isn't a bug introduced by this patch, but does llcp_sock_connect()
lack a state check to prevent it from being called on sockets in LLCP_BOUND
or LLCP_LISTEN states?

If a user creates an AF_NFC socket and calls bind(), placing the socket in
the LLCP_BOUND state, it links into the local->sockets list via its sk_node.
If the user then calls connect(), which only checks for LLCP_CONNECTED and
LLCP_CONNECTING states:

net/nfc/llcp_sock.c:llcp_sock_connect() {
     if (sk->sk_state == LLCP_CONNECTED) {
         ret = -EISCONN;
         goto error;
     }
     if (sk->sk_state == LLCP_CONNECTING) {
         ret = -EINPROGRESS;
         goto error;
     }
     ...
}

It proceeds to overwrite llcp_sock->service_name (leaking the previously
allocated string) and calls nfc_llcp_sock_link(&local->connecting_sockets,
sk).

This executes hlist_add_head() on a node (sk->sk_node) that is already part
of another list, thereby corrupting the pointers of both the sockets and
connecting_sockets lists.

 > +		nfc_put_device(llcp_sock->dev);

Can this introduce a kernel panic via a NULL pointer dereference when a
blocking connect() call fails or is interrupted?

In llcp_sock_connect(), if the wait fails or is interrupted by a signal,
execution jumps to the sock_unlink error path:

net/nfc/llcp_sock.c:llcp_sock_connect() {
     sk->sk_state = LLCP_CONNECTING;

     ret = sock_wait_state(sk, LLCP_CONNECTED,
                           sock_sndtimeo(sk, flags & O_NONBLOCK));
     if (ret && ret != -EINPROGRESS)
         goto sock_unlink;
     ...
sock_llcp_nullify:
     llcp_sock->local = NULL;
     llcp_sock->dev = NULL;
}

This error path nullifies llcp_sock->dev and drops the device reference,
but crucially fails to reset sk->sk_state back to LLCP_CLOSED.

When the user subsequently closes the socket, llcp_sock_destruct() is
invoked. The newly added logic checks for LLCP_CONNECTING sockets and
unconditionally calls nfc_put_device(llcp_sock->dev).

Since dev is NULL, nfc_put_device() evaluates &NULL->dev:

include/net/nfc/nfc.h:nfc_put_device() {
     put_device(&dev->dev);
}

This evaluates to a non-zero struct offset and bypasses the internal NULL
check in put_device(), attempting to decrement the reference count of
unmapped memory.

 >
 >  	skb_queue_purge(&sk->sk_receive_queue);
 >

      reply	other threads:[~2026-07-19 13:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-07 18:35 [PATCH] nfc: llcp: Fix nfc_dev refcount leak in connect Shuangpeng Bai
2026-07-19 13:44 ` David Heidelberg [this message]

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=1c2cd3d9-7d97-482c-aced-63bc93faf430@ixit.cz \
    --to=david@ixit.cz \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=oe-linux-nfc@lists.linux.dev \
    --cc=pabeni@redhat.com \
    --cc=shuangpeng.kernel@gmail.com \
    /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