Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp45356rwb; Mon, 26 Sep 2022 14:29:43 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Mvn5hVi5MzZmNgulf/Qdxxibg2uJbqkgaH4nD1QRmZpytwciaq6BhQkPHNILrwewCxA97 X-Received: by 2002:a17:90b:4f4b:b0:200:876b:c1c8 with SMTP id pj11-20020a17090b4f4b00b00200876bc1c8mr853552pjb.32.1664227783353; Mon, 26 Sep 2022 14:29:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664227783; cv=none; d=google.com; s=arc-20160816; b=Lp8H5ey1XXuDkwvYnATiOPaMOfHcz0TlhKo9ZezcRR4EMmBuAg7UGIGYvyjVDwMpie IxVqg7922b0zcEeLLd7VFIrJhC6s7T9fNoMbtHcIHKQMXNpZlhNU0LchfOfjz5cLnFjk svCBdiZ5xbX5xWiBDYz4pFbvngWK2D9FfbHjgVEA9uPUdxRBq78595vsczjQxn+ryKHU SlADBNX/+cR6zEPhiAImeRA3A8hi81gPUKl8jqSYOaVVzJ7CTiiVAQ3VsL3e2BO7mkYo BRd7fm12XAkvzCKNeqOq6NaAUUo8sEpt/hW2EeL0cO4S28DVePYaMDgjwbzQD7gACe4/ r3OQ== 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=qVwbDe/CKHPffhBAourbEKUefE612zCKMLUh2u+s1wk=; b=tqoUHBhqyekxpcdB20CQGKgVVDSF+uOvR9/spsG+xOPP8JZQVSLJmuqkgnIWCdP2cR IT7zmvbiCBaahh2/DN+x9NWTOaBVkhNQavMWBy5hcTgCMCf4Ii0tkZDBtL1KBGLYHLoD b8q5vvGuO7h/2xMN1jU7hXmHzF6xUhgPmiGYWDP6YhtO94mJjnBdfw06qwN4vsrDGpAC EEDODTAhrn7zNa511rmtDM3SwWndc+mBu89OMU73vg03VK31JtEaQJtJ8GD5jOsWvZl/ 9Yf0wobAbIShCO8SBfYe7nwGqCoY3iqcsflVyf04kq6zDAkwzH27XhDB6kQ4pxxI4WpW KPPg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m11-20020a170902f64b00b00178a944c068si11593797plg.478.2022.09.26.14.29.31; Mon, 26 Sep 2022 14:29:43 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230149AbiIZVUi (ORCPT + 99 others); Mon, 26 Sep 2022 17:20:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230447AbiIZVUf (ORCPT ); Mon, 26 Sep 2022 17:20:35 -0400 Received: from mail.inka.de (mail.inka.de [IPv6:2a04:c9c7:0:1073:217:a4ff:fe3b:e77c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F6B772B70; Mon, 26 Sep 2022 14:20:33 -0700 (PDT) Received: from mail3.berkhan-weisser.de ([2a03:4000:54:b9a::4]) by mail.inka.de with esmtpsa id 1ocudL-003t12-Fi; Mon, 26 Sep 2022 22:23:19 +0200 Received: from 127.0.0.1 (helo=localhost.localdomain) by mail3.berkhan-weisser.de with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.94.2) (envelope-from ) id 1ocudL-007Trr-3k; Mon, 26 Sep 2022 22:23:19 +0200 From: Enrik Berkhan To: linux-input@vger.kernel.org Cc: linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, Rishi Gupta , Enrik Berkhan Subject: [PATCH v1 4/4] HID: mcp2221: avoid stale rxbuf pointer Date: Mon, 26 Sep 2022 22:22:39 +0200 Message-Id: <20220926202239.16379-5-Enrik.Berkhan@inka.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220926202239.16379-1-Enrik.Berkhan@inka.de> References: <20220926202239.16379-1-Enrik.Berkhan@inka.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=unavailable 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 In case the MCP2221 driver receives an unexpected read complete report from the device, the data should not be copied to mcp->rxbuf. The pointer might be NULL or even stale, having been set during an earlier transaction. Further, some bounds checking has been added. Signed-off-by: Enrik Berkhan --- drivers/hid/hid-mcp2221.c | 44 +++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c index d17839e09ebc..faccb3c03d33 100644 --- a/drivers/hid/hid-mcp2221.c +++ b/drivers/hid/hid-mcp2221.c @@ -94,6 +94,7 @@ struct mcp2221 { u8 *rxbuf; u8 txbuf[64]; int rxbuf_idx; + int rxbuf_len; int status; u8 cur_i2c_clk_div; struct gpio_chip *gc; @@ -286,15 +287,13 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp, total_len = smbus_len; mcp->rxbuf = smbus_buf; } + mcp->rxbuf_len = total_len; + mcp->rxbuf_idx = 0; spin_unlock_bh(&mcp->raw_event_lock); ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4); if (ret) - return ret; - - spin_lock_bh(&mcp->raw_event_lock); - mcp->rxbuf_idx = 0; - spin_unlock_bh(&mcp->raw_event_lock); + goto out_invalidate_rxbuf; do { spin_lock_bh(&mcp->raw_event_lock); @@ -304,15 +303,22 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp, ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); if (ret) - return ret; + goto out_invalidate_rxbuf; ret = mcp_chk_last_cmd_status(mcp); if (ret) - return ret; + goto out_invalidate_rxbuf; usleep_range(980, 1000); } while (mcp->rxbuf_idx < total_len); +out_invalidate_rxbuf: + spin_lock_bh(&mcp->raw_event_lock); + mcp->rxbuf = NULL; + mcp->rxbuf_len = 0; + mcp->rxbuf_idx = 0; + spin_unlock_bh(&mcp->raw_event_lock); + return ret; } @@ -500,9 +506,15 @@ static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr, spin_lock_bh(&mcp->raw_event_lock); mcp->rxbuf_idx = 0; mcp->rxbuf = data->block; + mcp->rxbuf_len = sizeof(data->block); mcp->txbuf[0] = MCP2221_I2C_GET_DATA; spin_unlock_bh(&mcp->raw_event_lock); ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); + spin_lock_bh(&mcp->raw_event_lock); + mcp->rxbuf_idx = 0; + mcp->rxbuf = NULL; + mcp->rxbuf_len = 0; + spin_unlock_bh(&mcp->raw_event_lock); if (ret) goto exit; } else { @@ -525,9 +537,15 @@ static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr, spin_lock_bh(&mcp->raw_event_lock); mcp->rxbuf_idx = 0; mcp->rxbuf = data->block; + mcp->rxbuf_len = sizeof(data->block); mcp->txbuf[0] = MCP2221_I2C_GET_DATA; spin_unlock_bh(&mcp->raw_event_lock); ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); + spin_lock_bh(&mcp->raw_event_lock); + mcp->rxbuf_idx = 0; + mcp->rxbuf = NULL; + mcp->rxbuf_len = 0; + spin_unlock_bh(&mcp->raw_event_lock); if (ret) goto exit; } else { @@ -756,6 +774,7 @@ static int mcp2221_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size) { u8 *buf; + int len; struct mcp2221 *mcp = hid_get_drvdata(hdev); spin_lock_bh(&mcp->raw_event_lock); @@ -813,9 +832,15 @@ static int mcp2221_raw_event(struct hid_device *hdev, break; } if (data[2] == MCP2221_I2C_READ_COMPL) { + if (mcp->rxbuf == NULL || mcp->rxbuf_idx >= mcp->rxbuf_len) + goto out; /* no complete() in this case */ + buf = mcp->rxbuf; - memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]); - mcp->rxbuf_idx = mcp->rxbuf_idx + data[3]; + len = data[3]; + if (len > mcp->rxbuf_len - mcp->rxbuf_idx) + len = mcp->rxbuf_len - mcp->rxbuf_idx; + memcpy(&buf[mcp->rxbuf_idx], &data[4], len); + mcp->rxbuf_idx = mcp->rxbuf_idx + len; mcp->status = 0; break; } @@ -865,6 +890,7 @@ static int mcp2221_raw_event(struct hid_device *hdev, complete(&mcp->wait_in_report); } +out: spin_unlock_bh(&mcp->raw_event_lock); return 1; -- 2.34.1