From: Joao Moreira <jmoreira@suse.de>
To: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: mbenes@suse.cz, mmarek@suse.cz, pmladek@suse.com, jikos@suse.cz,
nstange@suse.de, jroedel@suse.de, matz@suse.de,
jpoimboe@redhat.com, khlebnikov@yandex-team.ru, jeyu@kernel.org,
jmoreira@suse.de
Subject: [PATCH 7/8] livepatch: Add sample livepatch module
Date: Tue, 29 Aug 2017 16:01:39 -0300 [thread overview]
Message-ID: <20170829190140.401-8-jmoreira@suse.de> (raw)
In-Reply-To: <20170829190140.401-1-jmoreira@suse.de>
From: Josh Poimboeuf <jpoimboe@redhat.com>
Add a new livepatch sample in samples/livepatch/ to make use of
symbols that must be post-processed to enable load-time relocation
resolution. As the new sample is to be used as an example, it is
annotated with KLP_MODULE_REOC and with KLP_SYMPOS macros.
The livepatch sample updates the function cmdline_proc_show to
print the string referenced by the symbol saved_command_line
appended by the string "livepatch=1".
[jmoreira:
* Switched from "updating existing" module to "adding a new" module
* Update module to use KLP_SYMPOS
* Comments on symbol resolution scheme
* Update Makefile
* Changelog
]
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Joao Moreira <jmoreira@suse.de>
---
samples/livepatch/Makefile | 4 +-
samples/livepatch/livepatch-annotated-sample.c | 128 +++++++++++++++++++++++++
2 files changed, 131 insertions(+), 1 deletion(-)
create mode 100644 samples/livepatch/livepatch-annotated-sample.c
diff --git a/samples/livepatch/Makefile b/samples/livepatch/Makefile
index 955e7ac12d2b..2690ef439bec 100644
--- a/samples/livepatch/Makefile
+++ b/samples/livepatch/Makefile
@@ -1,2 +1,4 @@
LIVEPATCH_livepatch-sample.o := y
-obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-sample.o
+LIVEPATCH_livepatch-annotated-sample.o := y
+obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-sample.o \
+ livepatch-annotated-sample.o
diff --git a/samples/livepatch/livepatch-annotated-sample.c b/samples/livepatch/livepatch-annotated-sample.c
new file mode 100644
index 000000000000..93da32610076
--- /dev/null
+++ b/samples/livepatch/livepatch-annotated-sample.c
@@ -0,0 +1,128 @@
+/*
+ * livepatch-annotated-sample.c - Kernel Live Patching Sample Module
+ *
+ * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/livepatch.h>
+#include <linux/seq_file.h>
+
+/*
+ * This (dumb) live patch overrides the function that prints the
+ * kernel boot cmdline when /proc/cmdline is read.
+ *
+ * This livepatch uses the symbol saved_command_line whose relocation
+ * must be resolved during load time. To enable that, this module
+ * must be post-processed by a tool called klp-convert, which embeds
+ * information to be used by the loader to solve the relocation.
+ *
+ * The module is annotated with KLP_MODULE_RELOC/KLP_SYMPOS macros.
+ * These annotations are used by klp-convert to infer that the symbol
+ * saved_command_line is in the object vmlinux.
+ *
+ * As saved_command_line has no other homonimous symbol across
+ * kernel objects, this annotation is not a requirement, and can be
+ * suppressed with no harm to klp-convert. Yet, it is kept here as an
+ * example on how to annotate livepatch modules that contain symbols
+ * whose names are used in more than one kernel object.
+ *
+ * Example:
+ *
+ * $ cat /proc/cmdline
+ * <your cmdline>
+ *
+ * $ insmod livepatch-sample.ko
+ * $ cat /proc/cmdline
+ * <your cmdline> livepatch=1
+ *
+ * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled
+ * $ cat /proc/cmdline
+ * <your cmdline>
+ */
+
+extern char *saved_command_line;
+
+static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
+{
+ seq_printf(m, "%s livepatch=1\n", saved_command_line);
+ return 0;
+}
+
+KLP_MODULE_RELOC(vmlinux) vmlinux_relocs[] = {
+ KLP_SYMPOS(saved_command_line, 0)
+};
+
+static struct klp_func vmlinux_funcs[] = {
+ {
+ .old_name = "cmdline_proc_show",
+ .new_func = livepatch_cmdline_proc_show,
+ }, { }
+};
+
+static struct klp_object objs[] = {
+ {
+ /* name being NULL means vmlinux */
+ .funcs = vmlinux_funcs,
+ }, { }
+};
+
+static struct klp_patch patch = {
+ .mod = THIS_MODULE,
+ .objs = objs,
+};
+
+static int livepatch_init(void)
+{
+ int ret;
+
+ if (!klp_have_reliable_stack() && !patch.immediate) {
+ /*
+ * WARNING: Be very careful when using 'patch.immediate' in
+ * your patches. It's ok to use it for simple patches like
+ * this, but for more complex patches which change function
+ * semantics, locking semantics, or data structures, it may not
+ * be safe. Use of this option will also prevent removal of
+ * the patch.
+ *
+ * See Documentation/livepatch/livepatch.txt for more details.
+ */
+ patch.immediate = true;
+ pr_notice("The consistency model isn't supported for your architecture. Bypassing safety mechanisms and applying the patch immediately.\n");
+ }
+
+ ret = klp_register_patch(&patch);
+ if (ret)
+ return ret;
+ ret = klp_enable_patch(&patch);
+ if (ret) {
+ WARN_ON(klp_unregister_patch(&patch));
+ return ret;
+ }
+ return 0;
+}
+
+static void livepatch_exit(void)
+{
+ WARN_ON(klp_unregister_patch(&patch));
+}
+
+module_init(livepatch_init);
+module_exit(livepatch_exit);
+MODULE_LICENSE("GPL");
--
2.12.0
next prev parent reply other threads:[~2017-08-29 19:02 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-29 19:01 [PATCH 0/8] livepatch: klp-convert tool Joao Moreira
2017-08-29 19:01 ` [PATCH 1/8] livepatch: Create and include UAPI headers Joao Moreira
2017-08-29 19:01 ` [PATCH 2/8] kbuild: Support for Symbols.list creation Joao Moreira
2017-08-31 15:24 ` Joe Lawrence
2017-08-31 17:34 ` Josh Poimboeuf
2017-09-04 7:23 ` Joao Moreira
2017-08-29 19:01 ` [PATCH 3/8] livepatch: Add klp-convert tool Joao Moreira
2017-08-30 20:03 ` Joao Moreira
2017-08-29 19:01 ` [PATCH 4/8] livepatch: Add klp-convert annotation helpers Joao Moreira
2017-08-29 19:01 ` [PATCH 5/8] modpost: Integrate klp-convert Joao Moreira
2017-08-29 19:01 ` [PATCH 6/8] modpost: Add modinfo flag to livepatch modules Joao Moreira
2017-08-29 19:01 ` Joao Moreira [this message]
2017-08-29 19:01 ` [PATCH 8/8] documentation: Update on livepatch elf format Joao Moreira
2017-08-30 18:00 ` [PATCH 0/8] livepatch: klp-convert tool Josh Poimboeuf
2017-10-10 14:17 ` Miroslav Benes
2017-10-11 2:46 ` Josh Poimboeuf
2017-10-11 12:42 ` Joao Moreira
2017-10-19 13:01 ` Josh Poimboeuf
2017-10-19 13:24 ` Miroslav Benes
2017-10-19 14:03 ` Josh Poimboeuf
2017-10-19 14:27 ` Miroslav Benes
2017-10-19 15:15 ` Josh Poimboeuf
2017-10-19 16:00 ` Miroslav Benes
2017-10-19 16:20 ` Josh Poimboeuf
2017-10-20 8:51 ` Miroslav Benes
2017-10-20 12:03 ` Josh Poimboeuf
2017-10-20 12:44 ` Torsten Duwe
2017-10-20 13:24 ` Josh Poimboeuf
2017-10-20 13:39 ` Miroslav Benes
2017-10-20 13:44 ` Torsten Duwe
2017-10-20 14:20 ` Josh Poimboeuf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170829190140.401-8-jmoreira@suse.de \
--to=jmoreira@suse.de \
--cc=jeyu@kernel.org \
--cc=jikos@suse.cz \
--cc=jpoimboe@redhat.com \
--cc=jroedel@suse.de \
--cc=khlebnikov@yandex-team.ru \
--cc=linux-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=matz@suse.de \
--cc=mbenes@suse.cz \
--cc=mmarek@suse.cz \
--cc=nstange@suse.de \
--cc=pmladek@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome