Skip to content

Commit a90118c

Browse files
johnhubbardKAGA-KOKO
authored andcommitted
x86/boot: Save fields explicitly, zero out everything else
Recent gcc compilers (gcc 9.1) generate warnings about an out of bounds memset, if the memset goes accross several fields of a struct. This generated a couple of warnings on x86_64 builds in sanitize_boot_params(). Fix this by explicitly saving the fields in struct boot_params that are intended to be preserved, and zeroing all the rest. [ tglx: Tagged for stable as it breaks the warning free build there as well ] Suggested-by: Thomas Gleixner <[email protected]> Suggested-by: H. Peter Anvin <[email protected]> Signed-off-by: John Hubbard <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent 5ed1c83 commit a90118c

File tree

1 file changed

+48
-15
lines changed

1 file changed

+48
-15
lines changed

arch/x86/include/asm/bootparam_utils.h

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818
* Note: efi_info is commonly left uninitialized, but that field has a
1919
* private magic, so it is better to leave it unchanged.
2020
*/
21+
22+
#define sizeof_mbr(type, member) ({ sizeof(((type *)0)->member); })
23+
24+
#define BOOT_PARAM_PRESERVE(struct_member) \
25+
{ \
26+
.start = offsetof(struct boot_params, struct_member), \
27+
.len = sizeof_mbr(struct boot_params, struct_member), \
28+
}
29+
30+
struct boot_params_to_save {
31+
unsigned int start;
32+
unsigned int len;
33+
};
34+
2135
static void sanitize_boot_params(struct boot_params *boot_params)
2236
{
2337
/*
@@ -35,21 +49,40 @@ static void sanitize_boot_params(struct boot_params *boot_params)
3549
* problems again.
3650
*/
3751
if (boot_params->sentinel) {
38-
/* fields in boot_params are left uninitialized, clear them */
39-
boot_params->acpi_rsdp_addr = 0;
40-
memset(&boot_params->ext_ramdisk_image, 0,
41-
(char *)&boot_params->efi_info -
42-
(char *)&boot_params->ext_ramdisk_image);
43-
memset(&boot_params->kbd_status, 0,
44-
(char *)&boot_params->hdr -
45-
(char *)&boot_params->kbd_status);
46-
memset(&boot_params->_pad7[0], 0,
47-
(char *)&boot_params->edd_mbr_sig_buffer[0] -
48-
(char *)&boot_params->_pad7[0]);
49-
memset(&boot_params->_pad8[0], 0,
50-
(char *)&boot_params->eddbuf[0] -
51-
(char *)&boot_params->_pad8[0]);
52-
memset(&boot_params->_pad9[0], 0, sizeof(boot_params->_pad9));
52+
static struct boot_params scratch;
53+
char *bp_base = (char *)boot_params;
54+
char *save_base = (char *)&scratch;
55+
int i;
56+
57+
const struct boot_params_to_save to_save[] = {
58+
BOOT_PARAM_PRESERVE(screen_info),
59+
BOOT_PARAM_PRESERVE(apm_bios_info),
60+
BOOT_PARAM_PRESERVE(tboot_addr),
61+
BOOT_PARAM_PRESERVE(ist_info),
62+
BOOT_PARAM_PRESERVE(acpi_rsdp_addr),
63+
BOOT_PARAM_PRESERVE(hd0_info),
64+
BOOT_PARAM_PRESERVE(hd1_info),
65+
BOOT_PARAM_PRESERVE(sys_desc_table),
66+
BOOT_PARAM_PRESERVE(olpc_ofw_header),
67+
BOOT_PARAM_PRESERVE(efi_info),
68+
BOOT_PARAM_PRESERVE(alt_mem_k),
69+
BOOT_PARAM_PRESERVE(scratch),
70+
BOOT_PARAM_PRESERVE(e820_entries),
71+
BOOT_PARAM_PRESERVE(eddbuf_entries),
72+
BOOT_PARAM_PRESERVE(edd_mbr_sig_buf_entries),
73+
BOOT_PARAM_PRESERVE(edd_mbr_sig_buffer),
74+
BOOT_PARAM_PRESERVE(e820_table),
75+
BOOT_PARAM_PRESERVE(eddbuf),
76+
};
77+
78+
memset(&scratch, 0, sizeof(scratch));
79+
80+
for (i = 0; i < ARRAY_SIZE(to_save); i++) {
81+
memcpy(save_base + to_save[i].start,
82+
bp_base + to_save[i].start, to_save[i].len);
83+
}
84+
85+
memcpy(boot_params, save_base, sizeof(*boot_params));
5386
}
5487
}
5588

0 commit comments

Comments
 (0)