[PATCH 2/8] Input: adi - bound two-stream packet merge in adi_move_bits()

From: Dmitry Torokhov

Date: Mon Nov 30 2026 - 00:43:01 EST


In two-stream mode, adi_move_bits() merges the second stream's samples
into the primary stream's data buffer at an offset derived from the
expected packet length, without validating either the length or the
resulting buffer indices and combined sample count. A zero length from
the raw header or an uninitialized primary stream produces an invalid
shift and offset, while overly large offsets or stream lengths write
past the end of the primary buffer into adjacent port state and allow
subsequent bit reads to overrun the buffer.

Reject non-positive lengths before computing the merge offset, and
reject any merge whose combined sample count or destination offset
would reach or exceed the buffer size. Invalidate both streams on
rejection so unmerged half-streams are not decoded as standalone
packets.

Reported-by: Guanglei Zhu <zhugl3@xxxxxxxxxxxx>
Link: https://lore.kernel.org/r/20261103111714.472080-1-zhugl3@xxxxxxxxxxxx
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
---
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 43c2e60fcb1c..3e34210de4ed 100644
--- a/drivers/input/joystick/adi.c
+++ b/drivers/input/joystick/adi.c
@@ -162,7 +162,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;
@@ -170,11 +170,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 err_out;
+
+ offset = ((length - 1) >> 1) + 1;
+ if (adi[0].ret + adi[1].ret >= ADI_MAX_LENGTH ||
+ offset + adi[1].ret >= ADI_MAX_LENGTH)
+ goto err_out;
+
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;
+
+err_out:
+ adi[0].ret = -1;
+ adi[1].ret = -1;
}

/*
--
2.56.1.839.g22a8290db9-goog