Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp87716rwi; Wed, 26 Oct 2022 21:07:22 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6TYbxwMNoMpBcR30ibMrViJDXg+f+jFw8OVyLmc2bfyIgupgB0l1D1mIZpGm8w74sYQGwZ X-Received: by 2002:a63:5266:0:b0:439:920b:fc65 with SMTP id s38-20020a635266000000b00439920bfc65mr39807169pgl.417.1666843641993; Wed, 26 Oct 2022 21:07:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666843641; cv=none; d=google.com; s=arc-20160816; b=Cl03/kLsJgLwj2nO5QmuEiUiuNJ022hbXYViV2pf4P4UF/kU8x6+A/R20OCD9tTwK9 KB+b7y5rUtp3QZdscRdL2YBk4QRhL+pzxRdsUWQcB6IVCw0lSNCUm6e52HNewCfY2LzM 9Y0nBuxDt18nEAii2RvG05mK3nGKfRzpRGSHTVRmF7z6AHLm5eXPAEW2RAck5V8TCGFq a72jLZ+2ID8Sum9GIYglmbUj9apoOoNyL30Z7ASC4kUn+91uXAOIPmrt3lyB69Pfw0sg YIxpXAEXaKW9ci+GyzOWrZRYhHzsqEPsYXItha1Dn11k3uHFh/W1ndqkZEK2KU0PxFDw RGhA== 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=h7yF1dXBnJEVhGGTIzHbHbpBrHmE7RRuUtKfNdRHcto=; b=ubG1LXSMubn1lw4BKE1nZndeBtyGuzJ8C2zhPHu8Bcw+y5fUFF4ppU+Xu6WGZIRkOK Mu0wJYZ2Nu5RDHGpolU15Xw3z7Co5N60Eaje6UzDImXQSuvHZMi7W6FaF4zNi5xy9j0g jGcxuLLsMwFQ/t3xrsqHSOXLLipXXwcQtfHAJqpWNoEY1NQLvSCL9+J3i25hp4ibiB0M iu07dWIcpdWmvXdVt5x5LMzKziNSpH9mQ+2q75EyLmqANetblhxtXKS0cpToCREbeMx4 APQq1HXgnzsiyzwd0zav8BA7DOLmWwCdGYCNwaPEJbrpZ643hImw4R662VpRjcV8oM22 Qvxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=gu6eTOkZ; 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 oa18-20020a17090b1bd200b001fb1bec57casi5385093pjb.61.2022.10.26.21.07.10; Wed, 26 Oct 2022 21:07:21 -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=gu6eTOkZ; 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 S233648AbiJ0Dds (ORCPT + 99 others); Wed, 26 Oct 2022 23:33:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234399AbiJ0Dda (ORCPT ); Wed, 26 Oct 2022 23:33:30 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5BCD15627C; Wed, 26 Oct 2022 20:32:23 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id j12so127966plj.5; Wed, 26 Oct 2022 20:32:23 -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 :message-id:reply-to; bh=h7yF1dXBnJEVhGGTIzHbHbpBrHmE7RRuUtKfNdRHcto=; b=gu6eTOkZtN4JP0DfX2VwONvMw+YB9vTjBp2U/9eEjpWrIdTvvb/NT8IBB/HSfr77T/ KVU109xPoqhChnO4jEJJeNoR4PxuEiH/nAXSGrDwiBwAXDYj5CYtkIuQ1KlG5Q4jPy2a /mw69vD27HVRoam4QlHY4I8x6oJdbrxeOUjLEnJsyCgctGEqkezSz0C/2pICsRoGWGW1 KKDcghoA6uiFfa96jQd/mQ8IQ/ySUBgXYphkyeiQYOdMfa2rNBeOauCMLxN1f6kzy/FU OPiSipbCxduZWTiG2SbeVpLYiA2gE90MJeOgKT9NtDNr4li1Lki+e+3JtuJxOzvZha/K eiHw== 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:message-id:reply-to; bh=h7yF1dXBnJEVhGGTIzHbHbpBrHmE7RRuUtKfNdRHcto=; b=tnP/pArVfmE4pCpknxqnHw2CLn7uAcr9aQLetUA12GeiUd00Z3rwlRu9jDvPOJKkFC 1Yr7pRvDwCcE67t3gZNBz1PMLcBMIjKlp4PPtIxLAFinTOeeLd+WQAw2Uh5NRnTySurv +oTWspNeVNGDnqWJfi1woZGUlXs5Ik5DTz411i1B/mojHGQOvJXT0DBWrS5YO66OAod2 huTKq+OQiaC6q6MlAnXNmZif3Eos7lFyOrPc5D/JFUtHicH8LwlINb7cxSMgF124NpNv SXNMG9j+Vg+pwDy6QW2O0t96QOzDmRFHCcwPLGoeGL0yudo+NZNwC3/gwBQBKiRn9Jjw bfmg== X-Gm-Message-State: ACrzQf1jNDQG1wk/9EM6bQmaYZ9Um2W8nf8xBDWLLuA1BsCterZvYShA wwNXyu6o+ai1H3KPUeyfXrQ= X-Received: by 2002:a17:902:a60d:b0:186:61f6:d7f4 with SMTP id u13-20020a170902a60d00b0018661f6d7f4mr37042665plq.75.1666841542726; Wed, 26 Oct 2022 20:32:22 -0700 (PDT) Received: from localhost.localdomain ([2402:7500:47a:8591:f9eb:aa62:7d76:f953]) by smtp.gmail.com with ESMTPSA id o11-20020a65458b000000b0043c732e1536sm65921pgq.45.2022.10.26.20.32.18 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 Oct 2022 20:32:21 -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 v3] mfd: mt6360: add boundary check in in mt6360 regmap read/write Date: Thu, 27 Oct 2022 11:32:09 +0800 Message-Id: <1666841529-12781-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 if virtual bank index is over the maximum. Refer to the discussion list on mt6370. https://lore.kernel.org/all/20220914013345.GA5802@cyhuang-hp-elitebook-840-g3.rt/ If not to check the boundary, there is also the same issue on mt6360. For mt6360 register virtual mapping, the normal range is 0 to 0x3FF. Below's the backtrace in my experiment to access mt6360 0x400 register with regmap_raw_read() and regmap_raw_write() function. 1) regmap_raw_read() Unable to handle kernel execute from non-executable memory at virtual address ffffffd4940c4c20 pc : platform_bus+0x8/0x2e8 lr : i2c_smbus_xfer+0x60/0x120 Call trace: platform_bus+0x8/0x2e8 i2c_smbus_read_i2c_block_data+0x74/0xc0 mt6360_regmap_read+0x9c/0x180 [mt6360_core] _regmap_raw_read+0xe4/0x278 regmap_raw_read+0xec/0x240 2) regmap_raw_write() Unable to handle kernel execute from non-executable memory at virtual address ffffffe4a0ac4c20 pc : platform_bus+0x8/0x2e8 lr : i2c_smbus_xfer+0x60/0x120 Call trace: platform_bus+0x8/0x2e8 i2c_smbus_write_i2c_block_data+0x84/0xd0 mt6360_regmap_write+0xa8/0x150 [mt6360_core] _regmap_raw_write_impl+0x6e8/0x828 _regmap_raw_write+0xb4/0x130 regmap_raw_write+0x74/0xb0 After adding the boundary check, the above two cases can be solved. Fixes: 3b0850440a06c (mfd: mt6360: Merge different sub-devices I2C read/write) Cc: stable@vger.kernel.org Signed-off-by: ChiYuan Huang --- Since v3: - Add backtrace log to help understanding what the potential risk is. 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