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