This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 8ed8dd84ffd power/battery: cancel pending work before freeing priv on 
close
8ed8dd84ffd is described below

commit 8ed8dd84ffd4834eb0b6074ebaf4da34e8535f7d
Author: v-liuchenxu3 <[email protected]>
AuthorDate: Tue May 19 11:31:31 2026 +0800

    power/battery: cancel pending work before freeing priv on close
    
    battery_charger_work and battery_gauge_work may still be queued on
    LPWORK when the fd is closed. bat_charger_close and bat_gauge_close
    free priv without canceling pending work, causing use-after-free when
    the work callback later dereferences the stale priv->fds pointer.
    
    Add work_cancel_sync before kmm_free to ensure no pending work
    references the freed priv structure.
    
    Signed-off-by: v-liuchenxu3 <[email protected]>
---
 drivers/power/battery/battery_charger.c | 1 +
 drivers/power/battery/battery_gauge.c   | 1 +
 drivers/power/battery/battery_monitor.c | 1 +
 3 files changed, 3 insertions(+)

diff --git a/drivers/power/battery/battery_charger.c 
b/drivers/power/battery/battery_charger.c
index a0fa3a45b12..d1ecfeac5c0 100644
--- a/drivers/power/battery/battery_charger.c
+++ b/drivers/power/battery/battery_charger.c
@@ -212,6 +212,7 @@ static int bat_charger_close(FAR struct file *filep)
 
   list_delete(&priv->node);
   nxmutex_unlock(&dev->batlock);
+  work_cancel_sync(LPWORK, &priv->work);
   nxmutex_destroy(&priv->lock);
   nxsem_destroy(&priv->wait);
   kmm_free(priv);
diff --git a/drivers/power/battery/battery_gauge.c 
b/drivers/power/battery/battery_gauge.c
index ab80f1e956c..84b046eb036 100644
--- a/drivers/power/battery/battery_gauge.c
+++ b/drivers/power/battery/battery_gauge.c
@@ -214,6 +214,7 @@ static int bat_gauge_close(FAR struct file *filep)
 
   list_delete(&priv->node);
   nxmutex_unlock(&dev->batlock);
+  work_cancel_sync(LPWORK, &priv->work);
   nxmutex_destroy(&priv->lock);
   nxsem_destroy(&priv->wait);
   kmm_free(priv);
diff --git a/drivers/power/battery/battery_monitor.c 
b/drivers/power/battery/battery_monitor.c
index 4ec6cd08796..d4f80271818 100644
--- a/drivers/power/battery/battery_monitor.c
+++ b/drivers/power/battery/battery_monitor.c
@@ -213,6 +213,7 @@ static int bat_monitor_close(FAR struct file *filep)
 
   list_delete(&priv->node);
   nxmutex_unlock(&dev->batlock);
+  work_cancel_sync(LPWORK, &priv->work);
   nxmutex_destroy(&priv->lock);
   nxsem_destroy(&priv->wait);
   kmm_free(priv);

Reply via email to