RE: [PATCH v2] x86/boot: save fields explicitly, zero out everything else

From: David Laight
Date: Wed Aug 07 2019 - 07:41:29 EST


From: john.hubbard@xxxxxxxxx
> Sent: 31 July 2019 06:46
>
> Recent gcc compilers (gcc 9.1) generate warnings about an
> out of bounds memset, if you trying memset across several fields
> of a struct. This generated a couple of warnings on x86_64 builds.
>
> Fix this by explicitly saving the fields in struct boot_params
> that are intended to be preserved, and zeroing all the rest.
>
> Suggested-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> Suggested-by: H. Peter Anvin <hpa@xxxxxxxxx>
> Signed-off-by: John Hubbard <jhubbard@xxxxxxxxxx>
> ---
> arch/x86/include/asm/bootparam_utils.h | 62 +++++++++++++++++++-------
> 1 file changed, 47 insertions(+), 15 deletions(-)
>
> diff --git a/arch/x86/include/asm/bootparam_utils.h b/arch/x86/include/asm/bootparam_utils.h
> index 101eb944f13c..514aee24b8de 100644
> --- a/arch/x86/include/asm/bootparam_utils.h
> +++ b/arch/x86/include/asm/bootparam_utils.h
> @@ -18,6 +18,20 @@
> * Note: efi_info is commonly left uninitialized, but that field has a
> * private magic, so it is better to leave it unchanged.
> */
> +
> +#define sizeof_mbr(type, member) ({ sizeof(((type *)0)->member); })
> +
> +#define BOOT_PARAM_PRESERVE(struct_member) \
> + { \
> + .start = offsetof(struct boot_params, struct_member), \
> + .len = sizeof_mbr(struct boot_params, struct_member), \
> + }
> +
> +struct boot_params_to_save {
> + unsigned int start;
> + unsigned int len;
> +};
> +
> static void sanitize_boot_params(struct boot_params *boot_params)
> {
> /*
> @@ -35,21 +49,39 @@ static void sanitize_boot_params(struct boot_params *boot_params)
> * problems again.
> */
> 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(&boot_params->_pad7[0], 0,
> - (char *)&boot_params->edd_mbr_sig_buffer[0] -
> - (char *)&boot_params->_pad7[0]);
> - memset(&boot_params->_pad8[0], 0,
> - (char *)&boot_params->eddbuf[0] -
> - (char *)&boot_params->_pad8[0]);
> - memset(&boot_params->_pad9[0], 0, sizeof(boot_params->_pad9));
...

How about replacing the above first using:
#define zero_struct_fields(ptr, from, to) memset(&ptr->from, 0, (char *)&ptr->to - (char *)&ptr->from)
zero_struct_fields(boot_params, ext_ramdisk_image, efi_info);
...
Which is absolutely identical to the original code.

The replacing the define with:
#define so(s, m) offsetof(typeof(*s), m)
#define zero_struct_fields(ptr, from, to) memset((char *)ptr + so(ptr, from), 0, so(ptr, to) - so(ptr, from))
which gcc probably doesn't complain about, but should generate identical code again.
There might be an existing define for so().

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)