From: Johan Hovold <johan@kernel.org>
To: bin luo <luob87709@gmail.com>
Cc: oe-linux-nfc@lists.linux.dev, david+nfc@ixit.cz
Subject: Re: [PATCH] NFC: fix skb ownership on data exchange errors
Date: Thu, 12 Nov 2026 09:02:54 +0100 [thread overview]
Message-ID: <avVyrsYGxh3ONYH8@hovoldconsulting.com> (raw)
In-Reply-To: <CALa7h7FHWZRHxnqEeAUEH=CNs6Y9pnkfPq1LrDHGfyCNaoqAeg@mail.gmail.com>
On Thu, Nov 12, 2026 at 12:50:52PM +0800, bin luo wrote:
> Hi David and Johan,
>
> Thanks for reviewing the report. I will follow up with this as an ordinary
> NFC bug fix.
>
> The issue is that a data-exchange completion may pass both a non-NULL
> response skb and a nonzero error to its callback, but the callback contract
> does not state who owns the skb in that case. In rawsock,
> rawsock_data_exchange_complete() jumps directly to its error handling path
> and never reaches the existing error_skb cleanup. The LLCP completion
> callback has the same ownership gap: nfc_llcp_recv() logs the error and
> returns without releasing a non-NULL skb.
>
> I propose to make the rule explicit: ownership of every non-NULL response
> skb transfers to the callback, irrespective of err. The rawsock and LLCP
> callbacks then release the skb on their error paths. Digital DEP currently
> calls the callback and then falls through to cleanup that may free resp
> again when rc is nonzero; setting resp to NULL immediately after the
> callback prevents that double-free. The digital_core completion already
> frees an errored response and passes NULL to its callback. The NCI loopback
> callback also needs to release skb if its connection lookup fails.
>
> The final five-file patch was built against Linux v7.4-rc1, commit
> 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4, with the same x86_64
> NFC_VIRTUAL_NCI, KASAN, and kmemleak configuration used for the baseline.
> The QEMU TCG guest ran 32 NCI 1.0 MIFARE exchanges with a nonzero data
> status. Each rawsock create/connect/send/error-receive sequence ran with
> effective UID 65534 after the root harness had provisioned the virtual NCI
> device and polling session. All 32 iterations passed. The fixed-kernel
> serial log contains no kmemleak unreferenced-object records and no KASAN
> report. The baseline run of the same test recorded 48 kmemleak suspects.
> This runtime test covers the NCI-to-rawsock path; the Digital DEP and LLCP
> changes were compile-tested but were not triggered by this QEMU selftest.
> The NCI loopback cleanup was compiled but that failure branch was not
> triggered by the QEMU selftest.
>
> For reproduction, the root harness opens /dev/virtual_nci, registers a
> virtual NCI controller, and starts polling for a MIFARE target. The test then
> switches its effective UID to 65534, creates an AF_NFC SOCK_SEQPACKET socket,
> connects to the target, and sends the MIFARE command 30 00. The virtual
> controller returns the NCI data frame 00 00 02 aa 01: connection id 0, RFU 0,
> payload length 2, response byte aa, and nonzero data status 01. The guest
> repeats this as 32 separately initialized virtual-device sessions and runs
> kmemleak scans afterward. The same test on the baseline produced 47 detailed
> unreferenced-object records; the fixed run produced none.
>
> The complete proposed change is inline below; no binary attachment is
> included.Best regards,
>
> Bin Luo
> University of Electronic Science and Technology of China (UESTC)
Please provide commit message in the common format (problem description
followed by a short indication on how the patch addresses that written
in imperative mood).
It's good to mention how you found and tested things but such
information, along with greetings etc, can also go under the --- line
below so that they don't end up in the commit logs.
See https://docs.kernel.org/process/submitting-patches.html for details.
> Signed-off-by: Bin Luo <luob87709@gmail.com>
>
> ---
> include/net/nfc/nfc.h | 2 ++
> net/nfc/nci/core.c | 1 +
> net/nfc/rawsock.c | 5 ++++-
> net/nfc/llcp_core.c | 2 ++
> net/nfc/digital_dep.c | 1 +
> 5 files changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/include/net/nfc/nfc.h b/include/net/nfc/nfc.h
> --- a/include/net/nfc/nfc.h
> +++ b/include/net/nfc/nfc.h
> @@ -38,5 +38,7 @@
> * When a rx or tx package is lost or corrupted or the target gets out
> * of the operating field, err is -EIO.
> + * If skb is non-NULL, ownership is transferred to the callback regardless
> + * of err. Providers must not access or free skb after invoking the callback.
> */
> typedef void (*data_exchange_cb_t)(void *context, struct sk_buff *skb,
> int err);
>
> diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
> --- a/net/nfc/rawsock.c
> +++ b/net/nfc/rawsock.c
> @@ -157,5 +157,8 @@
> pr_debug("sk=%p err=%d\n", sk, err);
>
> - if (err)
> - goto error;
> + if (err) {
> + if (skb)
> + kfree_skb(skb);
> + goto error;
> + }
Also note that your patch is whitespace damaged (tabs removed) so
something is wrong with your email setup.
Try sending the patch to yourself first and make sure you can run
checkpatch and apply the result using git am.
git-format-patch and git-send-email are useful tools for this.
I believe you should also CC the netdev list.
Johan
prev parent reply other threads:[~2026-11-12 8:02 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 4:50 [PATCH] NFC: fix skb ownership on data exchange errors bin luo
2026-11-12 8:02 ` Johan Hovold [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=avVyrsYGxh3ONYH8@hovoldconsulting.com \
--to=johan@kernel.org \
--cc=david+nfc@ixit.cz \
--cc=luob87709@gmail.com \
--cc=oe-linux-nfc@lists.linux.dev \
/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