Skip to content

Commit 425595a

Browse files
Jessica YuJiri Kosina
authored andcommitted
livepatch: reuse module loader code to write relocations
Reuse module loader code to write relocations, thereby eliminating the need for architecture specific relocation code in livepatch. Specifically, reuse the apply_relocate_add() function in the module loader to write relocations instead of duplicating functionality in livepatch's arch-dependent klp_write_module_reloc() function. In order to accomplish this, livepatch modules manage their own relocation sections (marked with the SHF_RELA_LIVEPATCH section flag) and livepatch-specific symbols (marked with SHN_LIVEPATCH symbol section index). To apply livepatch relocation sections, 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 and the s390 klp_write_module_reloc() function stub. They are no longer needed since relocation work has been offloaded to module loader. Lastly, mark the module as a livepatch module so that the module loader canappropriately identify and initialize it. Signed-off-by: Jessica Yu <jeyu@redhat.com> Reviewed-by: Miroslav Benes <mbenes@suse.cz> Acked-by: Josh Poimboeuf <jpoimboe@redhat.com> Acked-by: Heiko Carstens <heiko.carstens@de.ibm.com> # for s390 changes Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent f31e096 commit 425595a

7 files changed

Lines changed: 95 additions & 154 deletions

File tree

arch/s390/include/asm/livepatch.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ static inline int klp_check_compiler_support(void)
2424
return 0;
2525
}
2626

27-
static inline int klp_write_module_reloc(struct module *mod, unsigned long
28-
type, unsigned long loc, unsigned long value)
29-
{
30-
/* not supported yet */
31-
return -ENOSYS;
32-
}
33-
3427
static inline void klp_arch_set_pc(struct pt_regs *regs, unsigned long ip)
3528
{
3629
regs->psw.addr = ip;

arch/x86/include/asm/livepatch.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ static inline int klp_check_compiler_support(void)
3232
#endif
3333
return 0;
3434
}
35-
int klp_write_module_reloc(struct module *mod, unsigned long type,
36-
unsigned long loc, unsigned long value);
3735

3836
static inline void klp_arch_set_pc(struct pt_regs *regs, unsigned long ip)
3937
{

arch/x86/kernel/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ obj-$(CONFIG_X86_MPPARSE) += mpparse.o
6767
obj-y += apic/
6868
obj-$(CONFIG_X86_REBOOTFIXUPS) += reboot_fixups_32.o
6969
obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o
70-
obj-$(CONFIG_LIVEPATCH) += livepatch.o
7170
obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
7271
obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o
7372
obj-$(CONFIG_X86_TSC) += trace_clock.o

arch/x86/kernel/livepatch.c

Lines changed: 0 additions & 70 deletions
This file was deleted.

include/linux/livepatch.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,9 @@ struct klp_func {
6464
struct list_head stack_node;
6565
};
6666

67-
/**
68-
* struct klp_reloc - relocation structure for live patching
69-
* @loc: address where the relocation will be written
70-
* @sympos: position in kallsyms to disambiguate symbols (optional)
71-
* @type: ELF relocation type
72-
* @name: name of the referenced symbol (for lookup/verification)
73-
* @addend: offset from the referenced symbol
74-
* @external: symbol is either exported or within the live patch module itself
75-
*/
76-
struct klp_reloc {
77-
unsigned long loc;
78-
unsigned long sympos;
79-
unsigned long type;
80-
const char *name;
81-
int addend;
82-
int external;
83-
};
84-
8567
/**
8668
* struct klp_object - kernel object structure for live patching
8769
* @name: module name (or NULL for vmlinux)
88-
* @relocs: relocation entries to be applied at load time
8970
* @funcs: function entries for functions to be patched in the object
9071
* @kobj: kobject for sysfs resources
9172
* @mod: kernel module associated with the patched object
@@ -95,7 +76,6 @@ struct klp_reloc {
9576
struct klp_object {
9677
/* external */
9778
const char *name;
98-
struct klp_reloc *relocs;
9979
struct klp_func *funcs;
10080

10181
/* internal */

kernel/livepatch/core.c

Lines changed: 94 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <linux/list.h>
2929
#include <linux/kallsyms.h>
3030
#include <linux/livepatch.h>
31+
#include <linux/elf.h>
32+
#include <linux/moduleloader.h>
3133
#include <asm/cacheflush.h>
3234

3335
/**
@@ -204,75 +206,109 @@ static int klp_find_object_symbol(const char *objname, const char *name,
204206
return -EINVAL;
205207
}
206208

207-
/*
208-
* external symbols are located outside the parent object (where the parent
209-
* object is either vmlinux or the kmod being patched).
210-
*/
211-
static int klp_find_external_symbol(struct module *pmod, const char *name,
212-
unsigned long *addr)
209+
static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod)
213210
{
214-
const struct kernel_symbol *sym;
215-
216-
/* first, check if it's an exported symbol */
217-
preempt_disable();
218-
sym = find_symbol(name, NULL, NULL, true, true);
219-
if (sym) {
220-
*addr = sym->value;
221-
preempt_enable();
222-
return 0;
223-
}
224-
preempt_enable();
211+
int i, cnt, vmlinux, ret;
212+
char objname[MODULE_NAME_LEN];
213+
char symname[KSYM_NAME_LEN];
214+
char *strtab = pmod->core_kallsyms.strtab;
215+
Elf_Rela *relas;
216+
Elf_Sym *sym;
217+
unsigned long sympos, addr;
225218

226219
/*
227-
* Check if it's in another .o within the patch module. This also
228-
* checks that the external symbol is unique.
220+
* Since the field widths for objname and symname in the sscanf()
221+
* call are hard-coded and correspond to MODULE_NAME_LEN and
222+
* KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
223+
* and KSYM_NAME_LEN have the values we expect them to have.
224+
*
225+
* Because the value of MODULE_NAME_LEN can differ among architectures,
226+
* we use the smallest/strictest upper bound possible (56, based on
227+
* the current definition of MODULE_NAME_LEN) to prevent overflows.
229228
*/
230-
return klp_find_object_symbol(pmod->name, name, 0, addr);
229+
BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128);
230+
231+
relas = (Elf_Rela *) relasec->sh_addr;
232+
/* For each rela in this klp relocation section */
233+
for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
234+
sym = pmod->core_kallsyms.symtab + ELF_R_SYM(relas[i].r_info);
235+
if (sym->st_shndx != SHN_LIVEPATCH) {
236+
pr_err("symbol %s is not marked as a livepatch symbol",
237+
strtab + sym->st_name);
238+
return -EINVAL;
239+
}
240+
241+
/* Format: .klp.sym.objname.symname,sympos */
242+
cnt = sscanf(strtab + sym->st_name,
243+
".klp.sym.%55[^.].%127[^,],%lu",
244+
objname, symname, &sympos);
245+
if (cnt != 3) {
246+
pr_err("symbol %s has an incorrectly formatted name",
247+
strtab + sym->st_name);
248+
return -EINVAL;
249+
}
250+
251+
/* klp_find_object_symbol() treats a NULL objname as vmlinux */
252+
vmlinux = !strcmp(objname, "vmlinux");
253+
ret = klp_find_object_symbol(vmlinux ? NULL : objname,
254+
symname, sympos, &addr);
255+
if (ret)
256+
return ret;
257+
258+
sym->st_value = addr;
259+
}
260+
261+
return 0;
231262
}
232263

