Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp970141rwb; Fri, 23 Sep 2022 06:40:01 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4V/7wovWk8vJ92lBuz1y6iNa3IIqEFLCisAszSVCBtDzfChAgUULQrevO8l1s+XvVsspKS X-Received: by 2002:a17:907:3e8b:b0:77f:587:3b1b with SMTP id hs11-20020a1709073e8b00b0077f05873b1bmr6995757ejc.617.1663940401661; Fri, 23 Sep 2022 06:40:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663940401; cv=none; d=google.com; s=arc-20160816; b=di02O0DPzX8xa+sJTN/iE5JU3dnWRVrFOZUZCP305yoUDS58g+PhBhLHXJaBrGPdtu oazWPYNgnL49MfG2U+qlRJkfIW2xNfaCoQf2mzByOBY6gKgFY5GKiV3YyfhUFa+u99gv HBnT1qh0aICiBvFzYG0NL/EAkx/puETxqiNzWLlpRil3c/jCdIdOUYbuk75B7lHjLsxz phOXk3tBtdPUfUcHVXcIsYBKfqkE+cp0ZiEb4T+AjzDv6Q2YG1ZJ/BahFRJPNEUe/OGa v/ohVumPeLLyJwuorhKb0nYzob84NVgb4RVd7TkwtWYpO2lXh3frNF03+D2zTNRrXdLk TkUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=c1x6a9Dy6cA2yJfOTPbS7GFIv1l4Bxu5D+effiZf1Wo=; b=wvs7jJemjg48hHe6TiTEFiUG9B81pNezWiz89+GwKkg8fwDF8x/aUZsPZpemw6RU6P 44COUAMCWfe4/SunjZ5uHrbkRXa5u0eVrb3jAxOFPsUC4NX+NvLgJhx3im2OVxqDkpCK G2bfbrC8Yjg+huHHQ7jXmKZiqss+xOHGVTST/vMQS0cqopdPMS6F2pkUUNdseW/x4guO B/wIAOY+2pDT1K1VWMWr0cScUv8nveYajDHp2cMSoakomnaloYHRwuyc+kA5xS+BeDa7 vvF/DTpT/we4aAP+nZHKX8JUmsDbC3wEEvlr5jqkLR77KMugqjzhLUkp7+KwuZZT5xqZ wKFw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-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 hb17-20020a170907161100b00782b2a97827si1215028ejc.242.2022.09.23.06.39.33; Fri, 23 Sep 2022 06:40:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-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-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-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 S231673AbiIWNfX (ORCPT + 99 others); Fri, 23 Sep 2022 09:35:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231304AbiIWNfR (ORCPT ); Fri, 23 Sep 2022 09:35:17 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D23397B0A; Fri, 23 Sep 2022 06:35:17 -0700 (PDT) Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MYtP43PmCzbndY; Fri, 23 Sep 2022 21:32:24 +0800 (CST) Received: from kwepemm600013.china.huawei.com (7.193.23.68) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 23 Sep 2022 21:35:15 +0800 Received: from huawei.com (10.175.127.227) by kwepemm600013.china.huawei.com (7.193.23.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 23 Sep 2022 21:35:14 +0800 From: Zhihao Cheng To: , , CC: , , , , Subject: [PATCH v3 3/3] quota: Add more checking after reading from quota file Date: Fri, 23 Sep 2022 21:45:54 +0800 Message-ID: <20220923134555.2623931-4-chengzhihao1@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220923134555.2623931-1-chengzhihao1@huawei.com> References: <20220923134555.2623931-1-chengzhihao1@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemm600013.china.huawei.com (7.193.23.68) 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-ext4@vger.kernel.org It would be better to do more sanity checking (eg. dqdh_entries, block no.) for the content read from quota file, which can prevent corrupting the quota file. Signed-off-by: Zhihao Cheng --- fs/quota/quota_tree.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/fs/quota/quota_tree.c b/fs/quota/quota_tree.c index 0fa73ca28045..0f1493e0f6d0 100644 --- a/fs/quota/quota_tree.c +++ b/fs/quota/quota_tree.c @@ -96,6 +96,11 @@ static int check_dquot_block_header(struct qtree_mem_dqinfo *info, err = do_check_range(info->dqi_sb, "dqdh_prev_free", le32_to_cpu(dh->dqdh_prev_free), 0, info->dqi_blocks - 1); + if (err) + return err; + err = do_check_range(info->dqi_sb, "dqdh_entries", + le16_to_cpu(dh->dqdh_entries), 0, + qtree_dqstr_in_blk(info)); return err; } @@ -348,6 +353,10 @@ static int do_insert_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot, } ref = (__le32 *)buf; newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]); + ret = do_check_range(dquot->dq_sb, "block", newblk, 0, + info->dqi_blocks - 1); + if (ret) + goto out_buf; if (!newblk) newson = 1; if (depth == info->dqi_qtree_depth - 1) { @@ -739,15 +748,21 @@ static int find_next_id(struct qtree_mem_dqinfo *info, qid_t *id, goto out_buf; } for (i = __get_index(info, *id, depth); i < epb; i++) { - if (ref[i] == cpu_to_le32(0)) { + uint blk_no = le32_to_cpu(ref[i]); + + if (blk_no == 0) { *id += level_inc; continue; } + ret = do_check_range(info->dqi_sb, "block", blk_no, 0, + info->dqi_blocks - 1); + if (ret) + goto out_buf; if (depth == info->dqi_qtree_depth - 1) { ret = 0; goto out_buf; } - ret = find_next_id(info, id, le32_to_cpu(ref[i]), depth + 1); + ret = find_next_id(info, id, blk_no, depth + 1); if (ret != -ENOENT) break; } -- 2.31.1