kexec_file: Add buffer hand-over for the next kernel
Subsystems (7)
v3 cover kexec_file: Add buffer hand-over for the next kernel
Hello,
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 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 | 114 +++++++++++++++++++++++++++++++-- include/linux/ima.h | 11 ++++ include/linux/kexec.h | 56 +++++++++++++--- kernel/kexec_core.c | 99 ++++++++++++++++++++++++++++ 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, 472 insertions(+), 25 deletions(-)
Signature · 1 line
1.9.1
PATCH v3 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
@@ -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
PATCH v3 2/5 powerpc: kexec_file: Add buffer hand-over support for the next kernel
In reply to Thiago Jung BauermannThe 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 | 114 +++++++++++++++++++++++++++++++-- 3 files changed, 120 insertions(+), 8 deletions(-)
arch/powerpc/include/asm/kexec.h +10 -2
@@ -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
@@ -209,7 +209,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 +109 -5
138 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v3 3/5 kexec_file: Allow skipping checksum calculation for some segments.
In reply to Thiago Jung BauermannAdd 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
@@ -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
@@ -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
PATCH v3 4/5 kexec_file: Add mechanism to update kexec segments.
In reply to Thiago Jung Bauermannkexec_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 | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+)
include/linux/kexec.h +2 -0
@@ -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 +99 -0
105 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v3 5/5 IMA: Demonstration code for kexec buffer passing.
In reply to Thiago Jung BauermannThis 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
@@ -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
@@ -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
@@ -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
@@ -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
Andrew Morton
maintainerQuoted 38 lines · On Thu, 25 Aug 2016 15:18:26 -0300 Thiago Jung Bauermann…
Hello,
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_loadcall and the actual reboot, IMA needs a way to update the buffer with those additional events before rebooting. One can minimize the interval between thekexec_file_loadand 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.
Quoted 7 lines
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_loadimplementation for PowerPC" patch series (which applies on top of v4.8-rc1):https://lists.infradead.org/pipermail/kexec/2016-August/016960.html
I grabbed these two patch series. I also merged the "IMA:
Demonstration code for kexec buffer passing." demonstration patch just to get things a bit of testing. I assume that once the "ima: carry the measurement list across kexec" series has stabilised, I should drop the demo patch and also grab those? If so, pelase start cc'ing me.
Am Donnerstag, 25 August 2016, 14:12:43 schrieb Andrew Morton:
Quoted 3 lines
I grabbed these two patch series. I also merged the "IMA:
Demonstration code for kexec buffer passing." demonstration patch just to get things a bit of testing.
Thank you very much!
Quoted 3 lines
I assume that once the "ima: carry the
measurement list across kexec" series has stabilised, I should drop the demo patch and also grab those? If so, pelase start cc'ing me.
I'm not sure how Mimi is planning to upstream that series.
Signature · 3 lines
[]'s Thiago Jung Bauermann IBM Linux Technology Center
Quoted 6 lines · Thiago Jung Bauermann
Am Donnerstag, 25 August 2016, 14:12:43 schrieb Andrew Morton:
Quoted 3 lines
I grabbed these two patch series. I also merged the "IMA:
Demonstration code for kexec buffer passing." demonstration patch just to get things a bit of testing.Thank you very much!
Thank you, Andrew.
Quoted 5 lines
Quoted 3 lines
I assume that once the "ima: carry the
measurement list across kexec" series has stabilised, I should drop the demo patch and also grab those? If so, pelase start cc'ing me.I'm not sure how Mimi is planning to upstream that series.
I'll re-post the patches shortly, cc'ing you. It will contain support for saving the binary measurement list on one architecture (eg. little endian) and restoring it on another (eg. big endian).
Mimi