Commit 270f7806 authored by Yannick Cote's avatar Yannick Cote Committed by Petr Mladek

selftests/livepatch: fix mem leaks in test-klp-shadow-vars

In some cases, when an error occurs during testing and the main test
routine returns, a memory leak occurs via leaving previously registered
shadow variables allocated in the kernel as well as shadow_ptr list
elements. From now on, in case of error, remove all allocated shadow
variables and shadow_ptr struct elements.
Signed-off-by: default avatarYannick Cote <ycote@redhat.com>
Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
Reviewed-by: default avatarKamalesh Babulal <kamalesh@linux.vnet.ibm.com>
Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
Acked-by: default avatarJoe Lawrence <joe.lawrence@redhat.com>
Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20200603182058.109470-5-ycote@redhat.com
parent 76efe6da
...@@ -170,6 +170,7 @@ static int test_klp_shadow_vars_init(void) ...@@ -170,6 +170,7 @@ static int test_klp_shadow_vars_init(void)
char *pndup[NUM_OBJS]; char *pndup[NUM_OBJS];
int nfields2[NUM_OBJS], *pnfields2[NUM_OBJS], **sv2[NUM_OBJS]; int nfields2[NUM_OBJS], *pnfields2[NUM_OBJS], **sv2[NUM_OBJS];
void **sv; void **sv;
int ret;
int i; int i;
ptr_id(NULL); ptr_id(NULL);
...@@ -196,31 +197,39 @@ static int test_klp_shadow_vars_init(void) ...@@ -196,31 +197,39 @@ static int test_klp_shadow_vars_init(void)
sizeof(pnfields1[i]), GFP_KERNEL, sizeof(pnfields1[i]), GFP_KERNEL,
shadow_ctor, &pnfields1[i]); shadow_ctor, &pnfields1[i]);
} }
if (!sv1[i]) if (!sv1[i]) {
return -ENOMEM; ret = -ENOMEM;
goto out;
}
pnfields2[i] = &nfields2[i]; pnfields2[i] = &nfields2[i];
ptr_id(pnfields2[i]); ptr_id(pnfields2[i]);
sv2[i] = shadow_alloc(&objs[i], SV_ID2, sizeof(pnfields2[i]), sv2[i] = shadow_alloc(&objs[i], SV_ID2, sizeof(pnfields2[i]),
GFP_KERNEL, shadow_ctor, &pnfields2[i]); GFP_KERNEL, shadow_ctor, &pnfields2[i]);
if (!sv2[i]) if (!sv2[i]) {
return -ENOMEM; ret = -ENOMEM;
goto out;
}
} }
/* pass 2: verify we find allocated svars and where they point to */ /* pass 2: verify we find allocated svars and where they point to */
for (i = 0; i < NUM_OBJS; i++) { for (i = 0; i < NUM_OBJS; i++) {
/* check the "char" svar for all objects */ /* check the "char" svar for all objects */
sv = shadow_get(&objs[i], SV_ID1); sv = shadow_get(&objs[i], SV_ID1);
if (!sv) if (!sv) {
return -EINVAL; ret = -EINVAL;
goto out;
}
if ((char **)sv == sv1[i] && *sv1[i] == pnfields1[i]) if ((char **)sv == sv1[i] && *sv1[i] == pnfields1[i])
pr_info(" got expected PTR%d -> PTR%d result\n", pr_info(" got expected PTR%d -> PTR%d result\n",
ptr_id(sv1[i]), ptr_id(*sv1[i])); ptr_id(sv1[i]), ptr_id(*sv1[i]));
/* check the "int" svar for all objects */ /* check the "int" svar for all objects */
sv = shadow_get(&objs[i], SV_ID2); sv = shadow_get(&objs[i], SV_ID2);
if (!sv) if (!sv) {
return -EINVAL; ret = -EINVAL;
goto out;
}
if ((int **)sv == sv2[i] && *sv2[i] == pnfields2[i]) if ((int **)sv == sv2[i] && *sv2[i] == pnfields2[i])
pr_info(" got expected PTR%d -> PTR%d result\n", pr_info(" got expected PTR%d -> PTR%d result\n",
ptr_id(sv2[i]), ptr_id(*sv2[i])); ptr_id(sv2[i]), ptr_id(*sv2[i]));
...@@ -233,8 +242,10 @@ static int test_klp_shadow_vars_init(void) ...@@ -233,8 +242,10 @@ static int test_klp_shadow_vars_init(void)
sv = shadow_get_or_alloc(&objs[i], SV_ID1, sizeof(pndup[i]), sv = shadow_get_or_alloc(&objs[i], SV_ID1, sizeof(pndup[i]),
GFP_KERNEL, shadow_ctor, &pndup[i]); GFP_KERNEL, shadow_ctor, &pndup[i]);
if (!sv) if (!sv) {
return -EINVAL; ret = -EINVAL;
goto out;
}
if ((char **)sv == sv1[i] && *sv1[i] == pnfields1[i]) if ((char **)sv == sv1[i] && *sv1[i] == pnfields1[i])
pr_info(" got expected PTR%d -> PTR%d result\n", pr_info(" got expected PTR%d -> PTR%d result\n",
ptr_id(sv1[i]), ptr_id(*sv1[i])); ptr_id(sv1[i]), ptr_id(*sv1[i]));
...@@ -251,8 +262,10 @@ static int test_klp_shadow_vars_init(void) ...@@ -251,8 +262,10 @@ static int test_klp_shadow_vars_init(void)
/* pass 5: check we still find <objs[*], SV_ID2> svar pairs */ /* pass 5: check we still find <objs[*], SV_ID2> svar pairs */
for (i = 0; i < NUM_OBJS; i++) { for (i = 0; i < NUM_OBJS; i++) {
sv = shadow_get(&objs[i], SV_ID2); /* 'int' pairs */ sv = shadow_get(&objs[i], SV_ID2); /* 'int' pairs */
if (!sv) if (!sv) {
return -EINVAL; ret = -EINVAL;
goto out;
}
if ((int **)sv == sv2[i] && *sv2[i] == pnfields2[i]) if ((int **)sv == sv2[i] && *sv2[i] == pnfields2[i])
pr_info(" got expected PTR%d -> PTR%d result\n", pr_info(" got expected PTR%d -> PTR%d result\n",
ptr_id(sv2[i]), ptr_id(*sv2[i])); ptr_id(sv2[i]), ptr_id(*sv2[i]));
...@@ -269,6 +282,12 @@ static int test_klp_shadow_vars_init(void) ...@@ -269,6 +282,12 @@ static int test_klp_shadow_vars_init(void)
free_ptr_list(); free_ptr_list();
return 0; return 0;
out:
shadow_free_all(SV_ID1, NULL); /* 'char' pairs */
shadow_free_all(SV_ID2, NULL); /* 'int' pairs */
free_ptr_list();
return ret;
} }
static void test_klp_shadow_vars_exit(void) static void test_klp_shadow_vars_exit(void)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment