Commit | Line | Data |
---|---|---|
40b0b3f8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
dc009d92 | 2 | /* |
2965faa5 | 3 | * kexec.c - kexec_load system call |
dc009d92 | 4 | * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com> |
dc009d92 EB |
5 | */ |
6 | ||
de90a6bc MH |
7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
8 | ||
c59ede7b | 9 | #include <linux/capability.h> |
dc009d92 EB |
10 | #include <linux/mm.h> |
11 | #include <linux/file.h> | |
a210fd32 | 12 | #include <linux/security.h> |
dc009d92 | 13 | #include <linux/kexec.h> |
8c5a1cf0 | 14 | #include <linux/mutex.h> |
dc009d92 | 15 | #include <linux/list.h> |
dc009d92 | 16 | #include <linux/syscalls.h> |
a43cac0d | 17 | #include <linux/vmalloc.h> |
2965faa5 | 18 | #include <linux/slab.h> |
dc009d92 | 19 | |
a43cac0d DY |
20 | #include "kexec_internal.h" |
21 | ||
255aedd9 VG |
22 | static int kimage_alloc_init(struct kimage **rimage, unsigned long entry, |
23 | unsigned long nr_segments, | |
5d700a0f | 24 | struct kexec_segment *segments, |
255aedd9 | 25 | unsigned long flags) |
dc009d92 | 26 | { |
255aedd9 | 27 | int ret; |
dc009d92 | 28 | struct kimage *image; |
255aedd9 VG |
29 | bool kexec_on_panic = flags & KEXEC_ON_CRASH; |
30 | ||
31 | if (kexec_on_panic) { | |
32 | /* Verify we have a valid entry point */ | |
43546d86 RK |
33 | if ((entry < phys_to_boot_phys(crashk_res.start)) || |
34 | (entry > phys_to_boot_phys(crashk_res.end))) | |
255aedd9 VG |
35 | return -EADDRNOTAVAIL; |
36 | } | |
dc009d92 EB |
37 | |
38 | /* Allocate and initialize a controlling structure */ | |
dabe7862 VG |
39 | image = do_kimage_alloc_init(); |
40 | if (!image) | |
41 | return -ENOMEM; | |
42 | ||
43 | image->start = entry; | |
5d700a0f AB |
44 | image->nr_segments = nr_segments; |
45 | memcpy(image->segment, segments, nr_segments * sizeof(*segments)); | |
dabe7862 | 46 | |
255aedd9 | 47 | if (kexec_on_panic) { |
cdf4b3fa | 48 | /* Enable special crash kernel control page alloc policy. */ |
255aedd9 VG |
49 | image->control_page = crashk_res.start; |
50 | image->type = KEXEC_TYPE_CRASH; | |
51 | } | |
52 | ||
cdf4b3fa XP |
53 | ret = sanity_check_segment_list(image); |
54 | if (ret) | |
55 | goto out_free_image; | |
56 | ||
dc009d92 EB |
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 | */ | |
255aedd9 | 62 | ret = -ENOMEM; |
dc009d92 | 63 | image->control_code_page = kimage_alloc_control_pages(image, |
163f6876 | 64 | get_order(KEXEC_CONTROL_PAGE_SIZE)); |
dc009d92 | 65 | if (!image->control_code_page) { |
e1bebcf4 | 66 | pr_err("Could not allocate control_code_buffer\n"); |
dabe7862 | 67 | goto out_free_image; |
dc009d92 EB |
68 | } |
69 | ||
255aedd9 VG |
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 | } | |
3ab83521 HY |
76 | } |
77 | ||
b92e7e0d ZY |
78 | *rimage = image; |
79 | return 0; | |
dabe7862 | 80 | out_free_control_pages: |
b92e7e0d | 81 | kimage_free_page_list(&image->control_pages); |
dabe7862 | 82 | out_free_image: |
b92e7e0d | 83 | kfree(image); |
255aedd9 | 84 | return ret; |
dc009d92 EB |
85 | } |
86 | ||
0eea0867 | 87 | static int do_kexec_load(unsigned long entry, unsigned long nr_segments, |
5d700a0f | 88 | struct kexec_segment *segments, unsigned long flags) |
0eea0867 MH |
89 | { |
90 | struct kimage **dest_image, *image; | |
91 | unsigned long i; | |
92 | int ret; | |
93 | ||
4b692e86 AB |
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 | ||
0eea0867 MH |
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)); | |
4b692e86 AB |
116 | ret = 0; |
117 | goto out_unlock; | |
0eea0867 MH |
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) | |
4b692e86 | 130 | goto out_unlock; |
0eea0867 | 131 | |
0eea0867 MH |
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 | ||
1229384f XP |
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 | ||
0eea0867 MH |
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 | ||
de68e4da PT |
155 | ret = machine_kexec_post_load(image); |
156 | if (ret) | |
157 | goto out; | |
158 | ||
0eea0867 MH |
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 | ||
0eea0867 | 166 | kimage_free(image); |
4b692e86 AB |
167 | out_unlock: |
168 | mutex_unlock(&kexec_mutex); | |
0eea0867 MH |
169 | return ret; |
170 | } | |
171 | ||
dc009d92 EB |
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 | |
002ace78 | 186 | * and then copies the image to it's final destination. And |
dc009d92 EB |
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 | */ | |
8c5a1cf0 | 192 | |
6b27aef0 DB |
193 | static inline int kexec_load_check(unsigned long nr_segments, |
194 | unsigned long flags) | |
dc009d92 | 195 | { |
a210fd32 MZ |
196 | int result; |
197 | ||
dc009d92 | 198 | /* We only trust the superuser with rebooting the system. */ |
7984754b | 199 | if (!capable(CAP_SYS_BOOT) || kexec_load_disabled) |
dc009d92 EB |
200 | return -EPERM; |
201 | ||
a210fd32 | 202 | /* Permit LSMs and IMA to fail the kexec */ |
b64fcae7 | 203 | result = security_kernel_load_data(LOADING_KEXEC_IMAGE, false); |
a210fd32 MZ |
204 | if (result < 0) |
205 | return result; | |
206 | ||
7d31f460 MG |
207 | /* |
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 | ||
dc009d92 EB |
215 | /* |
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 | ||
dc009d92 EB |
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 | ||
6b27aef0 DB |
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 | { | |
5d700a0f AB |
234 | struct kexec_segment *ksegments; |
235 | unsigned long result; | |
6b27aef0 DB |
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 | ||
5d700a0f AB |
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); | |
dc009d92 | 252 | |
dc009d92 EB |
253 | return result; |
254 | } | |
255 | ||
256 | #ifdef CONFIG_COMPAT | |
ca2c405a HC |
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) | |
dc009d92 EB |
261 | { |
262 | struct compat_kexec_segment in; | |
5d700a0f | 263 | struct kexec_segment *ksegments; |
dc009d92 EB |
264 | unsigned long i, result; |
265 | ||
6b27aef0 DB |
266 | result = kexec_load_check(nr_segments, flags); |
267 | if (result) | |
268 | return result; | |
269 | ||
dc009d92 EB |
270 | /* Don't allow clients that don't understand the native |
271 | * architecture to do anything. | |
272 | */ | |
72414d3f | 273 | if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT) |
dc009d92 | 274 | return -EINVAL; |
dc009d92 | 275 | |
5d700a0f AB |
276 | ksegments = kmalloc_array(nr_segments, sizeof(ksegments[0]), |
277 | GFP_KERNEL); | |
278 | if (!ksegments) | |
279 | return -ENOMEM; | |
280 | ||
e1bebcf4 | 281 | for (i = 0; i < nr_segments; i++) { |
dc009d92 | 282 | result = copy_from_user(&in, &segments[i], sizeof(in)); |
72414d3f | 283 | if (result) |
5d700a0f | 284 | goto fail; |
dc009d92 | 285 | |
5d700a0f AB |
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; | |
dc009d92 EB |
290 | } |
291 | ||
6b27aef0 DB |
292 | result = do_kexec_load(entry, nr_segments, ksegments, flags); |
293 | ||
5d700a0f AB |
294 | fail: |
295 | kfree(ksegments); | |
6b27aef0 | 296 | return result; |
dc009d92 EB |
297 | } |
298 | #endif |