kexec_file.c 31 KB

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