kexec_file.c 31 KB

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