blob: 332c4fd12cb1460774498ba030846f819ebe23c5 [file] [log] [blame]
Dave Younga43cac02015-09-09 15:38:51 -07001/*
2 * kexec: kexec_file_load system call
3 *
4 * Copyright (C) 2014 Red Hat Inc.
5 * Authors:
6 * Vivek Goyal <vgoyal@redhat.com>
7 *
8 * This source code is licensed under the GNU General Public License,
9 * Version 2. See the file COPYING for more details.
10 */
11
Minfei Huangde90a6b2015-11-06 16:32:45 -080012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Dave Younga43cac02015-09-09 15:38:51 -070014#include <linux/capability.h>
15#include <linux/mm.h>
16#include <linux/file.h>
17#include <linux/slab.h>
18#include <linux/kexec.h>
19#include <linux/mutex.h>
20#include <linux/list.h>
Mimi Zoharb804def2016-01-14 20:59:14 -050021#include <linux/fs.h>
Mimi Zohar7b8589c2016-12-19 16:22:48 -080022#include <linux/ima.h>
Dave Younga43cac02015-09-09 15:38:51 -070023#include <crypto/hash.h>
24#include <crypto/sha.h>
25#include <linux/syscalls.h>
26#include <linux/vmalloc.h>
27#include "kexec_internal.h"
28
Dave Younga43cac02015-09-09 15:38:51 -070029static int kexec_calculate_store_digests(struct kimage *image);
30
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -070031/*
32 * Currently this is the only default function that is exported as some
33 * architectures need it to do additional handlings.
34 * In the future, other default functions may be exported too if required.
35 */
36int kexec_image_probe_default(struct kimage *image, void *buf,
37 unsigned long buf_len)
38{
39 const struct kexec_file_ops * const *fops;
40 int ret = -ENOEXEC;
41
42 for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
43 ret = (*fops)->probe(buf, buf_len);
44 if (!ret) {
45 image->fops = *fops;
46 return ret;
47 }
48 }
49
50 return ret;
51}
52
Dave Younga43cac02015-09-09 15:38:51 -070053/* Architectures can provide this probe function */
54int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
55 unsigned long buf_len)
56{
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -070057 return kexec_image_probe_default(image, buf, buf_len);
58}
59
60static void *kexec_image_load_default(struct kimage *image)
61{
62 if (!image->fops || !image->fops->load)
63 return ERR_PTR(-ENOEXEC);
64
65 return image->fops->load(image, image->kernel_buf,
66 image->kernel_buf_len, image->initrd_buf,
67 image->initrd_buf_len, image->cmdline_buf,
68 image->cmdline_buf_len);
Dave Younga43cac02015-09-09 15:38:51 -070069}
70
71void * __weak arch_kexec_kernel_image_load(struct kimage *image)
72{
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -070073 return kexec_image_load_default(image);
74}
75
76static int kexec_image_post_load_cleanup_default(struct kimage *image)
77{
78 if (!image->fops || !image->fops->cleanup)
79 return 0;
80
81 return image->fops->cleanup(image->image_loader_data);
Dave Younga43cac02015-09-09 15:38:51 -070082}
83
84int __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
85{
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -070086 return kexec_image_post_load_cleanup_default(image);
Dave Younga43cac02015-09-09 15:38:51 -070087}
88
Xunlei Pang978e30c2016-01-20 15:00:36 -080089#ifdef CONFIG_KEXEC_VERIFY_SIG
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -070090static int kexec_image_verify_sig_default(struct kimage *image, void *buf,
91 unsigned long buf_len)
92{
93 if (!image->fops || !image->fops->verify_sig) {
94 pr_debug("kernel loader does not support signature verification.\n");
95 return -EKEYREJECTED;
96 }
97
98 return image->fops->verify_sig(buf, buf_len);
99}
100
Dave Younga43cac02015-09-09 15:38:51 -0700101int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
102 unsigned long buf_len)
103{
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -0700104 return kexec_image_verify_sig_default(image, buf, buf_len);
Dave Younga43cac02015-09-09 15:38:51 -0700105}
Xunlei Pang978e30c2016-01-20 15:00:36 -0800106#endif
Dave Younga43cac02015-09-09 15:38:51 -0700107
108/* Apply relocations of type RELA */
109int __weak
110arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
111 unsigned int relsec)
112{
113 pr_err("RELA relocation unsupported.\n");
114 return -ENOEXEC;
115}
116
117/* Apply relocations of type REL */
118int __weak
119arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
120 unsigned int relsec)
121{
122 pr_err("REL relocation unsupported.\n");
123 return -ENOEXEC;
124}
125
126/*
127 * Free up memory used by kernel, initrd, and command line. This is temporary
128 * memory allocation which is not needed any more after these buffers have
129 * been loaded into separate segments and have been copied elsewhere.
130 */
131void kimage_file_post_load_cleanup(struct kimage *image)
132{
133 struct purgatory_info *pi = &image->purgatory_info;
134
135 vfree(image->kernel_buf);
136 image->kernel_buf = NULL;
137
138 vfree(image->initrd_buf);
139 image->initrd_buf = NULL;
140
141 kfree(image->cmdline_buf);
142 image->cmdline_buf = NULL;
143
144 vfree(pi->purgatory_buf);
145 pi->purgatory_buf = NULL;
146
147 vfree(pi->sechdrs);
148 pi->sechdrs = NULL;
149
150 /* See if architecture has anything to cleanup post load */
151 arch_kimage_file_post_load_cleanup(image);
152
153 /*
154 * Above call should have called into bootloader to free up
155 * any data stored in kimage->image_loader_data. It should
156 * be ok now to free it up.
157 */
158 kfree(image->image_loader_data);
159 image->image_loader_data = NULL;
160}
161
162/*
163 * In file mode list of segments is prepared by kernel. Copy relevant
164 * data from user space, do error checking, prepare segment list
165 */
166static int
167kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
168 const char __user *cmdline_ptr,
169 unsigned long cmdline_len, unsigned flags)
170{
171 int ret = 0;
172 void *ldata;
Mimi Zoharb804def2016-01-14 20:59:14 -0500173 loff_t size;
Dave Younga43cac02015-09-09 15:38:51 -0700174
Mimi Zoharb804def2016-01-14 20:59:14 -0500175 ret = kernel_read_file_from_fd(kernel_fd, &image->kernel_buf,
176 &size, INT_MAX, READING_KEXEC_IMAGE);
Dave Younga43cac02015-09-09 15:38:51 -0700177 if (ret)
178 return ret;
Mimi Zoharb804def2016-01-14 20:59:14 -0500179 image->kernel_buf_len = size;
Dave Younga43cac02015-09-09 15:38:51 -0700180
Mimi Zohar7b8589c2016-12-19 16:22:48 -0800181 /* IMA needs to pass the measurement list to the next kernel. */
182 ima_add_kexec_buffer(image);
183
Dave Younga43cac02015-09-09 15:38:51 -0700184 /* Call arch image probe handlers */
185 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
186 image->kernel_buf_len);
Dave Younga43cac02015-09-09 15:38:51 -0700187 if (ret)
188 goto out;
189
190#ifdef CONFIG_KEXEC_VERIFY_SIG
191 ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
192 image->kernel_buf_len);
193 if (ret) {
194 pr_debug("kernel signature verification failed.\n");
195 goto out;
196 }
197 pr_debug("kernel signature verification successful.\n");
198#endif
199 /* It is possible that there no initramfs is being loaded */
200 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
Mimi Zoharb804def2016-01-14 20:59:14 -0500201 ret = kernel_read_file_from_fd(initrd_fd, &image->initrd_buf,
202 &size, INT_MAX,
203 READING_KEXEC_INITRAMFS);
Dave Younga43cac02015-09-09 15:38:51 -0700204 if (ret)
205 goto out;
Mimi Zoharb804def2016-01-14 20:59:14 -0500206 image->initrd_buf_len = size;
Dave Younga43cac02015-09-09 15:38:51 -0700207 }
208
209 if (cmdline_len) {
Al Viroa9bd8df2017-05-13 18:39:01 -0400210 image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);
211 if (IS_ERR(image->cmdline_buf)) {
212 ret = PTR_ERR(image->cmdline_buf);
213 image->cmdline_buf = NULL;
Dave Younga43cac02015-09-09 15:38:51 -0700214 goto out;
215 }
216
217 image->cmdline_buf_len = cmdline_len;
218
219 /* command line should be a string with last byte null */
220 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
221 ret = -EINVAL;
222 goto out;
223 }
224 }
225
226 /* Call arch image load handlers */
227 ldata = arch_kexec_kernel_image_load(image);
228
229 if (IS_ERR(ldata)) {
230 ret = PTR_ERR(ldata);
231 goto out;
232 }
233
234 image->image_loader_data = ldata;
235out:
236 /* In case of error, free up all allocated memory in this function */
237 if (ret)
238 kimage_file_post_load_cleanup(image);
239 return ret;
240}
241
242static int
243kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
244 int initrd_fd, const char __user *cmdline_ptr,
245 unsigned long cmdline_len, unsigned long flags)
246{
247 int ret;
248 struct kimage *image;
249 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
250
251 image = do_kimage_alloc_init();
252 if (!image)
253 return -ENOMEM;
254
255 image->file_mode = 1;
256
257 if (kexec_on_panic) {
258 /* Enable special crash kernel control page alloc policy. */
259 image->control_page = crashk_res.start;
260 image->type = KEXEC_TYPE_CRASH;
261 }
262
263 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
264 cmdline_ptr, cmdline_len, flags);
265 if (ret)
266 goto out_free_image;
267
268 ret = sanity_check_segment_list(image);
269 if (ret)
270 goto out_free_post_load_bufs;
271
272 ret = -ENOMEM;
273 image->control_code_page = kimage_alloc_control_pages(image,
274 get_order(KEXEC_CONTROL_PAGE_SIZE));
275 if (!image->control_code_page) {
276 pr_err("Could not allocate control_code_buffer\n");
277 goto out_free_post_load_bufs;
278 }
279
280 if (!kexec_on_panic) {
281 image->swap_page = kimage_alloc_control_pages(image, 0);
282 if (!image->swap_page) {
283 pr_err("Could not allocate swap buffer\n");
284 goto out_free_control_pages;
285 }
286 }
287
288 *rimage = image;
289 return 0;
290out_free_control_pages:
291 kimage_free_page_list(&image->control_pages);
292out_free_post_load_bufs:
293 kimage_file_post_load_cleanup(image);
294out_free_image:
295 kfree(image);
296 return ret;
297}
298
299SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
300 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
301 unsigned long, flags)
302{
303 int ret = 0, i;
304 struct kimage **dest_image, *image;
305
306 /* We only trust the superuser with rebooting the system. */
307 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
308 return -EPERM;
309
310 /* Make sure we have a legal set of flags */
311 if (flags != (flags & KEXEC_FILE_FLAGS))
312 return -EINVAL;
313
314 image = NULL;
315
316 if (!mutex_trylock(&kexec_mutex))
317 return -EBUSY;
318
319 dest_image = &kexec_image;
Xunlei Pang9b492cf2016-05-23 16:24:10 -0700320 if (flags & KEXEC_FILE_ON_CRASH) {
Dave Younga43cac02015-09-09 15:38:51 -0700321 dest_image = &kexec_crash_image;
Xunlei Pang9b492cf2016-05-23 16:24:10 -0700322 if (kexec_crash_image)
323 arch_kexec_unprotect_crashkres();
324 }
Dave Younga43cac02015-09-09 15:38:51 -0700325
326 if (flags & KEXEC_FILE_UNLOAD)
327 goto exchange;
328
329 /*
330 * In case of crash, new kernel gets loaded in reserved region. It is
331 * same memory where old crash kernel might be loaded. Free any
332 * current crash dump kernel before we corrupt it.
333 */
334 if (flags & KEXEC_FILE_ON_CRASH)
335 kimage_free(xchg(&kexec_crash_image, NULL));
336
337 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
338 cmdline_len, flags);
339 if (ret)
340 goto out;
341
342 ret = machine_kexec_prepare(image);
343 if (ret)
344 goto out;
345
Xunlei Pang12293842017-07-12 14:33:21 -0700346 /*
347 * Some architecture(like S390) may touch the crash memory before
348 * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
349 */
350 ret = kimage_crash_copy_vmcoreinfo(image);
351 if (ret)
352 goto out;
353
Dave Younga43cac02015-09-09 15:38:51 -0700354 ret = kexec_calculate_store_digests(image);
355 if (ret)
356 goto out;
357
358 for (i = 0; i < image->nr_segments; i++) {
359 struct kexec_segment *ksegment;
360
361 ksegment = &image->segment[i];
362 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
363 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
364 ksegment->memsz);
365
366 ret = kimage_load_segment(image, &image->segment[i]);
367 if (ret)
368 goto out;
369 }
370
371 kimage_terminate(image);
372
373 /*
374 * Free up any temporary buffers allocated which are not needed
375 * after image has been loaded
376 */
377 kimage_file_post_load_cleanup(image);
378exchange:
379 image = xchg(dest_image, image);
380out:
Xunlei Pang9b492cf2016-05-23 16:24:10 -0700381 if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
382 arch_kexec_protect_crashkres();
383
Dave Younga43cac02015-09-09 15:38:51 -0700384 mutex_unlock(&kexec_mutex);
385 kimage_free(image);
386 return ret;
387}
388
389static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
390 struct kexec_buf *kbuf)
391{
392 struct kimage *image = kbuf->image;
393 unsigned long temp_start, temp_end;
394
395 temp_end = min(end, kbuf->buf_max);
396 temp_start = temp_end - kbuf->memsz;
397
398 do {
399 /* align down start */
400 temp_start = temp_start & (~(kbuf->buf_align - 1));
401
402 if (temp_start < start || temp_start < kbuf->buf_min)
403 return 0;
404
405 temp_end = temp_start + kbuf->memsz - 1;
406
407 /*
408 * Make sure this does not conflict with any of existing
409 * segments
410 */
411 if (kimage_is_destination_range(image, temp_start, temp_end)) {
412 temp_start = temp_start - PAGE_SIZE;
413 continue;
414 }
415
416 /* We found a suitable memory range */
417 break;
418 } while (1);
419
420 /* If we are here, we found a suitable memory range */
421 kbuf->mem = temp_start;
422
423 /* Success, stop navigating through remaining System RAM ranges */
424 return 1;
425}
426
427static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
428 struct kexec_buf *kbuf)
429{
430 struct kimage *image = kbuf->image;
431 unsigned long temp_start, temp_end;
432
433 temp_start = max(start, kbuf->buf_min);
434
435 do {
436 temp_start = ALIGN(temp_start, kbuf->buf_align);
437 temp_end = temp_start + kbuf->memsz - 1;
438
439 if (temp_end > end || temp_end > kbuf->buf_max)
440 return 0;
441 /*
442 * Make sure this does not conflict with any of existing
443 * segments
444 */
445 if (kimage_is_destination_range(image, temp_start, temp_end)) {
446 temp_start = temp_start + PAGE_SIZE;
447 continue;
448 }
449
450 /* We found a suitable memory range */
451 break;
452 } while (1);
453
454 /* If we are here, we found a suitable memory range */
455 kbuf->mem = temp_start;
456
457 /* Success, stop navigating through remaining System RAM ranges */
458 return 1;
459}
460
Tom Lendacky1d2e7332017-10-20 09:30:51 -0500461static int locate_mem_hole_callback(struct resource *res, void *arg)
Dave Younga43cac02015-09-09 15:38:51 -0700462{
463 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
Tom Lendacky1d2e7332017-10-20 09:30:51 -0500464 u64 start = res->start, end = res->end;
Dave Younga43cac02015-09-09 15:38:51 -0700465 unsigned long sz = end - start + 1;
466
467 /* Returning 0 will take to next memory range */
468 if (sz < kbuf->memsz)
469 return 0;
470
471 if (end < kbuf->buf_min || start > kbuf->buf_max)
472 return 0;
473
474 /*
475 * Allocate memory top down with-in ram range. Otherwise bottom up
476 * allocation.
477 */
478 if (kbuf->top_down)
479 return locate_mem_hole_top_down(start, end, kbuf);
480 return locate_mem_hole_bottom_up(start, end, kbuf);
481}
482
Thiago Jung Bauermann60fe3912016-11-29 23:45:47 +1100483/**
484 * arch_kexec_walk_mem - call func(data) on free memory regions
485 * @kbuf: Context info for the search. Also passed to @func.
486 * @func: Function to call for each memory region.
487 *
488 * Return: The memory walk will stop when func returns a non-zero value
489 * and that value will be returned. If all free regions are visited without
490 * func returning non-zero, then zero will be returned.
491 */
492int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf,
Tom Lendacky1d2e7332017-10-20 09:30:51 -0500493 int (*func)(struct resource *, void *))
Thiago Jung Bauermann60fe3912016-11-29 23:45:47 +1100494{
495 if (kbuf->image->type == KEXEC_TYPE_CRASH)
496 return walk_iomem_res_desc(crashk_res.desc,
497 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
498 crashk_res.start, crashk_res.end,
499 kbuf, func);
500 else
501 return walk_system_ram_res(0, ULONG_MAX, kbuf, func);
502}
503
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100504/**
Thiago Jung Bauermanne2e806f2016-11-29 23:45:49 +1100505 * kexec_locate_mem_hole - find free memory for the purgatory or the next kernel
506 * @kbuf: Parameters for the memory search.
507 *
508 * On success, kbuf->mem will have the start address of the memory region found.
509 *
510 * Return: 0 on success, negative errno on error.
511 */
512int kexec_locate_mem_hole(struct kexec_buf *kbuf)
513{
514 int ret;
515
516 ret = arch_kexec_walk_mem(kbuf, locate_mem_hole_callback);
517
518 return ret == 1 ? 0 : -EADDRNOTAVAIL;
519}
520
521/**
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100522 * kexec_add_buffer - place a buffer in a kexec segment
523 * @kbuf: Buffer contents and memory parameters.
524 *
525 * This function assumes that kexec_mutex is held.
526 * On successful return, @kbuf->mem will have the physical address of
527 * the buffer in memory.
528 *
529 * Return: 0 on success, negative errno on error.
Dave Younga43cac02015-09-09 15:38:51 -0700530 */
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100531int kexec_add_buffer(struct kexec_buf *kbuf)
Dave Younga43cac02015-09-09 15:38:51 -0700532{
533
534 struct kexec_segment *ksegment;
Dave Younga43cac02015-09-09 15:38:51 -0700535 int ret;
536
537 /* Currently adding segment this way is allowed only in file mode */
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100538 if (!kbuf->image->file_mode)
Dave Younga43cac02015-09-09 15:38:51 -0700539 return -EINVAL;
540
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100541 if (kbuf->image->nr_segments >= KEXEC_SEGMENT_MAX)
Dave Younga43cac02015-09-09 15:38:51 -0700542 return -EINVAL;
543
544 /*
545 * Make sure we are not trying to add buffer after allocating
546 * control pages. All segments need to be placed first before
547 * any control pages are allocated. As control page allocation
548 * logic goes through list of segments to make sure there are
549 * no destination overlaps.
550 */
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100551 if (!list_empty(&kbuf->image->control_pages)) {
Dave Younga43cac02015-09-09 15:38:51 -0700552 WARN_ON(1);
553 return -EINVAL;
554 }
555
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100556 /* Ensure minimum alignment needed for segments. */
557 kbuf->memsz = ALIGN(kbuf->memsz, PAGE_SIZE);
558 kbuf->buf_align = max(kbuf->buf_align, PAGE_SIZE);
Dave Younga43cac02015-09-09 15:38:51 -0700559
560 /* Walk the RAM ranges and allocate a suitable range for the buffer */
Thiago Jung Bauermanne2e806f2016-11-29 23:45:49 +1100561 ret = kexec_locate_mem_hole(kbuf);
562 if (ret)
563 return ret;
Dave Younga43cac02015-09-09 15:38:51 -0700564
565 /* Found a suitable memory range */
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100566 ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
Dave Younga43cac02015-09-09 15:38:51 -0700567 ksegment->kbuf = kbuf->buffer;
568 ksegment->bufsz = kbuf->bufsz;
569 ksegment->mem = kbuf->mem;
570 ksegment->memsz = kbuf->memsz;
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100571 kbuf->image->nr_segments++;
Dave Younga43cac02015-09-09 15:38:51 -0700572 return 0;
573}
574
575/* Calculate and store the digest of segments */
576static int kexec_calculate_store_digests(struct kimage *image)
577{
578 struct crypto_shash *tfm;
579 struct shash_desc *desc;
580 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
581 size_t desc_size, nullsz;
582 char *digest;
583 void *zero_buf;
584 struct kexec_sha_region *sha_regions;
585 struct purgatory_info *pi = &image->purgatory_info;
586
AKASHI Takahirob799a092018-04-13 15:35:45 -0700587 if (!IS_ENABLED(CONFIG_ARCH_HAS_KEXEC_PURGATORY))
588 return 0;
589
Dave Younga43cac02015-09-09 15:38:51 -0700590 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
591 zero_buf_sz = PAGE_SIZE;
592
593 tfm = crypto_alloc_shash("sha256", 0, 0);
594 if (IS_ERR(tfm)) {
595 ret = PTR_ERR(tfm);
596 goto out;
597 }
598
599 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
600 desc = kzalloc(desc_size, GFP_KERNEL);
601 if (!desc) {
602 ret = -ENOMEM;
603 goto out_free_tfm;
604 }
605
606 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
607 sha_regions = vzalloc(sha_region_sz);
608 if (!sha_regions)
609 goto out_free_desc;
610
611 desc->tfm = tfm;
612 desc->flags = 0;
613
614 ret = crypto_shash_init(desc);
615 if (ret < 0)
616 goto out_free_sha_regions;
617
618 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
619 if (!digest) {
620 ret = -ENOMEM;
621 goto out_free_sha_regions;
622 }
623
624 for (j = i = 0; i < image->nr_segments; i++) {
625 struct kexec_segment *ksegment;
626
627 ksegment = &image->segment[i];
628 /*
629 * Skip purgatory as it will be modified once we put digest
630 * info in purgatory.
631 */
632 if (ksegment->kbuf == pi->purgatory_buf)
633 continue;
634
635 ret = crypto_shash_update(desc, ksegment->kbuf,
636 ksegment->bufsz);
637 if (ret)
638 break;
639
640 /*
641 * Assume rest of the buffer is filled with zero and
642 * update digest accordingly.
643 */
644 nullsz = ksegment->memsz - ksegment->bufsz;
645 while (nullsz) {
646 unsigned long bytes = nullsz;
647
648 if (bytes > zero_buf_sz)
649 bytes = zero_buf_sz;
650 ret = crypto_shash_update(desc, zero_buf, bytes);
651 if (ret)
652 break;
653 nullsz -= bytes;
654 }
655
656 if (ret)
657 break;
658
659 sha_regions[j].start = ksegment->mem;
660 sha_regions[j].len = ksegment->memsz;
661 j++;
662 }
663
664 if (!ret) {
665 ret = crypto_shash_final(desc, digest);
666 if (ret)
667 goto out_free_digest;
Thomas Gleixner40c50c12017-03-10 13:17:18 +0100668 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
669 sha_regions, sha_region_sz, 0);
Dave Younga43cac02015-09-09 15:38:51 -0700670 if (ret)
671 goto out_free_digest;
672
Thomas Gleixner40c50c12017-03-10 13:17:18 +0100673 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
674 digest, SHA256_DIGEST_SIZE, 0);
Dave Younga43cac02015-09-09 15:38:51 -0700675 if (ret)
676 goto out_free_digest;
677 }
678
679out_free_digest:
680 kfree(digest);
681out_free_sha_regions:
682 vfree(sha_regions);
683out_free_desc:
684 kfree(desc);
685out_free_tfm:
686 kfree(tfm);
687out:
688 return ret;
689}
690
AKASHI Takahirob799a092018-04-13 15:35:45 -0700691#ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY
Dave Younga43cac02015-09-09 15:38:51 -0700692/* Actually load purgatory. Lot of code taken from kexec-tools */
693static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
694 unsigned long max, int top_down)
695{
696 struct purgatory_info *pi = &image->purgatory_info;
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100697 unsigned long align, bss_align, bss_sz, bss_pad;
698 unsigned long entry, load_addr, curr_load_addr, bss_addr, offset;
Dave Younga43cac02015-09-09 15:38:51 -0700699 unsigned char *buf_addr, *src;
700 int i, ret = 0, entry_sidx = -1;
701 const Elf_Shdr *sechdrs_c;
702 Elf_Shdr *sechdrs = NULL;
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100703 struct kexec_buf kbuf = { .image = image, .bufsz = 0, .buf_align = 1,
704 .buf_min = min, .buf_max = max,
705 .top_down = top_down };
Dave Younga43cac02015-09-09 15:38:51 -0700706
707 /*
708 * sechdrs_c points to section headers in purgatory and are read
709 * only. No modifications allowed.
710 */
711 sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
712
713 /*
714 * We can not modify sechdrs_c[] and its fields. It is read only.
715 * Copy it over to a local copy where one can store some temporary
716 * data and free it at the end. We need to modify ->sh_addr and
717 * ->sh_offset fields to keep track of permanent and temporary
718 * locations of sections.
719 */
720 sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
721 if (!sechdrs)
722 return -ENOMEM;
723
724 memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
725
726 /*
727 * We seem to have multiple copies of sections. First copy is which
728 * is embedded in kernel in read only section. Some of these sections
729 * will be copied to a temporary buffer and relocated. And these
730 * sections will finally be copied to their final destination at
731 * segment load time.
732 *
733 * Use ->sh_offset to reflect section address in memory. It will
734 * point to original read only copy if section is not allocatable.
735 * Otherwise it will point to temporary copy which will be relocated.
736 *
737 * Use ->sh_addr to contain final address of the section where it
738 * will go during execution time.
739 */
740 for (i = 0; i < pi->ehdr->e_shnum; i++) {
741 if (sechdrs[i].sh_type == SHT_NOBITS)
742 continue;
743
744 sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
745 sechdrs[i].sh_offset;
746 }
747
748 /*
749 * Identify entry point section and make entry relative to section
750 * start.
751 */
752 entry = pi->ehdr->e_entry;
753 for (i = 0; i < pi->ehdr->e_shnum; i++) {
754 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
755 continue;
756
757 if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
758 continue;
759
760 /* Make entry section relative */
761 if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
762 ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
763 pi->ehdr->e_entry)) {
764 entry_sidx = i;
765 entry -= sechdrs[i].sh_addr;
766 break;
767 }
768 }
769
770 /* Determine how much memory is needed to load relocatable object. */
Dave Younga43cac02015-09-09 15:38:51 -0700771 bss_align = 1;
Dave Younga43cac02015-09-09 15:38:51 -0700772 bss_sz = 0;
773
774 for (i = 0; i < pi->ehdr->e_shnum; i++) {
775 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
776 continue;
777
778 align = sechdrs[i].sh_addralign;
779 if (sechdrs[i].sh_type != SHT_NOBITS) {
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100780 if (kbuf.buf_align < align)
781 kbuf.buf_align = align;
782 kbuf.bufsz = ALIGN(kbuf.bufsz, align);
783 kbuf.bufsz += sechdrs[i].sh_size;
Dave Younga43cac02015-09-09 15:38:51 -0700784 } else {
785 /* bss section */
786 if (bss_align < align)
787 bss_align = align;
788 bss_sz = ALIGN(bss_sz, align);
789 bss_sz += sechdrs[i].sh_size;
790 }
791 }
792
793 /* Determine the bss padding required to align bss properly */
794 bss_pad = 0;
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100795 if (kbuf.bufsz & (bss_align - 1))
796 bss_pad = bss_align - (kbuf.bufsz & (bss_align - 1));
Dave Younga43cac02015-09-09 15:38:51 -0700797
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100798 kbuf.memsz = kbuf.bufsz + bss_pad + bss_sz;
Dave Younga43cac02015-09-09 15:38:51 -0700799
800 /* Allocate buffer for purgatory */
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100801 kbuf.buffer = vzalloc(kbuf.bufsz);
802 if (!kbuf.buffer) {
Dave Younga43cac02015-09-09 15:38:51 -0700803 ret = -ENOMEM;
804 goto out;
805 }
806
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100807 if (kbuf.buf_align < bss_align)
808 kbuf.buf_align = bss_align;
Dave Younga43cac02015-09-09 15:38:51 -0700809
810 /* Add buffer to segment list */
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100811 ret = kexec_add_buffer(&kbuf);
Dave Younga43cac02015-09-09 15:38:51 -0700812 if (ret)
813 goto out;
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100814 pi->purgatory_load_addr = kbuf.mem;
Dave Younga43cac02015-09-09 15:38:51 -0700815
816 /* Load SHF_ALLOC sections */
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100817 buf_addr = kbuf.buffer;
Dave Younga43cac02015-09-09 15:38:51 -0700818 load_addr = curr_load_addr = pi->purgatory_load_addr;
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100819 bss_addr = load_addr + kbuf.bufsz + bss_pad;
Dave Younga43cac02015-09-09 15:38:51 -0700820
821 for (i = 0; i < pi->ehdr->e_shnum; i++) {
822 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
823 continue;
824
825 align = sechdrs[i].sh_addralign;
826 if (sechdrs[i].sh_type != SHT_NOBITS) {
827 curr_load_addr = ALIGN(curr_load_addr, align);
828 offset = curr_load_addr - load_addr;
829 /* We already modifed ->sh_offset to keep src addr */
830 src = (char *) sechdrs[i].sh_offset;
831 memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
832
833 /* Store load address and source address of section */
834 sechdrs[i].sh_addr = curr_load_addr;
835
836 /*
837 * This section got copied to temporary buffer. Update
838 * ->sh_offset accordingly.
839 */
840 sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
841
842 /* Advance to the next address */
843 curr_load_addr += sechdrs[i].sh_size;
844 } else {
845 bss_addr = ALIGN(bss_addr, align);
846 sechdrs[i].sh_addr = bss_addr;
847 bss_addr += sechdrs[i].sh_size;
848 }
849 }
850
851 /* Update entry point based on load address of text section */
852 if (entry_sidx >= 0)
853 entry += sechdrs[entry_sidx].sh_addr;
854
855 /* Make kernel jump to purgatory after shutdown */
856 image->start = entry;
857
858 /* Used later to get/set symbol values */
859 pi->sechdrs = sechdrs;
860
861 /*
862 * Used later to identify which section is purgatory and skip it
863 * from checksumming.
864 */
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100865 pi->purgatory_buf = kbuf.buffer;
Dave Younga43cac02015-09-09 15:38:51 -0700866 return ret;
867out:
868 vfree(sechdrs);
Thiago Jung Bauermannec2b9bf2016-11-29 23:45:48 +1100869 vfree(kbuf.buffer);
Dave Younga43cac02015-09-09 15:38:51 -0700870 return ret;
871}
872
873static int kexec_apply_relocations(struct kimage *image)
874{
875 int i, ret;
876 struct purgatory_info *pi = &image->purgatory_info;
877 Elf_Shdr *sechdrs = pi->sechdrs;
878
879 /* Apply relocations */
880 for (i = 0; i < pi->ehdr->e_shnum; i++) {
881 Elf_Shdr *section, *symtab;
882
883 if (sechdrs[i].sh_type != SHT_RELA &&
884 sechdrs[i].sh_type != SHT_REL)
885 continue;
886
887 /*
888 * For section of type SHT_RELA/SHT_REL,
889 * ->sh_link contains section header index of associated
890 * symbol table. And ->sh_info contains section header
891 * index of section to which relocations apply.
892 */
893 if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
894 sechdrs[i].sh_link >= pi->ehdr->e_shnum)
895 return -ENOEXEC;
896
897 section = &sechdrs[sechdrs[i].sh_info];
898 symtab = &sechdrs[sechdrs[i].sh_link];
899
900 if (!(section->sh_flags & SHF_ALLOC))
901 continue;
902
903 /*
904 * symtab->sh_link contain section header index of associated
905 * string table.
906 */
907 if (symtab->sh_link >= pi->ehdr->e_shnum)
908 /* Invalid section number? */
909 continue;
910
911 /*
912 * Respective architecture needs to provide support for applying
913 * relocations of type SHT_RELA/SHT_REL.
914 */
915 if (sechdrs[i].sh_type == SHT_RELA)
916 ret = arch_kexec_apply_relocations_add(pi->ehdr,
917 sechdrs, i);
918 else if (sechdrs[i].sh_type == SHT_REL)
919 ret = arch_kexec_apply_relocations(pi->ehdr,
920 sechdrs, i);
921 if (ret)
922 return ret;
923 }
924
925 return 0;
926}
927
928/* Load relocatable purgatory object and relocate it appropriately */
929int kexec_load_purgatory(struct kimage *image, unsigned long min,
930 unsigned long max, int top_down,
931 unsigned long *load_addr)
932{
933 struct purgatory_info *pi = &image->purgatory_info;
934 int ret;
935
936 if (kexec_purgatory_size <= 0)
937 return -EINVAL;
938
939 if (kexec_purgatory_size < sizeof(Elf_Ehdr))
940 return -ENOEXEC;
941
942 pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
943
944 if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
945 || pi->ehdr->e_type != ET_REL
946 || !elf_check_arch(pi->ehdr)
947 || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
948 return -ENOEXEC;
949
950 if (pi->ehdr->e_shoff >= kexec_purgatory_size
951 || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
952 kexec_purgatory_size - pi->ehdr->e_shoff))
953 return -ENOEXEC;
954
955 ret = __kexec_load_purgatory(image, min, max, top_down);
956 if (ret)
957 return ret;
958
959 ret = kexec_apply_relocations(image);
960 if (ret)
961 goto out;
962
963 *load_addr = pi->purgatory_load_addr;
964 return 0;
965out:
966 vfree(pi->sechdrs);
Thiago Jung Bauermann070c43e2016-09-01 16:14:44 -0700967 pi->sechdrs = NULL;
968
Dave Younga43cac02015-09-09 15:38:51 -0700969 vfree(pi->purgatory_buf);
Thiago Jung Bauermann070c43e2016-09-01 16:14:44 -0700970 pi->purgatory_buf = NULL;
Dave Younga43cac02015-09-09 15:38:51 -0700971 return ret;
972}
973
974static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
975 const char *name)
976{
977 Elf_Sym *syms;
978 Elf_Shdr *sechdrs;
979 Elf_Ehdr *ehdr;
980 int i, k;
981 const char *strtab;
982
983 if (!pi->sechdrs || !pi->ehdr)
984 return NULL;
985
986 sechdrs = pi->sechdrs;
987 ehdr = pi->ehdr;
988
989 for (i = 0; i < ehdr->e_shnum; i++) {
990 if (sechdrs[i].sh_type != SHT_SYMTAB)
991 continue;
992
993 if (sechdrs[i].sh_link >= ehdr->e_shnum)
994 /* Invalid strtab section number */
995 continue;
996 strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset;
997 syms = (Elf_Sym *)sechdrs[i].sh_offset;
998
999 /* Go through symbols for a match */
1000 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
1001 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
1002 continue;
1003
1004 if (strcmp(strtab + syms[k].st_name, name) != 0)
1005 continue;
1006
1007 if (syms[k].st_shndx == SHN_UNDEF ||
1008 syms[k].st_shndx >= ehdr->e_shnum) {
1009 pr_debug("Symbol: %s has bad section index %d.\n",
1010 name, syms[k].st_shndx);
1011 return NULL;
1012 }
1013
1014 /* Found the symbol we are looking for */
1015 return &syms[k];
1016 }
1017 }
1018
1019 return NULL;
1020}
1021
1022void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
1023{
1024 struct purgatory_info *pi = &image->purgatory_info;
1025 Elf_Sym *sym;
1026 Elf_Shdr *sechdr;
1027
1028 sym = kexec_purgatory_find_symbol(pi, name);
1029 if (!sym)
1030 return ERR_PTR(-EINVAL);
1031
1032 sechdr = &pi->sechdrs[sym->st_shndx];
1033
1034 /*
1035 * Returns the address where symbol will finally be loaded after
1036 * kexec_load_segment()
1037 */
1038 return (void *)(sechdr->sh_addr + sym->st_value);
1039}
1040
1041/*
1042 * Get or set value of a symbol. If "get_value" is true, symbol value is
1043 * returned in buf otherwise symbol value is set based on value in buf.
1044 */
1045int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
1046 void *buf, unsigned int size, bool get_value)
1047{
1048 Elf_Sym *sym;
1049 Elf_Shdr *sechdrs;
1050 struct purgatory_info *pi = &image->purgatory_info;
1051 char *sym_buf;
1052
1053 sym = kexec_purgatory_find_symbol(pi, name);
1054 if (!sym)
1055 return -EINVAL;
1056
1057 if (sym->st_size != size) {
1058 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
1059 name, (unsigned long)sym->st_size, size);
1060 return -EINVAL;
1061 }
1062
1063 sechdrs = pi->sechdrs;
1064
1065 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1066 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
1067 get_value ? "get" : "set");
1068 return -EINVAL;
1069 }
1070
1071 sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset +
1072 sym->st_value;
1073
1074 if (get_value)
1075 memcpy((void *)buf, sym_buf, size);
1076 else
1077 memcpy((void *)sym_buf, buf, size);
1078
1079 return 0;
1080}
AKASHI Takahirob799a092018-04-13 15:35:45 -07001081#endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */