From patchwork Tue Jul 20 05:52:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 481486 Delivered-To: patch@linaro.org Received: by 2002:a02:c94a:0:0:0:0:0 with SMTP id u10csp4810352jao; Mon, 19 Jul 2021 22:52:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxqymocgG0AyH/LP6yiR4fCuNISSQLBG4axMKX7FeR3RB5ImKtEVskZsuo1naAkQ19AFqfM X-Received: by 2002:a17:906:d20b:: with SMTP id w11mr31451719ejz.242.1626760371974; Mon, 19 Jul 2021 22:52:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626760371; cv=none; d=google.com; s=arc-20160816; b=bfPOlyGEIIndd4VUQ2sR87WreizvZDsSoxm/R3Q8avrj511TX+Yk1saI5WRxzLY6IU 5Pfdfm6uDxrd9FLJ8vwL8O2Co/e7yzq/9tW0PVP3Rzl6YneqqSzGYSM2MQAI4yOBwwjQ mo4+03a8fvL7Xb+DoyZtt3Ul7PsDob4qqE6K7UPXVHHHDinE+k9jTGja1IUiOBQ2oUbR cB/SGnMZndSJNnkorwoOz7d456e1H5b+Fd9/C3fQG+DU5tezF7C1L6GPxOfyEzqy/xxY biIvvUywfeXis53vMiqqdiBLueh6wquNf3StPC8GNqpMezcxu1+BIHIdk7/BpslgWEzl 9uSg== 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=LINZrPn/BTfyK0kYVbgnfe4w/3EQ6I9BwkjotwBP61g=; b=SJPL0izws+hW7XbL2GAfrk86Fai5yH4nJOBqk0MttIgQuZXnEQCPE9VUro0OdizFJY zDcSJ/WvsSQjOAJc7IMPjgionzF1wxcREnKuMPCO0SszirBIQqLw0sugAOOrVAQlN/fY 2djWDPDzt6vVdnJdsXGtQ6CzJowZpqkiZWI5scgmH1Z5jJnhfMErq3IxsHrA7fZ6otBa 4qNJqSvnGuA+xk0Hp4DJAam9xEbEqVvOr6Ev6ICtQHBR15Gryd11sbWkXfDYylkwFQ67 Yb3DLxciFTB5fiFv8q61hQetxCsk2K818k9hUxTTv47HD5CdLpBXApYgTmQlJ4+2xY9g DFjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Dq9A6XC+; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id j25si1063675eja.483.2021.07.19.22.52.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jul 2021 22:52:51 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Dq9A6XC+; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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 DFDE88262E; Tue, 20 Jul 2021 07:52:50 +0200 (CEST) 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="Dq9A6XC+"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1B5EF82956; Tue, 20 Jul 2021 07:52:49 +0200 (CEST) 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-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) (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 F083281BDA for ; Tue, 20 Jul 2021 07:52:45 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=takahiro.akashi@linaro.org Received: by mail-pj1-x1034.google.com with SMTP id cu14so13085769pjb.0 for ; Mon, 19 Jul 2021 22:52: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=LINZrPn/BTfyK0kYVbgnfe4w/3EQ6I9BwkjotwBP61g=; b=Dq9A6XC+c2U5RDOOMmn6jq/5dOA5LAVcz/Ti+yvuMtDF/gEohYGOYIacNmaRDyrcxt rGDUJ/xgKeXmIyXjSugLuk8vxEYQaA8ROQmytkMtbQzc8Lhe6tdSABaGfBD14ebcQts/ /T3tpdIW0oeoZL6i9o1qZMg67nEMI/MGwRO1Ni2N8XRIr6InHhAP25ftar71aAyMLlC+ kwTJBv1U23mylRUjbNElPVHXu8Qn1go3tsvFvEy2gY9a8GMa6+pCpiLiTM3mMW+ROGKR cGesdrG1VPH0yOnViyPme/vIRiWdPMHnsHdFVSLKuNy4uLnm1v2q50T7iazLdzbB48oG j0Ag== 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=LINZrPn/BTfyK0kYVbgnfe4w/3EQ6I9BwkjotwBP61g=; b=aga7ead/+rN2fEgX/vSwLlO36aav0uVDSfIDDGtlw4EqkLAANh0M+SV6UhZgeIPvCI rRrVMiOZH2gYUUZx6Q2uXzC0a+A6OEw0XA+q+buxcRZHJZe6H7phFVESDKqoM4HOHLnY 5p53Hcalm9HFG4KOdgO0xHf2SHS3HCRdXaUO6p/Ykoe74IThF/hMpO+m2adkd7+t5lj2 YCOJUvKBkc60p+JDyZgeE758mGNMS7i7Dd3tYjnkeLMMyEIdhmoENpfqQTgtZ0N3sDj3 l91bUILiIfBy0NTPNdqmvLRqNZmcr26tElrRZBW6Mm7Po42nh/hmzsQT2XLW06oo5rpL wxfg== X-Gm-Message-State: AOAM5334PtyM4h2bjoGmuq5la+1Gem/v2ffMo/9bGIGUTjMzAl+6xS52 FHjoEPhKbVhaiQbBvj3DfgnD0w== X-Received: by 2002:a17:90a:7e81:: with SMTP id j1mr28177100pjl.26.1626760364026; Mon, 19 Jul 2021 22:52:44 -0700 (PDT) Received: from localhost.localdomain (p784a236a.tkyea130.ap.so-net.ne.jp. [120.74.35.106]) by smtp.gmail.com with ESMTPSA id q3sm23668622pfb.184.2021.07.19.22.52.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jul 2021 22:52:43 -0700 (PDT) From: AKASHI Takahiro To: xypron.glpk@gmx.de, agraf@csgraf.de Cc: u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH v2] efi_loader: capsule: remove authentication data Date: Tue, 20 Jul 2021 14:52:05 +0900 Message-Id: <20210720055205.103060-1-takahiro.akashi@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.103.2 at phobos.denx.de X-Virus-Status: Clean If capsule authentication is disabled and yet a capsule file is signed, its signature must be removed from image data to flush. Otherwise, the firmware will be corrupted after update. Fixes: 04be98bd6bcf ("efi: capsule: Add support for uefi capsule authentication") Signed-off-by: AKASHI Takahiro --- v2: rebase v1 on top of Ilias's patch --- lib/efi_loader/efi_capsule.c | 70 +++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 13 deletions(-) -- 2.31.0 diff --git a/lib/efi_loader/efi_capsule.c b/lib/efi_loader/efi_capsule.c index 3c029378de0a..26990bc2df4a 100644 --- a/lib/efi_loader/efi_capsule.c +++ b/lib/efi_loader/efi_capsule.c @@ -218,6 +218,39 @@ skip: return NULL; } +/** + * efi_remove_auth_hdr - remove authentication data from image + * @image: Pointer to pointer to Image + * @image_size: Pointer to Image size + * + * Remove the authentication data from image if possible. + * Update @image and @image_size. + * + * Return: status code + */ +static efi_status_t efi_remove_auth_hdr(void **image, efi_uintn_t *image_size) +{ + struct efi_firmware_image_authentication *auth_hdr; + efi_status_t ret = EFI_INVALID_PARAMETER; + + auth_hdr = (struct efi_firmware_image_authentication *)*image; + if (*image_size < sizeof(*auth_hdr)) + goto out; + + if (auth_hdr->auth_info.hdr.dwLength <= + offsetof(struct win_certificate_uefi_guid, cert_data)) + goto out; + + *image = (uint8_t *)*image + sizeof(auth_hdr->monotonic_count) + + auth_hdr->auth_info.hdr.dwLength; + *image_size = *image_size - auth_hdr->auth_info.hdr.dwLength - + sizeof(auth_hdr->monotonic_count); + + ret = EFI_SUCCESS; +out: + return ret; +} + #if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE) static int efi_get_public_key_data(void **pkey, efi_uintn_t *pkey_len) @@ -254,21 +287,15 @@ efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_s if (capsule == NULL || capsule_size == 0) goto out; - auth_hdr = (struct efi_firmware_image_authentication *)capsule; - if (capsule_size < sizeof(*auth_hdr)) - goto out; - - if (auth_hdr->auth_info.hdr.dwLength <= - offsetof(struct win_certificate_uefi_guid, cert_data)) + *image = (uint8_t *)capsule; + *image_size = capsule_size; + if (efi_remove_auth_hdr(image, image_size) != EFI_SUCCESS) goto out; + auth_hdr = (struct efi_firmware_image_authentication *)capsule; if (guidcmp(&auth_hdr->auth_info.cert_type, &efi_guid_cert_type_pkcs7)) goto out; - *image = (uint8_t *)capsule + sizeof(auth_hdr->monotonic_count) + - auth_hdr->auth_info.hdr.dwLength; - *image_size = capsule_size - auth_hdr->auth_info.hdr.dwLength - - sizeof(auth_hdr->monotonic_count); memcpy(&monotonic_count, &auth_hdr->monotonic_count, sizeof(monotonic_count)); @@ -348,7 +375,7 @@ static efi_status_t efi_capsule_update_firmware( { struct efi_firmware_management_capsule_header *capsule; struct efi_firmware_management_capsule_image_header *image; - size_t capsule_size; + size_t capsule_size, image_binary_size; void *image_binary, *vendor_code; efi_handle_t *handles; efi_uintn_t no_handles; @@ -410,13 +437,30 @@ static efi_status_t efi_capsule_update_firmware( } /* do update */ + if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) && + !(image->image_capsule_support & + CAPSULE_SUPPORT_AUTHENTICATION)) { + /* no signature */ + ret = EFI_SECURITY_VIOLATION; + goto out; + } + image_binary = (void *)image + sizeof(*image); - vendor_code = image_binary + image->update_image_size; + image_binary_size = image->update_image_size; + vendor_code = image_binary + image_binary_size; + if (!IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) && + (image->image_capsule_support & + CAPSULE_SUPPORT_AUTHENTICATION)) { + ret = efi_remove_auth_hdr(&image_binary, + &image_binary_size); + if (ret != EFI_SUCCESS) + goto out; + } abort_reason = NULL; ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index, image_binary, - image->update_image_size, + image_binary_size, vendor_code, NULL, &abort_reason)); if (ret != EFI_SUCCESS) {