Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Stack clash mitigation in GCC, Part 3

May 22, 2020
Jeff Law
Related topics:
SecurityC, C#, C++Linux

Share:

    In previous posts, Stack Clash Mitigation in GCC — Background and Stack Clash mitigation in GCC: Why -fstack-check is not the answer, I hopefully showed the basics of how stack clash attacks are structured and why GCC's existing -fstack-check mechanism is insufficient for protection.

    So, what should we do? Clearly we want something similar to -fstack-check, but without the fundamental problems. Enter a new option: -fstack-clash-protection.

    The key principles for code generation to prevent a stack clash attack are:

    • No single allocation can be greater than a page. The compiler must translate large requests into a series of page- or smaller-sized requests.
    • As pages are allocated, emit instructions to probe them. (Let's call these explicit probes.)
    • A series of sub-page allocations without intervening probes can not allocate more than a page in total.

    A naive implementation around these principles could be highly inefficient, but this option provides the basis for building a secure, high-performance implementation.

    Implicit probes to improve performance

    A stack access that occurs naturally in the code is an implicit probe. An implicit probe implies no additional cost; therefore, using an implicit probe rather than an explicit probe is advantageous. For example, an implicit probe might occur due to the behavior of the target architecture, the requirements of the application binary interface (ABI), or by analysis of existing memory references in the program.

    For example, the call instruction on many processors pushes the return address onto the stack. Thus, the call instruction would fault if the stack was in the stack guard. This is an implicit probe at *sp. Some application binary interfaces require that *sp always contains a back-chain pointer (the pointer to the next outer stack frame). Thus, every stack allocation is required to atomically update *sp. Again, this is an implicit probe of *sp.

    We can also analyze the generated code. For example, on one target the caller allocates space for the callee to save registers. Thus, in the callee, a register save to *(sp + 48) is an implicit probe at *(sp + 48). On other targets, the callee often pushes pairs of registers onto the stack at function entry. Those pushes are implicit probes at *sp.

    It turns out that taking advantage of the implicit probes noted above can dramatically decrease the amount of explicit probing. If we look at glibc as an example on x86 and PPC we see that less than 2% of the functions in glibc require explicit probing in their prologues. For example, if a function allocates less than a page of stack space on these architectures, then no explicit probe is necessary.

    Current status

    Red Hat's engineers implemented -fstack-clash-protection for all Red Hat Enterprise Linux (RHEL) targets starting with RHEL 7.5. RHEL 7.5 enables -fstack-clash-protection for glibc only. Starting with RHEL 8, the entire distribution is compiled with -fstack-clash-protection and annobin/annocheck are used to verify that the distribution was compiled with the proper flags.

    Fedora 27 and later enable -fstack-clash-protection by default for all packages using the standard default compilation options (note that there is no -fstack-clash-protection support for 32-bit ARM targets).

    GCC 8 includes -fstack-clash-protection support for the Intel, IBM Power, IBM Z series, and ARM's aarch64 targets.

    LLVM 11 will include stack clash protections for Intel 64 and AMD64 written by Serge Guelton.

    Testing

    Red Hat's engineers have written a variety of tests to verify the analysis of static and dynamic stack utilization. Red Hat's engineers have also written regression tests for all bugs reported against -fstack-clash-protection since its introduction to GCC. These tests run as a part of the standard regression testing process for GCC. Most of the tests are portable enough to be usable on other targets if one was to implement stack clash mitigation on a currently unsupported target.

    Red Hat's engineers also implemented a scanner that can examine relocatable objects, executables, and dynamic shared objects. The scanner looks for violations of the key principles listed above within an instruction window and notifies the developer of suspicious code. Red Hat has used the scanner to scan key libraries and objects (with hand verification of all the sequences reported as potentially vulnerable by the scanner). This practice proved particularly useful in verifying that Fedora 27 was consistently using -fstack-clash-protection and invalidating improvements to the aarch64 implementation made by ARM's engineers.

    Last updated: May 19, 2020

    Recent Posts

    • Assessing AI for OpenShift operations: Advanced configurations

    • OpenShift Lightspeed: Assessing AI for OpenShift operations

    • OpenShift Data Foundation and HashiCorp Vault securing data

    • Axolotl meets LLM Compressor: Fast, sparse, open

    • What’s new for developers in Red Hat OpenShift 4.19

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue