Takashi Iwai 58141e
From 9fefb6201c4f8dd9f58c581b2a66e5cde2895ea2 Mon Sep 17 00:00:00 2001
Takashi Iwai 58141e
From: Pietro Borrello <borrello@diag.uniroma1.it>
Takashi Iwai 58141e
Date: Sun, 12 Feb 2023 18:59:59 +0000
Takashi Iwai 58141e
Subject: [PATCH] HID: bigben: use spinlock to protect concurrent accesses
Takashi Iwai 58141e
Git-commit: 9fefb6201c4f8dd9f58c581b2a66e5cde2895ea2
Takashi Iwai 58141e
Patch-mainline: v6.3-rc1
Takashi Iwai 58141e
References: CVE-2023-25012 bsc#1207560
Takashi Iwai 58141e
Takashi Iwai 58141e
bigben driver has a worker that may access data concurrently.
Takashi Iwai 58141e
Proct the accesses using a spinlock.
Takashi Iwai 58141e
Takashi Iwai 58141e
Fixes: 256a90ed9e46 ("HID: hid-bigbenff: driver for BigBen Interactive PS3OFMINIPAD gamepad")
Takashi Iwai 58141e
Signed-off-by: Pietro Borrello <borrello@diag.uniroma1.it>
Takashi Iwai 58141e
Link: https://lore.kernel.org/r/20230125-hid-unregister-leds-v4-1-7860c5763c38@diag.uniroma1.it
Takashi Iwai 58141e
Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Takashi Iwai 58141e
Acked-by: Takashi Iwai <tiwai@suse.de>
Takashi Iwai 58141e
Takashi Iwai 58141e
---
Takashi Iwai 58141e
 drivers/hid/hid-bigbenff.c | 52 ++++++++++++++++++++++++++++++++++++--
Takashi Iwai 58141e
 1 file changed, 50 insertions(+), 2 deletions(-)
