From patchwork Sat Jan 16 15:28:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 364673 Delivered-To: patch@linaro.org Received: by 2002:a02:ccad:0:0:0:0:0 with SMTP id t13csp824368jap; Sat, 16 Jan 2021 07:28:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJz+UTfoFUxvfkY/MDfKShepG15DomcbctB58tmIihuk/Y21lkc36WFA1ZQ3vX0ha5V21lr0 X-Received: by 2002:a17:906:ce49:: with SMTP id se9mr558509ejb.341.1610810896438; Sat, 16 Jan 2021 07:28:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610810896; cv=none; d=google.com; s=arc-20160816; b=Ilzmv6uKwEMXlZSWfqz+p1ZuiSI7PCv6MBPEuNPlKJ2PbmLVodKjUxSI4AHTdF836l A+Jlk8KDTOmVvxFWc+YFi85ue9piS6WxWg3R3A57u/vXveMFS0jXTscIvNEemHTDPcio jh6mGkQ1erxqRTUP1SiKhX41X+BJPmfJ1HTJ3x/flAsuWGZUgITWyhvpE8qAhsXnlO6+ YqgTcoVD2mjka1SFaC3d1qMK+YvxKvdIv5qA+OWXTJw4CgYx4+Nshyt6takU8qfW789k mRTGRQ1F16q6soV4OGqzp/44r0FoI3ysm+R+LqrQEjIqqhlpTP9DiY9TdJSUcQK0hX8F mD4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=Gza34zWuEI4EvEW+tsA1iUGAVRMJ9OrNRJzjYPD6sew=; b=GqSr+yx2aChK6QAsF/Hjl3MZlVE4KCIv1vkZY5YACpQvSJ5zouM/l3xPaCFz1RC/YM ayaq4wtL7FTtkbi8nUGxCu3ZvqpNIAq4FdiVM5JMKjEG2NSdJBkvO3jRLgEtGuNJHkwL zFUleVjZPoLt83GKsHupAI6vT1RNKZH9i4BQBsz1N+jVsS+EQjiOTXhDe8fAkm35JVKt rN4LzbDHp3FkNs3De+HAth9ItIUg1HgBkDR6jFFfkpDNMoERetqqfx9JmCvOe6S8vjnk aev/eug47pgkf/50bWEyZNLfcacX9TjegzVhBYknCCuwsXjqVYzBYuZFok1N08fT3+W/ baOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fVVIOmUg; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id qx4si4415679ejb.547.2021.01.16.07.28.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Jan 2021 07:28:16 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fVVIOmUg; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 453D9827F3; Sat, 16 Jan 2021 16:28:13 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="fVVIOmUg"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D1BD9827F8; Sat, 16 Jan 2021 16:28:11 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id BE688827E3 for ; Sat, 16 Jan 2021 16:28:08 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ilias.apalodimas@linaro.org Received: by mail-wm1-x32d.google.com with SMTP id s24so2624108wmj.0 for ; Sat, 16 Jan 2021 07:28:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Gza34zWuEI4EvEW+tsA1iUGAVRMJ9OrNRJzjYPD6sew=; b=fVVIOmUgLr5ARfAtWr+mquSS2b2kUmKBwdVaVyqV2PoLO2l7UeOgxmHS179E7jFTX6 kgwRJXsPS6q/lLKpVTAcNYpPdhE4qeByuWEkn3+t5qgd6vq0X/lh7/wB18hAioaEW+Td yWDROUxB7hsTHrWjCtEUD7PDyIct2Kal4roggPxEGGPQrKuLFevbDIN78T2Ickq4b1gk YJiFvMF+ez/JYGWh7h0STFenRa/oaX/OO/da2IfaJ3m5goAPrIpKhRXdcVOu4N58NZYd TsScXXFmeEg1L0WLdrvooCxz6nqcZL768HZX/J1/MpWWoeIN/vbARGjf8WByUAj3rgJb zDMg== 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:mime-version :content-transfer-encoding; bh=Gza34zWuEI4EvEW+tsA1iUGAVRMJ9OrNRJzjYPD6sew=; b=gP4e4GDEySPpAu3GsN7+j9xsVEbEPOeBpOA162ljzurzP9IvAGfW9ZwCuyiS0OORS9 ZEXDSHKkTjGgMFGYfdnCwS2rHKP87Fk8hOZO3gPJ7wZ4+j1WH4klWGlUXAQbvjm+INgq rneodMvpcRheFnyO2S6B05d7kJefS4hlTSfHb+FnMUA+xu/iKQ2yS/ijzzlwxz5wHsT8 OOpi8xVAXEoxzYJjUjmJhBGxb3tKM3UvhY2JltNnKa2gQq6ne9/5exdOjKvuS8dNt9S4 5URLfYAkq3nUhqKdMiNFRHMwYQ6j6fNAxpS8GGeIdRGYs8oMGVQc+ivFMsMVNRogw/gO Nh2A== X-Gm-Message-State: AOAM531vDj+2O//kBL7c/JJOVTTwHEh5z0giK2AUECpFbCcFW8wlwu8P EYPMD43aegSBUZ9hVO3VGT6eMw== X-Received: by 2002:a05:600c:4417:: with SMTP id u23mr13738775wmn.100.1610810888247; Sat, 16 Jan 2021 07:28:08 -0800 (PST) Received: from apalos.home ([2a02:587:4667:3e9:2e56:dcff:fe9a:8f06]) by smtp.gmail.com with ESMTPSA id b133sm17496791wme.33.2021.01.16.07.28.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Jan 2021 07:28:07 -0800 (PST) From: Ilias Apalodimas To: xypron.glpk@gmx.de Cc: Ilias Apalodimas , Atish Patra , Atish Patra , Alexander Graf , u-boot@lists.denx.de Subject: [PATCH v2] efi_loader: Avoid emitting efi_var_buf to .GOT Date: Sat, 16 Jan 2021 17:28:04 +0200 Message-Id: <20210116152805.296254-1-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.30.0 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean Atish reports that on RISC-V, accessing the EFI variables causes a kernel panic. An objdump of the file verifies that, since the global pointer for efi_var_buf ends up in .GOT section which is not mapped in virtual address space for Linux. 0000000000000084 : 84: 715d addi sp,sp,-80 * objdump -dr 0000000000000086 <.LCFI2>: 86: e0a2 sd s0,64(sp) 88: fc26 sd s1,56(sp) 8a: e486 sd ra,72(sp) 8c: f84a sd s2,48(sp) 8e: f44e sd s3,40(sp) 90: f052 sd s4,32(sp) 92: ec56 sd s5,24(sp) 94: 00000497 auipc s1,0x0 94: R_RISCV_GOT_HI20 efi_var_buf 98: 0004b483 ld s1,0(s1) # 94 <.LCFI2+0xe> 98: R_RISCV_PCREL_LO12_I .L0 98: R_RISCV_RELAX *ABS* * objdump -t 0000000000000084 g F .text.efi_runtime 00000000000000b8 efi_var_mem_find With the patch applied: * objdump -dr 0000000000000086 <.LCFI2>: 86: e0a2 sd s0,64(sp) 88: fc26 sd s1,56(sp) 8a: e486 sd ra,72(sp) 8c: f84a sd s2,48(sp) 8e: f44e sd s3,40(sp) 90: f052 sd s4,32(sp) 92: ec56 sd s5,24(sp) 94: 00000497 auipc s1,0x0 94: R_RISCV_PCREL_HI20 .LANCHOR0 94: R_RISCV_RELAX *ABS* 98: 00048493 mv s1,s1 98: R_RISCV_PCREL_LO12_I .L0 98: R_RISCV_RELAX *ABS* * objdump -t 0000000000000008 l O .data.efi_runtime 0000000000000008 efi_var_buf On arm64 this works, because there's no .GOT entries for this and everything is converted to relative references. * objdump -dr (identical pre-post patch, only the new function shows up) 00000000000000b4 : b4: aa0003ee mov x14, x0 b8: 9000000a adrp x10, 0 b8: R_AARCH64_ADR_PREL_PG_HI21 .data.efi_runtime bc: 91000140 add x0, x10, #0x0 bc: R_AARCH64_ADD_ABS_LO12_NC .data.efi_runtime c0: aa0103ed mov x13, x1 c4: 79400021 ldrh w1, [x1] c8: aa0203eb mov x11, x2 cc: f9400400 ldr x0, [x0, #8] d0: b940100c ldr w12, [x0, #16] d4: 8b0c000c add x12, x0, x12 So let's switch efi_var_buf to static and create a helper function for anyone that needs to update it. Fixes: e01aed47d6a0 ("efi_loader: Enable run-time variable support for tee based variables") Reported-by: Atish Patra Tested-by: Atish Patra Reviewed-by: Heinrich Schuchardt Signed-off-by: Ilias Apalodimas --- Changes since v1: - Changed some comments on function and variable declarations - Added Reviewed-by and Tested-by tags include/efi_variable.h | 11 +++++++++++ lib/efi_loader/efi_var_mem.c | 13 ++++++++++++- lib/efi_loader/efi_variable_tee.c | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) -- 2.30.0 diff --git a/include/efi_variable.h b/include/efi_variable.h index 4704a3c16e65..89eef1ee5b20 100644 --- a/include/efi_variable.h +++ b/include/efi_variable.h @@ -306,4 +306,15 @@ efi_status_t __efi_runtime EFIAPI efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size, u16 *variable_name, efi_guid_t *guid); +/** + * efi_var_buf_update() - udpate memory buffer for variables + * + * @var_buf: source buffer + * + * This function copies to the memory buffer for UEFI variables. Call this + * function in ExitBootServices() if memory backed variables are only used + * at runtime to fill the buffer + */ +void efi_var_buf_update(struct efi_var_file *var_buf); + #endif diff --git a/lib/efi_loader/efi_var_mem.c b/lib/efi_loader/efi_var_mem.c index d155f25f60e6..bb94fd0da60d 100644 --- a/lib/efi_loader/efi_var_mem.c +++ b/lib/efi_loader/efi_var_mem.c @@ -10,7 +10,13 @@ #include #include -struct efi_var_file __efi_runtime_data *efi_var_buf; +/* + * The variables efi_var_file and efi_var_entry must be static to avoid + * referencing them via the global offset table (section .got). The GOT + * is neither mapped as EfiRuntimeServicesData nor do we support its + * relocation during SetVirtualAddressMap() + */ +static struct efi_var_file __efi_runtime_data *efi_var_buf; static struct efi_var_entry __efi_runtime_data *efi_current_var; /** @@ -339,3 +345,8 @@ efi_get_next_variable_name_mem(efi_uintn_t *variable_name_size, return EFI_SUCCESS; } + +void efi_var_buf_update(struct efi_var_file *var_buf) +{ + memcpy(efi_var_buf, var_buf, EFI_VAR_BUF_SIZE); +} diff --git a/lib/efi_loader/efi_variable_tee.c b/lib/efi_loader/efi_variable_tee.c index be6f3dfad469..c69330443801 100644 --- a/lib/efi_loader/efi_variable_tee.c +++ b/lib/efi_loader/efi_variable_tee.c @@ -692,7 +692,7 @@ void efi_variables_boot_exit_notify(void) if (ret != EFI_SUCCESS) log_err("Can't populate EFI variables. No runtime variables will be available\n"); else - memcpy(efi_var_buf, var_buf, len); + efi_var_buf_update(var_buf); free(var_buf); /* Update runtime service table */