livepatch-shadow-mod.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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-mod.c - Shadow variables, buggy module demo
  19. *
  20. * Purpose
  21. * -------
  22. *
  23. * As a demonstration of livepatch shadow variable API, this module
  24. * introduces memory leak behavior that livepatch modules
  25. * livepatch-shadow-fix1.ko and livepatch-shadow-fix2.ko correct and
  26. * enhance.
  27. *
  28. * WARNING - even though the livepatch-shadow-fix modules patch the
  29. * memory leak, please load these modules at your own risk -- some
  30. * amount of memory may leaked before the bug is patched.
  31. *
  32. *
  33. * Usage
  34. * -----
  35. *
  36. * Step 1 - Load the buggy demonstration module:
  37. *
  38. * insmod samples/livepatch/livepatch-shadow-mod.ko
  39. *
  40. * Watch dmesg output for a few moments to see new dummy being allocated
  41. * and a periodic cleanup check. (Note: a small amount of memory is
  42. * being leaked.)
  43. *
  44. *
  45. * Step 2 - Load livepatch fix1:
  46. *
  47. * insmod samples/livepatch/livepatch-shadow-fix1.ko
  48. *
  49. * Continue watching dmesg and note that now livepatch_fix1_dummy_free()
  50. * and livepatch_fix1_dummy_alloc() are logging messages about leaked
  51. * memory and eventually leaks prevented.
  52. *
  53. *
  54. * Step 3 - Load livepatch fix2 (on top of fix1):
  55. *
  56. * insmod samples/livepatch/livepatch-shadow-fix2.ko
  57. *
  58. * This module extends functionality through shadow variables, as a new
  59. * "check" counter is added to the dummy structure. Periodic dmesg
  60. * messages will log these as dummies are cleaned up.
  61. *
  62. *
  63. * Step 4 - Cleanup
  64. *
  65. * Unwind the demonstration by disabling the livepatch fix modules, then
  66. * removing them and the demo module:
  67. *
  68. * echo 0 > /sys/kernel/livepatch/livepatch_shadow_fix2/enabled
  69. * echo 0 > /sys/kernel/livepatch/livepatch_shadow_fix1/enabled
  70. * rmmod livepatch-shadow-fix2
  71. * rmmod livepatch-shadow-fix1
  72. * rmmod livepatch-shadow-mod
  73. */
  74. #include <linux/kernel.h>
  75. #include <linux/module.h>
  76. #include <linux/sched.h>
  77. #include <linux/slab.h>
  78. #include <linux/stat.h>
  79. #include <linux/workqueue.h>
  80. MODULE_LICENSE("GPL");
  81. MODULE_AUTHOR("Joe Lawrence <joe.lawrence@redhat.com>");
  82. MODULE_DESCRIPTION("Buggy module for shadow variable demo");
  83. /* Allocate new dummies every second */
  84. #define ALLOC_PERIOD 1
  85. /* Check for expired dummies after a few new ones have been allocated */
  86. #define CLEANUP_PERIOD (3 * ALLOC_PERIOD)
  87. /* Dummies expire after a few cleanup instances */
  88. #define EXPIRE_PERIOD (4 * CLEANUP_PERIOD)
  89. /*
  90. * Keep a list of all the dummies so we can clean up any residual ones
  91. * on module exit
  92. */
  93. LIST_HEAD(dummy_list);
  94. DEFINE_MUTEX(dummy_list_mutex);
  95. struct dummy {
  96. struct list_head list;
  97. unsigned long jiffies_expire;
  98. };
  99. noinline struct dummy *dummy_alloc(void)
  100. {
  101. struct dummy *d;
  102. void *leak;
  103. d = kzalloc(sizeof(*d), GFP_KERNEL);
  104. if (!d)
  105. return NULL;
  106. d->jiffies_expire = jiffies +
  107. msecs_to_jiffies(1000 * EXPIRE_PERIOD);
  108. /* Oops, forgot to save leak! */
  109. leak = kzalloc(sizeof(int), GFP_KERNEL);
  110. if (!leak) {
  111. kfree(d);
  112. return NULL;
  113. }
  114. pr_info("%s: dummy @ %p, expires @ %lx\n",
  115. __func__, d, d->jiffies_expire);
  116. return d;
  117. }
  118. noinline void dummy_free(struct dummy *d)
  119. {
  120. pr_info("%s: dummy @ %p, expired = %lx\n",
  121. __func__, d, d->jiffies_expire);
  122. kfree(d);
  123. }
  124. noinline bool dummy_check(struct dummy *d, unsigned long jiffies)
  125. {
  126. return time_after(jiffies, d->jiffies_expire);
  127. }
  128. /*
  129. * alloc_work_func: allocates new dummy structures, allocates additional
  130. * memory, aptly named "leak", but doesn't keep
  131. * permanent record of it.
  132. */
  133. static void alloc_work_func(struct work_struct *work);
  134. static DECLARE_DELAYED_WORK(alloc_dwork, alloc_work_func);
  135. static void alloc_work_func(struct work_struct *work)
  136. {
  137. struct dummy *d;
  138. d = dummy_alloc();
  139. if (!d)
  140. return;
  141. mutex_lock(&dummy_list_mutex);
  142. list_add(&d->list, &dummy_list);
  143. mutex_unlock(&dummy_list_mutex);
  144. schedule_delayed_work(&alloc_dwork,
  145. msecs_to_jiffies(1000 * ALLOC_PERIOD));
  146. }
  147. /*
  148. * cleanup_work_func: frees dummy structures. Without knownledge of
  149. * "leak", it leaks the additional memory that
  150. * alloc_work_func created.
  151. */
  152. static void cleanup_work_func(struct work_struct *work);
  153. static DECLARE_DELAYED_WORK(cleanup_dwork, cleanup_work_func);
  154. static void cleanup_work_func(struct work_struct *work)
  155. {
  156. struct dummy *d, *tmp;
  157. unsigned long j;
  158. j = jiffies;
  159. pr_info("%s: jiffies = %lx\n", __func__, j);
  160. mutex_lock(&dummy_list_mutex);
  161. list_for_each_entry_safe(d, tmp, &dummy_list, list) {
  162. /* Kick out and free any expired dummies */
  163. if (dummy_check(d, j)) {
  164. list_del(&d->list);
  165. dummy_free(d);
  166. }
  167. }
  168. mutex_unlock(&dummy_list_mutex);
  169. schedule_delayed_work(&cleanup_dwork,
  170. msecs_to_jiffies(1000 * CLEANUP_PERIOD));
  171. }
  172. static int livepatch_shadow_mod_init(void)
  173. {
  174. schedule_delayed_work(&alloc_dwork,
  175. msecs_to_jiffies(1000 * ALLOC_PERIOD));
  176. schedule_delayed_work(&cleanup_dwork,
  177. msecs_to_jiffies(1000 * CLEANUP_PERIOD));
  178. return 0;
  179. }
  180. static void livepatch_shadow_mod_exit(void)
  181. {
  182. struct dummy *d, *tmp;
  183. /* Wait for any dummies at work */
  184. cancel_delayed_work_sync(&alloc_dwork);
  185. cancel_delayed_work_sync(&cleanup_dwork);
  186. /* Cleanup residual dummies */
  187. list_for_each_entry_safe(d, tmp, &dummy_list, list) {
  188. list_del(&d->list);
  189. dummy_free(d);
  190. }
  191. }
  192. module_init(livepatch_shadow_mod_init);
  193. module_exit(livepatch_shadow_mod_exit);