Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp1531416pxb; Fri, 18 Feb 2022 09:37:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJyG8cg4qttv9/YKfqimn+Iprz4bdA1a2gYaqSs7bR+IQUyTwvMKaRf5N9aKpyXZR9ps0eU9 X-Received: by 2002:a17:906:52c7:b0:6ce:a880:50a3 with SMTP id w7-20020a17090652c700b006cea88050a3mr7111898ejn.437.1645205868641; Fri, 18 Feb 2022 09:37:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645205868; cv=none; d=google.com; s=arc-20160816; b=jfWj0qBox2uooHVZAl4d7+jyZWV6vfGZntAT5igiwO0NTiATyQc/28yFzlirA5/7pZ HYfktk90mLWVVYe/EhoO56HFUE9dL2YL1OIbVbX6xOgqLnowkLQ1X8EdMAZDyjEuLJcZ Ob4ENmV9Vi+YBJS0LC0Wu6mA4I9taxZFZBng2gszdCydZfPvAMmw69zHZFXUgI270svP J22Et+cOdK0O5lsz79IpG3molB1Cj/A6w2l26h2XCh5AIEm0PaeqrLzEM1Xr8hhfewP7 sqCSHP/+YBi2uPyReeFq0lhGf+BAAyPHR/j7peZw7P+qvh64XIoteiu1X+sBlJ+3/0DR SVCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:subject:cc:to:from:date :user-agent:message-id:dkim-signature; bh=sgeqFPf4iiUQcYgsDEuMEVfklmLydc0q++yO3hJu8EU=; b=F6Z6IUiF7PtWRS/pQqvzSwoAuzTEurQ/QJvcCZgCzuLGYB34p35wUWY6doX1riYN3q LNeCZ8Nk2cGbC9tGbVSyJZyuERNit/FjAan19Uvzpy8JEB/lxFxcqWuU87hRFkuupm75 VWugLDCleZXDzQIzGxV2XHxZID1VKrztU0pinXfYBNDhaOIQLxnKNyn8I/7MdwlDY1ov yMXQM1OOeX5V3f4wNE8TbbWDP/8gEm0a4QXPKXlnt9zqdN2X+uRa967n42nZ1k4wvjCJ 0QkjZZHg+JbmpigqjQRJx+xW9nRG94Zl/W2YNiuPA4XdFWW/DdvIWyIkHvL0/M4EEjhg RlFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=BNPmhSiz; 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 ky10si4034421ejc.745.2022.02.18.09.37.23; Fri, 18 Feb 2022 09:37:48 -0800 (PST) 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; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=BNPmhSiz; 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 S238320AbiBRRQI (ORCPT + 99 others); Fri, 18 Feb 2022 12:16:08 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:43302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238217AbiBRRPQ (ORCPT ); Fri, 18 Feb 2022 12:15:16 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69F694C409 for ; Fri, 18 Feb 2022 09:14:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=sgeqFPf4iiUQcYgsDEuMEVfklmLydc0q++yO3hJu8EU=; b=BNPmhSizei7KamWTrucufg2DV6 Xz0oo0FcAYJ4mB/hGuO8uNpYVaKbkJDsY3l3t9vYh/Ju2eCUG+6utIKmluRPT1ABskKlTJCaIZe46 B3kKTujBP0318rkxkuq3H6XsDiZ0ESQV9l+r7WSbyWpVONp67TQkQFSieFQWTiEGPjcGcqQ7wS62w 2ZhJGXZo4radMEKJCIZsNNg5Qussvt5j4+DGf7PfwS3J2rg9QOSfLttHwG02OZ9KMTtrbDNYX1MiR wmRa2HOmJclSG5u3GU7dYek/hNk2qoLpSTAlvhsshCx4XAcKv/ZjmB1wLUw79fuEjZAq+j7OBrC3i k7sSTm+Q==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nL6q9-00Gl8Z-QP; Fri, 18 Feb 2022 17:14:41 +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 B7AB83009C4; Fri, 18 Feb 2022 18:14:39 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id 14CD6203C2B2F; Fri, 18 Feb 2022 18:14:39 +0100 (CET) Message-ID: <20220218171408.867389898@infradead.org> User-Agent: quilt/0.66 Date: Fri, 18 Feb 2022 17:49:07 +0100 From: Peter Zijlstra To: x86@kernel.org, joao@overdrivepizza.com, hjl.tools@gmail.com, jpoimboe@redhat.com, andrew.cooper3@citrix.com Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, ndesaulniers@google.com, keescook@chromium.org, samitolvanen@google.com, mark.rutland@arm.com, alyssa.milburn@intel.com Subject: [PATCH 05/29] x86: Base IBT bits References: <20220218164902.008644515@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,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 Add Kconfig, Makefile and basic instruction support for x86 IBT. TODO: clang Signed-off-by: Peter Zijlstra (Intel) --- arch/x86/Kconfig | 15 ++++++++++++ arch/x86/Makefile | 5 +++- arch/x86/include/asm/ibt.h | 53 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1861,6 +1861,21 @@ config X86_UMIP specific cases in protected and virtual-8086 modes. Emulated results are dummy. +config CC_HAS_IBT + # GCC >= 9 and binutils >= 2.29 + # Retpoline check to work around https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93654 + def_bool $(cc-option, -fcf-protection=branch -mindirect-branch-register) && $(as-instr,endbr64) + +config X86_IBT + prompt "Indirect Branch Tracking" + bool + depends on X86_64 && CC_HAS_IBT + help + Build the kernel with support for Indirect Branch Tracking, a + hardware supported CFI scheme. Any indirect call must land on + an ENDBR instruction, as such, the compiler will litter the + code with them to make this happen. + config X86_INTEL_MEMORY_PROTECTION_KEYS prompt "Memory Protection Keys" def_bool y --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -62,8 +62,11 @@ export BITS # KBUILD_CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -# Intel CET isn't enabled in the kernel +ifeq ($(CONFIG_X86_IBT),y) +KBUILD_CFLAGS += $(call cc-option,-fcf-protection=branch) +else KBUILD_CFLAGS += $(call cc-option,-fcf-protection=none) +endif ifeq ($(CONFIG_X86_32),y) BITS := 32 --- /dev/null +++ b/arch/x86/include/asm/ibt.h @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_IBT_H +#define _ASM_X86_IBT_H + +#ifdef CONFIG_X86_IBT + +#ifndef __ASSEMBLY__ + +#ifdef CONFIG_X86_64 +#define ASM_ENDBR "endbr64\n\t" +#else +#define ASM_ENDBR "endbr32\n\t" +#endif + +#define __noendbr __attribute__((nocf_check)) + +/* + * A bit convoluted, but matches both endbr32 and endbr64 without + * having either as literal in the text. + */ +static inline bool is_endbr(const void *addr) +{ + unsigned int val = ~*(unsigned int *)addr; + val |= 0x01000000U; + return val == ~0xfa1e0ff3; +} + +#else /* __ASSEMBLY__ */ + +#ifdef CONFIG_X86_64 +#define ENDBR endbr64 +#else +#define ENDBR endbr32 +#endif + +#endif /* __ASSEMBLY__ */ + +#else /* !IBT */ + +#ifndef __ASSEMBLY__ + +#define ASM_ENDBR + +#define __noendbr + +#else /* __ASSEMBLY__ */ + +#define ENDBR + +#endif /* __ASSEMBLY__ */ + +#endif /* CONFIG_X86_IBT */ +#endif /* _ASM_X86_IBT_H */