kexec_file.c 31 KB

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