livepatch-shadow-fix2.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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-fix2.c - Shadow variables, livepatch demo
  7. *
  8. * Purpose
  9. * -------
  10. *
  11. * Adds functionality to livepatch-shadow-mod's in-flight data
  12. * structures through a shadow variable. The livepatch patches a
  13. * routine that periodically inspects data structures, incrementing a
  14. * per-data-structure counter, creating the counter if needed.
  15. *
  16. *
  17. * Usage
  18. * -----
  19. *
  20. * This module is not intended to be standalone. See the "Usage"
  21. * section of livepatch-shadow-mod.c.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/livepatch.h>
  27. #include <linux/slab.h>
  28. /* Shadow variable enums */
  29. #define SV_LEAK 1
  30. #define SV_COUNTER 2
  31. struct dummy {
  32. struct list_head list;
  33. unsigned long jiffies_expire;
  34. };
  35. static bool livepatch_fix2_dummy_check(struct dummy *d, unsigned long jiffies)
  36. {
  37. int *shadow_count;
  38. /*
  39. * Patch: handle in-flight dummy structures, if they do not
  40. * already have a SV_COUNTER shadow variable, then attach a
  41. * new one.
  42. */
  43. shadow_count = klp_shadow_get_or_alloc(d, SV_COUNTER,
  44. sizeof(*shadow_count), GFP_NOWAIT,
  45. NULL, NULL);
  46. if (shadow_count)
  47. *shadow_count += 1;
  48. return time_after(jiffies, d->jiffies_expire);
  49. }
  50. static void livepatch_fix2_dummy_leak_dtor(void *obj, void *shadow_data)
  51. {
  52. void *d = obj;
  53. int **shadow_leak = shadow_data;
  54. kfree(*shadow_leak);
  55. pr_info("%s: dummy @ %p, prevented leak @ %p\n",
  56. __func__, d, *shadow_leak);
  57. }
  58. static void livepatch_fix2_dummy_free(struct dummy *d)
  59. {
  60. int **shadow_leak;
  61. int *shadow_count;
  62. /* Patch: copy the memory leak patch from the fix1 module. */
  63. shadow_leak = klp_shadow_get(d, SV_LEAK);
  64. if (shadow_leak)
  65. klp_shadow_free(d, SV_LEAK, livepatch_fix2_dummy_leak_dtor);
  66. else
  67. pr_info("%s: dummy @ %p leaked!\n", __func__, d);
  68. /*
  69. * Patch: fetch the SV_COUNTER shadow variable and display
  70. * the final count. Detach the shadow variable.
  71. */
  72. shadow_count = klp_shadow_get(d, SV_COUNTER);
  73. if (shadow_count) {
  74. pr_info("%s: dummy @ %p, check counter = %d\n",
  75. __func__, d, *shadow_count);
  76. klp_shadow_free(d, SV_COUNTER, NULL);
  77. }
  78. kfree(d);
  79. }
  80. static struct klp_func funcs[] = {
  81. {
  82. .old_name = "dummy_check",
  83. .new_func = livepatch_fix2_dummy_check,
  84. },
  85. {
  86. .old_name = "dummy_free",
  87. .new_func = livepatch_fix2_dummy_free,
  88. }, { }
  89. };
  90. static struct klp_object objs[] = {
  91. {
  92. .name = "livepatch_shadow_mod",
  93. .funcs = funcs,
  94. }, { }
  95. };
  96. static struct klp_patch patch = {
  97. .mod = THIS_MODULE,
  98. .objs = objs,
  99. };
  100. static int livepatch_shadow_fix2_init(void)
  101. {
  102. return klp_enable_patch(&patch);
  103. }
  104. static void livepatch_shadow_fix2_exit(void)
  105. {
  106. /* Cleanup any existing SV_COUNTER shadow variables */
  107. klp_shadow_free_all(SV_COUNTER, NULL);
  108. }
  109. module_init(livepatch_shadow_fix2_init);
  110. module_exit(livepatch_shadow_fix2_exit);
  111. MODULE_LICENSE("GPL");
  112. MODULE_INFO(livepatch, "Y");