From patchwork Wed Feb 23 22:34:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1596925 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=Jon3gpW8; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4K3rTH288bz9sG1 for ; Thu, 24 Feb 2022 09:35:14 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C63AF3858430 for ; Wed, 23 Feb 2022 22:35:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C63AF3858430 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1645655711; bh=u+i6WrMQviOTyI2AcZwycYXJWGS+zb36nLUGVueWfO8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Jon3gpW893NadWn/m74jjMgtGXL2NlAk4m85zOZYdJQrzVgr7LrlX+4dSiCRLKHhh VyISU6Ro7fEX/m+iK3tjHivC6JoZ9pZOJxK/zxDszSuCasSUnxIWfAvBZ65EaJtZCA sms/MI3fjzX94hJzaDm0EB+NEvH9Wm11SzJrSOb4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id B16EC3858D28 for ; Wed, 23 Feb 2022 22:34:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B16EC3858D28 Received: by mail-pl1-x632.google.com with SMTP id s1so56158plg.12 for ; Wed, 23 Feb 2022 14:34:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=u+i6WrMQviOTyI2AcZwycYXJWGS+zb36nLUGVueWfO8=; b=HmiRtwdjECCu4HmwCqKmBQsdVTlSE6Z251iCO7ivIbBUWNw5UAARYstgX1S4Va+IyQ t/kdxn7DmCWb1jJrIeSGLb1ysIt0066CV0KKeegx+YvTcwAgGIFMmngBmPDZTzatx7EH /fiAZW3QNZjxpVF847ZyUcb/YtgRZEO3zwLopgLVIeU2Fx5JGDcU0r0OH8Hn3xso+yyw 8UlN+23jS0RXiEf0FNSv+OrxSaZ0XfArQBPrts29kdTCaJxsu6wlS9vz3Ce52FOOtYv2 hV74ATb7TxLoLvCzbHqS1k9FI5ar1osBV8kYS2Df4umk6NzXWObvLEwAsRtAUrmu6mMG kfmQ== X-Gm-Message-State: AOAM530ZTJ4Gk92kaUfWCLbPs/QjlKBOdaIVB4Wj1ezsClcCJUE4xlfO 87OeeC5gI2Gl+BRTmNYhU2X0+a7UdCU= X-Google-Smtp-Source: ABdhPJwTt9HCYOX3o/R8QtOxlU8DEfOixz14gpXkqE3fiy8AuKaDeKBuGjT+lFKAVan48VkNQAtKIg== X-Received: by 2002:a17:902:ab92:b0:14d:8c80:dbff with SMTP id f18-20020a170902ab9200b0014d8c80dbffmr1668458plr.89.1645655689274; Wed, 23 Feb 2022 14:34:49 -0800 (PST) Received: from gnu-tgl-3.localdomain ([172.58.35.88]) by smtp.gmail.com with ESMTPSA id q9sm539231pfk.31.2022.02.23.14.34.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Feb 2022 14:34:48 -0800 (PST) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 9985FC03BF for ; Wed, 23 Feb 2022 14:34:47 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] Add -fcf-check-attribute=[yes|no|none] for Linux kernel Date: Wed, 23 Feb 2022 14:34:47 -0800 Message-Id: <20220223223447.624621-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3029.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Gcc-patches" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" When compiling Linux kernel with -fcf-protection=branch to enable x86 Indiret Branch Tracking (IBT), ENDBR is added to all global functions. This creates more "legal" forward edges than necessary. -mmanual-endbr provides a way to insert ENDBR instruction at function entry only via the 'cf_check' function attribute and programmers can add the 'cf_check' function attribute to functions which can be reached by indirect branch. Add -fcf-check-attribute=[yes|no|none] to imply "cf_check" or "nocf_check" function attributes so that GCC can produce a diagnostic when there is a mismatch in cf_check or nocf_check function attributes. This has been tested on Linux kernel. gcc/ PR target/102953 * attribs.cc (decl_attributes): Add implied cf_check or nocf_check function attributes. * common.opt: Add -fcf-check-attribute=. * flag-types.h (cf_check_attribute): New. * doc/invoke.texi: Document -fcf-check-attribute=. gcc/c/ PR target/102953 * c-decl.cc (diagnose_mismatched_decls): Check implied cf_check and nocf_check function attributes. gcc/testsuite/ PR target/102953 * gcc.target/i386/pr102953-3.c: New test. * gcc.target/i386/pr102953-4.c: Likewise. * gcc.target/i386/pr102953-5.c: Likewise. * gcc.target/i386/pr102953-6.c: Likewise. --- gcc/attribs.cc | 19 +++++++++++++++++++ gcc/c/c-decl.cc | 22 +++++++++++++++++++++- gcc/common.opt | 16 ++++++++++++++++ gcc/doc/invoke.texi | 12 ++++++++++++ gcc/flag-types.h | 8 ++++++++ gcc/testsuite/gcc.target/i386/pr102953-3.c | 8 ++++++++ gcc/testsuite/gcc.target/i386/pr102953-4.c | 7 +++++++ gcc/testsuite/gcc.target/i386/pr102953-5.c | 7 +++++++ gcc/testsuite/gcc.target/i386/pr102953-6.c | 8 ++++++++ 9 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.target/i386/pr102953-3.c create mode 100644 gcc/testsuite/gcc.target/i386/pr102953-4.c create mode 100644 gcc/testsuite/gcc.target/i386/pr102953-5.c create mode 100644 gcc/testsuite/gcc.target/i386/pr102953-6.c diff --git a/gcc/attribs.cc b/gcc/attribs.cc index 497dcff84df..4af9df5d976 100644 --- a/gcc/attribs.cc +++ b/gcc/attribs.cc @@ -689,6 +689,25 @@ decl_attributes (tree *node, tree attributes, int flags, attributes = tree_cons (get_identifier ("no_icf"), NULL, attributes); } + /* -fcf-check-attribute=[yes|no] implies "cf_check" or "nocf_check" + function attribute. */ + if (TREE_CODE (*node) == FUNCTION_DECL + && flag_cf_check_attribute != CF_CHECK_ATTRIBUTE_NONE + && !fndecl_built_in_p (*node) + && lookup_attribute ("nocf_check", + DECL_ATTRIBUTES (*node)) == nullptr + && lookup_attribute ("cf_check", + DECL_ATTRIBUTES (*node)) == nullptr + && (!attributes + || (lookup_attribute ("nocf_check", attributes) == nullptr + && lookup_attribute ("cf_check", attributes) == nullptr))) + { + const char *attr = (flag_cf_check_attribute == CF_CHECK_ATTRIBUTE_YES + ? "cf_check" : "nocf_check"); + attributes = tree_cons (get_identifier (attr), nullptr, + attributes); + } + targetm.insert_attributes (*node, &attributes); /* Note that attributes on the same declaration are not necessarily diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index c701f07befe..787c39dc0fe 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -2133,7 +2133,27 @@ diagnose_mismatched_decls (tree newdecl, tree olddecl, error ("conflicting type qualifiers for %q+D", newdecl); } else - error ("conflicting types for %q+D; have %qT", newdecl, newtype); + { + if (flag_cf_check_attribute == CF_CHECK_ATTRIBUTE_NO + && (!lookup_attribute ("nocf_check", + TYPE_ATTRIBUTES (oldtype)) + != !lookup_attribute ("nocf_check", + TYPE_ATTRIBUTES (newtype)))) + error ("conflicting types for %q+D; have %qT with " + "implied % attribute", + newdecl, newtype); + else if (flag_cf_check_attribute == CF_CHECK_ATTRIBUTE_YES + && (!lookup_attribute ("cf_check", + TYPE_ATTRIBUTES (oldtype)) + != !lookup_attribute ("cf_check", + TYPE_ATTRIBUTES (newtype)))) + error ("conflicting types for %q+D; have %qT with " + "implied % attribute", + newdecl, newtype); + else + error ("conflicting types for %q+D; have %qT", + newdecl, newtype); + } diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype); locate_old_decl (olddecl); return false; diff --git a/gcc/common.opt b/gcc/common.opt index c21e5273ae3..b6353e5587f 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -1873,6 +1873,22 @@ Enum(cf_protection_level) String(check) Value(CF_CHECK) EnumValue Enum(cf_protection_level) String(none) Value(CF_NONE) +fcf-check-attribute= +Target RejectNegative Joined Enum(cf_check_attribute) Var(flag_cf_check_attribute) Init(CF_CHECK_ATTRIBUTE_NONE) +-fcf-check-attribute=[none|yes|no] Select the implied function attribute. + +Enum +Name(cf_check_attribute) Type(enum cf_check_attribute) UnknownError(unknown default Control-Flow attribute %qs) + +EnumValue +Enum(cf_check_attribute) String(none) Value(CF_CHECK_ATTRIBUTE_NONE) + +EnumValue +Enum(cf_check_attribute) String(yes) Value(CF_CHECK_ATTRIBUTE_YES) + +EnumValue +Enum(cf_check_attribute) String(no) Value(CF_CHECK_ATTRIBUTE_NO) + finstrument-functions Common Var(flag_instrument_function_entry_exit) Instrument function entry and exit with profiling calls. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index fb3dec4ab0c..a24f435b370 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -605,6 +605,7 @@ Objective-C and Objective-C++ Dialects}. -fasan-shadow-offset=@var{number} -fsanitize-sections=@var{s1},@var{s2},... @gol -fsanitize-undefined-trap-on-error -fbounds-check @gol -fcf-protection=@r{[}full@r{|}branch@r{|}return@r{|}none@r{|}check@r{]} @gol +-fcf-check-attribute=@r{[}none@r{|}yes@r{|}no@r{]} @gol -fharden-compares -fharden-conditional-branches @gol -fstack-protector -fstack-protector-all -fstack-protector-strong @gol -fstack-protector-explicit -fstack-check @gol @@ -15971,6 +15972,17 @@ Currently the x86 GNU/Linux target provides an implementation based on Intel Control-flow Enforcement Technology (CET) which works for i686 processor or newer. +@item -fcf-check-attribute=@r{[}none@r{|}yes@r{|}no@r{]} +@opindex fcf-check-attribute +Select the implied function attribute for code instrumentation of +control-flow transfers. + +The value @code{yes} makes the @code{cf_check} attribute implied on +functions. The value @code{no} makes the @code{nocf_check} attribute +implied on functions. The value @code{none}, which is the default, +doesn't imply the @code{cf_check} nor @code{nocf_check} attributes on +functions. + @item -fharden-compares @opindex fharden-compares For every logical test that survives gimple optimizations and is diff --git a/gcc/flag-types.h b/gcc/flag-types.h index 2c8498169e0..c84d67b1ac8 100644 --- a/gcc/flag-types.h +++ b/gcc/flag-types.h @@ -449,6 +449,14 @@ enum cf_protection_level CF_CHECK = 1 << 3 }; +/* Default Control-Flow Protection attribute. */ +enum cf_check_attribute +{ + CF_CHECK_ATTRIBUTE_NONE = 0, + CF_CHECK_ATTRIBUTE_YES, + CF_CHECK_ATTRIBUTE_NO +}; + /* Parloops schedule type. */ enum parloops_schedule_type { diff --git a/gcc/testsuite/gcc.target/i386/pr102953-3.c b/gcc/testsuite/gcc.target/i386/pr102953-3.c new file mode 100644 index 00000000000..fd92c9c577e --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr102953-3.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-Wall -fcf-protection -mmanual-endbr -fcf-check-attribute=no" } */ + +static void __attribute__((cf_check)) foo(void); +static void foo(void) /* { dg-error "implied 'nocf_check' attribute" } */ +{ +} +void (*ptr)(void) = foo; /* { dg-warning "incompatible pointer type" } */ diff --git a/gcc/testsuite/gcc.target/i386/pr102953-4.c b/gcc/testsuite/gcc.target/i386/pr102953-4.c new file mode 100644 index 00000000000..cd8f4279180 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr102953-4.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-Wall -fcf-protection -mmanual-endbr -fcf-check-attribute=no" } */ + +static void foo(void) +{ +} +void (*ptr)(void) = foo; /* { dg-warning "incompatible pointer type" } */ diff --git a/gcc/testsuite/gcc.target/i386/pr102953-5.c b/gcc/testsuite/gcc.target/i386/pr102953-5.c new file mode 100644 index 00000000000..b1bd4afe85f --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr102953-5.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-Wall -fcf-protection -mmanual-endbr -fcf-check-attribute=no" } */ + +extern void foo(void); +void __attribute__((cf_check)) foo(void) /* { dg-error "implied 'nocf_check' attribute" } */ +{ +} diff --git a/gcc/testsuite/gcc.target/i386/pr102953-6.c b/gcc/testsuite/gcc.target/i386/pr102953-6.c new file mode 100644 index 00000000000..ee0c66f94cb --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr102953-6.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-Wall -fcf-protection -mmanual-endbr -fcf-check-attribute=no" } */ + +static int __attribute__((cf_check)) foo(char a[], int b) +{ + return 0; +} +int (*ptr)(char[], int) = foo;