[PATCH 1/6] power: supply: max77759: fix use-after-free of tcpm_psy
From: André Draszik
Date: Fri Nov 27 2026 - 14:10:07 EST
Unbinding the TCPM supplier triggers a use-after-free oops in
psy_work_item():
max77759-charger max77759-charger.1.auto: Failed to get
CURRENT_MAX psy property, ret=-19
Internal error: Oops - BRK: 00000000f20003e8 [#1] SMP
CPU: 6 UID: 0 PID: 96 Comm: kworker/6:2 Tainted: G U
Hardware name: Oriole (DT)
Workqueue: events psy_work_item [max77759_charger]
pc : queued_spin_lock_slowpath+0x388/0x3c0
lr : queued_spin_lock_slowpath+0x120/0x3c0
psy_changed() caches the raw struct power_supply * in chg->tcpm_psy
without holding a reference. When the TCPM supply unbinds,
psy_work_item() gets -ENODEV, schedules a 10 ms retry, and dereferences
the freed chg->tcpm_psy pointer.
Read ONLINE and CURRENT_MAX directly inside psy_changed() where that's
safe and store the plain integer values for psy_work_item() while
dropping max77759_charger::tcpm_psy.
On read error, fall back to offline (0) so psy_work_item() disables
charging without retrying against a disappearing supply. This is fine,
because the only in-tree provider matching "tcpm-source" is tcpm, which
returns cached fields from memory. Charger I2C retries on write error
remain unchanged.
Fixes: 70d7dd27f6dc ("power: supply: max77759: add charger driver")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: André Draszik <andre.draszik@xxxxxxxxxx>
---
drivers/power/supply/max77759_charger.c | 55 ++++++++++++++++++---------------
1 file changed, 30 insertions(+), 25 deletions(-)
diff --git a/drivers/power/supply/max77759_charger.c b/drivers/power/supply/max77759_charger.c
index e792327ff14f..a75005c0b582 100644
--- a/drivers/power/supply/max77759_charger.c
+++ b/drivers/power/supply/max77759_charger.c
@@ -77,9 +77,10 @@ struct max77759_charger {
struct power_supply *psy;
struct regulator_dev *chgin_otg_rdev;
struct notifier_block nb;
- struct power_supply *tcpm_psy;
struct delayed_work psy_work;
- struct mutex retry_lock; /* Protects psy_work_retry_cnt */
+ struct mutex retry_lock; /* Protects psy_work state below */
+ int tcpm_current_limit;
+ int tcpm_online;
u32 psy_work_retry_cnt;
int irqs[NUM_IRQS];
struct mutex lock; /* protects the state below */
@@ -595,30 +596,15 @@ static void psy_work_item(struct work_struct *work)
{
struct max77759_charger *chg =
container_of(work, struct max77759_charger, psy_work.work);
- union power_supply_propval current_limit, online;
- int ret;
-
- ret = power_supply_get_property(chg->tcpm_psy,
- POWER_SUPPLY_PROP_CURRENT_MAX,
- ¤t_limit);
- if (ret) {
- dev_err(chg->dev,
- "Failed to get CURRENT_MAX psy property, ret=%d\n",
- ret);
- goto err;
- }
+ int current_limit, online, ret;
- ret = power_supply_get_property(chg->tcpm_psy, POWER_SUPPLY_PROP_ONLINE,
- &online);
- if (ret) {
- dev_err(chg->dev,
- "Failed to get ONLINE psy property, ret=%d\n",
- ret);
- goto err;
+ scoped_guard(mutex, &chg->retry_lock) {
+ current_limit = chg->tcpm_current_limit;
+ online = chg->tcpm_online;
}
- if (online.intval && current_limit.intval) {
- ret = set_input_current_limit(chg, current_limit.intval);
+ if (online && current_limit) {
+ ret = set_input_current_limit(chg, current_limit);
if (ret) {
dev_err(chg->dev,
"Unable to set current limit, ret=%d\n", ret);
@@ -660,16 +646,35 @@ static int psy_changed(struct notifier_block *nb, unsigned long evt, void *data)
{
struct max77759_charger *chg = container_of(nb, struct max77759_charger,
nb);
+ union power_supply_propval current_limit, online;
static const char *psy_name = "tcpm-source";
struct power_supply *psy = data;
+ int ret;
if (!strnstr(psy->desc->name, psy_name, strlen(psy_name)) ||
evt != PSY_EVENT_PROP_CHANGED)
return NOTIFY_OK;
- chg->tcpm_psy = psy;
- scoped_guard(mutex, &chg->retry_lock)
+ ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
+ &online);
+ if (!ret && online.intval)
+ ret = power_supply_get_property(psy,
+ POWER_SUPPLY_PROP_CURRENT_MAX,
+ ¤t_limit);
+ if (ret) {
+ if (ret != -ENODEV)
+ dev_err(chg->dev,
+ "Failed to get supplier psy property: %d\n",
+ ret);
+ online.intval = 0;
+ }
+
+ scoped_guard(mutex, &chg->retry_lock) {
+ chg->tcpm_online = online.intval;
+ chg->tcpm_current_limit = online.intval ?
+ current_limit.intval : 0;
chg->psy_work_retry_cnt = 0;
+ }
schedule_delayed_work(&chg->psy_work, 0);
--
2.56.1.839.g22a8290db9-goog