* [RFC PATCH] gcc-9: workaround array bounds warning on boot_params cleaning
@ 2019-08-06 16:30 Yauheni Kaliuta
0 siblings, 0 replies; 2+ messages in thread
From: Yauheni Kaliuta @ 2019-08-06 16:30 UTC (permalink / raw)
To: linux-kernel; +Cc: Ed Kellett
The code is supposed to clear several fields in the structure with
memset() and it produces the warning:
In file included from arch/x86/kernel/head64.c:17:
In function ‘memset’,
inlined from ‘sanitize_boot_params’ at ./arch/x86/include/asm/bootparam_utils.h:40:3,
inlined from ‘copy_bootdata’ at arch/x86/kernel/head64.c:391:2:
./include/linux/string.h:344:9: warning: ‘__builtin_memset’ offset [197, 448] from the object at ‘boot_params’ is out of the bounds of referenced subobject ‘ext_ramdisk_image’ with type ‘unsigned int’ at offset 192 [-Warray-bounds]
344 | return __builtin_memset(p, c, size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
since gcc is aware of the field sizes of the structure.
Blind gcc treating the structure as a byte array and calculate
positions with offsetof().
Suggested-by: Ed Kellett <e@kellett.im>
Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
---
arch/x86/include/asm/bootparam_utils.h | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/arch/x86/include/asm/bootparam_utils.h b/arch/x86/include/asm/bootparam_utils.h
index 101eb944f13c..16e567a08b54 100644
--- a/arch/x86/include/asm/bootparam_utils.h
+++ b/arch/x86/include/asm/bootparam_utils.h
@@ -37,12 +37,14 @@ static void sanitize_boot_params(struct boot_params *boot_params)
if (boot_params->sentinel) {
/* fields in boot_params are left uninitialized, clear them */
boot_params->acpi_rsdp_addr = 0;
- memset(&boot_params->ext_ramdisk_image, 0,
- (char *)&boot_params->efi_info -
- (char *)&boot_params->ext_ramdisk_image);
- memset(&boot_params->kbd_status, 0,
- (char *)&boot_params->hdr -
- (char *)&boot_params->kbd_status);
+ memset((u8 *)boot_params + offsetof(struct boot_params, ext_ramdisk_image),
+ 0,
+ offsetof(struct boot_params, efi_info) -
+ offsetof(struct boot_params, ext_ramdisk_image));
+ memset((u8 *)boot_params + offsetof(struct boot_params, _pad7[0]),
+ 0,
+ offsetof(struct boot_params, edd_mbr_sig_buffer[0]) -
+ offsetof(struct boot_params, _pad7[0]));
memset(&boot_params->_pad7[0], 0,
(char *)&boot_params->edd_mbr_sig_buffer[0] -
(char *)&boot_params->_pad7[0]);
--
2.22.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [RFC PATCH] gcc-9: workaround array bounds warning on boot_params cleaning
@ 2019-08-06 18:54 Fihlman Emil
0 siblings, 0 replies; 2+ messages in thread
From: Fihlman Emil @ 2019-08-06 18:54 UTC (permalink / raw)
To: yauheni.kaliuta; +Cc: e, linux-kernel
It should also be possibly to simply cast to u64 and then to u8 * unless
the compiler overzealously tracks out of bound accesses even when
6.3.2.3 #5-6 explicitly allows pointer-integer-pointer conversions like
this and removes previous guarantees. Ie.
memset((u8 *)((u64)(&(boot_params->ext_ramdisk_image))), 0, size)
I apologise for not having the in-reply-to header.
Emil Fihlman
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-08-06 19:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-06 16:30 [RFC PATCH] gcc-9: workaround array bounds warning on boot_params cleaning Yauheni Kaliuta
2019-08-06 18:54 Fihlman Emil
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).