kexec_file.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kexec: kexec_file_load system call
  4. *
  5. * Copyright (C) 2014 Red Hat Inc.
  6. * Authors:
  7. * Vivek Goyal <vgoyal@redhat.com>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/capability.h>
  11. #include <linux/mm.h>
  12. #include <linux/file.h>
  13. #include <linux/slab.h>
  14. #include <linux/kexec.h>
  15. #include <linux/memblock.h>
  16. #include <linux/mutex.h>
  17. #include <linux/list.h>
  18. #include <linux/fs.h>
  19. #include <linux/ima.h>
  20. #include <crypto/hash.h>
  21. #include <crypto/sha2.h>
  22. #include <linux/elf.h>
  23. #include <linux/elfcore.h>
  24. #include <linux/kernel.h>
  25. #include <linux/kernel_read_file.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/vmalloc.h>
  28. #include "kexec_internal.h"
  29. #ifdef CONFIG_KEXEC_SIG
  30. static bool sig_enforce = IS_ENABLED(CONFIG_KEXEC_SIG_FORCE);
  31. void set_kexec_sig_enforced(void)
  32. {
  33. sig_enforce = true;
  34. }
  35. #endif
  36. static int kexec_calculate_store_digests(struct kimage *image);
  37. /* Maximum size in bytes for kernel/initrd files. */
  38. #define KEXEC_FILE_SIZE_MAX min_t(s64, 4LL << 30, SSIZE_MAX)
  39. /*
  40. * Currently this is the only default function that is exported as some
  41. * architectures need it to do additional handlings.
  42. * In the future, other default functions may be exported too if required.
  43. */
  44. int kexec_image_probe_default(struct kimage *image, void *buf,
  45. unsigned long buf_len)
  46. {
  47. const struct kexec_file_ops * const *fops;
  48. int ret = -ENOEXEC;
  49. for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
  50. ret = (*fops)->probe(buf, buf_len);
  51. if (!ret) {
  52. image->fops = *fops;
  53. return ret;
  54. }
  55. }
  56. return ret;
  57. }
  58. static void *kexec_image_load_default(struct kimage *image)
  59. {
  60. if (!image->fops || !image->fops->load)
  61. return ERR_PTR(-ENOEXEC);
  62. return image->fops->load(image, image->kernel_buf,
  63. image->kernel_buf_len, image->initrd_buf,
  64. image->initrd_buf_len, image->cmdline_buf,
  65. image->cmdline_buf_len);
  66. }
  67. int kexec_image_post_load_cleanup_default(struct kimage *image)
  68. {
  69. if (!image->fops || !image->fops->cleanup)
  70. return 0;
  71. return image->fops->cleanup(image->image_loader_data);
  72. }
  73. /*
  74. * Free up memory used by kernel, initrd, and command line. This is temporary
  75. * memory allocation which is not needed any more after these buffers have
  76. * been loaded into separate segments and have been copied elsewhere.
  77. */
  78. void kimage_file_post_load_cleanup(struct kimage *image)
  79. {
  80. struct purgatory_info *pi = &image->purgatory_info;
  81. vfree(image->kernel_buf);
  82. image->kernel_buf = NULL;
  83. vfree(image->initrd_buf);
  84. image->initrd_buf = NULL;
  85. kfree(image->cmdline_buf);
  86. image->cmdline_buf = NULL;
  87. vfree(pi->purgatory_buf);
  88. pi->purgatory_buf = NULL;
  89. vfree(pi->sechdrs);
  90. pi->sechdrs = NULL;
  91. #ifdef CONFIG_IMA_KEXEC
  92. vfree(image->ima_buffer);
  93. image->ima_buffer = NULL;
  94. #endif /* CONFIG_IMA_KEXEC */
  95. /* See if architecture has anything to cleanup post load */
  96. arch_kimage_file_post_load_cleanup(image);
  97. /*
  98. * Above call should have called into bootloader to free up
  99. * any data stored in kimage->image_loader_data. It should
  100. * be ok now to free it up.
  101. */
  102. kfree(image->image_loader_data);
  103. image->image_loader_data = NULL;
  104. kexec_file_dbg_print = false;
  105. }
  106. #ifdef CONFIG_KEXEC_SIG
  107. #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
  108. int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len)
  109. {
  110. int ret;
  111. ret = verify_pefile_signature(kernel, kernel_len,
  112. VERIFY_USE_SECONDARY_KEYRING,
  113. VERIFYING_KEXEC_PE_SIGNATURE);
  114. if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) {
  115. ret = verify_pefile_signature(kernel, kernel_len,
  116. VERIFY_USE_PLATFORM_KEYRING,
  117. VERIFYING_KEXEC_PE_SIGNATURE);
  118. }
  119. return ret;
  120. }
  121. #endif
  122. static int kexec_image_verify_sig(struct kimage *image, void *buf,
  123. unsigned long buf_len)
  124. {
  125. if (!image->fops || !image->fops->verify_sig) {
  126. pr_debug("kernel loader does not support signature verification.\n");
  127. return -EKEYREJECTED;
  128. }
  129. return image->fops->verify_sig(buf, buf_len);
  130. }
  131. static int
  132. kimage_validate_signature(struct kimage *image)
  133. {
  134. int ret;
  135. ret = kexec_image_verify_sig(image, image->kernel_buf,
  136. image->kernel_buf_len);
  137. if (ret) {
  138. if (sig_enforce) {
  139. pr_notice("Enforced kernel signature verification failed (%d).\n", ret);
  140. return ret;
  141. }
  142. /*
  143. * If IMA is guaranteed to appraise a signature on the kexec
  144. * image, permit it even if the kernel is otherwise locked
  145. * down.
  146. */
  147. if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
  148. security_locked_down(LOCKDOWN_KEXEC))
  149. return -EPERM;
  150. pr_debug("kernel signature verification failed (%d).\n", ret);
  151. }
  152. return 0;
  153. }
  154. #endif
  155. /*
  156. * In file mode list of segments is prepared by kernel. Copy relevant
  157. * data from user space, do error checking, prepare segment list
  158. */
  159. static int
  160. kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
  161. const char __user *cmdline_ptr,
  162. unsigned long cmdline_len, unsigned flags)
  163. {
  164. ssize_t ret;
  165. void *ldata;
  166. ret = kernel_read_file_from_fd(kernel_fd, 0, &image->kernel_buf,
  167. KEXEC_FILE_SIZE_MAX, NULL,
  168. READING_KEXEC_IMAGE);
  169. if (ret < 0)
  170. return ret;
  171. image->kernel_buf_len = ret;
  172. kexec_dprintk("kernel: %p kernel_size: %#lx\n",
  173. image->kernel_buf, image->kernel_buf_len);
  174. /* Call arch image probe handlers */
  175. ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
  176. image->kernel_buf_len);
  177. if (ret)
  178. goto out;
  179. #ifdef CONFIG_KEXEC_SIG
  180. ret = kimage_validate_signature(image);
  181. if (ret)
  182. goto out;
  183. #endif
  184. /* It is possible that there no initramfs is being loaded */
  185. if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
  186. ret = kernel_read_file_from_fd(initrd_fd, 0, &image->initrd_buf,
  187. KEXEC_FILE_SIZE_MAX, NULL,
  188. READING_KEXEC_INITRAMFS);
  189. if (ret < 0)
  190. goto out;
  191. image->initrd_buf_len = ret;
  192. ret = 0;
  193. }
  194. if (cmdline_len) {
  195. image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);
  196. if (IS_ERR(image->cmdline_buf)) {
  197. ret = PTR_ERR(image->cmdline_buf);
  198. image->cmdline_buf = NULL;
  199. goto out;
  200. }
  201. image->cmdline_buf_len = cmdline_len;
  202. /* command line should be a string with last byte null */
  203. if (image->cmdline_buf[cmdline_len - 1] != '\0') {
  204. ret = -EINVAL;
  205. goto out;
  206. }
  207. ima_kexec_cmdline(kernel_fd, image->cmdline_buf,
  208. image->cmdline_buf_len - 1);
  209. }
  210. /* IMA needs to pass the measurement list to the next kernel. */
  211. ima_add_kexec_buffer(image);
  212. /* Call image load handler */
  213. ldata = kexec_image_load_default(image);
  214. if (IS_ERR(ldata)) {
  215. ret = PTR_ERR(ldata);
  216. goto out;
  217. }
  218. image->image_loader_data = ldata;
  219. out:
  220. /* In case of error, free up all allocated memory in this function */
  221. if (ret)
  222. kimage_file_post_load_cleanup(image);
  223. return ret;
  224. }
  225. static int
  226. kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
  227. int initrd_fd, const char __user *cmdline_ptr,
  228. unsigned long cmdline_len, unsigned long flags)
  229. {
  230. int ret;
  231. struct kimage *image;
  232. bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
  233. image = do_kimage_alloc_init();
  234. if (!image)
  235. return -ENOMEM;
  236. kexec_file_dbg_print = !!(flags & KEXEC_FILE_DEBUG);
  237. image->file_mode = 1;
  238. #ifdef CONFIG_CRASH_DUMP
  239. if (kexec_on_panic) {
  240. /* Enable special crash kernel control page alloc policy. */
  241. image->control_page = crashk_res.start;
  242. image->type = KEXEC_TYPE_CRASH;
  243. }
  244. #endif
  245. ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
  246. cmdline_ptr, cmdline_len, flags);
  247. if (ret)
  248. goto out_free_image;
  249. ret = sanity_check_segment_list(image);
  250. if (ret)
  251. goto out_free_post_load_bufs;
  252. ret = -ENOMEM;
  253. image->control_code_page = kimage_alloc_control_pages(image,
  254. get_order(KEXEC_CONTROL_PAGE_SIZE));
  255. if (!image->control_code_page) {
  256. pr_err("Could not allocate control_code_buffer\n");
  257. goto out_free_post_load_bufs;
  258. }
  259. if (!kexec_on_panic) {
  260. image->swap_page = kimage_alloc_control_pages(image, 0);
  261. if (!image->swap_page) {
  262. pr_err("Could not allocate swap buffer\n");
  263. goto out_free_control_pages;
  264. }
  265. }
  266. *rimage = image;
  267. return 0;
  268. out_free_control_pages:
  269. kimage_free_page_list(&image->control_pages);
  270. out_free_post_load_bufs:
  271. kimage_file_post_load_cleanup(image);
  272. out_free_image:
  273. kfree(image);
  274. return ret;
  275. }
  276. SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
  277. unsigned long, cmdline_len, const char __user *, cmdline_ptr,
  278. unsigned long, flags)
  279. {
  280. int image_type = (flags & KEXEC_FILE_ON_CRASH) ?
  281. KEXEC_TYPE_CRASH : KEXEC_TYPE_DEFAULT;
  282. struct kimage **dest_image, *image;
  283. int ret = 0, i;
  284. /* We only trust the superuser with rebooting the system. */
  285. if (!kexec_load_permitted(image_type))
  286. return -EPERM;
  287. /* Make sure we have a legal set of flags */
  288. if (flags != (flags & KEXEC_FILE_FLAGS))
  289. return -EINVAL;
  290. image = NULL;
  291. if (!kexec_trylock())
  292. return -EBUSY;
  293. #ifdef CONFIG_CRASH_DUMP
  294. if (image_type == KEXEC_TYPE_CRASH) {
  295. dest_image = &kexec_crash_image;
  296. if (kexec_crash_image)
  297. arch_kexec_unprotect_crashkres();
  298. } else
  299. #endif
  300. dest_image = &kexec_image;
  301. if (flags & KEXEC_FILE_UNLOAD)
  302. goto exchange;
  303. /*
  304. * In case of crash, new kernel gets loaded in reserved region. It is
  305. * same memory where old crash kernel might be loaded. Free any
  306. * current crash dump kernel before we corrupt it.
  307. */
  308. if (flags & KEXEC_FILE_ON_CRASH)
  309. kimage_free(xchg(&kexec_crash_image, NULL));
  310. ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
  311. cmdline_len, flags);
  312. if (ret)
  313. goto out;
  314. #ifdef CONFIG_CRASH_HOTPLUG
  315. if ((flags & KEXEC_FILE_ON_CRASH) && arch_crash_hotplug_support(image, flags))
  316. image->hotplug_support = 1;
  317. #endif
  318. ret = machine_kexec_prepare(image);
  319. if (ret)
  320. goto out;
  321. /*
  322. * Some architecture(like S390) may touch the crash memory before
  323. * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
  324. */
  325. ret = kimage_crash_copy_vmcoreinfo(image);
  326. if (ret)
  327. goto out;
  328. ret = kexec_calculate_store_digests(image);
  329. if (ret)
  330. goto out;
  331. kexec_dprintk("nr_segments = %lu\n", image->nr_segments);
  332. for (i = 0; i < image->nr_segments; i++) {
  333. struct kexec_segment *ksegment;
  334. ksegment = &image->segment[i];
  335. kexec_dprintk("segment[%d]: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
  336. i, ksegment->buf, ksegment->bufsz, ksegment->mem,
  337. ksegment->memsz);
  338. ret = kimage_load_segment(image, &image->segment[i]);
  339. if (ret)
  340. goto out;
  341. }
  342. kimage_terminate(image);
  343. ret = machine_kexec_post_load(image);
  344. if (ret)
  345. goto out;
  346. kexec_dprintk("kexec_file_load: type:%u, start:0x%lx head:0x%lx flags:0x%lx\n",
  347. image->type, image->start, image->head, flags);
  348. /*
  349. * Free up any temporary buffers allocated which are not needed
  350. * after image has been loaded
  351. */
  352. kimage_file_post_load_cleanup(image);
  353. exchange:
  354. image = xchg(dest_image, image);
  355. out:
  356. #ifdef CONFIG_CRASH_DUMP
  357. if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
  358. arch_kexec_protect_crashkres();
  359. #endif
  360. kexec_unlock();
  361. kimage_free(image);
  362. return ret;
  363. }
  364. static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
  365. struct kexec_buf *kbuf)
  366. {
  367. struct kimage *image = kbuf->image;
  368. unsigned long temp_start, temp_end;
  369. temp_end = min(end, kbuf->buf_max);
  370. temp_start = temp_end - kbuf->memsz + 1;
  371. do {
  372. /* align down start */
  373. temp_start = ALIGN_DOWN(temp_start, kbuf->buf_align);
  374. if (temp_start < start || temp_start < kbuf->buf_min)
  375. return 0;
  376. temp_end = temp_start + kbuf->memsz - 1;
  377. /*
  378. * Make sure this does not conflict with any of existing
  379. * segments
  380. */
  381. if (kimage_is_destination_range(image, temp_start, temp_end)) {
  382. temp_start = temp_start - PAGE_SIZE;
  383. continue;
  384. }
  385. /* We found a suitable memory range */
  386. break;
  387. } while (1);
  388. /* If we are here, we found a suitable memory range */
  389. kbuf->mem = temp_start;
  390. /* Success, stop navigating through remaining System RAM ranges */
  391. return 1;
  392. }
  393. static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
  394. struct kexec_buf *kbuf)
  395. {
  396. struct kimage *image = kbuf->image;
  397. unsigned long temp_start, temp_end;
  398. temp_start = max(start, kbuf->buf_min);
  399. do {
  400. temp_start = ALIGN(temp_start, kbuf->buf_align);
  401. temp_end = temp_start + kbuf->memsz - 1;
  402. if (temp_end > end || temp_end > kbuf->buf_max)
  403. return 0;
  404. /*
  405. * Make sure this does not conflict with any of existing
  406. * segments
  407. */
  408. if (kimage_is_destination_range(image, temp_start, temp_end)) {
  409. temp_start = temp_start + PAGE_SIZE;
  410. continue;
  411. }
  412. /* We found a suitable memory range */
  413. break;
  414. } while (1);
  415. /* If we are here, we found a suitable memory range */
  416. kbuf->mem = temp_start;
  417. /* Success, stop navigating through remaining System RAM ranges */
  418. return 1;
  419. }
  420. static int locate_mem_hole_callback(struct resource *res, void *arg)
  421. {
  422. struct kexec_buf *kbuf = (struct kexec_buf *)arg;
  423. u64 start = res->start, end = res->end;
  424. unsigned long sz = end - start + 1;
  425. /* Returning 0 will take to next memory range */
  426. /* Don't use memory that will be detected and handled by a driver. */
  427. if (res->flags & IORESOURCE_SYSRAM_DRIVER_MANAGED)
  428. return 0;
  429. if (sz < kbuf->memsz)
  430. return 0;
  431. if (end < kbuf->buf_min || start > kbuf->buf_max)
  432. return 0;
  433. /*
  434. * Allocate memory top down with-in ram range. Otherwise bottom up
  435. * allocation.
  436. */
  437. if (kbuf->top_down)
  438. return locate_mem_hole_top_down(start, end, kbuf);
  439. return locate_mem_hole_bottom_up(start, end, kbuf);
  440. }
  441. #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
  442. static int kexec_walk_memblock(struct kexec_buf *kbuf,
  443. int (*func)(struct resource *, void *))
  444. {
  445. int ret = 0;
  446. u64 i;
  447. phys_addr_t mstart, mend;
  448. struct resource res = { };
  449. #ifdef CONFIG_CRASH_DUMP
  450. if (kbuf->image->type == KEXEC_TYPE_CRASH)
  451. return func(&crashk_res, kbuf);
  452. #endif
  453. /*
  454. * Using MEMBLOCK_NONE will properly skip MEMBLOCK_DRIVER_MANAGED. See
  455. * IORESOURCE_SYSRAM_DRIVER_MANAGED handling in
  456. * locate_mem_hole_callback().
  457. */
  458. if (kbuf->top_down) {
  459. for_each_free_mem_range_reverse(i, NUMA_NO_NODE, MEMBLOCK_NONE,
  460. &mstart, &mend, NULL) {
  461. /*
  462. * In memblock, end points to the first byte after the
  463. * range while in kexec, end points to the last byte
  464. * in the range.
  465. */
  466. res.start = mstart;
  467. res.end = mend - 1;
  468. ret = func(&res, kbuf);
  469. if (ret)
  470. break;
  471. }
  472. } else {
  473. for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
  474. &mstart, &mend, NULL) {
  475. /*
  476. * In memblock, end points to the first byte after the
  477. * range while in kexec, end points to the last byte
  478. * in the range.
  479. */
  480. res.start = mstart;
  481. res.end = mend - 1;
  482. ret = func(&res, kbuf);
  483. if (ret)
  484. break;
  485. }
  486. }
  487. return ret;
  488. }
  489. #else
  490. static int kexec_walk_memblock(struct kexec_buf *kbuf,
  491. int (*func)(struct resource *, void *))
  492. {
  493. return 0;
  494. }
  495. #endif
  496. /**
  497. * kexec_walk_resources - call func(data) on free memory regions
  498. * @kbuf: Context info for the search. Also passed to @func.
  499. * @func: Function to call for each memory region.
  500. *
  501. * Return: The memory walk will stop when func returns a non-zero value
  502. * and that value will be returned. If all free regions are visited without
  503. * func returning non-zero, then zero will be returned.
  504. */
  505. static int kexec_walk_resources(struct kexec_buf *kbuf,
  506. int (*func)(struct resource *, void *))
  507. {
  508. #ifdef CONFIG_CRASH_DUMP
  509. if (kbuf->image->type == KEXEC_TYPE_CRASH)
  510. return walk_iomem_res_desc(crashk_res.desc,
  511. IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
  512. crashk_res.start, crashk_res.end,
  513. kbuf, func);
  514. #endif
  515. if (kbuf->top_down)
  516. return walk_system_ram_res_rev(0, ULONG_MAX, kbuf, func);
  517. else
  518. return walk_system_ram_res(0, ULONG_MAX, kbuf, func);
  519. }
  520. /**
  521. * kexec_locate_mem_hole - find free memory for the purgatory or the next kernel
  522. * @kbuf: Parameters for the memory search.
  523. *
  524. * On success, kbuf->mem will have the start address of the memory region found.
  525. *
  526. * Return: 0 on success, negative errno on error.
  527. */
  528. int kexec_locate_mem_hole(struct kexec_buf *kbuf)
  529. {
  530. int ret;
  531. /* Arch knows where to place */
  532. if (kbuf->mem != KEXEC_BUF_MEM_UNKNOWN)
  533. return 0;
  534. if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
  535. ret = kexec_walk_resources(kbuf, locate_mem_hole_callback);
  536. else
  537. ret = kexec_walk_memblock(kbuf, locate_mem_hole_callback);
  538. return ret == 1 ? 0 : -EADDRNOTAVAIL;
  539. }
  540. /**
  541. * kexec_add_buffer - place a buffer in a kexec segment
  542. * @kbuf: Buffer contents and memory parameters.
  543. *
  544. * This function assumes that kexec_lock is held.
  545. * On successful return, @kbuf->mem will have the physical address of
  546. * the buffer in memory.
  547. *
  548. * Return: 0 on success, negative errno on error.
  549. */
  550. int kexec_add_buffer(struct kexec_buf *kbuf)
  551. {
  552. struct kexec_segment *ksegment;
  553. int ret;
  554. /* Currently adding segment this way is allowed only in file mode */
  555. if (!kbuf->image->file_mode)
  556. return -EINVAL;
  557. if (kbuf->image->nr_segments >= KEXEC_SEGMENT_MAX)
  558. return -EINVAL;
  559. /*
  560. * Make sure we are not trying to add buffer after allocating
  561. * control pages. All segments need to be placed first before
  562. * any control pages are allocated. As control page allocation
  563. * logic goes through list of segments to make sure there are
  564. * no destination overlaps.
  565. */
  566. if (!list_empty(&kbuf->image->control_pages)) {
  567. WARN_ON(1);
  568. return -EINVAL;
  569. }
  570. /* Ensure minimum alignment needed for segments. */
  571. kbuf->memsz = ALIGN(kbuf->memsz, PAGE_SIZE);
  572. kbuf->buf_align = max(kbuf->buf_align, PAGE_SIZE);
  573. /* Walk the RAM ranges and allocate a suitable range for the buffer */
  574. ret = arch_kexec_locate_mem_hole(kbuf);
  575. if (ret)
  576. return ret;
  577. /* Found a suitable memory range */
  578. ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
  579. ksegment->kbuf = kbuf->buffer;
  580. ksegment->bufsz = kbuf->bufsz;
  581. ksegment->mem = kbuf->mem;
  582. ksegment->memsz = kbuf->memsz;
  583. kbuf->image->nr_segments++;
  584. return 0;
  585. }
  586. /* Calculate and store the digest of segments */
  587. static int kexec_calculate_store_digests(struct kimage *image)
  588. {
  589. struct crypto_shash *tfm;
  590. struct shash_desc *desc;
  591. int ret = 0, i, j, zero_buf_sz, sha_region_sz;
  592. size_t desc_size, nullsz;
  593. char *digest;
  594. void *zero_buf;
  595. struct kexec_sha_region *sha_regions;
  596. struct purgatory_info *pi = &image->purgatory_info;
  597. if (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY))
  598. return 0;
  599. zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
  600. zero_buf_sz = PAGE_SIZE;
  601. tfm = crypto_alloc_shash("sha256", 0, 0);
  602. if (IS_ERR(tfm)) {
  603. ret = PTR_ERR(tfm);
  604. goto out;
  605. }
  606. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  607. desc = kzalloc(desc_size, GFP_KERNEL);
  608. if (!desc) {
  609. ret = -ENOMEM;
  610. goto out_free_tfm;
  611. }
  612. sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
  613. sha_regions = vzalloc(sha_region_sz);
  614. if (!sha_regions) {
  615. ret = -ENOMEM;
  616. goto out_free_desc;
  617. }
  618. desc->tfm = tfm;
  619. ret = crypto_shash_init(desc);
  620. if (ret < 0)
  621. goto out_free_sha_regions;
  622. digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
  623. if (!digest) {
  624. ret = -ENOMEM;
  625. goto out_free_sha_regions;
  626. }
  627. for (j = i = 0; i < image->nr_segments; i++) {
  628. struct kexec_segment *ksegment;
  629. #ifdef CONFIG_CRASH_HOTPLUG
  630. /* Exclude elfcorehdr segment to allow future changes via hotplug */
  631. if (i == image->elfcorehdr_index)
  632. continue;
  633. #endif
  634. ksegment = &image->segment[i];
  635. /*
  636. * Skip purgatory as it will be modified once we put digest
  637. * info in purgatory.
  638. */
  639. if (ksegment->kbuf == pi->purgatory_buf)
  640. continue;
  641. ret = crypto_shash_update(desc, ksegment->kbuf,
  642. ksegment->bufsz);
  643. if (ret)
  644. break;
  645. /*
  646. * Assume rest of the buffer is filled with zero and
  647. * update digest accordingly.
  648. */
  649. nullsz = ksegment->memsz - ksegment->bufsz;
  650. while (nullsz) {
  651. unsigned long bytes = nullsz;
  652. if (bytes > zero_buf_sz)
  653. bytes = zero_buf_sz;
  654. ret = crypto_shash_update(desc, zero_buf, bytes);
  655. if (ret)
  656. break;
  657. nullsz -= bytes;
  658. }
  659. if (ret)
  660. break;
  661. sha_regions[j].start = ksegment->mem;
  662. sha_regions[j].len = ksegment->memsz;
  663. j++;
  664. }
  665. if (!ret) {
  666. ret = crypto_shash_final(desc, digest);
  667. if (ret)
  668. goto out_free_digest;
  669. ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
  670. sha_regions, sha_region_sz, 0);
  671. if (ret)
  672. goto out_free_digest;
  673. ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
  674. digest, SHA256_DIGEST_SIZE, 0);
  675. if (ret)
  676. goto out_free_digest;
  677. }
  678. out_free_digest:
  679. kfree(digest);
  680. out_free_sha_regions:
  681. vfree(sha_regions);
  682. out_free_desc:
  683. kfree(desc);
  684. out_free_tfm:
  685. kfree(tfm);
  686. out:
  687. return ret;
  688. }
  689. #ifdef CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY
  690. /*
  691. * kexec_purgatory_setup_kbuf - prepare buffer to load purgatory.
  692. * @pi: Purgatory to be loaded.
  693. * @kbuf: Buffer to setup.
  694. *
  695. * Allocates the memory needed for the buffer. Caller is responsible to free
  696. * the memory after use.
  697. *
  698. * Return: 0 on success, negative errno on error.
  699. */
  700. static int kexec_purgatory_setup_kbuf(struct purgatory_info *pi,
  701. struct kexec_buf *kbuf)
  702. {
  703. const Elf_Shdr *sechdrs;
  704. unsigned long bss_align;
  705. unsigned long bss_sz;
  706. unsigned long align;
  707. int i, ret;
  708. sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
  709. kbuf->buf_align = bss_align = 1;
  710. kbuf->bufsz = bss_sz = 0;
  711. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  712. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  713. continue;
  714. align = sechdrs[i].sh_addralign;
  715. if (sechdrs[i].sh_type != SHT_NOBITS) {
  716. if (kbuf->buf_align < align)
  717. kbuf->buf_align = align;
  718. kbuf->bufsz = ALIGN(kbuf->bufsz, align);
  719. kbuf->bufsz += sechdrs[i].sh_size;
  720. } else {
  721. if (bss_align < align)
  722. bss_align = align;
  723. bss_sz = ALIGN(bss_sz, align);
  724. bss_sz += sechdrs[i].sh_size;
  725. }
  726. }
  727. kbuf->bufsz = ALIGN(kbuf->bufsz, bss_align);
  728. kbuf->memsz = kbuf->bufsz + bss_sz;
  729. if (kbuf->buf_align < bss_align)
  730. kbuf->buf_align = bss_align;
  731. kbuf->buffer = vzalloc(kbuf->bufsz);
  732. if (!kbuf->buffer)
  733. return -ENOMEM;
  734. pi->purgatory_buf = kbuf->buffer;
  735. ret = kexec_add_buffer(kbuf);
  736. if (ret)
  737. goto out;
  738. return 0;
  739. out:
  740. vfree(pi->purgatory_buf);
  741. pi->purgatory_buf = NULL;
  742. return ret;
  743. }
  744. /*
  745. * kexec_purgatory_setup_sechdrs - prepares the pi->sechdrs buffer.
  746. * @pi: Purgatory to be loaded.
  747. * @kbuf: Buffer prepared to store purgatory.
  748. *
  749. * Allocates the memory needed for the buffer. Caller is responsible to free
  750. * the memory after use.
  751. *
  752. * Return: 0 on success, negative errno on error.
  753. */
  754. static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
  755. struct kexec_buf *kbuf)
  756. {
  757. unsigned long bss_addr;
  758. unsigned long offset;
  759. size_t sechdrs_size;
  760. Elf_Shdr *sechdrs;
  761. int i;
  762. /*
  763. * The section headers in kexec_purgatory are read-only. In order to
  764. * have them modifiable make a temporary copy.
  765. */
  766. sechdrs_size = array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum);
  767. sechdrs = vzalloc(sechdrs_size);
  768. if (!sechdrs)
  769. return -ENOMEM;
  770. memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff, sechdrs_size);
  771. pi->sechdrs = sechdrs;
  772. offset = 0;
  773. bss_addr = kbuf->mem + kbuf->bufsz;
  774. kbuf->image->start = pi->ehdr->e_entry;
  775. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  776. unsigned long align;
  777. void *src, *dst;
  778. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  779. continue;
  780. align = sechdrs[i].sh_addralign;
  781. if (sechdrs[i].sh_type == SHT_NOBITS) {
  782. bss_addr = ALIGN(bss_addr, align);
  783. sechdrs[i].sh_addr = bss_addr;
  784. bss_addr += sechdrs[i].sh_size;
  785. continue;
  786. }
  787. offset = ALIGN(offset, align);
  788. /*
  789. * Check if the segment contains the entry point, if so,
  790. * calculate the value of image->start based on it.
  791. * If the compiler has produced more than one .text section
  792. * (Eg: .text.hot), they are generally after the main .text
  793. * section, and they shall not be used to calculate
  794. * image->start. So do not re-calculate image->start if it
  795. * is not set to the initial value, and warn the user so they
  796. * have a chance to fix their purgatory's linker script.
  797. */
  798. if (sechdrs[i].sh_flags & SHF_EXECINSTR &&
  799. pi->ehdr->e_entry >= sechdrs[i].sh_addr &&
  800. pi->ehdr->e_entry < (sechdrs[i].sh_addr
  801. + sechdrs[i].sh_size) &&
  802. !WARN_ON(kbuf->image->start != pi->ehdr->e_entry)) {
  803. kbuf->image->start -= sechdrs[i].sh_addr;
  804. kbuf->image->start += kbuf->mem + offset;
  805. }
  806. src = (void *)pi->ehdr + sechdrs[i].sh_offset;
  807. dst = pi->purgatory_buf + offset;
  808. memcpy(dst, src, sechdrs[i].sh_size);
  809. sechdrs[i].sh_addr = kbuf->mem + offset;
  810. sechdrs[i].sh_offset = offset;
  811. offset += sechdrs[i].sh_size;
  812. }
  813. return 0;
  814. }
  815. static int kexec_apply_relocations(struct kimage *image)
  816. {
  817. int i, ret;
  818. struct purgatory_info *pi = &image->purgatory_info;
  819. const Elf_Shdr *sechdrs;
  820. sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
  821. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  822. const Elf_Shdr *relsec;
  823. const Elf_Shdr *symtab;
  824. Elf_Shdr *section;
  825. relsec = sechdrs + i;
  826. if (relsec->sh_type != SHT_RELA &&
  827. relsec->sh_type != SHT_REL)
  828. continue;
  829. /*
  830. * For section of type SHT_RELA/SHT_REL,
  831. * ->sh_link contains section header index of associated
  832. * symbol table. And ->sh_info contains section header
  833. * index of section to which relocations apply.
  834. */
  835. if (relsec->sh_info >= pi->ehdr->e_shnum ||
  836. relsec->sh_link >= pi->ehdr->e_shnum)
  837. return -ENOEXEC;
  838. section = pi->sechdrs + relsec->sh_info;
  839. symtab = sechdrs + relsec->sh_link;
  840. if (!(section->sh_flags & SHF_ALLOC))
  841. continue;
  842. /*
  843. * symtab->sh_link contain section header index of associated
  844. * string table.
  845. */
  846. if (symtab->sh_link >= pi->ehdr->e_shnum)
  847. /* Invalid section number? */
  848. continue;
  849. /*
  850. * Respective architecture needs to provide support for applying
  851. * relocations of type SHT_RELA/SHT_REL.
  852. */
  853. if (relsec->sh_type == SHT_RELA)
  854. ret = arch_kexec_apply_relocations_add(pi, section,
  855. relsec, symtab);
  856. else if (relsec->sh_type == SHT_REL)
  857. ret = arch_kexec_apply_relocations(pi, section,
  858. relsec, symtab);
  859. if (ret)
  860. return ret;
  861. }
  862. return 0;
  863. }
  864. /*
  865. * kexec_load_purgatory - Load and relocate the purgatory object.
  866. * @image: Image to add the purgatory to.
  867. * @kbuf: Memory parameters to use.
  868. *
  869. * Allocates the memory needed for image->purgatory_info.sechdrs and
  870. * image->purgatory_info.purgatory_buf/kbuf->buffer. Caller is responsible
  871. * to free the memory after use.
  872. *
  873. * Return: 0 on success, negative errno on error.
  874. */
  875. int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf)
  876. {
  877. struct purgatory_info *pi = &image->purgatory_info;
  878. int ret;
  879. if (kexec_purgatory_size <= 0)
  880. return -EINVAL;
  881. pi->ehdr = (const Elf_Ehdr *)kexec_purgatory;
  882. ret = kexec_purgatory_setup_kbuf(pi, kbuf);
  883. if (ret)
  884. return ret;
  885. ret = kexec_purgatory_setup_sechdrs(pi, kbuf);
  886. if (ret)
  887. goto out_free_kbuf;
  888. ret = kexec_apply_relocations(image);
  889. if (ret)
  890. goto out;
  891. return 0;
  892. out:
  893. vfree(pi->sechdrs);
  894. pi->sechdrs = NULL;
  895. out_free_kbuf:
  896. vfree(pi->purgatory_buf);
  897. pi->purgatory_buf = NULL;
  898. return ret;
  899. }
  900. /*
  901. * kexec_purgatory_find_symbol - find a symbol in the purgatory
  902. * @pi: Purgatory to search in.
  903. * @name: Name of the symbol.
  904. *
  905. * Return: pointer to symbol in read-only symtab on success, NULL on error.
  906. */
  907. static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
  908. const char *name)
  909. {
  910. const Elf_Shdr *sechdrs;
  911. const Elf_Ehdr *ehdr;
  912. const Elf_Sym *syms;
  913. const char *strtab;
  914. int i, k;
  915. if (!pi->ehdr)
  916. return NULL;
  917. ehdr = pi->ehdr;
  918. sechdrs = (void *)ehdr + ehdr->e_shoff;
  919. for (i = 0; i < ehdr->e_shnum; i++) {
  920. if (sechdrs[i].sh_type != SHT_SYMTAB)
  921. continue;
  922. if (sechdrs[i].sh_link >= ehdr->e_shnum)
  923. /* Invalid strtab section number */
  924. continue;
  925. strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
  926. syms = (void *)ehdr + sechdrs[i].sh_offset;
  927. /* Go through symbols for a match */
  928. for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
  929. if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
  930. continue;
  931. if (strcmp(strtab + syms[k].st_name, name) != 0)
  932. continue;
  933. if (syms[k].st_shndx == SHN_UNDEF ||
  934. syms[k].st_shndx >= ehdr->e_shnum) {
  935. pr_debug("Symbol: %s has bad section index %d.\n",
  936. name, syms[k].st_shndx);
  937. return NULL;
  938. }
  939. /* Found the symbol we are looking for */
  940. return &syms[k];
  941. }
  942. }
  943. return NULL;
  944. }
  945. void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
  946. {
  947. struct purgatory_info *pi = &image->purgatory_info;
  948. const Elf_Sym *sym;
  949. Elf_Shdr *sechdr;
  950. sym = kexec_purgatory_find_symbol(pi, name);
  951. if (!sym)
  952. return ERR_PTR(-EINVAL);
  953. sechdr = &pi->sechdrs[sym->st_shndx];
  954. /*
  955. * Returns the address where symbol will finally be loaded after
  956. * kexec_load_segment()
  957. */
  958. return (void *)(sechdr->sh_addr + sym->st_value);
  959. }
  960. /*
  961. * Get or set value of a symbol. If "get_value" is true, symbol value is
  962. * returned in buf otherwise symbol value is set based on value in buf.
  963. */
  964. int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
  965. void *buf, unsigned int size, bool get_value)
  966. {
  967. struct purgatory_info *pi = &image->purgatory_info;
  968. const Elf_Sym *sym;
  969. Elf_Shdr *sec;
  970. char *sym_buf;
  971. sym = kexec_purgatory_find_symbol(pi, name);
  972. if (!sym)
  973. return -EINVAL;
  974. if (sym->st_size != size) {
  975. pr_err("symbol %s size mismatch: expected %lu actual %u\n",
  976. name, (unsigned long)sym->st_size, size);
  977. return -EINVAL;
  978. }
  979. sec = pi->sechdrs + sym->st_shndx;
  980. if (sec->sh_type == SHT_NOBITS) {
  981. pr_err("symbol %s is in a bss section. Cannot %s\n", name,
  982. get_value ? "get" : "set");
  983. return -EINVAL;
  984. }
  985. sym_buf = (char *)pi->purgatory_buf + sec->sh_offset + sym->st_value;
  986. if (get_value)
  987. memcpy((void *)buf, sym_buf, size);
  988. else
  989. memcpy((void *)sym_buf, buf, size);
  990. return 0;
  991. }
  992. #endif /* CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY */