From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5D53DF43841 for ; Thu, 16 Apr 2026 06:35:28 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4fx7YV5SgGz2yYq; Thu, 16 Apr 2026 16:35:26 +1000 (AEST) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip=115.124.30.124 ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1776321326; cv=none; b=Q+d/osuR3yp9d5GviEhCEEutIeq657kBcJ6/MpHqsZU84y23zsMAlwRE09PeIVQSKF/AR6WZMHzMngR8Y30nNmX5/Zc5sUnOZti9k+XhMp9zCb650RlhenfRHFMEx5+qMCR1JgCwIBGyK8RCr4ON8ltDQjuHsS6zp23Cx6ETBwSq5mYIsENl9suqGtPTpv1cmu428ZSU6E0FY/W9eaR/aJJV/bHTHISTdQIVk4Kyl8qybLpYqO4FTCkrxgWUKJNj0OEJIo3Vuk73l6XJmXFikonZKqgs44ijD0fNapVpRH/4hzj+QuCKwPyJDEDUqKX8u4neV+TpuD8i2HSJkBUa5g== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1776321326; c=relaxed/relaxed; bh=1gGpFvXLkLrcGRymd826yLSlyLTpic7Vaa2yZ0FduQY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nzr+W+GbEUr5V+i2OvBGZVW/MLRJid1NYslZaawVn/IEnQz1IOw5gMdkLzK1uvbIEZxzDlmiSSyHWae9CKNlbGNQrnqFeh91lDAKdDuwjy0eVL8BP72d0n+Hz6AMHZDi0YaHQaNhXwzaKcjwv8/G0Dfa2/r4EpQNLMLIdTQ8Ubh+Y4nisVkCZ14v4bkd0WokP9Am8dcCDHuVotieaWLhCBjX9AGcREc+D/W4jGhPJ9fUSd5tPiePXPrC6/Kf3cPLOayPEx2iT+c7ogtszmV1XZoOJP4QxKlSOguI5cH4sAaG4U9j0h+3YW+XllY6sIt68hFmbbpuFpiQ6h3dOL+zeg== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; dkim=pass (1024-bit key; unprotected) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=ZOTwvZrp; dkim-atps=neutral; spf=pass (client-ip=115.124.30.124; helo=out30-124.freemail.mail.aliyun.com; envelope-from=hsiangkao@linux.alibaba.com; receiver=lists.ozlabs.org) smtp.mailfrom=linux.alibaba.com Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=ZOTwvZrp; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=linux.alibaba.com (client-ip=115.124.30.124; helo=out30-124.freemail.mail.aliyun.com; envelope-from=hsiangkao@linux.alibaba.com; receiver=lists.ozlabs.org) Received: from out30-124.freemail.mail.aliyun.com (out30-124.freemail.mail.aliyun.com [115.124.30.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4fx7YS41dnz2xSX for ; Thu, 16 Apr 2026 16:35:23 +1000 (AEST) DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1776321319; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=1gGpFvXLkLrcGRymd826yLSlyLTpic7Vaa2yZ0FduQY=; b=ZOTwvZrpZ1AOeObrvrvcyuY0hR7ve/ycaFzbM1RCHJr50ujvfQHo1XzQ7qoalk48vpkDOT1vndtHKP8EkLPN3poMumdPubdV0HENaX0E5sys/Qta6hpij0qUdyXo/tPLiLYOkA5ikn9EiZCGcFuf61Kooyn2E3D2NDQui3u8CCw= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R121e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam011083073210;MF=hsiangkao@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0X17JG1Z_1776321312; Received: from x31i01179.sqa.na131.tbsite.net(mailfrom:hsiangkao@linux.alibaba.com fp:SMTPD_---0X17JG1Z_1776321312 cluster:ay36) by smtp.aliyun-inc.com; Thu, 16 Apr 2026 14:35:16 +0800 From: Gao Xiang To: linux-erofs@lists.ozlabs.org Cc: LKML , Gao Xiang , Yuhao Jiang , Junrui Luo , stable@vger.kernel.org Subject: [PATCH v2] erofs: fix the out-of-bounds nameoff handling for trailing dirents Date: Thu, 16 Apr 2026 14:35:11 +0800 Message-ID: <20260416063511.3173774-1-hsiangkao@linux.alibaba.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20260416060305.3129334-1-hsiangkao@linux.alibaba.com> References: <20260416060305.3129334-1-hsiangkao@linux.alibaba.com> X-Mailing-List: linux-erofs@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Subscribe: , , List-Unsubscribe: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Currently we already have boundary-checks for nameoffs, but the trailing dirents are special since the namelens are calculated with strnlen() with unchecked nameoffs. If a crafted EROFS has a trailing dirent with nameoff >= maxsize, maxsize - nameoff can underflow, causing strnlen() to read past the directory block. Fixes: 3aa8ec716e52 ("staging: erofs: add directory operations") Fixes: 33bac912840f ("staging: erofs: keep corrupted fs from crashing kernel in erofs_readdir()") Reported-by: Yuhao Jiang Reported-by: Junrui Luo Closes: https://lore.kernel.org/r/A0FD7E0F-7558-49B0-8BC8-EB1ECDB2479A@outlook.com Cc: stable@vger.kernel.org Signed-off-by: Gao Xiang --- v2: - should use in_range32() instead. fs/erofs/dir.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/fs/erofs/dir.c b/fs/erofs/dir.c index e5132575b9d3..c7717149c5ed 100644 --- a/fs/erofs/dir.c +++ b/fs/erofs/dir.c @@ -19,20 +19,18 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx, const char *de_name = (char *)dentry_blk + nameoff; unsigned int de_namelen; - /* the last dirent in the block? */ - if (de + 1 >= end) - de_namelen = strnlen(de_name, maxsize - nameoff); - else + /* non-trailing dirent in the directory block? */ + if (de + 1 < end) de_namelen = le16_to_cpu(de[1].nameoff) - nameoff; + else if (maxsize <= nameoff) + goto err_bogus; + else + de_namelen = strnlen(de_name, maxsize - nameoff); - /* a corrupted entry is found */ - if (nameoff + de_namelen > maxsize || - de_namelen > EROFS_NAME_LEN) { - erofs_err(dir->i_sb, "bogus dirent @ nid %llu", - EROFS_I(dir)->nid); - DBG_BUGON(1); - return -EFSCORRUPTED; - } + /* a corrupted entry is found (including negative namelen) */ + if (!in_range32(de_namelen, 1, EROFS_NAME_LEN) || + nameoff + de_namelen > maxsize) + goto err_bogus; if (!dir_emit(ctx, de_name, de_namelen, erofs_nid_to_ino64(EROFS_SB(dir->i_sb), @@ -42,6 +40,10 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx, ctx->pos += sizeof(struct erofs_dirent); } return 0; +err_bogus: + erofs_err(dir->i_sb, "bogus dirent @ nid %llu", EROFS_I(dir)->nid); + DBG_BUGON(1); + return -EFSCORRUPTED; } static int erofs_readdir(struct file *f, struct dir_context *ctx) -- 2.43.5