kexec_file.c 31 KB

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