Skip to contentSkip to conversation

comedi: addi_apci_3xxx: fix subdevice count underallocation

PATCH In review Yang Zi 3 participants 5 messages Aug 25 – Oct 14 Comedi drivers

lore
Share & tools
lore

Yang Zi

author
#0 lore raw

PATCH 1/1 comedi: addi_apci_3xxx: fix subdevice count underallocation

In apci3xxx_auto_attach(), the number of subdevices is computed with

    n_subdevices = (board->ai_n_chan ? 0 : 1) + board->has_ao + ...

The ternary is inverted: when the board has analog input channels (ai_n_chan != 0), it counts 0 instead of 1, so the AI subdevice that is actually created is not accounted for. As a result comedi_alloc_subdevices() allocates one entry too few, and the later subdevice initialization (notably the TTL Digital I/O subdevice, which is populated last) writes past the end of the allocated array, causing a slab out-of-bounds write.

Flip the ternary to count 1 when ai_n_chan is non-zero and 0 otherwise, matching the subdevice creation order in the function.

---

drivers/comedi/drivers/addi_apci_3xxx.c +0 -0
index 695cce103177..1dcda82d9503 100644
@@ -787,7 +787,7 @@ static int apci3xxx_auto_attach(struct comedi_device *dev,

             dev->irq = pcidev->irq;
     }

-    n_subdevices = (board->ai_n_chan ? 0 : 1) + board->has_ao + +    n_subdevices = (board->ai_n_chan ? 1 : 0) + board->has_ao + board->has_dig_in + board->has_dig_out + board->has_ttl_io;
     ret = comedi_alloc_subdevices(dev, n_subdevices);

maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 22 lines · Yang Zi

In apci3xxx_auto_attach(), the number of subdevices is computed with

    n_subdevices = (board->ai_n_chan ? 0 : 1) + board->has_ao + ...

The ternary is inverted: when the board has analog input channels (ai_n_chan != 0), it counts 0 instead of 1, so the AI subdevice that is actually created is not accounted for. As a result comedi_alloc_subdevices() allocates one entry too few, and the later subdevice initialization (notably the TTL Digital I/O subdevice, which is populated last) writes past the end of the allocated array, causing a slab out-of-bounds write.

Flip the ternary to count 1 when ai_n_chan is non-zero and 0 otherwise, matching the subdevice creation order in the function.

---

drivers/comedi/drivers/addi_apci_3xxx.c +0 -0
index 695cce103177..1dcda82d9503 100644

