Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp5377512rwb; Sun, 4 Dec 2022 20:35:14 -0800 (PST) X-Google-Smtp-Source: AA0mqf5VcHnSnqm5eKbjhpXzu85P4NRzrRqJFkh2yGzzHCxGknZ1lwpH4d/5u1DNjFsVG14gzdCD X-Received: by 2002:a63:1563:0:b0:477:8fed:811f with SMTP id 35-20020a631563000000b004778fed811fmr54545348pgv.343.1670214914066; Sun, 04 Dec 2022 20:35:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670214914; cv=none; d=google.com; s=arc-20160816; b=Ys/8GwDZNo4jkf+dAd5jB4tLh6WJw31pOdVU7lBQs3BO5KFIlOdjumheDqEbGOeXII S83mFVlwfDwsVo0IGyNrhl5WfDMs/e8eIZOne47kJLUNOhJBaQFsiXeR1+5Lf6NS2uZU 0dUjaXfMo7Otkw9NYKyylZ12KT/R5iKeaixqaksJDd35wJssWY3XKaqEa/SAzYwko2CL ElkYYO/K74D6dBDcbjhk/PZBL2Ev3oVCus68HwPyjK2DOP+jgx7LsLW8evmSfUxWnUwb 4K2s+wPIH222LTwuhmz2xpCA2uGSv84qu8XeBkIggc2d9oL+42XYT1BW1+8cuemHBj5K Nmcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=os0pbH/0PfRnZNluiRehdPw8JXxQVfTi7K2Xb6j6m9Y=; b=MXpJsp4fpVdgk6MR9NKEnCZ7id6/CP3XbLiOg+ADUZUN7HKqZtbHKGrtnIeCScijIW Y5s3NHuZD/EK12O9S0Fm+qqpzlVZJLz0BSsVzlXPPQ+yAc+imjbvlS+D+LNkcWlrX3ZX g63OC8rcqTflF0zphl7Nlr+7CKUGV9hNnHmKH7uYr65RJn27qy192ganGplECrDTwwXX gHeq4m5GP0LNa2unG3dcx3+51xdfj/6JPmGea1MICqZiCRmxARceag37I8vjRq/r8dK/ a6WGvnowpSXqdosYnXqlKiOtmdMSTx9EwJkHp/Q/V5W9xOfxDNHs+WfsrgF/RHzU6cbR Gd8g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t191-20020a6381c8000000b00478701e57c8si12637715pgd.807.2022.12.04.20.35.03; Sun, 04 Dec 2022 20:35:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231407AbiLEDxC (ORCPT + 82 others); Sun, 4 Dec 2022 22:53:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231405AbiLEDw7 (ORCPT ); Sun, 4 Dec 2022 22:52:59 -0500 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B83E7655D for ; Sun, 4 Dec 2022 19:52:57 -0800 (PST) Received: from dggpemm500013.china.huawei.com (unknown [172.30.72.56]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4NQV3t0khVz15N5c; Mon, 5 Dec 2022 11:52:10 +0800 (CST) Received: from ubuntu1804.huawei.com (10.67.175.36) by dggpemm500013.china.huawei.com (7.185.36.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 5 Dec 2022 11:52:56 +0800 From: Chen Zhongjin To: , , CC: , , , , Subject: [PATCH -next v3] erofs: Fix pcluster memleak when its block address is zero Date: Mon, 5 Dec 2022 11:49:57 +0800 Message-ID: <20221205034957.90362-1-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500013.china.huawei.com (7.185.36.172) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org syzkaller reported a memleak: https://syzkaller.appspot.com/bug?id=62f37ff612f0021641eda5b17f056f1668aa9aed unreferenced object 0xffff88811009c7f8 (size 136): ... backtrace: [] z_erofs_do_read_page+0x99b/0x1740 [] z_erofs_readahead+0x24e/0x580 [] read_pages+0x86/0x3d0 ... syzkaller constructed a case: in z_erofs_register_pcluster(), ztailpacking = false and map->m_pa = zero. This makes pcl->obj.index be zero although pcl is not a inline pcluster. Then following path adds refcount for grp, but the refcount won't be put because pcl is inline. z_erofs_readahead() z_erofs_do_read_page() # for another page z_erofs_collector_begin() erofs_find_workgroup() erofs_workgroup_get() Since it's illegal for the block address of a pcluster to be zero, add check here to avoid registering the pcluster which would be leaked. Fixes: cecf864d3d76 ("erofs: support inline data decompression") Reported-by: syzbot+6f8cd9a0155b366d227f@syzkaller.appspotmail.com Signed-off-by: Chen Zhongjin --- v1 -> v2: As Gao's advice, we should fail to register pcluster if m_pa is zero. Maked it this way and changed the commit message. v2 -> v3: Slightly fix commit message and add -next tag. --- fs/erofs/zdata.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c index b792d424d774..7826634f4f51 100644 --- a/fs/erofs/zdata.c +++ b/fs/erofs/zdata.c @@ -488,7 +488,8 @@ static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe) struct erofs_workgroup *grp; int err; - if (!(map->m_flags & EROFS_MAP_ENCODED)) { + if (!(map->m_flags & EROFS_MAP_ENCODED) || + !(map->m_pa >> PAGE_SHIFT)) { DBG_BUGON(1); return -EFSCORRUPTED; } -- 2.17.1