2019-05-19 21:51:43 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-09-01 04:37:41 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Joe Lawrence <joe.lawrence@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* livepatch-shadow-fix1.c - Shadow variables, livepatch demo
|
|
|
|
*
|
|
|
|
* Purpose
|
|
|
|
* -------
|
|
|
|
*
|
|
|
|
* Fixes the memory leak introduced in livepatch-shadow-mod through the
|
|
|
|
* use of a shadow variable. This fix demonstrates the "extending" of
|
|
|
|
* short-lived data structures by patching its allocation and release
|
|
|
|
* functions.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Usage
|
|
|
|
* -----
|
|
|
|
*
|
|
|
|
* This module is not intended to be standalone. See the "Usage"
|
|
|
|
* section of livepatch-shadow-mod.c.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/livepatch.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
|
|
|
/* Shadow variable enums */
|
|
|
|
#define SV_LEAK 1
|
|
|
|
|
|
|
|
/* Allocate new dummies every second */
|
|
|
|
#define ALLOC_PERIOD 1
|
|
|
|
/* Check for expired dummies after a few new ones have been allocated */
|
|
|
|
#define CLEANUP_PERIOD (3 * ALLOC_PERIOD)
|
|
|
|
/* Dummies expire after a few cleanup instances */
|
|
|
|
#define EXPIRE_PERIOD (4 * CLEANUP_PERIOD)
|
|
|
|
|
|
|
|
struct dummy {
|
|
|
|
struct list_head list;
|
|
|
|
unsigned long jiffies_expire;
|
|
|
|
};
|
|
|
|
|
2018-04-16 19:36:46 +08:00
|
|
|
/*
|
|
|
|
* The constructor makes more sense together with klp_shadow_get_or_alloc().
|
|
|
|
* In this example, it would be safe to assign the pointer also to the shadow
|
|
|
|
* variable returned by klp_shadow_alloc(). But we wanted to show the more
|
|
|
|
* complicated use of the API.
|
|
|
|
*/
|
|
|
|
static int shadow_leak_ctor(void *obj, void *shadow_data, void *ctor_data)
|
|
|
|
{
|
2020-01-16 23:31:42 +08:00
|
|
|
int **shadow_leak = shadow_data;
|
2020-01-16 23:31:44 +08:00
|
|
|
int **leak = ctor_data;
|
2018-04-16 19:36:46 +08:00
|
|
|
|
2020-01-16 23:31:44 +08:00
|
|
|
if (!ctor_data)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*shadow_leak = *leak;
|
2018-04-16 19:36:46 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-24 09:48:16 +08:00
|
|
|
static struct dummy *livepatch_fix1_dummy_alloc(void)
|
2017-09-01 04:37:41 +08:00
|
|
|
{
|
|
|
|
struct dummy *d;
|
2020-01-16 23:31:42 +08:00
|
|
|
int *leak;
|
2020-01-16 23:31:45 +08:00
|
|
|
int **shadow_leak;
|
2017-09-01 04:37:41 +08:00
|
|
|
|
|
|
|
d = kzalloc(sizeof(*d), GFP_KERNEL);
|
|
|
|
if (!d)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
d->jiffies_expire = jiffies +
|
|
|
|
msecs_to_jiffies(1000 * EXPIRE_PERIOD);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Patch: save the extra memory location into a SV_LEAK shadow
|
|
|
|
* variable. A patched dummy_free routine can later fetch this
|
|
|
|
* pointer to handle resource release.
|
|
|
|
*/
|
2020-01-16 23:31:42 +08:00
|
|
|
leak = kzalloc(sizeof(*leak), GFP_KERNEL);
|
2020-01-16 23:31:45 +08:00
|
|
|
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;
|
2018-12-15 00:56:10 +08:00
|
|
|
}
|
|
|
|
|
2017-09-01 04:37:41 +08:00
|
|
|
pr_info("%s: dummy @ %p, expires @ %lx\n",
|
|
|
|
__func__, d, d->jiffies_expire);
|
|
|
|
|
|
|
|
return d;
|
2020-01-16 23:31:45 +08:00
|
|
|
|
|
|
|
err_shadow:
|
|
|
|
kfree(leak);
|
|
|
|
err_leak:
|
|
|
|
kfree(d);
|
|
|
|
return NULL;
|
2017-09-01 04:37:41 +08:00
|
|
|
}
|
|
|
|
|
2018-04-16 19:36:47 +08:00
|
|
|
static void livepatch_fix1_dummy_leak_dtor(void *obj, void *shadow_data)
|
|
|
|
{
|
|
|
|
void *d = obj;
|
2020-01-16 23:31:42 +08:00
|
|
|
int **shadow_leak = shadow_data;
|
2018-04-16 19:36:47 +08:00
|
|
|
|
|
|
|
pr_info("%s: dummy @ %p, prevented leak @ %p\n",
|
|
|
|
__func__, d, *shadow_leak);
|
2022-03-20 09:51:43 +08:00
|
|
|
kfree(*shadow_leak);
|
2018-04-16 19:36:47 +08:00
|
|
|
}
|
|
|
|
|
2019-01-24 09:48:16 +08:00
|
|
|
static void livepatch_fix1_dummy_free(struct dummy *d)
|
2017-09-01 04:37:41 +08:00
|
|
|
{
|
2020-01-16 23:31:42 +08:00
|
|
|
int **shadow_leak;
|
2017-09-01 04:37:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Patch: fetch the saved SV_LEAK shadow variable, detach and
|
|
|
|
* free it. Note: handle cases where this shadow variable does
|
|
|
|
* not exist (ie, dummy structures allocated before this livepatch
|
|
|
|
* was loaded.)
|
|
|
|
*/
|
|
|
|
shadow_leak = klp_shadow_get(d, SV_LEAK);
|
2018-04-16 19:36:47 +08:00
|
|
|
if (shadow_leak)
|
|
|
|
klp_shadow_free(d, SV_LEAK, livepatch_fix1_dummy_leak_dtor);
|
|
|
|
else
|
2017-09-01 04:37:41 +08:00
|
|
|
pr_info("%s: dummy @ %p leaked!\n", __func__, d);
|
|
|
|
|
|
|
|
kfree(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct klp_func funcs[] = {
|
|
|
|
{
|
|
|
|
.old_name = "dummy_alloc",
|
|
|
|
.new_func = livepatch_fix1_dummy_alloc,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.old_name = "dummy_free",
|
|
|
|
.new_func = livepatch_fix1_dummy_free,
|
|
|
|
}, { }
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct klp_object objs[] = {
|
|
|
|
{
|
|
|
|
.name = "livepatch_shadow_mod",
|
|
|
|
.funcs = funcs,
|
|
|
|
}, { }
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct klp_patch patch = {
|
|
|
|
.mod = THIS_MODULE,
|
|
|
|
.objs = objs,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int livepatch_shadow_fix1_init(void)
|
|
|
|
{
|
2019-01-09 20:43:23 +08:00
|
|
|
return klp_enable_patch(&patch);
|
2017-09-01 04:37:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void livepatch_shadow_fix1_exit(void)
|
|
|
|
{
|
|
|
|
/* Cleanup any existing SV_LEAK shadow variables */
|
2018-04-16 19:36:47 +08:00
|
|
|
klp_shadow_free_all(SV_LEAK, livepatch_fix1_dummy_leak_dtor);
|
2017-09-01 04:37:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(livepatch_shadow_fix1_init);
|
|
|
|
module_exit(livepatch_shadow_fix1_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_INFO(livepatch, "Y");
|