Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH] HID: wacom: fix use-after-free of battery power supply from input path
@ 2026-11-30  1:54 Jaegyun Lee
  0 siblings, 0 replies; only message in thread
From: Jaegyun Lee @ 2026-11-30  1:54 UTC (permalink / raw)
  To: Ping Cheng, Jason Gerecke, Jiri Kosina, Benjamin Tissoires
  Cc: Jaegyun Lee, linux-input, Jinmo Yang, stable

__wacom_notify_battery() runs from the HID input report path and calls
power_supply_changed() on wacom_battery->battery without any locking.
wacom_destroy_battery() releases that supply with devres_release_group()
from wacom_battery_work(), wacom_aes_battery_handler() and
wacom_wireless_work(), and only then clears ->battery.  An input report
that arrives during the release makes power_supply_changed() touch freed
memory.

With CONFIG_DEBUG_OBJECTS_WORK=y on v7.4-rc5, the supply is freed with
its ->changed_work still queued:

[   22.884165] ODEBUG: free active (active state 0) object: (____ptrval____) object type: work_struct hint: power_supply_changed_work+0x0/0x290
[   22.885513] WARNING: lib/debugobjects.c:629 at debug_print_object+0x1a4/0x3b0, CPU#1: kworker/1:1/40
[   22.889350] CPU: 1 UID: 0 PID: 40 Comm: kworker/1:1 Not tainted 7.4.0-rc5 #20 PREEMPT
[   22.890096] Hardware name: QEMU Ubuntu 26.04 PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1ubuntu1 04/01/2014
[   22.891060] Workqueue: events wacom_battery_work
[   22.898926] Call Trace:
[   22.899671]  debug_check_no_obj_freed+0x496/0x5f0
[   22.902563]  kfree+0x2a8/0x5b0
[   22.903327]  device_release+0xd7/0x280
[   22.904590]  kobject_put+0x1b8/0x340
[   22.904971]  device_unregister+0x2c/0x40
[   22.905334]  release_nodes+0xbc/0x380
[   22.906171]  devres_release_group+0x1c7/0x2d0
[   22.907351]  wacom_battery_work+0xec/0x180
[   22.907714]  process_one_work+0xa81/0x1ac0
[   22.910051]  worker_thread+0x728/0x1060
[   22.910994]  kthread+0x3ac/0x4a0
[   22.911295]  ret_from_fork+0x7a9/0x9b0
[   22.913392]  ret_from_fork_asm+0x1a/0x30

Protect ->battery with a per-battery spinlock: the input path holds it
across power_supply_changed(), and teardown takes and clears the pointer
under it before releasing the supply.

Clearing ->battery first lets wacom_battery_work() open a new devres
group while another worker is still releasing the old one.  Both groups
are keyed by &battery->bat_desc and find_group() returns the newest, so
if the new group is opened before the teardown looks it up, the teardown
releases the new group and the creator's devres_close_group() hits the
BUG_ON() in add_dr() on the old, already closed group.  Serialize
creation against teardown with a dedicated mutex.  wacom->lock cannot be
used: power_supply_unregister() waits for the brightness work of any LED
bound to the supply's triggers, and wacom_led_brightness_set() takes
wacom->lock.

Do the same for the remote batteries.

Fixes: fce9957d8f61 ("HID: wacom: Allow dynamic battery creation/destruction")
Cc: stable@vger.kernel.org
Co-developed-by: Jinmo Yang <jinmo44.yang@gmail.com>
Signed-off-by: Jinmo Yang <jinmo44.yang@gmail.com>
Signed-off-by: Jaegyun Lee <dreams200260@gmail.com>
---
 drivers/hid/wacom.h     |  2 ++
 drivers/hid/wacom_sys.c | 47 +++++++++++++++++++++++++++++++++++------
 drivers/hid/wacom_wac.c |  3 +++
 3 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/drivers/hid/wacom.h b/drivers/hid/wacom.h