Takashi Iwai 58141e
Takashi Iwai 58141e
diff --git a/drivers/hid/hid-bigbenff.c b/drivers/hid/hid-bigbenff.c
Takashi Iwai 58141e
index e8b16665860d..ed3d2d7bc1dd 100644
Takashi Iwai 58141e
--- a/drivers/hid/hid-bigbenff.c
Takashi Iwai 58141e
+++ b/drivers/hid/hid-bigbenff.c
Takashi Iwai 58141e
@@ -174,6 +174,7 @@ static __u8 pid0902_rdesc_fixed[] = {
Takashi Iwai 58141e
 struct bigben_device {
Takashi Iwai 58141e
 	struct hid_device *hid;
Takashi Iwai 58141e
 	struct hid_report *report;
Takashi Iwai 58141e
+	spinlock_t lock;
Takashi Iwai 58141e
 	bool removed;
Takashi Iwai 58141e
 	u8 led_state;         /* LED1 = 1 .. LED4 = 8 */
Takashi Iwai 58141e
 	u8 right_motor_on;    /* right motor off/on 0/1 */
Takashi Iwai 58141e
@@ -190,12 +191,27 @@ static void bigben_worker(struct work_struct *work)
Takashi Iwai 58141e
 	struct bigben_device *bigben = container_of(work,
Takashi Iwai 58141e
 		struct bigben_device, worker);
Takashi Iwai 58141e
 	struct hid_field *report_field = bigben->report->field[0];
Takashi Iwai 58141e
+	bool do_work_led = false;
Takashi Iwai 58141e
+	bool do_work_ff = false;
Takashi Iwai 58141e
+	u8 *buf;
Takashi Iwai 58141e
+	u32 len;
Takashi Iwai 58141e
+	unsigned long flags;
Takashi Iwai 58141e
 
Takashi Iwai 58141e
 	if (bigben->removed || !report_field)
Takashi Iwai 58141e
 		return;
Takashi Iwai 58141e
 
Takashi Iwai 58141e
+	buf = hid_alloc_report_buf(bigben->report, GFP_KERNEL);
Takashi Iwai 58141e
+	if (!buf)
Takashi Iwai 58141e
+		return;
Takashi Iwai 58141e
+
Takashi Iwai 58141e
+	len = hid_report_len(bigben->report);
Takashi Iwai 58141e
+
Takashi Iwai 58141e
+	/* LED work */
Takashi Iwai 58141e
+	spin_lock_irqsave(&bigben->lock, flags);
Takashi Iwai 58141e
+
Takashi Iwai 58141e
 	if (bigben->work_led) {
Takashi Iwai 58141e
 		bigben->work_led = false;
Takashi Iwai 58141e
+		do_work_led = true;
Takashi Iwai 58141e
 		report_field->value[0] = 0x01; /* 1 = led message */
Takashi Iwai 58141e
 		report_field->value[1] = 0x08; /* reserved value, always 8 */
Takashi Iwai 58141e
 		report_field->value[2] = bigben->led_state;
Takashi Iwai 58141e
@@ -204,11 +220,22 @@ static void bigben_worker(struct work_struct *work)
Takashi Iwai 58141e
 		report_field->value[5] = 0x00; /* padding */
Takashi Iwai 58141e
 		report_field->value[6] = 0x00; /* padding */
Takashi Iwai 58141e
 		report_field->value[7] = 0x00; /* padding */
Takashi Iwai 58141e
-		hid_hw_request(bigben->hid, bigben->report, HID_REQ_SET_REPORT);
Takashi Iwai 58141e
+		hid_output_report(bigben->report, buf);
Takashi Iwai 58141e
+	}
Takashi Iwai 58141e
+
Takashi Iwai 58141e
+	spin_unlock_irqrestore(&bigben->lock, flags);
Takashi Iwai 58141e
+
Takashi Iwai 58141e
+	if (do_work_led) {
Takashi Iwai 58141e
+		hid_hw_raw_request(bigben->hid, bigben->report->id, buf, len,
Takashi Iwai 58141e
+				   bigben->report->type, HID_REQ_SET_REPORT);
Takashi Iwai 58141e
 	}
Takashi Iwai 58141e
 
Takashi Iwai 58141e
+	/* FF work */
Takashi Iwai 58141e
+	spin_lock_irqsave(&bigben->lock, flags);
Takashi Iwai 58141e
+
Takashi Iwai 58141e
 	if (bigben->work_ff) {
Takashi Iwai 58141e
 		bigben->work_ff = false;
Takashi Iwai 58141e
+		do_work_ff = true;
Takashi Iwai 58141e
 		report_field->value[0] = 0x02; /* 2 = rumble effect message */
Takashi Iwai 58141e
 		report_field->value[1] = 0x08; /* reserved value, always 8 */
Takashi Iwai 58141e
 		report_field->value[2] = bigben->right_motor_on;
Takashi Iwai 58141e
@@ -217,8 +244,17 @@ static void bigben_worker(struct work_struct *work)
Takashi Iwai 58141e
 		report_field->value[5] = 0x00; /* padding */
Takashi Iwai 58141e
 		report_field->value[6] = 0x00; /* padding */
Takashi Iwai 58141e
 		report_field->value[7] = 0x00; /* padding */
Takashi Iwai 58141e
-		hid_hw_request(bigben->hid, bigben->report, HID_REQ_SET_REPORT);
Takashi Iwai 58141e
+		hid_output_report(bigben->report, buf);
Takashi Iwai 58141e
+	}
Takashi Iwai 58141e
+
Takashi Iwai 58141e
+	spin_unlock_irqrestore(&bigben->lock, flags);
Takashi Iwai 58141e
+
Takashi Iwai 58141e
+	if (do_work_ff) {
Takashi Iwai 58141e
+		hid_hw_raw_request(bigben->hid, bigben->report->id, buf, len,
Takashi Iwai 58141e
+				   bigben->report->type, HID_REQ_SET_REPORT);
Takashi Iwai 58141e
 	}
Takashi Iwai 58141e
+
Takashi Iwai 58141e
+	kfree(buf);
Takashi Iwai 58141e
 }
Takashi Iwai 58141e
 
Takashi Iwai 58141e
 static int hid_bigben_play_effect(struct input_dev *dev, void *data,
Takashi Iwai 58141e
@@ -228,6 +264,7 @@ static int hid_bigben_play_effect(struct input_dev *dev, void *data,
Takashi Iwai 58141e
 	struct bigben_device *bigben = hid_get_drvdata(hid);
Takashi Iwai 58141e
 	u8 right_motor_on;
Takashi Iwai 58141e
 	u8 left_motor_force;
Takashi Iwai 58141e
+	unsigned long flags;
Takashi Iwai 58141e
 
Takashi Iwai 58141e
 	if (!bigben) {
Takashi Iwai 58141e
 		hid_err(hid, "no device data\n");
Takashi Iwai 58141e
@@ -242,9 +279,12 @@ static int hid_bigben_play_effect(struct input_dev *dev, void *data,
Takashi Iwai 58141e
 
Takashi Iwai 58141e
 	if (right_motor_on != bigben->right_motor_on ||
Takashi Iwai 58141e
 			left_motor_force != bigben->left_motor_force) {
Takashi Iwai 58141e
+		spin_lock_irqsave(&bigben->lock, flags);
Takashi Iwai 58141e
 		bigben->right_motor_on   = right_motor_on;
Takashi Iwai 58141e
 		bigben->left_motor_force = left_motor_force;
Takashi Iwai 58141e
 		bigben->work_ff = true;
Takashi Iwai 58141e
+		spin_unlock_irqrestore(&bigben->lock, flags);
Takashi Iwai 58141e
+
Takashi Iwai 58141e
 		schedule_work(&bigben->worker);
Takashi Iwai 58141e
 	}
Takashi Iwai 58141e
 
Takashi Iwai 58141e
@@ -259,6 +299,7 @@ static void bigben_set_led(struct led_classdev *led,
Takashi Iwai 58141e
 	struct bigben_device *bigben = hid_get_drvdata(hid);
Takashi Iwai 58141e
 	int n;
Takashi Iwai 58141e
 	bool work;
Takashi Iwai 58141e
+	unsigned long flags;
Takashi Iwai 58141e
 
Takashi Iwai 58141e
 	if (!bigben) {
Takashi Iwai 58141e
 		hid_err(hid, "no device data\n");
Takashi Iwai 58141e
@@ -267,6 +308,7 @@ static void bigben_set_led(struct led_classdev *led,
Takashi Iwai 58141e
 
Takashi Iwai 58141e
 	for (n = 0; n < NUM_LEDS; n++) {
Takashi Iwai 58141e
 		if (led == bigben->leds[n]) {
Takashi Iwai 58141e
+			spin_lock_irqsave(&bigben->lock, flags);
Takashi Iwai 58141e
 			if (value == LED_OFF) {
Takashi Iwai 58141e
 				work = (bigben->led_state & BIT(n));
Takashi Iwai 58141e
 				bigben->led_state &= ~BIT(n);
Takashi Iwai 58141e
@@ -274,6 +316,7 @@ static void bigben_set_led(struct led_classdev *led,
Takashi Iwai 58141e
 				work = !(bigben->led_state & BIT(n));
Takashi Iwai 58141e
 				bigben->led_state |= BIT(n);
Takashi Iwai 58141e
 			}
Takashi Iwai 58141e
+			spin_unlock_irqrestore(&bigben->lock, flags);
Takashi Iwai 58141e
 
Takashi Iwai 58141e
 			if (work) {
Takashi Iwai 58141e
 				bigben->work_led = true;
Takashi Iwai 58141e
@@ -307,8 +350,12 @@ static enum led_brightness bigben_get_led(struct led_classdev *led)
Takashi Iwai 58141e
 static void bigben_remove(struct hid_device *hid)
Takashi Iwai 58141e
 {
Takashi Iwai 58141e
 	struct bigben_device *bigben = hid_get_drvdata(hid);
Takashi Iwai 58141e
+	unsigned long flags;
Takashi Iwai 58141e
 
Takashi Iwai 58141e
+	spin_lock_irqsave(&bigben->lock, flags);
Takashi Iwai 58141e
 	bigben->removed = true;
Takashi Iwai 58141e
+	spin_unlock_irqrestore(&bigben->lock, flags);
Takashi Iwai 58141e
+
Takashi Iwai 58141e
 	cancel_work_sync(&bigben->worker);
Takashi Iwai 58141e
 	hid_hw_stop(hid);
Takashi Iwai 58141e
 }
Takashi Iwai 58141e
@@ -362,6 +409,7 @@ static int bigben_probe(struct hid_device *hid,
Takashi Iwai 58141e
 	set_bit(FF_RUMBLE, hidinput->input->ffbit);
Takashi Iwai 58141e
 
Takashi Iwai 58141e
 	INIT_WORK(&bigben->worker, bigben_worker);
Takashi Iwai 58141e
+	spin_lock_init(&bigben->lock);
Takashi Iwai 58141e
 
Takashi Iwai 58141e
 	error = input_ff_create_memless(hidinput->input, NULL,
Takashi Iwai 58141e
 		hid_bigben_play_effect);
Takashi Iwai 58141e
-- 
Takashi Iwai 58141e
2.35.3
Takashi Iwai 58141e