livepatch-shadow-mod.c 5.3 KB

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