Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 2 | /* |
| 3 | * livepatch-sample.c - Kernel Live Patching Sample Module |
| 4 | * |
| 5 | * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com> |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 6 | */ |
| 7 | |
Josh Poimboeuf | d83a7cb | 2017-02-13 19:42:40 -0600 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/livepatch.h> |
| 13 | |
| 14 | /* |
| 15 | * This (dumb) live patch overrides the function that prints the |
| 16 | * kernel boot cmdline when /proc/cmdline is read. |
| 17 | * |
| 18 | * Example: |
Josh Poimboeuf | 700a304 | 2014-12-23 17:06:43 -0600 | [diff] [blame] | 19 | * |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 20 | * $ cat /proc/cmdline |
| 21 | * <your cmdline> |
Josh Poimboeuf | 700a304 | 2014-12-23 17:06:43 -0600 | [diff] [blame] | 22 | * |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 23 | * $ insmod livepatch-sample.ko |
| 24 | * $ cat /proc/cmdline |
| 25 | * this has been live patched |
Josh Poimboeuf | 700a304 | 2014-12-23 17:06:43 -0600 | [diff] [blame] | 26 | * |
| 27 | * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled |
| 28 | * $ cat /proc/cmdline |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 29 | * <your cmdline> |
| 30 | */ |
| 31 | |
| 32 | #include <linux/seq_file.h> |
| 33 | static int livepatch_cmdline_proc_show(struct seq_file *m, void *v) |
| 34 | { |
| 35 | seq_printf(m, "%s\n", "this has been live patched"); |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static struct klp_func funcs[] = { |
| 40 | { |
| 41 | .old_name = "cmdline_proc_show", |
| 42 | .new_func = livepatch_cmdline_proc_show, |
| 43 | }, { } |
| 44 | }; |
| 45 | |
| 46 | static struct klp_object objs[] = { |
| 47 | { |
| 48 | /* name being NULL means vmlinux */ |
| 49 | .funcs = funcs, |
| 50 | }, { } |
| 51 | }; |
| 52 | |
| 53 | static struct klp_patch patch = { |
| 54 | .mod = THIS_MODULE, |
| 55 | .objs = objs, |
| 56 | }; |
| 57 | |
| 58 | static int livepatch_init(void) |
| 59 | { |
Petr Mladek | 958ef1e | 2019-01-09 13:43:23 +0100 | [diff] [blame] | 60 | return klp_enable_patch(&patch); |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static void livepatch_exit(void) |
| 64 | { |
Seth Jennings | 13d1cf7 | 2014-12-16 11:58:20 -0600 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | module_init(livepatch_init); |
| 68 | module_exit(livepatch_exit); |
| 69 | MODULE_LICENSE("GPL"); |
Jessica Yu | 425595a | 2016-03-22 20:03:18 -0400 | [diff] [blame] | 70 | MODULE_INFO(livepatch, "Y"); |