From patchwork Sun Sep 3 12:07:55 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 9936179 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 A410A6037D for ; Sun, 3 Sep 2017 12:13:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 95353286A9 for ; Sun, 3 Sep 2017 12:13:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 89370286B3; Sun, 3 Sep 2017 12:13:45 +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 880FE286A9 for ; Sun, 3 Sep 2017 12:13:44 +0000 (UTC) Received: (qmail 20201 invoked by uid 550); 3 Sep 2017 12:09:54 -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 20028 invoked from network); 3 Sep 2017 12:09:52 -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=BAR5k+gPdTFvm6mZiwnkZkYXohHduAYubblckAmCbZU=; b=fn45U9ChqA2sKEu5mUamDO3N+beOtjoWfWWT6Cw65UTANLtIp24YZ1+Xn6PjST1BFY Sc4RmVMslOaH8STU6tpHczpyH6TI38lqifQTrAOY3KkS4hSytdKs4KUYv+y7zXMY+N3Y rslDaPRE/4bp+MtukxiIas4HEYrsY4xstDVJw= 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=BAR5k+gPdTFvm6mZiwnkZkYXohHduAYubblckAmCbZU=; b=fY6hEdtKEACnVpybXpA9FCmT4K/oDwHPn9KCFJ4B73+uYIhEIbBzHOqHzROziB4F0k +ojLqdssbXw2+HC3OtR62zEFwxtRu5KtQWvzNWJD8nUadShqySwwf8dUQAZw2cFTJdG4 cAWZwQ0ntMAW8Ijyo6VepG3yRmMHDqxhBrLtr55qAkegXdzFWUG+TtkiJs+tfFVr+HHl lMdecYYRmfh20eMMnosllz5ZzAhuAk3ctRVc35Afe/y31jlMrFgL6R5H8CumCjpnSxLg uwvve/VkVkZEVDnstBUZr6CBN4LIsw/x/Y2vbOrJFqTj3bgtX//ocd0Qt27Bhzoi/BNW qPHA== X-Gm-Message-State: AHPjjUgaKBUE3s/WsxEauPRsZOZGmEVqUCV0/7mLVF9fcxiPU5OCjgmZ UUppKqeAzj+oCfKF X-Google-Smtp-Source: ADKCNb58Xsts19mHJM8jj6KIUZTBRdmTmkUbYsnomBvobpGxTIy0LjSkx2NRDhq7xU4r7EeLXBFHhg== X-Received: by 10.28.133.75 with SMTP id h72mr2532915wmd.113.1504440581107; Sun, 03 Sep 2017 05:09:41 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, kernel-hardening@lists.openwall.com Cc: Ard Biesheuvel , Arnd Bergmann , Nicolas Pitre , Russell King , Kees Cook , Thomas Garnier , Marc Zyngier , Mark Rutland , Tony Lindgren , Matt Fleming , Dave Martin Date: Sun, 3 Sep 2017 13:07:55 +0100 Message-Id: <20170903120757.14968-28-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170903120757.14968-1-ard.biesheuvel@linaro.org> References: <20170903120757.14968-1-ard.biesheuvel@linaro.org> Subject: [kernel-hardening] [PATCH v2 27/29] 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 | 11 +++++++---- 3 files changed, 10 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..810e60e76211 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,8 @@ 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_t(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 +75,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 +104,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; }