Skip to content
This repository was archived by the owner on Sep 18, 2020. It is now read-only.

Commit a2599ab

Browse files
author
Matthew Garrett
committed
Rework linux command
We want a single buffer that contains the entire kernel image in order to perform a TPM measurement. Allocate one and copy the entire kernel into it before pulling out the individual blocks later on.
1 parent 1e32d63 commit a2599ab

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

grub-core/loader/i386/linux.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -681,12 +681,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
681681
grub_file_t file = 0;
682682
struct linux_kernel_header lh;
683683
grub_uint8_t setup_sects;
684-
grub_size_t real_size, prot_size, prot_file_size;
684+
grub_size_t real_size, prot_size, prot_file_size, kernel_offset;
685685
grub_ssize_t len;
686686
int i;
687687
grub_size_t align, min_align;
688688
int relocatable;
689689
grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
690+
grub_uint8_t *kernel = NULL;
690691

691692
grub_dl_ref (my_mod);
692693

@@ -700,14 +701,25 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
700701
if (! file)
701702
goto fail;
702703

703-
if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
704+
len = grub_file_size (file);
705+
kernel = grub_malloc (len);
706+
if (!kernel)
707+
{
708+
grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
709+
goto fail;
710+
}
711+
712+
if (grub_file_read (file, kernel, len) != len)
704713
{
705714
if (!grub_errno)
706715
grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
707716
argv[0]);
708717
goto fail;
709718
}
710719

720+
grub_memcpy (&lh, kernel, sizeof (lh));
721+
kernel_offset = sizeof (lh);
722+
711723
if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
712724
{
713725
grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
@@ -807,13 +819,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
807819
linux_params.ps_mouse = linux_params.padding10 = 0;
808820

809821
len = sizeof (linux_params) - sizeof (lh);
810-
if (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)
811-
{
812-
if (!grub_errno)
813-
grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
814-
argv[0]);
815-
goto fail;
816-
}
822+
823+
grub_memcpy (&linux_params + sizeof (lh), kernel + kernel_offset, len);
824+
kernel_offset += len;
817825

818826
linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
819827

@@ -872,7 +880,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
872880

873881
/* The other parameters are filled when booting. */
874882

875-
grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
883+
kernel_offset = real_size + GRUB_DISK_SECTOR_SIZE;
876884

877885
grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
878886
(unsigned) real_size, (unsigned) prot_size);
@@ -1018,9 +1026,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
10181026

10191027
grub_pass_verity_hash(&lh, linux_cmdline);
10201028
len = prot_file_size;
1021-
if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno)
1022-
grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
1023-
argv[0]);
1029+
grub_memcpy (prot_mode_mem, kernel + kernel_offset, len);
10241030

10251031
if (grub_errno == GRUB_ERR_NONE)
10261032
{
@@ -1031,6 +1037,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
10311037

10321038
fail:
10331039

1040+
grub_free (kernel);
1041+
10341042
if (file)
10351043
grub_file_close (file);
10361044

0 commit comments

Comments
 (0)