This is a note to let you know that I've just added the patch titled x86,objtool: Create .return_sites to the 5.18-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: x86-objtool-create-.return_sites.patch and it can be found in the queue-5.18 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From foo@baz Tue Jul 12 05:03:58 PM CEST 2022 From: Peter Zijlstra <peterz@xxxxxxxxxxxxx> Date: Tue, 14 Jun 2022 23:15:38 +0200 Subject: x86,objtool: Create .return_sites From: Peter Zijlstra <peterz@xxxxxxxxxxxxx> commit d9e9d2300681d68a775c28de6aa6e5290ae17796 upstream. Find all the return-thunk sites and record them in a .return_sites section such that the kernel can undo this. Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx> Signed-off-by: Borislav Petkov <bp@xxxxxxx> Reviewed-by: Josh Poimboeuf <jpoimboe@xxxxxxxxxx> Signed-off-by: Borislav Petkov <bp@xxxxxxx> Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@xxxxxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- tools/objtool/arch/x86/decode.c | 5 ++ tools/objtool/check.c | 74 ++++++++++++++++++++++++++++++++ tools/objtool/include/objtool/arch.h | 1 tools/objtool/include/objtool/elf.h | 1 tools/objtool/include/objtool/objtool.h | 1 tools/objtool/objtool.c | 1 6 files changed, 83 insertions(+) --- a/tools/objtool/arch/x86/decode.c +++ b/tools/objtool/arch/x86/decode.c @@ -787,3 +787,8 @@ bool arch_is_retpoline(struct symbol *sy { return !strncmp(sym->name, "__x86_indirect_", 15); } + +bool arch_is_rethunk(struct symbol *sym) +{ + return !strcmp(sym->name, "__x86_return_thunk"); +} --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -747,6 +747,52 @@ static int create_retpoline_sites_sectio return 0; } +static int create_return_sites_sections(struct objtool_file *file) +{ + struct instruction *insn; + struct section *sec; + int idx; + + sec = find_section_by_name(file->elf, ".return_sites"); + if (sec) { + WARN("file already has .return_sites, skipping"); + return 0; + } + + idx = 0; + list_for_each_entry(insn, &file->return_thunk_list, call_node) + idx++; + + if (!idx) + return 0; + + sec = elf_create_section(file->elf, ".return_sites", 0, + sizeof(int), idx); + if (!sec) { + WARN("elf_create_section: .return_sites"); + return -1; + } + + idx = 0; + list_for_each_entry(insn, &file->return_thunk_list, call_node) { + + int *site = (int *)sec->data->d_buf + idx; + *site = 0; + + if (elf_add_reloc_to_insn(file->elf, sec, + idx * sizeof(int), + R_X86_64_PC32, + insn->sec, insn->offset)) { + WARN("elf_add_reloc_to_insn: .return_sites"); + return -1; + } + + idx++; + } + + return 0; +} + static int create_ibt_endbr_seal_sections(struct objtool_file *file) { struct instruction *insn; @@ -1081,6 +1127,11 @@ __weak bool arch_is_retpoline(struct sym return false; } +__weak bool arch_is_rethunk(struct symbol *sym) +{ + return false; +} + #define NEGATIVE_RELOC ((void *)-1L) static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn) @@ -1248,6 +1299,18 @@ static void add_retpoline_call(struct ob annotate_call_site(file, insn, false); } +static void add_return_call(struct objtool_file *file, struct instruction *insn) +{ + /* + * Return thunk tail calls are really just returns in disguise, + * so convert them accordingly. + */ + insn->type = INSN_RETURN; + insn->retpoline_safe = true; + + list_add_tail(&insn->call_node, &file->return_thunk_list); +} + static bool same_function(struct instruction *insn1, struct instruction *insn2) { return insn1->func->pfunc == insn2->func->pfunc; @@ -1300,6 +1363,9 @@ static int add_jump_destinations(struct } else if (reloc->sym->retpoline_thunk) { add_retpoline_call(file, insn); continue; + } else if (reloc->sym->return_thunk) { + add_return_call(file, insn); + continue; } else if (insn->func) { /* * External sibling call or internal sibling call with @@ -2182,6 +2248,9 @@ static int classify_symbols(struct objto if (arch_is_retpoline(func)) func->retpoline_thunk = true; + if (arch_is_rethunk(func)) + func->return_thunk = true; + if (!strcmp(func->name, "__fentry__")) func->fentry = true; @@ -3935,6 +4004,11 @@ int check(struct objtool_file *file) if (ret < 0) goto out; warnings += ret; + + ret = create_return_sites_sections(file); + if (ret < 0) + goto out; + warnings += ret; } if (mcount) { --- a/tools/objtool/include/objtool/arch.h +++ b/tools/objtool/include/objtool/arch.h @@ -89,6 +89,7 @@ const char *arch_ret_insn(int len); int arch_decode_hint_reg(u8 sp_reg, int *base); bool arch_is_retpoline(struct symbol *sym); +bool arch_is_rethunk(struct symbol *sym); int arch_rewrite_retpolines(struct objtool_file *file); --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -57,6 +57,7 @@ struct symbol { u8 uaccess_safe : 1; u8 static_call_tramp : 1; u8 retpoline_thunk : 1; + u8 return_thunk : 1; u8 fentry : 1; u8 profiling_func : 1; struct list_head pv_target; --- a/tools/objtool/include/objtool/objtool.h +++ b/tools/objtool/include/objtool/objtool.h @@ -24,6 +24,7 @@ struct objtool_file { struct list_head insn_list; DECLARE_HASHTABLE(insn_hash, 20); struct list_head retpoline_call_list; + struct list_head return_thunk_list; struct list_head static_call_list; struct list_head mcount_loc_list; struct list_head endbr_list; --- a/tools/objtool/objtool.c +++ b/tools/objtool/objtool.c @@ -126,6 +126,7 @@ struct objtool_file *objtool_open_read(c INIT_LIST_HEAD(&file.insn_list); hash_init(file.insn_hash); INIT_LIST_HEAD(&file.retpoline_call_list); + INIT_LIST_HEAD(&file.return_thunk_list); INIT_LIST_HEAD(&file.static_call_list); INIT_LIST_HEAD(&file.mcount_loc_list); INIT_LIST_HEAD(&file.endbr_list); Patches currently in stable-queue which might be from peterz@xxxxxxxxxxxxx are queue-5.18/x86-sev-avoid-using-__x86_return_thunk.patch queue-5.18/kvm-vmx-prevent-rsb-underflow-before-vmenter.patch queue-5.18/x86-ftrace-use-alternative-ret-encoding.patch queue-5.18/objtool-re-add-unwind_hint_-save_restore.patch queue-5.18/x86-bugs-add-retbleed-ibpb.patch queue-5.18/x86-bugs-enable-stibp-for-jmp2ret.patch queue-5.18/x86-retpoline-cleanup-some-ifdefery.patch queue-5.18/kvm-vmx-flatten-__vmx_vcpu_run.patch queue-5.18/x86-kvm-vmx-make-noinstr-clean.patch queue-5.18/x86-retbleed-add-fine-grained-kconfig-knobs.patch queue-5.18/x86-cpu-amd-add-spectral-chicken.patch queue-5.18/kvm-vmx-fix-ibrs-handling-after-vmexit.patch queue-5.18/kvm-vmx-prevent-guest-rsb-poisoning-attacks-with-eibrs.patch queue-5.18/x86-vsyscall_emu-64-don-t-use-ret-in-vsyscall-emulation.patch queue-5.18/x86-add-magic-amd-return-thunk.patch queue-5.18/x86-bugs-keep-a-per-cpu-ia32_spec_ctrl-value.patch queue-5.18/x86-objtool-create-.return_sites.patch queue-5.18/x86-kvm-fix-setcc-emulation-for-return-thunks.patch queue-5.18/x86-retpoline-swizzle-retpoline-thunk.patch queue-5.18/x86-speculation-fix-firmware-entry-spec_ctrl-handling.patch queue-5.18/x86-speculation-add-spectre_v2-ibrs-option-to-support-kernel-ibrs.patch queue-5.18/x86-xen-add-untrain_ret.patch queue-5.18/x86-undo-return-thunk-damage.patch queue-5.18/x86-entry-avoid-very-early-ret.patch queue-5.18/x86-entry-move-push_and_clear_regs-back-into-error_entry.patch queue-5.18/x86-speculation-fill-rsb-on-vmexit-for-ibrs.patch queue-5.18/objtool-add-entry-unret-validation.patch queue-5.18/kvm-vmx-convert-launched-argument-to-flags.patch queue-5.18/x86-bpf-use-alternative-ret-encoding.patch queue-5.18/x86-common-stamp-out-the-stepping-madness.patch queue-5.18/x86-bugs-split-spectre_v2_select_mitigation-and-spectre_v2_user_select_mitigation.patch queue-5.18/x86-bugs-report-intel-retbleed-vulnerability.patch queue-5.18/x86-cpufeatures-move-retpoline-flags-to-word-11.patch queue-5.18/x86-speculation-fix-spec_ctrl-write-on-smt-state-change.patch queue-5.18/x86-retpoline-use-mfunction-return.patch queue-5.18/x86-xen-rename-sys-entry-points.patch queue-5.18/x86-bugs-optimize-spec_ctrl-msr-writes.patch queue-5.18/x86-bugs-report-amd-retbleed-vulnerability.patch queue-5.18/x86-static_call-use-alternative-ret-encoding.patch queue-5.18/x86-speculation-fix-rsb-filling-with-config_retpoline-n.patch queue-5.18/x86-use-return-thunk-in-asm-code.patch queue-5.18/intel_idle-disable-ibrs-during-long-idle.patch queue-5.18/x86-entry-remove-skip_r11rcx.patch queue-5.18/x86-speculation-use-cached-host-spec_ctrl-value-for-guest-entry-exit.patch queue-5.18/x86-bugs-add-amd-retbleed-boot-parameter.patch queue-5.18/x86-entry-add-kernel-ibrs-implementation.patch queue-5.18/objtool-treat-.text.__x86.-as-noinstr.patch queue-5.18/objtool-update-retpoline-validation.patch