livepatch.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * livepatch.c - x86-specific Kernel Live Patching Core
  3. *
  4. * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
  5. * Copyright (C) 2014 SUSE
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/page_types.h>
  24. #include <asm/elf.h>
  25. #include <asm/livepatch.h>
  26. /**
  27. * klp_write_module_reloc() - write a relocation in a module
  28. * @mod: module in which the section to be modified is found
  29. * @type: ELF relocation type (see asm/elf.h)
  30. * @loc: address that the relocation should be written to
  31. * @value: relocation value (sym address + addend)
  32. *
  33. * This function writes a relocation to the specified location for
  34. * a particular module.
  35. */
  36. int klp_write_module_reloc(struct module *mod, unsigned long type,
  37. unsigned long loc, unsigned long value)
  38. {
  39. int ret, numpages, size = 4;
  40. bool readonly;
  41. unsigned long val;
  42. unsigned long core = (unsigned long)mod->module_core;
  43. unsigned long core_size = mod->core_size;
  44. switch (type) {
  45. case R_X86_64_NONE:
  46. return 0;
  47. case R_X86_64_64:
  48. val = value;
  49. size = 8;
  50. break;
  51. case R_X86_64_32:
  52. val = (u32)value;
  53. break;
  54. case R_X86_64_32S:
  55. val = (s32)value;
  56. break;
  57. case R_X86_64_PC32:
  58. case R_X86_64_PLT32:
  59. val = (u32)(value - loc);
  60. break;
  61. default:
  62. /* unsupported relocation type */
  63. return -EINVAL;
  64. }
  65. if (loc < core || loc >= core + core_size)
  66. /* loc does not point to any symbol inside the module */
  67. return -EINVAL;
  68. readonly = false;
  69. #ifdef CONFIG_DEBUG_SET_MODULE_RONX
  70. if (loc < core + mod->core_ro_size)
  71. readonly = true;
  72. #endif
  73. /* determine if the relocation spans a page boundary */
  74. numpages = ((loc & PAGE_MASK) == ((loc + size) & PAGE_MASK)) ? 1 : 2;
  75. if (readonly)
  76. set_memory_rw(loc & PAGE_MASK, numpages);
  77. ret = probe_kernel_write((void *)loc, &val, size);
  78. if (readonly)
  79. set_memory_ro(loc & PAGE_MASK, numpages);
  80. return ret;
  81. }