Skip to content

Commit

Permalink
livepatch: Handle allocation failure in the sample of shadow variable…
Browse files Browse the repository at this point in the history
… API

klp_shadow_alloc() is not handled in the sample of shadow variable API.
It is not strictly necessary because livepatch_fix1_dummy_free() is
able to handle the potential failure. But it is an example and it should
use the API a clean way.

Signed-off-by: Petr Mladek <[email protected]>
Reviewed-by: Joe Lawrence <[email protected]>
Acked-by: Miroslav Benes <[email protected]>
Reviewed-by: Kamalesh Babulal <[email protected]>
Signed-off-by: Jiri Kosina <[email protected]>
  • Loading branch information
pmladek authored and Jiri Kosina committed Jan 17, 2020
1 parent be6da98 commit f46e49a
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions samples/livepatch/livepatch-shadow-fix1.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static struct dummy *livepatch_fix1_dummy_alloc(void)
{
struct dummy *d;
int *leak;
int **shadow_leak;

d = kzalloc(sizeof(*d), GFP_KERNEL);
if (!d)
Expand All @@ -80,18 +81,27 @@ static struct dummy *livepatch_fix1_dummy_alloc(void)
* pointer to handle resource release.
*/
leak = kzalloc(sizeof(*leak), GFP_KERNEL);
if (!leak) {
kfree(d);
return NULL;
if (!leak)
goto err_leak;

shadow_leak = klp_shadow_alloc(d, SV_LEAK, sizeof(leak), GFP_KERNEL,
shadow_leak_ctor, &leak);
if (!shadow_leak) {
pr_err("%s: failed to allocate shadow variable for the leaking pointer: dummy @ %p, leak @ %p\n",
__func__, d, leak);
goto err_shadow;
}

klp_shadow_alloc(d, SV_LEAK, sizeof(leak), GFP_KERNEL,
shadow_leak_ctor, &leak);

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

return d;

err_shadow:
kfree(leak);
err_leak:
kfree(d);
return NULL;
}

static void livepatch_fix1_dummy_leak_dtor(void *obj, void *shadow_data)
Expand Down

0 comments on commit f46e49a

Please sign in to comment.