Skip to content

Instantly share code, notes, and snippets.

@masami256
Created July 16, 2016 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masami256/5d3da79446f7f201f31528fcf7629033 to your computer and use it in GitHub Desktop.
Save masami256/5d3da79446f7f201f31528fcf7629033 to your computer and use it in GitHub Desktop.
livepatch test module
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/livepatch.h>
#include <linux/seq_file.h>
#include <linux/string.h>
#include <linux/sched.h>
#include <linux/pid_namespace.h>
MODULE_DESCRIPTION("livepatch test module");
MODULE_AUTHOR("masami256");
MODULE_LICENSE("GPL");
static char *target_comm = NULL;
module_param(target_comm, charp, S_IRUGO);
MODULE_PARM_DESC(target_comm, "Target process name");
static pid_t target_pid = 0;
module_param(target_pid, int, S_IRUGO);
MODULE_PARM_DESC(target_pid, "Target pid");
static unsigned int target_inum = 0;
module_param(target_inum, uint, S_IRUGO);
MODULE_PARM_DESC(target_inum, "Target pid namespace's inode number");
static int livetest_cmdline_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "hello, world\n");
return 0;
}
static struct klp_func funcs[] = {
{
.old_name = "cmdline_proc_show",
.new_func = livetest_cmdline_proc_show,
},
{
}
};
static struct klp_object objs[] = {
{
.funcs = funcs,
},
{
}
};
static struct klp_patch patch = {
.mod = THIS_MODULE,
.objs = objs,
};
static bool (*need_patch_apply_func)(void) = NULL;
static bool need_patch_apply_by_comm(void)
{
return !strcmp(target_comm, current->comm);
}
static bool need_patch_apply_by_pid(void)
{
return target_pid == current->pid;
}
static bool need_patch_apply_by_pidns(void)
{
struct pid_namespace *pidns = task_active_pid_ns(current);
return target_inum == pidns->ns.inum;
}
static void livetest_determin_patch_apply_func(void)
{
if (target_comm)
need_patch_apply_func = need_patch_apply_by_comm;
else if (target_pid)
need_patch_apply_func = need_patch_apply_by_pid;
else if (target_inum)
need_patch_apply_func = need_patch_apply_by_pidns;
}
static bool livetest_need_patch_apply(void)
{
return need_patch_apply_func();
}
static int livetest_init(void)
{
int ret = 0;
livetest_determin_patch_apply_func();
klp_set_need_patch_apply_func(&livetest_need_patch_apply);
ret = klp_register_patch(&patch);
if (ret) {
pr_warn("failed to register patch\n");
return ret;
}
ret = klp_enable_patch(&patch);
if (ret) {
WARN_ON(klp_unregister_patch(&patch));
return ret;
}
return ret;
}
static void livetest_cleanup(void)
{
WARN_ON(klp_disable_patch(&patch));
WARN_ON(klp_unregister_patch(&patch));
}
module_init(livetest_init);
module_exit(livetest_cleanup);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment