blob: e34f871e69b1ae60ebd8718dd1f0063a4db00e52 [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:
Josh Poimboeuf700a3042014-12-23 17:06:43 -060029 *
Seth Jennings13d1cf72014-12-16 11:58:20 -060030 * $ cat /proc/cmdline
31 * <your cmdline>
Josh Poimboeuf700a3042014-12-23 17:06:43 -060032 *
Seth Jennings13d1cf72014-12-16 11:58:20 -060033 * $ insmod livepatch-sample.ko
34 * $ cat /proc/cmdline
35 * this has been live patched
Josh Poimboeuf700a3042014-12-23 17:06:43 -060036 *
37 * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled
38 * $ cat /proc/cmdline
Seth Jennings13d1cf72014-12-16 11:58:20 -060039 * <your cmdline>
40 */
41
42#include <linux/seq_file.h>
43static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
44{
45 seq_printf(m, "%s\n", "this has been live patched");
46 return 0;
47}
48
49static struct klp_func funcs[] = {
50 {
51 .old_name = "cmdline_proc_show",
52 .new_func = livepatch_cmdline_proc_show,
53 }, { }
54};
55
56static struct klp_object objs[] = {
57 {
58 /* name being NULL means vmlinux */
59 .funcs = funcs,
60 }, { }
61};
62
63static struct klp_patch patch = {
64 .mod = THIS_MODULE,
65 .objs = objs,
66};
67
68static int livepatch_init(void)
69{
70 int ret;
71
72 ret = klp_register_patch(&patch);
73 if (ret)
74 return ret;
75 ret = klp_enable_patch(&patch);
76 if (ret) {
77 WARN_ON(klp_unregister_patch(&patch));
78 return ret;
79 }
80 return 0;
81}
82
83static void livepatch_exit(void)
84{
85 WARN_ON(klp_disable_patch(&patch));
86 WARN_ON(klp_unregister_patch(&patch));
87}
88
89module_init(livepatch_init);
90module_exit(livepatch_exit);
91MODULE_LICENSE("GPL");
Jessica Yu425595a2016-03-22 20:03:18 -040092MODULE_INFO(livepatch, "Y");