Skip to content

Commit 5f15136

Browse files
arndbBenjamin Tissoires
authored andcommitted
HID: hyperv: avoid struct memcpy overrun warning
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: 542f25a ("HID: hyperv: Replace one-element array with flexible-array member") Signed-off-by: Arnd Bergmann <[email protected]> Reviewed-by: Michael Kelley <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Benjamin Tissoires <[email protected]>
1 parent f9abdcc commit 5f15136

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

drivers/hid/hid-hyperv.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,17 @@ static void mousevsc_on_receive(struct hv_device *device,
258258

259259
switch (hid_msg_hdr->type) {
260260
case SYNTH_HID_PROTOCOL_RESPONSE:
261+
len = struct_size(pipe_msg, data, pipe_msg->size);
262+
261263
/*
262264
* While it will be impossible for us to protect against
263265
* malicious/buggy hypervisor/host, add a check here to
264266
* ensure we don't corrupt memory.
265267
*/
266-
if (struct_size(pipe_msg, data, pipe_msg->size)
267-
> sizeof(struct mousevsc_prt_msg)) {
268-
WARN_ON(1);
268+
if (WARN_ON(len > sizeof(struct mousevsc_prt_msg)))
269269
break;
270-
}
271270

272-
memcpy(&input_dev->protocol_resp, pipe_msg,
273-
struct_size(pipe_msg, data, pipe_msg->size));
271+
memcpy(&input_dev->protocol_resp, pipe_msg, len);
274272
complete(&input_dev->wait_event);
275273
break;
276274

0 commit comments

Comments
 (0)