Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1304948 > unrolled thread
| Started by | Jessica Yu <jeyu@redhat.com> |
|---|---|
| First post | 2016-01-08 20:30 +0100 |
| Last post | 2016-01-14 10:20 +0100 |
| Articles | 19 — 5 participants |
Back to article view | Back to linux.kernel
[RFC PATCH v3 0/6] (mostly) Arch-independent livepatch Jessica Yu <jeyu@redhat.com> - 2016-01-08 20:30 +0100
[RFC PATCH v3 1/6] Elf: add livepatch-specific Elf constants Jessica Yu <jeyu@redhat.com> - 2016-01-08 20:30 +0100
[RFC PATCH v3 3/6] module: s390: keep mod_arch_specific for livepatch modules Jessica Yu <jeyu@redhat.com> - 2016-01-08 20:30 +0100
[RFC PATCH v3 5/6] samples: livepatch: mark as livepatch module Jessica Yu <jeyu@redhat.com> - 2016-01-08 20:30 +0100
[RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations Jessica Yu <jeyu@redhat.com> - 2016-01-08 20:40 +0100
Re: [RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations Petr Mladek <pmladek@suse.com> - 2016-01-11 18:00 +0100
Re: [RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations Josh Poimboeuf <jpoimboe@redhat.com> - 2016-01-11 22:00 +0100
Re: [RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations Josh Poimboeuf <jpoimboe@redhat.com> - 2016-01-11 22:40 +0100
Re: livepatch: reuse module loader code to write relocations Jessica Yu <jeyu@redhat.com> - 2016-01-11 23:40 +0100
Re: livepatch: reuse module loader code to write relocations Josh Poimboeuf <jpoimboe@redhat.com> - 2016-01-12 04:10 +0100
Re: livepatch: reuse module loader code to write relocations Petr Mladek <pmladek@suse.com> - 2016-01-12 10:20 +0100
Re: livepatch: reuse module loader code to write relocations Jessica Yu <jeyu@redhat.com> - 2016-01-14 06:10 +0100
Re: [RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations Miroslav Benes <mbenes@suse.cz> - 2016-01-12 17:50 +0100
Re: livepatch: reuse module loader code to write relocations Jessica Yu <jeyu@redhat.com> - 2016-01-14 05:00 +0100
Re: livepatch: reuse module loader code to write relocations Miroslav Benes <mbenes@suse.cz> - 2016-01-14 10:10 +0100
Re: [RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations Miroslav Benes <mbenes@suse.cz> - 2016-01-13 10:20 +0100
Re: [RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations Jiri Kosina <jikos@kernel.org> - 2016-01-13 10:40 +0100
Re: livepatch: reuse module loader code to write relocations Jessica Yu <jeyu@redhat.com> - 2016-01-13 19:40 +0100
Re: livepatch: reuse module loader code to write relocations Miroslav Benes <mbenes@suse.cz> - 2016-01-14 10:20 +0100
| From | Jessica Yu <jeyu@redhat.com> |
|---|---|
| Date | 2016-01-08 20:30 +0100 |
| Subject | [RFC PATCH v3 0/6] (mostly) Arch-independent livepatch |
| Message-ID | <qOKTv-2Pp-3@gated-at.bofh.it> |
This patchset removes livepatch's need for architecture-specific relocation
code by leveraging existing code in the module loader to perform
arch-dependent work. Specifically, instead of duplicating code and
re-implementing what the apply_relocate_add() function in the module loader
already does in livepatch's klp_write_module_reloc(), we reuse
apply_relocate_add() to write relocations. The hope is that this will make
livepatch more easily portable to other architectures and greatly reduce
the amount of arch-specific code required to port livepatch to a particular
architecture.
Background: Why does livepatch need to write its own relocations?
==
A typical livepatch module contains patched versions of functions that can
reference non-exported global symbols and non-included local symbols.
Relocations referencing these types of symbols cannot be left in as-is
since the kernel module loader cannot resolve them and will therefore
reject the livepatch module. Furthermore, we cannot apply relocations that
affect modules not loaded yet at run time (e.g. a patch to a driver). The
current kpatch build system therefore solves this problem by embedding
special "dynrela" (dynamic reloc) sections in the resulting patch module
Elf output. Using these dynrela sections, livepatch can correctly resolve
symbols while taking into account its scope and what module the symbol
belongs to, and then manually apply the dynamic relocations.
Motivation: Why is having arch-dependent relocation code a problem?
==
The original motivation for this patchset stems from the increasing
roadblocks encountered while attempting to port livepatch to s390.
Specifically, there were problems dealing with s390 PLT and GOT relocation
types (R_390_{PLT,GOT}), which are handled differently from x86's
relocation types (which are much simpler to deal with, and a single
livepatch function (klp_write_module_reloc()) has been sufficient enough).
These s390 reloc types cannot be handled by simply performing a calculation
(as in the x86 case). For s390 modules with PLT/GOT relocations, the kernel
module loader allocates and fills in PLT+GOT table entries for every symbol
referenced by a PLT/GOT reloc in module core memory. So the problem of
porting livepatch to s390 became much more complicated than simply writing
an s390-specific klp_write_module_reloc() function. How can livepatch
handle these relocation types if the s390 module loader needs to allocate
and fill PLT/GOT entries ahead of time? The potential solutions were: 1)
have livepatch possibly allocate and maintain its own PLT/GOT tables for
every patch module (requiring even more arch-specific code), 2) modify the
s390 module loader heavily to accommodate livepatch modules (i.e. allocate
all the needed PLT/GOT entries for livepatch in advance but refrain from
applying relocations for to-be-patched modules), or 3) eliminate this
potential mess by leveraging module loader code to do all the relocation
work, letting livepatch off the hook completely. Solution #3 is what this
patchset implements.
How does this patchset remedy these problems?
==
Reusing the module loader code to perform livepatch relocations means that
livepatch no longer needs arch-specific reloc code and the aforementioned
problems with s390 PLT/GOT reloc types disappear (because we let the module
loader do all the relocation work for us). It will enable livepatch to be
more easily ported to other architectures.
Summary of proposed changes
==
This patch series enables livepatch to use the module loader's
apply_relocate_add() function to apply livepatch relocations (i.e. what
used to be dynrelas). apply_relocate_add() requires access to a patch
module's section headers, symbol table, reloc section indices, etc., and all
of these are accessible through the load_info struct used in the module
loader. Therefore we persist module Elf information (copied from load_info)
for livepatch modules.
The ELF-related changes enable livepatch to patch modules that are not yet
loaded (as well as patch vmlinux when kaslr is enabled). In order to use
apply_relocate_add(), we need real SHT_RELA sections to pass in. A
complication here is that relocations for not-yet-loaded modules should not
be applied when the patch module loads; they should only be applied once
the target module is loaded. Thus kpatch build scripts were modified to
output a livepatch module that contains special .klp.rel. sections that
are managed by livepatch and are applied at the appropriate time (i.e. when
target module loads). They are marked with a special SHF_RELA_LIVEPATCH
section flag to indicate to the module loader that livepatch will handle
them. The SHN_LIVEPATCH shndx marks symbols that need to be resolved
once their respective target module loads. So, the module loader ignores
these symbols and does not attempt to resolve them. These ELF constants
were selected from OS-specific ranges according to the definitions from
glibc.
Based on the following branch:
- git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livepatching.git
for-4.5/core
v3:
- Remove usage of the klp_reloc_sec struct, since we can simply loop
through the patch module's section headers.
- Remove necessity of the "external" flag by prefixing symbol names
with the object name and extracting this name during symbol
resolution.
- Create CONFIG_LIVEPATCH and !CONFIG_LIVEPATCH versions of
{copy,free}_module_elf(), is_livepatch_module(), and
check_livepatch_modinfo().
- Encoded symbol position of a livepatch sym in its st_other field.
- Various bug fixes from v2
v2:
- Copy only the minimum required Elf information for livepatch modules to
make the call to apply_relocate_add(), not the entire load_info struct
and the redundant copy of the module in memory
- Add module->klp flag for simple identification of livepatch modules
- s390: remove redundant vfree() and preserve mod_arch_specific if
livepatch module
- Use array format instead of a linked list for klp_reloc_secs
- Add new documentation describing the format of a livepatch module in
Documentation/livepatch
Jessica Yu (6):
Elf: add livepatch-specific Elf constants
module: preserve Elf information for livepatch modules
module: s390: keep mod_arch_specific for livepatch modules
livepatch: reuse module loader code to write relocations
samples: livepatch: mark as livepatch module
Documentation: livepatch: outline the Elf format of a livepatch module
Documentation/livepatch/patch-module-format.txt | 98 ++++++++++++++++
arch/s390/kernel/module.c | 16 ++-
arch/x86/include/asm/livepatch.h | 2 -
arch/x86/kernel/Makefile | 1 -
arch/x86/kernel/livepatch.c | 70 -----------
include/linux/livepatch.h | 33 +++---
include/linux/module.h | 31 +++++
include/uapi/linux/elf.h | 10 +-
kernel/livepatch/core.c | 104 ++++++++---------
kernel/module.c | 149 +++++++++++++++++++++---
samples/livepatch/livepatch-sample.c | 1 +
11 files changed, 347 insertions(+), 168 deletions(-)
create mode 100644 Documentation/livepatch/patch-module-format.txt
delete mode 100644 arch/x86/kernel/livepatch.c
--
2.4.3
[toc] | [next] | [standalone]
| From | Jessica Yu <jeyu@redhat.com> |
|---|---|
| Date | 2016-01-08 20:30 +0100 |
| Subject | [RFC PATCH v3 1/6] Elf: add livepatch-specific Elf constants |
| Message-ID | <qOKTw-2Pp-27@gated-at.bofh.it> |
| In reply to | #1304948 |
Add livepatch Elf relocation section flag (SHF_RELA_LIVEPATCH), and symbol
section index (SHN_LIVEPATCH). The values of these Elf constants were
selected from OS-specific ranges according to the definitions from glibc.
Livepatch relocation sections are marked with SHF_RELA_LIVEPATCH to
indicate to the module loader that it should not apply that relocation
section and that livepatch will handle them.
The SHN_LIVEPATCH shndx marks symbols that will be resolved by livepatch.
The module loader ignores these symbols and does not attempt to resolve
them.
Signed-off-by: Jessica Yu <jeyu@redhat.com>
---
include/uapi/linux/elf.h | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h
index 71e1d0e..cb4a72f 100644
--- a/include/uapi/linux/elf.h
+++ b/include/uapi/linux/elf.h
@@ -282,16 +282,18 @@ typedef struct elf64_phdr {
#define SHT_HIUSER 0xffffffff
/* sh_flags */
-#define SHF_WRITE 0x1
-#define SHF_ALLOC 0x2
-#define SHF_EXECINSTR 0x4
-#define SHF_MASKPROC 0xf0000000
+#define SHF_WRITE 0x1
+#define SHF_ALLOC 0x2
+#define SHF_EXECINSTR 0x4
+#define SHF_RELA_LIVEPATCH 0x00100000
+#define SHF_MASKPROC 0xf0000000
/* special section indexes */
#define SHN_UNDEF 0
#define SHN_LORESERVE 0xff00
#define SHN_LOPROC 0xff00
#define SHN_HIPROC 0xff1f
+#define SHN_LIVEPATCH 0xff20
#define SHN_ABS 0xfff1
#define SHN_COMMON 0xfff2
#define SHN_HIRESERVE 0xffff
--
2.4.3
[toc] | [prev] | [next] | [standalone]
| From | Jessica Yu <jeyu@redhat.com> |
|---|---|
| Date | 2016-01-08 20:30 +0100 |
| Subject | [RFC PATCH v3 3/6] module: s390: keep mod_arch_specific for livepatch modules |
| Message-ID | <qOKTx-2Pp-31@gated-at.bofh.it> |
| In reply to | #1304948 |
Livepatch needs to utilize the symbol information contained in the
mod_arch_specific struct in order to be able to call the s390
apply_relocate_add() function to apply relocations. Keep a reference to
syminfo if the module is a livepatch module. Remove the redundant vfree()
in module_finalize() since module_arch_freeing_init() (which also frees
those structures) is called in do_init_module(). If the module isn't a
livepatch module, we free the structures in module_arch_freeing_init() as
usual.
Signed-off-by: Jessica Yu <jeyu@redhat.com>
---
arch/s390/kernel/module.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/arch/s390/kernel/module.c b/arch/s390/kernel/module.c
index 7873e17..fbc0789 100644
--- a/arch/s390/kernel/module.c
+++ b/arch/s390/kernel/module.c
@@ -51,6 +51,10 @@ void *module_alloc(unsigned long size)
void module_arch_freeing_init(struct module *mod)
{
+ if (is_livepatch_module(mod) &&
+ mod->state == MODULE_STATE_LIVE)
+ return;
+
vfree(mod->arch.syminfo);
mod->arch.syminfo = NULL;
}
@@ -425,7 +429,5 @@ int module_finalize(const Elf_Ehdr *hdr,
struct module *me)
{
jump_label_apply_nops(me);
- vfree(me->arch.syminfo);
- me->arch.syminfo = NULL;
return 0;
}
--
2.4.3
[toc] | [prev] | [next] | [standalone]
| From | Jessica Yu <jeyu@redhat.com> |
|---|---|
| Date | 2016-01-08 20:30 +0100 |
| Subject | [RFC PATCH v3 5/6] samples: livepatch: mark as livepatch module |
| Message-ID | <qOKTw-2Pp-25@gated-at.bofh.it> |
| In reply to | #1304948 |
Mark the module as a livepatch module so that the module loader can
appropriately identify and initialize it.
Signed-off-by: Jessica Yu <jeyu@redhat.com>
---
samples/livepatch/livepatch-sample.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/samples/livepatch/livepatch-sample.c b/samples/livepatch/livepatch-sample.c
index fb8c861..e34f871 100644
--- a/samples/livepatch/livepatch-sample.c
+++ b/samples/livepatch/livepatch-sample.c
@@ -89,3 +89,4 @@ static void livepatch_exit(void)
module_init(livepatch_init);
module_exit(livepatch_exit);
MODULE_LICENSE("GPL");
+MODULE_INFO(livepatch, "Y");
--
2.4.3
[toc] | [prev] | [next] | [standalone]
| From | Jessica Yu <jeyu@redhat.com> |
|---|---|
| Date | 2016-01-08 20:40 +0100 |
| Subject | [RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations |
| Message-ID | <qOL3d-2Tb-33@gated-at.bofh.it> |
| In reply to | #1304948 |
Reuse module loader code to write relocations, thereby eliminating the need
for architecture specific relocation code in livepatch. Namely, we reuse
apply_relocate_add() in the module loader to write relocations instead of
duplicating functionality in livepatch's klp_write_module_reloc(). To apply
relocation sections, remaining SHN_LIVEPATCH symbols referenced by relocs
are resolved and then apply_relocate_add() is called to apply those
relocations.
In addition, remove x86 livepatch relocation code. It is no longer needed
since symbol resolution and relocation work have been offloaded to module
loader.
Signed-off-by: Jessica Yu <jeyu@redhat.com>
---
arch/x86/include/asm/livepatch.h | 2 -
arch/x86/kernel/Makefile | 1 -
arch/x86/kernel/livepatch.c | 70 ---------------------------
include/linux/livepatch.h | 33 +++++--------
kernel/livepatch/core.c | 101 +++++++++++++++++++--------------------
5 files changed, 62 insertions(+), 145 deletions(-)
delete mode 100644 arch/x86/kernel/livepatch.c
diff --git a/arch/x86/include/asm/livepatch.h b/arch/x86/include/asm/livepatch.h
index 19c099a..7312e25 100644
--- a/arch/x86/include/asm/livepatch.h
+++ b/arch/x86/include/asm/livepatch.h
@@ -33,8 +33,6 @@ static inline int klp_check_compiler_support(void)
#endif
return 0;
}
-int klp_write_module_reloc(struct module *mod, unsigned long type,
- unsigned long loc, unsigned long value);
static inline void klp_arch_set_pc(struct pt_regs *regs, unsigned long ip)
{
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index b1b78ff..c5e9a5c 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -67,7 +67,6 @@ obj-$(CONFIG_X86_MPPARSE) += mpparse.o
obj-y += apic/
obj-$(CONFIG_X86_REBOOTFIXUPS) += reboot_fixups_32.o
obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o
-obj-$(CONFIG_LIVEPATCH) += livepatch.o
obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o
obj-$(CONFIG_X86_TSC) += trace_clock.o
diff --git a/arch/x86/kernel/livepatch.c b/arch/x86/kernel/livepatch.c
deleted file mode 100644
index 92fc1a5..0000000
--- a/arch/x86/kernel/livepatch.c
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * livepatch.c - x86-specific Kernel Live Patching Core
- *
- * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
- * Copyright (C) 2014 SUSE
- *
- * 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/>.
- */
-
-#include <linux/module.h>
-#include <linux/uaccess.h>
-#include <asm/elf.h>
-#include <asm/livepatch.h>
-
-/**
- * klp_write_module_reloc() - write a relocation in a module
- * @mod: module in which the section to be modified is found
- * @type: ELF relocation type (see asm/elf.h)
- * @loc: address that the relocation should be written to
- * @value: relocation value (sym address + addend)
- *
- * This function writes a relocation to the specified location for
- * a particular module.
- */
-int klp_write_module_reloc(struct module *mod, unsigned long type,
- unsigned long loc, unsigned long value)
-{
- size_t size = 4;
- unsigned long val;
- unsigned long core = (unsigned long)mod->core_layout.base;
- unsigned long core_size = mod->core_layout.size;
-
- switch (type) {
- case R_X86_64_NONE:
- return 0;
- case R_X86_64_64:
- val = value;
- size = 8;
- break;
- case R_X86_64_32:
- val = (u32)value;
- break;
- case R_X86_64_32S:
- val = (s32)value;
- break;
- case R_X86_64_PC32:
- val = (u32)(value - loc);
- break;
- default:
- /* unsupported relocation type */
- return -EINVAL;
- }
-
- if (loc < core || loc >= core + core_size)
- /* loc does not point to any symbol inside the module */
- return -EINVAL;
-
- return probe_kernel_write((void *)loc, &val, size);
-}
diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h
index a882865..2f12ce7 100644
--- a/include/linux/livepatch.h
+++ b/include/linux/livepatch.h
@@ -65,27 +65,8 @@ struct klp_func {
};
/**
- * struct klp_reloc - relocation structure for live patching
- * @loc: address where the relocation will be written
- * @sympos: position in kallsyms to disambiguate symbols (optional)
- * @type: ELF relocation type
- * @name: name of the referenced symbol (for lookup/verification)
- * @addend: offset from the referenced symbol
- * @external: symbol is either exported or within the live patch module itself
- */
-struct klp_reloc {
- unsigned long loc;
- unsigned long sympos;
- unsigned long type;
- const char *name;
- int addend;
- int external;
-};
-
-/**
* struct klp_object - kernel object structure for live patching
* @name: module name (or NULL for vmlinux)
- * @relocs: relocation entries to be applied at load time
* @funcs: function entries for functions to be patched in the object
* @kobj: kobject for sysfs resources
* @mod: kernel module associated with the patched object
@@ -95,7 +76,6 @@ struct klp_reloc {
struct klp_object {
/* external */
const char *name;
- struct klp_reloc *relocs;
struct klp_func *funcs;
/* internal */
@@ -123,6 +103,19 @@ struct klp_patch {
enum klp_state state;
};
+/*
+ * Livepatch-specific symbols and relocation
+ * sections are prefixed with a tag:
+ * .klp.rel. for relocation sections
+ * .klp.sym. for livepatch symbols
+ */
+#define KLP_TAG_LEN 9
+/*
+ * Livepatch-specific bits for specifying symbol
+ * positions in the Elf_Sym st_other field
+ */
+#define KLP_SYMPOS(o) (o >> 2) & 0xff
+
#define klp_for_each_object(patch, obj) \
for (obj = patch->objs; obj->funcs; obj++)
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index bc2c85c..64536a4 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -28,6 +28,9 @@
#include <linux/list.h>
#include <linux/kallsyms.h>
#include <linux/livepatch.h>
+#include <linux/elf.h>
+#include <linux/string.h>
+#include <linux/moduleloader.h>
#include <asm/cacheflush.h>
/**
@@ -204,74 +207,70 @@ static int klp_find_object_symbol(const char *objname, const char *name,
return -EINVAL;
}
-/*
- * external symbols are located outside the parent object (where the parent
- * object is either vmlinux or the kmod being patched).
- */
-static int klp_find_external_symbol(struct module *pmod, const char *name,
- unsigned long *addr)
+static int klp_resolve_symbols(Elf_Shdr *relsec, struct module *pmod)
{
- const struct kernel_symbol *sym;
+ int i, len, ret = 0;
+ Elf_Rela *relas;
+ Elf_Sym *sym;
+ char *symname, *sym_objname;
- /* first, check if it's an exported symbol */
- preempt_disable();
- sym = find_symbol(name, NULL, NULL, true, true);
- if (sym) {
- *addr = sym->value;
- preempt_enable();
- return 0;
+ relas = (Elf_Rela *) relsec->sh_addr;
+ /* For each rela in this .klp.rel. section */
+ for (i = 0; i < relsec->sh_size / sizeof(Elf_Rela); i++) {
+ sym = pmod->core_symtab + ELF_R_SYM(relas[i].r_info);
+ symname = pmod->core_strtab + sym->st_name;
+
+ len = strcspn(symname + KLP_TAG_LEN, ".");
+ sym_objname = strncmp(symname + KLP_TAG_LEN, "vmlinux", len) ?
+ kstrndup(symname + KLP_TAG_LEN, len, GFP_KERNEL) : NULL;
+ /* .klp.sym.objname.[symbol_name] */
+ symname += KLP_TAG_LEN + len + 1;
+
+ ret = klp_find_object_symbol(sym_objname, symname,
+ KLP_SYMPOS(sym->st_other),
+ (unsigned long *) &sym->st_value);
+ kfree(sym_objname);
+ if (ret)
+ return ret;
}
- preempt_enable();
- /*
- * Check if it's in another .o within the patch module. This also
- * checks that the external symbol is unique.
- */
- return klp_find_object_symbol(pmod->name, name, 0, addr);
+ return ret;
}
static int klp_write_object_relocations(struct module *pmod,
struct klp_object *obj)
{
- int ret = 0;
- unsigned long val;
- struct klp_reloc *reloc;
+ int i, len, ret = 0;
+ char *secname;
+ const char *objname;
if (WARN_ON(!klp_is_object_loaded(obj)))
return -EINVAL;
- if (WARN_ON(!obj->relocs))
- return -EINVAL;
+ objname = klp_is_module(obj) ? obj->name : "vmlinux";
module_disable_ro(pmod);
+ /* For each klp rela section for this object */
+ for (i = 1; i < pmod->info->hdr->e_shnum; i++) {
+ if (!(pmod->info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH))
+ continue;
+
+ secname = pmod->info->secstrings + pmod->info->sechdrs[i].sh_name;
+ /* .klp.rel.[objname].section_name */
+ len = strcspn(secname + KLP_TAG_LEN, ".");
+
+ if (strncmp(objname, secname + KLP_TAG_LEN, len))
+ continue;
- for (reloc = obj->relocs; reloc->name; reloc++) {
- /* discover the address of the referenced symbol */
- if (reloc->external) {
- if (reloc->sympos > 0) {
- pr_err("non-zero sympos for external reloc symbol '%s' is not supported\n",
- reloc->name);
- ret = -EINVAL;
- goto out;
- }
- ret = klp_find_external_symbol(pmod, reloc->name, &val);
- } else
- ret = klp_find_object_symbol(obj->name,
- reloc->name,
- reloc->sympos,
- &val);
+ ret = klp_resolve_symbols(pmod->info->sechdrs + i, pmod);
if (ret)
goto out;
- ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
- val + reloc->addend);
- if (ret) {
- pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
- reloc->name, val, ret);
+ ret = apply_relocate_add(pmod->info->sechdrs, pmod->core_strtab,
+ pmod->info->index.sym, i, pmod);
+ if (ret)
goto out;
- }
}
-
out:
module_enable_ro(pmod);
return ret;
@@ -703,11 +702,9 @@ static int klp_init_object_loaded(struct klp_patch *patch,
struct klp_func *func;
int ret;
- if (obj->relocs) {
- ret = klp_write_object_relocations(patch->mod, obj);
- if (ret)
- return ret;
- }
+ ret = klp_write_object_relocations(patch->mod, obj);
+ if (ret)
+ return ret;
klp_for_each_func(obj, func) {
ret = klp_find_object_symbol(obj->name, func->old_name,
--
2.4.3
[toc] | [prev] | [next] | [standalone]
| From | Petr Mladek <pmladek@suse.com> |
|---|---|
| Date | 2016-01-11 18:00 +0100 |
| Subject | Re: [RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations |
| Message-ID | <qPNZ0-53I-13@gated-at.bofh.it> |
| In reply to | #1304958 |
On Fri 2016-01-08 14:28:22, Jessica Yu wrote:
> Reuse module loader code to write relocations, thereby eliminating the need
> for architecture specific relocation code in livepatch. Namely, we reuse
> apply_relocate_add() in the module loader to write relocations instead of
> duplicating functionality in livepatch's klp_write_module_reloc(). To apply
> relocation sections, remaining SHN_LIVEPATCH symbols referenced by relocs
> are resolved and then apply_relocate_add() is called to apply those
> relocations.
>
> In addition, remove x86 livepatch relocation code. It is no longer needed
> since symbol resolution and relocation work have been offloaded to module
> loader.
>
> --- a/kernel/livepatch/core.c
> +++ b/kernel/livepatch/core.c
> @@ -204,74 +207,70 @@ static int klp_find_object_symbol(const char *objname, const char *name,
> return -EINVAL;
> }
>
> -/*
> - * external symbols are located outside the parent object (where the parent
> - * object is either vmlinux or the kmod being patched).
> - */
> -static int klp_find_external_symbol(struct module *pmod, const char *name,
> - unsigned long *addr)
> +static int klp_resolve_symbols(Elf_Shdr *relsec, struct module *pmod)
> {
> - const struct kernel_symbol *sym;
> + int i, len, ret = 0;
> + Elf_Rela *relas;
> + Elf_Sym *sym;
> + char *symname, *sym_objname;
>
> - /* first, check if it's an exported symbol */
> - preempt_disable();
> - sym = find_symbol(name, NULL, NULL, true, true);
> - if (sym) {
> - *addr = sym->value;
> - preempt_enable();
> - return 0;
> + relas = (Elf_Rela *) relsec->sh_addr;
> + /* For each rela in this .klp.rel. section */
> + for (i = 0; i < relsec->sh_size / sizeof(Elf_Rela); i++) {
> + sym = pmod->core_symtab + ELF_R_SYM(relas[i].r_info);
> + symname = pmod->core_strtab + sym->st_name;
> +
> + len = strcspn(symname + KLP_TAG_LEN, ".");
We should check that len is non-zero. Otherwise, sym_objname might
be empty string and symname might overflow below.
Also we should check that symname really starts with .klp.sym. to
avoid invalid memory access.
> + sym_objname = strncmp(symname + KLP_TAG_LEN, "vmlinux", len) ?
> + kstrndup(symname + KLP_TAG_LEN, len, GFP_KERNEL) : NULL;
> + /* .klp.sym.objname.[symbol_name] */
> + symname += KLP_TAG_LEN + len + 1;
> +
> + ret = klp_find_object_symbol(sym_objname, symname,
> + KLP_SYMPOS(sym->st_other),
> + (unsigned long *) &sym->st_value);
> + kfree(sym_objname);
> + if (ret)
> + return ret;
> }
> - preempt_enable();
>
> - /*
> - * Check if it's in another .o within the patch module. This also
> - * checks that the external symbol is unique.
> - */
> - return klp_find_object_symbol(pmod->name, name, 0, addr);
> + return ret;
> }
>
> static int klp_write_object_relocations(struct module *pmod,
> struct klp_object *obj)
> {
> - int ret = 0;
> - unsigned long val;
> - struct klp_reloc *reloc;
> + int i, len, ret = 0;
> + char *secname;
> + const char *objname;
>
> if (WARN_ON(!klp_is_object_loaded(obj)))
> return -EINVAL;
>
> - if (WARN_ON(!obj->relocs))
> - return -EINVAL;
> + objname = klp_is_module(obj) ? obj->name : "vmlinux";
>
> module_disable_ro(pmod);
> + /* For each klp rela section for this object */
> + for (i = 1; i < pmod->info->hdr->e_shnum; i++) {
> + if (!(pmod->info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH))
> + continue;
> +
> + secname = pmod->info->secstrings + pmod->info->sechdrs[i].sh_name;
> + /* .klp.rel.[objname].section_name */
> + len = strcspn(secname + KLP_TAG_LEN, ".");
We should check that secname realy starts with .klp.rel. string
to avoid access out of memmory.
Otherwise, nice work.
Thanks,
Petr.
> +
> + if (strncmp(objname, secname + KLP_TAG_LEN, len))
> + continue;
>
> - for (reloc = obj->relocs; reloc->name; reloc++) {
> - /* discover the address of the referenced symbol */
> - if (reloc->external) {
> - if (reloc->sympos > 0) {
> - pr_err("non-zero sympos for external reloc symbol '%s' is not supported\n",
> - reloc->name);
> - ret = -EINVAL;
> - goto out;
> - }
> - ret = klp_find_external_symbol(pmod, reloc->name, &val);
> - } else
> - ret = klp_find_object_symbol(obj->name,
> - reloc->name,
> - reloc->sympos,
> - &val);
> + ret = klp_resolve_symbols(pmod->info->sechdrs + i, pmod);
> if (ret)
> goto out;
>
> - ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
> - val + reloc->addend);
> - if (ret) {
> - pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
> - reloc->name, val, ret);
> + ret = apply_relocate_add(pmod->info->sechdrs, pmod->core_strtab,
> + pmod->info->index.sym, i, pmod);
> + if (ret)
> goto out;
> - }
> }
> -
> out:
> module_enable_ro(pmod);
> return ret;
[toc] | [prev] | [next] | [standalone]
| From | Josh Poimboeuf <jpoimboe@redhat.com> |
|---|---|
| Date | 2016-01-11 22:00 +0100 |
| Subject | Re: [RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations |
| Message-ID | <qPRJh-7Ao-23@gated-at.bofh.it> |
| In reply to | #1306511 |
On Mon, Jan 11, 2016 at 05:56:13PM +0100, Petr Mladek wrote:
> On Fri 2016-01-08 14:28:22, Jessica Yu wrote:
> > Reuse module loader code to write relocations, thereby eliminating the need
> > for architecture specific relocation code in livepatch. Namely, we reuse
> > apply_relocate_add() in the module loader to write relocations instead of
> > duplicating functionality in livepatch's klp_write_module_reloc(). To apply
> > relocation sections, remaining SHN_LIVEPATCH symbols referenced by relocs
> > are resolved and then apply_relocate_add() is called to apply those
> > relocations.
> >
> > In addition, remove x86 livepatch relocation code. It is no longer needed
> > since symbol resolution and relocation work have been offloaded to module
> > loader.
> >
> > --- a/kernel/livepatch/core.c
> > +++ b/kernel/livepatch/core.c
> > @@ -204,74 +207,70 @@ static int klp_find_object_symbol(const char *objname, const char *name,
> > return -EINVAL;
> > }
> >
> > -/*
> > - * external symbols are located outside the parent object (where the parent
> > - * object is either vmlinux or the kmod being patched).
> > - */
> > -static int klp_find_external_symbol(struct module *pmod, const char *name,
> > - unsigned long *addr)
> > +static int klp_resolve_symbols(Elf_Shdr *relsec, struct module *pmod)
> > {
> > - const struct kernel_symbol *sym;
> > + int i, len, ret = 0;
> > + Elf_Rela *relas;
> > + Elf_Sym *sym;
> > + char *symname, *sym_objname;
> >
> > - /* first, check if it's an exported symbol */
> > - preempt_disable();
> > - sym = find_symbol(name, NULL, NULL, true, true);
> > - if (sym) {
> > - *addr = sym->value;
> > - preempt_enable();
> > - return 0;
> > + relas = (Elf_Rela *) relsec->sh_addr;
> > + /* For each rela in this .klp.rel. section */
> > + for (i = 0; i < relsec->sh_size / sizeof(Elf_Rela); i++) {
> > + sym = pmod->core_symtab + ELF_R_SYM(relas[i].r_info);
> > + symname = pmod->core_strtab + sym->st_name;
> > +
> > + len = strcspn(symname + KLP_TAG_LEN, ".");
>
> We should check that len is non-zero. Otherwise, sym_objname might
> be empty string and symname might overflow below.
>
> Also we should check that symname really starts with .klp.sym. to
> avoid invalid memory access.
It would also be good to check for SHN_LIVEPATCH.
--
Josh
[toc] | [prev] | [next] | [standalone]
| From | Josh Poimboeuf <jpoimboe@redhat.com> |
|---|---|
| Date | 2016-01-11 22:40 +0100 |
| Subject | Re: [RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations |
| Message-ID | <qPSlY-84V-7@gated-at.bofh.it> |
| In reply to | #1304958 |
On Fri, Jan 08, 2016 at 02:28:22PM -0500, Jessica Yu wrote:
> Reuse module loader code to write relocations, thereby eliminating the need
> for architecture specific relocation code in livepatch. Namely, we reuse
> apply_relocate_add() in the module loader to write relocations instead of
> duplicating functionality in livepatch's klp_write_module_reloc(). To apply
> relocation sections, remaining SHN_LIVEPATCH symbols referenced by relocs
> are resolved and then apply_relocate_add() is called to apply those
> relocations.
>
> In addition, remove x86 livepatch relocation code. It is no longer needed
> since symbol resolution and relocation work have been offloaded to module
> loader.
>
> Signed-off-by: Jessica Yu <jeyu@redhat.com>
> ---
> arch/x86/include/asm/livepatch.h | 2 -
> arch/x86/kernel/Makefile | 1 -
> arch/x86/kernel/livepatch.c | 70 ---------------------------
> include/linux/livepatch.h | 33 +++++--------
> kernel/livepatch/core.c | 101 +++++++++++++++++++--------------------
> 5 files changed, 62 insertions(+), 145 deletions(-)
> delete mode 100644 arch/x86/kernel/livepatch.c
>
> diff --git a/arch/x86/include/asm/livepatch.h b/arch/x86/include/asm/livepatch.h
> index 19c099a..7312e25 100644
> --- a/arch/x86/include/asm/livepatch.h
> +++ b/arch/x86/include/asm/livepatch.h
> @@ -33,8 +33,6 @@ static inline int klp_check_compiler_support(void)
> #endif
> return 0;
> }
> -int klp_write_module_reloc(struct module *mod, unsigned long type,
> - unsigned long loc, unsigned long value);
>
> static inline void klp_arch_set_pc(struct pt_regs *regs, unsigned long ip)
> {
> diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
> index b1b78ff..c5e9a5c 100644
> --- a/arch/x86/kernel/Makefile
> +++ b/arch/x86/kernel/Makefile
> @@ -67,7 +67,6 @@ obj-$(CONFIG_X86_MPPARSE) += mpparse.o
> obj-y += apic/
> obj-$(CONFIG_X86_REBOOTFIXUPS) += reboot_fixups_32.o
> obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o
> -obj-$(CONFIG_LIVEPATCH) += livepatch.o
> obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
> obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o
> obj-$(CONFIG_X86_TSC) += trace_clock.o
> diff --git a/arch/x86/kernel/livepatch.c b/arch/x86/kernel/livepatch.c
> deleted file mode 100644
> index 92fc1a5..0000000
> --- a/arch/x86/kernel/livepatch.c
> +++ /dev/null
> @@ -1,70 +0,0 @@
> -/*
> - * livepatch.c - x86-specific Kernel Live Patching Core
> - *
> - * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
> - * Copyright (C) 2014 SUSE
> - *
> - * 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/>.
> - */
> -
> -#include <linux/module.h>
> -#include <linux/uaccess.h>
> -#include <asm/elf.h>
> -#include <asm/livepatch.h>
> -
> -/**
> - * klp_write_module_reloc() - write a relocation in a module
> - * @mod: module in which the section to be modified is found
> - * @type: ELF relocation type (see asm/elf.h)
> - * @loc: address that the relocation should be written to
> - * @value: relocation value (sym address + addend)
> - *
> - * This function writes a relocation to the specified location for
> - * a particular module.
> - */
> -int klp_write_module_reloc(struct module *mod, unsigned long type,
> - unsigned long loc, unsigned long value)
> -{
> - size_t size = 4;
> - unsigned long val;
> - unsigned long core = (unsigned long)mod->core_layout.base;
> - unsigned long core_size = mod->core_layout.size;
> -
> - switch (type) {
> - case R_X86_64_NONE:
> - return 0;
> - case R_X86_64_64:
> - val = value;
> - size = 8;
> - break;
> - case R_X86_64_32:
> - val = (u32)value;
> - break;
> - case R_X86_64_32S:
> - val = (s32)value;
> - break;
> - case R_X86_64_PC32:
> - val = (u32)(value - loc);
> - break;
> - default:
> - /* unsupported relocation type */
> - return -EINVAL;
> - }
> -
> - if (loc < core || loc >= core + core_size)
> - /* loc does not point to any symbol inside the module */
> - return -EINVAL;
> -
> - return probe_kernel_write((void *)loc, &val, size);
> -}
> diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h
> index a882865..2f12ce7 100644
> --- a/include/linux/livepatch.h
> +++ b/include/linux/livepatch.h
> @@ -65,27 +65,8 @@ struct klp_func {
> };
>
> /**
> - * struct klp_reloc - relocation structure for live patching
> - * @loc: address where the relocation will be written
> - * @sympos: position in kallsyms to disambiguate symbols (optional)
> - * @type: ELF relocation type
> - * @name: name of the referenced symbol (for lookup/verification)
> - * @addend: offset from the referenced symbol
> - * @external: symbol is either exported or within the live patch module itself
> - */
> -struct klp_reloc {
> - unsigned long loc;
> - unsigned long sympos;
> - unsigned long type;
> - const char *name;
> - int addend;
> - int external;
> -};
> -
> -/**
> * struct klp_object - kernel object structure for live patching
> * @name: module name (or NULL for vmlinux)
> - * @relocs: relocation entries to be applied at load time
> * @funcs: function entries for functions to be patched in the object
> * @kobj: kobject for sysfs resources
> * @mod: kernel module associated with the patched object
> @@ -95,7 +76,6 @@ struct klp_reloc {
> struct klp_object {
> /* external */
> const char *name;
> - struct klp_reloc *relocs;
> struct klp_func *funcs;
>
> /* internal */
> @@ -123,6 +103,19 @@ struct klp_patch {
> enum klp_state state;
> };
>
> +/*
> + * Livepatch-specific symbols and relocation
> + * sections are prefixed with a tag:
> + * .klp.rel. for relocation sections
> + * .klp.sym. for livepatch symbols
> + */
> +#define KLP_TAG_LEN 9
> +/*
> + * Livepatch-specific bits for specifying symbol
> + * positions in the Elf_Sym st_other field
> + */
> +#define KLP_SYMPOS(o) (o >> 2) & 0xff
> +
Can st_value be used instead? I think we ended up deciding that would
be better:
https://lkml.kernel.org/g/20151210213328.GA6553@packer-debian-8-amd64.digitalocean.com
Because:
- st_value is easily viewable in readelf
- st_other has some arch-specific uses
And another reason not previously discussed:
- st_other is an unsigned char, which limits sympos to values < 64
> #define klp_for_each_object(patch, obj) \
> for (obj = patch->objs; obj->funcs; obj++)
>
> diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
> index bc2c85c..64536a4 100644
> --- a/kernel/livepatch/core.c
> +++ b/kernel/livepatch/core.c
> @@ -28,6 +28,9 @@
> #include <linux/list.h>
> #include <linux/kallsyms.h>
> #include <linux/livepatch.h>
> +#include <linux/elf.h>
> +#include <linux/string.h>
> +#include <linux/moduleloader.h>
> #include <asm/cacheflush.h>
>
> /**
> @@ -204,74 +207,70 @@ static int klp_find_object_symbol(const char *objname, const char *name,
> return -EINVAL;
> }
>
> -/*
> - * external symbols are located outside the parent object (where the parent
> - * object is either vmlinux or the kmod being patched).
> - */
> -static int klp_find_external_symbol(struct module *pmod, const char *name,
> - unsigned long *addr)
> +static int klp_resolve_symbols(Elf_Shdr *relsec, struct module *pmod)
> {
> - const struct kernel_symbol *sym;
> + int i, len, ret = 0;
> + Elf_Rela *relas;
> + Elf_Sym *sym;
> + char *symname, *sym_objname;
>
> - /* first, check if it's an exported symbol */
> - preempt_disable();
> - sym = find_symbol(name, NULL, NULL, true, true);
> - if (sym) {
> - *addr = sym->value;
> - preempt_enable();
> - return 0;
> + relas = (Elf_Rela *) relsec->sh_addr;
> + /* For each rela in this .klp.rel. section */
> + for (i = 0; i < relsec->sh_size / sizeof(Elf_Rela); i++) {
> + sym = pmod->core_symtab + ELF_R_SYM(relas[i].r_info);
> + symname = pmod->core_strtab + sym->st_name;
> +
> + len = strcspn(symname + KLP_TAG_LEN, ".");
It would be nice to have a comment here similar to the below one:
/* .klp.sym.[objname].symbol_name */
> + sym_objname = strncmp(symname + KLP_TAG_LEN, "vmlinux", len) ?
I think there's a bug here in the (unlikely) case where the module's
name is a subset of "vmlinux", e.g. "vm".
> + kstrndup(symname + KLP_TAG_LEN, len, GFP_KERNEL) : NULL;
> + /* .klp.sym.objname.[symbol_name] */
> + symname += KLP_TAG_LEN + len + 1;
> +
> + ret = klp_find_object_symbol(sym_objname, symname,
> + KLP_SYMPOS(sym->st_other),
> + (unsigned long *) &sym->st_value);
> + kfree(sym_objname);
> + if (ret)
> + return ret;
> }
> - preempt_enable();
>
> - /*
> - * Check if it's in another .o within the patch module. This also
> - * checks that the external symbol is unique.
> - */
> - return klp_find_object_symbol(pmod->name, name, 0, addr);
> + return ret;
> }
>
> static int klp_write_object_relocations(struct module *pmod,
> struct klp_object *obj)
> {
> - int ret = 0;
> - unsigned long val;
> - struct klp_reloc *reloc;
> + int i, len, ret = 0;
> + char *secname;
> + const char *objname;
>
> if (WARN_ON(!klp_is_object_loaded(obj)))
> return -EINVAL;
>
> - if (WARN_ON(!obj->relocs))
> - return -EINVAL;
> + objname = klp_is_module(obj) ? obj->name : "vmlinux";
>
> module_disable_ro(pmod);
> + /* For each klp rela section for this object */
> + for (i = 1; i < pmod->info->hdr->e_shnum; i++) {
> + if (!(pmod->info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH))
> + continue;
> +
> + secname = pmod->info->secstrings + pmod->info->sechdrs[i].sh_name;
> + /* .klp.rel.[objname].section_name */
> + len = strcspn(secname + KLP_TAG_LEN, ".");
> +
> + if (strncmp(objname, secname + KLP_TAG_LEN, len))
> + continue;
Same problem here. For ".klp.rel.foo.sym", an objname of "foobar" will
give a false match.
> - for (reloc = obj->relocs; reloc->name; reloc++) {
> - /* discover the address of the referenced symbol */
> - if (reloc->external) {
> - if (reloc->sympos > 0) {
> - pr_err("non-zero sympos for external reloc symbol '%s' is not supported\n",
> - reloc->name);
> - ret = -EINVAL;
> - goto out;
> - }
> - ret = klp_find_external_symbol(pmod, reloc->name, &val);
> - } else
> - ret = klp_find_object_symbol(obj->name,
> - reloc->name,
> - reloc->sympos,
> - &val);
> + ret = klp_resolve_symbols(pmod->info->sechdrs + i, pmod);
> if (ret)
> goto out;
>
> - ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
> - val + reloc->addend);
> - if (ret) {
> - pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
> - reloc->name, val, ret);
> + ret = apply_relocate_add(pmod->info->sechdrs, pmod->core_strtab,
> + pmod->info->index.sym, i, pmod);
> + if (ret)
> goto out;
> - }
> }
> -
> out:
> module_enable_ro(pmod);
> return ret;
> @@ -703,11 +702,9 @@ static int klp_init_object_loaded(struct klp_patch *patch,
> struct klp_func *func;
> int ret;
>
> - if (obj->relocs) {
> - ret = klp_write_object_relocations(patch->mod, obj);
> - if (ret)
> - return ret;
> - }
> + ret = klp_write_object_relocations(patch->mod, obj);
> + if (ret)
> + return ret;
>
> klp_for_each_func(obj, func) {
> ret = klp_find_object_symbol(obj->name, func->old_name,
> --
> 2.4.3
>
--
Josh
[toc] | [prev] | [next] | [standalone]
| From | Jessica Yu <jeyu@redhat.com> |
|---|---|
| Date | 2016-01-11 23:40 +0100 |
| Subject | Re: livepatch: reuse module loader code to write relocations |
| Message-ID | <qPTi2-n6-13@gated-at.bofh.it> |
| In reply to | #1306718 |
+++ Josh Poimboeuf [11/01/16 15:33 -0600]:
>On Fri, Jan 08, 2016 at 02:28:22PM -0500, Jessica Yu wrote:
>> Reuse module loader code to write relocations, thereby eliminating the need
>> for architecture specific relocation code in livepatch. Namely, we reuse
>> apply_relocate_add() in the module loader to write relocations instead of
>> duplicating functionality in livepatch's klp_write_module_reloc(). To apply
>> relocation sections, remaining SHN_LIVEPATCH symbols referenced by relocs
>> are resolved and then apply_relocate_add() is called to apply those
>> relocations.
>>
>> In addition, remove x86 livepatch relocation code. It is no longer needed
>> since symbol resolution and relocation work have been offloaded to module
>> loader.
>>
>> Signed-off-by: Jessica Yu <jeyu@redhat.com>
>> ---
>> arch/x86/include/asm/livepatch.h | 2 -
>> arch/x86/kernel/Makefile | 1 -
>> arch/x86/kernel/livepatch.c | 70 ---------------------------
>> include/linux/livepatch.h | 33 +++++--------
>> kernel/livepatch/core.c | 101 +++++++++++++++++++--------------------
>> 5 files changed, 62 insertions(+), 145 deletions(-)
>> delete mode 100644 arch/x86/kernel/livepatch.c
>>
>> diff --git a/arch/x86/include/asm/livepatch.h b/arch/x86/include/asm/livepatch.h
>> index 19c099a..7312e25 100644
>> --- a/arch/x86/include/asm/livepatch.h
>> +++ b/arch/x86/include/asm/livepatch.h
>> @@ -33,8 +33,6 @@ static inline int klp_check_compiler_support(void)
>> #endif
>> return 0;
>> }
>> -int klp_write_module_reloc(struct module *mod, unsigned long type,
>> - unsigned long loc, unsigned long value);
>>
>> static inline void klp_arch_set_pc(struct pt_regs *regs, unsigned long ip)
>> {
>> diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
>> index b1b78ff..c5e9a5c 100644
>> --- a/arch/x86/kernel/Makefile
>> +++ b/arch/x86/kernel/Makefile
>> @@ -67,7 +67,6 @@ obj-$(CONFIG_X86_MPPARSE) += mpparse.o
>> obj-y += apic/
>> obj-$(CONFIG_X86_REBOOTFIXUPS) += reboot_fixups_32.o
>> obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o
>> -obj-$(CONFIG_LIVEPATCH) += livepatch.o
>> obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
>> obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o
>> obj-$(CONFIG_X86_TSC) += trace_clock.o
>> diff --git a/arch/x86/kernel/livepatch.c b/arch/x86/kernel/livepatch.c
>> deleted file mode 100644
>> index 92fc1a5..0000000
>> --- a/arch/x86/kernel/livepatch.c
>> +++ /dev/null
>> @@ -1,70 +0,0 @@
>> -/*
>> - * livepatch.c - x86-specific Kernel Live Patching Core
>> - *
>> - * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
>> - * Copyright (C) 2014 SUSE
>> - *
>> - * 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/>.
>> - */
>> -
>> -#include <linux/module.h>
>> -#include <linux/uaccess.h>
>> -#include <asm/elf.h>
>> -#include <asm/livepatch.h>
>> -
>> -/**
>> - * klp_write_module_reloc() - write a relocation in a module
>> - * @mod: module in which the section to be modified is found
>> - * @type: ELF relocation type (see asm/elf.h)
>> - * @loc: address that the relocation should be written to
>> - * @value: relocation value (sym address + addend)
>> - *
>> - * This function writes a relocation to the specified location for
>> - * a particular module.
>> - */
>> -int klp_write_module_reloc(struct module *mod, unsigned long type,
>> - unsigned long loc, unsigned long value)
>> -{
>> - size_t size = 4;
>> - unsigned long val;
>> - unsigned long core = (unsigned long)mod->core_layout.base;
>> - unsigned long core_size = mod->core_layout.size;
>> -
>> - switch (type) {
>> - case R_X86_64_NONE:
>> - return 0;
>> - case R_X86_64_64:
>> - val = value;
>> - size = 8;
>> - break;
>> - case R_X86_64_32:
>> - val = (u32)value;
>> - break;
>> - case R_X86_64_32S:
>> - val = (s32)value;
>> - break;
>> - case R_X86_64_PC32:
>> - val = (u32)(value - loc);
>> - break;
>> - default:
>> - /* unsupported relocation type */
>> - return -EINVAL;
>> - }
>> -
>> - if (loc < core || loc >= core + core_size)
>> - /* loc does not point to any symbol inside the module */
>> - return -EINVAL;
>> -
>> - return probe_kernel_write((void *)loc, &val, size);
>> -}
>> diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h
>> index a882865..2f12ce7 100644
>> --- a/include/linux/livepatch.h
>> +++ b/include/linux/livepatch.h
>> @@ -65,27 +65,8 @@ struct klp_func {
>> };
>>
>> /**
>> - * struct klp_reloc - relocation structure for live patching
>> - * @loc: address where the relocation will be written
>> - * @sympos: position in kallsyms to disambiguate symbols (optional)
>> - * @type: ELF relocation type
>> - * @name: name of the referenced symbol (for lookup/verification)
>> - * @addend: offset from the referenced symbol
>> - * @external: symbol is either exported or within the live patch module itself
>> - */
>> -struct klp_reloc {
>> - unsigned long loc;
>> - unsigned long sympos;
>> - unsigned long type;
>> - const char *name;
>> - int addend;
>> - int external;
>> -};
>> -
>> -/**
>> * struct klp_object - kernel object structure for live patching
>> * @name: module name (or NULL for vmlinux)
>> - * @relocs: relocation entries to be applied at load time
>> * @funcs: function entries for functions to be patched in the object
>> * @kobj: kobject for sysfs resources
>> * @mod: kernel module associated with the patched object
>> @@ -95,7 +76,6 @@ struct klp_reloc {
>> struct klp_object {
>> /* external */
>> const char *name;
>> - struct klp_reloc *relocs;
>> struct klp_func *funcs;
>>
>> /* internal */
>> @@ -123,6 +103,19 @@ struct klp_patch {
>> enum klp_state state;
>> };
>>
>> +/*
>> + * Livepatch-specific symbols and relocation
>> + * sections are prefixed with a tag:
>> + * .klp.rel. for relocation sections
>> + * .klp.sym. for livepatch symbols
>> + */
>> +#define KLP_TAG_LEN 9
>> +/*
>> + * Livepatch-specific bits for specifying symbol
>> + * positions in the Elf_Sym st_other field
>> + */
>> +#define KLP_SYMPOS(o) (o >> 2) & 0xff
>> +
>
>Can st_value be used instead? I think we ended up deciding that would
>be better:
>
> https://lkml.kernel.org/g/20151210213328.GA6553@packer-debian-8-amd64.digitalocean.com
>
>Because:
>
>- st_value is easily viewable in readelf
>- st_other has some arch-specific uses
>
>And another reason not previously discussed:
>
>- st_other is an unsigned char, which limits sympos to values < 64
I originally wanted to encode the symbol position in st_value, but
I've discovered that since st_value is overwritten once the
symbol is (first) resolved, we no longer have the symbol position if we
need to resolve the symbols again (so we wouldn't be able to resolve
them). This could happen when we patch a module that loads and
unloads more than once for example.
I chose st_other since it isn't touched in s390x kernel code nor in
x86 kernel code (it does get used in the x86 userspace reloc tool in
arch/x86/tools, which is why I left the first two bits alone for
ELF_ST_VISIBILITY, and the rest had undefined usage). However this
isn't the best approach and I'm afraid of stepping on other arch's
toes in future patches. Perhaps there is another field where we can
stuff the sympos in? st_size for instance doesn't seem to be touched
anywhere in the module loader. I don't know if I'd want to stuff
sympos in the sym name, there's enough going on there..
Jessica
[toc] | [prev] | [next] | [standalone]
| From | Josh Poimboeuf <jpoimboe@redhat.com> |
|---|---|
| Date | 2016-01-12 04:10 +0100 |
| Subject | Re: livepatch: reuse module loader code to write relocations |
| Message-ID | <qPXvk-3n8-11@gated-at.bofh.it> |
| In reply to | #1306782 |
On Mon, Jan 11, 2016 at 05:35:13PM -0500, Jessica Yu wrote:
> +++ Josh Poimboeuf [11/01/16 15:33 -0600]:
> >On Fri, Jan 08, 2016 at 02:28:22PM -0500, Jessica Yu wrote:
> >>Reuse module loader code to write relocations, thereby eliminating the need
> >>for architecture specific relocation code in livepatch. Namely, we reuse
> >>apply_relocate_add() in the module loader to write relocations instead of
> >>duplicating functionality in livepatch's klp_write_module_reloc(). To apply
> >>relocation sections, remaining SHN_LIVEPATCH symbols referenced by relocs
> >>are resolved and then apply_relocate_add() is called to apply those
> >>relocations.
> >>
> >>In addition, remove x86 livepatch relocation code. It is no longer needed
> >>since symbol resolution and relocation work have been offloaded to module
> >>loader.
> >>
> >>Signed-off-by: Jessica Yu <jeyu@redhat.com>
> >>---
> >> arch/x86/include/asm/livepatch.h | 2 -
> >> arch/x86/kernel/Makefile | 1 -
> >> arch/x86/kernel/livepatch.c | 70 ---------------------------
> >> include/linux/livepatch.h | 33 +++++--------
> >> kernel/livepatch/core.c | 101 +++++++++++++++++++--------------------
> >> 5 files changed, 62 insertions(+), 145 deletions(-)
> >> delete mode 100644 arch/x86/kernel/livepatch.c
> >>
> >>diff --git a/arch/x86/include/asm/livepatch.h b/arch/x86/include/asm/livepatch.h
> >>index 19c099a..7312e25 100644
> >>--- a/arch/x86/include/asm/livepatch.h
> >>+++ b/arch/x86/include/asm/livepatch.h
> >>@@ -33,8 +33,6 @@ static inline int klp_check_compiler_support(void)
> >> #endif
> >> return 0;
> >> }
> >>-int klp_write_module_reloc(struct module *mod, unsigned long type,
> >>- unsigned long loc, unsigned long value);
> >>
> >> static inline void klp_arch_set_pc(struct pt_regs *regs, unsigned long ip)
> >> {
> >>diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
> >>index b1b78ff..c5e9a5c 100644
> >>--- a/arch/x86/kernel/Makefile
> >>+++ b/arch/x86/kernel/Makefile
> >>@@ -67,7 +67,6 @@ obj-$(CONFIG_X86_MPPARSE) += mpparse.o
> >> obj-y += apic/
> >> obj-$(CONFIG_X86_REBOOTFIXUPS) += reboot_fixups_32.o
> >> obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o
> >>-obj-$(CONFIG_LIVEPATCH) += livepatch.o
> >> obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
> >> obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o
> >> obj-$(CONFIG_X86_TSC) += trace_clock.o
> >>diff --git a/arch/x86/kernel/livepatch.c b/arch/x86/kernel/livepatch.c
> >>deleted file mode 100644
> >>index 92fc1a5..0000000
> >>--- a/arch/x86/kernel/livepatch.c
> >>+++ /dev/null
> >>@@ -1,70 +0,0 @@
> >>-/*
> >>- * livepatch.c - x86-specific Kernel Live Patching Core
> >>- *
> >>- * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
> >>- * Copyright (C) 2014 SUSE
> >>- *
> >>- * 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/>.
> >>- */
> >>-
> >>-#include <linux/module.h>
> >>-#include <linux/uaccess.h>
> >>-#include <asm/elf.h>
> >>-#include <asm/livepatch.h>
> >>-
> >>-/**
> >>- * klp_write_module_reloc() - write a relocation in a module
> >>- * @mod: module in which the section to be modified is found
> >>- * @type: ELF relocation type (see asm/elf.h)
> >>- * @loc: address that the relocation should be written to
> >>- * @value: relocation value (sym address + addend)
> >>- *
> >>- * This function writes a relocation to the specified location for
> >>- * a particular module.
> >>- */
> >>-int klp_write_module_reloc(struct module *mod, unsigned long type,
> >>- unsigned long loc, unsigned long value)
> >>-{
> >>- size_t size = 4;
> >>- unsigned long val;
> >>- unsigned long core = (unsigned long)mod->core_layout.base;
> >>- unsigned long core_size = mod->core_layout.size;
> >>-
> >>- switch (type) {
> >>- case R_X86_64_NONE:
> >>- return 0;
> >>- case R_X86_64_64:
> >>- val = value;
> >>- size = 8;
> >>- break;
> >>- case R_X86_64_32:
> >>- val = (u32)value;
> >>- break;
> >>- case R_X86_64_32S:
> >>- val = (s32)value;
> >>- break;
> >>- case R_X86_64_PC32:
> >>- val = (u32)(value - loc);
> >>- break;
> >>- default:
> >>- /* unsupported relocation type */
> >>- return -EINVAL;
> >>- }
> >>-
> >>- if (loc < core || loc >= core + core_size)
> >>- /* loc does not point to any symbol inside the module */
> >>- return -EINVAL;
> >>-
> >>- return probe_kernel_write((void *)loc, &val, size);
> >>-}
> >>diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h
> >>index a882865..2f12ce7 100644
> >>--- a/include/linux/livepatch.h
> >>+++ b/include/linux/livepatch.h
> >>@@ -65,27 +65,8 @@ struct klp_func {
> >> };
> >>
> >> /**
> >>- * struct klp_reloc - relocation structure for live patching
> >>- * @loc: address where the relocation will be written
> >>- * @sympos: position in kallsyms to disambiguate symbols (optional)
> >>- * @type: ELF relocation type
> >>- * @name: name of the referenced symbol (for lookup/verification)
> >>- * @addend: offset from the referenced symbol
> >>- * @external: symbol is either exported or within the live patch module itself
> >>- */
> >>-struct klp_reloc {
> >>- unsigned long loc;
> >>- unsigned long sympos;
> >>- unsigned long type;
> >>- const char *name;
> >>- int addend;
> >>- int external;
> >>-};
> >>-
> >>-/**
> >> * struct klp_object - kernel object structure for live patching
> >> * @name: module name (or NULL for vmlinux)
> >>- * @relocs: relocation entries to be applied at load time
> >> * @funcs: function entries for functions to be patched in the object
> >> * @kobj: kobject for sysfs resources
> >> * @mod: kernel module associated with the patched object
> >>@@ -95,7 +76,6 @@ struct klp_reloc {
> >> struct klp_object {
> >> /* external */
> >> const char *name;
> >>- struct klp_reloc *relocs;
> >> struct klp_func *funcs;
> >>
> >> /* internal */
> >>@@ -123,6 +103,19 @@ struct klp_patch {
> >> enum klp_state state;
> >> };
> >>
> >>+/*
> >>+ * Livepatch-specific symbols and relocation
> >>+ * sections are prefixed with a tag:
> >>+ * .klp.rel. for relocation sections
> >>+ * .klp.sym. for livepatch symbols
> >>+ */
> >>+#define KLP_TAG_LEN 9
> >>+/*
> >>+ * Livepatch-specific bits for specifying symbol
> >>+ * positions in the Elf_Sym st_other field
> >>+ */
> >>+#define KLP_SYMPOS(o) (o >> 2) & 0xff
> >>+
> >
> >Can st_value be used instead? I think we ended up deciding that would
> >be better:
> >
> > https://lkml.kernel.org/g/20151210213328.GA6553@packer-debian-8-amd64.digitalocean.com
> >
> >Because:
> >
> >- st_value is easily viewable in readelf
> >- st_other has some arch-specific uses
> >
> >And another reason not previously discussed:
> >
> >- st_other is an unsigned char, which limits sympos to values < 64
>
> I originally wanted to encode the symbol position in st_value, but
> I've discovered that since st_value is overwritten once the
> symbol is (first) resolved, we no longer have the symbol position if we
> need to resolve the symbols again (so we wouldn't be able to resolve
> them). This could happen when we patch a module that loads and
> unloads more than once for example.
Ah, good point.
> I chose st_other since it isn't touched in s390x kernel code nor in
> x86 kernel code (it does get used in the x86 userspace reloc tool in
> arch/x86/tools, which is why I left the first two bits alone for
> ELF_ST_VISIBILITY, and the rest had undefined usage). However this
> isn't the best approach and I'm afraid of stepping on other arch's
> toes in future patches. Perhaps there is another field where we can
> stuff the sympos in? st_size for instance doesn't seem to be touched
> anywhere in the module loader. I don't know if I'd want to stuff
> sympos in the sym name, there's enough going on there..
I think st_other still isn't going to work because of possible arch
conflicts and because of its small size (6 bits) otherwise.
st_size could work. It doesn't _seem_ to be used by the module code,
though it's hard to confirm that for all arches. But it makes me a
little nervous to override that field. For example, might it confuse
some user-space tools out there, or the linker?
Maybe encoding it in the sym name is the safest bet. Why not, we've
already got a bunch of other stuff there anyway :-) One idea would be
to append it with ',<sympos>' to be consistent with the sysfs entries:
.klp.sym.vmlinux.symname,1
--
Josh
[toc] | [prev] | [next] | [standalone]
| From | Petr Mladek <pmladek@suse.com> |
|---|---|
| Date | 2016-01-12 10:20 +0100 |
| Subject | Re: livepatch: reuse module loader code to write relocations |
| Message-ID | <qQ3hn-7jI-1@gated-at.bofh.it> |
| In reply to | #1306953 |
On Mon 2016-01-11 21:05:52, Josh Poimboeuf wrote:
> On Mon, Jan 11, 2016 at 05:35:13PM -0500, Jessica Yu wrote:
> > +++ Josh Poimboeuf [11/01/16 15:33 -0600]:
> > >On Fri, Jan 08, 2016 at 02:28:22PM -0500, Jessica Yu wrote:
> > >>Reuse module loader code to write relocations, thereby eliminating the need
> > >>for architecture specific relocation code in livepatch. Namely, we reuse
> > >>apply_relocate_add() in the module loader to write relocations instead of
> > >>duplicating functionality in livepatch's klp_write_module_reloc(). To apply
> > >>relocation sections, remaining SHN_LIVEPATCH symbols referenced by relocs
> > >>are resolved and then apply_relocate_add() is called to apply those
> > >>relocations.
> > >>
> > >>In addition, remove x86 livepatch relocation code. It is no longer needed
> > >>since symbol resolution and relocation work have been offloaded to module
> > >>loader.
> > >>
> > >>Signed-off-by: Jessica Yu <jeyu@redhat.com>
> > >>---
> > >> arch/x86/include/asm/livepatch.h | 2 -
> > >> arch/x86/kernel/Makefile | 1 -
> > >> arch/x86/kernel/livepatch.c | 70 ---------------------------
> > >> include/linux/livepatch.h | 33 +++++--------
> > >> kernel/livepatch/core.c | 101 +++++++++++++++++++--------------------
> > >> 5 files changed, 62 insertions(+), 145 deletions(-)
> > >> delete mode 100644 arch/x86/kernel/livepatch.c
> > >>
> > >>diff --git a/arch/x86/include/asm/livepatch.h b/arch/x86/include/asm/livepatch.h
> > >>index 19c099a..7312e25 100644
> > >>--- a/arch/x86/include/asm/livepatch.h
> > >>+++ b/arch/x86/include/asm/livepatch.h
> > >>@@ -33,8 +33,6 @@ static inline int klp_check_compiler_support(void)
> > >> #endif
> > >> return 0;
> > >> }
> > >>-int klp_write_module_reloc(struct module *mod, unsigned long type,
> > >>- unsigned long loc, unsigned long value);
> > >>
> > >> static inline void klp_arch_set_pc(struct pt_regs *regs, unsigned long ip)
> > >> {
> > >>diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
> > >>index b1b78ff..c5e9a5c 100644
> > >>--- a/arch/x86/kernel/Makefile
> > >>+++ b/arch/x86/kernel/Makefile
> > >>@@ -67,7 +67,6 @@ obj-$(CONFIG_X86_MPPARSE) += mpparse.o
> > >> obj-y += apic/
> > >> obj-$(CONFIG_X86_REBOOTFIXUPS) += reboot_fixups_32.o
> > >> obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o
> > >>-obj-$(CONFIG_LIVEPATCH) += livepatch.o
> > >> obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
> > >> obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o
> > >> obj-$(CONFIG_X86_TSC) += trace_clock.o
> > >>diff --git a/arch/x86/kernel/livepatch.c b/arch/x86/kernel/livepatch.c
> > >>deleted file mode 100644
> > >>index 92fc1a5..0000000
> > >>--- a/arch/x86/kernel/livepatch.c
> > >>+++ /dev/null
> > >>@@ -1,70 +0,0 @@
> > >>-/*
> > >>- * livepatch.c - x86-specific Kernel Live Patching Core
> > >>- *
> > >>- * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
> > >>- * Copyright (C) 2014 SUSE
> > >>- *
> > >>- * 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/>.
> > >>- */
> > >>-
> > >>-#include <linux/module.h>
> > >>-#include <linux/uaccess.h>
> > >>-#include <asm/elf.h>
> > >>-#include <asm/livepatch.h>
> > >>-
> > >>-/**
> > >>- * klp_write_module_reloc() - write a relocation in a module
> > >>- * @mod: module in which the section to be modified is found
> > >>- * @type: ELF relocation type (see asm/elf.h)
> > >>- * @loc: address that the relocation should be written to
> > >>- * @value: relocation value (sym address + addend)
> > >>- *
> > >>- * This function writes a relocation to the specified location for
> > >>- * a particular module.
> > >>- */
> > >>-int klp_write_module_reloc(struct module *mod, unsigned long type,
> > >>- unsigned long loc, unsigned long value)
> > >>-{
> > >>- size_t size = 4;
> > >>- unsigned long val;
> > >>- unsigned long core = (unsigned long)mod->core_layout.base;
> > >>- unsigned long core_size = mod->core_layout.size;
> > >>-
> > >>- switch (type) {
> > >>- case R_X86_64_NONE:
> > >>- return 0;
> > >>- case R_X86_64_64:
> > >>- val = value;
> > >>- size = 8;
> > >>- break;
> > >>- case R_X86_64_32:
> > >>- val = (u32)value;
> > >>- break;
> > >>- case R_X86_64_32S:
> > >>- val = (s32)value;
> > >>- break;
> > >>- case R_X86_64_PC32:
> > >>- val = (u32)(value - loc);
> > >>- break;
> > >>- default:
> > >>- /* unsupported relocation type */
> > >>- return -EINVAL;
> > >>- }
> > >>-
> > >>- if (loc < core || loc >= core + core_size)
> > >>- /* loc does not point to any symbol inside the module */
> > >>- return -EINVAL;
> > >>-
> > >>- return probe_kernel_write((void *)loc, &val, size);
> > >>-}
> > >>diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h
> > >>index a882865..2f12ce7 100644
> > >>--- a/include/linux/livepatch.h
> > >>+++ b/include/linux/livepatch.h
> > >>@@ -65,27 +65,8 @@ struct klp_func {
> > >> };
> > >>
> > >> /**
> > >>- * struct klp_reloc - relocation structure for live patching
> > >>- * @loc: address where the relocation will be written
> > >>- * @sympos: position in kallsyms to disambiguate symbols (optional)
> > >>- * @type: ELF relocation type
> > >>- * @name: name of the referenced symbol (for lookup/verification)
> > >>- * @addend: offset from the referenced symbol
> > >>- * @external: symbol is either exported or within the live patch module itself
> > >>- */
> > >>-struct klp_reloc {
> > >>- unsigned long loc;
> > >>- unsigned long sympos;
> > >>- unsigned long type;
> > >>- const char *name;
> > >>- int addend;
> > >>- int external;
> > >>-};
> > >>-
> > >>-/**
> > >> * struct klp_object - kernel object structure for live patching
> > >> * @name: module name (or NULL for vmlinux)
> > >>- * @relocs: relocation entries to be applied at load time
> > >> * @funcs: function entries for functions to be patched in the object
> > >> * @kobj: kobject for sysfs resources
> > >> * @mod: kernel module associated with the patched object
> > >>@@ -95,7 +76,6 @@ struct klp_reloc {
> > >> struct klp_object {
> > >> /* external */
> > >> const char *name;
> > >>- struct klp_reloc *relocs;
> > >> struct klp_func *funcs;
> > >>
> > >> /* internal */
> > >>@@ -123,6 +103,19 @@ struct klp_patch {
> > >> enum klp_state state;
> > >> };
> > >>
> > >>+/*
> > >>+ * Livepatch-specific symbols and relocation
> > >>+ * sections are prefixed with a tag:
> > >>+ * .klp.rel. for relocation sections
> > >>+ * .klp.sym. for livepatch symbols
> > >>+ */
> > >>+#define KLP_TAG_LEN 9
> > >>+/*
> > >>+ * Livepatch-specific bits for specifying symbol
> > >>+ * positions in the Elf_Sym st_other field
> > >>+ */
> > >>+#define KLP_SYMPOS(o) (o >> 2) & 0xff
> > >>+
> > >Can st_value be used instead? I think we ended up deciding that would
> > >be better:
>
> Maybe encoding it in the sym name is the safest bet. Why not, we've
> already got a bunch of other stuff there anyway :-) One idea would be
> to append it with ',<sympos>' to be consistent with the sysfs entries:
>
> .klp.sym.vmlinux.symname,1
This looks like the most safe solution to me as well.
Best Regards,
Petr
[toc] | [prev] | [next] | [standalone]
| From | Jessica Yu <jeyu@redhat.com> |
|---|---|
| Date | 2016-01-14 06:10 +0100 |
| Subject | Re: livepatch: reuse module loader code to write relocations |
| Message-ID | <qQIkz-29Y-23@gated-at.bofh.it> |
| In reply to | #1306953 |
+++ Josh Poimboeuf [11/01/16 21:05 -0600]:
>On Mon, Jan 11, 2016 at 05:35:13PM -0500, Jessica Yu wrote:
>> +++ Josh Poimboeuf [11/01/16 15:33 -0600]:
>> >On Fri, Jan 08, 2016 at 02:28:22PM -0500, Jessica Yu wrote:
>> >>Reuse module loader code to write relocations, thereby eliminating the need
>> >>for architecture specific relocation code in livepatch. Namely, we reuse
>> >>apply_relocate_add() in the module loader to write relocations instead of
>> >>duplicating functionality in livepatch's klp_write_module_reloc(). To apply
>> >>relocation sections, remaining SHN_LIVEPATCH symbols referenced by relocs
>> >>are resolved and then apply_relocate_add() is called to apply those
>> >>relocations.
>> >>
>> >>In addition, remove x86 livepatch relocation code. It is no longer needed
>> >>since symbol resolution and relocation work have been offloaded to module
>> >>loader.
>> >>
>> >>Signed-off-by: Jessica Yu <jeyu@redhat.com>
>> >>---
>> >> arch/x86/include/asm/livepatch.h | 2 -
>> >> arch/x86/kernel/Makefile | 1 -
>> >> arch/x86/kernel/livepatch.c | 70 ---------------------------
>> >> include/linux/livepatch.h | 33 +++++--------
>> >> kernel/livepatch/core.c | 101 +++++++++++++++++++--------------------
>> >> 5 files changed, 62 insertions(+), 145 deletions(-)
>> >> delete mode 100644 arch/x86/kernel/livepatch.c
>> >>
>> >>diff --git a/arch/x86/include/asm/livepatch.h b/arch/x86/include/asm/livepatch.h
>> >>index 19c099a..7312e25 100644
>> >>--- a/arch/x86/include/asm/livepatch.h
>> >>+++ b/arch/x86/include/asm/livepatch.h
>> >>@@ -33,8 +33,6 @@ static inline int klp_check_compiler_support(void)
>> >> #endif
>> >> return 0;
>> >> }
>> >>-int klp_write_module_reloc(struct module *mod, unsigned long type,
>> >>- unsigned long loc, unsigned long value);
>> >>
>> >> static inline void klp_arch_set_pc(struct pt_regs *regs, unsigned long ip)
>> >> {
>> >>diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
>> >>index b1b78ff..c5e9a5c 100644
>> >>--- a/arch/x86/kernel/Makefile
>> >>+++ b/arch/x86/kernel/Makefile
>> >>@@ -67,7 +67,6 @@ obj-$(CONFIG_X86_MPPARSE) += mpparse.o
>> >> obj-y += apic/
>> >> obj-$(CONFIG_X86_REBOOTFIXUPS) += reboot_fixups_32.o
>> >> obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o
>> >>-obj-$(CONFIG_LIVEPATCH) += livepatch.o
>> >> obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
>> >> obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o
>> >> obj-$(CONFIG_X86_TSC) += trace_clock.o
>> >>diff --git a/arch/x86/kernel/livepatch.c b/arch/x86/kernel/livepatch.c
>> >>deleted file mode 100644
>> >>index 92fc1a5..0000000
>> >>--- a/arch/x86/kernel/livepatch.c
>> >>+++ /dev/null
>> >>@@ -1,70 +0,0 @@
>> >>-/*
>> >>- * livepatch.c - x86-specific Kernel Live Patching Core
>> >>- *
>> >>- * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
>> >>- * Copyright (C) 2014 SUSE
>> >>- *
>> >>- * 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/>.
>> >>- */
>> >>-
>> >>-#include <linux/module.h>
>> >>-#include <linux/uaccess.h>
>> >>-#include <asm/elf.h>
>> >>-#include <asm/livepatch.h>
>> >>-
>> >>-/**
>> >>- * klp_write_module_reloc() - write a relocation in a module
>> >>- * @mod: module in which the section to be modified is found
>> >>- * @type: ELF relocation type (see asm/elf.h)
>> >>- * @loc: address that the relocation should be written to
>> >>- * @value: relocation value (sym address + addend)
>> >>- *
>> >>- * This function writes a relocation to the specified location for
>> >>- * a particular module.
>> >>- */
>> >>-int klp_write_module_reloc(struct module *mod, unsigned long type,
>> >>- unsigned long loc, unsigned long value)
>> >>-{
>> >>- size_t size = 4;
>> >>- unsigned long val;
>> >>- unsigned long core = (unsigned long)mod->core_layout.base;
>> >>- unsigned long core_size = mod->core_layout.size;
>> >>-
>> >>- switch (type) {
>> >>- case R_X86_64_NONE:
>> >>- return 0;
>> >>- case R_X86_64_64:
>> >>- val = value;
>> >>- size = 8;
>> >>- break;
>> >>- case R_X86_64_32:
>> >>- val = (u32)value;
>> >>- break;
>> >>- case R_X86_64_32S:
>> >>- val = (s32)value;
>> >>- break;
>> >>- case R_X86_64_PC32:
>> >>- val = (u32)(value - loc);
>> >>- break;
>> >>- default:
>> >>- /* unsupported relocation type */
>> >>- return -EINVAL;
>> >>- }
>> >>-
>> >>- if (loc < core || loc >= core + core_size)
>> >>- /* loc does not point to any symbol inside the module */
>> >>- return -EINVAL;
>> >>-
>> >>- return probe_kernel_write((void *)loc, &val, size);
>> >>-}
>> >>diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h
>> >>index a882865..2f12ce7 100644
>> >>--- a/include/linux/livepatch.h
>> >>+++ b/include/linux/livepatch.h
>> >>@@ -65,27 +65,8 @@ struct klp_func {
>> >> };
>> >>
>> >> /**
>> >>- * struct klp_reloc - relocation structure for live patching
>> >>- * @loc: address where the relocation will be written
>> >>- * @sympos: position in kallsyms to disambiguate symbols (optional)
>> >>- * @type: ELF relocation type
>> >>- * @name: name of the referenced symbol (for lookup/verification)
>> >>- * @addend: offset from the referenced symbol
>> >>- * @external: symbol is either exported or within the live patch module itself
>> >>- */
>> >>-struct klp_reloc {
>> >>- unsigned long loc;
>> >>- unsigned long sympos;
>> >>- unsigned long type;
>> >>- const char *name;
>> >>- int addend;
>> >>- int external;
>> >>-};
>> >>-
>> >>-/**
>> >> * struct klp_object - kernel object structure for live patching
>> >> * @name: module name (or NULL for vmlinux)
>> >>- * @relocs: relocation entries to be applied at load time
>> >> * @funcs: function entries for functions to be patched in the object
>> >> * @kobj: kobject for sysfs resources
>> >> * @mod: kernel module associated with the patched object
>> >>@@ -95,7 +76,6 @@ struct klp_reloc {
>> >> struct klp_object {
>> >> /* external */
>> >> const char *name;
>> >>- struct klp_reloc *relocs;
>> >> struct klp_func *funcs;
>> >>
>> >> /* internal */
>> >>@@ -123,6 +103,19 @@ struct klp_patch {
>> >> enum klp_state state;
>> >> };
>> >>
>> >>+/*
>> >>+ * Livepatch-specific symbols and relocation
>> >>+ * sections are prefixed with a tag:
>> >>+ * .klp.rel. for relocation sections
>> >>+ * .klp.sym. for livepatch symbols
>> >>+ */
>> >>+#define KLP_TAG_LEN 9
>> >>+/*
>> >>+ * Livepatch-specific bits for specifying symbol
>> >>+ * positions in the Elf_Sym st_other field
>> >>+ */
>> >>+#define KLP_SYMPOS(o) (o >> 2) & 0xff
>> >>+
>> >
>> >Can st_value be used instead? I think we ended up deciding that would
>> >be better:
>> >
>> > https://lkml.kernel.org/g/20151210213328.GA6553@packer-debian-8-amd64.digitalocean.com
>> >
>> >Because:
>> >
>> >- st_value is easily viewable in readelf
>> >- st_other has some arch-specific uses
>> >
>> >And another reason not previously discussed:
>> >
>> >- st_other is an unsigned char, which limits sympos to values < 64
>>
>> I originally wanted to encode the symbol position in st_value, but
>> I've discovered that since st_value is overwritten once the
>> symbol is (first) resolved, we no longer have the symbol position if we
>> need to resolve the symbols again (so we wouldn't be able to resolve
>> them). This could happen when we patch a module that loads and
>> unloads more than once for example.
>
>Ah, good point.
>
>> I chose st_other since it isn't touched in s390x kernel code nor in
>> x86 kernel code (it does get used in the x86 userspace reloc tool in
>> arch/x86/tools, which is why I left the first two bits alone for
>> ELF_ST_VISIBILITY, and the rest had undefined usage). However this
>> isn't the best approach and I'm afraid of stepping on other arch's
>> toes in future patches. Perhaps there is another field where we can
>> stuff the sympos in? st_size for instance doesn't seem to be touched
>> anywhere in the module loader. I don't know if I'd want to stuff
>> sympos in the sym name, there's enough going on there..
>
>I think st_other still isn't going to work because of possible arch
>conflicts and because of its small size (6 bits) otherwise.
>
>st_size could work. It doesn't _seem_ to be used by the module code,
>though it's hard to confirm that for all arches. But it makes me a
>little nervous to override that field. For example, might it confuse
>some user-space tools out there, or the linker?
>
>Maybe encoding it in the sym name is the safest bet. Why not, we've
>already got a bunch of other stuff there anyway :-) One idea would be
>to append it with ',<sympos>' to be consistent with the sysfs entries:
>
> .klp.sym.vmlinux.symname,1
Sure, as long as commas aren't allowed in symbol names, that will work. :-)
[toc] | [prev] | [next] | [standalone]
| From | Miroslav Benes <mbenes@suse.cz> |
|---|---|
| Date | 2016-01-12 17:50 +0100 |
| Subject | Re: [RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations |
| Message-ID | <qQaiS-3xk-13@gated-at.bofh.it> |
| In reply to | #1304958 |
Hi Jessica,
I walked through the series and it looks really nice. Others have already
pointed out the issues I also found, so only few minor things below.
First thing, could you copy&paste the information and reasoning from the
cover letter to the changelogs where appropriate? It is very detailed and
it would be a pity to lost it.
On Fri, 8 Jan 2016, Jessica Yu wrote:
> diff --git a/arch/x86/include/asm/livepatch.h b/arch/x86/include/asm/livepatch.h
> index 19c099a..7312e25 100644
> --- a/arch/x86/include/asm/livepatch.h
> +++ b/arch/x86/include/asm/livepatch.h
> @@ -33,8 +33,6 @@ static inline int klp_check_compiler_support(void)
> #endif
> return 0;
> }
> -int klp_write_module_reloc(struct module *mod, unsigned long type,
> - unsigned long loc, unsigned long value);
You left klp_write_module_reloc() in arch/s390/include/asm/livepatch.h I'm
afraid. Anyway it would be really great if you managed to test the series
on s390 somehow. Just to know that all the roadblocks are really gone.
> -/*
> - * external symbols are located outside the parent object (where the parent
> - * object is either vmlinux or the kmod being patched).
> - */
> -static int klp_find_external_symbol(struct module *pmod, const char *name,
> - unsigned long *addr)
> +static int klp_resolve_symbols(Elf_Shdr *relsec, struct module *pmod)
> {
> - const struct kernel_symbol *sym;
> + int i, len, ret = 0;
> + Elf_Rela *relas;
> + Elf_Sym *sym;
> + char *symname, *sym_objname;
>
> - /* first, check if it's an exported symbol */
> - preempt_disable();
> - sym = find_symbol(name, NULL, NULL, true, true);
> - if (sym) {
> - *addr = sym->value;
> - preempt_enable();
> - return 0;
> + relas = (Elf_Rela *) relsec->sh_addr;
> + /* For each rela in this .klp.rel. section */
> + for (i = 0; i < relsec->sh_size / sizeof(Elf_Rela); i++) {
> + sym = pmod->core_symtab + ELF_R_SYM(relas[i].r_info);
> + symname = pmod->core_strtab + sym->st_name;
Maybe it would be better to use pmod->symtab and pmod->strtab everywhere.
It should be the same, but core_* versions are only helpers used in
load_module and friends. There is even a comment in
include/linux/module.h.
/*
* We keep the symbol and string tables for kallsyms.
* The core_* fields below are temporary, loader-only (they
* could really be discarded after module init).
*/
We should respect that.
Thanks,
Miroslav
[toc] | [prev] | [next] | [standalone]
| From | Jessica Yu <jeyu@redhat.com> |
|---|---|
| Date | 2016-01-14 05:00 +0100 |
| Subject | Re: livepatch: reuse module loader code to write relocations |
| Message-ID | <qQHeO-1eM-9@gated-at.bofh.it> |
| In reply to | #1307621 |
+++ Miroslav Benes [12/01/16 17:40 +0100]:
>
>Hi Jessica,
>
>I walked through the series and it looks really nice. Others have already
>pointed out the issues I also found, so only few minor things below.
>
>First thing, could you copy&paste the information and reasoning from the
>cover letter to the changelogs where appropriate? It is very detailed and
>it would be a pity to lost it.
Thanks Miroslav! I'll do that.
>On Fri, 8 Jan 2016, Jessica Yu wrote:
>
>> diff --git a/arch/x86/include/asm/livepatch.h b/arch/x86/include/asm/livepatch.h
>> index 19c099a..7312e25 100644
>> --- a/arch/x86/include/asm/livepatch.h
>> +++ b/arch/x86/include/asm/livepatch.h
>> @@ -33,8 +33,6 @@ static inline int klp_check_compiler_support(void)
>> #endif
>> return 0;
>> }
>> -int klp_write_module_reloc(struct module *mod, unsigned long type,
>> - unsigned long loc, unsigned long value);
>
>You left klp_write_module_reloc() in arch/s390/include/asm/livepatch.h I'm
>afraid. Anyway it would be really great if you managed to test the series
>on s390 somehow. Just to know that all the roadblocks are really gone.
Ah, thanks for catching that. I will also try testing the patchset on
s390x and report back.
>> -/*
>> - * external symbols are located outside the parent object (where the parent
>> - * object is either vmlinux or the kmod being patched).
>> - */
>> -static int klp_find_external_symbol(struct module *pmod, const char *name,
>> - unsigned long *addr)
>> +static int klp_resolve_symbols(Elf_Shdr *relsec, struct module *pmod)
>> {
>> - const struct kernel_symbol *sym;
>> + int i, len, ret = 0;
>> + Elf_Rela *relas;
>> + Elf_Sym *sym;
>> + char *symname, *sym_objname;
>>
>> - /* first, check if it's an exported symbol */
>> - preempt_disable();
>> - sym = find_symbol(name, NULL, NULL, true, true);
>> - if (sym) {
>> - *addr = sym->value;
>> - preempt_enable();
>> - return 0;
>> + relas = (Elf_Rela *) relsec->sh_addr;
>> + /* For each rela in this .klp.rel. section */
>> + for (i = 0; i < relsec->sh_size / sizeof(Elf_Rela); i++) {
>> + sym = pmod->core_symtab + ELF_R_SYM(relas[i].r_info);
>> + symname = pmod->core_strtab + sym->st_name;
>
>Maybe it would be better to use pmod->symtab and pmod->strtab everywhere.
>It should be the same, but core_* versions are only helpers used in
>load_module and friends. There is even a comment in
>include/linux/module.h.
>
> /*
> * We keep the symbol and string tables for kallsyms.
> * The core_* fields below are temporary, loader-only (they
> * could really be discarded after module init).
> */
>
>We should respect that.
I admit I'm a bit confused by the comment, I can't seem to find where
core_symtab and core_strtab are purportedly discarded after module
init (perhaps I'm missing something?). IMO it sounds more like it's
describing mod->symtab and mod->strtab instead, because these are in
module init memory and are freed later. In any case, my reason for using
core_symtab is that the original symbol table (mod->symtab) is marked
with INIT_OFFSET_MASK in layout_symtab() (see kernel/module.c), and is
therefore in init memory. This memory is freed near the end of
do_init_module() with do_free_init(). Since core_symtab is in module core
memory, for livepatch modules I simply used core_symtab to hold a
full copy of the symbol table instead of the slimmed down version that
it was originally intended to hold.
Alternatively, we can tweak layout_symtab() to *not* mark the symtab
with INIT_OFFSET_MASK and put it in core memory instead. I think
either way will work, but maybe it is cleaner to do it this way
instead.
Jessica
[toc] | [prev] | [next] | [standalone]
| From | Miroslav Benes <mbenes@suse.cz> |
|---|---|
| Date | 2016-01-14 10:10 +0100 |
| Subject | Re: livepatch: reuse module loader code to write relocations |
| Message-ID | <qQM4O-4UH-11@gated-at.bofh.it> |
| In reply to | #1308985 |
On Wed, 13 Jan 2016, Jessica Yu wrote: > > Maybe it would be better to use pmod->symtab and pmod->strtab everywhere. > > It should be the same, but core_* versions are only helpers used in > > load_module and friends. There is even a comment in > > include/linux/module.h. > > > > /* > > * We keep the symbol and string tables for kallsyms. > > * The core_* fields below are temporary, loader-only (they > > * could really be discarded after module init). > > */ > > > > We should respect that. > > I admit I'm a bit confused by the comment, I can't seem to find where > core_symtab and core_strtab are purportedly discarded after module > init (perhaps I'm missing something?). IMO it sounds more like it's > describing mod->symtab and mod->strtab instead, because these are in > module init memory and are freed later. I think it just says that core_* symbols are used as temporary tables during module loading. They are not discarded anywhere but they could be (and maybe they'll be in the future). So it is better not to depend on them. > In any case, my reason for using > core_symtab is that the original symbol table (mod->symtab) is marked > with INIT_OFFSET_MASK in layout_symtab() (see kernel/module.c), and is > therefore in init memory. This memory is freed near the end of > do_init_module() with do_free_init(). Since core_symtab is in module core > memory, for livepatch modules I simply used core_symtab to hold a > full copy of the symbol table instead of the slimmed down version that > it was originally intended to hold. But both mod->symtab and mod->strtab are changed to point to their core_ versions right before do_free_init is called in do_init_module. So they should be the same. My remark was more of an academic question. I believe it is not a functional thing, just the matter of taste. But maybe I am missing something. > Alternatively, we can tweak layout_symtab() to *not* mark the symtab > with INIT_OFFSET_MASK and put it in core memory instead. I think > either way will work, but maybe it is cleaner to do it this way > instead. Yeah, I wouldn't do this. core_* symbols are ok from functional point of view. Thanks, Miroslav
[toc] | [prev] | [next] | [standalone]
| From | Miroslav Benes <mbenes@suse.cz> |
|---|---|
| Date | 2016-01-13 10:20 +0100 |
| Subject | Re: [RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations |
| Message-ID | <qQpKW-60h-3@gated-at.bofh.it> |
| In reply to | #1304958 |
On Fri, 8 Jan 2016, Jessica Yu wrote:
> static int klp_write_object_relocations(struct module *pmod,
> struct klp_object *obj)
> {
> - int ret = 0;
> - unsigned long val;
> - struct klp_reloc *reloc;
> + int i, len, ret = 0;
> + char *secname;
> + const char *objname;
>
> if (WARN_ON(!klp_is_object_loaded(obj)))
> return -EINVAL;
>
> - if (WARN_ON(!obj->relocs))
> - return -EINVAL;
> + objname = klp_is_module(obj) ? obj->name : "vmlinux";
>
> module_disable_ro(pmod);
> + /* For each klp rela section for this object */
> + for (i = 1; i < pmod->info->hdr->e_shnum; i++) {
> + if (!(pmod->info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH))
> + continue;
One more thing. If the module does not specify it is a live patch module
in modinfo (with MODULE_INFO(livepatch, "Y")), but it is a perfect live
patch module otherwise (it calls klp_register_patch in its init function),
the kernel crashes here. pmod->info is not initialized at all. This should
be fixed. Perhaps the easiest would be to call
klp_write_object_relocations() in klp_init_object_loaded() only if
is_livepatch_module() returns true. Similar to a check for obj->relocs
before.
Miroslav
[toc] | [prev] | [next] | [standalone]
| From | Jiri Kosina <jikos@kernel.org> |
|---|---|
| Date | 2016-01-13 10:40 +0100 |
| Subject | Re: [RFC PATCH v3 4/6] livepatch: reuse module loader code to write relocations |
| Message-ID | <qQq4j-68Z-31@gated-at.bofh.it> |
| In reply to | #1308188 |
On Wed, 13 Jan 2016, Miroslav Benes wrote:
> > {
> > - int ret = 0;
> > - unsigned long val;
> > - struct klp_reloc *reloc;
> > + int i, len, ret = 0;
> > + char *secname;
> > + const char *objname;
> >
> > if (WARN_ON(!klp_is_object_loaded(obj)))
> > return -EINVAL;
> >
> > - if (WARN_ON(!obj->relocs))
> > - return -EINVAL;
> > + objname = klp_is_module(obj) ? obj->name : "vmlinux";
> >
> > module_disable_ro(pmod);
> > + /* For each klp rela section for this object */
> > + for (i = 1; i < pmod->info->hdr->e_shnum; i++) {
> > + if (!(pmod->info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH))
> > + continue;
>
> One more thing. If the module does not specify it is a live patch module
> in modinfo (with MODULE_INFO(livepatch, "Y")), but it is a perfect live
> patch module otherwise (it calls klp_register_patch in its init function),
Side note: I think we should at least issue some light warning in such
case anyway.
--
Jiri Kosina
SUSE Labs
[toc] | [prev] | [next] | [standalone]
| From | Jessica Yu <jeyu@redhat.com> |
|---|---|
| Date | 2016-01-13 19:40 +0100 |
| Subject | Re: livepatch: reuse module loader code to write relocations |
| Message-ID | <qQyuR-3Ce-7@gated-at.bofh.it> |
| In reply to | #1308188 |
+++ Miroslav Benes [13/01/16 10:19 +0100]:
>On Fri, 8 Jan 2016, Jessica Yu wrote:
>
>> static int klp_write_object_relocations(struct module *pmod,
>> struct klp_object *obj)
>> {
>> - int ret = 0;
>> - unsigned long val;
>> - struct klp_reloc *reloc;
>> + int i, len, ret = 0;
>> + char *secname;
>> + const char *objname;
>>
>> if (WARN_ON(!klp_is_object_loaded(obj)))
>> return -EINVAL;
>>
>> - if (WARN_ON(!obj->relocs))
>> - return -EINVAL;
>> + objname = klp_is_module(obj) ? obj->name : "vmlinux";
>>
>> module_disable_ro(pmod);
>> + /* For each klp rela section for this object */
>> + for (i = 1; i < pmod->info->hdr->e_shnum; i++) {
>> + if (!(pmod->info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH))
>> + continue;
>
>One more thing. If the module does not specify it is a live patch module
>in modinfo (with MODULE_INFO(livepatch, "Y")), but it is a perfect live
>patch module otherwise (it calls klp_register_patch in its init function),
>the kernel crashes here. pmod->info is not initialized at all. This should
>be fixed. Perhaps the easiest would be to call
>klp_write_object_relocations() in klp_init_object_loaded() only if
>is_livepatch_module() returns true. Similar to a check for obj->relocs
>before.
Hm yes, that's a problem. To remedy this, I think it makes sense to
require all livepatch modules to identify themselves with the modinfo
attribute, since it is a very simple requirement. If some module calls
klp_register_patch() and it does not have the livepatch attribute,
klp_register_patch() can just return an error. We can call
is_livepatch_module() at the beginning of klp_register_patch(), and
proceed only if the check succeeds, since we'll then know that the
required structures have been properly initialized in the module
loader. What do you think?
Jessica
[toc] | [prev] | [next] | [standalone]
| From | Miroslav Benes <mbenes@suse.cz> |
|---|---|
| Date | 2016-01-14 10:20 +0100 |
| Subject | Re: livepatch: reuse module loader code to write relocations |
| Message-ID | <qQMeu-4Y2-17@gated-at.bofh.it> |
| In reply to | #1308719 |
On Wed, 13 Jan 2016, Jessica Yu wrote:
> +++ Miroslav Benes [13/01/16 10:19 +0100]:
> > On Fri, 8 Jan 2016, Jessica Yu wrote:
> >
> > > static int klp_write_object_relocations(struct module *pmod,
> > > struct klp_object *obj)
> > > {
> > > - int ret = 0;
> > > - unsigned long val;
> > > - struct klp_reloc *reloc;
> > > + int i, len, ret = 0;
> > > + char *secname;
> > > + const char *objname;
> > >
> > > if (WARN_ON(!klp_is_object_loaded(obj)))
> > > return -EINVAL;
> > >
> > > - if (WARN_ON(!obj->relocs))
> > > - return -EINVAL;
> > > + objname = klp_is_module(obj) ? obj->name : "vmlinux";
> > >
> > > module_disable_ro(pmod);
> > > + /* For each klp rela section for this object */
> > > + for (i = 1; i < pmod->info->hdr->e_shnum; i++) {
> > > + if (!(pmod->info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH))
> > > + continue;
> >
> > One more thing. If the module does not specify it is a live patch module
> > in modinfo (with MODULE_INFO(livepatch, "Y")), but it is a perfect live
> > patch module otherwise (it calls klp_register_patch in its init function),
> > the kernel crashes here. pmod->info is not initialized at all. This should
> > be fixed. Perhaps the easiest would be to call
> > klp_write_object_relocations() in klp_init_object_loaded() only if
> > is_livepatch_module() returns true. Similar to a check for obj->relocs
> > before.
>
> Hm yes, that's a problem. To remedy this, I think it makes sense to
> require all livepatch modules to identify themselves with the modinfo
> attribute, since it is a very simple requirement. If some module calls
> klp_register_patch() and it does not have the livepatch attribute,
> klp_register_patch() can just return an error. We can call
> is_livepatch_module() at the beginning of klp_register_patch(), and
> proceed only if the check succeeds, since we'll then know that the
> required structures have been properly initialized in the module
> loader. What do you think?
This is similar to what Jiri proposed in his mail. It is up to you. Both
ways (the warning and the check, or what you propose) are fine.
Miroslav
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web