blob: 29fe5cd420472ad2e9e81188132d0ce47b6a55b8 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Joe Lawrence439e7272017-08-31 16:37:41 -04002/*
3 * Copyright (C) 2017 Joe Lawrence <joe.lawrence@redhat.com>
Joe Lawrence439e7272017-08-31 16:37:41 -04004 */
5
6/*
7 * livepatch-shadow-fix2.c - Shadow variables, livepatch demo
8 *
9 * Purpose
10 * -------
11 *
12 * Adds functionality to livepatch-shadow-mod's in-flight data
13 * structures through a shadow variable. The livepatch patches a
14 * routine that periodically inspects data structures, incrementing a
15 * per-data-structure counter, creating the counter if needed.
16 *
17 *
18 * Usage
19 * -----
20 *
21 * This module is not intended to be standalone. See the "Usage"
22 * section of livepatch-shadow-mod.c.
23 */
24
25#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/livepatch.h>
30#include <linux/slab.h>
31
32/* Shadow variable enums */
33#define SV_LEAK 1
34#define SV_COUNTER 2
35
36struct dummy {
37 struct list_head list;
38 unsigned long jiffies_expire;
39};
40
Nicholas Mc Guireb73d5dc2019-01-24 02:48:16 +010041static bool livepatch_fix2_dummy_check(struct dummy *d, unsigned long jiffies)
Joe Lawrence439e7272017-08-31 16:37:41 -040042{
43 int *shadow_count;
Joe Lawrence439e7272017-08-31 16:37:41 -040044
45 /*
46 * Patch: handle in-flight dummy structures, if they do not
47 * already have a SV_COUNTER shadow variable, then attach a
48 * new one.
49 */
Joe Lawrence439e7272017-08-31 16:37:41 -040050 shadow_count = klp_shadow_get_or_alloc(d, SV_COUNTER,
Petr Mladeke91c25182018-04-16 13:36:46 +020051 sizeof(*shadow_count), GFP_NOWAIT,
52 NULL, NULL);
Joe Lawrence439e7272017-08-31 16:37:41 -040053 if (shadow_count)
54 *shadow_count += 1;
55
56 return time_after(jiffies, d->jiffies_expire);
57}
58
Petr Mladek3b2c77d2018-04-16 13:36:47 +020059static void livepatch_fix2_dummy_leak_dtor(void *obj, void *shadow_data)
60{
61 void *d = obj;
Petr Mladek8f6b8862020-01-16 16:31:42 +010062 int **shadow_leak = shadow_data;
Petr Mladek3b2c77d2018-04-16 13:36:47 +020063
64 kfree(*shadow_leak);
65 pr_info("%s: dummy @ %p, prevented leak @ %p\n",
66 __func__, d, *shadow_leak);
67}
68
Nicholas Mc Guireb73d5dc2019-01-24 02:48:16 +010069static void livepatch_fix2_dummy_free(struct dummy *d)
Joe Lawrence439e7272017-08-31 16:37:41 -040070{
Petr Mladek8f6b8862020-01-16 16:31:42 +010071 int **shadow_leak;
Joe Lawrence439e7272017-08-31 16:37:41 -040072 int *shadow_count;
73
74 /* Patch: copy the memory leak patch from the fix1 module. */
75 shadow_leak = klp_shadow_get(d, SV_LEAK);
Petr Mladek3b2c77d2018-04-16 13:36:47 +020076 if (shadow_leak)
77 klp_shadow_free(d, SV_LEAK, livepatch_fix2_dummy_leak_dtor);
78 else
Joe Lawrence439e7272017-08-31 16:37:41 -040079 pr_info("%s: dummy @ %p leaked!\n", __func__, d);
Joe Lawrence439e7272017-08-31 16:37:41 -040080
81 /*
82 * Patch: fetch the SV_COUNTER shadow variable and display
83 * the final count. Detach the shadow variable.
84 */
85 shadow_count = klp_shadow_get(d, SV_COUNTER);
86 if (shadow_count) {
87 pr_info("%s: dummy @ %p, check counter = %d\n",
88 __func__, d, *shadow_count);
Petr Mladek3b2c77d2018-04-16 13:36:47 +020089 klp_shadow_free(d, SV_COUNTER, NULL);
Joe Lawrence439e7272017-08-31 16:37:41 -040090 }
91
92 kfree(d);
93}
94
95static struct klp_func funcs[] = {
96 {
97 .old_name = "dummy_check",
98 .new_func = livepatch_fix2_dummy_check,
99 },
100 {
101 .old_name = "dummy_free",
102 .new_func = livepatch_fix2_dummy_free,
103 }, { }
104};
105
106static struct klp_object objs[] = {
107 {
108 .name = "livepatch_shadow_mod",
109 .funcs = funcs,
110 }, { }
111};
112
113static struct klp_patch patch = {
114 .mod = THIS_MODULE,
115 .objs = objs,
116};
117
118static int livepatch_shadow_fix2_init(void)
119{
Petr Mladek958ef1e2019-01-09 13:43:23 +0100120 return klp_enable_patch(&patch);
Joe Lawrence439e7272017-08-31 16:37:41 -0400121}
122
123static void livepatch_shadow_fix2_exit(void)
124{
125 /* Cleanup any existing SV_COUNTER shadow variables */
Petr Mladek3b2c77d2018-04-16 13:36:47 +0200126 klp_shadow_free_all(SV_COUNTER, NULL);
Joe Lawrence439e7272017-08-31 16:37:41 -0400127}
128
129module_init(livepatch_shadow_fix2_init);
130module_exit(livepatch_shadow_fix2_exit);
131MODULE_LICENSE("GPL");
132MODULE_INFO(livepatch, "Y");