From patchwork Fri May 29 06:41:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 1300443 X-Patchwork-Delegate: xypron.glpk@gmx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=RlI19IyE; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49YFP64tFfz9sPF for ; Fri, 29 May 2020 16:42:34 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 37F1E818C7; Fri, 29 May 2020 08:42:29 +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="RlI19IyE"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id F318981999; Fri, 29 May 2020 08:42:22 +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,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) (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 88AC481767 for ; Fri, 29 May 2020 08:42:19 +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-pg1-x544.google.com with SMTP id p21so922935pgm.13 for ; Thu, 28 May 2020 23:42:19 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=yeV395/zHfyUrvNtQF076qOpVCvZU4cKfneTexV1sE0=; b=RlI19IyE3z0Jgodl+MEWEvZxDEbtymZf8B+bALAk0BX79tWod+xkhmGpnGgezNeEU9 hwqH2qpQn5wjrqs2FarVlGJv9SToUYme0tV5jyutdRCBSP6z3ZNmGYyXdKqx2mAj9rtX 3x092IqMEZ684ygbD3lL03pHh5/MI5/TvHZmw85R4lwRQNsLgSWOu/Qj5CsoF2B+TKE2 o62qtRaewr9S7NH9hmR27AfM+KFKQBjxuXZUEVsEuBbgpKdXyP2MqCiySgwJNN6S1YPr tE0eDT+GnCTdEiBrC2IQTmRSpZm0tIwCyzeWf3wtsDrk4LDoyRgdQdGeYeUfvNgXnKU/ 2m0Q== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=yeV395/zHfyUrvNtQF076qOpVCvZU4cKfneTexV1sE0=; b=RVXk3vkjKdQQV3yIRKygM/s4MFEmnA0qRpZPjVsNvQoENwNOXMY0wWcC450WjubA/C PAWXyEh5bDQmHCCVfpoT/UnHvszP3lC4/OcSE7rqMH5A9WWxfoJpsNDw7H+gFCjC8XFR QcZwGHAi8NpXbVvy/jBAXqF8VjtQ2/9hhDlHwJJ+QKu6EjDgWcAJs5XZ1ro13f8mvLrD ZMLCzATK/jWvz1DHaA3pdoqyoDRtChCGWMCMLvfJeRGJc88kwokfTcUGMfd8h5nwSlqS Qff1/zV0Ot6A5xWGo5pv1+TCWzRmixJKT0FVYvrwx4mfIt4UtGJ/24qtUpggHornN03y RQ3g== X-Gm-Message-State: AOAM532kqX21EBp1VTzGKSWrLUGM17h2F9falC3ENkkhtz+0L+kmnVRo LQh5DZfew/SPRcN+/ybX4BX0pA== X-Google-Smtp-Source: ABdhPJx26PQVPn46gkHhcTpxKTleBWCBUXQqNhiMPTPmWJ6dqc9CciKfaK0cVazILpCk63m7ZL8gNw== X-Received: by 2002:a63:da11:: with SMTP id c17mr6629905pgh.71.1590734537890; Thu, 28 May 2020 23:42:17 -0700 (PDT) Received: from localhost.localdomain (pdb6272d5.tkyea130.ap.so-net.ne.jp. [219.98.114.213]) by smtp.gmail.com with ESMTPSA id q5sm6483128pfl.199.2020.05.28.23.42.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 May 2020 23:42:17 -0700 (PDT) From: AKASHI Takahiro To: xypron.glpk@gmx.de, agraf@csgraf.de Cc: sughosh.ganu@linaro.org, mail@patrick-wildt.de, u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH 02/13] efi_loader: image_loader: add a check against certificate type of authenticode Date: Fri, 29 May 2020 15:41:19 +0900 Message-Id: <20200529064130.28332-3-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.25.2 In-Reply-To: <20200529064130.28332-1-takahiro.akashi@linaro.org> References: <20200529064130.28332-1-takahiro.akashi@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.30rc1 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.2 at phobos.denx.de X-Virus-Status: Clean UEFI specification requires that we shall support three type of certificates of authenticode in PE image: WIN_CERT_TYPE_EFI_GUID with the guid, EFI_CERT_TYPE_PCKS7_GUID WIN_CERT_TYPE_PKCS_SIGNED_DATA WIN_CERT_TYPE_EFI_PKCS1_15 As EDK2 does, we will support the first two that are pkcs7 SignedData. Signed-off-by: AKASHI Takahiro --- lib/efi_loader/efi_image_loader.c | 55 ++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/lib/efi_loader/efi_image_loader.c b/lib/efi_loader/efi_image_loader.c index 7f35b1e58fe5..a13ba3692ec2 100644 --- a/lib/efi_loader/efi_image_loader.c +++ b/lib/efi_loader/efi_image_loader.c @@ -375,7 +375,7 @@ bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp, } /* Return Certificates Table */ - if (authsz) { + if (authsz > 0) { if (len < authoff + authsz) { debug("%s: Size for auth too large: %u >= %zu\n", __func__, authsz, len - authoff); @@ -480,8 +480,8 @@ static bool efi_image_authenticate(void *efi, size_t efi_size) struct pkcs7_message *msg = NULL; struct efi_signature_store *db = NULL, *dbx = NULL; struct x509_certificate *cert = NULL; - void *new_efi = NULL; - size_t new_efi_size; + void *new_efi = NULL, *auth, *wincerts_end; + size_t new_efi_size, auth_size; bool ret = false; if (!efi_secure_boot_enabled()) @@ -530,20 +530,51 @@ static bool efi_image_authenticate(void *efi, size_t efi_size) } /* go through WIN_CERTIFICATE list */ - for (wincert = wincerts; - (void *)wincert < (void *)wincerts + wincerts_len; + for (wincert = wincerts, wincerts_end = (void *)wincerts + wincerts_len; + (void *)wincert < wincerts_end; wincert = (void *)wincert + ALIGN(wincert->dwLength, 8)) { - if (wincert->dwLength < sizeof(*wincert)) { - debug("%s: dwLength too small: %u < %zu\n", - __func__, wincert->dwLength, sizeof(*wincert)); - goto err; + if ((void *)wincert + sizeof(*wincert) >= wincerts_end) + break; + + if (wincert->dwLength <= sizeof(*wincert)) { + debug("dwLength too small: %u < %zu\n", + wincert->dwLength, sizeof(*wincert)); + continue; } - msg = pkcs7_parse_message((void *)wincert + sizeof(*wincert), - wincert->dwLength - sizeof(*wincert)); + + debug("WIN_CERTIFICATE_TYPE: 0x%x\n", + wincert->wCertificateType); + + auth = (void *)wincert + sizeof(*wincert); + auth_size = wincert->dwLength - sizeof(*wincert); + if (wincert->wCertificateType == WIN_CERT_TYPE_EFI_GUID) { + if (auth + sizeof(efi_guid_t) >= wincerts_end) + break; + + if (auth_size <= sizeof(efi_guid_t)) { + debug("dwLength too small: %u < %zu\n", + wincert->dwLength, sizeof(*wincert)); + continue; + } + if (guidcmp(auth, &efi_guid_cert_type_pkcs7)) { + debug("Certificate type not supported: %pUl\n", + auth); + continue; + } + + auth += sizeof(efi_guid_t); + auth_size -= sizeof(efi_guid_t); + } else if (wincert->wCertificateType + != WIN_CERT_TYPE_PKCS_SIGNED_DATA) { + debug("Certificate type not supported\n"); + continue; + } + + msg = pkcs7_parse_message(auth, auth_size); if (IS_ERR(msg)) { debug("Parsing image's signature failed\n"); msg = NULL; - goto err; + continue; } /* try black-list first */