Thread View: linux.kernel.netdev
4 messages
4 total messages
Started by Tristan Madani
Sun, 25 Oct 2026 21:02
[PATCH v2 net] can: peak_usb: fix use-after-free on PCAN-USB disconnect and init failure
Author: Tristan Madani
Date: Sun, 25 Oct 2026 21:02
Date: Sun, 25 Oct 2026 21:02
62 lines
13081 bytes
13081 bytes
The PCAN-USB adapter uses a restart_timer for bus error recovery
but does not define a dev_free callback. When the device is
unplugged, peak_usb_disconnect() frees the candev without cancelling
the timer. If a bus error armed the timer and USB disconnect races
with the timer callback, the timer fires on freed memory.
The same timer is initialized in pcan_usb_init() via timer_setup(),
but on error (e.g. failed USB command to read the serial number) the
function returns without cancelling it. Because dev_init failure in
peak_usb_create_dev() jumps to lbl_unregister_candev and skips the
adap_dev_free label, the dev_free callback is never invoked either.
Add a pcan_usb_free() callback that calls timer_shutdown_sync() on
the restart_timer, register it as the adapter dev_free handler, and
also cancel the timer directly in pcan_usb_init() on error before
returning.
Fixes: 46be265d3388 ("can: usb: PEAK-System Technik PCAN-USB specific part")
Cc: stable@vger.kernel.org
Signed-off-by: Tristan Madani <tristan@talencesecurity.com>
---
drivers/net/can/usb/peak_usb/pcan_usb.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c
index 8fd058c328564e..440d8e2cfb6fe6 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb.c
@@ -882,6 +882,14 @@ static int pcan_usb_start(struct peak_usb_device *dev)
return pcan_usb_set_ext_vcc(dev, 0);
}
+static void pcan_usb_free(struct peak_usb_device *dev)
+{
+ struct pcan_usb *pdev =
+ container_of(dev, struct pcan_usb, dev);
+
+ timer_shutdown_sync(&pdev->restart_timer);
+}
+
static int pcan_usb_init(struct peak_usb_device *dev)
{
struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
@@ -901,6 +909,7 @@ static int pcan_usb_init(struct peak_usb_device *dev)
dev_err(dev->netdev->dev.parent,
"unable to read %s serial number (err %d)\n",
pcan_usb.name, err);
+ timer_shutdown_sync(&pdev->restart_timer);
return err;
}
@@ -1051,6 +1060,7 @@ const struct peak_usb_adapter pcan_usb = {
.dev_decode_buf = pcan_usb_decode_buf,
.dev_encode_msg = pcan_usb_encode_msg,
.dev_start = pcan_usb_start,
+ .dev_free = pcan_usb_free,
.dev_restart_async = pcan_usb_restart_async,
.do_get_berr_counter = pcan_usb_get_berr_counter,
};
--
2.47.3
Re: [PATCH v2 net] can: peak_usb: fix use-after-free on PCAN-USB disconnect and init failure
Author: netdev-bot+sinfo
Date: Sun, 25 Oct 2026 21:05
Date: Sun, 25 Oct 2026 21:05
22 lines
8638 bytes
8638 bytes
Hi! This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information: - How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan. - Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages. Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then. The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Re: [PATCH v2 net] can: peak_usb: fix use-after-free on PCAN-USB disconnect and init failure
Author: Tristan Madani
Date: Mon, 26 Oct 2026 13:51
Date: Mon, 26 Oct 2026 13:51
10 lines
11455 bytes
11455 bytes
The issue was found by manual code inspection of USB CAN driver disconnect and error handling paths, specifically looking for missing timer/work cancellation before device structure teardown. The bug has not been triggered in practice. The race window is between a bus error arming restart_timer in pcan_usb_restart_async() and a concurrent USB disconnect calling peak_usb_disconnect() which frees the candev. The PCAN-USB adapter does not register a dev_free callback, so the timer is never cancelled on the disconnect path.
Re: [PATCH v2 net] can: peak_usb: fix use-after-free on PCAN-USB disconnect and init failure
Author: netdev-bot+sashiko
Date: Mon, 26 Oct 2026 21:03
Date: Mon, 26 Oct 2026 21:03
113 lines
11808 bytes
11808 bytes
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 ยท High: 1 ยท Medium: 0 ยท 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).
- [High] The patch does not fully fix the init-failure use-after-free it
claims to fix.
--
can: peak_usb: fix use-after-free on PCAN-USB disconnect and init failure
This patch adds a pcan_usb_free() dev_free callback that shuts down the
restart_timer with timer_shutdown_sync(). It also shuts the timer down
directly in pcan_usb_init() when reading the serial number fails. The goal
is to stop the timer firing on freed memory after disconnect or after a
failed init.
> diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c
> index 8fd058c328564..440d8e2cfb6fe 100644
> --- a/drivers/net/can/usb/peak_usb/pcan_usb.c
> +++ b/drivers/net/can/usb/peak_usb/pcan_usb.c
[ ... ]
> @@ -901,6 +909,7 @@ static int pcan_usb_init(struct peak_usb_device *dev)
> dev_err(dev->netdev->dev.parent,
> "unable to read %s serial number (err %d)\n",
> pcan_usb.name, err);
> + timer_shutdown_sync(&pdev->restart_timer);
> return err;
> }
[Severity: High]
Does this fully close the init-failure use-after-free? An asynchronous
restart URB can still re-arm this timer after pdev has been freed.
In peak_usb_create_dev(), register_candev() is called before dev_init():
drivers/net/can/usb/peak_usb/pcan_usb_core.c:peak_usb_create_dev() {
...
err = register_candev(netdev);
...
if (dev->adapter->dev_init) {
err = dev->adapter->dev_init(dev);
if (err)
goto lbl_unregister_candev;
}
...
}
So the netdev can be brought up while pcan_usb_init() is blocked in
pcan_usb_get_serial(). If a bus-off restart happens in that window
(restart-ms or CAN_MODE_START), this path runs:
peak_usb_set_mode()->peak_usb_restart()->pcan_usb_restart_async()
and pcan_usb_restart_async() submits a URB with pdev as its context:
usb_fill_bulk_urb(urb, dev->udev,
usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
buf, PCAN_USB_CMD_LEN,
pcan_usb_restart_pending, pdev);
return usb_submit_urb(urb, GFP_ATOMIC);
This URB is never anchored. Only rx_submitted and tx_submitted are
anchored, so peak_usb_unlink_all_urbs() neither kills it nor waits for it.
If the serial number command then fails, the timer_shutdown_sync() added
here only waits for the timer callback. The timer is not armed yet,
because the URB has not completed.
The unwind then goes through unregister_candev() and free_candev(). The
probe failure path in usb_probe_interface() does not flush the interface
endpoints either.
When the URB completes later, from a late device ACK or from an unplug
that flushes the endpoint, can pcan_usb_restart_pending() touch the
freed pdev?
static void pcan_usb_restart_pending(struct urb *urb)
{
struct pcan_usb *pdev = urb->context;
/* the PCAN-USB needs time to restart */
mod_timer(&pdev->restart_timer,
jiffies + msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
...
}
Once the memory has been reused, the shutdown marker on the timer is gone.
That means mod_timer() could enqueue a timer that lives in freed storage.
The disconnect path looks fine, since usb_unbind_interface() flushes the
interface URBs before calling ->disconnect. The init-failure path the
commit message names does not look closed, though.
Would it work to delay register_candev() until dev_init() succeeds? The
other option is to anchor the restart URB and drain it before shutting
down the timer and freeing the candev.
[ ... ]
--
Sashiko AI review ยท https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261025210256.629830-1-tristan%40talencesecurity.com
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads