Skip to content

Commit

Permalink
measure buffer that is used (linux16) GH #9
Browse files Browse the repository at this point in the history
  • Loading branch information
neusdan committed Jan 2, 2016
1 parent 6e593ed commit 344c189
Showing 1 changed file with 42 additions and 28 deletions.
70 changes: 42 additions & 28 deletions grub-core/loader/i386/pc/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,14 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
grub_file_t file = 0;
struct linux_kernel_header lh;
grub_uint8_t setup_sects;
grub_size_t real_size;
grub_size_t real_size, kernelBufOffset = 0;
grub_ssize_t len;
int i;
char *grub_linux_prot_chunk;
int grub_linux_is_bzimage;
grub_addr_t grub_linux_prot_target;
grub_err_t err;
grub_uint8_t* kernelBuf = 0;

grub_dl_ref (my_mod);

Expand All @@ -147,13 +148,24 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
if (! file)
goto fail;

if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
{
if (!grub_errno)
grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
argv[0]);
goto fail;
}
/* Begin TCG Extension */
kernelBuf = grub_malloc( file->size );
if( ! kernelBuf )
{
grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("allocating kernel buffer failed"));
goto fail;
}

if (grub_file_read (file, kernelBuf, file->size) != (grub_ssize_t) file->size)
{
if (!grub_errno)
grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
argv[0]);
goto fail;
}

grub_memcpy( &lh, kernelBuf, sizeof(lh) );
kernelBufOffset = sizeof(lh);

if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
{
Expand Down Expand Up @@ -309,22 +321,21 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
grub_linux_real_target,
GRUB_LINUX_CL_OFFSET
+ maximal_cmdline_size);
if (err)
return err;
if (err) {
grub_free(kernelBuf);
return err;
}

grub_linux_real_chunk = get_virtual_current_address (ch);
}

/* Put the real mode code at the temporary address. */
grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh));

len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
if (grub_file_read (file, grub_linux_real_chunk + sizeof (lh), len) != len)
{
if (!grub_errno)
grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
argv[0]);
goto fail;
}

grub_memcpy( grub_linux_real_chunk + sizeof (lh), kernelBuf + kernelBufOffset, len );
kernelBufOffset+= len;

if (lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_MAGIC_SIGNATURE)
|| grub_le_to_cpu16 (lh.version) < 0x0200)
Expand Down Expand Up @@ -353,39 +364,42 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
grub_linux_prot_target,
grub_linux16_prot_size);
if (err)
return err;
if (err) {
grub_free( kernelBuf);
return err;
}

grub_linux_prot_chunk = get_virtual_current_address (ch);
}

len = grub_linux16_prot_size;
if (grub_file_read (file, grub_linux_prot_chunk, grub_linux16_prot_size)
!= (grub_ssize_t) grub_linux16_prot_size && !grub_errno)
grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
argv[0]);

grub_memcpy( grub_linux_prot_chunk, kernelBuf + kernelBufOffset, len );

if (grub_errno == GRUB_ERR_NONE)
{
grub_loader_set (grub_linux16_boot, grub_linux_unload, 0);
loaded = 1;
DEBUG_PRINT( ("measured linux16 kernel: \n") );
grub_TPM_measure_buffer( kernelBuf, file->size, TPM_LOADED_FILES_PCR );
}

fail:

if (file)
grub_file_close (file);

if(kernelBuf) {
grub_free(kernelBuf);
}
/* End TCG Extension */

if (grub_errno != GRUB_ERR_NONE)
{
grub_dl_unref (my_mod);
loaded = 0;
grub_relocator_unload (relocator);
}
/* Begin TCG Extension */
else { /* file successfully loaded */
grub_TPM_measure_file( argv[0], TPM_LOADED_FILES_PCR );
}
/* End TCG Extension */

return grub_errno;
}
Expand Down

0 comments on commit 344c189

Please sign in to comment.