Skip to content

Commit fece72b

Browse files
fix(build): put the whole GOT inside the RELRO window on ELF targets
Adds -Wl,-z,relro and -Wl,-z,now to ELF_HARDENING_FLAGS, and asserts the resulting property on the produced artifact rather than on the compiler's willingness to accept a flag. The shipped Linux binaries measurably lacked the property. Built the way .github/workflows/_build.yml builds them (STATIC=1, gcc 13.3 / GNU ld 2.42, Ubuntu 24.04 aarch64), PT_GNU_RELRO ends at 0x11DA0000 in both cases: without -z now: .got 0x11d9ec20+0x13c8, .got.plt 0x11d9ffe8+0x40 -> .got.plt ends at 0x11DA0028, 40 bytes PAST the window. Those GOT slots stayed writable for the whole process lifetime, in every Linux binary shipped to date. with -z now: .got.plt folds into .got, the whole GOT sits inside the window and is re-mapped read-only after startup. Both flags are free at runtime for a static binary -- everything resolves at link time, so there is no lazy binding left to pay for. -z relro is already this toolchain's default and changes nothing here; it is named anyway so the property stops depending on one distro's spec file, since the musl/portable and glibc-floor images are different toolchains and a default is not a guarantee. A1c/A1d in scripts/ci/check-binary-composition.sh assert the OUTCOME on the binary via readelf, for the same reason A1 exists beside the .note.GNU-stack annotation: a flag the linker accepts is not evidence the artifact gained anything. PR DeusData#1138 demonstrated that directly -- its -pie was silently discarded under -static, and its -D_FORTIFY_SOURCE=2 silently DOWNGRADED Ubuntu's default of 3, both while every compile probe passed. The readers use awk rather than a short-circuiting reader on purpose: a reader that exits at the first match leaves the upstream taking EPIPE, and under `set -o pipefail` the satisfied case is reported as the failing one. Deliberately NOT taken from DeusData#1138: -D_FORTIFY_SOURCE=2 (a downgrade on the release platform), -fPIE/-pie (inert under STATIC=1), -fstack-protector-strong (real but unmeasured cost, and DeusData#1138 injected it into SQLite, tree-sitter and mimalloc -- the indexing hot loop), and the $(shell) probe machinery (~10 compiler spawns per Makefile parse, making the binary a function of the builder's toolchain). Co-authored-by: PR9000 <119280965+PR9000@users.noreply.github.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 5d96377 commit fece72b

2 files changed

Lines changed: 176 additions & 7 deletions

File tree

‎Makefile.cbm‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,35 @@ endif
181181
# ~259 MB of tree-sitter parse tables was mapped executable at runtime while the
182182
# amd64 build of the same source mapped them R only. That is a large ROP gadget
183183
# surface for data that is never executed, and it is invisible to section flags.
184+
#
185+
# -z relro + -z now finish the same job on the OTHER writable-but-shouldn't-be
186+
# region: the GOT. Both are free at runtime here (a static binary resolves
187+
# everything at link time, so there is no lazy binding left to pay for), and
188+
# they are added because the SHIPPED artifact measurably lacked the property --
189+
# not because the linker accepts the flags. Measured on this binary, built the
190+
# way .github/workflows/_build.yml builds the Linux release (STATIC=1, gcc 13.3
191+
# / GNU ld 2.42, Ubuntu 24.04 aarch64); PT_GNU_RELRO ends at 0x11DA0000 in both:
192+
#
193+
# without -z now: .got 0x11d9ec20+0x13c8, .got.plt 0x11d9ffe8+0x40
194+
# -> .got.plt ends at 0x11DA0028, i.e. 40 bytes PAST the
195+
# window: those GOT slots stayed writable for the whole
196+
# process lifetime, in every Linux binary we have shipped
197+
# with -z now: .got.plt folded into .got (0x11d9ec20+0x13c8, ends
198+
# 0x11D9FFE8) -> the whole GOT is inside the window and is
199+
# re-mapped read-only after startup
200+
#
201+
# -z relro is already this toolchain's default and changes nothing here; it is
202+
# named anyway so the property stops depending on one distro's spec file (the
203+
# musl/portable and glibc-floor images are different toolchains, and a default
204+
# is not a guarantee). A1c/A1d in scripts/ci/check-binary-composition.sh assert
205+
# the OUTCOME on the produced binary, because a flag the compiler accepts is
206+
# not evidence that the artifact gained anything -- the same reason A1 exists
207+
# next to the .note.GNU-stack annotation.
184208
ELF_HARDENING_FLAGS :=
185209
ifeq ($(IS_LINUX),yes)
186210
ifneq ($(IS_MINGW),yes)
187-
ELF_HARDENING_FLAGS := -Wl,-z,noexecstack -Wl,-z,separate-code
211+
ELF_HARDENING_FLAGS := -Wl,-z,noexecstack -Wl,-z,separate-code \
212+
-Wl,-z,relro -Wl,-z,now
188213
endif
189214
endif
190215

‎scripts/ci/check-binary-composition.sh‎

