From be6da98425b69388ed31b18bd2497f826116f29b Mon Sep 17 00:00:00 2001 From: Petr Mladek Date: Thu, 16 Jan 2020 16:31:44 +0100 Subject: livepatch/samples/selftest: Use klp_shadow_alloc() API correctly The commit e91c2518a5d22a ("livepatch: Initialize shadow variables safely by a custom callback") leads to the following static checker warning: samples/livepatch/livepatch-shadow-fix1.c:86 livepatch_fix1_dummy_alloc() error: 'klp_shadow_alloc()' 'leak' too small (4 vs 8) It is because klp_shadow_alloc() is used a wrong way: int *leak; shadow_leak = klp_shadow_alloc(d, SV_LEAK, sizeof(leak), GFP_KERNEL, shadow_leak_ctor, leak); The code is supposed to store the "leak" pointer into the shadow variable. 3rd parameter correctly passes size of the data (size of pointer). But the 5th parameter is wrong. It should pass pointer to the data (pointer to the pointer) but it passes the pointer directly. It works because shadow_leak_ctor() handle "ctor_data" as the data instead of pointer to the data. But it is semantically wrong and confusing. The same problem is also in the module used by selftests. In this case, "pvX" variables are introduced. They represent the data stored in the shadow variables. Reported-by: Dan Carpenter Signed-off-by: Petr Mladek Reviewed-by: Joe Lawrence Acked-by: Miroslav Benes Reviewed-by: Kamalesh Babulal Signed-off-by: Jiri Kosina --- samples/livepatch/livepatch-shadow-fix1.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'samples') diff --git a/samples/livepatch/livepatch-shadow-fix1.c b/samples/livepatch/livepatch-shadow-fix1.c index bab12bdb753f..de0363b288a7 100644 --- a/samples/livepatch/livepatch-shadow-fix1.c +++ b/samples/livepatch/livepatch-shadow-fix1.c @@ -53,9 +53,12 @@ struct dummy { static int shadow_leak_ctor(void *obj, void *shadow_data, void *ctor_data) { int **shadow_leak = shadow_data; - int *leak = ctor_data; + int **leak = ctor_data; - *shadow_leak = leak; + if (!ctor_data) + return -EINVAL; + + *shadow_leak = *leak; return 0; } @@ -83,7 +86,7 @@ static struct dummy *livepatch_fix1_dummy_alloc(void) } klp_shadow_alloc(d, SV_LEAK, sizeof(leak), GFP_KERNEL, - shadow_leak_ctor, leak); + shadow_leak_ctor, &leak); pr_info("%s: dummy @ %p, expires @ %lx\n", __func__, d, d->jiffies_expire); -- cgit v1.2.3-59-g8ed1b