Skip to contentSkip to conversation

kexec_file: Add buffer hand-over for the next kernel

PATCH In review Thiago Jung Bauermann 2 participants 9 messages Sep 15, 2016

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

v5 cover kexec_file: Add buffer hand-over for the next kernel

Hello,

This version of the patch series fixes two issues:

1. The previous version modified struct kexec_segment, but that broke the ABI for at least 32-bit ARM.

2. The previous version didn't include the hand-over buffer in the kexec image checksum verification.

Now the kexec image checksum covers the hand-over buffer, and kexec_update_segment verifes the checksum before updating the segment, and calculates the new checksum afterwards.

In fact, for powerpc the image checksum now covers everything except the digest itself so even the purgatory is verified. This is accomplished in patch 1, and is an improvement that can be considered separately from the other patches in the series. Unfortunately, x86 modifies the purgatory segment during kernel_kexec so it still has to skip the purgatory from the checksum.

Original cover letter:

This patch series implements a mechanism which allows the kernel to pass on a buffer to the kernel that will be kexec'd. This buffer is passed as a segment which is added to the kimage when it is being prepared by kexec_file_load.

How the second kernel is informed of this buffer is architecture-specific. On powerpc, this is done via the device tree, by checking
the properties /chosen/linux,kexec-handover-buffer-start and /chosen/linux,kexec-handover-buffer-end, which is analogous to how the kernel finds the initrd.

This is needed because the Integrity Measurement Architecture subsystem needs to preserve its measurement list accross the kexec reboot. The following patch series for the IMA subsystem uses this feature for that purpose:

https://lists.infradead.org/pipermail/kexec/2016-August/016745.html

This is so that IMA can implement trusted boot support on the OpenPower platform, because on such systems an intermediary Linux instance running as part of the firmware is used to boot the target operating system via kexec. Using this mechanism, IMA on this intermediary instance can hand over to the target OS the measurements of the components that were used to boot it.

Because there could be additional measurement events between the kexec_file_load call and the actual reboot, IMA needs a way to update the buffer with those additional events before rebooting. One can minimize the interval between the kexec_file_load and the reboot syscalls, but as small as it can be, there is always the possibility that the measurement list will be out of date at the time of reboot.

To address this issue, this patch series also introduces kexec_update_segment, which allows a reboot notifier to change the contents of the image segment during the reboot process.

The last patch is not intended to be merged, it just demonstrates how this feature can be used.

This series applies on top of v8 of the "kexec_file_load implementation for PowerPC" patch series (which applies on top of v4.8-rc5 and -rc6):

https://lists.infradead.org/pipermail/kexec/2016-September/017123.html

Changes for v5:
- Rebased series on kexec_file_load patch series v8.
- Patch "kexec_file: Include the purgatory segment in the kexec image checksum."
- New patch.
- Patch "kexec_file: Allow skipping checksum calculation for some segments."
- Dropped patch.
- Patch "kexec_file: Add mechanism to update kexec segments."
- Mostly rewritten.
- Verify the kexec image checksum before updating the segment, and calculate the new checksum afterwards.

Changes for v4:
- Rebased series on kexec_file_load patch series v7.
- Patch "powerpc: kexec_file: Add buffer hand-over support for the next kernel"
- Convert hand-over buffer address to physical address when calling memblock_free in kexec_free_handover_buffer.
- Delete hand-over buffer properties from the live device tree in kexec_free_handover_buffer.
- Remove the memory reservation and the properties for the hand-over buffer received from the previous kernel in setup_handover_buffer.
- Patch "IMA: Demonstration code for kexec buffer passing."
- Fix checkpatch warnings. (Andrew Morton)

Changes for v3:
- Rebased series on kexec_file_load patch series v6.
Both patch series apply cleanly on todays' Linus master branch, except for a few lines of fuzz in arch/powerpc/Makefile and arch/powerpc/Kconfig.
- Patch "kexec_file: Add buffer hand-over support for the next kernel"
- Fix compilation warning in <linux/kexec.h> by adding a struct kexec_buf forward declaration when CONFIG_KEXEC_FILE=n. (Fenguang Wu)
- Patch "kexec_file: Allow skipping checksum calculation for some segments."
- Substitute checksum argument in kexec_add_buffer with skip_checksum member in struct kexec_buf, as suggested by Dave Young.
- Patch "kexec_file: Add mechanism to update kexec segments."
- Use kmap_atomic in kexec_update_segment, as suggested by Andrew Morton.
- Fix build warning on m68k by passing unsigned long value to __va instead of void *. (Fenguang Wu)
- Change bufsz and memsz arguments of kexec_update_segment to size_t to fix compilation warning. (Fenguang Wu)
- Patch "kexec: Share logic to copy segment page contents."
- Dropped this patch.
- Patch "IMA: Demonstration code for kexec buffer passing."
- Update to use kexec_buf.skip_checksum instead of passing it in kexec_add_buffer.

