TExcelTAD
Home › Programming for Security: Python, Bash, PowerShell & C Tools › Reading Compiler Output and Assembly

Reading Compiler Output and Assembly

Source code is what the developer meant; machine code is what the CPU does. When the two disagree, the machine code wins, and security work keeps landing in that gap: a bounds check the optimiser removed, a comparison compiled as signed when the author assumed unsigned, a binary shipped without the hardening flags the build system was supposed to add, a malware sample with no source at all. You don't need to write assembly to work in security, but reading twenty lines of compiler output and recognising a comparison, a branch and a function call is a core skill. The full language is taught in x86-64 Assembly (see its compiler output lesson); reverse engineering at scale comes in the reverse-engineering course.

Definitions (x86-64, Intel syntax)

  • Registers: rdi, rsi, rdx, rcx, r8, r9 carry the first six integer arguments (System V ABI on Linux); rax (eax for 32 bits) carries the return value; rsp is the stack pointer.
  • cmp a, b sets flags from a − b; the next conditional jump reads them.
  • Signed jumps: jl / jge (less / greater-or-equal, as signed numbers). Unsigned jumps: jb / jae (below / above-or-equal). The same cmp followed by a different jump means a different check.
  • mov eax, DWORD PTR [rdi+rsi*4]: load 4 bytes from address rdi + rsi*4, i.e. a[i] for an int array.
  • movsx (written movsxd by some disassemblers for 32 → 64 bits): sign-extend a smaller value (an int index becomes a 64-bit offset, negative stays negative).

A bounds check, signed and unsigned

The session compiles two versions of "return a[i] if i is in range, else −1". The only difference is the type of i. It prints the optimised assembly without assembler directives.

bash · try it yourself
cat > get.c <<'SRC'
#include <stddef.h>
int get_unsigned(const int *a, size_t n, size_t i) {
    if (i >= n) return -1;
    return a[i];
}
int get_signed(const int *a, int n, int i) {
    if (i >= n) return -1;      /* forgot: i < 0 */
    return a[i];
}
SRC
gcc -O2 -S -masm=intel -fno-asynchronous-unwind-tables -fcf-protection=none -o - get.c \
  | grep -vE '^\s*\.(file|text|p2align|globl|type|size|ident|section)|^\s*
  

	.intel_syntax noprefix
get_unsigned:
	cmp	rdx, rsi
	jnb	.L3
	mov	eax, DWORD PTR [rdi+rdx*4]
	ret
.L3:
	mov	eax, -1
	ret
get_signed:
	cmp	edx, esi
	jge	.L7
	movsx	rdx, edx
	mov	eax, DWORD PTR [rdi+rdx*4]
	ret
.L7:
	mov	eax, -1
	ret

The two functions are almost the same instruction for instruction. The difference is the jump after cmp: the unsigned version uses jnb (jump if not below, the same as jae), the signed one jge. For the unsigned check a "negative" index such as (size_t)-1 is a huge number and fails i < n. For the signed check i = −1 is less than n, passes, and movsx rdx, edx turns it into the 64-bit offset −1, so the load reads the 4 bytes before the array. That is an out-of-bounds read (CWE-125) visible in four instructions. The source fix is if (i < 0 || i >= n), or an unsigned index type.

The same check in hand-written assembly

The program below runs a bounds check on three indexes and prints the verdict for each with the write system call (no C library). Change the values in tests and run it again.

nasm · try it yourself
section .data
tests:      dq 2, 8, -1                ; indexes to check against n = 8
ok_msg:     db "in bounds", 10
ok_len      equ $ - ok_msg
bad_msg:    db "OUT OF BOUNDS", 10
bad_len     equ $ - bad_msg

section .text
global _start
_start:
    xor ebx, ebx                ; rbx = test number
.loop:
    mov rax, [tests + rbx*8]    ; rax = index
    cmp rax, 8                  ; compare with n = 8
    jae .bad                    ; UNSIGNED: -1 is 0xFFFF...FFFF, so it is "above"
    mov rsi, ok_msg
    mov edx, ok_len
    jmp .print
.bad:
    mov rsi, bad_msg
    mov edx, bad_len
.print:
    mov eax, 1                  ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    inc rbx
    cmp rbx, 3
    jb .loop
    mov eax, 60                 ; exit(0)
    xor edi, edi
    syscall
in bounds
OUT OF BOUNDS
OUT OF BOUNDS

Replace jae .bad with jge .bad (the signed jump) and the third line changes to in bounds: the same bug as get_signed, one mnemonic wide.

Is the binary hardened?

