x86/setup_data: validate indirect entry sizes before dereferencing them
PATCH 1/2 x86/setup_data: validate indirect entry sizes before dereferencing them
Several x86 setup_data consumers treat SETUP_INDIRECT entries as though struct setup_indirect is always fully present once the outer setup_data header has been read.
That assumption is too strong. A malformed boot-time setup_data entry can carry a short or overflowing data->len, causing the kernel to remap less than a full indirect header and then dereference indirect->type, addr, or len outside the mapped range.
Add a small shared helper for validating setup_data entry sizes, and use it before remapping or dereferencing indirect payloads in the runtime x86 setup_data readers. When an indirect entry is malformed, keep treating the outer setup_data payload as opaque data instead of walking the embedded header.
For setup_data reservation, still reserve the outer entry header before skipping malformed oversized payloads so early allocations cannot overlap the bootloader metadata itself.
--- arch/x86/include/asm/setup.h | 17 +++++++++++++++++ arch/x86/kernel/kdebugfs.c | 5 ++--- arch/x86/kernel/ksysfs.c | 16 +++++++--------- arch/x86/kernel/setup.c | 16 +++++++++++----- arch/x86/mm/ioremap.c | 5 ++--- 5 files changed, 39 insertions(+), 20 deletions(-)
arch/x86/include/asm/setup.h +17 -0
@@ -29,6 +29,7 @@#ifndef __ASSEMBLER__#include <linux/cache.h> +#include <linux/overflow.h> #include <asm/bootparam.h> #include <asm/x86_init.h>
@@ -82,6 +83,22 @@ static inline void x86_ce4100_early_setup(void) { }extern struct boot_params boot_params;extern char _text[]; +static inline bool setup_data_entry_size (u32 data_len,size_t *size) +{ +return !check_add_overflow (sizeof (struct setup_data ), (size_t )data_len, + size); +} + +static inline bool setup_data_indirect_valid (const struct setup_data *data, +size_t *size) +{ +if (data->type !=SETUP_INDIRECT ) +return false; +if (data->len <sizeof (struct setup_indirect )) +return false; +return setup_data_entry_size (data->len, size); +} +static inline bool kaslr_enabled (void ) { return IS_ENABLED(CONFIG_RANDOMIZE_MEMORY) &&
arch/x86/kernel/kdebugfs.c +2 -3
@@ -94,7 +94,7 @@ static int __init create_setup_data_nodes(struct dentry *parent)u64 pa_data, pa_next;struct dentry *d;int error; -u32 len; +size_t len;int no =0 ; d =debugfs_create_dir ("setup_data" , parent);
@@ -116,8 +116,7 @@ static int __init create_setup_data_nodes(struct dentry *parent) } pa_next = data->next; -if (data->type ==SETUP_INDIRECT ) { - len =sizeof (*data) + data->len; +if (setup_data_indirect_valid (data, &len)) {memunmap (data); data =memremap (pa_data, len,MEMREMAP_WB );if (!data) {
arch/x86/kernel/ksysfs.c +7 -9
@@ -95,7 +95,7 @@ static int __init get_setup_data_size(int nr, size_t *size)struct setup_indirect *indirect;struct setup_data *data;int i =0 ; -u32 len; +size_t len;while (pa_data) { data =memremap (pa_data,sizeof (*data),MEMREMAP_WB );
@@ -104,8 +104,7 @@ static int __init get_setup_data_size(int nr, size_t *size) pa_next = data->next;if (nr == i) { -if (data->type ==SETUP_INDIRECT ) { - len =sizeof (*data) + data->len; +if (setup_data_indirect_valid (data, &len)) {memunmap (data); data =memremap (pa_data, len,MEMREMAP_WB );if (!data)
@@ -139,7 +138,7 @@ static ssize_t type_show(struct kobject *kobj,struct setup_data *data;int nr, ret;u64 paddr; -u32 len; +size_t len; ret =kobj_to_setup_data_nr (kobj, &nr);if (ret)
@@ -152,8 +151,7 @@ static ssize_t type_show(struct kobject *kobj,if (!data)return -ENOMEM ; -if (data->type ==SETUP_INDIRECT ) { - len =sizeof (*data) + data->len; +if (setup_data_indirect_valid (data, &len)) {memunmap (data); data =memremap (paddr, len,MEMREMAP_WB );if (!data)
@@ -179,7 +177,8 @@ static ssize_t setup_data_data_read(struct file *fp,struct setup_indirect *indirect;struct setup_data *data;int nr, ret =0 ; -u64 paddr, len; +u64 paddr; +size_t len;void *p; ret =kobj_to_setup_data_nr (kobj, &nr);
@@ -193,8 +192,7 @@ static ssize_t setup_data_data_read(struct file *fp,if (!data)return -ENOMEM ; -if (data->type ==SETUP_INDIRECT ) { - len =sizeof (*data) + data->len; +if (setup_data_indirect_valid (data, &len)) {memunmap (data); data =memremap (paddr, len,MEMREMAP_WB );if (!data)
arch/x86/kernel/setup.c +11 -5
@@ -570,7 +570,7 @@ static void __init memblock_x86_reserve_range_setup_data(void)struct setup_indirect *indirect;struct setup_data *data;u64 pa_data, pa_next; -u32 len; +size_t len, data_len; pa_data = boot_params.hdr.setup_data;while (pa_data) {
@@ -580,13 +580,19 @@ static void __init memblock_x86_reserve_range_setup_data(void)return ; } - len =sizeof (*data); pa_next = data->next; +if (!setup_data_entry_size (data->len, &data_len)) { +memblock_reserve_kern (pa_data,sizeof (*data)); +pr_warn ("setup: ignoring setup_data entry with oversized length\n" ); +early_memunmap (data,sizeof (*data)); + pa_data = pa_next; +continue ; + } -memblock_reserve_kern (pa_data,sizeof (*data) + data->len); + len =sizeof (*data); +memblock_reserve_kern (pa_data, data_len); -if (data->type ==SETUP_INDIRECT ) { - len += data->len; +if (setup_data_indirect_valid (data, &len)) {early_memunmap (data,sizeof (*data)); data =early_memremap (pa_data, len);if (!data) {
arch/x86/mm/ioremap.c +2 -3
@@ -647,7 +647,7 @@ static bool __ref __memremap_is_setup_data(resource_size_t phys_addr, bool early paddr = boot_params.hdr.setup_data;while (paddr) { -unsigned int len, size; +size_t len, size;if (phys_addr == paddr)return true;
@@ -675,8 +675,7 @@ static bool __ref __memremap_is_setup_data(resource_size_t phys_addr, bool earlyreturn true; } -if (data->type ==SETUP_INDIRECT ) { - size += len; +if (setup_data_indirect_valid (data, &size)) {if (early) {early_memunmap (data, setup_data_sz); data =early_memremap_decrypted (paddr, size);
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH 2/2 x86/boot/compressed/kaslr: validate indirect setup_data payloads
mem_avoid_overlap() checks SETUP_INDIRECT entries in the boot-time setup_data chain and reads an embedded struct setup_indirect directly from ptr->data.
If ptr->len is smaller than sizeof(struct setup_indirect), that access can run past the setup_data payload before compressed x86 boot has finished walking the list.
Require the indirect payload to be large enough before dereferencing it. Short entries are still covered by the outer setup_data avoidance range, so this only drops the unsafe embedded-header walk.
--- arch/x86/boot/compressed/kaslr.c | 1 + 1 file changed, 1 insertion(+)
arch/x86/boot/compressed/kaslr.c +1 -0
@@ -432,6 +432,7 @@ static bool mem_avoid_overlap(struct mem_vector *img, }if (ptr->type ==SETUP_INDIRECT && + ptr->len >=sizeof (struct setup_indirect ) && ((struct setup_indirect *)ptr->data)->type !=SETUP_INDIRECT ) { avoid.start = ((struct setup_indirect *)ptr->data)->addr; avoid.size = ((struct setup_indirect *)ptr->data)->len;
Signature · 1 line
2.50.1 (Apple Git-155)
Borislav Petkov
maintainerQuoted 8 lines · Pengpeng Hou
Several x86
setup_dataconsumers treatSETUP_INDIRECTentries as though structsetup_indirectis always fully present once the outersetup_dataheader has been read.That assumption is too strong. A malformed boot-time
setup_dataentry can carry a short or overflowingdata->len, causing the kernel to remap less than a full indirect header and then dereferenceindirect->type, addr, or len outside the mapped range.
And? Why do we care?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteHi Boris,
Because the failure mode here is not just “the bootloader passed nonsense” in a semantic sense; the current code can dereference past the mapped payload of a malformed SETUP_INDIRECT entry.
The affected paths first read the outer struct setup_data header, and then some of them treat data->len as though it always covers a full struct setup_indirect. If data->len is short, or wraps when sizeof(struct setup_data) + data->len is computed, the kernel can remap or reserve less than a full indirect header and still read indirect->type, addr, or len beyond that range.
So the thing I was trying to harden is not “make every bad setup_data entry boot successfully”; it is “do not turn malformed boot metadata into a kernel OOB read while walking setup_data”.
I think that is worth handling because setup_data is still external boot input to the kernel. It can come not only from a normal bootloader path, but also from kexec-style handoff and virtualized boot flows.
The intended fallback here is narrow and local: if an indirect entry is malformed, keep treating the outer setup_data payload as opaque instead of walking the embedded header. That way we avoid the out-of-bounds read without trying to make malformed metadata otherwise “work”.
Thanks,
Pengpeng
Borislav Petkov
maintainerQuoted 3 lines · Pengpeng Hou
I think that is worth handling because
setup_datais still external boot input to the kernel. It can come not only from a normal bootloader path, but also from kexec-style handoff and virtualized boot flows.
I'm asking you to explain the attack vector in detail. In which of those examples don't you need physical access to the machine and root?
IOW, what scenario exactly are we protecting here against?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteH. Peter Anvin
reviewerQuoted 10 lines · Borislav Petkov
Quoted 3 lines · Pengpeng Hou
I think that is worth handling because
setup_datais still external boot input to the kernel. It can come not only from a normal bootloader path, but also from kexec-style handoff and virtualized boot flows.I'm asking you to explain the attack vector in detail. In which of those examples don't you need physical access to the machine and root?
IOW, what scenario exactly are we protecting here against?
Sounds like he is more concerned about data corruption than malicious data. At least one possible cause could be a DMA device that has not been properly shut down before kdump – that's a known problem with no good solution and thus is probabilistic.
What is the likelihood that these sanity checks would catch that specific kind of corruption and still be able to produce a meaningful dump? Probably infinitesimal.
Hi Boris,
You're right to press on the threat model here.
I do not have a compelling scenario for this series that avoids already privileged control over the boot-time state. The setup_data chain is boot metadata coming from the bootloader / kexec handoff / virtualized boot path, so this is better characterized as a robustness issue against malformed setup_data than as a meaningful security boundary for an unprivileged attacker.
So while I still think the size assumptions are too strong in these callers, I do not think I have a strong enough answer to "why should x86 care?" in the way you are asking.
I will drop the whole series.
Thanks,
Pengpeng