From: Masami Hiramatsu <mhiramat@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: x86@kernel.org, joao@overdrivepizza.com, hjl.tools@gmail.com,
jpoimboe@redhat.com, andrew.cooper3@citrix.com,
linux-kernel@vger.kernel.org, ndesaulniers@google.com,
keescook@chromium.org, samitolvanen@google.com,
mark.rutland@arm.com, alyssa.milburn@intel.com, mbenes@suse.cz,
rostedt@goodmis.org, mhiramat@kernel.org,
alexei.starovoitov@gmail.com
Subject: Re: [PATCH v3 19/39] x86/ibt,kprobes: Cure sym+0 equals fentry woes
Date: Mon, 7 Mar 2022 18:44:34 +0900 [thread overview]
Message-ID: <20220307184434.4b7e5d83c9d16a983adedce5@kernel.org> (raw)
In-Reply-To: <20220303112826.104222438@infradead.org>
On Thu, 03 Mar 2022 12:23:40 +0100
Peter Zijlstra <peterz@infradead.org> wrote:
> Rework _kprobe_addr() to only ever do 2 kallsyms calls and require
> only a single architecture/weak function -- although currently powerpc
> still uses 2.
OK, and we need to note here that if the kernel enables this
X86_KERNEL_IBT, the kprobe address is possible to be sym+4 because
kprobes will skip the ENDBR.
Others looks good to me.
Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
Thank you,
>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
> arch/powerpc/kernel/kprobes.c | 34 +++++++++++++--------
> arch/x86/kernel/kprobes/core.c | 28 ++++++++++++++---
> include/linux/kprobes.h | 3 +
> kernel/kprobes.c | 66 ++++++++++++++++++++++++++++++++---------
> 4 files changed, 98 insertions(+), 33 deletions(-)
>
> --- a/arch/powerpc/kernel/kprobes.c
> +++ b/arch/powerpc/kernel/kprobes.c
> @@ -105,6 +105,27 @@ kprobe_opcode_t *kprobe_lookup_name(cons
> return addr;
> }
>
> +static bool arch_kprobe_on_func_entry(unsigned long offset)
> +{
> +#ifdef PPC64_ELF_ABI_v2
> +#ifdef CONFIG_KPROBES_ON_FTRACE
> + return offset <= 16;
> +#else
> + return offset <= 8;
> +#endif
> +#else
> + return !offset;
> +#endif
> +}
> +
> +/* XXX try and fold the magic of kprobe_lookup_name() in this */
> +kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offset,
> + bool *on_func_entry)
> +{
> + *on_func_entry = arch_kprobe_on_func_entry(offset);
> + return (kprobe_opcode_t *)(addr + offset);
Just for make sure,
> +}
> +
> void *alloc_insn_page(void)
> {
> void *page;
> @@ -218,19 +239,6 @@ static nokprobe_inline void set_current_
> kcb->kprobe_saved_msr = regs->msr;
> }
>
> -bool arch_kprobe_on_func_entry(unsigned long offset)
> -{
> -#ifdef PPC64_ELF_ABI_v2
> -#ifdef CONFIG_KPROBES_ON_FTRACE
> - return offset <= 16;
> -#else
> - return offset <= 8;
> -#endif
> -#else
> - return !offset;
> -#endif
> -}
> -
> void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
> {
> ri->ret_addr = (kprobe_opcode_t *)regs->link;
> --- a/arch/x86/kernel/kprobes/core.c
> +++ b/arch/x86/kernel/kprobes/core.c
> @@ -52,6 +52,7 @@
> #include <asm/insn.h>
> #include <asm/debugreg.h>
> #include <asm/set_memory.h>
> +#include <asm/ibt.h>
>
> #include "common.h"
>
> @@ -197,13 +198,14 @@ __recover_probed_insn(kprobe_opcode_t *b
>
> kp = get_kprobe((void *)addr);
> faddr = ftrace_location(addr);
> +
> /*
> - * Addresses inside the ftrace location are refused by
> - * arch_check_ftrace_location(). Something went terribly wrong
> - * if such an address is checked here.
> + * In case addr maps to sym+0 ftrace_location() might return something
> + * other than addr. In that case consider it the same as !faddr.
> */
> - if (WARN_ON(faddr && faddr != addr))
> - return 0UL;
> + if (faddr && faddr != addr)
> + faddr = 0;
> +
> /*
> * Use the current code if it is not modified by Kprobe
> * and it cannot be modified by ftrace.
> @@ -301,6 +303,22 @@ static int can_probe(unsigned long paddr
> return (addr == paddr);
> }
>
> +/* If the x86 support IBT (ENDBR) it must be skipped. */
> +kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offset,
> + bool *on_func_entry)
> +{
> + if (is_endbr(*(u32 *)addr)) {
> + *on_func_entry = !offset || offset == 4;
> + if (*on_func_entry)
> + offset = 4;
> +
> + } else {
> + *on_func_entry = !offset;
> + }
> +
> + return (kprobe_opcode_t *)(addr + offset);
> +}
> +
> /*
> * Copy an instruction with recovering modified instruction by kprobes
> * and adjust the displacement if the instruction uses the %rip-relative
> --- a/include/linux/kprobes.h
> +++ b/include/linux/kprobes.h
> @@ -265,7 +265,6 @@ extern int arch_init_kprobes(void);
> extern void kprobes_inc_nmissed_count(struct kprobe *p);
> extern bool arch_within_kprobe_blacklist(unsigned long addr);
> extern int arch_populate_kprobe_blacklist(void);
> -extern bool arch_kprobe_on_func_entry(unsigned long offset);
> extern int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
>
> extern bool within_kprobe_blacklist(unsigned long addr);
> @@ -384,6 +383,8 @@ static inline struct kprobe_ctlblk *get_
> }
>
> kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
> +kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offset, bool *on_func_entry);
> +
> int register_kprobe(struct kprobe *p);
> void unregister_kprobe(struct kprobe *p);
> int register_kprobes(struct kprobe **kps, int num);
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -1489,24 +1489,68 @@ bool within_kprobe_blacklist(unsigned lo
> }
>
> /*
> + * arch_adjust_kprobe_addr - adjust the address
> + * @addr: symbol base address
> + * @offset: offset within the symbol
> + * @on_func_entry: was this @addr+@offset on the function entry
> + *
> + * Typically returns @addr + @offset, except for special cases where the
> + * function might be prefixed by a CFI landing pad, in that case any offset
> + * inside the landing pad is mapped to the first 'real' instruction of the
> + * symbol.
> + *
> + * Specifically, for things like IBT/BTI, skip the resp. ENDBR/BTI.C
> + * instruction at +0.
> + */
> +kprobe_opcode_t *__weak arch_adjust_kprobe_addr(unsigned long addr,
> + unsigned long offset,
> + bool *on_func_entry)
> +{
> + *on_func_entry = !offset;
> + return (kprobe_opcode_t *)(addr + offset);
> +}
> +
> +/*
> * If 'symbol_name' is specified, look it up and add the 'offset'
> * to it. This way, we can specify a relative address to a symbol.
> * This returns encoded errors if it fails to look up symbol or invalid
> * combination of parameters.
> */
> -static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr,
> - const char *symbol_name, unsigned int offset)
> +static kprobe_opcode_t *
> +_kprobe_addr(kprobe_opcode_t *addr, const char *symbol_name,
> + unsigned long offset, bool *on_func_entry)
> {
> if ((symbol_name && addr) || (!symbol_name && !addr))
> goto invalid;
>
> if (symbol_name) {
> + /*
> + * Input: @sym + @offset
> + * Output: @addr + @offset
> + *
> + * NOTE: kprobe_lookup_name() does *NOT* fold the offset
> + * argument into it's output!
> + */
> addr = kprobe_lookup_name(symbol_name, offset);
> if (!addr)
> return ERR_PTR(-ENOENT);
> }
>
> - addr = (kprobe_opcode_t *)(((char *)addr) + offset);
> + /*
> + * So here we have @addr + @offset, displace it into a new
> + * @addr' + @offset' where @addr' is the symbol start address.
> + */
> + addr = (void *)addr + offset;
> + if (!kallsyms_lookup_size_offset((unsigned long)addr, NULL, &offset))
> + return ERR_PTR(-ENOENT);
> + addr = (void *)addr - offset;
> +
> + /*
> + * Then ask the architecture to re-combine them, taking care of
> + * magical function entry details while telling us if this was indeed
> + * at the start of the function.
> + */
> + addr = arch_adjust_kprobe_addr((unsigned long)addr, offset, on_func_entry);
> if (addr)
> return addr;
>
> @@ -1516,7 +1560,8 @@ static kprobe_opcode_t *_kprobe_addr(kpr
>
> static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
> {
> - return _kprobe_addr(p->addr, p->symbol_name, p->offset);
> + bool on_func_entry;
> + return _kprobe_addr(p->addr, p->symbol_name, p->offset, &on_func_entry);
> }
>
> /*
> @@ -2047,11 +2092,6 @@ static int pre_handler_kretprobe(struct
> }
> NOKPROBE_SYMBOL(pre_handler_kretprobe);
>
> -bool __weak arch_kprobe_on_func_entry(unsigned long offset)
> -{
> - return !offset;
> -}
> -
> /**
> * kprobe_on_func_entry() -- check whether given address is function entry
> * @addr: Target address
> @@ -2067,15 +2107,13 @@ bool __weak arch_kprobe_on_func_entry(un
> */
> int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset)
> {
> - kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset);
> + bool on_func_entry;
> + kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset, &on_func_entry);
>
> if (IS_ERR(kp_addr))
> return PTR_ERR(kp_addr);
>
> - if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset))
> - return -ENOENT;
> -
> - if (!arch_kprobe_on_func_entry(offset))
> + if (!on_func_entry)
> return -EINVAL;
>
> return 0;
>
>
--
Masami Hiramatsu <mhiramat@kernel.org>
next prev parent reply other threads:[~2022-03-07 10:11 UTC|newest]
Thread overview: 64+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-03 11:23 [PATCH v3 00/39] x86: Kernel IBT Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 01/39] static_call: Avoid building empty .static_call_sites Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 02/39] x86/module: Fix the paravirt vs alternative order Peter Zijlstra
2022-03-08 13:58 ` [tip: x86/urgent] " tip-bot2 for Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 03/39] objtool: Add --dry-run Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 04/39] x86/ibt: Base IBT bits Peter Zijlstra
2022-03-07 11:17 ` Peter Zijlstra
2022-03-07 12:25 ` Joao Moreira
2022-03-07 18:19 ` Nick Desaulniers
2022-03-03 11:23 ` [PATCH v3 05/39] x86/ibt: Add ANNOTATE_NOENDBR Peter Zijlstra
2022-03-04 18:59 ` Josh Poimboeuf
2022-03-04 20:22 ` Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 06/39] x86/text-patching: Make text_gen_insn() play nice with ANNOTATE_NOENDBR Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 07/39] x86/ibt,paravirt: Use text_gen_insn() for paravirt_patch() Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 08/39] x86/entry: Cleanup PARAVIRT Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 09/39] x86/entry,xen: Early rewrite of restore_regs_and_return_to_kernel() Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 10/39] x86/ibt,xen: Sprinkle the ENDBR Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 11/39] x86/ibt,entry: Sprinkle ENDBR dust Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 12/39] x86/linkage: Add ENDBR to SYM_FUNC_START*() Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 13/39] x86/ibt,paravirt: Sprinkle ENDBR Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 14/39] x86/ibt,crypto: Add ENDBR for the jump-table entries Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 15/39] x86/ibt,kvm: Add ENDBR to fastops Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 16/39] x86/ibt,ftrace: Search for __fentry__ location Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 17/39] x86/livepatch: Validate " Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 18/39] x86/ibt,ftrace: Make function-graph play nice Peter Zijlstra
2022-03-04 17:51 ` Josh Poimboeuf
2022-03-04 19:48 ` Peter Zijlstra
2022-03-04 21:03 ` Josh Poimboeuf
2022-03-04 21:44 ` Peter Zijlstra
2022-03-04 22:05 ` Josh Poimboeuf
2022-03-04 23:08 ` David Laight
2022-03-03 11:23 ` [PATCH v3 19/39] x86/ibt,kprobes: Cure sym+0 equals fentry woes Peter Zijlstra
2022-03-07 9:44 ` Masami Hiramatsu [this message]
2022-03-03 11:23 ` [PATCH v3 20/39] x86/ibt,bpf: Add ENDBR instructions to prologue and trampoline Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 21/39] x86/ibt,ftrace: Add ENDBR to samples/ftrace Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 22/39] x86/ibt: Add IBT feature, MSR and #CP handling Peter Zijlstra
2022-03-04 17:57 ` Josh Poimboeuf
2022-03-04 20:38 ` Peter Zijlstra
2022-03-04 18:07 ` Josh Poimboeuf
2022-03-04 20:39 ` Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 23/39] x86/alternative: Simplify int3_selftest_ip Peter Zijlstra
2022-03-04 18:02 ` Josh Poimboeuf
2022-03-04 18:09 ` Josh Poimboeuf
2022-03-03 11:23 ` [PATCH v3 24/39] x86/ibt: Disable IBT around firmware Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 25/39] x86/bugs: Disable Retpoline when IBT Peter Zijlstra
2022-03-04 17:39 ` Josh Poimboeuf
2022-03-03 11:23 ` [PATCH v3 26/39] x86/ibt: Annotate text references Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 27/39] x86/ibt,ftrace: Annotate ftrace code patching Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 28/39] x86/ibt,sev: Annotations Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 29/39] x86/ibt: Dont generate ENDBR in .discard.text Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 30/39] x86/ibt: Ensure module init/exit points have references Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 31/39] objtool: Rename --duplicate to --lto Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 32/39] Kbuild: Allow whole module objtool runs Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 33/39] objtool: Read the NOENDBR annotation Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 34/39] objtool: Have WARN_FUNC fall back to sym+off Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 35/39] objtool: Add IBT/ENDBR decoding Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 36/39] objtool: Validate IBT assumptions Peter Zijlstra
2022-03-03 11:23 ` [PATCH v3 37/39] objtool: Optionally WARN about unused ANNOTATE_NOENDBR Peter Zijlstra
2022-03-04 18:27 ` Josh Poimboeuf
2022-03-03 11:23 ` [PATCH v3 38/39] objtool: Find unused ENDBR instructions Peter Zijlstra
2022-03-03 11:24 ` [PATCH v3 39/39] x86/alternative: Use .ibt_endbr_seal to seal indirect calls Peter Zijlstra
2022-03-04 19:09 ` [PATCH v3 00/39] x86: Kernel IBT Josh Poimboeuf
2022-03-04 20:22 ` Peter Zijlstra
2022-03-04 21:39 ` Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220307184434.4b7e5d83c9d16a983adedce5@kernel.org \
--to=mhiramat@kernel.org \
--cc=alexei.starovoitov@gmail.com \
--cc=alyssa.milburn@intel.com \
--cc=andrew.cooper3@citrix.com \
--cc=hjl.tools@gmail.com \
--cc=joao@overdrivepizza.com \
--cc=jpoimboe@redhat.com \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mbenes@suse.cz \
--cc=ndesaulniers@google.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=samitolvanen@google.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®