drivers/comedi/drivers/addi_apci_3xxx.c static int apci3xxx_auto_attach(struct comedi_device *dev, · line 793

Quoted 8 lines
drivers/comedi/drivers/addi_apci_3xxx.c +1 -1
@@ -787,7 +787,7 @@ static int apci3xxx_auto_attach(struct comedi_device *dev,
              dev->irq = pcidev->irq;
      }
 
-    n_subdevices = (board->ai_n_chan ? 0 : 1) + board->has_ao +
+    n_subdevices = (board->ai_n_chan ? 1 : 0) + board->has_ao +
                 board->has_dig_in + board->has_dig_out +
                 board->has_ttl_io;
      ret = comedi_alloc_subdevices(dev, n_subdevices);

Thanks for catching this bug. Looks good!

Signature · 4 lines
-=( Ian Abbott <[email protected]> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-
#2 lore raw
In reply to Yang Zi
Quoted 24 lines · Yang Zi

In apci3xxx_auto_attach(), the number of subdevices is computed with

    n_subdevices = (board->ai_n_chan ? 0 : 1) + board->has_ao + ...

The ternary is inverted: when the board has analog input channels (ai_n_chan != 0), it counts 0 instead of 1, so the AI subdevice that is actually created is not accounted for. As a result comedi_alloc_subdevices() allocates one entry too few, and the later subdevice initialization (notably the TTL Digital I/O subdevice, which is populated last) writes past the end of the allocated array, causing a slab out-of-bounds write.

Flip the ternary to count 1 when ai_n_chan is non-zero and 0 otherwise, matching the subdevice creation order in the function.

---

drivers/comedi/drivers/addi_apci_3xxx.c +0 -0
index 695cce103177..1dcda82d9503 100644
@@ -787,7 +787,7 @@ static int apci3xxx_auto_attach(struct comedi_device *dev,
              dev->irq = pcidev->irq;
      }
Quoted 8 lines


-    n_subdevices = (board->ai_n_chan ? 0 : 1) + board->has_ao + +    n_subdevices = (board->ai_n_chan ? 1 : 0) + board->has_ao + board->has_dig_in + board->has_dig_out + board->has_ttl_io;
     ret = comedi_alloc_subdevices(dev, n_subdevices);

This is corrupted and can not be applied :(

maintainer
#3 lore raw

PATCH v2 1/1 comedi: addi_apci_3xxx: fix subdevice count underallocation

In reply to Yang Zi

From: Yang Zi <[email protected]>

In apci3xxx_auto_attach(), the number of subdevices is computed with

    n_subdevices = (board->ai_n_chan ? 0 : 1) + board->has_ao + ...

The ternary is inverted: when the board has analog input channels (ai_n_chan != 0), it counts 0 instead of 1, so the AI subdevice that is actually created is not accounted for. As a result comedi_alloc_subdevices() allocates one entry too few, and the later subdevice initialization (notably the TTL Digital I/O subdevice, which is populated last) writes past the end of the allocated array, causing a slab out-of-bounds write.

Flip the ternary to count 1 when ai_n_chan is non-zero and 0 otherwise, matching the subdevice creation order in the function.

---
 drivers/comedi/drivers/addi_apci_3xxx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

---
v2: Decorrupted patch in original email and appended my "Reviewed-by" line. -- Ian Abbott

drivers/comedi/drivers/addi_apci_3xxx.c +1 -1
index 695cce1031779..1dcda82d95032 100644
@@ -787,7 +787,7 @@ static int apci3xxx_auto_attach(struct comedi_device *dev,
 			dev->irq = pcidev->irq;
 	}
 
-	n_subdevices = (board->ai_n_chan ? 0 : 1) + board->has_ao +
+	n_subdevices = (board->ai_n_chan ? 1 : 0) + board->has_ao +
 		       board->has_dig_in + board->has_dig_out +
 		       board->has_ttl_io;
 	ret = comedi_alloc_subdevices(dev, n_subdevices);
Signature · 1 line
2.53.0
maintainer Reviewed-by on v2 1/1
#4 lore raw
Quoted 17 lines · Ian Abbott

From: Yang Zi <[email protected]>

In apci3xxx_auto_attach(), the number of subdevices is computed with

    n_subdevices = (board->ai_n_chan ? 0 : 1) + board->has_ao + ...

The ternary is inverted: when the board has analog input channels (ai_n_chan != 0), it counts 0 instead of 1, so the AI subdevice that is actually created is not accounted for. As a result comedi_alloc_subdevices() allocates one entry too few, and the later subdevice initialization (notably the TTL Digital I/O subdevice, which is populated last) writes past the end of the allocated array, causing a slab out-of-bounds write.

Flip the ternary to count 1 when ai_n_chan is non-zero and 0 otherwise, matching the subdevice creation order in the function.

Quoted 7 lines
---
  drivers/comedi/drivers/addi_apci_3xxx.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

---
v2: Decorrupted patch in original email and appended my "Reviewed-by" line. -- Ian Abbott

Except that I forgot to add my "Reviewed-by" line!

Quoted 6 lines
drivers/comedi/drivers/addi_apci_3xxx.c +0 -0
index 695cce1031779..1dcda82d95032 100644

drivers/comedi/drivers/addi_apci_3xxx.c static int apci3xxx_auto_attach(struct comedi_device *dev, · line 793

Quoted 8 lines
drivers/comedi/drivers/addi_apci_3xxx.c +1 -1
@@ -787,7 +787,7 @@ static int apci3xxx_auto_attach(struct comedi_device *dev,
  			dev->irq = pcidev->irq;
  	}
 
-	n_subdevices = (board->ai_n_chan ? 0 : 1) + board->has_ao +
+	n_subdevices = (board->ai_n_chan ? 1 : 0) + board->has_ao +
  		       board->has_dig_in + board->has_dig_out +
  		       board->has_ttl_io;
  	ret = comedi_alloc_subdevices(dev, n_subdevices);
Signature · 4 lines
-=( Ian Abbott <[email protected]> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-