Skip to contentSkip to conversation

x86/boot: fix relying on link order

PATCH Superseded Alexander Lobakin 4 participants 13 messages Nov 1, 2022 – Dec 7, 2022 x86

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover x86/boot: fix relying on link order

The series contains stuff I caught last week while working on some x86 code. It can be considered a material for fixes or for next, I'm okay with either, although leaning more towards fixes :P

Notes on patches:
* 0001: I didn't put any "Fixes:" tag since it's not linear. The lines changed with this patch came from the initial x86 KASLR series, but that unconditional jump to the kernel beginning already was there. It goes at least from the set that brought relocatable kernel support to x86, but this is quite prehistoric already and might not look really relatable. So up to you whether it needs any.
* 0002: doesn't fix anything, except that having any files listed in that whitelist already is an architecture bug :D And it wouldn't be convenient to decouple it from #0001, but up to you as well.

From v1[0]:
* collect the Tested-by tags (Jiri);
* don't add pathetic returns after noreturn error() (Jiri);
* debug-print the entry point offset via debug_putaddr() before booting (Jiri);
* always have an empty line before return statements (Jiri).

[0] https://lore.kernel.org/all/[email protected]

Alexander Lobakin (2):
x86/boot: robustify calling startup_{32,64}() from the decompressor
code
scripts/head-object-list: remove x86 from the list

 arch/x86/boot/compressed/head_32.S |  2 +-
 arch/x86/boot/compressed/head_64.S |  2 +-
 arch/x86/boot/compressed/misc.c    | 16 ++++++++++------
 scripts/head-object-list.txt       |  6 ------
 4 files changed, 12 insertions(+), 14 deletions(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH v2 1/2 x86/boot: robustify calling startup_{32,64}() from the decompressor code

After commit ce697ccee1a8 ("kbuild: remove head-y syntax"), I started digging whether x86 is ready for removing this old cruft. Removing its objects from the list makes the kernel unbootable. This applies only to bzImage, vmlinux still works correctly. The reason is that with no strict object order determined by the linker arguments, not the linker script, startup_64 can be placed not right at the beginning of the kernel.
Here's vmlinux.map's beginning before removing:

ffffffff81000000 vmlinux.o:(.head.text)

ffffffff81000000                 startup_64
ffffffff81000070                 secondary_startup_64
ffffffff81000075                 secondary_startup_64_no_verify
ffffffff81000160                 verify_cpu

and after:

ffffffff81000000 vmlinux.o:(.head.text)

ffffffff81000000                 pvh_start_xen
ffffffff81000080                 startup_64
ffffffff810000f0                 secondary_startup_64
ffffffff810000f5                 secondary_startup_64_no_verify

Not a problem itself, but the self-extractor code has the address of that function hardcoded the beginning, not looking onto the ELF header, which always contains the address of startup_{32,64}().

So, instead of doing an "act of blind faith", just take the address from the ELF header and extract a relative offset to the entry point. The decompressor function already returns a pointer to the beginning of the kernel to the Asm code, which then jumps to it, so add that offset to the return value.
This doesn't change anything for now, but allows to resign from the "head object list" for x86 and makes sure valid Kbuild or any other improvements won't break anything here in general.

---
 arch/x86/boot/compressed/head_32.S |  2 +-
 arch/x86/boot/compressed/head_64.S |  2 +-
 arch/x86/boot/compressed/misc.c    | 16 ++++++++++------
 3 files changed, 12 insertions(+), 8 deletions(-)
arch/x86/boot/compressed/head_32.S +1 -1
index 3b354eb9516d..56f9847e208b 100644
@@ -187,7 +187,7 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
 	leal	boot_heap@GOTOFF(%ebx), %eax
 	pushl	%eax			/* heap area */
 	pushl	%esi			/* real mode pointer */
-	call	extract_kernel		/* returns kernel location in %eax */
+	call	extract_kernel		/* returns kernel entry point in %eax */
 	addl	$24, %esp
 
 /*
arch/x86/boot/compressed/head_64.S +1 -1
index d33f060900d2..aeba5aa3d26c 100644
@@ -593,7 +593,7 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
 	movl	input_len(%rip), %ecx	/* input_len */
 	movq	%rbp, %r8		/* output target address */
 	movl	output_len(%rip), %r9d	/* decompressed length, end of relocs */
-	call	extract_kernel		/* returns kernel location in %rax */
+	call	extract_kernel		/* returns kernel entry point in %rax */
 	popq	%rsi
 
 /*
arch/x86/boot/compressed/misc.c +10 -6
index cf690d8712f4..2548d7fb243e 100644
@@ -277,7 +277,7 @@ static inline void handle_relocations(void *output, unsigned long output_len,
 { }
 #endif
 
-static void parse_elf(void *output)
+static size_t parse_elf(void *output)
 {
 #ifdef CONFIG_X86_64
 	Elf64_Ehdr ehdr;
@@ -287,16 +287,15 @@ static void parse_elf(void *output)
 	Elf32_Phdr *phdrs, *phdr;
 #endif
 	void *dest;
+	size_t off;
 	int i;
 
 	memcpy(&ehdr, output, sizeof(ehdr));
 	if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
 	   ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
 	   ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
-	   ehdr.e_ident[EI_MAG3] != ELFMAG3) {
+	   ehdr.e_ident[EI_MAG3] != ELFMAG3)
 		error("Kernel is not a valid ELF file");
-		return;
-	}
 
 	debug_putstr("Parsing ELF... ");
 
@@ -305,6 +304,7 @@ static void parse_elf(void *output)
 		error("Failed to allocate space for phdrs");
 
 	memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
+	off = ehdr.e_entry - phdrs->p_paddr;
 
 	for (i = 0; i < ehdr.e_phnum; i++) {
 		phdr = &phdrs[i];
@@ -328,6 +328,8 @@ static void parse_elf(void *output)
 	}
 
 	free(phdrs);
+
+	return off;
 }
 
 /*
@@ -356,6 +358,7 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
 	const unsigned long kernel_total_size = VO__end - VO__text;
 	unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
 	unsigned long needed_size;
+	size_t off;
 
 	/* Retain x86 boot parameters pointer passed from startup_32/64. */
 	boot_params = rmode;
@@ -456,14 +459,15 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
 	debug_putstr("\nDecompressing Linux... ");
 	__decompress(input_data, input_len, NULL, NULL, output, output_len,
 			NULL, error);
-	parse_elf(output);
+	off = parse_elf(output);
+	debug_putaddr(off);
 	handle_relocations(output, output_len, virt_addr);
 	debug_putstr("done.\nBooting the kernel.\n");
 
 	/* Disable exception handling before booting the kernel */
 	cleanup_exception_handling();
 
-	return output;
+	return output + off;
 }
 
 void fortify_panic(const char *name)