Changes for v2:
- Rebased on v5 of kexec_file_load implementation for PowerPC patch series.
- Patch "kexec_file: Add buffer hand-over support for the next kernel"
- Changed kexec_add_handover_buffer to receive a struct kexec_buf, as suggested by Dave Young.
- Patch "powerpc: kexec_file: Add buffer hand-over support for the next kernel"
- Moved setup_handover_buffer from kexec_elf_64.c to machine_kexec_64.c.
- Call setup_handover_buffer from setup_new_fdt instead of elf64_load.
- Changed kexec_get_handover_buffer to read from the expanded device tree instead of the flattened device tree.
- Patch "kexec_file: Add mechanism to update kexec segments.":
- Removed unnecessary "#include <linux/highmem.h>" in kexec_file.c.
- Round up memsz argument to PAGE_SIZE.
- Check if kexec_image is NULL in kexec_update_segment.
- Patch "IMA: Demonstration code for kexec buffer passing."
- Avoid registering reboot notifier again if kexec_file_load is called more than once.

Thiago Jung Bauermann (5):
kexec_file: Include the purgatory segment in the kexec image checksum.
kexec_file: Add buffer hand-over support for the next kernel
powerpc: kexec_file: Add buffer hand-over support for the next kernel
kexec_file: Add mechanism to update kexec segments.
IMA: Demonstration code for kexec buffer passing.

 arch/powerpc/include/asm/kexec.h       |  12 +-
 arch/powerpc/kernel/kexec_elf_64.c     |   2 +-
 arch/powerpc/kernel/machine_kexec_64.c | 274 +++++++++++++++---
 arch/powerpc/purgatory/purgatory.c     |   4 +-
 arch/x86/Kconfig                       |   6 +
 arch/x86/purgatory/purgatory.c         |   2 +-
 include/linux/ima.h                    |  11 +
 include/linux/kexec.h                  |  39 +++
 kernel/kexec_core.c                    |   5 -
 kernel/kexec_file.c                    | 503 +++++++++++++++++++++++++++++++--
 kernel/kexec_internal.h                |   6 +
 security/integrity/ima/ima.h           |   5 +
 security/integrity/ima/ima_init.c      |  26 ++
 security/integrity/ima/ima_template.c  |  85 ++++++
 14 files changed, 893 insertions(+), 87 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH v5 1/5 kexec_file: Include the purgatory segment in the kexec image checksum.

Currently, the purgatory segment is skipped from the kexec image checksum because it is modified to include the calculated digest.

By putting the digest in a separate kexec segment, we can include the purgatory segment in the kexec image verification since it won't need to be modified anymore.

With this change, the only part of the kexec image that is not covered by the checksum is the digest itself.

Even with the digest stored separately, x86 needs to leave the purgatory segment out of the checksum calculation because it modifies the purgatory code in relocate_kernel. We use CONFIG_ARCH_MODIFIES_KEXEC_PURGATORY to allow the powerpc purgatory to be protected by the checksum while still preserving x86 behavior.

---
 arch/powerpc/purgatory/purgatory.c |   4 +-
 arch/x86/Kconfig                   |   6 +++
 arch/x86/purgatory/purgatory.c     |   2 +-
 include/linux/kexec.h              |   6 +++
 kernel/kexec_file.c                | 100 +++++++++++++++++++++++++------------
 5 files changed, 84 insertions(+), 34 deletions(-)
arch/powerpc/purgatory/purgatory.c +2 -2
index 5b006d685cf2..f19ac3d5a7d5 100644
@@ -17,7 +17,7 @@
 #include "kexec-sha256.h"
 
 struct kexec_sha_region sha_regions[SHA256_REGIONS] = {};
