From patchwork Mon Aug 14 12:54:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 9898863 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0D07F602BA for ; Mon, 14 Aug 2017 12:59:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0176A206AC for ; Mon, 14 Aug 2017 12:59:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EA61528606; Mon, 14 Aug 2017 12:59:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id CF456206AC for ; Mon, 14 Aug 2017 12:59:43 +0000 (UTC) Received: (qmail 29993 invoked by uid 550); 14 Aug 2017 12:56:11 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 28388 invoked from network); 14 Aug 2017 12:56:04 -0000 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=dwhqOOBCU30p+mWG6uyW1B91+GelmcgcGHL0ZAy4nhw=; b=GWCHlaa0Bzk68/gqJ5Vg6VUirCDNpcmgLz/UtSOZbNQ2seaRAYCYqH3lHO8obDtxh+ QI9xeer7aES6sKtCBSVQ+fJvrQZOU3XeJp+Sy6PWSX2v0VLlbekxJfy3G+f5TwMufSbl wPfOnRoWYfTcuz8U4mh5NN6b8VyQRH0aZgulI= 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=dwhqOOBCU30p+mWG6uyW1B91+GelmcgcGHL0ZAy4nhw=; b=AVEl9o6oNHz3v67ud9Nm2eS0uaLi4upGMIfe4HLr8SlKI6Kr5ZTc1Gool/nEw6Ge94 dAwsIZ6me3PWYuRXDa+qVJnJY5IJf1fMXeAX/MPXxx6deoUNf6U7KqaU2dTlHfW2Ke4U AtooOIWUuHvWQngRqdiLQUXefzeVjmHJtxir5nptuPnzMmtH0hY6hpG62tmbml0uNeUi j5/yEf34Z3LPCpl75j2BEBMMUVr4SGvSYkG1ggOrv+utX1DKRKsvlkYc4QiY1LLrxQqp hsarm1Ccj4FyBCt/TxmEq9IK4SKFyZfo5+IR7moy+SzTF6PdxQ/WhHMqzbwoZm8ZOFgb 6SSg== X-Gm-Message-State: AHYfb5hCIeaxaMQBJM61cLwafa7FTvblTvWMrG+40A9Jo6+NBc831RFi 7slDwsl1n3Pi6wtmZvfQOA== X-Received: by 10.223.195.120 with SMTP id e53mr3022544wrg.115.1502715352539; Mon, 14 Aug 2017 05:55:52 -0700 (PDT) From: Ard Biesheuvel To: kernel-hardening@lists.openwall.com Cc: linux-arm-kernel@lists.infradead.org, Ard Biesheuvel , Arnd Bergmann , Nicolas Pitre , Russell King , Kees Cook , Thomas Garnier , Marc Zyngier , Mark Rutland , Tony Lindgren , Matt Fleming , Dave Martin Date: Mon, 14 Aug 2017 13:54:08 +0100 Message-Id: <20170814125411.22604-28-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170814125411.22604-1-ard.biesheuvel@linaro.org> References: <20170814125411.22604-1-ard.biesheuvel@linaro.org> Subject: [kernel-hardening] [PATCH 27/30] efi/libstub: add 'max' parameter to efi_random_alloc() X-Virus-Scanned: ClamAV using ClamSMTP Add an upper limit to efi_random_alloc() so we can use it to randomly allocate the ARM kernel in lowmem. Cc: Matt Fleming Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/arm64-stub.c | 2 +- drivers/firmware/efi/libstub/efistub.h | 3 ++- drivers/firmware/efi/libstub/random.c | 10 ++++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c index b4c2589d7c91..940766f90adb 100644 --- a/drivers/firmware/efi/libstub/arm64-stub.c +++ b/drivers/firmware/efi/libstub/arm64-stub.c @@ -94,7 +94,7 @@ efi_status_t handle_kernel_image(efi_system_table_t *sys_table_arg, *reserve_size = kernel_memsize + offset; status = efi_random_alloc(sys_table_arg, *reserve_size, MIN_KIMG_ALIGN, reserve_addr, - (u32)phys_seed); + (u32)phys_seed, ULONG_MAX); *image_addr = *reserve_addr + offset; } else { diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h index 83f268c05007..3a670a5f759f 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -60,7 +60,8 @@ efi_status_t efi_get_random_bytes(efi_system_table_t *sys_table, 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 *addr, unsigned long random_seed, + unsigned long max); efi_status_t check_platform_features(efi_system_table_t *sys_table_arg); diff --git a/drivers/firmware/efi/libstub/random.c b/drivers/firmware/efi/libstub/random.c index 7e72954d5860..85b80a4a85b3 100644 --- a/drivers/firmware/efi/libstub/random.c +++ b/drivers/firmware/efi/libstub/random.c @@ -42,7 +42,8 @@ efi_status_t efi_get_random_bytes(efi_system_table_t *sys_table_arg, */ static unsigned long get_entry_num_slots(efi_memory_desc_t *md, unsigned long size, - unsigned long align_shift) + unsigned long align_shift, + unsigned long max) { unsigned long align = 1UL << align_shift; u64 first_slot, last_slot, region_end; @@ -50,7 +51,7 @@ static unsigned long get_entry_num_slots(efi_memory_desc_t *md, if (md->type != EFI_CONVENTIONAL_MEMORY) return 0; - region_end = min((u64)ULONG_MAX, md->phys_addr + md->num_pages*EFI_PAGE_SIZE - 1); + region_end = min((u64)max, md->phys_addr + md->num_pages*EFI_PAGE_SIZE - 1); first_slot = round_up(md->phys_addr, align); last_slot = round_down(region_end - size + 1, align); @@ -73,7 +74,8 @@ 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 random_seed, + unsigned long max) { unsigned long map_size, desc_size, total_slots = 0, target_slot; unsigned long buff_size; @@ -101,7 +103,7 @@ efi_status_t efi_random_alloc(efi_system_table_t *sys_table_arg, efi_memory_desc_t *md = (void *)memory_map + map_offset; unsigned long slots; - slots = get_entry_num_slots(md, size, ilog2(align)); + slots = get_entry_num_slots(md, size, ilog2(align), max); MD_NUM_SLOTS(md) = slots; total_slots += slots; }