[PATCH v6 02/13] HID: haptic: Fix use-after-free when draining workqueue

From: Rong Zhang

Date: Wed Dec 02 2026 - 13:35:00 EST


The haptic->wq is destroyed after freeing effects and their report
buffers, which is too late. If any work is still pending or running
before the workqueue destruction, haptic_work_handler() will access the
just-freed contents, causing a use-after-free.

Fix it by destroying the workqueue before freeing haptic resources. The
allocation sequence is also adjusted to the reverse.

Since the workqueue allocation is anyway touched, also drop the prefix
constant by integrating it into the fmt string. The workqueue's name now
also uses the input device's kobject name instead of the HID devices's
descriptor name, as the former is unique and much shorter while the
latter is prone to name collision and WQ_NAME_LEN warning.

Also call __clear_bit(EV_FF, dev->evbit) in the bail-out path to match
the behavior of input_ff_destroy().

Fixes: 344ff3584957 ("HID: haptic: initialize haptic device")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Rong Zhang <i@xxxxxxxx>
---
Changes in v6:
- Name the workqueue with the input device's kobject name to fix name
collision and WQ_NAME_LEN warning
- This can be triggered by the Logitech Bolt Receiver once the
following changes are applied
---
drivers/hid/hid-haptic.c | 30 ++++++++++++++++--------------
1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index c20c14bdb3e8..503d332a14f8 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -382,6 +382,9 @@ static void hid_haptic_destroy(struct ff_device *ff)
struct hid_haptic_device *haptic = ff->private;
int r;

+ destroy_workqueue(haptic->wq);
+ haptic->wq = NULL;
+
kfree(haptic->stop_effect.report_buf);
haptic->stop_effect.report_buf = NULL;

@@ -392,9 +395,6 @@ static void hid_haptic_destroy(struct ff_device *ff)
}
haptic->effect = NULL;

- destroy_workqueue(haptic->wq);
- haptic->wq = NULL;
-
kfree(haptic->duration_map);
haptic->duration_map = NULL;

@@ -411,7 +411,6 @@ int hid_haptic_init(struct hid_device *hdev,
struct ff_haptic_effect stop_effect = {
.hid_usage = HID_HP_WAVEFORMSTOP & HID_USAGE,
};
- const char *prefix = "hid-haptic";

haptic->hdev = hdev;
haptic->max_waveform_id = max(2u, haptic->max_waveform_id);
@@ -446,15 +445,11 @@ int hid_haptic_init(struct hid_device *hdev,
haptic->manual_trigger_report_len =
hid_report_len(haptic->manual_trigger_report);
mutex_init(&haptic->manual_trigger_mutex);
- haptic->wq = alloc_ordered_workqueue("%s %s", 0, prefix, hdev->name);
- if (!haptic->wq) {
- ret = -ENOMEM;
- goto duration_map;
- }
+
haptic->effect = kzalloc_objs(struct hid_haptic_effect, FF_MAX_EFFECTS);
if (!haptic->effect) {
ret = -ENOMEM;
- goto output_queue;
+ goto duration_map;
}
for (r = 0; r < FF_MAX_EFFECTS; r++) {
haptic->effect[r].report_buf =
@@ -483,12 +478,18 @@ int hid_haptic_init(struct hid_device *hdev,
fill_effect_buf(haptic, &stop_effect, &haptic->stop_effect,
HID_HAPTIC_ORDINAL_WAVEFORMSTOP);

+ haptic->wq = alloc_ordered_workqueue("hid-haptic %s", 0, dev_name(&dev->dev));
+ if (!haptic->wq) {
+ ret = -ENOMEM;
+ goto stop_buffer_free;
+ }
+
input_set_capability(dev, EV_FF, FF_HAPTIC);

ret = input_ff_create(dev, FF_MAX_EFFECTS);
if (ret) {
dev_err(&hdev->dev, "Failed to create ff device.\n");
- goto stop_buffer_free;
+ goto reset_evbit;
}

ff = dev->ff;
@@ -499,6 +500,10 @@ int hid_haptic_init(struct hid_device *hdev,
ff->destroy = hid_haptic_destroy;
return 0;

+reset_evbit:
+ __clear_bit(EV_FF, dev->evbit);
+ destroy_workqueue(haptic->wq);
+ haptic->wq = NULL;
stop_buffer_free:
kfree(haptic->stop_effect.report_buf);
haptic->stop_effect.report_buf = NULL;
@@ -507,9 +512,6 @@ int hid_haptic_init(struct hid_device *hdev,
kfree(haptic->effect[r].report_buf);
kfree(haptic->effect);
haptic->effect = NULL;
-output_queue:
- destroy_workqueue(haptic->wq);
- haptic->wq = NULL;
duration_map:
kfree(haptic->duration_map);
haptic->duration_map = NULL;

--
2.55.0