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 15D8AC83F2C for ; Mon, 4 Sep 2023 09:53:27 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4RfP8j524Zz3bcS for ; Mon, 4 Sep 2023 19:53:25 +1000 (AEST) Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=linux.alibaba.com (client-ip=115.124.30.119; helo=out30-119.freemail.mail.aliyun.com; envelope-from=hsiangkao@linux.alibaba.com; receiver=lists.ozlabs.org) Received: from out30-119.freemail.mail.aliyun.com (out30-119.freemail.mail.aliyun.com [115.124.30.119]) (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 4RfP8Z6vS0z2xdg for ; Mon, 4 Sep 2023 19:53:18 +1000 (AEST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046051;MF=hsiangkao@linux.alibaba.com;NM=1;PH=DS;RN=3;SR=0;TI=SMTPD_---0VrJ7uei_1693821192; Received: from e18g06460.et15sqa.tbsite.net(mailfrom:hsiangkao@linux.alibaba.com fp:SMTPD_---0VrJ7uei_1693821192) by smtp.aliyun-inc.com; Mon, 04 Sep 2023 17:53:13 +0800 From: Gao Xiang To: linux-erofs@lists.ozlabs.org Subject: [PATCH v2 RESEND] erofs-utils: fsck: refuse illegel filename Date: Mon, 4 Sep 2023 17:53:11 +0800 Message-Id: <20230904095311.65096-1-hsiangkao@linux.alibaba.com> X-Mailer: git-send-email 2.24.4 In-Reply-To: <20230904095127.61351-1-hsiangkao@linux.alibaba.com> References: <20230904095127.61351-1-hsiangkao@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: linux-erofs@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development of Linux EROFS file system List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Gao Xiang Errors-To: linux-erofs-bounces+linux-erofs=archiver.kernel.org@lists.ozlabs.org Sender: "Linux-erofs" From: Guo Xuenan In some crafted erofs images, fsck.erofs may write outside the destination directory, which may be used to do some dangerous things. This commit fixes by checking all directory entry names with a '/' character when fscking. Squashfs also met the same situation [1], and have already fixed it here [2]. [1] https://github.com/plougher/squashfs-tools/issues/72 [2] https://github.com/plougher/squashfs-tools/commit/79b5a555058eef4e1e7ff220c344d39f8cd09646 Fixes: 412c8f908132 ("erofs-utils: fsck: add --extract=X support to extract to path X") Signed-off-by: Guo Xuenan Signed-off-by: Gao Xiang --- v1: https://lore.kernel.org/r/20230531072612.2643983-2-guoxuenan@huawei.com changes since v1: - check this only if fsck is enabled (extraction and fsck is impacted); - don't treat the middle '\0' as an illegel name since such cases are actually reserved and can be handled properly. lib/dir.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/dir.c b/lib/dir.c index fff0bc0..3731f0c 100644 --- a/lib/dir.c +++ b/lib/dir.c @@ -4,6 +4,19 @@ #include "erofs/print.h" #include "erofs/dir.h" +/* filename should not have a '/' in the name string */ +static bool erofs_validate_filename(const char *dname, int size) +{ + char *name = (char *)dname; + + while (name - dname < size) { + if (*name == '/') + return false; + ++name; + } + return true; +} + static int traverse_dirents(struct erofs_dir_context *ctx, void *dentry_blk, unsigned int lblk, unsigned int next_nameoff, unsigned int maxsize, @@ -102,6 +115,10 @@ static int traverse_dirents(struct erofs_dir_context *ctx, } break; } + } else if (fsck && + !erofs_validate_filename(de_name, de_namelen)) { + errmsg = "corrupted dirent with illegal filename"; + goto out; } ret = ctx->cb(ctx); if (ret) { -- 2.24.4