Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp823263ybg; Sun, 26 Jul 2020 23:53:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyjXWrsOIkRaIotor39IfQMV+cqpmhvizboWLfCO3gHgQJm3hSTMEDk/5nf6wgHu8jbKqjt X-Received: by 2002:a17:906:17cc:: with SMTP id u12mr19264836eje.344.1595832798798; Sun, 26 Jul 2020 23:53:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595832798; cv=none; d=google.com; s=arc-20160816; b=wBtNdGCQ9WCr69eUvhHx8QLSUiTK0wzBnwtLRKihu+UqLY6lIAtSfgYBTWVqHCshpQ utP7mh8cr7ceJMcS3i6K2b15/iacuXN9NzP4laFsf2GKM04ZMuxjKYyH0mTF8VlaizPK Tamur/vZNT4Dgn0IKZHaQbRavdT4ACPLOI/mVbZ8seCIjA8SaU+HTcd1b2s7NJA2KVhz bqxV+aSESJBLLkuwWvpJ2WwgdOmPK43d6hWEH0ZZAYsZeuhfukWjy95u9rVPjBMZ90qc IeXmYYIC8RseHAhGftw23FFO95p/9zJrKZNhkvuoj0Hyox6Gwt4XZmO59ht1SAdURKGd 9AvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=X5aX1F3bl0ee2C8RVr9F/Di04B8++e8RAXU6+PbQJtw=; b=PQwPNZ7a7Cm3oT3VMGhZW8AGxtBX0IYAzZPHk7/XpNBqD24y7QIrEEFdDh6MU2XEIn c4u2v1blXL7lMcNqi1+54l65XEE4G33/lx4667UwP3ch3Sz3BbQmUf2VKvRBKGUBgVJg jV6H0Wc7BRJnmnBADUuYpqrmTDiT9tY0N4GSb9FEwHEsewWCIv4BxJFjDQ+EDAqgLZ7Y bPi9B4ywRVuI/BxSGKIci84NAOObWsRHOM09LDWXng1wdwxiODedLDLb6UW2e2nS25+3 oFHHpu7RYRBXlSPmO0JC+vDBCPydy3CVhaTwiGTlgoYJXJ/FmvVLLiDTODFDpCHEy9EM hX8Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a8si5205703edr.123.2020.07.26.23.52.56; Sun, 26 Jul 2020 23:53:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726854AbgG0Guq (ORCPT + 99 others); Mon, 27 Jul 2020 02:50:46 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:50362 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726116AbgG0Guq (ORCPT ); Mon, 27 Jul 2020 02:50:46 -0400 Received: from DGGEMS402-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 020D2A30C70C8211127B; Mon, 27 Jul 2020 14:50:44 +0800 (CST) Received: from huawei.com (10.175.104.82) by DGGEMS402-HUB.china.huawei.com (10.3.19.202) with Microsoft SMTP Server id 14.3.487.0; Mon, 27 Jul 2020 14:50:41 +0800 From: Huang Guobin To: , , CC: Subject: [PATCH v2] lib: Verify array index is correct before using it Date: Mon, 27 Jul 2020 02:59:10 -0400 Message-ID: <20200727065910.1628-1-huangguobin4@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.104.82] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This code reads from the array before verifying that "c" is a valid index. Move test array offset code before use to fix it. Fixes: 2da572c959dd ("lib: add software 842 compression/decompression") Signed-off-by: Huang Guobin --- lib/842/842_compress.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/842/842_compress.c b/lib/842/842_compress.c index c02baa4168e1..c37bfe0b9346 100644 --- a/lib/842/842_compress.c +++ b/lib/842/842_compress.c @@ -11,6 +11,7 @@ #define MODULE_NAME "842_compress" #include +#include #include "842.h" #include "842_debugfs.h" @@ -222,12 +223,14 @@ static int add_bits(struct sw842_param *p, u64 d, u8 n) static int add_template(struct sw842_param *p, u8 c) { int ret, i, b = 0; - u8 *t = comp_ops[c]; + u8 *t = NULL; bool inv = false; if (c >= OPS_MAX) return -EINVAL; + c = array_index_nospec(c, OPS_MAX); + t = comp_ops[c]; pr_debug("template %x\n", t[4]); ret = add_bits(p, t[4], OP_BITS); @@ -379,12 +382,14 @@ static int add_end_template(struct sw842_param *p) static bool check_template(struct sw842_param *p, u8 c) { - u8 *t = comp_ops[c]; + u8 *t = NULL; int i, match, b = 0; if (c >= OPS_MAX) return false; + c = array_index_nospec(c, OPS_MAX); + t = comp_ops[c]; for (i = 0; i < 4; i++) { if (t[i] & OP_ACTION_INDEX) { if (t[i] & OP_AMOUNT_2) -- 2.17.1