Skip to content

Commit ddc4e6d

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livepatching
Pull livepatching updates from Jiri Kosina: - fix for patching modules that contain .altinstructions or .parainstructions sections, from Jessica Yu - make TAINT_LIVEPATCH a per-module flag (so that it's immediately clear which module caused the taint), from Josh Poimboeuf * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livepatching: livepatch/module: make TAINT_LIVEPATCH module-specific Documentation: livepatch: add section about arch-specific code livepatch/x86: apply alternatives and paravirt patches after relocations livepatch: use arch_klp_init_object_loaded() to finish arch-specific tasks
2 parents bc75450 + 2992ef2 commit ddc4e6d

File tree

6 files changed

+107
-14
lines changed

6 files changed

+107
-14
lines changed

Documentation/livepatch/module-elf-format.txt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Table of Contents
2525
3.3.2 Required name format
2626
3.3.3 Example livepatch symbol names
2727
3.3.4 Example `readelf --symbols` output
28-
4. Symbol table and Elf section access
28+
4. Architecture-specific sections
29+
5. Symbol table and Elf section access
2930

3031
----------------------------
3132
0. Background and motivation
@@ -46,7 +47,7 @@ architecture.
4647

4748
Since apply_relocate_add() requires access to a module's section header
4849
table, symbol table, and relocation section indices, Elf information is
49-
preserved for livepatch modules (see section 4). Livepatch manages its own
50+
preserved for livepatch modules (see section 5). Livepatch manages its own
5051
relocation sections and symbols, which are described in this document. The
5152
Elf constants used to mark livepatch symbols and relocation sections were
5253
selected from OS-specific ranges according to the definitions from glibc.
@@ -117,7 +118,7 @@ also possible for a livepatch module to have no livepatch relocation
117118
sections, as in the case of the sample livepatch module (see
118119
samples/livepatch).
119120

120-
Since Elf information is preserved for livepatch modules (see Section 4), a
121+
Since Elf information is preserved for livepatch modules (see Section 5), a
121122
livepatch relocation section can be applied simply by passing in the
122123
appropriate section index to apply_relocate_add(), which then uses it to
123124
access the relocation section and apply the relocations.
@@ -292,8 +293,19 @@ Symbol table '.symtab' contains 127 entries:
292293
[*] Note that the 'Ndx' (Section index) for these symbols is SHN_LIVEPATCH (0xff20).
293294
"OS" means OS-specific.
294295

296+
---------------------------------
297+
4. Architecture-specific sections
298+
---------------------------------
299+
Architectures may override arch_klp_init_object_loaded() to perform
300+
additional arch-specific tasks when a target module loads, such as applying
301+
arch-specific sections. On x86 for example, we must apply per-object
302+
.altinstructions and .parainstructions sections when a target module loads.
303+
These sections must be prefixed with ".klp.arch.$objname." so that they can
304+
be easily identified when iterating through a patch module's Elf sections
305+
(See arch/x86/kernel/livepatch.c for a complete example).
306+
295307
--------------------------------------
296-
4. Symbol table and Elf section access
308+
5. Symbol table and Elf section access
297309
--------------------------------------
298310
A livepatch module's symbol table is accessible through module->symtab.
299311

arch/x86/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ obj-$(CONFIG_X86_MPPARSE) += mpparse.o
8383
obj-y += apic/
8484
obj-$(CONFIG_X86_REBOOTFIXUPS) += reboot_fixups_32.o
8585
obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o
86+
obj-$(CONFIG_LIVEPATCH) += livepatch.o
8687
obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
8788
obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o
8889
obj-$(CONFIG_X86_TSC) += trace_clock.o

arch/x86/kernel/livepatch.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* livepatch.c - x86-specific Kernel Live Patching Core
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include <linux/module.h>
19+
#include <linux/kallsyms.h>
20+
#include <linux/livepatch.h>
21+
#include <asm/text-patching.h>
22+
23+
/* Apply per-object alternatives. Based on x86 module_finalize() */
24+
void arch_klp_init_object_loaded(struct klp_patch *patch,
25+
struct klp_object *obj)
26+
{
27+
int cnt;
28+
struct klp_modinfo *info;
29+
Elf_Shdr *s, *alt = NULL, *para = NULL;
30+
void *aseg, *pseg;
31+
const char *objname;
32+
char sec_objname[MODULE_NAME_LEN];
33+
char secname[KSYM_NAME_LEN];
34+
35+
info = patch->mod->klp_info;
36+
objname = obj->name ? obj->name : "vmlinux";
37+
38+
/* See livepatch core code for BUILD_BUG_ON() explanation */
39+
BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128);
40+
41+
for (s = info->sechdrs; s < info->sechdrs + info->hdr.e_shnum; s++) {
42+
/* Apply per-object .klp.arch sections */
43+
cnt = sscanf(info->secstrings + s->sh_name,
44+
".klp.arch.%55[^.].%127s",
45+
sec_objname, secname);
46+
if (cnt != 2)
47+
continue;
48+
if (strcmp(sec_objname, objname))
49+
continue;
50+
if (!strcmp(".altinstructions", secname))
51+
alt = s;
52+
if (!strcmp(".parainstructions", secname))
53+
para = s;
54+
}
55+
56+
if (alt) {
57+
aseg = (void *) alt->sh_addr;
58+
apply_alternatives(aseg, aseg + alt->sh_size);
59+
}
60+
61+
if (para) {
62+
pseg = (void *) para->sh_addr;
63+
apply_paravirt(pseg, pseg + para->sh_size);
64+
}
65+
}

