__do_hidpp_send_message_sync() stores a pointer to the caller's stack-allocated response buffer in hidpp->send_receive_buf while holding hidpp->send_mutex, whereas hidpp_raw_hidpp_event() runs in interrupt context without holding hidpp->send_mutex. Clearing hidpp->send_receive_buf before releasing hidpp->send_mutex is insufficient: if hidpp_raw_hidpp_event() loads hidpp->send_receive_buf just before wait_event_timeout() expires and the caller returns, writing *answer = *report corrupts up to 64 bytes of the popped kernel stack frame. Furthermore, hidpp->send_receive_buf was published before initialising *response = *message, and error paths zeroed *response while hidpp->send_receive_buf remained visible. Protect hidpp->send_receive_buf and hidpp->answer_available with a spinlock across setup, completion, and teardown so that hidpp_raw_hidpp_event() cannot access the response buffer once __do_hidpp_send_message_sync() finishes. Fixes: 2f31c5252910 ("HID: Introduce hidpp, a module to handle Logitech hid++ devices") Fixes: e2aaf2d3ad92 ("HID: logitech-hidpp: fix race condition when accessing stale stack pointer") Closes: https://sashiko.dev/#/bug/linux-fed46492-e564-402a-b7b3-8a761e52e5e7 Cc: stable@vger.kernel.org Signed-off-by: Lee Jones --- drivers/hid/hid-logitech-hidpp.c | 58 +++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/drivers/hid/hid-logitech-hidpp.c b/drivers/hid/hid-logitech-hidpp.c index 60fc6b86a085..6c95c4bcdd42 100644 --- a/drivers/hid/hid-logitech-hidpp.c +++ b/drivers/hid/hid-logitech-hidpp.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -185,6 +186,8 @@ struct hidpp_device { struct hid_device *hid_dev; struct input_dev *input; struct mutex send_mutex; + /* protects send_receive_buf and answer_available */ + spinlock_t send_receive_lock; void *send_receive_buf; char *name; /* will never be NULL and should not be freed */ wait_queue_head_t wait; @@ -296,39 +299,44 @@ static int __do_hidpp_send_message_sync(struct hidpp_device *hidpp, struct hidpp_report *message, struct hidpp_report *response) { + unsigned long flags; int ret; __must_hold(&hidpp->send_mutex); - hidpp->send_receive_buf = response; - hidpp->answer_available = false; - /* * So that we can later validate the answer when it arrives * in hidpp_raw_event */ *response = *message; + spin_lock_irqsave(&hidpp->send_receive_lock, flags); + hidpp->send_receive_buf = response; + hidpp->answer_available = false; + spin_unlock_irqrestore(&hidpp->send_receive_lock, flags); + ret = __hidpp_send_report(hidpp->hid_dev, message); if (ret) { dbg_hid("__hidpp_send_report returned err: %d\n", ret); - memset(response, 0, sizeof(struct hidpp_report)); - goto out; + goto err_zero; } - if (!wait_event_timeout(hidpp->wait, hidpp->answer_available, - 5*HZ)) { + if (!wait_event_timeout(hidpp->wait, + READ_ONCE(hidpp->answer_available), 5 * HZ)) { dbg_hid("%s:timeout waiting for response\n", __func__); - memset(response, 0, sizeof(struct hidpp_report)); ret = -ETIMEDOUT; - goto out; + goto err_zero; } + spin_lock_irqsave(&hidpp->send_receive_lock, flags); + hidpp->send_receive_buf = NULL; + spin_unlock_irqrestore(&hidpp->send_receive_lock, flags); + if (response->report_id == REPORT_ID_HIDPP_SHORT && response->rap.sub_id == HIDPP_ERROR) { ret = response->rap.params[1]; dbg_hid("%s:got hidpp error %02X\n", __func__, ret); - goto out; + return ret; } if ((response->report_id == REPORT_ID_HIDPP_LONG || @@ -336,13 +344,16 @@ static int __do_hidpp_send_message_sync(struct hidpp_device *hidpp, response->fap.feature_index == HIDPP20_ERROR) { ret = response->fap.params[1]; dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret); - goto out; + return ret; } - ret = 0; + return 0; -out: +err_zero: + spin_lock_irqsave(&hidpp->send_receive_lock, flags); hidpp->send_receive_buf = NULL; + spin_unlock_irqrestore(&hidpp->send_receive_lock, flags); + memset(response, 0, sizeof(struct hidpp_report)); return ret; } @@ -4125,27 +4136,33 @@ static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data, * previously sent command. */ if (unlikely(mutex_is_locked(&hidpp->send_mutex))) { + unsigned long flags; + bool matched = false; + + spin_lock_irqsave(&hidpp->send_receive_lock, flags); question = hidpp->send_receive_buf; answer = hidpp->send_receive_buf; - if (!question) - return 0; - /* * Check for a correct hidpp20 answer or the corresponding * error */ - if (hidpp_match_answer(question, report) || - hidpp_match_error(question, report)) { + if (question && (hidpp_match_answer(question, report) || + hidpp_match_error(question, report))) { *answer = *report; - hidpp->answer_available = true; + hidpp->send_receive_buf = NULL; + WRITE_ONCE(hidpp->answer_available, true); + matched = true; + } + spin_unlock_irqrestore(&hidpp->send_receive_lock, flags); + + if (matched) { wake_up(&hidpp->wait); /* * This was an answer to a command that this driver sent * We return 1 to hid-core to avoid forwarding the * command upstream as it has been treated by the driver */ - return 1; } } @@ -4723,6 +4740,7 @@ static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id) INIT_WORK(&hidpp->work, hidpp_connect_event); INIT_WORK(&hidpp->reset_hi_res_work, hidpp_reset_hi_res_handler); mutex_init(&hidpp->send_mutex); + spin_lock_init(&hidpp->send_receive_lock); init_waitqueue_head(&hidpp->wait); /* indicates we are handling the battery properties in the kernel */ -- 2.56.1.750.gf70dc682cf-goog