Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp3035969iog; Mon, 20 Jun 2022 09:48:07 -0700 (PDT) X-Google-Smtp-Source: AGRyM1teJ7pDG9siu7wtkmJLjQiICo3vxUNLKVgbWo5a9LzylorKLcxFVBWTDak2pv9s8kKzzgF6 X-Received: by 2002:a05:6402:50d0:b0:431:70e1:956 with SMTP id h16-20020a05640250d000b0043170e10956mr30505197edb.205.1655743687271; Mon, 20 Jun 2022 09:48:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655743687; cv=none; d=google.com; s=arc-20160816; b=UTyh5Ej4NAK41dLEeMtlwu4UCPp5SU+Vbbl/UEzK3jl25fstbrWpsxutJQQZOaEs5b mdCVwYpywzF3cQVI7gquzrAU00TJDvVvbX1eh+64eEsw88sAkAfl3JgKKZnWW/EaYYzL y5VsV47j9map8c0lxzud0RIERnwQv4MAo2VEF+A+ROGHygQ4jYp+6UGbwfUy6BgKecra 9ioVrXvkzeEfvr0Azat7E+8wjDDtymU+Qo55qMoGt80yG2jkeTC3ZpoYraVsiIRilasf EJA2ynkFe/sQt57XhECbW5V464hr7PsAMhgGgowInmDm5EVAnLv/2iU78MfMSHUlfIpX +hWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:content-transfer-encoding:mime-version :message-id:date:subject:cc:from:dkim-signature; bh=UPuXuwaATnCiD95hRgHKAmygUc1xM5cEW1R932PHfNc=; b=SPajU+WGz2G497Z8yGPMarcCEnqV1O8mR3uHOVHqmLHCMVrvnrWRBOVr3VBU86ktL4 sFJW1qSTyjcPzhwNgCo1mcq4s6PiWvfsFTflPJ+IpkhslbarHvxO1KPQj9p03rEjmQ72 9zON5rk9xGDZngRpgjlBKj4F45SkYQAjccjJuBy/gDnH4Y4Tnbu6X+wCw49BeZJ7MetG l5RYxHKwV2V6qZEbhp5YAJ8N+3gUmW16Ew/X+ge47reVv2vhAh8WU3TAabZVkxT1jbTw 4oobglynhiGMARqZccAJnOo3SlZHfbzRq7G7Oz1PQg9qRqgcMzikSL+Suszgh+GPHHTU DxXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@oracle.com header.s=corp-2021-07-09 header.b=dg7RELv6; 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=fail (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h16-20020a17090791d000b00712034f1a2bsi11752211ejz.589.2022.06.20.09.47.40; Mon, 20 Jun 2022 09:48:07 -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=fail header.i=@oracle.com header.s=corp-2021-07-09 header.b=dg7RELv6; 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=fail (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242269AbiFTQ24 (ORCPT + 99 others); Mon, 20 Jun 2022 12:28:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244553AbiFTQ2u (ORCPT ); Mon, 20 Jun 2022 12:28:50 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1FBB9BE0C; Mon, 20 Jun 2022 09:28:50 -0700 (PDT) Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 25KDDo2h010544; Mon, 20 Jun 2022 16:28:43 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding; s=corp-2021-07-09; bh=UPuXuwaATnCiD95hRgHKAmygUc1xM5cEW1R932PHfNc=; b=dg7RELv6yPPnxOGDkTR5jTbbt/a4uBWvKqvW6rVBDSmyfiYqNcxvJ6hbGeK5fSQW87I4 aYhAOH7NvJxVs/sZYDrjPyFXRf5ifmiit0SjiJvrCTQCkKrzVdtwpPJV1PIktm75QrFd pcScMHs3Ez6s+nMaB8Of+ioRXmijOHKKdBKbCI2vOmdjrL6gujVzusIthL9WQ2TJAiZS gc8gm8sEUYtERkHexZGWR8I5TGuvfOWRMnFk2rmZq0OVU23VMGOFBSH6eFJuEQbR36vS 4lEhgX2BV/sk37X9JmtDmVb2bKLZTCWLZSYwGTMvsfoMkbiktPiAqAT03bkdPn5jBur5 dA== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3gs78tunpu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 20 Jun 2022 16:28:43 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 25KGKfjN000850; Mon, 20 Jun 2022 16:28:42 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3gtg5tjd40-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 20 Jun 2022 16:28:42 +0000 Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 25KGSf6Y019345; Mon, 20 Jun 2022 16:28:41 GMT Received: from ca-dev112.us.oracle.com (ca-dev112.us.oracle.com [10.147.25.63]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3gtg5tjd3a-1; Mon, 20 Jun 2022 16:28:41 +0000 From: Harshit Mogalapalli Cc: harshit.m.mogalapalli@oracle.com, dan.carpenter@oracle.com, Rishi Gupta , Jiri Kosina , Benjamin Tissoires , linux-i2c@vger.kernel.org, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] HID: mcp2221: prevent a buffer overflow in mcp_smbus_write() Date: Mon, 20 Jun 2022 09:28:24 -0700 Message-Id: <20220620162824.58937-1-harshit.m.mogalapalli@oracle.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-GUID: m7rxXkTFRvbfervoWggKeeMRoHZOjazw X-Proofpoint-ORIG-GUID: m7rxXkTFRvbfervoWggKeeMRoHZOjazw X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Smatch Warning: drivers/hid/hid-mcp2221.c:388 mcp_smbus_write() error: __memcpy() '&mcp->txbuf[5]' too small (59 vs 255) drivers/hid/hid-mcp2221.c:388 mcp_smbus_write() error: __memcpy() 'buf' too small (34 vs 255) The 'len' variable can take a value between 0-255 as it can come from data->block[0] and it is user data. So add an bound check to prevent a buffer overflow in memcpy(). Fixes: 67a95c21463d ("HID: mcp2221: add usb to i2c-smbus host bridge") Signed-off-by: Harshit Mogalapalli --- I believe I2C_SMBUS_BLOCK_MAX (32) is the appropriate limit to use here but the &mcp->txbuf[5] array could actually fit 59 bytes which is the destination in this case. I don't know why the buffer is larger than expected. drivers/hid/hid-mcp2221.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c index 4211b9839209..de52e9f7bb8c 100644 --- a/drivers/hid/hid-mcp2221.c +++ b/drivers/hid/hid-mcp2221.c @@ -385,6 +385,9 @@ static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr, data_len = 7; break; default: + if (len > I2C_SMBUS_BLOCK_MAX) + return -EINVAL; + memcpy(&mcp->txbuf[5], buf, len); data_len = len + 5; } -- 2.31.1