kpatch/kmod/core
Evgenii Shatokhin 3bd131612d kmod/core: Safely remove the replaced functions
If atomic replacement is used for the old-style patches (the patches
that depend on kpatch.ko), the kernel might crash if the new patch
changes a smaller set of functions than the patch being replaced.

kpatch_apply_patch() does check if the functions from the patch to be
replaced are currently running. However, the functions are removed from
'kpatch_func_hash' in kpatch_register() only after stop_machine() and
kpatch_apply_patch() have finished:

	ret = stop_machine(kpatch_apply_patch, kpmod, NULL);

	/*
	 * For the replace case, remove any obsolete funcs from the hash and
	 * the ftrace filter, and disable the owning patch module so that it
	 * can be removed.
	 */
	if (!ret && replace) {
		struct kpatch_module *kpmod2, *safe;

		hash_for_each_rcu(kpatch_func_hash, i, func, node) {
			if (func->op != KPATCH_OP_UNPATCH)
				continue;
			if (func->force)
				force = 1;
			hash_del_rcu(&func->node);
			WARN_ON(kpatch_ftrace_remove_func(func->old_addr));
		}
	<...>

As a result, the kernel may end up with an inconsistent set of patched
functions. Some of the functions from the replaced patch could
still be running, while some would be already reverted to the original
ones.

I observed kernel crashes in such situations when I was trying to
replace a patch with a new one without a faulty fix.

Let us remove the replaced patched functions from 'kpatch_func_hash'
in kpatch_apply_patch() to avoid such issues.

Signed-off-by: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
2019-07-02 17:40:49 +03:00
..
core.c kmod/core: Safely remove the replaced functions 2019-07-02 17:40:49 +03:00
kpatch.h kmod: add support for in-kernel livepatch hooks 2018-03-23 10:32:14 -04:00
Makefile kmod/core: fix definition of KERNELRELEASE 2018-02-19 13:13:03 +03:00
shadow.c smatch: fix kpatch_shadow_alloc cleanup 2016-12-16 14:26:11 -05:00