Linux CAN drivers development
 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
  0 siblings, 1 reply; 2+ 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] 2+ 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
  0 siblings, 0 replies; 2+ 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] 2+ messages in thread

end of thread, other threads:[~2026-11-23  9:09 UTC | newest]

Thread overview: 2+ 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox