Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4596927rwb; Tue, 8 Aug 2023 10:42:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE2X9p+d1Ogbkw/uRtvKeiFpaCbq1GlNVEuYT4tKKKUP/2pnkSHbOgPIeiZcbmKwwoXrWpb X-Received: by 2002:a05:6a20:7349:b0:13e:99c0:fe7f with SMTP id v9-20020a056a20734900b0013e99c0fe7fmr221648pzc.11.1691516527188; Tue, 08 Aug 2023 10:42:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691516527; cv=none; d=google.com; s=arc-20160816; b=k0hpJ3r3CxUcBgM0aMMLOMzXOXL1i4vvBqTmus8VZ9xWqw48SftLZVZ3LwTNjTP1e7 UgI52ShGBp7PHp+Yp2jp4Fy2SxDI5YtN8Xg18CqZqbqEWHyZah7eOSmFhYIWRie1bkHS 2Ym/VIsABWdvuklvOb3HKUN3ATGNAP6svZH89lRBBzbyCPZLMU39Co8zRxD5X8ljAiFE FYi3k21g3ANkizm8qKsZ0uVWUcXhT9rtOU3UbbarJa6+gZRgj5UAchq9KkFtYAjn6FJ9 pS6iSGw/c7TfOL+8dPdtdp7IVYugsj2lzOqv14EH7gTyoda9vgc0cst/BhhcchdajSoY Tw3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=CCsPqlpWJZk2Y6BdMqZeDawq4IwargCWojdv25BU75E=; fh=upm4t26Ojn/J6N+Q9k8Qqzx52VymNDAlYlARzLeNh5s=; b=Z4rwL6qWpqfICGKboMObJ/2AIjk8egVBrK891zo842d6N62JzFmAapiwOEkKGRaMTt ATqCAt5c86AqU3iw95oUgYaQTdT4DUVEHxTCxa/XzZVJC7TeqQA/Y/bd+yPEcqg9k+GN 2ksZvAxrdTJlmwYTIVQt+egXJ703HfrVNJf1OdFwVGh0O7xjgS817rAz3+ixcr0Wze7M S4h9B5iNxuNCjxUjBJLHxKoGwBdSXL90zw33y8rni5JRzvPURhcG2AWCF2x2FCXhn6K8 UOW27bOcLVUpN+e70/3jxfYbsppHp4TDne7AEcIWzdSH6gpjUQMrtcR6lzUpHiD5ul9F ZLlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=txQcgdi7; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s2-20020a056a0008c200b0068288aaf240si7676613pfu.212.2023.08.08.10.41.55; Tue, 08 Aug 2023 10:42:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=txQcgdi7; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232548AbjHHQqo (ORCPT + 99 others); Tue, 8 Aug 2023 12:46:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231224AbjHHQqA (ORCPT ); Tue, 8 Aug 2023 12:46:00 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5AA8E44475; Tue, 8 Aug 2023 08:56:29 -0700 (PDT) Date: Tue, 08 Aug 2023 08:43:58 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1691484239; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CCsPqlpWJZk2Y6BdMqZeDawq4IwargCWojdv25BU75E=; b=txQcgdi7KG/kLXNoNuqfB20nXY+ch6PSGQ4xpLFT8zr2NPl1eDpHjFlKIE6xQpP1q+6xTf eG0qOpXPLW+8Tl4/2oPP9yI4vs5vsvrFuc6AxpOHkMs0GWDOrGIT7NZYcqvbed5ZWN1f+z WsVMMTMRCWYvgQuBwqmX+KHaJrKsRrMEe17dcDqGCBZnIvjwXUmH/WiuBDRnBLmIPaA9/Y 32w4hSTtGhwFXaBXWbymA6BA0rKl7uewyd7SJIpUEO+ngV2P5qczh9/UpNplsE+CTo8Tke vecyFnAL3Ak42Guhnr7Nx5hEj5/5QG+lm8LcD+D5hNH/AQzrd+aag3BS1Sy+sg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1691484239; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CCsPqlpWJZk2Y6BdMqZeDawq4IwargCWojdv25BU75E=; b=Omu1CWTba3ACvQeNjuQxTISfnTyoSQF2DwEtA3vFlz8Zh2glGuDyGBvzNnjf0Ve1qaBoi3 1nQ2wP2Am2SMRBCg== From: "tip-bot2 for Ard Biesheuvel" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/boot] efi/libstub: Add limit argument to efi_random_alloc() Cc: Ard Biesheuvel , "Borislav Petkov (AMD)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230807162720.545787-22-ardb@kernel.org> References: <20230807162720.545787-22-ardb@kernel.org> MIME-Version: 1.0 Message-ID: <169148423849.27769.4118568530248068899.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,DATE_IN_PAST_06_12, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/boot branch of tip: Commit-ID: bc5ddceff4c14494d83449ad45c985e6cd353fce Gitweb: https://git.kernel.org/tip/bc5ddceff4c14494d83449ad45c985e6cd353fce Author: Ard Biesheuvel AuthorDate: Mon, 07 Aug 2023 18:27:18 +02:00 Committer: Borislav Petkov (AMD) CommitterDate: Mon, 07 Aug 2023 21:01:46 +02:00 efi/libstub: Add limit argument to efi_random_alloc() x86 will need to limit the kernel memory allocation to the lowest 512 MiB of memory, to match the behavior of the existing bare metal KASLR physical randomization logic. So in preparation for that, add a limit parameter to efi_random_alloc() and wire it up. Signed-off-by: Ard Biesheuvel Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/r/20230807162720.545787-22-ardb@kernel.org --- drivers/firmware/efi/libstub/arm64-stub.c | 2 +- drivers/firmware/efi/libstub/efistub.h | 2 +- drivers/firmware/efi/libstub/randomalloc.c | 10 ++++++---- drivers/firmware/efi/libstub/zboot.c | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c index 770b8ec..8c40fc8 100644 --- a/drivers/firmware/efi/libstub/arm64-stub.c +++ b/drivers/firmware/efi/libstub/arm64-stub.c @@ -106,7 +106,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, */ status = efi_random_alloc(*reserve_size, min_kimg_align, reserve_addr, phys_seed, - EFI_LOADER_CODE); + EFI_LOADER_CODE, EFI_ALLOC_LIMIT); if (status != EFI_SUCCESS) efi_warn("efi_random_alloc() failed: 0x%lx\n", status); } else { diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h index 06b7abc..9823f6f 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -956,7 +956,7 @@ efi_status_t efi_get_random_bytes(unsigned long size, u8 *out); efi_status_t efi_random_alloc(unsigned long size, unsigned long align, unsigned long *addr, unsigned long random_seed, - int memory_type); + int memory_type, unsigned long alloc_limit); efi_status_t efi_random_get_seed(void); diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmware/efi/libstub/randomalloc.c index 32c7a54..674a064 100644 --- a/drivers/firmware/efi/libstub/randomalloc.c +++ b/drivers/firmware/efi/libstub/randomalloc.c @@ -16,7 +16,8 @@ */ static unsigned long get_entry_num_slots(efi_memory_desc_t *md, unsigned long size, - unsigned long align_shift) + unsigned long align_shift, + u64 alloc_limit) { unsigned long align = 1UL << align_shift; u64 first_slot, last_slot, region_end; @@ -29,7 +30,7 @@ static unsigned long get_entry_num_slots(efi_memory_desc_t *md, return 0; region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1, - (u64)EFI_ALLOC_LIMIT); + alloc_limit); if (region_end < size) return 0; @@ -54,7 +55,8 @@ efi_status_t efi_random_alloc(unsigned long size, unsigned long align, unsigned long *addr, unsigned long random_seed, - int memory_type) + int memory_type, + unsigned long alloc_limit) { unsigned long total_slots = 0, target_slot; unsigned long total_mirrored_slots = 0; @@ -76,7 +78,7 @@ efi_status_t efi_random_alloc(unsigned long size, efi_memory_desc_t *md = (void *)map->map + map_offset; unsigned long slots; - slots = get_entry_num_slots(md, size, ilog2(align)); + slots = get_entry_num_slots(md, size, ilog2(align), alloc_limit); MD_NUM_SLOTS(md) = slots; total_slots += slots; if (md->attribute & EFI_MEMORY_MORE_RELIABLE) diff --git a/drivers/firmware/efi/libstub/zboot.c b/drivers/firmware/efi/libstub/zboot.c index e5d7fa1..bdb17ea 100644 --- a/drivers/firmware/efi/libstub/zboot.c +++ b/drivers/firmware/efi/libstub/zboot.c @@ -119,7 +119,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab) } status = efi_random_alloc(alloc_size, min_kimg_align, &image_base, - seed, EFI_LOADER_CODE); + seed, EFI_LOADER_CODE, EFI_ALLOC_LIMIT); if (status != EFI_SUCCESS) { efi_err("Failed to allocate memory\n"); goto free_cmdline;