Skip to content

Commit a625357

Browse files
mrutland-armctmarinas
authored andcommitted
arm64: ftrace: consistently handle PLTs.
Sometimes it is necessary to use a PLT entry to call an ftrace trampoline. This is handled by ftrace_make_call() and ftrace_make_nop(), with each having *almost* identical logic, but this is not handled by ftrace_modify_call() since its introduction in commit: 3b23e49 ("arm64: implement ftrace with regs") Due to this, if we ever were to call ftrace_modify_call() for a callsite which requires a PLT entry for a trampoline, then either: a) If the old addr requires a trampoline, ftrace_modify_call() will use an out-of-range address to generate the 'old' branch instruction. This will result in warnings from aarch64_insn_gen_branch_imm() and ftrace_modify_code(), and no instructions will be modified. As ftrace_modify_call() will return an error, this will result in subsequent internal ftrace errors. b) If the old addr does not require a trampoline, but the new addr does, ftrace_modify_call() will use an out-of-range address to generate the 'new' branch instruction. This will result in warnings from aarch64_insn_gen_branch_imm(), and ftrace_modify_code() will replace the 'old' branch with a BRK. This will result in a kernel panic when this BRK is later executed. Practically speaking, case (a) is vastly more likely than case (b), and typically this will result in internal ftrace errors that don't necessarily affect the rest of the system. This can be demonstrated with an out-of-tree test module which triggers ftrace_modify_call(), e.g. | # insmod test_ftrace.ko | test_ftrace: Function test_function raw=0xffffb3749399201c, callsite=0xffffb37493992024 | branch_imm_common: offset out of range | branch_imm_common: offset out of range | ------------[ ftrace bug ]------------ | ftrace failed to modify | [<ffffb37493992024>] test_function+0x8/0x38 [test_ftrace] | actual: 1d:00:00:94 | Updating ftrace call site to call a different ftrace function | ftrace record flags: e0000002 | (2) R | expected tramp: ffffb374ae42ed54 | ------------[ cut here ]------------ | WARNING: CPU: 0 PID: 165 at kernel/trace/ftrace.c:2085 ftrace_bug+0x280/0x2b0 | Modules linked in: test_ftrace(+) | CPU: 0 PID: 165 Comm: insmod Not tainted 5.19.0-rc2-00002-g4d9ead8b45ce #13 | Hardware name: linux,dummy-virt (DT) | pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--) | pc : ftrace_bug+0x280/0x2b0 | lr : ftrace_bug+0x280/0x2b0 | sp : ffff80000839ba00 | x29: ffff80000839ba00 x28: 0000000000000000 x27: ffff80000839bcf0 | x26: ffffb37493994180 x25: ffffb374b0991c28 x24: ffffb374b0d70000 | x23: 00000000ffffffea x22: ffffb374afcc33b0 x21: ffffb374b08f9cc8 | x20: ffff572b8462c000 x19: ffffb374b08f9000 x18: ffffffffffffffff | x17: 6c6c6163202c6331 x16: ffffb374ae5ad110 x15: ffffb374b0d51ee4 | x14: 0000000000000000 x13: 3435646532346561 x12: 3437336266666666 | x11: 203a706d61727420 x10: 6465746365707865 x9 : ffffb374ae5149e8 | x8 : 336266666666203a x7 : 706d617274206465 x6 : 00000000fffff167 | x5 : ffff572bffbc4a08 x4 : 00000000fffff167 x3 : 0000000000000000 | x2 : 0000000000000000 x1 : ffff572b84461e00 x0 : 0000000000000022 | Call trace: | ftrace_bug+0x280/0x2b0 | ftrace_replace_code+0x98/0xa0 | ftrace_modify_all_code+0xe0/0x144 | arch_ftrace_update_code+0x14/0x20 | ftrace_startup+0xf8/0x1b0 | register_ftrace_function+0x38/0x90 | test_ftrace_init+0xd0/0x1000 [test_ftrace] | do_one_initcall+0x50/0x2b0 | do_init_module+0x50/0x1f0 | load_module+0x17c8/0x1d64 | __do_sys_finit_module+0xa8/0x100 | __arm64_sys_finit_module+0x2c/0x3c | invoke_syscall+0x50/0x120 | el0_svc_common.constprop.0+0xdc/0x100 | do_el0_svc+0x3c/0xd0 | el0_svc+0x34/0xb0 | el0t_64_sync_handler+0xbc/0x140 | el0t_64_sync+0x18c/0x190 | ---[ end trace 0000000000000000 ]--- We can solve this by consistently determining whether to use a PLT entry for an address. Note that since (the earlier) commit: f1a54ae ("arm64: module/ftrace: intialize PLT at load time") ... we can consistently determine the PLT address that a given callsite will use, and therefore ftrace_make_nop() does not need to skip validation when a PLT is in use. This patch factors the existing logic out of ftrace_make_call() and ftrace_make_nop() into a common ftrace_find_callable_addr() helper function, which is used by ftrace_make_call(), ftrace_make_nop(), and ftrace_modify_call(). In ftrace_make_nop() the patching is consistently validated by ftrace_modify_code() as we can always determine what the old instruction should have been. Fixes: 3b23e49 ("arm64: implement ftrace with regs") Signed-off-by: Mark Rutland <[email protected]> Cc: Ard Biesheuvel <[email protected]> Cc: Will Deacon <[email protected]> Tested-by: "Ivan T. Ivanov" <[email protected]> Reviewed-by: Chengming Zhou <[email protected]> Reviewed-by: Ard Biesheuvel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 3eefdf9 commit a625357

