aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorPetr Mladek <pmladek@suse.com>2019-02-04 08:56:52 -0500
committerPetr Mladek <pmladek@suse.com>2019-02-06 05:01:57 -0500
commit49ee4dd2e753cd13d157361d4bd28b548e3d0ee7 (patch)
tree282f204261746a62ef583584cc0e5e66a2283a4a /lib
parent86e43f23c17126e32820a1b37d747d06f3056570 (diff)
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>
Diffstat (limited to 'lib')
-rw-r--r--lib/livepatch/test_klp_shadow_vars.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/livepatch/test_klp_shadow_vars.c b/lib/livepatch/test_klp_shadow_vars.c
index f5441c193166..fe5c413efe96 100644
--- a/lib/livepatch/test_klp_shadow_vars.c
+++ b/lib/livepatch/test_klp_shadow_vars.c
@@ -154,22 +154,37 @@ static int test_klp_shadow_vars_init(void)
154 * Allocate a few shadow variables with different <obj> and <id>. 154 * Allocate a few shadow variables with different <obj> and <id>.
155 */ 155 */
156 sv1 = shadow_alloc(obj, id, size, gfp_flags, shadow_ctor, &var1); 156 sv1 = shadow_alloc(obj, id, size, gfp_flags, shadow_ctor, &var1);
157 if (!sv1)
158 return -ENOMEM;
159
157 sv2 = shadow_alloc(obj + 1, id, size, gfp_flags, shadow_ctor, &var2); 160 sv2 = shadow_alloc(obj + 1, id, size, gfp_flags, shadow_ctor, &var2);
161 if (!sv2)
162 return -ENOMEM;
163
158 sv3 = shadow_alloc(obj, id + 1, size, gfp_flags, shadow_ctor, &var3); 164 sv3 = shadow_alloc(obj, id + 1, size, gfp_flags, shadow_ctor, &var3);
165 if (!sv3)
166 return -ENOMEM;
159 167
160 /* 168 /*
161 * Verify we can find our new shadow variables and that they point 169 * Verify we can find our new shadow variables and that they point
162 * to expected data. 170 * to expected data.
163 */ 171 */
164 ret = shadow_get(obj, id); 172 ret = shadow_get(obj, id);
173 if (!ret)
174 return -EINVAL;
165 if (ret == sv1 && *sv1 == &var1) 175 if (ret == sv1 && *sv1 == &var1)
166 pr_info(" got expected PTR%d -> PTR%d result\n", 176 pr_info(" got expected PTR%d -> PTR%d result\n",
167 ptr_id(sv1), ptr_id(*sv1)); 177 ptr_id(sv1), ptr_id(*sv1));
178
168 ret = shadow_get(obj + 1, id); 179 ret = shadow_get(obj + 1, id);
180 if (!ret)
181 return -EINVAL;
169 if (ret == sv2 && *sv2 == &var2) 182 if (ret == sv2 && *sv2 == &var2)
170 pr_info(" got expected PTR%d -> PTR%d result\n", 183 pr_info(" got expected PTR%d -> PTR%d result\n",
171 ptr_id(sv2), ptr_id(*sv2)); 184 ptr_id(sv2), ptr_id(*sv2));
172 ret = shadow_get(obj, id + 1); 185 ret = shadow_get(obj, id + 1);
186 if (!ret)
187 return -EINVAL;
173 if (ret == sv3 && *sv3 == &var3) 188 if (ret == sv3 && *sv3 == &var3)
174 pr_info(" got expected PTR%d -> PTR%d result\n", 189 pr_info(" got expected PTR%d -> PTR%d result\n",
175 ptr_id(sv3), ptr_id(*sv3)); 190 ptr_id(sv3), ptr_id(*sv3));
@@ -179,7 +194,12 @@ static int test_klp_shadow_vars_init(void)
179 * The second invocation should return the same shadow var. 194 * The second invocation should return the same shadow var.
180 */ 195 */
181 sv4 = shadow_get_or_alloc(obj + 2, id, size, gfp_flags, shadow_ctor, &var4); 196 sv4 = shadow_get_or_alloc(obj + 2, id, size, gfp_flags, shadow_ctor, &var4);
197 if (!sv4)
198 return -ENOMEM;
199
182 ret = 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);
201 if (!ret)
202 return -EINVAL;
183 if (ret == sv4 && *sv4 == &var4) 203 if (ret == sv4 && *sv4 == &var4)
184 pr_info(" got expected PTR%d -> PTR%d result\n", 204 pr_info(" got expected PTR%d -> PTR%d result\n",
185 ptr_id(sv4), ptr_id(*sv4)); 205 ptr_id(sv4), ptr_id(*sv4));
@@ -207,6 +227,8 @@ static int test_klp_shadow_vars_init(void)
207 * We should still find an <id+1> variable. 227 * We should still find an <id+1> variable.
208 */ 228 */
209 ret = shadow_get(obj, id + 1); 229 ret = shadow_get(obj, id + 1);
230 if (!ret)
231 return -EINVAL;
210 if (ret == sv3 && *sv3 == &var3) 232 if (ret == sv3 && *sv3 == &var3)
211 pr_info(" got expected PTR%d -> PTR%d result\n", 233 pr_info(" got expected PTR%d -> PTR%d result\n",
212 ptr_id(sv3), ptr_id(*sv3)); 234 ptr_id(sv3), ptr_id(*sv3));