Received: from list by lists.gnu.org with archive (Exim 4.71) id 1cVxdw-00017m-IO for mharc-grub-devel@gnu.org; Tue, 24 Jan 2017 04:44:00 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43785) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cVoNZ-0001gJ-Co for grub-devel@gnu.org; Mon, 23 Jan 2017 18:50:30 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cVoNY-00083P-Cy for grub-devel@gnu.org; Mon, 23 Jan 2017 18:50:29 -0500 Received: from cavan.codon.org.uk ([2a00:1098:0:80:1000:c:0:1]:55905) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cVoNY-000832-6p for grub-devel@gnu.org; Mon, 23 Jan 2017 18:50:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=codon.org.uk; s=63138784; h=Subject:References:In-Reply-To:Message-Id:Date:Cc:To:From; bh=g9bFaw99Km0Nv3pqYqjO/E8F0urX2tSNS9ho48VF5mk=; b=BTsvT0P0XNmKQqSCeY2+Y8sqgKgjP9PIJMMb8vvFN2JHsjyVpxOdcoOoeY+JjzFcA5H+F/ZLtsM5N4dNddzyvkKjTRHwJViWT558gVw8w3Mq3N6oFn/nR/4MbtsVTO78wa3csLoWb+2HHBW3TJhgfsDPPgQ6Z5XiRUMrIer6DPM=; Received: from [2603:3024:1c06:3af3:3252:cbff:fee6:e579] (helo=xps13-mjg59.libcore.so) by cavan.codon.org.uk with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1cVoNR-00028t-ML; Mon, 23 Jan 2017 23:50:24 +0000 From: Matthew Garrett To: grub-devel@gnu.org Cc: Matthew Garrett Date: Mon, 23 Jan 2017 15:49:49 -0800 Message-Id: <20170123234954.22689-3-mjg59@coreos.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170123234954.22689-1-mjg59@coreos.com> References: <20170123234954.22689-1-mjg59@coreos.com> X-SA-Do-Not-Run: Yes X-SA-Exim-Connect-IP: 2603:3024:1c06:3af3:3252:cbff:fee6:e579 X-SA-Exim-Mail-From: mjg59@codon.org.uk Subject: [PATCH 2/7] Rework linux command X-SA-Exim-Version: 4.2.1 (built Mon, 26 Dec 2011 16:54:46 +0000) X-SA-Exim-Scanned: Yes (on cavan.codon.org.uk) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 2a00:1098:0:80:1000:c:0:1 X-Mailman-Approved-At: Tue, 24 Jan 2017 04:43:51 -0500 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 23 Jan 2017 23:50:30 -0000 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. --- grub-core/loader/i386/linux.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c index b15b8cc..5e54ec9 100644 --- a/grub-core/loader/i386/linux.c +++ b/grub-core/loader/i386/linux.c @@ -680,12 +680,13 @@ 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, prot_size, prot_file_size; + grub_size_t real_size, prot_size, prot_file_size, kernel_offset; grub_ssize_t len; int i; grub_size_t align, min_align; int relocatable; grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR; + grub_uint8_t *kernel = NULL; grub_dl_ref (my_mod); @@ -699,7 +700,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), if (! file) goto fail; - if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh)) + len = grub_file_size (file); + kernel = grub_malloc (len); + if (!kernel) + { + grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer")); + goto fail; + } + + if (grub_file_read (file, kernel, len) != len) { if (!grub_errno) grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), @@ -707,6 +716,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), goto fail; } + grub_memcpy (&lh, kernel, sizeof (lh)); + kernel_offset = sizeof (lh); + if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55)) { grub_error (GRUB_ERR_BAD_OS, "invalid magic number"); @@ -806,13 +818,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), linux_params.ps_mouse = linux_params.padding10 = 0; len = sizeof (linux_params) - sizeof (lh); - if (grub_file_read (file, (char *) &linux_params + 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 (&linux_params + sizeof (lh), kernel + kernel_offset, len); + kernel_offset += len; linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE; @@ -871,7 +879,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), /* The other parameters are filled when booting. */ - grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE); + kernel_offset = real_size + GRUB_DISK_SECTOR_SIZE; grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n", (unsigned) real_size, (unsigned) prot_size); @@ -1016,9 +1024,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), - (sizeof (LINUX_IMAGE) - 1)); len = prot_file_size; - if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno) - grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), - argv[0]); + grub_memcpy (prot_mode_mem, kernel + kernel_offset, len); if (grub_errno == GRUB_ERR_NONE) { @@ -1029,6 +1035,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), fail: + grub_free (kernel); + if (file) grub_file_close (file); -- 2.9.3