All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] can: peak_pci: fix UAF and leak of PCIeC resources
@ 2026-11-23  9:01 b.lane130
  2026-11-23  9:09 ` sashiko-bot
  2026-11-24 22:26 ` netdev-bot+sashiko
  0 siblings, 2 replies; 3+ messages in thread
From: b.lane130 @ 2026-11-23  9:01 UTC (permalink / raw)
  To: mkl, mailhol, s.grosjean, wg; +Cc: linux-can, Bruce Lane

From: Bruce Lane <b.lane130@outlook.com>

If register_sja1000dev() fails on the first channel in peak_pci_probe(),
failure_free_dev clears drvdata and frees dev without calling
peak_pciec_remove(), leaking the PCIeC card and leaving the I2C adapter
registered.

If probing fails on a later channel, failure_remove_channels frees all
devices in the loop and then dereferences chan->pciec_card afterwards,
causing a use-after-free. This is the same issue in commit 949fe9b35570
("can: peak_pci: peak_pci_remove(): fix UAF").

Call peak_pciec_remove() under failure_free_dev, and change the cleanup
order under failure_remove_channels.

Fixes: e6d9c80b7ca1 ("can: peak_pci: add support of some new PEAK-System PCI cards")
Signed-off-by: Bruce Lane <b.lane130@outlook.com>
---
 drivers/net/can/sja1000/peak_pci.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/net/can/sja1000/peak_pci.c b/drivers/net/can/sja1000/peak_pci.c
index 69c61ccf621d..d5f862f05480 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);
 
@@ -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);
 	}
 
-	/* free any PCIeC resources too */
-	if (chan && chan->pciec_card)
-		peak_pciec_remove(chan->pciec_card);
-
 	pci_iounmap(pdev, reg_base);
 
 failure_unmap_cfg_base:
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] can: peak_pci: fix UAF and leak of PCIeC resources
  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
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-23  9:09 UTC (permalink / raw)
  To: b.lane130
  Cc: linux-can, Marc Kleine-Budde, Oliver Hartkopp, Vincent Mailhol,
	Oleksij Rempel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] can: peak_pci: UAF of chan->pciec_card in peak_pci_probe()
  rollback
--

commit 966ed02e76bd87e25cf2422b16a91f40918e28bf
Author: Bruce Lane <b.lane130@outlook.com>

can: peak_pci: fix UAF and leak of PCIeC resources

This commit adds peak_pciec_remove() to the failure_free_dev error path
in peak_pci_probe() and moves the peak_pciec_remove() call in
failure_remove_channels inside the channel teardown loop.

> 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

[ ... ]

> @@ -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 calling peak_pciec_remove() before unregister_sja1000dev() cause a
use-after-free of chan->pciec_card when unwinding channel 0?

peak_pciec_remove() frees the struct peak_pciec_card pointed to by
chan->pciec_card, and unregister_sja1000dev() then calls
set_reset_mode() -> priv->write_reg().  On PCAN-ExpressCard devices,
priv->write_reg is peak_pciec_write_reg(), which dereferences
chan->pciec_card:

drivers/net/can/sja1000/peak_pci.c:peak_pciec_write_reg() {
	struct peak_pci_chan *chan = priv->priv;
	struct peak_pciec_card *card = chan->pciec_card;
	int c = (priv->reg_base - card->reg_base) / PEAK_PCI_CHAN_SIZE;
	...
}

Calling unregister_sja1000dev(dev) before peak_pciec_remove() and
free_sja1000dev(dev) afterwards avoids freeing chan->pciec_card while
unregister_sja1000dev() still needs it.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/BN8PR06MB6177DC485B9CA4729F930DBDA4452@BN8PR06MB6177.namprd06.prod.outlook.com?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] can: peak_pci: fix UAF and leak of PCIeC resources
  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
  1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 22:26 UTC (permalink / raw)
  To: b.lane130; +Cc: mkl, mailhol, s.grosjean, wg, linux-can, kuba

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-24 22:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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.