livepatch-sample.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/livepatch.h>
  22. /*
  23. * This (dumb) live patch overrides the function that prints the
  24. * kernel boot cmdline when /proc/cmdline is read.
  25. *
  26. * Example:
  27. *
  28. * $ cat /proc/cmdline
  29. * <your cmdline>
  30. *
  31. * $ insmod livepatch-sample.ko
  32. * $ cat /proc/cmdline
  33. * this has been live patched
  34. *
  35. * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled
  36. * $ cat /proc/cmdline
  37. * <your cmdline>
  38. */
  39. #include <linux/seq_file.h>
  40. static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
  41. {
  42. seq_printf(m, "%s\n", "this has been live patched");
  43. return 0;
  44. }
  45. static struct klp_func funcs[] = {
  46. {
  47. .old_name = "cmdline_proc_show",
  48. .new_func = livepatch_cmdline_proc_show,
  49. }, { }
  50. };
  51. static struct klp_object objs[] = {
  52. {
  53. /* name being NULL means vmlinux */
  54. .funcs = funcs,
  55. }, { }
  56. };
  57. static struct klp_patch patch = {
  58. .mod = THIS_MODULE,
  59. .objs = objs,
  60. };
  61. static int livepatch_init(void)
  62. {
  63. int ret;
  64. ret = klp_register_patch(&patch);
  65. if (ret)
  66. return ret;
  67. ret = klp_enable_patch(&patch);
  68. if (ret) {
  69. WARN_ON(klp_unregister_patch(&patch));
  70. return ret;
  71. }
  72. return 0;
  73. }
  74. static void livepatch_exit(void)
  75. {
  76. WARN_ON(klp_disable_patch(&patch));
  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");