efi_loader: Address some issues found by Smatch
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]>
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
@@ -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
PATCH v2 2/3 efi_loader: Prevent dereference of uninitialised variable
In reply to Andrew GoodbodyIf 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
@@ -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
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_protocolthenprotocol_interfaceis never assigned to. Instead returnEFI_UNSUPPORTEDas per the spec.This issue found by Smatch.
Also eliminate the use of the variable
protocol_interfaceas it is not needed.Signed-off-by Andrew Goodbody <[email protected]>
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@@ -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--;
Re: [PATCH v2 2/3] efi_loader: Prevent dereference of uninitialised variable
In reply to Andrew GoodbodyThanks Andrew
Quoted 5 lines · Andrew Goodbody
If phandler is returned as NULL from
efi_search_protocolthenprotocol_interfaceis never assigned to. Instead returnEFI_UNSUPPORTEDas per the spec.
Quoted 7 lines
This issue found by Smatch.
Also eliminate the use of the variable
protocol_interfaceas it is not needed.Signed-off-by Andrew Goodbody <[email protected]>---
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@@ -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
PATCH v2 3/3 efi_loader: Prevent free of uninitialised pointer
In reply to Andrew GoodbodyTaking 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
@@ -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