xref: /linux-6.15/kernel/livepatch/core.c (revision 7c8e2bdd)
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>
22b56b36eeSJosh Poimboeuf #include <asm/cacheflush.h>
2310517429SJiri Kosina #include "core.h"
24c349cdcaSJosh Poimboeuf #include "patch.h"
2592c9abf5SPetr Mladek #include "state.h"
26d83a7cb3SJosh Poimboeuf #include "transition.h"
27b700e7f0SSeth Jennings 
283c33f5b9SJosh Poimboeuf /*
29d83a7cb3SJosh Poimboeuf  * klp_mutex is a coarse lock which serializes access to klp data.  All
30d83a7cb3SJosh Poimboeuf  * accesses to klp-related variables and structures must have mutex protection,
31d83a7cb3SJosh Poimboeuf  * except within the following functions which carefully avoid the need for it:
32d83a7cb3SJosh Poimboeuf  *
33d83a7cb3SJosh Poimboeuf  * - klp_ftrace_handler()
34d83a7cb3SJosh Poimboeuf  * - klp_update_patch_state()
353c33f5b9SJosh Poimboeuf  */
36d83a7cb3SJosh Poimboeuf DEFINE_MUTEX(klp_mutex);
373c33f5b9SJosh Poimboeuf 
38958ef1e3SPetr Mladek /*
39958ef1e3SPetr Mladek  * Actively used patches: enabled or in transition. Note that replaced
40958ef1e3SPetr Mladek  * or disabled patches are not listed even though the related kernel
41958ef1e3SPetr Mladek  * module still can be loaded.
42958ef1e3SPetr Mladek  */
4368007289SPetr Mladek LIST_HEAD(klp_patches);
44b700e7f0SSeth Jennings 
45b700e7f0SSeth Jennings static struct kobject *klp_root_kobj;
46b700e7f0SSeth Jennings 
47b700e7f0SSeth Jennings static bool klp_is_module(struct klp_object *obj)
48b700e7f0SSeth Jennings {
49b700e7f0SSeth Jennings 	return obj->name;
50b700e7f0SSeth Jennings }
51b700e7f0SSeth Jennings 
52b700e7f0SSeth Jennings /* sets obj->mod if object is not vmlinux and module is found */
53b700e7f0SSeth Jennings static void klp_find_object_module(struct klp_object *obj)
54b700e7f0SSeth Jennings {
558cb2c2dcSPetr Mladek 	struct module *mod;
568cb2c2dcSPetr Mladek 
57b700e7f0SSeth Jennings 	if (!klp_is_module(obj))
58b700e7f0SSeth Jennings 		return;
59b700e7f0SSeth Jennings 
60b700e7f0SSeth Jennings 	mutex_lock(&module_mutex);
61b700e7f0SSeth Jennings 	/*
628cb2c2dcSPetr Mladek 	 * We do not want to block removal of patched modules and therefore
638cb2c2dcSPetr Mladek 	 * we do not take a reference here. The patches are removed by
647e545d6eSJessica Yu 	 * klp_module_going() instead.
65b700e7f0SSeth Jennings 	 */
668cb2c2dcSPetr Mladek 	mod = find_module(obj->name);
678cb2c2dcSPetr Mladek 	/*
687e545d6eSJessica Yu 	 * Do not mess work of klp_module_coming() and klp_module_going().
697e545d6eSJessica Yu 	 * Note that the patch might still be needed before klp_module_going()
708cb2c2dcSPetr Mladek 	 * is called. Module functions can be called even in the GOING state
718cb2c2dcSPetr Mladek 	 * until mod->exit() finishes. This is especially important for
728cb2c2dcSPetr Mladek 	 * patches that modify semantic of the functions.
738cb2c2dcSPetr Mladek 	 */
748cb2c2dcSPetr Mladek 	if (mod && mod->klp_alive)
758cb2c2dcSPetr Mladek 		obj->mod = mod;
768cb2c2dcSPetr Mladek 
77b700e7f0SSeth Jennings 	mutex_unlock(&module_mutex);
78b700e7f0SSeth Jennings }
79b700e7f0SSeth Jennings 
80b700e7f0SSeth Jennings static bool klp_initialized(void)
81b700e7f0SSeth Jennings {
82e76ff06aSNicholas Mc Guire 	return !!klp_root_kobj;
83b700e7f0SSeth Jennings }
84b700e7f0SSeth Jennings 
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 
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 *objname;
121b700e7f0SSeth Jennings 	const char *name;
122b700e7f0SSeth Jennings 	unsigned long addr;
123b700e7f0SSeth Jennings 	unsigned long count;
124b2b018efSChris J Arges 	unsigned long pos;
125b700e7f0SSeth Jennings };
126b700e7f0SSeth Jennings 
127b700e7f0SSeth Jennings static int klp_find_callback(void *data, const char *name,
128b700e7f0SSeth Jennings 			     struct module *mod, unsigned long addr)
129b700e7f0SSeth Jennings {
130b700e7f0SSeth Jennings 	struct klp_find_arg *args = data;
131b700e7f0SSeth Jennings 
132b700e7f0SSeth Jennings 	if ((mod && !args->objname) || (!mod && args->objname))
133b700e7f0SSeth Jennings 		return 0;
134b700e7f0SSeth Jennings 
135b700e7f0SSeth Jennings 	if (strcmp(args->name, name))
136b700e7f0SSeth Jennings 		return 0;
137b700e7f0SSeth Jennings 
138b700e7f0SSeth Jennings 	if (args->objname && strcmp(args->objname, mod->name))
139b700e7f0SSeth Jennings 		return 0;
140b700e7f0SSeth Jennings 
141b700e7f0SSeth Jennings 	args->addr = addr;
142b700e7f0SSeth Jennings 	args->count++;
143b700e7f0SSeth Jennings 
144b2b018efSChris J Arges 	/*
145b2b018efSChris J Arges 	 * Finish the search when the symbol is found for the desired position
146b2b018efSChris J Arges 	 * or the position is not defined for a non-unique symbol.
147b2b018efSChris J Arges 	 */
148b2b018efSChris J Arges 	if ((args->pos && (args->count == args->pos)) ||
149b2b018efSChris J Arges 	    (!args->pos && (args->count > 1)))
150b2b018efSChris J Arges 		return 1;
151b2b018efSChris J Arges 
152b700e7f0SSeth Jennings 	return 0;
153b700e7f0SSeth Jennings }
154b700e7f0SSeth Jennings 
155b700e7f0SSeth Jennings static int klp_find_object_symbol(const char *objname, const char *name,
156b2b018efSChris J Arges 				  unsigned long sympos, unsigned long *addr)
157b700e7f0SSeth Jennings {
158b700e7f0SSeth Jennings 	struct klp_find_arg args = {
159b700e7f0SSeth Jennings 		.objname = objname,
160b700e7f0SSeth Jennings 		.name = name,
161b700e7f0SSeth Jennings 		.addr = 0,
162b2b018efSChris J Arges 		.count = 0,
163b2b018efSChris J Arges 		.pos = sympos,
164b700e7f0SSeth Jennings 	};
165b700e7f0SSeth Jennings 
1669a1bd63cSMiroslav Benes 	mutex_lock(&module_mutex);
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);
1719a1bd63cSMiroslav Benes 	mutex_unlock(&module_mutex);
172b700e7f0SSeth Jennings 
173b2b018efSChris J Arges 	/*
174b2b018efSChris J Arges 	 * Ensure an address was found. If sympos is 0, ensure symbol is unique;
175b2b018efSChris J Arges 	 * otherwise ensure the symbol position count matches sympos.
176b2b018efSChris J Arges 	 */
177b2b018efSChris J Arges 	if (args.addr == 0)
178b700e7f0SSeth Jennings 		pr_err("symbol '%s' not found in symbol table\n", name);
179b2b018efSChris J Arges 	else if (args.count > 1 && sympos == 0) {
180f995b5f7SPetr Mladek 		pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n",
181f995b5f7SPetr Mladek 		       name, objname);
182b2b018efSChris J Arges 	} else if (sympos != args.count && sympos > 0) {
183b2b018efSChris J Arges 		pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
184b2b018efSChris J Arges 		       sympos, name, objname ? objname : "vmlinux");
185b2b018efSChris J Arges 	} else {
186b700e7f0SSeth Jennings 		*addr = args.addr;
187b700e7f0SSeth Jennings 		return 0;
188b700e7f0SSeth Jennings 	}
189b700e7f0SSeth Jennings 
190b700e7f0SSeth Jennings 	*addr = 0;
191b700e7f0SSeth Jennings 	return -EINVAL;
192b700e7f0SSeth Jennings }
193b700e7f0SSeth Jennings 
194*7c8e2bddSJosh Poimboeuf static int klp_resolve_symbols(Elf64_Shdr *sechdrs, const char *strtab,
195*7c8e2bddSJosh Poimboeuf 			       unsigned int symndx, Elf_Shdr *relasec)
196b700e7f0SSeth Jennings {
197425595a7SJessica Yu 	int i, cnt, vmlinux, ret;
198425595a7SJessica Yu 	char objname[MODULE_NAME_LEN];
199425595a7SJessica Yu 	char symname[KSYM_NAME_LEN];
200425595a7SJessica Yu 	Elf_Rela *relas;
201425595a7SJessica Yu 	Elf_Sym *sym;
202425595a7SJessica Yu 	unsigned long sympos, addr;
203b700e7f0SSeth Jennings 
204b2b018efSChris J Arges 	/*
205425595a7SJessica Yu 	 * Since the field widths for objname and symname in the sscanf()
206425595a7SJessica Yu 	 * call are hard-coded and correspond to MODULE_NAME_LEN and
207425595a7SJessica Yu 	 * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
208425595a7SJessica Yu 	 * and KSYM_NAME_LEN have the values we expect them to have.
209425595a7SJessica Yu 	 *
210425595a7SJessica Yu 	 * Because the value of MODULE_NAME_LEN can differ among architectures,
211425595a7SJessica Yu 	 * we use the smallest/strictest upper bound possible (56, based on
212425595a7SJessica Yu 	 * the current definition of MODULE_NAME_LEN) to prevent overflows.
213b2b018efSChris J Arges 	 */
214425595a7SJessica Yu 	BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128);
215425595a7SJessica Yu 
216425595a7SJessica Yu 	relas = (Elf_Rela *) relasec->sh_addr;
217425595a7SJessica Yu 	/* For each rela in this klp relocation section */
218425595a7SJessica Yu 	for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
219*7c8e2bddSJosh Poimboeuf 		sym = (Elf64_Sym *)sechdrs[symndx].sh_addr + ELF_R_SYM(relas[i].r_info);
220425595a7SJessica Yu 		if (sym->st_shndx != SHN_LIVEPATCH) {
22177f8f39aSJosh Poimboeuf 			pr_err("symbol %s is not marked as a livepatch symbol\n",
222425595a7SJessica Yu 			       strtab + sym->st_name);
223425595a7SJessica Yu 			return -EINVAL;
224425595a7SJessica Yu 		}
225425595a7SJessica Yu 
226425595a7SJessica Yu 		/* Format: .klp.sym.objname.symname,sympos */
227425595a7SJessica Yu 		cnt = sscanf(strtab + sym->st_name,
228425595a7SJessica Yu 			     ".klp.sym.%55[^.].%127[^,],%lu",
229425595a7SJessica Yu 			     objname, symname, &sympos);
230425595a7SJessica Yu 		if (cnt != 3) {
23177f8f39aSJosh Poimboeuf 			pr_err("symbol %s has an incorrectly formatted name\n",
232425595a7SJessica Yu 			       strtab + sym->st_name);
233425595a7SJessica Yu 			return -EINVAL;
234425595a7SJessica Yu 		}
235425595a7SJessica Yu 
236425595a7SJessica Yu 		/* klp_find_object_symbol() treats a NULL objname as vmlinux */
237425595a7SJessica Yu 		vmlinux = !strcmp(objname, "vmlinux");
238425595a7SJessica Yu 		ret = klp_find_object_symbol(vmlinux ? NULL : objname,
239425595a7SJessica Yu 					     symname, sympos, &addr);
240425595a7SJessica Yu 		if (ret)
241425595a7SJessica Yu 			return ret;
242425595a7SJessica Yu 
243425595a7SJessica Yu 		sym->st_value = addr;
244425595a7SJessica Yu 	}
245425595a7SJessica Yu 
246425595a7SJessica Yu 	return 0;
247b700e7f0SSeth Jennings }
248b700e7f0SSeth Jennings 
249*7c8e2bddSJosh Poimboeuf /*
250*7c8e2bddSJosh Poimboeuf  * At a high-level, there are two types of klp relocation sections: those which
251*7c8e2bddSJosh Poimboeuf  * reference symbols which live in vmlinux; and those which reference symbols
252*7c8e2bddSJosh Poimboeuf  * which live in other modules.  This function is called for both types:
253*7c8e2bddSJosh Poimboeuf  *
254*7c8e2bddSJosh Poimboeuf  * 1) When a klp module itself loads, the module code calls this function to
255*7c8e2bddSJosh Poimboeuf  *    write vmlinux-specific klp relocations (.klp.rela.vmlinux.* sections).
256*7c8e2bddSJosh Poimboeuf  *    These relocations are written to the klp module text to allow the patched
257*7c8e2bddSJosh Poimboeuf  *    code/data to reference unexported vmlinux symbols.  They're written as
258*7c8e2bddSJosh Poimboeuf  *    early as possible to ensure that other module init code (.e.g.,
259*7c8e2bddSJosh Poimboeuf  *    jump_label_apply_nops) can access any unexported vmlinux symbols which
260*7c8e2bddSJosh Poimboeuf  *    might be referenced by the klp module's special sections.
261*7c8e2bddSJosh Poimboeuf  *
262*7c8e2bddSJosh Poimboeuf  * 2) When a to-be-patched module loads -- or is already loaded when a
263*7c8e2bddSJosh Poimboeuf  *    corresponding klp module loads -- klp code calls this function to write
264*7c8e2bddSJosh Poimboeuf  *    module-specific klp relocations (.klp.rela.{module}.* sections).  These
265*7c8e2bddSJosh Poimboeuf  *    are written to the klp module text to allow the patched code/data to
266*7c8e2bddSJosh Poimboeuf  *    reference symbols which live in the to-be-patched module or one of its
267*7c8e2bddSJosh Poimboeuf  *    module dependencies.  Exported symbols are supported, in addition to
268*7c8e2bddSJosh Poimboeuf  *    unexported symbols, in order to enable late module patching, which allows
269*7c8e2bddSJosh Poimboeuf  *    the to-be-patched module to be loaded and patched sometime *after* the
270*7c8e2bddSJosh Poimboeuf  *    klp module is loaded.
271*7c8e2bddSJosh Poimboeuf  */
272*7c8e2bddSJosh Poimboeuf int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
273*7c8e2bddSJosh Poimboeuf 			     const char *shstrtab, const char *strtab,
274*7c8e2bddSJosh Poimboeuf 			     unsigned int symndx, unsigned int secndx,
275*7c8e2bddSJosh Poimboeuf 			     const char *objname)
276b700e7f0SSeth Jennings {
277*7c8e2bddSJosh Poimboeuf 	int cnt, ret;
278425595a7SJessica Yu 	char sec_objname[MODULE_NAME_LEN];
279*7c8e2bddSJosh Poimboeuf 	Elf_Shdr *sec = sechdrs + secndx;
280b56b36eeSJosh Poimboeuf 
281425595a7SJessica Yu 	/*
282425595a7SJessica Yu 	 * Format: .klp.rela.sec_objname.section_name
283425595a7SJessica Yu 	 * See comment in klp_resolve_symbols() for an explanation
284425595a7SJessica Yu 	 * of the selected field width value.
285425595a7SJessica Yu 	 */
286*7c8e2bddSJosh Poimboeuf 	cnt = sscanf(shstrtab + sec->sh_name, ".klp.rela.%55[^.]",
287*7c8e2bddSJosh Poimboeuf 		     sec_objname);
288425595a7SJessica Yu 	if (cnt != 1) {
28977f8f39aSJosh Poimboeuf 		pr_err("section %s has an incorrectly formatted name\n",
290*7c8e2bddSJosh Poimboeuf 		       shstrtab + sec->sh_name);
291*7c8e2bddSJosh Poimboeuf 		return -EINVAL;
292b700e7f0SSeth Jennings 	}
293425595a7SJessica Yu 
294*7c8e2bddSJosh Poimboeuf 	if (strcmp(objname ? objname : "vmlinux", sec_objname))
295*7c8e2bddSJosh Poimboeuf 		return 0;
296425595a7SJessica Yu 
297*7c8e2bddSJosh Poimboeuf 	ret = klp_resolve_symbols(sechdrs, strtab, symndx, sec);
298064c89dfSChris J Arges 	if (ret)
299b56b36eeSJosh Poimboeuf 		return ret;
300*7c8e2bddSJosh Poimboeuf 
301*7c8e2bddSJosh Poimboeuf 	return apply_relocate_add(sechdrs, strtab, symndx, secndx, pmod);
302b700e7f0SSeth Jennings }
303b700e7f0SSeth Jennings 
304b700e7f0SSeth Jennings /*
305b700e7f0SSeth Jennings  * Sysfs Interface
306b700e7f0SSeth Jennings  *
307b700e7f0SSeth Jennings  * /sys/kernel/livepatch
308b700e7f0SSeth Jennings  * /sys/kernel/livepatch/<patch>
309b700e7f0SSeth Jennings  * /sys/kernel/livepatch/<patch>/enabled
310d83a7cb3SJosh Poimboeuf  * /sys/kernel/livepatch/<patch>/transition
311c99a2be7SMiroslav Benes  * /sys/kernel/livepatch/<patch>/force
312b700e7f0SSeth Jennings  * /sys/kernel/livepatch/<patch>/<object>
313444f9e99SChris J Arges  * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
314b700e7f0SSeth Jennings  */
31526c3e98eSPetr Mladek static int __klp_disable_patch(struct klp_patch *patch);
316b700e7f0SSeth Jennings 
317b700e7f0SSeth Jennings static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
318b700e7f0SSeth Jennings 			     const char *buf, size_t count)
319b700e7f0SSeth Jennings {
320b700e7f0SSeth Jennings 	struct klp_patch *patch;
321b700e7f0SSeth Jennings 	int ret;
32268ae4b2bSJosh Poimboeuf 	bool enabled;
323b700e7f0SSeth Jennings 
32468ae4b2bSJosh Poimboeuf 	ret = kstrtobool(buf, &enabled);
325b700e7f0SSeth Jennings 	if (ret)
32668ae4b2bSJosh Poimboeuf 		return ret;
327b700e7f0SSeth Jennings 
328b700e7f0SSeth Jennings 	patch = container_of(kobj, struct klp_patch, kobj);
329b700e7f0SSeth Jennings 
330b700e7f0SSeth Jennings 	mutex_lock(&klp_mutex);
331b700e7f0SSeth Jennings 
33268ae4b2bSJosh Poimboeuf 	if (patch->enabled == enabled) {
333b700e7f0SSeth Jennings 		/* already in requested state */
334b700e7f0SSeth Jennings 		ret = -EINVAL;
335958ef1e3SPetr Mladek 		goto out;
336b700e7f0SSeth Jennings 	}
337b700e7f0SSeth Jennings 
338958ef1e3SPetr Mladek 	/*
339958ef1e3SPetr Mladek 	 * Allow to reverse a pending transition in both ways. It might be
340958ef1e3SPetr Mladek 	 * necessary to complete the transition without forcing and breaking
341958ef1e3SPetr Mladek 	 * the system integrity.
342958ef1e3SPetr Mladek 	 *
343958ef1e3SPetr Mladek 	 * Do not allow to re-enable a disabled patch.
344958ef1e3SPetr Mladek 	 */
345958ef1e3SPetr Mladek 	if (patch == klp_transition_patch)
346d83a7cb3SJosh Poimboeuf 		klp_reverse_transition();
347958ef1e3SPetr Mladek 	else if (!enabled)
348b700e7f0SSeth Jennings 		ret = __klp_disable_patch(patch);
349958ef1e3SPetr Mladek 	else
350958ef1e3SPetr Mladek 		ret = -EINVAL;
351958ef1e3SPetr Mladek 
352958ef1e3SPetr Mladek out:
353958ef1e3SPetr Mladek 	mutex_unlock(&klp_mutex);
354958ef1e3SPetr Mladek 
355b700e7f0SSeth Jennings 	if (ret)
356b700e7f0SSeth Jennings 		return ret;
357958ef1e3SPetr Mladek 	return count;
358b700e7f0SSeth Jennings }
359b700e7f0SSeth Jennings 
360b700e7f0SSeth Jennings static ssize_t enabled_show(struct kobject *kobj,
361b700e7f0SSeth Jennings 			    struct kobj_attribute *attr, char *buf)
362b700e7f0SSeth Jennings {
363b700e7f0SSeth Jennings 	struct klp_patch *patch;
364b700e7f0SSeth Jennings 
365b700e7f0SSeth Jennings 	patch = container_of(kobj, struct klp_patch, kobj);
3660dade9f3SJosh Poimboeuf 	return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled);
367b700e7f0SSeth Jennings }
368b700e7f0SSeth Jennings 
369d83a7cb3SJosh Poimboeuf static ssize_t transition_show(struct kobject *kobj,
370d83a7cb3SJosh Poimboeuf 			       struct kobj_attribute *attr, char *buf)
371d83a7cb3SJosh Poimboeuf {
372d83a7cb3SJosh Poimboeuf 	struct klp_patch *patch;
373d83a7cb3SJosh Poimboeuf 
374d83a7cb3SJosh Poimboeuf 	patch = container_of(kobj, struct klp_patch, kobj);
375d83a7cb3SJosh Poimboeuf 	return snprintf(buf, PAGE_SIZE-1, "%d\n",
376d83a7cb3SJosh Poimboeuf 			patch == klp_transition_patch);
377b700e7f0SSeth Jennings }
378b700e7f0SSeth Jennings 
379c99a2be7SMiroslav Benes static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
380c99a2be7SMiroslav Benes 			   const char *buf, size_t count)
381c99a2be7SMiroslav Benes {
382c99a2be7SMiroslav Benes 	struct klp_patch *patch;
383c99a2be7SMiroslav Benes 	int ret;
384c99a2be7SMiroslav Benes 	bool val;
385c99a2be7SMiroslav Benes 
386c99a2be7SMiroslav Benes 	ret = kstrtobool(buf, &val);
387c99a2be7SMiroslav Benes 	if (ret)
388c99a2be7SMiroslav Benes 		return ret;
389c99a2be7SMiroslav Benes 
3908869016dSMiroslav Benes 	if (!val)
3918869016dSMiroslav Benes 		return count;
3928869016dSMiroslav Benes 
3938869016dSMiroslav Benes 	mutex_lock(&klp_mutex);
3948869016dSMiroslav Benes 
3958869016dSMiroslav Benes 	patch = container_of(kobj, struct klp_patch, kobj);
3968869016dSMiroslav Benes 	if (patch != klp_transition_patch) {
3978869016dSMiroslav Benes 		mutex_unlock(&klp_mutex);
3988869016dSMiroslav Benes 		return -EINVAL;
3998869016dSMiroslav Benes 	}
4008869016dSMiroslav Benes 
401c99a2be7SMiroslav Benes 	klp_force_transition();
402c99a2be7SMiroslav Benes 
4038869016dSMiroslav Benes 	mutex_unlock(&klp_mutex);
4048869016dSMiroslav Benes 
405c99a2be7SMiroslav Benes 	return count;
406c99a2be7SMiroslav Benes }
407c99a2be7SMiroslav Benes 
408b700e7f0SSeth Jennings static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
409d83a7cb3SJosh Poimboeuf static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition);
410c99a2be7SMiroslav Benes static struct kobj_attribute force_kobj_attr = __ATTR_WO(force);
411b700e7f0SSeth Jennings static struct attribute *klp_patch_attrs[] = {
412b700e7f0SSeth Jennings 	&enabled_kobj_attr.attr,
413d83a7cb3SJosh Poimboeuf 	&transition_kobj_attr.attr,
414c99a2be7SMiroslav Benes 	&force_kobj_attr.attr,
415b700e7f0SSeth Jennings 	NULL
416b700e7f0SSeth Jennings };
41770283454SKimberly Brown ATTRIBUTE_GROUPS(klp_patch);
418b700e7f0SSeth Jennings 
419e1452b60SJason Baron static void klp_free_object_dynamic(struct klp_object *obj)
420e1452b60SJason Baron {
421e1452b60SJason Baron 	kfree(obj->name);
422e1452b60SJason Baron 	kfree(obj);
423e1452b60SJason Baron }
424e1452b60SJason Baron 
425f68d67cfSPetr Mladek static void klp_init_func_early(struct klp_object *obj,
426f68d67cfSPetr Mladek 				struct klp_func *func);
427f68d67cfSPetr Mladek static void klp_init_object_early(struct klp_patch *patch,
428f68d67cfSPetr Mladek 				  struct klp_object *obj);
4294d141ab3SPetr Mladek 
430f68d67cfSPetr Mladek static struct klp_object *klp_alloc_object_dynamic(const char *name,
431f68d67cfSPetr Mladek 						   struct klp_patch *patch)
432e1452b60SJason Baron {
433e1452b60SJason Baron 	struct klp_object *obj;
434e1452b60SJason Baron 
435e1452b60SJason Baron 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
436e1452b60SJason Baron 	if (!obj)
437e1452b60SJason Baron 		return NULL;
438e1452b60SJason Baron 
439e1452b60SJason Baron 	if (name) {
440e1452b60SJason Baron 		obj->name = kstrdup(name, GFP_KERNEL);
441e1452b60SJason Baron 		if (!obj->name) {
442e1452b60SJason Baron 			kfree(obj);
443e1452b60SJason Baron 			return NULL;
444e1452b60SJason Baron 		}
445e1452b60SJason Baron 	}
446e1452b60SJason Baron 
447f68d67cfSPetr Mladek 	klp_init_object_early(patch, obj);
448e1452b60SJason Baron 	obj->dynamic = true;
449e1452b60SJason Baron 
450e1452b60SJason Baron 	return obj;
451e1452b60SJason Baron }
452e1452b60SJason Baron 
453e1452b60SJason Baron static void klp_free_func_nop(struct klp_func *func)
454e1452b60SJason Baron {
455e1452b60SJason Baron 	kfree(func->old_name);
456e1452b60SJason Baron 	kfree(func);
457e1452b60SJason Baron }
458e1452b60SJason Baron 
459e1452b60SJason Baron static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func,
460e1452b60SJason Baron 					   struct klp_object *obj)
461e1452b60SJason Baron {
462e1452b60SJason Baron 	struct klp_func *func;
463e1452b60SJason Baron 
464e1452b60SJason Baron 	func = kzalloc(sizeof(*func), GFP_KERNEL);
465e1452b60SJason Baron 	if (!func)
466e1452b60SJason Baron 		return NULL;
467e1452b60SJason Baron 
468e1452b60SJason Baron 	if (old_func->old_name) {
469e1452b60SJason Baron 		func->old_name = kstrdup(old_func->old_name, GFP_KERNEL);
470e1452b60SJason Baron 		if (!func->old_name) {
471e1452b60SJason Baron 			kfree(func);
472e1452b60SJason Baron 			return NULL;
473e1452b60SJason Baron 		}
474e1452b60SJason Baron 	}
475e1452b60SJason Baron 
476f68d67cfSPetr Mladek 	klp_init_func_early(obj, func);
477e1452b60SJason Baron 	/*
478e1452b60SJason Baron 	 * func->new_func is same as func->old_func. These addresses are
479e1452b60SJason Baron 	 * set when the object is loaded, see klp_init_object_loaded().
480e1452b60SJason Baron 	 */
481e1452b60SJason Baron 	func->old_sympos = old_func->old_sympos;
482e1452b60SJason Baron 	func->nop = true;
483e1452b60SJason Baron 
484e1452b60SJason Baron 	return func;
485e1452b60SJason Baron }
486e1452b60SJason Baron 
487e1452b60SJason Baron static int klp_add_object_nops(struct klp_patch *patch,
488e1452b60SJason Baron 			       struct klp_object *old_obj)
489e1452b60SJason Baron {
490e1452b60SJason Baron 	struct klp_object *obj;
491e1452b60SJason Baron 	struct klp_func *func, *old_func;
492e1452b60SJason Baron 
493e1452b60SJason Baron 	obj = klp_find_object(patch, old_obj);
494e1452b60SJason Baron 
495e1452b60SJason Baron 	if (!obj) {
496f68d67cfSPetr Mladek 		obj = klp_alloc_object_dynamic(old_obj->name, patch);
497e1452b60SJason Baron 		if (!obj)
498e1452b60SJason Baron 			return -ENOMEM;
499e1452b60SJason Baron 	}
500e1452b60SJason Baron 
501e1452b60SJason Baron 	klp_for_each_func(old_obj, old_func) {
502e1452b60SJason Baron 		func = klp_find_func(obj, old_func);
503e1452b60SJason Baron 		if (func)
504e1452b60SJason Baron 			continue;
505e1452b60SJason Baron 
506e1452b60SJason Baron 		func = klp_alloc_func_nop(old_func, obj);
507e1452b60SJason Baron 		if (!func)
508e1452b60SJason Baron 			return -ENOMEM;
509e1452b60SJason Baron 	}
510e1452b60SJason Baron 
511e1452b60SJason Baron 	return 0;
512e1452b60SJason Baron }
513e1452b60SJason Baron 
514e1452b60SJason Baron /*
515e1452b60SJason Baron  * Add 'nop' functions which simply return to the caller to run
516e1452b60SJason Baron  * the original function. The 'nop' functions are added to a
517e1452b60SJason Baron  * patch to facilitate a 'replace' mode.
518e1452b60SJason Baron  */
519e1452b60SJason Baron static int klp_add_nops(struct klp_patch *patch)
520e1452b60SJason Baron {
521e1452b60SJason Baron 	struct klp_patch *old_patch;
522e1452b60SJason Baron 	struct klp_object *old_obj;
523e1452b60SJason Baron 
524ecba29f4SPetr Mladek 	klp_for_each_patch(old_patch) {
525e1452b60SJason Baron 		klp_for_each_object(old_patch, old_obj) {
526e1452b60SJason Baron 			int err;
527e1452b60SJason Baron 
528e1452b60SJason Baron 			err = klp_add_object_nops(patch, old_obj);
529e1452b60SJason Baron 			if (err)
530e1452b60SJason Baron 				return err;
531e1452b60SJason Baron 		}
532e1452b60SJason Baron 	}
533e1452b60SJason Baron 
534e1452b60SJason Baron 	return 0;
535e1452b60SJason Baron }
536e1452b60SJason Baron 
537b700e7f0SSeth Jennings static void klp_kobj_release_patch(struct kobject *kobj)
538b700e7f0SSeth Jennings {
5393ec24776SJosh Poimboeuf 	struct klp_patch *patch;
5403ec24776SJosh Poimboeuf 
5413ec24776SJosh Poimboeuf 	patch = container_of(kobj, struct klp_patch, kobj);
5423ec24776SJosh Poimboeuf 	complete(&patch->finish);
543b700e7f0SSeth Jennings }
544b700e7f0SSeth Jennings 
545b700e7f0SSeth Jennings static struct kobj_type klp_ktype_patch = {
546b700e7f0SSeth Jennings 	.release = klp_kobj_release_patch,
547b700e7f0SSeth Jennings 	.sysfs_ops = &kobj_sysfs_ops,
54870283454SKimberly Brown 	.default_groups = klp_patch_groups,
549b700e7f0SSeth Jennings };
550b700e7f0SSeth Jennings 
551cad706dfSMiroslav Benes static void klp_kobj_release_object(struct kobject *kobj)
552cad706dfSMiroslav Benes {
553e1452b60SJason Baron 	struct klp_object *obj;
554e1452b60SJason Baron 
555e1452b60SJason Baron 	obj = container_of(kobj, struct klp_object, kobj);
556e1452b60SJason Baron 
557e1452b60SJason Baron 	if (obj->dynamic)
558e1452b60SJason Baron 		klp_free_object_dynamic(obj);
559cad706dfSMiroslav Benes }
560cad706dfSMiroslav Benes 
561cad706dfSMiroslav Benes static struct kobj_type klp_ktype_object = {
562cad706dfSMiroslav Benes 	.release = klp_kobj_release_object,
563cad706dfSMiroslav Benes 	.sysfs_ops = &kobj_sysfs_ops,
564cad706dfSMiroslav Benes };
565cad706dfSMiroslav Benes 
566b700e7f0SSeth Jennings static void klp_kobj_release_func(struct kobject *kobj)
567b700e7f0SSeth Jennings {
568e1452b60SJason Baron 	struct klp_func *func;
569e1452b60SJason Baron 
570e1452b60SJason Baron 	func = container_of(kobj, struct klp_func, kobj);
571e1452b60SJason Baron 
572e1452b60SJason Baron 	if (func->nop)
573e1452b60SJason Baron 		klp_free_func_nop(func);
574b700e7f0SSeth Jennings }
575b700e7f0SSeth Jennings 
576b700e7f0SSeth Jennings static struct kobj_type klp_ktype_func = {
577b700e7f0SSeth Jennings 	.release = klp_kobj_release_func,
578b700e7f0SSeth Jennings 	.sysfs_ops = &kobj_sysfs_ops,
579b700e7f0SSeth Jennings };
580b700e7f0SSeth Jennings 
581d697bad5SPetr Mladek static void __klp_free_funcs(struct klp_object *obj, bool nops_only)
582b700e7f0SSeth Jennings {
583e1452b60SJason Baron 	struct klp_func *func, *tmp_func;
584b700e7f0SSeth Jennings 
585e1452b60SJason Baron 	klp_for_each_func_safe(obj, func, tmp_func) {
586d697bad5SPetr Mladek 		if (nops_only && !func->nop)
587d697bad5SPetr Mladek 			continue;
588d697bad5SPetr Mladek 
589d697bad5SPetr Mladek 		list_del(&func->node);
590b700e7f0SSeth Jennings 		kobject_put(&func->kobj);
591b700e7f0SSeth Jennings 	}
5920430f78bSPetr Mladek }
593b700e7f0SSeth Jennings 
594b700e7f0SSeth Jennings /* Clean up when a patched object is unloaded */
595b700e7f0SSeth Jennings static void klp_free_object_loaded(struct klp_object *obj)
596b700e7f0SSeth Jennings {
597b700e7f0SSeth Jennings 	struct klp_func *func;
598b700e7f0SSeth Jennings 
599b700e7f0SSeth Jennings 	obj->mod = NULL;
600b700e7f0SSeth Jennings 
601e1452b60SJason Baron 	klp_for_each_func(obj, func) {
60219514910SPetr Mladek 		func->old_func = NULL;
603e1452b60SJason Baron 
604e1452b60SJason Baron 		if (func->nop)
605e1452b60SJason Baron 			func->new_func = NULL;
606e1452b60SJason Baron 	}
607b700e7f0SSeth Jennings }
608b700e7f0SSeth Jennings 
609d697bad5SPetr Mladek static void __klp_free_objects(struct klp_patch *patch, bool nops_only)
610b700e7f0SSeth Jennings {
611e1452b60SJason Baron 	struct klp_object *obj, *tmp_obj;
612b700e7f0SSeth Jennings 
613e1452b60SJason Baron 	klp_for_each_object_safe(patch, obj, tmp_obj) {
614d697bad5SPetr Mladek 		__klp_free_funcs(obj, nops_only);
615d697bad5SPetr Mladek 
616d697bad5SPetr Mladek 		if (nops_only && !obj->dynamic)
617d697bad5SPetr Mladek 			continue;
618d697bad5SPetr Mladek 
619d697bad5SPetr Mladek 		list_del(&obj->node);
620cad706dfSMiroslav Benes 		kobject_put(&obj->kobj);
621b700e7f0SSeth Jennings 	}
622b700e7f0SSeth Jennings }
623b700e7f0SSeth Jennings 
624d697bad5SPetr Mladek static void klp_free_objects(struct klp_patch *patch)
625d697bad5SPetr Mladek {
626d697bad5SPetr Mladek 	__klp_free_objects(patch, false);
627d697bad5SPetr Mladek }
628d697bad5SPetr Mladek 
629d697bad5SPetr Mladek static void klp_free_objects_dynamic(struct klp_patch *patch)
630d697bad5SPetr Mladek {
631d697bad5SPetr Mladek 	__klp_free_objects(patch, true);
632d697bad5SPetr Mladek }
633d697bad5SPetr Mladek 
6340430f78bSPetr Mladek /*
6350430f78bSPetr Mladek  * This function implements the free operations that can be called safely
6360430f78bSPetr Mladek  * under klp_mutex.
6370430f78bSPetr Mladek  *
6380430f78bSPetr Mladek  * The operation must be completed by calling klp_free_patch_finish()
6390430f78bSPetr Mladek  * outside klp_mutex.
6400430f78bSPetr Mladek  */
6417e35e4ebSPetr Mladek static void klp_free_patch_start(struct klp_patch *patch)
642b700e7f0SSeth Jennings {
643b700e7f0SSeth Jennings 	if (!list_empty(&patch->list))
644b700e7f0SSeth Jennings 		list_del(&patch->list);
6450430f78bSPetr Mladek 
6460430f78bSPetr Mladek 	klp_free_objects(patch);
6470430f78bSPetr Mladek }
6480430f78bSPetr Mladek 
6490430f78bSPetr Mladek /*
6500430f78bSPetr Mladek  * This function implements the free part that must be called outside
6510430f78bSPetr Mladek  * klp_mutex.
6520430f78bSPetr Mladek  *
6530430f78bSPetr Mladek  * It must be called after klp_free_patch_start(). And it has to be
6540430f78bSPetr Mladek  * the last function accessing the livepatch structures when the patch
6550430f78bSPetr Mladek  * gets disabled.
6560430f78bSPetr Mladek  */
6570430f78bSPetr Mladek static void klp_free_patch_finish(struct klp_patch *patch)
6580430f78bSPetr Mladek {
6590430f78bSPetr Mladek 	/*
6600430f78bSPetr Mladek 	 * Avoid deadlock with enabled_store() sysfs callback by
6610430f78bSPetr Mladek 	 * calling this outside klp_mutex. It is safe because
6620430f78bSPetr Mladek 	 * this is called when the patch gets disabled and it
6630430f78bSPetr Mladek 	 * cannot get enabled again.
6640430f78bSPetr Mladek 	 */
6650430f78bSPetr Mladek 	kobject_put(&patch->kobj);
6660430f78bSPetr Mladek 	wait_for_completion(&patch->finish);
667958ef1e3SPetr Mladek 
668958ef1e3SPetr Mladek 	/* Put the module after the last access to struct klp_patch. */
669958ef1e3SPetr Mladek 	if (!patch->forced)
670958ef1e3SPetr Mladek 		module_put(patch->mod);
671958ef1e3SPetr Mladek }
672958ef1e3SPetr Mladek 
673958ef1e3SPetr Mladek /*
674958ef1e3SPetr Mladek  * The livepatch might be freed from sysfs interface created by the patch.
675958ef1e3SPetr Mladek  * This work allows to wait until the interface is destroyed in a separate
676958ef1e3SPetr Mladek  * context.
677958ef1e3SPetr Mladek  */
678958ef1e3SPetr Mladek static void klp_free_patch_work_fn(struct work_struct *work)
679958ef1e3SPetr Mladek {
680958ef1e3SPetr Mladek 	struct klp_patch *patch =
681958ef1e3SPetr Mladek 		container_of(work, struct klp_patch, free_work);
682958ef1e3SPetr Mladek 
683958ef1e3SPetr Mladek 	klp_free_patch_finish(patch);
684b700e7f0SSeth Jennings }
685b700e7f0SSeth Jennings 
6867e35e4ebSPetr Mladek void klp_free_patch_async(struct klp_patch *patch)
6877e35e4ebSPetr Mladek {
6887e35e4ebSPetr Mladek 	klp_free_patch_start(patch);
6897e35e4ebSPetr Mladek 	schedule_work(&patch->free_work);
6907e35e4ebSPetr Mladek }
6917e35e4ebSPetr Mladek 
6927e35e4ebSPetr Mladek void klp_free_replaced_patches_async(struct klp_patch *new_patch)
6937e35e4ebSPetr Mladek {
6947e35e4ebSPetr Mladek 	struct klp_patch *old_patch, *tmp_patch;
6957e35e4ebSPetr Mladek 
6967e35e4ebSPetr Mladek 	klp_for_each_patch_safe(old_patch, tmp_patch) {
6977e35e4ebSPetr Mladek 		if (old_patch == new_patch)
6987e35e4ebSPetr Mladek 			return;
6997e35e4ebSPetr Mladek 		klp_free_patch_async(old_patch);
7007e35e4ebSPetr Mladek 	}
7017e35e4ebSPetr Mladek }
7027e35e4ebSPetr Mladek 
703b700e7f0SSeth Jennings static int klp_init_func(struct klp_object *obj, struct klp_func *func)
704b700e7f0SSeth Jennings {
705e1452b60SJason Baron 	if (!func->old_name)
706e1452b60SJason Baron 		return -EINVAL;
707e1452b60SJason Baron 
708e1452b60SJason Baron 	/*
709e1452b60SJason Baron 	 * NOPs get the address later. The patched module must be loaded,
710e1452b60SJason Baron 	 * see klp_init_object_loaded().
711e1452b60SJason Baron 	 */
712e1452b60SJason Baron 	if (!func->new_func && !func->nop)
713f09d9086SMiroslav Benes 		return -EINVAL;
714f09d9086SMiroslav Benes 
7156e9df95bSKamalesh Babulal 	if (strlen(func->old_name) >= KSYM_NAME_LEN)
7166e9df95bSKamalesh Babulal 		return -EINVAL;
7176e9df95bSKamalesh Babulal 
7183c33f5b9SJosh Poimboeuf 	INIT_LIST_HEAD(&func->stack_node);
7190dade9f3SJosh Poimboeuf 	func->patched = false;
720d83a7cb3SJosh Poimboeuf 	func->transition = false;
721b700e7f0SSeth Jennings 
722444f9e99SChris J Arges 	/* The format for the sysfs directory is <function,sympos> where sympos
723444f9e99SChris J Arges 	 * is the nth occurrence of this symbol in kallsyms for the patched
724444f9e99SChris J Arges 	 * object. If the user selects 0 for old_sympos, then 1 will be used
725444f9e99SChris J Arges 	 * since a unique symbol will be the first occurrence.
726444f9e99SChris J Arges 	 */
7274d141ab3SPetr Mladek 	return kobject_add(&func->kobj, &obj->kobj, "%s,%lu",
7284d141ab3SPetr Mladek 			   func->old_name,
729444f9e99SChris J Arges 			   func->old_sympos ? func->old_sympos : 1);
730b700e7f0SSeth Jennings }
731b700e7f0SSeth Jennings 
732255e732cSJessica Yu /* Arches may override this to finish any remaining arch-specific tasks */
733255e732cSJessica Yu void __weak arch_klp_init_object_loaded(struct klp_patch *patch,
734255e732cSJessica Yu 					struct klp_object *obj)
735255e732cSJessica Yu {
736255e732cSJessica Yu }
737255e732cSJessica Yu 
738*7c8e2bddSJosh Poimboeuf int klp_apply_object_relocs(struct klp_patch *patch, struct klp_object *obj)
739*7c8e2bddSJosh Poimboeuf {
740*7c8e2bddSJosh Poimboeuf 	int i, ret;
741*7c8e2bddSJosh Poimboeuf 	struct klp_modinfo *info = patch->mod->klp_info;
742*7c8e2bddSJosh Poimboeuf 
743*7c8e2bddSJosh Poimboeuf 	for (i = 1; i < info->hdr.e_shnum; i++) {
744*7c8e2bddSJosh Poimboeuf 		Elf_Shdr *sec = info->sechdrs + i;
745*7c8e2bddSJosh Poimboeuf 
746*7c8e2bddSJosh Poimboeuf 		if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
747*7c8e2bddSJosh Poimboeuf 			continue;
748*7c8e2bddSJosh Poimboeuf 
749*7c8e2bddSJosh Poimboeuf 		ret = klp_apply_section_relocs(patch->mod, info->sechdrs,
750*7c8e2bddSJosh Poimboeuf 					       info->secstrings,
751*7c8e2bddSJosh Poimboeuf 					       patch->mod->core_kallsyms.strtab,
752*7c8e2bddSJosh Poimboeuf 					       info->symndx, i, obj->name);
753*7c8e2bddSJosh Poimboeuf 		if (ret)
754*7c8e2bddSJosh Poimboeuf 			return ret;
755*7c8e2bddSJosh Poimboeuf 	}
756*7c8e2bddSJosh Poimboeuf 
757*7c8e2bddSJosh Poimboeuf 	return 0;
758*7c8e2bddSJosh Poimboeuf }
759*7c8e2bddSJosh Poimboeuf 
760b700e7f0SSeth Jennings /* parts of the initialization that is done only when the object is loaded */
761b700e7f0SSeth Jennings static int klp_init_object_loaded(struct klp_patch *patch,
762b700e7f0SSeth Jennings 				  struct klp_object *obj)
763b700e7f0SSeth Jennings {
764b700e7f0SSeth Jennings 	struct klp_func *func;
765b700e7f0SSeth Jennings 	int ret;
766b700e7f0SSeth Jennings 
7679f255b63SJosh Poimboeuf 	mutex_lock(&text_mutex);
768255e732cSJessica Yu 	module_disable_ro(patch->mod);
769*7c8e2bddSJosh Poimboeuf 
770*7c8e2bddSJosh Poimboeuf 	if (klp_is_module(obj)) {
771*7c8e2bddSJosh Poimboeuf 		/*
772*7c8e2bddSJosh Poimboeuf 		 * Only write module-specific relocations here
773*7c8e2bddSJosh Poimboeuf 		 * (.klp.rela.{module}.*).  vmlinux-specific relocations were
774*7c8e2bddSJosh Poimboeuf 		 * written earlier during the initialization of the klp module
775*7c8e2bddSJosh Poimboeuf 		 * itself.
776*7c8e2bddSJosh Poimboeuf 		 */
777*7c8e2bddSJosh Poimboeuf 		ret = klp_apply_object_relocs(patch, obj);
778255e732cSJessica Yu 		if (ret) {
779255e732cSJessica Yu 			module_enable_ro(patch->mod, true);
7809f255b63SJosh Poimboeuf 			mutex_unlock(&text_mutex);
781b700e7f0SSeth Jennings 			return ret;
782255e732cSJessica Yu 		}
783*7c8e2bddSJosh Poimboeuf 	}
784255e732cSJessica Yu 
785255e732cSJessica Yu 	arch_klp_init_object_loaded(patch, obj);
786b700e7f0SSeth Jennings 
787*7c8e2bddSJosh Poimboeuf 	module_enable_ro(patch->mod, true);
7889f255b63SJosh Poimboeuf 	mutex_unlock(&text_mutex);
7899f255b63SJosh Poimboeuf 
7908cdd043aSJiri Slaby 	klp_for_each_func(obj, func) {
791b2b018efSChris J Arges 		ret = klp_find_object_symbol(obj->name, func->old_name,
792b2b018efSChris J Arges 					     func->old_sympos,
79319514910SPetr Mladek 					     (unsigned long *)&func->old_func);
794b700e7f0SSeth Jennings 		if (ret)
795b700e7f0SSeth Jennings 			return ret;
796f5e547f4SJosh Poimboeuf 
79719514910SPetr Mladek 		ret = kallsyms_lookup_size_offset((unsigned long)func->old_func,
798f5e547f4SJosh Poimboeuf 						  &func->old_size, NULL);
799f5e547f4SJosh Poimboeuf 		if (!ret) {
800f5e547f4SJosh Poimboeuf 			pr_err("kallsyms size lookup failed for '%s'\n",
801f5e547f4SJosh Poimboeuf 			       func->old_name);
802f5e547f4SJosh Poimboeuf 			return -ENOENT;
803f5e547f4SJosh Poimboeuf 		}
804f5e547f4SJosh Poimboeuf 
805e1452b60SJason Baron 		if (func->nop)
806e1452b60SJason Baron 			func->new_func = func->old_func;
807e1452b60SJason Baron 
808f5e547f4SJosh Poimboeuf 		ret = kallsyms_lookup_size_offset((unsigned long)func->new_func,
809f5e547f4SJosh Poimboeuf 						  &func->new_size, NULL);
810f5e547f4SJosh Poimboeuf 		if (!ret) {
811f5e547f4SJosh Poimboeuf 			pr_err("kallsyms size lookup failed for '%s' replacement\n",
812f5e547f4SJosh Poimboeuf 			       func->old_name);
813f5e547f4SJosh Poimboeuf 			return -ENOENT;
814f5e547f4SJosh Poimboeuf 		}
815b700e7f0SSeth Jennings 	}
816b700e7f0SSeth Jennings 
817b700e7f0SSeth Jennings 	return 0;
818b700e7f0SSeth Jennings }
819b700e7f0SSeth Jennings 
820b700e7f0SSeth Jennings static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
821b700e7f0SSeth Jennings {
822b700e7f0SSeth Jennings 	struct klp_func *func;
823b700e7f0SSeth Jennings 	int ret;
824b700e7f0SSeth Jennings 	const char *name;
825b700e7f0SSeth Jennings 
8266e9df95bSKamalesh Babulal 	if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN)
8276e9df95bSKamalesh Babulal 		return -EINVAL;
8286e9df95bSKamalesh Babulal 
8290dade9f3SJosh Poimboeuf 	obj->patched = false;
8308cb2c2dcSPetr Mladek 	obj->mod = NULL;
831b700e7f0SSeth Jennings 
832b700e7f0SSeth Jennings 	klp_find_object_module(obj);
833b700e7f0SSeth Jennings 
834b700e7f0SSeth Jennings 	name = klp_is_module(obj) ? obj->name : "vmlinux";
8354d141ab3SPetr Mladek 	ret = kobject_add(&obj->kobj, &patch->kobj, "%s", name);
836cad706dfSMiroslav Benes 	if (ret)
837cad706dfSMiroslav Benes 		return ret;
838b700e7f0SSeth Jennings 
8398cdd043aSJiri Slaby 	klp_for_each_func(obj, func) {
840b700e7f0SSeth Jennings 		ret = klp_init_func(obj, func);
841b700e7f0SSeth Jennings 		if (ret)
8420430f78bSPetr Mladek 			return ret;
843b700e7f0SSeth Jennings 	}
844b700e7f0SSeth Jennings 
8450430f78bSPetr Mladek 	if (klp_is_object_loaded(obj))
846b700e7f0SSeth Jennings 		ret = klp_init_object_loaded(patch, obj);
8470430f78bSPetr Mladek 
8480430f78bSPetr Mladek 	return ret;
8490430f78bSPetr Mladek }
8500430f78bSPetr Mladek 
851f68d67cfSPetr Mladek static void klp_init_func_early(struct klp_object *obj,
852f68d67cfSPetr Mladek 				struct klp_func *func)
853f68d67cfSPetr Mladek {
854f68d67cfSPetr Mladek 	kobject_init(&func->kobj, &klp_ktype_func);
855f68d67cfSPetr Mladek 	list_add_tail(&func->node, &obj->func_list);
856f68d67cfSPetr Mladek }
857f68d67cfSPetr Mladek 
858f68d67cfSPetr Mladek static void klp_init_object_early(struct klp_patch *patch,
859f68d67cfSPetr Mladek 				  struct klp_object *obj)
860f68d67cfSPetr Mladek {
861f68d67cfSPetr Mladek 	INIT_LIST_HEAD(&obj->func_list);
862f68d67cfSPetr Mladek 	kobject_init(&obj->kobj, &klp_ktype_object);
863f68d67cfSPetr Mladek 	list_add_tail(&obj->node, &patch->obj_list);
864f68d67cfSPetr Mladek }
865f68d67cfSPetr Mladek 
8660430f78bSPetr Mladek static int klp_init_patch_early(struct klp_patch *patch)
8670430f78bSPetr Mladek {
8680430f78bSPetr Mladek 	struct klp_object *obj;
8690430f78bSPetr Mladek 	struct klp_func *func;
8700430f78bSPetr Mladek 
8710430f78bSPetr Mladek 	if (!patch->objs)
8720430f78bSPetr Mladek 		return -EINVAL;
8730430f78bSPetr Mladek 
8740430f78bSPetr Mladek 	INIT_LIST_HEAD(&patch->list);
87520e55025SJason Baron 	INIT_LIST_HEAD(&patch->obj_list);
8764d141ab3SPetr Mladek 	kobject_init(&patch->kobj, &klp_ktype_patch);
8770430f78bSPetr Mladek 	patch->enabled = false;
87868007289SPetr Mladek 	patch->forced = false;
879958ef1e3SPetr Mladek 	INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
8800430f78bSPetr Mladek 	init_completion(&patch->finish);
8810430f78bSPetr Mladek 
88220e55025SJason Baron 	klp_for_each_object_static(patch, obj) {
8830430f78bSPetr Mladek 		if (!obj->funcs)
8840430f78bSPetr Mladek 			return -EINVAL;
8850430f78bSPetr Mladek 
886f68d67cfSPetr Mladek 		klp_init_object_early(patch, obj);
8870430f78bSPetr Mladek 
88820e55025SJason Baron 		klp_for_each_func_static(obj, func) {
889f68d67cfSPetr Mladek 			klp_init_func_early(obj, func);
89020e55025SJason Baron 		}
891b700e7f0SSeth Jennings 	}
892b700e7f0SSeth Jennings 
893958ef1e3SPetr Mladek 	if (!try_module_get(patch->mod))
894958ef1e3SPetr Mladek 		return -ENODEV;
895958ef1e3SPetr Mladek 
896b700e7f0SSeth Jennings 	return 0;
897b700e7f0SSeth Jennings }
898b700e7f0SSeth Jennings 
899b700e7f0SSeth Jennings static int klp_init_patch(struct klp_patch *patch)
900b700e7f0SSeth Jennings {
901b700e7f0SSeth Jennings 	struct klp_object *obj;
902b700e7f0SSeth Jennings 	int ret;
903b700e7f0SSeth Jennings 
9044d141ab3SPetr Mladek 	ret = kobject_add(&patch->kobj, klp_root_kobj, "%s", patch->mod->name);
905958ef1e3SPetr Mladek 	if (ret)
9063ec24776SJosh Poimboeuf 		return ret;
907b700e7f0SSeth Jennings 
908e1452b60SJason Baron 	if (patch->replace) {
909e1452b60SJason Baron 		ret = klp_add_nops(patch);
910e1452b60SJason Baron 		if (ret)
911e1452b60SJason Baron 			return ret;
912e1452b60SJason Baron 	}
913e1452b60SJason Baron 
9148cdd043aSJiri Slaby 	klp_for_each_object(patch, obj) {
915b700e7f0SSeth Jennings 		ret = klp_init_object(patch, obj);
916b700e7f0SSeth Jennings 		if (ret)
917958ef1e3SPetr Mladek 			return ret;
918b700e7f0SSeth Jennings 	}
919b700e7f0SSeth Jennings 
92099590ba5SJosh Poimboeuf 	list_add_tail(&patch->list, &klp_patches);
921b700e7f0SSeth Jennings 
922b700e7f0SSeth Jennings 	return 0;
923b700e7f0SSeth Jennings }
924b700e7f0SSeth Jennings 
92526c3e98eSPetr Mladek static int __klp_disable_patch(struct klp_patch *patch)
92626c3e98eSPetr Mladek {
92726c3e98eSPetr Mladek 	struct klp_object *obj;
92826c3e98eSPetr Mladek 
92926c3e98eSPetr Mladek 	if (WARN_ON(!patch->enabled))
93026c3e98eSPetr Mladek 		return -EINVAL;
93126c3e98eSPetr Mladek 
93226c3e98eSPetr Mladek 	if (klp_transition_patch)
93326c3e98eSPetr Mladek 		return -EBUSY;
93426c3e98eSPetr Mladek 
93526c3e98eSPetr Mladek 	klp_init_transition(patch, KLP_UNPATCHED);
93626c3e98eSPetr Mladek 
93726c3e98eSPetr Mladek 	klp_for_each_object(patch, obj)
93826c3e98eSPetr Mladek 		if (obj->patched)
93926c3e98eSPetr Mladek 			klp_pre_unpatch_callback(obj);
94026c3e98eSPetr Mladek 
94126c3e98eSPetr Mladek 	/*
94226c3e98eSPetr Mladek 	 * Enforce the order of the func->transition writes in
94326c3e98eSPetr Mladek 	 * klp_init_transition() and the TIF_PATCH_PENDING writes in
94426c3e98eSPetr Mladek 	 * klp_start_transition().  In the rare case where klp_ftrace_handler()
94526c3e98eSPetr Mladek 	 * is called shortly after klp_update_patch_state() switches the task,
94626c3e98eSPetr Mladek 	 * this ensures the handler sees that func->transition is set.
94726c3e98eSPetr Mladek 	 */
94826c3e98eSPetr Mladek 	smp_wmb();
94926c3e98eSPetr Mladek 
95026c3e98eSPetr Mladek 	klp_start_transition();
95126c3e98eSPetr Mladek 	patch->enabled = false;
952958ef1e3SPetr Mladek 	klp_try_complete_transition();
95326c3e98eSPetr Mladek 
95426c3e98eSPetr Mladek 	return 0;
95526c3e98eSPetr Mladek }
95626c3e98eSPetr Mladek 
95726c3e98eSPetr Mladek static int __klp_enable_patch(struct klp_patch *patch)
95826c3e98eSPetr Mladek {
95926c3e98eSPetr Mladek 	struct klp_object *obj;
96026c3e98eSPetr Mladek 	int ret;
96126c3e98eSPetr Mladek 
96226c3e98eSPetr Mladek 	if (klp_transition_patch)
96326c3e98eSPetr Mladek 		return -EBUSY;
96426c3e98eSPetr Mladek 
96526c3e98eSPetr Mladek 	if (WARN_ON(patch->enabled))
96626c3e98eSPetr Mladek 		return -EINVAL;
96726c3e98eSPetr Mladek 
96826c3e98eSPetr Mladek 	pr_notice("enabling patch '%s'\n", patch->mod->name);
96926c3e98eSPetr Mladek 
97026c3e98eSPetr Mladek 	klp_init_transition(patch, KLP_PATCHED);
97126c3e98eSPetr Mladek 
97226c3e98eSPetr Mladek 	/*
97326c3e98eSPetr Mladek 	 * Enforce the order of the func->transition writes in
97426c3e98eSPetr Mladek 	 * klp_init_transition() and the ops->func_stack writes in
97526c3e98eSPetr Mladek 	 * klp_patch_object(), so that klp_ftrace_handler() will see the
97626c3e98eSPetr Mladek 	 * func->transition updates before the handler is registered and the
97726c3e98eSPetr Mladek 	 * new funcs become visible to the handler.
97826c3e98eSPetr Mladek 	 */
97926c3e98eSPetr Mladek 	smp_wmb();
98026c3e98eSPetr Mladek 
98126c3e98eSPetr Mladek 	klp_for_each_object(patch, obj) {
98226c3e98eSPetr Mladek 		if (!klp_is_object_loaded(obj))
98326c3e98eSPetr Mladek 			continue;
98426c3e98eSPetr Mladek 
98526c3e98eSPetr Mladek 		ret = klp_pre_patch_callback(obj);
98626c3e98eSPetr Mladek 		if (ret) {
98726c3e98eSPetr Mladek 			pr_warn("pre-patch callback failed for object '%s'\n",
98826c3e98eSPetr Mladek 				klp_is_module(obj) ? obj->name : "vmlinux");
98926c3e98eSPetr Mladek 			goto err;
99026c3e98eSPetr Mladek 		}
99126c3e98eSPetr Mladek 
99226c3e98eSPetr Mladek 		ret = klp_patch_object(obj);
99326c3e98eSPetr Mladek 		if (ret) {
99426c3e98eSPetr Mladek 			pr_warn("failed to patch object '%s'\n",
99526c3e98eSPetr Mladek 				klp_is_module(obj) ? obj->name : "vmlinux");
99626c3e98eSPetr Mladek 			goto err;
99726c3e98eSPetr Mladek 		}
99826c3e98eSPetr Mladek 	}
99926c3e98eSPetr Mladek 
100026c3e98eSPetr Mladek 	klp_start_transition();
100126c3e98eSPetr Mladek 	patch->enabled = true;
1002958ef1e3SPetr Mladek 	klp_try_complete_transition();
100326c3e98eSPetr Mladek 
100426c3e98eSPetr Mladek 	return 0;
100526c3e98eSPetr Mladek err:
100626c3e98eSPetr Mladek 	pr_warn("failed to enable patch '%s'\n", patch->mod->name);
100726c3e98eSPetr Mladek 
100826c3e98eSPetr Mladek 	klp_cancel_transition();
100926c3e98eSPetr Mladek 	return ret;
101026c3e98eSPetr Mladek }
101126c3e98eSPetr Mladek 
101226c3e98eSPetr Mladek /**
1013958ef1e3SPetr Mladek  * klp_enable_patch() - enable the livepatch
1014958ef1e3SPetr Mladek  * @patch:	patch to be enabled
101526c3e98eSPetr Mladek  *
1016958ef1e3SPetr Mladek  * Initializes the data structure associated with the patch, creates the sysfs
1017958ef1e3SPetr Mladek  * interface, performs the needed symbol lookups and code relocations,
1018958ef1e3SPetr Mladek  * registers the patched functions with ftrace.
1019958ef1e3SPetr Mladek  *
1020958ef1e3SPetr Mladek  * This function is supposed to be called from the livepatch module_init()
1021958ef1e3SPetr Mladek  * callback.
102226c3e98eSPetr Mladek  *
102326c3e98eSPetr Mladek  * Return: 0 on success, otherwise error
102426c3e98eSPetr Mladek  */
102526c3e98eSPetr Mladek int klp_enable_patch(struct klp_patch *patch)
102626c3e98eSPetr Mladek {
102726c3e98eSPetr Mladek 	int ret;
102826c3e98eSPetr Mladek 
1029958ef1e3SPetr Mladek 	if (!patch || !patch->mod)
1030958ef1e3SPetr Mladek 		return -EINVAL;
103126c3e98eSPetr Mladek 
1032958ef1e3SPetr Mladek 	if (!is_livepatch_module(patch->mod)) {
1033958ef1e3SPetr Mladek 		pr_err("module %s is not marked as a livepatch module\n",
1034958ef1e3SPetr Mladek 		       patch->mod->name);
1035958ef1e3SPetr Mladek 		return -EINVAL;
103626c3e98eSPetr Mladek 	}
103726c3e98eSPetr Mladek 
1038958ef1e3SPetr Mladek 	if (!klp_initialized())
1039958ef1e3SPetr Mladek 		return -ENODEV;
1040958ef1e3SPetr Mladek 
1041958ef1e3SPetr Mladek 	if (!klp_have_reliable_stack()) {
104231adf230SPetr Mladek 		pr_warn("This architecture doesn't have support for the livepatch consistency model.\n");
104331adf230SPetr Mladek 		pr_warn("The livepatch transition may never complete.\n");
1044958ef1e3SPetr Mladek 	}
1045958ef1e3SPetr Mladek 
1046958ef1e3SPetr Mladek 	mutex_lock(&klp_mutex);
1047958ef1e3SPetr Mladek 
104892c9abf5SPetr Mladek 	if (!klp_is_patch_compatible(patch)) {
104992c9abf5SPetr Mladek 		pr_err("Livepatch patch (%s) is not compatible with the already installed livepatches.\n",
105092c9abf5SPetr Mladek 			patch->mod->name);
105192c9abf5SPetr Mladek 		mutex_unlock(&klp_mutex);
105292c9abf5SPetr Mladek 		return -EINVAL;
105392c9abf5SPetr Mladek 	}
105492c9abf5SPetr Mladek 
1055958ef1e3SPetr Mladek 	ret = klp_init_patch_early(patch);
1056958ef1e3SPetr Mladek 	if (ret) {
1057958ef1e3SPetr Mladek 		mutex_unlock(&klp_mutex);
1058958ef1e3SPetr Mladek 		return ret;
1059958ef1e3SPetr Mladek 	}
1060958ef1e3SPetr Mladek 
1061958ef1e3SPetr Mladek 	ret = klp_init_patch(patch);
1062958ef1e3SPetr Mladek 	if (ret)
1063958ef1e3SPetr Mladek 		goto err;
1064958ef1e3SPetr Mladek 
106526c3e98eSPetr Mladek 	ret = __klp_enable_patch(patch);
1066958ef1e3SPetr Mladek 	if (ret)
1067958ef1e3SPetr Mladek 		goto err;
1068958ef1e3SPetr Mladek 
1069958ef1e3SPetr Mladek 	mutex_unlock(&klp_mutex);
1070958ef1e3SPetr Mladek 
1071958ef1e3SPetr Mladek 	return 0;
107226c3e98eSPetr Mladek 
107326c3e98eSPetr Mladek err:
1074958ef1e3SPetr Mladek 	klp_free_patch_start(patch);
1075958ef1e3SPetr Mladek 
107626c3e98eSPetr Mladek 	mutex_unlock(&klp_mutex);
1077958ef1e3SPetr Mladek 
1078958ef1e3SPetr Mladek 	klp_free_patch_finish(patch);
1079958ef1e3SPetr Mladek 
108026c3e98eSPetr Mladek 	return ret;
108126c3e98eSPetr Mladek }
108226c3e98eSPetr Mladek EXPORT_SYMBOL_GPL(klp_enable_patch);
108326c3e98eSPetr Mladek 
1084ef8daf8eSJoe Lawrence /*
10857e35e4ebSPetr Mladek  * This function unpatches objects from the replaced livepatches.
1086e1452b60SJason Baron  *
1087e1452b60SJason Baron  * We could be pretty aggressive here. It is called in the situation where
10887e35e4ebSPetr Mladek  * these structures are no longer accessed from the ftrace handler.
10897e35e4ebSPetr Mladek  * All functions are redirected by the klp_transition_patch. They
10907e35e4ebSPetr Mladek  * use either a new code or they are in the original code because
10917e35e4ebSPetr Mladek  * of the special nop function patches.
1092e1452b60SJason Baron  *
1093e1452b60SJason Baron  * The only exception is when the transition was forced. In this case,
1094e1452b60SJason Baron  * klp_ftrace_handler() might still see the replaced patch on the stack.
1095e1452b60SJason Baron  * Fortunately, it is carefully designed to work with removed functions
1096e1452b60SJason Baron  * thanks to RCU. We only have to keep the patches on the system. Also
1097e1452b60SJason Baron  * this is handled transparently by patch->module_put.
1098e1452b60SJason Baron  */
10997e35e4ebSPetr Mladek void klp_unpatch_replaced_patches(struct klp_patch *new_patch)
1100e1452b60SJason Baron {
11017e35e4ebSPetr Mladek 	struct klp_patch *old_patch;
1102e1452b60SJason Baron 
11037e35e4ebSPetr Mladek 	klp_for_each_patch(old_patch) {
1104e1452b60SJason Baron 		if (old_patch == new_patch)
1105e1452b60SJason Baron 			return;
1106e1452b60SJason Baron 
1107e1452b60SJason Baron 		old_patch->enabled = false;
1108e1452b60SJason Baron 		klp_unpatch_objects(old_patch);
1109e1452b60SJason Baron 	}
1110e1452b60SJason Baron }
1111e1452b60SJason Baron 
1112e1452b60SJason Baron /*
1113d697bad5SPetr Mladek  * This function removes the dynamically allocated 'nop' functions.
1114d697bad5SPetr Mladek  *
1115d697bad5SPetr Mladek  * We could be pretty aggressive. NOPs do not change the existing
1116d697bad5SPetr Mladek  * behavior except for adding unnecessary delay by the ftrace handler.
1117d697bad5SPetr Mladek  *
1118d697bad5SPetr Mladek  * It is safe even when the transition was forced. The ftrace handler
1119d697bad5SPetr Mladek  * will see a valid ops->func_stack entry thanks to RCU.
1120d697bad5SPetr Mladek  *
1121d697bad5SPetr Mladek  * We could even free the NOPs structures. They must be the last entry
1122d697bad5SPetr Mladek  * in ops->func_stack. Therefore unregister_ftrace_function() is called.
1123d697bad5SPetr Mladek  * It does the same as klp_synchronize_transition() to make sure that
1124d697bad5SPetr Mladek  * nobody is inside the ftrace handler once the operation finishes.
1125d697bad5SPetr Mladek  *
1126d697bad5SPetr Mladek  * IMPORTANT: It must be called right after removing the replaced patches!
1127d697bad5SPetr Mladek  */
1128d697bad5SPetr Mladek void klp_discard_nops(struct klp_patch *new_patch)
1129d697bad5SPetr Mladek {
1130d697bad5SPetr Mladek 	klp_unpatch_objects_dynamic(klp_transition_patch);
1131d697bad5SPetr Mladek 	klp_free_objects_dynamic(klp_transition_patch);
1132d697bad5SPetr Mladek }
1133d697bad5SPetr Mladek 
1134d697bad5SPetr Mladek /*
1135ef8daf8eSJoe Lawrence  * Remove parts of patches that touch a given kernel module. The list of
1136ef8daf8eSJoe Lawrence  * patches processed might be limited. When limit is NULL, all patches
1137ef8daf8eSJoe Lawrence  * will be handled.
1138ef8daf8eSJoe Lawrence  */
1139ef8daf8eSJoe Lawrence static void klp_cleanup_module_patches_limited(struct module *mod,
1140ef8daf8eSJoe Lawrence 					       struct klp_patch *limit)
1141ef8daf8eSJoe Lawrence {
1142ef8daf8eSJoe Lawrence 	struct klp_patch *patch;
1143ef8daf8eSJoe Lawrence 	struct klp_object *obj;
1144ef8daf8eSJoe Lawrence 
1145ecba29f4SPetr Mladek 	klp_for_each_patch(patch) {
1146ef8daf8eSJoe Lawrence 		if (patch == limit)
1147ef8daf8eSJoe Lawrence 			break;
1148ef8daf8eSJoe Lawrence 
1149ef8daf8eSJoe Lawrence 		klp_for_each_object(patch, obj) {
1150ef8daf8eSJoe Lawrence 			if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1151ef8daf8eSJoe Lawrence 				continue;
1152ef8daf8eSJoe Lawrence 
1153fc41efc1SJiri Kosina 			if (patch != klp_transition_patch)
1154fc41efc1SJiri Kosina 				klp_pre_unpatch_callback(obj);
1155fc41efc1SJiri Kosina 
1156ef8daf8eSJoe Lawrence 			pr_notice("reverting patch '%s' on unloading module '%s'\n",
1157ef8daf8eSJoe Lawrence 				  patch->mod->name, obj->mod->name);
1158ef8daf8eSJoe Lawrence 			klp_unpatch_object(obj);
1159fc41efc1SJiri Kosina 
1160fc41efc1SJiri Kosina 			klp_post_unpatch_callback(obj);
1161ef8daf8eSJoe Lawrence 
1162ef8daf8eSJoe Lawrence 			klp_free_object_loaded(obj);
1163ef8daf8eSJoe Lawrence 			break;
1164ef8daf8eSJoe Lawrence 		}
1165ef8daf8eSJoe Lawrence 	}
1166ef8daf8eSJoe Lawrence }
1167ef8daf8eSJoe Lawrence 
11687e545d6eSJessica Yu int klp_module_coming(struct module *mod)
1169b700e7f0SSeth Jennings {
117036e505c1SMinfei Huang 	int ret;
1171b700e7f0SSeth Jennings 	struct klp_patch *patch;
1172b700e7f0SSeth Jennings 	struct klp_object *obj;
1173b700e7f0SSeth Jennings 
11747e545d6eSJessica Yu 	if (WARN_ON(mod->state != MODULE_STATE_COMING))
11757e545d6eSJessica Yu 		return -EINVAL;
1176b700e7f0SSeth Jennings 
1177dcf550e5SJosh Poimboeuf 	if (!strcmp(mod->name, "vmlinux")) {
1178dcf550e5SJosh Poimboeuf 		pr_err("vmlinux.ko: invalid module name");
1179dcf550e5SJosh Poimboeuf 		return -EINVAL;
1180dcf550e5SJosh Poimboeuf 	}
1181dcf550e5SJosh Poimboeuf 
1182b700e7f0SSeth Jennings 	mutex_lock(&klp_mutex);
11838cb2c2dcSPetr Mladek 	/*
11847e545d6eSJessica Yu 	 * Each module has to know that klp_module_coming()
11857e545d6eSJessica Yu 	 * has been called. We never know what module will
11867e545d6eSJessica Yu 	 * get patched by a new patch.
11878cb2c2dcSPetr Mladek 	 */
11888cb2c2dcSPetr Mladek 	mod->klp_alive = true;
11898cb2c2dcSPetr Mladek 
1190ecba29f4SPetr Mladek 	klp_for_each_patch(patch) {
11918cdd043aSJiri Slaby 		klp_for_each_object(patch, obj) {
1192b700e7f0SSeth Jennings 			if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1193b700e7f0SSeth Jennings 				continue;
1194b700e7f0SSeth Jennings 
1195b700e7f0SSeth Jennings 			obj->mod = mod;
11967e545d6eSJessica Yu 
11977e545d6eSJessica Yu 			ret = klp_init_object_loaded(patch, obj);
119836e505c1SMinfei Huang 			if (ret) {
11997e545d6eSJessica Yu 				pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
12007e545d6eSJessica Yu 					patch->mod->name, obj->mod->name, ret);
12017e545d6eSJessica Yu 				goto err;
120236e505c1SMinfei Huang 			}
12037e545d6eSJessica Yu 
12047e545d6eSJessica Yu 			pr_notice("applying patch '%s' to loading module '%s'\n",
12057e545d6eSJessica Yu 				  patch->mod->name, obj->mod->name);
12067e545d6eSJessica Yu 
120793862e38SJoe Lawrence 			ret = klp_pre_patch_callback(obj);
120893862e38SJoe Lawrence 			if (ret) {
120993862e38SJoe Lawrence 				pr_warn("pre-patch callback failed for object '%s'\n",
121093862e38SJoe Lawrence 					obj->name);
121193862e38SJoe Lawrence 				goto err;
121293862e38SJoe Lawrence 			}
121393862e38SJoe Lawrence 
12140dade9f3SJosh Poimboeuf 			ret = klp_patch_object(obj);
12157e545d6eSJessica Yu 			if (ret) {
12167e545d6eSJessica Yu 				pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
12177e545d6eSJessica Yu 					patch->mod->name, obj->mod->name, ret);
121893862e38SJoe Lawrence 
121993862e38SJoe Lawrence 				klp_post_unpatch_callback(obj);
12207e545d6eSJessica Yu 				goto err;
12217e545d6eSJessica Yu 			}
1222b700e7f0SSeth Jennings 
122393862e38SJoe Lawrence 			if (patch != klp_transition_patch)
122493862e38SJoe Lawrence 				klp_post_patch_callback(obj);
122593862e38SJoe Lawrence 
1226b700e7f0SSeth Jennings 			break;
1227b700e7f0SSeth Jennings 		}
1228b700e7f0SSeth Jennings 	}
1229b700e7f0SSeth Jennings 
1230b700e7f0SSeth Jennings 	mutex_unlock(&klp_mutex);
1231b700e7f0SSeth Jennings 
1232b700e7f0SSeth Jennings 	return 0;
12337e545d6eSJessica Yu 
12347e545d6eSJessica Yu err:
12357e545d6eSJessica Yu 	/*
12367e545d6eSJessica Yu 	 * If a patch is unsuccessfully applied, return
12377e545d6eSJessica Yu 	 * error to the module loader.
12387e545d6eSJessica Yu 	 */
12397e545d6eSJessica Yu 	pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
12407e545d6eSJessica Yu 		patch->mod->name, obj->mod->name, obj->mod->name);
12417e545d6eSJessica Yu 	mod->klp_alive = false;
12424ff96fb5SMiroslav Benes 	obj->mod = NULL;
1243ef8daf8eSJoe Lawrence 	klp_cleanup_module_patches_limited(mod, patch);
12447e545d6eSJessica Yu 	mutex_unlock(&klp_mutex);
12457e545d6eSJessica Yu 
12467e545d6eSJessica Yu 	return ret;
1247b700e7f0SSeth Jennings }
1248b700e7f0SSeth Jennings 
12497e545d6eSJessica Yu void klp_module_going(struct module *mod)
12507e545d6eSJessica Yu {
12517e545d6eSJessica Yu 	if (WARN_ON(mod->state != MODULE_STATE_GOING &&
12527e545d6eSJessica Yu 		    mod->state != MODULE_STATE_COMING))
12537e545d6eSJessica Yu 		return;
12547e545d6eSJessica Yu 
12557e545d6eSJessica Yu 	mutex_lock(&klp_mutex);
12567e545d6eSJessica Yu 	/*
12577e545d6eSJessica Yu 	 * Each module has to know that klp_module_going()
12587e545d6eSJessica Yu 	 * has been called. We never know what module will
12597e545d6eSJessica Yu 	 * get patched by a new patch.
12607e545d6eSJessica Yu 	 */
12617e545d6eSJessica Yu 	mod->klp_alive = false;
12627e545d6eSJessica Yu 
1263ef8daf8eSJoe Lawrence 	klp_cleanup_module_patches_limited(mod, NULL);
12647e545d6eSJessica Yu 
12657e545d6eSJessica Yu 	mutex_unlock(&klp_mutex);
12667e545d6eSJessica Yu }
1267b700e7f0SSeth Jennings 
126826029d88SMinfei Huang static int __init klp_init(void)
1269b700e7f0SSeth Jennings {
1270b700e7f0SSeth Jennings 	klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
12717e545d6eSJessica Yu 	if (!klp_root_kobj)
12727e545d6eSJessica Yu 		return -ENOMEM;
1273b700e7f0SSeth Jennings 
1274b700e7f0SSeth Jennings 	return 0;
1275b700e7f0SSeth Jennings }
1276b700e7f0SSeth Jennings 
1277b700e7f0SSeth Jennings module_init(klp_init);
1278