From mboxrd@z Thu Jan 1 00:00:00 1970 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 phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 48452C433F5 for ; Tue, 25 Jan 2022 07:26:07 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 612BC81D99; Tue, 25 Jan 2022 08:26:05 +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="AcuDCaYC"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CFAC983035; Tue, 25 Jan 2022 08:26:03 +0100 (CET) Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) (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 404B680FD6 for ; Tue, 25 Jan 2022 08:26:00 +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-x431.google.com with SMTP id v13so18023421wrv.10 for ; Mon, 24 Jan 2022 23:26:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=Ni862XjsKtfmFy5vJxIfNe1FO/wsiqINniv4nFpXgnU=; b=AcuDCaYCf0py4fBVG7kEACHOLnH6lUJnG11gpD5qR2pjAas0fI0Vt8gn7e2BJSaqBk +3E0OIqspqKv2mjtauVJixFWDwelmcNy8aGOPUCNOMwRSPKXn7hzkD1wrBe1PncM84vY NOnZl6+fLJTGmB7Ps3N6yvRfE8BCF8E9YWq1PYPt4zBj8pdkbGOiEbmVYNW1k/pOYNVy jHVuzZwUViRbUO3TsN6Mxt2Es3ADsikjgvSdo4fXTeBPnf/Td8kYGXGDbxq+aUOGo+Ej ndO13xKaHGxp2ahSWlxkRfsSNLZvCoNpFrPI0biWgdqqlUX3Kqn+Um4dWkWG5cSwZtUh jaKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=Ni862XjsKtfmFy5vJxIfNe1FO/wsiqINniv4nFpXgnU=; b=T6Q3pp7nWrP9o6NlVSDZpO9otMXFYhYtzp/u3JxWBWPyGNXtMTXTFoPKXIOeSugt5L 1FpTJe5dEgu2Ia0tLE5Y+wGlH2OXKVV68abIpIaTFEjVjTqSKbiuoTaOjn1gwSM608nx vs9CdobC84FlgkeYjSc8IeB+94knNtCfvHBKAto9YQnYPJioyrosfLBcLJWR/xBHwZcC aVyiQ0JR6k4ZLV9muDJWnGV/bb4/VvDlOCV6JMMM6o3n5hvAZQJnJMd06GPfyZw6nLrW QCR4tHhqCN4fRJIN2TchVEV43bUVkaZFg0Oq+sqxBFe0mqAek75daf40eUGOonHzeKAh SP3Q== X-Gm-Message-State: AOAM531TtgvDt4OYKluDvBnK9H8IKE7vIU/sDtANVSTvExDL978u7nOL e+C+QmTo06U1bdkuyJVkiOk1yQ== X-Google-Smtp-Source: ABdhPJwVt0hszQo36LDxCxCF5lmRLcrJxAML6BM1J1mw9VwEgHq0FkJGvtxhhUFBB0sblocqkEdyAg== X-Received: by 2002:adf:f0cb:: with SMTP id x11mr16686533wro.222.1643095559675; Mon, 24 Jan 2022 23:25:59 -0800 (PST) Received: from hades (athedsl-4461669.home.otenet.gr. [94.71.4.85]) by smtp.gmail.com with ESMTPSA id y15sm19334897wry.36.2022.01.24.23.25.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 23:25:59 -0800 (PST) Date: Tue, 25 Jan 2022 09:25:56 +0200 From: Ilias Apalodimas To: AKASHI Takahiro , xypron.glpk@gmx.de, Ilias Apalodimas , u-boot@lists.denx.de Subject: Re: [RFC PATCH] efi_loader: clean up uefi secure boot image verification logic Message-ID: References: <20220124153621.662350-1-ilias.apalodimas@linaro.org> <20220125023107.GB9031@laputa> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220125023107.GB9031@laputa> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.5 at phobos.denx.de X-Virus-Status: Clean Hi Akashi-san, On Tue, Jan 25, 2022 at 11:31:07AM +0900, AKASHI Takahiro wrote: > Hi Ilias, > > On Mon, Jan 24, 2022 at 05:36:20PM +0200, Ilias Apalodimas wrote: > > From: Ilias Apalodimas > > > > We currently distinguish between signed and non signed PE/COFF > > executables while trying to authenticate signatures and/or sha256 > > hashes in db and dbx. That code duplication can be avoided. > > Thank you for cleaning up the code. > The change you made here looks good. > I want you to revise some wordings for clarification. > > > On sha256 hashes we don't really care if the image is signed or > > not. > > The sentence above might sound a bit misleading? I can change that to 'when checking for sha256 hashes in db' or something along those lines > > > The logic can be implemented in > > 1. Is the sha256 of the image in dbx > > Please explicitly mention that the image will be rejected > in this case unlike the other cases below. sure > > > 2. Is the image signed with a certificate that's found in db and > > not in dbx > > 3. The image carries a cert which is signed by a cert in db (and > > not in dbx, and the image can be verified against the former) > > (2) and (3) are the rules applied only if the image is signed. So, > 2. If the image is signed, > 2-1. ... > 2-2. ... > > In addition, your (3) should be described in a similar way as (2). > For instance, > 3. Is the image signed with a certificate that is carried in > the image and the cert is signed by another cert in the image > and so on, or in db (... ). Doesn't the 'signed against the former' describes the same thing here? > > (although it is difficult to describe the logic precisely.) > > > 4. Is the sha256 of the image in db > > So weed out the 'special' case handling unsigned images. > > > > While at it move the checking of a hash outside the certificate > > verification loop which isn't really needed and check for the hash > > only once. > > It might be my preference, but I would like to add > assert(ret == false); > before efi_signature_lookup_digest(regs, db) to indicate this is the last > resort to authenticate the image. I am not a fan of asserts myself. How about a comment or an EFI_PRINT? > > > Also allow a mix of signatures and hashes in db instead > > of explicitly breaking out the sha verification loop if a signature > > happens to be added first. > > I don't think that your change gives us an extra case of "allowing". > Please elaborate if I misunderstand something. This might actually be better of in a separated patch. The patch for efi_signature.c changes the logic a bit. Right now when we try to verify a hash and find a non supported algorithm we stop processing db. But that assumes that the hashes are added first and the signatures follow. IOW if you add in db everything works fine when you try to authenticate an image with it's hash. If the order is reversed the code rejects the image (while it shouldn't) > > > It's worth noting that (2) only works if the certificate is self > > signed, but that canb be fixed in a following patch. > > Yes, please. > > -Takahiro Akashi Thanks /Ilias > > > Signed-off-by: Ilias Apalodimas > > --- > > lib/efi_loader/efi_image_loader.c | 84 ++++++------------------------- > > lib/efi_loader/efi_signature.c | 2 +- > > 2 files changed, 15 insertions(+), 71 deletions(-) > > > > diff --git a/lib/efi_loader/efi_image_loader.c b/lib/efi_loader/efi_image_loader.c > > index 255613eb72ba..53d54ca42f5c 100644 > > --- a/lib/efi_loader/efi_image_loader.c > > +++ b/lib/efi_loader/efi_image_loader.c > > @@ -516,63 +516,16 @@ err: > > } > > > > #ifdef CONFIG_EFI_SECURE_BOOT > > -/** > > - * efi_image_unsigned_authenticate() - authenticate unsigned image with > > - * SHA256 hash > > - * @regs: List of regions to be verified > > - * > > - * If an image is not signed, it doesn't have a signature. In this case, > > - * its message digest is calculated and it will be compared with one of > > - * hash values stored in signature databases. > > - * > > - * Return: true if authenticated, false if not > > - */ > > -static bool efi_image_unsigned_authenticate(struct efi_image_regions *regs) > > -{ > > - struct efi_signature_store *db = NULL, *dbx = NULL; > > - bool ret = false; > > - > > - dbx = efi_sigstore_parse_sigdb(L"dbx"); > > - if (!dbx) { > > - EFI_PRINT("Getting signature database(dbx) failed\n"); > > - goto out; > > - } > > - > > - db = efi_sigstore_parse_sigdb(L"db"); > > - if (!db) { > > - EFI_PRINT("Getting signature database(db) failed\n"); > > - goto out; > > - } > > - > > - /* try black-list first */ > > - if (efi_signature_lookup_digest(regs, dbx)) { > > - EFI_PRINT("Image is not signed and its digest found in \"dbx\"\n"); > > - goto out; > > - } > > - > > - /* try white-list */ > > - if (efi_signature_lookup_digest(regs, db)) > > - ret = true; > > - else > > - EFI_PRINT("Image is not signed and its digest not found in \"db\" or \"dbx\"\n"); > > - > > -out: > > - efi_sigstore_free(db); > > - efi_sigstore_free(dbx); > > - > > - return ret; > > -} > > - > > /** > > * efi_image_authenticate() - verify a signature of signed image > > * @efi: Pointer to image > > * @efi_size: Size of @efi > > * > > - * A signed image should have its signature stored in a table of its PE header. > > + * An image should have its signature stored in a table of its PE header. > > * So if an image is signed and only if if its signature is verified using > > - * signature databases, an image is authenticated. > > - * If an image is not signed, its validity is checked by using > > - * efi_image_unsigned_authenticated(). > > + * signature databases or if it's sha256 is found in db an image is > > + * authenticated. > > + * > > * TODO: > > * When AuditMode==0, if the image's signature is not found in > > * the authorized database, or is found in the forbidden database, > > @@ -608,14 +561,7 @@ static bool efi_image_authenticate(void *efi, size_t efi_size) > > if (!efi_image_parse(new_efi, efi_size, ®s, &wincerts, > > &wincerts_len)) { > > EFI_PRINT("Parsing PE executable image failed\n"); > > - goto err; > > - } > > - > > - if (!wincerts) { > > - /* The image is not signed */ > > - ret = efi_image_unsigned_authenticate(regs); > > - > > - goto err; > > + goto out; > > } > > > > /* > > @@ -624,18 +570,18 @@ static bool efi_image_authenticate(void *efi, size_t efi_size) > > db = efi_sigstore_parse_sigdb(L"db"); > > if (!db) { > > EFI_PRINT("Getting signature database(db) failed\n"); > > - goto err; > > + goto out; > > } > > > > dbx = efi_sigstore_parse_sigdb(L"dbx"); > > if (!dbx) { > > EFI_PRINT("Getting signature database(dbx) failed\n"); > > - goto err; > > + goto out; > > } > > > > if (efi_signature_lookup_digest(regs, dbx)) { > > EFI_PRINT("Image's digest was found in \"dbx\"\n"); > > - goto err; > > + goto out; > > } > > > > /* > > @@ -729,20 +675,18 @@ static bool efi_image_authenticate(void *efi, size_t efi_size) > > /* try white-list */ > > if (efi_signature_verify(regs, msg, db, dbx)) { > > ret = true; > > - break; > > + goto out; > > } > > > > EFI_PRINT("Signature was not verified by \"db\"\n"); > > > > - if (efi_signature_lookup_digest(regs, db)) { > > - ret = true; > > - break; > > - } > > - > > - EFI_PRINT("Image's digest was not found in \"db\" or \"dbx\"\n"); > > } > > > > -err: > > + if (efi_signature_lookup_digest(regs, db)) > > + ret = true; > > + else > > + EFI_PRINT("Image's digest was not found in \"db\" or \"dbx\"\n"); > > +out: > > efi_sigstore_free(db); > > efi_sigstore_free(dbx); > > pkcs7_free_message(msg); > > diff --git a/lib/efi_loader/efi_signature.c b/lib/efi_loader/efi_signature.c > > index 3243e2c60de0..8fa82f8cea4c 100644 > > --- a/lib/efi_loader/efi_signature.c > > +++ b/lib/efi_loader/efi_signature.c > > @@ -176,7 +176,7 @@ bool efi_signature_lookup_digest(struct efi_image_regions *regs, > > if (guidcmp(&siglist->sig_type, &efi_guid_sha256)) { > > EFI_PRINT("Digest algorithm is not supported: %pUs\n", > > &siglist->sig_type); > > - break; > > + continue; > > } > > > > if (!efi_hash_regions(regs->reg, regs->num, &hash, &size)) { > > -- > > 2.30.2 > >