233264
static int klp_write_object_relocations(struct module *pmod,
234265
struct klp_object *obj)
235266
{
236-
int ret = 0;
237-
unsigned long val;
238-
struct klp_reloc *reloc;
267+
int i, cnt, ret = 0;
268+
const char *objname, *secname;
269+
char sec_objname[MODULE_NAME_LEN];
270+
Elf_Shdr *sec;
239271

240272
if (WARN_ON(!klp_is_object_loaded(obj)))
241273
return -EINVAL;
242274

243-
if (WARN_ON(!obj->relocs))
244-
return -EINVAL;
275+
objname = klp_is_module(obj) ? obj->name : "vmlinux";
245276

246277
module_disable_ro(pmod);
278+
/* For each klp relocation section */
279+
for (i = 1; i < pmod->klp_info->hdr.e_shnum; i++) {
280+
sec = pmod->klp_info->sechdrs + i;
281+
secname = pmod->klp_info->secstrings + sec->sh_name;
282+
if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
283+
continue;
247284

248-
for (reloc = obj->relocs; reloc->name; reloc++) {
249-
/* discover the address of the referenced symbol */
250-
if (reloc->external) {
251-
if (reloc->sympos > 0) {
252-
pr_err("non-zero sympos for external reloc symbol '%s' is not supported\n",
253-
reloc->name);
254-
ret = -EINVAL;
255-
goto out;
256-
}
257-
ret = klp_find_external_symbol(pmod, reloc->name, &val);
258-
} else
259-
ret = klp_find_object_symbol(obj->name,
260-
reloc->name,
261-
reloc->sympos,
262-
&val);
285+
/*
286+
* Format: .klp.rela.sec_objname.section_name
287+
* See comment in klp_resolve_symbols() for an explanation
288+
* of the selected field width value.
289+
*/
290+
cnt = sscanf(secname, ".klp.rela.%55[^.]", sec_objname);
291+
if (cnt != 1) {
292+
pr_err("section %s has an incorrectly formatted name",
293+
secname);
294+
ret = -EINVAL;
295+
break;
296+
}
297+
298+
if (strcmp(objname, sec_objname))
299+
continue;
300+
301+
ret = klp_resolve_symbols(sec, pmod);
263302
if (ret)
264-
goto out;
303+
break;
265304

266-
ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
267-
val + reloc->addend);
268-
if (ret) {
269-
pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
270-
reloc->name, val, ret);
271-
goto out;
272-
}
305+
ret = apply_relocate_add(pmod->klp_info->sechdrs,
306+
pmod->core_kallsyms.strtab,
307+
pmod->klp_info->symndx, i, pmod);
308+
if (ret)
309+
break;
273310
}
274311

275-
out:
276312
module_enable_ro(pmod);
277313
return ret;
278314
}
@@ -703,11 +739,9 @@ static int klp_init_object_loaded(struct klp_patch *patch,
703739
struct klp_func *func;
704740
int ret;
705741

706-
if (obj->relocs) {
707-
ret = klp_write_object_relocations(patch->mod, obj);
708-
if (ret)
709-
return ret;
710-
}
742+
ret = klp_write_object_relocations(patch->mod, obj);
743+
if (ret)
744+
return ret;
711745

712746
klp_for_each_func(obj, func) {
713747
ret = klp_find_object_symbol(obj->name, func->old_name,
@@ -842,6 +876,12 @@ int klp_register_patch(struct klp_patch *patch)
842876
{
843877
int ret;
844878

879+
if (!is_livepatch_module(patch->mod)) {
880+
pr_err("module %s is not marked as a livepatch module",
881+
patch->mod->name);
882+
return -EINVAL;
883+
}
884+
845885
if (!klp_initialized())
846886
return -ENODEV;
847887

samples/livepatch/livepatch-sample.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,4 @@ static void livepatch_exit(void)
8989
module_init(livepatch_init);
9090
module_exit(livepatch_exit);
9191
MODULE_LICENSE("GPL");
92+
MODULE_INFO(livepatch, "Y");

0 commit comments

Comments
 (0)