Skip to content

Commit 5f30b2e

Browse files
Nicholas Mc GuireJiri Kosina
authored andcommitted
livepatch: check kzalloc return values
kzalloc() return should always be checked - notably in example code where this may be seen as reference. On failure of allocation in livepatch_fix1_dummy_alloc() respectively dummy_alloc() previous allocation is freed (thanks to Petr Mladek <pmladek@suse.com> for catching this) and NULL returned. Signed-off-by: Nicholas Mc Guire <hofrat@osadl.org> Fixes: 439e727 ("livepatch: introduce shadow variable API") Acked-by: Joe Lawrence <joe.lawrence@redhat.com> Reviewed-by: Petr Mladek <pmladek@suse.com> Acked-by: Miroslav Benes <mbenes@suse.cz> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 3933ec7 commit 5f30b2e

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

samples/livepatch/livepatch-shadow-fix1.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ struct dummy *livepatch_fix1_dummy_alloc(void)
8989
* pointer to handle resource release.
9090
*/
9191
leak = kzalloc(sizeof(int), GFP_KERNEL);
92+
if (!leak) {
93+
kfree(d);
94+
return NULL;
95+
}
96+
9297
klp_shadow_alloc(d, SV_LEAK, sizeof(leak), GFP_KERNEL,
9398
shadow_leak_ctor, leak);
9499

samples/livepatch/livepatch-shadow-mod.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ noinline struct dummy *dummy_alloc(void)
118118

119119
/* Oops, forgot to save leak! */
120120
leak = kzalloc(sizeof(int), GFP_KERNEL);
121+
if (!leak) {
122+
kfree(d);
123+
return NULL;
124+
}
121125

122126
pr_info("%s: dummy @ %p, expires @ %lx\n",
123127
__func__, d, d->jiffies_expire);

0 commit comments

Comments
 (0)