Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp41933pxb; Wed, 20 Apr 2022 15:31:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyuTiOL74okDoE9ZOIFE2nClsWG5sC54zvdFv8JejhNKkYatWkZJIhyLO9mWZmWHURd9sGa X-Received: by 2002:a05:6a00:24cb:b0:50a:8151:9abc with SMTP id d11-20020a056a0024cb00b0050a81519abcmr15811565pfv.57.1650493882072; Wed, 20 Apr 2022 15:31:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650493882; cv=none; d=google.com; s=arc-20160816; b=RYwZpyRRbzGZhjqFw94nZT7kMMHssYYbVxuUIwlIsHaJHrY8fBWnnJgNTOi8nlwRkA KFmG5cwBDLSVjciu0G5HoLqMHrJ8Vcz+GPzvbOVGT8t5UrB77VDhaVE2ZsH6P83iUuT8 G1Gc+M0/fWQOq6pRqm/3PWvwpoeWBlYuQFzzHnW6hku6PzB6WIFDcQrcbasKDxLipccf YyjRMyhrtoZWYpAytx3Oj3LEfmjJX9HuHl1k0svnWgMpWYHoSxLfUDzQmTTiJID2k4jT vvem/pywOSTzsc1ZPbdZq3wOcirAU+bMs8T8Ociwimmjf23Fl1/S/RcV881DbkJABW6+ 2b2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=d8/aInVpTMR8pFMq5rEFohfPyYz9aGCEis23UFA/XnQ=; b=hOkkh6IlkcDVJJ0XUuQnJF7lrPGuqMR+7XfXwaua0aoFb5vMYyuiRWPICCSfZna/vv 6PU/GBT1NdVgEiEyi/Pex0wIoGfRuUb/XMuKO2XK1ciA4UeR6oUmR6NA9GsTwCk8L3cx uI5US5TEkfmfCS1Ke3cqYjmboypJO1nH3j5P7BW3bwLjN/ouRNMIKOK5F6D6MnN9M8SA rmqmMxyFwJT289MEAAPn6BasauoILifSE41eQOXR/6kPjbZ6Ok45iiTqD25cA93aZo5V jXEuthaPynGUQh+ame8BMmlao/jt3ZFTiXm5Odk6RYXY3Lw8MEbNiCE0YetKDSLuFe0X 7+2A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id om2-20020a17090b3a8200b001cdbf9d04d6si3255834pjb.178.2022.04.20.15.31.05; Wed, 20 Apr 2022 15:31:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358612AbiDTAqU (ORCPT + 99 others); Tue, 19 Apr 2022 20:46:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358616AbiDTAqD (ORCPT ); Tue, 19 Apr 2022 20:46:03 -0400 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5A8817A93; Tue, 19 Apr 2022 17:43:17 -0700 (PDT) Received: (Authenticated sender: joao@overdrivepizza.com) by mail.gandi.net (Postfix) with ESMTPSA id 964FC1BF205; Wed, 20 Apr 2022 00:43:08 +0000 (UTC) From: joao@overdrivepizza.com To: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Cc: joao@overdrivepizza.com, peterz@infradead.org, jpoimboe@redhat.com, andrew.cooper3@citrix.com, keescook@chromium.org, samitolvanen@google.com, mark.rutland@arm.com, hjl.tools@gmail.com, alyssa.milburn@linux.intel.com, ndesaulniers@google.com, gabriel.gomes@linux.intel.com, rick.p.edgecombe@intel.com Subject: [RFC PATCH 04/11] x86/module: Support FineIBT in modules Date: Tue, 19 Apr 2022 17:42:34 -0700 Message-Id: <20220420004241.2093-5-joao@overdrivepizza.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220420004241.2093-1-joao@overdrivepizza.com> References: <20220420004241.2093-1-joao@overdrivepizza.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joao Moreira Identify direct branch relocations targeting FineIBT hash check sequence and fix the offset to bypass it. Invoke objtool with fineibt flag for modules to fix in-module direct branches too. Signed-off-by: Joao Moreira Tinkered-from-patches-by: Peter Zijlstra --- arch/x86/kernel/module.c | 45 +++++++++++++++++++++++++++++++++++++--- scripts/Makefile.build | 1 + 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c index b98ffcf4d250..4afe71ae3e56 100644 --- a/arch/x86/kernel/module.c +++ b/arch/x86/kernel/module.c @@ -128,6 +128,41 @@ int apply_relocate(Elf32_Shdr *sechdrs, return 0; } #else /*X86_64*/ + +// shamelessly reshaped from PeterZ's IBT patches v2 +static inline void fineibt_branch_fix(void *loc, u64 *val) +{ +#ifdef CONFIG_X86_KERNEL_FINEIBT + const void *addr = (void *)(4 + *val); + union text_poke_insn text; + u32 insn; + + if (get_kernel_nofault(insn, addr) || !is_endbr(insn)) + return; + + if (get_kernel_nofault(text, addr+4) || text.opcode != SUB_INSN_OPCODE) + return; + + if (get_kernel_nofault(text, addr+11) || text.opcode != JE_INSN_OPCODE) + return; + + if (get_kernel_nofault(text, addr+13) || + text.opcode != CALL_INSN_OPCODE) + return; + + /* validate jmp.d32/call @ loc */ + if (WARN_ONCE(get_kernel_nofault(text, loc-1) || + (text.opcode != CALL_INSN_OPCODE && + text.opcode != JMP32_INSN_OPCODE), + "Unexpected code at: %pS\n", loc)) + return; + + DEBUGP("FineIBT fixed direct branch: %pS\n", addr); + + *val += 18; +#endif +} + static int __apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex, @@ -139,6 +174,7 @@ static int __apply_relocate_add(Elf64_Shdr *sechdrs, Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr; Elf64_Sym *sym; void *loc; + int type; u64 val; DEBUGP("Applying relocate section %u to %u\n", @@ -153,13 +189,14 @@ static int __apply_relocate_add(Elf64_Shdr *sechdrs, sym = (Elf64_Sym *)sechdrs[symindex].sh_addr + ELF64_R_SYM(rel[i].r_info); + type = ELF64_R_TYPE(rel[i].r_info); + DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n", - (int)ELF64_R_TYPE(rel[i].r_info), - sym->st_value, rel[i].r_addend, (u64)loc); + type, sym->st_value, rel[i].r_addend, (u64)loc); val = sym->st_value + rel[i].r_addend; - switch (ELF64_R_TYPE(rel[i].r_info)) { + switch (type) { case R_X86_64_NONE: break; case R_X86_64_64: @@ -185,6 +222,8 @@ static int __apply_relocate_add(Elf64_Shdr *sechdrs, case R_X86_64_PLT32: if (*(u32 *)loc != 0) goto invalid_relocation; + if (type == R_X86_64_PLT32) + fineibt_branch_fix(loc, &val); val -= (u64)loc; write(loc, &val, 4); #if 0 diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 9717e6f6fb31..d8862673b416 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -230,6 +230,7 @@ objtool_args = \ $(if $(CONFIG_UNWINDER_ORC),orc generate,check) \ $(if $(part-of-module), --module) \ $(if $(CONFIG_X86_KERNEL_IBT), --lto --ibt) \ + $(if $(CONFIG_X86_KERNEL_FINEIBT), --fineibt) \ $(if $(CONFIG_FRAME_POINTER),, --no-fp) \ $(if $(CONFIG_GCOV_KERNEL)$(CONFIG_LTO_CLANG), --no-unreachable)\ $(if $(CONFIG_RETPOLINE), --retpoline) \ -- 2.35.1