From: Baoli Zhang <baoli.zhang@linux.intel.com>
To: "K. Y. Srinivasan" <kys@microsoft.com>,
Haiyang Zhang <haiyangz@microsoft.com>,
Wei Liu <wei.liu@kernel.org>, Dexuan Cui <decui@microsoft.com>,
Long Li <longli@microsoft.com>, Jiri Kosina <jikos@kernel.org>,
Benjamin Tissoires <bentiss@kernel.org>,
Michael Bommarito <michael.bommarito@gmail.com>,
linux-hyperv@vger.kernel.org, linux-input@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: Baoli Zhang <baoli.zhang@linux.intel.com>
Subject: [PATCH] HID: hyperv: fix fortify write overflow in KUnit device info tests
Date: Mon, 7 Sep 2026 08:46:17 +0800 [thread overview]
Message-ID: <20260907004619.2479833-1-baoli.zhang@linux.intel.com> (raw)
allyesconfig fails to build:
In function 'fortify_memset_chk',
inlined from 'mousevsc_device_info_valid_descriptor':
./include/linux/fortify-string.h:430:25: error: call to
'__write_overflow_field' declared with attribute warning: detected
write beyond size of field (1st parameter); maybe use struct_group()?
Both tests place a report descriptor in bytes allocated past
struct synthhid_device_info, then memset() it, and derive its address
from the hid_descriptor member:
report = ((u8 *)&info->hid_descriptor) + info->hid_descriptor.bLength;
Because that expression is a member reference, the fortify checks bound
the write by sizeof(info->hid_descriptor). bLength is exactly that size
here, so the remaining room in the member is zero and every write is
flagged, even though the memory really came from the caller's
kunit_kzalloc(sizeof(*info) + N).
Compute the address from the start of info instead, mirroring the
desc_offset arithmetic that mousevsc_on_receive_device_info() itself
uses. Deriving from info gives the whole allocation as the object, so
the writes are no longer attributed to a single member. The result is
the same address: (u8 *)&info->hid_descriptor is by definition
(u8 *)info + offsetof(struct synthhid_device_info, hid_descriptor), so
test behaviour is unchanged.
sizeof(*info) deliberately is not used for this: hid_descriptor is
__packed while synthhid_device_info is not, so the struct may carry
trailing padding and the report would land at the wrong offset.
Fixes: 83df7b5fa6735 ("HID: hyperv: add KUnit coverage for device info bounds")
Signed-off-by: Baoli Zhang <baoli.zhang@linux.intel.com>
---
drivers/hid/hid-hyperv.c | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/drivers/hid/hid-hyperv.c b/drivers/hid/hid-hyperv.c
index 6579bd19da13a..f1042b36d10b7 100644
--- a/drivers/hid/hid-hyperv.c
+++ b/drivers/hid/hid-hyperv.c
@@ -657,6 +657,25 @@ static struct mousevsc_dev *mousevsc_kunit_alloc_dev(struct kunit *test)
return input_dev;
}
+/*
+ * Address of the report descriptor that mousevsc_on_receive_device_info()
+ * expects immediately after the HID descriptor.
+ *
+ * Computed from the start of @info, mirroring the desc_offset arithmetic in
+ * mousevsc_on_receive_device_info(), rather than from &info->hid_descriptor.
+ * The report lives in the extra bytes the caller allocated past the struct, so
+ * deriving it from the member would leave the fortify checks bounding writes
+ * by sizeof(info->hid_descriptor) and reject them. Note sizeof(*info) cannot
+ * be used instead: hid_descriptor is packed while synthhid_device_info is not,
+ * so the struct may carry trailing padding.
+ */
+static u8 *mousevsc_kunit_report_desc(struct synthhid_device_info *info)
+{
+ return (u8 *)info +
+ offsetof(struct synthhid_device_info, hid_descriptor) +
+ info->hid_descriptor.bLength;
+}
+
static void mousevsc_device_info_zero_blength(struct kunit *test)
{
struct synthhid_device_info *info;
@@ -687,7 +706,7 @@ static void mousevsc_device_info_valid_descriptor(struct kunit *test)
info->hid_descriptor.bLength = sizeof(struct hid_descriptor);
info->hid_descriptor.rpt_desc.wDescriptorLength = cpu_to_le16(4);
- report = ((u8 *)&info->hid_descriptor) + info->hid_descriptor.bLength;
+ report = mousevsc_kunit_report_desc(info);
memset(report, 0x42, 4);
mousevsc_on_receive_device_info(input_dev, info, sizeof(*info) + 4);
@@ -713,7 +732,7 @@ static void mousevsc_device_info_report_desc_oob(struct kunit *test)
info->hid_descriptor.bLength = sizeof(struct hid_descriptor);
info->hid_descriptor.rpt_desc.wDescriptorLength = cpu_to_le16(64);
- report = ((u8 *)&info->hid_descriptor) + info->hid_descriptor.bLength;
+ report = mousevsc_kunit_report_desc(info);
memset(report, 0x42, 8);
mousevsc_on_receive_device_info(input_dev, info, sizeof(*info) + 8);
--
2.43.0
next reply other threads:[~2026-09-07 0:56 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-07 0:46 Baoli Zhang [this message]
2026-09-07 13:19 ` Benjamin Tissoires
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260907004619.2479833-1-baoli.zhang@linux.intel.com \
--to=baoli.zhang@linux.intel.com \
--cc=bentiss@kernel.org \
--cc=decui@microsoft.com \
--cc=haiyangz@microsoft.com \
--cc=jikos@kernel.org \
--cc=kys@microsoft.com \
--cc=linux-hyperv@vger.kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=longli@microsoft.com \
--cc=michael.bommarito@gmail.com \
--cc=wei.liu@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®