efi_selftest: manageprotocols fixes and enhancements
cover efi_selftest: manageprotocols fixes and enhancements
Hi,
This patch series contains fixes and enhancements for the UEFI Protocols management test. It is broken down in small steps to ease review; feel free to squash and/or cherry-pick.
It is possible to verify with valgrind that the first couple of fixes in this series do indeed remove invalid writes, with the following steps:
1. Modify the efi_allocate_pool() and efi_free_pool() functions to call malloc() and free() respectively.
2. Build U-Boot with sandbox_defconfig and CONFIG_VALGRIND=y.
3. Run the following command:
$ valgrind --suppressions=scripts/u-boot.supp \
./u-boot -T -c "setenv efi_selftest manage protocols; \
bootefi selftest"Best regards,
Vincent.
Vincent Stehlé (5):
efi_selftest: fix buffer overflow
efi_selftest: fix buffer overflow and memory leak
efi_selftest: remove unnecessary initializations
efi_selftest: fix buffer size and count computations
efi_selftest: cosmetic: fix spelling in comments
.../efi_selftest_manageprotocols.c | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-)
Signature · 1 line
2.51.0
PATCH 1/5 efi_selftest: fix buffer overflow
The test of the UEFI LocateHandleBuffer() function clears a returned buffer at some point to reuse it, but there is an error in the size computation, which leads to a buffer overflow; fix it.
--- lib/efi_selftest/efi_selftest_manageprotocols.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/efi_selftest/efi_selftest_manageprotocols.c +1 -1
@@ -241,7 +241,7 @@ static int execute(void)return EFI_ST_FAILURE ; }/* Clear the buffer, we are reusing it it the next step. */ - boottime->set_mem (buffer,sizeof (efi_handle_t) * buffer_size,0 ); + boottime->set_mem (buffer,sizeof (efi_handle_t) * count,0 );/* * Test LocateHandle with ByProtocol
Signature · 1 line
2.51.0
Re: [PATCH 1/5] efi_selftest: fix buffer overflow
Quoted 10 lines · Vincent Stehlé
The test of the UEFI
LocateHandleBuffer()function clears a returned buffer at some point to reuse it, but there is an error in the size computation, which leads to a buffer overflow; fix it.Fixes 927ca890b09f ("efi_selftest: test protocol management")Signed-off-by Vincent Stehlé <[email protected]>Cc Heinrich Schuchardt <[email protected]>Cc Ilias Apalodimas <[email protected]>Cc Tom Rini <[email protected]>---
Quoted 16 lines
lib/efi_selftest/efi_selftest_manageprotocols.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/efi_selftest/efi_selftest_manageprotocols.c +1
-1@@ -241,7 +241,7 @@ static int execute(void)return EFI_ST_FAILURE ; }/* Clear the buffer, we are reusing it it the next step. */ - boottime->set_mem (buffer,sizeof (efi_handle_t) * buffer_size,0 ); + boottime->set_mem (buffer,sizeof (efi_handle_t) * count,0 );/* * Test LocateHandle with ByProtocol
Re: [PATCH 1/5] efi_selftest: fix buffer overflow
In reply to Vincent StehléQuoted 18 lines · Vincent Stehlé
The test of the UEFI
LocateHandleBuffer()function clears a returned buffer at some point to reuse it, but there is an error in the size computation, which leads to a buffer overflow; fix it.Fixes 927ca890b09f ("efi_selftest: test protocol management")Signed-off-by Vincent Stehlé <[email protected]>Cc Heinrich Schuchardt <[email protected]>Cc Ilias Apalodimas <[email protected]>Cc Tom Rini <[email protected]>--- lib/efi_selftest/efi_selftest_manageprotocols.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/efi_selftest/efi_selftest_manageprotocols.c +0
-0
lib/efi_selftest/efi_selftest_manageprotocols.c static int execute(void) · line 247
Quoted 8 lines
lib/efi_selftest/efi_selftest_manageprotocols.c +1
-1@@ -241,7 +241,7 @@ static int execute(void)return EFI_ST_FAILURE ; }/* Clear the buffer, we are reusing it it the next step. */ - boottime->set_mem (buffer,sizeof (efi_handle_t) * buffer_size,0 ); + boottime->set_mem (buffer,sizeof (efi_handle_t) * count,0 );/* * Test LocateHandle with ByProtocol
Hello Vincent,
Thank you for reviewing the code and pointing to an issue.
The fix looks incomplete to me:
In line 167 we allocate a buffer with LocateHandleBuffer(). Assigning buffer_size in line 173 does not make any sense, as we free the buffer in line 185.
In line 223 we allocate another buffer with LocateHandleBuffer().
Assigning the value of buffer_size value to count before the invocation doesn't make much sense.
You fix in line 244 looks correct.
Line 249 sets count to an arbitrary value that is not related to the size of the buffer.
Line 260 sets variable buffer_size and buffer_size is again used in line 306 to set count to an unused value.
We should completely remove variable buffer_size from the function.
Best regards
Heinrich
Re: [PATCH 1/5] efi_selftest: fix buffer overflow
On Tue, Feb 24, 2026 at 04:45:48PM +0100, Heinrich Schuchardt wrote:
Hi Heinrich,
Thanks for your review and sorry for the late reply. My answers below.
Best regards,
Vincent.
Quoted 29 lines · Vincent Stehlé
Quoted 18 lines
The test of the UEFI
LocateHandleBuffer()function clears a returned buffer at some point to reuse it, but there is an error in the size computation, which leads to a buffer overflow; fix it.Fixes 927ca890b09f ("efi_selftest: test protocol management")Signed-off-by Vincent Stehlé <[email protected]>Cc Heinrich Schuchardt <[email protected]>Cc Ilias Apalodimas <[email protected]>Cc Tom Rini <[email protected]>--- lib/efi_selftest/efi_selftest_manageprotocols.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/efi_selftest/efi_selftest_manageprotocols.c +0
-0lib/efi_selftest/efi_selftest_manageprotocols.c static int execute(void) · line 246
Quoted 7 lines
lib/efi_selftest/efi_selftest_manageprotocols.c +1
-1@@ -241,7 +241,7 @@ static int execute(void)return EFI_ST_FAILURE ; }/* Clear the buffer, we are reusing it it the next step. */ - boottime->set_mem (buffer,sizeof (efi_handle_t) * buffer_size,0 ); + boottime->set_mem (buffer,sizeof (efi_handle_t) * count,0 );/* * Test LocateHandle with ByProtocol Hello Vincent,
Quoted 3 lines
Thank you for reviewing the code and pointing to an issue.
The fix looks incomplete to me:
You are right: I broke down all the fixes into multiple steps, which explains why the first patch ("fix buffer overflow") is not the full story.
I thought this would ease reviewing but now I wonder if that was maybe a bad idea. Let me know if I should send a v2 with the series as a single patch if that helps.
Quoted 4 lines
In line 167 we allocate a buffer with
LocateHandleBuffer(). Assigningbuffer_sizein line 173 does not make any sense, as we free the buffer in line 185.
This is removed in patch 4 ("fix buffer size and count computations").
Quoted 4 lines
In line 223 we allocate another buffer with
LocateHandleBuffer(). Assigning the value ofbuffer_sizevalue to count before the invocation doesn't make much sense.
This is removed in patch 3 ("remove unnecessary initializations").
Quoted 5 lines
You fix in line 244 looks correct.
Line 249 sets count to an arbitrary value that is not related to the size of the buffer.
This part is reworked in patch 4 to use buffer_size instead.
Quoted 3 lines
Line 260 sets variable
buffer_sizeandbuffer_sizeis again used in line 306 to set count to an unused value.
This is removed in patch 3.
Quoted 2 lines
We should completely remove variable
buffer_sizefrom the function.
The patch series takes a different approach: keep both count and buffer_size variables, and make sure to use those variables to contain quantities corresponding to their respective names.
Quoted 4 lines
Best regards
Heinrich
PATCH 2/5 efi_selftest: fix buffer overflow and memory leak
In reply to Vincent StehléThe test of the UEFI UninstallMultipleProtocolInterfaces() function clears a buffer returned by LocateHandleBuffer() at some point, but there is an error in the size computation, which leads to a buffer overflow. The buffer is also never freed, which leads to a memory leak.
As the buffer is in fact not reused during the test, fix both errors by simply freeing the buffer.
--- lib/efi_selftest/efi_selftest_manageprotocols.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
lib/efi_selftest/efi_selftest_manageprotocols.c +6 -1
@@ -319,7 +319,12 @@ static int execute(void)efi_st_error ("Failed to locate new handle\n" );return EFI_ST_FAILURE ; } - boottime->set_mem (buffer,sizeof (efi_handle_t) * buffer_size,0 ); +/* Release buffer */ + ret = boottime->free_pool (buffer); +if (ret !=EFI_SUCCESS ) { +efi_st_error ("FreePool failed\n" ); +return EFI_ST_FAILURE ; + }/* * Test ProtocolsPerHandle
Signature · 1 line
2.51.0
Re: [PATCH 2/5] efi_selftest: fix buffer overflow and memory leak
Quoted 9 lines · Vincent Stehlé
The test of the UEFI
UninstallMultipleProtocolInterfaces()function clears a buffer returned byLocateHandleBuffer()at some point, but there is an error in the size computation, which leads to a buffer overflow. The buffer is also never freed, which leads to a memory leak.As the buffer is in fact not reused during the test, fix both errors by simply freeing the buffer.
Quoted 6 lines
Fixes 927ca890b09f ("efi_selftest: test protocol management")Signed-off-by Vincent Stehlé <[email protected]>Cc Heinrich Schuchardt <[email protected]>Cc Ilias Apalodimas <[email protected]>Cc Tom Rini <[email protected]>---
Quoted 24 lines
lib/efi_selftest/efi_selftest_manageprotocols.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)lib/efi_selftest/efi_selftest_manageprotocols.c +6
-2@@ -319,7 +319,12 @@ static int execute(void)efi_st_error ("Failed to locate new handle\n" );return EFI_ST_FAILURE ; } - boottime->set_mem (buffer,sizeof (efi_handle_t) * buffer_size,0 ); +/* Release buffer */ + ret = boottime->free_pool (buffer); +if (ret !=EFI_SUCCESS ) { +efi_st_error ("FreePool failed\n" ); +return EFI_ST_FAILURE ; + }/* * Test ProtocolsPerHandle -- 2.51.0
PATCH 3/5 efi_selftest: remove unnecessary initializations
In reply to Vincent StehléThe UEFI function LocateHandleBuffer() always writes to the value pointed to by its NoHandles argument (in the test, `count'); remove the unnecessary initializations.
Remove also one copy to buffer_size, which has become unnecessary.
--- lib/efi_selftest/efi_selftest_manageprotocols.c | 3 --- 1 file changed, 3 deletions(-)
lib/efi_selftest/efi_selftest_manageprotocols.c +0 -3
@@ -219,7 +219,6 @@ static int execute(void)* * handle1, handle2 */ - count = buffer_size; ret = boottime->locate_handle_buffer (BY_PROTOCOL , &guid1,NULL , &count, &buffer);if (ret !=EFI_SUCCESS ) {
@@ -257,7 +256,6 @@ static int execute(void)efi_st_error ("LocateHandle failed to locate new handles\n" );return EFI_ST_FAILURE ; } - buffer_size = count; ret =find_in_buffer (handle1, count, buffer);if (ret !=EFI_SUCCESS ) {efi_st_error ("LocateHandle failed to locate new handles\n" );
@@ -303,7 +301,6 @@ static int execute(void)/* * Check that the protocols are really uninstalled. */ - count = buffer_size; ret = boottime->locate_handle_buffer (BY_PROTOCOL , &guid1,NULL , &count, &buffer);if (ret !=EFI_SUCCESS ) {
Signature · 1 line
2.51.0
Re: [PATCH 3/5] efi_selftest: remove unnecessary initializations
Quoted 11 lines · Vincent Stehlé
The UEFI function
LocateHandleBuffer()always writes to the value pointed to by its NoHandles argument (in the test, `count'); remove the unnecessary initializations.Remove also one copy to
buffer_size, which has become unnecessary.Signed-off-by Vincent Stehlé <[email protected]>Cc Heinrich Schuchardt <[email protected]>Cc Ilias Apalodimas <[email protected]>Cc Tom Rini <[email protected]>---
Quoted 31 lines
lib/efi_selftest/efi_selftest_manageprotocols.c | 3 --- 1 file changed, 3 deletions(-)lib/efi_selftest/efi_selftest_manageprotocols.c +0
-3@@ -219,7 +219,6 @@ static int execute(void)* * handle1, handle2 */ - count = buffer_size; ret = boottime->locate_handle_buffer (BY_PROTOCOL , &guid1,NULL , &count, &buffer);if (ret !=EFI_SUCCESS ) {@@ -257,7 +256,6 @@ static int execute(void)efi_st_error ("LocateHandle failed to locate new handles\n" );return EFI_ST_FAILURE ; } - buffer_size = count; ret =find_in_buffer (handle1, count, buffer);if (ret !=EFI_SUCCESS ) {efi_st_error ("LocateHandle failed to locate new handles\n" );@@ -303,7 +301,6 @@ static int execute(void)/* * Check that the protocols are really uninstalled. */ - count = buffer_size; ret = boottime->locate_handle_buffer (BY_PROTOCOL , &guid1,NULL , &count, &buffer);if (ret !=EFI_SUCCESS ) {
PATCH 4/5 efi_selftest: fix buffer size and count computations
In reply to Vincent StehléWhen testing the UEFI LocateHandle() function, the BufferSize argument is erroneously computed with a count from three test steps before, leading to an incorrect input buffer size.
Also, while the buffer size returned by LocateHandle() is correctly converted to a count during a subsequent comparison, it is incorrectly passed "as is" as a count to later calls to find_in_buffer().
Finally, the variables usage during this test step is misleading, as the buffer_size' variable is used to hold a count, and the count' variable is used to hold a buffer size.
Fix all those issues at once, and remove also one copy to buffer_size, which has now become unnecessary.
--- lib/efi_selftest/efi_selftest_manageprotocols.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
lib/efi_selftest/efi_selftest_manageprotocols.c +4 -4
@@ -170,7 +170,6 @@ static int execute(void)efi_st_error ("LocateHandleBuffer with AllHandles failed\n" );return EFI_ST_FAILURE ; } - buffer_size = count; ret =find_in_buffer (handle1, count, buffer);if (ret !=EFI_SUCCESS ) {efi_st_error ("LocateHandleBuffer failed to locate new handle\n" );
@@ -245,14 +244,15 @@ static int execute(void)/* * Test LocateHandle with ByProtocol */ - count = buffer_size *sizeof (efi_handle_t); + buffer_size = count *sizeof (efi_handle_t); ret = boottime->locate_handle (BY_PROTOCOL , &guid1,NULL , - &count, buffer); + &buffer_size, buffer);if (ret !=EFI_SUCCESS ) {efi_st_error ("LocateHandle with ByProtocol failed\n" );return EFI_ST_FAILURE ; } -if (count /sizeof (efi_handle_t) !=2 ) { + count = buffer_size /sizeof (efi_handle_t); +if (count !=2 ) {efi_st_error ("LocateHandle failed to locate new handles\n" );return EFI_ST_FAILURE ; }
Signature · 1 line
2.51.0
Re: [PATCH 4/5] efi_selftest: fix buffer size and count computations
Quoted 13 lines · Vincent Stehlé
When testing the UEFI
LocateHandle()function, the BufferSize argument is erroneously computed with a count from three test steps before, leading to an incorrect input buffer size.Also, while the buffer size returned by
LocateHandle()is correctly converted to a count during a subsequent comparison, it is incorrectly passed "as is" as a count to later calls tofind_in_buffer().Finally, the variables usage during this test step is misleading, as the
buffer_size' variable is used to hold a count, and thecount' variable is used to hold a buffer size.
Quoted 8 lines
Fix all those issues at once, and remove also one copy to
buffer_size, which has now become unnecessary.Signed-off-by Vincent Stehlé <[email protected]>Cc Heinrich Schuchardt <[email protected]>Cc Ilias Apalodimas <[email protected]>Cc Tom Rini <[email protected]>---
Quoted 37 lines
lib/efi_selftest/efi_selftest_manageprotocols.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)lib/efi_selftest/efi_selftest_manageprotocols.c +4
-5@@ -170,7 +170,6 @@ static int execute(void)efi_st_error ("LocateHandleBuffer with AllHandles failed\n" );return EFI_ST_FAILURE ; } - buffer_size = count; ret =find_in_buffer (handle1, count, buffer);if (ret !=EFI_SUCCESS ) {efi_st_error ("LocateHandleBuffer failed to locate new handle\n" );@@ -245,14 +244,15 @@ static int execute(void)/* * Test LocateHandle with ByProtocol */ - count = buffer_size *sizeof (efi_handle_t); + buffer_size = count *sizeof (efi_handle_t); ret = boottime->locate_handle (BY_PROTOCOL , &guid1,NULL , - &count, buffer); + &buffer_size, buffer);if (ret !=EFI_SUCCESS ) {efi_st_error ("LocateHandle with ByProtocol failed\n" );return EFI_ST_FAILURE ; } -if (count /sizeof (efi_handle_t) !=2 ) { + count = buffer_size /sizeof (efi_handle_t); +if (count !=2 ) {efi_st_error ("LocateHandle failed to locate new handles\n" );return EFI_ST_FAILURE ; } --2.51.0
PATCH 5/5 efi_selftest: cosmetic: fix spelling in comments
In reply to Vincent StehléFix a few UEFI function names, as well as a typo.
--- lib/efi_selftest/efi_selftest_manageprotocols.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
lib/efi_selftest/efi_selftest_manageprotocols.c +4 -4
@@ -6,7 +6,7 @@* * This unit test checks the following protocol services: * InstallProtocolInterface, UninstallProtocolInterface, -* InstallMultipleProtocolsInterfaces, UninstallMultipleProtocolsInterfaces, +* InstallMultipleProtocolInterfaces, UninstallMultipleProtocolInterfaces, * HandleProtocol, ProtocolsPerHandle, * LocateHandle, LocateHandleBuffer. */
@@ -188,7 +188,7 @@ static int execute(void) }/* -* Test error handling in UninstallMultipleProtocols +* Test error handling in UninstallMultipleProtocolInterfaces * * These are the installed protocol interfaces on handle 2: *
@@ -238,7 +238,7 @@ static int execute(void)efi_st_error ("LocateHandleBuffer failed to locate new handle\n" );return EFI_ST_FAILURE ; } -/* Clear the buffer, we are reusing it it the next step. */ +/* Clear the buffer, we are reusing it in the next step. */ boottime->set_mem (buffer,sizeof (efi_handle_t) * count,0 );/*
@@ -287,7 +287,7 @@ static int execute(void) }/* -* Test UninstallMultipleProtocols +* Test UninstallMultipleProtocolInterfaces */ ret = boottime->uninstall_multiple_protocol_interfaces( handle2,
Signature · 1 line
2.51.0
Re: [PATCH 5/5] efi_selftest: cosmetic: fix spelling in comments
Quoted 7 lines · Vincent Stehlé
Fix a few UEFI function names, as well as a typo.
Signed-off-by Vincent Stehlé <[email protected]>Cc Heinrich Schuchardt <[email protected]>Cc Ilias Apalodimas <[email protected]>Cc Tom Rini <[email protected]>---
Quoted 43 lines
lib/efi_selftest/efi_selftest_manageprotocols.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)lib/efi_selftest/efi_selftest_manageprotocols.c +4
-4@@ -6,7 +6,7 @@* * This unit test checks the following protocol services: * InstallProtocolInterface, UninstallProtocolInterface, -* InstallMultipleProtocolsInterfaces, UninstallMultipleProtocolsInterfaces, +* InstallMultipleProtocolInterfaces, UninstallMultipleProtocolInterfaces, * HandleProtocol, ProtocolsPerHandle, * LocateHandle, LocateHandleBuffer. */ @@ -188,7 +188,7 @@ static int execute(void) }/* -* Test error handling in UninstallMultipleProtocols +* Test error handling in UninstallMultipleProtocolInterfaces * * These are the installed protocol interfaces on handle 2: * @@ -238,7 +238,7 @@ static int execute(void)efi_st_error ("LocateHandleBuffer failed to locate new handle\n" );return EFI_ST_FAILURE ; } -/* Clear the buffer, we are reusing it it the next step. */ +/* Clear the buffer, we are reusing it in the next step. */ boottime->set_mem (buffer,sizeof (efi_handle_t) * count,0 );/* @@ -287,7 +287,7 @@ static int execute(void) }/* -* Test UninstallMultipleProtocols +* Test UninstallMultipleProtocolInterfaces */ ret = boottime->uninstall_multiple_protocol_interfaces( handle2,
Re: [PATCH 5/5] efi_selftest: cosmetic: fix spelling in comments
In reply to Vincent StehléQuoted 6 lines · Vincent Stehlé
Fix a few UEFI function names, as well as a typo.
Signed-off-by Vincent Stehlé <[email protected]>Cc Heinrich Schuchardt <[email protected]>Cc Ilias Apalodimas <[email protected]>Cc Tom Rini <[email protected]>
Quoted 44 lines
--- lib/efi_selftest/efi_selftest_manageprotocols.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)lib/efi_selftest/efi_selftest_manageprotocols.c +4
-4@@ -6,7 +6,7 @@* * This unit test checks the following protocol services: * InstallProtocolInterface, UninstallProtocolInterface, -* InstallMultipleProtocolsInterfaces, UninstallMultipleProtocolsInterfaces, +* InstallMultipleProtocolInterfaces, UninstallMultipleProtocolInterfaces, * HandleProtocol, ProtocolsPerHandle, * LocateHandle, LocateHandleBuffer. */ @@ -188,7 +188,7 @@ static int execute(void) }/* -* Test error handling in UninstallMultipleProtocols +* Test error handling in UninstallMultipleProtocolInterfaces * * These are the installed protocol interfaces on handle 2: * @@ -238,7 +238,7 @@ static int execute(void)efi_st_error ("LocateHandleBuffer failed to locate new handle\n" );return EFI_ST_FAILURE ; } -/* Clear the buffer, we are reusing it it the next step. */ +/* Clear the buffer, we are reusing it in the next step. */ boottime->set_mem (buffer,sizeof (efi_handle_t) * count,0 );/* @@ -287,7 +287,7 @@ static int execute(void) }/* -* Test UninstallMultipleProtocols +* Test UninstallMultipleProtocolInterfaces */ ret = boottime->uninstall_multiple_protocol_interfaces( handle2,