Skip to contentSkip to conversation

efi: fix and test getinfo() returned buffer size

PATCH New Vincent Stehlé 1 participant 3 messages Nov 12

lore
Share & tools
lore
#0 lore raw

cover efi: fix and test getinfo() returned buffer size

This couple of patches does the following:
- Fix the buffer size returned by EFI_FILE_PROTOCOL.GetInfo().
- Enhance the existing block device selftest to verify it.

The existing selftest exercises only one path of GetInfo() out of three, but as the fix is common to all paths this is a good start already.

---
Vincent Stehlé (2):
efi_loader: fix getinfo() returned buffer size
efi_selftest: verify getinfo() returned buffer size

 lib/efi_loader/efi_file.c                    | 11 +++++------
 lib/efi_selftest/efi_selftest_block_device.c |  6 ++++++
 2 files changed, 11 insertions(+), 6 deletions(-)

---
base-commit: 3cb2cbbc75aa7fe2391f662607d6b2f46c1292a7
change-id: 20261112-getinfo-5c5e514bbc98

Best regards,

Signature · 1 line
Vincent.
#1 lore raw

PATCH 1/2 efi_loader: fix getinfo() returned buffer size

The EFI_FILE_PROTOCOL.GetInfo() function must update the buffer size, but it does so only when the buffer is too small.
Update the buffer size on the return path to fix this.

Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Tom Rini <[email protected]>
---
 lib/efi_loader/efi_file.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
lib/efi_loader/efi_file.c +5 -6
index 7dee57d594a..d44aa5eab7f 100644
@@ -846,6 +846,7 @@ static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
 	struct file_handle *fh = to_fh(file);
 	efi_status_t ret = EFI_SUCCESS;
 	u16 *dst;
+	efi_uintn_t required_size = 0;
 
 	EFI_ENTRY("%p, %pUs, %p, %p", file, info_type, buffer_size, buffer);
 
@@ -858,14 +859,12 @@ static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
 	if (!guidcmp(info_type, &efi_file_info_guid)) {
 		struct efi_file_info *info = buffer;
 		char *filename = basename(fh);
-		unsigned int required_size;
 		loff_t file_size;
 
 		/* check buffer size: */
 		required_size = sizeof(*info) +
 				2 * (utf8_utf16_strlen(filename) + 1);
 		if (*buffer_size < required_size) {
-			*buffer_size = required_size;
 			ret = EFI_BUFFER_TOO_SMALL;
 			goto error;
 		}
@@ -896,7 +895,6 @@ static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
 	} else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
 		struct efi_file_system_info *info = buffer;
 		struct disk_partition part;
-		efi_uintn_t required_size;
 		int r;
 
 		if (fh->fs->part >= 1)
@@ -909,7 +907,6 @@ static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
 		}
 		required_size = sizeof(*info) + 2;
 		if (*buffer_size < required_size) {
-			*buffer_size = required_size;
 			ret = EFI_BUFFER_TOO_SMALL;
 			goto error;
 		}
@@ -933,8 +930,8 @@ static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
 		 */
 		info->volume_label[0] = 0;
 	} else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
-		if (*buffer_size < 2) {
-			*buffer_size = 2;
+		required_size = 2;
+		if (*buffer_size < required_size) {
 			ret = EFI_BUFFER_TOO_SMALL;
 			goto error;
 		}
@@ -944,6 +941,8 @@ static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
 	}
 
 error:
+	if (buffer_size && required_size)
+		*buffer_size = required_size;
 	return EFI_EXIT(ret);
 }
 
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/2 efi_selftest: verify getinfo() returned buffer size

In reply to Vincent Stehlé

In the block device selftest, verify correctness of the buffer size returned by EFI_FILE_PROTOCOL.GetInfo().

This can run on the sandbox with the following command:

./u-boot -T -c 'setenv efi_selftest block device; bootefi selftest'

Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Tom Rini <[email protected]>
---
 lib/efi_selftest/efi_selftest_block_device.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/efi_selftest/efi_selftest_block_device.c +6 -0
index 847d93693ee..ae064189603 100644
@@ -467,6 +467,12 @@ static int execute(void)
 		efi_st_error("Failed to get file system info\n");
 		return EFI_ST_FAILURE;
 	}
+	if (buf_size != system_info.info.size) {
+		efi_st_error("Buffer size %u differs from info size %u\n",
+			     (unsigned int)buf_size,
+			     (unsigned int)system_info.info.size);
+		return EFI_ST_FAILURE;
+	}
 	if (system_info.info.block_size != 512) {
 		efi_st_error("Wrong block size %u, expected 512\n",
 			     system_info.info.block_size);
Signature · 1 line
2.53.0