Linux Input/HID development
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, "Filipe Laíns" <lains@riseup.net>,
	"Bastien Nocera" <hadess@hadess.net>,
	"Jiri Kosina" <jikos@kernel.org>,
	"Benjamin Tissoires" <bentiss@kernel.org>,
	"Lopez Casado" <nlopezcasad@logitech.com>,
	"Benoît Sevens" <bsevens@google.com>,
	linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 03/10] HID: logitech-hidpp: Synchronise send_receive_buf against raw events
Date: Mon, 23 Nov 2026 15:50:47 +0000	[thread overview]
Message-ID: <20261123155138.2907049-4-lee@kernel.org> (raw)
In-Reply-To: <20261123155138.2907049-1-lee@kernel.org>

__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 <lee@kernel.org>
---
 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 <linux/slab.h>
 #include <linux/sched.h>
 #include <linux/sched/clock.h>
+#include <linux/spinlock.h>
 #include <linux/kfifo.h>
 #include <linux/input/mt.h>
 #include <linux/workqueue.h>
@@ -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


  parent reply	other threads:[~2026-11-23 15:52 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 15:50 [PATCH 00/10] HID: Assorted bug fixes across HID core and drivers Lee Jones
2026-11-23 15:50 ` [PATCH 01/10] HID: core: Protect hid->inputs traversal in hid_check_keys_pressed() Lee Jones
2026-11-23 15:50 ` [PATCH 02/10] HID: mcp2221: Synchronise rxbuf access against raw_event callback Lee Jones
2026-11-23 15:50 ` Lee Jones [this message]
2026-11-23 15:50 ` [PATCH 04/10] HID: sensor-hub: Serialise synchronous read teardown under data->lock Lee Jones
2026-11-23 15:50 ` [PATCH 05/10] HID: roccat: pyra: Resolve USB device from parent in actual_profile Lee Jones
2026-11-23 15:50 ` [PATCH 06/10] HID: hyperv: Validate VMBus packet and payload bounds on receive Lee Jones
2026-11-23 15:50 ` [PATCH 07/10] HID: hid-goodix-spi: Validate package sizes in goodix_hid_irq() Lee Jones

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=20261123155138.2907049-4-lee@kernel.org \
    --to=lee@kernel.org \
    --cc=bentiss@kernel.org \
    --cc=bsevens@google.com \
    --cc=hadess@hadess.net \
    --cc=jikos@kernel.org \
    --cc=lains@riseup.net \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nlopezcasad@logitech.com \
    --cc=stable@vger.kernel.org \
    /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