From nobody Fri Sep 5 20:15:07 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 227CAC49EC3 for ; Tue, 23 Aug 2022 11:27:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357866AbiHWL0y (ORCPT ); Tue, 23 Aug 2022 07:26:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357896AbiHWLVW (ORCPT ); Tue, 23 Aug 2022 07:21:22 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3CAA8E473; Tue, 23 Aug 2022 02:23:11 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D89D861174; Tue, 23 Aug 2022 09:23:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DD75AC4314B; Tue, 23 Aug 2022 09:23:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661246590; bh=TOkOChexb3xBoqLim2uMn4EOROVQrWdPD/gVHuPVRlQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W4WFBNHyB95kvjyWUhlea/KAJL2oLAaM3+aZTY8YwcMtIep3V+Ba5vhu3taB27/JC 9/aCP3G5gfILSBdywvjCMMnr+RxW1Fbcj286X3WoafxEN77eVhnuTwl1zLP21UY3pP kuY9YzygvdmervKejuEVDVWmXJgKS4kr/yn3d/P0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Harshit Mogalapalli , Jiri Kosina , Sasha Levin Subject: [PATCH 5.4 157/389] HID: cp2112: prevent a buffer overflow in cp2112_xfer() Date: Tue, 23 Aug 2022 10:23:55 +0200 Message-Id: <20220823080122.160717191@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080115.331990024@linuxfoundation.org> References: <20220823080115.331990024@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Harshit Mogalapalli [ Upstream commit 381583845d19cb4bd21c8193449385f3fefa9caf ] Smatch warnings: drivers/hid/hid-cp2112.c:793 cp2112_xfer() error: __memcpy() 'data->block[1]' too small (33 vs 255) drivers/hid/hid-cp2112.c:793 cp2112_xfer() error: __memcpy() 'buf' too small (64 vs 255) The 'read_length' variable is provided by 'data->block[0]' which comes from user and it(read_length) can take a value between 0-255. Add an upper bound to 'read_length' variable to prevent a buffer overflow in memcpy(). Fixes: 542134c0375b ("HID: cp2112: Fix I2C_BLOCK_DATA transactions") Signed-off-by: Harshit Mogalapalli Signed-off-by: Jiri Kosina Signed-off-by: Sasha Levin --- drivers/hid/hid-cp2112.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/hid/hid-cp2112.c b/drivers/hid/hid-cp2112.c index db1b55df0d13..340408f8c8ab 100644 --- a/drivers/hid/hid-cp2112.c +++ b/drivers/hid/hid-cp2112.c @@ -787,6 +787,11 @@ static int cp2112_xfer(struct i2c_adapter *adap, u16 a= ddr, data->word =3D le16_to_cpup((__le16 *)buf); break; case I2C_SMBUS_I2C_BLOCK_DATA: + if (read_length > I2C_SMBUS_BLOCK_MAX) { + ret =3D -EINVAL; + goto power_normal; + } + memcpy(data->block + 1, buf, read_length); break; case I2C_SMBUS_BLOCK_DATA: --=20 2.35.1