HID: hyperv: fix fortify write overflow in KUnit device info tests
Subsystems (3)
PATCH 1/1 HID: hyperv: fix fortify write overflow in KUnit device info tests
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.
--- drivers/hid/hid-hyperv.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/hid/hid-hyperv.c +21 -2
@@ -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 );
Signature · 1 line
2.43.0
Benjamin Tissoires
maintainerQuoted 28 lines · 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 usestruct_group()?Both tests place a report descriptor in bytes allocated past struct
synthhid_device_info, thenmemset()it, and derive its address from thehid_descriptormember: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'skunit_kzalloc(sizeof(*info) + N).Compute the address from the start of info instead, mirroring the
desc_offsetarithmetic thatmousevsc_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.
Quoted 6 lines
sizeof(*info)deliberately is not used for this:hid_descriptoris__packedwhilesynthhid_device_infois 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 <[email protected]>
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
Quoted 56 lines
--- drivers/hid/hid-hyperv.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-)drivers/hid/hid-hyperv.c +21
-2@@ -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 );Signature · 1 line
2.43.0