-u8 sha256_digest[SHA256_DIGEST_SIZE] = { 0 };
+u8 *sha256_digest = NULL;
 
 int verify_sha256_digest(void)
 {
@@ -40,7 +40,7 @@ int verify_sha256_digest(void)
 		printf("\n");
 
 		printf("sha256_digest: ");
-		for (i = 0; i < sizeof(sha256_digest); i++)
+		for (i = 0; i < SHA256_DIGEST_SIZE; i++)
 			printf("%hhx ", sha256_digest[i]);
 
 		printf("\n");
arch/x86/Kconfig +6 -0
index 2a1f0ce7c59a..dcd1679f3005 100644
@@ -1792,6 +1792,11 @@ config SECCOMP
 
 source kernel/Kconfig.hz
 
+# x86 needs to relocate the purgatory after the checksum is calculated,
+# therefore the purgatory cannot be part of the kexec image checksum.
+config ARCH_MODIFIES_KEXEC_PURGATORY
+	bool
+
 config KEXEC
 	bool "kexec system call"
 	select KEXEC_CORE
@@ -1812,6 +1817,7 @@ config KEXEC
 config KEXEC_FILE
 	bool "kexec file based system call"
 	select KEXEC_CORE
+	select ARCH_MODIFIES_KEXEC_PURGATORY
 	select BUILD_BIN2C
 	depends on X86_64
 	depends on CRYPTO=y
arch/x86/purgatory/purgatory.c +1 -1
index 25e068ba3382..391c6a66cb03 100644
@@ -22,7 +22,7 @@ unsigned long backup_dest = 0;
 unsigned long backup_src = 0;
 unsigned long backup_sz = 0;
 
-u8 sha256_digest[SHA256_DIGEST_SIZE] = { 0 };
+u8 *sha256_digest = NULL;
 
 struct sha_region sha_regions[16] = {};
 
include/linux/kexec.h +6 -0
index d419d0e51fe5..2a96292ee544 100644
@@ -124,8 +124,14 @@ struct purgatory_info {
 	 */
 	void *purgatory_buf;
 
+	/* Digest of the contents of segments. */
+	void *digest_buf;
+
 	/* Address where purgatory is finally loaded and is executed from */
 	unsigned long purgatory_load_addr;
+
+	/* Address where the digest is loaded. */
+	unsigned long digest_load_addr;
 };
 
 typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size);
kernel/kexec_file.c +69 -31

150 lines not on this page · raw message

Signature · 1 line
1.9.1
#5 lore raw

PATCH v5 2/5 kexec_file: Add buffer hand-over support for the next kernel

In reply to Thiago Jung Bauermann

The buffer hand-over mechanism allows the currently running kernel to pass data to kernel that will be kexec'd via a kexec segment. The second kernel can check whether the previous kernel sent data and retrieve it.

This is the architecture-independent part of the feature.

---
 include/linux/kexec.h | 31 +++++++++++++++++++++++
 kernel/kexec_file.c   | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+)
include/linux/kexec.h +31 -0
index 2a96292ee544..768245aa76bf 100644
@@ -389,6 +389,37 @@ static inline void *boot_phys_to_virt(unsigned long entry)
 	return phys_to_virt(boot_phys_to_phys(entry));
 }
 
+#ifdef CONFIG_KEXEC_FILE
+bool __weak kexec_can_hand_over_buffer(void);
+int __weak arch_kexec_add_handover_buffer(struct kimage *image,
+					  unsigned long load_addr,
+					  unsigned long size);
+int kexec_add_handover_buffer(struct kexec_buf *kbuf);
+int __weak kexec_get_handover_buffer(void **addr, unsigned long *size);
+int __weak kexec_free_handover_buffer(void);
+#else
+struct kexec_buf;
+
+static inline bool kexec_can_hand_over_buffer(void)
+{
+	return false;
+}
+
+static inline int kexec_add_handover_buffer(struct kexec_buf *kbuf)
+{
+	return -ENOTSUPP;
+}
+
+static inline int kexec_get_handover_buffer(void **addr, unsigned long *size)
+{
+	return -ENOTSUPP;
+}
+
+static inline int kexec_free_handover_buffer(void)
+{
+	return -ENOTSUPP;
+}
+#endif /* CONFIG_KEXEC_FILE */
 #else /* !CONFIG_KEXEC_CORE */
 struct pt_regs;
 struct task_struct;
