Linux Input/HID development
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: linux-input@vger.kernel.org
Cc: Lee Jones <lee@kernel.org>, Vojtech Pavlik <vojtech@ucw.cz>,
	Guanglei Zhu <zhugl3@xiaopeng.com>,
	linux-kernel@vger.kernel.org, sashiko-bot@kernel.org
Subject: [PATCH 3/8] Input: adi - validate ID packet axis and button counts
Date: Sun, 29 Nov 2026 21:42:40 -0800	[thread overview]
Message-ID: <20261130054245.2215181-4-dmitry.torokhov@gmail.com> (raw)
In-Reply-To: <20261130054245.2215181-1-dmitry.torokhov@gmail.com>

adi_id_decode() only validates parsed axis, hat, and button counts
against the overall packet bit length, allowing malformed ID packets to
trigger integer underflow or out-of-bounds accesses against the static
per-model capability tables:

- When the 10-bit flag is set, subtracting a larger 8-bit axis count
  from the total axis count underflows.
- For ThunderPad Digital and WingMan GamePad, subtracting 4 pad bits
  from an insufficient button count underflows or leaves the pad index
  unreachable during packet decoding.
- Valid packet lengths up to 255 bits can describe far more axes or
  buttons than the static per-model tables contain, causing
  out-of-bounds reads, bitmask writes, and axis lookups during input
  registration, centering, and polling.

Consolidate the parallel model metadata arrays into a single descriptor
table that records each model's axis and key table sizes, attach the
selected model descriptor to the per-device state during ID decoding,
and reject ID packets that underflow axis or button counts or exceed
the selected model's table capacities.

Reported-by: sashiko-bot@kernel.org
Closes: https://sashiko.dev/#/bug/linux-92046c21-e0a8-4274-a502-20ca723131b1
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/input/joystick/adi.c | 107 ++++++++++++++++++++++++++---------
 1 file changed, 80 insertions(+), 27 deletions(-)

diff --git a/drivers/input/joystick/adi.c b/drivers/input/joystick/adi.c
index 3e34210de4ed..7eb193ca5390 100644
--- a/drivers/input/joystick/adi.c
+++ b/drivers/input/joystick/adi.c
@@ -42,20 +42,22 @@ MODULE_LICENSE("GPL");
 #define ADI_FLAG_HAT		0x04
 #define ADI_FLAG_10BIT		0x08
 
+#define ADI_ID_WMED		0x00
 #define ADI_ID_TPD		0x01
+#define ADI_ID_SIDECAR		0x02
+#define ADI_ID_CM2		0x03
+#define ADI_ID_WMI		0x04
+#define ADI_ID_WMF		0x05
 #define ADI_ID_WGP		0x06
+#define ADI_ID_WMED3D		0x07
 #define ADI_ID_WGPE		0x08
+#define ADI_ID_WGP_USB		0x09
 #define ADI_ID_MAX		0x0a
 
 /*
  * Names, buttons, axes ...
  */
 
-static char *adi_names[] = {	"WingMan Extreme Digital", "ThunderPad Digital", "SideCar", "CyberMan 2",
-				"WingMan Interceptor", "WingMan Formula", "WingMan GamePad",
-				"WingMan Extreme Digital 3D", "WingMan GamePad Extreme",
-				"WingMan GamePad USB", "Unknown Device %#x" };
-
 static char adi_wmgpe_abs[] =	{ ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y };
 static char adi_wmi_abs[] =	{ ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
 static char adi_wmed3d_abs[] =	{ ABS_X, ABS_Y, ABS_THROTTLE, ABS_RZ, ABS_HAT0X, ABS_HAT0Y };
@@ -67,11 +69,35 @@ 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_model {
+	const char *name;
+	const char *abs;
+	const short *key;
+	unsigned char num_abs;
+	unsigned char num_key;
+};
+
+#define ADI_MODEL(_name, _abs, _key) {	\
+	.name = (_name),		\
+	.abs = (_abs),			\
+	.num_abs = ARRAY_SIZE(_abs),	\
+	.key = (_key),			\
+	.num_key = ARRAY_SIZE(_key),	\
+}
 
