All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
	X86 ML <x86@kernel.org>,
	joao@overdrivepizza.com, hjl.tools@gmail.com,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	LKML <linux-kernel@vger.kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Kees Cook <keescook@chromium.org>,
	Sami Tolvanen <samitolvanen@google.com>,
	Mark Rutland <mark.rutland@arm.com>,
	alyssa.milburn@intel.com, Miroslav Benes <mbenes@suse.cz>,
	Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>, bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH v4 00/45] x86: Kernel IBT
Date: Tue, 15 Mar 2022 10:00:43 +0100	[thread overview]
Message-ID: <20220315090043.GB8939@worktop.programming.kicks-ass.net> (raw)
In-Reply-To: <20220314204402.rpd5hqzzev4ugtdt@apollo>

On Tue, Mar 15, 2022 at 02:14:02AM +0530, Kumar Kartikeya Dwivedi wrote:

> [ Note: I have no experience with trampoline code or IBT so what follows might
> 	be incorrect. ]
> 
> In case of fexit and fmod_ret, we call original function (but skip
> X86_PATCH_SIZE bytes), with ENDBR we must also skip those 4 bytes, but in some
> cases like bpf_fentry_test1, for which this test has fmod_ret prog, compiler
> (gcc 11) emits endbr64, but not for do_init_module, for which we do fexit.
> 
> This means for do_init_module module, orig_call += X86_PATCH_SIZE +
> ENDBR_INSN_SIZE would skip more bytes than needed to emit call to original
> function, which explains why I was seeing crash in the middle of
> 'mov edx, 0x10' instruction.
> 
> The diff below fixes the problem for me, and allows the test to pass.
> 
> diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
> index b98e1c95bcc4..760c9a3c075f 100644
> --- a/arch/x86/net/bpf_jit_comp.c
> +++ b/arch/x86/net/bpf_jit_comp.c
> @@ -2031,11 +2031,14 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i
> 
>         ip_off = stack_size;
> 
> -       if (flags & BPF_TRAMP_F_SKIP_FRAME)
> +       if (flags & BPF_TRAMP_F_SKIP_FRAME) {
>                 /* skip patched call instruction and point orig_call to actual
>                  * body of the kernel function.
>                  */
> -               orig_call += X86_PATCH_SIZE + ENDBR_INSN_SIZE;
> +               if (is_endbr(*(u32 *)orig_call))
> +                       orig_call += ENDBR_INSN_SIZE;
> +               orig_call += X86_PATCH_SIZE;
> +       }
> 
>         prog = image;

Hmm, so I was under the impression that this was targeting the NOP from
emit_prologue(), and that has an unconditional ENDBR. If this is instead
targeting the 'start of random kernel function' then yes, what you
propose will work.

(obviously, once we go do more complicated CFI schemes, all this needs
revisiting yet again).

I don't seem able to run this mod_race test, it keeps saying:

  tgl-build# ./test_progs -v -t mod_race
  bpf_testmod.ko is already unloaded.
  Loading bpf_testmod.ko...
  Successfully loaded bpf_testmod.ko.
  Summary: 0/0 PASSED, 0 SKIPPED, 0 FAILED
  Successfully unloaded bpf_testmod.ko.

Which I'm taking to mean I'm doing it wrong... so I can't immediately
verify, but your proposal looks sane so I'll fold it in.

Thanks!

  reply	other threads:[~2022-03-15  9:01 UTC|newest]