Signature · 1 line
2.38.1
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] x86/boot: robustify calling startup_{32,64}() from the decompressor code

Quoted 36 lines · Alexander Lobakin

After commit ce697ccee1a8 ("kbuild: remove head-y syntax"), I started digging whether x86 is ready for removing this old cruft. Removing its objects from the list makes the kernel unbootable. This applies only to bzImage, vmlinux still works correctly. The reason is that with no strict object order determined by the linker arguments, not the linker script, startup_64 can be placed not right at the beginning of the kernel.
Here's vmlinux.map's beginning before removing:

ffffffff81000000 vmlinux.o:(.head.text)

ffffffff81000000                 startup_64
ffffffff81000070                 secondary_startup_64
ffffffff81000075                 secondary_startup_64_no_verify
ffffffff81000160                 verify_cpu

and after:

ffffffff81000000 vmlinux.o:(.head.text)

ffffffff81000000                 pvh_start_xen
ffffffff81000080                 startup_64
ffffffff810000f0                 secondary_startup_64
ffffffff810000f5                 secondary_startup_64_no_verify

Not a problem itself, but the self-extractor code has the address of that function hardcoded the beginning, not looking onto the ELF header, which always contains the address of startup_{32,64}().

So, instead of doing an "act of blind faith", just take the address from the ELF header and extract a relative offset to the entry point. The decompressor function already returns a pointer to the beginning of the kernel to the Asm code, which then jumps to it, so add that offset to the return value.
This doesn't change anything for now, but allows to resign from the "head object list" for x86 and makes sure valid Kbuild or any other improvements won't break anything here in general.

Quoted 2 lines

thanks,

Signature · 2 lines
js
suse labs
#3 lore raw

