ocfs2/cluster: hold a reference on the heartbeat thread
PATCH 1/1 ocfs2/cluster: hold a reference on the heartbeat thread
Since commit 688bc88e2046 ("ocfs2/cluster: keep heartbeat local node stable"), o2hb_thread() leaves its loop and returns when the local node changes, for example after "echo 0 > node/<name>/local". The thread was started with kthread_run() and nothing holds a reference to its task_struct, so the task is freed once it exits, while reg->hr_task still points to it.
Reading the region's pid attribute then reads the freed task, and removing the region calls kthread_stop() on it:
BUG: KASAN: slab-use-after-free in o2hb_region_pid_show+0xb3/0xc0 refcount_t: addition on 0; use-after-free. Oops: Oops: 0000 [#1] SMP KASAN NOPTI RIP: 0010:kthread_stop+0xb1/0x390
The thread could already return by itself before, when heartbeat start was aborted or on an unclean stop, but the local node change makes it reachable from userspace at any time.
Create the thread parked, take a reference on it and only then wake it, so the reference cannot race with the thread exiting. Drop it with kthread_stop_put().
---
Reproduced under QEMU x86_64 with KASAN, using one node over configfs and a loop device as the heartbeat region:
echo 0 > cluster/c1/node/n0/local # heartbeat thread returns cat cluster/c1/heartbeat/<uuid>/pid # reads the freed task_struct rmdir cluster/c1/heartbeat/<uuid> # kthread_stop() on it
With the patch, that sequence, a normal region removal and a start interrupted by a signal ran clean on two CPUs, five rounds each, and every heartbeat task_struct was freed.
fs/ocfs2/cluster/heartbeat.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
fs/ocfs2/cluster/heartbeat.c +8 -4
@@ -1966,18 +1966,22 @@ static ssize_t o2hb_region_dev_store(struct config_item *item,atomic_set (®->hr_unsteady_iterations, (live_threshold *3 ));o2hb_set_region_stopping (reg, false); - hb_task =kthread_run (o2hb_thread, reg,"o2hb-%s" , - reg->hr_item.ci_name); + hb_task =kthread_create (o2hb_thread, reg,"o2hb-%s" , + reg->hr_item.ci_name);if (IS_ERR (hb_task)) { ret =PTR_ERR (hb_task);mlog_errno (ret); goto out; } +/* The thread may exit on its own, so pin it before it can run. */ +get_task_struct (hb_task);spin_lock (&o2hb_live_lock); reg->hr_task = hb_task;spin_unlock (&o2hb_live_lock); +wake_up_process (hb_task); + ret =wait_event_interruptible (o2hb_steady_queue,atomic_read (®->hr_steady_iterations) ==0 || reg->hr_node_deleted);
@@ -2022,7 +2026,7 @@ static ssize_t o2hb_region_dev_store(struct config_item *item,spin_unlock (&o2hb_live_lock);if (hb_task) -kthread_stop (hb_task); +kthread_stop_put (hb_task);o2hb_unmap_slot_data (reg);
@@ -2208,7 +2212,7 @@ static void o2hb_heartbeat_group_drop_item(struct config_group *group,spin_unlock (&o2hb_live_lock);if (hb_task) -kthread_stop (hb_task); +kthread_stop_put (hb_task);if (o2hb_global_heartbeat_active ()) {spin_lock (&o2hb_live_lock);
Signature · 1 line
2.53.0
Quoted 15 lines · Karl Mehltretter
Since commit
688bc88e2046("ocfs2/cluster: keep heartbeat local node stable"),o2hb_thread()leaves its loop and returns when the local node changes, for example after "echo 0 > node/<name>/local". The thread was started withkthread_run()and nothing holds a reference to itstask_struct, so the task is freed once it exits, whilereg->hr_taskstill points to it.Reading the region's pid attribute then reads the freed task, and removing the region calls
kthread_stop()on it:BUG: KASAN: slab-use-after-free in o2hb_region_pid_show+0xb3/0xc0 refcount_t: addition on 0; use-after-free. Oops: Oops: 0000 [#1] SMP KASAN NOPTI RIP: 0010:kthread_stop+0xb1/0x390
Quoted 5 lines
The thread could already return by itself before, when heartbeat start was aborted or on an unclean stop, but the local node change makes it reachable from userspace at any time.
Create the thread parked, take a reference on it and only then wake it,
Just a wording nit. kthread_create() doesn't leave the thread parked in the kthread_park() sense. __kthread_parkme() only runs after the first wake_up_process(), so "parked" here may be a little confused.
Quoted 8 lines
so the reference cannot race with the thread exiting. Drop it with
kthread_stop_put().Fixes 688bc88e2046 ("ocfs2/cluster: keep heartbeat local node stable")Reported-by Sashiko <[email protected]>Assisted-by LLMSigned-off-by Karl Mehltretter <[email protected]>
The change looks fine to me.
Quoted 62 lines
---
Reproduced under QEMUx86_64with KASAN, using one node over configfs and a loop device as the heartbeat region:echo 0 > cluster/c1/node/n0/local # heartbeat thread returns cat cluster/c1/heartbeat/<uuid>/pid # reads the freed task_struct rmdir cluster/c1/heartbeat/<uuid> # kthread_stop() on itWith the patch, that sequence, a normal region removal and a start interrupted by a signal ran clean on two CPUs, five rounds each, and every heartbeat
task_structwas freed.fs/ocfs2/cluster/heartbeat.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)fs/ocfs2/cluster/heartbeat.c +8
-4@@ -1966,18 +1966,22 @@ static ssize_t o2hb_region_dev_store(struct config_item *item,atomic_set (®->hr_unsteady_iterations, (live_threshold *3 ));o2hb_set_region_stopping (reg, false); - hb_task =kthread_run (o2hb_thread, reg,"o2hb-%s" , - reg->hr_item.ci_name); + hb_task =kthread_create (o2hb_thread, reg,"o2hb-%s" , + reg->hr_item.ci_name);if (IS_ERR (hb_task)) { ret =PTR_ERR (hb_task);mlog_errno (ret); goto out; } +/* The thread may exit on its own, so pin it before it can run. */ +get_task_struct (hb_task);spin_lock (&o2hb_live_lock); reg->hr_task = hb_task;spin_unlock (&o2hb_live_lock); +wake_up_process (hb_task); + ret =wait_event_interruptible (o2hb_steady_queue,atomic_read (®->hr_steady_iterations) ==0 || reg->hr_node_deleted);@@ -2022,7 +2026,7 @@ static ssize_t o2hb_region_dev_store(struct config_item *item,spin_unlock (&o2hb_live_lock);if (hb_task) -kthread_stop (hb_task); +kthread_stop_put (hb_task);o2hb_unmap_slot_data (reg);@@ -2208,7 +2212,7 @@ static void o2hb_heartbeat_group_drop_item(struct config_group *group,spin_unlock (&o2hb_live_lock);if (hb_task) -kthread_stop (hb_task); +kthread_stop_put (hb_task);if (o2hb_global_heartbeat_active ()) {spin_lock (&o2hb_live_lock);
Quoted 22 lines · On Wed, 23 Sep 2026 10:45:10 +0800 Joseph Qi <[email protected]>…
Quoted 19 lines · Karl Mehltretter
Since commit
688bc88e2046("ocfs2/cluster: keep heartbeat local node stable"),o2hb_thread()leaves its loop and returns when the local node changes, for example after "echo 0 > node/<name>/local". The thread was started withkthread_run()and nothing holds a reference to itstask_struct, so the task is freed once it exits, whilereg->hr_taskstill points to it.Reading the region's pid attribute then reads the freed task, and removing the region calls
kthread_stop()on it:BUG: KASAN: slab-use-after-free in o2hb_region_pid_show+0xb3/0xc0 refcount_t: addition on 0; use-after-free. Oops: Oops: 0000 [#1] SMP KASAN NOPTI RIP: 0010:kthread_stop+0xb1/0x390The thread could already return by itself before, when heartbeat start was aborted or on an unclean stop, but the local node change makes it reachable from userspace at any time.
Quoted 5 lines
Quoted 1 line
Create the thread parked, take a reference on it and only then wake it,
Just a wording nit.
kthread_create()doesn't leave the thread parked in thekthread_park()sense.__kthread_parkme()only runs after the firstwake_up_process(), so "parked" here may be a little confused.
I simply did s/parked //.
Quoted 2 lines
The change looks fine to me.
Reviewed-by Joseph Qi <[email protected]>
Thanks as always.