From patchwork Fri May 29 06:41:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 1300444 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=2a01:238:438b:c500:173d:9f52:ddab:ee01; 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=vvF+i0Xk; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (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 49YFPK2StGz9sPF for ; Fri, 29 May 2020 16:42:45 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7BF8A819A6; Fri, 29 May 2020 08:42:31 +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="vvF+i0Xk"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id BE4C58192F; Fri, 29 May 2020 08:42:25 +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-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) (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 65B138196A for ; Fri, 29 May 2020 08:42:22 +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-x542.google.com with SMTP id w20so931251pga.6 for ; Thu, 28 May 2020 23:42:22 -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=mK/NQinhBIAWxTsYj3aB9FOFfwDVl1pk3aa50Iv/hqE=; b=vvF+i0XkFf/zzQpGeO8HFRK8pGpNeZ3Fx3cMR75Ft1ZuyFmqVc9LEn3vESEwNVZPeG uhHL0/o3+OFzGpKs7d5BTfshnSHvem6x/4SYzXHhSL8HGk+j0BGQdWwX8Ym2RSMndBw9 ZMA4qQ9Tqd7NIu5X/0zGrXDB6Nd8hgt/qHeQPHg/ZFlmGc6WWSeFn5RAUneeQne2JlmY Msjnd/nCl/jhGYEokOtnCWQzfkWfeJR/S8seifVEoXESBDBqh+7nYpFzcnnaFWcrKJYr wxjuliFHM3EpeAj7Ee81zQAdpSNGTJM1jNR9d1qomrRS+50cUtiCgwUkpzcZhGlE7F9J OVxA== 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=mK/NQinhBIAWxTsYj3aB9FOFfwDVl1pk3aa50Iv/hqE=; b=WZoL9aEcnghbhfl33Db/+8WnJRU2ru/oiszD/3KyO4z77+TQ/HnDu3v6KcUhV6LJh4 uBUID+nK/KRHxB3FtircmrnvQAHyouy2OfwkGnFBocqGugg2Uq0LqiYtoDYsZWPULdrw +HEp1MxkUqcn6/WMWFtQQyWPsIP1bV8Y+revJwk405yu9CIe3ZBJzfmbd5kNeBgVgwW/ hQzDujIxrZe5hnKrBcSHRfXzNV5Wuco1iMu0290O9E7ytuYN4fP/W1Bj3+OkEtDwjq+R z+dOW4mYPmL6UMxr8t4zw4v6VSGq6beuJEUU/CtzR+XP8zvxWAdX5E5+ClhtMJLCzVAT 56gQ== X-Gm-Message-State: AOAM5327MV7X9ux4usgkdSKpAPMznm7HQ3vNWsp6NkOx17SAFR/rMkS8 tzVTXNanePik48Opc4l2vHWfyg== X-Google-Smtp-Source: ABdhPJyJzRddLQb3erlWxD5Cf6Bv2qFbUZlb1XZSvoEEhzEIqBe7fvozEXraXwxBwnmAKMT2ZkLvvA== X-Received: by 2002:a62:cf07:: with SMTP id b7mr7135989pfg.102.1590734540900; Thu, 28 May 2020 23:42:20 -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.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 May 2020 23:42:20 -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 03/13] efi_loader: image_loader: retrieve authenticode only if it exists Date: Fri, 29 May 2020 15:41:20 +0900 Message-Id: <20200529064130.28332-4-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 Since certificate table, which is indexed by IMAGE_DIRECTORY_ENTRY_SECURITY and contains authenticode in PE image, doesn't always exist, we should make sure that we will retrieve its pointer only if it exists. Signed-off-by: AKASHI Takahiro Reviewed-by: Heinrich Schuchardt --- lib/efi_loader/efi_image_loader.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/efi_loader/efi_image_loader.c b/lib/efi_loader/efi_image_loader.c index a13ba3692ec2..5cdaa93519e7 100644 --- a/lib/efi_loader/efi_image_loader.c +++ b/lib/efi_loader/efi_image_loader.c @@ -266,6 +266,8 @@ bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp, dos = (void *)efi; nt = (void *)(efi + dos->e_lfanew); + authoff = 0; + authsz = 0; /* * Count maximum number of regions to be digested. @@ -304,25 +306,36 @@ bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp, efi_image_region_add(regs, &opt->DataDirectory[ctidx] + 1, efi + opt->SizeOfHeaders, 0); + + authoff = opt->DataDirectory[ctidx].VirtualAddress; + authsz = opt->DataDirectory[ctidx].Size; } bytes_hashed = opt->SizeOfHeaders; align = opt->FileAlignment; - authoff = opt->DataDirectory[ctidx].VirtualAddress; - authsz = opt->DataDirectory[ctidx].Size; } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader; + /* Skip CheckSum */ efi_image_region_add(regs, efi, &opt->CheckSum, 0); - efi_image_region_add(regs, &opt->Subsystem, - &opt->DataDirectory[ctidx], 0); - efi_image_region_add(regs, &opt->DataDirectory[ctidx] + 1, - efi + opt->SizeOfHeaders, 0); + if (nt->OptionalHeader.NumberOfRvaAndSizes <= ctidx) { + efi_image_region_add(regs, + &opt->Subsystem, + efi + opt->SizeOfHeaders, 0); + } else { + /* Skip Certificates Table */ + efi_image_region_add(regs, &opt->Subsystem, + &opt->DataDirectory[ctidx], 0); + efi_image_region_add(regs, + &opt->DataDirectory[ctidx] + 1, + efi + opt->SizeOfHeaders, 0); + + authoff = opt->DataDirectory[ctidx].VirtualAddress; + authsz = opt->DataDirectory[ctidx].Size; + } bytes_hashed = opt->SizeOfHeaders; align = opt->FileAlignment; - authoff = opt->DataDirectory[ctidx].VirtualAddress; - authsz = opt->DataDirectory[ctidx].Size; } else { debug("%s: Invalid optional header magic %x\n", __func__, nt->OptionalHeader.Magic);