kernel/kexec_file.c +68 -0

74 lines not on this page · raw message

Signature · 1 line
1.9.1
#6 lore raw

PATCH v5 3/5 powerpc: kexec_file: Add buffer hand-over support for the next kernel

In reply to Thiago Jung Bauermann

The buffer hand-over mechanism allows the currently running kernel to pass data to kernel that will be kexec'd via a kexec segment. The second kernel can check whether the previous kernel sent data and retrieve it.

This is the architecture-specific part.

---
 arch/powerpc/include/asm/kexec.h       |  12 +-
 arch/powerpc/kernel/kexec_elf_64.c     |   2 +-
 arch/powerpc/kernel/machine_kexec_64.c | 274 +++++++++++++++++++++++++++------
 3 files changed, 240 insertions(+), 48 deletions(-)
arch/powerpc/include/asm/kexec.h +10 -2
index 73f88b5f9bd1..b8e32194ce63 100644
@@ -92,12 +92,20 @@ static inline bool kdump_in_progress(void)
 }
 
 #ifdef CONFIG_KEXEC_FILE
+#define ARCH_HAS_KIMAGE_ARCH
+
+struct kimage_arch {
+	phys_addr_t handover_buffer_addr;
+	unsigned long handover_buffer_size;
+};
+
 int setup_purgatory(struct kimage *image, const void *slave_code,
 		    const void *fdt, unsigned long kernel_load_addr,
 		    unsigned long fdt_load_addr, unsigned long stack_top,
 		    int debug);
-int setup_new_fdt(void *fdt, unsigned long initrd_load_addr,
-		  unsigned long initrd_len, const char *cmdline);
+int setup_new_fdt(const struct kimage *image, void *fdt,
+		  unsigned long initrd_load_addr, unsigned long initrd_len,
+		  const char *cmdline);
 bool find_debug_console(const void *fdt);
 #endif /* CONFIG_KEXEC_FILE */
 
arch/powerpc/kernel/kexec_elf_64.c +1 -1
index 3cc8ebce1a86..0c576e300384 100644
@@ -208,7 +208,7 @@ void *elf64_load(struct kimage *image, char *kernel_buf,
 		goto out;
 	}
 
-	ret = setup_new_fdt(fdt, initrd_load_addr, initrd_len, cmdline);
+	ret = setup_new_fdt(image, fdt, initrd_load_addr, initrd_len, cmdline);
 	if (ret)
 		goto out;
 
arch/powerpc/kernel/machine_kexec_64.c +229 -45

323 lines not on this page · raw message

Signature · 1 line
1.9.1
#7 lore raw

PATCH v5 4/5 kexec_file: Add mechanism to update kexec segments.

In reply to Thiago Jung Bauermann

kexec_update_segment allows a given segment in kexec_image to have its contents updated. This is useful if the current kernel wants to send information to the next kernel that is up-to-date at the time of reboot.

Before modifying the segment the image checksum is verified, and after the segment is updated the checksum is recalculated and updated in the kexec image.

---
 include/linux/kexec.h   |   2 +
 kernel/kexec_core.c     |   5 -
 kernel/kexec_file.c     | 331 ++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/kexec_internal.h |   6 +
 4 files changed, 339 insertions(+), 5 deletions(-)
include/linux/kexec.h +2 -0
index 768245aa76bf..81aca6acc3b0 100644
@@ -183,6 +183,8 @@ int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf,
 			       int (*func)(u64, u64, void *));
 extern int kexec_add_buffer(struct kexec_buf *kbuf);
 int kexec_locate_mem_hole(struct kexec_buf *kbuf);
+int kexec_update_segment(const char *buffer, size_t bufsz,
+			 unsigned long load_addr, size_t memsz);
 #endif /* CONFIG_KEXEC_FILE */
 
 struct kimage {
kernel/kexec_core.c +0 -5
index 561675589511..a86596984454 100644
@@ -551,11 +551,6 @@ void kimage_terminate(struct kimage *image)
 	*image->entry = IND_DONE;
 }
 
