All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Horman <horms@kernel.org>
To: Ren Wei <weir@nebusec.ai>
Cc: oe-linux-nfc@lists.linux.dev, netdev@vger.kernel.org,
	david@ixit.cz, davem@davemloft.net, edumazet@google.com,
	kuba@kernel.org, pabeni@redhat.com, kees@kernel.org,
	pengpeng@iscas.ac.cn, raoxu@uniontech.com, rosenp@gmail.com,
	dddddd@hust.edu.cn, joe@dama.to, ian.ray@gehealthcare.com,
	kuniyu@google.com, linma@zju.edu.cn, vega@nebusec.ai,
	rakukuip@gmail.com
Subject: Re: [PATCH v3 1/1] net: nfc: fix use-after-free in nfc_get_local_general_bytes
Date: Sat, 12 Sep 2026 11:16:33 +0100	[thread overview]
Message-ID: <20260912101633.GG48209@horms.kernel.org> (raw)
In-Reply-To: <3cbaac3bee23f8ff3a3284ed32d347696eb1d208.1788841683.git.rakukuip@gmail.com>

On Wed, Sep 09, 2026 at 01:19:24PM +0800, Ren Wei wrote:
> From: Luxiao Xu <rakukuip@gmail.com>
> 
> Commit 6709d4b7bc2e ("net: nfc: Fix use-after-free caused by
> nfc_llcp_find_local") attempted to fix a use-after-free (UAF) issue by
> invoking nfc_llcp_local_put(local) after accessing local->gb. However,
> if the reference count drops to zero, local is freed immediately,
> leading to a use-after-free when callers access the returned pointer.
> Alternative approaches using dynamic allocation (e.g. kmemdup) introduced
> memory leaks because callers consistently treat the returned pointer as
> borrowed memory.
> 
> Fix this properly by refactoring nfc_llcp_general_bytes() and
> nfc_get_local_general_bytes() to accept a caller-provided output buffer
> (out_gb) and its maximum length (gb_max_len). The general bytes are
> safely copied into out_gb before calling nfc_llcp_local_put(local),
> ensuring safe lifetime management without ownership transfer complications.
> 
> Update all callers across drivers (microread, pn533, pn544, st21nfca,
> digital_dep, and nci) to provide their own destination buffers and pass
> them to nfc_get_local_general_bytes().
> 
> Fixes: 6709d4b7bc2e ("net: nfc: Fix use-after-free caused by nfc_llcp_find_local")
> Cc: stable@vger.kernel.org
> Reported-by: Vega <vega@nebusec.ai>
> Assisted-by: LLM
> Signed-off-by: Luxiao Xu <rakukuip@gmail.com>
> Signed-off-by: Ren Wei <weir@nebusec.ai>
> ---
> v3:
>  - Include <net/nfc/nfc.h> in pn533.h to fix build error in uart.c
>    caused by undefined NFC_MAX_GT_LEN.
>  - Restore nci_request() in nci_set_local_general_bytes() to preserve
>    ndev->req_lock synchronization (avoid unlocked __nci_request() via
>    nci_set_config()).
> v2:
>  - Use caller-provided output buffers to fix UAF instead of dynamic
>    allocation (kmemdup), avoiding memory leaks.

Overall this patch looks good to me.
But I'd appreciate it if you could consider my suggestion further below.

Reviewed-by: Simon Horman <horms@kernel.org>

...

> diff --git a/net/nfc/llcp_core.c b/net/nfc/llcp_core.c
> index dc65c719f35f..24cf212bf3f8 100644
> --- a/net/nfc/llcp_core.c
> +++ b/net/nfc/llcp_core.c
> @@ -635,23 +635,32 @@ static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
>  	return ret;
>  }
>  
> -u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
> +u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, u8 *out_gb, size_t gb_max_len,
> +			   size_t *general_bytes_len)
>  {
>  	struct nfc_llcp_local *local;
>  
> +	if (!out_gb || !general_bytes_len)
> +		return NULL;
> +
>  	local = nfc_llcp_find_local(dev);
> -	if (local == NULL) {
> +	if (!local) {
>  		*general_bytes_len = 0;
>  		return NULL;
>  	}
>  
>  	nfc_llcp_build_gb(local);
>  
> -	*general_bytes_len = local->gb_len;
> +	if (local->gb_len) {
> +		*general_bytes_len = min_t(size_t, local->gb_len, gb_max_len);
> +		memcpy(out_gb, local->gb, *general_bytes_len);
> +	} else {
> +		*general_bytes_len = 0;
> +	}
>  
>  	nfc_llcp_local_put(local);
>  
> -	return local->gb;
> +	return out_gb;
>  }

As far as I can see the return value of nfc_llcp_general_bytes() is now
ignored. And for a bug fix the approach you have taken looks good,
as the interface provided by nfc_llcp_general_bytes() is only slightly
modified.

But, FWIIW, I think it would be cleaner if nfc_llcp_general_bytes()
returned the length, or 0 on error. And the general_bytes_len parameter was
passed by value rather than reference.

Something like this:

size_t nfc_llcp_general_bytes(struct nfc_dev *dev, u8 *out_gb,
			      size_t gb_max_len, size_t general_bytes_len)
{
	...

	if (error_condition)
		return 0;

	...

	return general_bytes_len;
}

Perhaps that approach could be considered as a follow-up.

      reply	other threads:[~2026-09-12 10:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-09  5:19 [PATCH v3 0/1] net: nfc: fix use-after-free in nfc_get_local_general_bytes Ren Wei
2026-09-09  5:19 ` [PATCH v3 1/1] " Ren Wei
2026-09-12 10:16   ` Simon Horman [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=20260912101633.GG48209@horms.kernel.org \
    --to=horms@kernel.org \
    --cc=davem@davemloft.net \
    --cc=david@ixit.cz \
    --cc=dddddd@hust.edu.cn \
    --cc=edumazet@google.com \
    --cc=ian.ray@gehealthcare.com \
    --cc=joe@dama.to \
    --cc=kees@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linma@zju.edu.cn \
    --cc=netdev@vger.kernel.org \
    --cc=oe-linux-nfc@lists.linux.dev \
    --cc=pabeni@redhat.com \
    --cc=pengpeng@iscas.ac.cn \
    --cc=rakukuip@gmail.com \
    --cc=raoxu@uniontech.com \
    --cc=rosenp@gmail.com \
    --cc=vega@nebusec.ai \
    --cc=weir@nebusec.ai \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.