livepatch-shadow-fix2.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2017 Joe Lawrence <joe.lawrence@redhat.com>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * livepatch-shadow-fix2.c - Shadow variables, livepatch demo
  19. *
  20. * Purpose
  21. * -------
  22. *
  23. * Adds functionality to livepatch-shadow-mod's in-flight data
  24. * structures through a shadow variable. The livepatch patches a
  25. * routine that periodically inspects data structures, incrementing a
  26. * per-data-structure counter, creating the counter if needed.
  27. *
  28. *
  29. * Usage
  30. * -----
  31. *
  32. * This module is not intended to be standalone. See the "Usage"
  33. * section of livepatch-shadow-mod.c.
  34. */
  35. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  36. #include <linux/module.h>
  37. #include <linux/kernel.h>
  38. #include <linux/livepatch.h>
  39. #include <linux/slab.h>
  40. /* Shadow variable enums */
  41. #define SV_LEAK 1
  42. #define SV_COUNTER 2
  43. struct dummy {
  44. struct list_head list;
  45. unsigned long jiffies_expire;
  46. };
  47. bool livepatch_fix2_dummy_check(struct dummy *d, unsigned long jiffies)
  48. {
  49. int *shadow_count;
  50. /*
  51. * Patch: handle in-flight dummy structures, if they do not
  52. * already have a SV_COUNTER shadow variable, then attach a
  53. * new one.
  54. */
  55. shadow_count = klp_shadow_get_or_alloc(d, SV_COUNTER,
  56. sizeof(*shadow_count), GFP_NOWAIT,
  57. NULL, NULL);
  58. if (shadow_count)
  59. *shadow_count += 1;
  60. return time_after(jiffies, d->jiffies_expire);
  61. }
  62. void livepatch_fix2_dummy_free(struct dummy *d)
  63. {
  64. void **shadow_leak, *leak;
  65. int *shadow_count;
  66. /* Patch: copy the memory leak patch from the fix1 module. */
  67. shadow_leak = klp_shadow_get(d, SV_LEAK);
  68. if (shadow_leak) {
  69. leak = *shadow_leak;
  70. klp_shadow_free(d, SV_LEAK);
  71. kfree(leak);
  72. pr_info("%s: dummy @ %p, prevented leak @ %p\n",
  73. __func__, d, leak);
  74. } else {
  75. pr_info("%s: dummy @ %p leaked!\n", __func__, d);
  76. }
  77. /*
  78. * Patch: fetch the SV_COUNTER shadow variable and display
  79. * the final count. Detach the shadow variable.
  80. */
  81. shadow_count = klp_shadow_get(d, SV_COUNTER);
  82. if (shadow_count) {
  83. pr_info("%s: dummy @ %p, check counter = %d\n",
  84. __func__, d, *shadow_count);
  85. klp_shadow_free(d, SV_COUNTER);
  86. }
  87. kfree(d);
  88. }
  89. static struct klp_func funcs[] = {
  90. {
  91. .old_name = "dummy_check",
  92. .new_func = livepatch_fix2_dummy_check,
  93. },
  94. {
  95. .old_name = "dummy_free",
  96. .new_func = livepatch_fix2_dummy_free,
  97. }, { }
  98. };
  99. static struct klp_object objs[] = {
  100. {
  101. .name = "livepatch_shadow_mod",
  102. .funcs = funcs,
  103. }, { }
  104. };
  105. static struct klp_patch patch = {
  106. .mod = THIS_MODULE,
  107. .objs = objs,
  108. };
  109. static int livepatch_shadow_fix2_init(void)
  110. {
  111. int ret;
  112. ret = klp_register_patch(&patch);
  113. if (ret)
  114. return ret;
  115. ret = klp_enable_patch(&patch);
  116. if (ret) {
  117. WARN_ON(klp_unregister_patch(&patch));
  118. return ret;
  119. }
  120. return 0;
  121. }
  122. static void livepatch_shadow_fix2_exit(void)
  123. {
  124. /* Cleanup any existing SV_COUNTER shadow variables */
  125. klp_shadow_free_all(SV_COUNTER);
  126. WARN_ON(klp_unregister_patch(&patch));
  127. }
  128. module_init(livepatch_shadow_fix2_init);
  129. module_exit(livepatch_shadow_fix2_exit);
  130. MODULE_LICENSE("GPL");
  131. MODULE_INFO(livepatch, "Y");