Skip to contentSkip to conversation

x86/kaslr: try process e820 entries if can not get suitable regions from efi

PATCH In review Lin Feng 3 participants 5 messages Jan 5, 2021 – Mar 17, 2021 x86

lore
Share & tools
lore

Lin Feng

author
#0 lore raw

PATCH 1/1 x86/kaslr: try process e820 entries if can not get suitable regions from efi

On efi64 x86_64 system, the EFI_CONVENTIONAL_MEMORY regions will not be mapped when making EFI runtime calls. So kexec-tools can not get these from /sys/firmware/efi/runtime-map. Then compressed boot os can not get suitable regions in process_efi_entries and print debug message as follow:
Physical KASLR disabled: no suitable memory region!
To enable physical kaslr with kexec, call process_e820_entries when no suitable regions in efi memmaps.

---

I find a regular of Kernel code and data placement with kexec. It seems unsafe. The reason is showed above.

I'm not familiar with efi firmware. I wonder if there are some risks to get regions according to e820 when there is no suitable region in efi memmaps.

---
 arch/x86/boot/compressed/kaslr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/boot/compressed/kaslr.c +3 -1
index b92fffbe761f..dbd7244b71aa 100644
@@ -685,6 +685,7 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)
 {
 	struct efi_info *e = &boot_params->efi_info;
 	bool efi_mirror_found = false;
+	bool efi_mem_region_found = false;
 	struct mem_vector region;
 	efi_memory_desc_t *md;
 	unsigned long pmap;
@@ -742,12 +743,13 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)
 		    !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
 			continue;
 
+		efi_mem_region_found = false;
 		region.start = md->phys_addr;
 		region.size = md->num_pages << EFI_PAGE_SHIFT;
 		if (process_mem_region(&region, minimum, image_size))
 			break;
 	}
-	return true;
+	return efi_mem_region_found;
 }
 #else
 static inline bool
Signature · 1 line
2.23.0
#1 lore raw

(cc Arvind)

Quoted 54 lines · Lin Feng

On efi64 x86_64 system, the EFI_CONVENTIONAL_MEMORY regions will not be mapped when making EFI runtime calls. So kexec-tools can not get these from /sys/firmware/efi/runtime-map. Then compressed boot os can not get suitable regions in process_efi_entries and print debug message as follow:
Physical KASLR disabled: no suitable memory region!
To enable physical kaslr with kexec, call process_e820_entries when no suitable regions in efi memmaps.

---

I find a regular of Kernel code and data placement with kexec. It seems unsafe. The reason is showed above.

I'm not familiar with efi firmware. I wonder if there are some risks to get regions according to e820 when there is no suitable region in efi memmaps.

---
 arch/x86/boot/compressed/kaslr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/boot/compressed/kaslr.c +3 -2
index b92fffbe761f..dbd7244b71aa 100644
@@ -685,6 +685,7 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)
 {
        struct efi_info *e = &boot_params->efi_info;
        bool efi_mirror_found = false;
+       bool efi_mem_region_found = false;
        struct mem_vector region;
        efi_memory_desc_t *md;
        unsigned long pmap;
@@ -742,12 +743,13 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)
                    !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
                        continue;
 
+               efi_mem_region_found = false;
                region.start = md->phys_addr;
                region.size = md->num_pages << EFI_PAGE_SHIFT;
                if (process_mem_region(&region, minimum, image_size))
                        break;
        }
-       return true;
+       return efi_mem_region_found;
 }
 #else
 static inline bool
--

2.23.0

#2 lore raw
Quoted 43 lines · Ard Biesheuvel

(cc Arvind)

Quoted 40 lines · Lin Feng

On efi64 x86_64 system, the EFI_CONVENTIONAL_MEMORY regions will not be mapped when making EFI runtime calls. So kexec-tools can not get these from /sys/firmware/efi/runtime-map. Then compressed boot os can not get suitable regions in process_efi_entries and print debug message as follow:
Physical KASLR disabled: no suitable memory region!
To enable physical kaslr with kexec, call process_e820_entries when no suitable regions in efi memmaps.

---

I find a regular of Kernel code and data placement with kexec. It seems unsafe. The reason is showed above.

I'm not familiar with efi firmware. I wonder if there are some risks to get regions according to e820 when there is no suitable region in efi memmaps.

---
 arch/x86/boot/compressed/kaslr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/boot/compressed/kaslr.c +1 -0
index b92fffbe761f..dbd7244b71aa 100644
@@ -685,6 +685,7 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)
 {
        struct efi_info *e = &boot_params->efi_info;
        bool efi_mirror_found = false;
+       bool efi_mem_region_found = false;
        struct mem_vector region;
        efi_memory_desc_t *md;
        unsigned long pmap;
@@ -742,12 +743,13 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)
                    !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
                        continue;
 
Quoted 1 line

arch/x86/boot/compressed/kaslr.c process_efi_entries(unsigned long minimum, unsigned long image_size) · line 746

Quoted 1 line
arch/x86/boot/compressed/kaslr.c +1 -0
+               efi_mem_region_found = false;

