livepatch.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Module livepatch support
  4. *
  5. * Copyright (C) 2016 Jessica Yu <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/string.h>
  9. #include <linux/slab.h>
  10. #include "internal.h"
  11. /*
  12. * Persist Elf information about a module. Copy the Elf header,
  13. * section header table, section string table, and symtab section
  14. * index from info to mod->klp_info.
  15. */
  16. int copy_module_elf(struct module *mod, struct load_info *info)
  17. {
  18. unsigned int size, symndx;
  19. int ret;
  20. size = sizeof(*mod->klp_info);
  21. mod->klp_info = kmalloc(size, GFP_KERNEL);
  22. if (!mod->klp_info)
  23. return -ENOMEM;
  24. /* Elf header */
  25. size = sizeof(mod->klp_info->hdr);
  26. memcpy(&mod->klp_info->hdr, info->hdr, size);
  27. /* Elf section header table */
  28. size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
  29. mod->klp_info->sechdrs = kmemdup(info->sechdrs, size, GFP_KERNEL);
  30. if (!mod->klp_info->sechdrs) {
  31. ret = -ENOMEM;
  32. goto free_info;
  33. }
  34. /* Elf section name string table */
  35. size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
  36. mod->klp_info->secstrings = kmemdup(info->secstrings, size, GFP_KERNEL);
  37. if (!mod->klp_info->secstrings) {
  38. ret = -ENOMEM;
  39. goto free_sechdrs;
  40. }
  41. /* Elf symbol section index */
  42. symndx = info->index.sym;
  43. mod->klp_info->symndx = symndx;
  44. /*
  45. * For livepatch modules, core_kallsyms.symtab is a complete
  46. * copy of the original symbol table. Adjust sh_addr to point
  47. * to core_kallsyms.symtab since the copy of the symtab in module
  48. * init memory is freed at the end of do_init_module().
  49. */
  50. mod->klp_info->sechdrs[symndx].sh_addr = (unsigned long)mod->core_kallsyms.symtab;
  51. return 0;
  52. free_sechdrs:
  53. kfree(mod->klp_info->sechdrs);
  54. free_info:
  55. kfree(mod->klp_info);
  56. return ret;
  57. }
  58. void free_module_elf(struct module *mod)
  59. {
  60. kfree(mod->klp_info->sechdrs);
  61. kfree(mod->klp_info->secstrings);
  62. kfree(mod->klp_info);
  63. }