Skip to contentSkip to conversation

efi_loader: Address some issues found by Smatch

PATCH In review Andrew Goodbody 3 participants 6 messages Jul 2, 2025

lore
Share & tools
lore
#0 lore raw

v2 cover efi_loader: Address some issues found by Smatch

Smatch reported that there were some unitialised variables and dereferencing of NULL pointers. This series addresses these issues.

---
Changes in v2:
- Address review comments on 2/3 to return EFI_UNSUPPORTED and eliminate
the use of protocol_interface
- Collect RB tags for unchanged patches
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrew Goodbody (3):
efi_loader: Prevent dereferencing NULL pointer
efi_loader: Prevent dereference of uninitialised variable
efi_loader: Prevent free of uninitialised pointer

 lib/efi_loader/efi_file.c | 2 +-
 lib/efi_loader/efi_http.c | 9 ++++-----
 lib/efi_loader/efi_net.c  | 2 +-
 3 files changed, 6 insertions(+), 7 deletions(-)

---
base-commit: 7027b445cc0bfb86204ecb1f1fe596f5895048d9
change-id: 20250701-efiloader_fix-63c3609c6ed8

Best regards,

Signature · 1 line
Andrew Goodbody <[email protected]>
#1 lore raw

PATCH v2 1/3 efi_loader: Prevent dereferencing NULL pointer

Taking the first goto error: in file_open could either result in an attempt to dereference fh when NULL or else free fh->path which has not been assigned to and so will be unknown. Avoid both of these problems by passing path to free instead of fh->path.

This issue found by Smatch.

---
 lib/efi_loader/efi_file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/efi_loader/efi_file.c +1 -1
index 7d81da8f2d8..19b43c4a625 100644
@@ -248,7 +248,7 @@ static struct efi_file_handle *file_open(struct file_system *fs,
 	return &fh->base;
 
 error:
-	free(fh->path);
+	free(path);
 	free(fh);
 	return NULL;
 }
Signature · 1 line
2.39.5
#2 lore raw

PATCH v2 2/3 efi_loader: Prevent dereference of uninitialised variable

In reply to Andrew Goodbody

If phandler is returned as NULL from efi_search_protocol then protocol_interface is never assigned to. Instead return EFI_UNSUPPORTED as per the spec.

This issue found by Smatch.

Also eliminate the use of the variable protocol_interface as it is not needed.

---
 lib/efi_loader/efi_http.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
