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 2013d1cf7eSSeth Jennings #include <linux/module.h> 2113d1cf7eSSeth Jennings #include <linux/kernel.h> 2213d1cf7eSSeth Jennings #include <linux/livepatch.h> 2313d1cf7eSSeth Jennings 2413d1cf7eSSeth Jennings /* 2513d1cf7eSSeth Jennings * This (dumb) live patch overrides the function that prints the 2613d1cf7eSSeth Jennings * kernel boot cmdline when /proc/cmdline is read. 2713d1cf7eSSeth Jennings * 2813d1cf7eSSeth Jennings * Example: 29*700a3048SJosh Poimboeuf * 3013d1cf7eSSeth Jennings * $ cat /proc/cmdline 3113d1cf7eSSeth Jennings * <your cmdline> 32*700a3048SJosh Poimboeuf * 3313d1cf7eSSeth Jennings * $ insmod livepatch-sample.ko 3413d1cf7eSSeth Jennings * $ cat /proc/cmdline 3513d1cf7eSSeth Jennings * this has been live patched 36*700a3048SJosh Poimboeuf * 37*700a3048SJosh Poimboeuf * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled 38*700a3048SJosh Poimboeuf * $ cat /proc/cmdline 3913d1cf7eSSeth Jennings * <your cmdline> 4013d1cf7eSSeth Jennings */ 4113d1cf7eSSeth Jennings 4213d1cf7eSSeth Jennings #include <linux/seq_file.h> 4313d1cf7eSSeth Jennings static int livepatch_cmdline_proc_show(struct seq_file *m, void *v) 4413d1cf7eSSeth Jennings { 4513d1cf7eSSeth Jennings seq_printf(m, "%s\n", "this has been live patched"); 4613d1cf7eSSeth Jennings return 0; 4713d1cf7eSSeth Jennings } 4813d1cf7eSSeth Jennings 4913d1cf7eSSeth Jennings static struct klp_func funcs[] = { 5013d1cf7eSSeth Jennings { 5113d1cf7eSSeth Jennings .old_name = "cmdline_proc_show", 5213d1cf7eSSeth Jennings .new_func = livepatch_cmdline_proc_show, 5313d1cf7eSSeth Jennings }, { } 5413d1cf7eSSeth Jennings }; 5513d1cf7eSSeth Jennings 5613d1cf7eSSeth Jennings static struct klp_object objs[] = { 5713d1cf7eSSeth Jennings { 5813d1cf7eSSeth Jennings /* name being NULL means vmlinux */ 5913d1cf7eSSeth Jennings .funcs = funcs, 6013d1cf7eSSeth Jennings }, { } 6113d1cf7eSSeth Jennings }; 6213d1cf7eSSeth Jennings 6313d1cf7eSSeth Jennings static struct klp_patch patch = { 6413d1cf7eSSeth Jennings .mod = THIS_MODULE, 6513d1cf7eSSeth Jennings .objs = objs, 6613d1cf7eSSeth Jennings }; 6713d1cf7eSSeth Jennings 6813d1cf7eSSeth Jennings static int livepatch_init(void) 6913d1cf7eSSeth Jennings { 7013d1cf7eSSeth Jennings int ret; 7113d1cf7eSSeth Jennings 7213d1cf7eSSeth Jennings ret = klp_register_patch(&patch); 7313d1cf7eSSeth Jennings if (ret) 7413d1cf7eSSeth Jennings return ret; 7513d1cf7eSSeth Jennings ret = klp_enable_patch(&patch); 7613d1cf7eSSeth Jennings if (ret) { 7713d1cf7eSSeth Jennings WARN_ON(klp_unregister_patch(&patch)); 7813d1cf7eSSeth Jennings return ret; 7913d1cf7eSSeth Jennings } 8013d1cf7eSSeth Jennings return 0; 8113d1cf7eSSeth Jennings } 8213d1cf7eSSeth Jennings 8313d1cf7eSSeth Jennings static void livepatch_exit(void) 8413d1cf7eSSeth Jennings { 8513d1cf7eSSeth Jennings WARN_ON(klp_disable_patch(&patch)); 8613d1cf7eSSeth Jennings WARN_ON(klp_unregister_patch(&patch)); 8713d1cf7eSSeth Jennings } 8813d1cf7eSSeth Jennings 8913d1cf7eSSeth Jennings module_init(livepatch_init); 9013d1cf7eSSeth Jennings module_exit(livepatch_exit); 9113d1cf7eSSeth Jennings MODULE_LICENSE("GPL"); 92