Thomas Gleixner | 40b0b3f | 2019-06-03 07:44:46 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 2 | /* |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 3 | * kexec.c - kexec_load system call |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 4 | * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com> |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Minfei Huang | de90a6b | 2015-11-06 16:32:45 -0800 | [diff] [blame] | 7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 8 | |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 9 | #include <linux/capability.h> |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 10 | #include <linux/mm.h> |
| 11 | #include <linux/file.h> |
Mimi Zohar | a210fd3 | 2018-07-13 14:05:57 -0400 | [diff] [blame] | 12 | #include <linux/security.h> |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 13 | #include <linux/kexec.h> |
Andrew Morton | 8c5a1cf | 2008-08-15 00:40:27 -0700 | [diff] [blame] | 14 | #include <linux/mutex.h> |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 15 | #include <linux/list.h> |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 16 | #include <linux/syscalls.h> |
Dave Young | a43cac0 | 2015-09-09 15:38:51 -0700 | [diff] [blame] | 17 | #include <linux/vmalloc.h> |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 18 | #include <linux/slab.h> |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 19 | |
Dave Young | a43cac0 | 2015-09-09 15:38:51 -0700 | [diff] [blame] | 20 | #include "kexec_internal.h" |
| 21 | |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame] | 22 | static int kimage_alloc_init(struct kimage **rimage, unsigned long entry, |
| 23 | unsigned long nr_segments, |
Arnd Bergmann | 5d700a0 | 2021-09-08 15:18:13 -0700 | [diff] [blame] | 24 | struct kexec_segment *segments, |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame] | 25 | unsigned long flags) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 26 | { |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame] | 27 | int ret; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 28 | struct kimage *image; |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame] | 29 | bool kexec_on_panic = flags & KEXEC_ON_CRASH; |
| 30 | |
| 31 | if (kexec_on_panic) { |
| 32 | /* Verify we have a valid entry point */ |
Russell King | 43546d8 | 2016-08-02 14:06:04 -0700 | [diff] [blame] | 33 | if ((entry < phys_to_boot_phys(crashk_res.start)) || |
| 34 | (entry > phys_to_boot_phys(crashk_res.end))) |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame] | 35 | return -EADDRNOTAVAIL; |
| 36 | } |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 37 | |
| 38 | /* Allocate and initialize a controlling structure */ |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 39 | image = do_kimage_alloc_init(); |
| 40 | if (!image) |
| 41 | return -ENOMEM; |
| 42 | |
| 43 | image->start = entry; |
Arnd Bergmann | 5d700a0 | 2021-09-08 15:18:13 -0700 | [diff] [blame] | 44 | image->nr_segments = nr_segments; |
| 45 | memcpy(image->segment, segments, nr_segments * sizeof(*segments)); |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 46 | |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame] | 47 | if (kexec_on_panic) { |
Xunlei Pang | cdf4b3f | 2016-01-20 15:00:31 -0800 | [diff] [blame] | 48 | /* Enable special crash kernel control page alloc policy. */ |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame] | 49 | image->control_page = crashk_res.start; |
| 50 | image->type = KEXEC_TYPE_CRASH; |
| 51 | } |
| 52 | |
Xunlei Pang | cdf4b3f | 2016-01-20 15:00:31 -0800 | [diff] [blame] | 53 | ret = sanity_check_segment_list(image); |
| 54 | if (ret) |
| 55 | goto out_free_image; |
| 56 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 57 | /* |
| 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 Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame] | 62 | ret = -ENOMEM; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 63 | image->control_code_page = kimage_alloc_control_pages(image, |
Huang Ying | 163f687 | 2008-08-15 00:40:22 -0700 | [diff] [blame] | 64 | get_order(KEXEC_CONTROL_PAGE_SIZE)); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 65 | if (!image->control_code_page) { |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 66 | pr_err("Could not allocate control_code_buffer\n"); |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 67 | goto out_free_image; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame] | 70 | 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 Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Zhang Yanfei | b92e7e0 | 2013-02-27 17:03:29 -0800 | [diff] [blame] | 78 | *rimage = image; |
| 79 | return 0; |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 80 | out_free_control_pages: |
Zhang Yanfei | b92e7e0 | 2013-02-27 17:03:29 -0800 | [diff] [blame] | 81 | kimage_free_page_list(&image->control_pages); |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 82 | out_free_image: |
Zhang Yanfei | b92e7e0 | 2013-02-27 17:03:29 -0800 | [diff] [blame] | 83 | kfree(image); |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame] | 84 | return ret; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Minfei Huang | 0eea086 | 2016-05-23 16:24:19 -0700 | [diff] [blame] | 87 | static int do_kexec_load(unsigned long entry, unsigned long nr_segments, |
Arnd Bergmann | 5d700a0 | 2021-09-08 15:18:13 -0700 | [diff] [blame] | 88 | struct kexec_segment *segments, unsigned long flags) |
Minfei Huang | 0eea086 | 2016-05-23 16:24:19 -0700 | [diff] [blame] | 89 | { |
| 90 | struct kimage **dest_image, *image; |
| 91 | unsigned long i; |
| 92 | int ret; |
| 93 | |
Arnd Bergmann | 4b692e8 | 2021-09-08 15:18:10 -0700 | [diff] [blame] | 94 | /* |
| 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 Huang | 0eea086 | 2016-05-23 16:24:19 -0700 | [diff] [blame] | 105 | 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 Bergmann | 4b692e8 | 2021-09-08 15:18:10 -0700 | [diff] [blame] | 116 | ret = 0; |
| 117 | goto out_unlock; |
Minfei Huang | 0eea086 | 2016-05-23 16:24:19 -0700 | [diff] [blame] | 118 | } |
| 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 Bergmann | 4b692e8 | 2021-09-08 15:18:10 -0700 | [diff] [blame] | 130 | goto out_unlock; |
Minfei Huang | 0eea086 | 2016-05-23 16:24:19 -0700 | [diff] [blame] | 131 | |
Minfei Huang | 0eea086 | 2016-05-23 16:24:19 -0700 | [diff] [blame] | 132 | 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 Pang | 1229384 | 2017-07-12 14:33:21 -0700 | [diff] [blame] | 139 | /* |
| 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 Huang | 0eea086 | 2016-05-23 16:24:19 -0700 | [diff] [blame] | 147 | 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 Tatashin | de68e4d | 2019-12-04 10:59:15 -0500 | [diff] [blame] | 155 | ret = machine_kexec_post_load(image); |
| 156 | if (ret) |
| 157 | goto out; |
| 158 | |
Minfei Huang | 0eea086 | 2016-05-23 16:24:19 -0700 | [diff] [blame] | 159 | /* Install the new kernel and uninstall the old */ |
| 160 | image = xchg(dest_image, image); |
| 161 | |
| 162 | out: |
| 163 | if ((flags & KEXEC_ON_CRASH) && kexec_crash_image) |
| 164 | arch_kexec_protect_crashkres(); |
| 165 | |
Minfei Huang | 0eea086 | 2016-05-23 16:24:19 -0700 | [diff] [blame] | 166 | kimage_free(image); |
Arnd Bergmann | 4b692e8 | 2021-09-08 15:18:10 -0700 | [diff] [blame] | 167 | out_unlock: |
| 168 | mutex_unlock(&kexec_mutex); |
Minfei Huang | 0eea086 | 2016-05-23 16:24:19 -0700 | [diff] [blame] | 169 | return ret; |
| 170 | } |
| 171 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 172 | /* |
| 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 Uytterhoeven | 002ace7 | 2013-09-15 11:35:37 +0200 | [diff] [blame] | 186 | * and then copies the image to it's final destination. And |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 187 | * 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 Morton | 8c5a1cf | 2008-08-15 00:40:27 -0700 | [diff] [blame] | 192 | |
Dominik Brodowski | 6b27aef | 2018-03-17 15:18:30 +0100 | [diff] [blame] | 193 | static inline int kexec_load_check(unsigned long nr_segments, |
| 194 | unsigned long flags) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 195 | { |
Mimi Zohar | a210fd3 | 2018-07-13 14:05:57 -0400 | [diff] [blame] | 196 | int result; |
| 197 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 198 | /* We only trust the superuser with rebooting the system. */ |
Kees Cook | 7984754 | 2014-01-23 15:55:59 -0800 | [diff] [blame] | 199 | if (!capable(CAP_SYS_BOOT) || kexec_load_disabled) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 200 | return -EPERM; |
| 201 | |
Mimi Zohar | a210fd3 | 2018-07-13 14:05:57 -0400 | [diff] [blame] | 202 | /* Permit LSMs and IMA to fail the kexec */ |
Kees Cook | b64fcae | 2020-10-02 10:38:20 -0700 | [diff] [blame] | 203 | result = security_kernel_load_data(LOADING_KEXEC_IMAGE, false); |
Mimi Zohar | a210fd3 | 2018-07-13 14:05:57 -0400 | [diff] [blame] | 204 | if (result < 0) |
| 205 | return result; |
| 206 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 207 | /* |
Matthew Garrett | 7d31f46 | 2019-08-19 17:17:42 -0700 | [diff] [blame] | 208 | * 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. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 216 | * 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. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 222 | /* 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 Brodowski | 6b27aef | 2018-03-17 15:18:30 +0100 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments, |
| 232 | struct kexec_segment __user *, segments, unsigned long, flags) |
| 233 | { |
Arnd Bergmann | 5d700a0 | 2021-09-08 15:18:13 -0700 | [diff] [blame] | 234 | struct kexec_segment *ksegments; |
| 235 | unsigned long result; |
Dominik Brodowski | 6b27aef | 2018-03-17 15:18:30 +0100 | [diff] [blame] | 236 | |
| 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 Bergmann | 5d700a0 | 2021-09-08 15:18:13 -0700 | [diff] [blame] | 246 | 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 Pang | 9b492cf | 2016-05-23 16:24:10 -0700 | [diff] [blame] | 252 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 253 | return result; |
| 254 | } |
| 255 | |
| 256 | #ifdef CONFIG_COMPAT |
Heiko Carstens | ca2c405 | 2014-03-04 17:13:42 +0100 | [diff] [blame] | 257 | COMPAT_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. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 261 | { |
| 262 | struct compat_kexec_segment in; |
Arnd Bergmann | 5d700a0 | 2021-09-08 15:18:13 -0700 | [diff] [blame] | 263 | struct kexec_segment *ksegments; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 264 | unsigned long i, result; |
| 265 | |
Dominik Brodowski | 6b27aef | 2018-03-17 15:18:30 +0100 | [diff] [blame] | 266 | result = kexec_load_check(nr_segments, flags); |
| 267 | if (result) |
| 268 | return result; |
| 269 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 270 | /* Don't allow clients that don't understand the native |
| 271 | * architecture to do anything. |
| 272 | */ |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 273 | if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 274 | return -EINVAL; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 275 | |
Arnd Bergmann | 5d700a0 | 2021-09-08 15:18:13 -0700 | [diff] [blame] | 276 | ksegments = kmalloc_array(nr_segments, sizeof(ksegments[0]), |
| 277 | GFP_KERNEL); |
| 278 | if (!ksegments) |
| 279 | return -ENOMEM; |
| 280 | |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 281 | for (i = 0; i < nr_segments; i++) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 282 | result = copy_from_user(&in, &segments[i], sizeof(in)); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 283 | if (result) |
Arnd Bergmann | 5d700a0 | 2021-09-08 15:18:13 -0700 | [diff] [blame] | 284 | goto fail; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 285 | |
Arnd Bergmann | 5d700a0 | 2021-09-08 15:18:13 -0700 | [diff] [blame] | 286 | 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. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Dominik Brodowski | 6b27aef | 2018-03-17 15:18:30 +0100 | [diff] [blame] | 292 | result = do_kexec_load(entry, nr_segments, ksegments, flags); |
| 293 | |
Arnd Bergmann | 5d700a0 | 2021-09-08 15:18:13 -0700 | [diff] [blame] | 294 | fail: |
| 295 | kfree(ksegments); |
Dominik Brodowski | 6b27aef | 2018-03-17 15:18:30 +0100 | [diff] [blame] | 296 | return result; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 297 | } |
| 298 | #endif |