From patchwork Thu Jan 18 07:12:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fangrui Song X-Patchwork-Id: 189015 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp172422dyb; Wed, 17 Jan 2024 23:13:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IGVpJXyf2h3p6DRpnog1VHWkeEyv+Qi6XpeF9/YZMPbDZLp6vhj5DFPuf1LXqgk9K+lhvZD X-Received: by 2002:a05:620a:7ed:b0:783:518b:9d57 with SMTP id k13-20020a05620a07ed00b00783518b9d57mr291301qkk.42.1705561994055; Wed, 17 Jan 2024 23:13:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705561994; cv=pass; d=google.com; s=arc-20160816; b=bkUdBwt+xN8VLNLbpANKGIexVGWKa4gEikmYnMyB8Ls1DW/lKi8K84vqonrszkWQPE EhmTGCdn6AsGkUs2ZgglDgAxp2aByqR8iiz4T3gnEOnNKB7P33omrJeND6JYU6yrH3Uc wu5AHHjbJwQeZF2MMoYIg5fvjp61P/rM1BaRzTfFciqxCNpGgL3Rn6zem/RHIYeEwNP0 lqHRyOBZLMunePJ2IqFPNQdTiaxSOlsQgx3G+pHYOoiz6wXcJ2lmD6Mpps08VHyzHbP5 rroaLeYrpEEqsbBZsFrXLxsz7ZSj1Bgx4C8l6qzyXqAArmqJPNT/0keBVJyOSU0iPkKH Fo4Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:cc:to:from:subject:message-id :mime-version:date:dkim-signature:arc-filter:dmarc-filter :delivered-to; bh=gLXHS/Bvg2hrX0eCvE2jG9b6XwOSF2CfwF8R9QxIZh4=; fh=X6GlR/9y8bm37EhPoh3HWjiYxjmTNuav2nBxs3xuEcE=; b=ZrysGNyMhvBv6CffZlT2l8rHNgDLDifmwU+WRpFGvw2uK8IV7pmdUepIRObXh4u+FM HzVqR0ImU7bqGTR8Adiqd+3lajdoBNTspRwSoUa6jOPA96IEL9T4oSLEAlGA53oZcnmy ZZnIMuYaB3G8iS3iJ13frttdxKjAF+vLN+RaFeDtvzU15mM2NXli0FK7vAUxiIie0deT wy2eyyoj8RtKxajcZJecJI2HxRrrNKNSOFI/0g1hY8TyM5ug62S1YL0DcjXKm41UFeHo JzcSZUHfKDqyJkdISevuKgHvF5DZcc/xwbCFwEvwxeXllZsvjUDWL3g0PZS2e/79EyT+ a/Yw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ocPob8fj; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id 11-20020a05620a048b00b00781958c7b37si12850116qkr.520.2024.01.17.23.13.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jan 2024 23:13:14 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ocPob8fj; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BC8263858C2A for ; Thu, 18 Jan 2024 07:13:13 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by sourceware.org (Postfix) with ESMTPS id B915A3858D33 for ; Thu, 18 Jan 2024 07:12:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B915A3858D33 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=flex--maskray.bounces.google.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B915A3858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b4a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705561956; cv=none; b=Obv6Z4XeBAa7m8bnYe/zIZ5PjK22zODjK9wQQkC7+97UNm5NJMAUpyURYOc+rMwjUxDfX3EbJ+i11E53tkVjoYRQfJ2Lo867b/JMhLSLLxE1EeQL6tr4Bpj6OK2z2rIxOZUrit59HQxFvBN/Q+erTm9I4a8xHv1xbzTDu7dUiW8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705561956; c=relaxed/simple; bh=Ygp5Vm9GWSv5v3Ujmjf1y0iGBENyZ+n9bSgQZRsJ1hQ=; h=DKIM-Signature:Date:Mime-Version:Message-ID:Subject:From:To; b=qiAlpekn8P1rO2+uLmNgJOAZYAZrq0POSD1+fe9K0D2D++L0OrwgPE6ccBoRIvc4ZXQHLrNwJ26MAMp6YLHpj/eZYje8yzdv+UEtKwQkGh2WDcAsVAKlI+YBNhaQJy57n7tmELnVEU1v1lYWorhDC8vfgLPdFfqUe6krnTBNHuA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-dc24eb17be6so1651244276.1 for ; Wed, 17 Jan 2024 23:12:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705561952; x=1706166752; darn=gcc.gnu.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=gLXHS/Bvg2hrX0eCvE2jG9b6XwOSF2CfwF8R9QxIZh4=; b=ocPob8fjcGssVdzexBzbkgAgmQXOf4zwjleWACRg5slqiKGMDGobw8zUzaMnLWMTgY 79G0IvqHx1Wg87PRhr7jYhsXeV2PYVsI0GRdOucyGnW8z7sFGjW3SiursQ5HD5q3F5DH cz/Ry3z52MvmK+L55NA3CBQ1QjzbYtOnJ5lLrHOp8d2zuNy7KThM30PmvvkNF7whc199 NLcAnQsnhwjNmJ2piSBL/+LD0ZwKalp70kcc+aOcYyvjmiZ4PQZHfoY59L0GYMHQ3bLy TcxdOlKdTepuhztrk7Eue+sqSVgnOeH32wIMIH0hZSraWe6UYRUPpWX7X/Vg8W9CJ5F5 kUYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705561952; x=1706166752; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=gLXHS/Bvg2hrX0eCvE2jG9b6XwOSF2CfwF8R9QxIZh4=; b=gi0G/X/ciV9hahtB7pWFpemSJtnAXu2Hhvnn316uWerZNRBpWRzpbBESjrB5buTNr1 0RuLaXPS5aL32B4ETIYy18kPFgPvVzAaV6Yf3kFf0KfZN+Q7m9umZjwzkQSPeAbHsckM nlIhfkZ2SCFcq32ZbPnsplHBPtWXAl268L9V9OQgoB115ehozjQDyJkHJbudBP/iZ2Pi FaDk4hGlgkHqMH6i4mERw4amLQYUR9XO+slOAmvFuJ+8zlI/wpjIrr/PNeN2VpEI3rom HoHTqxzShyuAFWqtPWGBVhgzvcdmc8xRafswxng39pn2r3bG5z+EFx3JJQ25uUBzD4tG uXgQ== X-Gm-Message-State: AOJu0Yymf9XrkTcTBF+sk5vNB0qHVgXcpactx5w/ra+Wpxm4YSfRw5Ai XSoGlTYPSe/5kuh3dMsd9WNlGSYyaIvMWQllGdQnmKO/W6mj6q+BmHmURUg+XSxARpq1+snaNTM Zf3JP/w== X-Received: from maskray.svl.corp.google.com ([2620:15c:2d3:205:4f17:d3f5:c825:1b99]) (user=maskray job=sendgmr) by 2002:a25:db87:0:b0:dc2:3b5f:ef6 with SMTP id g129-20020a25db87000000b00dc23b5f0ef6mr168860ybf.13.1705561951879; Wed, 17 Jan 2024 23:12:31 -0800 (PST) Date: Wed, 17 Jan 2024 23:12:29 -0800 Mime-Version: 1.0 Message-ID: <20240118071229.2950767-1-maskray@google.com> Subject: [PATCH] i386: Default to -mcet-switch [PR104816] From: Fangrui Song To: Jan Hubicka , Uros Bizjak , "H.J. Lu" , gcc-patches@gcc.gnu.org Cc: joao@overdrivepizza.com, Fangrui Song X-Spam-Status: No, score=-19.7 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788411373573445938 X-GMAIL-MSGID: 1788411373573445938 When -fcf-protection=branch is used, with the current -mno-cet-switch default, a NOTRACK indirect jump is generated for jump tables, which can target a non-ENDBR instruction. However, the overwhelming opinion is to avoid NOTRACK (PR104816) to improve safety. Projects such as Linux kernel and Xen even specify -fno-jump-table to avoid NOTRACK. Therefore, let's change the default. Note, for `default: __builtin_unreachable()`, LLVM AArch64 even made a decision (https://reviews.llvm.org/D155485) to keep the range check, which can otherwise be optimized out. This reinforces the opinion that people want protection for jump tables. #define DO A(0) A(1) A(2) A(3) A(4) A(5) A(6) A(7) A(8) A(9) A(10) A(11) A(12) A(13) #define A(i) void bar##i(); DO #undef A void ext(); void foo(int i) { switch (i) { #define A(i) case i: bar##i(); break; DO // -mbranch-protection=bti causes Clang AArch64 to keep the i <= 13 range check default: __builtin_unreachable(); } ext(); } gcc/ChangeLog: PR target/104816 * config/i386/i386.opt: Default to -mcet-switch. * doc/invoke.texi: Update doc. gcc/testsuite/ChangeLog: * gcc.target/i386/cet-switch-1.c: Add -mno-cet-switch. * gcc.target/i386/cet-switch-2.c: Remove -mcet-switch to check the default. --- gcc/config/i386/i386.opt | 2 +- gcc/doc/invoke.texi | 19 +++++++++---------- gcc/testsuite/gcc.target/i386/cet-switch-1.c | 2 +- gcc/testsuite/gcc.target/i386/cet-switch-2.c | 2 +- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/gcc/config/i386/i386.opt b/gcc/config/i386/i386.opt index 5b4f1bff25f..0e168f3c07a 100644 --- a/gcc/config/i386/i386.opt +++ b/gcc/config/i386/i386.opt @@ -1074,7 +1074,7 @@ Enable shadow stack built-in functions from Control-flow Enforcement Technology (CET). mcet-switch -Target Var(flag_cet_switch) Init(0) +Target Var(flag_cet_switch) Init(1) Turn on CET instrumentation for switch statements that use a jump table and an indirect jump. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 16e31a3c6db..720be71f8fa 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -1455,7 +1455,7 @@ See RS/6000 and PowerPC Options. -msse4a -m3dnow -m3dnowa -mpopcnt -mabm -mbmi -mtbm -mfma4 -mxop -madx -mlzcnt -mbmi2 -mfxsr -mxsave -mxsaveopt -mrtm -mhle -mlwp -mmwaitx -mclzero -mpku -mthreads -mgfni -mvaes -mwaitpkg --mshstk -mmanual-endbr -mcet-switch -mforce-indirect-call +-mshstk -mmanual-endbr -mno-cet-switch -mforce-indirect-call -mavx512vbmi2 -mavx512bf16 -menqcmd -mvpclmulqdq -mavx512bitalg -mmovdiri -mmovdir64b -mavx512vpopcntdq -mavx5124fmaps -mavx512vnni -mavx5124vnniw -mprfchw -mrdpid @@ -34886,16 +34886,15 @@ function attribute. This is useful when used with the option @option{-fcf-protection=branch} to control ENDBR insertion at the function entry. +@opindex mno-cet-switch @opindex mcet-switch -@item -mcet-switch -By default, CET instrumentation is turned off on switch statements that -use a jump table and indirect branch track is disabled. Since jump -tables are stored in read-only memory, this does not result in a direct -loss of hardening. But if the jump table index is attacker-controlled, -the indirect jump may not be constrained by CET. This option turns on -CET instrumentation to enable indirect branch track for switch statements -with jump tables which leads to the jump targets reachable via any indirect -jumps. +@item -mno-cet-switch +When @option{-fcf-protection=branch} is enabled, by default, switch statements +that use a jump table are instrumented to use ENDBR instructions and constrain +the indirect jump with CET to protect against an attacker-controlled jump table +index. @option{-mno-cet-switch} generates a NOTRACK indirect jump and removes +ENDBR instructions, which may make the jump table smaller at the cost of an +unprotected indirect jump. @opindex mcall-ms2sysv-xlogues @opindex mno-call-ms2sysv-xlogues diff --git a/gcc/testsuite/gcc.target/i386/cet-switch-1.c b/gcc/testsuite/gcc.target/i386/cet-switch-1.c index afe5adc2f3d..4931c3ad1d2 100644 --- a/gcc/testsuite/gcc.target/i386/cet-switch-1.c +++ b/gcc/testsuite/gcc.target/i386/cet-switch-1.c @@ -1,6 +1,6 @@ /* Verify that CET works. */ /* { dg-do compile } */ -/* { dg-options "-O -fcf-protection" } */ +/* { dg-options "-O -fcf-protection -mno-cet-switch" } */ /* { dg-final { scan-assembler-times "endbr32" 1 { target ia32 } } } */ /* { dg-final { scan-assembler-times "endbr64" 1 { target { ! ia32 } } } } */ /* { dg-final { scan-assembler-times "notrack jmp\[ \t]+\[*]" 1 } } */ diff --git a/gcc/testsuite/gcc.target/i386/cet-switch-2.c b/gcc/testsuite/gcc.target/i386/cet-switch-2.c index 69ddc6fd5b7..11578d1a30c 100644 --- a/gcc/testsuite/gcc.target/i386/cet-switch-2.c +++ b/gcc/testsuite/gcc.target/i386/cet-switch-2.c @@ -1,6 +1,6 @@ /* Verify that CET works. */ /* { dg-do compile } */ -/* { dg-options "-O -fcf-protection -mcet-switch" } */ +/* { dg-options "-O -fcf-protection" } */ /* { dg-final { scan-assembler-times "endbr32" 12 { target ia32 } } } */ /* { dg-final { scan-assembler-times "endbr64" 12 { target { ! ia32 } } } } */ /* { dg-final { scan-assembler-times "\[ \t]+jmp\[ \t]+\[*]" 1 } } */