-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_model adi_models[] = {
+	[ADI_ID_WMED] = ADI_MODEL("WingMan Extreme Digital", adi_wmi_abs, adi_wmi_key),
+	[ADI_ID_TPD] = ADI_MODEL("ThunderPad Digital", adi_wmgpe_abs, adi_wmgpe_key),
+	[ADI_ID_SIDECAR] = ADI_MODEL("SideCar", adi_wmf_abs, adi_cm2_key),
+	[ADI_ID_CM2] = ADI_MODEL("CyberMan 2", adi_cm2_abs, adi_cm2_key),
+	[ADI_ID_WMI] = ADI_MODEL("WingMan Interceptor", adi_wmi_abs, adi_wmi_key),
+	[ADI_ID_WMF] = ADI_MODEL("WingMan Formula", adi_wmf_abs, adi_cm2_key),
+	[ADI_ID_WGP] = ADI_MODEL("WingMan GamePad", adi_wmgpe_abs, adi_wmgpe_key),
+	[ADI_ID_WMED3D] = ADI_MODEL("WingMan Extreme Digital 3D", adi_wmed3d_abs, adi_wmed3d_key),
+	[ADI_ID_WGPE] = ADI_MODEL("WingMan GamePad Extreme", adi_wmgpe_abs, adi_wmgpe_key),
+	[ADI_ID_WGP_USB] = ADI_MODEL("WingMan GamePad USB", adi_wmgpe_abs, adi_wmgpe_key),
+	[ADI_ID_MAX] = ADI_MODEL("Unknown Device %#x", adi_wmi_abs, adi_wmi_key),
+};
 
 /*
  * Hat to axis conversion arrays.
@@ -97,8 +123,7 @@ struct adi {
 	char axes8;
 	signed char pad;
 	char hats;
-	char *abs;
-	short *key;
+	const struct adi_model *model;
 	char name[ADI_MAX_NAME_LENGTH];
 	char cname[ADI_MAX_CNAME_LENGTH];
 	char phys[ADI_MAX_PHYS_LENGTH];
@@ -112,6 +137,11 @@ struct adi_port {
 	int reads;
 };
 
+static int adi_num_axes(const struct adi *adi)
+{
+	return adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2;
+}
+
 /*
  * adi_read_packet() reads a Logitech ADI packet.
  */
@@ -211,8 +241,8 @@ static inline int adi_get_bits(struct adi *adi, int count)
 static int adi_decode(struct adi *adi)
 {
 	struct input_dev *dev = adi->dev;
-	char *abs = adi->abs;
-	short *key = adi->key;
+	const char *abs = adi->model->abs;
+	const short *key = adi->model->key;
 	int i, t;
 
 	if (adi->ret < adi->length || adi->id != (adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4)))
@@ -324,6 +354,8 @@ static void adi_init_digital(struct gameport *gameport)
 
 static void adi_id_decode(struct adi *adi, struct adi_port *port)
 {
+	struct device *dev = &port->gameport->dev;
+	const struct adi_model *model;
 	int i, t;
 
 	if (adi->ret < ADI_MIN_ID_LENGTH) /* Minimum ID packet length */
@@ -361,6 +393,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) {
+			dev_warn(dev, "Bad 10-bit axis count (%d > %d)\n",
+				 i, adi->axes8);
+			adi->length = 0;
+			return;
+		}
 		adi->axes10 = adi->axes8 - i;
 		adi->axes8 = i;
 	}
@@ -381,37 +419,52 @@ 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;
-			adi->buttons -= 4;
 			break;
 		default:
 			adi->pad = -1;
 			break;
 	}
