Message ID | 20230705140242.844167-1-arnd@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp1904226vqx; Wed, 5 Jul 2023 07:21:19 -0700 (PDT) X-Google-Smtp-Source: APBJJlGwtkaUOV7CyBPsGYYirr3YQl+PtaT9G15n9NAeXGe5L1S+ba50w4reW65xaYfbr+VBXXar X-Received: by 2002:a17:902:ea03:b0:1b8:7f95:7ba2 with SMTP id s3-20020a170902ea0300b001b87f957ba2mr16946060plg.42.1688566879590; Wed, 05 Jul 2023 07:21:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688566879; cv=none; d=google.com; s=arc-20160816; b=qp3FftL6u2Laaj1QGM0XB3lu4WawOxjNS6csoh1YG1CxGNJm1JYVnzSccdBe4waMYE SO8cYtyQ0HyC32gs744QKtHqv4veTmNJsM+hMMgCTePQ2hNChiXvL+pbX/oFR9SgEwQR nvxxzRmqg0k1LhHSC9fzUBNONqihz5c6cyQi0FWzj+9y8vs/J08En3uTqwoaOQNssfls JzxrNri+v+P3AktqA8wn7ZSGq/DuSiizmFT4yVxqqasxBobBmQtkbW9gc1PFm0CybqJH P+SE7Q1rsqLq/+KMjO6nIJJrFZI+Hhn14rAH1uSSKUXotDxn7gnW++EJrcJbRoaietcB Bx5A== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=Ir3/x461lH1DvkNO2K8DshBkSin2qWHJmZoyv/+E4J0=; fh=jQx/OM00VL0I/YT8WM9trZ7LiBSkv/6pomkSQjpF13s=; b=UhczC06iXfTgoMWE6sgXzgv8C9Oug4YqkOdrMfP0OcTe6soCDtVt/MscgwtPGZCvme olq9IBJKAKZJcuI32Wo2GoRRkyLrY0iXPYoOKH3gSIahyt3x8zS4TfgQl+ji1O3G3CnV uSvfjiiCSIAO+hve/NSh4/RsywE3mlFrOFPh6rG3vKm/CNx0Qgcoulnh7K8g5ySGh5Hp xIrBq8EYj/AN1PC+1zB5mwI2lBQjZj1U5bBQWEx7PvhDVbkah4XGzzeCQFVRpH3mTCSP i+oZGo6rr6tEWWistNJzBie3wydjn+CqQKC92dtqDyscRjI4sIPBke/szME2FsswsM2W fdGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bTRx9QYe; 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=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n15-20020a170903110f00b001b041591a5fsi24603140plh.459.2023.07.05.07.21.06; Wed, 05 Jul 2023 07:21:19 -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=@kernel.org header.s=k20201202 header.b=bTRx9QYe; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232442AbjGEOCy (ORCPT <rfc822;tebrre53rla2o@gmail.com> + 99 others); Wed, 5 Jul 2023 10:02:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231865AbjGEOCv (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 5 Jul 2023 10:02:51 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1FB6211B; Wed, 5 Jul 2023 07:02:50 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id AC71A61568; Wed, 5 Jul 2023 14:02:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE4E5C433C7; Wed, 5 Jul 2023 14:02:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1688565769; bh=8Pv519rVX2nyzfcP+WWJAfxHrjLTPQrVejjsds7b0e8=; h=From:To:Cc:Subject:Date:From; b=bTRx9QYeOOUu652xyFmZdnhpFBlT1LXlf0Ecjj9AqPmaZ+k4P5cbVpk6enhALEj+4 a++Be9rRcL1yiFdllRaQpJkGq+/dACPEV0zCo8uGcqIMkwlkcT4Ast+4B3zEqtA1my 1yBNwB/rHhuV7+nPSGNDHyjhl1Y31bpbXvZr1mlYn5elxSVYB5RM2nn4wn9qN5bmxL FhXk8K0TM+LeSS3LrDieZiEoszeAJyznP28Jfz7n7JlnTk3REgjOePQES9Lrc3TQHq SlhmZXawVNCyXfrzkOfyjPxviE83s/157jrxJcl8wZPOP79iNgmJs7hVoxKps5UvLW XTlf78IsOIZ2w== From: Arnd Bergmann <arnd@kernel.org> To: Jiri Kosina <jikos@kernel.org>, Benjamin Tissoires <benjamin.tissoires@redhat.com> Cc: Arnd Bergmann <arnd@arndb.de>, "K. Y. Srinivasan" <kys@microsoft.com>, Haiyang Zhang <haiyangz@microsoft.com>, Wei Liu <wei.liu@kernel.org>, Dexuan Cui <decui@microsoft.com>, Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com>, Michael Kelley <mikelley@microsoft.com>, Dawei Li <set_pte_at@outlook.com>, Yang Yingliang <yangyingliang@huawei.com>, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= <linux@weissschuh.net>, linux-hyperv@vger.kernel.org, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] HID: hyperv: avoid struct memcpy overrun warning Date: Wed, 5 Jul 2023 16:02:24 +0200 Message-Id: <20230705140242.844167-1-arnd@kernel.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,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 Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770590704313695183?= X-GMAIL-MSGID: =?utf-8?q?1770590704313695183?= |
Series |
HID: hyperv: avoid struct memcpy overrun warning
|
|
Commit Message
Arnd Bergmann
July 5, 2023, 2:02 p.m. UTC
From: Arnd Bergmann <arnd@arndb.de> A previous patch addressed the fortified memcpy warning for most builds, but I still see this one with gcc-9: In file included from include/linux/string.h:254, from drivers/hid/hid-hyperv.c:8: In function 'fortify_memcpy_chk', inlined from 'mousevsc_on_receive' at drivers/hid/hid-hyperv.c:272:3: include/linux/fortify-string.h:583:4: error: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning] 583 | __write_overflow_field(p_size_field, size); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ My guess is that the WARN_ON() itself is what confuses gcc, so it no longer sees that there is a correct range check. Rework the code in a way that helps readability and avoids the warning. Fixes: 542f25a944715 ("HID: hyperv: Replace one-element array with flexible-array member") Signed-off-by: Arnd Bergmann <arnd@arndb.de> --- drivers/hid/hid-hyperv.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
Comments
From: Arnd Bergmann <arnd@kernel.org> Sent: Wednesday, July 5, 2023 7:02 AM > > A previous patch addressed the fortified memcpy warning for most > builds, but I still see this one with gcc-9: > > In file included from include/linux/string.h:254, > from drivers/hid/hid-hyperv.c:8: > In function 'fortify_memcpy_chk', > inlined from 'mousevsc_on_receive' at drivers/hid/hid-hyperv.c:272:3: > include/linux/fortify-string.h:583:4: error: call to '__write_overflow_field' declared with > attribute warning: detected write beyond size of field (1st parameter); maybe use > struct_group()? [-Werror=attribute-warning] > 583 | __write_overflow_field(p_size_field, size); > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > My guess is that the WARN_ON() itself is what confuses gcc, so it no > longer sees that there is a correct range check. Rework the code in a > way that helps readability and avoids the warning. > > Fixes: 542f25a944715 ("HID: hyperv: Replace one-element array with flexible-array member") > Signed-off-by: Arnd Bergmann <arnd@arndb.de> > --- > drivers/hid/hid-hyperv.c | 10 ++++------ > 1 file changed, 4 insertions(+), 6 deletions(-) > > diff --git a/drivers/hid/hid-hyperv.c b/drivers/hid/hid-hyperv.c > index 49d4a26895e76..f33485d83d24f 100644 > --- a/drivers/hid/hid-hyperv.c > +++ b/drivers/hid/hid-hyperv.c > @@ -258,19 +258,17 @@ static void mousevsc_on_receive(struct hv_device *device, > > switch (hid_msg_hdr->type) { > case SYNTH_HID_PROTOCOL_RESPONSE: > + len = struct_size(pipe_msg, data, pipe_msg->size); > + > /* > * While it will be impossible for us to protect against > * malicious/buggy hypervisor/host, add a check here to > * ensure we don't corrupt memory. > */ > - if (struct_size(pipe_msg, data, pipe_msg->size) > - > sizeof(struct mousevsc_prt_msg)) { > - WARN_ON(1); > + if (WARN_ON(len > sizeof(struct mousevsc_prt_msg))) > break; > - } > > - memcpy(&input_dev->protocol_resp, pipe_msg, > - struct_size(pipe_msg, data, pipe_msg->size)); > + memcpy(&input_dev->protocol_resp, pipe_msg, len); > complete(&input_dev->wait_event); > break; > > -- > 2.39.2 Reviewed-by: Michael Kelley <mikelley@microsoft.com>
On Wed, 05 Jul 2023 16:02:24 +0200, Arnd Bergmann wrote: > A previous patch addressed the fortified memcpy warning for most > builds, but I still see this one with gcc-9: > > In file included from include/linux/string.h:254, > from drivers/hid/hid-hyperv.c:8: > In function 'fortify_memcpy_chk', > inlined from 'mousevsc_on_receive' at drivers/hid/hid-hyperv.c:272:3: > include/linux/fortify-string.h:583:4: error: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning] > 583 | __write_overflow_field(p_size_field, size); > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > [...] Applied to https://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git (for-6.5/upstream-fixes), thanks! [1/1] HID: hyperv: avoid struct memcpy overrun warning https://git.kernel.org/hid/hid/c/5f151364b1da Cheers,
On Wed, Jul 05, 2023 at 04:02:24PM +0200, Arnd Bergmann wrote: > From: Arnd Bergmann <arnd@arndb.de> > > A previous patch addressed the fortified memcpy warning for most > builds, but I still see this one with gcc-9: JFYI: as of today I have run Linux Next with `make W=1 allmodconfig` on x86_64 and it seems there are still tons of similar issues which break the build.
On Mon, Jul 17, 2023, at 11:36, Andy Shevchenko wrote: > On Wed, Jul 05, 2023 at 04:02:24PM +0200, Arnd Bergmann wrote: >> From: Arnd Bergmann <arnd@arndb.de> >> >> A previous patch addressed the fortified memcpy warning for most >> builds, but I still see this one with gcc-9: > > JFYI: as of today I have run Linux Next with `make W=1 allmodconfig` on x86_64 > and it seems there are still tons of similar issues which break the build. It's a bit more complex: - yes, there are lots of warnings for memcpy() read overflow when you build allmodconfig kernels with W=1. I have patches for all of these and plan to submit them over time. - This particular one is a memcpy /write/ overflow, i.e. the destination type overflows with the length according to gcc's understanding of the structures. This warning is enabled even without W=1, though it may only show up in certain configurations or compiler versions. Arnd
diff --git a/drivers/hid/hid-hyperv.c b/drivers/hid/hid-hyperv.c index 49d4a26895e76..f33485d83d24f 100644 --- a/drivers/hid/hid-hyperv.c +++ b/drivers/hid/hid-hyperv.c @@ -258,19 +258,17 @@ static void mousevsc_on_receive(struct hv_device *device, switch (hid_msg_hdr->type) { case SYNTH_HID_PROTOCOL_RESPONSE: + len = struct_size(pipe_msg, data, pipe_msg->size); + /* * While it will be impossible for us to protect against * malicious/buggy hypervisor/host, add a check here to * ensure we don't corrupt memory. */ - if (struct_size(pipe_msg, data, pipe_msg->size) - > sizeof(struct mousevsc_prt_msg)) { - WARN_ON(1); + if (WARN_ON(len > sizeof(struct mousevsc_prt_msg))) break; - } - memcpy(&input_dev->protocol_resp, pipe_msg, - struct_size(pipe_msg, data, pipe_msg->size)); + memcpy(&input_dev->protocol_resp, pipe_msg, len); complete(&input_dev->wait_event); break;