Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

comedi: Fail COMEDI_INSNLIST ioctl if n_insns is too large

The handling of the `COMEDI_INSNLIST` ioctl allocates a kernel buffer to
hold the array of `struct comedi_insn`, getting the length from the
`n_insns` member of the `struct comedi_insnlist` supplied by the user.
The allocation will fail with a WARNING and a stack dump if it is too
large.

Avoid that by failing with an `-EINVAL` error if the supplied `n_insns`
value is unreasonable.

Define the limit on the `n_insns` value in the `MAX_INSNS` macro. Set
this to the same value as `MAX_SAMPLES` (65536), which is the maximum
allowed sum of the values of the member `n` in the array of `struct
comedi_insn`, and sensible comedi instructions will have an `n` of at
least 1.

Reported-by: syzbot+d6995b62e5ac7d79557a@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=d6995b62e5ac7d79557a
Fixes: ed9eccbe8970 ("Staging: add comedi core")
Tested-by: Ian Abbott <abbotti@mev.co.uk>
Cc: stable@vger.kernel.org # 5.13+
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Link: https://lore.kernel.org/r/20250704120405.83028-1-abbotti@mev.co.uk
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Ian Abbott and committed by
Greg Kroah-Hartman
08ae4b20 2aa4ad62

+16
+16
drivers/comedi/comedi_fops.c
··· 1589 1589 return i; 1590 1590 } 1591 1591 1592 + #define MAX_INSNS MAX_SAMPLES 1593 + static int check_insnlist_len(struct comedi_device *dev, unsigned int n_insns) 1594 + { 1595 + if (n_insns > MAX_INSNS) { 1596 + dev_dbg(dev->class_dev, "insnlist length too large\n"); 1597 + return -EINVAL; 1598 + } 1599 + return 0; 1600 + } 1601 + 1592 1602 /* 1593 1603 * COMEDI_INSN ioctl 1594 1604 * synchronous instruction ··· 2249 2239 rc = -EFAULT; 2250 2240 break; 2251 2241 } 2242 + rc = check_insnlist_len(dev, insnlist.n_insns); 2243 + if (rc) 2244 + break; 2252 2245 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL); 2253 2246 if (!insns) { 2254 2247 rc = -ENOMEM; ··· 3155 3142 if (copy_from_user(&insnlist32, compat_ptr(arg), sizeof(insnlist32))) 3156 3143 return -EFAULT; 3157 3144 3145 + rc = check_insnlist_len(dev, insnlist32.n_insns); 3146 + if (rc) 3147 + return rc; 3158 3148 insns = kcalloc(insnlist32.n_insns, sizeof(*insns), GFP_KERNEL); 3159 3149 if (!insns) 3160 3150 return -ENOMEM;