PATCH v2 2/2 scripts/head-object-list: remove x86 from the list

In reply to Alexander Lobakin

Now that x86 boot code is not hardcoded to the particular linking order, remove x86 files from the list and let them be placed inside the vmlinux according only to the linker script and linker preferences.

---
 scripts/head-object-list.txt | 6 ------
 1 file changed, 6 deletions(-)
scripts/head-object-list.txt +0 -6
index b16326a92c45..405d2942898a 100644
@@ -44,10 +44,4 @@ arch/s390/kernel/head64.o
 arch/sh/kernel/head_32.o
 arch/sparc/kernel/head_32.o
 arch/sparc/kernel/head_64.o
-arch/x86/kernel/head_32.o
-arch/x86/kernel/head_64.o
-arch/x86/kernel/head32.o
-arch/x86/kernel/head64.o
-arch/x86/kernel/ebda.o
-arch/x86/kernel/platform-quirks.o
 arch/xtensa/kernel/head.o
Signature · 1 line
2.38.1
#4 lore raw
In reply to Alexander Lobakin

From: Alexander Lobakin <[email protected]>
Date: Tue, 1 Nov 2022 17:15:27 +0100

Quoted 31 lines

The series contains stuff I caught last week while working on some x86 code. It can be considered a material for fixes or for next, I'm okay with either, although leaning more towards fixes :P

Notes on patches:
* 0001: I didn't put any "Fixes:" tag since it's not linear. The lines changed with this patch came from the initial x86 KASLR series, but that unconditional jump to the kernel beginning already was there. It goes at least from the set that brought relocatable kernel support to x86, but this is quite prehistoric already and might not look really relatable. So up to you whether it needs any.
* 0002: doesn't fix anything, except that having any files listed in that whitelist already is an architecture bug :D And it wouldn't be convenient to decouple it from #0001, but up to you as well.

From v1[0]:
* collect the Tested-by tags (Jiri);
* don't add pathetic returns after noreturn error() (Jiri);
* debug-print the entry point offset via debug_putaddr() before booting (Jiri);
* always have an empty line before return statements (Jiri).

[0] https://lore.kernel.org/all/[email protected]

Alexander Lobakin (2):
x86/boot: robustify calling startup_{32,64}() from the decompressor
code
scripts/head-object-list: remove x86 from the list

Quoted 5 lines
 arch/x86/boot/compressed/head_32.S |  2 +-
 arch/x86/boot/compressed/head_64.S |  2 +-
 arch/x86/boot/compressed/misc.c    | 16 ++++++++++------
 scripts/head-object-list.txt       |  6 ------
 4 files changed, 12 insertions(+), 14 deletions(-)

Ping?

Also, I managed to remove .head.text at all from x86. Would it be better to respin this series with the third patch or send a follow-up later?

Quoted 3 lines
Signature · 2 lines
-- 
2.38.1

Thanks,
Olek

#5 lore raw
Quoted 6 lines · Alexander Lobakin

Ping?

Also, I managed to remove .head.text at all from x86. Would it be better to respin this series with the third patch or send a follow-up later?

Hi Alexander,

Things are a bit busy in the review queue at the moment. As always, we'd love help reviewing stuff. So, while you're waiting for us to review this, could you perhaps look around and find a series that's also hurting for review tags?

#6 lore raw

From: Dave Hansen <[email protected]>
Date: Tue, 8 Nov 2022 15:09:07 -0800

Quoted 9 lines · Alexander Lobakin
Quoted 6 lines

Ping?

Also, I managed to remove .head.text at all from x86. Would it be better to respin this series with the third patch or send a follow-up later?

Hi Alexander,

Hey,

Quoted 5 lines

Things are a bit busy in the review queue at the moment. As always, we'd love help reviewing stuff. So, while you're waiting for us to review this, could you perhaps look around and find a series that's also hurting for review tags?

I've got Reviewed-by and Tested-by from Jiri, isn't that enough? Or I need also some other group to get tags from?

Thanks,
Olek

maintainer
#7 lore raw
Quoted 7 lines · Alexander Lobakin
Quoted 4 lines

Things are a bit busy in the review queue at the moment. As always, we'd love help reviewing stuff. So, while you're waiting for us to review this, could you perhaps look around and find a series that's also hurting for review tags?

