From patchwork Thu Mar 25 11:02:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 408773 Delivered-To: patch@linaro.org Received: by 2002:a02:8562:0:0:0:0:0 with SMTP id g89csp422199jai; Thu, 25 Mar 2021 04:02:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyT1+BLsl86dzJMSJl91jFTcyCBm8GrlQHNE/2Cf2utNNKhh7ymsG25tbwKTSXWSYCdYoZl X-Received: by 2002:a17:906:95c9:: with SMTP id n9mr8578430ejy.16.1616670171120; Thu, 25 Mar 2021 04:02:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616670171; cv=none; d=google.com; s=arc-20160816; b=c/eRPPAfglzro/6hGmIKFEEtXw+7YrXDN0Vcbp1wLXbADAqmKiMsMPrkMDSxLMtW3a lAd92jGdT2p67BETgdbKNYSKi9NUi7L0or39Z7iL5wBoiMRxseccst7aC5YyhpJ0/6j2 cAdAmYeQu1uzzwyazdNYAc1NtyQnZ+ASAe9gPqigpeEdS296q2ESBn1HfTnt+l/ZI55J YKeyz7fQlsqGta3aaXKHeNfnx29nnBgDHDnxmqamonyK3kjV+a83zSflrS/dhkKxN+iX aAIGo7xWdmU1GLIMvZqifdx7AtqA/+a6AlmiDMdnihY5CL9LCavDRV4OWiIbCd0kgeOv 7w0w== 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=u/HQ2SmhktYUDUXQwfIGQ5tBCt7uaOJLZvB7M4LYfps=; b=uvLsVQdx3uc912hiWMqASe82WQxyjBcv7kdUa6k9FS7yMwqXV7EBckiLMbSu4ZE4+x O6vDZwo7+DNpFm3qxbueyTwtlwNmYsPzZkZQBu7KhbNNfu6VAyKarWucWS2SrfosV0p4 SC9RN9IpSm7w9V7B2bccmb83JSeEWiEP5/7GSs1J+g2E4OMQs63+nsz5Y6tbcyW1JD2Q I9Y3NTxjL23o9wBFgPC6gJW6SxwaumYTOBH7FpyEluMXlij+ehjlDdSDoMEnij82+bV2 3McAWlQkNAS+Qn4Fyxm6ajJZgJlI2IzBwI6tDaMunyAWFwGnzSI/ndW2KY39Dls8jIIJ 3K7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="m/7Uf0ss"; 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 co27si3796082edb.376.2021.03.25.04.02.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 04:02:51 -0700 (PDT) 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="m/7Uf0ss"; 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 4313A82822; Thu, 25 Mar 2021 12:02:50 +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="m/7Uf0ss"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 18C0F82838; Thu, 25 Mar 2021 12:02:48 +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,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) (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 2EB67827BC for ; Thu, 25 Mar 2021 12:02:45 +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-wr1-x430.google.com with SMTP id c8so1802911wrq.11 for ; Thu, 25 Mar 2021 04:02:45 -0700 (PDT) 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=u/HQ2SmhktYUDUXQwfIGQ5tBCt7uaOJLZvB7M4LYfps=; b=m/7Uf0ssn24O9m805M5u1aQokjtWWJ0bpFv1qlnkzRN5OiEVbHPRrjvGqAPT3DgAiJ dqtWTK0TY9wKrPeSCosZCK8j+z8tkj61oYmHQODGpgkkq39fb5lzmw/t+KqHsxTsrtQ3 Mnzrw8WQxvpYU1VZP414Dh1TEBg7AUFKf4jCWMFoCEHXT5SxRsruwDi1UmvGS3MmtzeJ kVPkZMEpCViQeQBJu3OnAoG7dZkHcGR5DO+MfTUY8Di+kZ2tp6vofqn1aNN0kSSp4wgS 32CsCjkt7hQxbLjOHmHZ6mZOGB21u2XYFfY3wSsoEEumI7DehNJqLl1ssVaGJSUQefTF XFTw== 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=u/HQ2SmhktYUDUXQwfIGQ5tBCt7uaOJLZvB7M4LYfps=; b=iBzVegq3xn7WDfayd91w+90YzdeDikEFs4Z00xvsC+3ZUueadm66skpSZgXHEjB59O yqqru4CbAuOKd/V1lC9f+ggbcW8rhSMVbmLUiI3G0MnqF4pULhP/DFhGGx5AGOXf9ENr cCP9PSem1GwbmbSnVG43dpzjBs1/gwugsJbps6ZMkAJxEuHAOyoXYXmmowwu9I/DY6D5 gwGG35V6iqqhyxKMgXj3GVcdEZw8icxbqIB+d7QA2Mgm3D3ebXn8hs63SVaCvc7X2rCz tRnG71XUYlBb4IIfLctGduFjR4WPmE/Ju5kOlyTKDqPdeE27vD9yJuCaRu1p7ibfm6N9 ud9A== X-Gm-Message-State: AOAM533pcw09tuUweGIaOjMrTVLzGcVPtrjsdwkC6eJgaIOEQMeHnQH4 pqJwa99Es+e3dUiSPLHuauEc0A== X-Received: by 2002:a5d:6945:: with SMTP id r5mr8132681wrw.367.1616670164761; Thu, 25 Mar 2021 04:02:44 -0700 (PDT) Received: from localhost.localdomain (ppp-94-64-113-158.home.otenet.gr. [94.64.113.158]) by smtp.gmail.com with ESMTPSA id p27sm6462283wmi.12.2021.03.25.04.02.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 04:02:44 -0700 (PDT) From: Ilias Apalodimas To: xypron.glpk@gmx.de Cc: Ilias Apalodimas , Alexander Graf , u-boot@lists.denx.de Subject: [PATCH v2] efi_loader: EFI TCG2 free efi memory on protocol failure Date: Thu, 25 Mar 2021 13:02:41 +0200 Message-Id: <20210325110242.1252320-1-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.31.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.4 at phobos.denx.de X-Virus-Status: Clean Current code doesn't free the efi allocated memory in case the protocol failed to install Fixes: c8d0fd582576 ("efi_loader: Introduce eventlog support for TCG2_PROTOCOL") Signed-off-by: Ilias Apalodimas --- changes since v1: - remove the installed config table as well - gather all cleanups in a single function and call that instead include/efi_loader.h | 2 ++ lib/efi_loader/efi_boottime.c | 2 +- lib/efi_loader/efi_tcg2.c | 43 ++++++++++++++++++++++++++++------- 3 files changed, 38 insertions(+), 9 deletions(-) -- 2.31.0 diff --git a/include/efi_loader.h b/include/efi_loader.h index 903bf60bc0a3..3a84b3c75597 100644 --- a/include/efi_loader.h +++ b/include/efi_loader.h @@ -604,6 +604,8 @@ efi_status_t efi_driver_init(void); int efi_memory_init(void); /* Adds new or overrides configuration table entry to the system table */ efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table); +/* Removes a configuration table from the list */ +void efi_remove_configuration_table(int i); /* Sets up a loaded image */ efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path, struct efi_device_path *file_path, diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c index 41b8949b0424..46de9d80391d 100644 --- a/lib/efi_loader/efi_boottime.c +++ b/lib/efi_loader/efi_boottime.c @@ -1610,7 +1610,7 @@ static efi_status_t EFIAPI efi_locate_handle_ext( * * @i: index of the table entry to be removed */ -static void efi_remove_configuration_table(int i) +void efi_remove_configuration_table(int i) { struct efi_configuration_table *this = &systab.tables[i]; struct efi_configuration_table *next = &systab.tables[i + 1]; diff --git a/lib/efi_loader/efi_tcg2.c b/lib/efi_loader/efi_tcg2.c index 797d6eb134f6..62f91df0159e 100644 --- a/lib/efi_loader/efi_tcg2.c +++ b/lib/efi_loader/efi_tcg2.c @@ -957,6 +957,26 @@ out: return ret; } +/** + * tcg2_uninit - remove the final event table and free efi memory on failures + */ +void tcg2_uninit(void) +{ + int i; + + for (i = 0; i < systab.nr_tables; i++) { + if (!guidcmp(&efi_guid_final_events, &systab.tables[i].guid)) { + efi_remove_configuration_table(i); + break; + } + } + + efi_free_pool(event_log.buffer); + event_log.buffer = NULL; + efi_free_pool(event_log.final_buffer); + event_log.final_buffer = NULL; +} + /** * create_final_event() - Create the final event and install the config * defined by the TCG EFI spec @@ -983,10 +1003,6 @@ static efi_status_t create_final_event(void) event_log.final_pos = sizeof(*final_event); ret = efi_install_configuration_table(&efi_guid_final_events, final_event); - if (ret != EFI_SUCCESS) - goto out; - - return EFI_SUCCESS; out: return ret; } @@ -1041,8 +1057,12 @@ static efi_status_t efi_init_event_log(void) event_log.last_event_size = event_log.pos; ret = create_final_event(); + if (ret != EFI_SUCCESS) + goto out; + return EFI_SUCCESS; out: + tcg2_uninit(); return ret; } @@ -1055,23 +1075,30 @@ out: */ efi_status_t efi_tcg2_register(void) { - efi_status_t ret; + efi_status_t ret = EFI_SUCCESS; struct udevice *dev; ret = platform_get_tpm2_device(&dev); if (ret != EFI_SUCCESS) { log_warning("Unable to find TPMv2 device\n"); - return EFI_SUCCESS; + ret = EFI_SUCCESS; + goto out; } ret = efi_init_event_log(); if (ret != EFI_SUCCESS) - return ret; + goto fail; ret = efi_add_protocol(efi_root, &efi_guid_tcg2_protocol, (void *)&efi_tcg2_protocol); - if (ret != EFI_SUCCESS) + if (ret != EFI_SUCCESS) { log_err("Cannot install EFI_TCG2_PROTOCOL\n"); + goto fail; + } +out: + return ret; +fail: + tcg2_uninit(); return ret; }