Skip to content

Commit 49ee4dd

Browse files
committed
livepatch: Proper error handling in the shadow variables selftest
Add proper error handling when allocating or getting shadow variables in the selftest. It prevents an invalid pointer access in some situations. It shows the good programming practice in the others. The error codes are just the best guess and specific for this particular test. In general, klp_shadow_alloc() returns NULL also when the given shadow variable has already been allocated. In addition, both klp_shadow_alloc() and klp_shadow_get_or_alloc() might fail from other reasons when the constructor fails. Note, that the error code is not really important even in the real life. The use of shadow variables should be transparent for the original livepatched code. Acked-by: Miroslav Benes <mbenes@suse.cz> Acked-by: Joe Lawrence <joe.lawrence@redhat.com> Signed-off-by: Petr Mladek <pmladek@suse.com>
1 parent 86e43f2 commit 49ee4dd

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

‎lib/livepatch/test_klp_shadow_vars.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,37 @@ static int test_klp_shadow_vars_init(void)
154154
* Allocate a few shadow variables with different <obj> and <id>.
155155
*/
156156
sv1 = shadow_alloc(obj, id, size, gfp_flags, shadow_ctor, &var1);
157+
if (!sv1)
158+
return -ENOMEM;
159+
157160
sv2 = shadow_alloc(obj + 1, id, size, gfp_flags, shadow_ctor, &var2);
161+
if (!sv2)
162+
return -ENOMEM;
163+
158164
sv3 = shadow_alloc(obj, id + 1, size, gfp_flags, shadow_ctor, &var3);
165+
if (!sv3)
166+
return -ENOMEM;
159167

160168
/*
161169
* Verify we can find our new shadow variables and that they point
162170
* to expected data.
163171
*/
164172
ret = shadow_get(obj, id);
173+
if (!ret)
174+
return -EINVAL;
165175
if (ret == sv1 && *sv1 == &var1)
166176
pr_info(" got expected PTR%d -> PTR%d result\n",
167177
ptr_id(sv1), ptr_id(*sv1));
178+
168179
ret = shadow_get(obj + 1, id);
180+
if (!ret)
181+
return -EINVAL;
169182
if (ret == sv2 && *sv2 == &var2)
170183
pr_info(" got expected PTR%d -> PTR%d result\n",
171184
ptr_id(sv2), ptr_id(*sv2));
172185
ret = shadow_get(obj, id + 1);
186+
if (!ret)
187+
return -EINVAL;
173188
if (ret == sv3 && *sv3 == &var3)
174189
pr_info(" got expected PTR%d -> PTR%d result\n",
175190
ptr_id(sv3), ptr_id(*sv3));
@@ -179,7 +194,12 @@ static int test_klp_shadow_vars_init(void)
179194
* The second invocation should return the same shadow var.
180195
*/
181196
sv4 = shadow_get_or_alloc(obj + 2, id, size, gfp_flags, shadow_ctor, &var4);
197+
if (!sv4)
198+
return -ENOMEM;
199+
182200
ret = shadow_get_or_alloc(obj + 2, id, size, gfp_flags, shadow_ctor, &var4);
201+
if (!ret)
202+
return -EINVAL;
183203
if (ret == sv4 && *sv4 == &var4)
184204
pr_info(" got expected PTR%d -> PTR%d result\n",
185205
ptr_id(sv4), ptr_id(*sv4));
@@ -207,6 +227,8 @@ static int test_klp_shadow_vars_init(void)
207227
* We should still find an <id+1> variable.
208228
*/
209229
ret = shadow_get(obj, id + 1);
230+
if (!ret)
231+
return -EINVAL;
210232
if (ret == sv3 && *sv3 == &var3)
211233
pr_info(" got expected PTR%d -> PTR%d result\n",
212234
ptr_id(sv3), ptr_id(*sv3));

0 commit comments

Comments
 (0)