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