[PATCH v3 4/4] Input: adi - check the decoded counts against the device tables
From: Guanglei Zhu
Date: Tue Dec 01 2026 - 07:23:43 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Guanglei Zhu <zhugl3@xxxxxxxxxxxx>
---
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