adi_id_decode() derives the counts from fields of the ID packet and hands them to adi_decode() and the input device setup: adi->axes10 = adi->axes8 - i; ... adi->buttons -= 4; Nothing checks that the packet leaves enough behind for the split. A 10-bit axis count larger than the 8-bit one, or a TPD or WGP packet with fewer than four buttons, subtracts below zero, and the loops that report the axes and buttons then quietly do nothing. The fields are plain char as well, so what they hold depends on whether char is signed on the architecture. Store the byte fields as u8 and pad as s8 so the values do not change with the architecture, and reject an ID packet whose split or button count would wrap instead of letting the wrapped value reach the loops that walk the axis and button tables. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Signed-off-by: Guanglei Zhu --- Tested with a bit stream builder for the ID packet, built with ASan. On master a packet whose 10-bit split is larger than the 8-bit count gives axes10 = -1, and a TPD packet with three buttons gives buttons = -1; the reporting loops then skip them. With this patch both packets are rejected with length = 0. Storing the counts as u8 without the checks wraps them to 255 instead, and the wrapped counts walk past adi_key[] and adi_abs[] - ASan reports a read past the 8 entry table. drivers/input/joystick/adi.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/drivers/input/joystick/adi.c b/drivers/input/joystick/adi.c index ad0845e..57a7978 100644 --- a/drivers/input/joystick/adi.c +++ b/drivers/input/joystick/adi.c @@ -91,18 +91,18 @@ struct adi { int length; int ret; int idx; - unsigned char id; - char buttons; - char axes10; - char axes8; - signed char pad; - char hats; + u8 id; + u8 buttons; + u8 axes10; + u8 axes8; + s8 pad; + u8 hats; char *abs; short *key; char name[ADI_MAX_NAME_LENGTH]; char cname[ADI_MAX_CNAME_LENGTH]; char phys[ADI_MAX_PHYS_LENGTH]; - unsigned char data[ADI_MAX_LENGTH]; + u8 data[ADI_MAX_LENGTH]; }; struct adi_port { @@ -364,6 +364,12 @@ static void adi_id_decode(struct adi *adi, struct adi_port *port) i = adi_get_bits(adi, 4); if (t & ADI_FLAG_10BIT) { + if (i > adi->axes8) { + printk(KERN_WARNING "adi: Bad axis split (%d of %d 10-bit)\n", + i, adi->axes8); + adi->length = 0; + return; + } adi->axes10 = adi->axes8 - i; adi->axes8 = i; } @@ -383,11 +389,14 @@ static void adi_id_decode(struct adi *adi, struct adi_port *port) switch (adi->id) { case ADI_ID_TPD: - adi->pad = 4; - adi->buttons -= 4; - break; case ADI_ID_WGP: - adi->pad = 0; + if (adi->buttons < 4) { + printk(KERN_WARNING "adi: Bad button count (%d) for device %#x\n", + adi->buttons, adi->id); + adi->length = 0; + return; + } + adi->pad = adi->id == ADI_ID_TPD ? 4 : 0; adi->buttons -= 4; break; default: -- 2.43.0