From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 D95B83939DA for ; Tue, 11 Aug 2026 02:43:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786416239; cv=none; b=B9nXyB3BAkYLYK4/8D4UWCcQLTNd6fAeYbF0ok9Uye1jIUEd4RtVovWa9wYsC40EJiVnkB/s7zQDi766zrq/SrErqf4fb3zCT4coK05ZLU9S/BD9EeCiTFCLvynaaIZYM4DgLlKozAZs88rOiOOBujXbjgbmdUd4r6DooLVYlO4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786416239; c=relaxed/simple; bh=WYEkUN4XgLmb2QSzxZbiGMtSGYajLh3kk1LmW6qb5KA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SM6yULSXUDqL6crS8SQrExEnM5XbdQ8xLPucZCjCQfbBuU0K8V7Gxsk3gM2BBWZcey0s0ZfelubD1Hhg5SkT66xRWK4jIXH4KOexblLSQo9yq7aroOsZaLQe+YjkaiBYTtstF+wafu0w8rdoxkrRsPC/5diWopDhm5N3nyXoqYw= 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=UOZ1vvoz; arc=none smtp.client-ip=209.85.214.182 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="UOZ1vvoz" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-2cace91f112so29336745ad.0 for ; Mon, 10 Aug 2026 19:43:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786416231; x=1787021031; 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=6rsMUA23aPPjN53adP0mvTSV6XbtGX1aRtlR4t+CMFI=; b=UOZ1vvozILViXCJlgcdQAKOjEPstRVWiTEWbzvvJt0hWXRDU3xT17MPx0tAr0mknpB pkoTAeHa33xg1jpxMA3t5brjutW5/Hjq1VJLdaUEPDzWv2MN8s0F0oKeQx84usKRQ/9z Ay4LSP4imAV1mHU0i+q676Yr30FXMuM+JcoRXiybbwT8cDupElxgWJfPT0MPwFelB7u+ 4ckbRMVUvkf6S4hvq1HkwURUJinzOkYeLlZfZ9Wl4FjIuXgPTc/8THbwBeijbVXvzgXh 9JtDNS//BUD9b8n3bAf2Dl5weHJ2k4O6Bv3wkI9QA5u+KlJdm23gFqMiOGOugzV11kXx qalQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786416231; x=1787021031; 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=6rsMUA23aPPjN53adP0mvTSV6XbtGX1aRtlR4t+CMFI=; b=K73URnh/+DV0HlbtPV7y4y7tnDeYy5kfXaff5119jvgW5twqxTzdbzMbwSyrmiziwm SD1EV4ip+dUDOr3J9N1w4QNLDQkS4wlOdPCJKl0KgM2aV4RSJauBPRkH61WBRh0uQ2sh 5ppx1L2Q+ZuhVD8qXW0fDZr4ocyyvD4C7xhD6CIiSPDMsplW2QGWJ71TO7JHxbpRigCI Pp0rVVvbVry4LEn2gWPAvmAa8QRjdakxgrqOw4bdmMQulykitRSsI/pslHVhKHhDkYGw Bm2ne8XzsaTL3rOBFPIORaXMYTEpCjI8dikYZYHHwtABO+JD78p+LKjoYvR0YuESD4pj xtGw== X-Forwarded-Encrypted: i=1; AHgh+RpnIxzusVhMdKTE701EIFY82wPoVdZOkNAurEUSiNycHL4DCBW5cSKZXCgMI6bzbUtJp/LuZWGJWqaKQ9Y=@vger.kernel.org X-Gm-Message-State: AOJu0YwLcyE+XSVlFJNfmNQCaAfxnMgDE2ReCWBza7XcxH2xOEAZk4Tg nIwwfLwybAKsUnqyQbWDybE9n0qrYZu6Vu0lKhGC648AZERnT1Z6K35m X-Gm-Gg: AR+sD10XzpJwxqScGjhAJ2gNO9hpHEoqaRPI2aD+T6EAmxrOgOug23+Iik/qzyFcIhY Ij/fZaeFv/6ZsXaLPC7W+vof0lIpCc4iCr7rb2ydRxzgEK2oqCaVVSNc1cSfol5ZnF2Jj8w0clx OWRPTVXQJBAv3GjUrgovQau+j7DMQedX4gE41d/So5nvlKoZhSjtyUKHAV+4t+gzhXGQw4GjWY1 IlhhkACC1FYpYJx8fz0iOIsztA2BU0F2p1hUsJlQkGQvkld6n0QAM93BesB4YsWrw79SnGVjaFw gSM8j3CpfZojGeO6eiafW/UGNQEduKtG66Ql0Vwpm2uissk8HxsoXp5Oyl+DTC3oOpPxus6DJjx OSzbh6b1x7Y29s8roYztTiMd+he1rkheNyer3s6GbFn8NDprhFsVljPEqYekVSrKmhKWq9Rafr4 yI4+4c0y4X3Tr3bmbOMBVt6627cXeJJZqtVYCujmF41MITnE2Cns9tTYRol7zKmx0kCHSEiAa2Z GQyrvSLGcWiWrD9 X-Received: by 2002:a17:902:f708:b0:2c9:d8c6:1dc3 with SMTP id d9443c01a7336-2d3175fee1fmr3925505ad.0.1786416231387; Mon, 10 Aug 2026 19:43:51 -0700 (PDT) Received: from osman.mioffice.cn ([43.224.245.178]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2d3160f47easm464165ad.64.2026.08.10.19.43.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Aug 2026 19:43:50 -0700 (PDT) From: Zhan Xusheng X-Google-Original-From: Zhan Xusheng To: Andrew Morton , Joseph Qi Cc: Mark Fasheh , Joel Becker , Junxiao Bi , Changwei Ge , Jun Piao , Heming Zhao , ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org, zhanxusheng@xiaomi.com Subject: [PATCH v2 1/2] ocfs2: bound-check dir entries in the readdir re-validation scan Date: Tue, 11 Aug 2026 10:43:36 +0800 Message-ID: <20260811024337.3972976-2-zhanxusheng@xiaomi.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260811024337.3972976-1-zhanxusheng@xiaomi.com> References: <20260811024337.3972976-1-zhanxusheng@xiaomi.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit When the inode version changed since the last readdir(), ocfs2_dir_foreach_blk_el() re-scans the directory block from its start to relocate the current position: for (i = 0; i < sb->s_blocksize && i < offset; ) { de = (struct ocfs2_dir_entry *)(bh->b_data + i); if (le16_to_cpu(de->rec_len) < OCFS2_DIR_REC_LEN(1)) break; i += le16_to_cpu(de->rec_len); } i walks the block on rec_len values taken from the block itself and the only thing tested is that rec_len is not too small, so a single bogus rec_len leaves i anywhere in the block, including its last OCFS2_DIR_REC_LEN(1) - 1 bytes. @offset comes from ctx->pos, which userspace moves with lseek() on the directory fd, and decides how far the walk gets. Two bounds are missing, both of which ocfs2_check_dir_entry() applies for the emit loop below. de->rec_len sits at byte offset 8 within the entry, so dereferencing de in that tail reads past the s_blocksize buffer. ocfs2_check_dir_entry() declines to look at an entry that close to the end: size - buf_offset < OCFS2_DIR_REC_LEN(1) Nothing bounds i += rec_len either, so i can end up past the block. The emit loop that follows is guarded by offset < sb->s_blocksize and does not run, but offset = i; ctx->pos = (ctx->pos & ~((loff_t)sb->s_blocksize - 1)) | offset; runs first and ORs a value with bits above the block mask into ctx->pos, corrupting the block number readdir() resumes from. ocfs2_check_dir_entry() rejects that as "directory entry overrun": next_offset = buf_offset + rlen; ... next_offset > size Apply both bounds. For a consistent directory this changes nothing: entries are at least OCFS2_DIR_REC_LEN(1) bytes and do not cross the end of the block, so no valid entry is skipped. Found by the sashiko review tool; fix approach suggested by Joseph Qi. Link: https://sashiko.dev/#/patchset/20260806022044.167962-1-zhanxusheng@xiaomi.com Suggested-by: Joseph Qi Cc: Mark Fasheh Cc: Joel Becker Cc: Junxiao Bi Cc: Changwei Ge Cc: Jun Piao Cc: Heming Zhao Signed-off-by: Zhan Xusheng --- fs/ocfs2/dir.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/fs/ocfs2/dir.c b/fs/ocfs2/dir.c index c30a86856d5b..39ce60874e17 100644 --- a/fs/ocfs2/dir.c +++ b/fs/ocfs2/dir.c @@ -1903,7 +1903,10 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode, * dirent right now. Scan from the start of the block * to make sure. */ if (!inode_eq_iversion(inode, *f_version)) { - for (i = 0; i < sb->s_blocksize && i < offset; ) { + for (i = 0; i + OCFS2_DIR_REC_LEN(1) <= sb->s_blocksize && + i < offset;) { + unsigned int rec_len; + de = (struct ocfs2_dir_entry *) (bh->b_data + i); /* It's too expensive to do a full * dirent test each time round this @@ -1911,10 +1914,11 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode, * least that it is non-zero. A * failure will be detected in the * dirent test below. */ - if (le16_to_cpu(de->rec_len) < - OCFS2_DIR_REC_LEN(1)) + rec_len = le16_to_cpu(de->rec_len); + if (rec_len < OCFS2_DIR_REC_LEN(1) || + i + rec_len > sb->s_blocksize) break; - i += le16_to_cpu(de->rec_len); + i += rec_len; } offset = i; ctx->pos = (ctx->pos & ~((loff_t)sb->s_blocksize - 1)) -- 2.43.0