diff options
28 files changed, 942 insertions, 47 deletions
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index be2d6d0a03a4..21e2d8863705 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt | |||
| @@ -1441,6 +1441,10 @@ | |||
| 1441 | The builtin appraise policy appraises all files | 1441 | The builtin appraise policy appraises all files |
| 1442 | owned by uid=0. | 1442 | owned by uid=0. |
| 1443 | 1443 | ||
| 1444 | ima_canonical_fmt [IMA] | ||
| 1445 | Use the canonical format for the binary runtime | ||
| 1446 | measurements, instead of host native format. | ||
| 1447 | |||
| 1444 | ima_hash= [IMA] | 1448 | ima_hash= [IMA] |
| 1445 | Format: { md5 | sha1 | rmd160 | sha256 | sha384 | 1449 | Format: { md5 | sha1 | rmd160 | sha256 | sha384 |
| 1446 | | sha512 | ... } | 1450 | | sha512 | ... } |
diff --git a/arch/Kconfig b/arch/Kconfig index 19483aea4bbc..99839c23d453 100644 --- a/arch/Kconfig +++ b/arch/Kconfig | |||
| @@ -5,6 +5,9 @@ | |||
| 5 | config KEXEC_CORE | 5 | config KEXEC_CORE |
| 6 | bool | 6 | bool |
| 7 | 7 | ||
| 8 | config HAVE_IMA_KEXEC | ||
| 9 | bool | ||
| 10 | |||
| 8 | config OPROFILE | 11 | config OPROFILE |
| 9 | tristate "OProfile system profiling" | 12 | tristate "OProfile system profiling" |
| 10 | depends on PROFILING | 13 | depends on PROFILING |
diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h index b71086d25195..bfe632808d77 100644 --- a/arch/arm64/include/asm/memory.h +++ b/arch/arm64/include/asm/memory.h | |||
| @@ -165,6 +165,11 @@ extern u64 kimage_vaddr; | |||
| 165 | /* the offset between the kernel virtual and physical mappings */ | 165 | /* the offset between the kernel virtual and physical mappings */ |
| 166 | extern u64 kimage_voffset; | 166 | extern u64 kimage_voffset; |
| 167 | 167 | ||
| 168 | static inline unsigned long kaslr_offset(void) | ||
| 169 | { | ||
| 170 | return kimage_vaddr - KIMAGE_VADDR; | ||
| 171 | } | ||
| 172 | |||
| 168 | /* | 173 | /* |
| 169 | * Allow all memory at the discovery stage. We will clip it later. | 174 | * Allow all memory at the discovery stage. We will clip it later. |
| 170 | */ | 175 | */ |
diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c index a53f52ac81c6..b051367e2149 100644 --- a/arch/arm64/kernel/setup.c +++ b/arch/arm64/kernel/setup.c | |||
| @@ -338,11 +338,11 @@ subsys_initcall(topology_init); | |||
| 338 | static int dump_kernel_offset(struct notifier_block *self, unsigned long v, | 338 | static int dump_kernel_offset(struct notifier_block *self, unsigned long v, |
| 339 | void *p) | 339 | void *p) |
| 340 | { | 340 | { |
| 341 | u64 const kaslr_offset = kimage_vaddr - KIMAGE_VADDR; | 341 | const unsigned long offset = kaslr_offset(); |
| 342 | 342 | ||
| 343 | if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_offset > 0) { | 343 | if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && offset > 0) { |
| 344 | pr_emerg("Kernel Offset: 0x%llx from 0x%lx\n", | 344 | pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n", |
| 345 | kaslr_offset, KIMAGE_VADDR); | 345 | offset, KIMAGE_VADDR); |
| 346 | } else { | 346 | } else { |
| 347 | pr_emerg("Kernel Offset: disabled\n"); | 347 | pr_emerg("Kernel Offset: disabled\n"); |
| 348 | } | 348 | } |
diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index 3da87e198878..a8ee573fe610 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig | |||
| @@ -469,6 +469,7 @@ config KEXEC | |||
| 469 | config KEXEC_FILE | 469 | config KEXEC_FILE |
| 470 | bool "kexec file based system call" | 470 | bool "kexec file based system call" |
| 471 | select KEXEC_CORE | 471 | select KEXEC_CORE |
| 472 | select HAVE_IMA_KEXEC | ||
| 472 | select BUILD_BIN2C | 473 | select BUILD_BIN2C |
| 473 | depends on PPC64 | 474 | depends on PPC64 |
| 474 | depends on CRYPTO=y | 475 | depends on CRYPTO=y |
diff --git a/arch/powerpc/include/asm/ima.h b/arch/powerpc/include/asm/ima.h new file mode 100644 index 000000000000..2313bdface34 --- /dev/null +++ b/arch/powerpc/include/asm/ima.h | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | #ifndef _ASM_POWERPC_IMA_H | ||
| 2 | #define _ASM_POWERPC_IMA_H | ||
| 3 | |||
| 4 | struct kimage; | ||
| 5 | |||
| 6 | int ima_get_kexec_buffer(void **addr, size_t *size); | ||
| 7 | int ima_free_kexec_buffer(void); | ||
| 8 | |||
| 9 | #ifdef CONFIG_IMA | ||
| 10 | void remove_ima_buffer(void *fdt, int chosen_node); | ||
| 11 | #else | ||
| 12 | static inline void remove_ima_buffer(void *fdt, int chosen_node) {} | ||
| 13 | #endif | ||
| 14 | |||
| 15 | #ifdef CONFIG_IMA_KEXEC | ||
| 16 | int arch_ima_add_kexec_buffer(struct kimage *image, unsigned long load_addr, | ||
| 17 | size_t size); | ||
| 18 | |||
| 19 | int setup_ima_buffer(const struct kimage *image, void *fdt, int chosen_node); | ||
| 20 | #else | ||
| 21 | static inline int setup_ima_buffer(const struct kimage *image, void *fdt, | ||
| 22 | int chosen_node) | ||
| 23 | { | ||
| 24 | remove_ima_buffer(fdt, chosen_node); | ||
| 25 | return 0; | ||
| 26 | } | ||
| 27 | #endif /* CONFIG_IMA_KEXEC */ | ||
| 28 | |||
| 29 | #endif /* _ASM_POWERPC_IMA_H */ | ||
diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h index 6c3b71502fbc..25668bc8cb2a 100644 --- a/arch/powerpc/include/asm/kexec.h +++ b/arch/powerpc/include/asm/kexec.h | |||
| @@ -94,11 +94,22 @@ static inline bool kdump_in_progress(void) | |||
| 94 | #ifdef CONFIG_KEXEC_FILE | 94 | #ifdef CONFIG_KEXEC_FILE |
| 95 | extern struct kexec_file_ops kexec_elf64_ops; | 95 | extern struct kexec_file_ops kexec_elf64_ops; |
| 96 | 96 | ||
| 97 | #ifdef CONFIG_IMA_KEXEC | ||
| 98 | #define ARCH_HAS_KIMAGE_ARCH | ||
| 99 | |||
| 100 | struct kimage_arch { | ||
| 101 | phys_addr_t ima_buffer_addr; | ||
| 102 | size_t ima_buffer_size; | ||
| 103 | }; | ||
| 104 | #endif | ||
| 105 | |||
| 97 | int setup_purgatory(struct kimage *image, const void *slave_code, | 106 | int setup_purgatory(struct kimage *image, const void *slave_code, |
| 98 | const void *fdt, unsigned long kernel_load_addr, | 107 | const void *fdt, unsigned long kernel_load_addr, |
| 99 | unsigned long fdt_load_addr); | 108 | unsigned long fdt_load_addr); |
| 100 | int setup_new_fdt(void *fdt, unsigned long initrd_load_addr, | 109 | int setup_new_fdt(const struct kimage *image, void *fdt, |
| 101 | unsigned long initrd_len, const char *cmdline); | 110 | unsigned long initrd_load_addr, unsigned long initrd_len, |
| 111 | const char *cmdline); | ||
| 112 | int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size); | ||
| 102 | #endif /* CONFIG_KEXEC_FILE */ | 113 | #endif /* CONFIG_KEXEC_FILE */ |
| 103 | 114 | ||
| 104 | #else /* !CONFIG_KEXEC_CORE */ | 115 | #else /* !CONFIG_KEXEC_CORE */ |
diff --git a/arch/powerpc/kernel/Makefile b/arch/powerpc/kernel/Makefile index a3a6047fd395..23f8082d7bfa 100644 --- a/arch/powerpc/kernel/Makefile +++ b/arch/powerpc/kernel/Makefile | |||
| @@ -112,6 +112,10 @@ obj-$(CONFIG_PCI_MSI) += msi.o | |||
| 112 | obj-$(CONFIG_KEXEC_CORE) += machine_kexec.o crash.o \ | 112 | obj-$(CONFIG_KEXEC_CORE) += machine_kexec.o crash.o \ |
| 113 | machine_kexec_$(BITS).o | 113 | machine_kexec_$(BITS).o |
| 114 | obj-$(CONFIG_KEXEC_FILE) += machine_kexec_file_$(BITS).o kexec_elf_$(BITS).o | 114 | obj-$(CONFIG_KEXEC_FILE) += machine_kexec_file_$(BITS).o kexec_elf_$(BITS).o |
| 115 | ifeq ($(CONFIG_HAVE_IMA_KEXEC)$(CONFIG_IMA),yy) | ||
| 116 | obj-y += ima_kexec.o | ||
| 117 | endif | ||
| 118 | |||
| 115 | obj-$(CONFIG_AUDIT) += audit.o | 119 | obj-$(CONFIG_AUDIT) += audit.o |
| 116 | obj64-$(CONFIG_AUDIT) += compat_audit.o | 120 | obj64-$(CONFIG_AUDIT) += compat_audit.o |
| 117 | 121 | ||
diff --git a/arch/powerpc/kernel/ima_kexec.c b/arch/powerpc/kernel/ima_kexec.c new file mode 100644 index 000000000000..5ea42c937ca9 --- /dev/null +++ b/arch/powerpc/kernel/ima_kexec.c | |||
| @@ -0,0 +1,223 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2016 IBM Corporation | ||
| 3 | * | ||
| 4 | * Authors: | ||
| 5 | * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com> | ||
| 6 | * | ||
| 7 | * This program is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU General Public License as published by | ||
| 9 | * the Free Software Foundation; either version 2 of the License, or | ||
| 10 | * (at your option) any later version. | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include <linux/slab.h> | ||
| 14 | #include <linux/kexec.h> | ||
| 15 | #include <linux/of.h> | ||
| 16 | #include <linux/memblock.h> | ||
| 17 | #include <linux/libfdt.h> | ||
| 18 | |||
| 19 | static int get_addr_size_cells(int *addr_cells, int *size_cells) | ||
| 20 | { | ||
| 21 | struct device_node *root; | ||
| 22 | |||
| 23 | root = of_find_node_by_path("/"); | ||
| 24 | if (!root) | ||
| 25 | return -EINVAL; | ||
| 26 | |||
| 27 | *addr_cells = of_n_addr_cells(root); | ||
| 28 | *size_cells = of_n_size_cells(root); | ||
| 29 | |||
| 30 | of_node_put(root); | ||
| 31 | |||
| 32 | return 0; | ||
| 33 | } | ||
| 34 | |||
| 35 | static int do_get_kexec_buffer(const void *prop, int len, unsigned long *addr, | ||
| 36 | size_t *size) | ||
| 37 | { | ||
| 38 | int ret, addr_cells, size_cells; | ||
| 39 | |||
| 40 | ret = get_addr_size_cells(&addr_cells, &size_cells); | ||
| 41 | if (ret) | ||
| 42 | return ret; | ||
| 43 | |||
| 44 | if (len < 4 * (addr_cells + size_cells)) | ||
| 45 | return -ENOENT; | ||
| 46 | |||
| 47 | *addr = of_read_number(prop, addr_cells); | ||
| 48 | *size = of_read_number(prop + 4 * addr_cells, size_cells); | ||
| 49 | |||
| 50 | return 0; | ||
| 51 | } | ||
| 52 | |||
| 53 | /** | ||
| 54 | * ima_get_kexec_buffer - get IMA buffer from the previous kernel | ||
| 55 | * @addr: On successful return, set to point to the buffer contents. | ||
| 56 | * @size: On successful return, set to the buffer size. | ||
| 57 | * | ||
| 58 | * Return: 0 on success, negative errno on error. | ||
| 59 | */ | ||
| 60 | int ima_get_kexec_buffer(void **addr, size_t *size) | ||
| 61 | { | ||
| 62 | int ret, len; | ||
| 63 | unsigned long tmp_addr; | ||
| 64 | size_t tmp_size; | ||
| 65 | const void *prop; | ||
| 66 | |||
| 67 | prop = of_get_property(of_chosen, "linux,ima-kexec-buffer", &len); | ||
| 68 | if (!prop) | ||
| 69 | return -ENOENT; | ||
| 70 | |||
| 71 | ret = do_get_kexec_buffer(prop, len, &tmp_addr, &tmp_size); | ||
| 72 | if (ret) | ||
| 73 | return ret; | ||
| 74 | |||
| 75 | *addr = __va(tmp_addr); | ||
| 76 | *size = tmp_size; | ||
| 77 | |||
| 78 | return 0; | ||
| 79 | } | ||
| 80 | |||
| 81 | /** | ||
| 82 | * ima_free_kexec_buffer - free memory used by the IMA buffer | ||
| 83 | */ | ||
| 84 | int ima_free_kexec_buffer(void) | ||
| 85 | { | ||
| 86 | int ret; | ||
| 87 | unsigned long addr; | ||
| 88 | size_t size; | ||
| 89 | struct property *prop; | ||
| 90 | |||
| 91 | prop = of_find_property(of_chosen, "linux,ima-kexec-buffer", NULL); | ||
| 92 | if (!prop) | ||
| 93 | return -ENOENT; | ||
| 94 | |||
| 95 | ret = do_get_kexec_buffer(prop->value, prop->length, &addr, &size); | ||
| 96 | if (ret) | ||
| 97 | return ret; | ||
| 98 | |||
| 99 | ret = of_remove_property(of_chosen, prop); | ||
| 100 | if (ret) | ||
| 101 | return ret; | ||
| 102 | |||
| 103 | return memblock_free(addr, size); | ||
| 104 | |||
| 105 | } | ||
| 106 | |||
| 107 | /** | ||
| 108 | * remove_ima_buffer - remove the IMA buffer property and reservation from @fdt | ||
| 109 | * | ||
| 110 | * The IMA measurement buffer is of no use to a subsequent kernel, so we always | ||
| 111 | * remove it from the device tree. | ||
| 112 | */ | ||
| 113 | void remove_ima_buffer(void *fdt, int chosen_node) | ||
| 114 | { | ||
| 115 | int ret, len; | ||
| 116 | unsigned long addr; | ||
| 117 | size_t size; | ||
| 118 | const void *prop; | ||
| 119 | |||
| 120 | prop = fdt_getprop(fdt, chosen_node, "linux,ima-kexec-buffer", &len); | ||
| 121 | if (!prop) | ||
| 122 | return; | ||
| 123 | |||
| 124 | ret = do_get_kexec_buffer(prop, len, &addr, &size); | ||
| 125 | fdt_delprop(fdt, chosen_node, "linux,ima-kexec-buffer"); | ||
| 126 | if (ret) | ||
| 127 | return; | ||
| 128 | |||
| 129 | ret = delete_fdt_mem_rsv(fdt, addr, size); | ||
| 130 | if (!ret) | ||
| 131 | pr_debug("Removed old IMA buffer reservation.\n"); | ||
| 132 | } | ||
| 133 | |||
| 134 | #ifdef CONFIG_IMA_KEXEC | ||
| 135 | /** | ||
| 136 | * arch_ima_add_kexec_buffer - do arch-specific steps to add the IMA buffer | ||
| 137 | * | ||
| 138 | * Architectures should use this function to pass on the IMA buffer | ||
| 139 | * information to the next kernel. | ||
| 140 | * | ||
| 141 | * Return: 0 on success, negative errno on error. | ||
| 142 | */ | ||
| 143 | int arch_ima_add_kexec_buffer(struct kimage *image, unsigned long load_addr, | ||
| 144 | size_t size) | ||
| 145 | { | ||
| 146 | image->arch.ima_buffer_addr = load_addr; | ||
| 147 | image->arch.ima_buffer_size = size; | ||
| 148 | |||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | |||
| 152 | static int write_number(void *p, u64 value, int cells) | ||
| 153 | { | ||
| 154 | if (cells == 1) { | ||
| 155 | u32 tmp; | ||
| 156 | |||
| 157 | if (value > U32_MAX) | ||
| 158 | return -EINVAL; | ||
| 159 | |||
| 160 | tmp = cpu_to_be32(value); | ||
| 161 | memcpy(p, &tmp, sizeof(tmp)); | ||
| 162 | } else if (cells == 2) { | ||
| 163 | u64 tmp; | ||
| 164 | |||
| 165 | tmp = cpu_to_be64(value); | ||
| 166 | memcpy(p, &tmp, sizeof(tmp)); | ||
| 167 | } else | ||
| 168 | return -EINVAL; | ||
| 169 | |||
| 170 | return 0; | ||
| 171 | } | ||
| 172 | |||
| 173 | /** | ||
| 174 | * setup_ima_buffer - add IMA buffer information to the fdt | ||
| 175 | * @image: kexec image being loaded. | ||
| 176 | * @fdt: Flattened device tree for the next kernel. | ||
| 177 | * @chosen_node: Offset to the chosen node. | ||
| 178 | * | ||
| 179 | * Return: 0 on success, or negative errno on error. | ||
| 180 | */ | ||
| 181 | int setup_ima_buffer(const struct kimage *image, void *fdt, int chosen_node) | ||
| 182 | { | ||
| 183 | int ret, addr_cells, size_cells, entry_size; | ||
| 184 | u8 value[16]; | ||
| 185 | |||
| 186 | remove_ima_buffer(fdt, chosen_node); | ||
| 187 | if (!image->arch.ima_buffer_size) | ||
| 188 | return 0; | ||
| 189 | |||
| 190 | ret = get_addr_size_cells(&addr_cells, &size_cells); | ||
| 191 | if (ret) | ||
| 192 | return ret; | ||
| 193 | |||
| 194 | entry_size = 4 * (addr_cells + size_cells); | ||
| 195 | |||
| 196 | if (entry_size > sizeof(value)) | ||
| 197 | return -EINVAL; | ||
| 198 | |||
| 199 | ret = write_number(value, image->arch.ima_buffer_addr, addr_cells); | ||
| 200 | if (ret) | ||
| 201 | return ret; | ||
| 202 | |||
| 203 | ret = write_number(value + 4 * addr_cells, image->arch.ima_buffer_size, | ||
| 204 | size_cells); | ||
| 205 | if (ret) | ||
| 206 | return ret; | ||
| 207 | |||
| 208 | ret = fdt_setprop(fdt, chosen_node, "linux,ima-kexec-buffer", value, | ||
| 209 | entry_size); | ||
| 210 | if (ret < 0) | ||
| 211 | return -EINVAL; | ||
| 212 | |||
| 213 | ret = fdt_add_mem_rsv(fdt, image->arch.ima_buffer_addr, | ||
| 214 | image->arch.ima_buffer_size); | ||
| 215 | if (ret) | ||
| 216 | return -EINVAL; | ||
| 217 | |||
| 218 | pr_debug("IMA buffer at 0x%llx, size = 0x%zx\n", | ||
| 219 | image->arch.ima_buffer_addr, image->arch.ima_buffer_size); | ||
| 220 | |||
| 221 | return 0; | ||
| 222 | } | ||
| 223 | #endif /* CONFIG_IMA_KEXEC */ | ||
diff --git a/arch/powerpc/kernel/kexec_elf_64.c b/arch/powerpc/kernel/kexec_elf_64.c index 6acffd34a70f..9a42309b091a 100644 --- a/arch/powerpc/kernel/kexec_elf_64.c +++ b/arch/powerpc/kernel/kexec_elf_64.c | |||
| @@ -627,7 +627,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf, | |||
| 627 | goto out; | 627 | goto out; |
| 628 | } | 628 | } |
| 629 | 629 | ||
| 630 | ret = setup_new_fdt(fdt, initrd_load_addr, initrd_len, cmdline); | 630 | ret = setup_new_fdt(image, fdt, initrd_load_addr, initrd_len, cmdline); |
| 631 | if (ret) | 631 | if (ret) |
| 632 | goto out; | 632 | goto out; |
| 633 | 633 | ||
diff --git a/arch/powerpc/kernel/machine_kexec_file_64.c b/arch/powerpc/kernel/machine_kexec_file_64.c index 7abc8a75ee48..992c0d258e5d 100644 --- a/arch/powerpc/kernel/machine_kexec_file_64.c +++ b/arch/powerpc/kernel/machine_kexec_file_64.c | |||
| @@ -27,6 +27,7 @@ | |||
| 27 | #include <linux/memblock.h> | 27 | #include <linux/memblock.h> |
| 28 | #include <linux/of_fdt.h> | 28 | #include <linux/of_fdt.h> |
| 29 | #include <linux/libfdt.h> | 29 | #include <linux/libfdt.h> |
| 30 | #include <asm/ima.h> | ||
| 30 | 31 | ||
| 31 | #define SLAVE_CODE_SIZE 256 | 32 | #define SLAVE_CODE_SIZE 256 |
| 32 | 33 | ||
| @@ -180,7 +181,7 @@ int setup_purgatory(struct kimage *image, const void *slave_code, | |||
| 180 | * | 181 | * |
| 181 | * Return: 0 on success, or negative errno on error. | 182 | * Return: 0 on success, or negative errno on error. |
| 182 | */ | 183 | */ |
| 183 | static int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size) | 184 | int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size) |
| 184 | { | 185 | { |
| 185 | int i, ret, num_rsvs = fdt_num_mem_rsv(fdt); | 186 | int i, ret, num_rsvs = fdt_num_mem_rsv(fdt); |
| 186 | 187 | ||
| @@ -209,6 +210,7 @@ static int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size | |||
| 209 | 210 | ||
| 210 | /* | 211 | /* |
| 211 | * setup_new_fdt - modify /chosen and memory reservation for the next kernel | 212 | * setup_new_fdt - modify /chosen and memory reservation for the next kernel |
| 213 | * @image: kexec image being loaded. | ||
| 212 | * @fdt: Flattened device tree for the next kernel. | 214 | * @fdt: Flattened device tree for the next kernel. |
| 213 | * @initrd_load_addr: Address where the next initrd will be loaded. | 215 | * @initrd_load_addr: Address where the next initrd will be loaded. |
| 214 | * @initrd_len: Size of the next initrd, or 0 if there will be none. | 216 | * @initrd_len: Size of the next initrd, or 0 if there will be none. |
| @@ -217,8 +219,9 @@ static int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size | |||
| 217 | * | 219 | * |
| 218 | * Return: 0 on success, or negative errno on error. | 220 | * Return: 0 on success, or negative errno on error. |
| 219 | */ | 221 | */ |
| 220 | int setup_new_fdt(void *fdt, unsigned long initrd_load_addr, | 222 | int setup_new_fdt(const struct kimage *image, void *fdt, |
| 221 | unsigned long initrd_len, const char *cmdline) | 223 | unsigned long initrd_load_addr, unsigned long initrd_len, |
| 224 | const char *cmdline) | ||
| 222 | { | 225 | { |
| 223 | int ret, chosen_node; | 226 | int ret, chosen_node; |
| 224 | const void *prop; | 227 | const void *prop; |
| @@ -328,6 +331,12 @@ int setup_new_fdt(void *fdt, unsigned long initrd_load_addr, | |||
| 328 | } | 331 | } |
| 329 | } | 332 | } |
| 330 | 333 | ||
| 334 | ret = setup_ima_buffer(image, fdt, chosen_node); | ||
| 335 | if (ret) { | ||
| 336 | pr_err("Error setting up the new device tree.\n"); | ||
| 337 | return ret; | ||
| 338 | } | ||
| 339 | |||
| 331 | ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0); | 340 | ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0); |
| 332 | if (ret) { | 341 | if (ret) { |
| 333 | pr_err("Error setting up the new device tree.\n"); | 342 | pr_err("Error setting up the new device tree.\n"); |
diff --git a/include/linux/ima.h b/include/linux/ima.h index 0eb7c2e7f0d6..7f6952f8d6aa 100644 --- a/include/linux/ima.h +++ b/include/linux/ima.h | |||
| @@ -11,6 +11,7 @@ | |||
| 11 | #define _LINUX_IMA_H | 11 | #define _LINUX_IMA_H |
| 12 | 12 | ||
| 13 | #include <linux/fs.h> | 13 | #include <linux/fs.h> |
| 14 | #include <linux/kexec.h> | ||
| 14 | struct linux_binprm; | 15 | struct linux_binprm; |
| 15 | 16 | ||
| 16 | #ifdef CONFIG_IMA | 17 | #ifdef CONFIG_IMA |
| @@ -23,6 +24,10 @@ extern int ima_post_read_file(struct file *file, void *buf, loff_t size, | |||
| 23 | enum kernel_read_file_id id); | 24 | enum kernel_read_file_id id); |
| 24 | extern void ima_post_path_mknod(struct dentry *dentry); | 25 | extern void ima_post_path_mknod(struct dentry *dentry); |
| 25 | 26 | ||
| 27 | #ifdef CONFIG_IMA_KEXEC | ||
| 28 | extern void ima_add_kexec_buffer(struct kimage *image); | ||
| 29 | #endif | ||
| 30 | |||
| 26 | #else | 31 | #else |
| 27 | static inline int ima_bprm_check(struct linux_binprm *bprm) | 32 | static inline int ima_bprm_check(struct linux_binprm *bprm) |
| 28 | { | 33 | { |
| @@ -62,6 +67,13 @@ static inline void ima_post_path_mknod(struct dentry *dentry) | |||
| 62 | 67 | ||
| 63 | #endif /* CONFIG_IMA */ | 68 | #endif /* CONFIG_IMA */ |
| 64 | 69 | ||
| 70 | #ifndef CONFIG_IMA_KEXEC | ||
| 71 | struct kimage; | ||
| 72 | |||
| 73 | static inline void ima_add_kexec_buffer(struct kimage *image) | ||
| 74 | {} | ||
| 75 | #endif | ||
| 76 | |||
| 65 | #ifdef CONFIG_IMA_APPRAISE | 77 | #ifdef CONFIG_IMA_APPRAISE |
| 66 | extern void ima_inode_post_setattr(struct dentry *dentry); | 78 | extern void ima_inode_post_setattr(struct dentry *dentry); |
| 67 | extern int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, | 79 | extern int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, |
diff --git a/include/linux/ratelimit.h b/include/linux/ratelimit.h index 57c9e0622a38..56375edf2ed2 100644 --- a/include/linux/ratelimit.h +++ b/include/linux/ratelimit.h | |||
| @@ -77,8 +77,11 @@ extern int ___ratelimit(struct ratelimit_state *rs, const char *func); | |||
| 77 | 77 | ||
| 78 | #ifdef CONFIG_PRINTK | 78 | #ifdef CONFIG_PRINTK |
| 79 | 79 | ||
| 80 | #define WARN_ON_RATELIMIT(condition, state) \ | 80 | #define WARN_ON_RATELIMIT(condition, state) ({ \ |
| 81 | WARN_ON((condition) && __ratelimit(state)) | 81 | bool __rtn_cond = !!(condition); \ |
| 82 | WARN_ON(__rtn_cond && __ratelimit(state)); \ | ||
| 83 | __rtn_cond; \ | ||
| 84 | }) | ||
| 82 | 85 | ||
| 83 | #define WARN_RATELIMIT(condition, format, ...) \ | 86 | #define WARN_RATELIMIT(condition, format, ...) \ |
| 84 | ({ \ | 87 | ({ \ |
diff --git a/kernel/kcov.c b/kernel/kcov.c index cc2fa35ca480..85e5546cd791 100644 --- a/kernel/kcov.c +++ b/kernel/kcov.c | |||
| @@ -19,6 +19,7 @@ | |||
| 19 | #include <linux/debugfs.h> | 19 | #include <linux/debugfs.h> |
| 20 | #include <linux/uaccess.h> | 20 | #include <linux/uaccess.h> |
| 21 | #include <linux/kcov.h> | 21 | #include <linux/kcov.h> |
| 22 | #include <asm/setup.h> | ||
| 22 | 23 | ||
| 23 | /* | 24 | /* |
| 24 | * kcov descriptor (one per opened debugfs file). | 25 | * kcov descriptor (one per opened debugfs file). |
| @@ -73,6 +74,11 @@ void notrace __sanitizer_cov_trace_pc(void) | |||
| 73 | if (mode == KCOV_MODE_TRACE) { | 74 | if (mode == KCOV_MODE_TRACE) { |
| 74 | unsigned long *area; | 75 | unsigned long *area; |
| 75 | unsigned long pos; | 76 | unsigned long pos; |
| 77 | unsigned long ip = _RET_IP_; | ||
| 78 | |||
| 79 | #ifdef CONFIG_RANDOMIZE_BASE | ||
| 80 | ip -= kaslr_offset(); | ||
| 81 | #endif | ||
| 76 | 82 | ||
| 77 | /* | 83 | /* |
| 78 | * There is some code that runs in interrupts but for which | 84 | * There is some code that runs in interrupts but for which |
| @@ -86,7 +92,7 @@ void notrace __sanitizer_cov_trace_pc(void) | |||
| 86 | /* The first word is number of subsequent PCs. */ | 92 | /* The first word is number of subsequent PCs. */ |
| 87 | pos = READ_ONCE(area[0]) + 1; | 93 | pos = READ_ONCE(area[0]) + 1; |
| 88 | if (likely(pos < t->kcov_size)) { | 94 | if (likely(pos < t->kcov_size)) { |
| 89 | area[pos] = _RET_IP_; | 95 | area[pos] = ip; |
| 90 | WRITE_ONCE(area[0], pos); | 96 | WRITE_ONCE(area[0], pos); |
| 91 | } | 97 | } |
| 92 | } | 98 | } |
diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 0c2df7f73792..b56a558e406d 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c | |||
| @@ -19,6 +19,7 @@ | |||
| 19 | #include <linux/mutex.h> | 19 | #include <linux/mutex.h> |
| 20 | #include <linux/list.h> | 20 | #include <linux/list.h> |
| 21 | #include <linux/fs.h> | 21 | #include <linux/fs.h> |
| 22 | #include <linux/ima.h> | ||
| 22 | #include <crypto/hash.h> | 23 | #include <crypto/hash.h> |
| 23 | #include <crypto/sha.h> | 24 | #include <crypto/sha.h> |
| 24 | #include <linux/syscalls.h> | 25 | #include <linux/syscalls.h> |
| @@ -132,6 +133,9 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd, | |||
| 132 | return ret; | 133 | return ret; |
| 133 | image->kernel_buf_len = size; | 134 | image->kernel_buf_len = size; |
| 134 | 135 | ||
| 136 | /* IMA needs to pass the measurement list to the next kernel. */ | ||
| 137 | ima_add_kexec_buffer(image); | ||
| 138 | |||
| 135 | /* Call arch image probe handlers */ | 139 | /* Call arch image probe handlers */ |
| 136 | ret = arch_kexec_kernel_image_probe(image, image->kernel_buf, | 140 | ret = arch_kexec_kernel_image_probe(image, image->kernel_buf, |
| 137 | image->kernel_buf_len); | 141 | image->kernel_buf_len); |
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 7446097f72bd..cb66a4648840 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug | |||
| @@ -26,7 +26,7 @@ config CONSOLE_LOGLEVEL_DEFAULT | |||
| 26 | the kernel bootargs. loglevel=<x> continues to override whatever | 26 | the kernel bootargs. loglevel=<x> continues to override whatever |
| 27 | value is specified here as well. | 27 | value is specified here as well. |
| 28 | 28 | ||
| 29 | Note: This does not affect the log level of un-prefixed prink() | 29 | Note: This does not affect the log level of un-prefixed printk() |
| 30 | usage in the kernel. That is controlled by the MESSAGE_LOGLEVEL_DEFAULT | 30 | usage in the kernel. That is controlled by the MESSAGE_LOGLEVEL_DEFAULT |
| 31 | option. | 31 | option. |
| 32 | 32 | ||
diff --git a/mm/fadvise.c b/mm/fadvise.c index 6c707bfe02fd..a43013112581 100644 --- a/mm/fadvise.c +++ b/mm/fadvise.c | |||
| @@ -139,7 +139,20 @@ SYSCALL_DEFINE4(fadvise64_64, int, fd, loff_t, offset, loff_t, len, int, advice) | |||
| 139 | } | 139 | } |
| 140 | 140 | ||
| 141 | if (end_index >= start_index) { | 141 | if (end_index >= start_index) { |
| 142 | unsigned long count = invalidate_mapping_pages(mapping, | 142 | unsigned long count; |
| 143 | |||
| 144 | /* | ||
| 145 | * It's common to FADV_DONTNEED right after | ||
| 146 | * the read or write that instantiates the | ||
| 147 | * pages, in which case there will be some | ||
| 148 | * sitting on the local LRU cache. Try to | ||
| 149 | * avoid the expensive remote drain and the | ||
| 150 | * second cache tree walk below by flushing | ||
| 151 | * them out right away. | ||
| 152 | */ | ||
| 153 | lru_add_drain(); | ||
| 154 | |||
| 155 | count = invalidate_mapping_pages(mapping, | ||
| 143 | start_index, end_index); | 156 | start_index, end_index); |
| 144 | 157 | ||
| 145 | /* | 158 | /* |
diff --git a/security/integrity/ima/Kconfig b/security/integrity/ima/Kconfig index 5487827fa86c..370eb2f4dd37 100644 --- a/security/integrity/ima/Kconfig +++ b/security/integrity/ima/Kconfig | |||
| @@ -27,6 +27,18 @@ config IMA | |||
| 27 | to learn more about IMA. | 27 | to learn more about IMA. |
| 28 | If unsure, say N. | 28 | If unsure, say N. |
| 29 | 29 | ||
| 30 | config IMA_KEXEC | ||
| 31 | bool "Enable carrying the IMA measurement list across a soft boot" | ||
| 32 | depends on IMA && TCG_TPM && HAVE_IMA_KEXEC | ||
| 33 | default n | ||
| 34 | help | ||
| 35 | TPM PCRs are only reset on a hard reboot. In order to validate | ||
| 36 | a TPM's quote after a soft boot, the IMA measurement list of the | ||
| 37 | running kernel must be saved and restored on boot. | ||
| 38 | |||
| 39 | Depending on the IMA policy, the measurement list can grow to | ||
| 40 | be very large. | ||
| 41 | |||
| 30 | config IMA_MEASURE_PCR_IDX | 42 | config IMA_MEASURE_PCR_IDX |
| 31 | int | 43 | int |
| 32 | depends on IMA | 44 | depends on IMA |
diff --git a/security/integrity/ima/Makefile b/security/integrity/ima/Makefile index 9aeaedad1e2b..29f198bde02b 100644 --- a/security/integrity/ima/Makefile +++ b/security/integrity/ima/Makefile | |||
| @@ -8,4 +8,5 @@ obj-$(CONFIG_IMA) += ima.o | |||
| 8 | ima-y := ima_fs.o ima_queue.o ima_init.o ima_main.o ima_crypto.o ima_api.o \ | 8 | ima-y := ima_fs.o ima_queue.o ima_init.o ima_main.o ima_crypto.o ima_api.o \ |
| 9 | ima_policy.o ima_template.o ima_template_lib.o | 9 | ima_policy.o ima_template.o ima_template_lib.o |
| 10 | ima-$(CONFIG_IMA_APPRAISE) += ima_appraise.o | 10 | ima-$(CONFIG_IMA_APPRAISE) += ima_appraise.o |
| 11 | ima-$(CONFIG_HAVE_IMA_KEXEC) += ima_kexec.o | ||
| 11 | obj-$(CONFIG_IMA_BLACKLIST_KEYRING) += ima_mok.o | 12 | obj-$(CONFIG_IMA_BLACKLIST_KEYRING) += ima_mok.o |
diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h index db25f54a04fe..5e6180a4da7d 100644 --- a/security/integrity/ima/ima.h +++ b/security/integrity/ima/ima.h | |||
| @@ -28,6 +28,10 @@ | |||
| 28 | 28 | ||
| 29 | #include "../integrity.h" | 29 | #include "../integrity.h" |
| 30 | 30 | ||
| 31 | #ifdef CONFIG_HAVE_IMA_KEXEC | ||
| 32 | #include <asm/ima.h> | ||
| 33 | #endif | ||
| 34 | |||
| 31 | enum ima_show_type { IMA_SHOW_BINARY, IMA_SHOW_BINARY_NO_FIELD_LEN, | 35 | enum ima_show_type { IMA_SHOW_BINARY, IMA_SHOW_BINARY_NO_FIELD_LEN, |
| 32 | IMA_SHOW_BINARY_OLD_STRING_FMT, IMA_SHOW_ASCII }; | 36 | IMA_SHOW_BINARY_OLD_STRING_FMT, IMA_SHOW_ASCII }; |
| 33 | enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8 }; | 37 | enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8 }; |
| @@ -81,6 +85,7 @@ struct ima_template_field { | |||
| 81 | 85 | ||
| 82 | /* IMA template descriptor definition */ | 86 | /* IMA template descriptor definition */ |
| 83 | struct ima_template_desc { | 87 | struct ima_template_desc { |
| 88 | struct list_head list; | ||
| 84 | char *name; | 89 | char *name; |
| 85 | char *fmt; | 90 | char *fmt; |
| 86 | int num_fields; | 91 | int num_fields; |
| @@ -102,6 +107,27 @@ struct ima_queue_entry { | |||
| 102 | }; | 107 | }; |
| 103 | extern struct list_head ima_measurements; /* list of all measurements */ | 108 | extern struct list_head ima_measurements; /* list of all measurements */ |
| 104 | 109 | ||
| 110 | /* Some details preceding the binary serialized measurement list */ | ||
| 111 | struct ima_kexec_hdr { | ||
| 112 | u16 version; | ||
| 113 | u16 _reserved0; | ||
| 114 | u32 _reserved1; | ||
| 115 | u64 buffer_size; | ||
| 116 | u64 count; | ||
| 117 | }; | ||
| 118 | |||
| 119 | #ifdef CONFIG_HAVE_IMA_KEXEC | ||
| 120 | void ima_load_kexec_buffer(void); | ||
| 121 | #else | ||
| 122 | static inline void ima_load_kexec_buffer(void) {} | ||
| 123 | #endif /* CONFIG_HAVE_IMA_KEXEC */ | ||
| 124 | |||
| 125 | /* | ||
| 126 | * The default binary_runtime_measurements list format is defined as the | ||
| 127 | * platform native format. The canonical format is defined as little-endian. | ||
| 128 | */ | ||
| 129 | extern bool ima_canonical_fmt; | ||
| 130 | |||
| 105 | /* Internal IMA function definitions */ | 131 | /* Internal IMA function definitions */ |
| 106 | int ima_init(void); | 132 | int ima_init(void); |
| 107 | int ima_fs_init(void); | 133 | int ima_fs_init(void); |
| @@ -122,7 +148,12 @@ int ima_init_crypto(void); | |||
| 122 | void ima_putc(struct seq_file *m, void *data, int datalen); | 148 | void ima_putc(struct seq_file *m, void *data, int datalen); |
| 123 | void ima_print_digest(struct seq_file *m, u8 *digest, u32 size); | 149 | void ima_print_digest(struct seq_file *m, u8 *digest, u32 size); |
| 124 | struct ima_template_desc *ima_template_desc_current(void); | 150 | struct ima_template_desc *ima_template_desc_current(void); |
| 151 | int ima_restore_measurement_entry(struct ima_template_entry *entry); | ||
| 152 | int ima_restore_measurement_list(loff_t bufsize, void *buf); | ||
| 153 | int ima_measurements_show(struct seq_file *m, void *v); | ||
| 154 | unsigned long ima_get_binary_runtime_size(void); | ||
| 125 | int ima_init_template(void); | 155 | int ima_init_template(void); |
| 156 | void ima_init_template_list(void); | ||
| 126 | 157 | ||
| 127 | /* | 158 | /* |
| 128 | * used to protect h_table and sha_table | 159 | * used to protect h_table and sha_table |
diff --git a/security/integrity/ima/ima_crypto.c b/security/integrity/ima/ima_crypto.c index 38f2ed830dd6..802d5d20f36f 100644 --- a/security/integrity/ima/ima_crypto.c +++ b/security/integrity/ima/ima_crypto.c | |||
| @@ -477,11 +477,13 @@ static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data, | |||
| 477 | u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 }; | 477 | u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 }; |
| 478 | u8 *data_to_hash = field_data[i].data; | 478 | u8 *data_to_hash = field_data[i].data; |
| 479 | u32 datalen = field_data[i].len; | 479 | u32 datalen = field_data[i].len; |
| 480 | u32 datalen_to_hash = | ||
| 481 | !ima_canonical_fmt ? datalen : cpu_to_le32(datalen); | ||
| 480 | 482 | ||
| 481 | if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) { | 483 | if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) { |
| 482 | rc = crypto_shash_update(shash, | 484 | rc = crypto_shash_update(shash, |
| 483 | (const u8 *) &field_data[i].len, | 485 | (const u8 *) &datalen_to_hash, |
| 484 | sizeof(field_data[i].len)); | 486 | sizeof(datalen_to_hash)); |
| 485 | if (rc) | 487 | if (rc) |
| 486 | break; | 488 | break; |
| 487 | } else if (strcmp(td->fields[i]->field_id, "n") == 0) { | 489 | } else if (strcmp(td->fields[i]->field_id, "n") == 0) { |
diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c index 3df46906492d..ca303e5d2b94 100644 --- a/security/integrity/ima/ima_fs.c +++ b/security/integrity/ima/ima_fs.c | |||
| @@ -28,6 +28,16 @@ | |||
| 28 | 28 | ||
| 29 | static DEFINE_MUTEX(ima_write_mutex); | 29 | static DEFINE_MUTEX(ima_write_mutex); |
| 30 | 30 | ||
| 31 | bool ima_canonical_fmt; | ||
| 32 | static int __init default_canonical_fmt_setup(char *str) | ||
| 33 | { | ||
| 34 | #ifdef __BIG_ENDIAN | ||
| 35 | ima_canonical_fmt = 1; | ||
| 36 | #endif | ||
| 37 | return 1; | ||
| 38 | } | ||
| 39 | __setup("ima_canonical_fmt", default_canonical_fmt_setup); | ||
| 40 | |||
| 31 | static int valid_policy = 1; | 41 | static int valid_policy = 1; |
| 32 | #define TMPBUFLEN 12 | 42 | #define TMPBUFLEN 12 |
| 33 | static ssize_t ima_show_htable_value(char __user *buf, size_t count, | 43 | static ssize_t ima_show_htable_value(char __user *buf, size_t count, |
| @@ -116,13 +126,13 @@ void ima_putc(struct seq_file *m, void *data, int datalen) | |||
| 116 | * [eventdata length] | 126 | * [eventdata length] |
| 117 | * eventdata[n]=template specific data | 127 | * eventdata[n]=template specific data |
| 118 | */ | 128 | */ |
| 119 | static int ima_measurements_show(struct seq_file *m, void *v) | 129 | int ima_measurements_show(struct seq_file *m, void *v) |
| 120 | { | 130 | { |
| 121 | /* the list never shrinks, so we don't need a lock here */ | 131 | /* the list never shrinks, so we don't need a lock here */ |
| 122 | struct ima_queue_entry *qe = v; | 132 | struct ima_queue_entry *qe = v; |
| 123 | struct ima_template_entry *e; | 133 | struct ima_template_entry *e; |
| 124 | char *template_name; | 134 | char *template_name; |
| 125 | int namelen; | 135 | u32 pcr, namelen, template_data_len; /* temporary fields */ |
| 126 | bool is_ima_template = false; | 136 | bool is_ima_template = false; |
| 127 | int i; | 137 | int i; |
| 128 | 138 | ||
| @@ -139,25 +149,29 @@ static int ima_measurements_show(struct seq_file *m, void *v) | |||
| 139 | * PCR used defaults to the same (config option) in | 149 | * PCR used defaults to the same (config option) in |
| 140 | * little-endian format, unless set in policy | 150 | * little-endian format, unless set in policy |
| 141 | */ | 151 | */ |
| 142 | ima_putc(m, &e->pcr, sizeof(e->pcr)); | 152 | pcr = !ima_canonical_fmt ? e->pcr : cpu_to_le32(e->pcr); |
| 153 | ima_putc(m, &pcr, sizeof(e->pcr)); | ||
| 143 | 154 | ||
| 144 | /* 2nd: template digest */ | 155 | /* 2nd: template digest */ |
| 145 | ima_putc(m, e->digest, TPM_DIGEST_SIZE); | 156 | ima_putc(m, e->digest, TPM_DIGEST_SIZE); |
| 146 | 157 | ||
| 147 | /* 3rd: template name size */ | 158 | /* 3rd: template name size */ |
| 148 | namelen = strlen(template_name); | 159 | namelen = !ima_canonical_fmt ? strlen(template_name) : |
| 160 | cpu_to_le32(strlen(template_name)); | ||
| 149 | ima_putc(m, &namelen, sizeof(namelen)); | 161 | ima_putc(m, &namelen, sizeof(namelen)); |
| 150 | 162 | ||
| 151 | /* 4th: template name */ | 163 | /* 4th: template name */ |
| 152 | ima_putc(m, template_name, namelen); | 164 | ima_putc(m, template_name, strlen(template_name)); |
| 153 | 165 | ||
| 154 | /* 5th: template length (except for 'ima' template) */ | 166 | /* 5th: template length (except for 'ima' template) */ |
| 155 | if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0) | 167 | if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0) |
| 156 | is_ima_template = true; | 168 | is_ima_template = true; |
| 157 | 169 | ||
| 158 | if (!is_ima_template) | 170 | if (!is_ima_template) { |
| 159 | ima_putc(m, &e->template_data_len, | 171 | template_data_len = !ima_canonical_fmt ? e->template_data_len : |
| 160 | sizeof(e->template_data_len)); | 172 | cpu_to_le32(e->template_data_len); |
| 173 | ima_putc(m, &template_data_len, sizeof(e->template_data_len)); | ||
| 174 | } | ||
| 161 | 175 | ||
| 162 | /* 6th: template specific data */ | 176 | /* 6th: template specific data */ |
| 163 | for (i = 0; i < e->template_desc->num_fields; i++) { | 177 | for (i = 0; i < e->template_desc->num_fields; i++) { |
diff --git a/security/integrity/ima/ima_init.c b/security/integrity/ima/ima_init.c index 2ac1f41db5c0..2967d497a665 100644 --- a/security/integrity/ima/ima_init.c +++ b/security/integrity/ima/ima_init.c | |||
| @@ -129,6 +129,8 @@ int __init ima_init(void) | |||
| 129 | if (rc != 0) | 129 | if (rc != 0) |
| 130 | return rc; | 130 | return rc; |
| 131 | 131 | ||
| 132 | ima_load_kexec_buffer(); | ||
| 133 | |||
| 132 | rc = ima_add_boot_aggregate(); /* boot aggregate must be first entry */ | 134 | rc = ima_add_boot_aggregate(); /* boot aggregate must be first entry */ |
| 133 | if (rc != 0) | 135 | if (rc != 0) |
| 134 | return rc; | 136 | return rc; |
diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/ima_kexec.c new file mode 100644 index 000000000000..e473eee913cb --- /dev/null +++ b/security/integrity/ima/ima_kexec.c | |||
| @@ -0,0 +1,168 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2016 IBM Corporation | ||
| 3 | * | ||
| 4 | * Authors: | ||
| 5 | * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com> | ||
| 6 | * Mimi Zohar <zohar@linux.vnet.ibm.com> | ||
| 7 | * | ||
| 8 | * This program is free software; you can redistribute it and/or modify | ||
| 9 | * it under the terms of the GNU General Public License as published by | ||
| 10 | * the Free Software Foundation; either version 2 of the License, or | ||
| 11 | * (at your option) any later version. | ||
| 12 | */ | ||
| 13 | #include <linux/seq_file.h> | ||
| 14 | #include <linux/vmalloc.h> | ||
| 15 | #include <linux/kexec.h> | ||
| 16 | #include "ima.h" | ||
| 17 | |||
| 18 | #ifdef CONFIG_IMA_KEXEC | ||
| 19 | static int ima_dump_measurement_list(unsigned long *buffer_size, void **buffer, | ||
| 20 | unsigned long segment_size) | ||
| 21 | { | ||
| 22 | struct ima_queue_entry *qe; | ||
| 23 | struct seq_file file; | ||
| 24 | struct ima_kexec_hdr khdr; | ||
| 25 | int ret = 0; | ||
| 26 | |||
| 27 | /* segment size can't change between kexec load and execute */ | ||
| 28 | file.buf = vmalloc(segment_size); | ||
| 29 | if (!file.buf) { | ||
| 30 | ret = -ENOMEM; | ||
| 31 | goto out; | ||
| 32 | } | ||
| 33 | |||
| 34 | file.size = segment_size; | ||
| 35 | file.read_pos = 0; | ||
| 36 | file.count = sizeof(khdr); /* reserved space */ | ||
| 37 | |||
| 38 | memset(&khdr, 0, sizeof(khdr)); | ||
| 39 | khdr.version = 1; | ||
| 40 | list_for_each_entry_rcu(qe, &ima_measurements, later) { | ||
| 41 | if (file.count < file.size) { | ||
| 42 | khdr.count++; | ||
| 43 | ima_measurements_show(&file, qe); | ||
| 44 | } else { | ||
| 45 | ret = -EINVAL; | ||
| 46 | break; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | if (ret < 0) | ||
| 51 | goto out; | ||
| 52 | |||
| 53 | /* | ||
| 54 | * fill in reserved space with some buffer details | ||
| 55 | * (eg. version, buffer size, number of measurements) | ||
| 56 | */ | ||
| 57 | khdr.buffer_size = file.count; | ||
| 58 | if (ima_canonical_fmt) { | ||
| 59 | khdr.version = cpu_to_le16(khdr.version); | ||
| 60 | khdr.count = cpu_to_le64(khdr.count); | ||
| 61 | khdr.buffer_size = cpu_to_le64(khdr.buffer_size); | ||
| 62 | } | ||
| 63 | memcpy(file.buf, &khdr, sizeof(khdr)); | ||
| 64 | |||
| 65 | print_hex_dump(KERN_DEBUG, "ima dump: ", DUMP_PREFIX_NONE, | ||
| 66 | 16, 1, file.buf, | ||
| 67 | file.count < 100 ? file.count : 100, true); | ||
| 68 | |||
| 69 | *buffer_size = file.count; | ||
| 70 | *buffer = file.buf; | ||
| 71 | out: | ||
| 72 | if (ret == -EINVAL) | ||
| 73 | vfree(file.buf); | ||
| 74 | return ret; | ||
| 75 | } | ||
| 76 | |||
| 77 | /* | ||
| 78 | * Called during kexec_file_load so that IMA can add a segment to the kexec | ||
| 79 | * image for the measurement list for the next kernel. | ||
| 80 | * | ||
| 81 | * This function assumes that kexec_mutex is held. | ||
| 82 | */ | ||
| 83 | void ima_add_kexec_buffer(struct kimage *image) | ||
| 84 | { | ||
| 85 | struct kexec_buf kbuf = { .image = image, .buf_align = PAGE_SIZE, | ||
| 86 | .buf_min = 0, .buf_max = ULONG_MAX, | ||
| 87 | .top_down = true }; | ||
| 88 | unsigned long binary_runtime_size; | ||
| 89 | |||
| 90 | /* use more understandable variable names than defined in kbuf */ | ||
| 91 | void *kexec_buffer = NULL; | ||
| 92 | size_t kexec_buffer_size; | ||
| 93 | size_t kexec_segment_size; | ||
| 94 | int ret; | ||
| 95 | |||
| 96 | /* | ||
| 97 | * Reserve an extra half page of memory for additional measurements | ||
| 98 | * added during the kexec load. | ||
| 99 | */ | ||
| 100 | binary_runtime_size = ima_get_binary_runtime_size(); | ||
| 101 | if (binary_runtime_size >= ULONG_MAX - PAGE_SIZE) | ||
| 102 | kexec_segment_size = ULONG_MAX; | ||
| 103 | else | ||
| 104 | kexec_segment_size = ALIGN(ima_get_binary_runtime_size() + | ||
| 105 | PAGE_SIZE / 2, PAGE_SIZE); | ||
| 106 | if ((kexec_segment_size == ULONG_MAX) || | ||
| 107 | ((kexec_segment_size >> PAGE_SHIFT) > totalram_pages / 2)) { | ||
| 108 | pr_err("Binary measurement list too large.\n"); | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | |||
| 112 | ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer, | ||
| 113 | kexec_segment_size); | ||
| 114 | if (!kexec_buffer) { | ||
| 115 | pr_err("Not enough memory for the kexec measurement buffer.\n"); | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | kbuf.buffer = kexec_buffer; | ||
| 120 | kbuf.bufsz = kexec_buffer_size; | ||
| 121 | kbuf.memsz = kexec_segment_size; | ||
| 122 | ret = kexec_add_buffer(&kbuf); | ||
| 123 | if (ret) { | ||
| 124 | pr_err("Error passing over kexec measurement buffer.\n"); | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | |||
| 128 | ret = arch_ima_add_kexec_buffer(image, kbuf.mem, kexec_segment_size); | ||
| 129 | if (ret) { | ||
| 130 | pr_err("Error passing over kexec measurement buffer.\n"); | ||
| 131 | return; | ||
| 132 | } | ||
| 133 | |||
| 134 | pr_debug("kexec measurement buffer for the loaded kernel at 0x%lx.\n", | ||
| 135 | kbuf.mem); | ||
| 136 | } | ||
| 137 | #endif /* IMA_KEXEC */ | ||
| 138 | |||
| 139 | /* | ||
| 140 | * Restore the measurement list from the previous kernel. | ||
| 141 | */ | ||
| 142 | void ima_load_kexec_buffer(void) | ||
| 143 | { | ||
| 144 | void *kexec_buffer = NULL; | ||
| 145 | size_t kexec_buffer_size = 0; | ||
| 146 | int rc; | ||
| 147 | |||
| 148 | rc = ima_get_kexec_buffer(&kexec_buffer, &kexec_buffer_size); | ||
| 149 | switch (rc) { | ||
| 150 | case 0: | ||
| 151 | rc = ima_restore_measurement_list(kexec_buffer_size, | ||
| 152 | kexec_buffer); | ||
| 153 | if (rc != 0) | ||
| 154 | pr_err("Failed to restore the measurement list: %d\n", | ||
| 155 | rc); | ||
| 156 | |||
| 157 | ima_free_kexec_buffer(); | ||
| 158 | break; | ||
| 159 | case -ENOTSUPP: | ||
| 160 | pr_debug("Restoring the measurement list not supported\n"); | ||
| 161 | break; | ||
| 162 | case -ENOENT: | ||
| 163 | pr_debug("No measurement list to restore\n"); | ||
| 164 | break; | ||
| 165 | default: | ||
| 166 | pr_debug("Error restoring the measurement list: %d\n", rc); | ||
| 167 | } | ||
| 168 | } | ||
diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c index 423d111b3b94..50818c60538b 100644 --- a/security/integrity/ima/ima_main.c +++ b/security/integrity/ima/ima_main.c | |||
| @@ -418,6 +418,7 @@ static int __init init_ima(void) | |||
| 418 | { | 418 | { |
| 419 | int error; | 419 | int error; |
| 420 | 420 | ||
| 421 | ima_init_template_list(); | ||
| 421 | hash_setup(CONFIG_IMA_DEFAULT_HASH); | 422 | hash_setup(CONFIG_IMA_DEFAULT_HASH); |
| 422 | error = ima_init(); | 423 | error = ima_init(); |
| 423 | if (!error) { | 424 | if (!error) { |
diff --git a/security/integrity/ima/ima_queue.c b/security/integrity/ima/ima_queue.c index 32f6ac0f96df..d9aa5ab71204 100644 --- a/security/integrity/ima/ima_queue.c +++ b/security/integrity/ima/ima_queue.c | |||
| @@ -29,6 +29,11 @@ | |||
| 29 | #define AUDIT_CAUSE_LEN_MAX 32 | 29 | #define AUDIT_CAUSE_LEN_MAX 32 |
| 30 | 30 | ||
| 31 | LIST_HEAD(ima_measurements); /* list of all measurements */ | 31 | LIST_HEAD(ima_measurements); /* list of all measurements */ |
| 32 | #ifdef CONFIG_IMA_KEXEC | ||
| 33 | static unsigned long binary_runtime_size; | ||
| 34 | #else | ||
| 35 | static unsigned long binary_runtime_size = ULONG_MAX; | ||
| 36 | #endif | ||
| 32 | 37 | ||
| 33 | /* key: inode (before secure-hashing a file) */ | 38 | /* key: inode (before secure-hashing a file) */ |
| 34 | struct ima_h_table ima_htable = { | 39 | struct ima_h_table ima_htable = { |
| @@ -64,12 +69,32 @@ static struct ima_queue_entry *ima_lookup_digest_entry(u8 *digest_value, | |||
| 64 | return ret; | 69 | return ret; |
| 65 | } | 70 | } |
| 66 | 71 | ||
| 72 | /* | ||
| 73 | * Calculate the memory required for serializing a single | ||
| 74 | * binary_runtime_measurement list entry, which contains a | ||
| 75 | * couple of variable length fields (e.g template name and data). | ||
| 76 | */ | ||
| 77 | static int get_binary_runtime_size(struct ima_template_entry *entry) | ||
| 78 | { | ||
| 79 | int size = 0; | ||
| 80 | |||
| 81 | size += sizeof(u32); /* pcr */ | ||
| 82 | size += sizeof(entry->digest); | ||
| 83 | size += sizeof(int); /* template name size field */ | ||
| 84 | size += strlen(entry->template_desc->name) + 1; | ||
| 85 | size += sizeof(entry->template_data_len); | ||
| 86 | size += entry->template_data_len; | ||
| 87 | return size; | ||
| 88 | } | ||
| 89 | |||
| 67 | /* ima_add_template_entry helper function: | 90 | /* ima_add_template_entry helper function: |
| 68 | * - Add template entry to measurement list and hash table. | 91 | * - Add template entry to the measurement list and hash table, for |
| 92 | * all entries except those carried across kexec. | ||
| 69 | * | 93 | * |
| 70 | * (Called with ima_extend_list_mutex held.) | 94 | * (Called with ima_extend_list_mutex held.) |
| 71 | */ | 95 | */ |
| 72 | static int ima_add_digest_entry(struct ima_template_entry *entry) | 96 | static int ima_add_digest_entry(struct ima_template_entry *entry, |
| 97 | bool update_htable) | ||
| 73 | { | 98 | { |
| 74 | struct ima_queue_entry *qe; | 99 | struct ima_queue_entry *qe; |
| 75 | unsigned int key; | 100 | unsigned int key; |
| @@ -85,11 +110,34 @@ static int ima_add_digest_entry(struct ima_template_entry *entry) | |||
| 85 | list_add_tail_rcu(&qe->later, &ima_measurements); | 110 | list_add_tail_rcu(&qe->later, &ima_measurements); |
| 86 | 111 | ||
| 87 | atomic_long_inc(&ima_htable.len); | 112 | atomic_long_inc(&ima_htable.len); |
| 88 | key = ima_hash_key(entry->digest); | 113 | if (update_htable) { |
| 89 | hlist_add_head_rcu(&qe->hnext, &ima_htable.queue[key]); | 114 | key = ima_hash_key(entry->digest); |
| 115 | hlist_add_head_rcu(&qe->hnext, &ima_htable.queue[key]); | ||
| 116 | } | ||
| 117 | |||
| 118 | if (binary_runtime_size != ULONG_MAX) { | ||
| 119 | int size; | ||
| 120 | |||
| 121 | size = get_binary_runtime_size(entry); | ||
| 122 | binary_runtime_size = (binary_runtime_size < ULONG_MAX - size) ? | ||
| 123 | binary_runtime_size + size : ULONG_MAX; | ||
| 124 | } | ||
| 90 | return 0; | 125 | return 0; |
| 91 | } | 126 | } |
| 92 | 127 | ||
| 128 | /* | ||
| 129 | * Return the amount of memory required for serializing the | ||
| 130 | * entire binary_runtime_measurement list, including the ima_kexec_hdr | ||
| 131 | * structure. | ||
| 132 | */ | ||
| 133 | unsigned long ima_get_binary_runtime_size(void) | ||
| 134 | { | ||
| 135 | if (binary_runtime_size >= (ULONG_MAX - sizeof(struct ima_kexec_hdr))) | ||
| 136 | return ULONG_MAX; | ||
| 137 | else | ||
| 138 | return binary_runtime_size + sizeof(struct ima_kexec_hdr); | ||
| 139 | }; | ||
| 140 | |||
| 93 | static int ima_pcr_extend(const u8 *hash, int pcr) | 141 | static int ima_pcr_extend(const u8 *hash, int pcr) |
| 94 | { | 142 | { |
| 95 | int result = 0; | 143 | int result = 0; |
| @@ -103,8 +151,13 @@ static int ima_pcr_extend(const u8 *hash, int pcr) | |||
| 103 | return result; | 151 | return result; |
| 104 | } | 152 | } |
| 105 | 153 | ||
| 106 | /* Add template entry to the measurement list and hash table, | 154 | /* |
| 107 | * and extend the pcr. | 155 | * Add template entry to the measurement list and hash table, and |
| 156 | * extend the pcr. | ||
| 157 | * | ||
| 158 | * On systems which support carrying the IMA measurement list across | ||
| 159 | * kexec, maintain the total memory size required for serializing the | ||
| 160 | * binary_runtime_measurements. | ||
| 108 | */ | 161 | */ |
| 109 | int ima_add_template_entry(struct ima_template_entry *entry, int violation, | 162 | int ima_add_template_entry(struct ima_template_entry *entry, int violation, |
| 110 | const char *op, struct inode *inode, | 163 | const char *op, struct inode *inode, |
| @@ -126,7 +179,7 @@ int ima_add_template_entry(struct ima_template_entry *entry, int violation, | |||
| 126 | } | 179 | } |
| 127 | } | 180 | } |
| 128 | 181 | ||
| 129 | result = ima_add_digest_entry(entry); | 182 | result = ima_add_digest_entry(entry, 1); |
| 130 | if (result < 0) { | 183 | if (result < 0) { |
| 131 | audit_cause = "ENOMEM"; | 184 | audit_cause = "ENOMEM"; |
| 132 | audit_info = 0; | 185 | audit_info = 0; |
| @@ -149,3 +202,13 @@ out: | |||
| 149 | op, audit_cause, result, audit_info); | 202 | op, audit_cause, result, audit_info); |
| 150 | return result; | 203 | return result; |
| 151 | } | 204 | } |
| 205 | |||
| 206 | int ima_restore_measurement_entry(struct ima_template_entry *entry) | ||
| 207 | { | ||
| 208 | int result = 0; | ||
| 209 | |||
| 210 | mutex_lock(&ima_extend_list_mutex); | ||
| 211 | result = ima_add_digest_entry(entry, 0); | ||
| 212 | mutex_unlock(&ima_extend_list_mutex); | ||
| 213 | return result; | ||
| 214 | } | ||
diff --git a/security/integrity/ima/ima_template.c b/security/integrity/ima/ima_template.c index febd12ed9b55..cebb37c63629 100644 --- a/security/integrity/ima/ima_template.c +++ b/security/integrity/ima/ima_template.c | |||
| @@ -15,16 +15,20 @@ | |||
| 15 | 15 | ||
| 16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | 16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 17 | 17 | ||
| 18 | #include <linux/rculist.h> | ||
| 18 | #include "ima.h" | 19 | #include "ima.h" |
| 19 | #include "ima_template_lib.h" | 20 | #include "ima_template_lib.h" |
| 20 | 21 | ||
| 21 | static struct ima_template_desc defined_templates[] = { | 22 | static struct ima_template_desc builtin_templates[] = { |
| 22 | {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT}, | 23 | {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT}, |
| 23 | {.name = "ima-ng", .fmt = "d-ng|n-ng"}, | 24 | {.name = "ima-ng", .fmt = "d-ng|n-ng"}, |
| 24 | {.name = "ima-sig", .fmt = "d-ng|n-ng|sig"}, | 25 | {.name = "ima-sig", .fmt = "d-ng|n-ng|sig"}, |
| 25 | {.name = "", .fmt = ""}, /* placeholder for a custom format */ | 26 | {.name = "", .fmt = ""}, /* placeholder for a custom format */ |
| 26 | }; | 27 | }; |
| 27 | 28 | ||
| 29 | static LIST_HEAD(defined_templates); | ||
| 30 | static DEFINE_SPINLOCK(template_list); | ||
| 31 | |||
| 28 | static struct ima_template_field supported_fields[] = { | 32 | static struct ima_template_field supported_fields[] = { |
| 29 | {.field_id = "d", .field_init = ima_eventdigest_init, | 33 | {.field_id = "d", .field_init = ima_eventdigest_init, |
| 30 | .field_show = ima_show_template_digest}, | 34 | .field_show = ima_show_template_digest}, |
| @@ -37,6 +41,7 @@ static struct ima_template_field supported_fields[] = { | |||
| 37 | {.field_id = "sig", .field_init = ima_eventsig_init, | 41 | {.field_id = "sig", .field_init = ima_eventsig_init, |
| 38 | .field_show = ima_show_template_sig}, | 42 | .field_show = ima_show_template_sig}, |
| 39 | }; | 43 | }; |
| 44 | #define MAX_TEMPLATE_NAME_LEN 15 | ||
| 40 | 45 | ||
| 41 | static struct ima_template_desc *ima_template; | 46 | static struct ima_template_desc *ima_template; |
| 42 | static struct ima_template_desc *lookup_template_desc(const char *name); | 47 | static struct ima_template_desc *lookup_template_desc(const char *name); |
| @@ -52,6 +57,8 @@ static int __init ima_template_setup(char *str) | |||
| 52 | if (ima_template) | 57 | if (ima_template) |
| 53 | return 1; | 58 | return 1; |
| 54 | 59 | ||
| 60 | ima_init_template_list(); | ||
| 61 | |||
| 55 | /* | 62 | /* |
| 56 | * Verify that a template with the supplied name exists. | 63 | * Verify that a template with the supplied name exists. |
| 57 | * If not, use CONFIG_IMA_DEFAULT_TEMPLATE. | 64 | * If not, use CONFIG_IMA_DEFAULT_TEMPLATE. |
| @@ -80,7 +87,7 @@ __setup("ima_template=", ima_template_setup); | |||
| 80 | 87 | ||
| 81 | static int __init ima_template_fmt_setup(char *str) | 88 | static int __init ima_template_fmt_setup(char *str) |
| 82 | { | 89 | { |
| 83 | int num_templates = ARRAY_SIZE(defined_templates); | 90 | int num_templates = ARRAY_SIZE(builtin_templates); |
| 84 | 91 | ||
| 85 | if (ima_template) | 92 | if (ima_template) |
| 86 | return 1; | 93 | return 1; |
| @@ -91,22 +98,28 @@ static int __init ima_template_fmt_setup(char *str) | |||
| 91 | return 1; | 98 | return 1; |
| 92 | } | 99 | } |
| 93 | 100 | ||
| 94 | defined_templates[num_templates - 1].fmt = str; | 101 | builtin_templates[num_templates - 1].fmt = str; |
| 95 | ima_template = defined_templates + num_templates - 1; | 102 | ima_template = builtin_templates + num_templates - 1; |
| 103 | |||
| 96 | return 1; | 104 | return 1; |
| 97 | } | 105 | } |
| 98 | __setup("ima_template_fmt=", ima_template_fmt_setup); | 106 | __setup("ima_template_fmt=", ima_template_fmt_setup); |
| 99 | 107 | ||
| 100 | static struct ima_template_desc *lookup_template_desc(const char *name) | 108 | static struct ima_template_desc *lookup_template_desc(const char *name) |
| 101 | { | 109 | { |
| 102 | int i; | 110 | struct ima_template_desc *template_desc; |
| 103 | 111 | int found = 0; | |
| 104 | for (i = 0; i < ARRAY_SIZE(defined_templates); i++) { | 112 | |
| 105 | if (strcmp(defined_templates[i].name, name) == 0) | 113 | rcu_read_lock(); |
| 106 | return defined_templates + i; | 114 | list_for_each_entry_rcu(template_desc, &defined_templates, list) { |
| 115 | if ((strcmp(template_desc->name, name) == 0) || | ||
| 116 | (strcmp(template_desc->fmt, name) == 0)) { | ||
| 117 | found = 1; | ||
| 118 | break; | ||
| 119 | } | ||
| 107 | } | 120 | } |
| 108 | 121 | rcu_read_unlock(); | |
| 109 | return NULL; | 122 | return found ? template_desc : NULL; |
| 110 | } | 123 | } |
| 111 | 124 | ||
| 112 | static struct ima_template_field *lookup_template_field(const char *field_id) | 125 | static struct ima_template_field *lookup_template_field(const char *field_id) |
| @@ -142,9 +155,14 @@ static int template_desc_init_fields(const char *template_fmt, | |||
| 142 | { | 155 | { |
| 143 | const char *template_fmt_ptr; | 156 | const char *template_fmt_ptr; |
| 144 | struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX]; | 157 | struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX]; |
| 145 | int template_num_fields = template_fmt_size(template_fmt); | 158 | int template_num_fields; |
| 146 | int i, len; | 159 | int i, len; |
| 147 | 160 | ||
| 161 | if (num_fields && *num_fields > 0) /* already initialized? */ | ||
| 162 | return 0; | ||
| 163 | |||
| 164 | template_num_fields = template_fmt_size(template_fmt); | ||
| 165 | |||
| 148 | if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) { | 166 | if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) { |
| 149 | pr_err("format string '%s' contains too many fields\n", | 167 | pr_err("format string '%s' contains too many fields\n", |
| 150 | template_fmt); | 168 | template_fmt); |
| @@ -182,11 +200,28 @@ static int template_desc_init_fields(const char *template_fmt, | |||
| 182 | return 0; | 200 | return 0; |
| 183 | } | 201 | } |
| 184 | 202 | ||
| 203 | void ima_init_template_list(void) | ||
| 204 | { | ||
| 205 | int i; | ||
| 206 | |||
| 207 | if (!list_empty(&defined_templates)) | ||
| 208 | return; | ||
| 209 | |||
| 210 | spin_lock(&template_list); | ||
| 211 | for (i = 0; i < ARRAY_SIZE(builtin_templates); i++) { | ||
| 212 | list_add_tail_rcu(&builtin_templates[i].list, | ||
| 213 | &defined_templates); | ||
| 214 | } | ||
| 215 | spin_unlock(&template_list); | ||
| 216 | } | ||
| 217 | |||
| 185 | struct ima_template_desc *ima_template_desc_current(void) | 218 | struct ima_template_desc *ima_template_desc_current(void) |
| 186 | { | 219 | { |
| 187 | if (!ima_template) | 220 | if (!ima_template) { |
| 221 | ima_init_template_list(); | ||
| 188 | ima_template = | 222 | ima_template = |
| 189 | lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE); | 223 | lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE); |
| 224 | } | ||
| 190 | return ima_template; | 225 | return ima_template; |
| 191 | } | 226 | } |
| 192 | 227 | ||
| @@ -205,3 +240,239 @@ int __init ima_init_template(void) | |||
| 205 | 240 | ||
| 206 | return result; | 241 | return result; |
| 207 | } | 242 | } |
| 243 | |||
| 244 | static struct ima_template_desc *restore_template_fmt(char *template_name) | ||
| 245 | { | ||
| 246 | struct ima_template_desc *template_desc = NULL; | ||
| 247 | int ret; | ||
| 248 | |||
| 249 | ret = template_desc_init_fields(template_name, NULL, NULL); | ||
| 250 | if (ret < 0) { | ||
| 251 | pr_err("attempting to initialize the template \"%s\" failed\n", | ||
| 252 | template_name); | ||
| 253 | goto out; | ||
| 254 | } | ||
| 255 | |||
| 256 | template_desc = kzalloc(sizeof(*template_desc), GFP_KERNEL); | ||
| 257 | if (!template_desc) | ||
| 258 | goto out; | ||
| 259 | |||
| 260 | template_desc->name = ""; | ||
| 261 | template_desc->fmt = kstrdup(template_name, GFP_KERNEL); | ||
| 262 | if (!template_desc->fmt) | ||
| 263 | goto out; | ||
| 264 | |||
| 265 | spin_lock(&template_list); | ||
| 266 | list_add_tail_rcu(&template_desc->list, &defined_templates); | ||
| 267 | spin_unlock(&template_list); | ||
| 268 | out: | ||
| 269 | return template_desc; | ||
| 270 | } | ||
| 271 | |||
| 272 | static int ima_restore_template_data(struct ima_template_desc *template_desc, | ||
| 273 | void *template_data, | ||
| 274 | int template_data_size, | ||
| 275 | struct ima_template_entry **entry) | ||
| 276 | { | ||
| 277 | struct binary_field_data { | ||
| 278 | u32 len; | ||
| 279 | u8 data[0]; | ||
| 280 | } __packed; | ||
| 281 | |||
| 282 | struct binary_field_data *field_data; | ||
| 283 | int offset = 0; | ||
| 284 | int ret = 0; | ||
| 285 | int i; | ||
| 286 | |||
| 287 | *entry = kzalloc(sizeof(**entry) + | ||
| 288 | template_desc->num_fields * sizeof(struct ima_field_data), | ||
| 289 | GFP_NOFS); | ||
| 290 | if (!*entry) | ||
| 291 | return -ENOMEM; | ||
| 292 | |||
| 293 | (*entry)->template_desc = template_desc; | ||
| 294 | for (i = 0; i < template_desc->num_fields; i++) { | ||
| 295 | field_data = template_data + offset; | ||
| 296 | |||
| 297 | /* Each field of the template data is prefixed with a length. */ | ||
| 298 | if (offset > (template_data_size - sizeof(*field_data))) { | ||
| 299 | pr_err("Restoring the template field failed\n"); | ||
| 300 | ret = -EINVAL; | ||
| 301 | break; | ||
| 302 | } | ||
| 303 | offset += sizeof(*field_data); | ||
| 304 | |||
| 305 | if (ima_canonical_fmt) | ||
| 306 | field_data->len = le32_to_cpu(field_data->len); | ||
| 307 | |||
| 308 | if (offset > (template_data_size - field_data->len)) { | ||
| 309 | pr_err("Restoring the template field data failed\n"); | ||
| 310 | ret = -EINVAL; | ||
| 311 | break; | ||
| 312 | } | ||
| 313 | offset += field_data->len; | ||
| 314 | |||
| 315 | (*entry)->template_data[i].len = field_data->len; | ||
| 316 | (*entry)->template_data_len += sizeof(field_data->len); | ||
| 317 | |||
| 318 | (*entry)->template_data[i].data = | ||
| 319 | kzalloc(field_data->len + 1, GFP_KERNEL); | ||
| 320 | if (!(*entry)->template_data[i].data) { | ||
| 321 | ret = -ENOMEM; | ||
| 322 | break; | ||
| 323 | } | ||
| 324 | memcpy((*entry)->template_data[i].data, field_data->data, | ||
| 325 | field_data->len); | ||
| 326 | (*entry)->template_data_len += field_data->len; | ||
| 327 | } | ||
| 328 | |||
| 329 | if (ret < 0) { | ||
| 330 | ima_free_template_entry(*entry); | ||
| 331 | *entry = NULL; | ||
| 332 | } | ||
| 333 | |||
| 334 | return ret; | ||
| 335 | } | ||
| 336 | |||
| 337 | /* Restore the serialized binary measurement list without extending PCRs. */ | ||
| 338 | int ima_restore_measurement_list(loff_t size, void *buf) | ||
| 339 | { | ||
| 340 | struct binary_hdr_v1 { | ||
| 341 | u32 pcr; | ||
| 342 | u8 digest[TPM_DIGEST_SIZE]; | ||
| 343 | u32 template_name_len; | ||
| 344 | char template_name[0]; | ||
| 345 | } __packed; | ||
| 346 | char template_name[MAX_TEMPLATE_NAME_LEN]; | ||
| 347 | |||
| 348 | struct binary_data_v1 { | ||
| 349 | u32 template_data_size; | ||
| 350 | char template_data[0]; | ||
| 351 | } __packed; | ||
| 352 | |||
| 353 | struct ima_kexec_hdr *khdr = buf; | ||
| 354 | struct binary_hdr_v1 *hdr_v1; | ||
| 355 | struct binary_data_v1 *data_v1; | ||
| 356 | |||
| 357 | void *bufp = buf + sizeof(*khdr); | ||
| 358 | void *bufendp; | ||
| 359 | struct ima_template_entry *entry; | ||
| 360 | struct ima_template_desc *template_desc; | ||
| 361 | unsigned long count = 0; | ||
| 362 | int ret = 0; | ||
| 363 | |||
| 364 | if (!buf || size < sizeof(*khdr)) | ||
| 365 | return 0; | ||
| 366 | |||
| 367 | if (ima_canonical_fmt) { | ||
| 368 | khdr->version = le16_to_cpu(khdr->version); | ||
| 369 | khdr->count = le64_to_cpu(khdr->count); | ||
| 370 | khdr->buffer_size = le64_to_cpu(khdr->buffer_size); | ||
| 371 | } | ||
| 372 | |||
| 373 | if (khdr->version != 1) { | ||
| 374 | pr_err("attempting to restore a incompatible measurement list"); | ||
| 375 | return -EINVAL; | ||
| 376 | } | ||
| 377 | |||
| 378 | if (khdr->count > ULONG_MAX - 1) { | ||
| 379 | pr_err("attempting to restore too many measurements"); | ||
| 380 | return -EINVAL; | ||
| 381 | } | ||
| 382 | |||
| 383 | /* | ||
| 384 | * ima kexec buffer prefix: version, buffer size, count | ||
| 385 | * v1 format: pcr, digest, template-name-len, template-name, | ||
| 386 | * template-data-size, template-data | ||
| 387 | */ | ||
| 388 | bufendp = buf + khdr->buffer_size; | ||
| 389 | while ((bufp < bufendp) && (count++ < khdr->count)) { | ||
| 390 | hdr_v1 = bufp; | ||
| 391 | if (bufp > (bufendp - sizeof(*hdr_v1))) { | ||
| 392 | pr_err("attempting to restore partial measurement\n"); | ||
| 393 | ret = -EINVAL; | ||
| 394 | break; | ||
| 395 | } | ||
| 396 | bufp += sizeof(*hdr_v1); | ||
| 397 | |||
| 398 | if (ima_canonical_fmt) | ||
| 399 | hdr_v1->template_name_len = | ||
| 400 | le32_to_cpu(hdr_v1->template_name_len); | ||
| 401 | |||
| 402 | if ((hdr_v1->template_name_len >= MAX_TEMPLATE_NAME_LEN) || | ||
| 403 | (bufp > (bufendp - hdr_v1->template_name_len))) { | ||
| 404 | pr_err("attempting to restore a template name \ | ||
| 405 | that is too long\n"); | ||
| 406 | ret = -EINVAL; | ||
| 407 | break; | ||
| 408 | } | ||
| 409 | data_v1 = bufp += (u_int8_t)hdr_v1->template_name_len; | ||
| 410 | |||
| 411 | /* template name is not null terminated */ | ||
| 412 | memcpy(template_name, hdr_v1->template_name, | ||
| 413 | hdr_v1->template_name_len); | ||
| 414 | template_name[hdr_v1->template_name_len] = 0; | ||
| 415 | |||
| 416 | if (strcmp(template_name, "ima") == 0) { | ||
| 417 | pr_err("attempting to restore an unsupported \ | ||
| 418 | template \"%s\" failed\n", template_name); | ||
| 419 | ret = -EINVAL; | ||
| 420 | break; | ||
| 421 | } | ||
| 422 | |||
| 423 | template_desc = lookup_template_desc(template_name); | ||
| 424 | if (!template_desc) { | ||
| 425 | template_desc = restore_template_fmt(template_name); | ||
| 426 | if (!template_desc) | ||
| 427 | break; | ||
| 428 | } | ||
| 429 | |||
| 430 | /* | ||
| 431 | * Only the running system's template format is initialized | ||
| 432 | * on boot. As needed, initialize the other template formats. | ||
| 433 | */ | ||
| 434 | ret = template_desc_init_fields(template_desc->fmt, | ||
| 435 | &(template_desc->fields), | ||
| 436 | &(template_desc->num_fields)); | ||
| 437 | if (ret < 0) { | ||
| 438 | pr_err("attempting to restore the template fmt \"%s\" \ | ||
| 439 | failed\n", template_desc->fmt); | ||
| 440 | ret = -EINVAL; | ||
| 441 | break; | ||
| 442 | } | ||
| 443 | |||
| 444 | if (bufp > (bufendp - sizeof(data_v1->template_data_size))) { | ||
| 445 | pr_err("restoring the template data size failed\n"); | ||
| 446 | ret = -EINVAL; | ||
| 447 | break; | ||
| 448 | } | ||
| 449 | bufp += (u_int8_t) sizeof(data_v1->template_data_size); | ||
| 450 | |||
| 451 | if (ima_canonical_fmt) | ||
| 452 | data_v1->template_data_size = | ||
| 453 | le32_to_cpu(data_v1->template_data_size); | ||
| 454 | |||
| 455 | if (bufp > (bufendp - data_v1->template_data_size)) { | ||
| 456 | pr_err("restoring the template data failed\n"); | ||
| 457 | ret = -EINVAL; | ||
| 458 | break; | ||
| 459 | } | ||
| 460 | bufp += data_v1->template_data_size; | ||
| 461 | |||
| 462 | ret = ima_restore_template_data(template_desc, | ||
| 463 | data_v1->template_data, | ||
| 464 | data_v1->template_data_size, | ||
| 465 | &entry); | ||
| 466 | if (ret < 0) | ||
| 467 | break; | ||
| 468 | |||
| 469 | memcpy(entry->digest, hdr_v1->digest, TPM_DIGEST_SIZE); | ||
| 470 | entry->pcr = | ||
| 471 | !ima_canonical_fmt ? hdr_v1->pcr : le32_to_cpu(hdr_v1->pcr); | ||
| 472 | ret = ima_restore_measurement_entry(entry); | ||
| 473 | if (ret < 0) | ||
| 474 | break; | ||
| 475 | |||
| 476 | } | ||
| 477 | return ret; | ||
| 478 | } | ||
diff --git a/security/integrity/ima/ima_template_lib.c b/security/integrity/ima/ima_template_lib.c index f9bae04ba176..f9ba37b3928d 100644 --- a/security/integrity/ima/ima_template_lib.c +++ b/security/integrity/ima/ima_template_lib.c | |||
| @@ -103,8 +103,11 @@ static void ima_show_template_data_binary(struct seq_file *m, | |||
| 103 | u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ? | 103 | u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ? |
| 104 | strlen(field_data->data) : field_data->len; | 104 | strlen(field_data->data) : field_data->len; |
| 105 | 105 | ||
| 106 | if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) | 106 | if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) { |
| 107 | ima_putc(m, &len, sizeof(len)); | 107 | u32 field_len = !ima_canonical_fmt ? len : cpu_to_le32(len); |
| 108 | |||
| 109 | ima_putc(m, &field_len, sizeof(field_len)); | ||
| 110 | } | ||
| 108 | 111 | ||
| 109 | if (!len) | 112 | if (!len) |
| 110 | return; | 113 | return; |
