xref: /openbmc/linux/lib/livepatch/test_klp_shadow_vars.c (revision c24c57a4cc8a2f64de32084958920773c0906bc7)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
3 
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5 
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>
11 
12 /*
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.
17  */
18 static LIST_HEAD(ptr_list);
19 struct shadow_ptr {
20 	void *ptr;
21 	int id;
22 	struct list_head list;
23 };
24 
25 static void free_ptr_list(void)
26 {
27 	struct shadow_ptr *sp, *tmp_sp;
28 
29 	list_for_each_entry_safe(sp, tmp_sp, &ptr_list, list) {
30 		list_del(&sp->list);
31 		kfree(sp);
32 	}
33 }
34 
35 static int ptr_id(void *ptr)
36 {
37 	struct shadow_ptr *sp;
38 	static int count;
39 
40 	list_for_each_entry(sp, &ptr_list, list) {
41 		if (sp->ptr == ptr)
42 			return sp->id;
43 	}
44 
45 	sp = kmalloc(sizeof(*sp), GFP_ATOMIC);
46 	if (!sp)
47 		return -ENOMEM;
48 	sp->ptr = ptr;
49 	sp->id = count++;
50 
51 	list_add(&sp->list, &ptr_list);
52 
53 	return sp->id;
54 }
55 
56 /*
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.
60  */
61 static void *shadow_get(void *obj, unsigned long id)
62 {
63 	int **sv;
64 
65 	sv = klp_shadow_get(obj, id);
66 	pr_info("klp_%s(obj=PTR%d, id=0x%lx) = PTR%d\n",
67 		__func__, ptr_id(obj), id, ptr_id(sv));
68 
69 	return sv;
70 }
71 
72 static void *shadow_alloc(void *obj, unsigned long id, size_t size,
73 			  gfp_t gfp_flags, klp_shadow_ctor_t ctor,
74 			  void *ctor_data)
75 {
76 	int *var = ctor_data;
77 	int **sv;
78 
79 	sv = klp_shadow_alloc(obj, id, size, gfp_flags, ctor, var);
80 	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",
81 		__func__, ptr_id(obj), id, size, &gfp_flags, ptr_id(ctor),
82 		ptr_id(var), ptr_id(sv));
83 
84 	return sv;
85 }
86 
87 static void *shadow_get_or_alloc(void *obj, unsigned long id, size_t size,
88 				 gfp_t gfp_flags, klp_shadow_ctor_t ctor,
89 				 void *ctor_data)
90 {
91 	int *var = ctor_data;
92 	int **sv;
93 
94 	sv = klp_shadow_get_or_alloc(obj, id, size, gfp_flags, ctor, var);
95 	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",
96 		__func__, ptr_id(obj), id, size, &gfp_flags, ptr_id(ctor),
97 		ptr_id(var), ptr_id(sv));
98 
99 	return sv;
100 }
101 
102 static void shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor)
103 {
104 	klp_shadow_free(obj, id, dtor);
105 	pr_info("klp_%s(obj=PTR%d, id=0x%lx, dtor=PTR%d)\n",
106 		__func__, ptr_id(obj), id, ptr_id(dtor));
107 }
108 
109 static void shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor)
110 {
111 	klp_shadow_free_all(id, dtor);
112 	pr_info("klp_%s(id=0x%lx, dtor=PTR%d)\n",
113 		__func__, id, ptr_id(dtor));
114 }
115 
116 
117 /* Shadow variable constructor - remember simple pointer data */
118 static int shadow_ctor(void *obj, void *shadow_data, void *ctor_data)
119 {
120 	int **sv = shadow_data;
121 	int *var = ctor_data;
122 
123 	*sv = var;
124 	pr_info("%s: PTR%d -> PTR%d\n",
125 		__func__, ptr_id(sv), ptr_id(var));
126 
127 	return 0;
128 }
129 
130 static void shadow_dtor(void *obj, void *shadow_data)
131 {
132 	int **sv = shadow_data;
133 
134 	pr_info("%s(obj=PTR%d, shadow_data=PTR%d)\n",
135 		__func__, ptr_id(obj), ptr_id(sv));
136 }
137 
138 static int test_klp_shadow_vars_init(void)
139 {
140 	void *obj			= THIS_MODULE;
141 	int id			= 0x1234;
142 	size_t size		= sizeof(int *);
143 	gfp_t gfp_flags		= GFP_KERNEL;
144 
145 	int var1, var2, var3, var4;
146 	int **sv1, **sv2, **sv3, **sv4;
147 
148 	int **sv;
149 
150 	ptr_id(NULL);
151 	ptr_id(&var1);
152 	ptr_id(&var2);
153 	ptr_id(&var3);
154 	ptr_id(&var4);
155 
156 	/*
157 	 * With an empty shadow variable hash table, expect not to find
158 	 * any matches.
159 	 */
160 	sv = shadow_get(obj, id);
161 	if (!sv)
162 		pr_info("  got expected NULL result\n");
163 
164 	/*
165 	 * Allocate a few shadow variables with different <obj> and <id>.
166 	 */
167 	sv1 = shadow_alloc(obj, id, size, gfp_flags, shadow_ctor, &var1);
168 	if (!sv1)
169 		return -ENOMEM;
170 
171 	sv2 = shadow_alloc(obj + 1, id, size, gfp_flags, shadow_ctor, &var2);
172 	if (!sv2)
173 		return -ENOMEM;
174 
175 	sv3 = shadow_alloc(obj, id + 1, size, gfp_flags, shadow_ctor, &var3);
176 	if (!sv3)
177 		return -ENOMEM;
178 
179 	/*
180 	 * Verify we can find our new shadow variables and that they point
181 	 * to expected data.
182 	 */
183 	sv = shadow_get(obj, id);
184 	if (!sv)
185 		return -EINVAL;
186 	if (sv == sv1 && *sv1 == &var1)
187 		pr_info("  got expected PTR%d -> PTR%d result\n",
188 			ptr_id(sv1), ptr_id(*sv1));
189 
190 	sv = shadow_get(obj + 1, id);
191 	if (!sv)
192 		return -EINVAL;
193 	if (sv == sv2 && *sv2 == &var2)
194 		pr_info("  got expected PTR%d -> PTR%d result\n",
195 			ptr_id(sv2), ptr_id(*sv2));
196 	sv = shadow_get(obj, id + 1);
197 	if (!sv)
198 		return -EINVAL;
199 	if (sv == sv3 && *sv3 == &var3)
200 		pr_info("  got expected PTR%d -> PTR%d result\n",
201 			ptr_id(sv3), ptr_id(*sv3));
202 
203 	/*
204 	 * Allocate or get a few more, this time with the same <obj>, <id>.
205 	 * The second invocation should return the same shadow var.
206 	 */
207 	sv4 = shadow_get_or_alloc(obj + 2, id, size, gfp_flags, shadow_ctor, &var4);
208 	if (!sv4)
209 		return -ENOMEM;
210 
211 	sv = shadow_get_or_alloc(obj + 2, id, size, gfp_flags, shadow_ctor, &var4);
212 	if (!sv)
213 		return -EINVAL;
214 	if (sv == sv4 && *sv4 == &var4)
215 		pr_info("  got expected PTR%d -> PTR%d result\n",
216 			ptr_id(sv4), ptr_id(*sv4));
217 
218 	/*
219 	 * Free the <obj=*, id> shadow variables and check that we can no
220 	 * longer find them.
221 	 */
222 	shadow_free(obj, id, shadow_dtor);			/* sv1 */
223 	sv = shadow_get(obj, id);
224 	if (!sv)
225 		pr_info("  got expected NULL result\n");
226 
227 	shadow_free(obj + 1, id, shadow_dtor);			/* sv2 */
228 	sv = shadow_get(obj + 1, id);
229 	if (!sv)
230 		pr_info("  got expected NULL result\n");
231 
232 	shadow_free(obj + 2, id, shadow_dtor);			/* sv4 */
233 	sv = shadow_get(obj + 2, id);
234 	if (!sv)
235 		pr_info("  got expected NULL result\n");
236 
237 	/*
238 	 * We should still find an <id+1> variable.
239 	 */
240 	sv = shadow_get(obj, id + 1);
241 	if (!sv)
242 		return -EINVAL;
243 	if (sv == sv3 && *sv3 == &var3)
244 		pr_info("  got expected PTR%d -> PTR%d result\n",
245 			ptr_id(sv3), ptr_id(*sv3));
246 
247 	/*
248 	 * Free all the <id+1> variables, too.
249 	 */
250 	shadow_free_all(id + 1, shadow_dtor);			/* sv3 */
251 	sv = shadow_get(obj, id);
252 	if (!sv)
253 		pr_info("  shadow_get() got expected NULL result\n");
254 
255 
256 	free_ptr_list();
257 
258 	return 0;
259 }
260 
261 static void test_klp_shadow_vars_exit(void)
262 {
263 }
264 
265 module_init(test_klp_shadow_vars_init);
266 module_exit(test_klp_shadow_vars_exit);
267 MODULE_LICENSE("GPL");
268 MODULE_AUTHOR("Joe Lawrence <joe.lawrence@redhat.com>");
269 MODULE_DESCRIPTION("Livepatch test: shadow variables");
270