-#define for_each_kimage_entry(image, ptr, entry) \
-	for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
-		ptr = (entry & IND_INDIRECTION) ? \
-			boot_phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
-
 static void kimage_free_entry(kimage_entry_t entry)
 {
 	struct page *page;
kernel/kexec_file.c +331 -0

343 lines not on this page · raw message

kernel/kexec_internal.h +6 -0
index 4cef7e4706b0..9bb05a08e31a 100644
@@ -14,6 +14,12 @@ int kimage_is_destination_range(struct kimage *image,
 
 extern struct mutex kexec_mutex;
 
+#define for_each_kimage_entry(image, ptr, entry) \
+	for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
+		ptr = (entry & IND_INDIRECTION) ? \
+			boot_phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
+
+
 #ifdef CONFIG_KEXEC_FILE
 struct kexec_sha_region {
 	unsigned long start;
Signature · 1 line
1.9.1
#8 lore raw

PATCH v5 5/5 IMA: Demonstration code for kexec buffer passing.

In reply to Thiago Jung Bauermann

This shows how kernel code can use the kexec buffer passing mechanism to pass information to the next kernel.

This patch is not intended to be committed.

---
 include/linux/ima.h                   | 11 +++++
 kernel/kexec_file.c                   |  4 ++
 security/integrity/ima/ima.h          |  5 +++
 security/integrity/ima/ima_init.c     | 26 +++++++++++
 security/integrity/ima/ima_template.c | 85 +++++++++++++++++++++++++++++++++++
 5 files changed, 131 insertions(+)
include/linux/ima.h +11 -0
index 0eb7c2e7f0d6..96528d007139 100644
@@ -11,6 +11,7 @@
 #define _LINUX_IMA_H
 
 #include <linux/fs.h>
+#include <linux/kexec.h>
 struct linux_binprm;
 
 #ifdef CONFIG_IMA
@@ -23,6 +24,10 @@ extern int ima_post_read_file(struct file *file, void *buf, loff_t size,
 			      enum kernel_read_file_id id);
 extern void ima_post_path_mknod(struct dentry *dentry);
 
+#ifdef CONFIG_KEXEC_FILE
+extern void ima_add_kexec_buffer(struct kimage *image);
+#endif
+
 #else
 static inline int ima_bprm_check(struct linux_binprm *bprm)
 {
@@ -60,6 +65,12 @@ static inline void ima_post_path_mknod(struct dentry *dentry)
 	return;
 }
 
+#ifdef CONFIG_KEXEC_FILE
+static inline void ima_add_kexec_buffer(struct kimage *image)
+{
+}
+#endif
+
 #endif /* CONFIG_IMA */
 
 #ifdef CONFIG_IMA_APPRAISE
kernel/kexec_file.c +4 -0
index fbcec07bb3f5..0146619479a6 100644
@@ -20,6 +20,7 @@
 #include <linux/list.h>
 #include <linux/highmem.h>
 #include <linux/fs.h>
+#include <linux/ima.h>
 #include <crypto/hash.h>
 #include <crypto/sha.h>
 #include <linux/syscalls.h>
@@ -252,6 +253,9 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
 		}
 	}
 
+	/* IMA needs to pass the measurement list to the next kernel. */
+	ima_add_kexec_buffer(image);
+
 	/* Call arch image load handlers */
 	ldata = arch_kexec_kernel_image_load(image);
 
security/integrity/ima/ima.h +5 -0
index db25f54a04fe..0334001055d7 100644
@@ -102,6 +102,11 @@ struct ima_queue_entry {
 };
 extern struct list_head ima_measurements;	/* list of all measurements */
 
+#ifdef CONFIG_KEXEC_FILE
+extern void *kexec_buffer;
+extern size_t kexec_buffer_size;
+#endif
+
 /* Internal IMA function definitions */
 int ima_init(void);
 int ima_fs_init(void);
security/integrity/ima/ima_init.c +26 -0
index 32912bd54ead..a1924d0f3b2b 100644
@@ -21,6 +21,7 @@
 #include <linux/scatterlist.h>
 #include <linux/slab.h>
 #include <linux/err.h>
+#include <linux/kexec.h>
 
 #include "ima.h"
 
@@ -104,6 +105,29 @@ void __init ima_load_x509(void)
 }
 #endif
 
