All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ilias Apalodimas <ilias.apalodimas@linaro.org>
To: Ruchika Gupta <ruchika.gupta@linaro.org>
Cc: u-boot@lists.denx.de, xypron.glpk@gmx.de, agraf@csgraf.de,
	 masahisa.kojima@linaro.org
Subject: Re: [PATCH v7 3/3] efi_loader: Extend PCR's for firmware measurements
Date: Fri, 26 Nov 2021 23:01:32 +0200	[thread overview]
Message-ID: <CAC_iWjKbW9n95e3=FmdrhNtDzNm3=SXP4aPD1AUFKFc-zWaphw@mail.gmail.com> (raw)
In-Reply-To: <20211126115301.1103687-3-ruchika.gupta@linaro.org>

Hi Ruchika,

On Fri, 26 Nov 2021 at 13:53, Ruchika Gupta <ruchika.gupta@linaro.org> wrote:
>
> Firmwares before U-Boot may be capable of doing tpm measurements
> and passing them to U-Boot in the form of eventlog. However there
> may be scenarios where the firmwares don't have TPM driver and
> are not capable of extending the measurements in the PCRs.
> Based on TCG spec, if previous firnware has extended PCR's, PCR0
> would not be 0. So, read the PCR0 to determine if the PCR's need
> to be extended as eventlog is parsed or not.
>
> Signed-off-by: Ruchika Gupta <ruchika.gupta@linaro.org>
> Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> Tested-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> ---
> v7:
> Addressed Heinrick's comments - Added missing parameter in function header
>
> v6: Changed TPM2_DIGEST_LEN to TPM2_SHA512_DIGEST_SIZE
>
> v5 : No change
>
> v4 : No change
>
> v3 :
> Rebase changes on top of changes made in first patch series
>
> v2 :
> Removed check for PCR0 in eventlog
>
>  lib/efi_loader/efi_tcg2.c | 76 +++++++++++++++++++++++++++++++++++++++
>  1 file changed, 76 insertions(+)
>
> diff --git a/lib/efi_loader/efi_tcg2.c b/lib/efi_loader/efi_tcg2.c
> index 5ded57fd29..d247179fbf 100644
> --- a/lib/efi_loader/efi_tcg2.c
> +++ b/lib/efi_loader/efi_tcg2.c
> @@ -199,6 +199,44 @@ static efi_status_t tcg2_pcr_extend(struct udevice *dev, u32 pcr_index,
>         return EFI_SUCCESS;
>  }
>
> +/* tcg2_pcr_read - Read PCRs for a TPM2 device for a given tpml_digest_values
> + *
> + * @dev:               device
> + * @pcr_index:         PCR index
> + * @digest_list:       list of digest algorithms to extend
> + *
> + * @Return: status code
> + */
> +static efi_status_t tcg2_pcr_read(struct udevice *dev, u32 pcr_index,
> +                                 struct tpml_digest_values *digest_list)
> +{
> +       struct tpm_chip_priv *priv;
> +       unsigned int updates, pcr_select_min;
> +       u32 rc;
> +       size_t i;
> +
> +       priv = dev_get_uclass_priv(dev);
> +       if (!priv)
> +               return EFI_DEVICE_ERROR;
> +
> +       pcr_select_min = priv->pcr_select_min;
> +
> +       for (i = 0; i < digest_list->count; i++) {
> +               u16 hash_alg = digest_list->digests[i].hash_alg;
> +               u8 *digest = (u8 *)&digest_list->digests[i].digest;
> +
> +               rc = tpm2_pcr_read(dev, pcr_index, pcr_select_min,
> +                                  hash_alg, digest, alg_to_len(hash_alg),
> +                                  &updates);
> +               if (rc) {
> +                       EFI_PRINT("Failed to read PCR\n");
> +                       return EFI_DEVICE_ERROR;
> +               }
> +       }
> +
> +       return EFI_SUCCESS;
> +}
> +
>  /* put_event - Append an agile event to an eventlog
>   *
>   * @pcr_index:         PCR index
> @@ -1461,6 +1499,8 @@ static efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer,
>         u32 pcr, pos;
>         u64 base;
>         u32 sz;
> +       bool extend_pcr = false;
> +       int i;
>
>         ret = platform_get_eventlog(dev, &base, &sz);
>         if (ret != EFI_SUCCESS)
> @@ -1482,6 +1522,26 @@ static efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer,
>                 return ret;
>         }
>
> +       ret = tcg2_pcr_read(dev, 0, &digest_list);
> +       if (ret) {
> +               log_err("Error reading PCR 0\n");
> +               return ret;
> +       }
> +
> +       /*
> +        * If PCR0 is 0, previous firmware didn't have the capability
> +        * to extend the PCR. In this scenario, extend the PCR as
> +        * the eventlog is parsed.
> +        */
> +       for (i = 0; i < digest_list.count; i++) {
> +               u8 buffer[TPM2_SHA512_DIGEST_SIZE] =  { 0 };

I missed that since it doesn't show on this diff but on [1/3].
However there's a prexisting variable in the begging called 'void
*buffer', so this needs a different name

> +               u16 hash_alg = digest_list.digests[i].hash_alg;
> +
> +               if (!memcmp((u8 *)&digest_list.digests[i].digest, buffer,
> +                           alg_to_len(hash_alg)))
> +                       extend_pcr = true;
> +       }
> +
>         while (pos < sz) {
>                 ret = tcg2_parse_event(dev, buffer, sz, &pos, &digest_list,
>                                        &pcr);
> @@ -1489,6 +1549,22 @@ static efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer,
>                         log_err("Error parsing event\n");
>                         return ret;
>                 }
> +               if (extend_pcr) {
> +                       ret = tcg2_pcr_extend(dev, pcr, &digest_list);
> +                       if (ret != EFI_SUCCESS) {
> +                               log_err("Error in extending PCR\n");
> +                               return ret;
> +                       }
> +
> +                       /* Clear the digest for next event */
> +                       for (i = 0; i < digest_list.count; i++) {
> +                               u16 hash_alg = digest_list.digests[i].hash_alg;
> +                               u8 *digest =
> +                                  (u8 *)&digest_list.digests[i].digest;
> +
> +                               memset(digest, 0, alg_to_len(hash_alg));
> +                       }
> +               }
>         }
>
>         memcpy(log_buffer, buffer, sz);
> --
> 2.25.1
>

Cheers
/Ilias

  reply	other threads:[~2021-11-26 21:02 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-26 11:52 [PATCH v7 1/3] efi_loader: Add check for event log passed from firmware Ruchika Gupta
2021-11-26 11:53 ` [v7 PATCH 2/3] tpm: use more algorithms than sha256 on pcr_read Ruchika Gupta
2021-11-26 11:53 ` [PATCH v7 3/3] efi_loader: Extend PCR's for firmware measurements Ruchika Gupta
2021-11-26 21:01   ` Ilias Apalodimas [this message]
2021-11-26 13:52 ` [PATCH v7 1/3] efi_loader: Add check for event log passed from firmware Ilias Apalodimas
2021-11-26 20:27   ` Heinrich Schuchardt
2021-11-26 20:58     ` Ilias Apalodimas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAC_iWjKbW9n95e3=FmdrhNtDzNm3=SXP4aPD1AUFKFc-zWaphw@mail.gmail.com' \
    --to=ilias.apalodimas@linaro.org \
    --cc=agraf@csgraf.de \
    --cc=masahisa.kojima@linaro.org \
    --cc=ruchika.gupta@linaro.org \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.