Received: by 2002:a05:6358:111d:b0:dc:6189:e246 with SMTP id f29csp2306108rwi; Thu, 3 Nov 2022 15:33:29 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7xC2D055AzOkBJ4xSTijF0o5L8dQX20OlqiPuGk8pTnJbl8HJ7Y2mvk6nJB0vXDLuDi97t X-Received: by 2002:a17:90b:3809:b0:214:1328:ac87 with SMTP id mq9-20020a17090b380900b002141328ac87mr883985pjb.12.1667514809408; Thu, 03 Nov 2022 15:33:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667514809; cv=none; d=google.com; s=arc-20160816; b=fLxzzi05qvA5+rtl7Odn6NwR4SYeAHrt2w5ZXfTzwYicFB5LrKBoy6cQjthvIBW5Bx xqJyQQ+CvcC139DYHgyi1dt/aZzigLMhw1VD6yweKOXzUGg3+UN+E15n4pNkxWtU1DRJ /ckmk0RO6/dYcgYtV/qHU3ijLMsh8q95wKc9ajsI+EL6LyrjfO0Cr/NARRWND7WjRhLY zrAwTXVVd1i3BZ83xY7ljjCIvTsb7F6MUQ0Pw6q/q216dpXicHLibg4Rfx21NDpfpeku bhLEcQs2ikKxYrKyYgBFdvUq+z7tDGhNOn4tjTPGYnw2HFk2pdaSyNSzhBk6BvDQ6TuX Fwyw== 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=MOW3UqpnjbZ8eKitUtFAjk6RDpZrpVOdyCW90p9jrZk=; b=MpZW1C+C1r1ytHQ+AFD/xVXFarXERLFe058i6ohPKYeYZS6IdP1WTlnjCU3NlN/k29 kD8TPh7cve4OypBQuT6INi9xagaK/OxfsrTkplw4KbncnXE3J15alvzegnhZ13qiA9wB g1qfehSYVJ7k6pVdAFTWfuTNAZkuUxCnvfHvk3JOQw6S8ABlQLj6OdyM4Onv+6tphlEl rDzLzR3+sbbMejTx2OvNkhGnc0Jmj2Lt12dVO/Lszn+KICp5poXUAFXq/z2Wsui/xhHc t20KMHQJ7lydHdhlSTUcYJzW1kyJ8s+qOiiy49K/9OIAGc6G/+zP6tu/QRoY/xr5CBhZ XXkw== 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 k128-20020a633d86000000b0044d72a10ab0si2350626pga.342.2022.11.03.15.33.16; Thu, 03 Nov 2022 15:33:29 -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 S231728AbiKCW1n (ORCPT + 98 others); Thu, 3 Nov 2022 18:27:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231707AbiKCW13 (ORCPT ); Thu, 3 Nov 2022 18:27:29 -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 1C21F2127D; Thu, 3 Nov 2022 15:27:27 -0700 (PDT) Received: from mail3.berkhan-weisser.de ([2a03:4000:54:b9a::4]) by mail.inka.de with esmtpsa id 1oqigI-005aIv-8o; Thu, 03 Nov 2022 23:27:26 +0100 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 1oqigH-005Ggn-T5; Thu, 03 Nov 2022 23:27:25 +0100 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 v2 3/3] HID: mcp2221: avoid stale rxbuf pointer Date: Thu, 3 Nov 2022 23:27:14 +0100 Message-Id: <20221103222714.21566-4-Enrik.Berkhan@inka.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221103222714.21566-1-Enrik.Berkhan@inka.de> References: <20220926202239.16379-1-Enrik.Berkhan@inka.de> <20221103222714.21566-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=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 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 | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c index 0705526231ec..471b29c3c501 100644 --- a/drivers/hid/hid-mcp2221.c +++ b/drivers/hid/hid-mcp2221.c @@ -98,6 +98,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; @@ -294,11 +295,12 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp, mcp->rxbuf = smbus_buf; } + mcp->rxbuf_len = total_len; + mcp->rxbuf_idx = 0; + ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4); if (ret) - return ret; - - mcp->rxbuf_idx = 0; + goto out_invalidate_rxbuf; do { memset(mcp->txbuf, 0, 4); @@ -306,15 +308,20 @@ 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: + mcp->rxbuf = NULL; + mcp->rxbuf_len = 0; + mcp->rxbuf_idx = 0; + return ret; } @@ -499,8 +506,12 @@ static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr, mcp->rxbuf_idx = 0; mcp->rxbuf = data->block; + mcp->rxbuf_len = sizeof(data->block); mcp->txbuf[0] = MCP2221_I2C_GET_DATA; ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); + mcp->rxbuf_idx = 0; + mcp->rxbuf = NULL; + mcp->rxbuf_len = 0; if (ret) goto exit; } else { @@ -522,8 +533,12 @@ static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr, mcp->rxbuf_idx = 0; mcp->rxbuf = data->block; + mcp->rxbuf_len = sizeof(data->block); mcp->txbuf[0] = MCP2221_I2C_GET_DATA; ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); + mcp->rxbuf_idx = 0; + mcp->rxbuf = NULL; + mcp->rxbuf_len = 0; if (ret) goto exit; } else { @@ -734,6 +749,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); switch (data[0]) { @@ -792,9 +808,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) + return 1; /* 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; } -- 2.34.1