Skip to contentSkip to conversation

x86/boot: Improve MBI2 structure check (was: Reduce assembly code)

PATCH Superseded Frediano Ziglio 2 participants 5 messages Oct 10, 2024 – Oct 15, 2024

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#3 lore raw

PATCH v9 2/2 x86/boot: Improve MBI2 structure check

In reply to Frediano Ziglio

Tag structure should contain at least the tag header. Entire tag structure must be contained inside MBI2 data.

---
Changes since v6:
- compare against total_size every time to avoid overflows.

Changes since v7:
- check for tag type being MULTIBOOT2_TAG_TYPE_END before checking tag size. This allows caller to not fill full tag (type field is before size field).

---
 xen/arch/x86/efi/mbi2.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
xen/arch/x86/efi/mbi2.c +5 -2
index 55a1777483..d2f0f0ee61 100644
@@ -13,6 +13,7 @@ efi_multiboot2_prelude(uint32_t magic, const multiboot2_fixed_t *mbi)
     EFI_HANDLE ImageHandle = NULL;
     EFI_SYSTEM_TABLE *SystemTable = NULL;
     const char *cmdline = NULL;
+    const void *const mbi_raw = (const void *)mbi;
     bool have_bs = false;
 
     if ( magic != MULTIBOOT2_BOOTLOADER_MAGIC )
@@ -21,8 +22,10 @@ efi_multiboot2_prelude(uint32_t magic, const multiboot2_fixed_t *mbi)
     /* Skip Multiboot2 information fixed part. */
     tag = _p(ROUNDUP((unsigned long)(mbi + 1), MULTIBOOT2_TAG_ALIGN));
 
-    for ( ; (const void *)tag - (const void *)mbi < mbi->total_size &&
-            tag->type != MULTIBOOT2_TAG_TYPE_END;
+    for ( ; (const void *)(tag + 1) - mbi_raw <= mbi->total_size &&
+            tag->type != MULTIBOOT2_TAG_TYPE_END &&
+            tag->size >= sizeof(*tag) &&
+            (const void *)tag + tag->size - mbi_raw <= mbi->total_size;
           tag = _p(ROUNDUP((unsigned long)tag + tag->size,
                    MULTIBOOT2_TAG_ALIGN)) )
     {
Signature · 1 line
2.34.1
#1 lore raw

PATCH v9 1/2 x86/boot: Align mbi2.c stack to 16 bytes

In reply to Frediano Ziglio

Doing previous testing with an Alder Lake Intel machine the with "x86/boot: Improve MBI2 structure check" commit test started to fail. Removing the commit makes the tests succeed however there was not apparent reason (looking at the code) for the failure.
So I instrumented code to output the structure and tested code with this extracted data with and without the mentioned commit and results were the same.
Compiled assembly code from lab was also fine beside not keeping the 16-byte alignment for the stack.
Turning on stack alignment solve the problem on Alder Lake machine.

Fixes eb21ce14d709 ('x86/boot: Rewrite EFI/MBI2 code partly in C')

---
Changes since v8:
- added "Fixes:" line;
- typo in commit message: Adler -> Alder;
- add ".init" to mbi2.o;
- reduce difference.

---
 xen/arch/x86/efi/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/efi/Makefile +2 -2
index 7e2b5c07de..c6678652fc 100644
@@ -9,11 +9,11 @@ $(obj)/%.o: $(src)/%.ihex FORCE
 $(obj)/boot.init.o: $(obj)/buildid.o
 
 $(call cc-option-add,cflags-stack-boundary,CC,-mpreferred-stack-boundary=4)
-$(addprefix $(obj)/,$(EFIOBJ-y)): CFLAGS_stack_boundary := $(cflags-stack-boundary)
+$(addprefix $(obj)/,$(EFIOBJ-y) mbi2.init.o): CFLAGS_stack_boundary := $(cflags-stack-boundary)
 
 obj-y := common-stub.o stub.o
 obj-$(XEN_BUILD_EFI) := $(filter-out %.init.o,$(EFIOBJ-y))
 obj-bin-$(XEN_BUILD_EFI) := $(filter %.init.o,$(EFIOBJ-y))
-obj-bin-y += mbi2.o
+obj-bin-y += mbi2.init.o
 extra-$(XEN_BUILD_EFI) += buildid.o relocs-dummy.o
 nocov-$(XEN_BUILD_EFI) += stub.o
Signature · 1 line
2.34.1
#0 lore raw

v9 cover x86/boot: Improve MBI2 structure check (was: Reduce assembly code)

This series came from part of the work of removing duplications between boot code and rewriting part of code from assembly to C.

Changes since v1, more details in specific commits:
- style updates;
- comments and descriptions improvements;
- other improvements.

Changes since v2:
- rebased on master, resolved conflicts;
- add comment on trampoline section.

Changes since v3:
- changed new function name;
- declare efi_multiboot2 in a separate header;
- distinguish entry point from using magic number;
- other minor changes (see commens in commits).

Changes since v4:
- rebase on staging;
- set %fs and %gs as other segment registers;
- style and other changes.

Changes since v5:
- fixed a typo.

Changes since v6:
- remove merged patch;
- comment and style;
- change some pointer checks to avoid overflows;
- rename parse-mbi2.c to mbi2.c.

Changes since v7:
- removed merged parts;
- add required stack alignment.

Changes since v8:
- added "Fixes:" line;
- typo in commit message: Adler -> Alder;
- add ".init" to mbi2.o;
- reduce difference in Makefile.

Frediano Ziglio (2):
x86/boot: Align mbi2.c stack to 16 bytes
x86/boot: Improve MBI2 structure check

 xen/arch/x86/efi/Makefile | 4 ++--
 xen/arch/x86/efi/mbi2.c   | 7 +++++--
 2 files changed, 7 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
Reviewed-by on v9 1/2
#2 lore raw

Re: [PATCH v9 1/2] x86/boot: Align mbi2.c stack to 16 bytes

In reply to Frediano Ziglio
Quoted 1 line · Frediano Ziglio

Doing previous testing with an Alder Lake Intel machine the with

Nit: flip "the" and "with"?

Quoted 10 lines

"x86/boot: Improve MBI2 structure check" commit test started to fail. Removing the commit makes the tests succeed however there was not apparent reason (looking at the code) for the failure.
So I instrumented code to output the structure and tested code with this extracted data with and without the mentioned commit and results were the same.
Compiled assembly code from lab was also fine beside not keeping the 16-byte alignment for the stack.
Turning on stack alignment solve the problem on Alder Lake machine.

Quoted 2 lines
Fixes eb21ce14d709 ('x86/boot: Rewrite EFI/MBI2 code partly in C')
#4 lore raw

Re: [PATCH v9 2/2] x86/boot: Improve MBI2 structure check

In reply to Frediano Ziglio
Quoted 5 lines · Frediano Ziglio

Tag structure should contain at least the tag header. Entire tag structure must be contained inside MBI2 data.

Nit (for the future): Tags in chronological order please (maybe with the exception of release acks, which personally I think best go last).

Jan