Skip to contentSkip to conversation

comedi: zero-init data in do_insn_ioctl

PATCH In review Arnaud Lecomte 2 participants 5 messages Jul 24, 2025 – Jul 25, 2025 Comedi drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 comedi: zero-init data in do_insn_ioctl

KMSAN reported a kernel-infoleak when copying instruction data back to userspace in do_insnlist_ioctl(). The issue occurs because allocated memory buffers weren't properly initialized (not
zero initialized) before being copied to
userspace, potentially leaking kernel memory.

---
 drivers/comedi/comedi_fops.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +1 -1
index c83fd14dd7ad..15fee829d14c 100644
@@ -1636,7 +1636,7 @@ static int do_insn_ioctl(struct comedi_device *dev,
 		n_data = MAX_SAMPLES;
 	}
 
-	data = kmalloc_array(n_data, sizeof(unsigned int), GFP_KERNEL);
+	data = kcalloc(n_data, sizeof(unsigned int), GFP_KERNEL);
 	if (!data) {
 		ret = -ENOMEM;
 		goto error;
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 19 lines · Arnaud Lecomte

KMSAN reported a kernel-infoleak when copying instruction data back to userspace in do_insnlist_ioctl(). The issue occurs because allocated memory buffers weren't properly initialized (not
zero initialized) before being copied to
userspace, potentially leaking kernel memory.

---
  drivers/comedi/comedi_fops.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +0 -0
index c83fd14dd7ad..15fee829d14c 100644

drivers/comedi/comedi_fops.c static int do_insn_ioctl(struct comedi_device *dev, · line 1642

Quoted 8 lines
drivers/comedi/comedi_fops.c +1 -1
@@ -1636,7 +1636,7 @@ static int do_insn_ioctl(struct comedi_device *dev,
  		n_data = MAX_SAMPLES;
  	}
 
-	data = kmalloc_array(n_data, sizeof(unsigned int), GFP_KERNEL);
+	data = kcalloc(n_data, sizeof(unsigned int), GFP_KERNEL);
  	if (!data) {
  		ret = -ENOMEM;
  		goto error;

I thought my commit 46d8c744136c ("comedi: Fix initialization of data for instructions that write to subdevice" would have fixed this as long as all the driver code was playing nicely, but it seems I was mistaken.

I don't think it is necessary to use kcalloc(). The buffer already gets initialized (partly by copy_from_user()) when insn->insn & INSN_MASK_WRITE is non-zero, so it just needs to be initialized when insn->insn & INSN_MASK_WRITE is zero too.

There is nearly identical code in do_insnlist_ioctl() that needs fixing, so it would be better to fix both at the same time.

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

Hi Ian,
Thanks for the feedback, I can send the revision of the patch by the end of the day if it is fine for you.

Quoted 35 lines · Ian Abbott
Quoted 26 lines · Arnaud Lecomte

KMSAN reported a kernel-infoleak when copying instruction data back to userspace in do_insnlist_ioctl(). The issue occurs because allocated memory buffers weren't properly initialized (not
zero initialized)  before being copied to
userspace, potentially leaking kernel memory.

---
  drivers/comedi/comedi_fops.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +1 -0
index c83fd14dd7ad..15fee829d14c 100644
@@ -1636,7 +1636,7 @@ static int do_insn_ioctl(struct comedi_device 
 *dev,
           n_data = MAX_SAMPLES;
       }
   -    data = kmalloc_array(n_data, sizeof(unsigned int), GFP_KERNEL);
+    data = kcalloc(n_data, sizeof(unsigned int), GFP_KERNEL);
       if (!data) {
           ret = -ENOMEM;
Quoted 1 line

          goto error;

I thought my commit 46d8c744136c ("comedi: Fix initialization of data for instructions that write to subdevice" would have fixed this as long as all the driver code was playing nicely, but it seems I was mistaken.

I don't think it is necessary to use kcalloc().  The buffer already gets initialized (partly by copy_from_user()) when insn->insn & INSN_MASK_WRITE is non-zero, so it just needs to be initialized when insn->insn & INSN_MASK_WRITE is zero too.

There is nearly identical code in do_insnlist_ioctl() that needs fixing, so it would be better to fix both at the same time.

maintainer
#3 lore raw
Quoted 2 lines · Arnaud

Hi Ian,
Thanks for the feedback, I can send the revision of the patch by the end of the day if it is fine for you.

Actually, I'm just building a (hopefully) fixed version at the moment. I'll add you to the Cc list for it.

Quoted 36 lines · Ian Abbott
Quoted 35 lines · Arnaud Lecomte
Quoted 26 lines

KMSAN reported a kernel-infoleak when copying instruction data back to userspace in do_insnlist_ioctl(). The issue occurs because allocated memory buffers weren't properly initialized (not
zero initialized)  before being copied to
userspace, potentially leaking kernel memory.

---
  drivers/comedi/comedi_fops.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/comedi/comedi_fops.c +1 -0
index c83fd14dd7ad..15fee829d14c 100644
@@ -1636,7 +1636,7 @@ static int do_insn_ioctl(struct comedi_device 
 *dev,
           n_data = MAX_SAMPLES;
       }
   -    data = kmalloc_array(n_data, sizeof(unsigned int), GFP_KERNEL);
+    data = kcalloc(n_data, sizeof(unsigned int), GFP_KERNEL);
       if (!data) {
           ret = -ENOMEM;
Quoted 1 line

          goto error;

I thought my commit 46d8c744136c ("comedi: Fix initialization of data for instructions that write to subdevice" would have fixed this as long as all the driver code was playing nicely, but it seems I was mistaken.

I don't think it is necessary to use kcalloc().  The buffer already gets initialized (partly by copy_from_user()) when insn->insn & INSN_MASK_WRITE is non-zero, so it just needs to be initialized when insn->insn & INSN_MASK_WRITE is zero too.

There is nearly identical code in do_insnlist_ioctl() that needs fixing, so it would be better to fix both at the same time.

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 )=-
#4 lore raw

Thanks Ian !
Keep me in touch on how it is going or if you need my help at any moment.

Quoted 2 lines · Ian Abbott

Actually, I'm just building a (hopefully) fixed version at the moment. I'll add you to the Cc list for it.