Skip to content
systemcoding.dev
Security

Verify Hardening in an ELF With readelf

Write a shell script that uses readelf and nm to fail a build when an ELF binary lacks PIE, full RELRO, a non-executable stack, a stack canary or CET notes.

7 min read
Summarize in AI
Flat illustration of source code passing through a compiler and linker into a binary, with the GNU, Linux and CMake logos.

A hardening flag is a claim about the build, and the only evidence is the binary that came out of it. Reading flags in a Makefile proves intent. readelf proves the result. This post turns the manual checks from the checksec post into a script that you can run in a build, in CI or as a post-link step, with one check per property, a clear pass or fail line and a nonzero exit status on any failure. It uses only readelf and nm from binutils and a POSIX shell, so it works on a minimal build host where installing extra tools is not an option. Examples target binutils 2.45, GCC 15 and Linux 6.12 on x86-64, and the script notes where AArch64 differs.

What each property looks like in an ELF

Every protection leaves a trace in a specific table of the file. The script is a lookup of these traces.

PropertyWhere to lookPass condition
PIEELF header, TypeDYN for an executable (and a PT_INTERP or DT_FLAGS_1 PIE)
Non-executable stackprogram header GNU_STACKflags do not contain E
RELROprogram header GNU_RELROpresent
Bind nowdynamic section BIND_NOW or FLAGS_1 NOWpresent
Stack canarydynamic symbols__stack_chk_fail imported, when the program has arrays
Fortifydynamic symbolsat least one __*_chk import, when fortifiable calls exist
CET or BTI.note.gnu.propertyIBT and SHSTK on x86, BTI and PAC on AArch64
No RWX segmentsprogram headersno LOAD segment with flags RWE
No text relocationsdynamic sectionno TEXTREL

The two properties that depend on the program's content, the canary and fortification, can legitimately be absent in a tiny program. The script reports them as a warning and not as a failure, and fails on the rest.

Reading the raw output

First see what readelf prints, so the script's patterns make sense. For a hardened executable app:

Text
$ readelf -h app | grep -E 'Type|Machine'
  Type:                              DYN (Position-Independent Executable file)
  Machine:                           Advanced Micro Devices X86-64

$ readelf -lW app | grep -E 'GNU_STACK|GNU_RELRO|LOAD'
  LOAD           0x000000 0x0000000000000000 0x0000000000000000 0x000628 0x000628 R   0x1000
  LOAD           0x001000 0x0000000000001000 0x0000000000001000 0x000215 0x000215 R E 0x1000
  LOAD           0x002000 0x0000000000002000 0x0000000000002000 0x0000b4 0x0000b4 R   0x1000
  LOAD           0x002db8 0x0000000000003db8 0x0000000000003db8 0x000258 0x000260 RW  0x1000
  GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0x10
  GNU_RELRO      0x002db8 0x0000000000003db8 0x0000000000003db8 0x000248 0x000248 R   0x1

$ readelf -dW app | grep -E 'BIND_NOW|FLAGS|TEXTREL'
 0x0000000000000018 (BIND_NOW)
 0x000000006ffffffb (FLAGS_1)            Flags: NOW PIE

The offsets and sizes are examples. What matters is the shape: one read-execute segment (R E) for code and none with RWE, a GNU_STACK of RW, a GNU_RELRO segment and a BIND_NOW entry. The -W flag stops readelf from wrapping lines, which keeps each header on one line for grep.

The script

verify-elf.shShell
set -u

bin=${1:?usage: verify-elf.sh <elf-file>}
fail=0

pass() { printf 'PASS  %s\n' "$1"; }
bad()  { printf 'FAIL  %s\n' "$1"; fail=1; }
warn() { printf 'WARN  %s\n' "$1"; }

hdr=$(readelf -hW "$bin")
prog=$(readelf -lW "$bin")
dyn=$(readelf -dW "$bin" 2>/dev/null || true)
notes=$(readelf -nW "$bin" 2>/dev/null || true)
syms=$(nm -D "$bin" 2>/dev/null || true)

case $hdr in
  *"DYN (Position-Independent Executable"*|*"DYN (Shared object"*) pass "pie" ;;
  *) bad "pie (type is not DYN)" ;;
esac

stack=$(printf '%s\n' "$prog" | grep GNU_STACK || true)
case $stack in
  "") bad "noexecstack (no GNU_STACK header)" ;;
  *RWE*|*" E "*) bad "noexecstack (stack is executable)" ;;
  *) pass "noexecstack" ;;
esac

printf '%s\n' "$prog" | grep -q GNU_RELRO && pass "relro" || bad "relro (no GNU_RELRO)"

case $dyn in
  *BIND_NOW*|*"Flags: NOW"*|*"Flags: BIND_NOW"*) pass "bind-now" ;;
  *) bad "bind-now (lazy binding left on)" ;;
esac

if printf '%s\n' "$prog" | grep LOAD | grep -q RWE; then
  bad "no-rwx (a LOAD segment is readable, writable and executable)"
else
  pass "no-rwx"
fi

