kexec_file.c 25 KB

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