Skip to contentSkip to conversation

kexec_file: Add buffer hand-over for the next kernel

PATCH Superseded Thiago Jung Bauermann 3 participants 12 messages Aug 30, 2016 – Sep 9, 2016

Subsystems (7)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

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

Hello,

The purpose of this new version of the series is to fix a small issue that I found, which is that the kernel doesn't remove the memory reservation for the hand-over buffer it received from the previous kernel in the device tree it sets up for the next kernel. The result is that for each successive kexec, a stale hand-over buffer is left behind, wasting memory.

This is fixed by changes to kexec_free_handover_buffer and setup_handover_buffer in patch 2. The other change is to fix checkpatch warnings in the last patch.

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 v6 of the "kexec_file_load implementation for PowerPC" patch series (which applies on top of v4.8-rc1):

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

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: Add buffer hand-over support for the next kernel
powerpc: kexec_file: Add buffer hand-over support for the next kernel
kexec_file: Allow skipping checksum calculation for some segments.
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 +++++++++++++++++++++++++++------
 include/linux/ima.h                    |  11 ++
 include/linux/kexec.h                  |  56 +++++--
 kernel/kexec_core.c                    |  98 ++++++++++++
 kernel/kexec_file.c                    |  87 ++++++++++-
 security/integrity/ima/ima.h           |   5 +
 security/integrity/ima/ima_init.c      |  26 ++++
 security/integrity/ima/ima_template.c  |  85 ++++++++++
 10 files changed, 591 insertions(+), 65 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH v4 1/5 kexec_file: Add buffer hand-over support for the next kernel

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 d419d0e51fe5..16561e96a6d7 100644
@@ -383,6 +383,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
#2 lore raw

PATCH v4 2/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
#3 lore raw

PATCH v4 3/5 kexec_file: Allow skipping checksum calculation for some segments.

In reply to Thiago Jung Bauermann

Add skip_checksum member to struct kexec_buf to specify whether the corresponding segment should be part of the checksum calculation.

The next patch will add a way to update segments after a kimage is loaded. Segments that will be updated in this way should not be checksummed, otherwise they will cause the purgatory checksum verification to fail when the machine is rebooted.

As a bonus, we don't need to special-case the purgatory segment anymore to avoid checksumming it.

Places using struct kexec_buf get false as the default value for skip_checksum since they all use designated initializers. Therefore, there is no behavior change with this patch and all segments except the purgatory are checksummed.

---
 include/linux/kexec.h | 23 ++++++++++++++---------
 kernel/kexec_file.c   | 15 +++++++--------
 2 files changed, 21 insertions(+), 17 deletions(-)
include/linux/kexec.h +14 -9
index 16561e96a6d7..edadff6c86ff 100644
@@ -100,6 +100,9 @@ struct kexec_segment {
 	size_t bufsz;
 	unsigned long mem;
 	size_t memsz;
+
+	/* Whether this segment is ignored in the checksum calculation. */
+	bool skip_checksum;
 };
 
 #ifdef CONFIG_COMPAT
