aboutsummaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
authorSeth Jennings <sjenning@redhat.com>2014-12-16 12:58:20 -0500
committerJiri Kosina <jkosina@suse.cz>2014-12-22 09:40:49 -0500
commit13d1cf7e702596e0cd8ec62afa6bd49c431f2d0c (patch)
tree0ee633d435755d634793462e0a6fc528205822dd /samples
parentb700e7f03df5d92f85fa5247fe1f557528d3363d (diff)
livepatch: samples: add sample live patching module
Add a sample live patching module. Signed-off-by: Seth Jennings <sjenning@redhat.com> Reviewed-by: Miroslav Benes <mbenes@suse.cz> Reviewed-by: Petr Mladek <pmladek@suse.cz> Reviewed-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
Diffstat (limited to 'samples')
-rw-r--r--samples/Kconfig7
-rw-r--r--samples/Makefile2
-rw-r--r--samples/livepatch/Makefile1
-rw-r--r--samples/livepatch/livepatch-sample.c87
4 files changed, 96 insertions, 1 deletions
diff --git a/samples/Kconfig b/samples/Kconfig
index 6181c2cc9ca0..0aed20df5f0b 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -63,4 +63,11 @@ config SAMPLE_RPMSG_CLIENT
63 to communicate with an AMP-configured remote processor over 63 to communicate with an AMP-configured remote processor over
64 the rpmsg bus. 64 the rpmsg bus.
65 65
66config SAMPLE_LIVE_PATCHING
67 tristate "Build live patching sample -- loadable modules only"
68 depends on LIVE_PATCHING && m
69 help
70 Builds a sample live patch that replaces the procfs handler
71 for /proc/cmdline to print "this has been live patched".
72
66endif # SAMPLES 73endif # SAMPLES
diff --git a/samples/Makefile b/samples/Makefile
index 1a60c62e2045..f00257bcc5a7 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -1,4 +1,4 @@
1# Makefile for Linux samples code 1# Makefile for Linux samples code
2 2
3obj-$(CONFIG_SAMPLES) += kobject/ kprobes/ trace_events/ \ 3obj-$(CONFIG_SAMPLES) += kobject/ kprobes/ trace_events/ livepatch/ \
4 hw_breakpoint/ kfifo/ kdb/ hidraw/ rpmsg/ seccomp/ 4 hw_breakpoint/ kfifo/ kdb/ hidraw/ rpmsg/ seccomp/
diff --git a/samples/livepatch/Makefile b/samples/livepatch/Makefile
new file mode 100644
index 000000000000..7f1cdc131a02
--- /dev/null
+++ b/samples/livepatch/Makefile
@@ -0,0 +1 @@
obj-$(CONFIG_SAMPLE_LIVE_PATCHING) += livepatch-sample.o
diff --git a/samples/livepatch/livepatch-sample.c b/samples/livepatch/livepatch-sample.c
new file mode 100644
index 000000000000..21f159d51074
--- /dev/null
+++ b/samples/livepatch/livepatch-sample.c
@@ -0,0 +1,87 @@
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
20#include <linux/module.h>
21#include <linux/kernel.h>
22#include <linux/livepatch.h>
23
24/*
25 * This (dumb) live patch overrides the function that prints the
26 * kernel boot cmdline when /proc/cmdline is read.
27 *
28 * Example:
29 * $ cat /proc/cmdline
30 * <your cmdline>
31 * $ insmod livepatch-sample.ko
32 * $ cat /proc/cmdline
33 * this has been live patched
34 * $ echo 0 > /sys/kernel/livepatch/klp_sample/enabled
35 * <your cmdline>
36 */
37
38#include <linux/seq_file.h>
39static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
40{
41 seq_printf(m, "%s\n", "this has been live patched");
42 return 0;
43}
44
45static struct klp_func funcs[] = {
46 {
47 .old_name = "cmdline_proc_show",
48 .new_func = livepatch_cmdline_proc_show,
49 }, { }
50};
51
52static struct klp_object objs[] = {
53 {
54 /* name being NULL means vmlinux */
55 .funcs = funcs,
56 }, { }
57};
58
59static struct klp_patch patch = {
60 .mod = THIS_MODULE,
61 .objs = objs,
62};
63
64static int livepatch_init(void)
65{
66 int ret;
67
68 ret = klp_register_patch(&patch);
69 if (ret)
70 return ret;
71 ret = klp_enable_patch(&patch);
72 if (ret) {
73 WARN_ON(klp_unregister_patch(&patch));
74 return ret;
75 }
76 return 0;
77}
78
79static void livepatch_exit(void)
80{
81 WARN_ON(klp_disable_patch(&patch));
82 WARN_ON(klp_unregister_patch(&patch));
83}
84
85module_init(livepatch_init);
86module_exit(livepatch_exit);
87MODULE_LICENSE("GPL");