livepatch-shadow-fix1.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /*
  53. * The constructor makes more sense together with klp_shadow_get_or_alloc().
  54. * In this example, it would be safe to assign the pointer also to the shadow
  55. * variable returned by klp_shadow_alloc(). But we wanted to show the more
  56. * complicated use of the API.
  57. */
  58. static int shadow_leak_ctor(void *obj, void *shadow_data, void *ctor_data)
  59. {
  60. void **shadow_leak = shadow_data;
  61. void *leak = ctor_data;
  62. *shadow_leak = leak;
  63. return 0;
  64. }
  65. struct dummy *livepatch_fix1_dummy_alloc(void)
  66. {
  67. struct dummy *d;
  68. void *leak;
  69. d = kzalloc(sizeof(*d), GFP_KERNEL);
  70. if (!d)
  71. return NULL;
  72. d->jiffies_expire = jiffies +
  73. msecs_to_jiffies(1000 * EXPIRE_PERIOD);
  74. /*
  75. * Patch: save the extra memory location into a SV_LEAK shadow
  76. * variable. A patched dummy_free routine can later fetch this
  77. * pointer to handle resource release.
  78. */
  79. leak = kzalloc(sizeof(int), GFP_KERNEL);
  80. klp_shadow_alloc(d, SV_LEAK, sizeof(leak), GFP_KERNEL,
  81. shadow_leak_ctor, leak);
  82. pr_info("%s: dummy @ %p, expires @ %lx\n",
  83. __func__, d, d->jiffies_expire);
  84. return d;
  85. }
  86. static void livepatch_fix1_dummy_leak_dtor(void *obj, void *shadow_data)
  87. {
  88. void *d = obj;
  89. void **shadow_leak = shadow_data;
  90. kfree(*shadow_leak);
  91. pr_info("%s: dummy @ %p, prevented leak @ %p\n",
  92. __func__, d, *shadow_leak);
  93. }
  94. void livepatch_fix1_dummy_free(struct dummy *d)
  95. {
  96. void **shadow_leak;
  97. /*
  98. * Patch: fetch the saved SV_LEAK shadow variable, detach and
  99. * free it. Note: handle cases where this shadow variable does
  100. * not exist (ie, dummy structures allocated before this livepatch
  101. * was loaded.)
  102. */
  103. shadow_leak = klp_shadow_get(d, SV_LEAK);
  104. if (shadow_leak)
  105. klp_shadow_free(d, SV_LEAK, livepatch_fix1_dummy_leak_dtor);
  106. else
  107. pr_info("%s: dummy @ %p leaked!\n", __func__, d);
  108. kfree(d);
  109. }
  110. static struct klp_func funcs[] = {
  111. {
  112. .old_name = "dummy_alloc",
  113. .new_func = livepatch_fix1_dummy_alloc,
  114. },
  115. {
  116. .old_name = "dummy_free",
  117. .new_func = livepatch_fix1_dummy_free,
  118. }, { }
  119. };
  120. static struct klp_object objs[] = {
  121. {
  122. .name = "livepatch_shadow_mod",
  123. .funcs = funcs,
  124. }, { }
  125. };
  126. static struct klp_patch patch = {
  127. .mod = THIS_MODULE,
  128. .objs = objs,
  129. };
  130. static int livepatch_shadow_fix1_init(void)
  131. {
  132. int ret;
  133. ret = klp_register_patch(&patch);
  134. if (ret)
  135. return ret;
  136. ret = klp_enable_patch(&patch);
  137. if (ret) {
  138. WARN_ON(klp_unregister_patch(&patch));
  139. return ret;
  140. }
  141. return 0;
  142. }
  143. static void livepatch_shadow_fix1_exit(void)
  144. {
  145. /* Cleanup any existing SV_LEAK shadow variables */
  146. klp_shadow_free_all(SV_LEAK, livepatch_fix1_dummy_leak_dtor);
  147. WARN_ON(klp_unregister_patch(&patch));
  148. }
  149. module_init(livepatch_shadow_fix1_init);
  150. module_exit(livepatch_shadow_fix1_exit);
  151. MODULE_LICENSE("GPL");
  152. MODULE_INFO(livepatch, "Y");