kexec_file.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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 <crypto/hash.h>
  21. #include <crypto/sha.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/vmalloc.h>
  24. #include "kexec_internal.h"
  25. /*
  26. * Declare these symbols weak so that if architecture provides a purgatory,
  27. * these will be overridden.
  28. */
  29. char __weak kexec_purgatory[0];
  30. size_t __weak kexec_purgatory_size = 0;
  31. static int kexec_calculate_store_digests(struct kimage *image);
  32. /* Architectures can provide this probe function */
  33. int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
  34. unsigned long buf_len)
  35. {
  36. return -ENOEXEC;
  37. }
  38. void * __weak arch_kexec_kernel_image_load(struct kimage *image)
  39. {
  40. return ERR_PTR(-ENOEXEC);
  41. }
  42. int __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
  43. {
  44. return -EINVAL;
  45. }
  46. #ifdef CONFIG_KEXEC_VERIFY_SIG
  47. int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
  48. unsigned long buf_len)
  49. {
  50. return -EKEYREJECTED;
  51. }
  52. #endif
  53. /* Apply relocations of type RELA */
  54. int __weak
  55. arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
  56. unsigned int relsec)
  57. {
  58. pr_err("RELA relocation unsupported.\n");
  59. return -ENOEXEC;
  60. }
  61. /* Apply relocations of type REL */
  62. int __weak
  63. arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
  64. unsigned int relsec)
  65. {
  66. pr_err("REL relocation unsupported.\n");
  67. return -ENOEXEC;
  68. }
  69. /*
  70. * Free up memory used by kernel, initrd, and command line. This is temporary
  71. * memory allocation which is not needed any more after these buffers have
  72. * been loaded into separate segments and have been copied elsewhere.
  73. */
  74. void kimage_file_post_load_cleanup(struct kimage *image)
  75. {
  76. struct purgatory_info *pi = &image->purgatory_info;
  77. vfree(image->kernel_buf);
  78. image->kernel_buf = NULL;
  79. vfree(image->initrd_buf);
  80. image->initrd_buf = NULL;
  81. kfree(image->cmdline_buf);
  82. image->cmdline_buf = NULL;
  83. vfree(pi->purgatory_buf);
  84. pi->purgatory_buf = NULL;
  85. vfree(pi->sechdrs);
  86. pi->sechdrs = NULL;
  87. /* See if architecture has anything to cleanup post load */
  88. arch_kimage_file_post_load_cleanup(image);
  89. /*
  90. * Above call should have called into bootloader to free up
  91. * any data stored in kimage->image_loader_data. It should
  92. * be ok now to free it up.
  93. */
  94. kfree(image->image_loader_data);
  95. image->image_loader_data = NULL;
  96. }
  97. /*
  98. * In file mode list of segments is prepared by kernel. Copy relevant
  99. * data from user space, do error checking, prepare segment list
  100. */
  101. static int
  102. kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
  103. const char __user *cmdline_ptr,
  104. unsigned long cmdline_len, unsigned flags)
  105. {
  106. int ret = 0;
  107. void *ldata;
  108. loff_t size;
  109. ret = kernel_read_file_from_fd(kernel_fd, &image->kernel_buf,
  110. &size, INT_MAX, READING_KEXEC_IMAGE);
  111. if (ret)
  112. return ret;
  113. image->kernel_buf_len = size;
  114. /* Call arch image probe handlers */
  115. ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
  116. image->kernel_buf_len);
  117. if (ret)
  118. goto out;
  119. #ifdef CONFIG_KEXEC_VERIFY_SIG
  120. ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
  121. image->kernel_buf_len);
  122. if (ret) {
  123. pr_debug("kernel signature verification failed.\n");
  124. goto out;
  125. }
  126. pr_debug("kernel signature verification successful.\n");
  127. #endif
  128. /* It is possible that there no initramfs is being loaded */
  129. if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
  130. ret = kernel_read_file_from_fd(initrd_fd, &image->initrd_buf,
  131. &size, INT_MAX,
  132. READING_KEXEC_INITRAMFS);
  133. if (ret)
  134. goto out;
  135. image->initrd_buf_len = size;
  136. }
  137. if (cmdline_len) {
  138. image->cmdline_buf = kzalloc(cmdline_len, GFP_KERNEL);
  139. if (!image->cmdline_buf) {
  140. ret = -ENOMEM;
  141. goto out;
  142. }
  143. ret = copy_from_user(image->cmdline_buf, cmdline_ptr,
  144. cmdline_len);
  145. if (ret) {
  146. ret = -EFAULT;
  147. goto out;
  148. }
  149. image->cmdline_buf_len = cmdline_len;
  150. /* command line should be a string with last byte null */
  151. if (image->cmdline_buf[cmdline_len - 1] != '\0') {
  152. ret = -EINVAL;
  153. goto out;
  154. }
  155. }
  156. /* Call arch image load handlers */
  157. ldata = arch_kexec_kernel_image_load(image);
  158. if (IS_ERR(ldata)) {
  159. ret = PTR_ERR(ldata);
  160. goto out;
  161. }
  162. image->image_loader_data = ldata;
  163. out:
  164. /* In case of error, free up all allocated memory in this function */
  165. if (ret)
  166. kimage_file_post_load_cleanup(image);
  167. return ret;
  168. }
  169. static int
  170. kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
  171. int initrd_fd, const char __user *cmdline_ptr,
  172. unsigned long cmdline_len, unsigned long flags)
  173. {
  174. int ret;
  175. struct kimage *image;
  176. bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
  177. image = do_kimage_alloc_init();
  178. if (!image)
  179. return -ENOMEM;
  180. image->file_mode = 1;
  181. if (kexec_on_panic) {
  182. /* Enable special crash kernel control page alloc policy. */
  183. image->control_page = crashk_res.start;
  184. image->type = KEXEC_TYPE_CRASH;
  185. }
  186. ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
  187. cmdline_ptr, cmdline_len, flags);
  188. if (ret)
  189. goto out_free_image;
  190. ret = sanity_check_segment_list(image);
  191. if (ret)
  192. goto out_free_post_load_bufs;
  193. ret = -ENOMEM;
  194. image->control_code_page = kimage_alloc_control_pages(image,
  195. get_order(KEXEC_CONTROL_PAGE_SIZE));
  196. if (!image->control_code_page) {
  197. pr_err("Could not allocate control_code_buffer\n");
  198. goto out_free_post_load_bufs;
  199. }
  200. if (!kexec_on_panic) {
  201. image->swap_page = kimage_alloc_control_pages(image, 0);
  202. if (!image->swap_page) {
  203. pr_err("Could not allocate swap buffer\n");
  204. goto out_free_control_pages;
  205. }
  206. }
  207. *rimage = image;
  208. return 0;
  209. out_free_control_pages:
  210. kimage_free_page_list(&image->control_pages);
  211. out_free_post_load_bufs:
  212. kimage_file_post_load_cleanup(image);
  213. out_free_image:
  214. kfree(image);
  215. return ret;
  216. }
  217. SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
  218. unsigned long, cmdline_len, const char __user *, cmdline_ptr,
  219. unsigned long, flags)
  220. {
  221. int ret = 0, i;
  222. struct kimage **dest_image, *image;
  223. /* We only trust the superuser with rebooting the system. */
  224. if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
  225. return -EPERM;
  226. /* Make sure we have a legal set of flags */
  227. if (flags != (flags & KEXEC_FILE_FLAGS))
  228. return -EINVAL;
  229. image = NULL;
  230. if (!mutex_trylock(&kexec_mutex))
  231. return -EBUSY;
  232. dest_image = &kexec_image;
  233. if (flags & KEXEC_FILE_ON_CRASH) {
  234. dest_image = &kexec_crash_image;
  235. if (kexec_crash_image)
  236. arch_kexec_unprotect_crashkres();
  237. }
  238. if (flags & KEXEC_FILE_UNLOAD)
  239. goto exchange;
  240. /*
  241. * In case of crash, new kernel gets loaded in reserved region. It is
  242. * same memory where old crash kernel might be loaded. Free any
  243. * current crash dump kernel before we corrupt it.
  244. */
  245. if (flags & KEXEC_FILE_ON_CRASH)
  246. kimage_free(xchg(&kexec_crash_image, NULL));
  247. ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
  248. cmdline_len, flags);
  249. if (ret)
  250. goto out;
  251. ret = machine_kexec_prepare(image);
  252. if (ret)
  253. goto out;
  254. ret = kexec_calculate_store_digests(image);
  255. if (ret)
  256. goto out;
  257. for (i = 0; i < image->nr_segments; i++) {
  258. struct kexec_segment *ksegment;
  259. ksegment = &image->segment[i];
  260. pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
  261. i, ksegment->buf, ksegment->bufsz, ksegment->mem,
  262. ksegment->memsz);
  263. ret = kimage_load_segment(image, &image->segment[i]);
  264. if (ret)
  265. goto out;
  266. }
  267. kimage_terminate(image);
  268. /*
  269. * Free up any temporary buffers allocated which are not needed
  270. * after image has been loaded
  271. */
  272. kimage_file_post_load_cleanup(image);
  273. exchange:
  274. image = xchg(dest_image, image);
  275. out:
  276. if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
  277. arch_kexec_protect_crashkres();
  278. mutex_unlock(&kexec_mutex);
  279. kimage_free(image);
  280. return ret;
  281. }
  282. static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
  283. struct kexec_buf *kbuf)
  284. {
  285. struct kimage *image = kbuf->image;
  286. unsigned long temp_start, temp_end;
  287. temp_end = min(end, kbuf->buf_max);
  288. temp_start = temp_end - kbuf->memsz;
  289. do {
  290. /* align down start */
  291. temp_start = temp_start & (~(kbuf->buf_align - 1));
  292. if (temp_start < start || temp_start < kbuf->buf_min)
  293. return 0;
  294. temp_end = temp_start + kbuf->memsz - 1;
  295. /*
  296. * Make sure this does not conflict with any of existing
  297. * segments
  298. */
  299. if (kimage_is_destination_range(image, temp_start, temp_end)) {
  300. temp_start = temp_start - PAGE_SIZE;
  301. continue;
  302. }
  303. /* We found a suitable memory range */
  304. break;
  305. } while (1);
  306. /* If we are here, we found a suitable memory range */
  307. kbuf->mem = temp_start;
  308. /* Success, stop navigating through remaining System RAM ranges */
  309. return 1;
  310. }
  311. static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
  312. struct kexec_buf *kbuf)
  313. {
  314. struct kimage *image = kbuf->image;
  315. unsigned long temp_start, temp_end;
  316. temp_start = max(start, kbuf->buf_min);
  317. do {
  318. temp_start = ALIGN(temp_start, kbuf->buf_align);
  319. temp_end = temp_start + kbuf->memsz - 1;
  320. if (temp_end > end || temp_end > kbuf->buf_max)
  321. return 0;
  322. /*
  323. * Make sure this does not conflict with any of existing
  324. * segments
  325. */
  326. if (kimage_is_destination_range(image, temp_start, temp_end)) {
  327. temp_start = temp_start + PAGE_SIZE;
  328. continue;
  329. }
  330. /* We found a suitable memory range */
  331. break;
  332. } while (1);
  333. /* If we are here, we found a suitable memory range */
  334. kbuf->mem = temp_start;
  335. /* Success, stop navigating through remaining System RAM ranges */
  336. return 1;
  337. }
  338. static int locate_mem_hole_callback(u64 start, u64 end, void *arg)
  339. {
  340. struct kexec_buf *kbuf = (struct kexec_buf *)arg;
  341. unsigned long sz = end - start + 1;
  342. /* Returning 0 will take to next memory range */
  343. if (sz < kbuf->memsz)
  344. return 0;
  345. if (end < kbuf->buf_min || start > kbuf->buf_max)
  346. return 0;
  347. /*
  348. * Allocate memory top down with-in ram range. Otherwise bottom up
  349. * allocation.
  350. */
  351. if (kbuf->top_down)
  352. return locate_mem_hole_top_down(start, end, kbuf);
  353. return locate_mem_hole_bottom_up(start, end, kbuf);
  354. }
  355. /*
  356. * Helper function for placing a buffer in a kexec segment. This assumes
  357. * that kexec_mutex is held.
  358. */
  359. int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz,
  360. unsigned long memsz, unsigned long buf_align,
  361. unsigned long buf_min, unsigned long buf_max,
  362. bool top_down, unsigned long *load_addr)
  363. {
  364. struct kexec_segment *ksegment;
  365. struct kexec_buf buf, *kbuf;
  366. int ret;
  367. /* Currently adding segment this way is allowed only in file mode */
  368. if (!image->file_mode)
  369. return -EINVAL;
  370. if (image->nr_segments >= KEXEC_SEGMENT_MAX)
  371. return -EINVAL;
  372. /*
  373. * Make sure we are not trying to add buffer after allocating
  374. * control pages. All segments need to be placed first before
  375. * any control pages are allocated. As control page allocation
  376. * logic goes through list of segments to make sure there are
  377. * no destination overlaps.
  378. */
  379. if (!list_empty(&image->control_pages)) {
  380. WARN_ON(1);
  381. return -EINVAL;
  382. }
  383. memset(&buf, 0, sizeof(struct kexec_buf));
  384. kbuf = &buf;
  385. kbuf->image = image;
  386. kbuf->buffer = buffer;
  387. kbuf->bufsz = bufsz;
  388. kbuf->memsz = ALIGN(memsz, PAGE_SIZE);
  389. kbuf->buf_align = max(buf_align, PAGE_SIZE);
  390. kbuf->buf_min = buf_min;
  391. kbuf->buf_max = buf_max;
  392. kbuf->top_down = top_down;
  393. /* Walk the RAM ranges and allocate a suitable range for the buffer */
  394. if (image->type == KEXEC_TYPE_CRASH)
  395. ret = walk_iomem_res_desc(crashk_res.desc,
  396. IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
  397. crashk_res.start, crashk_res.end, kbuf,
  398. locate_mem_hole_callback);
  399. else
  400. ret = walk_system_ram_res(0, -1, kbuf,
  401. locate_mem_hole_callback);
  402. if (ret != 1) {
  403. /* A suitable memory range could not be found for buffer */
  404. return -EADDRNOTAVAIL;
  405. }
  406. /* Found a suitable memory range */
  407. ksegment = &image->segment[image->nr_segments];
  408. ksegment->kbuf = kbuf->buffer;
  409. ksegment->bufsz = kbuf->bufsz;
  410. ksegment->mem = kbuf->mem;
  411. ksegment->memsz = kbuf->memsz;
  412. image->nr_segments++;
  413. *load_addr = ksegment->mem;
  414. return 0;
  415. }
  416. /* Calculate and store the digest of segments */
  417. static int kexec_calculate_store_digests(struct kimage *image)
  418. {
  419. struct crypto_shash *tfm;
  420. struct shash_desc *desc;
  421. int ret = 0, i, j, zero_buf_sz, sha_region_sz;
  422. size_t desc_size, nullsz;
  423. char *digest;
  424. void *zero_buf;
  425. struct kexec_sha_region *sha_regions;
  426. struct purgatory_info *pi = &image->purgatory_info;
  427. zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
  428. zero_buf_sz = PAGE_SIZE;
  429. tfm = crypto_alloc_shash("sha256", 0, 0);
  430. if (IS_ERR(tfm)) {
  431. ret = PTR_ERR(tfm);
  432. goto out;
  433. }
  434. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  435. desc = kzalloc(desc_size, GFP_KERNEL);
  436. if (!desc) {
  437. ret = -ENOMEM;
  438. goto out_free_tfm;
  439. }
  440. sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
  441. sha_regions = vzalloc(sha_region_sz);
  442. if (!sha_regions)
  443. goto out_free_desc;
  444. desc->tfm = tfm;
  445. desc->flags = 0;
  446. ret = crypto_shash_init(desc);
  447. if (ret < 0)
  448. goto out_free_sha_regions;
  449. digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
  450. if (!digest) {
  451. ret = -ENOMEM;
  452. goto out_free_sha_regions;
  453. }
  454. for (j = i = 0; i < image->nr_segments; i++) {
  455. struct kexec_segment *ksegment;
  456. ksegment = &image->segment[i];
  457. /*
  458. * Skip purgatory as it will be modified once we put digest
  459. * info in purgatory.
  460. */
  461. if (ksegment->kbuf == pi->purgatory_buf)
  462. continue;
  463. ret = crypto_shash_update(desc, ksegment->kbuf,
  464. ksegment->bufsz);
  465. if (ret)
  466. break;
  467. /*
  468. * Assume rest of the buffer is filled with zero and
  469. * update digest accordingly.
  470. */
  471. nullsz = ksegment->memsz - ksegment->bufsz;
  472. while (nullsz) {
  473. unsigned long bytes = nullsz;
  474. if (bytes > zero_buf_sz)
  475. bytes = zero_buf_sz;
  476. ret = crypto_shash_update(desc, zero_buf, bytes);
  477. if (ret)
  478. break;
  479. nullsz -= bytes;
  480. }
  481. if (ret)
  482. break;
  483. sha_regions[j].start = ksegment->mem;
  484. sha_regions[j].len = ksegment->memsz;
  485. j++;
  486. }
  487. if (!ret) {
  488. ret = crypto_shash_final(desc, digest);
  489. if (ret)
  490. goto out_free_digest;
  491. ret = kexec_purgatory_get_set_symbol(image, "sha_regions",
  492. sha_regions, sha_region_sz, 0);
  493. if (ret)
  494. goto out_free_digest;
  495. ret = kexec_purgatory_get_set_symbol(image, "sha256_digest",
  496. digest, SHA256_DIGEST_SIZE, 0);
  497. if (ret)
  498. goto out_free_digest;
  499. }
  500. out_free_digest:
  501. kfree(digest);
  502. out_free_sha_regions:
  503. vfree(sha_regions);
  504. out_free_desc:
  505. kfree(desc);
  506. out_free_tfm:
  507. kfree(tfm);
  508. out:
  509. return ret;
  510. }
  511. /* Actually load purgatory. Lot of code taken from kexec-tools */
  512. static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
  513. unsigned long max, int top_down)
  514. {
  515. struct purgatory_info *pi = &image->purgatory_info;
  516. unsigned long align, buf_align, bss_align, buf_sz, bss_sz, bss_pad;
  517. unsigned long memsz, entry, load_addr, curr_load_addr, bss_addr, offset;
  518. unsigned char *buf_addr, *src;
  519. int i, ret = 0, entry_sidx = -1;
  520. const Elf_Shdr *sechdrs_c;
  521. Elf_Shdr *sechdrs = NULL;
  522. void *purgatory_buf = NULL;
  523. /*
  524. * sechdrs_c points to section headers in purgatory and are read
  525. * only. No modifications allowed.
  526. */
  527. sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
  528. /*
  529. * We can not modify sechdrs_c[] and its fields. It is read only.
  530. * Copy it over to a local copy where one can store some temporary
  531. * data and free it at the end. We need to modify ->sh_addr and
  532. * ->sh_offset fields to keep track of permanent and temporary
  533. * locations of sections.
  534. */
  535. sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
  536. if (!sechdrs)
  537. return -ENOMEM;
  538. memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
  539. /*
  540. * We seem to have multiple copies of sections. First copy is which
  541. * is embedded in kernel in read only section. Some of these sections
  542. * will be copied to a temporary buffer and relocated. And these
  543. * sections will finally be copied to their final destination at
  544. * segment load time.
  545. *
  546. * Use ->sh_offset to reflect section address in memory. It will
  547. * point to original read only copy if section is not allocatable.
  548. * Otherwise it will point to temporary copy which will be relocated.
  549. *
  550. * Use ->sh_addr to contain final address of the section where it
  551. * will go during execution time.
  552. */
  553. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  554. if (sechdrs[i].sh_type == SHT_NOBITS)
  555. continue;
  556. sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
  557. sechdrs[i].sh_offset;
  558. }
  559. /*
  560. * Identify entry point section and make entry relative to section
  561. * start.
  562. */
  563. entry = pi->ehdr->e_entry;
  564. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  565. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  566. continue;
  567. if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
  568. continue;
  569. /* Make entry section relative */
  570. if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
  571. ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
  572. pi->ehdr->e_entry)) {
  573. entry_sidx = i;
  574. entry -= sechdrs[i].sh_addr;
  575. break;
  576. }
  577. }
  578. /* Determine how much memory is needed to load relocatable object. */
  579. buf_align = 1;
  580. bss_align = 1;
  581. buf_sz = 0;
  582. bss_sz = 0;
  583. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  584. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  585. continue;
  586. align = sechdrs[i].sh_addralign;
  587. if (sechdrs[i].sh_type != SHT_NOBITS) {
  588. if (buf_align < align)
  589. buf_align = align;
  590. buf_sz = ALIGN(buf_sz, align);
  591. buf_sz += sechdrs[i].sh_size;
  592. } else {
  593. /* bss section */
  594. if (bss_align < align)
  595. bss_align = align;
  596. bss_sz = ALIGN(bss_sz, align);
  597. bss_sz += sechdrs[i].sh_size;
  598. }
  599. }
  600. /* Determine the bss padding required to align bss properly */
  601. bss_pad = 0;
  602. if (buf_sz & (bss_align - 1))
  603. bss_pad = bss_align - (buf_sz & (bss_align - 1));
  604. memsz = buf_sz + bss_pad + bss_sz;
  605. /* Allocate buffer for purgatory */
  606. purgatory_buf = vzalloc(buf_sz);
  607. if (!purgatory_buf) {
  608. ret = -ENOMEM;
  609. goto out;
  610. }
  611. if (buf_align < bss_align)
  612. buf_align = bss_align;
  613. /* Add buffer to segment list */
  614. ret = kexec_add_buffer(image, purgatory_buf, buf_sz, memsz,
  615. buf_align, min, max, top_down,
  616. &pi->purgatory_load_addr);
  617. if (ret)
  618. goto out;
  619. /* Load SHF_ALLOC sections */
  620. buf_addr = purgatory_buf;
  621. load_addr = curr_load_addr = pi->purgatory_load_addr;
  622. bss_addr = load_addr + buf_sz + bss_pad;
  623. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  624. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  625. continue;
  626. align = sechdrs[i].sh_addralign;
  627. if (sechdrs[i].sh_type != SHT_NOBITS) {
  628. curr_load_addr = ALIGN(curr_load_addr, align);
  629. offset = curr_load_addr - load_addr;
  630. /* We already modifed ->sh_offset to keep src addr */
  631. src = (char *) sechdrs[i].sh_offset;
  632. memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
  633. /* Store load address and source address of section */
  634. sechdrs[i].sh_addr = curr_load_addr;
  635. /*
  636. * This section got copied to temporary buffer. Update
  637. * ->sh_offset accordingly.
  638. */
  639. sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
  640. /* Advance to the next address */
  641. curr_load_addr += sechdrs[i].sh_size;
  642. } else {
  643. bss_addr = ALIGN(bss_addr, align);
  644. sechdrs[i].sh_addr = bss_addr;
  645. bss_addr += sechdrs[i].sh_size;
  646. }
  647. }
  648. /* Update entry point based on load address of text section */
  649. if (entry_sidx >= 0)
  650. entry += sechdrs[entry_sidx].sh_addr;
  651. /* Make kernel jump to purgatory after shutdown */
  652. image->start = entry;
  653. /* Used later to get/set symbol values */
  654. pi->sechdrs = sechdrs;
  655. /*
  656. * Used later to identify which section is purgatory and skip it
  657. * from checksumming.
  658. */
  659. pi->purgatory_buf = purgatory_buf;
  660. return ret;
  661. out:
  662. vfree(sechdrs);
  663. vfree(purgatory_buf);
  664. return ret;
  665. }
  666. static int kexec_apply_relocations(struct kimage *image)
  667. {
  668. int i, ret;
  669. struct purgatory_info *pi = &image->purgatory_info;
  670. Elf_Shdr *sechdrs = pi->sechdrs;
  671. /* Apply relocations */
  672. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  673. Elf_Shdr *section, *symtab;
  674. if (sechdrs[i].sh_type != SHT_RELA &&
  675. sechdrs[i].sh_type != SHT_REL)
  676. continue;
  677. /*
  678. * For section of type SHT_RELA/SHT_REL,
  679. * ->sh_link contains section header index of associated
  680. * symbol table. And ->sh_info contains section header
  681. * index of section to which relocations apply.
  682. */
  683. if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
  684. sechdrs[i].sh_link >= pi->ehdr->e_shnum)
  685. return -ENOEXEC;
  686. section = &sechdrs[sechdrs[i].sh_info];
  687. symtab = &sechdrs[sechdrs[i].sh_link];
  688. if (!(section->sh_flags & SHF_ALLOC))
  689. continue;
  690. /*
  691. * symtab->sh_link contain section header index of associated
  692. * string table.
  693. */
  694. if (symtab->sh_link >= pi->ehdr->e_shnum)
  695. /* Invalid section number? */
  696. continue;
  697. /*
  698. * Respective architecture needs to provide support for applying
  699. * relocations of type SHT_RELA/SHT_REL.
  700. */
  701. if (sechdrs[i].sh_type == SHT_RELA)
  702. ret = arch_kexec_apply_relocations_add(pi->ehdr,
  703. sechdrs, i);
  704. else if (sechdrs[i].sh_type == SHT_REL)
  705. ret = arch_kexec_apply_relocations(pi->ehdr,
  706. sechdrs, i);
  707. if (ret)
  708. return ret;
  709. }
  710. return 0;
  711. }
  712. /* Load relocatable purgatory object and relocate it appropriately */
  713. int kexec_load_purgatory(struct kimage *image, unsigned long min,
  714. unsigned long max, int top_down,
  715. unsigned long *load_addr)
  716. {
  717. struct purgatory_info *pi = &image->purgatory_info;
  718. int ret;
  719. if (kexec_purgatory_size <= 0)
  720. return -EINVAL;
  721. if (kexec_purgatory_size < sizeof(Elf_Ehdr))
  722. return -ENOEXEC;
  723. pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
  724. if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
  725. || pi->ehdr->e_type != ET_REL
  726. || !elf_check_arch(pi->ehdr)
  727. || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
  728. return -ENOEXEC;
  729. if (pi->ehdr->e_shoff >= kexec_purgatory_size
  730. || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
  731. kexec_purgatory_size - pi->ehdr->e_shoff))
  732. return -ENOEXEC;
  733. ret = __kexec_load_purgatory(image, min, max, top_down);
  734. if (ret)
  735. return ret;
  736. ret = kexec_apply_relocations(image);
  737. if (ret)
  738. goto out;
  739. *load_addr = pi->purgatory_load_addr;
  740. return 0;
  741. out:
  742. vfree(pi->sechdrs);
  743. pi->sechdrs = NULL;
  744. vfree(pi->purgatory_buf);
  745. pi->purgatory_buf = NULL;
  746. return ret;
  747. }
  748. static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
  749. const char *name)
  750. {
  751. Elf_Sym *syms;
  752. Elf_Shdr *sechdrs;
  753. Elf_Ehdr *ehdr;
  754. int i, k;
  755. const char *strtab;
  756. if (!pi->sechdrs || !pi->ehdr)
  757. return NULL;
  758. sechdrs = pi->sechdrs;
  759. ehdr = pi->ehdr;
  760. for (i = 0; i < ehdr->e_shnum; i++) {
  761. if (sechdrs[i].sh_type != SHT_SYMTAB)
  762. continue;
  763. if (sechdrs[i].sh_link >= ehdr->e_shnum)
  764. /* Invalid strtab section number */
  765. continue;
  766. strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset;
  767. syms = (Elf_Sym *)sechdrs[i].sh_offset;
  768. /* Go through symbols for a match */
  769. for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
  770. if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
  771. continue;
  772. if (strcmp(strtab + syms[k].st_name, name) != 0)
  773. continue;
  774. if (syms[k].st_shndx == SHN_UNDEF ||
  775. syms[k].st_shndx >= ehdr->e_shnum) {
  776. pr_debug("Symbol: %s has bad section index %d.\n",
  777. name, syms[k].st_shndx);
  778. return NULL;
  779. }
  780. /* Found the symbol we are looking for */
  781. return &syms[k];
  782. }
  783. }
  784. return NULL;
  785. }
  786. void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
  787. {
  788. struct purgatory_info *pi = &image->purgatory_info;
  789. Elf_Sym *sym;
  790. Elf_Shdr *sechdr;
  791. sym = kexec_purgatory_find_symbol(pi, name);
  792. if (!sym)
  793. return ERR_PTR(-EINVAL);
  794. sechdr = &pi->sechdrs[sym->st_shndx];
  795. /*
  796. * Returns the address where symbol will finally be loaded after
  797. * kexec_load_segment()
  798. */
  799. return (void *)(sechdr->sh_addr + sym->st_value);
  800. }
  801. /*
  802. * Get or set value of a symbol. If "get_value" is true, symbol value is
  803. * returned in buf otherwise symbol value is set based on value in buf.
  804. */
  805. int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
  806. void *buf, unsigned int size, bool get_value)
  807. {
  808. Elf_Sym *sym;
  809. Elf_Shdr *sechdrs;
  810. struct purgatory_info *pi = &image->purgatory_info;
  811. char *sym_buf;
  812. sym = kexec_purgatory_find_symbol(pi, name);
  813. if (!sym)
  814. return -EINVAL;
  815. if (sym->st_size != size) {
  816. pr_err("symbol %s size mismatch: expected %lu actual %u\n",
  817. name, (unsigned long)sym->st_size, size);
  818. return -EINVAL;
  819. }
  820. sechdrs = pi->sechdrs;
  821. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  822. pr_err("symbol %s is in a bss section. Cannot %s\n", name,
  823. get_value ? "get" : "set");
  824. return -EINVAL;
  825. }
  826. sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset +
  827. sym->st_value;
  828. if (get_value)
  829. memcpy((void *)buf, sym_buf, size);
  830. else
  831. memcpy((void *)sym_buf, buf, size);
  832. return 0;
  833. }