+#ifdef CONFIG_KEXEC_FILE
+static void ima_load_kexec_buffer(void)
+{
+	int rc;
+
+	/* Fetch the buffer from the previous kernel, if any. */
+	rc = kexec_get_handover_buffer(&kexec_buffer, &kexec_buffer_size);
+	if (rc == 0) {
+		/* Demonstrate that buffer handover works. */
+		pr_err("kexec buffer contents: %s\n", (char *) kexec_buffer);
+		pr_err("kexec buffer contents after update: %s\n",
+		       (char *) kexec_buffer + 4 * PAGE_SIZE + 10);
+
+		kexec_free_handover_buffer();
+	} else if (rc == -ENOENT)
+		pr_debug("No kexec buffer from the previous kernel.\n");
+	else
+		pr_debug("Error restoring kexec buffer: %d\n", rc);
+}
+#else
+static void ima_load_kexec_buffer(void) { }
+#endif
+
 int __init ima_init(void)
 {
 	u8 pcr_i[TPM_DIGEST_SIZE];
@@ -134,5 +158,7 @@ int __init ima_init(void)
 
 	ima_init_policy();
 
+	ima_load_kexec_buffer();
+
 	return ima_fs_init();
 }
security/integrity/ima/ima_template.c +85 -0

97 lines not on this page · raw message

Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH v5 1/5] kexec_file: Include the purgatory segment in the kexec image checksum.

In reply to Thiago Jung Bauermann

Hi Thiago,

arch/x86/Kconfig config SECCOMP · line 1799

Quoted 13 lines
arch/x86/Kconfig +5 -0
index 2a1f0ce7c59a..dcd1679f3005 100644
@@ -1792,6 +1792,11 @@ config SECCOMP
 
 source kernel/Kconfig.hz
 
+# x86 needs to relocate the purgatory after the checksum is calculated,
+# therefore the purgatory cannot be part of the kexec image checksum.
+config ARCH_MODIFIES_KEXEC_PURGATORY
+	bool
+

The above should probably be in arch/Kconfig (with an appropriately changed comment) since it is used in generic code.

Signature · 2 lines
Cheers,
Stephen Rothwell
#3 lore raw

Re: [PATCH v5 1/5] kexec_file: Include the purgatory segment in the kexec image checksum.

Hello Stephen,

Am Donnerstag, 15 September 2016, 11:43:08 schrieb Stephen Rothwell:

Quoted 10 lines

Hi Thiago,

Quoted 8 lines
arch/x86/Kconfig +0 -0
index 2a1f0ce7c59a..dcd1679f3005 100644
@@ -1792,6 +1792,11 @@ config SECCOMP
 
 source kernel/Kconfig.hz
 
Quoted 8 lines

arch/x86/Kconfig config SECCOMP · line 1799

Quoted 5 lines
arch/x86/Kconfig +5 -0
+# x86 needs to relocate the purgatory after the checksum is calculated,
+# therefore the purgatory cannot be part of the kexec image checksum.
+config ARCH_MODIFIES_KEXEC_PURGATORY
+	bool
+

The above should probably be in arch/Kconfig (with an appropriately changed comment) since it is used in generic code.

Thanks for your quick response! I'll make that change tomorrow and send an updated version of just this patch.

Signature · 3 lines
[]'s
Thiago Jung Bauermann
IBM Linux Technology Center
#4 lore raw

Re: [PATCH v5 1/5] kexec_file: Include the purgatory segment in the kexec image checksum.

In reply to Stephen Rothwell

Hello Stephen,

Am Donnerstag, 15 September 2016, 11:43:08 schrieb Stephen Rothwell:

Quoted 10 lines

Hi Thiago,

Quoted 8 lines
arch/x86/Kconfig +0 -0
index 2a1f0ce7c59a..dcd1679f3005 100644
@@ -1792,6 +1792,11 @@ config SECCOMP
 
 source kernel/Kconfig.hz
 
Quoted 8 lines

arch/x86/Kconfig config SECCOMP · line 1799

Quoted 5 lines
arch/x86/Kconfig +5 -0
+# x86 needs to relocate the purgatory after the checksum is calculated,
+# therefore the purgatory cannot be part of the kexec image checksum.
+config ARCH_MODIFIES_KEXEC_PURGATORY
+	bool
+

The above should probably be in arch/Kconfig (with an appropriately changed comment) since it is used in generic code.

Here is the new version, with that change.

