From patchwork Fri Mar 24 13:24:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 95937 Delivered-To: patch@linaro.org Received: by 10.140.89.233 with SMTP id v96csp43765qgd; Fri, 24 Mar 2017 06:24:45 -0700 (PDT) X-Received: by 10.99.173.6 with SMTP id g6mr8813174pgf.75.1490361885915; Fri, 24 Mar 2017 06:24:45 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w15si2862053plk.292.2017.03.24.06.24.45; Fri, 24 Mar 2017 06:24:45 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-efi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-efi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-efi-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757252AbdCXNYl (ORCPT + 2 others); Fri, 24 Mar 2017 09:24:41 -0400 Received: from mail-wm0-f52.google.com ([74.125.82.52]:37988 "EHLO mail-wm0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935805AbdCXNYh (ORCPT ); Fri, 24 Mar 2017 09:24:37 -0400 Received: by mail-wm0-f52.google.com with SMTP id t189so2181173wmt.1 for ; Fri, 24 Mar 2017 06:24:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=zxQ5vquC/dh00BbVfL5jk/2fRGRP4TAoGv2MNGxi6e8=; b=DYphlG4+gCPNdZ5xXhkijcQ1Ps4J99zIDj2MHCR9wQBZDbaw/8mu8olJ42dnpTBWpp DQJoOTGYWEEgGdZwqEMpgXJykGTiL7dgonQeu48uBHfKa6L+mOebgzBwcXPCk9APY/l8 FtyGnw9H6lVS1o9Bxl72VMgKJzG/E2Ol4M2z0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=zxQ5vquC/dh00BbVfL5jk/2fRGRP4TAoGv2MNGxi6e8=; b=lgHSBsm2PEyFkp99Gy2OOIeN1ayr+v5zSaHDDlrlL5pINdylbTvbpkOkVBTLGOdRs4 zd08ByoTxk1HuF+HDHRjODcnyWMwdpBeb4oY5Dyh+OlwHBNttWAGnPJFB7j7mZdFgwjU S3RSOuTYl2n987i6zt5FgTeBHo5fS07Q4DdZo9TRP6hnp4P4cDgBscV4QEPtjLwbBZ8G xQDGWBgDEPArJBck7YKNyXwIUBOM1d5/HF0wJce0jH3025pa9RlJZGH5q7UwRyEHpxDV bGV7m5G7cuUILCWycI/dpS/plszXEdHnRFsSd8Q8J53iV+EtryUEOosqCxyvxe7c8VYA jLIw== X-Gm-Message-State: AFeK/H0FAIWoy+rWOK+h/OQEx2ZVFB57Fs6ZMLrOgpWtYgHb+2AEL9qaR+HRQgY7hwZZAdtU X-Received: by 10.28.87.6 with SMTP id l6mr2952121wmb.109.1490361869701; Fri, 24 Mar 2017 06:24:29 -0700 (PDT) Received: from localhost.localdomain ([196.67.95.24]) by smtp.gmail.com with ESMTPSA id 92sm2924626wrh.8.2017.03.24.06.24.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 24 Mar 2017 06:24:28 -0700 (PDT) From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org, matt@codeblueprint.co.uk, leif.lindholm@linaro.org, rfranz@cavium.com, mingo@kernel.org, bp@alien8.de, mark.rutland@arm.com, kernel-hardening@lists.openwall.com, Ard Biesheuvel Subject: [PATCH 4/4] ef/libstub: arm/arm64: randomize the base of the UEFI rt services region Date: Fri, 24 Mar 2017 13:24:10 +0000 Message-Id: <20170324132410.16628-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170324132410.16628-1-ard.biesheuvel@linaro.org> References: <20170324132410.16628-1-ard.biesheuvel@linaro.org> Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org Update the allocation logic for the virtual mapping of the UEFI runtime services to start from a randomized base address if KASLR is in effect, and if the UEFI firmware exposes an implementation of EFI_RNG_PROTOCOL. This makes it more difficult to predict the location of exploitable data structures in the runtime UEFI firmware, which increases robustness against attacks. Note that these regions are only mapped during the time a runtime service call is in progress, and only on a single CPU at a time, bit give the lack of a downside, let's enable it nonetheless. Cc: Ingo Molnar Cc: Borislav Petkov Cc: Matt Fleming Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/arm-stub.c | 48 ++++++++++++++------ 1 file changed, 35 insertions(+), 13 deletions(-) -- 2.9.3 -- To unsubscribe from this list: send the line "unsubscribe linux-efi" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/firmware/efi/libstub/arm-stub.c b/drivers/firmware/efi/libstub/arm-stub.c index 6f522e3091af..eb5225884098 100644 --- a/drivers/firmware/efi/libstub/arm-stub.c +++ b/drivers/firmware/efi/libstub/arm-stub.c @@ -18,6 +18,22 @@ #include "efistub.h" +/* + * This is the base address at which to start allocating virtual memory ranges + * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use + * any allocation we choose, and eliminate the risk of a conflict after kexec. + * The value chosen is the largest non-zero power of 2 suitable for this purpose + * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can + * be mapped efficiently. + * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split, + * map everything below 1 GB. (512 MB is a reasonable upper bound for the + * entire footprint of the UEFI runtime services memory regions) + */ +#define EFI_RT_VIRTUAL_BASE SZ_512M +#define EFI_RT_VIRTUAL_SIZE SZ_512M + +static u64 efi_virt_base = EFI_RT_VIRTUAL_BASE; + efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg, void *__image, void **__fh) { @@ -209,6 +225,25 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table, efi_random_get_seed(sys_table); + if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !nokaslr()) { + /* + * Randomize the base of the UEFI runtime services region. + * Preserve the 2 MB alignment of the region by taking a + * shift of 21 bit positions into account when scaling + * the headroom value using a 32-bit random value. + */ + u64 headroom = TASK_SIZE - EFI_RT_VIRTUAL_BASE - + EFI_RT_VIRTUAL_SIZE; + u32 rnd; + + status = efi_get_random_bytes(sys_table, sizeof(rnd), + (u8 *)&rnd); + if (status == EFI_SUCCESS) { + efi_virt_base = EFI_RT_VIRTUAL_BASE + + (((headroom >> 21) * rnd) >> (32 - 21)); + } + } + new_fdt_addr = fdt_addr; status = allocate_new_fdt_and_exit_boot(sys_table, handle, &new_fdt_addr, efi_get_max_fdt_addr(dram_base), @@ -238,18 +273,6 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table, return EFI_ERROR; } -/* - * This is the base address at which to start allocating virtual memory ranges - * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use - * any allocation we choose, and eliminate the risk of a conflict after kexec. - * The value chosen is the largest non-zero power of 2 suitable for this purpose - * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can - * be mapped efficiently. - * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split, - * map everything below 1 GB. - */ -#define EFI_RT_VIRTUAL_BASE SZ_512M - static int cmp_mem_desc(const void *l, const void *r) { const efi_memory_desc_t *left = l, *right = r; @@ -299,7 +322,6 @@ void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size, unsigned long desc_size, efi_memory_desc_t *runtime_map, int *count) { - u64 efi_virt_base = EFI_RT_VIRTUAL_BASE; efi_memory_desc_t *in, *prev = NULL, *out = runtime_map; int l;