kexec_file.c 25 KB

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