efi: Free malloc regions on exit
PATCH 1/1 efi: Free malloc regions on exit
When we exit grub, we don't free all the memory that we allocated earlier for our heap region. This can cause problems with setups where you try to descend the boot order using "exit" entries, such as PXE -> HD boot scenarios.
--- grub-core/kern/efi/init.c | 1 + grub-core/kern/efi/mm.c | 24 ++++++++++++++++++++++++ include/grub/efi/efi.h | 1 + 3 files changed, 26 insertions(+)
grub-core/kern/efi/init.c +1 -0
@@ -77,4 +77,5 @@ grub_efi_fini (void) {grub_efidisk_fini ();grub_console_fini (); +grub_efi_memory_fini (); }
grub-core/kern/efi/mm.c +24 -0
@@ -49,6 +49,12 @@ static grub_efi_uintn_t finish_desc_size;static grub_efi_uint32_t finish_desc_version;int grub_efi_is_finished =0 ; +struct efi_allocation { +grub_uint64_t start_addr; +grub_uint64_t pages; +} efi_allocated_memory[16 ]; +unsigned int efi_allocated_memory_idx =0 ; +/* Allocate pages. Return the pointer to the first of allocated pages. */ void * grub_efi_allocate_pages (grub_efi_physical_address_t address,
@@ -408,6 +414,13 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map, (void *) ((grub_addr_t ) start), (unsigned) pages); +/* Track up to 16 regions that we allocate from */ +if (efi_allocated_memory_idx <ARRAY_SIZE (efi_allocated_memory)) { + efi_allocated_memory[efi_allocated_memory_idx].start_addr = start; + efi_allocated_memory[efi_allocated_memory_idx].pages = pages; + efi_allocated_memory_idx++; + } +grub_mm_init_region (addr,PAGES_TO_BYTES (pages)); required_pages -= pages;
@@ -419,6 +432,17 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,grub_fatal ("too little memory" ); } +void +grub_efi_memory_fini (void ) +{ +unsigned int i; + +for (i =0 ; i < efi_allocated_memory_idx; i++) { +grub_efi_free_pages (efi_allocated_memory[i].start_addr, + efi_allocated_memory[i].pages); + } +} +#if 0 /* Print the memory map. */ static void
include/grub/efi/efi.h +1 -0
@@ -48,6 +48,7 @@ EXPORT_FUNC(grub_efi_get_memory_map) (grub_efi_uintn_t *memory_map_size, grub_efi_uintn_t *map_key, grub_efi_uintn_t *descriptor_size, grub_efi_uint32_t *descriptor_version); +void grub_efi_memory_fini (void );grub_efi_loaded_image_t *EXPORT_FUNC (grub_efi_get_loaded_image ) (grub_efi_handle_t image_handle);void EXPORT_FUNC (grub_efi_print_device_path ) (grub_efi_device_path_t *dp);char *EXPORT_FUNC (grub_efi_get_filename ) (grub_efi_device_path_t *dp);
Signature · 1 line
1.8.5.6
19.05.2016 16:37, Alexander Graf пишет:
Quoted 17 lines
When we exit grub, we don't free all the memory that we allocated earlier for our heap region. This can cause problems with setups where you try to descend the boot order using "exit" entries, such as PXE -> HD boot scenarios.
Signed-off-by Alexander Graf <[email protected]>--- grub-core/kern/efi/init.c | 1 + grub-core/kern/efi/mm.c | 24 ++++++++++++++++++++++++ include/grub/efi/efi.h | 1 + 3 files changed, 26 insertions(+)grub-core/kern/efi/init.c +0
-0
grub-core/kern/efi/init.c grub_efi_fini (void) · line 81
Quoted 5 lines
grub-core/kern/efi/init.c +1
-0@@ -77,4 +77,5 @@ grub_efi_fini (void) {grub_efidisk_fini ();grub_console_fini (); +grub_efi_memory_fini (); }
Note that grub_efi_fini() is called not only during exit, but also by grub_loader_boot (grub_machine_fini); and - at least, theoretically - grub_loader_boot_func can fail and we return back to GRUB. Which leaves us with heap pointing to already freed area. We probably cannot do anything useful at this point anyway, but this may lead to corruption of memory allocated by other EFI drivers.
May be it should be called explicitly only in exit path.
Also it is not called during chainload at all, which should have the same problem (i.e. conceptually it does not matter whether we exit grub and select next binary from EFI menu or simply try to chainload it from grub).
Quoted 18 lines
grub-core/kern/efi/mm.c +6
-0@@ -49,6 +49,12 @@ static grub_efi_uintn_t finish_desc_size;static grub_efi_uint32_t finish_desc_version;int grub_efi_is_finished =0 ; +struct efi_allocation { +grub_uint64_t start_addr; +grub_uint64_t pages; +} efi_allocated_memory[16 ]; +unsigned int efi_allocated_memory_idx =0 ; +/* Allocate pages. Return the pointer to the first of allocated pages. */ void * grub_efi_allocate_pages (grub_efi_physical_address_t address,
grub-core/kern/efi/mm.c add_memory_regions (grub_efi_memory_descriptor_t *memory_map, · line 423
Quoted 10 lines
grub-core/kern/efi/mm.c +7
-0@@ -408,6 +414,13 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map, (void *) ((grub_addr_t ) start), (unsigned) pages); +/* Track up to 16 regions that we allocate from */ +if (efi_allocated_memory_idx <ARRAY_SIZE (efi_allocated_memory)) { + efi_allocated_memory[efi_allocated_memory_idx].start_addr = start; + efi_allocated_memory[efi_allocated_memory_idx].pages = pages; + efi_allocated_memory_idx++; + } +
Can we walk regions list instead? May be we could store original address and size in region descriptor?
grub-core/kern/efi/mm.c add_memory_regions (grub_efi_memory_descriptor_t *memory_map, · line 426
Quoted 3 lines
grub-core/kern/efi/mm.c +0
-0grub_mm_init_region (addr,PAGES_TO_BYTES (pages)); required_pages -= pages;
Hmm ... grub_mm_init_region may silently skip some regions. So this is strictly speaking wrong (not related to your patch).
Quoted 31 lines
grub-core/kern/efi/mm.c +11
-0@@ -419,6 +432,17 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,grub_fatal ("too little memory" ); } +void +grub_efi_memory_fini (void ) +{ +unsigned int i; + +for (i =0 ; i < efi_allocated_memory_idx; i++) { +grub_efi_free_pages (efi_allocated_memory[i].start_addr, + efi_allocated_memory[i].pages); + } +} +#if 0 /* Print the memory map. */ static voidinclude/grub/efi/efi.h +1
-0@@ -48,6 +48,7 @@ EXPORT_FUNC(grub_efi_get_memory_map) (grub_efi_uintn_t *memory_map_size, grub_efi_uintn_t *map_key, grub_efi_uintn_t *descriptor_size, grub_efi_uint32_t *descriptor_version); +void grub_efi_memory_fini (void );grub_efi_loaded_image_t *EXPORT_FUNC (grub_efi_get_loaded_image ) (grub_efi_handle_t image_handle);void EXPORT_FUNC (grub_efi_print_device_path ) (grub_efi_device_path_t *dp);char *EXPORT_FUNC (grub_efi_get_filename ) (grub_efi_device_path_t *dp);
Quoted 22 lines · Andrei Borzenkov
19.05.2016 16:37, Alexander Graf пишет:
Quoted 21 lines
When we exit grub, we don't free all the memory that we allocated earlier for our heap region. This can cause problems with setups where you try to descend the boot order using "exit" entries, such as PXE -> HD boot scenarios.
Signed-off-by Alexander Graf <[email protected]>--- grub-core/kern/efi/init.c | 1 + grub-core/kern/efi/mm.c | 24 ++++++++++++++++++++++++ include/grub/efi/efi.h | 1 + 3 files changed, 26 insertions(+)grub-core/kern/efi/init.c +1
-0@@ -77,4 +77,5 @@ grub_efi_fini (void) {grub_efidisk_fini ();grub_console_fini (); +grub_efi_memory_fini ();
Quoted 8 lines
grub-core/kern/efi/init.c grub_efi_fini (void) · line 81
Quoted 1 line
grub-core/kern/efi/init.c +0
-0}Note that
grub_efi_fini()is called not only during exit, but also bygrub_loader_boot(grub_machine_fini); and - at least, theoretically -grub_loader_boot_funccan fail and we return back to GRUB. Which leaves us with heap pointing to already freed area. We probably cannot do anything useful at this point anyway, but this may lead to corruption of memory allocated by other EFI drivers.
I think grub_machine_fini is called without GRUB_LOADER_FLAG_NORETURN flag set in above-mentioned case so that it should be fine.
Thanks,
Michael
Quoted 84 lines
May be it should be called explicitly only in exit path.
Also it is not called during chainload at all, which should have the same problem (i.e. conceptually it does not matter whether we exit grub and select next binary from EFI menu or simply try to chainload it from grub).
Quoted 18 lines
grub-core/kern/efi/mm.c +6
-0@@ -49,6 +49,12 @@ static grub_efi_uintn_t finish_desc_size;static grub_efi_uint32_t finish_desc_version;int grub_efi_is_finished =0 ; +struct efi_allocation { +grub_uint64_t start_addr; +grub_uint64_t pages; +} efi_allocated_memory[16 ]; +unsigned int efi_allocated_memory_idx =0 ; +/* Allocate pages. Return the pointer to the first of allocated pages. */ void * grub_efi_allocate_pages (grub_efi_physical_address_t address,grub-core/kern/efi/mm.c add_memory_regions (grub_efi_memory_descriptor_t *memory_map, · line 423
Quoted 10 lines
grub-core/kern/efi/mm.c +7
-0@@ -408,6 +414,13 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map, (void *) ((grub_addr_t ) start), (unsigned) pages); +/* Track up to 16 regions that we allocate from */ +if (efi_allocated_memory_idx <ARRAY_SIZE (efi_allocated_memory)) { + efi_allocated_memory[efi_allocated_memory_idx].start_addr = start; + efi_allocated_memory[efi_allocated_memory_idx].pages = pages; + efi_allocated_memory_idx++; + } +Can we walk regions list instead? May be we could store original address and size in region descriptor?
grub-core/kern/efi/mm.c add_memory_regions (grub_efi_memory_descriptor_t *memory_map, · line 426
Quoted 3 lines
grub-core/kern/efi/mm.c +0
-0grub_mm_init_region (addr,PAGES_TO_BYTES (pages)); required_pages -= pages;Hmm ...
grub_mm_init_regionmay silently skip some regions. So this is strictly speaking wrong (not related to your patch).Quoted 24 lines
grub-core/kern/efi/mm.c +11
-0@@ -419,6 +432,17 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,grub_fatal ("too little memory" ); } +void +grub_efi_memory_fini (void ) +{ +unsigned int i; + +for (i =0 ; i < efi_allocated_memory_idx; i++) { +grub_efi_free_pages (efi_allocated_memory[i].start_addr, + efi_allocated_memory[i].pages); + } +} +#if 0 /* Print the memory map. */ static voidinclude/grub/efi/efi.h +0
-0include/grub/efi/efi.h EXPORT_FUNC(grub_efi_get_memory_map) (grub_efi_uintn_t *memory_map_size, · line 54
Quoted 7 lines
include/grub/efi/efi.h +1
-0@@ -48,6 +48,7 @@ EXPORT_FUNC(grub_efi_get_memory_map) (grub_efi_uintn_t *memory_map_size, grub_efi_uintn_t *map_key, grub_efi_uintn_t *descriptor_size, grub_efi_uint32_t *descriptor_version); +void grub_efi_memory_fini (void );grub_efi_loaded_image_t *EXPORT_FUNC (grub_efi_get_loaded_image ) (grub_efi_handle_t image_handle);void EXPORT_FUNC (grub_efi_print_device_path ) (grub_efi_device_path_t *dp);char *EXPORT_FUNC (grub_efi_get_filename ) (grub_efi_device_path_t *dp);_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 5 lines
-----Original Message-----
From: Grub-devel [mailto:[email protected]] On Behalf Of Alexander Graf
Sent: Thursday, May 19, 2016 8:38 AM
Subject: [PATCH] efi: Free malloc regions on exit
...
Quoted 1 line
+struct
efi_allocation{
If no other file is using this, mark it as static.
grub-core/kern/efi/mm.c static grub_efi_uintn_t finish_desc_size; · line 53
Quoted 1 line
grub-core/kern/efi/mm.c +1
-0+grub_uint64_t start_addr;
That should be grub_efi_physical_address_t.
grub-core/kern/efi/mm.c static grub_efi_uintn_t finish_desc_size; · line 54
Quoted 1 line
grub-core/kern/efi/mm.c +1
-0+grub_uint64_t pages;
That should be grub_efi_uint64_t (the parameter type for add_memory_regions) or grub_efi_uintn_t (the parameter type for grub_efi_allocate_pages and grub_efi_free_pages).
Quoted 1 line
+} efi_allocated_memory[16];
Why 16 and not some other number? Consider a #define and making the comment about 16 in add_memory_regions more generic.
Quoted 1 line
+unsigned int
efi_allocated_memory_idx= 0;
The C language definition guarantees that global variables are initialized to 0.
Quoted 12 lines
(quoted diff) +4
-0@@ -408,6 +414,13 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map, (void *) ((grub_addr_t) start), (unsigned) pages); + /* Track up to 16 regions that we allocate from */ + if (efi_allocated_memory_idx < ARRAY_SIZE(efi_allocated_memory)) { + efi_allocated_memory[efi_allocated_memory_idx].start_addr = start; + efi_allocated_memory[efi_allocated_memory_idx].pages =pages;
grub-core/kern/efi/mm.c add_memory_regions (grub_efi_memory_descriptor_t *memory_map, · line 423
Quoted 3 lines
grub-core/kern/efi/mm.c +3
-0+ efi_allocated_memory_idx++; + } +
Consider printing if the magic number is exceeded, rather than silently changing the behavior.
Quoted 8 lines
grub_mm_init_region (addr, PAGES_TO_BYTES (pages)); required_pages -= pages;grub-core/kern/efi/mm.c +0
-0@@ -419,6 +432,17 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,grub_fatal ("too little memory" ); }
grub-core/kern/efi/mm.c add_memory_regions (grub_efi_memory_descriptor_t · line 445
Quoted 10 lines
grub-core/kern/efi/mm.c +10
-0+void +grub_efi_memory_fini (void ) +{ +unsigned int i; + +for (i =0 ; i < efi_allocated_memory_idx; i++) { +grub_efi_free_pages (efi_allocated_memory[i].start_addr, + efi_allocated_memory[i].pages); + } +}
Consider clearing efi_allocated_memory_idx after that, since this function cannot be sure it's the last one to use a global variable.
Clearing start_addr might help prevent stale pointer reference bugs. Pointers that are no longer valid are dangerous to leave around.
---
Robert Elliott, HPE Persistent Memory
Quoted 8 lines · Robert (Persistent Memory)
Quoted 5 lines
-----Original Message-----
From: Grub-devel [mailto:[email protected]] On Behalf Of Alexander Graf
Sent: Thursday, May 19, 2016 8:38 AM
Subject: [PATCH] efi: Free malloc regions on exit...
Quoted 1 line
+struct
efi_allocation{If no other file is using this, mark it as static.
Good idea.
Quoted 8 lines
grub-core/kern/efi/mm.c static grub_efi_uintn_t finish_desc_size; · line 53
Quoted 2 lines
grub-core/kern/efi/mm.c +1
-0+grub_uint64_t start_addr;That should be
grub_efi_physical_address_t.grub-core/kern/efi/mm.c static grub_efi_uintn_t finish_desc_size; · line 54
Quoted 1 line
grub-core/kern/efi/mm.c +1
-0+grub_uint64_t pages;That should be
grub_efi_uint64_t(the parameter type foradd_memory_regions) orgrub_efi_uintn_t(the parameter type forgrub_efi_allocate_pagesandgrub_efi_free_pages).
Yup for the type changes.
Quoted 4 lines
Quoted 2 lines
+} efi_allocated_memory[16];
Why 16 and not some other number? Consider a #define and making the comment about 16 in
add_memory_regionsmore generic.
I've added some helpful comment above and moved it into a #define.
Quoted 4 lines
Quoted 2 lines
+unsigned int
efi_allocated_memory_idx= 0;The C language definition guarantees that global variables are initialized to 0.
Yes, but multiple other global variables in the file also initialize with 0. Last time I checked gcc just puts those variables into bss either way, so you don't really lose anything by explicitly saying = 0 here. It does increase readability imho though to have it.
Quoted 7 lines
Quoted 7 lines
grub-core/kern/efi/mm.c +0
-0@@ -408,6 +414,13 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map, (void *) ((grub_addr_t ) start), (unsigned) pages);
Quoted 12 lines
grub-core/kern/efi/mm.c add_memory_regions (grub_efi_memory_descriptor_t *memory_map, · line 423
Quoted 10 lines
(quoted diff) +4
-0+ /* Track up to 16 regions that we allocate from */ + if (efi_allocated_memory_idx < ARRAY_SIZE(efi_allocated_memory)) { + efi_allocated_memory[efi_allocated_memory_idx].start_addr = start; + efi_allocated_memory[efi_allocated_memory_idx].pages =pages;
grub-core/kern/efi/mm.c +3
-0+ efi_allocated_memory_idx++; + } +Consider printing if the magic number is exceeded, rather than silently changing the behavior.
Good idea. I don't think anyone really cares enough - there should still be enough ram getting free'd on exit if memory is that heavily segmented. But I suppose firmware authors want to be aware that their memory map is a mess, and this could tell them :).
Quoted 9 lines
Quoted 9 lines
grub_mm_init_region (addr, PAGES_TO_BYTES (pages)); required_pages -= pages;grub-core/kern/efi/mm.c +0
-0@@ -419,6 +432,17 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,grub_fatal ("too little memory" ); }
Quoted 13 lines
grub-core/kern/efi/mm.c add_memory_regions (grub_efi_memory_descriptor_t *memory_map, · line 444
Quoted 10 lines
grub-core/kern/efi/mm.c +10
-0+void +grub_efi_memory_fini (void ) +{ +unsigned int i; + +for (i =0 ; i < efi_allocated_memory_idx; i++) { +grub_efi_free_pages (efi_allocated_memory[i].start_addr, + efi_allocated_memory[i].pages); + } +}Consider clearing
efi_allocated_memory_idxafter that, since this function cannot be sure it's the last one to use a global variable.
I'm not sure it's incredibly helpful (since this is a _fini function that basically denotes the end of grub's life), but sure.
Quoted 4 lines
Clearing
start_addrmight help prevent stale pointer reference bugs. Pointers that are no longer valid are dangerous to leave around.
That one however I don't agree with. Array elements are only really valid if their counter considers them as valid. If we initialize the counter to 0 that means the whole array should automatically be considered unused.
Thanks a lot for the review :).
Alex
20.05.2016 07:34, Michael Chang пишет:
Quoted 32 lines · Andrei Borzenkov
Quoted 22 lines
19.05.2016 16:37, Alexander Graf пишет:
Quoted 21 lines
When we exit grub, we don't free all the memory that we allocated earlier for our heap region. This can cause problems with setups where you try to descend the boot order using "exit" entries, such as PXE -> HD boot scenarios.
Signed-off-by Alexander Graf <[email protected]>--- grub-core/kern/efi/init.c | 1 + grub-core/kern/efi/mm.c | 24 ++++++++++++++++++++++++ include/grub/efi/efi.h | 1 + 3 files changed, 26 insertions(+)grub-core/kern/efi/init.c +1
-0@@ -77,4 +77,5 @@ grub_efi_fini (void) {grub_efidisk_fini ();grub_console_fini (); +grub_efi_memory_fini ();Quoted 8 lines
grub-core/kern/efi/init.c grub_efi_fini (void) · line 81
Quoted 1 line
grub-core/kern/efi/init.c +0
-0}Note that
grub_efi_fini()is called not only during exit, but also bygrub_loader_boot(grub_machine_fini); and - at least, theoretically -grub_loader_boot_funccan fail and we return back to GRUB. Which leaves us with heap pointing to already freed area. We probably cannot do anything useful at this point anyway, but this may lead to corruption of memory allocated by other EFI drivers.
Quoted 3 lines
I think
grub_machine_finiis called withoutGRUB_LOADER_FLAG_NORETURNflag set in above-mentioned case so that it should be fine.
Well, there are calls both with and without GRUB_LOADER_FLAG_NORETURN. It is true that for EFI platform all *existing* calls are without, but nothing really forces it. I am a bit uneasy about it. Ideally I'd prefer clean code path that frees memory immediately before existing GRUB.
Quoted 11 lines
Thanks,
MichaelQuoted 8 lines
May be it should be called explicitly only in exit path.
Also it is not called during chainload at all, which should have the same problem (i.e. conceptually it does not matter whether we exit grub and select next binary from EFI menu or simply try to chainload it from grub).
No comments?