From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796387443; b=1JQWqrekNBz1BqcsoWq/uLKReZP+h9/kBXYGj7p3k4ieZQxv5ftDjSmqLq/JFIad1SLD 64Cwkh6Ezwh0S0hOA7F4G//lRDaQIeSw7JPV4HWnDpttyYeUIKWnb4k74aDJpgcN1ccmW ce65HZwybd9CRR5a9SUkzhM3jT20PogdCcB5Ieeeo3/XVaCf6flZEfs+S+pG7dcWQXdG9 hYPX11uTdGVK3kURuGECskhjAxazxSco441U6qyQjkSaOkLboQig8XvFcRn61anxlFm4g +oBBBThQSpTasDFHTSuaPqnARDVqd3mq9b7jvtaFGBvrmxTBzkcTsMPnuQ3yLhgrjtA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387443; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=j8v/ePltFPinGY+D7fn7jTgx2cpJuPfYMw2E5q4n9RM=; b=tOKvNNFUW68K0lddb7YShYTBMT81PuB8l552M9MTOFoX5vfVkIy7/ygisrF+RgqfpD1J N7hL3rkERYEifpcDbf56YG/ue7ICinE0qZbiiMfqQghdV7U25TMxQWUrJR8/9Euu8olqS LcqkT4f/Fw0GMkMDN210lUMW37phNeOJs1oBZD/7wfkdn/gQYhuqZzJAh3ZgIvz/PNG5S HpU9acq41AIPoyPhYZklMVijENWGbO4qaBeHaWf+oGnCrBxWPoMTK3ULShZVz6WiONxwB GN7AVjdldt2AaIgMVkfmmM8cIUdcCFf2mmhWZ1vm2gwhXzQ+OqMhhHJBUX5OSLAh0nA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=uEssSIOA; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F78E606A32C; Fri, 4 Dec 2026 12:30:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=uEssSIOA Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E2EC1F000C2; Fri, 4 Dec 2026 12:30:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387442; bh=j8v/ePltFPinGY+D7fn7jTgx2cpJuPfYMw2E5q4n9RM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=uEssSIOA/IQd8dfQdmcZKkvF77mis+0BNmtj5e3F/8PlYycvtXu6auCT3iG5Ao7Jz Q96996nP4cUw/7ZGD/SmHxiTNdIUH76NpQ6mOBUZYlYXyjfBQnIXJ9eKUIN5TjrlDd TXuvoKKm8HZ9qayoLQy0WyktJ+QXUo7iWPjADles= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Karl Mehltretter , Andrew Morton , Sashiko , Joseph Qi , Mark Fasheh , Joel Becker , Cen Zhang , Junxiao Bi , Heming Zhao , Sasha Levin Subject: [PATCH 6.1 0246/1059] ocfs2/cluster: hold a reference on the heartbeat thread Date: Fri, 4 Dec 2026 18:56:17 +0900 Message-ID: <20261204100650.647096538@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Karl Mehltretter [ Upstream commit aa2e38aabaae87236290e486a2b77cfa3adfbebe ] 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//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, 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(). Link: https://lore.kernel.org/20260923010149.14391-1-kmehltretter@gmail.com Fixes: 688bc88e2046 ("ocfs2/cluster: keep heartbeat local node stable") Signed-off-by: Karl Mehltretter Signed-off-by: Andrew Morton Reported-by: Sashiko Closes: https://sashiko.dev/#/patchset/20260616074931.3774929-1-zzzccc427%40gmail.com Reviewed-by: Joseph Qi Assisted-by: LLM Cc: Mark Fasheh Cc: Joel Becker Cc: Cen Zhang Cc: Junxiao Bi Cc: Heming Zhao [ sashal: Open-coded kthread_stop_put() as kthread_stop() plus put_task_struct() at both call sites, and placed wake_up_process() before this tree's existing hr_dev_mutex unlock. ] Signed-off-by: Sasha Levin --- fs/ocfs2/cluster/heartbeat.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/fs/ocfs2/cluster/heartbeat.c b/fs/ocfs2/cluster/heartbeat.c index e641c9c415532..12f87ee72a852 100644 --- a/fs/ocfs2/cluster/heartbeat.c +++ b/fs/ocfs2/cluster/heartbeat.c @@ -1955,18 +1955,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 out3; } + /* 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); + /* * Don't hold hr_dev_mutex across the wait, which can take seconds. * hr_bdev is already set, so other writers still get -EINVAL. @@ -2016,8 +2020,10 @@ static ssize_t o2hb_region_dev_store(struct config_item *item, reg->hr_task = NULL; spin_unlock(&o2hb_live_lock); - if (hb_task) + if (hb_task) { kthread_stop(hb_task); + put_task_struct(hb_task); + } o2hb_unmap_slot_data(reg); @@ -2215,8 +2221,10 @@ static void o2hb_heartbeat_group_drop_item(struct config_group *group, reg->hr_item_dropped = 1; spin_unlock(&o2hb_live_lock); - if (hb_task) + if (hb_task) { kthread_stop(hb_task); + put_task_struct(hb_task); + } if (o2hb_global_heartbeat_active()) { spin_lock(&o2hb_live_lock); -- 2.53.0