efi: zboot: Ensure zboot PIC
PATCH 1/1 efi: zboot: Ensure zboot PIC
Using objcopy to reform vmlinuz.efi.elf to vmlinuz.efi will not convey any relocation information. That means vmlinuz.efi is expected to be PIC.
At present, vmlinuz.efi is PIC. But it is better to adopt the same solution used by the kernel to resolve the code relocation issue by itself. That is to resolve R_AARCH64_RELATIVE at the runtime.
--- drivers/firmware/efi/libstub/Makefile | 2 +- drivers/firmware/efi/libstub/Makefile.zboot | 2 +- drivers/firmware/efi/libstub/zboot-entry.S | 35 +++++++++++++++++++++ drivers/firmware/efi/libstub/zboot.c | 2 +- drivers/firmware/efi/libstub/zboot.lds | 6 ++++ 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 drivers/firmware/efi/libstub/zboot-entry.S
drivers/firmware/efi/libstub/Makefile +1 -1
@@ -95,7 +95,7 @@ lib-$(CONFIG_LOONGARCH) += loongarch.o loongarch-stub.o CFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) zboot-obj-$(CONFIG_RISCV) := lib-clz_ctz.o lib-ashldi3.o -lib-$(CONFIG_EFI_ZBOOT) += zboot.o $(zboot-obj-y) +lib-$(CONFIG_EFI_ZBOOT) += zboot-entry.o zboot.o $(zboot-obj-y) extra-y := $(lib-y) lib-y := $(patsubst %.o,%.stub.o,$(lib-y))
drivers/firmware/efi/libstub/Makefile.zboot +1 -1
@@ -36,7 +36,7 @@ $(obj)/zboot-header.o: $(srctree)/drivers/firmware/efi/libstub/zboot-header.S FO ZBOOT_DEPS := $(obj)/zboot-header.o $(objtree)/drivers/firmware/efi/libstub/lib.a -LDFLAGS_vmlinuz.efi.elf := -T $(srctree)/drivers/firmware/efi/libstub/zboot.lds +LDFLAGS_vmlinuz.efi.elf := --no-undefined -X -shared -Bsymbolic -z notext --no-apply-dynamic-relocs -T $(srctree)/drivers/firmware/efi/libstub/zboot.lds $(obj)/vmlinuz.efi.elf: $(obj)/vmlinuz.o $(ZBOOT_DEPS) FORCE $(call if_changed,ld)
drivers/firmware/efi/libstub/zboot-entry.S new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <asm/elf.h> +#define ZBOOT_HEADER_BASE 0 + + .text +/* + * x0: efi_handle_t + * x1: efi_system_table_t * + */ + .global efi_zboot_entry +efi_zboot_entry: + adrp x2, efi_zboot_header + add x2, x2, :lo12:efi_zboot_header + mov x3, ZBOOT_HEADER_BASE + sub x3, x2, x3 // delta between actual and linked address + adrp x4, _rela_start + add x4, x4, :lo12:_rela_start + adrp x5, _rela_end + add x5, x5, :lo12:_rela_end + +0: cmp x5, x4 + b.hs 1f + ldp x6, x7, [x4], #24 + ldr x8, [x4, #-8] + cmp w7, #R_AARCH64_RELATIVE + b.ne 0b + add x8, x8, x3 + str x8, [x6, x3] + b 0b + +1: + dsb ishst + ic iallu + b efi_zboot_main
drivers/firmware/efi/libstub/zboot.c +1 -1
@@ -58,7 +58,7 @@ void __weak efi_cache_sync_image(unsigned long image_base, } asmlinkage efi_status_t __efiapi -efi_zboot_entry (efi_handle_t handle,efi_system_table_t *systab) +efi_zboot_main (efi_handle_t handle,efi_system_table_t *systab) {unsigned long compressed_size = _gzdata_end - _gzdata_start;unsigned long image_base, alloc_size, code_size;
drivers/firmware/efi/libstub/zboot.lds +6 -0
@@ -12,6 +12,12 @@ SECTIONS *(.text* .init.text*) } + .rela.dyn : ALIGN(8) { + __efistub__rela_start = .; + *(.rela .rela*) + __efistub__rela_end = .; + } + .rodata : ALIGN(8) { __efistub__gzdata_start = .; *(.gzdata)
Signature · 1 line
2.31.1
Ard Biesheuvel
maintainerQuoted 6 lines · Pingfan Liu
Using objcopy to reform vmlinuz.efi.elf to vmlinuz.efi will not convey any relocation information. That means vmlinuz.efi is expected to be PIC.
At present, vmlinuz.efi is PIC. But it is better to adopt the same
Why is it better?
Quoted 3 lines
solution used by the kernel to resolve the code relocation issue by itself. That is to resolve
R_AARCH64_RELATIVEat the runtime.
This breaks other architectures.
Quoted 118 lines
Signed-off-by Pingfan Liu <[email protected]>Cc Ard Biesheuvel <[email protected]>Cc Kees Cook <[email protected]>Cc Catalin Marinas <[email protected]>Cc Sami Tolvanen <[email protected]>Cc Huacai Chen <[email protected]>--- drivers/firmware/efi/libstub/Makefile | 2 +- drivers/firmware/efi/libstub/Makefile.zboot | 2 +- drivers/firmware/efi/libstub/zboot-entry.S | 35 +++++++++++++++++++++ drivers/firmware/efi/libstub/zboot.c | 2 +- drivers/firmware/efi/libstub/zboot.lds | 6 ++++ 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 drivers/firmware/efi/libstub/zboot-entry.Sdrivers/firmware/efi/libstub/Makefile +1
-1@@ -95,7 +95,7 @@ lib-$(CONFIG_LOONGARCH) += loongarch.o loongarch-stub.o CFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) zboot-obj-$(CONFIG_RISCV) := lib-clz_ctz.o lib-ashldi3.o -lib-$(CONFIG_EFI_ZBOOT) += zboot.o $(zboot-obj-y) +lib-$(CONFIG_EFI_ZBOOT) += zboot-entry.o zboot.o $(zboot-obj-y) extra-y := $(lib-y) lib-y := $(patsubst %.o,%.stub.o,$(lib-y))drivers/firmware/efi/libstub/Makefile.zboot +1
-1@@ -36,7 +36,7 @@ $(obj)/zboot-header.o: $(srctree)/drivers/firmware/efi/libstub/zboot-header.S FO ZBOOT_DEPS := $(obj)/zboot-header.o $(objtree)/drivers/firmware/efi/libstub/lib.a -LDFLAGS_vmlinuz.efi.elf := -T $(srctree)/drivers/firmware/efi/libstub/zboot.lds +LDFLAGS_vmlinuz.efi.elf := --no-undefined -X -shared -Bsymbolic -z notext --no-apply-dynamic-relocs -T $(srctree)/drivers/firmware/efi/libstub/zboot.lds $(obj)/vmlinuz.efi.elf: $(obj)/vmlinuz.o $(ZBOOT_DEPS) FORCE $(call if_changed,ld)drivers/firmware/efi/libstub/zboot-entry.S new file +35
-0@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <asm/elf.h> +#define ZBOOT_HEADER_BASE 0 + + .text +/* + * x0: efi_handle_t + * x1: efi_system_table_t * + */ + .global efi_zboot_entry +efi_zboot_entry: + adrp x2, efi_zboot_header + add x2, x2, :lo12:efi_zboot_header + mov x3, ZBOOT_HEADER_BASE + sub x3, x2, x3 // delta between actual and linked address + adrp x4, _rela_start + add x4, x4, :lo12:_rela_start + adrp x5, _rela_end + add x5, x5, :lo12:_rela_end + +0: cmp x5, x4 + b.hs 1f + ldp x6, x7, [x4], #24 + ldr x8, [x4, #-8] + cmp w7, #R_AARCH64_RELATIVE + b.ne 0b + add x8, x8, x3 + str x8, [x6, x3] + b 0b + +1: + dsb ishst + ic iallu + b efi_zboot_maindrivers/firmware/efi/libstub/zboot.c +1
-1@@ -58,7 +58,7 @@ void __weak efi_cache_sync_image(unsigned long image_base, } asmlinkage efi_status_t __efiapi -efi_zboot_entry (efi_handle_t handle,efi_system_table_t *systab) +efi_zboot_main (efi_handle_t handle,efi_system_table_t *systab) {unsigned long compressed_size = _gzdata_end - _gzdata_start;unsigned long image_base, alloc_size, code_size;drivers/firmware/efi/libstub/zboot.lds +6
-1@@ -12,6 +12,12 @@ SECTIONS *(.text* .init.text*) } + .rela.dyn : ALIGN(8) { + __efistub__rela_start = .; + *(.rela .rela*) + __efistub__rela_end = .; + } + .rodata : ALIGN(8) { __efistub__gzdata_start = .; *(.gzdata) --2.31.1
Quoted 11 lines · Ard Biesheuvel
Quoted 6 lines · Pingfan Liu
Using objcopy to reform vmlinuz.efi.elf to vmlinuz.efi will not convey any relocation information. That means vmlinuz.efi is expected to be PIC.
At present, vmlinuz.efi is PIC. But it is better to adopt the same
Why is it better?
I think except carefully coded with asm language, there is no guarantee which prevents the compiler from generating position dependent code. Or is there any presumption here?
Quoted 6 lines
Quoted 3 lines
solution used by the kernel to resolve the code relocation issue by itself. That is to resolve
R_AARCH64_RELATIVEat the runtime.This breaks other architectures.
Oops, could it be done by:
#if defined(_aarch64_)
#define R_ARCH_RELATIVE R_AARCH64_RELATIVE
#else
#define R_ARCH_RELATIVE 0
#endif
Since any r_type equaling 0 can not be ejected into the relocation section, this macro will make the code dummy in essential.
Thanks,
Pingfan
Quoted 118 lines
Quoted 118 lines
Signed-off-by Pingfan Liu <[email protected]>Cc Ard Biesheuvel <[email protected]>Cc Kees Cook <[email protected]>Cc Catalin Marinas <[email protected]>Cc Sami Tolvanen <[email protected]>Cc Huacai Chen <[email protected]>--- drivers/firmware/efi/libstub/Makefile | 2 +- drivers/firmware/efi/libstub/Makefile.zboot | 2 +- drivers/firmware/efi/libstub/zboot-entry.S | 35 +++++++++++++++++++++ drivers/firmware/efi/libstub/zboot.c | 2 +- drivers/firmware/efi/libstub/zboot.lds | 6 ++++ 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 drivers/firmware/efi/libstub/zboot-entry.Sdrivers/firmware/efi/libstub/Makefile +1
-1@@ -95,7 +95,7 @@ lib-$(CONFIG_LOONGARCH) += loongarch.o loongarch-stub.o CFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) zboot-obj-$(CONFIG_RISCV) := lib-clz_ctz.o lib-ashldi3.o -lib-$(CONFIG_EFI_ZBOOT) += zboot.o $(zboot-obj-y) +lib-$(CONFIG_EFI_ZBOOT) += zboot-entry.o zboot.o $(zboot-obj-y) extra-y := $(lib-y) lib-y := $(patsubst %.o,%.stub.o,$(lib-y))drivers/firmware/efi/libstub/Makefile.zboot +1
-1@@ -36,7 +36,7 @@ $(obj)/zboot-header.o: $(srctree)/drivers/firmware/efi/libstub/zboot-header.S FO ZBOOT_DEPS := $(obj)/zboot-header.o $(objtree)/drivers/firmware/efi/libstub/lib.a -LDFLAGS_vmlinuz.efi.elf := -T $(srctree)/drivers/firmware/efi/libstub/zboot.lds +LDFLAGS_vmlinuz.efi.elf := --no-undefined -X -shared -Bsymbolic -z notext --no-apply-dynamic-relocs -T $(srctree)/drivers/firmware/efi/libstub/zboot.lds $(obj)/vmlinuz.efi.elf: $(obj)/vmlinuz.o $(ZBOOT_DEPS) FORCE $(call if_changed,ld)drivers/firmware/efi/libstub/zboot-entry.S new file +35
-0@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <asm/elf.h> +#define ZBOOT_HEADER_BASE 0 + + .text +/* + * x0: efi_handle_t + * x1: efi_system_table_t * + */ + .global efi_zboot_entry +efi_zboot_entry: + adrp x2, efi_zboot_header + add x2, x2, :lo12:efi_zboot_header + mov x3, ZBOOT_HEADER_BASE + sub x3, x2, x3 // delta between actual and linked address + adrp x4, _rela_start + add x4, x4, :lo12:_rela_start + adrp x5, _rela_end + add x5, x5, :lo12:_rela_end + +0: cmp x5, x4 + b.hs 1f + ldp x6, x7, [x4], #24 + ldr x8, [x4, #-8] + cmp w7, #R_AARCH64_RELATIVE + b.ne 0b + add x8, x8, x3 + str x8, [x6, x3] + b 0b + +1: + dsb ishst + ic iallu + b efi_zboot_maindrivers/firmware/efi/libstub/zboot.c +1
-1@@ -58,7 +58,7 @@ void __weak efi_cache_sync_image(unsigned long image_base, } asmlinkage efi_status_t __efiapi -efi_zboot_entry (efi_handle_t handle,efi_system_table_t *systab) +efi_zboot_main (efi_handle_t handle,efi_system_table_t *systab) {unsigned long compressed_size = _gzdata_end - _gzdata_start;unsigned long image_base, alloc_size, code_size;drivers/firmware/efi/libstub/zboot.lds +6
-1@@ -12,6 +12,12 @@ SECTIONS *(.text* .init.text*) } + .rela.dyn : ALIGN(8) { + __efistub__rela_start = .; + *(.rela .rela*) + __efistub__rela_end = .; + } + .rodata : ALIGN(8) { __efistub__gzdata_start = .; *(.gzdata) --2.31.1
Ard Biesheuvel
maintainerQuoted 9 lines · Pingfan Liu
Quoted 7 lines · Ard Biesheuvel
Quoted 5 lines · Pingfan Liu
Using objcopy to reform vmlinuz.efi.elf to vmlinuz.efi will not convey any relocation information. That means vmlinuz.efi is expected to be PIC.
Quoted 9 lines
Quoted 4 lines
Quoted 1 line
At present, vmlinuz.efi is PIC. But it is better to adopt the same
Why is it better?
I think except carefully coded with asm language, there is no guarantee which prevents the compiler from generating position dependent code. Or is there any presumption here?
All object files built under drivers/firmware/efi/libstub are scanned for absolute relocations, and if any exist, the build is aborted.
Quoted 14 lines
Quoted 6 lines
Quoted 3 lines
solution used by the kernel to resolve the code relocation issue by itself. That is to resolve
R_AARCH64_RELATIVEat the runtime.This breaks other architectures.
Oops, could it be done by:
#ifdefined(_aarch64_)
#defineR_ARCH_RELATIVER_AARCH64_RELATIVE
#else
#defineR_ARCH_RELATIVE0
#endif
Quoted 3 lines
Since any
r_typeequaling 0 can not be ejected into the relocation section, this macro will make the code dummy in essential.
So the assembler instructions you are using are also implemented on RISC-V and LoongArch?
Quoted 16 lines · Ard Biesheuvel
Quoted 14 lines · Pingfan Liu
Quoted 11 lines · Ard Biesheuvel
Quoted 6 lines · Pingfan Liu
Using objcopy to reform vmlinuz.efi.elf to vmlinuz.efi will not convey any relocation information. That means vmlinuz.efi is expected to be PIC.
At present, vmlinuz.efi is PIC. But it is better to adopt the same
Why is it better?
Quoted 8 lines
Quoted 4 lines
I think except carefully coded with asm language, there is no guarantee which prevents the compiler from generating position dependent code. Or is there any presumption here?
All object files built under
drivers/firmware/efi/libstubare scanned for absolute relocations, and if any exist, the build is aborted.
Oh, I got the code piece now. Thank you very much for pointing this out. And only if R_AARCH64_ABS64 is unavoidable, there is no need to ask the code to fix the relocation by itself.
But one more question, there are many other "S+A" types of relocation, let's say R_AARCH64_ABS32 or R_AARCH64_LDST64_ABS_LO12_NC. It is also sensitive to the delta between loading and linking. Should all of them be excluded by the Makefile?
Quoted 14 lines
Quoted 14 lines
Quoted 6 lines
Quoted 3 lines
solution used by the kernel to resolve the code relocation issue by itself. That is to resolve
R_AARCH64_RELATIVEat the runtime.This breaks other architectures.
Oops, could it be done by:
#ifdefined(_aarch64_)
#defineR_ARCH_RELATIVER_AARCH64_RELATIVE
#else
#defineR_ARCH_RELATIVE0
#endif
Quoted 6 lines
Quoted 3 lines
Since any
r_typeequaling 0 can not be ejected into the relocation section, this macro will make the code dummy in essential.So the assembler instructions you are using are also implemented on RISC-V and LoongArch?
Oops. The whole file should be arch dependent. In those cases, they should be a single jump instruction.
Appreciate for your help.
Thanks,
Pingfan
Ard Biesheuvel
maintainerQuoted 27 lines · Pingfan Liu
Quoted 16 lines · Ard Biesheuvel
Quoted 14 lines · Pingfan Liu
Quoted 11 lines · Ard Biesheuvel
Quoted 6 lines · Pingfan Liu
Using objcopy to reform vmlinuz.efi.elf to vmlinuz.efi will not convey any relocation information. That means vmlinuz.efi is expected to be PIC.
At present, vmlinuz.efi is PIC. But it is better to adopt the same
Why is it better?
Quoted 8 lines
Quoted 4 lines
I think except carefully coded with asm language, there is no guarantee which prevents the compiler from generating position dependent code. Or is there any presumption here?
All object files built under
drivers/firmware/efi/libstubare scanned for absolute relocations, and if any exist, the build is aborted.
Quoted 9 lines
Oh, I got the code piece now. Thank you very much for pointing this out. And only if
R_AARCH64_ABS64is unavoidable, there is no need to ask the code to fix the relocation by itself.But one more question, there are many other "S+A" types of relocation, let's say
R_AARCH64_ABS32orR_AARCH64_LDST64_ABS_LO12_NC. It is also sensitive to the delta between loading and linking. Should all of them be excluded by theMakefile?
None of those can be used to relocate absolute addresses, so these are not the ones we should care about.
There are the ones related to the GOT, which could result in GOT entries in the final executable with R_AARCH64_RELATIVE relocations. However, we use 'hidden' visibility for all codegen related to the EFI stub, so the compiler knows all symbol references can be resolved at link time. This means that under -fpic or -fpie, it will always use relative references.
Quoted 29 lines
Quoted 14 lines
Quoted 14 lines
Quoted 6 lines
Quoted 3 lines
solution used by the kernel to resolve the code relocation issue by itself. That is to resolve
R_AARCH64_RELATIVEat the runtime.This breaks other architectures.
Oops, could it be done by:
#ifdefined(_aarch64_)
#defineR_ARCH_RELATIVER_AARCH64_RELATIVE
#else
#defineR_ARCH_RELATIVE0
#endifQuoted 6 lines
Quoted 3 lines
Since any
r_typeequaling 0 can not be ejected into the relocation section, this macro will make the code dummy in essential.So the assembler instructions you are using are also implemented on RISC-V and LoongArch?
Oops. The whole file should be arch dependent. In those cases, they should be a single jump instruction.
Appreciate for your help.
Thanks,
Pingfan
Quoted 42 lines · Ard Biesheuvel
Quoted 27 lines · Pingfan Liu
Quoted 16 lines · Ard Biesheuvel
Quoted 14 lines · Pingfan Liu
Quoted 11 lines · Ard Biesheuvel
Quoted 6 lines · Pingfan Liu
Using objcopy to reform vmlinuz.efi.elf to vmlinuz.efi will not convey any relocation information. That means vmlinuz.efi is expected to be PIC.
At present, vmlinuz.efi is PIC. But it is better to adopt the same
Why is it better?
Quoted 8 lines
Quoted 4 lines
I think except carefully coded with asm language, there is no guarantee which prevents the compiler from generating position dependent code. Or is there any presumption here?
All object files built under
drivers/firmware/efi/libstubare scanned for absolute relocations, and if any exist, the build is aborted.Quoted 9 lines
Oh, I got the code piece now. Thank you very much for pointing this out. And only if
R_AARCH64_ABS64is unavoidable, there is no need to ask the code to fix the relocation by itself.But one more question, there are many other "S+A" types of relocation, let's say
R_AARCH64_ABS32orR_AARCH64_LDST64_ABS_LO12_NC. It is also sensitive to the delta between loading and linking. Should all of them be excluded by theMakefile?None of those can be used to relocate absolute addresses, so these are not the ones we should care about.
Quoted 7 lines
There are the ones related to the GOT, which could result in GOT entries in the final executable with
R_AARCH64_RELATIVErelocations. However, we use 'hidden' visibility for all codegen related to the EFI stub, so the compiler knows all symbol references can be resolved at link time. This means that under -fpic or -fpie, it will always use relative references.
Appreciate for the elaboration. I got the whole story now.
Thanks,
Pingfan
Quoted 30 lines
Quoted 30 lines
Quoted 15 lines
Quoted 15 lines
Quoted 7 lines
Quoted 4 lines
solution used by the kernel to resolve the code relocation issue by itself. That is to resolve
R_AARCH64_RELATIVEat the runtime.This breaks other architectures.
Oops, could it be done by:
#ifdefined(_aarch64_)
#defineR_ARCH_RELATIVER_AARCH64_RELATIVE
#else
#defineR_ARCH_RELATIVE0
#endifQuoted 6 lines
Quoted 3 lines
Since any
r_typeequaling 0 can not be ejected into the relocation section, this macro will make the code dummy in essential.So the assembler instructions you are using are also implemented on RISC-V and LoongArch?
Oops. The whole file should be arch dependent. In those cases, they should be a single jump instruction.
Appreciate for your help.
Thanks,
Pingfan