kexec_file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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 (flags & KEXEC_FILE_UNLOAD)
  236. goto exchange;
  237. /*
  238. * In case of crash, new kernel gets loaded in reserved region. It is
  239. * same memory where old crash kernel might be loaded. Free any
  240. * current crash dump kernel before we corrupt it.
  241. */
  242. if (flags & KEXEC_FILE_ON_CRASH)
  243. kimage_free(xchg(&kexec_crash_image, NULL));
  244. ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
  245. cmdline_len, flags);
  246. if (ret)
  247. goto out;
  248. ret = machine_kexec_prepare(image);
  249. if (ret)
  250. goto out;
  251. ret = kexec_calculate_store_digests(image);
  252. if (ret)
  253. goto out;
  254. for (i = 0; i < image->nr_segments; i++) {
  255. struct kexec_segment *ksegment;
  256. ksegment = &image->segment[i];
  257. pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
  258. i, ksegment->buf, ksegment->bufsz, ksegment->mem,
  259. ksegment->memsz);
  260. ret = kimage_load_segment(image, &image->segment[i]);
  261. if (ret)
  262. goto out;
  263. }
  264. kimage_terminate(image);
  265. /*
  266. * Free up any temporary buffers allocated which are not needed
  267. * after image has been loaded
  268. */
  269. kimage_file_post_load_cleanup(image);
  270. exchange:
  271. image = xchg(dest_image, image);
  272. out:
  273. mutex_unlock(&kexec_mutex);
  274. kimage_free(image);
  275. return ret;
  276. }
  277. static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
  278. struct kexec_buf *kbuf)
  279. {
  280. struct kimage *image = kbuf->image;
  281. unsigned long temp_start, temp_end;
  282. temp_end = min(end, kbuf->buf_max);
  283. temp_start = temp_end - kbuf->memsz;
  284. do {
  285. /* align down start */
  286. temp_start = temp_start & (~(kbuf->buf_align - 1));
  287. if (temp_start < start || temp_start < kbuf->buf_min)
  288. return 0;
  289. temp_end = temp_start + kbuf->memsz - 1;
  290. /*
  291. * Make sure this does not conflict with any of existing
  292. * segments
  293. */
  294. if (kimage_is_destination_range(image, temp_start, temp_end)) {
  295. temp_start = temp_start - PAGE_SIZE;
  296. continue;
  297. }
  298. /* We found a suitable memory range */
  299. break;
  300. } while (1);
  301. /* If we are here, we found a suitable memory range */
  302. kbuf->mem = temp_start;
  303. /* Success, stop navigating through remaining System RAM ranges */
  304. return 1;
  305. }
  306. static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
  307. struct kexec_buf *kbuf)
  308. {
  309. struct kimage *image = kbuf->image;
  310. unsigned long temp_start, temp_end;
  311. temp_start = max(start, kbuf->buf_min);
  312. do {
  313. temp_start = ALIGN(temp_start, kbuf->buf_align);
  314. temp_end = temp_start + kbuf->memsz - 1;
  315. if (temp_end > end || temp_end > kbuf->buf_max)
  316. return 0;
  317. /*
  318. * Make sure this does not conflict with any of existing
  319. * segments
  320. */
  321. if (kimage_is_destination_range(image, temp_start, temp_end)) {
  322. temp_start = temp_start + PAGE_SIZE;
  323. continue;
  324. }
  325. /* We found a suitable memory range */
  326. break;
  327. } while (1);
  328. /* If we are here, we found a suitable memory range */
  329. kbuf->mem = temp_start;
  330. /* Success, stop navigating through remaining System RAM ranges */
  331. return 1;
  332. }
  333. static int locate_mem_hole_callback(u64 start, u64 end, void *arg)
  334. {
  335. struct kexec_buf *kbuf = (struct kexec_buf *)arg;
  336. unsigned long sz = end - start + 1;
  337. /* Returning 0 will take to next memory range */
  338. if (sz < kbuf->memsz)
  339. return 0;
  340. if (end < kbuf->buf_min || start > kbuf->buf_max)
  341. return 0;
  342. /*
  343. * Allocate memory top down with-in ram range. Otherwise bottom up
  344. * allocation.
  345. */
  346. if (kbuf->top_down)
  347. return locate_mem_hole_top_down(start, end, kbuf);
  348. return locate_mem_hole_bottom_up(start, end, kbuf);
  349. }
  350. /*
  351. * Helper function for placing a buffer in a kexec segment. This assumes
  352. * that kexec_mutex is held.
  353. */
  354. int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz,
  355. unsigned long memsz, unsigned long buf_align,
  356. unsigned long buf_min, unsigned long buf_max,
  357. bool top_down, unsigned long *load_addr)
  358. {
  359. struct kexec_segment *ksegment;
  360. struct kexec_buf buf, *kbuf;
  361. int ret;
  362. /* Currently adding segment this way is allowed only in file mode */
  363. if (!image->file_mode)
  364. return -EINVAL;
  365. if (image->nr_segments >= KEXEC_SEGMENT_MAX)
  366. return -EINVAL;
  367. /*
  368. * Make sure we are not trying to add buffer after allocating
  369. * control pages. All segments need to be placed first before
  370. * any control pages are allocated. As control page allocation
  371. * logic goes through list of segments to make sure there are
  372. * no destination overlaps.
  373. */
  374. if (!list_empty(&image->control_pages)) {
  375. WARN_ON(1);
  376. return -EINVAL;
  377. }
  378. memset(&buf, 0, sizeof(struct kexec_buf));
  379. kbuf = &buf;
  380. kbuf->image = image;
  381. kbuf->buffer = buffer;
  382. kbuf->bufsz = bufsz;
  383. kbuf->memsz = ALIGN(memsz, PAGE_SIZE);
  384. kbuf->buf_align = max(buf_align, PAGE_SIZE);
  385. kbuf->buf_min = buf_min;
  386. kbuf->buf_max = buf_max;
  387. kbuf->top_down = top_down;
  388. /* Walk the RAM ranges and allocate a suitable range for the buffer */
  389. if (image->type == KEXEC_TYPE_CRASH)
  390. ret = walk_iomem_res_desc(crashk_res.desc,
  391. IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
  392. crashk_res.start, crashk_res.end, kbuf,
  393. locate_mem_hole_callback);
  394. else
  395. ret = walk_system_ram_res(0, -1, kbuf,
  396. locate_mem_hole_callback);
  397. if (ret != 1) {
  398. /* A suitable memory range could not be found for buffer */
  399. return -EADDRNOTAVAIL;
  400. }
  401. /* Found a suitable memory range */
  402. ksegment = &image->segment[image->nr_segments];
  403. ksegment->kbuf = kbuf->buffer;
  404. ksegment->bufsz = kbuf->bufsz;
  405. ksegment->mem = kbuf->mem;
  406. ksegment->memsz = kbuf->memsz;
  407. image->nr_segments++;
  408. *load_addr = ksegment->mem;
  409. return 0;
  410. }
  411. /* Calculate and store the digest of segments */
  412. static int kexec_calculate_store_digests(struct kimage *image)
  413. {
  414. struct crypto_shash *tfm;
  415. struct shash_desc *desc;
  416. int ret = 0, i, j, zero_buf_sz, sha_region_sz;
  417. size_t desc_size, nullsz;
  418. char *digest;
  419. void *zero_buf;
  420. struct kexec_sha_region *sha_regions;
  421. struct purgatory_info *pi = &image->purgatory_info;
  422. zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
  423. zero_buf_sz = PAGE_SIZE;
  424. tfm = crypto_alloc_shash("sha256", 0, 0);
  425. if (IS_ERR(tfm)) {
  426. ret = PTR_ERR(tfm);
  427. goto out;
  428. }
  429. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  430. desc = kzalloc(desc_size, GFP_KERNEL);
  431. if (!desc) {
  432. ret = -ENOMEM;
  433. goto out_free_tfm;
  434. }
  435. sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
  436. sha_regions = vzalloc(sha_region_sz);
  437. if (!sha_regions)
  438. goto out_free_desc;
  439. desc->tfm = tfm;
  440. desc->flags = 0;
  441. ret = crypto_shash_init(desc);
  442. if (ret < 0)
  443. goto out_free_sha_regions;
  444. digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
  445. if (!digest) {
  446. ret = -ENOMEM;
  447. goto out_free_sha_regions;
  448. }
  449. for (j = i = 0; i < image->nr_segments; i++) {
  450. struct kexec_segment *ksegment;
  451. ksegment = &image->segment[i];
  452. /*
  453. * Skip purgatory as it will be modified once we put digest
  454. * info in purgatory.
  455. */
  456. if (ksegment->kbuf == pi->purgatory_buf)
  457. continue;
  458. ret = crypto_shash_update(desc, ksegment->kbuf,
  459. ksegment->bufsz);
  460. if (ret)
  461. break;
  462. /*
  463. * Assume rest of the buffer is filled with zero and
  464. * update digest accordingly.
  465. */
  466. nullsz = ksegment->memsz - ksegment->bufsz;
  467. while (nullsz) {
  468. unsigned long bytes = nullsz;
  469. if (bytes > zero_buf_sz)
  470. bytes = zero_buf_sz;
  471. ret = crypto_shash_update(desc, zero_buf, bytes);
  472. if (ret)
  473. break;
  474. nullsz -= bytes;
  475. }
  476. if (ret)
  477. break;
  478. sha_regions[j].start = ksegment->mem;
  479. sha_regions[j].len = ksegment->memsz;
  480. j++;
  481. }
  482. if (!ret) {
  483. ret = crypto_shash_final(desc, digest);
  484. if (ret)
  485. goto out_free_digest;
  486. ret = kexec_purgatory_get_set_symbol(image, "sha_regions",
  487. sha_regions, sha_region_sz, 0);
  488. if (ret)
  489. goto out_free_digest;
  490. ret = kexec_purgatory_get_set_symbol(image, "sha256_digest",
  491. digest, SHA256_DIGEST_SIZE, 0);
  492. if (ret)
  493. goto out_free_digest;
  494. }
  495. out_free_digest:
  496. kfree(digest);
  497. out_free_sha_regions:
  498. vfree(sha_regions);
  499. out_free_desc:
  500. kfree(desc);
  501. out_free_tfm:
  502. kfree(tfm);
  503. out:
  504. return ret;
  505. }
  506. /* Actually load purgatory. Lot of code taken from kexec-tools */
  507. static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
  508. unsigned long max, int top_down)
  509. {
  510. struct purgatory_info *pi = &image->purgatory_info;
  511. unsigned long align, buf_align, bss_align, buf_sz, bss_sz, bss_pad;
  512. unsigned long memsz, entry, load_addr, curr_load_addr, bss_addr, offset;
  513. unsigned char *buf_addr, *src;
  514. int i, ret = 0, entry_sidx = -1;
  515. const Elf_Shdr *sechdrs_c;
  516. Elf_Shdr *sechdrs = NULL;
  517. void *purgatory_buf = NULL;
  518. /*
  519. * sechdrs_c points to section headers in purgatory and are read
  520. * only. No modifications allowed.
  521. */
  522. sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
  523. /*
  524. * We can not modify sechdrs_c[] and its fields. It is read only.
  525. * Copy it over to a local copy where one can store some temporary
  526. * data and free it at the end. We need to modify ->sh_addr and
  527. * ->sh_offset fields to keep track of permanent and temporary
  528. * locations of sections.
  529. */
  530. sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
  531. if (!sechdrs)
  532. return -ENOMEM;
  533. memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
  534. /*
  535. * We seem to have multiple copies of sections. First copy is which
  536. * is embedded in kernel in read only section. Some of these sections
  537. * will be copied to a temporary buffer and relocated. And these
  538. * sections will finally be copied to their final destination at
  539. * segment load time.
  540. *
  541. * Use ->sh_offset to reflect section address in memory. It will
  542. * point to original read only copy if section is not allocatable.
  543. * Otherwise it will point to temporary copy which will be relocated.
  544. *
  545. * Use ->sh_addr to contain final address of the section where it
  546. * will go during execution time.
  547. */
  548. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  549. if (sechdrs[i].sh_type == SHT_NOBITS)
  550. continue;
  551. sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
  552. sechdrs[i].sh_offset;
  553. }
  554. /*
  555. * Identify entry point section and make entry relative to section
  556. * start.
  557. */
  558. entry = pi->ehdr->e_entry;
  559. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  560. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  561. continue;
  562. if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
  563. continue;
  564. /* Make entry section relative */
  565. if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
  566. ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
  567. pi->ehdr->e_entry)) {
  568. entry_sidx = i;
  569. entry -= sechdrs[i].sh_addr;
  570. break;
  571. }
  572. }
  573. /* Determine how much memory is needed to load relocatable object. */
  574. buf_align = 1;
  575. bss_align = 1;
  576. buf_sz = 0;
  577. bss_sz = 0;
  578. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  579. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  580. continue;
  581. align = sechdrs[i].sh_addralign;
  582. if (sechdrs[i].sh_type != SHT_NOBITS) {
  583. if (buf_align < align)
  584. buf_align = align;
  585. buf_sz = ALIGN(buf_sz, align);
  586. buf_sz += sechdrs[i].sh_size;
  587. } else {
  588. /* bss section */
  589. if (bss_align < align)
  590. bss_align = align;
  591. bss_sz = ALIGN(bss_sz, align);
  592. bss_sz += sechdrs[i].sh_size;
  593. }
  594. }
  595. /* Determine the bss padding required to align bss properly */
  596. bss_pad = 0;
  597. if (buf_sz & (bss_align - 1))
  598. bss_pad = bss_align - (buf_sz & (bss_align - 1));
  599. memsz = buf_sz + bss_pad + bss_sz;
  600. /* Allocate buffer for purgatory */
  601. purgatory_buf = vzalloc(buf_sz);
  602. if (!purgatory_buf) {
  603. ret = -ENOMEM;
  604. goto out;
  605. }
  606. if (buf_align < bss_align)
  607. buf_align = bss_align;
  608. /* Add buffer to segment list */
  609. ret = kexec_add_buffer(image, purgatory_buf, buf_sz, memsz,
  610. buf_align, min, max, top_down,
  611. &pi->purgatory_load_addr);
  612. if (ret)
  613. goto out;
  614. /* Load SHF_ALLOC sections */
  615. buf_addr = purgatory_buf;
  616. load_addr = curr_load_addr = pi->purgatory_load_addr;
  617. bss_addr = load_addr + buf_sz + bss_pad;
  618. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  619. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  620. continue;
  621. align = sechdrs[i].sh_addralign;
  622. if (sechdrs[i].sh_type != SHT_NOBITS) {
  623. curr_load_addr = ALIGN(curr_load_addr, align);
  624. offset = curr_load_addr - load_addr;
  625. /* We already modifed ->sh_offset to keep src addr */
  626. src = (char *) sechdrs[i].sh_offset;
  627. memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
  628. /* Store load address and source address of section */
  629. sechdrs[i].sh_addr = curr_load_addr;
  630. /*
  631. * This section got copied to temporary buffer. Update
  632. * ->sh_offset accordingly.
  633. */
  634. sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
  635. /* Advance to the next address */
  636. curr_load_addr += sechdrs[i].sh_size;
  637. } else {
  638. bss_addr = ALIGN(bss_addr, align);
  639. sechdrs[i].sh_addr = bss_addr;
  640. bss_addr += sechdrs[i].sh_size;
  641. }
  642. }
  643. /* Update entry point based on load address of text section */
  644. if (entry_sidx >= 0)
  645. entry += sechdrs[entry_sidx].sh_addr;
  646. /* Make kernel jump to purgatory after shutdown */
  647. image->start = entry;
  648. /* Used later to get/set symbol values */
  649. pi->sechdrs = sechdrs;
  650. /*
  651. * Used later to identify which section is purgatory and skip it
  652. * from checksumming.
  653. */
  654. pi->purgatory_buf = purgatory_buf;
  655. return ret;
  656. out:
  657. vfree(sechdrs);
  658. vfree(purgatory_buf);
  659. return ret;
  660. }
  661. static int kexec_apply_relocations(struct kimage *image)
  662. {
  663. int i, ret;
  664. struct purgatory_info *pi = &image->purgatory_info;
  665. Elf_Shdr *sechdrs = pi->sechdrs;
  666. /* Apply relocations */
  667. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  668. Elf_Shdr *section, *symtab;
  669. if (sechdrs[i].sh_type != SHT_RELA &&
  670. sechdrs[i].sh_type != SHT_REL)
  671. continue;
  672. /*
  673. * For section of type SHT_RELA/SHT_REL,
  674. * ->sh_link contains section header index of associated
  675. * symbol table. And ->sh_info contains section header
  676. * index of section to which relocations apply.
  677. */
  678. if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
  679. sechdrs[i].sh_link >= pi->ehdr->e_shnum)
  680. return -ENOEXEC;
  681. section = &sechdrs[sechdrs[i].sh_info];
  682. symtab = &sechdrs[sechdrs[i].sh_link];
  683. if (!(section->sh_flags & SHF_ALLOC))
  684. continue;
  685. /*
  686. * symtab->sh_link contain section header index of associated
  687. * string table.
  688. */
  689. if (symtab->sh_link >= pi->ehdr->e_shnum)
  690. /* Invalid section number? */
  691. continue;
  692. /*
  693. * Respective architecture needs to provide support for applying
  694. * relocations of type SHT_RELA/SHT_REL.
  695. */
  696. if (sechdrs[i].sh_type == SHT_RELA)
  697. ret = arch_kexec_apply_relocations_add(pi->ehdr,
  698. sechdrs, i);
  699. else if (sechdrs[i].sh_type == SHT_REL)
  700. ret = arch_kexec_apply_relocations(pi->ehdr,
  701. sechdrs, i);
  702. if (ret)
  703. return ret;
  704. }
  705. return 0;
  706. }
  707. /* Load relocatable purgatory object and relocate it appropriately */
  708. int kexec_load_purgatory(struct kimage *image, unsigned long min,
  709. unsigned long max, int top_down,
  710. unsigned long *load_addr)
  711. {
  712. struct purgatory_info *pi = &image->purgatory_info;
  713. int ret;
  714. if (kexec_purgatory_size <= 0)
  715. return -EINVAL;
  716. if (kexec_purgatory_size < sizeof(Elf_Ehdr))
  717. return -ENOEXEC;
  718. pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
  719. if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
  720. || pi->ehdr->e_type != ET_REL
  721. || !elf_check_arch(pi->ehdr)
  722. || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
  723. return -ENOEXEC;
  724. if (pi->ehdr->e_shoff >= kexec_purgatory_size
  725. || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
  726. kexec_purgatory_size - pi->ehdr->e_shoff))
  727. return -ENOEXEC;
  728. ret = __kexec_load_purgatory(image, min, max, top_down);
  729. if (ret)
  730. return ret;
  731. ret = kexec_apply_relocations(image);
  732. if (ret)
  733. goto out;
  734. *load_addr = pi->purgatory_load_addr;
  735. return 0;
  736. out:
  737. vfree(pi->sechdrs);
  738. vfree(pi->purgatory_buf);
  739. return ret;
  740. }
  741. static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
  742. const char *name)
  743. {
  744. Elf_Sym *syms;
  745. Elf_Shdr *sechdrs;
  746. Elf_Ehdr *ehdr;
  747. int i, k;
  748. const char *strtab;
  749. if (!pi->sechdrs || !pi->ehdr)
  750. return NULL;
  751. sechdrs = pi->sechdrs;
  752. ehdr = pi->ehdr;
  753. for (i = 0; i < ehdr->e_shnum; i++) {
  754. if (sechdrs[i].sh_type != SHT_SYMTAB)
  755. continue;
  756. if (sechdrs[i].sh_link >= ehdr->e_shnum)
  757. /* Invalid strtab section number */
  758. continue;
  759. strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset;
  760. syms = (Elf_Sym *)sechdrs[i].sh_offset;
  761. /* Go through symbols for a match */
  762. for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
  763. if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
  764. continue;
  765. if (strcmp(strtab + syms[k].st_name, name) != 0)
  766. continue;
  767. if (syms[k].st_shndx == SHN_UNDEF ||
  768. syms[k].st_shndx >= ehdr->e_shnum) {
  769. pr_debug("Symbol: %s has bad section index %d.\n",
  770. name, syms[k].st_shndx);
  771. return NULL;
  772. }
  773. /* Found the symbol we are looking for */
  774. return &syms[k];
  775. }
  776. }
  777. return NULL;
  778. }
  779. void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
  780. {
  781. struct purgatory_info *pi = &image->purgatory_info;
  782. Elf_Sym *sym;
  783. Elf_Shdr *sechdr;
  784. sym = kexec_purgatory_find_symbol(pi, name);
  785. if (!sym)
  786. return ERR_PTR(-EINVAL);
  787. sechdr = &pi->sechdrs[sym->st_shndx];
  788. /*
  789. * Returns the address where symbol will finally be loaded after
  790. * kexec_load_segment()
  791. */
  792. return (void *)(sechdr->sh_addr + sym->st_value);
  793. }
  794. /*
  795. * Get or set value of a symbol. If "get_value" is true, symbol value is
  796. * returned in buf otherwise symbol value is set based on value in buf.
  797. */
  798. int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
  799. void *buf, unsigned int size, bool get_value)
  800. {
  801. Elf_Sym *sym;
  802. Elf_Shdr *sechdrs;
  803. struct purgatory_info *pi = &image->purgatory_info;
  804. char *sym_buf;
  805. sym = kexec_purgatory_find_symbol(pi, name);
  806. if (!sym)
  807. return -EINVAL;
  808. if (sym->st_size != size) {
  809. pr_err("symbol %s size mismatch: expected %lu actual %u\n",
  810. name, (unsigned long)sym->st_size, size);
  811. return -EINVAL;
  812. }
  813. sechdrs = pi->sechdrs;
  814. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  815. pr_err("symbol %s is in a bss section. Cannot %s\n", name,
  816. get_value ? "get" : "set");
  817. return -EINVAL;
  818. }
  819. sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset +
  820. sym->st_value;
  821. if (get_value)
  822. memcpy((void *)buf, sym_buf, size);
  823. else
  824. memcpy((void *)sym_buf, buf, size);
  825. return 0;
  826. }