Skip to content

Commit 7dd86cf

Browse files
committed
Merge tag 'livepatching-for-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/livepatching/livepatching
Pull livepatching updates from Petr Mladek: - Allow reloading a livepatched module by clearing livepatch-specific relocations in the livepatch module. Otherwise, the repeated load would fail on consistency checks. * tag 'livepatching-for-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/livepatching/livepatching: livepatch,x86: Clear relocation targets on a module removal x86/module: remove unused code in __apply_relocate_add
2 parents d876315 + 0c05e7b commit 7dd86cf

File tree

3 files changed

+126
-50
lines changed

3 files changed

+126
-50
lines changed

arch/x86/kernel/module.c

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,27 @@ int apply_relocate(Elf32_Shdr *sechdrs,
129129
return 0;
130130
}
131131
#else /*X86_64*/
132-
static int __apply_relocate_add(Elf64_Shdr *sechdrs,
132+
static int __write_relocate_add(Elf64_Shdr *sechdrs,
133133
const char *strtab,
134134
unsigned int symindex,
135135
unsigned int relsec,
136136
struct module *me,
137-
void *(*write)(void *dest, const void *src, size_t len))
137+
void *(*write)(void *dest, const void *src, size_t len),
138+
bool apply)
138139
{
139140
unsigned int i;
140141
Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
141142
Elf64_Sym *sym;
142143
void *loc;
143144
u64 val;
145+
u64 zero = 0ULL;
144146

145-
DEBUGP("Applying relocate section %u to %u\n",
147+
DEBUGP("%s relocate section %u to %u\n",
148+
apply ? "Applying" : "Clearing",
146149
relsec, sechdrs[relsec].sh_info);
147150
for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
151+
size_t size;
152+
148153
/* This is where to make the change */
149154
loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
150155
+ rel[i].r_offset;
@@ -162,56 +167,53 @@ static int __apply_relocate_add(Elf64_Shdr *sechdrs,
162167

163168
switch (ELF64_R_TYPE(rel[i].r_info)) {
164169
case R_X86_64_NONE:
165-
break;
170+
continue; /* nothing to write */
166171
case R_X86_64_64:
167-
if (*(u64 *)loc != 0)
168-
goto invalid_relocation;
169-
write(loc, &val, 8);
172+
size = 8;
170173
break;
171174
case R_X86_64_32:
172-
if (*(u32 *)loc != 0)
173-
goto invalid_relocation;
174-
write(loc, &val, 4);
175-
if (val != *(u32 *)loc)
175+
if (val != *(u32 *)&val)
176176
goto overflow;
177+
size = 4;
177178
break;
178179
case R_X86_64_32S:
179-
if (*(s32 *)loc != 0)
180-
goto invalid_relocation;
181-
write(loc, &val, 4);
182-
if ((s64)val != *(s32 *)loc)
180+
if ((s64)val != *(s32 *)&val)
183181
goto overflow;
182+
size = 4;
184183
break;
185184
case R_X86_64_PC32:
186185
case R_X86_64_PLT32:
187-
if (*(u32 *)loc != 0)
188-
goto invalid_relocation;
189186
val -= (u64)loc;
190-
write(loc, &val, 4);
191-
#if 0
192-
if ((s64)val != *(s32 *)loc)
193-
goto overflow;
194-
#endif
187+
size = 4;
195188
break;
196189
case R_X86_64_PC64:
197-
if (*(u64 *)loc != 0)
198-
goto invalid_relocation;
199190
val -= (u64)loc;
200-
write(loc, &val, 8);
191+
size = 8;
201192
break;
202193
default:
203194
pr_err("%s: Unknown rela relocation: %llu\n",
204195
me->name, ELF64_R_TYPE(rel[i].r_info));
205196
return -ENOEXEC;
206197
}
198+
199+
if (apply) {
200+
if (memcmp(loc, &zero, size)) {
201+
pr_err("x86/modules: Invalid relocation target, existing value is nonzero for type %d, loc %p, val %Lx\n",
202+
(int)ELF64_R_TYPE(rel[i].r_info), loc, val);
203+
return -ENOEXEC;
204+
}
205+
write(loc, &val, size);
206+
} else {
207+
if (memcmp(loc, &val, size)) {
208+
pr_warn("x86/modules: Invalid relocation target, existing value does not match expected value for type %d, loc %p, val %Lx\n",
209+
(int)ELF64_R_TYPE(rel[i].r_info), loc, val);
210+
return -ENOEXEC;
211+
}
212+
write(loc, &zero, size);
213+
}
207214
}
208215
return 0;
209216

210-
invalid_relocation:
211-
pr_err("x86/modules: Skipping invalid relocation target, existing value is nonzero for type %d, loc %p, val %Lx\n",
212-
(int)ELF64_R_TYPE(rel[i].r_info), loc, val);
213-
return -ENOEXEC;
214-
215217
overflow:
216218
pr_err("overflow in relocation type %d val %Lx\n",
217219
(int)ELF64_R_TYPE(rel[i].r_info), val);
@@ -220,11 +222,12 @@ static int __apply_relocate_add(Elf64_Shdr *sechdrs,
220222
return -ENOEXEC;
221223
}
222224

223-
int apply_relocate_add(Elf64_Shdr *sechdrs,
224-
const char *strtab,
225-
unsigned int symindex,
226-
unsigned int relsec,
227-
struct module *me)
225+
static int write_relocate_add(Elf64_Shdr *sechdrs,
226+
const char *strtab,
227+
unsigned int symindex,
228+
unsigned int relsec,
229+
struct module *me,
230+
bool apply)
228231
{
229232
int ret;
230233
bool early = me->state == MODULE_STATE_UNFORMED;
@@ -235,8 +238,8 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
235238
mutex_lock(&text_mutex);
236239
}
237240

238-
ret = __apply_relocate_add(sechdrs, strtab, symindex, relsec, me,
239-
write);
241+
ret = __write_relocate_add(sechdrs, strtab, symindex, relsec, me,
242+
write, apply);
240243

241244
if (!early) {
242245
text_poke_sync();
@@ -246,6 +249,26 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
246249
return ret;
247250
}
248251

252+
int apply_relocate_add(Elf64_Shdr *sechdrs,
253+
const char *strtab,
254+
unsigned int symindex,
255+
unsigned int relsec,
256+
struct module *me)
257+
{
258+
return write_relocate_add(sechdrs, strtab, symindex, relsec, me, true);
259+
}
260+
261+
#ifdef CONFIG_LIVEPATCH
262+
void clear_relocate_add(Elf64_Shdr *sechdrs,
263+
const char *strtab,
264+
unsigned int symindex,
265+
unsigned int relsec,
266+
struct module *me)
267+
{
268+
write_relocate_add(sechdrs, strtab, symindex, relsec, me, false);
269+
}
270+
#endif
271+
249272
#endif
250273

251274
int module_finalize(const Elf_Ehdr *hdr,

include/linux/moduleloader.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ int apply_relocate_add(Elf_Shdr *sechdrs,
7575
unsigned int symindex,
7676
unsigned int relsec,
7777
struct module *mod);
78+
#ifdef CONFIG_LIVEPATCH
79+
/*
80+
* Some architectures (namely x86_64 and ppc64) perform sanity checks when
81+
* applying relocations. If a patched module gets unloaded and then later
82+
* reloaded (and re-patched), klp re-applies relocations to the replacement
83+
* function(s). Any leftover relocations from the previous loading of the
84+
* patched module might trigger the sanity checks.
85+
*
86+
* To prevent that, when unloading a patched module, clear out any relocations
87+
* that might trigger arch-specific sanity checks on a future module reload.
88+
*/
89+
void clear_relocate_add(Elf_Shdr *sechdrs,
90+
const char *strtab,
91+
unsigned int symindex,
92+
unsigned int relsec,
93+
struct module *me);
94+
#endif
7895
#else
7996
static inline int apply_relocate_add(Elf_Shdr *sechdrs,
8097
const char *strtab,

kernel/livepatch/core.c

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,14 @@ static int klp_resolve_symbols(Elf_Shdr *sechdrs, const char *strtab,
260260
return 0;
261261
}
262262

263+
void __weak clear_relocate_add(Elf_Shdr *sechdrs,
264+
const char *strtab,
265+
unsigned int symindex,
266+
unsigned int relsec,
267+
struct module *me)
268+
{
269+
}
270+
263271
/*
264272
* At a high-level, there are two types of klp relocation sections: those which
265273
* reference symbols which live in vmlinux; and those which reference symbols
@@ -283,10 +291,10 @@ static int klp_resolve_symbols(Elf_Shdr *sechdrs, const char *strtab,
283291
* the to-be-patched module to be loaded and patched sometime *after* the
284292
* klp module is loaded.
285293
*/
286-
int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
287-
const char *shstrtab, const char *strtab,
288-
unsigned int symndx, unsigned int secndx,
289-
const char *objname)
294+
static int klp_write_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
295+
const char *shstrtab, const char *strtab,
296+
unsigned int symndx, unsigned int secndx,
297+
const char *objname, bool apply)
290298
{
291299
int cnt, ret;
292300
char sec_objname[MODULE_NAME_LEN];
@@ -308,11 +316,26 @@ int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
308316
if (strcmp(objname ? objname : "vmlinux", sec_objname))
309317
return 0;
310318

311-
ret = klp_resolve_symbols(sechdrs, strtab, symndx, sec, sec_objname);
312-
if (ret)
313-
return ret;
319+
if (apply) {
320+
ret = klp_resolve_symbols(sechdrs, strtab, symndx,
321+
sec, sec_objname);
322+
if (ret)
323+
return ret;
324+
325+
return apply_relocate_add(sechdrs, strtab, symndx, secndx, pmod);
326+
}
327+
328+
clear_relocate_add(sechdrs, strtab, symndx, secndx, pmod);
329+
return 0;
330+
}
314331

315-
return apply_relocate_add(sechdrs, strtab, symndx, secndx, pmod);
332+
int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
333+
const char *shstrtab, const char *strtab,
334+
unsigned int symndx, unsigned int secndx,
335+
const char *objname)
336+
{
337+
return klp_write_section_relocs(pmod, sechdrs, shstrtab, strtab, symndx,
338+
secndx, objname, true);
316339
}
317340

318341
/*
@@ -761,8 +784,9 @@ static int klp_init_func(struct klp_object *obj, struct klp_func *func)
761784
func->old_sympos ? func->old_sympos : 1);
762785
}
763786

764-
static int klp_apply_object_relocs(struct klp_patch *patch,
765-
struct klp_object *obj)
787+
static int klp_write_object_relocs(struct klp_patch *patch,
788+
struct klp_object *obj,
789+
bool apply)
766790
{
767791
int i, ret;
768792
struct klp_modinfo *info = patch->mod->klp_info;
@@ -773,17 +797,29 @@ static int klp_apply_object_relocs(struct klp_patch *patch,
773797
if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
774798
continue;
775799

776-
ret = klp_apply_section_relocs(patch->mod, info->sechdrs,
800+
ret = klp_write_section_relocs(patch->mod, info->sechdrs,
777801
info->secstrings,
778802
patch->mod->core_kallsyms.strtab,
779-
info->symndx, i, obj->name);
803+
info->symndx, i, obj->name, apply);
780804
if (ret)
781805
return ret;
782806
}
783807

784808
return 0;
785809
}
786810

811+
static int klp_apply_object_relocs(struct klp_patch *patch,
812+
struct klp_object *obj)
813+
{
814+
return klp_write_object_relocs(patch, obj, true);
815+
}
816+
817+
static void klp_clear_object_relocs(struct klp_patch *patch,
818+
struct klp_object *obj)
819+
{
820+
klp_write_object_relocs(patch, obj, false);
821+
}
822+
787823
/* parts of the initialization that is done only when the object is loaded */
788824
static int klp_init_object_loaded(struct klp_patch *patch,
789825
struct klp_object *obj)
@@ -1171,7 +1207,7 @@ static void klp_cleanup_module_patches_limited(struct module *mod,
11711207
klp_unpatch_object(obj);
11721208

11731209
klp_post_unpatch_callback(obj);
1174-
1210+
klp_clear_object_relocs(patch, obj);
11751211
klp_free_object_loaded(obj);
11761212
break;
11771213
}

0 commit comments

Comments
 (0)