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