From nobody Tue Feb 10 16:18:33 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D6EBEC761AF for ; Wed, 29 Mar 2023 05:33:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229803AbjC2FdP (ORCPT ); Wed, 29 Mar 2023 01:33:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229884AbjC2Fc5 (ORCPT ); Wed, 29 Mar 2023 01:32:57 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 515C1358E for ; Tue, 28 Mar 2023 22:32:37 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id s13so8133457wmr.4 for ; Tue, 28 Mar 2023 22:32:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1680067954; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=o/RerhG4T/yFf2BoY2e3Gr2ZtC7zbwdaRkEeOQ/2QmI=; b=hZW97tAfR9MepbrUzxjza8gsmYfWRPxGTIOoFYDjD+S1T8fwx72pbxz1UA1FX/IEkJ 4SRrCtDTIFUdrq7mlb9fKf5EOnr96bTWKT8bywcwUvQifb4v+p8Ue8RnersW52+PBS+o qkcUG1yBco/P45By3Mzf35wT8zqXB6Wy3574wZ0bty0uesnTGDa/wR1vZmjNCf5WBzSz b8XPy/IhmNb26OIhwjOKY1YF0wM6bh8wWgGQ1Fi7LWREA2R8Pl074hVgTs7ajBlQU5+A 1YVDYxWYesJMo5rbtLiVtykD2GWbrsjERnBdSwcqE8boiqT5yNbZKZFrtU+FQOTYaF4x 4Avw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680067954; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=o/RerhG4T/yFf2BoY2e3Gr2ZtC7zbwdaRkEeOQ/2QmI=; b=u4tXe1+0QhDlHTLeeRrouHzOHs23HB4brkuWmJzXh1chxnRbBoR+9pPqFWNLhINAYv 7A7MCSpjqty18DEy3uObuU3h+AQgSZQq6YIS9+qbm98tNdvLmpSKXC4xe7U3S6Sct6FV pxOfT3XCvXkyZdpQTVG3oEzcd/H1KKATx6Wt1r79PomgwDLHvtzRhRNFX3NIeVb+lHBj UR8NhEVkg/CVwqrqF8ZH+uOuh60ruTyM0axGz9dm/lletI2U5qrab21+bvIXsswxb+ip 9gzab4wU0sYX1ksEuhZ7R84JjStuAImtw5Yadv/Bk4+Sv+Mf6UJj881PgI3ZJkswKPtP ceJQ== X-Gm-Message-State: AO0yUKVcjg2R6jwZV5FvdwcRoJ2segRJpSD+4tZohUYkvUJod/PViQpf 0LvijmtdfhRCJ9EHfDXJjZSfDb+78vVb1nPrUZw= X-Google-Smtp-Source: AK7set8Z9evQXF1R8BE4vRjNMI06RxjawH9XbXADarypRfaTnz+UPMsRsqXQL3zUUU2PDeNv3uYbVA== X-Received: by 2002:a05:600c:22d5:b0:3ee:3d2d:841a with SMTP id 21-20020a05600c22d500b003ee3d2d841amr15109287wmg.24.1680067954608; Tue, 28 Mar 2023 22:32:34 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id h16-20020a05600c315000b003eda46d6792sm880724wmo.32.2023.03.28.22.32.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 22:32:34 -0700 (PDT) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Ard Biesheuvel , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Alexandre Ghiti Subject: [PATCH v2 3/4] arm64: libstub: Move KASLR handling functions to efi-stub-helper.c Date: Wed, 29 Mar 2023 07:29:25 +0200 Message-Id: <20230329052926.69632-4-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230329052926.69632-1-alexghiti@rivosinc.com> References: <20230329052926.69632-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This prepares for riscv to use the same functions to handle the p=C4=A5ysic= al kernel move when KASLR is enabled. Signed-off-by: Alexandre Ghiti --- drivers/firmware/efi/libstub/arm64-stub.c | 111 +------------ .../firmware/efi/libstub/efi-stub-helper.c | 151 ++++++++++++++++++ drivers/firmware/efi/libstub/efistub.h | 8 + 3 files changed, 167 insertions(+), 103 deletions(-) diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/e= fi/libstub/arm64-stub.c index d4a6b12a8741..b9e0d2cf09aa 100644 --- a/drivers/firmware/efi/libstub/arm64-stub.c +++ b/drivers/firmware/efi/libstub/arm64-stub.c @@ -14,42 +14,6 @@ =20 #include "efistub.h" =20 -/* - * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., f= ail - * to provide space, and fail to zero it). Check for this condition by dou= ble - * checking that the first and the last byte of the image are covered by t= he - * same EFI memory map entry. - */ -static bool check_image_region(u64 base, u64 size) -{ - struct efi_boot_memmap *map; - efi_status_t status; - bool ret =3D false; - int map_offset; - - status =3D efi_get_memory_map(&map, false); - if (status !=3D EFI_SUCCESS) - return false; - - for (map_offset =3D 0; map_offset < map->map_size; map_offset +=3D map->d= esc_size) { - efi_memory_desc_t *md =3D (void *)map->map + map_offset; - u64 end =3D md->phys_addr + md->num_pages * EFI_PAGE_SIZE; - - /* - * Find the region that covers base, and return whether - * it covers base+size bytes. - */ - if (base >=3D md->phys_addr && base < end) { - ret =3D (base + size) <=3D end; - break; - } - } - - efi_bs_call(free_pool, map); - - return ret; -} - efi_status_t handle_kernel_image(unsigned long *image_addr, unsigned long *image_size, unsigned long *reserve_addr, @@ -59,31 +23,6 @@ efi_status_t handle_kernel_image(unsigned long *image_ad= dr, { efi_status_t status; unsigned long kernel_size, kernel_codesize, kernel_memsize; - u32 phys_seed =3D 0; - u64 min_kimg_align =3D efi_get_kimg_min_align(); - - if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { - efi_guid_t li_fixed_proto =3D LINUX_EFI_LOADED_IMAGE_FIXED_GUID; - void *p; - - if (efi_nokaslr) { - efi_info("KASLR disabled on kernel command line\n"); - } else if (efi_bs_call(handle_protocol, image_handle, - &li_fixed_proto, &p) =3D=3D EFI_SUCCESS) { - efi_info("Image placement fixed by loader\n"); - } else { - status =3D efi_get_random_bytes(sizeof(phys_seed), - (u8 *)&phys_seed); - if (status =3D=3D EFI_NOT_FOUND) { - efi_info("EFI_RNG_PROTOCOL unavailable\n"); - efi_nokaslr =3D true; - } else if (status !=3D EFI_SUCCESS) { - efi_err("efi_get_random_bytes() failed (0x%lx)\n", - status); - efi_nokaslr =3D true; - } - } - } =20 if (image->image_base !=3D _text) efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n"= ); @@ -96,49 +35,15 @@ efi_status_t handle_kernel_image(unsigned long *image_a= ddr, kernel_codesize =3D __inittext_end - _text; kernel_memsize =3D kernel_size + (_end - _edata); *reserve_size =3D kernel_memsize; + *image_addr =3D (unsigned long)_text; =20 - if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed !=3D 0) { - /* - * If KASLR is enabled, and we have some randomness available, - * locate the kernel at a randomized offset in physical memory. - */ - status =3D efi_random_alloc(*reserve_size, min_kimg_align, - reserve_addr, phys_seed, - EFI_LOADER_CODE); - if (status !=3D EFI_SUCCESS) - efi_warn("efi_random_alloc() failed: 0x%lx\n", status); - } else { - status =3D EFI_OUT_OF_RESOURCES; - } - - if (status !=3D EFI_SUCCESS) { - if (!check_image_region((u64)_text, kernel_memsize)) { - efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n"= ); - } else if (IS_ALIGNED((u64)_text, min_kimg_align) && - (u64)_end < EFI_ALLOC_LIMIT) { - /* - * Just execute from wherever we were loaded by the - * UEFI PE/COFF loader if the placement is suitable. - */ - *image_addr =3D (u64)_text; - *reserve_size =3D 0; - return EFI_SUCCESS; - } - - status =3D efi_allocate_pages_aligned(*reserve_size, reserve_addr, - ULONG_MAX, min_kimg_align, - EFI_LOADER_CODE); - - if (status !=3D EFI_SUCCESS) { - efi_err("Failed to relocate kernel\n"); - *reserve_size =3D 0; - return status; - } - } - - *image_addr =3D *reserve_addr; - memcpy((void *)*image_addr, _text, kernel_size); - caches_clean_inval_pou(*image_addr, *image_addr + kernel_codesize); + status =3D efi_kaslr_relocate_kernel(image_addr, + reserve_addr, reserve_size, + kernel_size, kernel_codesize, + kernel_memsize, + efi_kaslr_get_phys_seed(image_handle)); + if (status !=3D EFI_SUCCESS) + return status; =20 return EFI_SUCCESS; } diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmw= are/efi/libstub/efi-stub-helper.c index 1e0203d74691..40ac2625949c 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -718,3 +718,154 @@ void efi_remap_image(unsigned long image_base, unsign= ed alloc_size, efi_warn("Failed to remap data region non-executable\n"); } } + +/** + * efi_kaslr_get_phys_seed() - Get random seed for physical kernel KASLR + * @image_handle: Handle to the image + * + * If KASLR is not disabled, obtain a random seed using EFI_RNG_PROTOCOL + * that will be used to move the kernel physical mapping. + * + * Return: the random seed + */ +u32 efi_kaslr_get_phys_seed(efi_handle_t image_handle) +{ + efi_status_t status; + u32 phys_seed; + efi_guid_t li_fixed_proto =3D LINUX_EFI_LOADED_IMAGE_FIXED_GUID; + void *p; + + if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE)) + return 0; + + if (efi_nokaslr) { + efi_info("KASLR disabled on kernel command line\n"); + } else if (efi_bs_call(handle_protocol, image_handle, + &li_fixed_proto, &p) =3D=3D EFI_SUCCESS) { + efi_info("Image placement fixed by loader\n"); + } else { + status =3D efi_get_random_bytes(sizeof(phys_seed), + (u8 *)&phys_seed); + if (status =3D=3D EFI_SUCCESS) { + return phys_seed; + } else if (status =3D=3D EFI_NOT_FOUND) { + efi_info("EFI_RNG_PROTOCOL unavailable\n"); + efi_nokaslr =3D true; + } else if (status !=3D EFI_SUCCESS) { + efi_err("efi_get_random_bytes() failed (0x%lx)\n", + status); + efi_nokaslr =3D true; + } + } + + return 0; +} + +/* + * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., f= ail + * to provide space, and fail to zero it). Check for this condition by dou= ble + * checking that the first and the last byte of the image are covered by t= he + * same EFI memory map entry. + */ +static bool check_image_region(u64 base, u64 size) +{ + struct efi_boot_memmap *map; + efi_status_t status; + bool ret =3D false; + int map_offset; + + status =3D efi_get_memory_map(&map, false); + if (status !=3D EFI_SUCCESS) + return false; + + for (map_offset =3D 0; map_offset < map->map_size; map_offset +=3D map->d= esc_size) { + efi_memory_desc_t *md =3D (void *)map->map + map_offset; + u64 end =3D md->phys_addr + md->num_pages * EFI_PAGE_SIZE; + + /* + * Find the region that covers base, and return whether + * it covers base+size bytes. + */ + if (base >=3D md->phys_addr && base < end) { + ret =3D (base + size) <=3D end; + break; + } + } + + efi_bs_call(free_pool, map); + + return ret; +} + +/** + * efi_kaslr_relocate_kernel() - Relocate the kernel (random if KASLR enab= led) + * @image_addr: Pointer to the current kernel location + * @reserve_addr: Pointer to the relocated kernel location + * @reserve_size: Size of the relocated kernel + * @kernel_size: Size of the text + data + * @kernel_codesize: Size of the text + * @kernel_memsize: Size of the text + data + bss + * @phys_seed: Random seed used for the relocation + * + * If KASLR is not enabled, this function relocates the kernel to a fixed + * address (or leave it as its current location). If KASLR is enabled, the + * kernel physical location is randomized using the seed in parameter. + * + * Return: status code, EFI_SUCCESS if relocation is successful + */ +efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr, + unsigned long *reserve_addr, + unsigned long *reserve_size, + unsigned long kernel_size, + unsigned long kernel_codesize, + unsigned long kernel_memsize, + u32 phys_seed) +{ + efi_status_t status; + u64 min_kimg_align =3D efi_get_kimg_min_align(); + + if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed !=3D 0) { + /* + * If KASLR is enabled, and we have some randomness available, + * locate the kernel at a randomized offset in physical memory. + */ + status =3D efi_random_alloc(*reserve_size, min_kimg_align, + reserve_addr, phys_seed, + EFI_LOADER_CODE); + if (status !=3D EFI_SUCCESS) + efi_warn("efi_random_alloc() failed: 0x%lx\n", status); + } else { + status =3D EFI_OUT_OF_RESOURCES; + } + + if (status !=3D EFI_SUCCESS) { + if (!check_image_region(*image_addr, kernel_memsize)) { + efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n"= ); + } else if (IS_ALIGNED(*image_addr, min_kimg_align) && + (u64)_end < EFI_ALLOC_LIMIT) { + /* + * Just execute from wherever we were loaded by the + * UEFI PE/COFF loader if the placement is suitable. + */ + *reserve_size =3D 0; + return EFI_SUCCESS; + } + + status =3D efi_allocate_pages_aligned(*reserve_size, reserve_addr, + ULONG_MAX, min_kimg_align, + EFI_LOADER_CODE); + + if (status !=3D EFI_SUCCESS) { + efi_err("Failed to relocate kernel\n"); + *reserve_size =3D 0; + return status; + } + } + + memcpy((void *)*reserve_addr, (void *)*image_addr, kernel_size); + *image_addr =3D *reserve_addr; + + caches_clean_inval_pou(*image_addr, *image_addr + kernel_codesize); + + return status; +} diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/= libstub/efistub.h index 6bd3bb86d967..64273371ce96 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -1098,5 +1098,13 @@ const u8 *__efi_get_smbios_string(u8 type, int offse= t, int recsize); =20 void efi_remap_image(unsigned long image_base, unsigned alloc_size, unsigned long code_size); +efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr, + unsigned long *reserve_addr, + unsigned long *reserve_size, + unsigned long kernel_size, + unsigned long kernel_codesize, + unsigned long kernel_memsize, + u32 phys_seed); +u32 efi_kaslr_get_phys_seed(efi_handle_t image_handle); =20 #endif --=20 2.37.2