^^ this should be true, not false.

Other than that, I think this should be okay. The reason EFI memmap is preferred over E820, according to commit

0982adc74673 ("x86/boot/KASLR: Work around firmware bugs by excluding EFI_BOOT_SERVICES_* and EFI_LOADER_* from KASLR's choice")

was to avoid allocating inside EFI_BOOT_SERVICES/EFI_LOADER_DATA etc. That's not a danger during kexec, and I believe runtime services regions should be marked as reserved in the E820 map, right?

Also, something a little fishy-looking here is that the first loop to see if there is any EFI_MEMORY_MORE_RELIABLE region does not apply any of the checks on the memory region type/attributes. If there is a mirror region but it isn't conventional memory, or if it was soft-reserved, we shouldn't be setting efi_mirror_found.

Quoted 13 lines
Quoted 13 lines
arch/x86/boot/compressed/kaslr.c +1 -2
                region.start = md->phys_addr;
                region.size = md->num_pages << EFI_PAGE_SHIFT;
                if (process_mem_region(&region, minimum, image_size))
                        break;
        }
-       return true;
+       return efi_mem_region_found;
 }
 #else
 static inline bool
--

2.23.0

#3 lore raw
Quoted 61 lines

-----Original Message-----
From: Arvind Sankar [mailto:[email protected]] On Behalf Of Arvind Sankar
Sent: Wednesday, January 6, 2021 5:01 AM
To: Ard Biesheuvel <[email protected]>
Cc: linfeng (M) <[email protected]>; Arvind Sankar <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Borislav Petkov <[email protected]>; Huangweidong (C) <[email protected]>; Wangjing (Hogan, Cloud Infrastructure Service Product Dept.) <[email protected]>; Wangxin (Alexander) <[email protected]>; Linux Kernel Mailing List <[email protected]>; Eric W. Biederman <[email protected]>
Subject: Re: [PATCH] x86/kaslr: try process e820 entries if can not get suitable regions from efi

Quoted 5 lines · Ard Biesheuvel

(cc Arvind)

Quoted 2 lines · Lin Feng

On efi64 x86_64 system, the EFI_CONVENTIONAL_MEMORY regions will

not

Quoted 22 lines
Quoted 22 lines

be mapped when making EFI runtime calls. So kexec-tools can not get these from /sys/firmware/efi/runtime-map. Then compressed boot os can not get suitable regions in process_efi_entries and print debug message as follow:
Physical KASLR disabled: no suitable memory region!
To enable physical kaslr with kexec, call process_e820_entries when no suitable regions in efi memmaps.

---

I find a regular of Kernel code and data placement with kexec. It seems unsafe. The reason is showed above.

I'm not familiar with efi firmware. I wonder if there are some risks to get regions according to e820 when there is no suitable region in efi memmaps.

---
 arch/x86/boot/compressed/kaslr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 14 lines