I've got Reviewed-by and Tested-by from Jiri, isn't that enough? Or I need also some other group to get tags from?

What he actually means is if *you* yourself help out with patch review. Like find a set on lkml which you're interested in - I believe there will be no shortage of such sets - and poke at it, review it, ask devil's advocate questions, etc.

The distribution of work - gazillion submitters vs a handful of maintainers simply cannot scale and instead of submitters pinging maintainers all the time when they can look at their set, submitters could review other submitters' work in the meantime, while waiting.

I.e., a win-win-win situation. :-)

Makes more sense?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#8 lore raw

From: Borislav Petkov <[email protected]>
Date: Mon, 21 Nov 2022 14:14:43 +0100

Quoted 14 lines · Alexander Lobakin
Quoted 7 lines
Quoted 4 lines

Things are a bit busy in the review queue at the moment. As always, we'd love help reviewing stuff. So, while you're waiting for us to review this, could you perhaps look around and find a series that's also hurting for review tags?

I've got Reviewed-by and Tested-by from Jiri, isn't that enough? Or I need also some other group to get tags from?

What he actually means is if *you* yourself help out with patch review. Like find a set on lkml which you're interested in - I believe there will be no shortage of such sets - and poke at it, review it, ask devil's advocate questions, etc.

Quoted 8 lines

The distribution of work - gazillion submitters vs a handful of maintainers simply cannot scale and instead of submitters pinging maintainers all the time when they can look at their set, submitters could review other submitters' work in the meantime, while waiting.

I.e., a win-win-win situation. :-)

Makes more sense?

I know, I got it from the first read :D I try to review stuff I have mature knowledge in each day, not that lots of them are from the x86 ML :s

Quoted 6 lines
Signature · 5 lines
-- 
Regards/Gruss,
    Boris.

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

Thanks,
Olek

#9 lore raw

From: Alexander Lobakin <[email protected]>
Date: Mon, 21 Nov 2022 17:00:30 +0100

Quoted 8 lines

From: Borislav Petkov <[email protected]>
Date: Mon, 21 Nov 2022 14:14:43 +0100

Quoted 5 lines · Alexander Lobakin
Quoted 4 lines
Quoted 4 lines

Things are a bit busy in the review queue at the moment. As always, we'd love help reviewing stuff. So, while you're waiting for us to review this, could you perhaps look around and find a series that's also hurting for review tags?

[...]

Quoted 3 lines

I know, I got it from the first read :D I try to review stuff I have mature knowledge in each day, not that lots of them are from the x86 ML :s

I was hoping it would hit one of the 6.1 RCs as a fix, oh well :D Why did some other fixes hit the x86 tree during that time then?

Quoted 10 lines
Quoted 7 lines
Signature · 5 lines
-- 
Regards/Gruss,
    Boris.

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

Thanks,
Olek

Thanks,
Olek

maintainer
#10 lore raw
Quoted 1 line · Alexander Lobakin

I was hoping it would hit one of the 6.1 RCs as a fix,

As a fix for which existing configuration which breaks if this fix is missing?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#11 lore raw

From: Borislav Petkov <[email protected]>
Date: Wed, 7 Dec 2022 16:24:00 +0100

Quoted 5 lines · Alexander Lobakin
Quoted 1 line

I was hoping it would hit one of the 6.1 RCs as a fix,

As a fix for which existing configuration which breaks if this fix is missing?

Ugh, fair enough :D Without it, FG-KASLR is broken, GCC-LTO is broken, but none of them is in the mainline.
I recall there were some folks with LLD for which this $(head-y) removal caused issues as well. But if they're quiet now, seems like they don't hardly need it.
But not every fix is a fix only when it's easy to find a repro, right...? But at least such are not urgent, you're correct here.

Quoted 6 lines
Signature · 5 lines
-- 
Regards/Gruss,
    Boris.

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

Thanks,
Olek

maintainer
#12 lore raw
Quoted 2 lines · Alexander Lobakin

But not every fix is a fix only when it's easy to find a repro, right...?

The basic rule is: only regression fixes during the -rc phase.

Documentation/process/2.Process.rst

although there it is not spelled out that it is regressions only. But this is the rule we adhere to in the tip tree in general.

Signature · 4 lines
Regards/Gruss,
    Boris.

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