Lines changed: 150 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
# contents independently inspectable; it does not establish which feature, if
1414
# any, caused an opaque third-party ML verdict.
1515
#
16-
# This script is the proof that each removal stayed removed. It asserts only
17-
# NEGATIVE properties (needle absent), plus one canary string we know ships,
18-
# because an absence check aimed at the wrong file — a compressed artifact, a
19-
# stub, a truncated download — would otherwise pass vacuously and read green.
20-
# A missing tool is a hard error for the same reason: a skipped assertion must
21-
# never look like a satisfied one.
16+
# This script is the proof that each removal stayed removed. The needle scans
17+
# assert NEGATIVE properties (needle absent), plus one canary string we know
18+
# ships, because an absence check aimed at the wrong file — a compressed
19+
# artifact, a stub, a truncated download — would otherwise pass vacuously and
20+
# read green. The A1* checks assert structural properties of the produced ELF
21+
# instead, for the mirror-image reason: a linker flag that was accepted is not
22+
# evidence that the binary gained anything, so the mitigation is measured in
23+
# the artifact. A missing tool is a hard error on both sides: a skipped
24+
# assertion must never look like a satisfied one.
2225
#
2326
# Usage: scripts/ci/check-binary-composition.sh <binary-or-dir>...
2427
# Directories are scanned recursively; format (ELF / Mach-O / PE) is detected
@@ -187,6 +190,62 @@ gnu_stack_flags() {
187190
esac
188191
}
189192

