Skip to content

Commit d02abd5

Browse files
aleksamagickagroeck
authored andcommitted
hwmon: (corsair-cpro) Protect ccp->wait_input_report with a spinlock
Through hidraw, userspace can cause a status report to be sent from the device. The parsing in ccp_raw_event() may happen in parallel to a send_usb_cmd() call (which resets the completion for tracking the report) if it's running on a different CPU where bottom half interrupts are not disabled. Add a spinlock around the complete_all() in ccp_raw_event() and reinit_completion() in send_usb_cmd() to prevent race issues. Fixes: 40c3a44 ("hwmon: add Corsair Commander Pro driver") Signed-off-by: Aleksa Savic <savicaleksa83@gmail.com> Acked-by: Marius Zachmann <mail@mariuszachmann.de> Link: https://lore.kernel.org/r/20240504092504.24158-4-savicaleksa83@gmail.com Signed-off-by: Guenter Roeck <linux@roeck-us.net>
1 parent 3a034a7 commit d02abd5

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

drivers/hwmon/corsair-cpro.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/module.h>
1717
#include <linux/mutex.h>
1818
#include <linux/slab.h>
19+
#include <linux/spinlock.h>
1920
#include <linux/types.h>
2021

2122
#define USB_VENDOR_ID_CORSAIR 0x1b1c
@@ -77,6 +78,8 @@
7778
struct ccp_device {
7879
struct hid_device *hdev;
7980
struct device *hwmon_dev;
81+
/* For reinitializing the completion below */
82+
spinlock_t wait_input_report_lock;
8083
struct completion wait_input_report;
8184
struct mutex mutex; /* whenever buffer is used, lock before send_usb_cmd */
8285
u8 *cmd_buffer;
@@ -118,7 +121,15 @@ static int send_usb_cmd(struct ccp_device *ccp, u8 command, u8 byte1, u8 byte2,
118121
ccp->cmd_buffer[2] = byte2;
119122
ccp->cmd_buffer[3] = byte3;
120123

124+
/*
125+
* Disable raw event parsing for a moment to safely reinitialize the
126+
* completion. Reinit is done because hidraw could have triggered
127+
* the raw event parsing and marked the ccp->wait_input_report
128+
* completion as done.
129+
*/
130+
spin_lock_bh(&ccp->wait_input_report_lock);
121131
reinit_completion(&ccp->wait_input_report);
132+
spin_unlock_bh(&ccp->wait_input_report_lock);
122133

123134
ret = hid_hw_output_report(ccp->hdev, ccp->cmd_buffer, OUT_BUFFER_SIZE);
124135
if (ret < 0)
@@ -136,11 +147,12 @@ static int ccp_raw_event(struct hid_device *hdev, struct hid_report *report, u8
136147
struct ccp_device *ccp = hid_get_drvdata(hdev);
137148

138149
/* only copy buffer when requested */
139-
if (completion_done(&ccp->wait_input_report))
140-
return 0;
141-
142-
memcpy(ccp->buffer, data, min(IN_BUFFER_SIZE, size));
143-
complete_all(&ccp->wait_input_report);
150+
spin_lock(&ccp->wait_input_report_lock);
151+
if (!completion_done(&ccp->wait_input_report)) {
152+
memcpy(ccp->buffer, data, min(IN_BUFFER_SIZE, size));
153+
complete_all(&ccp->wait_input_report);
154+
}
155+
spin_unlock(&ccp->wait_input_report_lock);
144156

145157
return 0;
146158
}
@@ -515,7 +527,9 @@ static int ccp_probe(struct hid_device *hdev, const struct hid_device_id *id)
515527

516528
ccp->hdev = hdev;
517529
hid_set_drvdata(hdev, ccp);
530+
518531
mutex_init(&ccp->mutex);
532+
spin_lock_init(&ccp->wait_input_report_lock);
519533
init_completion(&ccp->wait_input_report);
520534

521535
hid_device_io_start(hdev);

0 commit comments

Comments
 (0)