livepatch-shadow-fix2.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. int count;
  51. /*
  52. * Patch: handle in-flight dummy structures, if they do not
  53. * already have a SV_COUNTER shadow variable, then attach a
  54. * new one.
  55. */
  56. count = 0;
  57. shadow_count = klp_shadow_get_or_alloc(d, SV_COUNTER,
  58. &count, sizeof(count),
  59. GFP_NOWAIT);
  60. if (shadow_count)
  61. *shadow_count += 1;
  62. return time_after(jiffies, d->jiffies_expire);
  63. }
  64. void livepatch_fix2_dummy_free(struct dummy *d)
  65. {
  66. void **shadow_leak, *leak;
  67. int *shadow_count;
  68. /* Patch: copy the memory leak patch from the fix1 module. */
  69. shadow_leak = klp_shadow_get(d, SV_LEAK);
  70. if (shadow_leak) {
  71. leak = *shadow_leak;
  72. klp_shadow_free(d, SV_LEAK);
  73. kfree(leak);
  74. pr_info("%s: dummy @ %p, prevented leak @ %p\n",
  75. __func__, d, leak);
  76. } else {
  77. pr_info("%s: dummy @ %p leaked!\n", __func__, d);
  78. }
  79. /*
  80. * Patch: fetch the SV_COUNTER shadow variable and display
  81. * the final count. Detach the shadow variable.
  82. */
  83. shadow_count = klp_shadow_get(d, SV_COUNTER);
  84. if (shadow_count) {
  85. pr_info("%s: dummy @ %p, check counter = %d\n",
  86. __func__, d, *shadow_count);
  87. klp_shadow_free(d, SV_COUNTER);
  88. }
  89. kfree(d);
  90. }
  91. static struct klp_func funcs[] = {
  92. {
  93. .old_name = "dummy_check",
  94. .new_func = livepatch_fix2_dummy_check,
  95. },
  96. {
  97. .old_name = "dummy_free",
  98. .new_func = livepatch_fix2_dummy_free,
  99. }, { }
  100. };
  101. static struct klp_object objs[] = {
  102. {
  103. .name = "livepatch_shadow_mod",
  104. .funcs = funcs,
  105. }, { }
  106. };
  107. static struct klp_patch patch = {
  108. .mod = THIS_MODULE,
  109. .objs = objs,
  110. };
  111. static int livepatch_shadow_fix2_init(void)
  112. {
  113. int ret;
  114. if (!klp_have_reliable_stack() && !patch.immediate) {
  115. /*
  116. * WARNING: Be very careful when using 'patch.immediate' in
  117. * your patches. It's ok to use it for simple patches like
  118. * this, but for more complex patches which change function
  119. * semantics, locking semantics, or data structures, it may not
  120. * be safe. Use of this option will also prevent removal of
  121. * the patch.
  122. *
  123. * See Documentation/livepatch/livepatch.txt for more details.
  124. */
  125. patch.immediate = true;
  126. pr_notice("The consistency model isn't supported for your architecture. Bypassing safety mechanisms and applying the patch immediately.\n");
  127. }
  128. ret = klp_register_patch(&patch);
  129. if (ret)
  130. return ret;
  131. ret = klp_enable_patch(&patch);
  132. if (ret) {
  133. WARN_ON(klp_unregister_patch(&patch));
  134. return ret;
  135. }
  136. return 0;
  137. }
  138. static void livepatch_shadow_fix2_exit(void)
  139. {
  140. /* Cleanup any existing SV_COUNTER shadow variables */
  141. klp_shadow_free_all(SV_COUNTER);
  142. WARN_ON(klp_unregister_patch(&patch));
  143. }
  144. module_init(livepatch_shadow_fix2_init);
  145. module_exit(livepatch_shadow_fix2_exit);
  146. MODULE_LICENSE("GPL");
  147. MODULE_INFO(livepatch, "Y");