kexec_file.c 25 KB

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