2019-06-03 13:44:46 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-06-26 05:57:52 +08:00
|
|
|
/*
|
2015-09-10 06:38:55 +08:00
|
|
|
* kexec.c - kexec_load system call
|
2005-06-26 05:57:52 +08:00
|
|
|
* Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
|
|
|
|
*/
|
|
|
|
|
kexec: use file name as the output message prefix
kexec output message misses the prefix "kexec", when Dave Young split the
kexec code. Now, we use file name as the output message prefix.
Currently, the format of output message:
[ 140.290795] SYSC_kexec_load: hello, world
[ 140.291534] kexec: sanity_check_segment_list: hello, world
Ideally, the format of output message:
[ 30.791503] kexec: SYSC_kexec_load, Hello, world
[ 79.182752] kexec_core: sanity_check_segment_list, Hello, world
Remove the custom prefix "kexec" in output message.
Signed-off-by: Minfei Huang <mnfhuang@gmail.com>
Acked-by: Dave Young <dyoung@redhat.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-11-07 08:32:45 +08:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2006-01-12 04:17:46 +08:00
|
|
|
#include <linux/capability.h>
|
2005-06-26 05:57:52 +08:00
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/file.h>
|
2018-07-14 02:05:57 +08:00
|
|
|
#include <linux/security.h>
|
2005-06-26 05:57:52 +08:00
|
|
|
#include <linux/kexec.h>
|
2008-08-15 15:40:27 +08:00
|
|
|
#include <linux/mutex.h>
|
2005-06-26 05:57:52 +08:00
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/syscalls.h>
|
2015-09-10 06:38:51 +08:00
|
|
|
#include <linux/vmalloc.h>
|
2015-09-10 06:38:55 +08:00
|
|
|
#include <linux/slab.h>
|
2005-06-26 05:57:52 +08:00
|
|
|
|
2015-09-10 06:38:51 +08:00
|
|
|
#include "kexec_internal.h"
|
|
|
|
|
2014-08-09 05:25:48 +08:00
|
|
|
static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
|
|
|
|
unsigned long nr_segments,
|
2021-09-09 06:18:13 +08:00
|
|
|
struct kexec_segment *segments,
|
2014-08-09 05:25:48 +08:00
|
|
|
unsigned long flags)
|
2005-06-26 05:57:52 +08:00
|
|
|
{
|
2014-08-09 05:25:48 +08:00
|
|
|
int ret;
|
2005-06-26 05:57:52 +08:00
|
|
|
struct kimage *image;
|
2014-08-09 05:25:48 +08:00
|
|
|
bool kexec_on_panic = flags & KEXEC_ON_CRASH;
|
|
|
|
|
|
|
|
if (kexec_on_panic) {
|
|
|
|
/* Verify we have a valid entry point */
|
2016-08-03 05:06:04 +08:00
|
|
|
if ((entry < phys_to_boot_phys(crashk_res.start)) ||
|
|
|
|
(entry > phys_to_boot_phys(crashk_res.end)))
|
2014-08-09 05:25:48 +08:00
|
|
|
return -EADDRNOTAVAIL;
|
|
|
|
}
|
2005-06-26 05:57:52 +08:00
|
|
|
|
|
|
|
/* Allocate and initialize a controlling structure */
|
2014-08-09 05:25:45 +08:00
|
|
|
image = do_kimage_alloc_init();
|
|
|
|
if (!image)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
image->start = entry;
|
2021-09-09 06:18:13 +08:00
|
|
|
image->nr_segments = nr_segments;
|
|
|
|
memcpy(image->segment, segments, nr_segments * sizeof(*segments));
|
2014-08-09 05:25:45 +08:00
|
|
|
|
2014-08-09 05:25:48 +08:00
|
|
|
if (kexec_on_panic) {
|
2016-01-21 07:00:31 +08:00
|
|
|
/* Enable special crash kernel control page alloc policy. */
|
2014-08-09 05:25:48 +08:00
|
|
|
image->control_page = crashk_res.start;
|
|
|
|
image->type = KEXEC_TYPE_CRASH;
|
|
|
|
}
|
|
|
|
|
2016-01-21 07:00:31 +08:00
|
|
|
ret = sanity_check_segment_list(image);
|
|
|
|
if (ret)
|
|
|
|
goto out_free_image;
|
|
|
|
|
2005-06-26 05:57:52 +08:00
|
|
|
/*
|
|
|
|
* Find a location for the control code buffer, and add it
|
|
|
|
* the vector of segments so that it's pages will also be
|
|
|
|
* counted as destination pages.
|
|
|
|
*/
|
2014-08-09 05:25:48 +08:00
|
|
|
ret = -ENOMEM;
|
2005-06-26 05:57:52 +08:00
|
|
|
image->control_code_page = kimage_alloc_control_pages(image,
|
2008-08-15 15:40:22 +08:00
|
|
|
get_order(KEXEC_CONTROL_PAGE_SIZE));
|
2005-06-26 05:57:52 +08:00
|
|
|
if (!image->control_code_page) {
|
2014-06-07 05:37:09 +08:00
|
|
|
pr_err("Could not allocate control_code_buffer\n");
|
2014-08-09 05:25:45 +08:00
|
|
|
goto out_free_image;
|
2005-06-26 05:57:52 +08:00
|
|
|
}
|
|
|
|
|
2014-08-09 05:25:48 +08:00
|
|
|
if (!kexec_on_panic) {
|
|
|
|
image->swap_page = kimage_alloc_control_pages(image, 0);
|
|
|
|
if (!image->swap_page) {
|
|
|
|
pr_err("Could not allocate swap buffer\n");
|
|
|
|
goto out_free_control_pages;
|
|
|
|
}
|
2008-07-26 10:45:07 +08:00
|
|
|
}
|
|
|
|
|
2013-02-28 09:03:29 +08:00
|
|
|
*rimage = image;
|
|
|
|
return 0;
|
2014-08-09 05:25:45 +08:00
|
|
|
out_free_control_pages:
|
2013-02-28 09:03:29 +08:00
|
|
|
kimage_free_page_list(&image->control_pages);
|
2014-08-09 05:25:45 +08:00
|
|
|
out_free_image:
|
2013-02-28 09:03:29 +08:00
|
|
|
kfree(image);
|
2014-08-09 05:25:48 +08:00
|
|
|
return ret;
|
2005-06-26 05:57:52 +08:00
|
|
|
}
|
|
|
|
|
2016-05-24 07:24:19 +08:00
|
|
|
static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
|
2021-09-09 06:18:13 +08:00
|
|
|
struct kexec_segment *segments, unsigned long flags)
|
2016-05-24 07:24:19 +08:00
|
|
|
{
|
|
|
|
struct kimage **dest_image, *image;
|
|
|
|
unsigned long i;
|
|
|
|
int ret;
|
|
|
|
|
2021-09-09 06:18:10 +08:00
|
|
|
/*
|
|
|
|
* Because we write directly to the reserved memory region when loading
|
2022-07-01 06:32:58 +08:00
|
|
|
* crash kernels we need a serialization here to prevent multiple crash
|
|
|
|
* kernels from attempting to load simultaneously.
|
2021-09-09 06:18:10 +08:00
|
|
|
*/
|
2022-07-01 06:32:58 +08:00
|
|
|
if (!kexec_trylock())
|
2021-09-09 06:18:10 +08:00
|
|
|
return -EBUSY;
|
|
|
|
|
2016-05-24 07:24:19 +08:00
|
|
|
if (flags & KEXEC_ON_CRASH) {
|
|
|
|
dest_image = &kexec_crash_image;
|
|
|
|
if (kexec_crash_image)
|
|
|
|
arch_kexec_unprotect_crashkres();
|
|
|
|
} else {
|
|
|
|
dest_image = &kexec_image;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nr_segments == 0) {
|
|
|
|
/* Uninstall image */
|
|
|
|
kimage_free(xchg(dest_image, NULL));
|
2021-09-09 06:18:10 +08:00
|
|
|
ret = 0;
|
|
|
|
goto out_unlock;
|
2016-05-24 07:24:19 +08:00
|
|
|
}
|
|
|
|
if (flags & KEXEC_ON_CRASH) {
|
|
|
|
/*
|
|
|
|
* Loading another kernel to switch to if this one
|
|
|
|
* crashes. Free any current crash dump kernel before
|
|
|
|
* we corrupt it.
|
|
|
|
*/
|
|
|
|
kimage_free(xchg(&kexec_crash_image, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = kimage_alloc_init(&image, entry, nr_segments, segments, flags);
|
|
|
|
if (ret)
|
2021-09-09 06:18:10 +08:00
|
|
|
goto out_unlock;
|
2016-05-24 07:24:19 +08:00
|
|
|
|
|
|
|
if (flags & KEXEC_PRESERVE_CONTEXT)
|
|
|
|
image->preserve_context = 1;
|
|
|
|
|
|
|
|
ret = machine_kexec_prepare(image);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
kdump: protect vmcoreinfo data under the crash memory
Currently vmcoreinfo data is updated at boot time subsys_initcall(), it
has the risk of being modified by some wrong code during system is
running.
As a result, vmcore dumped may contain the wrong vmcoreinfo. Later on,
when using "crash", "makedumpfile", etc utility to parse this vmcore, we
probably will get "Segmentation fault" or other unexpected errors.
E.g. 1) wrong code overwrites vmcoreinfo_data; 2) further crashes the
system; 3) trigger kdump, then we obviously will fail to recognize the
crash context correctly due to the corrupted vmcoreinfo.
Now except for vmcoreinfo, all the crash data is well
protected(including the cpu note which is fully updated in the crash
path, thus its correctness is guaranteed). Given that vmcoreinfo data
is a large chunk prepared for kdump, we better protect it as well.
To solve this, we relocate and copy vmcoreinfo_data to the crash memory
when kdump is loading via kexec syscalls. Because the whole crash
memory will be protected by existing arch_kexec_protect_crashkres()
mechanism, we naturally protect vmcoreinfo_data from write(even read)
access under kernel direct mapping after kdump is loaded.
Since kdump is usually loaded at the very early stage after boot, we can
trust the correctness of the vmcoreinfo data copied.
On the other hand, we still need to operate the vmcoreinfo safe copy
when crash happens to generate vmcoreinfo_note again, we rely on vmap()
to map out a new kernel virtual address and update to use this new one
instead in the following crash_save_vmcoreinfo().
BTW, we do not touch vmcoreinfo_note, because it will be fully updated
using the protected vmcoreinfo_data after crash which is surely correct
just like the cpu crash note.
Link: http://lkml.kernel.org/r/1493281021-20737-3-git-send-email-xlpang@redhat.com
Signed-off-by: Xunlei Pang <xlpang@redhat.com>
Tested-by: Michael Holzheu <holzheu@linux.vnet.ibm.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Dave Young <dyoung@redhat.com>
Cc: Eric Biederman <ebiederm@xmission.com>
Cc: Hari Bathini <hbathini@linux.vnet.ibm.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-07-13 05:33:21 +08:00
|
|
|
/*
|
|
|
|
* Some architecture(like S390) may touch the crash memory before
|
|
|
|
* machine_kexec_prepare(), we must copy vmcoreinfo data after it.
|
|
|
|
*/
|
|
|
|
ret = kimage_crash_copy_vmcoreinfo(image);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2016-05-24 07:24:19 +08:00
|
|
|
for (i = 0; i < nr_segments; i++) {
|
|
|
|
ret = kimage_load_segment(image, &image->segment[i]);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
kimage_terminate(image);
|
|
|
|
|
2019-12-04 23:59:15 +08:00
|
|
|
ret = machine_kexec_post_load(image);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2016-05-24 07:24:19 +08:00
|
|
|
/* Install the new kernel and uninstall the old */
|
|
|
|
image = xchg(dest_image, image);
|
|
|
|
|
|
|
|
out:
|
|
|
|
if ((flags & KEXEC_ON_CRASH) && kexec_crash_image)
|
|
|
|
arch_kexec_protect_crashkres();
|
|
|
|
|
|
|
|
kimage_free(image);
|
2021-09-09 06:18:10 +08:00
|
|
|
out_unlock:
|
2022-07-01 06:32:58 +08:00
|
|
|
kexec_unlock();
|
2016-05-24 07:24:19 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-06-26 05:57:52 +08:00
|
|
|
/*
|
|
|
|
* Exec Kernel system call: for obvious reasons only root may call it.
|
|
|
|
*
|
|
|
|
* This call breaks up into three pieces.
|
|
|
|
* - A generic part which loads the new kernel from the current
|
|
|
|
* address space, and very carefully places the data in the
|
|
|
|
* allocated pages.
|
|
|
|
*
|
|
|
|
* - A generic part that interacts with the kernel and tells all of
|
|
|
|
* the devices to shut down. Preventing on-going dmas, and placing
|
|
|
|
* the devices in a consistent state so a later kernel can
|
|
|
|
* reinitialize them.
|
|
|
|
*
|
|
|
|
* - A machine specific part that includes the syscall number
|
2013-09-15 17:35:37 +08:00
|
|
|
* and then copies the image to it's final destination. And
|
2005-06-26 05:57:52 +08:00
|
|
|
* jumps into the image at entry.
|
|
|
|
*
|
|
|
|
* kexec does not sync, or unmount filesystems so if you need
|
|
|
|
* that to happen you need to do that yourself.
|
|
|
|
*/
|
2008-08-15 15:40:27 +08:00
|
|
|
|
2018-03-17 22:18:30 +08:00
|
|
|
static inline int kexec_load_check(unsigned long nr_segments,
|
|
|
|
unsigned long flags)
|
2005-06-26 05:57:52 +08:00
|
|
|
{
|
2018-07-14 02:05:57 +08:00
|
|
|
int result;
|
|
|
|
|
2005-06-26 05:57:52 +08:00
|
|
|
/* We only trust the superuser with rebooting the system. */
|
2023-01-04 22:38:47 +08:00
|
|
|
if (!kexec_load_permitted())
|
2005-06-26 05:57:52 +08:00
|
|
|
return -EPERM;
|
|
|
|
|
2018-07-14 02:05:57 +08:00
|
|
|
/* Permit LSMs and IMA to fail the kexec */
|
2020-10-03 01:38:20 +08:00
|
|
|
result = security_kernel_load_data(LOADING_KEXEC_IMAGE, false);
|
2018-07-14 02:05:57 +08:00
|
|
|
if (result < 0)
|
|
|
|
return result;
|
|
|
|
|
2019-08-20 08:17:42 +08:00
|
|
|
/*
|
|
|
|
* kexec can be used to circumvent module loading restrictions, so
|
|
|
|
* prevent loading in that case
|
|
|
|
*/
|
|
|
|
result = security_locked_down(LOCKDOWN_KEXEC);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
2005-06-26 05:57:52 +08:00
|
|
|
/*
|
|
|
|
* Verify we have a legal set of flags
|
|
|
|
* This leaves us room for future extensions.
|
|
|
|
*/
|
|
|
|
if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Put an artificial cap on the number
|
|
|
|
* of segments passed to kexec_load.
|
|
|
|
*/
|
|
|
|
if (nr_segments > KEXEC_SEGMENT_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2018-03-17 22:18:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
|
|
|
|
struct kexec_segment __user *, segments, unsigned long, flags)
|
|
|
|
{
|
2021-09-09 06:18:13 +08:00
|
|
|
struct kexec_segment *ksegments;
|
|
|
|
unsigned long result;
|
2018-03-17 22:18:30 +08:00
|
|
|
|
|
|
|
result = kexec_load_check(nr_segments, flags);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
/* Verify we are on the appropriate architecture */
|
|
|
|
if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
|
|
|
|
((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2021-09-09 06:18:13 +08:00
|
|
|
ksegments = memdup_user(segments, nr_segments * sizeof(ksegments[0]));
|
|
|
|
if (IS_ERR(ksegments))
|
|
|
|
return PTR_ERR(ksegments);
|
|
|
|
|
|
|
|
result = do_kexec_load(entry, nr_segments, ksegments, flags);
|
|
|
|
kfree(ksegments);
|
2005-06-26 05:57:52 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_COMPAT
|
2014-03-05 00:13:42 +08:00
|
|
|
COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
|
|
|
|
compat_ulong_t, nr_segments,
|
|
|
|
struct compat_kexec_segment __user *, segments,
|
|
|
|
compat_ulong_t, flags)
|
2005-06-26 05:57:52 +08:00
|
|
|
{
|
|
|
|
struct compat_kexec_segment in;
|
2021-09-09 06:18:13 +08:00
|
|
|
struct kexec_segment *ksegments;
|
2005-06-26 05:57:52 +08:00
|
|
|
unsigned long i, result;
|
|
|
|
|
2018-03-17 22:18:30 +08:00
|
|
|
result = kexec_load_check(nr_segments, flags);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
2005-06-26 05:57:52 +08:00
|
|
|
/* Don't allow clients that don't understand the native
|
|
|
|
* architecture to do anything.
|
|
|
|
*/
|
2005-06-26 05:58:28 +08:00
|
|
|
if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
|
2005-06-26 05:57:52 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2021-09-09 06:18:13 +08:00
|
|
|
ksegments = kmalloc_array(nr_segments, sizeof(ksegments[0]),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!ksegments)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-06-07 05:37:09 +08:00
|
|
|
for (i = 0; i < nr_segments; i++) {
|
2005-06-26 05:57:52 +08:00
|
|
|
result = copy_from_user(&in, &segments[i], sizeof(in));
|
2005-06-26 05:58:28 +08:00
|
|
|
if (result)
|
2021-09-09 06:18:13 +08:00
|
|
|
goto fail;
|
2005-06-26 05:57:52 +08:00
|
|
|
|
2021-09-09 06:18:13 +08:00
|
|
|
ksegments[i].buf = compat_ptr(in.buf);
|
|
|
|
ksegments[i].bufsz = in.bufsz;
|
|
|
|
ksegments[i].mem = in.mem;
|
|
|
|
ksegments[i].memsz = in.memsz;
|
2005-06-26 05:57:52 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 22:18:30 +08:00
|
|
|
result = do_kexec_load(entry, nr_segments, ksegments, flags);
|
|
|
|
|
2021-09-09 06:18:13 +08:00
|
|
|
fail:
|
|
|
|
kfree(ksegments);
|
2018-03-17 22:18:30 +08:00
|
|
|
return result;
|
2005-06-26 05:57:52 +08:00
|
|
|
}
|
|
|
|
#endif
|