xref: /linux-6.15/kernel/livepatch/core.c (revision febaa65c)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b700e7f0SSeth Jennings /*
3b700e7f0SSeth Jennings  * core.c - Kernel Live Patching Core
4b700e7f0SSeth Jennings  *
5b700e7f0SSeth Jennings  * Copyright (C) 2014 Seth Jennings <[email protected]>
6b700e7f0SSeth Jennings  * Copyright (C) 2014 SUSE
7b700e7f0SSeth Jennings  */
8b700e7f0SSeth Jennings 
9b700e7f0SSeth Jennings #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10b700e7f0SSeth Jennings 
11b700e7f0SSeth Jennings #include <linux/module.h>
12b700e7f0SSeth Jennings #include <linux/kernel.h>
13b700e7f0SSeth Jennings #include <linux/mutex.h>
14b700e7f0SSeth Jennings #include <linux/slab.h>
15b700e7f0SSeth Jennings #include <linux/list.h>
16b700e7f0SSeth Jennings #include <linux/kallsyms.h>
17b700e7f0SSeth Jennings #include <linux/livepatch.h>
18425595a7SJessica Yu #include <linux/elf.h>
19425595a7SJessica Yu #include <linux/moduleloader.h>
203ec24776SJosh Poimboeuf #include <linux/completion.h>
219f255b63SJosh Poimboeuf #include <linux/memory.h>
22a0060505SChristoph Hellwig #include <linux/rcupdate.h>
23b56b36eeSJosh Poimboeuf #include <asm/cacheflush.h>
2410517429SJiri Kosina #include "core.h"
25c349cdcaSJosh Poimboeuf #include "patch.h"
2692c9abf5SPetr Mladek #include "state.h"
27d83a7cb3SJosh Poimboeuf #include "transition.h"
28b700e7f0SSeth Jennings 
293c33f5b9SJosh Poimboeuf /*
30d83a7cb3SJosh Poimboeuf  * klp_mutex is a coarse lock which serializes access to klp data.  All
31d83a7cb3SJosh Poimboeuf  * accesses to klp-related variables and structures must have mutex protection,
32d83a7cb3SJosh Poimboeuf  * except within the following functions which carefully avoid the need for it:
33d83a7cb3SJosh Poimboeuf  *
34d83a7cb3SJosh Poimboeuf  * - klp_ftrace_handler()
35d83a7cb3SJosh Poimboeuf  * - klp_update_patch_state()
36e3ff7c60SJosh Poimboeuf  * - __klp_sched_try_switch()
373c33f5b9SJosh Poimboeuf  */
38d83a7cb3SJosh Poimboeuf DEFINE_MUTEX(klp_mutex);
393c33f5b9SJosh Poimboeuf 
40958ef1e3SPetr Mladek /*
41958ef1e3SPetr Mladek  * Actively used patches: enabled or in transition. Note that replaced
42958ef1e3SPetr Mladek  * or disabled patches are not listed even though the related kernel
43958ef1e3SPetr Mladek  * module still can be loaded.
44958ef1e3SPetr Mladek  */
4568007289SPetr Mladek LIST_HEAD(klp_patches);
46b700e7f0SSeth Jennings 
47b700e7f0SSeth Jennings static struct kobject *klp_root_kobj;
48b700e7f0SSeth Jennings 
klp_is_module(struct klp_object * obj)49b700e7f0SSeth Jennings static bool klp_is_module(struct klp_object *obj)
50b700e7f0SSeth Jennings {
51b700e7f0SSeth Jennings 	return obj->name;
52b700e7f0SSeth Jennings }
53b700e7f0SSeth Jennings 
54b700e7f0SSeth Jennings /* sets obj->mod if object is not vmlinux and module is found */
klp_find_object_module(struct klp_object * obj)55b700e7f0SSeth Jennings static void klp_find_object_module(struct klp_object *obj)
56b700e7f0SSeth Jennings {
578cb2c2dcSPetr Mladek 	struct module *mod;
588cb2c2dcSPetr Mladek 
59b700e7f0SSeth Jennings 	if (!klp_is_module(obj))
60b700e7f0SSeth Jennings 		return;
61b700e7f0SSeth Jennings 
62*febaa65cSSebastian Andrzej Siewior 	guard(rcu)();
63b700e7f0SSeth Jennings 	/*
648cb2c2dcSPetr Mladek 	 * We do not want to block removal of patched modules and therefore
658cb2c2dcSPetr Mladek 	 * we do not take a reference here. The patches are removed by
667e545d6eSJessica Yu 	 * klp_module_going() instead.
67b700e7f0SSeth Jennings 	 */
688cb2c2dcSPetr Mladek 	mod = find_module(obj->name);
698cb2c2dcSPetr Mladek 	/*
707e545d6eSJessica Yu 	 * Do not mess work of klp_module_coming() and klp_module_going().
717e545d6eSJessica Yu 	 * Note that the patch might still be needed before klp_module_going()
728cb2c2dcSPetr Mladek 	 * is called. Module functions can be called even in the GOING state
738cb2c2dcSPetr Mladek 	 * until mod->exit() finishes. This is especially important for
748cb2c2dcSPetr Mladek 	 * patches that modify semantic of the functions.
758cb2c2dcSPetr Mladek 	 */
768cb2c2dcSPetr Mladek 	if (mod && mod->klp_alive)
778cb2c2dcSPetr Mladek 		obj->mod = mod;
78b700e7f0SSeth Jennings }
79b700e7f0SSeth Jennings 
klp_initialized(void)80b700e7f0SSeth Jennings static bool klp_initialized(void)
81b700e7f0SSeth Jennings {
82e76ff06aSNicholas Mc Guire 	return !!klp_root_kobj;
83b700e7f0SSeth Jennings }
84b700e7f0SSeth Jennings 
klp_find_func(struct klp_object * obj,struct klp_func * old_func)85e1452b60SJason Baron static struct klp_func *klp_find_func(struct klp_object *obj,
86e1452b60SJason Baron 				      struct klp_func *old_func)
87e1452b60SJason Baron {
88e1452b60SJason Baron 	struct klp_func *func;
89e1452b60SJason Baron 
90e1452b60SJason Baron 	klp_for_each_func(obj, func) {
91e1452b60SJason Baron 		if ((strcmp(old_func->old_name, func->old_name) == 0) &&
92e1452b60SJason Baron 		    (old_func->old_sympos == func->old_sympos)) {
93e1452b60SJason Baron 			return func;
94e1452b60SJason Baron 		}
95e1452b60SJason Baron 	}
96e1452b60SJason Baron 
97e1452b60SJason Baron 	return NULL;
98e1452b60SJason Baron }
99e1452b60SJason Baron 
klp_find_object(struct klp_patch * patch,struct klp_object * old_obj)100e1452b60SJason Baron static struct klp_object *klp_find_object(struct klp_patch *patch,
101e1452b60SJason Baron 					  struct klp_object *old_obj)
102e1452b60SJason Baron {
103e1452b60SJason Baron 	struct klp_object *obj;
104e1452b60SJason Baron 
105e1452b60SJason Baron 	klp_for_each_object(patch, obj) {
106e1452b60SJason Baron 		if (klp_is_module(old_obj)) {
107e1452b60SJason Baron 			if (klp_is_module(obj) &&
108e1452b60SJason Baron 			    strcmp(old_obj->name, obj->name) == 0) {
109e1452b60SJason Baron 				return obj;
110e1452b60SJason Baron 			}
111e1452b60SJason Baron 		} else if (!klp_is_module(obj)) {
112e1452b60SJason Baron 			return obj;
113e1452b60SJason Baron 		}
114e1452b60SJason Baron 	}
115e1452b60SJason Baron 
116e1452b60SJason Baron 	return NULL;
117e1452b60SJason Baron }
118e1452b60SJason Baron 
119b700e7f0SSeth Jennings struct klp_find_arg {
120b700e7f0SSeth Jennings 	const char *name;
121b700e7f0SSeth Jennings 	unsigned long addr;
122b700e7f0SSeth Jennings 	unsigned long count;
123b2b018efSChris J Arges 	unsigned long pos;
124b700e7f0SSeth Jennings };
125b700e7f0SSeth Jennings 
klp_match_callback(void * data,unsigned long addr)1269cb37357SZhen Lei static int klp_match_callback(void *data, unsigned long addr)
1279cb37357SZhen Lei {
1289cb37357SZhen Lei 	struct klp_find_arg *args = data;
1299cb37357SZhen Lei 
1309cb37357SZhen Lei 	args->addr = addr;
1319cb37357SZhen Lei 	args->count++;
1329cb37357SZhen Lei 
1339cb37357SZhen Lei 	/*
1349cb37357SZhen Lei 	 * Finish the search when the symbol is found for the desired position
1359cb37357SZhen Lei 	 * or the position is not defined for a non-unique symbol.
1369cb37357SZhen Lei 	 */
1379cb37357SZhen Lei 	if ((args->pos && (args->count == args->pos)) ||
1389cb37357SZhen Lei 	    (!args->pos && (args->count > 1)))
1399cb37357SZhen Lei 		return 1;
1409cb37357SZhen Lei 
1419cb37357SZhen Lei 	return 0;
1429cb37357SZhen Lei }
1439cb37357SZhen Lei 
klp_find_callback(void * data,const char * name,unsigned long addr)1443703bd54SZhen Lei static int klp_find_callback(void *data, const char *name, unsigned long addr)
1454f1354d5SZhen Lei {
1464f1354d5SZhen Lei 	struct klp_find_arg *args = data;
1474f1354d5SZhen Lei 
1484f1354d5SZhen Lei 	if (strcmp(args->name, name))
1494f1354d5SZhen Lei 		return 0;
1504f1354d5SZhen Lei 
1514f1354d5SZhen Lei 	return klp_match_callback(data, addr);
1524f1354d5SZhen Lei }
1534f1354d5SZhen Lei 
klp_find_object_symbol(const char * objname,const char * name,unsigned long sympos,unsigned long * addr)154b700e7f0SSeth Jennings static int klp_find_object_symbol(const char *objname, const char *name,
155b2b018efSChris J Arges 				  unsigned long sympos, unsigned long *addr)
156b700e7f0SSeth Jennings {
157b700e7f0SSeth Jennings 	struct klp_find_arg args = {
158b700e7f0SSeth Jennings 		.name = name,
159b700e7f0SSeth Jennings 		.addr = 0,
160b2b018efSChris J Arges 		.count = 0,
161b2b018efSChris J Arges 		.pos = sympos,
162b700e7f0SSeth Jennings 	};
163b700e7f0SSeth Jennings 
16472f04b50SZhou Chengming 	if (objname)
16507cc2c93SZhen Lei 		module_kallsyms_on_each_symbol(objname, klp_find_callback, &args);
16672f04b50SZhou Chengming 	else
1679cb37357SZhen Lei 		kallsyms_on_each_match_symbol(klp_match_callback, name, &args);
168b700e7f0SSeth Jennings 
169b2b018efSChris J Arges 	/*
170b2b018efSChris J Arges 	 * Ensure an address was found. If sympos is 0, ensure symbol is unique;
171b2b018efSChris J Arges 	 * otherwise ensure the symbol position count matches sympos.
172b2b018efSChris J Arges 	 */
173b2b018efSChris J Arges 	if (args.addr == 0)
174b700e7f0SSeth Jennings 		pr_err("symbol '%s' not found in symbol table\n", name);
175b2b018efSChris J Arges 	else if (args.count > 1 && sympos == 0) {
176f995b5f7SPetr Mladek 		pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n",
177f995b5f7SPetr Mladek 		       name, objname);
178b2b018efSChris J Arges 	} else if (sympos != args.count && sympos > 0) {
179b2b018efSChris J Arges 		pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
180b2b018efSChris J Arges 		       sympos, name, objname ? objname : "vmlinux");
181b2b018efSChris J Arges 	} else {
182b700e7f0SSeth Jennings 		*addr = args.addr;
183b700e7f0SSeth Jennings 		return 0;
184b700e7f0SSeth Jennings 	}
185b700e7f0SSeth Jennings 
186b700e7f0SSeth Jennings 	*addr = 0;
187b700e7f0SSeth Jennings 	return -EINVAL;
188b700e7f0SSeth Jennings }
189b700e7f0SSeth Jennings 
klp_resolve_symbols(Elf_Shdr * sechdrs,const char * strtab,unsigned int symndx,Elf_Shdr * relasec,const char * sec_objname)1902f293651SChristophe Leroy static int klp_resolve_symbols(Elf_Shdr *sechdrs, const char *strtab,
191ca376a93SJosh Poimboeuf 			       unsigned int symndx, Elf_Shdr *relasec,
192ca376a93SJosh Poimboeuf 			       const char *sec_objname)
193b700e7f0SSeth Jennings {
194ca376a93SJosh Poimboeuf 	int i, cnt, ret;
195ca376a93SJosh Poimboeuf 	char sym_objname[MODULE_NAME_LEN];
196ca376a93SJosh Poimboeuf 	char sym_name[KSYM_NAME_LEN];
197425595a7SJessica Yu 	Elf_Rela *relas;
198425595a7SJessica Yu 	Elf_Sym *sym;
199425595a7SJessica Yu 	unsigned long sympos, addr;
200ca376a93SJosh Poimboeuf 	bool sym_vmlinux;
201ca376a93SJosh Poimboeuf 	bool sec_vmlinux = !strcmp(sec_objname, "vmlinux");
202b700e7f0SSeth Jennings 
203b2b018efSChris J Arges 	/*
204ca376a93SJosh Poimboeuf 	 * Since the field widths for sym_objname and sym_name in the sscanf()
205425595a7SJessica Yu 	 * call are hard-coded and correspond to MODULE_NAME_LEN and
206425595a7SJessica Yu 	 * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
207425595a7SJessica Yu 	 * and KSYM_NAME_LEN have the values we expect them to have.
208425595a7SJessica Yu 	 *
209425595a7SJessica Yu 	 * Because the value of MODULE_NAME_LEN can differ among architectures,
210425595a7SJessica Yu 	 * we use the smallest/strictest upper bound possible (56, based on
211425595a7SJessica Yu 	 * the current definition of MODULE_NAME_LEN) to prevent overflows.
212b2b018efSChris J Arges 	 */
213b8a94bfbSMiguel Ojeda 	BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 512);
214425595a7SJessica Yu 
215425595a7SJessica Yu 	relas = (Elf_Rela *) relasec->sh_addr;
216425595a7SJessica Yu 	/* For each rela in this klp relocation section */
217425595a7SJessica Yu 	for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
2182f293651SChristophe Leroy 		sym = (Elf_Sym *)sechdrs[symndx].sh_addr + ELF_R_SYM(relas[i].r_info);
219425595a7SJessica Yu 		if (sym->st_shndx != SHN_LIVEPATCH) {
22077f8f39aSJosh Poimboeuf 			pr_err("symbol %s is not marked as a livepatch symbol\n",
221425595a7SJessica Yu 			       strtab + sym->st_name);
222425595a7SJessica Yu 			return -EINVAL;
223425595a7SJessica Yu 		}
224425595a7SJessica Yu 
225ca376a93SJosh Poimboeuf 		/* Format: .klp.sym.sym_objname.sym_name,sympos */
226425595a7SJessica Yu 		cnt = sscanf(strtab + sym->st_name,
227b8a94bfbSMiguel Ojeda 			     ".klp.sym.%55[^.].%511[^,],%lu",
228ca376a93SJosh Poimboeuf 			     sym_objname, sym_name, &sympos);
229425595a7SJessica Yu 		if (cnt != 3) {
23077f8f39aSJosh Poimboeuf 			pr_err("symbol %s has an incorrectly formatted name\n",
231425595a7SJessica Yu 			       strtab + sym->st_name);
232425595a7SJessica Yu 			return -EINVAL;
233425595a7SJessica Yu 		}
234425595a7SJessica Yu 
235ca376a93SJosh Poimboeuf 		sym_vmlinux = !strcmp(sym_objname, "vmlinux");
236ca376a93SJosh Poimboeuf 
237ca376a93SJosh Poimboeuf 		/*
238ca376a93SJosh Poimboeuf 		 * Prevent module-specific KLP rela sections from referencing
239ca376a93SJosh Poimboeuf 		 * vmlinux symbols.  This helps prevent ordering issues with
240ca376a93SJosh Poimboeuf 		 * module special section initializations.  Presumably such
241ca376a93SJosh Poimboeuf 		 * symbols are exported and normal relas can be used instead.
242ca376a93SJosh Poimboeuf 		 */
243ca376a93SJosh Poimboeuf 		if (!sec_vmlinux && sym_vmlinux) {
24467e18e13SZheng Yejian 			pr_err("invalid access to vmlinux symbol '%s' from module-specific livepatch relocation section\n",
245ca376a93SJosh Poimboeuf 			       sym_name);
246ca376a93SJosh Poimboeuf 			return -EINVAL;
247ca376a93SJosh Poimboeuf 		}
248ca376a93SJosh Poimboeuf 
249425595a7SJessica Yu 		/* klp_find_object_symbol() treats a NULL objname as vmlinux */
250ca376a93SJosh Poimboeuf 		ret = klp_find_object_symbol(sym_vmlinux ? NULL : sym_objname,
251ca376a93SJosh Poimboeuf 					     sym_name, sympos, &addr);
252425595a7SJessica Yu 		if (ret)
253425595a7SJessica Yu 			return ret;
254425595a7SJessica Yu 
255425595a7SJessica Yu 		sym->st_value = addr;
256425595a7SJessica Yu 	}
257425595a7SJessica Yu 
258425595a7SJessica Yu 	return 0;
259b700e7f0SSeth Jennings }
260b700e7f0SSeth Jennings 
clear_relocate_add(Elf_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)2610c05e7bdSSong Liu void __weak clear_relocate_add(Elf_Shdr *sechdrs,
2620c05e7bdSSong Liu 		   const char *strtab,
2630c05e7bdSSong Liu 		   unsigned int symindex,
2640c05e7bdSSong Liu 		   unsigned int relsec,
2650c05e7bdSSong Liu 		   struct module *me)
2660c05e7bdSSong Liu {
2670c05e7bdSSong Liu }
2680c05e7bdSSong Liu 
2697c8e2bddSJosh Poimboeuf /*
2707c8e2bddSJosh Poimboeuf  * At a high-level, there are two types of klp relocation sections: those which
2717c8e2bddSJosh Poimboeuf  * reference symbols which live in vmlinux; and those which reference symbols
2727c8e2bddSJosh Poimboeuf  * which live in other modules.  This function is called for both types:
2737c8e2bddSJosh Poimboeuf  *
2747c8e2bddSJosh Poimboeuf  * 1) When a klp module itself loads, the module code calls this function to
2757c8e2bddSJosh Poimboeuf  *    write vmlinux-specific klp relocations (.klp.rela.vmlinux.* sections).
2767c8e2bddSJosh Poimboeuf  *    These relocations are written to the klp module text to allow the patched
2777c8e2bddSJosh Poimboeuf  *    code/data to reference unexported vmlinux symbols.  They're written as
2787c8e2bddSJosh Poimboeuf  *    early as possible to ensure that other module init code (.e.g.,
2797c8e2bddSJosh Poimboeuf  *    jump_label_apply_nops) can access any unexported vmlinux symbols which
2807c8e2bddSJosh Poimboeuf  *    might be referenced by the klp module's special sections.
2817c8e2bddSJosh Poimboeuf  *
2827c8e2bddSJosh Poimboeuf  * 2) When a to-be-patched module loads -- or is already loaded when a
2837c8e2bddSJosh Poimboeuf  *    corresponding klp module loads -- klp code calls this function to write
2847c8e2bddSJosh Poimboeuf  *    module-specific klp relocations (.klp.rela.{module}.* sections).  These
2857c8e2bddSJosh Poimboeuf  *    are written to the klp module text to allow the patched code/data to
2867c8e2bddSJosh Poimboeuf  *    reference symbols which live in the to-be-patched module or one of its
2877c8e2bddSJosh Poimboeuf  *    module dependencies.  Exported symbols are supported, in addition to
2887c8e2bddSJosh Poimboeuf  *    unexported symbols, in order to enable late module patching, which allows
2897c8e2bddSJosh Poimboeuf  *    the to-be-patched module to be loaded and patched sometime *after* the
2907c8e2bddSJosh Poimboeuf  *    klp module is loaded.
2917c8e2bddSJosh Poimboeuf  */
klp_write_section_relocs(struct module * pmod,Elf_Shdr * sechdrs,const char * shstrtab,const char * strtab,unsigned int symndx,unsigned int secndx,const char * objname,bool apply)2920c05e7bdSSong Liu static int klp_write_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
2937c8e2bddSJosh Poimboeuf 				    const char *shstrtab, const char *strtab,
2947c8e2bddSJosh Poimboeuf 				    unsigned int symndx, unsigned int secndx,
2950c05e7bdSSong Liu 				    const char *objname, bool apply)
296b700e7f0SSeth Jennings {
2977c8e2bddSJosh Poimboeuf 	int cnt, ret;
298425595a7SJessica Yu 	char sec_objname[MODULE_NAME_LEN];
2997c8e2bddSJosh Poimboeuf 	Elf_Shdr *sec = sechdrs + secndx;
300b56b36eeSJosh Poimboeuf 
301425595a7SJessica Yu 	/*
302425595a7SJessica Yu 	 * Format: .klp.rela.sec_objname.section_name
303425595a7SJessica Yu 	 * See comment in klp_resolve_symbols() for an explanation
304425595a7SJessica Yu 	 * of the selected field width value.
305425595a7SJessica Yu 	 */
3067c8e2bddSJosh Poimboeuf 	cnt = sscanf(shstrtab + sec->sh_name, ".klp.rela.%55[^.]",
3077c8e2bddSJosh Poimboeuf 		     sec_objname);
308425595a7SJessica Yu 	if (cnt != 1) {
30977f8f39aSJosh Poimboeuf 		pr_err("section %s has an incorrectly formatted name\n",
3107c8e2bddSJosh Poimboeuf 		       shstrtab + sec->sh_name);
3117c8e2bddSJosh Poimboeuf 		return -EINVAL;
312b700e7f0SSeth Jennings 	}
313425595a7SJessica Yu 
3147c8e2bddSJosh Poimboeuf 	if (strcmp(objname ? objname : "vmlinux", sec_objname))
3157c8e2bddSJosh Poimboeuf 		return 0;
316425595a7SJessica Yu 
3170c05e7bdSSong Liu 	if (apply) {
3180c05e7bdSSong Liu 		ret = klp_resolve_symbols(sechdrs, strtab, symndx,
3190c05e7bdSSong Liu 					  sec, sec_objname);
320064c89dfSChris J Arges 		if (ret)
321b56b36eeSJosh Poimboeuf 			return ret;
3227c8e2bddSJosh Poimboeuf 
3237c8e2bddSJosh Poimboeuf 		return apply_relocate_add(sechdrs, strtab, symndx, secndx, pmod);
324b700e7f0SSeth Jennings 	}
325b700e7f0SSeth Jennings 
3260c05e7bdSSong Liu 	clear_relocate_add(sechdrs, strtab, symndx, secndx, pmod);
3270c05e7bdSSong Liu 	return 0;
3280c05e7bdSSong Liu }
3290c05e7bdSSong Liu 
klp_apply_section_relocs(struct module * pmod,Elf_Shdr * sechdrs,const char * shstrtab,const char * strtab,unsigned int symndx,unsigned int secndx,const char * objname)3300c05e7bdSSong Liu int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
3310c05e7bdSSong Liu 			     const char *shstrtab, const char *strtab,
3320c05e7bdSSong Liu 			     unsigned int symndx, unsigned int secndx,
3330c05e7bdSSong Liu 			     const char *objname)
3340c05e7bdSSong Liu {
3350c05e7bdSSong Liu 	return klp_write_section_relocs(pmod, sechdrs, shstrtab, strtab, symndx,
3360c05e7bdSSong Liu 					secndx, objname, true);
3370c05e7bdSSong Liu }
3380c05e7bdSSong Liu 
339b700e7f0SSeth Jennings /*
340b700e7f0SSeth Jennings  * Sysfs Interface
341b700e7f0SSeth Jennings  *
342b700e7f0SSeth Jennings  * /sys/kernel/livepatch
343b700e7f0SSeth Jennings  * /sys/kernel/livepatch/<patch>
344b700e7f0SSeth Jennings  * /sys/kernel/livepatch/<patch>/enabled
345d83a7cb3SJosh Poimboeuf  * /sys/kernel/livepatch/<patch>/transition
346c99a2be7SMiroslav Benes  * /sys/kernel/livepatch/<patch>/force
347adb68ed2SYafang Shao  * /sys/kernel/livepatch/<patch>/replace
3483dae09deSWardenjohn  * /sys/kernel/livepatch/<patch>/stack_order
349b700e7f0SSeth Jennings  * /sys/kernel/livepatch/<patch>/<object>
350bb26cfd9SSong Liu  * /sys/kernel/livepatch/<patch>/<object>/patched
351444f9e99SChris J Arges  * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
352b700e7f0SSeth Jennings  */
35326c3e98eSPetr Mladek static int __klp_disable_patch(struct klp_patch *patch);
354b700e7f0SSeth Jennings 
enabled_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)355b700e7f0SSeth Jennings static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
356b700e7f0SSeth Jennings 			     const char *buf, size_t count)
357b700e7f0SSeth Jennings {
358b700e7f0SSeth Jennings 	struct klp_patch *patch;
359b700e7f0SSeth Jennings 	int ret;
36068ae4b2bSJosh Poimboeuf 	bool enabled;
361b700e7f0SSeth Jennings 
36268ae4b2bSJosh Poimboeuf 	ret = kstrtobool(buf, &enabled);
363b700e7f0SSeth Jennings 	if (ret)
36468ae4b2bSJosh Poimboeuf 		return ret;
365b700e7f0SSeth Jennings 
366b700e7f0SSeth Jennings 	patch = container_of(kobj, struct klp_patch, kobj);
367b700e7f0SSeth Jennings 
368b700e7f0SSeth Jennings 	mutex_lock(&klp_mutex);
369b700e7f0SSeth Jennings 
37068ae4b2bSJosh Poimboeuf 	if (patch->enabled == enabled) {
371b700e7f0SSeth Jennings 		/* already in requested state */
372b700e7f0SSeth Jennings 		ret = -EINVAL;
373958ef1e3SPetr Mladek 		goto out;
374b700e7f0SSeth Jennings 	}
375b700e7f0SSeth Jennings 
376958ef1e3SPetr Mladek 	/*
377958ef1e3SPetr Mladek 	 * Allow to reverse a pending transition in both ways. It might be
378958ef1e3SPetr Mladek 	 * necessary to complete the transition without forcing and breaking
379958ef1e3SPetr Mladek 	 * the system integrity.
380958ef1e3SPetr Mladek 	 *
381958ef1e3SPetr Mladek 	 * Do not allow to re-enable a disabled patch.
382958ef1e3SPetr Mladek 	 */
383958ef1e3SPetr Mladek 	if (patch == klp_transition_patch)
384d83a7cb3SJosh Poimboeuf 		klp_reverse_transition();
385958ef1e3SPetr Mladek 	else if (!enabled)
386b700e7f0SSeth Jennings 		ret = __klp_disable_patch(patch);
387958ef1e3SPetr Mladek 	else
388958ef1e3SPetr Mladek 		ret = -EINVAL;
389958ef1e3SPetr Mladek 
390958ef1e3SPetr Mladek out:
391958ef1e3SPetr Mladek 	mutex_unlock(&klp_mutex);
392958ef1e3SPetr Mladek 
393b700e7f0SSeth Jennings 	if (ret)
394b700e7f0SSeth Jennings 		return ret;
395958ef1e3SPetr Mladek 	return count;
396b700e7f0SSeth Jennings }
397b700e7f0SSeth Jennings 
enabled_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)398b700e7f0SSeth Jennings static ssize_t enabled_show(struct kobject *kobj,
399b700e7f0SSeth Jennings 			    struct kobj_attribute *attr, char *buf)
400b700e7f0SSeth Jennings {
401b700e7f0SSeth Jennings 	struct klp_patch *patch;
402b700e7f0SSeth Jennings 
403b700e7f0SSeth Jennings 	patch = container_of(kobj, struct klp_patch, kobj);
40492052692SYafang Shao 	return sysfs_emit(buf, "%d\n", patch->enabled);
405b700e7f0SSeth Jennings }
406b700e7f0SSeth Jennings 
transition_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)407d83a7cb3SJosh Poimboeuf static ssize_t transition_show(struct kobject *kobj,
408d83a7cb3SJosh Poimboeuf 			       struct kobj_attribute *attr, char *buf)
409d83a7cb3SJosh Poimboeuf {
410d83a7cb3SJosh Poimboeuf 	struct klp_patch *patch;
411d83a7cb3SJosh Poimboeuf 
412d83a7cb3SJosh Poimboeuf 	patch = container_of(kobj, struct klp_patch, kobj);
41392052692SYafang Shao 	return sysfs_emit(buf, "%d\n", patch == klp_transition_patch);
414b700e7f0SSeth Jennings }
415b700e7f0SSeth Jennings 
force_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)416c99a2be7SMiroslav Benes static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
417c99a2be7SMiroslav Benes 			   const char *buf, size_t count)
418c99a2be7SMiroslav Benes {
419c99a2be7SMiroslav Benes 	struct klp_patch *patch;
420c99a2be7SMiroslav Benes 	int ret;
421c99a2be7SMiroslav Benes 	bool val;
422c99a2be7SMiroslav Benes 
423c99a2be7SMiroslav Benes 	ret = kstrtobool(buf, &val);
424c99a2be7SMiroslav Benes 	if (ret)
425c99a2be7SMiroslav Benes 		return ret;
426c99a2be7SMiroslav Benes 
4278869016dSMiroslav Benes 	if (!val)
4288869016dSMiroslav Benes 		return count;
4298869016dSMiroslav Benes 
4308869016dSMiroslav Benes 	mutex_lock(&klp_mutex);
4318869016dSMiroslav Benes 
4328869016dSMiroslav Benes 	patch = container_of(kobj, struct klp_patch, kobj);
4338869016dSMiroslav Benes 	if (patch != klp_transition_patch) {
4348869016dSMiroslav Benes 		mutex_unlock(&klp_mutex);
4358869016dSMiroslav Benes 		return -EINVAL;
4368869016dSMiroslav Benes 	}
4378869016dSMiroslav Benes 
438c99a2be7SMiroslav Benes 	klp_force_transition();
439c99a2be7SMiroslav Benes 
4408869016dSMiroslav Benes 	mutex_unlock(&klp_mutex);
4418869016dSMiroslav Benes 
442c99a2be7SMiroslav Benes 	return count;
443c99a2be7SMiroslav Benes }
444c99a2be7SMiroslav Benes 
replace_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)445adb68ed2SYafang Shao static ssize_t replace_show(struct kobject *kobj,
446adb68ed2SYafang Shao 			    struct kobj_attribute *attr, char *buf)
447adb68ed2SYafang Shao {
448adb68ed2SYafang Shao 	struct klp_patch *patch;
449adb68ed2SYafang Shao 
450adb68ed2SYafang Shao 	patch = container_of(kobj, struct klp_patch, kobj);
451adb68ed2SYafang Shao 	return sysfs_emit(buf, "%d\n", patch->replace);
452adb68ed2SYafang Shao }
453adb68ed2SYafang Shao 
stack_order_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)4543dae09deSWardenjohn static ssize_t stack_order_show(struct kobject *kobj,
4553dae09deSWardenjohn 				struct kobj_attribute *attr, char *buf)
4563dae09deSWardenjohn {
4573dae09deSWardenjohn 	struct klp_patch *patch, *this_patch;
4583dae09deSWardenjohn 	int stack_order = 0;
4593dae09deSWardenjohn 
4603dae09deSWardenjohn 	this_patch = container_of(kobj, struct klp_patch, kobj);
4613dae09deSWardenjohn 
4623dae09deSWardenjohn 	mutex_lock(&klp_mutex);
4633dae09deSWardenjohn 
4643dae09deSWardenjohn 	klp_for_each_patch(patch) {
4653dae09deSWardenjohn 		stack_order++;
4663dae09deSWardenjohn 		if (patch == this_patch)
4673dae09deSWardenjohn 			break;
4683dae09deSWardenjohn 	}
4693dae09deSWardenjohn 
4703dae09deSWardenjohn 	mutex_unlock(&klp_mutex);
4713dae09deSWardenjohn 
4723dae09deSWardenjohn 	return sysfs_emit(buf, "%d\n", stack_order);
4733dae09deSWardenjohn }
4743dae09deSWardenjohn 
475b700e7f0SSeth Jennings static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
476d83a7cb3SJosh Poimboeuf static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition);
477c99a2be7SMiroslav Benes static struct kobj_attribute force_kobj_attr = __ATTR_WO(force);
478adb68ed2SYafang Shao static struct kobj_attribute replace_kobj_attr = __ATTR_RO(replace);
4793dae09deSWardenjohn static struct kobj_attribute stack_order_kobj_attr = __ATTR_RO(stack_order);
480b700e7f0SSeth Jennings static struct attribute *klp_patch_attrs[] = {
481b700e7f0SSeth Jennings 	&enabled_kobj_attr.attr,
482d83a7cb3SJosh Poimboeuf 	&transition_kobj_attr.attr,
483c99a2be7SMiroslav Benes 	&force_kobj_attr.attr,
484adb68ed2SYafang Shao 	&replace_kobj_attr.attr,
4853dae09deSWardenjohn 	&stack_order_kobj_attr.attr,
486b700e7f0SSeth Jennings 	NULL
487b700e7f0SSeth Jennings };
48870283454SKimberly Brown ATTRIBUTE_GROUPS(klp_patch);
489b700e7f0SSeth Jennings 
patched_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)490bb26cfd9SSong Liu static ssize_t patched_show(struct kobject *kobj,
491bb26cfd9SSong Liu 			    struct kobj_attribute *attr, char *buf)
492bb26cfd9SSong Liu {
493bb26cfd9SSong Liu 	struct klp_object *obj;
494bb26cfd9SSong Liu 
495bb26cfd9SSong Liu 	obj = container_of(kobj, struct klp_object, kobj);
496bb26cfd9SSong Liu 	return sysfs_emit(buf, "%d\n", obj->patched);
497bb26cfd9SSong Liu }
498bb26cfd9SSong Liu 
499bb26cfd9SSong Liu static struct kobj_attribute patched_kobj_attr = __ATTR_RO(patched);
500bb26cfd9SSong Liu static struct attribute *klp_object_attrs[] = {
501bb26cfd9SSong Liu 	&patched_kobj_attr.attr,
502bb26cfd9SSong Liu 	NULL,
503bb26cfd9SSong Liu };
504bb26cfd9SSong Liu ATTRIBUTE_GROUPS(klp_object);
505bb26cfd9SSong Liu 
klp_free_object_dynamic(struct klp_object * obj)506e1452b60SJason Baron static void klp_free_object_dynamic(struct klp_object *obj)
507e1452b60SJason Baron {
508e1452b60SJason Baron 	kfree(obj->name);
509e1452b60SJason Baron 	kfree(obj);
510e1452b60SJason Baron }
511e1452b60SJason Baron 
512f68d67cfSPetr Mladek static void klp_init_func_early(struct klp_object *obj,
513f68d67cfSPetr Mladek 				struct klp_func *func);
514f68d67cfSPetr Mladek static void klp_init_object_early(struct klp_patch *patch,
515f68d67cfSPetr Mladek 				  struct klp_object *obj);
5164d141ab3SPetr Mladek 
klp_alloc_object_dynamic(const char * name,struct klp_patch * patch)517f68d67cfSPetr Mladek static struct klp_object *klp_alloc_object_dynamic(const char *name,
518f68d67cfSPetr Mladek 						   struct klp_patch *patch)
519e1452b60SJason Baron {
520e1452b60SJason Baron 	struct klp_object *obj;
521e1452b60SJason Baron 
522e1452b60SJason Baron 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
523e1452b60SJason Baron 	if (!obj)
524e1452b60SJason Baron 		return NULL;
525e1452b60SJason Baron 
526e1452b60SJason Baron 	if (name) {
527e1452b60SJason Baron 		obj->name = kstrdup(name, GFP_KERNEL);
528e1452b60SJason Baron 		if (!obj->name) {
529e1452b60SJason Baron 			kfree(obj);
530e1452b60SJason Baron 			return NULL;
531e1452b60SJason Baron 		}
532e1452b60SJason Baron 	}
533e1452b60SJason Baron 
534f68d67cfSPetr Mladek 	klp_init_object_early(patch, obj);
535e1452b60SJason Baron 	obj->dynamic = true;
536e1452b60SJason Baron 
537e1452b60SJason Baron 	return obj;
538e1452b60SJason Baron }
539e1452b60SJason Baron 
klp_free_func_nop(struct klp_func * func)540e1452b60SJason Baron static void klp_free_func_nop(struct klp_func *func)
541e1452b60SJason Baron {
542e1452b60SJason Baron 	kfree(func->old_name);
543e1452b60SJason Baron 	kfree(func);
544e1452b60SJason Baron }
545e1452b60SJason Baron 
klp_alloc_func_nop(struct klp_func * old_func,struct klp_object * obj)546e1452b60SJason Baron static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func,
547e1452b60SJason Baron 					   struct klp_object *obj)
548e1452b60SJason Baron {
549e1452b60SJason Baron 	struct klp_func *func;
550e1452b60SJason Baron 
551e1452b60SJason Baron 	func = kzalloc(sizeof(*func), GFP_KERNEL);
552e1452b60SJason Baron 	if (!func)
553e1452b60SJason Baron 		return NULL;
554e1452b60SJason Baron 
555e1452b60SJason Baron 	if (old_func->old_name) {
556e1452b60SJason Baron 		func->old_name = kstrdup(old_func->old_name, GFP_KERNEL);
557e1452b60SJason Baron 		if (!func->old_name) {
558e1452b60SJason Baron 			kfree(func);
559e1452b60SJason Baron 			return NULL;
560e1452b60SJason Baron 		}
561e1452b60SJason Baron 	}
562e1452b60SJason Baron 
563f68d67cfSPetr Mladek 	klp_init_func_early(obj, func);
564e1452b60SJason Baron 	/*
565e1452b60SJason Baron 	 * func->new_func is same as func->old_func. These addresses are
566e1452b60SJason Baron 	 * set when the object is loaded, see klp_init_object_loaded().
567e1452b60SJason Baron 	 */
568e1452b60SJason Baron 	func->old_sympos = old_func->old_sympos;
569e1452b60SJason Baron 	func->nop = true;
570e1452b60SJason Baron 
571e1452b60SJason Baron 	return func;
572e1452b60SJason Baron }
573e1452b60SJason Baron 
klp_add_object_nops(struct klp_patch * patch,struct klp_object * old_obj)574e1452b60SJason Baron static int klp_add_object_nops(struct klp_patch *patch,
575e1452b60SJason Baron 			       struct klp_object *old_obj)
576e1452b60SJason Baron {
577e1452b60SJason Baron 	struct klp_object *obj;
578e1452b60SJason Baron 	struct klp_func *func, *old_func;
579e1452b60SJason Baron 
580e1452b60SJason Baron 	obj = klp_find_object(patch, old_obj);
581e1452b60SJason Baron 
582e1452b60SJason Baron 	if (!obj) {
583f68d67cfSPetr Mladek 		obj = klp_alloc_object_dynamic(old_obj->name, patch);
584e1452b60SJason Baron 		if (!obj)
585e1452b60SJason Baron 			return -ENOMEM;
586e1452b60SJason Baron 	}
587e1452b60SJason Baron 
588e1452b60SJason Baron 	klp_for_each_func(old_obj, old_func) {
589e1452b60SJason Baron 		func = klp_find_func(obj, old_func);
590e1452b60SJason Baron 		if (func)
591e1452b60SJason Baron 			continue;
592e1452b60SJason Baron 
593e1452b60SJason Baron 		func = klp_alloc_func_nop(old_func, obj);
594e1452b60SJason Baron 		if (!func)
595e1452b60SJason Baron 			return -ENOMEM;
596e1452b60SJason Baron 	}
597e1452b60SJason Baron 
598e1452b60SJason Baron 	return 0;
599e1452b60SJason Baron }
600e1452b60SJason Baron 
601e1452b60SJason Baron /*
602e1452b60SJason Baron  * Add 'nop' functions which simply return to the caller to run the
603e1452b60SJason Baron  * original function.
604e1452b60SJason Baron  *
605e1452b60SJason Baron  * They are added only when the atomic replace mode is used and only for
606e1452b60SJason Baron  * functions which are currently livepatched but are no longer included
607e1452b60SJason Baron  * in the new livepatch.
608e1452b60SJason Baron  */
klp_add_nops(struct klp_patch * patch)609e1452b60SJason Baron static int klp_add_nops(struct klp_patch *patch)
610e1452b60SJason Baron {
611ecba29f4SPetr Mladek 	struct klp_patch *old_patch;
612e1452b60SJason Baron 	struct klp_object *old_obj;
613e1452b60SJason Baron 
614e1452b60SJason Baron 	klp_for_each_patch(old_patch) {
615e1452b60SJason Baron 		klp_for_each_object(old_patch, old_obj) {
616e1452b60SJason Baron 			int err;
617e1452b60SJason Baron 
618e1452b60SJason Baron 			err = klp_add_object_nops(patch, old_obj);
619e1452b60SJason Baron 			if (err)
620e1452b60SJason Baron 				return err;
621e1452b60SJason Baron 		}
622e1452b60SJason Baron 	}
623e1452b60SJason Baron 
624b700e7f0SSeth Jennings 	return 0;
625b700e7f0SSeth Jennings }
6263ec24776SJosh Poimboeuf 
klp_kobj_release_patch(struct kobject * kobj)6273ec24776SJosh Poimboeuf static void klp_kobj_release_patch(struct kobject *kobj)
6283ec24776SJosh Poimboeuf {
6293ec24776SJosh Poimboeuf 	struct klp_patch *patch;
630b700e7f0SSeth Jennings 
631b700e7f0SSeth Jennings 	patch = container_of(kobj, struct klp_patch, kobj);
6321b47b80eSThomas Weißschuh 	complete(&patch->finish);
633b700e7f0SSeth Jennings }
634b700e7f0SSeth Jennings 
63570283454SKimberly Brown static const struct kobj_type klp_ktype_patch = {
636b700e7f0SSeth Jennings 	.release = klp_kobj_release_patch,
637b700e7f0SSeth Jennings 	.sysfs_ops = &kobj_sysfs_ops,
638cad706dfSMiroslav Benes 	.default_groups = klp_patch_groups,
639cad706dfSMiroslav Benes };
640e1452b60SJason Baron 
klp_kobj_release_object(struct kobject * kobj)641e1452b60SJason Baron static void klp_kobj_release_object(struct kobject *kobj)
642e1452b60SJason Baron {
643e1452b60SJason Baron 	struct klp_object *obj;
644e1452b60SJason Baron 
645e1452b60SJason Baron 	obj = container_of(kobj, struct klp_object, kobj);
646cad706dfSMiroslav Benes 
647cad706dfSMiroslav Benes 	if (obj->dynamic)
6481b47b80eSThomas Weißschuh 		klp_free_object_dynamic(obj);
649cad706dfSMiroslav Benes }
650cad706dfSMiroslav Benes 
651bb26cfd9SSong Liu static const struct kobj_type klp_ktype_object = {
652cad706dfSMiroslav Benes 	.release = klp_kobj_release_object,
653cad706dfSMiroslav Benes 	.sysfs_ops = &kobj_sysfs_ops,
654b700e7f0SSeth Jennings 	.default_groups = klp_object_groups,
655b700e7f0SSeth Jennings };
656e1452b60SJason Baron 
klp_kobj_release_func(struct kobject * kobj)657e1452b60SJason Baron static void klp_kobj_release_func(struct kobject *kobj)
658e1452b60SJason Baron {
659e1452b60SJason Baron 	struct klp_func *func;
660e1452b60SJason Baron 
661e1452b60SJason Baron 	func = container_of(kobj, struct klp_func, kobj);
662b700e7f0SSeth Jennings 
663b700e7f0SSeth Jennings 	if (func->nop)
6641b47b80eSThomas Weißschuh 		klp_free_func_nop(func);
665b700e7f0SSeth Jennings }
666b700e7f0SSeth Jennings 
667b700e7f0SSeth Jennings static const struct kobj_type klp_ktype_func = {
668b700e7f0SSeth Jennings 	.release = klp_kobj_release_func,
669d697bad5SPetr Mladek 	.sysfs_ops = &kobj_sysfs_ops,
670b700e7f0SSeth Jennings };
671e1452b60SJason Baron 
__klp_free_funcs(struct klp_object * obj,bool nops_only)672b700e7f0SSeth Jennings static void __klp_free_funcs(struct klp_object *obj, bool nops_only)
673e1452b60SJason Baron {
674d697bad5SPetr Mladek 	struct klp_func *func, *tmp_func;
675d697bad5SPetr Mladek 
676d697bad5SPetr Mladek 	klp_for_each_func_safe(obj, func, tmp_func) {
677d697bad5SPetr Mladek 		if (nops_only && !func->nop)
678b700e7f0SSeth Jennings 			continue;
679b700e7f0SSeth Jennings 
6800430f78bSPetr Mladek 		list_del(&func->node);
681b700e7f0SSeth Jennings 		kobject_put(&func->kobj);
682b700e7f0SSeth Jennings 	}
683b700e7f0SSeth Jennings }
684b700e7f0SSeth Jennings 
685b700e7f0SSeth Jennings /* Clean up when a patched object is unloaded */
klp_free_object_loaded(struct klp_object * obj)686b700e7f0SSeth Jennings static void klp_free_object_loaded(struct klp_object *obj)
687b700e7f0SSeth Jennings {
688b700e7f0SSeth Jennings 	struct klp_func *func;
689e1452b60SJason Baron 
69019514910SPetr Mladek 	obj->mod = NULL;
691e1452b60SJason Baron 
692e1452b60SJason Baron 	klp_for_each_func(obj, func) {
693e1452b60SJason Baron 		func->old_func = NULL;
694e1452b60SJason Baron 
695b700e7f0SSeth Jennings 		if (func->nop)
696b700e7f0SSeth Jennings 			func->new_func = NULL;
697d697bad5SPetr Mladek 	}
698b700e7f0SSeth Jennings }
699e1452b60SJason Baron 
__klp_free_objects(struct klp_patch * patch,bool nops_only)700b700e7f0SSeth Jennings static void __klp_free_objects(struct klp_patch *patch, bool nops_only)
701e1452b60SJason Baron {
702d697bad5SPetr Mladek 	struct klp_object *obj, *tmp_obj;
703d697bad5SPetr Mladek 
704d697bad5SPetr Mladek 	klp_for_each_object_safe(patch, obj, tmp_obj) {
705d697bad5SPetr Mladek 		__klp_free_funcs(obj, nops_only);
706d697bad5SPetr Mladek 
707d697bad5SPetr Mladek 		if (nops_only && !obj->dynamic)
708cad706dfSMiroslav Benes 			continue;
709b700e7f0SSeth Jennings 
710b700e7f0SSeth Jennings 		list_del(&obj->node);
711b700e7f0SSeth Jennings 		kobject_put(&obj->kobj);
712d697bad5SPetr Mladek 	}
713d697bad5SPetr Mladek }
714d697bad5SPetr Mladek 
klp_free_objects(struct klp_patch * patch)715d697bad5SPetr Mladek static void klp_free_objects(struct klp_patch *patch)
716d697bad5SPetr Mladek {
717d697bad5SPetr Mladek 	__klp_free_objects(patch, false);
718d697bad5SPetr Mladek }
719d697bad5SPetr Mladek 
klp_free_objects_dynamic(struct klp_patch * patch)720d697bad5SPetr Mladek static void klp_free_objects_dynamic(struct klp_patch *patch)
721d697bad5SPetr Mladek {
7220430f78bSPetr Mladek 	__klp_free_objects(patch, true);
7230430f78bSPetr Mladek }
7240430f78bSPetr Mladek 
7250430f78bSPetr Mladek /*
7260430f78bSPetr Mladek  * This function implements the free operations that can be called safely
7270430f78bSPetr Mladek  * under klp_mutex.
7280430f78bSPetr Mladek  *
7297e35e4ebSPetr Mladek  * The operation must be completed by calling klp_free_patch_finish()
730b700e7f0SSeth Jennings  * outside klp_mutex.
731b700e7f0SSeth Jennings  */
klp_free_patch_start(struct klp_patch * patch)732b700e7f0SSeth Jennings static void klp_free_patch_start(struct klp_patch *patch)
7330430f78bSPetr Mladek {
7340430f78bSPetr Mladek 	if (!list_empty(&patch->list))
7350430f78bSPetr Mladek 		list_del(&patch->list);
7360430f78bSPetr Mladek 
7370430f78bSPetr Mladek 	klp_free_objects(patch);
7380430f78bSPetr Mladek }
7390430f78bSPetr Mladek 
7400430f78bSPetr Mladek /*
7410430f78bSPetr Mladek  * This function implements the free part that must be called outside
7420430f78bSPetr Mladek  * klp_mutex.
7430430f78bSPetr Mladek  *
7440430f78bSPetr Mladek  * It must be called after klp_free_patch_start(). And it has to be
7450430f78bSPetr Mladek  * the last function accessing the livepatch structures when the patch
7460430f78bSPetr Mladek  * gets disabled.
7470430f78bSPetr Mladek  */
klp_free_patch_finish(struct klp_patch * patch)7480430f78bSPetr Mladek static void klp_free_patch_finish(struct klp_patch *patch)
7490430f78bSPetr Mladek {
7500430f78bSPetr Mladek 	/*
7510430f78bSPetr Mladek 	 * Avoid deadlock with enabled_store() sysfs callback by
7520430f78bSPetr Mladek 	 * calling this outside klp_mutex. It is safe because
7530430f78bSPetr Mladek 	 * this is called when the patch gets disabled and it
7540430f78bSPetr Mladek 	 * cannot get enabled again.
755958ef1e3SPetr Mladek 	 */
756958ef1e3SPetr Mladek 	kobject_put(&patch->kobj);
757958ef1e3SPetr Mladek 	wait_for_completion(&patch->finish);
758958ef1e3SPetr Mladek 
759958ef1e3SPetr Mladek 	/* Put the module after the last access to struct klp_patch. */
760958ef1e3SPetr Mladek 	if (!patch->forced)
761958ef1e3SPetr Mladek 		module_put(patch->mod);
762958ef1e3SPetr Mladek }
763958ef1e3SPetr Mladek 
764958ef1e3SPetr Mladek /*
765958ef1e3SPetr Mladek  * The livepatch might be freed from sysfs interface created by the patch.
766958ef1e3SPetr Mladek  * This work allows to wait until the interface is destroyed in a separate
767958ef1e3SPetr Mladek  * context.
768958ef1e3SPetr Mladek  */
klp_free_patch_work_fn(struct work_struct * work)769958ef1e3SPetr Mladek static void klp_free_patch_work_fn(struct work_struct *work)
770958ef1e3SPetr Mladek {
771958ef1e3SPetr Mladek 	struct klp_patch *patch =
772b700e7f0SSeth Jennings 		container_of(work, struct klp_patch, free_work);
773b700e7f0SSeth Jennings 
7747e35e4ebSPetr Mladek 	klp_free_patch_finish(patch);
7757e35e4ebSPetr Mladek }
7767e35e4ebSPetr Mladek 
klp_free_patch_async(struct klp_patch * patch)7777e35e4ebSPetr Mladek void klp_free_patch_async(struct klp_patch *patch)
7787e35e4ebSPetr Mladek {
7797e35e4ebSPetr Mladek 	klp_free_patch_start(patch);
7807e35e4ebSPetr Mladek 	schedule_work(&patch->free_work);
7817e35e4ebSPetr Mladek }
7827e35e4ebSPetr Mladek 
klp_free_replaced_patches_async(struct klp_patch * new_patch)7837e35e4ebSPetr Mladek void klp_free_replaced_patches_async(struct klp_patch *new_patch)
7847e35e4ebSPetr Mladek {
7857e35e4ebSPetr Mladek 	struct klp_patch *old_patch, *tmp_patch;
7867e35e4ebSPetr Mladek 
7877e35e4ebSPetr Mladek 	klp_for_each_patch_safe(old_patch, tmp_patch) {
7887e35e4ebSPetr Mladek 		if (old_patch == new_patch)
7897e35e4ebSPetr Mladek 			return;
7907e35e4ebSPetr Mladek 		klp_free_patch_async(old_patch);
791b700e7f0SSeth Jennings 	}
792b700e7f0SSeth Jennings }
793e1452b60SJason Baron 
klp_init_func(struct klp_object * obj,struct klp_func * func)794e1452b60SJason Baron static int klp_init_func(struct klp_object *obj, struct klp_func *func)
795e1452b60SJason Baron {
796e1452b60SJason Baron 	if (!func->old_name)
797e1452b60SJason Baron 		return -EINVAL;
798e1452b60SJason Baron 
799e1452b60SJason Baron 	/*
800e1452b60SJason Baron 	 * NOPs get the address later. The patched module must be loaded,
801f09d9086SMiroslav Benes 	 * see klp_init_object_loaded().
802f09d9086SMiroslav Benes 	 */
8036e9df95bSKamalesh Babulal 	if (!func->new_func && !func->nop)
8046e9df95bSKamalesh Babulal 		return -EINVAL;
8056e9df95bSKamalesh Babulal 
8063c33f5b9SJosh Poimboeuf 	if (strlen(func->old_name) >= KSYM_NAME_LEN)
8070dade9f3SJosh Poimboeuf 		return -EINVAL;
808d83a7cb3SJosh Poimboeuf 
809b700e7f0SSeth Jennings 	INIT_LIST_HEAD(&func->stack_node);
810444f9e99SChris J Arges 	func->patched = false;
811444f9e99SChris J Arges 	func->transition = false;
812444f9e99SChris J Arges 
813444f9e99SChris J Arges 	/* The format for the sysfs directory is <function,sympos> where sympos
814444f9e99SChris J Arges 	 * is the nth occurrence of this symbol in kallsyms for the patched
8154d141ab3SPetr Mladek 	 * object. If the user selects 0 for old_sympos, then 1 will be used
8164d141ab3SPetr Mladek 	 * since a unique symbol will be the first occurrence.
817444f9e99SChris J Arges 	 */
818b700e7f0SSeth Jennings 	return kobject_add(&func->kobj, &obj->kobj, "%s,%lu",
819b700e7f0SSeth Jennings 			   func->old_name,
8200c05e7bdSSong Liu 			   func->old_sympos ? func->old_sympos : 1);
8210c05e7bdSSong Liu }
8220c05e7bdSSong Liu 
klp_write_object_relocs(struct klp_patch * patch,struct klp_object * obj,bool apply)8237c8e2bddSJosh Poimboeuf static int klp_write_object_relocs(struct klp_patch *patch,
8247c8e2bddSJosh Poimboeuf 				   struct klp_object *obj,
8257c8e2bddSJosh Poimboeuf 				   bool apply)
8267c8e2bddSJosh Poimboeuf {
8277c8e2bddSJosh Poimboeuf 	int i, ret;
8287c8e2bddSJosh Poimboeuf 	struct klp_modinfo *info = patch->mod->klp_info;
8297c8e2bddSJosh Poimboeuf 
8307c8e2bddSJosh Poimboeuf 	for (i = 1; i < info->hdr.e_shnum; i++) {
8317c8e2bddSJosh Poimboeuf 		Elf_Shdr *sec = info->sechdrs + i;
8327c8e2bddSJosh Poimboeuf 
8330c05e7bdSSong Liu 		if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
8347c8e2bddSJosh Poimboeuf 			continue;
8357c8e2bddSJosh Poimboeuf 
8360c05e7bdSSong Liu 		ret = klp_write_section_relocs(patch->mod, info->sechdrs,
8377c8e2bddSJosh Poimboeuf 					       info->secstrings,
8387c8e2bddSJosh Poimboeuf 					       patch->mod->core_kallsyms.strtab,
8397c8e2bddSJosh Poimboeuf 					       info->symndx, i, obj->name, apply);
8407c8e2bddSJosh Poimboeuf 		if (ret)
8417c8e2bddSJosh Poimboeuf 			return ret;
8427c8e2bddSJosh Poimboeuf 	}
8437c8e2bddSJosh Poimboeuf 
8440c05e7bdSSong Liu 	return 0;
8450c05e7bdSSong Liu }
8460c05e7bdSSong Liu 
klp_apply_object_relocs(struct klp_patch * patch,struct klp_object * obj)8470c05e7bdSSong Liu static int klp_apply_object_relocs(struct klp_patch *patch,
8480c05e7bdSSong Liu 				   struct klp_object *obj)
8490c05e7bdSSong Liu {
8500c05e7bdSSong Liu 	return klp_write_object_relocs(patch, obj, true);
8510c05e7bdSSong Liu }
8520c05e7bdSSong Liu 
klp_clear_object_relocs(struct klp_patch * patch,struct klp_object * obj)8530c05e7bdSSong Liu static void klp_clear_object_relocs(struct klp_patch *patch,
8540c05e7bdSSong Liu 				    struct klp_object *obj)
8550c05e7bdSSong Liu {
856b700e7f0SSeth Jennings 	klp_write_object_relocs(patch, obj, false);
857b700e7f0SSeth Jennings }
858b700e7f0SSeth Jennings 
859b700e7f0SSeth Jennings /* parts of the initialization that is done only when the object is loaded */
klp_init_object_loaded(struct klp_patch * patch,struct klp_object * obj)860b700e7f0SSeth Jennings static int klp_init_object_loaded(struct klp_patch *patch,
861b700e7f0SSeth Jennings 				  struct klp_object *obj)
862b700e7f0SSeth Jennings {
8631d05334dSPeter Zijlstra 	struct klp_func *func;
8647c8e2bddSJosh Poimboeuf 	int ret;
8657c8e2bddSJosh Poimboeuf 
8667c8e2bddSJosh Poimboeuf 	if (klp_is_module(obj)) {
8677c8e2bddSJosh Poimboeuf 		/*
8687c8e2bddSJosh Poimboeuf 		 * Only write module-specific relocations here
8697c8e2bddSJosh Poimboeuf 		 * (.klp.rela.{module}.*).  vmlinux-specific relocations were
8707c8e2bddSJosh Poimboeuf 		 * written earlier during the initialization of the klp module
8711d05334dSPeter Zijlstra 		 * itself.
872b700e7f0SSeth Jennings 		 */
873255e732cSJessica Yu 		ret = klp_apply_object_relocs(patch, obj);
8749f255b63SJosh Poimboeuf 		if (ret)
8758cdd043aSJiri Slaby 			return ret;
876b2b018efSChris J Arges 	}
877b2b018efSChris J Arges 
87819514910SPetr Mladek 	klp_for_each_func(obj, func) {
879b700e7f0SSeth Jennings 		ret = klp_find_object_symbol(obj->name, func->old_name,
880b700e7f0SSeth Jennings 					     func->old_sympos,
881f5e547f4SJosh Poimboeuf 					     (unsigned long *)&func->old_func);
88219514910SPetr Mladek 		if (ret)
883f5e547f4SJosh Poimboeuf 			return ret;
884f5e547f4SJosh Poimboeuf 
885f5e547f4SJosh Poimboeuf 		ret = kallsyms_lookup_size_offset((unsigned long)func->old_func,
886f5e547f4SJosh Poimboeuf 						  &func->old_size, NULL);
887f5e547f4SJosh Poimboeuf 		if (!ret) {
888f5e547f4SJosh Poimboeuf 			pr_err("kallsyms size lookup failed for '%s'\n",
889f5e547f4SJosh Poimboeuf 			       func->old_name);
890e1452b60SJason Baron 			return -ENOENT;
891e1452b60SJason Baron 		}
892e1452b60SJason Baron 
893f5e547f4SJosh Poimboeuf 		if (func->nop)
894f5e547f4SJosh Poimboeuf 			func->new_func = func->old_func;
895f5e547f4SJosh Poimboeuf 
896f5e547f4SJosh Poimboeuf 		ret = kallsyms_lookup_size_offset((unsigned long)func->new_func,
897f5e547f4SJosh Poimboeuf 						  &func->new_size, NULL);
898f5e547f4SJosh Poimboeuf 		if (!ret) {
899f5e547f4SJosh Poimboeuf 			pr_err("kallsyms size lookup failed for '%s' replacement\n",
900b700e7f0SSeth Jennings 			       func->old_name);
901b700e7f0SSeth Jennings 			return -ENOENT;
902b700e7f0SSeth Jennings 		}
903b700e7f0SSeth Jennings 	}
904b700e7f0SSeth Jennings 
905b700e7f0SSeth Jennings 	return 0;
906b700e7f0SSeth Jennings }
907b700e7f0SSeth Jennings 
klp_init_object(struct klp_patch * patch,struct klp_object * obj)908b700e7f0SSeth Jennings static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
909b700e7f0SSeth Jennings {
910b700e7f0SSeth Jennings 	struct klp_func *func;
9116e9df95bSKamalesh Babulal 	int ret;
9126e9df95bSKamalesh Babulal 	const char *name;
9136e9df95bSKamalesh Babulal 
9140dade9f3SJosh Poimboeuf 	if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN)
9158cb2c2dcSPetr Mladek 		return -EINVAL;
916b700e7f0SSeth Jennings 
917b700e7f0SSeth Jennings 	obj->patched = false;
918b700e7f0SSeth Jennings 	obj->mod = NULL;
919b700e7f0SSeth Jennings 
9204d141ab3SPetr Mladek 	klp_find_object_module(obj);
921cad706dfSMiroslav Benes 
922cad706dfSMiroslav Benes 	name = klp_is_module(obj) ? obj->name : "vmlinux";
923b700e7f0SSeth Jennings 	ret = kobject_add(&obj->kobj, &patch->kobj, "%s", name);
9248cdd043aSJiri Slaby 	if (ret)
925b700e7f0SSeth Jennings 		return ret;
926b700e7f0SSeth Jennings 
9270430f78bSPetr Mladek 	klp_for_each_func(obj, func) {
928b700e7f0SSeth Jennings 		ret = klp_init_func(obj, func);
929b700e7f0SSeth Jennings 		if (ret)
9300430f78bSPetr Mladek 			return ret;
931b700e7f0SSeth Jennings 	}
9320430f78bSPetr Mladek 
9330430f78bSPetr Mladek 	if (klp_is_object_loaded(obj))
9340430f78bSPetr Mladek 		ret = klp_init_object_loaded(patch, obj);
9350430f78bSPetr Mladek 
936f68d67cfSPetr Mladek 	return ret;
937f68d67cfSPetr Mladek }
938f68d67cfSPetr Mladek 
klp_init_func_early(struct klp_object * obj,struct klp_func * func)939f68d67cfSPetr Mladek static void klp_init_func_early(struct klp_object *obj,
940f68d67cfSPetr Mladek 				struct klp_func *func)
941f68d67cfSPetr Mladek {
942f68d67cfSPetr Mladek 	kobject_init(&func->kobj, &klp_ktype_func);
943f68d67cfSPetr Mladek 	list_add_tail(&func->node, &obj->func_list);
944f68d67cfSPetr Mladek }
945f68d67cfSPetr Mladek 
klp_init_object_early(struct klp_patch * patch,struct klp_object * obj)946f68d67cfSPetr Mladek static void klp_init_object_early(struct klp_patch *patch,
947f68d67cfSPetr Mladek 				  struct klp_object *obj)
948f68d67cfSPetr Mladek {
949f68d67cfSPetr Mladek 	INIT_LIST_HEAD(&obj->func_list);
950f68d67cfSPetr Mladek 	kobject_init(&obj->kobj, &klp_ktype_object);
9515ef3dd20SDavid Vernet 	list_add_tail(&obj->node, &patch->obj_list);
9520430f78bSPetr Mladek }
9530430f78bSPetr Mladek 
klp_init_patch_early(struct klp_patch * patch)9540430f78bSPetr Mladek static void klp_init_patch_early(struct klp_patch *patch)
9550430f78bSPetr Mladek {
9560430f78bSPetr Mladek 	struct klp_object *obj;
95720e55025SJason Baron 	struct klp_func *func;
9584d141ab3SPetr Mladek 
9590430f78bSPetr Mladek 	INIT_LIST_HEAD(&patch->list);
96068007289SPetr Mladek 	INIT_LIST_HEAD(&patch->obj_list);
961958ef1e3SPetr Mladek 	kobject_init(&patch->kobj, &klp_ktype_patch);
9620430f78bSPetr Mladek 	patch->enabled = false;
9630430f78bSPetr Mladek 	patch->forced = false;
96420e55025SJason Baron 	INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
965f68d67cfSPetr Mladek 	init_completion(&patch->finish);
9660430f78bSPetr Mladek 
96720e55025SJason Baron 	klp_for_each_object_static(patch, obj) {
968f68d67cfSPetr Mladek 		klp_init_object_early(patch, obj);
96920e55025SJason Baron 
970b700e7f0SSeth Jennings 		klp_for_each_func_static(obj, func) {
971b700e7f0SSeth Jennings 			klp_init_func_early(obj, func);
972b700e7f0SSeth Jennings 		}
973b700e7f0SSeth Jennings 	}
974b700e7f0SSeth Jennings }
975b700e7f0SSeth Jennings 
klp_init_patch(struct klp_patch * patch)976b700e7f0SSeth Jennings static int klp_init_patch(struct klp_patch *patch)
977b700e7f0SSeth Jennings {
9784d141ab3SPetr Mladek 	struct klp_object *obj;
979958ef1e3SPetr Mladek 	int ret;
9803ec24776SJosh Poimboeuf 
981b700e7f0SSeth Jennings 	ret = kobject_add(&patch->kobj, klp_root_kobj, "%s", patch->mod->name);
982e1452b60SJason Baron 	if (ret)
983e1452b60SJason Baron 		return ret;
984e1452b60SJason Baron 
985e1452b60SJason Baron 	if (patch->replace) {
986e1452b60SJason Baron 		ret = klp_add_nops(patch);
987e1452b60SJason Baron 		if (ret)
9888cdd043aSJiri Slaby 			return ret;
989b700e7f0SSeth Jennings 	}
990b700e7f0SSeth Jennings 
991958ef1e3SPetr Mladek 	klp_for_each_object(patch, obj) {
992b700e7f0SSeth Jennings 		ret = klp_init_object(patch, obj);
993b700e7f0SSeth Jennings 		if (ret)
99499590ba5SJosh Poimboeuf 			return ret;
995b700e7f0SSeth Jennings 	}
996b700e7f0SSeth Jennings 
997b700e7f0SSeth Jennings 	list_add_tail(&patch->list, &klp_patches);
998b700e7f0SSeth Jennings 
99926c3e98eSPetr Mladek 	return 0;
100026c3e98eSPetr Mladek }
100126c3e98eSPetr Mladek 
__klp_disable_patch(struct klp_patch * patch)100226c3e98eSPetr Mladek static int __klp_disable_patch(struct klp_patch *patch)
100326c3e98eSPetr Mladek {
100426c3e98eSPetr Mladek 	struct klp_object *obj;
100526c3e98eSPetr Mladek 
100626c3e98eSPetr Mladek 	if (WARN_ON(!patch->enabled))
100726c3e98eSPetr Mladek 		return -EINVAL;
100826c3e98eSPetr Mladek 
1009d927752fSWardenjohn 	if (klp_transition_patch)
101026c3e98eSPetr Mladek 		return -EBUSY;
101126c3e98eSPetr Mladek 
101226c3e98eSPetr Mladek 	klp_init_transition(patch, KLP_TRANSITION_UNPATCHED);
101326c3e98eSPetr Mladek 
101426c3e98eSPetr Mladek 	klp_for_each_object(patch, obj)
101526c3e98eSPetr Mladek 		if (obj->patched)
101626c3e98eSPetr Mladek 			klp_pre_unpatch_callback(obj);
101726c3e98eSPetr Mladek 
101826c3e98eSPetr Mladek 	/*
101926c3e98eSPetr Mladek 	 * Enforce the order of the func->transition writes in
102026c3e98eSPetr Mladek 	 * klp_init_transition() and the TIF_PATCH_PENDING writes in
102126c3e98eSPetr Mladek 	 * klp_start_transition().  In the rare case where klp_ftrace_handler()
102226c3e98eSPetr Mladek 	 * is called shortly after klp_update_patch_state() switches the task,
102326c3e98eSPetr Mladek 	 * this ensures the handler sees that func->transition is set.
102426c3e98eSPetr Mladek 	 */
102526c3e98eSPetr Mladek 	smp_wmb();
1026958ef1e3SPetr Mladek 
102726c3e98eSPetr Mladek 	klp_start_transition();
102826c3e98eSPetr Mladek 	patch->enabled = false;
102926c3e98eSPetr Mladek 	klp_try_complete_transition();
103026c3e98eSPetr Mladek 
103126c3e98eSPetr Mladek 	return 0;
103226c3e98eSPetr Mladek }
103326c3e98eSPetr Mladek 
__klp_enable_patch(struct klp_patch * patch)103426c3e98eSPetr Mladek static int __klp_enable_patch(struct klp_patch *patch)
103526c3e98eSPetr Mladek {
103626c3e98eSPetr Mladek 	struct klp_object *obj;
103726c3e98eSPetr Mladek 	int ret;
103826c3e98eSPetr Mladek 
103926c3e98eSPetr Mladek 	if (klp_transition_patch)
104026c3e98eSPetr Mladek 		return -EBUSY;
104126c3e98eSPetr Mladek 
104226c3e98eSPetr Mladek 	if (WARN_ON(patch->enabled))
104326c3e98eSPetr Mladek 		return -EINVAL;
1044d927752fSWardenjohn 
104526c3e98eSPetr Mladek 	pr_notice("enabling patch '%s'\n", patch->mod->name);
104626c3e98eSPetr Mladek 
104726c3e98eSPetr Mladek 	klp_init_transition(patch, KLP_TRANSITION_PATCHED);
104826c3e98eSPetr Mladek 
104926c3e98eSPetr Mladek 	/*
105026c3e98eSPetr Mladek 	 * Enforce the order of the func->transition writes in
105126c3e98eSPetr Mladek 	 * klp_init_transition() and the ops->func_stack writes in
105226c3e98eSPetr Mladek 	 * klp_patch_object(), so that klp_ftrace_handler() will see the
105326c3e98eSPetr Mladek 	 * func->transition updates before the handler is registered and the
105426c3e98eSPetr Mladek 	 * new funcs become visible to the handler.
105526c3e98eSPetr Mladek 	 */
105626c3e98eSPetr Mladek 	smp_wmb();
105726c3e98eSPetr Mladek 
105826c3e98eSPetr Mladek 	klp_for_each_object(patch, obj) {
105926c3e98eSPetr Mladek 		if (!klp_is_object_loaded(obj))
106026c3e98eSPetr Mladek 			continue;
106126c3e98eSPetr Mladek 
106226c3e98eSPetr Mladek 		ret = klp_pre_patch_callback(obj);
106326c3e98eSPetr Mladek 		if (ret) {
106426c3e98eSPetr Mladek 			pr_warn("pre-patch callback failed for object '%s'\n",
106526c3e98eSPetr Mladek 				klp_is_module(obj) ? obj->name : "vmlinux");
106626c3e98eSPetr Mladek 			goto err;
106726c3e98eSPetr Mladek 		}
106826c3e98eSPetr Mladek 
106926c3e98eSPetr Mladek 		ret = klp_patch_object(obj);
107026c3e98eSPetr Mladek 		if (ret) {
107126c3e98eSPetr Mladek 			pr_warn("failed to patch object '%s'\n",
107226c3e98eSPetr Mladek 				klp_is_module(obj) ? obj->name : "vmlinux");
107326c3e98eSPetr Mladek 			goto err;
107426c3e98eSPetr Mladek 		}
107526c3e98eSPetr Mladek 	}
1076958ef1e3SPetr Mladek 
107726c3e98eSPetr Mladek 	klp_start_transition();
107826c3e98eSPetr Mladek 	patch->enabled = true;
107926c3e98eSPetr Mladek 	klp_try_complete_transition();
108026c3e98eSPetr Mladek 
108126c3e98eSPetr Mladek 	return 0;
108226c3e98eSPetr Mladek err:
108326c3e98eSPetr Mladek 	pr_warn("failed to enable patch '%s'\n", patch->mod->name);
108426c3e98eSPetr Mladek 
108526c3e98eSPetr Mladek 	klp_cancel_transition();
108626c3e98eSPetr Mladek 	return ret;
1087958ef1e3SPetr Mladek }
1088958ef1e3SPetr Mladek 
108926c3e98eSPetr Mladek /**
1090958ef1e3SPetr Mladek  * klp_enable_patch() - enable the livepatch
1091958ef1e3SPetr Mladek  * @patch:	patch to be enabled
1092958ef1e3SPetr Mladek  *
1093958ef1e3SPetr Mladek  * Initializes the data structure associated with the patch, creates the sysfs
1094958ef1e3SPetr Mladek  * interface, performs the needed symbol lookups and code relocations,
1095958ef1e3SPetr Mladek  * registers the patched functions with ftrace.
109626c3e98eSPetr Mladek  *
109726c3e98eSPetr Mladek  * This function is supposed to be called from the livepatch module_init()
109826c3e98eSPetr Mladek  * callback.
109926c3e98eSPetr Mladek  *
110026c3e98eSPetr Mladek  * Return: 0 on success, otherwise error
110126c3e98eSPetr Mladek  */
klp_enable_patch(struct klp_patch * patch)11025ef3dd20SDavid Vernet int klp_enable_patch(struct klp_patch *patch)
110326c3e98eSPetr Mladek {
11045ef3dd20SDavid Vernet 	int ret;
1105958ef1e3SPetr Mladek 	struct klp_object *obj;
110626c3e98eSPetr Mladek 
11075ef3dd20SDavid Vernet 	if (!patch || !patch->mod || !patch->objs)
11085ef3dd20SDavid Vernet 		return -EINVAL;
11095ef3dd20SDavid Vernet 
11105ef3dd20SDavid Vernet 	klp_for_each_object_static(patch, obj) {
11115ef3dd20SDavid Vernet 		if (!obj->funcs)
11125ef3dd20SDavid Vernet 			return -EINVAL;
1113958ef1e3SPetr Mladek 	}
1114958ef1e3SPetr Mladek 
1115958ef1e3SPetr Mladek 
1116958ef1e3SPetr Mladek 	if (!is_livepatch_module(patch->mod)) {
111726c3e98eSPetr Mladek 		pr_err("module %s is not marked as a livepatch module\n",
111826c3e98eSPetr Mladek 		       patch->mod->name);
1119958ef1e3SPetr Mladek 		return -EINVAL;
1120958ef1e3SPetr Mladek 	}
1121958ef1e3SPetr Mladek 
1122958ef1e3SPetr Mladek 	if (!klp_initialized())
112331adf230SPetr Mladek 		return -ENODEV;
112431adf230SPetr Mladek 
1125958ef1e3SPetr Mladek 	if (!klp_have_reliable_stack()) {
1126958ef1e3SPetr Mladek 		pr_warn("This architecture doesn't have support for the livepatch consistency model.\n");
1127958ef1e3SPetr Mladek 		pr_warn("The livepatch transition may never complete.\n");
1128958ef1e3SPetr Mladek 	}
112992c9abf5SPetr Mladek 
113092c9abf5SPetr Mladek 	mutex_lock(&klp_mutex);
113192c9abf5SPetr Mladek 
113292c9abf5SPetr Mladek 	if (!klp_is_patch_compatible(patch)) {
113392c9abf5SPetr Mladek 		pr_err("Livepatch patch (%s) is not compatible with the already installed livepatches.\n",
113492c9abf5SPetr Mladek 			patch->mod->name);
113592c9abf5SPetr Mladek 		mutex_unlock(&klp_mutex);
113650a0f3f5SYang Yingliang 		return -EINVAL;
113750a0f3f5SYang Yingliang 	}
11385ef3dd20SDavid Vernet 
113950a0f3f5SYang Yingliang 	if (!try_module_get(patch->mod)) {
11405ef3dd20SDavid Vernet 		mutex_unlock(&klp_mutex);
11415ef3dd20SDavid Vernet 		return -ENODEV;
1142958ef1e3SPetr Mladek 	}
1143958ef1e3SPetr Mladek 
1144958ef1e3SPetr Mladek 	klp_init_patch_early(patch);
1145958ef1e3SPetr Mladek 
1146958ef1e3SPetr Mladek 	ret = klp_init_patch(patch);
114726c3e98eSPetr Mladek 	if (ret)
1148958ef1e3SPetr Mladek 		goto err;
1149958ef1e3SPetr Mladek 
1150958ef1e3SPetr Mladek 	ret = __klp_enable_patch(patch);
1151958ef1e3SPetr Mladek 	if (ret)
1152958ef1e3SPetr Mladek 		goto err;
1153958ef1e3SPetr Mladek 
115426c3e98eSPetr Mladek 	mutex_unlock(&klp_mutex);
115526c3e98eSPetr Mladek 
1156958ef1e3SPetr Mladek 	return 0;
1157958ef1e3SPetr Mladek 
115826c3e98eSPetr Mladek err:
1159958ef1e3SPetr Mladek 	klp_free_patch_start(patch);
1160958ef1e3SPetr Mladek 
1161958ef1e3SPetr Mladek 	mutex_unlock(&klp_mutex);
116226c3e98eSPetr Mladek 
116326c3e98eSPetr Mladek 	klp_free_patch_finish(patch);
116426c3e98eSPetr Mladek 
116526c3e98eSPetr Mladek 	return ret;
1166ef8daf8eSJoe Lawrence }
11677e35e4ebSPetr Mladek EXPORT_SYMBOL_GPL(klp_enable_patch);
1168e1452b60SJason Baron 
1169e1452b60SJason Baron /*
11707e35e4ebSPetr Mladek  * This function unpatches objects from the replaced livepatches.
11717e35e4ebSPetr Mladek  *
11727e35e4ebSPetr Mladek  * We could be pretty aggressive here. It is called in the situation where
11737e35e4ebSPetr Mladek  * these structures are no longer accessed from the ftrace handler.
1174e1452b60SJason Baron  * All functions are redirected by the klp_transition_patch. They
1175e1452b60SJason Baron  * use either a new code or they are in the original code because
1176e1452b60SJason Baron  * of the special nop function patches.
1177e1452b60SJason Baron  *
1178e1452b60SJason Baron  * The only exception is when the transition was forced. In this case,
1179e1452b60SJason Baron  * klp_ftrace_handler() might still see the replaced patch on the stack.
1180e1452b60SJason Baron  * Fortunately, it is carefully designed to work with removed functions
11817e35e4ebSPetr Mladek  * thanks to RCU. We only have to keep the patches on the system. Also
1182e1452b60SJason Baron  * this is handled transparently by patch->module_put.
11837e35e4ebSPetr Mladek  */
klp_unpatch_replaced_patches(struct klp_patch * new_patch)1184e1452b60SJason Baron void klp_unpatch_replaced_patches(struct klp_patch *new_patch)
11857e35e4ebSPetr Mladek {
1186e1452b60SJason Baron 	struct klp_patch *old_patch;
1187e1452b60SJason Baron 
1188e1452b60SJason Baron 	klp_for_each_patch(old_patch) {
1189e1452b60SJason Baron 		if (old_patch == new_patch)
1190e1452b60SJason Baron 			return;
1191e1452b60SJason Baron 
1192e1452b60SJason Baron 		old_patch->enabled = false;
1193e1452b60SJason Baron 		klp_unpatch_objects(old_patch);
1194e1452b60SJason Baron 	}
1195d697bad5SPetr Mladek }
1196d697bad5SPetr Mladek 
1197d697bad5SPetr Mladek /*
1198d697bad5SPetr Mladek  * This function removes the dynamically allocated 'nop' functions.
1199d697bad5SPetr Mladek  *
1200d697bad5SPetr Mladek  * We could be pretty aggressive. NOPs do not change the existing
1201d697bad5SPetr Mladek  * behavior except for adding unnecessary delay by the ftrace handler.
1202d697bad5SPetr Mladek  *
1203d697bad5SPetr Mladek  * It is safe even when the transition was forced. The ftrace handler
1204d697bad5SPetr Mladek  * will see a valid ops->func_stack entry thanks to RCU.
1205d697bad5SPetr Mladek  *
1206d697bad5SPetr Mladek  * We could even free the NOPs structures. They must be the last entry
1207d697bad5SPetr Mladek  * in ops->func_stack. Therefore unregister_ftrace_function() is called.
1208d697bad5SPetr Mladek  * It does the same as klp_synchronize_transition() to make sure that
1209d697bad5SPetr Mladek  * nobody is inside the ftrace handler once the operation finishes.
1210d697bad5SPetr Mladek  *
1211d697bad5SPetr Mladek  * IMPORTANT: It must be called right after removing the replaced patches!
1212d697bad5SPetr Mladek  */
klp_discard_nops(struct klp_patch * new_patch)1213d697bad5SPetr Mladek void klp_discard_nops(struct klp_patch *new_patch)
1214d697bad5SPetr Mladek {
1215d697bad5SPetr Mladek 	klp_unpatch_objects_dynamic(klp_transition_patch);
1216d697bad5SPetr Mladek 	klp_free_objects_dynamic(klp_transition_patch);
1217ef8daf8eSJoe Lawrence }
1218ef8daf8eSJoe Lawrence 
1219ef8daf8eSJoe Lawrence /*
1220ef8daf8eSJoe Lawrence  * Remove parts of patches that touch a given kernel module. The list of
1221ef8daf8eSJoe Lawrence  * patches processed might be limited. When limit is NULL, all patches
1222ef8daf8eSJoe Lawrence  * will be handled.
1223ef8daf8eSJoe Lawrence  */
klp_cleanup_module_patches_limited(struct module * mod,struct klp_patch * limit)1224ef8daf8eSJoe Lawrence static void klp_cleanup_module_patches_limited(struct module *mod,
1225ef8daf8eSJoe Lawrence 					       struct klp_patch *limit)
1226ef8daf8eSJoe Lawrence {
1227ecba29f4SPetr Mladek 	struct klp_patch *patch;
1228ef8daf8eSJoe Lawrence 	struct klp_object *obj;
1229ef8daf8eSJoe Lawrence 
1230ef8daf8eSJoe Lawrence 	klp_for_each_patch(patch) {
1231ef8daf8eSJoe Lawrence 		if (patch == limit)
1232ef8daf8eSJoe Lawrence 			break;
1233ef8daf8eSJoe Lawrence 
1234ef8daf8eSJoe Lawrence 		klp_for_each_object(patch, obj) {
1235fc41efc1SJiri Kosina 			if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1236fc41efc1SJiri Kosina 				continue;
1237fc41efc1SJiri Kosina 
1238ef8daf8eSJoe Lawrence 			if (patch != klp_transition_patch)
1239ef8daf8eSJoe Lawrence 				klp_pre_unpatch_callback(obj);
1240ef8daf8eSJoe Lawrence 
1241fc41efc1SJiri Kosina 			pr_notice("reverting patch '%s' on unloading module '%s'\n",
1242fc41efc1SJiri Kosina 				  patch->mod->name, obj->mod->name);
12430c05e7bdSSong Liu 			klp_unpatch_object(obj);
1244ef8daf8eSJoe Lawrence 
1245ef8daf8eSJoe Lawrence 			klp_post_unpatch_callback(obj);
1246ef8daf8eSJoe Lawrence 			klp_clear_object_relocs(patch, obj);
1247ef8daf8eSJoe Lawrence 			klp_free_object_loaded(obj);
1248ef8daf8eSJoe Lawrence 			break;
1249ef8daf8eSJoe Lawrence 		}
12507e545d6eSJessica Yu 	}
1251b700e7f0SSeth Jennings }
125236e505c1SMinfei Huang 
klp_module_coming(struct module * mod)1253b700e7f0SSeth Jennings int klp_module_coming(struct module *mod)
1254b700e7f0SSeth Jennings {
1255b700e7f0SSeth Jennings 	int ret;
12567e545d6eSJessica Yu 	struct klp_patch *patch;
12577e545d6eSJessica Yu 	struct klp_object *obj;
1258b700e7f0SSeth Jennings 
1259dcf550e5SJosh Poimboeuf 	if (WARN_ON(mod->state != MODULE_STATE_COMING))
126066d8529dSZhen Lei 		return -EINVAL;
1261dcf550e5SJosh Poimboeuf 
1262dcf550e5SJosh Poimboeuf 	if (!strcmp(mod->name, "vmlinux")) {
1263dcf550e5SJosh Poimboeuf 		pr_err("vmlinux.ko: invalid module name\n");
1264b700e7f0SSeth Jennings 		return -EINVAL;
12658cb2c2dcSPetr Mladek 	}
12667e545d6eSJessica Yu 
12677e545d6eSJessica Yu 	mutex_lock(&klp_mutex);
12687e545d6eSJessica Yu 	/*
12698cb2c2dcSPetr Mladek 	 * Each module has to know that klp_module_coming()
12708cb2c2dcSPetr Mladek 	 * has been called. We never know what module will
12718cb2c2dcSPetr Mladek 	 * get patched by a new patch.
1272ecba29f4SPetr Mladek 	 */
12738cdd043aSJiri Slaby 	mod->klp_alive = true;
1274b700e7f0SSeth Jennings 
1275b700e7f0SSeth Jennings 	klp_for_each_patch(patch) {
1276b700e7f0SSeth Jennings 		klp_for_each_object(patch, obj) {
1277b700e7f0SSeth Jennings 			if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
12787e545d6eSJessica Yu 				continue;
12797e545d6eSJessica Yu 
128036e505c1SMinfei Huang 			obj->mod = mod;
12817e545d6eSJessica Yu 
12827e545d6eSJessica Yu 			ret = klp_init_object_loaded(patch, obj);
12837e545d6eSJessica Yu 			if (ret) {
128436e505c1SMinfei Huang 				pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
12857e545d6eSJessica Yu 					patch->mod->name, obj->mod->name, ret);
12867e545d6eSJessica Yu 				goto err;
12877e545d6eSJessica Yu 			}
12887e545d6eSJessica Yu 
128993862e38SJoe Lawrence 			pr_notice("applying patch '%s' to loading module '%s'\n",
129093862e38SJoe Lawrence 				  patch->mod->name, obj->mod->name);
129193862e38SJoe Lawrence 
129293862e38SJoe Lawrence 			ret = klp_pre_patch_callback(obj);
129393862e38SJoe Lawrence 			if (ret) {
129493862e38SJoe Lawrence 				pr_warn("pre-patch callback failed for object '%s'\n",
129593862e38SJoe Lawrence 					obj->name);
12960dade9f3SJosh Poimboeuf 				goto err;
12977e545d6eSJessica Yu 			}
12987e545d6eSJessica Yu 
12997e545d6eSJessica Yu 			ret = klp_patch_object(obj);
130093862e38SJoe Lawrence 			if (ret) {
130193862e38SJoe Lawrence 				pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
13027e545d6eSJessica Yu 					patch->mod->name, obj->mod->name, ret);
13037e545d6eSJessica Yu 
1304b700e7f0SSeth Jennings 				klp_post_unpatch_callback(obj);
130593862e38SJoe Lawrence 				goto err;
130693862e38SJoe Lawrence 			}
130793862e38SJoe Lawrence 
1308b700e7f0SSeth Jennings 			if (patch != klp_transition_patch)
1309b700e7f0SSeth Jennings 				klp_post_patch_callback(obj);
1310b700e7f0SSeth Jennings 
1311b700e7f0SSeth Jennings 			break;
1312b700e7f0SSeth Jennings 		}
1313b700e7f0SSeth Jennings 	}
1314b700e7f0SSeth Jennings 
13157e545d6eSJessica Yu 	mutex_unlock(&klp_mutex);
13167e545d6eSJessica Yu 
13177e545d6eSJessica Yu 	return 0;
13187e545d6eSJessica Yu 
13197e545d6eSJessica Yu err:
13207e545d6eSJessica Yu 	/*
13217e545d6eSJessica Yu 	 * If a patch is unsuccessfully applied, return
13227e545d6eSJessica Yu 	 * error to the module loader.
13237e545d6eSJessica Yu 	 */
13244ff96fb5SMiroslav Benes 	pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
1325ef8daf8eSJoe Lawrence 		patch->mod->name, obj->mod->name, obj->mod->name);
13267e545d6eSJessica Yu 	mod->klp_alive = false;
13277e545d6eSJessica Yu 	obj->mod = NULL;
13287e545d6eSJessica Yu 	klp_cleanup_module_patches_limited(mod, patch);
1329b700e7f0SSeth Jennings 	mutex_unlock(&klp_mutex);
1330b700e7f0SSeth Jennings 
13317e545d6eSJessica Yu 	return ret;
13327e545d6eSJessica Yu }
13337e545d6eSJessica Yu 
klp_module_going(struct module * mod)13347e545d6eSJessica Yu void klp_module_going(struct module *mod)
13357e545d6eSJessica Yu {
13367e545d6eSJessica Yu 	if (WARN_ON(mod->state != MODULE_STATE_GOING &&
13377e545d6eSJessica Yu 		    mod->state != MODULE_STATE_COMING))
13387e545d6eSJessica Yu 		return;
13397e545d6eSJessica Yu 
13407e545d6eSJessica Yu 	mutex_lock(&klp_mutex);
13417e545d6eSJessica Yu 	/*
13427e545d6eSJessica Yu 	 * Each module has to know that klp_module_going()
13437e545d6eSJessica Yu 	 * has been called. We never know what module will
13447e545d6eSJessica Yu 	 * get patched by a new patch.
1345ef8daf8eSJoe Lawrence 	 */
13467e545d6eSJessica Yu 	mod->klp_alive = false;
13477e545d6eSJessica Yu 
13487e545d6eSJessica Yu 	klp_cleanup_module_patches_limited(mod, NULL);
1349b700e7f0SSeth Jennings 
135026029d88SMinfei Huang 	mutex_unlock(&klp_mutex);
1351b700e7f0SSeth Jennings }
1352b700e7f0SSeth Jennings 
klp_init(void)13537e545d6eSJessica Yu static int __init klp_init(void)
13547e545d6eSJessica Yu {
1355b700e7f0SSeth Jennings 	klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
1356b700e7f0SSeth Jennings 	if (!klp_root_kobj)
1357b700e7f0SSeth Jennings 		return -ENOMEM;
1358b700e7f0SSeth Jennings 
1359b700e7f0SSeth Jennings 	return 0;
1360 }
1361 
1362 module_init(klp_init);
1363