kexec_file.c 32 KB

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