]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
batman-adv: fix tp_meter counter underflow during shutdown
authorLuxiao Xu <rakukuip@gmail.com>
Mon, 11 May 2026 16:52:09 +0000 (18:52 +0200)
committerSven Eckelmann <sven@narfation.org>
Mon, 11 May 2026 17:52:40 +0000 (19:52 +0200)
batadv_tp_sender_shutdown() unconditionally decrements the "sending"
atomic counter. If multiple paths (e.g. timeout, user cancel, and
normal finish) call this function, the counter can underflow to -1.

Since the sender logic treats any non-zero value as "still sending",
a negative value causes the sender kthread to loop indefinitely.
This leads to a use-after-free when the interface is removed while
the zombie thread is still active.

Fix this by using atomic_xchg() to ensure the counter only transitions
from 1 to 0 once.

Fixes: 33a3bb4a3345 ("batman-adv: throughput meter implementation")
Cc: stable@kernel.org
Reported-by: Yuan Tan <yuantan098@gmail.com>
Reported-by: Yifan Wu <yifanwucs@gmail.com>
Reported-by: Juefei Pu <tomapufckgml@gmail.com>
Reported-by: Xin Liu <bird@lzu.edu.cn>
Signed-off-by: Luxiao Xu <rakukuip@gmail.com>
Signed-off-by: Ren Wei <n05ec@lzu.edu.cn>
[sven: added missing change in batadv_tp_send]
Signed-off-by: Sven Eckelmann <sven@narfation.org>
net/batman-adv/tp_meter.c

index 066c76113fc4336a2acebd6935aa9b8b7af64926..a4397aa881dd44a89d7b139e12661da1cf182df5 100644 (file)
@@ -451,7 +451,7 @@ static void batadv_tp_sender_end(struct batadv_priv *bat_priv,
 static void batadv_tp_sender_shutdown(struct batadv_tp_vars *tp_vars,
                                      enum batadv_tp_meter_reason reason)
 {
-       if (!atomic_dec_and_test(&tp_vars->sending))
+       if (atomic_xchg(&tp_vars->sending, 0) != 1)
                return;
 
        tp_vars->reason = reason;
@@ -885,7 +885,7 @@ static int batadv_tp_send(void *arg)
                                   "Meter: %s() cannot send packets (%d)\n",
                                   __func__, err);
                        /* ensure nobody else tries to stop the thread now */
-                       if (atomic_dec_and_test(&tp_vars->sending))
+                       if (atomic_xchg(&tp_vars->sending, 0) == 1)
                                tp_vars->reason = err;
                        break;
                }