mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] HID: hyperv: fix fortify write overflow in KUnit device info tests
@ 2026-09-07  0:46 Baoli Zhang
  2026-09-07 13:19 ` Benjamin Tissoires
  0 siblings, 1 reply; 2+ messages in thread
From: Baoli Zhang @ 2026-09-07  0:46 UTC (permalink / raw)
  To: K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
	Jiri Kosina, Benjamin Tissoires, Michael Bommarito, linux-hyperv,
	linux-input, linux-kernel
  Cc: Baoli Zhang

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] HID: hyperv: fix fortify write overflow in KUnit device info tests
  2026-09-07  0:46 [PATCH] HID: hyperv: fix fortify write overflow in KUnit device info tests Baoli Zhang
@ 2026-09-07 13:19 ` Benjamin Tissoires
  0 siblings, 0 replies; 2+ messages in thread
From: Benjamin Tissoires @ 2026-09-07 13:19 UTC (permalink / raw)
  To: Baoli Zhang
  Cc: K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
	Jiri Kosina, Michael Bommarito, linux-hyperv, linux-input,
	linux-kernel

On Sep 07 2026, Baoli Zhang wrote:
> 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>

Please always check is a patch is not already pending in linux-next or
in mainline linux.

d0ad81b2b5fe ("HID: hyperv: make pointer arithmetics understandable for
FORTIFY_SOURCE") already fixes this very same issue.

Cheers,
Benjamin

> ---
>  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
> 
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-09-07 13:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-07  0:46 [PATCH] HID: hyperv: fix fortify write overflow in KUnit device info tests Baoli Zhang
2026-09-07 13:19 ` Benjamin Tissoires

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®