livepatch.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/elf.h>
  23. #include <asm/livepatch.h>
  24. /**
  25. * klp_write_module_reloc() - write a relocation in a module
  26. * @mod: module in which the section to be modified is found
  27. * @type: ELF relocation type (see asm/elf.h)
  28. * @loc: address that the relocation should be written to
  29. * @value: relocation value (sym address + addend)
  30. *
  31. * This function writes a relocation to the specified location for
  32. * a particular module.
  33. */
  34. int klp_write_module_reloc(struct module *mod, unsigned long type,
  35. unsigned long loc, unsigned long value)
  36. {
  37. size_t size = 4;
  38. unsigned long val;
  39. unsigned long core = (unsigned long)mod->core_layout.base;
  40. unsigned long core_size = mod->core_layout.size;
  41. switch (type) {
  42. case R_X86_64_NONE:
  43. return 0;
  44. case R_X86_64_64:
  45. val = value;
  46. size = 8;
  47. break;
  48. case R_X86_64_32:
  49. val = (u32)value;
  50. break;
  51. case R_X86_64_32S:
  52. val = (s32)value;
  53. break;
  54. case R_X86_64_PC32:
  55. val = (u32)(value - loc);
  56. break;
  57. default:
  58. /* unsupported relocation type */
  59. return -EINVAL;
  60. }
  61. if (loc < core || loc >= core + core_size)
  62. /* loc does not point to any symbol inside the module */
  63. return -EINVAL;
  64. return probe_kernel_write((void *)loc, &val, size);
  65. }