Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 1 | /* |
| 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 | |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 21 | |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/livepatch.h> |
| 25 | |
| 26 | /* |
| 27 | * This (dumb) live patch overrides the function that prints the |
| 28 | * kernel boot cmdline when /proc/cmdline is read. |
| 29 | * |
| 30 | * Example: |
Josh Poimboeuf | 700a304 | 2014-12-23 17:06:43 -0600 | [diff] [blame] | 31 | * |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 32 | * $ cat /proc/cmdline |
| 33 | * <your cmdline> |
Josh Poimboeuf | 700a304 | 2014-12-23 17:06:43 -0600 | [diff] [blame] | 34 | * |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 35 | * $ insmod livepatch-sample.ko |
| 36 | * $ cat /proc/cmdline |
| 37 | * this has been live patched |
Josh Poimboeuf | 700a304 | 2014-12-23 17:06:43 -0600 | [diff] [blame] | 38 | * |
| 39 | * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled |
| 40 | * $ cat /proc/cmdline |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 41 | * <your cmdline> |
| 42 | */ |
| 43 | |
| 44 | #include <linux/seq_file.h> |
| 45 | static int livepatch_cmdline_proc_show(struct seq_file *m, void *v) |
| 46 | { |
| 47 | seq_printf(m, "%s\n", "this has been live patched"); |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static struct klp_func funcs[] = { |
| 52 | { |
| 53 | .old_name = "cmdline_proc_show", |
| 54 | .new_func = livepatch_cmdline_proc_show, |
| 55 | }, { } |
| 56 | }; |
| 57 | |
| 58 | static struct klp_object objs[] = { |
| 59 | { |
| 60 | /* name being NULL means vmlinux */ |
| 61 | .funcs = funcs, |
| 62 | }, { } |
| 63 | }; |
| 64 | |
| 65 | static struct klp_patch patch = { |
| 66 | .mod = THIS_MODULE, |
| 67 | .objs = objs, |
| 68 | }; |
| 69 | |
| 70 | static int livepatch_init(void) |
| 71 | { |
| 72 | int ret; |
| 73 | |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 74 | if (!klp_have_reliable_stack() && !patch.immediate) { |
| 75 | /* |
| 76 | * WARNING: Be very careful when using 'patch.immediate' in |
| 77 | * your patches. It's ok to use it for simple patches like |
| 78 | * this, but for more complex patches which change function |
| 79 | * semantics, locking semantics, or data structures, it may not |
| 80 | * be safe. Use of this option will also prevent removal of |
| 81 | * the patch. |
| 82 | * |
| 83 | * See Documentation/livepatch/livepatch.txt for more details. |
| 84 | */ |
| 85 | patch.immediate = true; |
| 86 | pr_notice("The consistency model isn't supported for your architecture. Bypassing safety mechanisms and applying the patch immediately.\n"); |
| 87 | } |
| 88 | |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 89 | ret = klp_register_patch(&patch); |
| 90 | if (ret) |
| 91 | return ret; |
| 92 | ret = klp_enable_patch(&patch); |
| 93 | if (ret) { |
| 94 | WARN_ON(klp_unregister_patch(&patch)); |
| 95 | return ret; |
| 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static void livepatch_exit(void) |
| 101 | { |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 102 | WARN_ON(klp_unregister_patch(&patch)); |
| 103 | } |
| 104 | |
| 105 | module_init(livepatch_init); |
| 106 | module_exit(livepatch_exit); |
| 107 | MODULE_LICENSE("GPL"); |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 108 | MODULE_INFO(livepatch, "Y"); |