livepatch-shadow-fix1.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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-fix1.c - Shadow variables, livepatch demo
  19. *
  20. * Purpose
  21. * -------
  22. *
  23. * Fixes the memory leak introduced in livepatch-shadow-mod through the
  24. * use of a shadow variable. This fix demonstrates the "extending" of
  25. * short-lived data structures by patching its allocation and release
  26. * functions.
  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. /* Allocate new dummies every second */
  43. #define ALLOC_PERIOD 1
  44. /* Check for expired dummies after a few new ones have been allocated */
  45. #define CLEANUP_PERIOD (3 * ALLOC_PERIOD)
  46. /* Dummies expire after a few cleanup instances */
  47. #define EXPIRE_PERIOD (4 * CLEANUP_PERIOD)
  48. struct dummy {
  49. struct list_head list;
  50. unsigned long jiffies_expire;
  51. };
  52. struct dummy *livepatch_fix1_dummy_alloc(void)
  53. {
  54. struct dummy *d;
  55. void *leak;
  56. d = kzalloc(sizeof(*d), GFP_KERNEL);
  57. if (!d)
  58. return NULL;
  59. d->jiffies_expire = jiffies +
  60. msecs_to_jiffies(1000 * EXPIRE_PERIOD);
  61. /*
  62. * Patch: save the extra memory location into a SV_LEAK shadow
  63. * variable. A patched dummy_free routine can later fetch this
  64. * pointer to handle resource release.
  65. */
  66. leak = kzalloc(sizeof(int), GFP_KERNEL);
  67. klp_shadow_alloc(d, SV_LEAK, &leak, sizeof(leak), GFP_KERNEL);
  68. pr_info("%s: dummy @ %p, expires @ %lx\n",
  69. __func__, d, d->jiffies_expire);
  70. return d;
  71. }
  72. void livepatch_fix1_dummy_free(struct dummy *d)
  73. {
  74. void **shadow_leak, *leak;
  75. /*
  76. * Patch: fetch the saved SV_LEAK shadow variable, detach and
  77. * free it. Note: handle cases where this shadow variable does
  78. * not exist (ie, dummy structures allocated before this livepatch
  79. * was loaded.)
  80. */
  81. shadow_leak = klp_shadow_get(d, SV_LEAK);
  82. if (shadow_leak) {
  83. leak = *shadow_leak;
  84. klp_shadow_free(d, SV_LEAK);
  85. kfree(leak);
  86. pr_info("%s: dummy @ %p, prevented leak @ %p\n",
  87. __func__, d, leak);
  88. } else {
  89. pr_info("%s: dummy @ %p leaked!\n", __func__, d);
  90. }
  91. kfree(d);
  92. }
  93. static struct klp_func funcs[] = {
  94. {
  95. .old_name = "dummy_alloc",
  96. .new_func = livepatch_fix1_dummy_alloc,
  97. },
  98. {
  99. .old_name = "dummy_free",
  100. .new_func = livepatch_fix1_dummy_free,
  101. }, { }
  102. };
  103. static struct klp_object objs[] = {
  104. {
  105. .name = "livepatch_shadow_mod",
  106. .funcs = funcs,
  107. }, { }
  108. };
  109. static struct klp_patch patch = {
  110. .mod = THIS_MODULE,
  111. .objs = objs,
  112. };
  113. static int livepatch_shadow_fix1_init(void)
  114. {
  115. int ret;
  116. if (!klp_have_reliable_stack() && !patch.immediate) {
  117. /*
  118. * WARNING: Be very careful when using 'patch.immediate' in
  119. * your patches. It's ok to use it for simple patches like
  120. * this, but for more complex patches which change function
  121. * semantics, locking semantics, or data structures, it may not
  122. * be safe. Use of this option will also prevent removal of
  123. * the patch.
  124. *
  125. * See Documentation/livepatch/livepatch.txt for more details.
  126. */
  127. patch.immediate = true;
  128. pr_notice("The consistency model isn't supported for your architecture. Bypassing safety mechanisms and applying the patch immediately.\n");
  129. }
  130. ret = klp_register_patch(&patch);
  131. if (ret)
  132. return ret;
  133. ret = klp_enable_patch(&patch);
  134. if (ret) {
  135. WARN_ON(klp_unregister_patch(&patch));
  136. return ret;
  137. }
  138. return 0;
  139. }
  140. static void livepatch_shadow_fix1_exit(void)
  141. {
  142. /* Cleanup any existing SV_LEAK shadow variables */
  143. klp_shadow_free_all(SV_LEAK);
  144. WARN_ON(klp_unregister_patch(&patch));
  145. }
  146. module_init(livepatch_shadow_fix1_init);
  147. module_exit(livepatch_shadow_fix1_exit);
  148. MODULE_LICENSE("GPL");
  149. MODULE_INFO(livepatch, "Y");