mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 2/2] x86/boot/compressed/kaslr: validate indirect setup_data payloads
  2026-04-04 13:48 [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them Pengpeng Hou
@ 2026-04-04  9:32 ` Pengpeng Hou
  2026-04-04 21:53 ` [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them Borislav Petkov
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Pengpeng Hou @ 2026-04-04  9:32 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86
  Cc: H. Peter Anvin, Andy Lutomirski, Peter Zijlstra, linux-kernel, pengpeng

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.

Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
---
 arch/x86/boot/compressed/kaslr.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
index 3b0948ad449f..1205ea588431 100644
--- a/arch/x86/boot/compressed/kaslr.c
+++ b/arch/x86/boot/compressed/kaslr.c
@@ -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;
-- 
2.50.1 (Apple Git-155)



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them
@ 2026-04-04 13:48 Pengpeng Hou
  2026-04-04  9:32 ` [PATCH 2/2] x86/boot/compressed/kaslr: validate indirect setup_data payloads Pengpeng Hou
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Pengpeng Hou @ 2026-04-04 13:48 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86
  Cc: H. Peter Anvin, Andy Lutomirski, Peter Zijlstra, linux-kernel, pengpeng

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.

Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
---
 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(-)

diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
index 914eb32581c7..e309e3e9586c 100644
--- a/arch/x86/include/asm/setup.h
+++ b/arch/x86/include/asm/setup.h
@@ -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) &&
diff --git a/arch/x86/kernel/kdebugfs.c b/arch/x86/kernel/kdebugfs.c
index e14ace32009f..31c15342b4cb 100644
--- a/arch/x86/kernel/kdebugfs.c
+++ b/arch/x86/kernel/kdebugfs.c
@@ -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) {
diff --git a/arch/x86/kernel/ksysfs.c b/arch/x86/kernel/ksysfs.c
index 1a6e1f89f294..eacd13892926 100644
--- a/arch/x86/kernel/ksysfs.c
+++ b/arch/x86/kernel/ksysfs.c
@@ -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)
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index eebcc9db1a1b..c66e337323a6 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -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) {
diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c
index 12c8180ca1ba..8a0b55763d65 100644
--- a/arch/x86/mm/ioremap.c
+++ b/arch/x86/mm/ioremap.c
@@ -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 early
 			return 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);
-- 
2.50.1 (Apple Git-155)



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them
  2026-04-04 13:48 [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them Pengpeng Hou
  2026-04-04  9:32 ` [PATCH 2/2] x86/boot/compressed/kaslr: validate indirect setup_data payloads Pengpeng Hou
@ 2026-04-04 21:53 ` Borislav Petkov
  2026-04-05  1:40 ` Pengpeng Hou
  2026-04-05  3:20 ` Pengpeng Hou
  3 siblings, 0 replies; 7+ messages in thread
From: Borislav Petkov @ 2026-04-04 21:53 UTC (permalink / raw)
  To: Pengpeng Hou
  Cc: Thomas Gleixner, Ingo Molnar, Dave Hansen, x86, H. Peter Anvin,
	Andy Lutomirski, Peter Zijlstra, linux-kernel

On Sat, Apr 04, 2026 at 09:48:44PM +0800, Pengpeng Hou wrote:
> 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.

And? Why do we care?

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them
  2026-04-04 13:48 [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them Pengpeng Hou
  2026-04-04  9:32 ` [PATCH 2/2] x86/boot/compressed/kaslr: validate indirect setup_data payloads Pengpeng Hou
  2026-04-04 21:53 ` [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them Borislav Petkov
@ 2026-04-05  1:40 ` Pengpeng Hou
  2026-04-05 11:06   ` Borislav Petkov
  2026-04-05  3:20 ` Pengpeng Hou
  3 siblings, 1 reply; 7+ messages in thread
From: Pengpeng Hou @ 2026-04-05  1:40 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86
  Cc: H. Peter Anvin, Andy Lutomirski, Peter Zijlstra, linux-kernel, pengpeng

Hi 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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them
  2026-04-04 13:48 [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them Pengpeng Hou
                   ` (2 preceding siblings ...)
  2026-04-05  1:40 ` Pengpeng Hou
@ 2026-04-05  3:20 ` Pengpeng Hou
  3 siblings, 0 replies; 7+ messages in thread
From: Pengpeng Hou @ 2026-04-05  3:20 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86
  Cc: H. Peter Anvin, Andy Lutomirski, Peter Zijlstra, linux-kernel, pengpeng

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them
  2026-04-05  1:40 ` Pengpeng Hou
@ 2026-04-05 11:06   ` Borislav Petkov
  2026-04-06 20:54     ` H. Peter Anvin
  0 siblings, 1 reply; 7+ messages in thread
From: Borislav Petkov @ 2026-04-05 11:06 UTC (permalink / raw)
  To: Pengpeng Hou
  Cc: Thomas Gleixner, Ingo Molnar, Dave Hansen, x86, H. Peter Anvin,
	Andy Lutomirski, Peter Zijlstra, linux-kernel

On Sun, Apr 05, 2026 at 09:40:00AM +0800, Pengpeng Hou wrote:
> 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.

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? 

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them
  2026-04-05 11:06   ` Borislav Petkov
@ 2026-04-06 20:54     ` H. Peter Anvin
  0 siblings, 0 replies; 7+ messages in thread
From: H. Peter Anvin @ 2026-04-06 20:54 UTC (permalink / raw)
  To: Borislav Petkov, Pengpeng Hou
  Cc: Thomas Gleixner, Ingo Molnar, Dave Hansen, x86, Andy Lutomirski,
	Peter Zijlstra, linux-kernel

On April 5, 2026 4:06:52 AM PDT, Borislav Petkov <bp@alien8.de> wrote:
>On Sun, Apr 05, 2026 at 09:40:00AM +0800, Pengpeng Hou wrote:
>> 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.
>
>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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-04-06 21:11 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-04-04 13:48 [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them Pengpeng Hou
2026-04-04  9:32 ` [PATCH 2/2] x86/boot/compressed/kaslr: validate indirect setup_data payloads Pengpeng Hou
2026-04-04 21:53 ` [PATCH 1/2] x86/setup_data: validate indirect entry sizes before dereferencing them Borislav Petkov
2026-04-05  1:40 ` Pengpeng Hou
2026-04-05 11:06   ` Borislav Petkov
2026-04-06 20:54     ` H. Peter Anvin
2026-04-05  3:20 ` Pengpeng Hou

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®