Skip to content

Commit e57da5d

Browse files
liu-song-6Kernel Patches Daemon
authored andcommitted
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_update_ipmodify() to __modify_ftrace_direct() and adjust some logic around the call. Signed-off-by: Song Liu <[email protected]> Reviewed-by: Jiri Olsa <[email protected]>
1 parent 887d458 commit e57da5d

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

kernel/trace/ftrace.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,8 @@ static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops)
19711971
*/
19721972
static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
19731973
struct ftrace_hash *old_hash,
1974-
struct ftrace_hash *new_hash)
1974+
struct ftrace_hash *new_hash,
1975+
bool update_target)
19751976
{
19761977
struct ftrace_page *pg;
19771978
struct dyn_ftrace *rec, *end = NULL;
@@ -2006,10 +2007,13 @@ static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
20062007
if (rec->flags & FTRACE_FL_DISABLED)
20072008
continue;
20082009

2009-
/* We need to update only differences of filter_hash */
2010+
/*
2011+
* Unless we are updating the target of a direct function,
2012+
* we only need to update differences of filter_hash
2013+
*/
20102014
in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
20112015
in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
2012-
if (in_old == in_new)
2016+
if (!update_target && (in_old == in_new))
20132017
continue;
20142018

20152019
if (in_new) {
@@ -2020,7 +2024,16 @@ static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
20202024
if (is_ipmodify)
20212025
goto rollback;
20222026

2023-
FTRACE_WARN_ON(rec->flags & FTRACE_FL_DIRECT);
2027+
/*
2028+
* If this is called by __modify_ftrace_direct()
2029+
* then it is only changing where the direct
2030+
* pointer is jumping to, and the record already
2031+
* points to a direct trampoline. If it isn't,
2032+
* then it is a bug to update ipmodify on a direct
2033+
* caller.
2034+
*/
2035+
FTRACE_WARN_ON(!update_target &&
2036+
(rec->flags & FTRACE_FL_DIRECT));
20242037

20252038
/*
20262039
* Another ops with IPMODIFY is already
@@ -2076,7 +2089,7 @@ static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
20762089
if (ftrace_hash_empty(hash))
20772090
hash = NULL;
20782091

2079-
return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
2092+
return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash, false);
20802093
}
20812094

20822095
/* Disabling always succeeds */
@@ -2087,7 +2100,7 @@ static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
20872100
if (ftrace_hash_empty(hash))
20882101
hash = NULL;
20892102

2090-
__ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
2103+
__ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH, false);
20912104
}
20922105

20932106
static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
@@ -2101,7 +2114,7 @@ static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
21012114
if (ftrace_hash_empty(new_hash))
21022115
new_hash = NULL;
21032116

2104-
return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
2117+
return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash, false);
21052118
}
21062119

21072120
static void print_ip_ins(const char *fmt, const unsigned char *p)
@@ -6114,7 +6127,7 @@ EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
61146127
static int
61156128
__modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
61166129
{
6117-
struct ftrace_hash *hash;
6130+
struct ftrace_hash *hash = ops->func_hash->filter_hash;
61186131
struct ftrace_func_entry *entry, *iter;
61196132
static struct ftrace_ops tmp_ops = {
61206133
.func = ftrace_stub,
@@ -6134,13 +6147,21 @@ __modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
61346147
if (err)
61356148
return err;
61366149

6150+
/*
6151+
* Call __ftrace_hash_update_ipmodify() here, so that we can call
6152+
* ops->ops_func for the ops. This is needed because the above
6153+
* register_ftrace_function_nolock() worked on tmp_ops.
6154+
*/
6155+
err = __ftrace_hash_update_ipmodify(ops, hash, hash, true);
6156+
if (err)
6157+
goto out;
6158+
61376159
/*
61386160
* Now the ftrace_ops_list_func() is called to do the direct callers.
61396161
* We can safely change the direct functions attached to each entry.
61406162
*/
61416163
mutex_lock(&ftrace_lock);
61426164

6143-
hash = ops->func_hash->filter_hash;
61446165
size = 1 << hash->size_bits;
61456166
for (i = 0; i < size; i++) {
61466167
hlist_for_each_entry(iter, &hash->buckets[i], hlist) {
@@ -6155,6 +6176,7 @@ __modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
61556176

61566177
mutex_unlock(&ftrace_lock);
61576178

6179+
out:
61586180
/* Removing the tmp_ops will add the updated direct callers to the functions */
61596181
unregister_ftrace_function(&tmp_ops);
61606182

0 commit comments

Comments
 (0)