lib/efi_loader/efi_http.c +4 -5
index 189317fe2d2..9a0f2675132 100644
@@ -453,7 +453,6 @@ static efi_status_t EFIAPI efi_http_service_binding_destroy_child(
 	efi_status_t ret = EFI_SUCCESS;
 	struct efi_http_instance *http_instance;
 	struct efi_handler *phandler;
-	void *protocol_interface;
 
 	if (num_instances == 0)
 		return EFI_EXIT(EFI_NOT_FOUND);
@@ -463,18 +462,18 @@ static efi_status_t EFIAPI efi_http_service_binding_destroy_child(
 
 	efi_search_protocol(child_handle, &efi_http_guid, &phandler);
 
-	if (phandler)
-		protocol_interface = phandler->protocol_interface;
+	if (!phandler)
+		return EFI_EXIT(EFI_UNSUPPORTED);
 
 	ret = efi_delete_handle(child_handle);
 	if (ret != EFI_SUCCESS)
 		return EFI_EXIT(ret);
 
-	http_instance = (struct efi_http_instance *)protocol_interface;
+	http_instance = phandler->protocol_interface;
 	efi_free_pool(http_instance->http_load_addr);
 	http_instance->http_load_addr = NULL;
 
-	free(protocol_interface);
+	free(phandler->protocol_interface);
 
 	num_instances--;
 
Signature · 1 line
2.39.5
Reviewed-by on v2 2/3
#3 lore raw

Re: [PATCH v2 2/3] efi_loader: Prevent dereference of uninitialised variable

Quoted 10 lines · Andrew Goodbody

If phandler is returned as NULL from efi_search_protocol then protocol_interface is never assigned to. Instead return EFI_UNSUPPORTED as per the spec.

This issue found by Smatch.

Also eliminate the use of the variable protocol_interface as it is not needed.

Thanks a lot for fixing this.

Quoted 40 lines
---
  lib/efi_loader/efi_http.c | 9 ++++-----
  1 file changed, 4 insertions(+), 5 deletions(-)
lib/efi_loader/efi_http.c +4 -5
index 189317fe2d2..9a0f2675132 100644
@@ -453,7 +453,6 @@ static efi_status_t EFIAPI efi_http_service_binding_destroy_child(
  	efi_status_t ret = EFI_SUCCESS;
  	struct efi_http_instance *http_instance;
  	struct efi_handler *phandler;
-	void *protocol_interface;
 
  	if (num_instances == 0)
  		return EFI_EXIT(EFI_NOT_FOUND);
@@ -463,18 +462,18 @@ static efi_status_t EFIAPI efi_http_service_binding_destroy_child(
 
  	efi_search_protocol(child_handle, &efi_http_guid, &phandler);
 
-	if (phandler)
-		protocol_interface = phandler->protocol_interface;
+	if (!phandler)
+		return EFI_EXIT(EFI_UNSUPPORTED);
 
  	ret = efi_delete_handle(child_handle);
  	if (ret != EFI_SUCCESS)
  		return EFI_EXIT(ret);
 
-	http_instance = (struct efi_http_instance *)protocol_interface;
+	http_instance = phandler->protocol_interface;
  	efi_free_pool(http_instance->http_load_addr);
  	http_instance->http_load_addr = NULL;
 
-	free(protocol_interface);
+	free(phandler->protocol_interface);
 
  	num_instances--;
 
Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] efi_loader: Prevent dereference of uninitialised variable

In reply to Andrew Goodbody

Thanks Andrew

Quoted 5 lines · Andrew Goodbody

If phandler is returned as NULL from efi_search_protocol then protocol_interface is never assigned to. Instead return EFI_UNSUPPORTED as per the spec.

Quoted 7 lines

This issue found by Smatch.

Also eliminate the use of the variable protocol_interface as it is not needed.

---

Quoted 42 lines
 lib/efi_loader/efi_http.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
lib/efi_loader/efi_http.c +4 -6
index 189317fe2d2..9a0f2675132 100644
@@ -453,7 +453,6 @@ static efi_status_t EFIAPI efi_http_service_binding_destroy_child(
        efi_status_t ret = EFI_SUCCESS;
        struct efi_http_instance *http_instance;
        struct efi_handler *phandler;
-       void *protocol_interface;
 
        if (num_instances == 0)
                return EFI_EXIT(EFI_NOT_FOUND);
@@ -463,18 +462,18 @@ static efi_status_t EFIAPI efi_http_service_binding_destroy_child(
 
        efi_search_protocol(child_handle, &efi_http_guid, &phandler);
 
-       if (phandler)
-               protocol_interface = phandler->protocol_interface;
+       if (!phandler)
+               return EFI_EXIT(EFI_UNSUPPORTED);
 
        ret = efi_delete_handle(child_handle);
        if (ret != EFI_SUCCESS)
                return EFI_EXIT(ret);
 
-       http_instance = (struct efi_http_instance *)protocol_interface;
+       http_instance = phandler->protocol_interface;
        efi_free_pool(http_instance->http_load_addr);
        http_instance->http_load_addr = NULL;
 
-       free(protocol_interface);
+       free(phandler->protocol_interface);
 
        num_instances--;
 
 
--

2.39.5

#5 lore raw

PATCH v2 3/3 efi_loader: Prevent free of uninitialised pointer

In reply to Andrew Goodbody

Taking a goto to out_of_resources before receive_lengths is assigned will result in an attempt to free an unitialised pointer. Instead initialise receive_lengths to NULL on declaration to prevent this from occurring.

This issue was found by Smatch.

---
 lib/efi_loader/efi_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/efi_loader/efi_net.c +1 -1
index 8e708d8d350..3d092523dd5 100644
@@ -1130,7 +1130,7 @@ efi_status_t efi_net_register(struct udevice *dev)
 	struct efi_net_obj *netobj;
 	void *transmit_buffer = NULL;
 	uchar **receive_buffer = NULL;
-	size_t *receive_lengths;
+	size_t *receive_lengths = NULL;
 	int i, j;
 
 	if (!dev) {
Signature · 1 line
2.39.5