Skip to content

Commit b700e7f

Browse files
sjenningJiri Kosina
authored andcommitted
livepatch: kernel: add support for live patching
This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions. It represents the greatest common functionality set between kpatch and kgraft and can accept patches built using either method. This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied in this version. [ jkosina@suse.cz: due to the number of contributions that got folded into this original patch from Seth Jennings, add SUSE's copyright as well, as discussed via e-mail ] Signed-off-by: Seth Jennings <sjenning@redhat.com> Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com> Reviewed-by: Miroslav Benes <mbenes@suse.cz> Reviewed-by: Petr Mladek <pmladek@suse.cz> Reviewed-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Signed-off-by: Miroslav Benes <mbenes@suse.cz> Signed-off-by: Petr Mladek <pmladek@suse.cz> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent c5f4546 commit b700e7f

11 files changed

Lines changed: 1273 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
What: /sys/kernel/livepatch
2+
Date: Nov 2014
3+
KernelVersion: 3.19.0
4+
Contact: live-patching@vger.kernel.org
5+
Description:
6+
Interface for kernel live patching
7+
8+
The /sys/kernel/livepatch directory contains subdirectories for
9+
each loaded live patch module.
10+
11+
What: /sys/kernel/livepatch/<patch>
12+
Date: Nov 2014
13+
KernelVersion: 3.19.0
14+
Contact: live-patching@vger.kernel.org
15+
Description:
16+
The patch directory contains subdirectories for each kernel
17+
object (vmlinux or a module) in which it patched functions.
18+
19+
What: /sys/kernel/livepatch/<patch>/enabled
20+
Date: Nov 2014
21+
KernelVersion: 3.19.0
22+
Contact: live-patching@vger.kernel.org
23+
Description:
24+
A writable attribute that indicates whether the patched
25+
code is currently applied. Writing 0 will disable the patch
26+
while writing 1 will re-enable the patch.
27+
28+
What: /sys/kernel/livepatch/<patch>/<object>
29+
Date: Nov 2014
30+
KernelVersion: 3.19.0
31+
Contact: live-patching@vger.kernel.org
32+
Description:
33+
The object directory contains subdirectories for each function
34+
that is patched within the object.
35+
36+
What: /sys/kernel/livepatch/<patch>/<object>/<function>
37+
Date: Nov 2014
38+
KernelVersion: 3.19.0
39+
Contact: live-patching@vger.kernel.org
40+
Description:
41+
The function directory contains attributes regarding the
42+
properties and state of the patched function.
43+
44+
There are currently no such attributes.

‎MAINTAINERS‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5784,6 +5784,19 @@ F: Documentation/misc-devices/lis3lv02d
57845784
F: drivers/misc/lis3lv02d/
57855785
F: drivers/platform/x86/hp_accel.c
57865786

5787+
LIVE PATCHING
5788+
M: Josh Poimboeuf <jpoimboe@redhat.com>
5789+
M: Seth Jennings <sjenning@redhat.com>
5790+
M: Jiri Kosina <jkosina@suse.cz>
5791+
M: Vojtech Pavlik <vojtech@suse.cz>
5792+
S: Maintained
5793+
F: kernel/livepatch/
5794+
F: include/linux/livepatch.h
5795+
F: arch/x86/include/asm/livepatch.h
5796+
F: arch/x86/kernel/livepatch.c
5797+
F: Documentation/ABI/testing/sysfs-kernel-livepatch
5798+
L: live-patching@vger.kernel.org
5799+
57875800
LLC (802.2)
57885801
M: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
57895802
S: Maintained

‎arch/x86/Kconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ config X86_64
1717
depends on 64BIT
1818
select X86_DEV_DMA_OPS
1919
select ARCH_USE_CMPXCHG_LOCKREF
20+
select ARCH_HAVE_LIVE_PATCHING
2021

2122
### Arch settings
2223
config X86
@@ -2008,6 +2009,8 @@ config CMDLINE_OVERRIDE
20082009
This is used to work around broken boot loaders. This should
20092010
be set to 'N' under normal conditions.
20102011

2012+
source "kernel/livepatch/Kconfig"
2013+
20112014
endmenu
20122015

20132016
config ARCH_ENABLE_MEMORY_HOTPLUG

‎arch/x86/include/asm/livepatch.h‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* livepatch.h - x86-specific Kernel Live Patching Core
3+
*
4+
* Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
5+
* Copyright (C) 2014 SUSE
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#ifndef _ASM_X86_LIVEPATCH_H
22+
#define _ASM_X86_LIVEPATCH_H
23+
24+
#include <linux/module.h>
25+
26+
#ifdef CONFIG_LIVE_PATCHING
27+
#ifndef CC_USING_FENTRY
28+
#error Your compiler must support -mfentry for live patching to work
29+
#endif
30+
extern int klp_write_module_reloc(struct module *mod, unsigned long type,
31+
unsigned long loc, unsigned long value);
32+
33+
#else
34+
#error Live patching support is disabled; check CONFIG_LIVE_PATCHING
35+
#endif
36+
37+
#endif /* _ASM_X86_LIVEPATCH_H */

‎arch/x86/kernel/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ obj-$(CONFIG_X86_MPPARSE) += mpparse.o
6363
obj-y += apic/
6464
obj-$(CONFIG_X86_REBOOTFIXUPS) += reboot_fixups_32.o
6565
obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o
66+
obj-$(CONFIG_LIVE_PATCHING) += livepatch.o
6667
obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
6768
obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o
6869
obj-$(CONFIG_X86_TSC) += trace_clock.o

‎arch/x86/kernel/livepatch.c‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* livepatch.c - x86-specific Kernel Live Patching Core
3+
*
4+
* Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
5+
* Copyright (C) 2014 SUSE
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#include <linux/module.h>
22+
#include <linux/uaccess.h>
23+
#include <asm/cacheflush.h>
24+
#include <asm/page_types.h>
25+
#include <asm/elf.h>
26+
#include <asm/livepatch.h>
27+
28+
/**
29+
* klp_write_module_reloc() - write a relocation in a module
30+
* @mod: module in which the section to be modified is found
31+
* @type: ELF relocation type (see asm/elf.h)
32+
* @loc: address that the relocation should be written to
33+
* @value: relocation value (sym address + addend)
34+
*
35+
* This function writes a relocation to the specified location for
36+
* a particular module.
37+
*/
38+
int klp_write_module_reloc(struct module *mod, unsigned long type,
39+
unsigned long loc, unsigned long value)
40+
{
41+
int ret, numpages, size = 4;
42+
bool readonly;
43+
unsigned long val;
44+
unsigned long core = (unsigned long)mod->module_core;
45+
unsigned long core_ro_size = mod->core_ro_size;
46+
unsigned long core_size = mod->core_size;
47+
48+
switch (type) {
49+
case R_X86_64_NONE:
50+
return 0;
51+
case R_X86_64_64:
52+
val = value;
53+
size = 8;
54+
break;
55+
case R_X86_64_32:
56+
val = (u32)value;
57+
break;
58+
case R_X86_64_32S:
59+
val = (s32)value;
60+
break;
61+
case R_X86_64_PC32:
62+
val = (u32)(value - loc);
63+
break;
64+
default:
65+
/* unsupported relocation type */
66+
return -EINVAL;
67+
}
68+
69+
if (loc < core || loc >= core + core_size)
70+
/* loc does not point to any symbol inside the module */
71+
return -EINVAL;
72+
73+
if (loc < core + core_ro_size)
74+
readonly = true;
75+
else
76+
readonly = false;
77+
78+
/* determine if the relocation spans a page boundary */
79+
numpages = ((loc & PAGE_MASK) == ((loc + size) & PAGE_MASK)) ? 1 : 2;
80+
81+
if (readonly)
82+
set_memory_rw(loc & PAGE_MASK, numpages);
83+
84+
ret = probe_kernel_write((void *)loc, &val, size);
85+
86+
if (readonly)
87+
set_memory_ro(loc & PAGE_MASK, numpages);
88+
89+
return ret;
90+
}

‎include/linux/livepatch.h‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* livepatch.h - Kernel Live Patching Core
3+
*
4+
* Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
5+
* Copyright (C) 2014 SUSE
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#ifndef _LINUX_LIVEPATCH_H_
22+
#define _LINUX_LIVEPATCH_H_
23+
24+
#include <linux/module.h>
25+
#include <linux/ftrace.h>
26+
27+
#if IS_ENABLED(CONFIG_LIVE_PATCHING)
28+
29+
#include <asm/livepatch.h>
30+
31+
enum klp_state {
32+
KLP_DISABLED,
33+
KLP_ENABLED
34+
};
35+
36+
/**
37+
* struct klp_func - function structure for live patching
38+
* @old_name: name of the function to be patched
39+
* @new_func: pointer to the patched function code
40+
* @old_addr: a hint conveying at what address the old function
41+
* can be found (optional, vmlinux patches only)
42+
* @kobj: kobject for sysfs resources
43+
* @fops: ftrace operations structure
44+
* @state: tracks function-level patch application state
45+
*/
46+
struct klp_func {
47+
/* external */
48+
const char *old_name;
49+
void *new_func;
50+
/*
51+
* The old_addr field is optional and can be used to resolve
52+
* duplicate symbol names in the vmlinux object. If this
53+
* information is not present, the symbol is located by name
54+
* with kallsyms. If the name is not unique and old_addr is
55+
* not provided, the patch application fails as there is no
56+
* way to resolve the ambiguity.
57+
*/
58+
unsigned long old_addr;
59+
60+
/* internal */
61+
struct kobject kobj;
62+
struct ftrace_ops *fops;
63+
enum klp_state state;
64+
};
65+
66+
/**
67+
* struct klp_reloc - relocation structure for live patching
68+
* @loc: address where the relocation will be written
69+
* @val: address of the referenced symbol (optional,
70+
* vmlinux patches only)
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 val;
79+
unsigned long type;
80+
const char *name;
81+
int addend;
82+
int external;
83+
};
84+
85+
/**
86+
* struct klp_object - kernel object structure for live patching
87+
* @name: module name (or NULL for vmlinux)
88+
* @relocs: relocation entries to be applied at load time
89+
* @funcs: function entries for functions to be patched in the object
90+
* @kobj: kobject for sysfs resources
91+
* @mod: kernel module associated with the patched object
92+
* (NULL for vmlinux)
93+
* @state: tracks object-level patch application state
94+
*/
95+
struct klp_object {
96+
/* external */
97+
const char *name;
98+
struct klp_reloc *relocs;
99+
struct klp_func *funcs;
100+
101+
/* internal */
102+
struct kobject *kobj;
103+
struct module *mod;
104+
enum klp_state state;
105+
};
106+
107+
/**
108+
* struct klp_patch - patch structure for live patching
109+
* @mod: reference to the live patch module
110+
* @objs: object entries for kernel objects to be patched
111+
* @list: list node for global list of registered patches
112+
* @kobj: kobject for sysfs resources
113+
* @state: tracks patch-level application state
114+
*/
115+
struct klp_patch {
116+
/* external */
117+
struct module *mod;
118+
struct klp_object *objs;
119+
120+
/* internal */
121+
struct list_head list;
122+
struct kobject kobj;
123+
enum klp_state state;
124+
};
125+
126+
extern int klp_register_patch(struct klp_patch *);
127+
extern int klp_unregister_patch(struct klp_patch *);
128+
extern int klp_enable_patch(struct klp_patch *);
129+
extern int klp_disable_patch(struct klp_patch *);
130+
131+
#endif /* CONFIG_LIVE_PATCHING */
132+
133+
#endif /* _LINUX_LIVEPATCH_H_ */

‎kernel/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ obj-y += power/
2626
obj-y += printk/
2727
obj-y += irq/
2828
obj-y += rcu/
29+
obj-y += livepatch/
2930

3031
obj-$(CONFIG_CHECKPOINT_RESTORE) += kcmp.o
3132
obj-$(CONFIG_FREEZER) += freezer.o

‎kernel/livepatch/Kconfig‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
config ARCH_HAVE_LIVE_PATCHING
2+
boolean
3+
help
4+
Arch supports kernel live patching
5+
6+
config LIVE_PATCHING
7+
boolean "Kernel Live Patching"
8+
depends on DYNAMIC_FTRACE_WITH_REGS
9+
depends on MODULES
10+
depends on SYSFS
11+
depends on KALLSYMS_ALL
12+
depends on ARCH_HAVE_LIVE_PATCHING
13+
help
14+
Say Y here if you want to support kernel live patching.
15+
This option has no runtime impact until a kernel "patch"
16+
module uses the interface provided by this option to register
17+
a patch, causing calls to patched functions to be redirected
18+
to new function code contained in the patch module.

‎kernel/livepatch/Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
obj-$(CONFIG_LIVE_PATCHING) += livepatch.o
2+
3+
livepatch-objs := core.o

0 commit comments

Comments
 (0)