Skip to contentSkip to conversation

can: sja1000: fix use after free in ems_pcmcia_add_card()

PATCH In review Dan Carpenter 2 participants 2 messages Nov 24, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 can: sja1000: fix use after free in ems_pcmcia_add_card()

If the last channel is not available then "dev" is freed. Fortunately, we can just use "pdev->irq" instead.

Also we should check if at least one channel was set up.

Fixes fd734c6f25ae ("can/sja1000: add driver for EMS PCMCIA card")

---
v3: add a check for if there is a channel.
v2: fix a bug in v1. Only one channel is required but my patch returned
if any channel was unavailable.

 drivers/net/can/sja1000/ems_pcmcia.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/net/can/sja1000/ems_pcmcia.c +6 -1
index e21b169c14c0..4642b6d4aaf7 100644
@@ -234,7 +234,12 @@ static int ems_pcmcia_add_card(struct pcmcia_device *pdev, unsigned long base)
 			free_sja1000dev(dev);
 	}
 
-	err = request_irq(dev->irq, &ems_pcmcia_interrupt, IRQF_SHARED,
+	if (!card->channels) {
+		err = -ENODEV;
+		goto failure_cleanup;
+	}
+
+	err = request_irq(pdev->irq, &ems_pcmcia_interrupt, IRQF_SHARED,
 			  DRV_NAME, card);
 	if (!err)
 		return 0;
Signature · 1 line
2.20.1
Acked-by Tested-by on v3 1/1
#1 lore raw
Quoted 7 lines · Dan Carpenter

If the last channel is not available then "dev" is freed. Fortunately, we can just use "pdev->irq" instead.

Also we should check if at least one channel was set up.

Fixes fd734c6f25ae ("can/sja1000: add driver for EMS PCMCIA card")

Thanks Dan!

Quoted 27 lines

---
v3: add a check for if there is a channel.
v2: fix a bug in v1. Only one channel is required but my patch returned
if any channel was unavailable.

  drivers/net/can/sja1000/ems_pcmcia.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
drivers/net/can/sja1000/ems_pcmcia.c +6 -1
index e21b169c14c0..4642b6d4aaf7 100644
@@ -234,7 +234,12 @@ static int ems_pcmcia_add_card(struct pcmcia_device *pdev, unsigned long base)
  			free_sja1000dev(dev);
  	}
 
-	err = request_irq(dev->irq, &ems_pcmcia_interrupt, IRQF_SHARED,
+	if (!card->channels) {
+		err = -ENODEV;
+		goto failure_cleanup;
+	}
+
+	err = request_irq(pdev->irq, &ems_pcmcia_interrupt, IRQF_SHARED,
  			  DRV_NAME, card);
  	if (!err)
  		return 0;