xref: /linux-6.15/kernel/livepatch/core.c (revision bb26cfd9)
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()
363c33f5b9SJosh Poimboeuf  */
37d83a7cb3SJosh Poimboeuf DEFINE_MUTEX(klp_mutex);
383c33f5b9SJosh Poimboeuf 
39958ef1e3SPetr Mladek /*
40958ef1e3SPetr Mladek  * Actively used patches: enabled or in transition. Note that replaced
41958ef1e3SPetr Mladek  * or disabled patches are not listed even though the related kernel
42958ef1e3SPetr Mladek  * module still can be loaded.
43958ef1e3SPetr Mladek  */
4468007289SPetr Mladek LIST_HEAD(klp_patches);
45b700e7f0SSeth Jennings 
46b700e7f0SSeth Jennings static struct kobject *klp_root_kobj;
47b700e7f0SSeth Jennings 
48b700e7f0SSeth Jennings static bool klp_is_module(struct klp_object *obj)
49b700e7f0SSeth Jennings {
50b700e7f0SSeth Jennings 	return obj->name;
51b700e7f0SSeth Jennings }
52b700e7f0SSeth Jennings 
53b700e7f0SSeth Jennings /* sets obj->mod if object is not vmlinux and module is found */
54b700e7f0SSeth Jennings static void klp_find_object_module(struct klp_object *obj)
55b700e7f0SSeth Jennings {
568cb2c2dcSPetr Mladek 	struct module *mod;
578cb2c2dcSPetr Mladek 
58b700e7f0SSeth Jennings 	if (!klp_is_module(obj))
59b700e7f0SSeth Jennings 		return;
60b700e7f0SSeth Jennings 
61a0060505SChristoph Hellwig 	rcu_read_lock_sched();
62b700e7f0SSeth Jennings 	/*
638cb2c2dcSPetr Mladek 	 * We do not want to block removal of patched modules and therefore
648cb2c2dcSPetr Mladek 	 * we do not take a reference here. The patches are removed by
657e545d6eSJessica Yu 	 * klp_module_going() instead.
66b700e7f0SSeth Jennings 	 */
678cb2c2dcSPetr Mladek 	mod = find_module(obj->name);
688cb2c2dcSPetr Mladek 	/*
697e545d6eSJessica Yu 	 * Do not mess work of klp_module_coming() and klp_module_going().
707e545d6eSJessica Yu 	 * Note that the patch might still be needed before klp_module_going()
718cb2c2dcSPetr Mladek 	 * is called. Module functions can be called even in the GOING state
728cb2c2dcSPetr Mladek 	 * until mod->exit() finishes. This is especially important for
738cb2c2dcSPetr Mladek 	 * patches that modify semantic of the functions.
748cb2c2dcSPetr Mladek 	 */
758cb2c2dcSPetr Mladek 	if (mod && mod->klp_alive)
768cb2c2dcSPetr Mladek 		obj->mod = mod;
778cb2c2dcSPetr Mladek 
78a0060505SChristoph Hellwig 	rcu_read_unlock_sched();
79b700e7f0SSeth Jennings }
80b700e7f0SSeth Jennings 
81b700e7f0SSeth Jennings static bool klp_initialized(void)
82b700e7f0SSeth Jennings {
83e76ff06aSNicholas Mc Guire 	return !!klp_root_kobj;
84b700e7f0SSeth Jennings }
85b700e7f0SSeth Jennings 
86e1452b60SJason Baron static struct klp_func *klp_find_func(struct klp_object *obj,
87e1452b60SJason Baron 				      struct klp_func *old_func)
88e1452b60SJason Baron {
89e1452b60SJason Baron 	struct klp_func *func;
90e1452b60SJason Baron 
91e1452b60SJason Baron 	klp_for_each_func(obj, func) {
92e1452b60SJason Baron 		if ((strcmp(old_func->old_name, func->old_name) == 0) &&
93e1452b60SJason Baron 		    (old_func->old_sympos == func->old_sympos)) {
94e1452b60SJason Baron 			return func;
95e1452b60SJason Baron 		}
96e1452b60SJason Baron 	}
97e1452b60SJason Baron 
98e1452b60SJason Baron 	return NULL;
99e1452b60SJason Baron }
100e1452b60SJason Baron 
101e1452b60SJason Baron static struct klp_object *klp_find_object(struct klp_patch *patch,
102e1452b60SJason Baron 					  struct klp_object *old_obj)
103e1452b60SJason Baron {
104e1452b60SJason Baron 	struct klp_object *obj;
105e1452b60SJason Baron 
106e1452b60SJason Baron 	klp_for_each_object(patch, obj) {
107e1452b60SJason Baron 		if (klp_is_module(old_obj)) {
108e1452b60SJason Baron 			if (klp_is_module(obj) &&
109e1452b60SJason Baron 			    strcmp(old_obj->name, obj->name) == 0) {
110e1452b60SJason Baron 				return obj;
111e1452b60SJason Baron 			}
112e1452b60SJason Baron 		} else if (!klp_is_module(obj)) {
113e1452b60SJason Baron 			return obj;
114e1452b60SJason Baron 		}
115e1452b60SJason Baron 	}
116e1452b60SJason Baron 
117e1452b60SJason Baron 	return NULL;
118e1452b60SJason Baron }
119e1452b60SJason Baron 
120b700e7f0SSeth Jennings struct klp_find_arg {
121b700e7f0SSeth Jennings 	const char *objname;
122b700e7f0SSeth Jennings 	const char *name;
123b700e7f0SSeth Jennings 	unsigned long addr;
124b700e7f0SSeth Jennings 	unsigned long count;
125b2b018efSChris J Arges 	unsigned long pos;
126b700e7f0SSeth Jennings };
127b700e7f0SSeth Jennings 
128b700e7f0SSeth Jennings static int klp_find_callback(void *data, const char *name,
129b700e7f0SSeth Jennings 			     struct module *mod, unsigned long addr)
130b700e7f0SSeth Jennings {
131b700e7f0SSeth Jennings 	struct klp_find_arg *args = data;
132b700e7f0SSeth Jennings 
133b700e7f0SSeth Jennings 	if ((mod && !args->objname) || (!mod && args->objname))
134b700e7f0SSeth Jennings 		return 0;
135b700e7f0SSeth Jennings 
136b700e7f0SSeth Jennings 	if (strcmp(args->name, name))
137b700e7f0SSeth Jennings 		return 0;
138b700e7f0SSeth Jennings 
139b700e7f0SSeth Jennings 	if (args->objname && strcmp(args->objname, mod->name))
140b700e7f0SSeth Jennings 		return 0;
141b700e7f0SSeth Jennings 
142b700e7f0SSeth Jennings 	args->addr = addr;
143b700e7f0SSeth Jennings 	args->count++;
144b700e7f0SSeth Jennings 
145b2b018efSChris J Arges 	/*
146b2b018efSChris J Arges 	 * Finish the search when the symbol is found for the desired position
147b2b018efSChris J Arges 	 * or the position is not defined for a non-unique symbol.
148b2b018efSChris J Arges 	 */
149b2b018efSChris J Arges 	if ((args->pos && (args->count == args->pos)) ||
150b2b018efSChris J Arges 	    (!args->pos && (args->count > 1)))
151b2b018efSChris J Arges 		return 1;
152b2b018efSChris J Arges 
153b700e7f0SSeth Jennings 	return 0;
154b700e7f0SSeth Jennings }
155b700e7f0SSeth Jennings 
156b700e7f0SSeth Jennings static int klp_find_object_symbol(const char *objname, const char *name,
157b2b018efSChris J Arges 				  unsigned long sympos, unsigned long *addr)
158b700e7f0SSeth Jennings {
159b700e7f0SSeth Jennings 	struct klp_find_arg args = {
160b700e7f0SSeth Jennings 		.objname = objname,
161b700e7f0SSeth Jennings 		.name = name,
162b700e7f0SSeth Jennings 		.addr = 0,
163b2b018efSChris J Arges 		.count = 0,
164b2b018efSChris J Arges 		.pos = sympos,
165b700e7f0SSeth Jennings 	};
166b700e7f0SSeth Jennings 
16772f04b50SZhou Chengming 	if (objname)
16872f04b50SZhou Chengming 		module_kallsyms_on_each_symbol(klp_find_callback, &args);
16972f04b50SZhou Chengming 	else
170b700e7f0SSeth Jennings 		kallsyms_on_each_symbol(klp_find_callback, &args);
171b700e7f0SSeth Jennings 
172b2b018efSChris J Arges 	/*
173b2b018efSChris J Arges 	 * Ensure an address was found. If sympos is 0, ensure symbol is unique;
174b2b018efSChris J Arges 	 * otherwise ensure the symbol position count matches sympos.
175b2b018efSChris J Arges 	 */
176b2b018efSChris J Arges 	if (args.addr == 0)
177b700e7f0SSeth Jennings 		pr_err("symbol '%s' not found in symbol table\n", name);
178b2b018efSChris J Arges 	else if (args.count > 1 && sympos == 0) {
179f995b5f7SPetr Mladek 		pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n",
180f995b5f7SPetr Mladek 		       name, objname);
181b2b018efSChris J Arges 	} else if (sympos != args.count && sympos > 0) {
182b2b018efSChris J Arges 		pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
183b2b018efSChris J Arges 		       sympos, name, objname ? objname : "vmlinux");
184b2b018efSChris J Arges 	} else {
185b700e7f0SSeth Jennings 		*addr = args.addr;
186b700e7f0SSeth Jennings 		return 0;
187b700e7f0SSeth Jennings 	}
188b700e7f0SSeth Jennings 
189b700e7f0SSeth Jennings 	*addr = 0;
190b700e7f0SSeth Jennings 	return -EINVAL;
191b700e7f0SSeth Jennings }
192b700e7f0SSeth Jennings 
1932f293651SChristophe Leroy static int klp_resolve_symbols(Elf_Shdr *sechdrs, const char *strtab,
194ca376a93SJosh Poimboeuf 			       unsigned int symndx, Elf_Shdr *relasec,
195ca376a93SJosh Poimboeuf 			       const char *sec_objname)
196b700e7f0SSeth Jennings {
197ca376a93SJosh Poimboeuf 	int i, cnt, ret;
198ca376a93SJosh Poimboeuf 	char sym_objname[MODULE_NAME_LEN];
199ca376a93SJosh Poimboeuf 	char sym_name[KSYM_NAME_LEN];
200425595a7SJessica Yu 	Elf_Rela *relas;
201425595a7SJessica Yu 	Elf_Sym *sym;
202425595a7SJessica Yu 	unsigned long sympos, addr;
203ca376a93SJosh Poimboeuf 	bool sym_vmlinux;
204ca376a93SJosh Poimboeuf 	bool sec_vmlinux = !strcmp(sec_objname, "vmlinux");
205b700e7f0SSeth Jennings 
206b2b018efSChris J Arges 	/*
207ca376a93SJosh Poimboeuf 	 * Since the field widths for sym_objname and sym_name in the sscanf()
208425595a7SJessica Yu 	 * call are hard-coded and correspond to MODULE_NAME_LEN and
209425595a7SJessica Yu 	 * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
210425595a7SJessica Yu 	 * and KSYM_NAME_LEN have the values we expect them to have.
211425595a7SJessica Yu 	 *
212425595a7SJessica Yu 	 * Because the value of MODULE_NAME_LEN can differ among architectures,
213425595a7SJessica Yu 	 * we use the smallest/strictest upper bound possible (56, based on
214425595a7SJessica Yu 	 * the current definition of MODULE_NAME_LEN) to prevent overflows.
215b2b018efSChris J Arges 	 */
216425595a7SJessica Yu 	BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128);
217425595a7SJessica Yu 
218425595a7SJessica Yu 	relas = (Elf_Rela *) relasec->sh_addr;
219425595a7SJessica Yu 	/* For each rela in this klp relocation section */
220425595a7SJessica Yu 	for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
2212f293651SChristophe Leroy 		sym = (Elf_Sym *)sechdrs[symndx].sh_addr + ELF_R_SYM(relas[i].r_info);
222425595a7SJessica Yu 		if (sym->st_shndx != SHN_LIVEPATCH) {
22377f8f39aSJosh Poimboeuf 			pr_err("symbol %s is not marked as a livepatch symbol\n",
224425595a7SJessica Yu 			       strtab + sym->st_name);
225425595a7SJessica Yu 			return -EINVAL;
226425595a7SJessica Yu 		}
227425595a7SJessica Yu 
228ca376a93SJosh Poimboeuf 		/* Format: .klp.sym.sym_objname.sym_name,sympos */
229425595a7SJessica Yu 		cnt = sscanf(strtab + sym->st_name,
230425595a7SJessica Yu 			     ".klp.sym.%55[^.].%127[^,],%lu",
231ca376a93SJosh Poimboeuf 			     sym_objname, sym_name, &sympos);
232425595a7SJessica Yu 		if (cnt != 3) {
23377f8f39aSJosh Poimboeuf 			pr_err("symbol %s has an incorrectly formatted name\n",
234425595a7SJessica Yu 			       strtab + sym->st_name);
235425595a7SJessica Yu 			return -EINVAL;
236425595a7SJessica Yu 		}
237425595a7SJessica Yu 
238ca376a93SJosh Poimboeuf 		sym_vmlinux = !strcmp(sym_objname, "vmlinux");
239ca376a93SJosh Poimboeuf 
240ca376a93SJosh Poimboeuf 		/*
241ca376a93SJosh Poimboeuf 		 * Prevent module-specific KLP rela sections from referencing
242ca376a93SJosh Poimboeuf 		 * vmlinux symbols.  This helps prevent ordering issues with
243ca376a93SJosh Poimboeuf 		 * module special section initializations.  Presumably such
244ca376a93SJosh Poimboeuf 		 * symbols are exported and normal relas can be used instead.
245ca376a93SJosh Poimboeuf 		 */
246ca376a93SJosh Poimboeuf 		if (!sec_vmlinux && sym_vmlinux) {
247ca376a93SJosh Poimboeuf 			pr_err("invalid access to vmlinux symbol '%s' from module-specific livepatch relocation section",
248ca376a93SJosh Poimboeuf 			       sym_name);
249ca376a93SJosh Poimboeuf 			return -EINVAL;
250ca376a93SJosh Poimboeuf 		}
251ca376a93SJosh Poimboeuf 
252425595a7SJessica Yu 		/* klp_find_object_symbol() treats a NULL objname as vmlinux */
253ca376a93SJosh Poimboeuf 		ret = klp_find_object_symbol(sym_vmlinux ? NULL : sym_objname,
254ca376a93SJosh Poimboeuf 					     sym_name, sympos, &addr);
255425595a7SJessica Yu 		if (ret)
256425595a7SJessica Yu 			return ret;
257425595a7SJessica Yu 
258425595a7SJessica Yu 		sym->st_value = addr;
259425595a7SJessica Yu 	}
260425595a7SJessica Yu 
261425595a7SJessica Yu 	return 0;
262b700e7f0SSeth Jennings }
263b700e7f0SSeth Jennings 
2647c8e2bddSJosh Poimboeuf /*
2657c8e2bddSJosh Poimboeuf  * At a high-level, there are two types of klp relocation sections: those which
2667c8e2bddSJosh Poimboeuf  * reference symbols which live in vmlinux; and those which reference symbols
2677c8e2bddSJosh Poimboeuf  * which live in other modules.  This function is called for both types:
2687c8e2bddSJosh Poimboeuf  *
2697c8e2bddSJosh Poimboeuf  * 1) When a klp module itself loads, the module code calls this function to
2707c8e2bddSJosh Poimboeuf  *    write vmlinux-specific klp relocations (.klp.rela.vmlinux.* sections).
2717c8e2bddSJosh Poimboeuf  *    These relocations are written to the klp module text to allow the patched
2727c8e2bddSJosh Poimboeuf  *    code/data to reference unexported vmlinux symbols.  They're written as
2737c8e2bddSJosh Poimboeuf  *    early as possible to ensure that other module init code (.e.g.,
2747c8e2bddSJosh Poimboeuf  *    jump_label_apply_nops) can access any unexported vmlinux symbols which
2757c8e2bddSJosh Poimboeuf  *    might be referenced by the klp module's special sections.
2767c8e2bddSJosh Poimboeuf  *
2777c8e2bddSJosh Poimboeuf  * 2) When a to-be-patched module loads -- or is already loaded when a
2787c8e2bddSJosh Poimboeuf  *    corresponding klp module loads -- klp code calls this function to write
2797c8e2bddSJosh Poimboeuf  *    module-specific klp relocations (.klp.rela.{module}.* sections).  These
2807c8e2bddSJosh Poimboeuf  *    are written to the klp module text to allow the patched code/data to
2817c8e2bddSJosh Poimboeuf  *    reference symbols which live in the to-be-patched module or one of its
2827c8e2bddSJosh Poimboeuf  *    module dependencies.  Exported symbols are supported, in addition to
2837c8e2bddSJosh Poimboeuf  *    unexported symbols, in order to enable late module patching, which allows
2847c8e2bddSJosh Poimboeuf  *    the to-be-patched module to be loaded and patched sometime *after* the
2857c8e2bddSJosh Poimboeuf  *    klp module is loaded.
2867c8e2bddSJosh Poimboeuf  */
2877c8e2bddSJosh Poimboeuf int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
2887c8e2bddSJosh Poimboeuf 			     const char *shstrtab, const char *strtab,
2897c8e2bddSJosh Poimboeuf 			     unsigned int symndx, unsigned int secndx,
2907c8e2bddSJosh Poimboeuf 			     const char *objname)
291b700e7f0SSeth Jennings {
2927c8e2bddSJosh Poimboeuf 	int cnt, ret;
293425595a7SJessica Yu 	char sec_objname[MODULE_NAME_LEN];
2947c8e2bddSJosh Poimboeuf 	Elf_Shdr *sec = sechdrs + secndx;
295b56b36eeSJosh Poimboeuf 
296425595a7SJessica Yu 	/*
297425595a7SJessica Yu 	 * Format: .klp.rela.sec_objname.section_name
298425595a7SJessica Yu 	 * See comment in klp_resolve_symbols() for an explanation
299425595a7SJessica Yu 	 * of the selected field width value.
300425595a7SJessica Yu 	 */
3017c8e2bddSJosh Poimboeuf 	cnt = sscanf(shstrtab + sec->sh_name, ".klp.rela.%55[^.]",
3027c8e2bddSJosh Poimboeuf 		     sec_objname);
303425595a7SJessica Yu 	if (cnt != 1) {
30477f8f39aSJosh Poimboeuf 		pr_err("section %s has an incorrectly formatted name\n",
3057c8e2bddSJosh Poimboeuf 		       shstrtab + sec->sh_name);
3067c8e2bddSJosh Poimboeuf 		return -EINVAL;
307b700e7f0SSeth Jennings 	}
308425595a7SJessica Yu 
3097c8e2bddSJosh Poimboeuf 	if (strcmp(objname ? objname : "vmlinux", sec_objname))
3107c8e2bddSJosh Poimboeuf 		return 0;
311425595a7SJessica Yu 
312ca376a93SJosh Poimboeuf 	ret = klp_resolve_symbols(sechdrs, strtab, symndx, sec, sec_objname);
313064c89dfSChris J Arges 	if (ret)
314b56b36eeSJosh Poimboeuf 		return ret;
3157c8e2bddSJosh Poimboeuf 
3167c8e2bddSJosh Poimboeuf 	return apply_relocate_add(sechdrs, strtab, symndx, secndx, pmod);
317b700e7f0SSeth Jennings }
318b700e7f0SSeth Jennings 
319b700e7f0SSeth Jennings /*
320b700e7f0SSeth Jennings  * Sysfs Interface
321b700e7f0SSeth Jennings  *
322b700e7f0SSeth Jennings  * /sys/kernel/livepatch
323b700e7f0SSeth Jennings  * /sys/kernel/livepatch/<patch>
324b700e7f0SSeth Jennings  * /sys/kernel/livepatch/<patch>/enabled
325d83a7cb3SJosh Poimboeuf  * /sys/kernel/livepatch/<patch>/transition
326c99a2be7SMiroslav Benes  * /sys/kernel/livepatch/<patch>/force
327b700e7f0SSeth Jennings  * /sys/kernel/livepatch/<patch>/<object>
328*bb26cfd9SSong Liu  * /sys/kernel/livepatch/<patch>/<object>/patched
329444f9e99SChris J Arges  * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
330b700e7f0SSeth Jennings  */
33126c3e98eSPetr Mladek static int __klp_disable_patch(struct klp_patch *patch);
332b700e7f0SSeth Jennings 
333b700e7f0SSeth Jennings static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
334b700e7f0SSeth Jennings 			     const char *buf, size_t count)
335b700e7f0SSeth Jennings {
336b700e7f0SSeth Jennings 	struct klp_patch *patch;
337b700e7f0SSeth Jennings 	int ret;
33868ae4b2bSJosh Poimboeuf 	bool enabled;
339b700e7f0SSeth Jennings 
34068ae4b2bSJosh Poimboeuf 	ret = kstrtobool(buf, &enabled);
341b700e7f0SSeth Jennings 	if (ret)
34268ae4b2bSJosh Poimboeuf 		return ret;
343b700e7f0SSeth Jennings 
344b700e7f0SSeth Jennings 	patch = container_of(kobj, struct klp_patch, kobj);
345b700e7f0SSeth Jennings 
346b700e7f0SSeth Jennings 	mutex_lock(&klp_mutex);
347b700e7f0SSeth Jennings 
34868ae4b2bSJosh Poimboeuf 	if (patch->enabled == enabled) {
349b700e7f0SSeth Jennings 		/* already in requested state */
350b700e7f0SSeth Jennings 		ret = -EINVAL;
351958ef1e3SPetr Mladek 		goto out;
352b700e7f0SSeth Jennings 	}
353b700e7f0SSeth Jennings 
354958ef1e3SPetr Mladek 	/*
355958ef1e3SPetr Mladek 	 * Allow to reverse a pending transition in both ways. It might be
356958ef1e3SPetr Mladek 	 * necessary to complete the transition without forcing and breaking
357958ef1e3SPetr Mladek 	 * the system integrity.
358958ef1e3SPetr Mladek 	 *
359958ef1e3SPetr Mladek 	 * Do not allow to re-enable a disabled patch.
360958ef1e3SPetr Mladek 	 */
361958ef1e3SPetr Mladek 	if (patch == klp_transition_patch)
362d83a7cb3SJosh Poimboeuf 		klp_reverse_transition();
363958ef1e3SPetr Mladek 	else if (!enabled)
364b700e7f0SSeth Jennings 		ret = __klp_disable_patch(patch);
365958ef1e3SPetr Mladek 	else
366958ef1e3SPetr Mladek 		ret = -EINVAL;
367958ef1e3SPetr Mladek 
368958ef1e3SPetr Mladek out:
369958ef1e3SPetr Mladek 	mutex_unlock(&klp_mutex);
370958ef1e3SPetr Mladek 
371b700e7f0SSeth Jennings 	if (ret)
372b700e7f0SSeth Jennings 		return ret;
373958ef1e3SPetr Mladek 	return count;
374b700e7f0SSeth Jennings }
375b700e7f0SSeth Jennings 
376b700e7f0SSeth Jennings static ssize_t enabled_show(struct kobject *kobj,
377b700e7f0SSeth Jennings 			    struct kobj_attribute *attr, char *buf)
378b700e7f0SSeth Jennings {
379b700e7f0SSeth Jennings 	struct klp_patch *patch;
380b700e7f0SSeth Jennings 
381b700e7f0SSeth Jennings 	patch = container_of(kobj, struct klp_patch, kobj);
3820dade9f3SJosh Poimboeuf 	return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled);
383b700e7f0SSeth Jennings }
384b700e7f0SSeth Jennings 
385d83a7cb3SJosh Poimboeuf static ssize_t transition_show(struct kobject *kobj,
386d83a7cb3SJosh Poimboeuf 			       struct kobj_attribute *attr, char *buf)
387d83a7cb3SJosh Poimboeuf {
388d83a7cb3SJosh Poimboeuf 	struct klp_patch *patch;
389d83a7cb3SJosh Poimboeuf 
390d83a7cb3SJosh Poimboeuf 	patch = container_of(kobj, struct klp_patch, kobj);
391d83a7cb3SJosh Poimboeuf 	return snprintf(buf, PAGE_SIZE-1, "%d\n",
392d83a7cb3SJosh Poimboeuf 			patch == klp_transition_patch);
393b700e7f0SSeth Jennings }
394b700e7f0SSeth Jennings 
395c99a2be7SMiroslav Benes static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
396c99a2be7SMiroslav Benes 			   const char *buf, size_t count)
397c99a2be7SMiroslav Benes {
398c99a2be7SMiroslav Benes 	struct klp_patch *patch;
399c99a2be7SMiroslav Benes 	int ret;
400c99a2be7SMiroslav Benes 	bool val;
401c99a2be7SMiroslav Benes 
402c99a2be7SMiroslav Benes 	ret = kstrtobool(buf, &val);
403c99a2be7SMiroslav Benes 	if (ret)
404c99a2be7SMiroslav Benes 		return ret;
405c99a2be7SMiroslav Benes 
4068869016dSMiroslav Benes 	if (!val)
4078869016dSMiroslav Benes 		return count;
4088869016dSMiroslav Benes 
4098869016dSMiroslav Benes 	mutex_lock(&klp_mutex);
4108869016dSMiroslav Benes 
4118869016dSMiroslav Benes 	patch = container_of(kobj, struct klp_patch, kobj);
4128869016dSMiroslav Benes 	if (patch != klp_transition_patch) {
4138869016dSMiroslav Benes 		mutex_unlock(&klp_mutex);
4148869016dSMiroslav Benes 		return -EINVAL;
4158869016dSMiroslav Benes 	}
4168869016dSMiroslav Benes 
417c99a2be7SMiroslav Benes 	klp_force_transition();
418c99a2be7SMiroslav Benes 
4198869016dSMiroslav Benes 	mutex_unlock(&klp_mutex);
4208869016dSMiroslav Benes 
421c99a2be7SMiroslav Benes 	return count;
422c99a2be7SMiroslav Benes }
423c99a2be7SMiroslav Benes 
424b700e7f0SSeth Jennings static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
425d83a7cb3SJosh Poimboeuf static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition);
426c99a2be7SMiroslav Benes static struct kobj_attribute force_kobj_attr = __ATTR_WO(force);
427b700e7f0SSeth Jennings static struct attribute *klp_patch_attrs[] = {
428b700e7f0SSeth Jennings 	&enabled_kobj_attr.attr,
429d83a7cb3SJosh Poimboeuf 	&transition_kobj_attr.attr,
430c99a2be7SMiroslav Benes 	&force_kobj_attr.attr,
431b700e7f0SSeth Jennings 	NULL
432b700e7f0SSeth Jennings };
43370283454SKimberly Brown ATTRIBUTE_GROUPS(klp_patch);
434b700e7f0SSeth Jennings 
435*bb26cfd9SSong Liu static ssize_t patched_show(struct kobject *kobj,
436*bb26cfd9SSong Liu 			    struct kobj_attribute *attr, char *buf)
437*bb26cfd9SSong Liu {
438*bb26cfd9SSong Liu 	struct klp_object *obj;
439*bb26cfd9SSong Liu 
440*bb26cfd9SSong Liu 	obj = container_of(kobj, struct klp_object, kobj);
441*bb26cfd9SSong Liu 	return sysfs_emit(buf, "%d\n", obj->patched);
442*bb26cfd9SSong Liu }
443*bb26cfd9SSong Liu 
444*bb26cfd9SSong Liu static struct kobj_attribute patched_kobj_attr = __ATTR_RO(patched);
445*bb26cfd9SSong Liu static struct attribute *klp_object_attrs[] = {
446*bb26cfd9SSong Liu 	&patched_kobj_attr.attr,
447*bb26cfd9SSong Liu 	NULL,
448*bb26cfd9SSong Liu };
449*bb26cfd9SSong Liu ATTRIBUTE_GROUPS(klp_object);
450*bb26cfd9SSong Liu 
451e1452b60SJason Baron static void klp_free_object_dynamic(struct klp_object *obj)
452e1452b60SJason Baron {
453e1452b60SJason Baron 	kfree(obj->name);
454e1452b60SJason Baron 	kfree(obj);
455e1452b60SJason Baron }
456e1452b60SJason Baron 
457f68d67cfSPetr Mladek static void klp_init_func_early(struct klp_object *obj,
458f68d67cfSPetr Mladek 				struct klp_func *func);
459f68d67cfSPetr Mladek static void klp_init_object_early(struct klp_patch *patch,
460f68d67cfSPetr Mladek 				  struct klp_object *obj);
4614d141ab3SPetr Mladek 
462f68d67cfSPetr Mladek static struct klp_object *klp_alloc_object_dynamic(const char *name,
463f68d67cfSPetr Mladek 						   struct klp_patch *patch)
464e1452b60SJason Baron {
465e1452b60SJason Baron 	struct klp_object *obj;
466e1452b60SJason Baron 
467e1452b60SJason Baron 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
468e1452b60SJason Baron 	if (!obj)
469e1452b60SJason Baron 		return NULL;
470e1452b60SJason Baron 
471e1452b60SJason Baron 	if (name) {
472e1452b60SJason Baron 		obj->name = kstrdup(name, GFP_KERNEL);
473e1452b60SJason Baron 		if (!obj->name) {
474e1452b60SJason Baron 			kfree(obj);
475e1452b60SJason Baron 			return NULL;
476e1452b60SJason Baron 		}
477e1452b60SJason Baron 	}
478e1452b60SJason Baron 
479f68d67cfSPetr Mladek 	klp_init_object_early(patch, obj);
480e1452b60SJason Baron 	obj->dynamic = true;
481e1452b60SJason Baron 
482e1452b60SJason Baron 	return obj;
483e1452b60SJason Baron }
484e1452b60SJason Baron 
485e1452b60SJason Baron static void klp_free_func_nop(struct klp_func *func)
486e1452b60SJason Baron {
487e1452b60SJason Baron 	kfree(func->old_name);
488e1452b60SJason Baron 	kfree(func);
489e1452b60SJason Baron }
490e1452b60SJason Baron 
491e1452b60SJason Baron static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func,
492e1452b60SJason Baron 					   struct klp_object *obj)
493e1452b60SJason Baron {
494e1452b60SJason Baron 	struct klp_func *func;
495e1452b60SJason Baron 
496e1452b60SJason Baron 	func = kzalloc(sizeof(*func), GFP_KERNEL);
497e1452b60SJason Baron 	if (!func)
498e1452b60SJason Baron 		return NULL;
499e1452b60SJason Baron 
500e1452b60SJason Baron 	if (old_func->old_name) {
501e1452b60SJason Baron 		func->old_name = kstrdup(old_func->old_name, GFP_KERNEL);
502e1452b60SJason Baron 		if (!func->old_name) {
503e1452b60SJason Baron 			kfree(func);
504e1452b60SJason Baron 			return NULL;
505e1452b60SJason Baron 		}
506e1452b60SJason Baron 	}
507e1452b60SJason Baron 
508f68d67cfSPetr Mladek 	klp_init_func_early(obj, func);
509e1452b60SJason Baron 	/*
510e1452b60SJason Baron 	 * func->new_func is same as func->old_func. These addresses are
511e1452b60SJason Baron 	 * set when the object is loaded, see klp_init_object_loaded().
512e1452b60SJason Baron 	 */
513e1452b60SJason Baron 	func->old_sympos = old_func->old_sympos;
514e1452b60SJason Baron 	func->nop = true;
515e1452b60SJason Baron 
516e1452b60SJason Baron 	return func;
517e1452b60SJason Baron }
518e1452b60SJason Baron 
519e1452b60SJason Baron static int klp_add_object_nops(struct klp_patch *patch,
520e1452b60SJason Baron 			       struct klp_object *old_obj)
521e1452b60SJason Baron {
522e1452b60SJason Baron 	struct klp_object *obj;
523e1452b60SJason Baron 	struct klp_func *func, *old_func;
524e1452b60SJason Baron 
525e1452b60SJason Baron 	obj = klp_find_object(patch, old_obj);
526e1452b60SJason Baron 
527e1452b60SJason Baron 	if (!obj) {
528f68d67cfSPetr Mladek 		obj = klp_alloc_object_dynamic(old_obj->name, patch);
529e1452b60SJason Baron 		if (!obj)
530e1452b60SJason Baron 			return -ENOMEM;
531e1452b60SJason Baron 	}
532e1452b60SJason Baron 
533e1452b60SJason Baron 	klp_for_each_func(old_obj, old_func) {
534e1452b60SJason Baron 		func = klp_find_func(obj, old_func);
535e1452b60SJason Baron 		if (func)
536e1452b60SJason Baron 			continue;
537e1452b60SJason Baron 
538e1452b60SJason Baron 		func = klp_alloc_func_nop(old_func, obj);
539e1452b60SJason Baron 		if (!func)
540e1452b60SJason Baron 			return -ENOMEM;
541e1452b60SJason Baron 	}
542e1452b60SJason Baron 
543e1452b60SJason Baron 	return 0;
544e1452b60SJason Baron }
545e1452b60SJason Baron 
546e1452b60SJason Baron /*
547e1452b60SJason Baron  * Add 'nop' functions which simply return to the caller to run
548e1452b60SJason Baron  * the original function. The 'nop' functions are added to a
549e1452b60SJason Baron  * patch to facilitate a 'replace' mode.
550e1452b60SJason Baron  */
551e1452b60SJason Baron static int klp_add_nops(struct klp_patch *patch)
552e1452b60SJason Baron {
553e1452b60SJason Baron 	struct klp_patch *old_patch;
554e1452b60SJason Baron 	struct klp_object *old_obj;
555e1452b60SJason Baron 
556ecba29f4SPetr Mladek 	klp_for_each_patch(old_patch) {
557e1452b60SJason Baron 		klp_for_each_object(old_patch, old_obj) {
558e1452b60SJason Baron 			int err;
559e1452b60SJason Baron 
560e1452b60SJason Baron 			err = klp_add_object_nops(patch, old_obj);
561e1452b60SJason Baron 			if (err)
562e1452b60SJason Baron 				return err;
563e1452b60SJason Baron 		}
564e1452b60SJason Baron 	}
565e1452b60SJason Baron 
566e1452b60SJason Baron 	return 0;
567e1452b60SJason Baron }
568e1452b60SJason Baron 
569b700e7f0SSeth Jennings static void klp_kobj_release_patch(struct kobject *kobj)
570b700e7f0SSeth Jennings {
5713ec24776SJosh Poimboeuf 	struct klp_patch *patch;
5723ec24776SJosh Poimboeuf 
5733ec24776SJosh Poimboeuf 	patch = container_of(kobj, struct klp_patch, kobj);
5743ec24776SJosh Poimboeuf 	complete(&patch->finish);
575b700e7f0SSeth Jennings }
576b700e7f0SSeth Jennings 
577b700e7f0SSeth Jennings static struct kobj_type klp_ktype_patch = {
578b700e7f0SSeth Jennings 	.release = klp_kobj_release_patch,
579b700e7f0SSeth Jennings 	.sysfs_ops = &kobj_sysfs_ops,
58070283454SKimberly Brown 	.default_groups = klp_patch_groups,
581b700e7f0SSeth Jennings };
582b700e7f0SSeth Jennings 
583cad706dfSMiroslav Benes static void klp_kobj_release_object(struct kobject *kobj)
584cad706dfSMiroslav Benes {
585e1452b60SJason Baron 	struct klp_object *obj;
586e1452b60SJason Baron 
587e1452b60SJason Baron 	obj = container_of(kobj, struct klp_object, kobj);
588e1452b60SJason Baron 
589e1452b60SJason Baron 	if (obj->dynamic)
590e1452b60SJason Baron 		klp_free_object_dynamic(obj);
591cad706dfSMiroslav Benes }
592cad706dfSMiroslav Benes 
593cad706dfSMiroslav Benes static struct kobj_type klp_ktype_object = {
594cad706dfSMiroslav Benes 	.release = klp_kobj_release_object,
595cad706dfSMiroslav Benes 	.sysfs_ops = &kobj_sysfs_ops,
596*bb26cfd9SSong Liu 	.default_groups = klp_object_groups,
597cad706dfSMiroslav Benes };
598cad706dfSMiroslav Benes 
599b700e7f0SSeth Jennings static void klp_kobj_release_func(struct kobject *kobj)
600b700e7f0SSeth Jennings {
601e1452b60SJason Baron 	struct klp_func *func;
602e1452b60SJason Baron 
603e1452b60SJason Baron 	func = container_of(kobj, struct klp_func, kobj);
604e1452b60SJason Baron 
605e1452b60SJason Baron 	if (func->nop)
606e1452b60SJason Baron 		klp_free_func_nop(func);
607b700e7f0SSeth Jennings }
608b700e7f0SSeth Jennings 
609b700e7f0SSeth Jennings static struct kobj_type klp_ktype_func = {
610b700e7f0SSeth Jennings 	.release = klp_kobj_release_func,
611b700e7f0SSeth Jennings 	.sysfs_ops = &kobj_sysfs_ops,
612b700e7f0SSeth Jennings };
613b700e7f0SSeth Jennings 
614d697bad5SPetr Mladek static void __klp_free_funcs(struct klp_object *obj, bool nops_only)
615b700e7f0SSeth Jennings {
616e1452b60SJason Baron 	struct klp_func *func, *tmp_func;
617b700e7f0SSeth Jennings 
618e1452b60SJason Baron 	klp_for_each_func_safe(obj, func, tmp_func) {
619d697bad5SPetr Mladek 		if (nops_only && !func->nop)
620d697bad5SPetr Mladek 			continue;
621d697bad5SPetr Mladek 
622d697bad5SPetr Mladek 		list_del(&func->node);
623b700e7f0SSeth Jennings 		kobject_put(&func->kobj);
624b700e7f0SSeth Jennings 	}
6250430f78bSPetr Mladek }
626b700e7f0SSeth Jennings 
627b700e7f0SSeth Jennings /* Clean up when a patched object is unloaded */
628b700e7f0SSeth Jennings static void klp_free_object_loaded(struct klp_object *obj)
629b700e7f0SSeth Jennings {
630b700e7f0SSeth Jennings 	struct klp_func *func;
631b700e7f0SSeth Jennings 
632b700e7f0SSeth Jennings 	obj->mod = NULL;
633b700e7f0SSeth Jennings 
634e1452b60SJason Baron 	klp_for_each_func(obj, func) {
63519514910SPetr Mladek 		func->old_func = NULL;
636e1452b60SJason Baron 
637e1452b60SJason Baron 		if (func->nop)
638e1452b60SJason Baron 			func->new_func = NULL;
639e1452b60SJason Baron 	}
640b700e7f0SSeth Jennings }
641b700e7f0SSeth Jennings 
642d697bad5SPetr Mladek static void __klp_free_objects(struct klp_patch *patch, bool nops_only)
643b700e7f0SSeth Jennings {
644e1452b60SJason Baron 	struct klp_object *obj, *tmp_obj;
645b700e7f0SSeth Jennings 
646e1452b60SJason Baron 	klp_for_each_object_safe(patch, obj, tmp_obj) {
647d697bad5SPetr Mladek 		__klp_free_funcs(obj, nops_only);
648d697bad5SPetr Mladek 
649d697bad5SPetr Mladek 		if (nops_only && !obj->dynamic)
650d697bad5SPetr Mladek 			continue;
651d697bad5SPetr Mladek 
652d697bad5SPetr Mladek 		list_del(&obj->node);
653cad706dfSMiroslav Benes 		kobject_put(&obj->kobj);
654b700e7f0SSeth Jennings 	}
655b700e7f0SSeth Jennings }
656b700e7f0SSeth Jennings 
657d697bad5SPetr Mladek static void klp_free_objects(struct klp_patch *patch)
658d697bad5SPetr Mladek {
659d697bad5SPetr Mladek 	__klp_free_objects(patch, false);
660d697bad5SPetr Mladek }
661d697bad5SPetr Mladek 
662d697bad5SPetr Mladek static void klp_free_objects_dynamic(struct klp_patch *patch)
663d697bad5SPetr Mladek {
664d697bad5SPetr Mladek 	__klp_free_objects(patch, true);
665d697bad5SPetr Mladek }
666d697bad5SPetr Mladek 
6670430f78bSPetr Mladek /*
6680430f78bSPetr Mladek  * This function implements the free operations that can be called safely
6690430f78bSPetr Mladek  * under klp_mutex.
6700430f78bSPetr Mladek  *
6710430f78bSPetr Mladek  * The operation must be completed by calling klp_free_patch_finish()
6720430f78bSPetr Mladek  * outside klp_mutex.
6730430f78bSPetr Mladek  */
6747e35e4ebSPetr Mladek static void klp_free_patch_start(struct klp_patch *patch)
675b700e7f0SSeth Jennings {
676b700e7f0SSeth Jennings 	if (!list_empty(&patch->list))
677b700e7f0SSeth Jennings 		list_del(&patch->list);
6780430f78bSPetr Mladek 
6790430f78bSPetr Mladek 	klp_free_objects(patch);
6800430f78bSPetr Mladek }
6810430f78bSPetr Mladek 
6820430f78bSPetr Mladek /*
6830430f78bSPetr Mladek  * This function implements the free part that must be called outside
6840430f78bSPetr Mladek  * klp_mutex.
6850430f78bSPetr Mladek  *
6860430f78bSPetr Mladek  * It must be called after klp_free_patch_start(). And it has to be
6870430f78bSPetr Mladek  * the last function accessing the livepatch structures when the patch
6880430f78bSPetr Mladek  * gets disabled.
6890430f78bSPetr Mladek  */
6900430f78bSPetr Mladek static void klp_free_patch_finish(struct klp_patch *patch)
6910430f78bSPetr Mladek {
6920430f78bSPetr Mladek 	/*
6930430f78bSPetr Mladek 	 * Avoid deadlock with enabled_store() sysfs callback by
6940430f78bSPetr Mladek 	 * calling this outside klp_mutex. It is safe because
6950430f78bSPetr Mladek 	 * this is called when the patch gets disabled and it
6960430f78bSPetr Mladek 	 * cannot get enabled again.
6970430f78bSPetr Mladek 	 */
6980430f78bSPetr Mladek 	kobject_put(&patch->kobj);
6990430f78bSPetr Mladek 	wait_for_completion(&patch->finish);
700958ef1e3SPetr Mladek 
701958ef1e3SPetr Mladek 	/* Put the module after the last access to struct klp_patch. */
702958ef1e3SPetr Mladek 	if (!patch->forced)
703958ef1e3SPetr Mladek 		module_put(patch->mod);
704958ef1e3SPetr Mladek }
705958ef1e3SPetr Mladek 
706958ef1e3SPetr Mladek /*
707958ef1e3SPetr Mladek  * The livepatch might be freed from sysfs interface created by the patch.
708958ef1e3SPetr Mladek  * This work allows to wait until the interface is destroyed in a separate
709958ef1e3SPetr Mladek  * context.
710958ef1e3SPetr Mladek  */
711958ef1e3SPetr Mladek static void klp_free_patch_work_fn(struct work_struct *work)
712958ef1e3SPetr Mladek {
713958ef1e3SPetr Mladek 	struct klp_patch *patch =
714958ef1e3SPetr Mladek 		container_of(work, struct klp_patch, free_work);
715958ef1e3SPetr Mladek 
716958ef1e3SPetr Mladek 	klp_free_patch_finish(patch);
717b700e7f0SSeth Jennings }
718b700e7f0SSeth Jennings 
7197e35e4ebSPetr Mladek void klp_free_patch_async(struct klp_patch *patch)
7207e35e4ebSPetr Mladek {
7217e35e4ebSPetr Mladek 	klp_free_patch_start(patch);
7227e35e4ebSPetr Mladek 	schedule_work(&patch->free_work);
7237e35e4ebSPetr Mladek }
7247e35e4ebSPetr Mladek 
7257e35e4ebSPetr Mladek void klp_free_replaced_patches_async(struct klp_patch *new_patch)
7267e35e4ebSPetr Mladek {
7277e35e4ebSPetr Mladek 	struct klp_patch *old_patch, *tmp_patch;
7287e35e4ebSPetr Mladek 
7297e35e4ebSPetr Mladek 	klp_for_each_patch_safe(old_patch, tmp_patch) {
7307e35e4ebSPetr Mladek 		if (old_patch == new_patch)
7317e35e4ebSPetr Mladek 			return;
7327e35e4ebSPetr Mladek 		klp_free_patch_async(old_patch);
7337e35e4ebSPetr Mladek 	}
7347e35e4ebSPetr Mladek }
7357e35e4ebSPetr Mladek 
736b700e7f0SSeth Jennings static int klp_init_func(struct klp_object *obj, struct klp_func *func)
737b700e7f0SSeth Jennings {
738e1452b60SJason Baron 	if (!func->old_name)
739e1452b60SJason Baron 		return -EINVAL;
740e1452b60SJason Baron 
741e1452b60SJason Baron 	/*
742e1452b60SJason Baron 	 * NOPs get the address later. The patched module must be loaded,
743e1452b60SJason Baron 	 * see klp_init_object_loaded().
744e1452b60SJason Baron 	 */
745e1452b60SJason Baron 	if (!func->new_func && !func->nop)
746f09d9086SMiroslav Benes 		return -EINVAL;
747f09d9086SMiroslav Benes 
7486e9df95bSKamalesh Babulal 	if (strlen(func->old_name) >= KSYM_NAME_LEN)
7496e9df95bSKamalesh Babulal 		return -EINVAL;
7506e9df95bSKamalesh Babulal 
7513c33f5b9SJosh Poimboeuf 	INIT_LIST_HEAD(&func->stack_node);
7520dade9f3SJosh Poimboeuf 	func->patched = false;
753d83a7cb3SJosh Poimboeuf 	func->transition = false;
754b700e7f0SSeth Jennings 
755444f9e99SChris J Arges 	/* The format for the sysfs directory is <function,sympos> where sympos
756444f9e99SChris J Arges 	 * is the nth occurrence of this symbol in kallsyms for the patched
757444f9e99SChris J Arges 	 * object. If the user selects 0 for old_sympos, then 1 will be used
758444f9e99SChris J Arges 	 * since a unique symbol will be the first occurrence.
759444f9e99SChris J Arges 	 */
7604d141ab3SPetr Mladek 	return kobject_add(&func->kobj, &obj->kobj, "%s,%lu",
7614d141ab3SPetr Mladek 			   func->old_name,
762444f9e99SChris J Arges 			   func->old_sympos ? func->old_sympos : 1);
763b700e7f0SSeth Jennings }
764b700e7f0SSeth Jennings 
765a4ae16f6SSamuel Zou static int klp_apply_object_relocs(struct klp_patch *patch,
766a4ae16f6SSamuel Zou 				   struct klp_object *obj)
7677c8e2bddSJosh Poimboeuf {
7687c8e2bddSJosh Poimboeuf 	int i, ret;
7697c8e2bddSJosh Poimboeuf 	struct klp_modinfo *info = patch->mod->klp_info;
7707c8e2bddSJosh Poimboeuf 
7717c8e2bddSJosh Poimboeuf 	for (i = 1; i < info->hdr.e_shnum; i++) {
7727c8e2bddSJosh Poimboeuf 		Elf_Shdr *sec = info->sechdrs + i;
7737c8e2bddSJosh Poimboeuf 
7747c8e2bddSJosh Poimboeuf 		if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
7757c8e2bddSJosh Poimboeuf 			continue;
7767c8e2bddSJosh Poimboeuf 
7777c8e2bddSJosh Poimboeuf 		ret = klp_apply_section_relocs(patch->mod, info->sechdrs,
7787c8e2bddSJosh Poimboeuf 					       info->secstrings,
7797c8e2bddSJosh Poimboeuf 					       patch->mod->core_kallsyms.strtab,
7807c8e2bddSJosh Poimboeuf 					       info->symndx, i, obj->name);
7817c8e2bddSJosh Poimboeuf 		if (ret)
7827c8e2bddSJosh Poimboeuf 			return ret;
7837c8e2bddSJosh Poimboeuf 	}
7847c8e2bddSJosh Poimboeuf 
7857c8e2bddSJosh Poimboeuf 	return 0;
7867c8e2bddSJosh Poimboeuf }
7877c8e2bddSJosh Poimboeuf 
788b700e7f0SSeth Jennings /* parts of the initialization that is done only when the object is loaded */
789b700e7f0SSeth Jennings static int klp_init_object_loaded(struct klp_patch *patch,
790b700e7f0SSeth Jennings 				  struct klp_object *obj)
791b700e7f0SSeth Jennings {
792b700e7f0SSeth Jennings 	struct klp_func *func;
793b700e7f0SSeth Jennings 	int ret;
794b700e7f0SSeth Jennings 
7951d05334dSPeter Zijlstra 	if (klp_is_module(obj)) {
7967c8e2bddSJosh Poimboeuf 		/*
7977c8e2bddSJosh Poimboeuf 		 * Only write module-specific relocations here
7987c8e2bddSJosh Poimboeuf 		 * (.klp.rela.{module}.*).  vmlinux-specific relocations were
7997c8e2bddSJosh Poimboeuf 		 * written earlier during the initialization of the klp module
8007c8e2bddSJosh Poimboeuf 		 * itself.
8017c8e2bddSJosh Poimboeuf 		 */
8027c8e2bddSJosh Poimboeuf 		ret = klp_apply_object_relocs(patch, obj);
8031d05334dSPeter Zijlstra 		if (ret)
804b700e7f0SSeth Jennings 			return ret;
805255e732cSJessica Yu 	}
8069f255b63SJosh Poimboeuf 
8078cdd043aSJiri Slaby 	klp_for_each_func(obj, func) {
808b2b018efSChris J Arges 		ret = klp_find_object_symbol(obj->name, func->old_name,
809b2b018efSChris J Arges 					     func->old_sympos,
81019514910SPetr Mladek 					     (unsigned long *)&func->old_func);
811b700e7f0SSeth Jennings 		if (ret)
812b700e7f0SSeth Jennings 			return ret;
813f5e547f4SJosh Poimboeuf 
81419514910SPetr Mladek 		ret = kallsyms_lookup_size_offset((unsigned long)func->old_func,
815f5e547f4SJosh Poimboeuf 						  &func->old_size, NULL);
816f5e547f4SJosh Poimboeuf 		if (!ret) {
817f5e547f4SJosh Poimboeuf 			pr_err("kallsyms size lookup failed for '%s'\n",
818f5e547f4SJosh Poimboeuf 			       func->old_name);
819f5e547f4SJosh Poimboeuf 			return -ENOENT;
820f5e547f4SJosh Poimboeuf 		}
821f5e547f4SJosh Poimboeuf 
822e1452b60SJason Baron 		if (func->nop)
823e1452b60SJason Baron 			func->new_func = func->old_func;
824e1452b60SJason Baron 
825f5e547f4SJosh Poimboeuf 		ret = kallsyms_lookup_size_offset((unsigned long)func->new_func,
826f5e547f4SJosh Poimboeuf 						  &func->new_size, NULL);
827f5e547f4SJosh Poimboeuf 		if (!ret) {
828f5e547f4SJosh Poimboeuf 			pr_err("kallsyms size lookup failed for '%s' replacement\n",
829f5e547f4SJosh Poimboeuf 			       func->old_name);
830f5e547f4SJosh Poimboeuf 			return -ENOENT;
831f5e547f4SJosh Poimboeuf 		}
832b700e7f0SSeth Jennings 	}
833b700e7f0SSeth Jennings 
834b700e7f0SSeth Jennings 	return 0;
835b700e7f0SSeth Jennings }
836b700e7f0SSeth Jennings 
837b700e7f0SSeth Jennings static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
838b700e7f0SSeth Jennings {
839b700e7f0SSeth Jennings 	struct klp_func *func;
840b700e7f0SSeth Jennings 	int ret;
841b700e7f0SSeth Jennings 	const char *name;
842b700e7f0SSeth Jennings 
8436e9df95bSKamalesh Babulal 	if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN)
8446e9df95bSKamalesh Babulal 		return -EINVAL;
8456e9df95bSKamalesh Babulal 
8460dade9f3SJosh Poimboeuf 	obj->patched = false;
8478cb2c2dcSPetr Mladek 	obj->mod = NULL;
848b700e7f0SSeth Jennings 
849b700e7f0SSeth Jennings 	klp_find_object_module(obj);
850b700e7f0SSeth Jennings 
851b700e7f0SSeth Jennings 	name = klp_is_module(obj) ? obj->name : "vmlinux";
8524d141ab3SPetr Mladek 	ret = kobject_add(&obj->kobj, &patch->kobj, "%s", name);
853cad706dfSMiroslav Benes 	if (ret)
854cad706dfSMiroslav Benes 		return ret;
855b700e7f0SSeth Jennings 
8568cdd043aSJiri Slaby 	klp_for_each_func(obj, func) {
857b700e7f0SSeth Jennings 		ret = klp_init_func(obj, func);
858b700e7f0SSeth Jennings 		if (ret)
8590430f78bSPetr Mladek 			return ret;
860b700e7f0SSeth Jennings 	}
861b700e7f0SSeth Jennings 
8620430f78bSPetr Mladek 	if (klp_is_object_loaded(obj))
863b700e7f0SSeth Jennings 		ret = klp_init_object_loaded(patch, obj);
8640430f78bSPetr Mladek 
8650430f78bSPetr Mladek 	return ret;
8660430f78bSPetr Mladek }
8670430f78bSPetr Mladek 
868f68d67cfSPetr Mladek static void klp_init_func_early(struct klp_object *obj,
869f68d67cfSPetr Mladek 				struct klp_func *func)
870f68d67cfSPetr Mladek {
871f68d67cfSPetr Mladek 	kobject_init(&func->kobj, &klp_ktype_func);
872f68d67cfSPetr Mladek 	list_add_tail(&func->node, &obj->func_list);
873f68d67cfSPetr Mladek }
874f68d67cfSPetr Mladek 
875f68d67cfSPetr Mladek static void klp_init_object_early(struct klp_patch *patch,
876f68d67cfSPetr Mladek 				  struct klp_object *obj)
877f68d67cfSPetr Mladek {
878f68d67cfSPetr Mladek 	INIT_LIST_HEAD(&obj->func_list);
879f68d67cfSPetr Mladek 	kobject_init(&obj->kobj, &klp_ktype_object);
880f68d67cfSPetr Mladek 	list_add_tail(&obj->node, &patch->obj_list);
881f68d67cfSPetr Mladek }
882f68d67cfSPetr Mladek 
8835ef3dd20SDavid Vernet static void klp_init_patch_early(struct klp_patch *patch)
8840430f78bSPetr Mladek {
8850430f78bSPetr Mladek 	struct klp_object *obj;
8860430f78bSPetr Mladek 	struct klp_func *func;
8870430f78bSPetr Mladek 
8880430f78bSPetr Mladek 	INIT_LIST_HEAD(&patch->list);
88920e55025SJason Baron 	INIT_LIST_HEAD(&patch->obj_list);
8904d141ab3SPetr Mladek 	kobject_init(&patch->kobj, &klp_ktype_patch);
8910430f78bSPetr Mladek 	patch->enabled = false;
89268007289SPetr Mladek 	patch->forced = false;
893958ef1e3SPetr Mladek 	INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
8940430f78bSPetr Mladek 	init_completion(&patch->finish);
8950430f78bSPetr Mladek 
89620e55025SJason Baron 	klp_for_each_object_static(patch, obj) {
897f68d67cfSPetr Mladek 		klp_init_object_early(patch, obj);
8980430f78bSPetr Mladek 
89920e55025SJason Baron 		klp_for_each_func_static(obj, func) {
900f68d67cfSPetr Mladek 			klp_init_func_early(obj, func);
90120e55025SJason Baron 		}
902b700e7f0SSeth Jennings 	}
903b700e7f0SSeth Jennings }
904b700e7f0SSeth Jennings 
905b700e7f0SSeth Jennings static int klp_init_patch(struct klp_patch *patch)
906b700e7f0SSeth Jennings {
907b700e7f0SSeth Jennings 	struct klp_object *obj;
908b700e7f0SSeth Jennings 	int ret;
909b700e7f0SSeth Jennings 
9104d141ab3SPetr Mladek 	ret = kobject_add(&patch->kobj, klp_root_kobj, "%s", patch->mod->name);
911958ef1e3SPetr Mladek 	if (ret)
9123ec24776SJosh Poimboeuf 		return ret;
913b700e7f0SSeth Jennings 
914e1452b60SJason Baron 	if (patch->replace) {
915e1452b60SJason Baron 		ret = klp_add_nops(patch);
916e1452b60SJason Baron 		if (ret)
917e1452b60SJason Baron 			return ret;
918e1452b60SJason Baron 	}
919e1452b60SJason Baron 
9208cdd043aSJiri Slaby 	klp_for_each_object(patch, obj) {
921b700e7f0SSeth Jennings 		ret = klp_init_object(patch, obj);
922b700e7f0SSeth Jennings 		if (ret)
923958ef1e3SPetr Mladek 			return ret;
924b700e7f0SSeth Jennings 	}
925b700e7f0SSeth Jennings 
92699590ba5SJosh Poimboeuf 	list_add_tail(&patch->list, &klp_patches);
927b700e7f0SSeth Jennings 
928b700e7f0SSeth Jennings 	return 0;
929b700e7f0SSeth Jennings }
930b700e7f0SSeth Jennings 
93126c3e98eSPetr Mladek static int __klp_disable_patch(struct klp_patch *patch)
93226c3e98eSPetr Mladek {
93326c3e98eSPetr Mladek 	struct klp_object *obj;
93426c3e98eSPetr Mladek 
93526c3e98eSPetr Mladek 	if (WARN_ON(!patch->enabled))
93626c3e98eSPetr Mladek 		return -EINVAL;
93726c3e98eSPetr Mladek 
93826c3e98eSPetr Mladek 	if (klp_transition_patch)
93926c3e98eSPetr Mladek 		return -EBUSY;
94026c3e98eSPetr Mladek 
94126c3e98eSPetr Mladek 	klp_init_transition(patch, KLP_UNPATCHED);
94226c3e98eSPetr Mladek 
94326c3e98eSPetr Mladek 	klp_for_each_object(patch, obj)
94426c3e98eSPetr Mladek 		if (obj->patched)
94526c3e98eSPetr Mladek 			klp_pre_unpatch_callback(obj);
94626c3e98eSPetr Mladek 
94726c3e98eSPetr Mladek 	/*
94826c3e98eSPetr Mladek 	 * Enforce the order of the func->transition writes in
94926c3e98eSPetr Mladek 	 * klp_init_transition() and the TIF_PATCH_PENDING writes in
95026c3e98eSPetr Mladek 	 * klp_start_transition().  In the rare case where klp_ftrace_handler()
95126c3e98eSPetr Mladek 	 * is called shortly after klp_update_patch_state() switches the task,
95226c3e98eSPetr Mladek 	 * this ensures the handler sees that func->transition is set.
95326c3e98eSPetr Mladek 	 */
95426c3e98eSPetr Mladek 	smp_wmb();
95526c3e98eSPetr Mladek 
95626c3e98eSPetr Mladek 	klp_start_transition();
95726c3e98eSPetr Mladek 	patch->enabled = false;
958958ef1e3SPetr Mladek 	klp_try_complete_transition();
95926c3e98eSPetr Mladek 
96026c3e98eSPetr Mladek 	return 0;
96126c3e98eSPetr Mladek }
96226c3e98eSPetr Mladek 
96326c3e98eSPetr Mladek static int __klp_enable_patch(struct klp_patch *patch)
96426c3e98eSPetr Mladek {
96526c3e98eSPetr Mladek 	struct klp_object *obj;
96626c3e98eSPetr Mladek 	int ret;
96726c3e98eSPetr Mladek 
96826c3e98eSPetr Mladek 	if (klp_transition_patch)
96926c3e98eSPetr Mladek 		return -EBUSY;
97026c3e98eSPetr Mladek 
97126c3e98eSPetr Mladek 	if (WARN_ON(patch->enabled))
97226c3e98eSPetr Mladek 		return -EINVAL;
97326c3e98eSPetr Mladek 
97426c3e98eSPetr Mladek 	pr_notice("enabling patch '%s'\n", patch->mod->name);
97526c3e98eSPetr Mladek 
97626c3e98eSPetr Mladek 	klp_init_transition(patch, KLP_PATCHED);
97726c3e98eSPetr Mladek 
97826c3e98eSPetr Mladek 	/*
97926c3e98eSPetr Mladek 	 * Enforce the order of the func->transition writes in
98026c3e98eSPetr Mladek 	 * klp_init_transition() and the ops->func_stack writes in
98126c3e98eSPetr Mladek 	 * klp_patch_object(), so that klp_ftrace_handler() will see the
98226c3e98eSPetr Mladek 	 * func->transition updates before the handler is registered and the
98326c3e98eSPetr Mladek 	 * new funcs become visible to the handler.
98426c3e98eSPetr Mladek 	 */
98526c3e98eSPetr Mladek 	smp_wmb();
98626c3e98eSPetr Mladek 
98726c3e98eSPetr Mladek 	klp_for_each_object(patch, obj) {
98826c3e98eSPetr Mladek 		if (!klp_is_object_loaded(obj))
98926c3e98eSPetr Mladek 			continue;
99026c3e98eSPetr Mladek 
99126c3e98eSPetr Mladek 		ret = klp_pre_patch_callback(obj);
99226c3e98eSPetr Mladek 		if (ret) {
99326c3e98eSPetr Mladek 			pr_warn("pre-patch callback failed for object '%s'\n",
99426c3e98eSPetr Mladek 				klp_is_module(obj) ? obj->name : "vmlinux");
99526c3e98eSPetr Mladek 			goto err;
99626c3e98eSPetr Mladek 		}
99726c3e98eSPetr Mladek 
99826c3e98eSPetr Mladek 		ret = klp_patch_object(obj);
99926c3e98eSPetr Mladek 		if (ret) {
100026c3e98eSPetr Mladek 			pr_warn("failed to patch object '%s'\n",
100126c3e98eSPetr Mladek 				klp_is_module(obj) ? obj->name : "vmlinux");
100226c3e98eSPetr Mladek 			goto err;
100326c3e98eSPetr Mladek 		}
100426c3e98eSPetr Mladek 	}
100526c3e98eSPetr Mladek 
100626c3e98eSPetr Mladek 	klp_start_transition();
100726c3e98eSPetr Mladek 	patch->enabled = true;
1008958ef1e3SPetr Mladek 	klp_try_complete_transition();
100926c3e98eSPetr Mladek 
101026c3e98eSPetr Mladek 	return 0;
101126c3e98eSPetr Mladek err:
101226c3e98eSPetr Mladek 	pr_warn("failed to enable patch '%s'\n", patch->mod->name);
101326c3e98eSPetr Mladek 
101426c3e98eSPetr Mladek 	klp_cancel_transition();
101526c3e98eSPetr Mladek 	return ret;
101626c3e98eSPetr Mladek }
101726c3e98eSPetr Mladek 
101826c3e98eSPetr Mladek /**
1019958ef1e3SPetr Mladek  * klp_enable_patch() - enable the livepatch
1020958ef1e3SPetr Mladek  * @patch:	patch to be enabled
102126c3e98eSPetr Mladek  *
1022958ef1e3SPetr Mladek  * Initializes the data structure associated with the patch, creates the sysfs
1023958ef1e3SPetr Mladek  * interface, performs the needed symbol lookups and code relocations,
1024958ef1e3SPetr Mladek  * registers the patched functions with ftrace.
1025958ef1e3SPetr Mladek  *
1026958ef1e3SPetr Mladek  * This function is supposed to be called from the livepatch module_init()
1027958ef1e3SPetr Mladek  * callback.
102826c3e98eSPetr Mladek  *
102926c3e98eSPetr Mladek  * Return: 0 on success, otherwise error
103026c3e98eSPetr Mladek  */
103126c3e98eSPetr Mladek int klp_enable_patch(struct klp_patch *patch)
103226c3e98eSPetr Mladek {
103326c3e98eSPetr Mladek 	int ret;
10345ef3dd20SDavid Vernet 	struct klp_object *obj;
103526c3e98eSPetr Mladek 
10365ef3dd20SDavid Vernet 	if (!patch || !patch->mod || !patch->objs)
1037958ef1e3SPetr Mladek 		return -EINVAL;
103826c3e98eSPetr Mladek 
10395ef3dd20SDavid Vernet 	klp_for_each_object_static(patch, obj) {
10405ef3dd20SDavid Vernet 		if (!obj->funcs)
10415ef3dd20SDavid Vernet 			return -EINVAL;
10425ef3dd20SDavid Vernet 	}
10435ef3dd20SDavid Vernet 
10445ef3dd20SDavid Vernet 
1045958ef1e3SPetr Mladek 	if (!is_livepatch_module(patch->mod)) {
1046958ef1e3SPetr Mladek 		pr_err("module %s is not marked as a livepatch module\n",
1047958ef1e3SPetr Mladek 		       patch->mod->name);
1048958ef1e3SPetr Mladek 		return -EINVAL;
104926c3e98eSPetr Mladek 	}
105026c3e98eSPetr Mladek 
1051958ef1e3SPetr Mladek 	if (!klp_initialized())
1052958ef1e3SPetr Mladek 		return -ENODEV;
1053958ef1e3SPetr Mladek 
1054958ef1e3SPetr Mladek 	if (!klp_have_reliable_stack()) {
105531adf230SPetr Mladek 		pr_warn("This architecture doesn't have support for the livepatch consistency model.\n");
105631adf230SPetr Mladek 		pr_warn("The livepatch transition may never complete.\n");
1057958ef1e3SPetr Mladek 	}
1058958ef1e3SPetr Mladek 
1059958ef1e3SPetr Mladek 	mutex_lock(&klp_mutex);
1060958ef1e3SPetr Mladek 
106192c9abf5SPetr Mladek 	if (!klp_is_patch_compatible(patch)) {
106292c9abf5SPetr Mladek 		pr_err("Livepatch patch (%s) is not compatible with the already installed livepatches.\n",
106392c9abf5SPetr Mladek 			patch->mod->name);
106492c9abf5SPetr Mladek 		mutex_unlock(&klp_mutex);
106592c9abf5SPetr Mladek 		return -EINVAL;
106692c9abf5SPetr Mladek 	}
106792c9abf5SPetr Mladek 
106850a0f3f5SYang Yingliang 	if (!try_module_get(patch->mod)) {
106950a0f3f5SYang Yingliang 		mutex_unlock(&klp_mutex);
10705ef3dd20SDavid Vernet 		return -ENODEV;
107150a0f3f5SYang Yingliang 	}
10725ef3dd20SDavid Vernet 
10735ef3dd20SDavid Vernet 	klp_init_patch_early(patch);
1074958ef1e3SPetr Mladek 
1075958ef1e3SPetr Mladek 	ret = klp_init_patch(patch);
1076958ef1e3SPetr Mladek 	if (ret)
1077958ef1e3SPetr Mladek 		goto err;
1078958ef1e3SPetr Mladek 
107926c3e98eSPetr Mladek 	ret = __klp_enable_patch(patch);
1080958ef1e3SPetr Mladek 	if (ret)
1081958ef1e3SPetr Mladek 		goto err;
1082958ef1e3SPetr Mladek 
1083958ef1e3SPetr Mladek 	mutex_unlock(&klp_mutex);
1084958ef1e3SPetr Mladek 
1085958ef1e3SPetr Mladek 	return 0;
108626c3e98eSPetr Mladek 
108726c3e98eSPetr Mladek err:
1088958ef1e3SPetr Mladek 	klp_free_patch_start(patch);
1089958ef1e3SPetr Mladek 
109026c3e98eSPetr Mladek 	mutex_unlock(&klp_mutex);
1091958ef1e3SPetr Mladek 
1092958ef1e3SPetr Mladek 	klp_free_patch_finish(patch);
1093958ef1e3SPetr Mladek 
109426c3e98eSPetr Mladek 	return ret;
109526c3e98eSPetr Mladek }
109626c3e98eSPetr Mladek EXPORT_SYMBOL_GPL(klp_enable_patch);
109726c3e98eSPetr Mladek 
1098ef8daf8eSJoe Lawrence /*
10997e35e4ebSPetr Mladek  * This function unpatches objects from the replaced livepatches.
1100e1452b60SJason Baron  *
1101e1452b60SJason Baron  * We could be pretty aggressive here. It is called in the situation where
11027e35e4ebSPetr Mladek  * these structures are no longer accessed from the ftrace handler.
11037e35e4ebSPetr Mladek  * All functions are redirected by the klp_transition_patch. They
11047e35e4ebSPetr Mladek  * use either a new code or they are in the original code because
11057e35e4ebSPetr Mladek  * of the special nop function patches.
1106e1452b60SJason Baron  *
1107e1452b60SJason Baron  * The only exception is when the transition was forced. In this case,
1108e1452b60SJason Baron  * klp_ftrace_handler() might still see the replaced patch on the stack.
1109e1452b60SJason Baron  * Fortunately, it is carefully designed to work with removed functions
1110e1452b60SJason Baron  * thanks to RCU. We only have to keep the patches on the system. Also
1111e1452b60SJason Baron  * this is handled transparently by patch->module_put.
1112e1452b60SJason Baron  */
11137e35e4ebSPetr Mladek void klp_unpatch_replaced_patches(struct klp_patch *new_patch)
1114e1452b60SJason Baron {
11157e35e4ebSPetr Mladek 	struct klp_patch *old_patch;
1116e1452b60SJason Baron 
11177e35e4ebSPetr Mladek 	klp_for_each_patch(old_patch) {
1118e1452b60SJason Baron 		if (old_patch == new_patch)
1119e1452b60SJason Baron 			return;
1120e1452b60SJason Baron 
1121e1452b60SJason Baron 		old_patch->enabled = false;
1122e1452b60SJason Baron 		klp_unpatch_objects(old_patch);
1123e1452b60SJason Baron 	}
1124e1452b60SJason Baron }
1125e1452b60SJason Baron 
1126e1452b60SJason Baron /*
1127d697bad5SPetr Mladek  * This function removes the dynamically allocated 'nop' functions.
1128d697bad5SPetr Mladek  *
1129d697bad5SPetr Mladek  * We could be pretty aggressive. NOPs do not change the existing
1130d697bad5SPetr Mladek  * behavior except for adding unnecessary delay by the ftrace handler.
1131d697bad5SPetr Mladek  *
1132d697bad5SPetr Mladek  * It is safe even when the transition was forced. The ftrace handler
1133d697bad5SPetr Mladek  * will see a valid ops->func_stack entry thanks to RCU.
1134d697bad5SPetr Mladek  *
1135d697bad5SPetr Mladek  * We could even free the NOPs structures. They must be the last entry
1136d697bad5SPetr Mladek  * in ops->func_stack. Therefore unregister_ftrace_function() is called.
1137d697bad5SPetr Mladek  * It does the same as klp_synchronize_transition() to make sure that
1138d697bad5SPetr Mladek  * nobody is inside the ftrace handler once the operation finishes.
1139d697bad5SPetr Mladek  *
1140d697bad5SPetr Mladek  * IMPORTANT: It must be called right after removing the replaced patches!
1141d697bad5SPetr Mladek  */
1142d697bad5SPetr Mladek void klp_discard_nops(struct klp_patch *new_patch)
1143d697bad5SPetr Mladek {
1144d697bad5SPetr Mladek 	klp_unpatch_objects_dynamic(klp_transition_patch);
1145d697bad5SPetr Mladek 	klp_free_objects_dynamic(klp_transition_patch);
1146d697bad5SPetr Mladek }
1147d697bad5SPetr Mladek 
1148d697bad5SPetr Mladek /*
1149ef8daf8eSJoe Lawrence  * Remove parts of patches that touch a given kernel module. The list of
1150ef8daf8eSJoe Lawrence  * patches processed might be limited. When limit is NULL, all patches
1151ef8daf8eSJoe Lawrence  * will be handled.
1152ef8daf8eSJoe Lawrence  */
1153ef8daf8eSJoe Lawrence static void klp_cleanup_module_patches_limited(struct module *mod,
1154ef8daf8eSJoe Lawrence 					       struct klp_patch *limit)
1155ef8daf8eSJoe Lawrence {
1156ef8daf8eSJoe Lawrence 	struct klp_patch *patch;
1157ef8daf8eSJoe Lawrence 	struct klp_object *obj;
1158ef8daf8eSJoe Lawrence 
1159ecba29f4SPetr Mladek 	klp_for_each_patch(patch) {
1160ef8daf8eSJoe Lawrence 		if (patch == limit)
1161ef8daf8eSJoe Lawrence 			break;
1162ef8daf8eSJoe Lawrence 
1163ef8daf8eSJoe Lawrence 		klp_for_each_object(patch, obj) {
1164ef8daf8eSJoe Lawrence 			if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1165ef8daf8eSJoe Lawrence 				continue;
1166ef8daf8eSJoe Lawrence 
1167fc41efc1SJiri Kosina 			if (patch != klp_transition_patch)
1168fc41efc1SJiri Kosina 				klp_pre_unpatch_callback(obj);
1169fc41efc1SJiri Kosina 
1170ef8daf8eSJoe Lawrence 			pr_notice("reverting patch '%s' on unloading module '%s'\n",
1171ef8daf8eSJoe Lawrence 				  patch->mod->name, obj->mod->name);
1172ef8daf8eSJoe Lawrence 			klp_unpatch_object(obj);
1173fc41efc1SJiri Kosina 
1174fc41efc1SJiri Kosina 			klp_post_unpatch_callback(obj);
1175ef8daf8eSJoe Lawrence 
1176ef8daf8eSJoe Lawrence 			klp_free_object_loaded(obj);
1177ef8daf8eSJoe Lawrence 			break;
1178ef8daf8eSJoe Lawrence 		}
1179ef8daf8eSJoe Lawrence 	}
1180ef8daf8eSJoe Lawrence }
1181ef8daf8eSJoe Lawrence 
11827e545d6eSJessica Yu int klp_module_coming(struct module *mod)
1183b700e7f0SSeth Jennings {
118436e505c1SMinfei Huang 	int ret;
1185b700e7f0SSeth Jennings 	struct klp_patch *patch;
1186b700e7f0SSeth Jennings 	struct klp_object *obj;
1187b700e7f0SSeth Jennings 
11887e545d6eSJessica Yu 	if (WARN_ON(mod->state != MODULE_STATE_COMING))
11897e545d6eSJessica Yu 		return -EINVAL;
1190b700e7f0SSeth Jennings 
1191dcf550e5SJosh Poimboeuf 	if (!strcmp(mod->name, "vmlinux")) {
1192dcf550e5SJosh Poimboeuf 		pr_err("vmlinux.ko: invalid module name");
1193dcf550e5SJosh Poimboeuf 		return -EINVAL;
1194dcf550e5SJosh Poimboeuf 	}
1195dcf550e5SJosh Poimboeuf 
1196b700e7f0SSeth Jennings 	mutex_lock(&klp_mutex);
11978cb2c2dcSPetr Mladek 	/*
11987e545d6eSJessica Yu 	 * Each module has to know that klp_module_coming()
11997e545d6eSJessica Yu 	 * has been called. We never know what module will
12007e545d6eSJessica Yu 	 * get patched by a new patch.
12018cb2c2dcSPetr Mladek 	 */
12028cb2c2dcSPetr Mladek 	mod->klp_alive = true;
12038cb2c2dcSPetr Mladek 
1204ecba29f4SPetr Mladek 	klp_for_each_patch(patch) {
12058cdd043aSJiri Slaby 		klp_for_each_object(patch, obj) {
1206b700e7f0SSeth Jennings 			if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1207b700e7f0SSeth Jennings 				continue;
1208b700e7f0SSeth Jennings 
1209b700e7f0SSeth Jennings 			obj->mod = mod;
12107e545d6eSJessica Yu 
12117e545d6eSJessica Yu 			ret = klp_init_object_loaded(patch, obj);
121236e505c1SMinfei Huang 			if (ret) {
12137e545d6eSJessica Yu 				pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
12147e545d6eSJessica Yu 					patch->mod->name, obj->mod->name, ret);
12157e545d6eSJessica Yu 				goto err;
121636e505c1SMinfei Huang 			}
12177e545d6eSJessica Yu 
12187e545d6eSJessica Yu 			pr_notice("applying patch '%s' to loading module '%s'\n",
12197e545d6eSJessica Yu 				  patch->mod->name, obj->mod->name);
12207e545d6eSJessica Yu 
122193862e38SJoe Lawrence 			ret = klp_pre_patch_callback(obj);
122293862e38SJoe Lawrence 			if (ret) {
122393862e38SJoe Lawrence 				pr_warn("pre-patch callback failed for object '%s'\n",
122493862e38SJoe Lawrence 					obj->name);
122593862e38SJoe Lawrence 				goto err;
122693862e38SJoe Lawrence 			}
122793862e38SJoe Lawrence 
12280dade9f3SJosh Poimboeuf 			ret = klp_patch_object(obj);
12297e545d6eSJessica Yu 			if (ret) {
12307e545d6eSJessica Yu 				pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
12317e545d6eSJessica Yu 					patch->mod->name, obj->mod->name, ret);
123293862e38SJoe Lawrence 
123393862e38SJoe Lawrence 				klp_post_unpatch_callback(obj);
12347e545d6eSJessica Yu 				goto err;
12357e545d6eSJessica Yu 			}
1236b700e7f0SSeth Jennings 
123793862e38SJoe Lawrence 			if (patch != klp_transition_patch)
123893862e38SJoe Lawrence 				klp_post_patch_callback(obj);
123993862e38SJoe Lawrence 
1240b700e7f0SSeth Jennings 			break;
1241b700e7f0SSeth Jennings 		}
1242b700e7f0SSeth Jennings 	}
1243b700e7f0SSeth Jennings 
1244b700e7f0SSeth Jennings 	mutex_unlock(&klp_mutex);
1245b700e7f0SSeth Jennings 
1246b700e7f0SSeth Jennings 	return 0;
12477e545d6eSJessica Yu 
12487e545d6eSJessica Yu err:
12497e545d6eSJessica Yu 	/*
12507e545d6eSJessica Yu 	 * If a patch is unsuccessfully applied, return
12517e545d6eSJessica Yu 	 * error to the module loader.
12527e545d6eSJessica Yu 	 */
12537e545d6eSJessica Yu 	pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
12547e545d6eSJessica Yu 		patch->mod->name, obj->mod->name, obj->mod->name);
12557e545d6eSJessica Yu 	mod->klp_alive = false;
12564ff96fb5SMiroslav Benes 	obj->mod = NULL;
1257ef8daf8eSJoe Lawrence 	klp_cleanup_module_patches_limited(mod, patch);
12587e545d6eSJessica Yu 	mutex_unlock(&klp_mutex);
12597e545d6eSJessica Yu 
12607e545d6eSJessica Yu 	return ret;
1261b700e7f0SSeth Jennings }
1262b700e7f0SSeth Jennings 
12637e545d6eSJessica Yu void klp_module_going(struct module *mod)
12647e545d6eSJessica Yu {
12657e545d6eSJessica Yu 	if (WARN_ON(mod->state != MODULE_STATE_GOING &&
12667e545d6eSJessica Yu 		    mod->state != MODULE_STATE_COMING))
12677e545d6eSJessica Yu 		return;
12687e545d6eSJessica Yu 
12697e545d6eSJessica Yu 	mutex_lock(&klp_mutex);
12707e545d6eSJessica Yu 	/*
12717e545d6eSJessica Yu 	 * Each module has to know that klp_module_going()
12727e545d6eSJessica Yu 	 * has been called. We never know what module will
12737e545d6eSJessica Yu 	 * get patched by a new patch.
12747e545d6eSJessica Yu 	 */
12757e545d6eSJessica Yu 	mod->klp_alive = false;
12767e545d6eSJessica Yu 
1277ef8daf8eSJoe Lawrence 	klp_cleanup_module_patches_limited(mod, NULL);
12787e545d6eSJessica Yu 
12797e545d6eSJessica Yu 	mutex_unlock(&klp_mutex);
12807e545d6eSJessica Yu }
1281b700e7f0SSeth Jennings 
128226029d88SMinfei Huang static int __init klp_init(void)
1283b700e7f0SSeth Jennings {
1284b700e7f0SSeth Jennings 	klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
12857e545d6eSJessica Yu 	if (!klp_root_kobj)
12867e545d6eSJessica Yu 		return -ENOMEM;
1287b700e7f0SSeth Jennings 
1288b700e7f0SSeth Jennings 	return 0;
1289b700e7f0SSeth Jennings }
1290b700e7f0SSeth Jennings 
1291b700e7f0SSeth Jennings module_init(klp_init);
1292