livepatch-shadow-fix2.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. static void livepatch_fix2_dummy_leak_dtor(void *obj, void *shadow_data)
  63. {
  64. void *d = obj;
  65. void **shadow_leak = shadow_data;
  66. kfree(*shadow_leak);
  67. pr_info("%s: dummy @ %p, prevented leak @ %p\n",
  68. __func__, d, *shadow_leak);
  69. }
  70. void livepatch_fix2_dummy_free(struct dummy *d)
  71. {
  72. void **shadow_leak;
  73. int *shadow_count;
  74. /* Patch: copy the memory leak patch from the fix1 module. */
  75. shadow_leak = klp_shadow_get(d, SV_LEAK);
  76. if (shadow_leak)
  77. klp_shadow_free(d, SV_LEAK, livepatch_fix2_dummy_leak_dtor);
  78. else
  79. pr_info("%s: dummy @ %p leaked!\n", __func__, d);
  80. /*
  81. * Patch: fetch the SV_COUNTER shadow variable and display
  82. * the final count. Detach the shadow variable.
  83. */
  84. shadow_count = klp_shadow_get(d, SV_COUNTER);
  85. if (shadow_count) {
  86. pr_info("%s: dummy @ %p, check counter = %d\n",
  87. __func__, d, *shadow_count);
  88. klp_shadow_free(d, SV_COUNTER, NULL);
  89. }
  90. kfree(d);
  91. }
  92. static struct klp_func funcs[] = {
  93. {
  94. .old_name = "dummy_check",
  95. .new_func = livepatch_fix2_dummy_check,
  96. },
  97. {
  98. .old_name = "dummy_free",
  99. .new_func = livepatch_fix2_dummy_free,
  100. }, { }
  101. };
  102. static struct klp_object objs[] = {
  103. {
  104. .name = "livepatch_shadow_mod",
  105. .funcs = funcs,
  106. }, { }
  107. };
  108. static struct klp_patch patch = {
  109. .mod = THIS_MODULE,
  110. .objs = objs,
  111. };
  112. static int livepatch_shadow_fix2_init(void)
  113. {
  114. int ret;
  115. ret = klp_register_patch(&patch);
  116. if (ret)
  117. return ret;
  118. ret = klp_enable_patch(&patch);
  119. if (ret) {
  120. WARN_ON(klp_unregister_patch(&patch));
  121. return ret;
  122. }
  123. return 0;
  124. }
  125. static void livepatch_shadow_fix2_exit(void)
  126. {
  127. /* Cleanup any existing SV_COUNTER shadow variables */
  128. klp_shadow_free_all(SV_COUNTER, NULL);
  129. WARN_ON(klp_unregister_patch(&patch));
  130. }
  131. module_init(livepatch_shadow_fix2_init);
  132. module_exit(livepatch_shadow_fix2_exit);
  133. MODULE_LICENSE("GPL");
  134. MODULE_INFO(livepatch, "Y");