kexec_file.c 25 KB

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