113d1cf7eSSeth Jennings /*
213d1cf7eSSeth Jennings  * livepatch-sample.c - Kernel Live Patching Sample Module
313d1cf7eSSeth Jennings  *
413d1cf7eSSeth Jennings  * Copyright (C) 2014 Seth Jennings <[email protected]>
513d1cf7eSSeth Jennings  *
613d1cf7eSSeth Jennings  * This program is free software; you can redistribute it and/or
713d1cf7eSSeth Jennings  * modify it under the terms of the GNU General Public License
813d1cf7eSSeth Jennings  * as published by the Free Software Foundation; either version 2
913d1cf7eSSeth Jennings  * of the License, or (at your option) any later version.
1013d1cf7eSSeth Jennings  *
1113d1cf7eSSeth Jennings  * This program is distributed in the hope that it will be useful,
1213d1cf7eSSeth Jennings  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1313d1cf7eSSeth Jennings  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1413d1cf7eSSeth Jennings  * GNU General Public License for more details.
1513d1cf7eSSeth Jennings  *
1613d1cf7eSSeth Jennings  * You should have received a copy of the GNU General Public License
1713d1cf7eSSeth Jennings  * along with this program; if not, see <http://www.gnu.org/licenses/>.
1813d1cf7eSSeth Jennings  */
1913d1cf7eSSeth Jennings 
20*d83a7cb3SJosh Poimboeuf #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21*d83a7cb3SJosh Poimboeuf 
2213d1cf7eSSeth Jennings #include <linux/module.h>
2313d1cf7eSSeth Jennings #include <linux/kernel.h>
2413d1cf7eSSeth Jennings #include <linux/livepatch.h>
2513d1cf7eSSeth Jennings 
2613d1cf7eSSeth Jennings /*
2713d1cf7eSSeth Jennings  * This (dumb) live patch overrides the function that prints the
2813d1cf7eSSeth Jennings  * kernel boot cmdline when /proc/cmdline is read.
2913d1cf7eSSeth Jennings  *
3013d1cf7eSSeth Jennings  * Example:
31700a3048SJosh Poimboeuf  *
3213d1cf7eSSeth Jennings  * $ cat /proc/cmdline
3313d1cf7eSSeth Jennings  * <your cmdline>
34700a3048SJosh Poimboeuf  *
3513d1cf7eSSeth Jennings  * $ insmod livepatch-sample.ko
3613d1cf7eSSeth Jennings  * $ cat /proc/cmdline
3713d1cf7eSSeth Jennings  * this has been live patched
38700a3048SJosh Poimboeuf  *
39700a3048SJosh Poimboeuf  * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled
40700a3048SJosh Poimboeuf  * $ cat /proc/cmdline
4113d1cf7eSSeth Jennings  * <your cmdline>
4213d1cf7eSSeth Jennings  */
4313d1cf7eSSeth Jennings 
4413d1cf7eSSeth Jennings #include <linux/seq_file.h>
4513d1cf7eSSeth Jennings static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
4613d1cf7eSSeth Jennings {
4713d1cf7eSSeth Jennings 	seq_printf(m, "%s\n", "this has been live patched");
4813d1cf7eSSeth Jennings 	return 0;
4913d1cf7eSSeth Jennings }
5013d1cf7eSSeth Jennings 
5113d1cf7eSSeth Jennings static struct klp_func funcs[] = {
5213d1cf7eSSeth Jennings 	{
5313d1cf7eSSeth Jennings 		.old_name = "cmdline_proc_show",
5413d1cf7eSSeth Jennings 		.new_func = livepatch_cmdline_proc_show,
5513d1cf7eSSeth Jennings 	}, { }
5613d1cf7eSSeth Jennings };
5713d1cf7eSSeth Jennings 
5813d1cf7eSSeth Jennings static struct klp_object objs[] = {
5913d1cf7eSSeth Jennings 	{
6013d1cf7eSSeth Jennings 		/* name being NULL means vmlinux */
6113d1cf7eSSeth Jennings 		.funcs = funcs,
6213d1cf7eSSeth Jennings 	}, { }
6313d1cf7eSSeth Jennings };
6413d1cf7eSSeth Jennings 
6513d1cf7eSSeth Jennings static struct klp_patch patch = {
6613d1cf7eSSeth Jennings 	.mod = THIS_MODULE,
6713d1cf7eSSeth Jennings 	.objs = objs,
6813d1cf7eSSeth Jennings };
6913d1cf7eSSeth Jennings 
7013d1cf7eSSeth Jennings static int livepatch_init(void)
7113d1cf7eSSeth Jennings {
7213d1cf7eSSeth Jennings 	int ret;
7313d1cf7eSSeth Jennings 
74*d83a7cb3SJosh Poimboeuf 	if (!klp_have_reliable_stack() && !patch.immediate) {
75*d83a7cb3SJosh Poimboeuf 		/*
76*d83a7cb3SJosh Poimboeuf 		 * WARNING: Be very careful when using 'patch.immediate' in
77*d83a7cb3SJosh Poimboeuf 		 * your patches.  It's ok to use it for simple patches like
78*d83a7cb3SJosh Poimboeuf 		 * this, but for more complex patches which change function
79*d83a7cb3SJosh Poimboeuf 		 * semantics, locking semantics, or data structures, it may not
80*d83a7cb3SJosh Poimboeuf 		 * be safe.  Use of this option will also prevent removal of
81*d83a7cb3SJosh Poimboeuf 		 * the patch.
82*d83a7cb3SJosh Poimboeuf 		 *
83*d83a7cb3SJosh Poimboeuf 		 * See Documentation/livepatch/livepatch.txt for more details.
84*d83a7cb3SJosh Poimboeuf 		 */
85*d83a7cb3SJosh Poimboeuf 		patch.immediate = true;
86*d83a7cb3SJosh Poimboeuf 		pr_notice("The consistency model isn't supported for your architecture.  Bypassing safety mechanisms and applying the patch immediately.\n");
87*d83a7cb3SJosh Poimboeuf 	}
88*d83a7cb3SJosh Poimboeuf 
8913d1cf7eSSeth Jennings 	ret = klp_register_patch(&patch);
9013d1cf7eSSeth Jennings 	if (ret)
9113d1cf7eSSeth Jennings 		return ret;
9213d1cf7eSSeth Jennings 	ret = klp_enable_patch(&patch);
9313d1cf7eSSeth Jennings 	if (ret) {
9413d1cf7eSSeth Jennings 		WARN_ON(klp_unregister_patch(&patch));
9513d1cf7eSSeth Jennings 		return ret;
9613d1cf7eSSeth Jennings 	}
9713d1cf7eSSeth Jennings 	return 0;
9813d1cf7eSSeth Jennings }
9913d1cf7eSSeth Jennings 
10013d1cf7eSSeth Jennings static void livepatch_exit(void)
10113d1cf7eSSeth Jennings {
10213d1cf7eSSeth Jennings 	WARN_ON(klp_disable_patch(&patch));
10313d1cf7eSSeth Jennings 	WARN_ON(klp_unregister_patch(&patch));
10413d1cf7eSSeth Jennings }
10513d1cf7eSSeth Jennings 
10613d1cf7eSSeth Jennings module_init(livepatch_init);
10713d1cf7eSSeth Jennings module_exit(livepatch_exit);
10813d1cf7eSSeth Jennings MODULE_LICENSE("GPL");
109425595a7SJessica Yu MODULE_INFO(livepatch, "Y");
110