Skip to content

Commit a50439c

Browse files
committed
ftrace: bpf: Fix IPMODIFY + DIRECT in modify_ftrace_direct()
ftrace_hash_ipmodify_enable() checks IPMODIFY and DIRECT ftrace_ops on the same kernel function. When needed, ftrace_hash_ipmodify_enable() calls ops->ops_func() to prepare the direct ftrace (BPF trampoline) to share the same function as the IPMODIFY ftrace (livepatch). ftrace_hash_ipmodify_enable() is called in register_ftrace_direct() path, but not called in modify_ftrace_direct() path. As a result, the following operations will break livepatch: 1. Load livepatch to a kernel function; 2. Attach fentry program to the kernel function; 3. Attach fexit program to the kernel function. After 3, the kernel function being used will not be the livepatched version, but the original version. Fix this by adding ftrace_hash_ipmodify_enable() to modify_ftrace_direct() and adjust some logic around the call. Signed-off-by: Song Liu <[email protected]>
1 parent 3355695 commit a50439c

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

kernel/bpf/trampoline.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
221221

222222
if (tr->func.ftrace_managed) {
223223
ftrace_set_filter_ip(tr->fops, (unsigned long)ip, 0, 1);
224+
/*
225+
* Clearing fops->trampoline_mutex and fops->NULL is
226+
* needed by the "goto again" case in
227+
* bpf_trampoline_update().
228+
*/
229+
tr->fops->trampoline = 0;
230+
tr->fops->func = NULL;
224231
ret = register_ftrace_direct(tr->fops, (long)new_addr);
225232
} else {
226233
ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
@@ -479,11 +486,6 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
479486
* BPF_TRAMP_F_SHARE_IPMODIFY is set, we can generate the
480487
* trampoline again, and retry register.
481488
*/
482-
/* reset fops->func and fops->trampoline for re-register */
483-
tr->fops->func = NULL;
484-
tr->fops->trampoline = 0;
485-
486-
/* free im memory and reallocate later */
487489
bpf_tramp_image_free(im);
488490
goto again;
489491
}

kernel/trace/ftrace.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,8 +2020,6 @@ static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
20202020
if (is_ipmodify)
20212021
goto rollback;
20222022

2023-
FTRACE_WARN_ON(rec->flags & FTRACE_FL_DIRECT);
2024-
20252023
/*
20262024
* Another ops with IPMODIFY is already
20272025
* attached. We are now attaching a direct
@@ -6128,6 +6126,15 @@ __modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
61286126
if (err)
61296127
return err;
61306128

6129+
/*
6130+
* Call ftrace_hash_ipmodify_enable() here, so that we can call
6131+
* ops->ops_func for the ops. This is needed because the above
6132+
* register_ftrace_function_nolock() worked on tmp_ops.
6133+
*/
6134+
err = ftrace_hash_ipmodify_enable(ops);
6135+
if (err)
6136+
goto out;
6137+
61316138
/*
61326139
* Now the ftrace_ops_list_func() is called to do the direct callers.
61336140
* We can safely change the direct functions attached to each entry.
@@ -6149,6 +6156,7 @@ __modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
61496156

61506157
mutex_unlock(&ftrace_lock);
61516158

6159+
out:
61526160
/* Removing the tmp_ops will add the updated direct callers to the functions */
61536161
unregister_ftrace_function(&tmp_ops);
61546162

0 commit comments

Comments
 (0)