Signature · 33 lines
[]'s
Thiago Jung Bauermann
IBM Linux Technology Center


Subject: [PATCH v5 1/5] kexec_file: Include the purgatory segment in the kexec
 image checksum.

Currently, the purgatory segment is skipped from the kexec image checksum
because it is modified to include the calculated digest.

By putting the digest in a separate kexec segment, we can include the
purgatory segment in the kexec image verification since it won't need
to be modified anymore.

With this change, the only part of the kexec image that is not covered
by the checksum is the digest itself.

Even with the digest stored separately, x86 needs to leave the purgatory
segment out of the checksum calculation because it modifies the purgatory
code in relocate_kernel. We use CONFIG_ARCH_MODIFIES_KEXEC_PURGATORY to
allow the powerpc purgatory to be protected by the checksum while still
preserving x86 behavior.

Signed-off-by: Thiago Jung Bauermann <[email protected]>
---
 arch/Kconfig                       |   8 +++
 arch/powerpc/purgatory/purgatory.c |   4 +-
 arch/x86/Kconfig                   |   1 +
 arch/x86/purgatory/purgatory.c     |   2 +-
 include/linux/kexec.h              |   6 +++
 kernel/kexec_file.c                | 100 +++++++++++++++++++++++++------------
 6 files changed, 87 insertions(+), 34 deletions(-)
arch/Kconfig +8 -0
index fd6e9712af81..b386d6c1d463 100644
@@ -5,6 +5,14 @@
 config KEXEC_CORE
 	bool
 
+#
+# Architectures that need to modify the purgatory segment after the
+# checksum is calculated need to select this option so that it won't
+# be part of the kexec image checksum.
+#
+config ARCH_MODIFIES_KEXEC_PURGATORY
+	bool
+
 config OPROFILE
 	tristate "OProfile system profiling"
 	depends on PROFILING
arch/powerpc/purgatory/purgatory.c +2 -2
index 5b006d685cf2..f19ac3d5a7d5 100644
@@ -17,7 +17,7 @@
 #include "kexec-sha256.h"
 
 struct kexec_sha_region sha_regions[SHA256_REGIONS] = {};
-u8 sha256_digest[SHA256_DIGEST_SIZE] = { 0 };
+u8 *sha256_digest = NULL;
 
 int verify_sha256_digest(void)
 {
@@ -40,7 +40,7 @@ int verify_sha256_digest(void)
 		printf("\n");
 
 		printf("sha256_digest: ");
-		for (i = 0; i < sizeof(sha256_digest); i++)
+		for (i = 0; i < SHA256_DIGEST_SIZE; i++)
 			printf("%hhx ", sha256_digest[i]);
 
 		printf("\n");
arch/x86/Kconfig +1 -0
index 2a1f0ce7c59a..4d34019f7479 100644
@@ -1812,6 +1812,7 @@ config KEXEC
 config KEXEC_FILE
 	bool "kexec file based system call"
 	select KEXEC_CORE
+	select ARCH_MODIFIES_KEXEC_PURGATORY
 	select BUILD_BIN2C
 	depends on X86_64
 	depends on CRYPTO=y
arch/x86/purgatory/purgatory.c +1 -1
index 25e068ba3382..391c6a66cb03 100644
@@ -22,7 +22,7 @@ unsigned long backup_dest = 0;
 unsigned long backup_src = 0;
 unsigned long backup_sz = 0;
 
-u8 sha256_digest[SHA256_DIGEST_SIZE] = { 0 };
+u8 *sha256_digest = NULL;
 
 struct sha_region sha_regions[16] = {};
 
include/linux/kexec.h +6 -0
index d419d0e51fe5..2a96292ee544 100644
@@ -124,8 +124,14 @@ struct purgatory_info {
 	 */
 	void *purgatory_buf;
 
+	/* Digest of the contents of segments. */
+	void *digest_buf;
+
 	/* Address where purgatory is finally loaded and is executed from */
 	unsigned long purgatory_load_addr;
+
+	/* Address where the digest is loaded. */
+	unsigned long digest_load_addr;
 };
 
 typedef int (kexec_probe_t)(const char *kernel_buf, unsigned long kernel_size);
kernel/kexec_file.c +69 -31

150 lines not on this page · raw message

Signature · 1 line
1.9.1