@@ -151,15 +154,16 @@ struct kexec_file_ops {
 
 /**
  * struct kexec_buf - parameters for finding a place for a buffer in memory
- * @image:	kexec image in which memory to search.
- * @buffer:	Contents which will be copied to the allocated memory.
- * @bufsz:	Size of @buffer.
- * @mem:	On return will have address of the buffer in memory.
- * @memsz:	Size for the buffer in memory.
- * @buf_align:	Minimum alignment needed.
- * @buf_min:	The buffer can't be placed below this address.
- * @buf_max:	The buffer can't be placed above this address.
- * @top_down:	Allocate from top of memory.
+ * @image:		kexec image in which memory to search.
+ * @buffer:		Contents which will be copied to the allocated memory.
+ * @bufsz:		Size of @buffer.
+ * @mem:		On return will have address of the buffer in memory.
+ * @memsz:		Size for the buffer in memory.
+ * @buf_align:		Minimum alignment needed.
+ * @buf_min:		The buffer can't be placed below this address.
+ * @buf_max:		The buffer can't be placed above this address.
+ * @top_down:		Allocate from top of memory.
+ * @skip_checksum:	Don't verify checksum for this buffer in purgatory.
  */
 struct kexec_buf {
 	struct kimage *image;
@@ -171,6 +175,7 @@ struct kexec_buf {
 	unsigned long buf_min;
 	unsigned long buf_max;
 	bool top_down;
+	bool skip_checksum;
 };
 
 int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf,
kernel/kexec_file.c +7 -8
index f5684adfad07..0e90d1446cb0 100644
@@ -584,6 +584,7 @@ int kexec_add_buffer(struct kexec_buf *kbuf)
 	ksegment->bufsz = kbuf->bufsz;
 	ksegment->mem = kbuf->mem;
 	ksegment->memsz = kbuf->memsz;
+	ksegment->skip_checksum = kbuf->skip_checksum;
 	kbuf->image->nr_segments++;
 	return 0;
 }
@@ -598,7 +599,6 @@ static int kexec_calculate_store_digests(struct kimage *image)
 	char *digest;
 	void *zero_buf;
 	struct kexec_sha_region *sha_regions;
-	struct purgatory_info *pi = &image->purgatory_info;
 
 	zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
 	zero_buf_sz = PAGE_SIZE;
@@ -638,11 +638,7 @@ static int kexec_calculate_store_digests(struct kimage *image)
 		struct kexec_segment *ksegment;
 
 		ksegment = &image->segment[i];
-		/*
-		 * Skip purgatory as it will be modified once we put digest
-		 * info in purgatory.
-		 */
-		if (ksegment->kbuf == pi->purgatory_buf)
+		if (ksegment->skip_checksum)
 			continue;
 
 		ret = crypto_shash_update(desc, ksegment->kbuf,
@@ -714,7 +710,7 @@ static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
 	Elf_Shdr *sechdrs = NULL;
 	struct kexec_buf kbuf = { .image = image, .bufsz = 0, .buf_align = 1,
 				  .buf_min = min, .buf_max = max,
-				  .top_down = top_down };
+				  .top_down = top_down, .skip_checksum = true };
 
 	/*
 	 * sechdrs_c points to section headers in purgatory and are read
@@ -819,7 +815,10 @@ static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
 	if (kbuf.buf_align < bss_align)
 		kbuf.buf_align = bss_align;
 
-	/* Add buffer to segment list */
+	/*
+	 * Add buffer to segment list. Don't checksum the segment as
+	 * it will be modified once we put digest info in purgatory.
+	 */
 	ret = kexec_add_buffer(&kbuf);
 	if (ret)
 		goto out;
Signature · 1 line
1.9.1
Nacked-by on v4 3/5 NAK
#4 lore raw

Re: [PATCH v4 3/5] kexec_file: Allow skipping checksum calculation for some segments.

Thiago Jung Bauermann <[email protected]> writes:

2> Add skip_checksum member to struct kexec_buf to specify whether the

Quoted 7 lines

corresponding segment should be part of the checksum calculation.

The next patch will add a way to update segments after a kimage is loaded. Segments that will be updated in this way should not be checksummed, otherwise they will cause the purgatory checksum verification to fail when the machine is rebooted.

Quoted 7 lines

As a bonus, we don't need to special-case the purgatory segment anymore to avoid checksumming it.

Places using struct kexec_buf get false as the default value for skip_checksum since they all use designated initializers. Therefore, there is no behavior change with this patch and all segments except the purgatory are checksummed.

What is the world. This fundamentally makes kexec unsafe to use. If any updates need to be made they should be made before they are part of the entire image checksum.

No way should this be merged anywhere ever.

Quoted 109 lines
---
 include/linux/kexec.h | 23 ++++++++++++++---------
 kernel/kexec_file.c   | 15 +++++++--------
 2 files changed, 21 insertions(+), 17 deletions(-)
include/linux/kexec.h +14 -9
index 16561e96a6d7..edadff6c86ff 100644
@@ -100,6 +100,9 @@ struct kexec_segment {
 	size_t bufsz;
 	unsigned long mem;
 	size_t memsz;
+
+	/* Whether this segment is ignored in the checksum calculation. */
+	bool skip_checksum;
 };
 
 #ifdef CONFIG_COMPAT
@@ -151,15 +154,16 @@ struct kexec_file_ops {
 
 /**
  * struct kexec_buf - parameters for finding a place for a buffer in memory
- * @image:	kexec image in which memory to search.
- * @buffer:	Contents which will be copied to the allocated memory.
- * @bufsz:	Size of @buffer.
- * @mem:	On return will have address of the buffer in memory.
- * @memsz:	Size for the buffer in memory.
- * @buf_align:	Minimum alignment needed.
- * @buf_min:	The buffer can't be placed below this address.
- * @buf_max:	The buffer can't be placed above this address.
- * @top_down:	Allocate from top of memory.
+ * @image:		kexec image in which memory to search.
+ * @buffer:		Contents which will be copied to the allocated memory.
+ * @bufsz:		Size of @buffer.
+ * @mem:		On return will have address of the buffer in memory.
+ * @memsz:		Size for the buffer in memory.
+ * @buf_align:		Minimum alignment needed.
+ * @buf_min:		The buffer can't be placed below this address.
+ * @buf_max:		The buffer can't be placed above this address.
+ * @top_down:		Allocate from top of memory.
+ * @skip_checksum:	Don't verify checksum for this buffer in purgatory.
  */
 struct kexec_buf {
 	struct kimage *image;
@@ -171,6 +175,7 @@ struct kexec_buf {
 	unsigned long buf_min;
 	unsigned long buf_max;
 	bool top_down;
+	bool skip_checksum;
 };
 
 int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf,
kernel/kexec_file.c +7 -8
index f5684adfad07..0e90d1446cb0 100644
@@ -584,6 +584,7 @@ int kexec_add_buffer(struct kexec_buf *kbuf)
 	ksegment->bufsz = kbuf->bufsz;
 	ksegment->mem = kbuf->mem;
 	ksegment->memsz = kbuf->memsz;
+	ksegment->skip_checksum = kbuf->skip_checksum;
 	kbuf->image->nr_segments++;
 	return 0;
 }
@@ -598,7 +599,6 @@ static int kexec_calculate_store_digests(struct kimage *image)
 	char *digest;
 	void *zero_buf;
 	struct kexec_sha_region *sha_regions;
-	struct purgatory_info *pi = &image->purgatory_info;
 
 	zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
 	zero_buf_sz = PAGE_SIZE;
@@ -638,11 +638,7 @@ static int kexec_calculate_store_digests(struct kimage *image)
 		struct kexec_segment *ksegment;
 
 		ksegment = &image->segment[i];
-		/*
-		 * Skip purgatory as it will be modified once we put digest
-		 * info in purgatory.
-		 */
-		if (ksegment->kbuf == pi->purgatory_buf)
+		if (ksegment->skip_checksum)
 			continue;
 
 		ret = crypto_shash_update(desc, ksegment->kbuf,
@@ -714,7 +710,7 @@ static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
 	Elf_Shdr *sechdrs = NULL;
 	struct kexec_buf kbuf = { .image = image, .bufsz = 0, .buf_align = 1,
 				  .buf_min = min, .buf_max = max,
-				  .top_down = top_down };
+				  .top_down = top_down, .skip_checksum = true };
 
 	/*
 	 * sechdrs_c points to section headers in purgatory and are read
@@ -819,7 +815,10 @@ static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
 	if (kbuf.buf_align < bss_align)
 		kbuf.buf_align = bss_align;
 
-	/* Add buffer to segment list */
+	/*
+	 * Add buffer to segment list. Don't checksum the segment as
+	 * it will be modified once we put digest info in purgatory.
+	 */
 	ret = kexec_add_buffer(&kbuf);
 	if (ret)
 		goto out;
#5 lore raw

PATCH v4 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.

---
 include/linux/kexec.h |  2 ++
 kernel/kexec_core.c   | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+)
include/linux/kexec.h +2 -0
index edadff6c86ff..ff3aa93649e2 100644
@@ -256,6 +256,8 @@ extern int kexec_purgatory_get_set_symbol(struct kimage *image,
 					  unsigned int size, bool get_value);
 extern void *kexec_purgatory_get_symbol_addr(struct kimage *image,
 					     const char *name);
+int kexec_update_segment(const char *buffer, size_t bufsz,
+			 unsigned long load_addr, size_t memsz);
 extern void __crash_kexec(struct pt_regs *);
 extern void crash_kexec(struct pt_regs *);
 int kexec_should_crash(struct task_struct *);
kernel/kexec_core.c +98 -0

104 lines not on this page · raw message

Signature · 1 line
1.9.1
#6 lore raw

PATCH v4 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 0e90d1446cb0..75c1b8d67a72 100644
@@ -19,6 +19,7 @@
 #include <linux/mutex.h>
 #include <linux/list.h>
 #include <linux/fs.h>
+#include <linux/ima.h>
 #include <crypto/hash.h>
 #include <crypto/sha.h>
 #include <linux/syscalls.h>
@@ -248,6 +249,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
Nacked-by on the v4 series NAK
#7 lore raw
In reply to Thiago Jung Bauermann
Quoted 11 lines · Thiago Jung Bauermann

Hello,

The purpose of this new version of the series is to fix a small issue that I found, which is that the kernel doesn't remove the memory reservation for the hand-over buffer it received from the previous kernel in the device tree it sets up for the next kernel. The result is that for each successive kexec, a stale hand-over buffer is left behind, wasting memory.

This is fixed by changes to kexec_free_handover_buffer and setup_handover_buffer in patch 2. The other change is to fix checkpatch warnings in the last patch.

This is fundamentally broken. You do not increase the integrity of a system by dropping integrity checks.

No. No. No. No.

Eric

#8 lore raw
Quoted 14 lines · [email protected] (Eric W. Biederman) writes:
Quoted 11 lines · Thiago Jung Bauermann

Hello,

The purpose of this new version of the series is to fix a small issue that I found, which is that the kernel doesn't remove the memory reservation for the hand-over buffer it received from the previous kernel in the device tree it sets up for the next kernel. The result is that for each successive kexec, a stale hand-over buffer is left behind, wasting memory.

This is fixed by changes to kexec_free_handover_buffer and setup_handover_buffer in patch 2. The other change is to fix checkpatch warnings in the last patch.

Quoted 6 lines

This is fundamentally broken. You do not increase the integrity of a system by dropping integrity checks.

No. No. No. No.

To be constructive the way we have handled similiar situations in the past (hotplu memory) is to call kexec_load again.

Eric

#9 lore raw

Am Mittwoch, 07 September 2016, 09:19:40 schrieb Eric W. Biederman:

Quoted 15 lines
Quoted 14 lines · [email protected] (Eric W. Biederman) writes:
Quoted 12 lines · Thiago Jung Bauermann

Hello,

The purpose of this new version of the series is to fix a small issue that I found, which is that the kernel doesn't remove the memory reservation for the hand-over buffer it received from the previous kernel in the device tree it sets up for the next kernel. The result is that for each successive kexec, a stale hand-over buffer is left behind, wasting memory.

This is fixed by changes to kexec_free_handover_buffer and setup_handover_buffer in patch 2. The other change is to fix checkpatch warnings in the last patch.

Quoted 6 lines
Quoted 6 lines

This is fundamentally broken. You do not increase the integrity of a system by dropping integrity checks.

No. No. No. No.

The IMA measurement list can be verified without the need of a checksum over its contents by replaying the PCR extend operations and checking that the result matches the registers in the TPM device. So the fact that it is not part of the kexec segments checksum verification doesn't actually reduce the integrity of the system.

Currently, IMA doesn't perform that verification when it restores the measurement list from the kexec handover buffer, so if you believe it's necessary to do that check at boot time, we could do one of the following:

1. Have IMA replay the PCR extend operations when it restores the measurement list from the handover buffer and validate it against the TPM PCRs, or

2. Have a buffer hash in the ima_kexec_hdr that IMA includes in the handover buffer, and verify the buffer checksum before restoring the measurement list.

What do you think?

Quoted 2 lines

To be constructive the way we have handled similiar situations in the past (hotplu memory) is to call kexec_load again.

Thanks for your suggestion. Unfortunately it's always possible for new measurements to be added to the measurement list between the kexec_file_load and the reboot. We see that happen in practice with system scripts and configuration files that are only read or executed during the reboot process. They are only measured by IMA as a result of the kexec execute.

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

Am Mittwoch, 07 September 2016, 09:19:40 schrieb Eric W. Biederman:

Quoted 20 lines
Quoted 19 lines · [email protected] (Eric W. Biederman) writes:
Quoted 9 lines · Thiago Jung Bauermann

Hello,

The purpose of this new version of the series is to fix a small issue that I found, which is that the kernel doesn't remove the memory reservation for the hand-over buffer it received from the previous kernel in the device tree it sets up for the next kernel. The result is that for each successive kexec, a stale hand-over buffer is left behind, wasting memory.

Quoted 3 lines

This is fixed by changes to kexec_free_handover_buffer and setup_handover_buffer in patch 2. The other change is to fix checkpatch warnings in the last patch.

This is fundamentally broken. You do not increase the integrity of a system by dropping integrity checks.

No. No. No. No.

Quoted 7 lines
Quoted 1 line
Quoted 1 line

The IMA measurement list can be verified without the need of a checksum over its contents by replaying the PCR extend operations and checking that the result matches the registers in the TPM device. So the fact that it is not part of the kexec segments checksum verification doesn't actually reduce the integrity of the system.

Bit flips and errant DMA transfers are the concern here. That happens routinely and can easily result in a corrupt data structure which may be non-trivial to verify.

Quoted 8 lines

Currently, IMA doesn't perform that verification when it restores the measurement list from the kexec handover buffer, so if you believe it's necessary to do that check at boot time, we could do one of the following:

1. Have IMA replay the PCR extend operations when it restores the measurement list from the handover buffer and validate it against the TPM PCRs, or

Quoted 5 lines

2. Have a buffer hash in the ima_kexec_hdr that IMA includes in the handover buffer, and verify the buffer checksum before restoring the measurement list.

What do you think?

I think you are playing very much with fire and I am extremely uncomfortable with the entire concept. I think you are making things more complicated in a way that will allow system to try and start booting when their memory is correct. Which may wind up corrupting someones non-volatile storage.

It makes me doubly nervous that this adds a general purpose facility that is generally not at all reusable.

I have seen malicious actors cause entirely too much damage to be at all comfortable using a data structure before we validate that it was valid before we started booting. This isn't the same case but it is close enough I don't trust someone just splatting data structures. We can't guarantee integrity but we should not bypass best practices either.

Quoted 8 lines
Quoted 2 lines

To be constructive the way we have handled similiar situations in the past (hotplu memory) is to call kexec_load again.

Thanks for your suggestion. Unfortunately it's always possible for new measurements to be added to the measurement list between the kexec_file_load and the reboot. We see that happen in practice with system scripts and configuration files that are only read or executed during the reboot process. They are only measured by IMA as a result of the kexec execute.

If I understand what you are saying correctly I expect things could be setup so that those measurements are forced to happen before kexec load.

Especially in a boot loader context which you described earlier I believe you should have quite a lot of control of the system. Having a facility that fundamentally undermines the design of kexec for a case where someone might do something you have not predicted does not make me comfortable.

Which is to say I don't see why you can't measure things before the kexec_load system call in a tightly controlled setup like a boot loader. Which should make it that in practice no measurements change. I believe that should increase the reliability of the system overall.

Having code in the kernel that updates a buffer that kexec will use after that buffer is loaded in memory honestely gives me the heebie jeebies.

Eric

#11 lore raw
Quoted 31 lines · Eric W. Biederman
Quoted 21 lines · Thiago Jung Bauermann

Am Mittwoch, 07 September 2016, 09:19:40 schrieb Eric W. Biederman:

Quoted 20 lines
Quoted 19 lines · [email protected] (Eric W. Biederman) writes:
Quoted 9 lines · Thiago Jung Bauermann

Hello,

The purpose of this new version of the series is to fix a small issue that I found, which is that the kernel doesn't remove the memory reservation for the hand-over buffer it received from the previous kernel in the device tree it sets up for the next kernel. The result is that for each successive kexec, a stale hand-over buffer is left behind, wasting memory.

Quoted 3 lines

This is fixed by changes to kexec_free_handover_buffer and setup_handover_buffer in patch 2. The other change is to fix checkpatch warnings in the last patch.

This is fundamentally broken. You do not increase the integrity of a system by dropping integrity checks.

No. No. No. No.

Quoted 7 lines
Quoted 1 line
Quoted 1 line

The IMA measurement list can be verified without the need of a checksum over its contents by replaying the PCR extend operations and checking that the result matches the registers in the TPM device. So the fact that it is not part of the kexec segments checksum verification doesn't actually reduce the integrity of the system.

Quoted 3 lines

Bit flips and errant DMA transfers are the concern here. That happens routinely and can easily result in a corrupt data structure which may be non-trivial to verify.

Point taken. Before using the measurement list, the hash needs to be verified. So then the question is when and where that hash should be taken and included.

New measurements can take place between the kexec load and execute. Those measurements would be included in an additional execute of kexec load, as you suggested. There are additional new measurements that are the result of the kexec execute itself, as Thiago pointed out. Having to know a priori what those measurements are, or should be, makes carrying the measurement list across kexec brittle.

One solution, as suggested by Thiago, would be to calculate the measurement list hash, include the hash in the ima_kexec_hdr and verify the hash, before restoring the measurement list. Another option would be to calculate the image hash on kexec load either with a zero filled measurement list segment or without the segment, verify the image hash on kexec execute, before re-calculating the image hash.

Quoted 12 lines
Quoted 12 lines

Currently, IMA doesn't perform that verification when it restores the measurement list from the kexec handover buffer, so if you believe it's necessary to do that check at boot time, we could do one of the following:

1. Have IMA replay the PCR extend operations when it restores the measurement list from the handover buffer and validate it against the TPM PCRs, or

2. Have a buffer hash in the ima_kexec_hdr that IMA includes in the handover buffer, and verify the buffer checksum before restoring the measurement list.

Quoted 7 lines
Quoted 1 line

What do you think?

I think you are playing very much with fire and I am extremely uncomfortable with the entire concept. I think you are making things more complicated in a way that will allow system to try and start booting when their memory is correct. Which may wind up corrupting someones non-volatile storage.

Everything that was previously included in the image hash, before this proposed patch set, is still included in the image hash.

Lets assume for a moment that the measurement list hash is stored in the ima_kexec_hdr. In addition to preventing the measurement list from being restored, we could either allow the reboot without the measurement list to occur or halt the system.

Rebooting without the measurement list would result in the attestation of the system failing.

Quoted 2 lines

It makes me doubly nervous that this adds a general purpose facility that is generally not at all reusable.

Thiago has already agreed to reverting the skip hash bit.

Quoted 25 lines

I have seen malicious actors cause entirely too much damage to be at all comfortable using a data structure before we validate that it was valid before we started booting. This isn't the same case but it is close enough I don't trust someone just splatting data structures. We can't guarantee integrity but we should not bypass best practices either.

Quoted 8 lines
Quoted 2 lines

To be constructive the way we have handled similiar situations in the past (hotplu memory) is to call kexec_load again.

Thanks for your suggestion. Unfortunately it's always possible for new measurements to be added to the measurement list between the kexec_file_load and the reboot. We see that happen in practice with system scripts and configuration files that are only read or executed during the reboot process. They are only measured by IMA as a result of the kexec execute.

If I understand what you are saying correctly I expect things could be setup so that those measurements are forced to happen before kexec load.

Especially in a boot loader context which you described earlier I believe you should have quite a lot of control of the system. Having a facility that fundamentally undermines the design of kexec for a case where someone might do something you have not predicted does not make me comfortable.

Quoted 4 lines

Which is to say I don't see why you can't measure things before the kexec_load system call in a tightly controlled setup like a boot loader. Which should make it that in practice no measurements change. I believe that should increase the reliability of the system overall.

Either we measure everything possible, making the measurement list unnecessarily large, or have a brittle system that could break the attestation of the target system, if any packages add new files/scripts that are accessed on a soft reboot.

Quoted 3 lines

Having code in the kernel that updates a buffer that kexec will use after that buffer is loaded in memory honestely gives me the heebie jeebies.

To address your understandable concerns, we suggested two possible solutions for including and verifying the measurement list hash.
- calculate the image hash on load, verify it on kexec reload, before recalculating the image hash with the measurement list segment.
- defer hashing and verifying the measurement list hash to IMA.

Mimi