arch/x86/boot/compressed/kaslr.c process_efi_entries(unsigned long minimum,

Quoted 14 lines
arch/x86/boot/compressed/kaslr.c +1 -0
index b92fffbe761f..dbd7244b71aa 100644
@@ -685,6 +685,7 @@ process_efi_entries(unsigned long minimum,
 unsigned long image_size)  {
        struct efi_info *e = &boot_params->efi_info;
        bool efi_mirror_found = false;
+       bool efi_mem_region_found = false;
        struct mem_vector region;
        efi_memory_desc_t *md;
        unsigned long pmap;
@@ -742,12 +743,13 @@ process_efi_entries(unsigned long minimum,

unsigned long image_size)

Quoted 6 lines
Quoted 1 line

arch/x86/boot/compressed/kaslr.c process_efi_entries(unsigned long minimum, · line 743

Quoted 1 line
arch/x86/boot/compressed/kaslr.c +0 -0
                    !(md->attribute &

EFI_MEMORY_MORE_RELIABLE))

Quoted 3 lines

arch/x86/boot/compressed/kaslr.c process_efi_entries(unsigned long minimum, unsigned long image_size) · line 746

Quoted 3 lines
arch/x86/boot/compressed/kaslr.c +1 -0
                        continue;
 
+               efi_mem_region_found = false;

^^ this should be true, not false.

You're right. It should be true here. Thanks for pointing out.

Quoted 10 lines

Other than that, I think this should be okay. The reason EFI memmap is preferred over E820, according to commit

0982adc74673 ("x86/boot/KASLR: Work around firmware bugs by excluding
EFI_BOOT_SERVICES_* and EFI_LOADER_* from KASLR's choice")

was to avoid allocating inside EFI_BOOT_SERVICES/EFI_LOADER_DATA etc. That's not a danger during kexec, and I believe runtime services regions should be marked as reserved in the E820 map, right?

Yes.

Quoted 6 lines

Also, something a little fishy-looking here is that the first loop to see if there is any EFI_MEMORY_MORE_RELIABLE region does not apply any of the checks on the memory region type/attributes. If there is a mirror region but it isn't conventional memory, or if it was soft-reserved, we shouldn't be setting efi_mirror_found.

I think so. And I wonder if the memory mirror doesn't work with kexec and ksalr only this patch used, because a lot of efi information is lost and e820 don't have any mirror regions information. Due to resource constraints, I haven't tested it yet. But it seems so.

Quoted 16 lines
Quoted 5 lines

arch/x86/boot/compressed/kaslr.c process_efi_entries(unsigned long minimum, unsigned long image_size)

Quoted 5 lines
arch/x86/boot/compressed/kaslr.c +0 -0
                region.start = md->phys_addr;
                region.size = md->num_pages << EFI_PAGE_SHIFT;
                if (process_mem_region(&region, minimum,

image_size))

Quoted 10 lines
Quoted 10 lines
arch/x86/boot/compressed/kaslr.c +1 -2
                        break;
        }
-       return true;
+       return efi_mem_region_found;
 }
 #else
 static inline bool
--

2.23.0

#4 lore raw
In reply to Arvind Sankar

On Wed, 17 Mar 09:54, Lin Feng <[email protected]> wrote:
After more than one month testing, we find that it is not suitable to process e820 directly in kexec to place the kernel code. Some regions, like tmplog and memattr tables, are not marked as reserved in e820.
Take tmplog, for example, the memory of table is marked as E820_TYPE_RAM in e820 and EFI_LOADER_DATA in efi. I wonder why not marked it as E820_TYPE_RESERVED, which is contrary to our expectations. So processing e820 directly in kexec is against the principles of the commit
0982adc74673 ("x86/boot/KASLR: Work around firmware bugs by excluding
We try to avoid placing kernel code or data on tmplog memory range in kexec. But unfortunately, the efi info is not runnable, so it is abandoned in function efi_map_regions. We can not get the info in kexec.
Any way, we skill haven't found a suitable solution. Any ideas, friends?

Quoted 88 lines · Lin Feng
Quoted 46 lines · Ard Biesheuvel
Quoted 44 lines

(cc Arvind)

Quoted 41 lines · Lin Feng

On efi64 x86_64 system, the EFI_CONVENTIONAL_MEMORY regions will not be mapped when making EFI runtime calls. So kexec-tools can not get these from /sys/firmware/efi/runtime-map. Then compressed boot os can not get suitable regions in process_efi_entries and print debug message as follow:
Physical KASLR disabled: no suitable memory region!
To enable physical kaslr with kexec, call process_e820_entries when no suitable regions in efi memmaps.

---

I find a regular of Kernel code and data placement with kexec. It seems unsafe. The reason is showed above.

I'm not familiar with efi firmware. I wonder if there are some risks to get regions according to e820 when there is no suitable region in efi memmaps.

---
 arch/x86/boot/compressed/kaslr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/boot/compressed/kaslr.c +1 -0
index b92fffbe761f..dbd7244b71aa 100644
@@ -685,6 +685,7 @@ process_efi_entries(unsigned long minimum,
 unsigned long image_size)  {
        struct efi_info *e = &boot_params->efi_info;
        bool efi_mirror_found = false;
+       bool efi_mem_region_found = false;
        struct mem_vector region;
        efi_memory_desc_t *md;
        unsigned long pmap;
@@ -742,12 +743,13 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)
                    !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
                        continue;
 
Quoted 2 lines
Quoted 1 line

arch/x86/boot/compressed/kaslr.c process_efi_entries(unsigned long minimum, unsigned long image_size) · line 746

Quoted 1 line
arch/x86/boot/compressed/kaslr.c +1 -0
+               efi_mem_region_found = false;

^^ this should be true, not false.

You're right. It should be true here. Thanks for pointing out.

Quoted 11 lines

Other than that, I think this should be okay. The reason EFI memmap is preferred over E820, according to commit

0982adc74673 ("x86/boot/KASLR: Work around firmware bugs by
excluding
EFI_BOOT_SERVICES_* and EFI_LOADER_* from KASLR's choice")

was to avoid allocating inside EFI_BOOT_SERVICES/EFI_LOADER_DATA etc. That's not a danger during kexec, and I believe runtime services regions should be marked as reserved in the E820 map, right?

Yes.

Quoted 6 lines

Also, something a little fishy-looking here is that the first loop to see if there is any EFI_MEMORY_MORE_RELIABLE region does not apply any of the checks on the memory region type/attributes. If there is a mirror region but it isn't conventional memory, or if it was soft-reserved, we shouldn't be setting efi_mirror_found.

I think so. And I wonder if the memory mirror doesn't work with kexec and ksalr only this patch used, because a lot of efi information is lost and e820 don't have any mirror regions information. Due to resource constraints, I haven't tested it yet.
But it seems so.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
arch/x86/boot/compressed/kaslr.c +1 -2
                region.start = md->phys_addr;
                region.size = md->num_pages << EFI_PAGE_SHIFT;
                if (process_mem_region(&region, minimum, image_size))
                        break;
        }
-       return true;
+       return efi_mem_region_found;
 }
 #else
 static inline bool
--

2.23.0