[PATCH v3 1/4] Input: adi - reject stream merges that do not fit the buffer
From: Guanglei Zhu
Date: Tue Dec 01 2026 - 07:23:24 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Guanglei Zhu <zhugl3@xxxxxxxxxxxx>
---
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