drivers/mt: restrict length of kzalloc in input_mt_init_slots()
syzbot found WARNING in input_mt_init_slots [1] when
struct_size(mt, slots, num_slots)=0x40006 where num_slots=0x10001,
which exceeds KMALLOC_MAX_SIZE (0x40000) and causes
order >= MAX_ORDER condition.
[1]
Call Trace:
alloc_pages_current+0x18c/0x2a0 mm/mempolicy.c:2267
alloc_pages include/linux/gfp.h:547 [inline]
kmalloc_order+0x2e/0xb0 mm/slab_common.c:837
kmalloc_order_trace+0x14/0x120 mm/slab_common.c:853
kmalloc include/linux/slab.h:557 [inline]
kzalloc include/linux/slab.h:682 [inline]
input_mt_init_slots drivers/input/input-mt.c:49 [inline]
Reported-by: syzbot+0122fa359a69694395d5@syzkaller.appspotmail.com
Change-Id: Ifbb9492d737564e536ba749d85560693d9235ee5
diff --git a/drivers/input/input-mt.c b/drivers/input/input-mt.c
index 44fe6f2..e542f45 100644
--- a/drivers/input/input-mt.c
+++ b/drivers/input/input-mt.c
@@ -40,13 +40,18 @@
{
struct input_mt *mt = dev->mt;
int i;
+ size_t mt_size = 0;
if (!num_slots)
return 0;
if (mt)
return mt->num_slots != num_slots ? -EINVAL : 0;
- mt = kzalloc(struct_size(mt, slots, num_slots), GFP_KERNEL);
+ mt_size = struct_size(mt, slots, num_slots);
+ if (mt_size > KMALLOC_MAX_SIZE)
+ return -ENOMEM;
+
+ mt = kzalloc(mt_size, GFP_KERNEL);
if (!mt)
goto err_mem;