index d2398b1cdbb2..1f70b0ef55bc 100644
--- a/drivers/hid/wacom.h
+++ b/drivers/hid/wacom.h
@@ -132,6 +132,7 @@ struct wacom_battery {
 	struct wacom *wacom;
 	struct power_supply_desc bat_desc;
 	struct power_supply *battery;
+	spinlock_t battery_lock;
 	char bat_name[WACOM_NAME_MAX];
 	int bat_status;
 	int battery_capacity;
@@ -160,6 +161,7 @@ struct wacom {
 	struct wacom_wac wacom_wac;
 	struct hid_device *hdev;
 	struct mutex lock;
+	struct mutex battery_mutex;
 	struct work_struct wireless_work;
 	struct work_struct battery_work;
 	struct work_struct remote_work;
diff --git a/drivers/hid/wacom_sys.c b/drivers/hid/wacom_sys.c
index deed513bf5c0..8e52f035b8c2 100644
--- a/drivers/hid/wacom_sys.c
+++ b/drivers/hid/wacom_sys.c
@@ -1824,11 +1824,16 @@ static int __wacom_initialize_battery(struct wacom *wacom,
 	struct power_supply_config psy_cfg = { .drv_data = battery, };
 	struct power_supply *ps_bat;
 	struct power_supply_desc *bat_desc = &battery->bat_desc;
+	unsigned long flags;
 	unsigned long n;
 	int error;
 
-	if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
+	mutex_lock(&wacom->battery_mutex);
+
+	if (!devres_open_group(dev, bat_desc, GFP_KERNEL)) {
+		mutex_unlock(&wacom->battery_mutex);
 		return -ENOMEM;
+	}
 
 	battery->wacom = wacom;
 
@@ -1850,13 +1855,17 @@ static int __wacom_initialize_battery(struct wacom *wacom,
 
 	power_supply_powers(ps_bat, &wacom->hdev->dev);
 
+	spin_lock_irqsave(&battery->battery_lock, flags);
 	battery->battery = ps_bat;
+	spin_unlock_irqrestore(&battery->battery_lock, flags);
 
 	devres_close_group(dev, bat_desc);
+	mutex_unlock(&wacom->battery_mutex);
 	return 0;
 
 err:
 	devres_release_group(dev, bat_desc);
+	mutex_unlock(&wacom->battery_mutex);
 	return error;
 }
 
@@ -1870,11 +1879,21 @@ static int wacom_initialize_battery(struct wacom *wacom)
 
 static void wacom_destroy_battery(struct wacom *wacom)
 {
-	if (wacom->battery.battery) {
+	struct power_supply *battery;
+	unsigned long flags;
+
+	mutex_lock(&wacom->battery_mutex);
+
+	spin_lock_irqsave(&wacom->battery.battery_lock, flags);
+	battery = wacom->battery.battery;
+	wacom->battery.battery = NULL;
+	spin_unlock_irqrestore(&wacom->battery.battery_lock, flags);
+
+	if (battery)
 		devres_release_group(&wacom->hdev->dev,
 				     &wacom->battery.bat_desc);
-		wacom->battery.battery = NULL;
-	}
+
+	mutex_unlock(&wacom->battery_mutex);
 }
 
 static void wacom_aes_battery_handler(struct work_struct *work)
@@ -2062,6 +2081,9 @@ static int wacom_initialize_remotes(struct wacom *wacom)
 	if (!remote)
 		return -ENOMEM;
 
+	for (i = 0; i < WACOM_MAX_REMOTES; i++)
+		spin_lock_init(&remote->remotes[i].battery.battery_lock);
+
 	wacom->remote = remote;
 
 	spin_lock_init(&remote->remote_lock);
@@ -2692,13 +2714,24 @@ static void wacom_wireless_work(struct work_struct *work)
 static void wacom_remote_destroy_battery(struct wacom *wacom, int index)
 {
 	struct wacom_remote *remote = wacom->remote;
+	struct power_supply *battery;
+	unsigned long flags;
 
-	if (remote->remotes[index].battery.battery) {
+	mutex_lock(&wacom->battery_mutex);
+
+	spin_lock_irqsave(&remote->remotes[index].battery.battery_lock, flags);
+	battery = remote->remotes[index].battery.battery;
+	remote->remotes[index].battery.battery = NULL;
+	spin_unlock_irqrestore(&remote->remotes[index].battery.battery_lock,
+			       flags);
+
+	if (battery) {
 		devres_release_group(&wacom->hdev->dev,
 				     &remote->remotes[index].battery.bat_desc);
-		remote->remotes[index].battery.battery = NULL;
 		remote->remotes[index].active_time = 0;
 	}
+
+	mutex_unlock(&wacom->battery_mutex);
 }
 
 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
@@ -2959,6 +2992,8 @@ static int wacom_probe(struct hid_device *hdev,
 	}
 
 	mutex_init(&wacom->lock);
+	mutex_init(&wacom->battery_mutex);
+	spin_lock_init(&wacom->battery.battery_lock);
 	INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
 	INIT_DELAYED_WORK(&wacom->aes_battery_work, wacom_aes_battery_handler);
 	INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
diff --git a/drivers/hid/wacom_wac.c b/drivers/hid/wacom_wac.c
index ab89a11d6740..6066551aea09 100644
--- a/drivers/hid/wacom_wac.c
+++ b/drivers/hid/wacom_wac.c
@@ -95,6 +95,7 @@ static void __wacom_notify_battery(struct wacom_battery *battery,
 		       battery->bat_charging     != bat_charging  ||
 		       battery->bat_connected    != bat_connected ||
 		       battery->ps_connected     != ps_connected;
+	unsigned long flags;
 
 	if (changed) {
 		battery->bat_status = bat_status;
@@ -103,8 +104,10 @@ static void __wacom_notify_battery(struct wacom_battery *battery,
 		battery->bat_connected = bat_connected;
 		battery->ps_connected = ps_connected;
 
+		spin_lock_irqsave(&battery->battery_lock, flags);
 		if (battery->battery)
 			power_supply_changed(battery->battery);
+		spin_unlock_irqrestore(&battery->battery_lock, flags);
 	}
 }
 

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.53.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-30  1:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  1:54 [PATCH] HID: wacom: fix use-after-free of battery power supply from input path Jaegyun Lee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox