Re: [PATCH v4] x86/kexec: Carry forward IMA measurement log on kexec

Jonathan McDowell
2022-05-18T10:42:09Z

On Tue, May 17, 2022 at 10:19:45AM -0700, Lakshmi Ramasubramanian wrote:
>
> > > > diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzimage64.c
> > > > index 170d0fd68b1f..54bd4ce5f908 100644
> > > > --- a/arch/x86/kernel/kexec-bzimage64.c
> > > > +++ b/arch/x86/kernel/kexec-bzimage64.c
> > > > @@ -186,6 +186,33 @@ setup_efi_state(struct boot_params *params, unsigned long params_load_addr,
> > > > }
> > > > #endif /* CONFIG_EFI */
> > > > +static void
> > > > +setup_ima_state(const struct kimage *image, struct boot_params *params,
> > > > + unsigned long params_load_addr,
> > > > + unsigned int ima_setup_data_offset)
> > > > +{
> > > > +#ifdef CONFIG_IMA_KEXEC
> > > > + struct setup_data *sd = (void *)params + ima_setup_data_offset;
> > > > + unsigned long setup_data_phys;
> > > > + struct ima_setup_data *ima;
> > > > +
> > > > + if (!image->ima_buffer_size)
> > > > + return;
> > > > +
> > > > + sd->type = SETUP_IMA;
> > > > + sd->len = sizeof(*ima);
> > > > +
> > > > + ima = (void *)sd + sizeof(struct setup_data);
> > > > + ima->addr = image->ima_buffer_addr;
> > > > + ima->size = image->ima_buffer_size;
> > > > +
> > > > + /* Add setup data */
> > > > + setup_data_phys = params_load_addr + ima_setup_data_offset;
> > > > + sd->next = params->hdr.setup_data;
> > > > + params->hdr.setup_data = setup_data_phys;
> > > > +#endif /* CONFIG_IMA_KEXEC */
> > > > +}
> > > > +
> > > > static int
> > > > setup_boot_parameters(struct kimage *image, struct boot_params *params,
> > > > unsigned long params_load_addr,
> > > > @@ -247,6 +274,13 @@ setup_boot_parameters(struct kimage *image, struct boot_params *params,
> > > > setup_efi_state(params, params_load_addr, efi_map_offset, efi_map_sz,
> > > > efi_setup_data_offset);
> > > > #endif
> > > > +
> > > > + /* Setup IMA log buffer state */
> > > > + setup_ima_state(image, params, params_load_addr,
> > > > + efi_setup_data_offset +
> > > > + sizeof(struct setup_data) +
> > > > + sizeof(struct efi_setup_data));
> > > Here you could check image->ima_buffer_size and call setup_ima_state() only
> > > if it is non-zero.
> >
> > setup_ima_state() has this check already.
>
> Yes - I noticed that.
> I was just suggesting a minor optimization - avoid making this function call
> if IMA buffer is not present.
>
> >
> > > > +
> > > > /* Setup EDD info */
> > > > memcpy(params->eddbuf, boot_params.eddbuf,
> > > > EDDMAXNR * sizeof(struct edd_info));
> > > > @@ -403,6 +437,10 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
> > > > sizeof(struct setup_data) +
> > > > sizeof(struct efi_setup_data);
> > > > + if (IS_ENABLED(CONFIG_IMA_KEXEC))
> > > > + kbuf.bufsz += sizeof(struct setup_data) +
> > > > + sizeof(struct ima_setup_data);
> > > > +
> > > > params = kzalloc(kbuf.bufsz, GFP_KERNEL);
> > > > if (!params)
> > > > return ERR_PTR(-ENOMEM);
> > > > diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
> > > > index 249981bf3d8a..ab5e7a351845 100644
> > > > --- a/arch/x86/kernel/setup.c
> > > > +++ b/arch/x86/kernel/setup.c
> > > > @@ -11,6 +11,7 @@
> > > > #include <linux/dma-map-ops.h>
> > > > #include <linux/dmi.h>
> > > > #include <linux/efi.h>
> > > > +#include <linux/ima.h>
> > > > #include <linux/init_ohci1394_dma.h>
> > > > #include <linux/initrd.h>
> > > > #include <linux/iscsi_ibft.h>
> > > > @@ -145,6 +146,11 @@ __visible unsigned long mmu_cr4_features __ro_after_init;
> > > > __visible unsigned long mmu_cr4_features __ro_after_init = X86_CR4_PAE;
> > > > #endif
> > > > +#ifdef CONFIG_IMA
> > > > +static phys_addr_t ima_kexec_buffer_phys;
> > > > +static size_t ima_kexec_buffer_size;
> > > > +#endif
> > > > +
> > > > /* Boot loader ID and version as integers, for the benefit of proc_dointvec */
> > > > int bootloader_type, bootloader_version;
> > > > @@ -335,6 +341,59 @@ static void __init reserve_initrd(void)
> > > > }
> > > > #endif /* CONFIG_BLK_DEV_INITRD */
> > > > +static void __init add_early_ima_buffer(u64 phys_addr)
> > > > +{
> > > > +#ifdef CONFIG_IMA
> > > > + struct ima_setup_data *data;
> > > > +
> > > > + data = early_memremap(phys_addr + sizeof(struct setup_data),
> > > > + sizeof(*data));
> > > > + if (!data) {
> > > > + pr_warn("setup: failed to memremap ima_setup_data entry\n");
> > > > + return;
> > > > + }
> > > Here if memory allocation fails, would kexec system call fail or would it
> > > only not add IMA buffer, but continue with the system call?
> >
> > This is run in the context of the *new* kernel. Boot will continue, but
> > the IMA buffer will not be successfully passed across. Effectively that
> > puts us in the same situation as now; things like TPM PCRs will have
> > been updated, but we won't have the log showing us how we got to the
> > current state.
> I think it is better to treat this error as a critical failure.

That's going to crash the entire system, because it's after we've
started execution of the new kernel. Given that the failure mode will
result in the lack of the logs, but not incorrect TPM PCRs, that seems a
bit extreme.

J.