Skip to contentSkip to conversation

lib/earlycpio: Mark find_cpio_data() __no_stack_protector

PATCH In review Borislav Petkov 2 participants 2 messages Aug 16, 2023 – Aug 18, 2023 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/earlycpio: Mark find_cpio_data() __no_stack_protector

From: "Borislav Petkov (AMD)" <[email protected]>

find_cpio_data() is called by the 32-bit x86 microcode loader while paging is not yet enabled and the CPU is running off physical addresses. However, when stack protector is enabled, the compiler adds the stack protection check for this function:

c1846480 <find_cpio_data>:

  c1846480:       55                      push   %ebp
  c1846481:       89 e5                   mov    %esp,%ebp
  c1846483:       57                      push   %edi

...

  c1846676:       e9 85 fe ff ff          jmp    c1846500 <find_cpio_data+0x80>
  c184667b:       e8 20 9e 02 00          call   c18704a0 <__stack_chk_fail>

which cannot work yet.

Prevent that from happening.

---
 lib/earlycpio.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/earlycpio.c +2 -2
index d2c37d64fd0c..d3efe707c3f7 100644
@@ -56,8 +56,8 @@ enum cpio_fields {
  *              the match returned an empty filename string.
  */
 
-struct cpio_data find_cpio_data(const char *path, void *data,
-				size_t len,  long *nextoff)
+struct cpio_data __no_stack_protector find_cpio_data(const char *path, void *data,
+						     size_t len,  long *nextoff)
 {
 	const size_t cpio_header_len = 8*C_NFIELDS - 2;
 	struct cpio_data cd = { NULL, 0, "" };
Signature · 1 line
2.42.0.rc0.25.ga82fb66fed25
#1 lore raw
Quoted 6 lines · Borislav Petkov

From: "Borislav Petkov (AMD)" <[email protected]>

find_cpio_data() is called by the 32-bit x86 microcode loader while paging is not yet enabled and the CPU is running off physical addresses. However, when stack protector is enabled, the compiler adds the stack protection check for this function:

There are a lot more functions which have the same problem.

It's completely unclear to me how this is supposed to work at that point where paging is disabled.

The stackprotector does:

321: 64 8b 35 00 00 00 00 mov %fs:0xc2686834,%esi
328: 89 75 f0 mov %esi,-0x10(%ebp)

....

 531:	8b 75 f0             	mov    -0x10(%ebp),%esi
 534:	64 2b 35 00 00 00 00 	sub    %fs:c2686834,%esi
 53b:	0f 85 42 01 00 00    	jne    683

c2686834 is the compile time address of __stack_chk_guard, which is a per CPU variable.

%fs is __DS_BOOT at that point which uses the boot GDT. __DS_BOOT is 4GB
data rw segment.

So this reads from some random place in memory or if there is not big enough memory it either reads from an alias address or just whatever the hardware decides to read from the void.

IOW, this just works by chance, but certainly not by any form of design.

Not sure what to do about that.

Thanks,

tglx