blob: 21f159d51074e60b8e7f6f74f8528993009a5374 [file] [log] [blame]
Seth Jennings13d1cf72014-12-16 11:58:20 -06001/*
2 * livepatch-sample.c - Kernel Live Patching Sample Module
3 *
4 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/module.h>
21#include <linux/kernel.h>
22#include <linux/livepatch.h>
23
24/*
25 * This (dumb) live patch overrides the function that prints the
26 * kernel boot cmdline when /proc/cmdline is read.
27 *
28 * Example:
29 * $ cat /proc/cmdline
30 * <your cmdline>
31 * $ insmod livepatch-sample.ko
32 * $ cat /proc/cmdline
33 * this has been live patched
34 * $ echo 0 > /sys/kernel/livepatch/klp_sample/enabled
35 * <your cmdline>
36 */
37
38#include <linux/seq_file.h>
39static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
40{
41 seq_printf(m, "%s\n", "this has been live patched");
42 return 0;
43}
44
45static struct klp_func funcs[] = {
46 {
47 .old_name = "cmdline_proc_show",
48 .new_func = livepatch_cmdline_proc_show,
49 }, { }
50};
51
52static struct klp_object objs[] = {
53 {
54 /* name being NULL means vmlinux */
55 .funcs = funcs,
56 }, { }
57};
58
59static struct klp_patch patch = {
60 .mod = THIS_MODULE,
61 .objs = objs,
62};
63
64static int livepatch_init(void)
65{
66 int ret;
67
68 ret = klp_register_patch(&patch);
69 if (ret)
70 return ret;
71 ret = klp_enable_patch(&patch);
72 if (ret) {
73 WARN_ON(klp_unregister_patch(&patch));
74 return ret;
75 }
76 return 0;
77}
78
79static void livepatch_exit(void)
80{
81 WARN_ON(klp_disable_patch(&patch));
82 WARN_ON(klp_unregister_patch(&patch));
83}
84
85module_init(livepatch_init);
86module_exit(livepatch_exit);
87MODULE_LICENSE("GPL");