Skip to contentSkip to conversation

comedi: Add/fix sanity check in ISA drivers to avoid null-ptr-deref

PATCH New Ian Abbott 1 participant 12 messages Jun 18 Comedi drivers

lore
Share & tools
lore

Ian Abbott

author maintainer
#0 lore raw

As reported in [1], several comedi ISA drivers register their interrupt handler before initializing the subdevice the handler uses which could result in null pointer dereferences if the device raises an interrupt prematurely.

A common way to avoid that in the comedi drivers is to check the device is fully set up by testing the dev->attached flag (where dev is a pointer to the struct comedi_device that is the context for the interrupt handler), so add that test to some drivers that need it. Some drivers already do the test, but do it after a potential null pointer dereference, so the code needs reordering to avoid that.

The report mentions the aio_iiro_16, comedi_parport, das6402, ni_atmio16d, pcm711, and pcmmio drivers. I already sent a patch for comedi_parport about 3 weeks ago [2], but this patch series also patches the dt2811, ni_at_a2150, pcm816, pcm818, pcmuio, and quatech_daqp_cs drivers.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

1) comedi: aio_iiro_16: Add sanity check to interrupt handler
2) comedi: das6402: Add sanity check to interrupt handler
3) comedi: dt2811: Fix sanity check in interrupt handler
4) comedi: ni_at_a2150: Fix sanity check in interrupt handler
5) comedi: ni_atmio16d: Add sanity check to interrupt handler
6) comedi: pcm711: Fix sanity check in interrupt handler
7) comedi: pcm816: Fix sanity check in interrupt handler
8) comedi: pcm818: Fix sanity check in interrupt handler
9) comedi: pcmmio: Add sanity check to interrupt handler
10) comedi: pcmuio: Add sanity check to interrupt handler
11) comedi: quatech_daqp_cs: Fix sanity check in interrupt handler

 drivers/comedi/drivers/aio_iiro_16.c     |  3 +++
 drivers/comedi/drivers/das6402.c         | 10 ++++++++--
 drivers/comedi/drivers/dt2811.c          |  7 +++++--
 drivers/comedi/drivers/ni_at_a2150.c     | 13 +++++++++----
 drivers/comedi/drivers/ni_atmio16d.c     |  3 +++
 drivers/comedi/drivers/pcl711.c          |  3 ++-
 drivers/comedi/drivers/pcl816.c          |  3 ++-
 drivers/comedi/drivers/pcl818.c          |  3 ++-
 drivers/comedi/drivers/pcmmio.c          |  3 +++
 drivers/comedi/drivers/pcmuio.c          |  5 ++++-
 drivers/comedi/drivers/quatech_daqp_cs.c |  4 +++-
 11 files changed, 44 insertions(+), 13 deletions(-)
author maintainer
#1 lore raw

PATCH 1/11 comedi: aio_iiro_16: Add sanity check to interrupt handler

The driver requests an interrupt handler for the device, after setting device registers to disable interrupt generation. The interrupt handler should not be called prematurely unless the user-configured I/O port base address and/or IRQ number are incorrect or the hardware is bad.

For safety, check the dev->attached flag in the interrupt handler to ensure the device has been fully set up, avoiding a possible null pointer dereference of dev->read_subdev.

---
 drivers/comedi/drivers/aio_iiro_16.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/comedi/drivers/aio_iiro_16.c +3 -0
index d5d18fa2c638b..52385b14a9a40 100644
@@ -59,6 +59,9 @@ static irqreturn_t aio_iiro_16_cos(int irq, void *d)
 	unsigned int status;
 	unsigned int val;
 
+	if (!dev->attached)
+		return IRQ_NONE;
+
 	status = inb(dev->iobase + AIO_IIRO_16_STATUS);
 	if (!(status & AIO_IIRO_16_STATUS_IRQE))
 		return IRQ_NONE;
Signature · 1 line
2.53.0
author maintainer
#2 lore raw

PATCH 2/11 comedi: das6402: Add sanity check to interrupt handler

In reply to Ian Abbott

The driver requests an interrupt handler for the device, after setting device registers to disable interrupt generation. The interrupt handler should not be called prematurely unless the user-configured I/O port base address and/or IRQ number are incorrect or the hardware is bad.

For safety, check the dev->attached flag in the interrupt handler to ensure the device has been fully set up, avoiding a possible null pointer dereference of dev->read_subdev.

---
 drivers/comedi/drivers/das6402.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/comedi/drivers/das6402.c +8 -2
index 516a5d5a28404..384e9ba8d5df6 100644
@@ -173,10 +173,16 @@ static irqreturn_t das6402_interrupt(int irq, void *d)
 {
 	struct comedi_device *dev = d;
 	struct comedi_subdevice *s = dev->read_subdev;
-	struct comedi_async *async = s->async;
-	struct comedi_cmd *cmd = &async->cmd;
+	struct comedi_async *async;
+	struct comedi_cmd *cmd;
 	unsigned int status;
 
+	if (!dev->attached)
+		return IRQ_NONE;
+
+	async = s->async;
+	cmd = &async->cmd;
+
 	status = inb(dev->iobase + DAS6402_STATUS_REG);
 	if ((status & DAS6402_STATUS_INT) == 0)
 		return IRQ_NONE;
Signature · 1 line
2.53.0
author maintainer
#3 lore raw

PATCH 3/11 comedi: dt2811: Fix sanity check in interrupt handler

In reply to Ian Abbott

The driver requests an interrupt handler for the device, after setting device registers to disable interrupt generation. The interrupt handler should not be called prematurely unless the user-configured I/O port base address and/or IRQ number are incorrect or the hardware is bad.

For safety, the interrupt handler checks the dev->attached flag to ensure the device is fully set up, but it currently does that after dereferencing dev->read_subdev, which may be NULL if dev->attached is false. Move the check to avoid the possible null pointer dereference.

---
 drivers/comedi/drivers/dt2811.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/comedi/drivers/dt2811.c +5 -2
index bcc4b5ef48e8d..0438b8c90e441 100644
@@ -193,13 +193,16 @@ static irqreturn_t dt2811_interrupt(int irq, void *d)
 {
 	struct comedi_device *dev = d;
 	struct comedi_subdevice *s = dev->read_subdev;
-	struct comedi_async *async = s->async;
-	struct comedi_cmd *cmd = &async->cmd;
+	struct comedi_async *async;
+	struct comedi_cmd *cmd;
 	unsigned int status;
 
 	if (!dev->attached)
 		return IRQ_NONE;
 
+	async = s->async;
+	cmd = &async->cmd;
+
 	status = inb(dev->iobase + DT2811_ADCSR_REG);
 
 	if (status & DT2811_ADCSR_ADERROR) {
Signature · 1 line
2.53.0
author maintainer
#4 lore raw

PATCH 4/11 comedi: ni_at_a2150: Fix sanity check in interrupt handler

In reply to Ian Abbott

The driver requests an interrupt handler for the device before it is fully set up.

For safety, the interrupt handler checks the dev->attached flag to ensure the device is fully set up, but it currently does that after dereferencing various pointers which may be NULL if dev->attached is false. Move the check to avoid the possible null pointer dereferences.

---
 drivers/comedi/drivers/ni_at_a2150.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/comedi/drivers/ni_at_a2150.c +9 -4
index 44221c928e327..86629b495fedc 100644
@@ -132,11 +132,11 @@ static irqreturn_t a2150_interrupt(int irq, void *d)
 	struct comedi_device *dev = d;
 	struct a2150_private *devpriv = dev->private;
 	struct comedi_isadma *dma = devpriv->dma;
-	struct comedi_isadma_desc *desc = &dma->desc[0];
+	struct comedi_isadma_desc *desc;
 	struct comedi_subdevice *s = dev->read_subdev;
-	struct comedi_async *async = s->async;
-	struct comedi_cmd *cmd = &async->cmd;
-	unsigned short *buf = desc->virt_addr;
+	struct comedi_async *async;
+	struct comedi_cmd *cmd;
+	unsigned short *buf;
 	unsigned int max_points, num_points, residue, leftover;
 	unsigned short dpnt;
 	int status;
@@ -145,6 +145,11 @@ static irqreturn_t a2150_interrupt(int irq, void *d)
 	if (!dev->attached)
 		return IRQ_HANDLED;
 
+	desc = &dma->desc[0];
+	async = s->async;
+	cmd = &async->cmd;
+	buf = desc->virt_addr;
+
 	status = inw(dev->iobase + STATUS_REG);
 	if ((status & INTR_BIT) == 0)
 		return IRQ_NONE;
Signature · 1 line
2.53.0
author maintainer
#5 lore raw

PATCH 5/11 comedi: ni_atmio16d: Add sanity check to interrupt handler

In reply to Ian Abbott

The driver requests an interrupt handler for the device, after setting device registers to disable interrupt generation. The interrupt handler should not be called prematurely unless the user-configured I/O port base address and/or IRQ number are incorrect or the hardware is bad.

For safety, check the dev->attached flag in the interrupt handler to ensure the device has been fully set up, avoiding a possible null pointer dereference of dev->read_subdev.

---
 drivers/comedi/drivers/ni_atmio16d.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/comedi/drivers/ni_atmio16d.c +3 -0
index b2772d9098960..6765cdc276cad 100644
@@ -223,6 +223,9 @@ static irqreturn_t atmio16d_interrupt(int irq, void *d)
 	struct comedi_subdevice *s = dev->read_subdev;
 	unsigned short val;
 
+	if (!dev->attached)
+		return IRQ_NONE;
+
 	val = inw(dev->iobase + AD_FIFO_REG);
 	comedi_buf_write_samples(s, &val, 1);
 	comedi_handle_events(dev, s);
Signature · 1 line
2.53.0
author maintainer
#6 lore raw

PATCH 6/11 comedi: pcm711: Fix sanity check in interrupt handler

In reply to Ian Abbott

The driver requests an interrupt handler for the device before it is fully set up.

For safety, the interrupt handler checks the dev->attached flag to ensure the device is fully set up, but it currently does that after dereferencing the dev->read_dev pointer which may be NULL if dev->attached is false. Move the check to avoid the possible null pointer dereference.

---
 drivers/comedi/drivers/pcl711.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/comedi/drivers/pcl711.c +2 -1
index 5d2c4b2aa3bb6..8ad5789966f02 100644
@@ -184,7 +184,7 @@ static irqreturn_t pcl711_interrupt(int irq, void *d)
 {
 	struct comedi_device *dev = d;
 	struct comedi_subdevice *s = dev->read_subdev;
-	struct comedi_cmd *cmd = &s->async->cmd;
+	struct comedi_cmd *cmd;
 	unsigned short data;
 
 	if (!dev->attached) {
@@ -192,6 +192,7 @@ static irqreturn_t pcl711_interrupt(int irq, void *d)
 		return IRQ_HANDLED;
 	}
 
+	cmd = &s->async->cmd;
 	data = pcl711_ai_get_sample(dev, s);
 
 	outb(PCL711_INT_STAT_CLR, dev->iobase + PCL711_INT_STAT_REG);
Signature · 1 line
2.53.0
author maintainer
#7 lore raw

PATCH 7/11 comedi: pcm816: Fix sanity check in interrupt handler

In reply to Ian Abbott

The driver requests an interrupt handler for the device before it is fully set up.

For safety, the interrupt handler checks the dev->attached flag to ensure the device is fully set up, but it currently does that after dereferencing the devpriv->dma pointer which may be NULL if dev->attached is false. Move the dereference of the devpriv->dma pointer after dev->attached has been checked to avoid the possible null pointer dereference.

---
 drivers/comedi/drivers/pcl816.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/comedi/drivers/pcl816.c +2 -1
index 1fcb2f798c7a5..b0d30cb1cfe9e 100644
@@ -242,7 +242,7 @@ static irqreturn_t pcl816_interrupt(int irq, void *d)
 	struct comedi_subdevice *s = dev->read_subdev;
 	struct pcl816_private *devpriv = dev->private;
 	struct comedi_isadma *dma = devpriv->dma;
-	struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma];
+	struct comedi_isadma_desc *desc;
 	unsigned int nsamples;
 	unsigned int bufptr;
 
@@ -257,6 +257,7 @@ static irqreturn_t pcl816_interrupt(int irq, void *d)
 		return IRQ_HANDLED;
 	}
 
+	desc = &dma->desc[dma->cur_dma];
 	nsamples = comedi_bytes_to_samples(s, desc->size) -
 		   devpriv->ai_poll_ptr;
 	bufptr = devpriv->ai_poll_ptr;
Signature · 1 line
2.53.0
author maintainer
#8 lore raw

PATCH 8/11 comedi: pcm818: Fix sanity check in interrupt handler

In reply to Ian Abbott

The driver requests an interrupt handler for the device before it is fully set up.

For safety, the interrupt handler checks the dev->attached flag to ensure the device is fully set up, but it currently does that after dereferencing the dev->read_dev pointer which may be NULL if dev->attached is false. Move the check to avoid the possible null pointer dereference.

---
 drivers/comedi/drivers/pcl818.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/comedi/drivers/pcl818.c +2 -1
index aa775a024fc7f..89f32c493caac 100644
@@ -534,13 +534,14 @@ static irqreturn_t pcl818_interrupt(int irq, void *d)
 	struct comedi_device *dev = d;
 	struct pcl818_private *devpriv = dev->private;
 	struct comedi_subdevice *s = dev->read_subdev;
-	struct comedi_cmd *cmd = &s->async->cmd;
+	struct comedi_cmd *cmd;
 
 	if (!dev->attached || !devpriv->ai_cmd_running) {
 		pcl818_ai_clear_eoc(dev);
 		return IRQ_HANDLED;
 	}
 
+	cmd = &s->async->cmd;
 	if (devpriv->ai_cmd_canceled) {
 		/*
 		 * The cleanup from ai_cancel() has been delayed
Signature · 1 line
2.53.0
author maintainer
#9 lore raw

PATCH 9/11 comedi: pcmmio: Add sanity check to interrupt handler

In reply to Ian Abbott

The driver requests an interrupt handler for the device, after setting device registers to disable interrupt generation. The interrupt handler should not be called prematurely unless the user-configured I/O port base address and/or IRQ number are incorrect or the hardware is bad.

For safety, check the dev->attached flag in the interrupt handler to ensure the device has been fully set up, avoiding a possible null pointer dereference of dev->read_subdev.

---
 drivers/comedi/drivers/pcmmio.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/comedi/drivers/pcmmio.c +3 -0
index d38202c8a12b8..f42b7343b4e45 100644
@@ -362,6 +362,9 @@ static irqreturn_t interrupt_pcmmio(int irq, void *d)
 	unsigned int triggered;
 	unsigned char int_pend;
 
+	if (!dev->attached)
+		return IRQ_NONE;
+
 	/* are there any interrupts pending */
 	int_pend = inb(dev->iobase + PCMMIO_INT_PENDING_REG) & 0x07;
 	if (!int_pend)
Signature · 1 line
2.53.0
author maintainer
#10 lore raw

PATCH 10/11 comedi: pcmuio: Add sanity check to interrupt handler

In reply to Ian Abbott

The driver requests an interrupt handler for the device, after setting device registers to disable interrupt generation. The interrupt handler should not be called prematurely unless the user-configured I/O port base address and/or IRQ number are incorrect or the hardware is bad.

For safety, check the dev->attached flag in the interrupt handler pcmuio_interrupt() to ensure the device has been fully set up, avoiding a possible null pointer dereference of dev->subdevices by pcmuio_handle_asic_interrupt().

Also make use of the IRQ_HANDLED(x) macro for the normal return path of the interrupt handler.

---
 drivers/comedi/drivers/pcmuio.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/comedi/drivers/pcmuio.c +4 -1
index 0995911a3ea30..d9995cbeecb62 100644
@@ -362,12 +362,15 @@ static irqreturn_t pcmuio_interrupt(int irq, void *d)
 	struct pcmuio_private *devpriv = dev->private;
 	int handled = 0;
 
+	if (!dev->attached)
+		return IRQ_NONE;
+
 	if (irq == dev->irq)
 		handled += pcmuio_handle_asic_interrupt(dev, 0);
 	if (irq == devpriv->irq2)
 		handled += pcmuio_handle_asic_interrupt(dev, 1);
 
-	return handled ? IRQ_HANDLED : IRQ_NONE;
+	return IRQ_RETVAL(handled);
 }
 
 /* chip->spinlock is already locked */
Signature · 1 line
2.53.0
author maintainer
#11 lore raw

PATCH 11/11 comedi: quatech_daqp_cs: Fix sanity check in interrupt handler

In reply to Ian Abbott

The driver requests an interrupt handler for the device before it is fully set up.

For safety, the interrupt handler checks the dev->attached flag to ensure the device is fully set up, but it currently does that after dereferencing the dev->read_dev pointer which may be NULL if dev->attached is false. Move the check to avoid the possible null pointer dereference.

---
 drivers/comedi/drivers/quatech_daqp_cs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/comedi/drivers/quatech_daqp_cs.c +3 -1
index 2a76c75c513b8..d5e3e213e233b 100644
@@ -211,13 +211,15 @@ static irqreturn_t daqp_interrupt(int irq, void *dev_id)
 {
 	struct comedi_device *dev = dev_id;
 	struct comedi_subdevice *s = dev->read_subdev;
-	struct comedi_cmd *cmd = &s->async->cmd;
+	struct comedi_cmd *cmd;
 	int loop_limit = 10000;
 	int status;
 
 	if (!dev->attached)
 		return IRQ_NONE;
 
+	cmd = &s->async->cmd;
+
 	status = inb(dev->iobase + DAQP_STATUS_REG);
 	if (!(status & DAQP_STATUS_EVENTS))
 		return IRQ_NONE;
Signature · 1 line
2.53.0