Reading a binary also means reading its headers. checksec reports the exploit mitigations a file was built with (Memory Protection explains each one); readelf shows the evidence.

bash · try it yourself
printf '#include <stdio.h>\nint main(void){char b[64]; if (fgets(b, sizeof b, stdin)) puts(b); return 0;}\n' > app.c
gcc -O2 app.c -o app-default
gcc -O0 -fno-stack-protector -no-pie -z execstack -z norelro app.c -o app-weak 2>/dev/null
for f in app-default app-weak; do
  printf '%-12s %s\n' "$f" "$(checksec --output=csv --file=$f | cut -d, -f1-4)"
done
echo "== evidence in the ELF headers"
for f in app-default app-weak; do
  printf '%-12s type=%s  stack=%s  canary=%s\n' "$f" \
    "$(readelf -h $f | awk '/Type:/{print $2}')" \
    "$(readelf -lW $f | awk '/GNU_STACK/{print $7}')" \
    "$(nm -D $f | grep -c __stack_chk_fail)"
done
app-default  Full RELRO,Canary found,NX enabled,PIE enabled
app-weak     No RELRO,No Canary found,NX disabled,No PIE
== evidence in the ELF headers
app-default  type=DYN  stack=RW  canary=1
app-weak     type=EXEC  stack=RWE  canary=0

DYN means a position-independent executable (PIE, which ASLR can move); EXEC is loaded at a fixed address. RW on GNU_STACK means a non-executable stack; RWE makes the stack executable, which classic stack-overflow exploits relied on. A reference to __stack_chk_fail shows the stack canary is compiled in: Ubuntu's GCC adds -fstack-protector-strong by default, and main has a local array, so it gets one. The weak build turned every one of these off by hand, which is exactly what a forgotten flag in a build system can do.

How these problems are detected

  • Read the disassembly of the check, not only the source, when reviewing a patch for a bounds-check fix: objdump -d -M intel, Compiler Explorer, or Ghidra for a whole binary.
  • Signed jumps after a length comparison (jl, jge, jg, jle next to a movsx) are a review hint for signed-index bugs.
  • Audit release binaries with checksec (or readelf, hardening-check on Debian) in CI, and fail the build when PIE, RELRO, NX or the canary is missing.

How they are fixed

  1. Fix the source: unsigned sizes and indexes (size_t), or both ends checked.
  2. Make hardening the build default: -O2 -D_FORTIFY_SOURCE=3 -fstack-protector-strong -fPIE -pie -Wl,-z,relro,-z,now -Wl,-z,noexecstack, plus -fcf-protection on x86 for CET.
  3. Verify the output, because flags get lost in build systems: a CI step that runs checksec on every artefact.

Pitfalls

  • AT&T versus Intel syntax: objdump defaults to AT&T (mov %rsi,%rax, source first). Use -M intel or read the operand order carefully.
  • Optimisation changes everything: -O0 code is verbose and maps line by line to the source; -O2 code is shorter, reordered and may have inlined the function you are looking for.
  • endbr64 at the start of functions is a CET landing pad, not part of the logic.

Practice

1. In the signed version, which instruction turns i = −1 into a negative 64-bit offset?

movsx rdx, edx: it sign-extends the 32-bit i to 64 bits, so the address rdi + rdx*4 is 4 bytes before the array.

2. Which single change to the nasm program makes index −1 pass the check?

jae .bad → jge .bad. The signed comparison treats 0xFFFFFFFFFFFFFFFF as −1, which is less than 8.

3. A vendor says their daemon is "built with all hardening". Which four things do you check on the binary?

readelf -h type DYN (PIE), readelf -lW GNU_STACK RW (NX) and GNU_RELRO plus BIND_NOW in readelf -d (full RELRO), and a __stack_chk_fail import for the canary; or all of it with checksec --file=.

Test yourself

After cmp rax, 8, which jump treats rax as an unsigned number?

  1. jl
  2. jge
  3. jae
  4. jg

Why does get_signed(a, 8, -1) read out of bounds?

  1. The compiler removed the check
  2. -1 is less than n as a signed int, passes the check, and is sign-extended to offset -1
  3. The array is too small
  4. movsxd is a bug in gcc

What does readelf type DYN tell you about an executable?

  1. It is a shared library only
  2. It is position-independent (PIE), so ASLR can load it at a different address each run
  3. It has no symbols
  4. It is stripped

GNU_STACK shows RWE. What does that mean?

  1. Full RELRO
  2. The stack is executable, removing the NX protection
  3. The canary is on
  4. The binary is signed