adi_move_bits() appends the second half of a two stream packet to the first at an offset taken from the 10-bit length field of the ID packet: for (i = 1; i <= adi[1].ret; i++) adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i]; Nothing compares the result with the size of the buffer. adi[0].data is the last member of struct adi and struct adi_port holds two of them back to back, so a large length field makes the loop write over the second struct adi and past the allocation, and the inflated ret then makes adi_get_bits() read past the buffer as well. A zero length, which the connect path can pass before the field has been validated, folds the second packet onto the first one instead. Reject a merge that does not fit, and compute the offset once. Use >= for the combined length so adi_get_bits() cannot walk to data[ret] with ret == ADI_MAX_LENGTH. Invalidate both half streams on the rejection so that the unmerged packets are not decoded as standalone reads. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Signed-off-by: Guanglei Zhu --- Tested with a user space replay of adi_move_bits(), built with ASan. On master a length of 1023 with a 200 byte first stream and a 255 byte second one writes at data[513..767]; struct adi_port is 912 bytes and adi[0].data sits at offset 200, so ASan reports a heap-buffer-overflow. A length of 0 moves the second stream to data[1..4], on top of the first one. With this patch both cases leave both streams at ret = -1 and the buffer untouched. drivers/input/joystick/adi.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/drivers/input/joystick/adi.c b/drivers/input/joystick/adi.c index e07b719..026f7de 100644 --- a/drivers/input/joystick/adi.c +++ b/drivers/input/joystick/adi.c @@ -161,7 +161,7 @@ static void adi_read_packet(struct adi_port *port) static void adi_move_bits(struct adi_port *port, int length) { - int i; + int i, offset; struct adi *adi = port->adi; adi[0].idx = adi[1].idx = 0; @@ -169,11 +169,24 @@ static void adi_move_bits(struct adi_port *port, int length) if (adi[0].ret <= 0 || adi[1].ret <= 0) return; if (adi[0].data[0] & 0x20 || ~adi[1].data[0] & 0x20) return; + if (length <= 0) + goto invalid; + + offset = ((length - 1) >> 1) + 1; + if (offset + adi[1].ret >= ADI_MAX_LENGTH || + adi[0].ret + adi[1].ret >= ADI_MAX_LENGTH) + goto invalid; + for (i = 1; i <= adi[1].ret; i++) - adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i]; + adi[0].data[offset + i] = adi[1].data[i]; adi[0].ret += adi[1].ret; adi[1].ret = -1; + return; + +invalid: + adi[0].ret = -1; + adi[1].ret = -1; } /* -- 2.43.0 The loop that samples the wire stores each bit with adi[i].data[++adi[i].ret] = w; so ret is incremented before the store, but the guard in front of it tests adi[i].ret < ADI_MAX_LENGTH. A packet that fills the buffer therefore writes data[ADI_MAX_LENGTH], one byte past the array, before the guard stops it. Test against ADI_MAX_LENGTH - 1. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Signed-off-by: Guanglei Zhu --- Tested with the same replay loop driven from a table instead of the gameport. On master a 257 byte packet stores data[256] and ASan reports a heap-buffer-overflow one byte past struct adi; with this patch the loop stops at ret = 255. drivers/input/joystick/adi.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/input/joystick/adi.c b/drivers/input/joystick/adi.c index 026f7de..ad0845e 100644 --- a/drivers/input/joystick/adi.c +++ b/drivers/input/joystick/adi.c @@ -141,10 +141,14 @@ static void adi_read_packet(struct adi_port *port) for (i = 0; i < 2; i++, w >>= 2, x >>= 2) { t[i]--; if ((w & 0x30) && s[i]) { - if ((w & 0x30) < 0x30 && adi[i].ret < ADI_MAX_LENGTH && t[i] > 0) { + if ((w & 0x30) < 0x30 && + adi[i].ret < ADI_MAX_LENGTH - 1 && + t[i] > 0) { adi[i].data[++adi[i].ret] = w; t[i] = gameport_time(gameport, ADI_MAX_STROBE); - } else t[i] = 0; + } else { + t[i] = 0; + } } else if (!(x & 0x30)) s[i] = 1; } } while (t[0] > 0 || t[1] > 0); -- 2.43.0 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 adi_id_decode() decodes the number of axes, hats and buttons from the ID packet, and adi_init_input(), adi_init_center() and adi_decode() then walk adi->abs and adi->key, the axis and key tables of the device: for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) set_bit(adi->abs[i], input_dev->absbit); The packet is free to claim more axes or buttons than the device has, which walks past the end of those static tables. Keep the size of both tables next to the pointers and reject an ID packet whose counts do not fit. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Signed-off-by: Guanglei Zhu --- Tested with the same builder: an ID packet for device 0 that asks for 15 axes and 15 buttons passes the length check, and the setup loop then reads adi_wmi_abs[9..14], past the end of that 9 entry table; ASan reports a global-buffer-overflow. With this patch the packet is rejected with length = 0. drivers/input/joystick/adi.c | 48 +++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/drivers/input/joystick/adi.c b/drivers/input/joystick/adi.c index 57a7978..a329fcb 100644 --- a/drivers/input/joystick/adi.c +++ b/drivers/input/joystick/adi.c @@ -67,11 +67,37 @@ static short adi_wmi_key[] = { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_B static short adi_wmed3d_key[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2 }; static short adi_cm2_key[] = { BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 }; -static char* adi_abs[] = { adi_wmi_abs, adi_wmgpe_abs, adi_wmf_abs, adi_cm2_abs, adi_wmi_abs, adi_wmf_abs, - adi_wmgpe_abs, adi_wmed3d_abs, adi_wmgpe_abs, adi_wmgpe_abs, adi_wmi_abs }; +struct adi_tables { + char *abs; + unsigned int abs_count; + short *key; + unsigned int key_count; +}; -static short* adi_key[] = { adi_wmi_key, adi_wmgpe_key, adi_cm2_key, adi_cm2_key, adi_wmi_key, adi_cm2_key, - adi_wmgpe_key, adi_wmed3d_key, adi_wmgpe_key, adi_wmgpe_key, adi_wmi_key }; +static const struct adi_tables adi_tables[] = { + { adi_wmi_abs, ARRAY_SIZE(adi_wmi_abs), adi_wmi_key, + ARRAY_SIZE(adi_wmi_key) }, + { adi_wmgpe_abs, ARRAY_SIZE(adi_wmgpe_abs), adi_wmgpe_key, + ARRAY_SIZE(adi_wmgpe_key) }, + { adi_wmf_abs, ARRAY_SIZE(adi_wmf_abs), adi_cm2_key, + ARRAY_SIZE(adi_cm2_key) }, + { adi_cm2_abs, ARRAY_SIZE(adi_cm2_abs), adi_cm2_key, + ARRAY_SIZE(adi_cm2_key) }, + { adi_wmi_abs, ARRAY_SIZE(adi_wmi_abs), adi_wmi_key, + ARRAY_SIZE(adi_wmi_key) }, + { adi_wmf_abs, ARRAY_SIZE(adi_wmf_abs), adi_cm2_key, + ARRAY_SIZE(adi_cm2_key) }, + { adi_wmgpe_abs, ARRAY_SIZE(adi_wmgpe_abs), adi_wmgpe_key, + ARRAY_SIZE(adi_wmgpe_key) }, + { adi_wmed3d_abs, ARRAY_SIZE(adi_wmed3d_abs), adi_wmed3d_key, + ARRAY_SIZE(adi_wmed3d_key) }, + { adi_wmgpe_abs, ARRAY_SIZE(adi_wmgpe_abs), adi_wmgpe_key, + ARRAY_SIZE(adi_wmgpe_key) }, + { adi_wmgpe_abs, ARRAY_SIZE(adi_wmgpe_abs), adi_wmgpe_key, + ARRAY_SIZE(adi_wmgpe_key) }, + { adi_wmi_abs, ARRAY_SIZE(adi_wmi_abs), adi_wmi_key, + ARRAY_SIZE(adi_wmi_key) }, +}; /* * Hat to axis conversion arrays. @@ -403,6 +429,16 @@ static void adi_id_decode(struct adi *adi, struct adi_port *port) adi->pad = -1; break; } + + t = adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX; + if (adi->axes10 + adi->axes8 + + (adi->hats + (adi->pad != -1)) * 2 > adi_tables[t].abs_count || + adi->buttons > adi_tables[t].key_count) { + printk(KERN_WARNING "adi: Device %#x has no room for %d axes and %d buttons\n", + adi->id, adi->axes10 + adi->axes8 + + (adi->hats + (adi->pad != -1)) * 2, adi->buttons); + adi->length = 0; + } } static int adi_init_input(struct adi *adi, struct adi_port *port, int half) @@ -421,8 +457,8 @@ static int adi_init_input(struct adi *adi, struct adi_port *port, int half) snprintf(adi->name, ADI_MAX_NAME_LENGTH, "Logitech %s [%s]", buf, adi->cname); snprintf(adi->phys, ADI_MAX_PHYS_LENGTH, "%s/input%d", port->gameport->phys, half); - adi->abs = adi_abs[t]; - adi->key = adi_key[t]; + adi->abs = adi_tables[t].abs; + adi->key = adi_tables[t].key; input_dev->name = adi->name; input_dev->phys = adi->phys; -- 2.43.0