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: 84315 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9B63B3858C2C for ; Thu, 18 Jan 2024 07:13:01 +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-Google-Smtp-Source: AGHT+IESs1MB2SGsH5shmNTimlwl8RwISuZMslU+HUI55L8hY4pnyW7ALVD/fO/IN3R7vBwzSG+daB2TvuqK 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+patchwork=sourceware.org@gcc.gnu.org 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 } } */