From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1792531445; b=0rjRplRD9219fnQFoLTQHdRS6fuRd8MuoeruFTGGTuy0uRVnQeoffdWk/kaHWyUY6+27 HV96fvpj0c+hNenunM0BcaEPTQKCWsTH6lfsWC6oMx0h54ND0QDsnqAHwTNqJY/2432d4 7WISj+HCF+OSyHqgLXROcolyA4zSncvwOiQ5ExLYwlFOiKPQmCnwd2feFkU1ei2dDAPwT lMYXW1fqFSKn1yjivnpvDFNqqzvD8aDrlhiFAx2fjMz5T2H0RDIXRIOjy/vnvMZdiOfWO p0zLJtiZqN+1K45B6vJ565B5J5kaDx+D73jfp0qk4gvbgT6Lj8xnRsQGIygKLPJ9r2Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792531445; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=sEPH3pV5NQDGveQ4gCC4SZc339s/ZpKzGxrmka5jI0c=; b=eakHoUX9aGeUGs+FlNZr6ts/mtygoLVjyhU5vofpwtg4HDNNArEqaOU8QFpIL7Y7QOMA 35Vf/PRtEY0TjVSCTnUaZ7bE8sJhotT9DdawEh3IcsUMXdsqt8zJoqvgma+OedHwZa1eK bcn3Z1q6fUXNDZITTmMTcofGYuKJKEx5AmQGXW8ZMMTgk4gJXHy33Sj5ltN054ioSkYxj TtWPxfBQd+brsjqRxyRP8UOzvKAHgwP/hV69a7wzrEpiV34eCLltmUJNJzP+oTpkx8qfg IaqzGQYOpGiZ98YdCKSa5qTKF+Jp0clUsBG0PqIM3xlEoNaO42IQXmHv/TdtZyVxjcg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=CERZWXVl; arc=none smtp.remote-ip=74.125.82.174 Received: from mail-dy1-f174.google.com (mail-dy1-f174.google.com [74.125.82.174]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 549BB606A321 for ; Tue, 20 Oct 2026 21:24:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 549BB606A321 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=CERZWXVl Received: by mail-dy1-f174.google.com with SMTP id 5a478bee46e88-3282db206d3so66714eec.0 for ; Tue, 20 Oct 2026 14:24:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1792531443; x=1793136243; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=sEPH3pV5NQDGveQ4gCC4SZc339s/ZpKzGxrmka5jI0c=; b=CERZWXVllHDgB2fcpmh8y7vhIBMPAbPsGsIbfgFPEa8e6D6LrV/bdkdlDalk6DFzcC rNPDOX32FdjLJyJQybFpaZ4zNcirdj/R1EEDpivRB1lU0tuwle6TRpRjVgvpTOSjNpIc 7Xv/yMbAbUNpwuZKH+zQ1Ksn8NTk44mGemC1viFCEoEqk6bmlVekdAWpTs5v8+6oQLvF Esd28WoU2GhRHYIlV35rAEXRu0ZIHrhl+SdT1i17sYFwX/euER3OVFUytWQBvq49Ycjx RtsH/3rZVvXjH/IkXM9ykGPwm7m1GHDH/vh00NX01Q1oOKlXJYFLx+tbsmSFdoPWrWMJ RHQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792531443; x=1793136243; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=sEPH3pV5NQDGveQ4gCC4SZc339s/ZpKzGxrmka5jI0c=; b=t90Q08Zm9V/ISgqbULTZNn75her19zHkwwGKlNJj8Z0c0JBtWSi7GIIvihxHAsCtlu sLmovkBzZ0vybIacQ9P94IoFiLThspAXWZJcAYgxHp9qfvNZ5iO7e9iXXpCWHrRCoE0x f1bnlFAqiwYaFlZ5EAc94X2hKDnAob1PUuIQ+pouKiAePd/OoBlFepce44gxku3sE1q2 xUG/lY68MlmbBeWuV07eD5RSu/BbFuJfKzaz6AlEbKRilJnE173ewtbVR4XjqAeQA/au 6iX+QaDJ2NRmdyF76yLyyoLMn91FoTurHRC0JA3TFs5UM4Ro7V396p5jtKFHClK8Vq05 gkpA== X-Forwarded-Encrypted: i=1; AKwUvBxUSeKmVHOUdXhtCIhkCM/37PB6DWMQR2jggRM7ePzvjvcD8vFsUlyBuiURYeofUnHNsLooPhSABzkVaaI=@vger.kernel.org X-Gm-Message-State: AFq9FYKFGUXcpOPAarLXxM4angW36FW30J6IjhTor3x+tdkxbON1QFKH Pex/R6x2CDy2Xt25hVybMiz7Xo+/M7o2Vow6r2Sbuquvcs7Ok5YDvM/F X-Gm-Gg: AYBFou2kx4foHcvfUW2tKlEqJV4No/EJLh4lMb4pKDDjoPBdVWUj1t6pzDNYvyaFEn5 K0soTV8MekX7myfFBgLwBomhvZfypuyRX11Hh8JCQFz/qqq5g0FWRQ2NJguvP1F6D9JVqtfd7Ov CrazQnLoZjFazoRAZCHI6p4lqdVq+QplpaBQ7uzmSq2e4+4EO3DWP9w9G9aouUthP9hZxe5PmUV 1900iRv9YQnx7PLoDD1B8Lc7Vf43LxLHJ+YFNv7U+0WA2FG4x9NVvpmITUDN8hHFqMH7ulIU150 WXXFyHO6QQcEzQe74HD4Q3NVA55IvQdGyavrpp9BxsUd5j2tBxD4Db8pSI210RKBhkllFG848Fb LIlFtRkiKHsUGoh+MHOONE9LXQsVfkQd2KlBUpu4adBydKJ5o/DQwCzuvKlMiX0AJw/xTgApqde KkXFU/xkSlGllu4qrhMA+TczaPhkKu6P9IFDyl3TTDRq/7XfTUY2dB3JzKPXkzP3jdLJd64Nj53 1I68SkYbm+cZP/3D7Ehs44/30pK1MU= X-Received: by 2002:a05:693c:884f:b0:359:9e84:dc99 with SMTP id 5a478bee46e88-367474becaemr467629eec.0.1792531443223; Tue, 20 Oct 2026 14:24:03 -0700 (PDT) Received: from localhost.localdomain ([139.167.143.182]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-36741e71eaesm2003444eec.26.2026.10.20.14.23.58 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 20 Oct 2026 14:24:02 -0700 (PDT) From: Eklavya Singh To: Mark Fasheh , Joel Becker , Joseph Qi Cc: Heming Zhao , Andrew Morton , Deepanshu Kartikey , Cen Zhang , ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org, syzbot+5d4e96acd0febccb5e87@syzkaller.appspotmail.com, stable@vger.kernel.org Subject: [PATCH v3] ocfs2/heartbeat: serialize concurrent writes to a region's dev attribute Date: Wed, 21 Oct 2026 02:53:39 +0530 Message-ID: <20261020212339.46591-1-eklavyasingh2106@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20261019072936.2279-1-eklavyasingh2106@gmail.com> References: <20261019072936.2279-1-eklavyasingh2106@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit o2hb_region_dev_store() checks reg->hr_bdev_file to see if the region is already started, but nothing stops two writers from doing that at the same time. configfs only serializes writes through the same open file, so two processes that each open the dev attribute can both get past the check: A: hr_bdev_file is NULL, opens file A, sets hr_bdev_file = A B: hr_bdev_file is NULL, opens file B, sets hr_bdev_file = B A: start fails, fput(hr_bdev_file) drops B, sets it to NULL B: start fails, fput(NULL) Both calls also use the same hr_slots, hr_slot_data and hr_tmp_block, so the error paths can free the slot data twice. syzbot hit the fput(NULL) with an unbound loop device, where every start fails with -EIO. With four threads doing the same thing on a KASAN kernel I get both crashes within a second: BUG: KASAN: double-free in o2hb_unmap_slot_data+0x14d/0x2f0 BUG: kernel NULL pointer dereference, address: 0000000000000158 Add a mutex to the region and hold it in dev_store(), in the block_bytes, start_block and blocks stores, and in dev_show(), since they all look at hr_bdev_file. dev_store() cleans up with goto, so it takes the mutex in a small wrapper rather than with guard(). The mutex is dropped while dev_store() waits for the heartbeat to become steady, which takes seconds, so a read of dev doesn't stall behind a start. hr_bdev_file is already set at that point, so other writers still get -EINVAL, and the cleanup after the wait retakes the mutex. With the patch the same test ran for 10 minutes with lockdep on and nothing was reported. So did a second one, with a working loop device, that interrupts starts during the wait while other threads read and write the attributes. Fixes: a7f6a5fb4bde ("[PATCH] OCFS2: The Second Oracle Cluster Filesystem") Reported-by: syzbot+5d4e96acd0febccb5e87@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=5d4e96acd0febccb5e87 Cc: stable@vger.kernel.org Signed-off-by: Eklavya Singh --- Changes in v3: - rebase onto linux-next - drop hr_dev_mutex while waiting for the heartbeat to become steady, so a read of dev that races a start no longer blocks (Joseph) - name the four attributes in the hr_dev_mutex comment and assert the mutex is held in __o2hb_region_dev_store() (Joseph) Changes in v2: - take the mutex in a wrapper around dev_store() instead of with guard(), since dev_store() uses goto for its cleanup Deepanshu Kartikey [1] and Cen Zhang [2] have posted patches for the same race, and they conflict with this one. This version also takes the mutex in dev_show(), which otherwise can read hr_bdev_file while a failed start is dropping it. [1] https://lore.kernel.org/all/20261020085100.8819-1-kartikey406@gmail.com/ [2] https://lore.kernel.org/all/pm-ocfs2-objects-candidate-0077-v7-34980443ec254b2a1ffc@gmail.com/ v2: https://lore.kernel.org/all/20261019072936.2279-1-eklavyasingh2106@gmail.com/ v1: https://lore.kernel.org/all/20261018163121.128911-1-eklavyasingh2106@gmail.com/ fs/ocfs2/cluster/heartbeat.c | 40 ++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/fs/ocfs2/cluster/heartbeat.c b/fs/ocfs2/cluster/heartbeat.c index b45adc3..7a18e71 100644 --- a/fs/ocfs2/cluster/heartbeat.c +++ b/fs/ocfs2/cluster/heartbeat.c @@ -269,6 +269,8 @@ struct o2hb_region { struct list_head hr_handler_list; /* Serializes timeout arming against failed-start and teardown. */ struct mutex hr_arming_mutex; + /* Serializes the dev, block_bytes, start_block and blocks attributes. */ + struct mutex hr_dev_mutex; bool hr_stopping; /* last hb status, 0 for success, other value for error. */ @@ -1681,6 +1683,7 @@ static ssize_t o2hb_region_block_bytes_store(struct config_item *item, unsigned long block_bytes; unsigned int block_bits; + guard(mutex)(®->hr_dev_mutex); if (reg->hr_bdev_file) return -EINVAL; @@ -1710,6 +1713,7 @@ static ssize_t o2hb_region_start_block_store(struct config_item *item, char *p = (char *)page; ssize_t ret; + guard(mutex)(®->hr_dev_mutex); if (reg->hr_bdev_file) return -EINVAL; @@ -1736,6 +1740,7 @@ static ssize_t o2hb_region_blocks_store(struct config_item *item, char *p = (char *)page; int ret; + guard(mutex)(®->hr_dev_mutex); if (reg->hr_bdev_file) return -EINVAL; @@ -1753,10 +1758,12 @@ static ssize_t o2hb_region_blocks_store(struct config_item *item, static ssize_t o2hb_region_dev_show(struct config_item *item, char *page) { + struct o2hb_region *reg = to_o2hb_region(item); unsigned int ret = 0; - if (to_o2hb_region(item)->hr_bdev_file) - ret = sprintf(page, "%pg\n", reg_bdev(to_o2hb_region(item))); + guard(mutex)(®->hr_dev_mutex); + if (reg->hr_bdev_file) + ret = sprintf(page, "%pg\n", reg_bdev(reg)); return ret; } @@ -1872,11 +1879,9 @@ out: * this is acting as commit; we set up all of hr_bdev_file and hr_task or * nothing */ -static ssize_t o2hb_region_dev_store(struct config_item *item, - const char *page, - size_t count) +static ssize_t __o2hb_region_dev_store(struct o2hb_region *reg, + const char *page, size_t count) { - struct o2hb_region *reg = to_o2hb_region(item); struct task_struct *hb_task; long fd; int sectsize; @@ -1884,6 +1889,8 @@ static ssize_t o2hb_region_dev_store(struct config_item *item, ssize_t ret = -EINVAL; int live_threshold; + lockdep_assert_held(®->hr_dev_mutex); + if (reg->hr_bdev_file) return -EINVAL; @@ -1987,9 +1994,15 @@ static ssize_t o2hb_region_dev_store(struct config_item *item, wake_up_process(hb_task); + /* + * Don't hold hr_dev_mutex across the wait, which can take seconds. + * hr_bdev_file is already set, so other writers still get -EINVAL. + */ + mutex_unlock(®->hr_dev_mutex); ret = wait_event_interruptible(o2hb_steady_queue, atomic_read(®->hr_steady_iterations) == 0 || reg->hr_node_deleted); + mutex_lock(®->hr_dev_mutex); if (ret) { atomic_set(®->hr_steady_iterations, 0); reg->hr_aborted_start = 1; @@ -2041,6 +2054,20 @@ out: return ret; } +static ssize_t o2hb_region_dev_store(struct config_item *item, + const char *page, + size_t count) +{ + struct o2hb_region *reg = to_o2hb_region(item); + ssize_t ret; + + mutex_lock(®->hr_dev_mutex); + ret = __o2hb_region_dev_store(reg, page, count); + mutex_unlock(®->hr_dev_mutex); + + return ret; +} + static ssize_t o2hb_region_pid_show(struct config_item *item, char *page) { struct o2hb_region *reg = to_o2hb_region(item); @@ -2143,6 +2170,7 @@ static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *g INIT_LIST_HEAD(®->hr_all_item); mutex_init(®->hr_arming_mutex); + mutex_init(®->hr_dev_mutex); reg->hr_stopping = true; INIT_DELAYED_WORK(®->hr_write_timeout_work, o2hb_write_timeout); INIT_DELAYED_WORK(®->hr_nego_timeout_work, o2hb_nego_timeout); -- 2.52.0