Thread overview: 193+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-08 15:30 [PATCH v4 00/45] x86: Kernel IBT Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 01/45] static_call: Avoid building empty .static_call_sites Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 02/45] objtool: Add --dry-run Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 03/45] objtool: Default ignore INT3 for unreachable Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 04/45] objtool,efi: Update __efi64_thunk annotation Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-09  8:35   ` [PATCH v4 04/45] " Miroslav Benes
2022-03-15 10:44   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 05/45] objtool: Have WARN_FUNC fall back to sym+off Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 06/45] x86/ibt: Base IBT bits Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 07/45] x86/ibt: Add ANNOTATE_NOENDBR Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 08/45] x86/text-patching: Make text_gen_insn() play nice with ANNOTATE_NOENDBR Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 09/45] x86/ibt,paravirt: Use text_gen_insn() for paravirt_patch() Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 10/45] x86/entry: Cleanup PARAVIRT Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 11/45] x86/entry,xen: Early rewrite of restore_regs_and_return_to_kernel() Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 12/45] x86/ibt,xen: Sprinkle the ENDBR Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 13/45] x86/ibt,entry: Sprinkle ENDBR dust Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 14/45] x86/linkage: Add ENDBR to SYM_FUNC_START*() Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 15/45] x86/ibt,paravirt: Sprinkle ENDBR Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 16/45] x86/ibt,crypto: Add ENDBR for the jump-table entries Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 17/45] x86/ibt,kvm: Add ENDBR to fastops Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 18/45] x86/ibt,ftrace: Search for __fentry__ location Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 19/45] x86/livepatch: Validate " Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-09  8:58   ` [PATCH v4 19/45] " Miroslav Benes
2022-03-15 10:44   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 20/45] x86/ibt,ftrace: Make function-graph play nice Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 21/45] x86/ibt,kprobes: Cure sym+0 equals fentry woes Peter Zijlstra
2022-03-09  7:55   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 22/45] x86/ibt,bpf: Add ENDBR instructions to prologue and trampoline Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:44   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 23/45] x86/ibt,ftrace: Add ENDBR to samples/ftrace Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 24/45] x86/ibt: Add IBT feature, MSR and #CP handling Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-09 13:56   ` [PATCH v4 24/45] " Andrew Cooper
2022-03-15 10:43   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 25/45] x86/ibt,kexec: Disable CET on kexec Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 26/45] x86/alternative: Simplify int3_selftest_ip Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 27/45] x86/ibt: Disable IBT around firmware Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 28/45] x86/bugs: Disable Retpoline when IBT Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 29/45] x86/ibt: Annotate text references Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-22  4:43   ` [PATCH v4 29/45] " Masami Hiramatsu
2022-03-22  4:45     ` Alexei Starovoitov
2022-03-08 15:30 ` [PATCH v4 30/45] x86/ibt,ftrace: Annotate ftrace code patching Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 31/45] x86/ibt,sev: Annotations Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 32/45] x86/ibt: Dont generate ENDBR in .discard.text Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 33/45] x86/ibt: Ensure module init/exit points have references Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 34/45] objtool: Rename --duplicate to --lto Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 35/45] objtool: Ignore extra-symbol code Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 36/45] x86: Mark stop_this_cpu() __noreturn Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 37/45] exit: Mark do_group_exit() __noreturn Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 38/45] objtool: Rework ASM_REACHABLE Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 39/45] x86: Annotate call_on_stack() Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 40/45] Kbuild: Allow whole module objtool runs Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 41/45] objtool: Read the NOENDBR annotation Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 42/45] objtool: Add IBT/ENDBR decoding Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 43/45] objtool: Validate IBT assumptions Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 44/45] objtool: Find unused ENDBR instructions Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-15 15:39     ` David Laight
2022-03-17 22:22       ` Josh Poimboeuf
2022-03-18 10:49         ` Peter Zijlstra
2022-03-08 15:30 ` [PATCH v4 45/45] x86/alternative: Use .ibt_endbr_seal to seal indirect calls Peter Zijlstra
2022-03-09  7:54   ` [tip: x86/core] " tip-bot2 for Peter Zijlstra
2022-03-15 10:43   ` tip-bot2 for Peter Zijlstra
2022-03-08 20:00 ` [PATCH v4 00/45] x86: Kernel IBT Alexei Starovoitov
2022-03-08 22:01   ` Peter Zijlstra
2022-03-08 22:32     ` Peter Zijlstra
2022-03-09  1:02       ` Peter Zijlstra
2022-03-09 19:09         ` Alexei Starovoitov
2022-03-10  9:35           ` Peter Zijlstra
2022-03-10 13:47             ` Peter Zijlstra
2022-03-10 14:37               ` Steven Rostedt
2022-03-11 15:23                 ` Peter Zijlstra
2022-03-10 16:29               ` Peter Zijlstra
2022-03-11 10:40                 ` Peter Zijlstra
2022-03-11 17:09                   ` Alexei Starovoitov
2022-03-12 15:44                     ` Peter Zijlstra
2022-03-13  1:33                       ` Alexei Starovoitov
2022-03-13  8:52                         ` Peter Zijlstra
2022-03-14 14:59                           ` Peter Zijlstra
2022-03-15  8:15                             ` Peter Zijlstra
2022-03-15 16:28                               ` Masahiro Yamada
2022-03-17 19:44                                 ` Peter Zijlstra
2022-03-18  2:07                                   ` David Laight
2022-03-17 18:15                               ` Masahiro Yamada
2022-03-17 19:52                                 ` Peter Zijlstra
2022-03-18 15:36                                 ` [tip: x86/core] kbuild: Fixup the IBT kbuild changes tip-bot2 for Peter Zijlstra
2022-03-18 16:15                                   ` Masahiro Yamada
2022-03-22 20:24                                 ` tip-bot2 for Peter Zijlstra
2022-03-15 16:26                             ` [PATCH v4 00/45] x86: Kernel IBT Masahiro Yamada
2022-03-17 19:36                               ` Peter Zijlstra
2022-03-14 15:33                           ` Peter Zijlstra
2022-03-15 10:43                             ` [tip: x86/core] x86: Annotate idtentry_df() tip-bot2 for Peter Zijlstra
2022-03-15 10:43                             ` [tip: x86/core] x86,objtool: Move the ASM_REACHABLE annotation to objtool.h tip-bot2 for Peter Zijlstra
2022-03-15 10:43                             ` [tip: x86/core] x86: Mark __invalid_creds() __noreturn tip-bot2 for Peter Zijlstra
2022-03-14 20:44                         ` [PATCH v4 00/45] x86: Kernel IBT Kumar Kartikeya Dwivedi
2022-03-15  9:00                           ` Peter Zijlstra [this message]
2022-03-15 10:05                             ` Kumar Kartikeya Dwivedi
2022-03-15 10:07                             ` Peter Zijlstra
2022-03-15 10:39                               ` Peter Zijlstra
2022-03-16  9:35                             ` Peter Zijlstra
2022-03-16 11:12                               ` Kumar Kartikeya Dwivedi
2022-03-15 18:26                           ` Alexei Starovoitov
2022-03-17 20:27                             ` Peter Zijlstra
2022-03-11 14:40               ` [tip: x86/core] x86,ftrace: Fix modify_ftrace_direct() tip-bot2 for Peter Zijlstra
2022-03-10  0:30       ` [PATCH v4 00/45] x86: Kernel IBT Nick Desaulniers
2022-03-10  9:05         ` Peter Zijlstra
2022-03-10  9:22           ` David Laight
2022-03-10 10:16             ` Peter Zijlstra
2022-03-10 20:49               ` Nick Desaulniers
2022-03-11 14:40               ` [tip: x86/core] x86: Fix {int3,ibt}_selftest() vs LTO tip-bot2 for Peter Zijlstra
2022-03-08 20:06 ` [PATCH v4 00/45] x86: Kernel IBT Josh Poimboeuf
2022-03-09  6:57   ` Josh Poimboeuf
2022-03-09 13:37     ` David Laight
2022-03-10 14:27       ` Peter Zijlstra
2022-03-09 12:51 ` Miroslav Benes

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=20220315090043.GB8939@worktop.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=alexei.starovoitov@gmail.com \
    --cc=alyssa.milburn@intel.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --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=memxor@gmail.com \
    --cc=mhiramat@kernel.org \
    --cc=ndesaulniers@google.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.