🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #27273

Re: [PATCH net v2 2/3] nfc: nfcsim: stop link and works before unregistering digital device

#27273
From: netdev-bot+sashiko
Date: Sat, 05 Dec 2026 06:57
242 lines
16829 bytes
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 (1) · Medium: 2 · Low: 0

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).

- [Medium] Clearing dev->up first in nfcsim_device_free() lets in-flight
  digital commands be dropped silently, which leaks the caller's…
- [Medium] nfc: nfcsim: dev->up race before unregister stalls recv_work
  teardown

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but
  port100_disconnect() (drivers/nfc/port100.c:1615-1634) has the same…

--

nfc: nfcsim: stop link and works before unregistering digital device

This patch reorders nfcsim_device_free() so that dev->up is cleared,
link_in is shut down, and send_work/recv_work are disabled before
nfc_digital_unregister_device() drains the digital command queue. The goal
is to close a remaining use-after-free where nfcsim_recv_wq() calls
digital_send_cmd_complete() on a digital_cmd already freed by the drain
loop.

> diff --git a/drivers/nfc/nfcsim.c b/drivers/nfc/nfcsim.c
> index f8e5c4da3b13d..f3ddae6d41823 100644
> --- a/drivers/nfc/nfcsim.c
> +++ b/drivers/nfc/nfcsim.c
> @@ -411,14 +411,14 @@ static struct nfcsim *nfcsim_device_new(struct nfcsim_link *link_in,
>  
>  static void nfcsim_device_free(struct nfcsim *dev)
>  {
> -	nfc_digital_unregister_device(dev->nfc_digital_dev);
> -
>  	dev->up = false;

[Severity: Medium]

If dev->up is cleared this early, can in-flight digital commands get dropped
without their completion callback being called?

The digital core is still running at this point. ddev->cmd_work,
cmd_complete_work and poll_work are only disabled later, inside
nfc_digital_unregister_device().

A new command can still be queued during that window. For example, say
nfcsim_recv_wq() passed the !dev->up check just before teardown and queued
cmd_complete_work. That work then runs after
disable_work_sync(&dev->recv_work) returns. Its cmd_cb can queue a DEP
ACK/RTOX via digital_in_send_cmd(). LLCP tx_work can also call
nfc_data_exchange()->digital_in_send(). shutting_down is not set yet at this
point.

digital_wq_cmd() then reaches nfcsim_send(), which now sees !dev->up:

drivers/nfc/nfcsim.c:nfcsim_send() {
    ...
	if (!dev->up) {
		NFCSIM_ERR(dev, "Device is down\n");
		return -ENODEV;
	}
    ...
}

The error path in digital_wq_cmd() then frees the cmd without calling
cmd->cmd_cb:

net/nfc/digital_core.c:digital_wq_cmd() {
    ...
	mutex_lock(&ddev->cmd_lock);
	list_del(&cmd->queue);
	mutex_unlock(&ddev->cmd_lock);

	kfree_skb(cmd->req);
	kfree(cmd->mdaa_params);
	kfree(cmd);
    ...
}

Would this leak the struct digital_data_exch allocated in digital_in_send()?
It only seems to be freed in digital_in_recv_dep_res().

ddev->saved_skb from digital_in_send_dep_req() also doesn't appear to be
freed, because nfc_digital_free_device() does not release it.

The upper layer would also never get an -ENODEV completion. That layer is
nfc_llcp_recv(), or rawsock_data_exchange_complete(), which drops a socket
reference.

Before this patch, dev->up stayed true until unregister had cancelled
cmd_work. nfcsim_send() then returned 0, and the command stayed queued until
the drain loop called cmd_cb(..., ERR_PTR(-ENODEV)).

Does the use-after-free fix need the early dev->up = false? It looks like
disable_work_sync(&dev->recv_work) before nfc_digital_unregister_device()
would be enough by itself. The commit message also doesn't mention this side
effect.

The window seems to remain after "nfc: digital: stop works and drain
commands before nfc_remove_device()" later in the series too. That change
disables cmd_work inside unregister, which is still after dev->up is
cleared.

>  
>  	nfcsim_link_shutdown(dev->link_in);
>  
> -	cancel_delayed_work_sync(&dev->send_work);
> -	cancel_work_sync(&dev->recv_work);
> +	disable_delayed_work_sync(&dev->send_work);
> +	disable_work_sync(&dev->recv_work);
> +
> +	nfc_digital_unregister_device(dev->nfc_digital_dev);

[Severity: Medium]
Is dev->up = false actually one-way at this point? dev->up is a plain
bool that tracks RF state. The digital device stays registered until
nfc_digital_unregister_device() runs below, so ddev->poll_work and the
netlink dev_up/start_poll paths are still live.

nfcsim_in_configure_hw(), nfcsim_tg_configure_hw() and
nfcsim_switch_rf(ddev, true) all set dev->up = true unconditionally:

drivers/nfc/nfcsim.c:nfcsim_in_configure_hw() {
    ...
	case NFC_DIGITAL_CONFIG_RF_TECH:
		dev->up = true;
		dev->mode = NFCSIM_MODE_INITIATOR;
		dev->rf_tech = param;
		break;
    ...
}

Say an in-flight nfcsim_recv_wq() is woken by nfcsim_link_shutdown()
and gets an error skb back. If digital_wq_poll() reconfigures the RF
tech before the !dev->up check, the worker sees dev->up == true again
and calls dev->cb().

That callback can move the poll on and send the next frame through
digital_wq_cmd()->nfcsim_send(). nfcsim_send() also sees dev->up ==
true and calls schedule_work(&dev->recv_work). This can happen while
this function is still waiting in
disable_delayed_work_sync(&dev->send_work).

The newly queued nfcsim_recv_wq() then sleeps in
nfcsim_link_recv_skb():

	rc = wait_event_interruptible_timeout(link->recv_wait,
					      link->cond,
					      msecs_to_jiffies(timeout));

The wait condition only tests link->cond, not link->shutdown. The
previous pass already reset link->cond = 0 (outside link->lock) after
nfcsim_link_shutdown() had set it. Nothing wakes the new worker, so
disable_work_sync(&dev->recv_work) waits for the full receive timeout.
For an NFC-DEP initiator that can be up to 5049 ms from
digital_rwt_map[], which stalls module unload.

There is a second variant. If dev->up flips back to true after both
disable calls but before unregister cancels ddev->cmd_work,
nfcsim_send() stores the skb in dev->link_out and returns 0. Its
schedule_work() and schedule_delayed_work() calls silently fail on the
disabled works.

This doesn't seem to reopen the digital_send_cmd_complete()
use-after-free, because recv_work is still disabled before the drain
loop. But the teardown now depends on dev->up staying false while the
digital core can still set it back to true.

Before this patch, unregister stopped poll_work and cmd_work before
dev->up was cleared, so this window did not exist. Patch 3/3 doesn't
touch nfcsim.c, so the window remains at the end of the series.

Would it make sense to use a separate one-way teardown flag that
nfcsim_send() and nfcsim_recv_wq() check, rather than reusing dev->up?
Alternatively, the digital side could be quiesced before the link is
shut down. Should nfcsim_link_recv_skb() also wake on link->shutdown
so a late worker cannot sleep for the whole timeout?

[Severity: High]

This isn't a bug introduced by this patch, but port100_disconnect() in
drivers/nfc/port100.c seems to have the same kind of teardown ordering
problem. It never cancels dev->cmd_complete_work:

drivers/nfc/port100.c:port100_disconnect() {
    ...
	nfc_digital_unregister_device(dev->nfc_digital_dev);
	nfc_digital_free_device(dev->nfc_digital_dev);

	usb_kill_urb(dev->in_urb);
	usb_kill_urb(dev->out_urb);

	usb_free_urb(dev->in_urb);
	usb_free_urb(dev->out_urb);

	kfree(dev->cmd);
    ...
}

On unbind, usb_unbind_interface() calls usb_disable_interface() before
->disconnect, because port100 doesn't set soft_unbind. That kills in_urb.

The completion handler, port100_recv_response() or port100_recv_ack(), then
runs with -ESHUTDOWN or -ENOENT and does:

	schedule_work(&dev->cmd_complete_work);

port100_disconnect() then frees state in three places:

  - the drain loop in nfc_digital_unregister_device() frees every
    digital_cmd
  - nfc_digital_free_device() frees ddev
  - kfree(dev->cmd) frees the port100 command

If port100_wq_cmd_complete() runs after that, port100_send_async_complete()
reads dev->cmd. By then dev->cmd may already be freed, or it may end up
freed twice.

It then calls port100_in_comm_rf_complete()->digital_send_cmd_complete().
Can that write cmd->resp into a digital_cmd that the drain loop already
freed, and call schedule_work() on a freed ddev->cmd_complete_work?

struct port100 is devm-allocated. If the work is delayed past devm cleanup,
it would also touch a freed struct port100.

Should port100_disconnect() cancel or disable dev->cmd_complete_work before
freeing this state?

>  
>  	nfc_digital_free_device(dev->nfc_digital_dev);
>

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204060656.503-1-luob87709%40gmail.com
Technical Details
Message-ID: <179645387869.3146.7132650635892113846@kernel.org>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377861-linux-kernel-netdev=dewy.ru
References: <20261204060656.503-3-luob87709@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377861-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net v2 2/3] nfc: nfcsim: stop link and works before
 unregistering digital device
Date: Sat, 05 Dec 2026 06:57:58 +0000
Organization: Usenet Network
Message-ID: <179645387869.3146.7132650635892113846@kernel.org>
References: <20261204060656.503-3-luob87709@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2971948"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:hfKlCkX6Wui0F1JNzBJhyexUb2Q= sha256:DeCepJmrQLRpcH4NXifku+8J6Es3AFixvFoaBEo3CxQ=
	sha1:Oqohe9o2a0SkUcozbgLXn7tJjYE= sha256:72PeT8Bu5wUTln5yBOqhD4WrXjD6JFHTJHUzZ7CM5bQ=
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 D2C24117D6D; Sat,  5 Dec 2026 09:58:18 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.642
X-Spam-Status: No, score=-3.642 tagged_above=-9999 required=6.31
	tests=[DKIMWL_WL_HIGH=-0.142, 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 B55WS3b4jwr8;
	Sat,  5 Dec 2026 09:58:15 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-377861-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 31605117D8A; Sat,  5 Dec 2026 09:58:05 +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 8EDCF29C44; Sat,  5 Dec 2026 06:58:01 +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=1796453881;
	b=agSW8/Iy+SQ0YwF+99DHi5TOJ+JsJw8wE6KRQ193M3uzo/MqF/+PvZtg1UWRg+wdB2Xn
	 UxD4Pqhn/J+FgMxidnJoAOT1On9+yTECH7P8TwwwEF+nFo79dS01F/mQNM8Gyssqvb8C9
	 e9qNeUexLxaTKvEW0ksu0WEXh+Yc97W6J/VyG5BD57qyy/aC9PkogYgXI7OQObtvOo11j
	 D/QHGw+ettfmg3c+FGIPriCu+9PB1BTdBvCo3a1qBorsKf0uaIY9wySiXKgGNWsmRQlEK
	 WMmLf08fq41kJ8R/pTRqYCha13FwnqqrX3AYQaZb4eNVS7/EEUn2dg94ffiraWylWVw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796453881;
	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=vXeaKNkcEoSsD0qtFcdteoFRi33gkPHjqLZgBmQRdHg=;
	b=nK/k/nc5LVRXANrg0hhW0k9Ne6leC47+TVP1xZZ3XEN/DvvGTP7LcEdblcDuv0yTw4XS
	 7QSQbq9cMWs1H7S89NvzzeASw7QkDQHXj5jZSk6f70FFEtGZndlsl5r2PKqsPCsxIbkvp
	 tZfAkvGZU9vJgxN8/rXPv9UY5QNqbMmdzhGuYi89tMEslc8rbpS+0mRWaDQSKJwPOBUqg
	 knwaVb6tg5l6k/CYIBcCAeIf0YKKfYZMLv3TYIzgXCtzvgAXF88hu7ckjvg6HtfXcK79P
	 HhNj8eShUmh1Nj6EabXQAZYv1ALVaqjutCA30GogUxBcUUEeXnK9+UOBUt7as70gc4A==
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=OxICVJPX;
	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 126FF606A33D;
	Sat,  5 Dec 2026 06:58:01 +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=OxICVJPX
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=1796453880;
	b=L8xTOxvqTJqJfR++pcsHXB2kgFZ/YqawsbcNpI138008vthviE9zKCfZulgzKJ19QZUV
	 fNL2WitwQwuQuZCyfPDcvzpOD0o5rSHSNXfeMmuX4cS/ZaHMm72BOuGkQXUqsZY3EcaWC
	 3Srp1DClfCaZljKTi4jBTKuepHTmRG8UZpn9sd8Ftlj5cpUkKmnZM6PGJF3i9oDsqiyRd
	 qQ/dgOO16psjYDkv2LIC2VYIoLKWFt/B1PbVitIFDykaaS+T2HFnewnO/9AkYIp8Ai5Wo
	 5WSt+DZlLwEHL3PgUAjRdWpcZ68wZH4GG+f7b62Yqsun9oO9bMzUoMTqUE5N3wKD0vg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796453880;
	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=vXeaKNkcEoSsD0qtFcdteoFRi33gkPHjqLZgBmQRdHg=;
	b=BCoEG75aSEjz0dcMFJlh4N7e8sX04hWR85k2QpTMSLmLg3VdAvoXVk1MWPidsh5uVNX2
	 kz6kmo1cM3fYyFaPobEfxeIALmdPbwoKAX/nTGLimh5gEpO+nIVximR8EjBb8m17nbKmJ
	 yBqVnJavAqErHAD/N5fmwU7TQHXMUpxmHBrdc3w/xqdsJ5YuXTep7RpIdByi/u/jmldfv
	 4loI7bGpyYu53aMINJZojyrXb2cqxY5aVP6RssUrpgRC6JlqHlDqvJLlac4Q5ER1Mq4LE
	 pt07PViQiVS39IbiPYbpmd0EAL/+je/WepHCtpbpjBskZuVA2z9QAsLsa5hpA5OcsGA==
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=OxICVJPX;
	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 35A51606A333;
	Sat,  5 Dec 2026 06:57:59 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 276E11F00A10;
	Sat,  5 Dec 2026 06:57:59 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1796453879;
	bh=vXeaKNkcEoSsD0qtFcdteoFRi33gkPHjqLZgBmQRdHg=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=OxICVJPXykAUCvrj+H+aB6kmFii2sBrkujMouXsCdmrT2r+I0ypjW7vfRTIF416J+
	 FwY/uvMcxJDQ/jHbo3s09YZQVW07T2L+Ze5Z3D3EMxK6eBoi4PCQ4YRgQQZ5ZthI+9
	 ynIZJukGM24HtR0ekEg/PUoZ+Hp2JgPbYRPL5epl/UYjtGfHXgqrkeluDWRYuKDjV2
	 lq/04Jfw5usI9J5TeckYqWLD0KFEbepiEUlJjGc8ogCUuFBQehd5NepR5Uw6e4TGDM
	 UzuiJSEmI0kJbfrqt7dKwwJ4NZ4WVJsyrK7uLfwBYTDcxg6nRgpv1lJgkGhel6KNOf
	 2v6kBZAZ2uiGg==
X-Cc: david@ixit.cz, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
 pabeni@redhat.com, horms@kernel.org, krzk@kernel.org,
 oe-linux-nfc@lists.linux.dev, netdev@vger.kernel.org,
 linux-kernel@vger.kernel.org, stable@vger.kernel.org
X-In-Reply-To: <20261204060656.503-3-luob87709@gmail.com>
X-sashiko-severity: Medium
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