Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp253458ybg; Tue, 28 Jul 2020 05:18:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFocVMS5GEHjeinJPKaOl5wgY+iZNvBf0hRwBFJ7FVNwUZUcg4yqL9Hv1I67sMV14jIvYJ X-Received: by 2002:a05:6402:363:: with SMTP id s3mr25190897edw.238.1595938733021; Tue, 28 Jul 2020 05:18:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595938733; cv=none; d=google.com; s=arc-20160816; b=p17iV30XyGRbdp7G2QvT6nH9iq1SpOLaXrqeuUBo7MzbACw9cgfIqtV5vgkdye42s3 UzDMn0wHT+7SQ5jST9sBrefDtj3pdI94RoEgCMu3U2v3Zc6Z8lWvoDqQSsvpWmNWanX9 uedZ+swL5KIO5Ysv/vuLu8X4RNVHhkrliGrILAP8Hv1DKB0YI52sTMuPL1dBOSxA3BjQ 65+YzAWMn936H6wK2SsKf9M7Ug8/8myxpKd5vFl2qbWnF5DdV166W6ONdP35Kwyq0lWl efvEJQ8rcBx4TPj73EL/OFir6nMCw2lgPdOd6H+bapQscBBAdS8LM12shzz3BwnFm6Ug zzaA== 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=anyxcEYQ/1MAUx/7rzLxiPBaaLi2IN9Z+0PrtiZo1xB1DU+vPUmaXVf4eZ46HF/r/I P2k7aoLPjhmNhz6vTIbWtIG/58BN/B1s68Nx6TAZmM77Co3uyzjWn1mmrSbIvpt7qpKP yC3wVKvg/yJMcRdEVdlBgxeQZoc3+u04V+rYLqdG6GO6zVFE+RoE44oThqaYrznHDLQg g3qEqf9okntB2d7FpUgSM1icROrtztEi+8d+ZTqPPWL2APOuc6kqdj8Ti5zBo3D9jHfO C6lPLFCOmBZZ3RZ6Tc8YHS5JAVzwnR12NkkcL0a8HUgbJVjA/4CixIt2U+C6E5WX1e80 +Apw== 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 w5si647562edv.155.2020.07.28.05.18.30; Tue, 28 Jul 2020 05:18:53 -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 S1729512AbgG1MNm (ORCPT + 99 others); Tue, 28 Jul 2020 08:13:42 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:8843 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728896AbgG1MNl (ORCPT ); Tue, 28 Jul 2020 08:13:41 -0400 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 5009447AC61F34E300ED; Tue, 28 Jul 2020 20:13:40 +0800 (CST) Received: from huawei.com (10.175.104.82) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.487.0; Tue, 28 Jul 2020 20:13:34 +0800 From: Huang Guobin To: , , , CC: Subject: [PATCH v2] lib: Verify array index is correct before using it Date: Tue, 28 Jul 2020 08:21:57 -0400 Message-ID: <20200728122157.23120-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