efi_loader: Fix memory map size check to avoid out-of-bounds access
authorStefan Brüns <stefan.bruens@rwth-aachen.de>
Sun, 9 Oct 2016 20:17:07 +0000 (22:17 +0200)
committerAlexander Graf <agraf@suse.de>
Tue, 18 Oct 2016 07:08:07 +0000 (09:08 +0200)
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.

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
Signed-off-by: Alexander Graf <agraf@suse.de>
lib/efi_loader/efi_memory.c

index ebe8e94c834c82fcef0d56a82a4a6d28e2353390..1d23783020213bf3d3bde7ad05483ae7fe1ec625 100644 (file)
@@ -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 */