blob: b5e40f06976815e60b96dc8166c333a2562b3565 [file] [log] [blame]
Thomas Gleixner40b0b3f2019-06-03 07:44:46 +02001// SPDX-License-Identifier: GPL-2.0-only
Eric W. Biedermandc009d92005-06-25 14:57:52 -07002/*
Dave Young2965faa2015-09-09 15:38:55 -07003 * kexec.c - kexec_load system call
Eric W. Biedermandc009d92005-06-25 14:57:52 -07004 * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
Eric W. Biedermandc009d92005-06-25 14:57:52 -07005 */
6
Minfei Huangde90a6b2015-11-06 16:32:45 -08007#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
Randy.Dunlapc59ede72006-01-11 12:17:46 -08009#include <linux/capability.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070010#include <linux/mm.h>
11#include <linux/file.h>
Mimi Zohara210fd32018-07-13 14:05:57 -040012#include <linux/security.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070013#include <linux/kexec.h>
Andrew Morton8c5a1cf2008-08-15 00:40:27 -070014#include <linux/mutex.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070015#include <linux/list.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070016#include <linux/syscalls.h>
Dave Younga43cac02015-09-09 15:38:51 -070017#include <linux/vmalloc.h>
Dave Young2965faa2015-09-09 15:38:55 -070018#include <linux/slab.h>
Alexander Nyberg6e274d12005-06-25 14:58:26 -070019
Dave Younga43cac02015-09-09 15:38:51 -070020#include "kexec_internal.h"
21
Vivek Goyal255aedd2014-08-08 14:25:48 -070022static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
23 unsigned long nr_segments,
Arnd Bergmann5d700a02021-09-08 15:18:13 -070024 struct kexec_segment *segments,
Vivek Goyal255aedd2014-08-08 14:25:48 -070025 unsigned long flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -070026{
Vivek Goyal255aedd2014-08-08 14:25:48 -070027 int ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -070028 struct kimage *image;
Vivek Goyal255aedd2014-08-08 14:25:48 -070029 bool kexec_on_panic = flags & KEXEC_ON_CRASH;
30
31 if (kexec_on_panic) {
32 /* Verify we have a valid entry point */
Russell King43546d82016-08-02 14:06:04 -070033 if ((entry < phys_to_boot_phys(crashk_res.start)) ||
34 (entry > phys_to_boot_phys(crashk_res.end)))
Vivek Goyal255aedd2014-08-08 14:25:48 -070035 return -EADDRNOTAVAIL;
36 }
Eric W. Biedermandc009d92005-06-25 14:57:52 -070037
38 /* Allocate and initialize a controlling structure */
Vivek Goyaldabe7862014-08-08 14:25:45 -070039 image = do_kimage_alloc_init();
40 if (!image)
41 return -ENOMEM;
42
43 image->start = entry;
Arnd Bergmann5d700a02021-09-08 15:18:13 -070044 image->nr_segments = nr_segments;
45 memcpy(image->segment, segments, nr_segments * sizeof(*segments));
Vivek Goyaldabe7862014-08-08 14:25:45 -070046
Vivek Goyal255aedd2014-08-08 14:25:48 -070047 if (kexec_on_panic) {
Xunlei Pangcdf4b3f2016-01-20 15:00:31 -080048 /* Enable special crash kernel control page alloc policy. */
Vivek Goyal255aedd2014-08-08 14:25:48 -070049 image->control_page = crashk_res.start;
50 image->type = KEXEC_TYPE_CRASH;
51 }
52
Xunlei Pangcdf4b3f2016-01-20 15:00:31 -080053 ret = sanity_check_segment_list(image);
54 if (ret)
55 goto out_free_image;
56
Eric W. Biedermandc009d92005-06-25 14:57:52 -070057 /*
58 * Find a location for the control code buffer, and add it
59 * the vector of segments so that it's pages will also be
60 * counted as destination pages.
61 */
Vivek Goyal255aedd2014-08-08 14:25:48 -070062 ret = -ENOMEM;
Eric W. Biedermandc009d92005-06-25 14:57:52 -070063 image->control_code_page = kimage_alloc_control_pages(image,
Huang Ying163f6872008-08-15 00:40:22 -070064 get_order(KEXEC_CONTROL_PAGE_SIZE));
Eric W. Biedermandc009d92005-06-25 14:57:52 -070065 if (!image->control_code_page) {
Fabian Fredericke1bebcf2014-06-06 14:37:09 -070066 pr_err("Could not allocate control_code_buffer\n");
Vivek Goyaldabe7862014-08-08 14:25:45 -070067 goto out_free_image;
Eric W. Biedermandc009d92005-06-25 14:57:52 -070068 }
69
Vivek Goyal255aedd2014-08-08 14:25:48 -070070 if (!kexec_on_panic) {
71 image->swap_page = kimage_alloc_control_pages(image, 0);
72 if (!image->swap_page) {
73 pr_err("Could not allocate swap buffer\n");
74 goto out_free_control_pages;
75 }
Huang Ying3ab83522008-07-25 19:45:07 -070076 }
77
Zhang Yanfeib92e7e02013-02-27 17:03:29 -080078 *rimage = image;
79 return 0;
Vivek Goyaldabe7862014-08-08 14:25:45 -070080out_free_control_pages:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -080081 kimage_free_page_list(&image->control_pages);
Vivek Goyaldabe7862014-08-08 14:25:45 -070082out_free_image:
Zhang Yanfeib92e7e02013-02-27 17:03:29 -080083 kfree(image);
Vivek Goyal255aedd2014-08-08 14:25:48 -070084 return ret;
Eric W. Biedermandc009d92005-06-25 14:57:52 -070085}
86
Minfei Huang0eea0862016-05-23 16:24:19 -070087static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
Arnd Bergmann5d700a02021-09-08 15:18:13 -070088 struct kexec_segment *segments, unsigned long flags)
Minfei Huang0eea0862016-05-23 16:24:19 -070089{
90 struct kimage **dest_image, *image;
91 unsigned long i;
92 int ret;
93
Arnd Bergmann4b692e82021-09-08 15:18:10 -070094 /*
95 * Because we write directly to the reserved memory region when loading
96 * crash kernels we need a mutex here to prevent multiple crash kernels
97 * from attempting to load simultaneously, and to prevent a crash kernel
98 * from loading over the top of a in use crash kernel.
99 *
100 * KISS: always take the mutex.
101 */
102 if (!mutex_trylock(&kexec_mutex))
103 return -EBUSY;
104
Minfei Huang0eea0862016-05-23 16:24:19 -0700105 if (flags & KEXEC_ON_CRASH) {
106 dest_image = &kexec_crash_image;
107 if (kexec_crash_image)
108 arch_kexec_unprotect_crashkres();
109 } else {
110 dest_image = &kexec_image;
111 }
112
113 if (nr_segments == 0) {
114 /* Uninstall image */
115 kimage_free(xchg(dest_image, NULL));
Arnd Bergmann4b692e82021-09-08 15:18:10 -0700116 ret = 0;
117 goto out_unlock;
Minfei Huang0eea0862016-05-23 16:24:19 -0700118 }
119 if (flags & KEXEC_ON_CRASH) {
120 /*
121 * Loading another kernel to switch to if this one
122 * crashes. Free any current crash dump kernel before
123 * we corrupt it.
124 */
125 kimage_free(xchg(&kexec_crash_image, NULL));
126 }
127
128 ret = kimage_alloc_init(&image, entry, nr_segments, segments, flags);
129 if (ret)
Arnd Bergmann4b692e82021-09-08 15:18:10 -0700130 goto out_unlock;
Minfei Huang0eea0862016-05-23 16:24:19 -0700131
Minfei Huang0eea0862016-05-23 16:24:19 -0700132 if (flags & KEXEC_PRESERVE_CONTEXT)
133 image->preserve_context = 1;
134
135 ret = machine_kexec_prepare(image);
136 if (ret)
137 goto out;
138
Xunlei Pang12293842017-07-12 14:33:21 -0700139 /*
140 * Some architecture(like S390) may touch the crash memory before
141 * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
142 */
143 ret = kimage_crash_copy_vmcoreinfo(image);
144 if (ret)
145 goto out;
146
Minfei Huang0eea0862016-05-23 16:24:19 -0700147 for (i = 0; i < nr_segments; i++) {
148 ret = kimage_load_segment(image, &image->segment[i]);
149 if (ret)
150 goto out;
151 }
152
153 kimage_terminate(image);
154
Pavel Tatashinde68e4d2019-12-04 10:59:15 -0500155 ret = machine_kexec_post_load(image);
156 if (ret)
157 goto out;
158
Minfei Huang0eea0862016-05-23 16:24:19 -0700159 /* Install the new kernel and uninstall the old */
160 image = xchg(dest_image, image);
161
162out:
163 if ((flags & KEXEC_ON_CRASH) && kexec_crash_image)
164 arch_kexec_protect_crashkres();
165
Minfei Huang0eea0862016-05-23 16:24:19 -0700166 kimage_free(image);
Arnd Bergmann4b692e82021-09-08 15:18:10 -0700167out_unlock:
168 mutex_unlock(&kexec_mutex);
Minfei Huang0eea0862016-05-23 16:24:19 -0700169 return ret;
170}
171
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700172/*
173 * Exec Kernel system call: for obvious reasons only root may call it.
174 *
175 * This call breaks up into three pieces.
176 * - A generic part which loads the new kernel from the current
177 * address space, and very carefully places the data in the
178 * allocated pages.
179 *
180 * - A generic part that interacts with the kernel and tells all of
181 * the devices to shut down. Preventing on-going dmas, and placing
182 * the devices in a consistent state so a later kernel can
183 * reinitialize them.
184 *
185 * - A machine specific part that includes the syscall number
Geert Uytterhoeven002ace72013-09-15 11:35:37 +0200186 * and then copies the image to it's final destination. And
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700187 * jumps into the image at entry.
188 *
189 * kexec does not sync, or unmount filesystems so if you need
190 * that to happen you need to do that yourself.
191 */
Andrew Morton8c5a1cf2008-08-15 00:40:27 -0700192
Dominik Brodowski6b27aef2018-03-17 15:18:30 +0100193static inline int kexec_load_check(unsigned long nr_segments,
194 unsigned long flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700195{
Mimi Zohara210fd32018-07-13 14:05:57 -0400196 int result;
197
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700198 /* We only trust the superuser with rebooting the system. */
Kees Cook79847542014-01-23 15:55:59 -0800199 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700200 return -EPERM;
201
Mimi Zohara210fd32018-07-13 14:05:57 -0400202 /* Permit LSMs and IMA to fail the kexec */
Kees Cookb64fcae2020-10-02 10:38:20 -0700203 result = security_kernel_load_data(LOADING_KEXEC_IMAGE, false);
Mimi Zohara210fd32018-07-13 14:05:57 -0400204 if (result < 0)
205 return result;
206
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700207 /*
Matthew Garrett7d31f462019-08-19 17:17:42 -0700208 * kexec can be used to circumvent module loading restrictions, so
209 * prevent loading in that case
210 */
211 result = security_locked_down(LOCKDOWN_KEXEC);
212 if (result)
213 return result;
214
215 /*
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700216 * Verify we have a legal set of flags
217 * This leaves us room for future extensions.
218 */
219 if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
220 return -EINVAL;
221
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700222 /* Put an artificial cap on the number
223 * of segments passed to kexec_load.
224 */
225 if (nr_segments > KEXEC_SEGMENT_MAX)
226 return -EINVAL;
227
Dominik Brodowski6b27aef2018-03-17 15:18:30 +0100228 return 0;
229}
230
231SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
232 struct kexec_segment __user *, segments, unsigned long, flags)
233{
Arnd Bergmann5d700a02021-09-08 15:18:13 -0700234 struct kexec_segment *ksegments;
235 unsigned long result;
Dominik Brodowski6b27aef2018-03-17 15:18:30 +0100236
237 result = kexec_load_check(nr_segments, flags);
238 if (result)
239 return result;
240
241 /* Verify we are on the appropriate architecture */
242 if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
243 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
244 return -EINVAL;
245
Arnd Bergmann5d700a02021-09-08 15:18:13 -0700246 ksegments = memdup_user(segments, nr_segments * sizeof(ksegments[0]));
247 if (IS_ERR(ksegments))
248 return PTR_ERR(ksegments);
249
250 result = do_kexec_load(entry, nr_segments, ksegments, flags);
251 kfree(ksegments);
Xunlei Pang9b492cf2016-05-23 16:24:10 -0700252
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700253 return result;
254}
255
256#ifdef CONFIG_COMPAT
Heiko Carstensca2c4052014-03-04 17:13:42 +0100257COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
258 compat_ulong_t, nr_segments,
259 struct compat_kexec_segment __user *, segments,
260 compat_ulong_t, flags)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700261{
262 struct compat_kexec_segment in;
Arnd Bergmann5d700a02021-09-08 15:18:13 -0700263 struct kexec_segment *ksegments;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700264 unsigned long i, result;
265
Dominik Brodowski6b27aef2018-03-17 15:18:30 +0100266 result = kexec_load_check(nr_segments, flags);
267 if (result)
268 return result;
269
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700270 /* Don't allow clients that don't understand the native
271 * architecture to do anything.
272 */
Maneesh Soni72414d32005-06-25 14:58:28 -0700273 if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700274 return -EINVAL;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700275
Arnd Bergmann5d700a02021-09-08 15:18:13 -0700276 ksegments = kmalloc_array(nr_segments, sizeof(ksegments[0]),
277 GFP_KERNEL);
278 if (!ksegments)
279 return -ENOMEM;
280
Fabian Fredericke1bebcf2014-06-06 14:37:09 -0700281 for (i = 0; i < nr_segments; i++) {
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700282 result = copy_from_user(&in, &segments[i], sizeof(in));
Maneesh Soni72414d32005-06-25 14:58:28 -0700283 if (result)
Arnd Bergmann5d700a02021-09-08 15:18:13 -0700284 goto fail;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700285
Arnd Bergmann5d700a02021-09-08 15:18:13 -0700286 ksegments[i].buf = compat_ptr(in.buf);
287 ksegments[i].bufsz = in.bufsz;
288 ksegments[i].mem = in.mem;
289 ksegments[i].memsz = in.memsz;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700290 }
291
Dominik Brodowski6b27aef2018-03-17 15:18:30 +0100292 result = do_kexec_load(entry, nr_segments, ksegments, flags);
293
Arnd Bergmann5d700a02021-09-08 15:18:13 -0700294fail:
295 kfree(ksegments);
Dominik Brodowski6b27aef2018-03-17 15:18:30 +0100296 return result;
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700297}
298#endif