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

Use compiler flags for stack protection in GCC and Clang

June 2, 2022
Serge Guelton Siddhesh Poyarekar
Related topics:
CompilersSecurity
Related products:
Red Hat Enterprise Linux

Share:

    A binary may be subject to a wide range of attacks, but smashing the stack for fun and profit is one of the most venerable ones. Stack-based attacks are also the lowest-cost form of attack: Stack layouts are quite predictable because data containing return addresses can be overwritten to gain control over program execution.

    Compiler developers have implemented a wide range of countermeasures in response. This article discusses the major stack protection mechanisms in the GNU Compiler Collection (GCC) and Clang, typical attack scenarios, and the compiler's attempts to prevent attacks.

    Stack canary

    A stack canary is the most rudimentary check for buffer overflows on the stack. The canary is an extra word of memory at the end of the stack frame with a value set at runtime. This value is presumably unknown to the attacker and checked for modification before jumping out of the function. A modification indicates the detection of a stack smashing followed by a termination routine.

    Stack canaries are added by GCC and Clang through these flags:

    • -fstack-protector
    • -fstack-protector-strong
    • -fstack-protector-all
    • -fstack-protector-explicit

    SafeStack and shadow stack

    This form of protection splits the stack into two distinct areas, storing precious variables and user variables in non-contiguous memory areas. The goal is to make it more difficult to smash one of the stacks from the other. This process occurs in hardware (e.g., x86_64 shadow stack) or in software such as LLVM's SafeStack.

    GCC and Clang add shadow stack through this flag:

    • -mshstk

    Clang adds SafeStack through this flag:

    • -fsanitize=safe-stack

    Fortified source

    The GNU C library (glibc) provides alternate implementations of some commonly used functions to smash the stack by copying a given amount of bytes from one address to another. These implementations typically use compiler support to check for memory bounds of objects. If an operation overflows these bounds, it would cause programs to terminate. This defense is called FORTIFY_SOURCE and the Red Hat blog has an excellent post on the subject.

    GCC and Clang select fortified sources using the preprocessor flag -D_FORTIFY_SOURCE=<n> with an optimization level greater than -O0. A higher <n> corresponds to greater protection. However, there may be a tradeoff in performance, and some programs that conform to the standard may fail due to stricter security checks. GCC (version 12 and later) and Clang (version 9.0 and later) support <n> up to 3.

    Control flow integrity

    Return-oriented programming (ROP) uses an initial stack smash to take control of an indirect jump and then executes an arbitrary sequence of instructions. One countermeasure to this kind of attack is to ensure that jump addresses and return addresses are correct by using hardware support or pure software.

    GCC and Clang can generate support code for Intel's Control-flow Enforcement Technology (CET) through this compiler flag:

    • -fcf-protection=[full]

    GCC and Clang can also generate support code for Branch Target Identification (BTI) on AArch64 using:

    • -mbranch-protection=none|bti|pac-ret+leaf|pac-ret[+leaf+b-key]|standard

    In addition to these flags, which require hardware support, Clang provides a software implementation of control flow integrity.

    • -fsanitize=cfi

    Stack allocation control

    The following options have an impact on the stack allocation. These options are not necessarily designed to provide extra security, but they may be a nice side-effect.

    On an x86 target, GCC and Clang provide the ability to automatically allocate a discontiguous stack when running out of stack memory. The -fsplit-stack activates this behavior.

    When using the GNU linker, it is possible to pass a stack limit to the program, reading from a symbol or a register. This process is supported by GCC using:

    • -fstack-limit-register
    • -fstack-limit-symbol

    These stack-allocated arrays are a common entry point for a stack-based attack. The Clang compiler makes it possible to reduce the attack surface by disallowing this pattern, subsequently preventing the use of stack-allocated arrays. The code -fno-stack-array enables this process.

    Stack usage and statistics

    Most stack usage consists of either call stack information or fixed-sized allocations in the form of local variables. There is, however, a facility to allocate dynamically sized objects on the stack by using the alloca() function or using variable-length arrays (VLAs). alloca() usage with sizes can be user-controlled. This is a common target for overflowing stacks or making them clash with other maps in memory. As a result, a well-behaved application must always keep track of them. Both GCC and Clang provide ways to control alloca() and stack usage to prevent clashes at the outset.

    The following flags allow finer control over stack usage in applications and provide a warning when alloca() or VLA cross developer-defined thresholds:

    • -Wframe-larger-than
    • -Walloca
    • -Walloca-larger-than
    • -Wvla -Wvla-larger-than

    There are also recursion checks as well as higher-level checks on stack usage to help developers get better control of the stack behavior.

    Here is a full list of warnings implemented in GCC:

    • -Wstack-usage
    • -fstack-usage
    • -Walloca
    • -Walloca-larger-than
    • -Wvla
    • -Wvla-larger-than
    • -Wframe-larger-than
    • -mwarn-dynamicstack (s390x)
    • -Wstack-protector
    • -Wtrampolines
    • -Winfinite-recursion

    And here is a list of the warnings implemented in Clang:

    • -fstack-usage
    • -Walloca
    • -Wframe-larger-than
    • -Wstack-usage

    Summary

    Both GCC and Clang provide a wide range of compiler flags to prevent stack-based attacks. Some of these flags relate to a specific kind of exploit. Others introduce generic protection. And some flags give feedback like warnings and reports to the user, providing a better understanding of the behavior of the stack program. Depending on the attack scenario, code size constraints, and execution speed, compilers provide a wide range of tools to address the attack.

    Last updated: August 3, 2022

    Recent Posts

    • Integrate vLLM inference on macOS/iOS with Llama Stack APIs

    • Optimize model serving at the edge with RawDeployment mode

    • Introducing Red Hat build of Cryostat 4.0

    • How we improved AI inference on macOS Podman containers

    • How OpenShift Virtualization supports VM live migration

    What’s up next?

    Linux server image

    The Intermediate Linux Cheat Sheet introduces developers and system administrators to more Linux commands they should know.

    Get the free cheat sheet
    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