Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp3167586rdb; Tue, 26 Dec 2023 20:17:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IF2KQOjCXOiH9DEqM6kgNtkN68y6v03X2z+WwrNycSuUUa69+OF8SKtQx6p11pvsjPLHuCl X-Received: by 2002:a05:620a:1347:b0:77f:a37f:95a6 with SMTP id c7-20020a05620a134700b0077fa37f95a6mr9853673qkl.52.1703650658009; Tue, 26 Dec 2023 20:17:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703650657; cv=none; d=google.com; s=arc-20160816; b=cSkfCJ8E/lAMCMrUS8rinvu1Aqnx+0zZBHleCB3ozSjCOI6qLK+6sL4iwNceMC+YJH Wxxe0IcGP1PvPexdbW4LWnkHVc1vsrxDc8RKBp9sw6kCHUO5qjJTUmgh//9hx6KgYpgY mogBomwmxjgbTM/7a6Kb90x/FyLRkS5G44Fv+zNyhhjvlRTT12+yxXMCErBsDc8soV1N kTB1vRpWGN/Ms3g0ojBqiGHveAziHNinxxd+9PWZKbdj6l/0l76WuEI6F7jKjCr39KLm g3pO2nDwqc3/2RrkRKAlR3JJzY1OoUQZ78NM+0mDRchFTabiDyLNH8+rJQyGIp9yO7FU qF2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=KEyvBG9guI/ijSBI61Zrai1RT6rFl5qFBlZJhomEVEc=; fh=bnrnBcbyx0sM9DAnoHOQn6e7uer82WeSGox/oq7y2Ak=; b=AsD2M91u/fOGLNjgwrxS7nJ+ElqE1pUSGghi+1BkZr7DxEF5ocDzfeecrKjIwXXv7E CUePl29QmTzk1Q/phxlDLUWQ68bYavKgvSnaIfrQsoi/4PLgtKzskX50GAtfba8myj3R e+UeRQAD/55TILPvM7XXmfANB2wW6Ez4/i59PZXTtPvtVgXFnPTo3atj1gL/atPiAziZ ydi6nO1JNoazjWpFbm//TudTfvWQmzsjo9u80nIrz+VqbHnwLHELJHBzUiIoBO/zNL18 TQAABpPq42kweg9o8CF18xoaywkOr4uEtXae3L/u43ScY8cW4rYCGx0vOmVJDjWTlwXL CLqw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-11815-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-11815-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id bs9-20020a05620a470900b0077f4d0ccb58si14864614qkb.453.2023.12.26.20.17.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Dec 2023 20:17:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-11815-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-11815-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-11815-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id B75DA1C21F65 for ; Wed, 27 Dec 2023 04:17:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6B1A03C30; Wed, 27 Dec 2023 04:17:31 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4160D3C15 for ; Wed, 27 Dec 2023 04:17:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R211e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045192;MF=hsiangkao@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0VzJyGwp_1703650640; Received: from e69b19392.et15sqa.tbsite.net(mailfrom:hsiangkao@linux.alibaba.com fp:SMTPD_---0VzJyGwp_1703650640) by smtp.aliyun-inc.com; Wed, 27 Dec 2023 12:17:25 +0800 From: Gao Xiang To: linux-erofs@lists.ozlabs.org, Yue Hu Cc: LKML , Chao Yu , Gao Xiang , bugreport@ubisectech.com Subject: [PATCH] erofs: fix inconsistent per-file compression format Date: Wed, 27 Dec 2023 12:17:18 +0800 Message-Id: <20231227041718.1428868-1-hsiangkao@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <1affdad9-20f1-4fca-95af-237fda3df2b1.bugreport@ubisectech.com> References: <1affdad9-20f1-4fca-95af-237fda3df2b1.bugreport@ubisectech.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 EROFS can select compression algorithms on a per-file basis, and each per-file compression algorithm needs to be marked in the on-disk superblock for initialization. However, syzkaller can generate inconsistent crafted images that use an unsupported algorithm for specific inodes; thus, an unexpected "BUG: kernel NULL pointer dereference" can be raised. Fix this by checking against `sbi->available_compr_algs` for each compressed inode. Incorrect !erofs_sb_has_compr_cfgs preset bitmap is now fixed together since it was harmless previously. Reported-by: Fixes: 14373711dd54 ("erofs: add on-disk compression configurations") Signed-off-by: Gao Xiang --- fs/erofs/decompressor.c | 2 +- fs/erofs/zmap.c | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/fs/erofs/decompressor.c b/fs/erofs/decompressor.c index 021be5feb1bc..af98e88908ee 100644 --- a/fs/erofs/decompressor.c +++ b/fs/erofs/decompressor.c @@ -398,7 +398,7 @@ int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb) int size, ret = 0; if (!erofs_sb_has_compr_cfgs(sbi)) { - sbi->available_compr_algs = Z_EROFS_COMPRESSION_LZ4; + sbi->available_compr_algs = 1 << Z_EROFS_COMPRESSION_LZ4; return z_erofs_load_lz4_config(sb, dsb, NULL, 0); } diff --git a/fs/erofs/zmap.c b/fs/erofs/zmap.c index 7b55111fd533..d513f2cd7521 100644 --- a/fs/erofs/zmap.c +++ b/fs/erofs/zmap.c @@ -578,7 +578,8 @@ static int z_erofs_fill_inode_lazy(struct inode *inode) { struct erofs_inode *const vi = EROFS_I(inode); struct super_block *const sb = inode->i_sb; - int err, headnr; + struct erofs_sb_info *sbi = EROFS_SB(sb); + int err, nr; erofs_off_t pos; struct erofs_buf buf = __EROFS_BUF_INITIALIZER; void *kaddr; @@ -622,12 +623,12 @@ static int z_erofs_fill_inode_lazy(struct inode *inode) vi->z_algorithmtype[0] = h->h_algorithmtype & 15; vi->z_algorithmtype[1] = h->h_algorithmtype >> 4; - headnr = 0; - if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX || - vi->z_algorithmtype[++headnr] >= Z_EROFS_COMPRESSION_MAX) { - erofs_err(sb, "unknown HEAD%u format %u for nid %llu, please upgrade kernel", - headnr + 1, vi->z_algorithmtype[headnr], vi->nid); - err = -EOPNOTSUPP; + nr = 0; + if (!(sbi->available_compr_algs & (1 << vi->z_algorithmtype[0])) || + !(sbi->available_compr_algs & (1 << vi->z_algorithmtype[++nr]))) { + erofs_err(sb, "inconsistent HEAD%u algorithm format %u for nid %llu", + nr + 1, vi->z_algorithmtype[nr], vi->nid); + err = -EFSCORRUPTED; goto out_put_metabuf; } -- 2.39.3