From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40AFD2FDC20 for ; Fri, 10 Apr 2026 03:42:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775792559; cv=none; b=qB39YPTI0E8PD/wX6wXqdTppCVH3jVmT+M1PqXxJb6oW0hI5HnyDOV3EAlox/y0pea4p8nhElAuIUVzAOU/gc1GxwNxFbVurDKpzqmrgzz0O/WwuJAtWNT/lfdw1KpGAHQhkKOFpyGQab7HjSpAtWV1kZyucPB2U/RBRnlpG2mI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775792559; c=relaxed/simple; bh=vOmolxgnRPkdtUJhQdO+bE/862+kcsqBFsf4bD/Oqw8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=JEmnU4QrJ6ThNxm/yoMB3y66v+7ODW/vxQGJ8YLCqZxzeqLGr5pvfwAeLiLHen9b8MFoSVdfJ32DoAQbwtcq5KlH3mKquA10BaayzPssqafaQkwZizk05rUAelQLFf1SqMCBUrJ/anxziS3MBQUzkw+VoR7y0xKVv/d9oezydUw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JZ4GXGlu; arc=none smtp.client-ip=209.85.210.169 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) header.d=gmail.com header.i=@gmail.com header.b="JZ4GXGlu" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-82a7ebc729dso825158b3a.3 for ; Thu, 09 Apr 2026 20:42:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775792557; x=1776397357; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Nmono6Lc90LMGK6WwMTdfcyRYLVposTEQlZ1VkUaDkE=; b=JZ4GXGluYmZOFBbAZuWATwTFT+sSqF439uh3b+Fr7E1V57GO5FObp2vRaShOyWcN+4 W0Bz4EWmLGav3u55MoAuxVT/xt0PCBv9mf2wcSu0tHe8BQzxSt85H60FVeUm3jxTSAdh Iy2LHAO7eaa2ruxGR+wRnWSGW9Bo+771im6HYbrusL7DHZuo0Ywubze+D4UwLxQSO6T1 d+yRwd9Vupl+UkC0GOHCytuOtSssKZtJNUN1VYKQFnOnRdDFiPYHU+mE4lTpuGRZQWrf Zu4A8905L5hp/2c9zgF8XyUHsrQHoC5H/banANwxmxEa3k7jX1z+ByaZocQHiLrxNnNh uvXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775792557; x=1776397357; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Nmono6Lc90LMGK6WwMTdfcyRYLVposTEQlZ1VkUaDkE=; b=mIzcQkVO2G4ba1mCV/xbNhnlc+OjJUQ32NF4Wtwshdv33z0t5bPT+yu7wu5zlP7D4/ Cgz56G3k5SqIwTjaUNmbECcxYm4Z7sq4v/Vpz3eaisKQYGdgcbKH5JwnmtHoQCidREoK mW9dCOlmDyFQhXsj9HUi3GCk6VT4z27wEwbXNOgDiazGBjvp/bLjOBcfi0jd5X3MT+Gf PRzgC7b53LJPLtOD7FQSiPBENJdU0NB7a8pDRSmAv51cE0eBCjAcpzIEmXt3D9HScTli HJgflWM+5DJ83xN/s0Y3MGWZXJlE1Trq/dvcfEQh6uEibCHm6YMOlBRKKsnDr45C1oWT QmOA== X-Forwarded-Encrypted: i=1; AJvYcCVaELCkR1qCGyrvzw9vzkXwxfR2g0ZluAidpjL2WTh4p2PV6J/M/yVUrMusvpQbA3kG3i+qem3eb0lWeh4=@vger.kernel.org X-Gm-Message-State: AOJu0YxW13HRPzjZjRbYazahH+EDK6kg4vPcWqpDYQzxlzztebANMB+y fVhi9nxY4/D+ksieeUFnHChOw3tAFxha4u/OZ+pVaAKa/5d+kt+4Jpn8 X-Gm-Gg: AeBDieuEnuIvJGpSfaZL46stNMW77nX0kc0ZmR5w69vf7IlSswPcpaN7/gP77dYOAAO 8JIVUpGqi+VMTaYtzdVcJj4jJ5hMN4b1DFuTzKpmFddfSpfy8Q6UbCAerwCVjpgmoGrR6TObsFh gnsS+jELmF7REhUiPgRtdn994mbhoAcHNaihkIKJyOWu5K3RkJusGK+qtsshuoxVLSVGSG3J8R3 AmOtShtWu8NAEyoszd/0/ts9BWC10iSXOOH0myS5/ForIIhk+g1tkerk+gDR29ynhaisRQjJKAq +JbZMuAPpbHlmnvJmLpOTyJOtwhOVWfzTMIWmLLkjH3YO+xHkY0UafGCqV4uk0Sux+2/2bCLJka LM/8Ndkynd9E592nYBqoMpZrwESnU38l13NZxXW7bRqHfxJYKHXFM0gi6eHbixHOIX1BP25ki4b hptjXPefv+kQdhSU+deRZNtt7cUm6XNGnRDe05IEE2ZAOf+8/rMeZkGXS2whQ5HWvq/ME3pA== X-Received: by 2002:a05:6a00:4b43:b0:820:2f9b:fe31 with SMTP id d2e1a72fcca58-82f0c221ac1mr2007721b3a.30.1775792557394; Thu, 09 Apr 2026 20:42:37 -0700 (PDT) Received: from kernel-fuzz.. ([138.199.21.245]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82f0c32e744sm1156784b3a.13.2026.04.09.20.42.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Apr 2026 20:42:36 -0700 (PDT) From: ZhengYuan Huang To: mark@fasheh.com, jlbec@evilplan.org, joseph.qi@linux.alibaba.com Cc: ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org, baijiaju1990@gmail.com, r33s3n6@gmail.com, zzzccc427@gmail.com, ZhengYuan Huang Subject: [PATCH v5] ocfs2: validate bg_bits during freefrag scan Date: Fri, 10 Apr 2026 11:42:20 +0800 Message-ID: <20260410034220.3825769-1-gality369@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit [BUG] A crafted filesystem can trigger an out-of-bounds bitmap walk when OCFS2_IOC_INFO is issued with OCFS2_INFO_FL_NON_COHERENT. BUG: KASAN: use-after-free in instrument_atomic_read include/linux/instrumented.h:68 [inline] BUG: KASAN: use-after-free in _test_bit include/asm-generic/bitops/instrumented-non-atomic.h:141 [inline] BUG: KASAN: use-after-free in test_bit_le include/asm-generic/bitops/le.h:21 [inline] BUG: KASAN: use-after-free in ocfs2_info_freefrag_scan_chain fs/ocfs2/ioctl.c:495 [inline] BUG: KASAN: use-after-free in ocfs2_info_freefrag_scan_bitmap fs/ocfs2/ioctl.c:588 [inline] BUG: KASAN: use-after-free in ocfs2_info_handle_freefrag fs/ocfs2/ioctl.c:662 [inline] BUG: KASAN: use-after-free in ocfs2_info_handle_request+0x1c66/0x3370 fs/ocfs2/ioctl.c:754 Read of size 8 at addr ffff888031bce000 by task syz.0.636/1435 Call Trace: __dump_stack lib/dump_stack.c:94 [inline] dump_stack_lvl+0xbe/0x130 lib/dump_stack.c:120 print_address_description mm/kasan/report.c:378 [inline] print_report+0xd1/0x650 mm/kasan/report.c:482 kasan_report+0xfb/0x140 mm/kasan/report.c:595 check_region_inline mm/kasan/generic.c:186 [inline] kasan_check_range+0x11c/0x200 mm/kasan/generic.c:200 __kasan_check_read+0x11/0x20 mm/kasan/shadow.c:31 instrument_atomic_read include/linux/instrumented.h:68 [inline] _test_bit include/asm-generic/bitops/instrumented-non-atomic.h:141 [inline] test_bit_le include/asm-generic/bitops/le.h:21 [inline] ocfs2_info_freefrag_scan_chain fs/ocfs2/ioctl.c:495 [inline] ocfs2_info_freefrag_scan_bitmap fs/ocfs2/ioctl.c:588 [inline] ocfs2_info_handle_freefrag fs/ocfs2/ioctl.c:662 [inline] ocfs2_info_handle_request+0x1c66/0x3370 fs/ocfs2/ioctl.c:754 ocfs2_info_handle+0x18d/0x2a0 fs/ocfs2/ioctl.c:828 ocfs2_ioctl+0x632/0x6e0 fs/ocfs2/ioctl.c:913 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:597 [inline] __se_sys_ioctl fs/ioctl.c:583 [inline] __x64_sys_ioctl+0x197/0x1e0 fs/ioctl.c:583 ... [CAUSE] ocfs2_info_freefrag_scan_chain() uses on-disk bg_bits directly as the bitmap scan limit. The coherent path reads group descriptors through ocfs2_read_group_descriptor(), which validates the descriptor before use. The non-coherent path uses ocfs2_read_blocks_sync() instead and skips that validation, so an impossible bg_bits value can drive the bitmap walk past the end of the block. [FIX] Compute the bitmap capacity from the filesystem format with ocfs2_group_bitmap_size(), report descriptors whose bg_bits exceeds that limit, and clamp the scan to the computed capacity. This keeps the freefrag report going while avoiding reads beyond the buffer. Fixes: d24a10b9f8ed ("Ocfs2: Add a new code 'OCFS2_INFO_FREEFRAG' for o2info ioctl.") Signed-off-by: ZhengYuan Huang --- v5: - move max_bitmap_bits computation out of the group loop - restore the untouched blank line after assigning bg v4: - add the freefrag introduction commit as a Fixes tag - use cluster allocator bitmap sizing with ocfs2_group_bitmap_size(..., 0, ...) - drop the unnecessary 8U suffix and tighten the mlog() formatting v3: - restore the empty-group fast path before computing bitmap limits - move the bg_bits clamp below bg_free_bits_count to skip extra work for empty groups v2: - use ocfs2_group_bitmap_size() instead of the on-disk bg_size field - clamp bg_bits to the computed bitmap capacity and continue scanning --- fs/ocfs2/ioctl.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/fs/ocfs2/ioctl.c b/fs/ocfs2/ioctl.c index b6864602814c..10f573a8b6b6 100644 --- a/fs/ocfs2/ioctl.c +++ b/fs/ocfs2/ioctl.c @@ -441,13 +441,16 @@ static int ocfs2_info_freefrag_scan_chain(struct ocfs2_super *osb, struct buffer_head *bh = NULL; struct ocfs2_group_desc *bg = NULL; - unsigned int max_bits, num_clusters; + unsigned int max_bits, max_bitmap_bits, num_clusters; unsigned int offset = 0, cluster, chunk; unsigned int chunk_free, last_chunksize = 0; if (!le32_to_cpu(rec->c_free)) goto bail; + max_bitmap_bits = 8 * ocfs2_group_bitmap_size(osb->sb, 0, + osb->s_feature_incompat); + do { if (!bg) blkno = le64_to_cpu(rec->c_blkno); @@ -479,6 +482,19 @@ static int ocfs2_info_freefrag_scan_chain(struct ocfs2_super *osb, continue; max_bits = le16_to_cpu(bg->bg_bits); + + /* + * Non-coherent scans read raw blocks and do not get the + * bg_bits validation from + * ocfs2_read_group_descriptor(). + */ + if (max_bits > max_bitmap_bits) { + mlog(ML_ERROR, + "Group desc #%llu has %u bits, max bitmap bits %u\n", + (unsigned long long)blkno, max_bits, max_bitmap_bits); + max_bits = max_bitmap_bits; + } + offset = 0; for (chunk = 0; chunk < chunks_in_group; chunk++) { -- 2.43.0