include/linux/livepatch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ int klp_unregister_patch(struct klp_patch *);
116116
int klp_enable_patch(struct klp_patch *);
117117
int klp_disable_patch(struct klp_patch *);
118118

119+
void arch_klp_init_object_loaded(struct klp_patch *patch,
120+
struct klp_object *obj);
121+
119122
/* Called from the module loader during module coming/going states */
120123
int klp_module_coming(struct module *mod);
121124
void klp_module_going(struct module *mod);

kernel/livepatch/core.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ static int klp_write_object_relocations(struct module *pmod,
274274

275275
objname = klp_is_module(obj) ? obj->name : "vmlinux";
276276

277-
module_disable_ro(pmod);
278277
/* For each klp relocation section */
279278
for (i = 1; i < pmod->klp_info->hdr.e_shnum; i++) {
280279
sec = pmod->klp_info->sechdrs + i;
@@ -309,7 +308,6 @@ static int klp_write_object_relocations(struct module *pmod,
309308
break;
310309
}
311310

312-
module_enable_ro(pmod, true);
313311
return ret;
314312
}
315313

@@ -547,9 +545,6 @@ static int __klp_enable_patch(struct klp_patch *patch)
547545
list_prev_entry(patch, list)->state == KLP_DISABLED)
548546
return -EBUSY;
549547

550-
pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
551-
add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
552-
553548
pr_notice("enabling patch '%s'\n", patch->mod->name);
554549

555550
klp_for_each_object(patch, obj) {
@@ -763,16 +758,28 @@ static int klp_init_func(struct klp_object *obj, struct klp_func *func)
763758
func->old_sympos ? func->old_sympos : 1);
764759
}
765760

761+
/* Arches may override this to finish any remaining arch-specific tasks */
762+
void __weak arch_klp_init_object_loaded(struct klp_patch *patch,
763+
struct klp_object *obj)
764+
{
765+
}
766+
766767
/* parts of the initialization that is done only when the object is loaded */
767768
static int klp_init_object_loaded(struct klp_patch *patch,
768769
struct klp_object *obj)
769770
{
770771
struct klp_func *func;
771772
int ret;
772773

774+
module_disable_ro(patch->mod);
773775
ret = klp_write_object_relocations(patch->mod, obj);
774-
if (ret)
776+
if (ret) {
777+
module_enable_ro(patch->mod, true);
775778
return ret;
779+
}
780+
781+
arch_klp_init_object_loaded(patch, obj);
782+
module_enable_ro(patch->mod, true);
776783

777784
klp_for_each_func(obj, func) {
778785
ret = klp_find_object_symbol(obj->name, func->old_name,

kernel/module.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,8 @@ static size_t module_flags_taint(struct module *mod, char *buf)
11491149
buf[l++] = 'C';
11501150
if (mod->taints & (1 << TAINT_UNSIGNED_MODULE))
11511151
buf[l++] = 'E';
1152+
if (mod->taints & (1 << TAINT_LIVEPATCH))
1153+
buf[l++] = 'K';
11521154
/*
11531155
* TAINT_FORCED_RMMOD: could be added.
11541156
* TAINT_CPU_OUT_OF_SPEC, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
@@ -2792,14 +2794,17 @@ static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned l
27922794
}
27932795

27942796
#ifdef CONFIG_LIVEPATCH
2795-
static int find_livepatch_modinfo(struct module *mod, struct load_info *info)
2797+
static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
27962798
{
2797-
mod->klp = get_modinfo(info, "livepatch") ? true : false;
2799+
if (get_modinfo(info, "livepatch")) {
2800+
mod->klp = true;
2801+
add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
2802+
}
27982803

27992804
return 0;
28002805
}
28012806
#else /* !CONFIG_LIVEPATCH */
2802-
static int find_livepatch_modinfo(struct module *mod, struct load_info *info)
2807+
static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
28032808
{
28042809
if (get_modinfo(info, "livepatch")) {
28052810
pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
@@ -2969,7 +2974,7 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags)
29692974
"is unknown, you have been warned.\n", mod->name);
29702975
}
29712976

2972-
err = find_livepatch_modinfo(mod, info);
2977+
err = check_modinfo_livepatch(mod, info);
29732978
if (err)
29742979
return err;
29752980

0 commit comments

Comments
 (0)