livepatch-sample.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * livepatch-sample.c - Kernel Live Patching Sample Module
  3. *
  4. * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/livepatch.h>
  23. /*
  24. * This (dumb) live patch overrides the function that prints the
  25. * kernel boot cmdline when /proc/cmdline is read.
  26. *
  27. * Example:
  28. *
  29. * $ cat /proc/cmdline
  30. * <your cmdline>
  31. *
  32. * $ insmod livepatch-sample.ko
  33. * $ cat /proc/cmdline
  34. * this has been live patched
  35. *
  36. * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled
  37. * $ cat /proc/cmdline
  38. * <your cmdline>
  39. */
  40. #include <linux/seq_file.h>
  41. static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
  42. {
  43. seq_printf(m, "%s\n", "this has been live patched");
  44. return 0;
  45. }
  46. static struct klp_func funcs[] = {
  47. {
  48. .old_name = "cmdline_proc_show",
  49. .new_func = livepatch_cmdline_proc_show,
  50. }, { }
  51. };
  52. static struct klp_object objs[] = {
  53. {
  54. /* name being NULL means vmlinux */
  55. .funcs = funcs,
  56. }, { }
  57. };
  58. static struct klp_patch patch = {
  59. .mod = THIS_MODULE,
  60. .objs = objs,
  61. };
  62. static int livepatch_init(void)
  63. {
  64. int ret;
  65. ret = klp_register_patch(&patch);
  66. if (ret)
  67. return ret;
  68. ret = klp_enable_patch(&patch);
  69. if (ret) {
  70. WARN_ON(klp_unregister_patch(&patch));
  71. return ret;
  72. }
  73. return 0;
  74. }
  75. static void livepatch_exit(void)
  76. {
  77. WARN_ON(klp_unregister_patch(&patch));
  78. }
  79. module_init(livepatch_init);
  80. module_exit(livepatch_exit);
  81. MODULE_LICENSE("GPL");
  82. MODULE_INFO(livepatch, "Y");