Several symbols can share one address:
ffffffff8ed7fef0 t __do_sys_fork
ffffffff8ed7fef0 T __ia32_sys_fork
ffffffff8ed7fef0 T __x64_sys_fork
klp_find_ops() looks the ops up by func->old_func, i.e. by address, so
two klp_funcs of the same livepatch naming two of these symbols resolve
to the same klp_ops and are both pushed onto one ops->func_stack.
This breaks the assumption that a single livepatch contributes at most
one entry to any func_stack. klp_ftrace_handler() picks the entry at
the top of the stack, but when both entries belong to the same livepatch
there is nothing that says which of them should be used in the PATCHED
state, and the UNPATCHED state has to end up at the original function
either way. klp_check_stack_func() cannot tell them apart either: it
asks whether the preceding entry is the original function or another
livepatch's replacement, and an aliased sibling is neither.
Patching two aliases of one function from a single livepatch was never
meaningful, so fail object initialization in klp_init_object_loaded()
rather than leave the redirection undefined.
Fixes: 3c33f5b99d68 ("livepatch: support for repatching a function")
Suggested-by: Petr Mladek <[email protected]>
Signed-off-by: Harry Hsu <[email protected]>
---
kernel/livepatch/core.c | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index 28d15ba58a26..0dd8cda5c9b8 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -866,7 +866,7 @@ static void klp_clear_object_relocs(struct klp_patch *patch,
static int klp_init_object_loaded(struct klp_patch *patch,
struct klp_object *obj)
{
- struct klp_func *func;
+ struct klp_func *func, *prev_func;
int ret;
if (klp_is_module(obj)) {
@@ -888,6 +888,21 @@ static int klp_init_object_loaded(struct klp_patch *patch,
if (ret)
return ret;
+ /*
+ * Aliased symbols share one address, so they would resolve to
+ * the same klp_ops and stack up on a single ops->func_stack,
+ * leaving the redirection ambiguous.
+ */
+ klp_for_each_func(obj, prev_func) {
+ if (prev_func == func)
+ break;
+ if (prev_func->old_func == func->old_func) {
+ pr_err("'%s' and '%s' resolve to the same
address, aliased symbols are not supported\n",
+ prev_func->old_name, func->old_name);
+ return -EINVAL;
+ }
+ }
+
ret = kallsyms_lookup_size_offset((unsigned long)func->old_func,
&func->old_size, NULL);
if (!ret) {
--
2.43.0