kexec_file.c 25 KB

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