livepatch-shadow-fix1.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. if (!leak) {
  81. kfree(d);
  82. return NULL;
  83. }
  84. klp_shadow_alloc(d, SV_LEAK, sizeof(leak), GFP_KERNEL,
  85. shadow_leak_ctor, leak);
  86. pr_info("%s: dummy @ %p, expires @ %lx\n",
  87. __func__, d, d->jiffies_expire);
  88. return d;
  89. }
  90. static void livepatch_fix1_dummy_leak_dtor(void *obj, void *shadow_data)
  91. {
  92. void *d = obj;
  93. void **shadow_leak = shadow_data;
  94. kfree(*shadow_leak);
  95. pr_info("%s: dummy @ %p, prevented leak @ %p\n",
  96. __func__, d, *shadow_leak);
  97. }
  98. void livepatch_fix1_dummy_free(struct dummy *d)
  99. {
  100. void **shadow_leak;
  101. /*
  102. * Patch: fetch the saved SV_LEAK shadow variable, detach and
  103. * free it. Note: handle cases where this shadow variable does
  104. * not exist (ie, dummy structures allocated before this livepatch
  105. * was loaded.)
  106. */
  107. shadow_leak = klp_shadow_get(d, SV_LEAK);
  108. if (shadow_leak)
  109. klp_shadow_free(d, SV_LEAK, livepatch_fix1_dummy_leak_dtor);
  110. else
  111. pr_info("%s: dummy @ %p leaked!\n", __func__, d);
  112. kfree(d);
  113. }
  114. static struct klp_func funcs[] = {
  115. {
  116. .old_name = "dummy_alloc",
  117. .new_func = livepatch_fix1_dummy_alloc,
  118. },
  119. {
  120. .old_name = "dummy_free",
  121. .new_func = livepatch_fix1_dummy_free,
  122. }, { }
  123. };
  124. static struct klp_object objs[] = {
  125. {
  126. .name = "livepatch_shadow_mod",
  127. .funcs = funcs,
  128. }, { }
  129. };
  130. static struct klp_patch patch = {
  131. .mod = THIS_MODULE,
  132. .objs = objs,
  133. };
  134. static int livepatch_shadow_fix1_init(void)
  135. {
  136. int ret;
  137. ret = klp_register_patch(&patch);
  138. if (ret)
  139. return ret;
  140. ret = klp_enable_patch(&patch);
  141. if (ret) {
  142. WARN_ON(klp_unregister_patch(&patch));
  143. return ret;
  144. }
  145. return 0;
  146. }
  147. static void livepatch_shadow_fix1_exit(void)
  148. {
  149. /* Cleanup any existing SV_LEAK shadow variables */
  150. klp_shadow_free_all(SV_LEAK, livepatch_fix1_dummy_leak_dtor);
  151. WARN_ON(klp_unregister_patch(&patch));
  152. }
  153. module_init(livepatch_shadow_fix1_init);
  154. module_exit(livepatch_shadow_fix1_exit);
  155. MODULE_LICENSE("GPL");
  156. MODULE_INFO(livepatch, "Y");