1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/list.h>
9 #include <linux/livepatch.h>
10 #include <linux/slab.h>
13 * Keep a small list of pointers so that we can print address-agnostic
14 * pointer values. Use a rolling integer count to differentiate the values.
15 * Ironically we could have used the shadow variable API to do this, but
16 * let's not lean too heavily on the very code we're testing.
18 static LIST_HEAD(ptr_list
);
22 struct list_head list
;
25 static void free_ptr_list(void)
27 struct shadow_ptr
*sp
, *tmp_sp
;
29 list_for_each_entry_safe(sp
, tmp_sp
, &ptr_list
, list
) {
35 static int ptr_id(void *ptr
)
37 struct shadow_ptr
*sp
;
40 list_for_each_entry(sp
, &ptr_list
, list
) {
45 sp
= kmalloc(sizeof(*sp
), GFP_ATOMIC
);
51 list_add(&sp
->list
, &ptr_list
);
57 * Shadow variable wrapper functions that echo the function and arguments
58 * to the kernel log for testing verification. Don't display raw pointers,
59 * but use the ptr_id() value instead.
61 static void *shadow_get(void *obj
, unsigned long id
)
63 void *ret
= klp_shadow_get(obj
, id
);
65 pr_info("klp_%s(obj=PTR%d, id=0x%lx) = PTR%d\n",
66 __func__
, ptr_id(obj
), id
, ptr_id(ret
));
71 static void *shadow_alloc(void *obj
, unsigned long id
, size_t size
,
72 gfp_t gfp_flags
, klp_shadow_ctor_t ctor
,
75 void *ret
= klp_shadow_alloc(obj
, id
, size
, gfp_flags
, ctor
,
77 pr_info("klp_%s(obj=PTR%d, id=0x%lx, size=%zx, gfp_flags=%pGg), ctor=PTR%d, ctor_data=PTR%d = PTR%d\n",
78 __func__
, ptr_id(obj
), id
, size
, &gfp_flags
, ptr_id(ctor
),
79 ptr_id(ctor_data
), ptr_id(ret
));
83 static void *shadow_get_or_alloc(void *obj
, unsigned long id
, size_t size
,
84 gfp_t gfp_flags
, klp_shadow_ctor_t ctor
,
87 void *ret
= klp_shadow_get_or_alloc(obj
, id
, size
, gfp_flags
, ctor
,
89 pr_info("klp_%s(obj=PTR%d, id=0x%lx, size=%zx, gfp_flags=%pGg), ctor=PTR%d, ctor_data=PTR%d = PTR%d\n",
90 __func__
, ptr_id(obj
), id
, size
, &gfp_flags
, ptr_id(ctor
),
91 ptr_id(ctor_data
), ptr_id(ret
));
95 static void shadow_free(void *obj
, unsigned long id
, klp_shadow_dtor_t dtor
)
97 klp_shadow_free(obj
, id
, dtor
);
98 pr_info("klp_%s(obj=PTR%d, id=0x%lx, dtor=PTR%d)\n",
99 __func__
, ptr_id(obj
), id
, ptr_id(dtor
));
102 static void shadow_free_all(unsigned long id
, klp_shadow_dtor_t dtor
)
104 klp_shadow_free_all(id
, dtor
);
105 pr_info("klp_%s(id=0x%lx, dtor=PTR%d)\n",
106 __func__
, id
, ptr_id(dtor
));
110 /* Shadow variable constructor - remember simple pointer data */
111 static int shadow_ctor(void *obj
, void *shadow_data
, void *ctor_data
)
113 int **shadow_int
= shadow_data
;
114 *shadow_int
= ctor_data
;
115 pr_info("%s: PTR%d -> PTR%d\n",
116 __func__
, ptr_id(shadow_int
), ptr_id(ctor_data
));
121 static void shadow_dtor(void *obj
, void *shadow_data
)
123 pr_info("%s(obj=PTR%d, shadow_data=PTR%d)\n",
124 __func__
, ptr_id(obj
), ptr_id(shadow_data
));
127 static int test_klp_shadow_vars_init(void)
129 void *obj
= THIS_MODULE
;
131 size_t size
= sizeof(int *);
132 gfp_t gfp_flags
= GFP_KERNEL
;
134 int var1
, var2
, var3
, var4
;
135 int **sv1
, **sv2
, **sv3
, **sv4
;
146 * With an empty shadow variable hash table, expect not to find
149 ret
= shadow_get(obj
, id
);
151 pr_info(" got expected NULL result\n");
154 * Allocate a few shadow variables with different <obj> and <id>.
156 sv1
= shadow_alloc(obj
, id
, size
, gfp_flags
, shadow_ctor
, &var1
);
160 sv2
= shadow_alloc(obj
+ 1, id
, size
, gfp_flags
, shadow_ctor
, &var2
);
164 sv3
= shadow_alloc(obj
, id
+ 1, size
, gfp_flags
, shadow_ctor
, &var3
);
169 * Verify we can find our new shadow variables and that they point
172 ret
= shadow_get(obj
, id
);
175 if (ret
== sv1
&& *sv1
== &var1
)
176 pr_info(" got expected PTR%d -> PTR%d result\n",
177 ptr_id(sv1
), ptr_id(*sv1
));
179 ret
= shadow_get(obj
+ 1, id
);
182 if (ret
== sv2
&& *sv2
== &var2
)
183 pr_info(" got expected PTR%d -> PTR%d result\n",
184 ptr_id(sv2
), ptr_id(*sv2
));
185 ret
= shadow_get(obj
, id
+ 1);
188 if (ret
== sv3
&& *sv3
== &var3
)
189 pr_info(" got expected PTR%d -> PTR%d result\n",
190 ptr_id(sv3
), ptr_id(*sv3
));
193 * Allocate or get a few more, this time with the same <obj>, <id>.
194 * The second invocation should return the same shadow var.
196 sv4
= shadow_get_or_alloc(obj
+ 2, id
, size
, gfp_flags
, shadow_ctor
, &var4
);
200 ret
= shadow_get_or_alloc(obj
+ 2, id
, size
, gfp_flags
, shadow_ctor
, &var4
);
203 if (ret
== sv4
&& *sv4
== &var4
)
204 pr_info(" got expected PTR%d -> PTR%d result\n",
205 ptr_id(sv4
), ptr_id(*sv4
));
208 * Free the <obj=*, id> shadow variables and check that we can no
211 shadow_free(obj
, id
, shadow_dtor
); /* sv1 */
212 ret
= shadow_get(obj
, id
);
214 pr_info(" got expected NULL result\n");
216 shadow_free(obj
+ 1, id
, shadow_dtor
); /* sv2 */
217 ret
= shadow_get(obj
+ 1, id
);
219 pr_info(" got expected NULL result\n");
221 shadow_free(obj
+ 2, id
, shadow_dtor
); /* sv4 */
222 ret
= shadow_get(obj
+ 2, id
);
224 pr_info(" got expected NULL result\n");
227 * We should still find an <id+1> variable.
229 ret
= shadow_get(obj
, id
+ 1);
232 if (ret
== sv3
&& *sv3
== &var3
)
233 pr_info(" got expected PTR%d -> PTR%d result\n",
234 ptr_id(sv3
), ptr_id(*sv3
));
237 * Free all the <id+1> variables, too.
239 shadow_free_all(id
+ 1, shadow_dtor
); /* sv3 */
240 ret
= shadow_get(obj
, id
);
242 pr_info(" shadow_get() got expected NULL result\n");
250 static void test_klp_shadow_vars_exit(void)
254 module_init(test_klp_shadow_vars_init
);
255 module_exit(test_klp_shadow_vars_exit
);
256 MODULE_LICENSE("GPL");
257 MODULE_AUTHOR("Joe Lawrence <joe.lawrence@redhat.com>");
258 MODULE_DESCRIPTION("Livepatch test: shadow variables");