kexec_file.c 25 KB

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