All of lore.kernel.org
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: b.lane130@outlook.com
Cc: mkl@pengutronix.de, mailhol@kernel.org,
	s.grosjean@peak-system.fr, wg@grandegger.com,
	linux-can@vger.kernel.org, kuba@kernel.org
Subject: Re: [PATCH] can: peak_pci: fix UAF and leak of PCIeC resources
Date: Tue, 24 Nov 2026 22:26:44 +0000	[thread overview]
Message-ID: <179555920456.3131.15451986872188013076@kernel.org> (raw)
In-Reply-To: <BN8PR06MB6177DC485B9CA4729F930DBDA4452@BN8PR06MB6177.namprd06.prod.outlook.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 1 (1) · Medium: 0 (1) · 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 adds a use-after-free in the failure_remove_channels
  loop of peak_pci_probe() (drivers/net/can/sja1000/peak_pci.c).

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch
  copies it.
- [Medium] This problem wasn't introduced by this patch, but
  peak_pciec_led_work() (drivers/net/can/sja1000/peak_pci.c:287) can…

--

can: peak_pci: fix UAF and leak of PCIeC resources

This commit adds a peak_pciec_remove() call to the failure_free_dev path
of peak_pci_probe(), so the PCIeC card is not leaked when the first
channel fails to register. It also moves the peak_pciec_remove() call in
failure_remove_channels into the channel loop, ahead of
unregister_sja1000dev() for the first channel, following the order used
by commit 949fe9b35570.

> diff --git a/drivers/net/can/sja1000/peak_pci.c b/drivers/net/can/sja1000/peak_pci.c
> index 69c61ccf621df..d5f862f05480a 100644
> --- a/drivers/net/can/sja1000/peak_pci.c
> +++ b/drivers/net/can/sja1000/peak_pci.c
> @@ -697,6 +697,10 @@ static int peak_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
>  	return 0;
>  
>  failure_free_dev:
> +	/* free any PCIeC resources too */
> +	if (!chan->prev_dev && chan->pciec_card)
> +		peak_pciec_remove(chan->pciec_card);
> +
>  	pci_set_drvdata(pdev, chan->prev_dev);
>  	free_sja1000dev(dev);

[Severity: Medium]
This is a pre-existing issue, not one introduced by this patch. On a
PCAN-ExpressCard, when this path runs for a channel i > 0, can
peak_pciec_led_work() read the net_device freed here?

peak_pciec_probe() adds each channel to the shared card, and nothing
ever clears that entry:

    chan->pciec_card = card;
    card->channel[card->chan_count++].netdev = dev;

peak_pciec_write_reg() arms led_work once channel 0 enters normal mode.
The work re-arms itself every HZ while any channel is up, and it reads
every entry below chan_count:

    netdev = card->channel[i].netdev;
    if (!netdev || !(netdev->flags & IFF_UP))
        continue;
    ...
    if (netdev->stats.rx_bytes != card->channel[i].prev_rx_bytes) {

Here, channel i is freed while card->channel[i] still points to it. The
failure_remove_channels loop then frees channels i-1..1. led_work is only
cancelled once the loop reaches peak_pciec_remove() for channel 0.

peak_pci_remove() has the same window. It unregisters and frees channels
N-1..1 before channel 0's peak_pciec_remove() calls
cancel_delayed_work_sync().

If can0 is up, can led_work run in that window and read the flags and
stats of a freed netdev?

>  
> @@ -709,15 +713,12 @@ static int peak_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
>  		priv = netdev_priv(dev);
>  		chan = priv->priv;
>  		prev_dev = chan->prev_dev;
> -
> +		if (!prev_dev && chan->pciec_card)
> +			peak_pciec_remove(chan->pciec_card);
>  		unregister_sja1000dev(dev);
>  		free_sja1000dev(dev);

[Severity: High]
Does this add a new use-after-free of the peak_pciec_card?

For the first channel, peak_pciec_remove() runs i2c_del_adapter() and
kfree(card), and then unregister_sja1000dev(dev) runs on that same
channel. Channel 0 is the one whose priv->write_reg was set to
peak_pciec_write_reg() in peak_pciec_probe(), so the call chain is:

peak_pci_probe()
  peak_pciec_remove(card)          /* kfree(card) */
  unregister_sja1000dev(dev0)
    set_reset_mode()
      priv->write_reg(priv, SJA1000_IER, IRQ_OFF)
        peak_pciec_write_reg()
          card = chan->pciec_card;
          c = (priv->reg_base - card->reg_base) / PEAK_PCI_CHAN_SIZE;

set_reset_mode() always does the IER write, so the freed card is read
every time this error path runs.

If the chip does not report MOD_RM (for example, can0 was brought up
after it registered), set_reset_mode() also writes SJA1000_MOD with
MOD_RM up to 100 times. Each write goes through peak_pciec_set_leds()
and peak_pciec_write_pca9553() to i2c_transfer() on card->led_chip,
which has already been deleted and freed. If can0 is up,
unregister_candev() then calls sja1000_close(), which runs
set_reset_mode() again.

can0 is also still registered between peak_pciec_remove() and
unregister_candev(). Could a concurrent "ip link set can0 up" write
MOD=0x00 here? That would reach peak_pciec_start_led_work() and call
schedule_delayed_work() on the freed card->led_work.

Before this patch, the only use-after-free on this path was the read of
chan->pciec_card from the freed netdev after the loop. Would it be better
to save the card pointer, stop led_work, unregister and free all the
netdevs, and only then call peak_pciec_remove() on the saved card?

[Severity: High]
This is a pre-existing issue, not one introduced by this patch. The new
loop copies the order used in peak_pci_remove(), and that function looks
like it has the same problem every time a PCAN-ExpressCard is removed:

drivers/net/can/sja1000/peak_pci.c:peak_pci_remove() {
    ...
		if (!prev_dev && chan->pciec_card)
			peak_pciec_remove(chan->pciec_card);
		unregister_sja1000dev(dev);
		free_sja1000dev(dev);
    ...
}

The card is freed before channel 0 is unregistered. set_reset_mode()
then reaches peak_pciec_write_reg() and reads card->reg_base from freed
memory.

On a normal rmmod or unbind with can0 up, the MOD_RM write also reaches
i2c_transfer() on the deleted and freed led_chip. sja1000_close(),
called from unregister_candev(), then does the same thing again.

The commit message cites 949fe9b35570 as the model for this order.
Should peak_pci_remove() get the same fix in this series?

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/BN8PR06MB6177DC485B9CA4729F930DBDA4452%40BN8PR06MB6177.namprd06.prod.outlook.com

      parent reply	other threads:[~2026-11-24 22:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  9:01 [PATCH] can: peak_pci: fix UAF and leak of PCIeC resources b.lane130
2026-11-23  9:09 ` sashiko-bot
2026-11-24 22:26 ` netdev-bot+sashiko [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=179555920456.3131.15451986872188013076@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=b.lane130@outlook.com \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=s.grosjean@peak-system.fr \
    --cc=wg@grandegger.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 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.