From: ard.biesheuvel@linaro.org (Ard Biesheuvel)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v4 20/22] efi: stub: add implementation of efi_random_alloc()
Date: Tue, 26 Jan 2016 18:10:47 +0100 [thread overview]
Message-ID: <1453828249-14467-21-git-send-email-ard.biesheuvel@linaro.org> (raw)
In-Reply-To: <1453828249-14467-1-git-send-email-ard.biesheuvel@linaro.org>
This implements efi_random_alloc(), which allocates a chunk of memory of
a certain size at a certain alignment, and uses the random_seed argument
it receives to randomize the address of the allocation.
This is implemented by iterating over the UEFI memory map, counting the
number of suitable slots (aligned offsets) within each region, and picking
a random number between 0 and 'number of slots - 1' to select the slot,
This should guarantee that each possible offset is chosen equally likely.
Suggested-by: Kees Cook <keescook@chromium.org>
Cc: Matt Fleming <matt@codeblueprint.co.uk>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
drivers/firmware/efi/libstub/efistub.h | 4 +
drivers/firmware/efi/libstub/random.c | 100 ++++++++++++++++++++
2 files changed, 104 insertions(+)
diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
index 206b7252b9d1..5ed3d3f38166 100644
--- a/drivers/firmware/efi/libstub/efistub.h
+++ b/drivers/firmware/efi/libstub/efistub.h
@@ -46,4 +46,8 @@ void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
efi_status_t efi_get_random_bytes(efi_system_table_t *sys_table,
unsigned long size, u8 *out);
+efi_status_t efi_random_alloc(efi_system_table_t *sys_table_arg,
+ unsigned long size, unsigned long align,
+ unsigned long *addr, unsigned long random_seed);
+
#endif
diff --git a/drivers/firmware/efi/libstub/random.c b/drivers/firmware/efi/libstub/random.c
index 97941ee5954f..b98346350230 100644
--- a/drivers/firmware/efi/libstub/random.c
+++ b/drivers/firmware/efi/libstub/random.c
@@ -33,3 +33,103 @@ efi_status_t efi_get_random_bytes(efi_system_table_t *sys_table_arg,
return rng->get_rng(rng, NULL, size, out);
}
+
+/*
+ * Return the number of slots covered by this entry, i.e., the number of
+ * addresses it covers that are suitably aligned and supply enough room
+ * for the allocation.
+ */
+static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
+ unsigned long size,
+ unsigned long align)
+{
+ u64 start, end;
+
+ if (md->type != EFI_CONVENTIONAL_MEMORY)
+ return 0;
+
+ start = round_up(md->phys_addr, align);
+ end = round_down(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - size,
+ align);
+
+ if (start > end)
+ return 0;
+
+ return (end - start + 1) / align;
+}
+
+/*
+ * The UEFI memory descriptors have a virtual address field that is only used
+ * when installing the virtual mapping using SetVirtualAddressMap(). Since it
+ * is unused here, we can reuse it to keep track of each descriptor's slot
+ * count.
+ */
+#define MD_NUM_SLOTS(md) ((md)->virt_addr)
+
+efi_status_t efi_random_alloc(efi_system_table_t *sys_table_arg,
+ unsigned long size,
+ unsigned long align,
+ unsigned long *addr,
+ unsigned long random_seed)
+{
+ unsigned long map_size, desc_size, total_slots = 0, target_slot;
+ efi_status_t status = EFI_NOT_FOUND;
+ efi_memory_desc_t *memory_map;
+ int map_offset;
+
+ status = efi_get_memory_map(sys_table_arg, &memory_map, &map_size,
+ &desc_size, NULL, NULL);
+ if (status != EFI_SUCCESS)
+ return status;
+
+ if (align < EFI_ALLOC_ALIGN)
+ align = EFI_ALLOC_ALIGN;
+
+ /* count the suitable slots in each memory map entry */
+ for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
+ efi_memory_desc_t *md = (void *)memory_map + map_offset;
+ unsigned long slots;
+
+ slots = get_entry_num_slots(md, size, align);
+ MD_NUM_SLOTS(md) = slots;
+ total_slots += slots;
+ }
+
+ /* find a random number between 0 and total_slots */
+ target_slot = (total_slots * (u16)random_seed) >> 16;
+
+ /*
+ * target_slot is now a value in the range [0, total_slots), and so
+ * it corresponds with exactly one of the suitable slots we recorded
+ * when iterating over the memory map the first time around.
+ *
+ * So iterate over the memory map again, subtracting the number of
+ * slots of each entry at each iteration, until we have found the entry
+ * that covers our chosen slot. Use the residual value of target_slot
+ * to calculate the randomly chosen address, and allocate it directly
+ * using EFI_ALLOCATE_ADDRESS.
+ */
+ for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
+ efi_memory_desc_t *md = (void *)memory_map + map_offset;
+ efi_physical_addr_t target;
+ unsigned long pages;
+
+ if (target_slot >= MD_NUM_SLOTS(md)) {
+ target_slot -= MD_NUM_SLOTS(md);
+ continue;
+ }
+
+ target = round_up(md->phys_addr, align) + target_slot * align;
+ pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
+
+ status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS,
+ EFI_LOADER_DATA, pages, &target);
+ if (status == EFI_SUCCESS)
+ *addr = target;
+ break;
+ }
+
+ efi_call_early(free_pool, memory_map);
+
+ return status;
+}
--
2.5.0
next prev parent reply other threads:[~2016-01-26 17:10 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-01-26 17:10 [PATCH v4 00/22] arm64: implement support for KASLR Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 01/22] of/fdt: make memblock minimum physical address arch configurable Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 02/22] arm64: introduce KIMAGE_VADDR as the virtual base of the kernel region Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 03/22] arm64: pgtable: implement static [pte|pmd|pud]_offset variants Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 04/22] arm64: decouple early fixmap init from linear mapping Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 05/22] arm64: kvm: deal with kernel symbols outside of " Ard Biesheuvel
2016-01-26 17:45 ` Marc Zyngier
2016-01-26 17:10 ` [PATCH v4 06/22] arm64: add support for ioremap() block mappings Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 07/22] arm64: move kernel image to base of vmalloc area Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 08/22] arm64: add support for module PLTs Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 09/22] extable: add support for relative extables to search and sort routines Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 10/22] arm64: switch to relative exception tables Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 11/22] arm64: avoid R_AARCH64_ABS64 relocations for Image header fields Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 12/22] arm64: avoid dynamic relocations in early boot code Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 13/22] arm64: allow kernel Image to be loaded anywhere in physical memory Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 14/22] arm64: make asm/elf.h available to asm files Ard Biesheuvel
2016-01-26 17:42 ` Mark Rutland
2016-01-26 17:10 ` [PATCH v4 15/22] scripts/sortextable: add support for ET_DYN binaries Ard Biesheuvel
2016-01-26 23:25 ` Kees Cook
2016-01-26 17:10 ` [PATCH v4 16/22] kallsyms: add support for relative offsets in kallsyms address table Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 17/22] arm64: add support for building the kernel as a relocate PIE binary Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 18/22] arm64: add support for kernel ASLR Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 19/22] efi: stub: implement efi_get_random_bytes() based on EFI_RNG_PROTOCOL Ard Biesheuvel
2016-01-26 17:10 ` Ard Biesheuvel [this message]
2016-01-26 23:52 ` [PATCH v4 20/22] efi: stub: add implementation of efi_random_alloc() Kees Cook
2016-01-29 15:39 ` Matt Fleming
2016-01-26 17:10 ` [PATCH v4 21/22] efi: stub: use high allocation for converted command line Ard Biesheuvel
2016-01-26 17:10 ` [PATCH v4 22/22] arm64: efi: invoke EFI_RNG_PROTOCOL to supply KASLR randomness Ard Biesheuvel
2016-01-29 15:57 ` Matt Fleming
2016-01-29 18:26 ` [PATCH v4 00/22] arm64: implement support for KASLR Catalin Marinas
2016-01-29 18:49 ` Ard Biesheuvel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1453828249-14467-21-git-send-email-ard.biesheuvel@linaro.org \
--to=ard.biesheuvel@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).