193+
# Echoes "<start-dec> <end-dec>" of the PT_GNU_RELRO window, empty if the
194+
# segment is absent, "unsupported" if the resolved reader cannot report it.
195+
# The hex→decimal conversion happens in the shell for the same reason
196+
# exec_load_bytes does it there: strtonum() is a gawk extension and CI's awk is
197+
# mawk, where it is undefined and the arithmetic would silently be 0.
198+
relro_range() {
199+
case "$ELF_READER_KIND" in
200+
readelf)
201+
"$ELF_READER" -lW "$1" 2>/dev/null |
202+
awk '/GNU_RELRO/ { print $3, $6; exit }' |
203+
while read -r vaddr memsz; do
204+
start=$((16#${vaddr#0x}))
205+
echo "$start $((start + 16#${memsz#0x}))"
206+
done
207+
;;
208+
objdump)
209+
echo unsupported
210+
;;
211+
esac
212+
}
213+
214+
# Echoes "<name> <start-dec> <end-dec>" for every GOT section, one per line.
215+
# The leading "[ 5]" index column is stripped BEFORE awk splits the line: its
216+
# width changes with the section count, so field numbers would otherwise shift
217+
# between binaries and the addresses would be read out of the wrong columns.
218+
got_sections() {
219+
"$ELF_READER" -SW "$1" 2>/dev/null |
220+
sed -e 's/^[[:space:]]*\[[[:space:]]*[0-9]*\][[:space:]]*//' |
221+
awk '$1 ~ /^\.got/ && $3 ~ /^[0-9a-fA-F]+$/ && $5 ~ /^[0-9a-fA-F]+$/ { print $1, $3, $5 }' |
222+
while read -r name addr size; do
223+
start=$((16#$addr))
224+
echo "$name $start $((start + 16#$size))"
225+
done
226+
}
227+
228+
# static | now | lazy — how the binary binds at load time.
229+
# Both tests are awk, not `grep -q`: grep exits on its first match, the reader
230+
# upstream takes EPIPE, and under `set -o pipefail` the satisfied case would be
231+
# reported as the failing one. awk consumes its whole input and cannot do that.
232+
bind_now_state() {
233+
if ! "$ELF_READER" -lW "$1" 2>/dev/null |
234+
awk '$1 == "DYNAMIC" { found = 1 } END { exit !found }'; then
235+
echo static
236+
return 0
237+
fi
238+
if "$ELF_READER" -dW "$1" 2>/dev/null |
239+
awk '/\(BIND_NOW\)/ { found = 1 }
240+
/\(FLAGS\)/ && /BIND_NOW/ { found = 1 }
241+
/\(FLAGS_1\)/ && / NOW([ ]|$)/ { found = 1 }
242+
END { exit !found }'; then
243+
echo now
244+
else
245+
echo lazy
246+
fi
247+
}
248+
190249
# ── Reporting ───────────────────────────────────────────────────────
191250
# PASS and FAIL both go to stdout so the per-assertion sequence stays in order
192251
# in a CI log (stderr would interleave nondeterministically); only the final
@@ -309,6 +368,91 @@ check_file() {
309368
printf 'n/a %-22s %s: segment-permission check is ELF-only\n' A1b-rodata-noexec "$token"
310369
fi
311370

371+
# A1c — RELRO. PT_GNU_RELRO is the window the loader re-maps read-only once
372+
# startup relocation is done. Without it .init_array, .fini_array,
373+
# .data.rel.ro and the GOT stay writable for the whole process lifetime,
374+
# which is what turns a stray write into control-flow hijack. ELF-only:
375+
# Mach-O and PE have no equivalent segment.
376+
relro_window=''
377+
if [ "$fmt" = elf ]; then
378+
relro_window=$(relro_range "$file")
379+
if [ "$relro_window" = unsupported ]; then
380+
printf 'n/a %-22s %s: %s cannot report segment addresses\n' \
381+
A1c-relro "$token" "$ELF_READER_KIND"
382+
elif [ -z "$relro_window" ]; then
383+
report FAIL A1c-relro "$token" \
384+
"no PT_GNU_RELRO program header — .data.rel.ro and the GOT stay writable for the process lifetime (link with -z relro)"
385+
else
386+
report PASS A1c-relro "$token" \
387+
"PT_GNU_RELRO covers $((${relro_window##* } - ${relro_window%% *})) bytes"
388+
fi
389+
else
390+
printf 'n/a %-22s %s: RELRO is a GNU/ELF segment\n' A1c-relro "$token"
391+
fi
392+
393+
# A1d — eager binding, asserted on the OUTCOME instead of on the flag.
394+
# -z now is what folds .got.plt into the RELRO window, but "the linker
395+
# accepted -z now" proves nothing about the artifact, so what is measured
396+
# here is the property itself: no GOT slot may be writable after startup,
397+
# i.e. every .got* section must lie inside A1c's window.
398+
#
399+
# That is not a formality on the shipped artifact. The Linux release
400+
# binaries are linked -static, and a static link on Ubuntu 24.04 / ld 2.42
401+
# emits PT_GNU_RELRO yet places .got.plt immediately PAST its end: in our
402+
# own release-shape binary .got.plt ran 0x11d9ffe8+0x40 against a window
403+
# ending at 0x11DA0000, so 40 bytes of GOT stayed writable for the process
404+
# lifetime. This assertion fails on that binary and passes on the one built
405+
# with -z now, which is the only reason to believe it measures anything.
406+
#
407+
# A binary with a PT_DYNAMIC is additionally required to carry
408+
# BIND_NOW/FLAGS_1 NOW, because there RELRO alone cannot help: lazy binding
409+
# writes the GOT after the loader has already re-protected it. A static
410+
# binary has no PT_DYNAMIC and nothing to bind at runtime, so GOT coverage
411+
# is the whole property there — a distinct reported outcome, never a skip.
412+
if [ "$fmt" != elf ]; then
413+
printf 'n/a %-22s %s: BIND_NOW is an ELF dynamic-section property\n' \
414+
A1d-bind-now "$token"
415+
elif [ "$relro_window" = unsupported ]; then
416+
printf 'n/a %-22s %s: %s cannot report section addresses\n' \
417+
A1d-bind-now "$token" "$ELF_READER_KIND"
418+
elif [ -z "$relro_window" ]; then
419+
report FAIL A1d-bind-now "$token" \
420+
"no PT_GNU_RELRO, so no GOT section can be read-only after relocation (see A1c)"
421+
else
422+
relro_start=${relro_window%% *}
423+
relro_end=${relro_window##* }
424+
got_seen=0
425+
got_writable=''
426+
while read -r got_name got_start got_end; do
427+
[ -z "$got_name" ] && continue
428+
got_seen=$((got_seen + 1))
429+
if [ "$got_start" -lt "$relro_start" ] || [ "$got_end" -gt "$relro_end" ]; then
430+
got_writable="$got_writable $got_name"
431+
fi
432+
done <<EOF
433+
$(got_sections "$file")
434+
EOF
435+
bind_state=$(bind_now_state "$file")
436+
if [ "$got_seen" -eq 0 ]; then
437+
# Same anti-vacuity rule as A0: with no GOT section to place, the
438+
# coverage test above is trivially satisfied and would read green.
439+
report FAIL A1d-bind-now "$token" \
440+
"no .got* section found ($ELF_READER) — the coverage test has nothing to check and would pass vacuously; section headers may have been removed"
441+
elif [ -n "$got_writable" ]; then
442+
report FAIL A1d-bind-now "$token" \
443+
"GOT section(s)$got_writable fall outside PT_GNU_RELRO [$relro_start,$relro_end) — they stay WRITABLE after relocation (link with -z now)"
444+
elif [ "$bind_state" = lazy ]; then
445+
report FAIL A1d-bind-now "$token" \
446+
"dynamic binary with neither BIND_NOW nor FLAGS_1 NOW — the GOT is filled lazily, after the loader has already applied RELRO (link with -z now)"
447+
elif [ "$bind_state" = static ]; then
448+
report PASS A1d-bind-now "$token" \
449+
"$got_seen GOT section(s) inside PT_GNU_RELRO; no PT_DYNAMIC, so nothing binds at runtime"
450+
else
451+
report PASS A1d-bind-now "$token" \
452+
"$got_seen GOT section(s) inside PT_GNU_RELRO and the dynamic section requests BIND_NOW"
453+
fi
454+
fi
455+
312456
# A2 — test-only seams.
313457
for needle in "${SEAM_NEEDLES[@]}"; do
314458
assert_absent "$file" "$token" A2-no-test-seams "$needle"

0 commit comments

Comments
 (0)