From nobody Tue Sep 29 07:39:38 2026 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: quoted-printable Content-Type: text/plain; charset="utf-8" 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 =3D 0; i < sb->s_blocksize && i < offset; ) { de =3D (struct ocfs2_dir_entry *)(bh->b_data + i); if (le16_to_cpu(de->rec_len) < OCFS2_DIR_REC_LEN(1)) break; i +=3D 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 +=3D 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 =3D i; ctx->pos =3D (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 =3D 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@xi= aomi.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 Reviewed-by: Joseph Qi --- 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 *in= ode, * dirent right now. Scan from the start of the block * to make sure. */ if (!inode_eq_iversion(inode, *f_version)) { - for (i =3D 0; i < sb->s_blocksize && i < offset; ) { + for (i =3D 0; i + OCFS2_DIR_REC_LEN(1) <=3D sb->s_blocksize && + i < offset;) { + unsigned int rec_len; + de =3D (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 *i= node, * 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 =3D le16_to_cpu(de->rec_len); + if (rec_len < OCFS2_DIR_REC_LEN(1) || + i + rec_len > sb->s_blocksize) break; - i +=3D le16_to_cpu(de->rec_len); + i +=3D rec_len; } offset =3D i; ctx->pos =3D (ctx->pos & ~((loff_t)sb->s_blocksize - 1)) --=20 2.43.0 From nobody Tue Sep 29 07:39:38 2026 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 1B0883905E6 for ; Tue, 11 Aug 2026 02:43:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786416244; cv=none; b=jrF6FWdjmDQGoLPuttwMY3/tITEMAajo8JMxN3X/L8/kDC9Pdlf5MluzjkLsDcpBcY1mpXPbTVReHL6Xfmh4bAgeOos+oTAotmK7owCA9kTIZ87C3iXj8dyJJaHM4fRrhHyIxPQ0wf9BUgdq4HpEkIhhAKM3ac9pWxf0mkOoTlE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786416244; c=relaxed/simple; bh=Zuczg2SkP/5ojRcZFRKQ4ku4YS+3+DzDOBTJc2/A9Po=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PWAen3KFSLEBKdvtJphm4kvGBBTEUIQP6J9SZZ57XefQLnhsR40obix89a8jCauYcup+Jhw38z8hpkIrTT/V0wLRTBHjaL5jj/eR4u6jCjv4UZYjaNRobG3UJftc7PB6qNdtB8Y83U86EvmQI7Qvwgz6N54piLuGqsG+c4StCe0= 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=KDyrxshG; arc=none smtp.client-ip=209.85.214.174 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="KDyrxshG" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2cee9b74ee1so21290015ad.3 for ; Mon, 10 Aug 2026 19:43:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786416236; x=1787021036; 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=uFGDd6MJX6QKx/u+FjV9oJmCytT4Rq2gvVt/3A3Rh9w=; b=KDyrxshGxJ5yTBq2WU5/vuTfMhumTBwAg9X8RbdU5GFqQniYF0328JnnbejVDoJz5O HHl81HuHbhtfyq+cPGCJkDj0a1ay0TErtiARWyxJMO+88+xH6yoDJZY4Z0P5JRhjDUbE lIucyBhY/yyICVOa5oAqST8Vw7IWAjapXMOxeJZkl5XPZ6cvhhFnDDgTd6a/eUti+Avr zt4PNoZj2dmSge5qKJtF7dl5HOstGABef7jvKlG7c8081Kme2wu+4bT/X8OfY0KeOTH5 gQw9uhIcYXJaFf7OZmRQWHqFe209vml5BZ9HFxnD//DVYHayk+S3/mYe4/DkWCrZlGCI nvLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786416236; x=1787021036; 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=uFGDd6MJX6QKx/u+FjV9oJmCytT4Rq2gvVt/3A3Rh9w=; b=W5xiGsXQOYpnbh9tDQbJZlwcC4+klMy/nkLXAwEKg6i684JG4Yh8nx0XbcpuSuygRE ppJmJtoBbIABuZdzd6+bf5i+5iHgnNVSrCziJLYJfzxktCwtGvXpBk3cIY82hremdEYj Ks4MqKDnosA04sR4vZFxgk9QtP7vJChz9EPy6SHzBNymN/CS5XA7hHIC5iahh0lkRyCB 67SsSXZZvJDhU2p1Z1Xvcu/iPXXZQMT9XtMVTOPUdGMR7DGbkzXRwZ99vgx3sdqnLNue k3Ipw81mqlxfdKYUwL+y0HPWhrXpc+x+5An9ssRakx7yaabzdbhmlkeBnlpKb5a9QMjD 7A8A== X-Forwarded-Encrypted: i=1; AHgh+RrV4Xz7HitVtDHEfSodnhrsyMbGMiZh/NCL26G9CojaOISjS8LwGFtTj7YTOoS7LDaDPMoW40+fS333RTE=@vger.kernel.org X-Gm-Message-State: AOJu0YxR0vVuOYcDbuvhfNmEZV2cxwZEBrUjOqlsR6iJQ0PZmMWcJDSa R5isGBPZ0yLGALUDH14ORoGQmKyM5tbEbvgYL3JqrCANk2YVEcWGy9lp X-Gm-Gg: AR+sD10TzJQtE1wT7kBLFllp3I4v/3FwysBQgN20wP9autkfXiOcJyDTYsNOp37us9L rJ9v+NNEGrzIC+VUtfWAdKjx1nmaOlUcWLTvj6JjjiAZ1aK1uz20p1ejbRAV7Q1NO70M0jBMFTq uOUmjP6t01sR1hRSjDo/pgJlDXo/S8lWmgMC/9eVENt1rKrGaufbyxDDnrRdGTcWY6Vd7ntDJ8e pv3iNtj2Lli/pdo8qysr41rEWdt3ylefyFJ7my9lp+ildIA7HF3S0q7/aqRtPopb5/RniAFl+kL ByKf+3bAY2mriRMJYA//y7h3ZMfZJeoIA0mfyHiJ0VhqUwS9i2Gui+suhYpOzOIgc8LBjYqpfdS mb3RbFE+1+5gHgc1yX2XUW55MBWBxNDF8kPoBzNj1K5MFNbDjAJqT8PDU2efAih7e7w12QRbHZ/ NjDi+kdUrEWFfSa39sWB6Mav43CNusWnhjwZAiC5+8JKh0SKdPUURTYIVqwAVSbLfwru6i1sLBl Sdz7rToIYU= X-Received: by 2002:a17:902:d54f:b0:2ca:6c8:abd8 with SMTP id d9443c01a7336-2d3178b48fbmr2660065ad.12.1786416236033; Mon, 10 Aug 2026 19:43:56 -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.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Aug 2026 19:43:55 -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 2/2] ocfs2: bound-check dir entries in the inline-data re-validation scan Date: Tue, 11 Aug 2026 10:43:37 +0800 Message-ID: <20260811024337.3972976-3-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: quoted-printable Content-Type: text/plain; charset="utf-8" ocfs2_dir_foreach_blk_id() re-scans the inline data area the same way ocfs2_dir_foreach_blk_el() re-scans a directory block, and is missing the same two bounds: for (i =3D 0; i < i_size_read(inode) && i < offset; ) { de =3D (struct ocfs2_dir_entry *)(data->id_data + i); if (le16_to_cpu(de->rec_len) < OCFS2_DIR_REC_LEN(1)) break; i +=3D le16_to_cpu(de->rec_len); } ocfs2_validate_inode_block() keeps i_size inside the inline area: if (le16_to_cpu(data->id_count) > ocfs2_max_inline_data_with_xattr(sb, di)) if (le64_to_cpu(di->i_size) > le16_to_cpu(data->id_count)) and that area runs to the end of the inode block, so for a full inline directory data->id_data + i_size is the end of di_bh->b_data. A bogus rec_len leaves i in the last OCFS2_DIR_REC_LEN(1) - 1 bytes of it, and de->rec_len, at byte offset 8 within the entry, is then read past the block. The emit loop below hands i_size_read(inode) to ocfs2_check_dir_entry(), which refuses both an entry that close to the end and one whose rec_len runs past it. Apply the same two bounds to the re-validation scan, reading i_size once into a local as ocfs2_check_dir_entry() takes it as @size. Unlike the extent case there is no mask to corrupt here: an unbounded i only sets ctx->pos past i_size, which ends the readdir early rather than moving it to the wrong place. Cc: Mark Fasheh Cc: Joel Becker Cc: Junxiao Bi Cc: Changwei Ge Cc: Jun Piao Cc: Heming Zhao Signed-off-by: Zhan Xusheng Reviewed-by: Joseph Qi --- fs/ocfs2/dir.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/fs/ocfs2/dir.c b/fs/ocfs2/dir.c index 39ce60874e17..863c69dec7da 100644 --- a/fs/ocfs2/dir.c +++ b/fs/ocfs2/dir.c @@ -1812,7 +1812,12 @@ static int ocfs2_dir_foreach_blk_id(struct inode *in= ode, * dirent right now. Scan from the start of the block * to make sure. */ if (!inode_eq_iversion(inode, *f_version)) { - for (i =3D 0; i < i_size_read(inode) && i < offset; ) { + loff_t size =3D i_size_read(inode); + + for (i =3D 0; i + OCFS2_DIR_REC_LEN(1) <=3D size && + i < offset;) { + unsigned int rec_len; + de =3D (struct ocfs2_dir_entry *) (data->id_data + i); /* It's too expensive to do a full @@ -1821,10 +1826,11 @@ static int ocfs2_dir_foreach_blk_id(struct inode *i= node, * 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 =3D le16_to_cpu(de->rec_len); + if (rec_len < OCFS2_DIR_REC_LEN(1) || + i + rec_len > size) break; - i +=3D le16_to_cpu(de->rec_len); + i +=3D rec_len; } ctx->pos =3D offset =3D i; *f_version =3D inode_query_iversion(inode); --=20 2.43.0