Control-flow Enforcement Technology (CET) provides the following
capabilities to defend against ROP/JOP style control-flow subversion
attacks:
- Shadow Stack - return address protection to defend against Return
  Oriented Programming,
- Indirect branch tracking - free branch protection to defend
  against
  Jump/Call Oriented Programming.

Details are described in the doc
https://software.intel.com/sites/default/files/managed/4d/2a/control-flow-enforcement-technology-preview.pdf

This patch enables CET in the compiler only (gcc directory). The
executables built with the CET enabled compiler can run successfully
on non-CET i386 HW as executed new instructions are NOPs there.
Functional testing can be done through Intel® Software Development
Emulator or Intel® SDE.

There will be more patches to enable remaining intrinsics, to support CET
in the compiler libraries (exception handling) and in glibc.

The patch adds 1) new options to control the technology, 2) three new
instructions (endbr, rdssp, inssp) and intrinsics, 3) a pass to generate
endbr instruction, 4) new 'notrack' attribute for functions and pointers
to function and code generation for it, 5) shadow stack processing in
setjmp/longjmp builtins.

Basic functional tests are added. Bootstrap is done successfully w/o and
w/ CET option (-mcet).

gcc/
    * builtins.c
                (expand_builtin_setjmp_setup): Add saving shadow stack pointer
       in jmpbuf using rdssp insatruction.
                (expand_builtin_longjmp): Add adjusting shadow stack pointer
       using incssp instruction.
    * c-family/c-attribs.c
                (handle_notrack_attribute): New function.
                (c_common_attribute_table): Add a 'notrack' attribute.
    * calls.c
                (emit_call_1): Set REG_CALL_NOTRACK on call insn.
                (flags_from_decl_or_type): Retrieve notrack attribute from a 
decl.
                (expand_call): Retrieve notrack attribute from a decl.
    * combine.c: Handle REG_CALL_NOTRACK.
    * common/config/i386/i386-common.c
                (OPTION_MASK_ISA_CET_SET, OPTION_MASK_ISA_CET_UNSET): New.
                (ix86_handle_option): Handle OPT_mcet.
    * config.gcc: Add cetintrin.h.
                * config/i386/cetintrin.h: New file.
    * config/i386/cpuid.h: (bit_CET) new bit.
    * config/i386/driver-i386.c
                (host_detect_local_cpu): Detect cet.
    * config/i386/i386-builtin.def
                (__builtin_ia32_rdsspd, __builtin_ia32_rdsspq,
                __builtin_ia32_incsspd, __builtin_ia32_incsspd): New intrinsics.
    * config/i386/i386-c.c
                (ix86_target_macros_internal): Define __CET__.
    * config/i386/i386-protos.h (ix86_notrack_prefixed_insn_p): New.
    * config/i386/i386.c
                (ix86_target_string): Add -mcet.
                (ix86_valid_target_attribute_inner_p): Add cet.
                (ix86_print_operand): Output notrack.
                BDESC_VERIFYS for CET intrinsics.
                (ix86_init_mmx_sse_builtins): Define CET intrinsics.
                (x86_output_mi_thunk): Add endbr instruction.
                (ix86_notrack_prefixed_insn_p): New function.
    * config/i386/i386.h
                (TARGET_CET, TARGET_CET_P): New.
    * config/i386/i386.md
                (define_insn "rdssp<mode>"): New instruction.
                (define_insn "incssp<mode>"): Likewise.
                (define_insn "nop_endbr"): Likewise.
    * config/i386/i386.opt
                (mcet, mcet-switch, mcet-indbranch-tracking,
                mcet-shadow-stack): New options.
    * config/i386/immintrin.h
                Add include <cetintrin.h>.
    * final.c
                (rest_of_handle_cet): New.
                (pass_data_handle_cet): New.
                (pass_handle_cet): New.
                (make_pass_handle_cet): New.
    * passes.def: (pass_handle_cet) Add pass.
    * reg-notes.def: (CALL_NOTRACK) New note for notrack.
    * timevar.def: (TV_CET) New.
    * tree-core.h: (ECF_NOTRACK) New.
    * tree-pass.h: (make_pass_handle_cet) New.

gcc/testsuite/

    * gcc.target/i386/cet-intrin.c: New test.
    * gcc.target/i386/cet-label.c: Likewise.
    * gcc.target/i386/cet-notrack.c: Likewise.
    * gcc.target/i386/cet-sjlj.c: Likewise.
    * gcc.target/i386/cet-switch-1.c: Likewise.
    * gcc.target/i386/cet-switch-2.c: Likewise.
---

Attachment: 0001-Enable-Control-flow-Enforcement-Technology-CET.patch
Description: 0001-Enable-Control-flow-Enforcement-Technology-CET.patch

Reply via email to