case $dyn in
  *TEXTREL*) bad "no-textrel (text relocations present)" ;;
  *) pass "no-textrel" ;;
esac

case $syms in
  *__stack_chk_fail*) pass "stack-canary" ;;
  *) warn "stack-canary (no __stack_chk_fail import; fine if no function has arrays)" ;;
esac

case $syms in
  *_chk@*|*_chk) pass "fortify" ;;
  *) warn "fortify (no _chk imports; fine if there are no fortifiable calls)" ;;
esac

machine=$(printf '%s\n' "$hdr" | sed -n 's/^ *Machine: *//p')
case $machine in
  *X86-64*)
    case $notes in
      *IBT*SHSTK*|*SHSTK*IBT*) pass "cet (IBT and SHSTK)" ;;
      *) bad "cet (x86 feature note lacks IBT or SHSTK)" ;;
    esac ;;
  *AArch64*)
    case $notes in
      *BTI*PAC*|*PAC*BTI*) pass "bti-pac" ;;
      *) bad "bti-pac (AArch64 feature note lacks BTI or PAC)" ;;
    esac ;;
  *) warn "cet/bti (machine $machine not checked)" ;;
esac

exit $fail

Run it on the hardened build and on a deliberately weak one:

Text
$ ./verify-elf.sh build/demo_hard; echo "exit=$?"
PASS  pie
PASS  noexecstack
PASS  relro
PASS  bind-now
PASS  no-rwx
PASS  no-textrel
PASS  stack-canary
PASS  fortify
PASS  cet (IBT and SHSTK)
exit=0

$ ./verify-elf.sh build/demo_weak; echo "exit=$?"
FAIL  pie (type is not DYN)
FAIL  noexecstack (stack is executable)
PASS  relro
FAIL  bind-now (lazy binding left on)
PASS  no-rwx
PASS  no-textrel
WARN  stack-canary (no __stack_chk_fail import; fine if no function has arrays)
WARN  fortify (no _chk imports; fine if there are no fortifiable calls)
FAIL  cet (x86 feature note lacks IBT or SHSTK)
exit=1

The weak build here is a variant of the one in the checksec post, linked with -no-pie -z execstack -z lazy and without CET. It still shows PASS relro, because distributions' linkers produce a GNU_RELRO header by default and only -z norelro removes it, which is a good reminder that partial RELRO is not enough and the bind-now check is what matters.

How the checks work, and where they can mislead

  • PIE. A DYN executable and a shared library look the same in the ELF header. The script is meant for executables, so a DYN is enough. To tell them apart, FLAGS_1 carries PIE in the dynamic section for executables, as the example shows, and a stricter check can require it.

  • Stack header. The pattern matches RWE on the GNU_STACK line. Some readelf versions print the flags as RWE with no spaces, others as RW E, so the script also matches a standalone E. Test it against your readelf before trusting a pass.

  • No RWX segments. Newer linkers warn when they produce one, with warning: a.out has a LOAD segment with RWX permissions, which binutils 2.39 introduced. The script checks the result as well, because the warning is easy to miss.

  • Bind now. The line BIND_NOW appears as a dynamic tag. Some toolchains set only FLAGS_1 NOW, so the script accepts both.

  • Symbols. nm -D lists dynamic symbols, so it sees imports from shared libraries. In a static executable, the canary function is linked in and appears in nm without -D. Add a fallback nm call for statically linked programs.

  • CET notes. The notes print as x86 feature: IBT, SHSTK, and the order and the exact words vary across binutils versions. If the script stops matching after an upgrade, print the raw note and adjust the pattern. AArch64 prints AArch64 feature: BTI, PAC.

Because the script is text matching on tool output, treat a new binutils release as a reason to rerun the script against a known-good and a known-bad sample, and keep both in the repository as test fixtures.

Wiring it in

A CMake post-build step, from the previous post, runs the script on the target file. In a Makefile:

make
check-hardening: $(BIN)
	./verify-elf.sh $(BIN)

In CI, run it on every artifact that you ship, after stripping and packaging, and on the container image's binaries too, since a base image upgrade can bring in unhardened programs. Strip the binary before checking if your release process strips, because strip does not change the checked properties but a post-processing tool might.

Extending the script

The script above is deliberately small, and projects tend to add to it in the same few directions. A check for RPATH and RUNPATH entries in the dynamic section catches a binary that searches a build directory or a relative path for libraries, which an attacker could plant a library in. A check that the executable has no DT_NEEDED entry for a library you ban, such as an old SSL library, uses the same readelf -d output with NEEDED in the pattern. A check on the interpreter name with readelf -l and INTERP confirms the binary will run with the loader you expect. For releases, a check that no section named .comment carries an unexpected compiler version string can catch a stray object from another toolchain.

Keep each addition as its own PASS or FAIL line with a short name, as the existing ones have, so a failing build says which property broke without a reader needing to open the script. And keep the fixtures current: one binary that must pass and one built with the flags turned off that must fail every check, stored in the repository and run by the script's own test target. A script that nobody tests will eventually pass everything.

Also inToolchains
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.