+
+	if (adi->pad >= 0) {
+		if (adi->buttons <= adi->pad + 4) {
+			dev_warn(dev, "Invalid button count %d for pad %d\n",
+				 adi->buttons, adi->pad);
+			adi->length = 0;
+			return;
+		}
+		adi->buttons -= 4;
+	}
+
+	model = &adi_models[adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX];
+	if (adi_num_axes(adi) > model->num_abs || adi->buttons > model->num_key) {
+		dev_warn(dev,
+			 "Model %#x reports too many axes (%d, max %d) or buttons (%d, max %d)\n",
+			 adi->id, adi_num_axes(adi), model->num_abs,
+			 adi->buttons, model->num_key);
+		adi->length = 0;
+		return;
+	}
+
+	adi->model = model;
 }
 
 static int adi_init_input(struct adi *adi, struct adi_port *port, int half)
 {
 	struct input_dev *input_dev;
 	char buf[ADI_MAX_NAME_LENGTH];
-	int i, t;
+	int i;
 
 	adi->dev = input_dev = input_allocate_device();
 	if (!input_dev)
 		return -ENOMEM;
 
-	t = adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX;
-
-	snprintf(buf, ADI_MAX_PHYS_LENGTH, adi_names[t], adi->id);
+	snprintf(buf, ADI_MAX_PHYS_LENGTH, adi->model->name, adi->id);
 	scnprintf(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];
-
 	input_dev->name = adi->name;
 	input_dev->phys = adi->phys;
 	input_dev->id.bustype = BUS_GAMEPORT;
@@ -427,11 +480,11 @@ static int adi_init_input(struct adi *adi, struct adi_port *port, int half)
 
 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
 
-	for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++)
-		set_bit(adi->abs[i], input_dev->absbit);
+	for (i = 0; i < adi_num_axes(adi); i++)
+		set_bit(adi->model->abs[i], input_dev->absbit);
 
 	for (i = 0; i < adi->buttons; i++)
-		set_bit(adi->key[i], input_dev->keybit);
+		set_bit(adi->model->key[i], input_dev->keybit);
 
 	return 0;
 }
@@ -443,9 +496,9 @@ static void adi_init_center(struct adi *adi)
 	if (!adi->length)
 		return;
 
-	for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) {
+	for (i = 0; i < adi_num_axes(adi); i++) {
 
-		t = adi->abs[i];
+		t = adi->model->abs[i];
 		x = input_abs_get_val(adi->dev, t);
 
 		if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE)
-- 
2.56.1.839.g22a8290db9-goog


  parent reply	other threads:[~2026-11-30  5:42 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  5:42 [PATCH 0/8] Input: adi - bounds checking fixes and driver cleanups Dmitry Torokhov
2026-11-30  5:42 ` [PATCH 1/8] Input: adi - fix out-of-bounds write in adi_read_packet() Dmitry Torokhov
2026-11-30  5:42 ` [PATCH 2/8] Input: adi - bound two-stream packet merge in adi_move_bits() Dmitry Torokhov
2026-11-30  5:42 ` Dmitry Torokhov [this message]
2026-11-30  5:42 ` [PATCH 4/8] Input: adi - use explicit fixed-width types and const tables Dmitry Torokhov
2026-11-30  5:42 ` [PATCH 5/8] Input: adi - simplify device name formatting Dmitry Torokhov
2026-11-30  5:42 ` [PATCH 6/8] Input: adi - use dev_warn() in adi_id_decode() Dmitry Torokhov
2026-11-30  5:42 ` [PATCH 7/8] Input: adi - use BIT() and GENMASK() for bitmasks and bit checks Dmitry Torokhov
2026-11-30  5:42 ` [PATCH 8/8] Input: adi - clean up formatting Dmitry Torokhov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130054245.2215181-4-dmitry.torokhov@gmail.com \
    --to=dmitry.torokhov@gmail.com \
    --cc=lee@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sashiko-bot@kernel.org \
    --cc=vojtech@ucw.cz \
    --cc=zhugl3@xiaopeng.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox