Skip to contentSkip to conversation

efi_loader: Fix memory map size check to avoid out-of-bounds access

PATCH New Stefan Brüns 2 participants 3 messages Oct 9, 2016 – Oct 13, 2016 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v4 2/7 efi_loader: Fix memory map size check to avoid out-of-bounds access

The current efi_get_memory_map() function overwrites the map_size property before reading its value. That way the sanity check whether our memory map fits into the given array always succeeds, potentially overwriting arbitrary payload memory.

This patch moves the property update write after its sanity check, so that the check actually verifies the correct value.

So far this has not triggered any known bugs, but we're better off safe than sorry.

If the buffer is to small, the returned memory_map_size indicates the required size to the caller.

---
 lib/efi_loader/efi_memory.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/efi_loader/efi_memory.c +2 -1
index ebe8e94..1d23783 100644
@@ -336,6 +336,7 @@ efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
 	ulong map_size = 0;
 	int map_entries = 0;
 	struct list_head *lhandle;
+	unsigned long provided_map_size = *memory_map_size;
 
 	list_for_each(lhandle, &efi_mem)
 		map_entries++;
@@ -350,7 +351,7 @@ efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
 	if (descriptor_version)
 		*descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
 
-	if (*memory_map_size < map_size)
+	if (provided_map_size < map_size)
 		return EFI_BUFFER_TOO_SMALL;
 
 	/* Copy list into array */
Signature · 1 line
2.10.0
#2 lore raw

PATCH v4 2/7 efi_loader: Fix memory map size check to avoid out-of-bounds access

Quoted 8 lines · Stefan Br?ns

The current efi_get_memory_map() function overwrites the map_size property before reading its value. That way the sanity check whether our memory map fits into the given array always succeeds, potentially overwriting arbitrary payload memory.

This patch moves the property update write after its sanity check, so that the check actually verifies the correct value.

Quoted 7 lines

So far this has not triggered any known bugs, but we're better off safe than sorry.

If the buffer is to small, the returned memory_map_size indicates the required size to the caller.

Alex

#3 lore raw

PATCH v4 2/7 efi_loader: Fix memory map size check to avoid out-of-bounds access

In reply to Stefan Brüns
Quoted 8 lines

The current efi_get_memory_map() function overwrites the map_size property before reading its value. That way the sanity check whether our memory map fits into the given array always succeeds, potentially overwriting arbitrary payload memory.

This patch moves the property update write after its sanity check, so that the check actually verifies the correct value.

Quoted 8 lines

So far this has not triggered any known bugs, but we're better off safe than sorry.

If the buffer is to small, the returned memory_map_size indicates the required size to the caller.

Thanks, applied to