From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3E1B11852 for ; Sat, 30 Apr 2022 09:24:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description; bh=f7uipT1rxINXEEkDub0QoWVvJ2yKFuDILl1sxKUMthA=; b=o65ng6t9hVrv4G5R8uT1BmQbza OEVdEctaniRc7CYwLHJJ21J8qSz0f78GOX2bJVsS9yQ9VKEzog6lRi7xp6Nyf6Og7tqlhjggbVRDt G8RWrWE2uvgLaLhC067Mv/LG+meqiyIc/k3KQ7683CjYZHl9c6IrTX12bvr+QR0OrHNdhfIestin+ 6vCpJgNWys0ccAmJ671ed2wQHBWfhsJNNOLg+0jJN0Tfc7qErwEDgtNqU771QJA4nN/EF7HvZQOWW 6a3KLq6dCyTjM9Bew2t9jAW0QoOzJ48qol38CwKAUBI2bybOAPiSQ2wUM2gjX47s2QIDlylseCO1y O1i1kHXw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nkjL2-009sWc-MC; Sat, 30 Apr 2022 09:24:29 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id CFE21300473; Sat, 30 Apr 2022 11:24:27 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id B393F2028EFE6; Sat, 30 Apr 2022 11:24:27 +0200 (CEST) Date: Sat, 30 Apr 2022 11:24:27 +0200 From: Peter Zijlstra To: Sami Tolvanen Cc: linux-kernel@vger.kernel.org, Kees Cook , Josh Poimboeuf , x86@kernel.org, Catalin Marinas , Will Deacon , Mark Rutland , Nathan Chancellor , Nick Desaulniers , Joao Moreira , Sedat Dilek , Steven Rostedt , linux-hardening@vger.kernel.org, linux-arm-kernel@lists.infradead.org, llvm@lists.linux.dev Subject: Re: [RFC PATCH 21/21] x86: Add support for CONFIG_CFI_CLANG Message-ID: References: <20220429203644.2868448-1-samitolvanen@google.com> <20220429203644.2868448-22-samitolvanen@google.com> Precedence: bulk X-Mailing-List: llvm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20220429203644.2868448-22-samitolvanen@google.com> On Fri, Apr 29, 2022 at 01:36:44PM -0700, Sami Tolvanen wrote: > Add CONFIG_CFI_CLANG error handling and allow the config to be selected > on x86_64. Might be useful to have an example output of all ths somewhere, because unless I go build my own clang again, I can't tell from these patches what actual codegen looks like. Going from the below, I seem to be able to reverse engineer some of it: .long \signature int3 int3 my_func: ENDBR ... ret And then the callsites look like (clang *always* uses r11, right?): cmpl \signature, -6(%r11) je 1f ud2 1: call __x86_indirect_thunk_r11 > Signed-off-by: Sami Tolvanen > --- > arch/x86/Kconfig | 1 + > arch/x86/include/asm/linkage.h | 7 ++++++ > arch/x86/kernel/traps.c | 39 +++++++++++++++++++++++++++++++++- > 3 files changed, 46 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig > index b0142e01002e..01db5c5c4dde 100644 > --- a/arch/x86/Kconfig > +++ b/arch/x86/Kconfig > @@ -108,6 +108,7 @@ config X86 > select ARCH_SUPPORTS_PAGE_TABLE_CHECK if X86_64 > select ARCH_SUPPORTS_NUMA_BALANCING if X86_64 > select ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP if NR_CPUS <= 4096 > + select ARCH_SUPPORTS_CFI_CLANG if X86_64 > select ARCH_SUPPORTS_LTO_CLANG > select ARCH_SUPPORTS_LTO_CLANG_THIN > select ARCH_USE_BUILTIN_BSWAP > diff --git a/arch/x86/include/asm/linkage.h b/arch/x86/include/asm/linkage.h > index 85865f1645bd..d20acf5ebae3 100644 > --- a/arch/x86/include/asm/linkage.h > +++ b/arch/x86/include/asm/linkage.h > @@ -25,6 +25,13 @@ > #define RET ret > #endif > > +#ifdef CONFIG_CFI_CLANG > +#define __CFI_TYPE(name) \ > + .fill 10, 1, 0x90 ASM_NL \ > + .4byte __kcfi_typeid_##name ASM_NL \ > + .fill 2, 1, 0xcc > +#endif > + > #else /* __ASSEMBLY__ */ > > #ifdef CONFIG_SLS > diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c > index 1563fb995005..b9e46e6ed83b 100644 > --- a/arch/x86/kernel/traps.c > +++ b/arch/x86/kernel/traps.c > @@ -40,6 +40,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -295,6 +296,41 @@ static inline void handle_invalid_op(struct pt_regs *regs) > ILL_ILLOPN, error_get_trap_addr(regs)); > } > > +#ifdef CONFIG_CFI_CLANG > +void *arch_get_cfi_target(unsigned long addr, struct pt_regs *regs) > +{ > + char buffer[MAX_INSN_SIZE]; > + int offset; > + struct insn insn; > + unsigned long *target; Reverse xmas please.. > + > + /* > + * The expected CFI check instruction sequence: > + *   cmpl    , -6(%reg) ; 7 bytes > + * je .Ltmp1 ; 2 bytes > + * ud2 ; <- addr > + * .Ltmp1: > + * > + * Therefore, the target address is in a register that we can > + * decode from the cmpl instruction. > + */ > + if (copy_from_kernel_nofault(buffer, (void *)addr - 9, MAX_INSN_SIZE)) > + return NULL; > + if (insn_decode(&insn, buffer, MAX_INSN_SIZE, INSN_MODE_64)) > + return NULL; insn_decode_kernel() > + if (insn.opcode.value != 0x81) > + return NULL; That's not sufficient to uniquely identify cmp, you also need to look at the modrm to find r==7 I think. > + > + offset = insn_get_modrm_rm_off(&insn, regs); > + if (offset < 0) > + return NULL; > + > + target = (void *)regs + offset; > + > + return (void *)*target; > +} > +#endif > + > static noinstr bool handle_bug(struct pt_regs *regs) > { > bool handled = false; > @@ -312,7 +348,8 @@ static noinstr bool handle_bug(struct pt_regs *regs) > */ > if (regs->flags & X86_EFLAGS_IF) > raw_local_irq_enable(); > - if (report_bug(regs->ip, regs) == BUG_TRAP_TYPE_WARN) { > + if (report_bug(regs->ip, regs) == BUG_TRAP_TYPE_WARN || > + report_cfi(regs->ip, regs) == BUG_TRAP_TYPE_WARN) { This way you'll first get a BUG splat and then tack on the CFI thing. Seems a bit daft to have two splats. > regs->ip += LEN_UD2; > handled = true; > } > -- > 2.36.0.464.gb9c8b46e94-goog >