mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: hyunjungg <hj351016@gmail.com>
To: Jiri Kosina <jikos@kernel.org>
Cc: Benjamin Tissoires <bentiss@kernel.org>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	linux-input@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org, Hyunjung Ko <hj351016@gmail.com>
Subject: [PATCH v3] HID: microsoft: fix use-after-free of ff_worker on teardown
Date: Mon,  9 Nov 2026 15:06:33 +0900	[thread overview]
Message-ID: <20261109060633.1231417-1-hj351016@gmail.com> (raw)

From: Hyunjung Ko <hj351016@gmail.com>

ms_play_effect() schedules ms->ff_worker to send the force-feedback
report; ms (and ms->output_report_dmabuf) is devm-allocated and freed
after ms_remove() returns, and the work was never cancelled on teardown,
so ms_ff_worker() could run against freed memory.

hid_hw_stop() in ms_remove() unregisters the input device, whose teardown
(input_ff_flush() -> ms_play_effect()) re-queues ff_worker, and a work
left running after ll_driver->stop() would also issue an output report on
an already-stopped transport. Disable the work with disable_work_sync()
*before* hid_hw_stop(), so it is drained while the transport is still live
and cannot be re-queued during teardown.

ff_worker was only INIT_WORK()'d in ms_input_configured() for
force-feedback devices, so initialise it unconditionally in ms_probe()
(making disable_work_sync() always valid) and also disable it on the
hid_hw_start() error path, whose unwind can likewise schedule it.

Not runtime-tested (requires MS force-feedback hardware).

Fixes: 1cfc77a64b71 ("HID: microsoft: move FF initialization to .input_configured()")
Cc: stable@vger.kernel.org
Signed-off-by: Hyunjung Ko <hj351016@gmail.com>
---
diff --git a/drivers/hid/hid-microsoft.c b/drivers/hid/hid-microsoft.c
index a7d3493a6141..5e4218beec92 100644
--- a/drivers/hid/hid-microsoft.c
+++ b/drivers/hid/hid-microsoft.c
@@ -335,7 +335,6 @@ static int ms_input_configured(struct hid_device *hdev, struct hid_input *hidinp
 		return 0;
 
 	ms->hdev = hdev;
-	INIT_WORK(&ms->ff_worker, ms_ff_worker);
 
 	ms->output_report_dmabuf = devm_kzalloc(&hdev->dev,
 						sizeof(struct xb1s_ff_report),
@@ -361,6 +360,13 @@ static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id)
 
 	hid_set_drvdata(hdev, ms);
 
+	/* ff_worker can be scheduled from input_ff_flush() during a failed
+	 * hid_hw_start() unwind and on removal, so initialise it
+	 * unconditionally before hid_hw_start() rather than only for
+	 * force-feedback devices in ms_input_configured().
+	 */
+	INIT_WORK(&ms->ff_worker, ms_ff_worker);
+
 	if (quirks & MS_NOGET)
 		hdev->quirks |= HID_QUIRK_NOGET;
 
@@ -377,6 +383,7 @@ static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id)
 				HID_CONNECT_HIDINPUT_FORCE : 0));
 	if (ret) {
 		hid_err(hdev, "hw start failed\n");
+		disable_work_sync(&ms->ff_worker);
 		return ret;
 	}
 
@@ -384,6 +391,16 @@ static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id)
 }
 static void ms_remove(struct hid_device *hdev)
 {
+	struct ms_data *ms = hid_get_drvdata(hdev);
+
+	/*
+	 * hid_hw_stop() tears down the input device, whose input_ff_flush()
+	 * -> ms_play_effect() path re-queues ff_worker, and a work still
+	 * running after ll_driver->stop() would issue an output report on a
+	 * stopped transport.  Disable the work first so it is drained while
+	 * the transport is live and cannot be re-queued during teardown.
+	 */
+	disable_work_sync(&ms->ff_worker);
 	hid_hw_stop(hdev);
 }
 
-- 
2.43.0


                 reply	other threads:[~2026-11-09  6:06 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261109060633.1231417-1-hj351016@gmail.com \
    --to=hj351016@gmail.com \
    --cc=bentiss@kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --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

all inboxes | Powered by JetHome®