livepatch-sample.c 2.0 KB

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