File tree

1 file changed

+66
-71
lines changed

1 file changed

+66
-71
lines changed

arch/arm64/kernel/ftrace.c

Lines changed: 66 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -78,47 +78,76 @@ static struct plt_entry *get_ftrace_plt(struct module *mod, unsigned long addr)
7878
}
7979

8080
/*
81-
* Turn on the call to ftrace_caller() in instrumented function
81+
* Find the address the callsite must branch to in order to reach '*addr'.
82+
*
83+
* Due to the limited range of 'BL' instructions, modules may be placed too far
84+
* away to branch directly and must use a PLT.
85+
*
86+
* Returns true when '*addr' contains a reachable target address, or has been
87+
* modified to contain a PLT address. Returns false otherwise.
8288
*/
83-
int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
89+
static bool ftrace_find_callable_addr(struct dyn_ftrace *rec,
90+
struct module *mod,
91+
unsigned long *addr)
8492
{
8593
unsigned long pc = rec->ip;
86-
u32 old, new;
87-
long offset = (long)addr - (long)pc;
94+
long offset = (long)*addr - (long)pc;
95+
struct plt_entry *plt;
8896

89-
if (offset < -SZ_128M || offset >= SZ_128M) {
90-
struct module *mod;
91-
struct plt_entry *plt;
97+
/*
98+
* When the target is within range of the 'BL' instruction, use 'addr'
99+
* as-is and branch to that directly.
100+
*/
101+
if (offset >= -SZ_128M && offset < SZ_128M)
102+
return true;
92103

93-
if (!IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
94-
return -EINVAL;
104+
/*
105+
* When the target is outside of the range of a 'BL' instruction, we
106+
* must use a PLT to reach it. We can only place PLTs for modules, and
107+
* only when module PLT support is built-in.
108+
*/
109+
if (!IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
110+
return false;
95111

96-
/*
97-
* On kernels that support module PLTs, the offset between the
98-
* branch instruction and its target may legally exceed the
99-
* range of an ordinary relative 'bl' opcode. In this case, we
100-
* need to branch via a trampoline in the module.
101-
*
102-
* NOTE: __module_text_address() must be called with preemption
103-
* disabled, but we can rely on ftrace_lock to ensure that 'mod'
104-
* retains its validity throughout the remainder of this code.
105-
*/
112+
/*
113+
* 'mod' is only set at module load time, but if we end up
114+
* dealing with an out-of-range condition, we can assume it
115+
* is due to a module being loaded far away from the kernel.
116+
*
117+
* NOTE: __module_text_address() must be called with preemption
118+
* disabled, but we can rely on ftrace_lock to ensure that 'mod'
119+
* retains its validity throughout the remainder of this code.
120+
*/
121+
if (!mod) {
106122
preempt_disable();
107123
mod = __module_text_address(pc);
108124
preempt_enable();
125+
}
109126

110-
if (WARN_ON(!mod))
111-
return -EINVAL;
127+
if (WARN_ON(!mod))
128+
return false;
112129

113-
plt = get_ftrace_plt(mod, addr);
114-
if (!plt) {
115-
pr_err("ftrace: no module PLT for %ps\n", (void *)addr);
116-
return -EINVAL;
117-
}
118-
119-
addr = (unsigned long)plt;
130+
plt = get_ftrace_plt(mod, *addr);
131+
if (!plt) {
132+
pr_err("ftrace: no module PLT for %ps\n", (void *)*addr);
133+
return false;
120134
}
121135

136+
*addr = (unsigned long)plt;
137+
return true;
138+
}
139+
140+
/*
141+
* Turn on the call to ftrace_caller() in instrumented function
142+
*/
143+
int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
144+
{
145+
unsigned long pc = rec->ip;
146+
u32 old, new;
147+
148+
if (!ftrace_find_callable_addr(rec, NULL, &addr))
149+
return -EINVAL;
150+
122151
old = aarch64_insn_gen_nop();
123152
new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
124153

@@ -132,6 +161,11 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
132161
unsigned long pc = rec->ip;
133162
u32 old, new;
134163

164+
if (!ftrace_find_callable_addr(rec, NULL, &old_addr))
165+
return -EINVAL;
166+
if (!ftrace_find_callable_addr(rec, NULL, &addr))
167+
return -EINVAL;
168+
135169
old = aarch64_insn_gen_branch_imm(pc, old_addr,
136170
AARCH64_INSN_BRANCH_LINK);
137171
new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
@@ -181,54 +215,15 @@ int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
181215
unsigned long addr)
182216
{
183217
unsigned long pc = rec->ip;
184-
bool validate = true;
185218
u32 old = 0, new;
186-
long offset = (long)addr - (long)pc;
187219

188-
if (offset < -SZ_128M || offset >= SZ_128M) {
189-
u32 replaced;
190-
191-
if (!IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
192-
return -EINVAL;
193-
194-
/*
195-
* 'mod' is only set at module load time, but if we end up
196-
* dealing with an out-of-range condition, we can assume it
197-
* is due to a module being loaded far away from the kernel.
198-
*/
199-
if (!mod) {
200-
preempt_disable();
201-
mod = __module_text_address(pc);
202-
preempt_enable();
203-
204-
if (WARN_ON(!mod))
205-
return -EINVAL;
206-
}
207-
208-
/*
209-
* The instruction we are about to patch may be a branch and
210-
* link instruction that was redirected via a PLT entry. In
211-
* this case, the normal validation will fail, but we can at
212-
* least check that we are dealing with a branch and link
213-
* instruction that points into the right module.
214-
*/
215-
if (aarch64_insn_read((void *)pc, &replaced))
216-
return -EFAULT;
217-
218-
if (!aarch64_insn_is_bl(replaced) ||
219-
!within_module(pc + aarch64_get_branch_offset(replaced),
220-
mod))
221-
return -EINVAL;
222-
223-
validate = false;
224-
} else {
225-
old = aarch64_insn_gen_branch_imm(pc, addr,
226-
AARCH64_INSN_BRANCH_LINK);
227-
}
220+
if (!ftrace_find_callable_addr(rec, mod, &addr))
221+
return -EINVAL;
228222

223+
old = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
229224
new = aarch64_insn_gen_nop();
230225

231-
return ftrace_modify_code(pc, old, new, validate);
226+
return ftrace_modify_code(pc, old, new, true);
232227
}
233228

234229
void arch_ftrace_update_code(int command)

0 commit comments

Comments
 (0)