tcp: detect malicious patterns in tcp_collapse_ofo_queue()
[linux/fpc-iii.git] / arch / x86 / kernel / kexec-bzimage64.c
blobca05f86481aace3a37cd6bf3fbe0e7d7ce3bb33a
1 /*
2 * Kexec bzImage loader
4 * Copyright (C) 2014 Red Hat Inc.
5 * Authors:
6 * Vivek Goyal <vgoyal@redhat.com>
8 * This source code is licensed under the GNU General Public License,
9 * Version 2. See the file COPYING for more details.
12 #define pr_fmt(fmt) "kexec-bzImage64: " fmt
14 #include <linux/string.h>
15 #include <linux/printk.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/kexec.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/efi.h>
22 #include <linux/verify_pefile.h>
23 #include <keys/system_keyring.h>
25 #include <asm/bootparam.h>
26 #include <asm/setup.h>
27 #include <asm/crash.h>
28 #include <asm/efi.h>
29 #include <asm/kexec-bzimage64.h>
31 #define MAX_ELFCOREHDR_STR_LEN 30 /* elfcorehdr=0x<64bit-value> */
34 * Defines lowest physical address for various segments. Not sure where
35 * exactly these limits came from. Current bzimage64 loader in kexec-tools
36 * uses these so I am retaining it. It can be changed over time as we gain
37 * more insight.
39 #define MIN_PURGATORY_ADDR 0x3000
40 #define MIN_BOOTPARAM_ADDR 0x3000
41 #define MIN_KERNEL_LOAD_ADDR 0x100000
42 #define MIN_INITRD_LOAD_ADDR 0x1000000
45 * This is a place holder for all boot loader specific data structure which
46 * gets allocated in one call but gets freed much later during cleanup
47 * time. Right now there is only one field but it can grow as need be.
49 struct bzimage64_data {
51 * Temporary buffer to hold bootparams buffer. This should be
52 * freed once the bootparam segment has been loaded.
54 void *bootparams_buf;
57 static int setup_initrd(struct boot_params *params,
58 unsigned long initrd_load_addr, unsigned long initrd_len)
60 params->hdr.ramdisk_image = initrd_load_addr & 0xffffffffUL;
61 params->hdr.ramdisk_size = initrd_len & 0xffffffffUL;
63 params->ext_ramdisk_image = initrd_load_addr >> 32;
64 params->ext_ramdisk_size = initrd_len >> 32;
66 return 0;
69 static int setup_cmdline(struct kimage *image, struct boot_params *params,
70 unsigned long bootparams_load_addr,
71 unsigned long cmdline_offset, char *cmdline,
72 unsigned long cmdline_len)
74 char *cmdline_ptr = ((char *)params) + cmdline_offset;
75 unsigned long cmdline_ptr_phys, len;
76 uint32_t cmdline_low_32, cmdline_ext_32;
78 memcpy(cmdline_ptr, cmdline, cmdline_len);
79 if (image->type == KEXEC_TYPE_CRASH) {
80 len = sprintf(cmdline_ptr + cmdline_len - 1,
81 " elfcorehdr=0x%lx", image->arch.elf_load_addr);
82 cmdline_len += len;
84 cmdline_ptr[cmdline_len - 1] = '\0';
86 pr_debug("Final command line is: %s\n", cmdline_ptr);
87 cmdline_ptr_phys = bootparams_load_addr + cmdline_offset;
88 cmdline_low_32 = cmdline_ptr_phys & 0xffffffffUL;
89 cmdline_ext_32 = cmdline_ptr_phys >> 32;
91 params->hdr.cmd_line_ptr = cmdline_low_32;
92 if (cmdline_ext_32)
93 params->ext_cmd_line_ptr = cmdline_ext_32;
95 return 0;
98 static int setup_e820_entries(struct boot_params *params)
100 unsigned int nr_e820_entries;
102 nr_e820_entries = e820_saved.nr_map;
104 /* TODO: Pass entries more than E820MAX in bootparams setup data */
105 if (nr_e820_entries > E820MAX)
106 nr_e820_entries = E820MAX;
108 params->e820_entries = nr_e820_entries;
109 memcpy(&params->e820_map, &e820_saved.map,
110 nr_e820_entries * sizeof(struct e820entry));
112 return 0;
115 #ifdef CONFIG_EFI
116 static int setup_efi_info_memmap(struct boot_params *params,
117 unsigned long params_load_addr,
118 unsigned int efi_map_offset,
119 unsigned int efi_map_sz)
121 void *efi_map = (void *)params + efi_map_offset;
122 unsigned long efi_map_phys_addr = params_load_addr + efi_map_offset;
123 struct efi_info *ei = &params->efi_info;
125 if (!efi_map_sz)
126 return 0;
128 efi_runtime_map_copy(efi_map, efi_map_sz);
130 ei->efi_memmap = efi_map_phys_addr & 0xffffffff;
131 ei->efi_memmap_hi = efi_map_phys_addr >> 32;
132 ei->efi_memmap_size = efi_map_sz;
134 return 0;
137 static int
138 prepare_add_efi_setup_data(struct boot_params *params,
139 unsigned long params_load_addr,
140 unsigned int efi_setup_data_offset)
142 unsigned long setup_data_phys;
143 struct setup_data *sd = (void *)params + efi_setup_data_offset;
144 struct efi_setup_data *esd = (void *)sd + sizeof(struct setup_data);
146 esd->fw_vendor = efi.fw_vendor;
147 esd->runtime = efi.runtime;
148 esd->tables = efi.config_table;
149 esd->smbios = efi.smbios;
151 sd->type = SETUP_EFI;
152 sd->len = sizeof(struct efi_setup_data);
154 /* Add setup data */
155 setup_data_phys = params_load_addr + efi_setup_data_offset;
156 sd->next = params->hdr.setup_data;
157 params->hdr.setup_data = setup_data_phys;
159 return 0;
162 static int
163 setup_efi_state(struct boot_params *params, unsigned long params_load_addr,
164 unsigned int efi_map_offset, unsigned int efi_map_sz,
165 unsigned int efi_setup_data_offset)
167 struct efi_info *current_ei = &boot_params.efi_info;
168 struct efi_info *ei = &params->efi_info;
170 if (!current_ei->efi_memmap_size)
171 return 0;
174 * If 1:1 mapping is not enabled, second kernel can not setup EFI
175 * and use EFI run time services. User space will have to pass
176 * acpi_rsdp=<addr> on kernel command line to make second kernel boot
177 * without efi.
179 if (efi_enabled(EFI_OLD_MEMMAP))
180 return 0;
182 ei->efi_loader_signature = current_ei->efi_loader_signature;
183 ei->efi_systab = current_ei->efi_systab;
184 ei->efi_systab_hi = current_ei->efi_systab_hi;
186 ei->efi_memdesc_version = current_ei->efi_memdesc_version;
187 ei->efi_memdesc_size = efi_get_runtime_map_desc_size();
189 setup_efi_info_memmap(params, params_load_addr, efi_map_offset,
190 efi_map_sz);
191 prepare_add_efi_setup_data(params, params_load_addr,
192 efi_setup_data_offset);
193 return 0;
195 #endif /* CONFIG_EFI */
197 static int
198 setup_boot_parameters(struct kimage *image, struct boot_params *params,
199 unsigned long params_load_addr,
200 unsigned int efi_map_offset, unsigned int efi_map_sz,
201 unsigned int efi_setup_data_offset)
203 unsigned int nr_e820_entries;
204 unsigned long long mem_k, start, end;
205 int i, ret = 0;
207 /* Get subarch from existing bootparams */
208 params->hdr.hardware_subarch = boot_params.hdr.hardware_subarch;
210 /* Copying screen_info will do? */
211 memcpy(&params->screen_info, &boot_params.screen_info,
212 sizeof(struct screen_info));
214 /* Fill in memsize later */
215 params->screen_info.ext_mem_k = 0;
216 params->alt_mem_k = 0;
218 /* Default APM info */
219 memset(&params->apm_bios_info, 0, sizeof(params->apm_bios_info));
221 /* Default drive info */
222 memset(&params->hd0_info, 0, sizeof(params->hd0_info));
223 memset(&params->hd1_info, 0, sizeof(params->hd1_info));
225 /* Default sysdesc table */
226 params->sys_desc_table.length = 0;
228 if (image->type == KEXEC_TYPE_CRASH) {
229 ret = crash_setup_memmap_entries(image, params);
230 if (ret)
231 return ret;
232 } else
233 setup_e820_entries(params);
235 nr_e820_entries = params->e820_entries;
237 for (i = 0; i < nr_e820_entries; i++) {
238 if (params->e820_map[i].type != E820_RAM)
239 continue;
240 start = params->e820_map[i].addr;
241 end = params->e820_map[i].addr + params->e820_map[i].size - 1;
243 if ((start <= 0x100000) && end > 0x100000) {
244 mem_k = (end >> 10) - (0x100000 >> 10);
245 params->screen_info.ext_mem_k = mem_k;
246 params->alt_mem_k = mem_k;
247 if (mem_k > 0xfc00)
248 params->screen_info.ext_mem_k = 0xfc00; /* 64M*/
249 if (mem_k > 0xffffffff)
250 params->alt_mem_k = 0xffffffff;
254 #ifdef CONFIG_EFI
255 /* Setup EFI state */
256 setup_efi_state(params, params_load_addr, efi_map_offset, efi_map_sz,
257 efi_setup_data_offset);
258 #endif
260 /* Setup EDD info */
261 memcpy(params->eddbuf, boot_params.eddbuf,
262 EDDMAXNR * sizeof(struct edd_info));
263 params->eddbuf_entries = boot_params.eddbuf_entries;
265 memcpy(params->edd_mbr_sig_buffer, boot_params.edd_mbr_sig_buffer,
266 EDD_MBR_SIG_MAX * sizeof(unsigned int));
268 return ret;
271 static int bzImage64_probe(const char *buf, unsigned long len)
273 int ret = -ENOEXEC;
274 struct setup_header *header;
276 /* kernel should be atleast two sectors long */
277 if (len < 2 * 512) {
278 pr_err("File is too short to be a bzImage\n");
279 return ret;
282 header = (struct setup_header *)(buf + offsetof(struct boot_params, hdr));
283 if (memcmp((char *)&header->header, "HdrS", 4) != 0) {
284 pr_err("Not a bzImage\n");
285 return ret;
288 if (header->boot_flag != 0xAA55) {
289 pr_err("No x86 boot sector present\n");
290 return ret;
293 if (header->version < 0x020C) {
294 pr_err("Must be at least protocol version 2.12\n");
295 return ret;
298 if (!(header->loadflags & LOADED_HIGH)) {
299 pr_err("zImage not a bzImage\n");
300 return ret;
303 if (!(header->xloadflags & XLF_KERNEL_64)) {
304 pr_err("Not a bzImage64. XLF_KERNEL_64 is not set.\n");
305 return ret;
308 if (!(header->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G)) {
309 pr_err("XLF_CAN_BE_LOADED_ABOVE_4G is not set.\n");
310 return ret;
314 * Can't handle 32bit EFI as it does not allow loading kernel
315 * above 4G. This should be handled by 32bit bzImage loader
317 if (efi_enabled(EFI_RUNTIME_SERVICES) && !efi_enabled(EFI_64BIT)) {
318 pr_debug("EFI is 32 bit. Can't load kernel above 4G.\n");
319 return ret;
322 /* I've got a bzImage */
323 pr_debug("It's a relocatable bzImage64\n");
324 ret = 0;
326 return ret;
329 static void *bzImage64_load(struct kimage *image, char *kernel,
330 unsigned long kernel_len, char *initrd,
331 unsigned long initrd_len, char *cmdline,
332 unsigned long cmdline_len)
335 struct setup_header *header;
336 int setup_sects, kern16_size, ret = 0;
337 unsigned long setup_header_size, params_cmdline_sz, params_misc_sz;
338 struct boot_params *params;
339 unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr;
340 unsigned long purgatory_load_addr;
341 unsigned long kernel_bufsz, kernel_memsz, kernel_align;
342 char *kernel_buf;
343 struct bzimage64_data *ldata;
344 struct kexec_entry64_regs regs64;
345 void *stack;
346 unsigned int setup_hdr_offset = offsetof(struct boot_params, hdr);
347 unsigned int efi_map_offset, efi_map_sz, efi_setup_data_offset;
349 header = (struct setup_header *)(kernel + setup_hdr_offset);
350 setup_sects = header->setup_sects;
351 if (setup_sects == 0)
352 setup_sects = 4;
354 kern16_size = (setup_sects + 1) * 512;
355 if (kernel_len < kern16_size) {
356 pr_err("bzImage truncated\n");
357 return ERR_PTR(-ENOEXEC);
360 if (cmdline_len > header->cmdline_size) {
361 pr_err("Kernel command line too long\n");
362 return ERR_PTR(-EINVAL);
366 * In case of crash dump, we will append elfcorehdr=<addr> to
367 * command line. Make sure it does not overflow
369 if (cmdline_len + MAX_ELFCOREHDR_STR_LEN > header->cmdline_size) {
370 pr_debug("Appending elfcorehdr=<addr> to command line exceeds maximum allowed length\n");
371 return ERR_PTR(-EINVAL);
374 /* Allocate and load backup region */
375 if (image->type == KEXEC_TYPE_CRASH) {
376 ret = crash_load_segments(image);
377 if (ret)
378 return ERR_PTR(ret);
382 * Load purgatory. For 64bit entry point, purgatory code can be
383 * anywhere.
385 ret = kexec_load_purgatory(image, MIN_PURGATORY_ADDR, ULONG_MAX, 1,
386 &purgatory_load_addr);
387 if (ret) {
388 pr_err("Loading purgatory failed\n");
389 return ERR_PTR(ret);
392 pr_debug("Loaded purgatory at 0x%lx\n", purgatory_load_addr);
396 * Load Bootparams and cmdline and space for efi stuff.
398 * Allocate memory together for multiple data structures so
399 * that they all can go in single area/segment and we don't
400 * have to create separate segment for each. Keeps things
401 * little bit simple
403 efi_map_sz = efi_get_runtime_map_size();
404 efi_map_sz = ALIGN(efi_map_sz, 16);
405 params_cmdline_sz = sizeof(struct boot_params) + cmdline_len +
406 MAX_ELFCOREHDR_STR_LEN;
407 params_cmdline_sz = ALIGN(params_cmdline_sz, 16);
408 params_misc_sz = params_cmdline_sz + efi_map_sz +
409 sizeof(struct setup_data) +
410 sizeof(struct efi_setup_data);
412 params = kzalloc(params_misc_sz, GFP_KERNEL);
413 if (!params)
414 return ERR_PTR(-ENOMEM);
415 efi_map_offset = params_cmdline_sz;
416 efi_setup_data_offset = efi_map_offset + efi_map_sz;
418 /* Copy setup header onto bootparams. Documentation/x86/boot.txt */
419 setup_header_size = 0x0202 + kernel[0x0201] - setup_hdr_offset;
421 /* Is there a limit on setup header size? */
422 memcpy(&params->hdr, (kernel + setup_hdr_offset), setup_header_size);
424 ret = kexec_add_buffer(image, (char *)params, params_misc_sz,
425 params_misc_sz, 16, MIN_BOOTPARAM_ADDR,
426 ULONG_MAX, 1, &bootparam_load_addr);
427 if (ret)
428 goto out_free_params;
429 pr_debug("Loaded boot_param, command line and misc at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
430 bootparam_load_addr, params_misc_sz, params_misc_sz);
432 /* Load kernel */
433 kernel_buf = kernel + kern16_size;
434 kernel_bufsz = kernel_len - kern16_size;
435 kernel_memsz = PAGE_ALIGN(header->init_size);
436 kernel_align = header->kernel_alignment;
438 ret = kexec_add_buffer(image, kernel_buf,
439 kernel_bufsz, kernel_memsz, kernel_align,
440 MIN_KERNEL_LOAD_ADDR, ULONG_MAX, 1,
441 &kernel_load_addr);
442 if (ret)
443 goto out_free_params;
445 pr_debug("Loaded 64bit kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
446 kernel_load_addr, kernel_memsz, kernel_memsz);
448 /* Load initrd high */
449 if (initrd) {
450 ret = kexec_add_buffer(image, initrd, initrd_len, initrd_len,
451 PAGE_SIZE, MIN_INITRD_LOAD_ADDR,
452 ULONG_MAX, 1, &initrd_load_addr);
453 if (ret)
454 goto out_free_params;
456 pr_debug("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
457 initrd_load_addr, initrd_len, initrd_len);
459 setup_initrd(params, initrd_load_addr, initrd_len);
462 setup_cmdline(image, params, bootparam_load_addr,
463 sizeof(struct boot_params), cmdline, cmdline_len);
465 /* bootloader info. Do we need a separate ID for kexec kernel loader? */
466 params->hdr.type_of_loader = 0x0D << 4;
467 params->hdr.loadflags = 0;
469 /* Setup purgatory regs for entry */
470 ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
471 sizeof(regs64), 1);
472 if (ret)
473 goto out_free_params;
475 regs64.rbx = 0; /* Bootstrap Processor */
476 regs64.rsi = bootparam_load_addr;
477 regs64.rip = kernel_load_addr + 0x200;
478 stack = kexec_purgatory_get_symbol_addr(image, "stack_end");
479 if (IS_ERR(stack)) {
480 pr_err("Could not find address of symbol stack_end\n");
481 ret = -EINVAL;
482 goto out_free_params;
485 regs64.rsp = (unsigned long)stack;
486 ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
487 sizeof(regs64), 0);
488 if (ret)
489 goto out_free_params;
491 ret = setup_boot_parameters(image, params, bootparam_load_addr,
492 efi_map_offset, efi_map_sz,
493 efi_setup_data_offset);
494 if (ret)
495 goto out_free_params;
497 /* Allocate loader specific data */
498 ldata = kzalloc(sizeof(struct bzimage64_data), GFP_KERNEL);
499 if (!ldata) {
500 ret = -ENOMEM;
501 goto out_free_params;
505 * Store pointer to params so that it could be freed after loading
506 * params segment has been loaded and contents have been copied
507 * somewhere else.
509 ldata->bootparams_buf = params;
510 return ldata;
512 out_free_params:
513 kfree(params);
514 return ERR_PTR(ret);
517 /* This cleanup function is called after various segments have been loaded */
518 static int bzImage64_cleanup(void *loader_data)
520 struct bzimage64_data *ldata = loader_data;
522 if (!ldata)
523 return 0;
525 kfree(ldata->bootparams_buf);
526 ldata->bootparams_buf = NULL;
528 return 0;
531 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
532 static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len)
534 bool trusted;
535 int ret;
537 ret = verify_pefile_signature(kernel, kernel_len,
538 system_trusted_keyring, &trusted);
539 if (ret < 0)
540 return ret;
541 if (!trusted)
542 return -EKEYREJECTED;
543 return 0;
545 #endif
547 struct kexec_file_ops kexec_bzImage64_ops = {
548 .probe = bzImage64_probe,
549 .load = bzImage64_load,
550 .cleanup = bzImage64_cleanup,
551 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
552 .verify_sig = bzImage64_verify_sig,
553 #endif