Skip to contentSkip to conversation

efi_loader: fix image and load_options leak on FDT load failure

PATCH Superseded Yuho Choi 3 participants 3 messages Oct 23

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH 1/1 efi_loader: fix image and load_options leak on FDT load failure

When efi_bootmgr_run() attempts to load an FDT specified in the active Boot#### option on non-ACPI platforms, load_fdt_from_load_option() may fail (e.g. if the file is missing or unreadable).

In this failure path, efi_bootmgr_run() returns the error code immediately, skipping the cleanup code at the end of the function. This leaks the loaded image handle, allocated memory pages, loaded image protocol, and the malloc'd load_options buffer.

Fix this by jumping to the cleanup label 'out' so that efi_unload_image() and free(load_options) are called before returning.

Fixes 39bac24a48b0 ("efi_loader: load device-tree specified in boot option")
---
 lib/efi_loader/efi_bootmgr.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/efi_loader/efi_bootmgr.c +2 -1
index 51ecb6ec1f..934f0c4060 100644
@@ -1323,7 +1323,7 @@ efi_status_t efi_bootmgr_run(void *fdt)
 		if (!fdt) {
 			ret = load_fdt_from_load_option(&fdt_lo);
 			if (ret != EFI_SUCCESS)
-				return ret;
+				goto out;
 			if (fdt_lo)
 				fdt = fdt_lo;
 		}
@@ -1339,6 +1339,7 @@ efi_status_t efi_bootmgr_run(void *fdt)
 	 */
 	ret = efi_install_fdt(fdt);
 
+out:
 	if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
 		free(fdt_lo);
 		if (fdt_distro)
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Yuho Choi

When efi_bootmgr_run() attempts to load an FDT specified in the active Boot#### option on non-ACPI platforms, load_fdt_from_load_option() may fail (e.g. if the file is missing or unreadable).

In this failure path, efi_bootmgr_run() returns the error code immediately, skipping the cleanup code at the end of the function. This leaks the loaded image handle, allocated memory pages, loaded image protocol, and the malloc'd load_options buffer.

Quoted 5 lines

Fix this by jumping to the cleanup label 'out' so that efi_unload_image() and free(load_options) are called before returning.

Fixes 39bac24a48b0 ("efi_loader: load device-tree specified in boot option")

I think this should be 39bac24a48bb. Besides that:

Quoted 28 lines
---
 lib/efi_loader/efi_bootmgr.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/efi_loader/efi_bootmgr.c +2 -1
index 51ecb6ec1f..934f0c4060 100644
@@ -1323,7 +1323,7 @@ efi_status_t efi_bootmgr_run(void *fdt)
 		if (!fdt) {
 			ret = load_fdt_from_load_option(&fdt_lo);
 			if (ret != EFI_SUCCESS)
-				return ret;
+				goto out;
 			if (fdt_lo)
 				fdt = fdt_lo;
 		}
@@ -1339,6 +1339,7 @@ efi_status_t efi_bootmgr_run(void *fdt)
 	 */
 	ret = efi_install_fdt(fdt);
 
+out:
 	if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
 		free(fdt_lo);
 		if (fdt_distro)
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#2 lore raw
In reply to Yuho Choi
Quoted 10 lines · Yuho Choi

When efi_bootmgr_run() attempts to load an FDT specified in the active Boot#### option on non-ACPI platforms, load_fdt_from_load_option() may fail (e.g. if the file is missing or unreadable).

In this failure path, efi_bootmgr_run() returns the error code immediately, skipping the cleanup code at the end of the function. This leaks the loaded image handle, allocated memory pages, loaded image protocol, and the malloc'd load_options buffer.

Quoted 4 lines

Fix this by jumping to the cleanup label 'out' so that efi_unload_image() and free(load_options) are called before returning.

Fixes 39bac24a48b0 ("efi_loader: load device-tree specified in boot option")

As Carlo pointed out, the SHA1 is probably from an internal tree; 39bac24a48bb is the right one

Quoted 29 lines
---
 lib/efi_loader/efi_bootmgr.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/efi_loader/efi_bootmgr.c +2 -2
index 51ecb6ec1f..934f0c4060 100644
@@ -1323,7 +1323,7 @@ efi_status_t efi_bootmgr_run(void *fdt)
                if (!fdt) {
                        ret = load_fdt_from_load_option(&fdt_lo);
                        if (ret != EFI_SUCCESS)
-                               return ret;
+                               goto out;
                        if (fdt_lo)
                                fdt = fdt_lo;
                }
@@ -1339,6 +1339,7 @@ efi_status_t efi_bootmgr_run(void *fdt)
         */
        ret = efi_install_fdt(fdt);
 
+out:
        if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
                free(fdt_lo);
                if (fdt_distro)
--

2.43.0