Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1439911rwb; Wed, 28 Sep 2022 19:13:09 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6Gi7KUGF1AbbD7F4Q9r8Ziy7A0MDcOk3cX16abEEcGWqHsW9OYz4c1zW0rgvbZ6TQFX7Tk X-Received: by 2002:a17:907:9603:b0:742:9ed3:3af2 with SMTP id gb3-20020a170907960300b007429ed33af2mr710445ejc.510.1664417589534; Wed, 28 Sep 2022 19:13:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664417589; cv=none; d=google.com; s=arc-20160816; b=BkVBE5pupfNeAgjJd1jW+BPezz4+9QkP7jTkcnKpVR0IKf/r+GsiOgKLA3m2uSiHg+ Mqc/p+mZ7ABowaufV/fZGMLwHLFM3JNf9cPPXvAugFTtB9DXfIS267D178cSh4vVRq9K SqXAS8pan/8kQVpyXqpG6EJXfRtaDRvHepEvpjktYXi/QzofknbMw21npGNjJ4fKm1PS FYtakCS9xvn+TtuccdMaqgcwPFBR4G+2fFN1ph6cUQ1sePejx8fbkSGsp7iLxlR73aCM YSp/02CA5wgT975UOcvu/m+WRSa8kauUL3ES08Eqqj08J8vgcU/JAr0SRBhT/35SDXtJ 6PmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=C5vB9AdNpMbk+2uUPlTv1wvt0hrX4ZZuVlJ7vkkQRH8=; b=aGcr4Te332fPrj4TPekPu0H5YvU1vI5rtrTEL2C0AX0kR7CNMLbQjGU2hcJPTnu06T aQzwdrGFEHZi11ocr4kpGKa0sYO2l8CN3l1fjg8elX7PgwAs9QoklkaSbOKlPskVrQNU 1ABLQqu5TO3578YEc9vnOsjExzQfVpO39XRwvYsNcqXDHnnoMHdrRbV6VDkak1fu7QDs 96BUE0zmAfbUtFKF6tXOav30vzMIkMbbRiNz/ZQp717LG2yZep4DURxq9wKEhm2XwLeB SoEzq4Jt7c1BYdDrJxA76l5jy8S15DZdqyqwsrKmooe+L5Y8y/q6MYfir+MNFS9sYDoA W/9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=hPDZmGiq; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o3-20020a170906768300b0077d8a28dc81si6191257ejm.594.2022.09.28.19.12.44; Wed, 28 Sep 2022 19:13:09 -0700 (PDT) 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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=hPDZmGiq; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233332AbiI2CA3 (ORCPT + 99 others); Wed, 28 Sep 2022 22:00:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231949AbiI2CA0 (ORCPT ); Wed, 28 Sep 2022 22:00:26 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE2625F13F; Wed, 28 Sep 2022 19:00:24 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id b21so11976plz.7; Wed, 28 Sep 2022 19:00:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=C5vB9AdNpMbk+2uUPlTv1wvt0hrX4ZZuVlJ7vkkQRH8=; b=hPDZmGiqxB9XD3Z6wxo4PZNRWs3oY/Iz5OeU+JgazAdaRNCYb0hY+XBfMT5h9LpDuK sCGI7yR8/B5SquXIbmemuUwjvIDSrPu4q0TQj4VgxJpWiQUjdojJRHZNtrxDqXrzln0J SC2HI4obfGuU+uLyFgsG6Jz52S87DuPUi+3oTAYb331UHYMDQuKvHA2vT1QADuBgiLVe 7VSIb6tXofr7qFnhoUnL5WuTwLGNHphva9ycidtoa1lkJdflmrIUQCf9iwfFzPojQGuX dzg1bswH2ufICBvvHb6SrbK5MKAhZ6SJjKefrLNz0CNT44/RBeJw5+BJ9/93nNc0aH6Y FW1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=C5vB9AdNpMbk+2uUPlTv1wvt0hrX4ZZuVlJ7vkkQRH8=; b=ERZchNlRsbSdg8yM9YOz9lo2AjD7M0rU8vOpJSWyFw0l5zShYWzsmrhg+U4oHc9P2m Hf7GGJGcpbMCD01+q7yRlOWhCzRu9aD+NA2zEtt1NGU4dclZ2yLZDxUiud/YvkBlGVwS 2vqd4rBHpjhzAEe2lxaATRCBleOWr7pKXRzVDvMDktWXEcG+qvV0EeFuz3v6bRtygAgO 8udTAD3fm9mT6GfC/QL+6QFipYegtoHHSevPVNWgEAActBXPE9l3gxCaxH7lfw4Zr9zW 7HhYS17+J7Y78eCt995LCN35jWZUk70+JxdFCwU6QSc1GZ9IRPBtey76tLhwENGZfZlA SnAw== X-Gm-Message-State: ACrzQf2+ufi4VhYWf2BW8IgDOPX1kk1WOQsMruy9Bc9e4scxTVqrmSow C3uTvolw2fsZL5ebCEnSUwZsHeAVVb63mg== X-Received: by 2002:a17:903:11cc:b0:178:aec1:18c3 with SMTP id q12-20020a17090311cc00b00178aec118c3mr994449plh.91.1664416823981; Wed, 28 Sep 2022 19:00:23 -0700 (PDT) Received: from localhost.localdomain ([2402:7500:579:1b3a:f99f:1552:d82e:aa5a]) by smtp.gmail.com with ESMTPSA id o6-20020a170902d4c600b00178bd916c64sm4512864plg.265.2022.09.28.19.00.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 28 Sep 2022 19:00:23 -0700 (PDT) From: cy_huang To: lee@kernel.org Cc: matthias.bgg@gmail.com, gene_chen@richtek.com, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org, ChiYuan Huang , stable@vger.kernel.org Subject: [PATCH v2] mfd: mt6360: add bound check in regmap read/write function Date: Thu, 29 Sep 2022 10:00:17 +0800 Message-Id: <1664416817-31590-1-git-send-email-u0084500@gmail.com> X-Mailer: git-send-email 2.7.4 X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,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 From: ChiYuan Huang Fix the potential risk for null pointer if bank index is over the maximum. Refer to the discussion list for the experiment result on mt6370. https://lore.kernel.org/all/20220914013345.GA5802@cyhuang-hp-elitebook-840-g3.rt/ If not to check the bound, there is the same issue on mt6360. Fixes: 3b0850440a06c (mfd: mt6360: Merge different sub-devices I2C read/write) Cc: stable@vger.kernel.org Signed-off-by: ChiYuan Huang --- Since v2: - Assign i2c bank variable after bank index is already checked. --- drivers/mfd/mt6360-core.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/drivers/mfd/mt6360-core.c b/drivers/mfd/mt6360-core.c index 6eaa677..d3b32eb 100644 --- a/drivers/mfd/mt6360-core.c +++ b/drivers/mfd/mt6360-core.c @@ -402,7 +402,7 @@ static int mt6360_regmap_read(void *context, const void *reg, size_t reg_size, struct mt6360_ddata *ddata = context; u8 bank = *(u8 *)reg; u8 reg_addr = *(u8 *)(reg + 1); - struct i2c_client *i2c = ddata->i2c[bank]; + struct i2c_client *i2c; bool crc_needed = false; u8 *buf; int buf_len = MT6360_ALLOC_READ_SIZE(val_size); @@ -410,6 +410,11 @@ static int mt6360_regmap_read(void *context, const void *reg, size_t reg_size, u8 crc; int ret; + if (bank >= MT6360_SLAVE_MAX) + return -EINVAL; + + i2c = ddata->i2c[bank]; + if (bank == MT6360_SLAVE_PMIC || bank == MT6360_SLAVE_LDO) { crc_needed = true; ret = mt6360_xlate_pmicldo_addr(®_addr, val_size); @@ -453,13 +458,18 @@ static int mt6360_regmap_write(void *context, const void *val, size_t val_size) struct mt6360_ddata *ddata = context; u8 bank = *(u8 *)val; u8 reg_addr = *(u8 *)(val + 1); - struct i2c_client *i2c = ddata->i2c[bank]; + struct i2c_client *i2c; bool crc_needed = false; u8 *buf; int buf_len = MT6360_ALLOC_WRITE_SIZE(val_size); int write_size = val_size - MT6360_REGMAP_REG_BYTE_SIZE; int ret; + if (bank >= MT6360_SLAVE_MAX) + return -EINVAL; + + i2c = ddata->i2c[bank]; + if (bank == MT6360_SLAVE_PMIC || bank == MT6360_SLAVE_LDO) { crc_needed = true; ret = mt6360_xlate_pmicldo_addr(®_addr, val_size - MT6360_REGMAP_REG_BYTE_SIZE); -- 2.7.4