ASLR & Stack Protection

Explore ASLR, PIE, stack canaries, and NX on Linux with GCC, then inspect a harmless C program to see what each mitigation does and where it falls short.

published: reading time: 6 min read author: Geek Workbench updated: November 5, 2026
Quick Summary

Explore ASLR, PIE, stack canaries, and NX on Linux with GCC, then inspect a harmless C program to see what each mitigation does and where it falls short.

ASLR & Stack Protection

ASLR, stack canaries, PIE, and NX reduce the impact of some memory errors. They work at different layers, and none repairs the bug itself. This guide uses a harmless local C program to show how the controls appear in a Linux binary.

Scope and assumptions

The commands below assume Linux on x86-64, GCC, ELF binaries, and GNU binutils such as readelf. Other Linux architectures may support different options or have different defaults. macOS uses Mach-O and Windows uses PE; their compiler flags and operating-system controls differ, so do not copy these commands as a cross-platform recipe.

The lab only compiles and inspects a program with fixed, safe input. It does not create an overflow or change system security settings. Distribution toolchains may enable extra hardening by default, so the inspection results can show protections beyond the option named in each command.

What each protection does

Protection Layer What it changes What it does not do
ASLR Operating system Randomizes selected process memory locations. On Linux, the kernel setting also controls whether heap placement is randomized. It does not stop a memory error. An address leak can reduce its value, and available randomness varies by architecture and process.
PIE Compiler and linker Builds the main executable so the loader can place it at different addresses. GCC uses position-independent code at compile time and the PIE option at link time. PIE alone does not enable system-wide ASLR.
Stack canary Compiler Adds a guard check to selected functions and terminates the process when the guard changes. It does not stop the write, cover every function, or protect heap and global data.
NX Loader and processor Marks the stack as non-executable so data placed there cannot run as instructions. It does not stop code reuse or data-only attacks, and it does not make the rest of the process memory non-executable.

ASLR and PIE are related but separate. PIE makes the main executable relocatable; the operating system’s ASLR policy determines whether its load address is randomized. Stack canaries and NX address different parts of the problem.

Safe local lab

Save this as lab.c in a scratch directory:

#include <stdio.h>

static void show_message(const char *text) {
    char message[64];

    if (snprintf(message, sizeof message, "%s", text) < 0) {
        return;
    }
    puts(message);
}

int main(void) {
    show_message("local mitigation lab");
    return 0;
}

The function has a local array for the canary example, but copies only a fixed, short string into it. No user input is read.

Inspect each control

Run these builds separately. Each one requests the named compiler or linker option; other protections may still come from your distribution’s defaults.

gcc -O0 -Wall -Wextra -fstack-protector-strong -o lab-canary lab.c
readelf -Ws lab-canary | grep __stack_chk_fail

gcc -O0 -Wall -Wextra -fPIE -pie -o lab-pie lab.c
readelf -h lab-pie | grep Type

gcc -O0 -Wall -Wextra -Wl,-z,noexecstack -o lab-nx lab.c
readelf -W -l lab-nx | grep GNU_STACK

cat /proc/sys/kernel/randomize_va_space

Look for these results:

  • Canary: the symbol table contains __stack_chk_fail, which GCC uses for a failed stack guard check. A missing symbol does not prove that every function lacks protection; compiler optimization and the code in the binary affect which checks are emitted.
  • PIE: the ELF Type is DYN. That type is also used for shared objects, so interpret it in the context of a program built with the PIE link option.
  • NX: the GNU_STACK program header lacks the X flag; RW means readable and writable, not executable.
  • ASLR: Linux reports 0, 1, or 2. The kernel documentation defines 0 as disabled, 1 as randomizing the mmap base, stack, VDSO, shared libraries, and PIE code, and 2 as adding heap randomization. Defaults depend on kernel configuration and architecture.

The last command only reads the current host’s Linux setting. The ASLR value is a system property, not a compiler flag embedded in lab.c.

Build with the controls together

gcc -O0 -Wall -Wextra \
  -fstack-protector-strong -fPIE -pie -Wl,-z,noexecstack \
  -o lab-hardened lab.c

./lab-hardened
readelf -Ws lab-hardened | grep __stack_chk_fail
readelf -h lab-hardened | grep Type
readelf -W -l lab-hardened | grep GNU_STACK
cat /proc/sys/kernel/randomize_va_space

The program should print:

local mitigation lab

Compare the inspection output with the separate builds. The flags request protections at compile or link time; the kernel’s ASLR policy remains the same for all of them.

Where these protections stop

A canary detects some stack overwrites when a protected function is about to return. It does not make an unsafe copy safe, and the process may already have corrupted other data before the check runs. Coverage depends on the compiler option, target, optimization, and function shape.

NX blocks instruction execution from memory marked non-executable. It does not prevent an attacker from reusing code that is already executable. ASLR can make those addresses harder to predict, but a memory disclosure can reveal them. PIE makes the main executable relocatable but cannot randomize it when the operating system does not apply ASLR.

The lab checks ELF metadata, not every runtime memory mapping or every code path. Compiler flags and inspection commands also differ across toolchains. Treat them as evidence about this one build, not a complete security audit.

Choose defenses for the platform

Start with the operating system and compiler’s supported hardening profile, then verify the artifacts produced by the release toolchain. On Linux with GCC, the options above illustrate canaries, PIE, and a non-executable stack; the kernel controls ASLR separately. For macOS, Windows, embedded systems, or a different compiler, follow that platform’s current vendor guidance and inspect the native executable format.

Keep system protections enabled according to the platform’s supported defaults. If a specific legacy program has a compatibility problem, test the smallest scoped exception and document why it is needed; changing a machine-wide setting can affect unrelated processes. Avoid weakening protections just to hide a crash: identify the underlying memory bug and fix it.

For new code, prefer a memory-safe language when it fits the project. In C and C++, validate lengths, use APIs that accept buffer sizes, and run sanitizers and tests during development. Hardening helps contain mistakes; it does not replace memory-safe design. For a closer look at pointers, allocation, and common memory errors, see Pointers and Memory Management.

Further reading

Category

Related Posts

Assembly Language Basics: Writing Code the CPU Understands

Learn x86-64 Linux assembly with the System V ABI: track registers, assemble and run a small program, debug it in GDB, then compare AArch64.

#operating-systems #assembly-language-basics #computer-science

Boolean Logic & Gates

Understanding AND, OR, NOT gates and how they combine into arithmetic logic units — the building blocks of every processor.

#operating-systems #boolean-logic-gates #computer-science

Boot Process

How firmware, bootloaders, kernels, and user space start an operating system, with Linux/x86 BIOS/MBR and UEFI paths plus practical troubleshooting.

#operating-systems #boot-process #computer-science