-
Notifications
You must be signed in to change notification settings - Fork 5k
build(linux): enable full RELRO, stack canaries, and _FORTIFY_SOURCE=3 #35751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
robobun
wants to merge
11
commits into
main
Choose a base branch
from
farm/fa4d619e/hardening-tranche1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a3e5776
build(linux): enable full RELRO, stack canaries, and _FORTIFY_SOURCE=3
robobun c3481a8
bsd.c: drop superfluous mode argument from O_PATH|O_DIRECTORY open
robobun 8c4ad16
test: assert full RELRO and stack canaries on linux ELF binaries
robobun e085e6b
process.getgroups: check the second getgroups(2) return value
robobun 26b6c66
ci: retrigger
robobun 0dfefe7
exe_format(elf): select the last PF_W PT_LOAD, not the first
robobun 7ef2669
exe_format(elf): tighten segment-selection comment; name the new erro…
robobun 7ac60d9
exe_format(elf): compress relro comment to one line
robobun f13302d
test(compile): compare PT_LOAD count to the source bun binary
robobun 154140f
[autofix.ci] apply automated fixes
autofix-ci[bot] d9cf132
build: acknowledge tranche-1 hardening size cost [skip size check]
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| #!/usr/bin/env bash | ||
| # Usage: scripts/verify-hardening.sh <path-to-bun> | ||
| # | ||
| # Prints a hardening truth table for a Linux bun binary using readelf and a | ||
| # live /proc/<pid>/maps probe, then a PASS/FAIL line per control. Exit status | ||
| # is the FAIL count. | ||
| # | ||
| # Controls checked: | ||
| # PIE ET_DYN (image participates in ASLR) | ||
| # RELRO GNU_RELRO segment present + DT_BIND_NOW (full RELRO) | ||
| # CANARY __stack_chk_fail imported | ||
| # FORTIFY at least one __*_chk@ libc import | ||
| # CET x86 IBT/SHSTK feature bits in .note.gnu.property | ||
| # NX GNU_STACK is RW, not RWE | ||
| # JIT-W^X no rwx mapping in a live process | ||
| # | ||
| # PIE/CET/JIT-W^X are expected FAIL today (tranche 2+). RELRO/CANARY/FORTIFY | ||
| # are the tranche-1 targets this patch flips to PASS. | ||
|
|
||
| set -uo pipefail | ||
|
|
||
| BIN=${1:?"usage: $0 <binary>"} | ||
| [[ -r "$BIN" ]] || { echo "error: cannot read $BIN" >&2; exit 64; } | ||
|
|
||
| readelf=${READELF:-readelf} | ||
| hdr=$("$readelf" -hW "$BIN" 2>/dev/null) | ||
| seg=$("$readelf" -lW "$BIN" 2>/dev/null) | ||
| dyn=$("$readelf" -dW "$BIN" 2>/dev/null) | ||
| dynsym=$("$readelf" --dyn-syms -W "$BIN" 2>/dev/null) | ||
| notes=$("$readelf" -nW "$BIN" 2>/dev/null) | ||
|
|
||
| elf_type=$(grep -oE 'Type:[[:space:]]+[A-Z]+' <<<"$hdr" | awk '{print $2}') | ||
| has_relro=$(grep -c 'GNU_RELRO' <<<"$seg") | ||
| has_bindnow=$(grep -cE 'BIND_NOW|FLAGS_1.*\bNOW\b' <<<"$dyn") | ||
| stack_perm=$(grep 'GNU_STACK' <<<"$seg" | grep -oE 'RW[E ]' | tr -d ' ') | ||
| n_canary=$(grep -c '__stack_chk_fail' <<<"$dynsym") | ||
| n_fortify=$(grep -cE '__[a-z_]+_chk(@|$)' <<<"$dynsym") | ||
| has_cet=$(grep -cE 'IBT|SHSTK|x86 feature:' <<<"$notes") | ||
| n_plt=$("$readelf" -rW "$BIN" 2>/dev/null | grep -c JUMP_SLOT) | ||
|
|
||
| # Live probe: start the binary, read its maps, look for rwx and record the | ||
| # image base (so repeated invocations show whether ASLR moved it). | ||
| rwx_count="n/a" | ||
| rwx_detail="" | ||
| image_base="n/a" | ||
| if [[ -x "$BIN" ]]; then | ||
| maps=$("$BIN" -e ' | ||
| const fs = require("fs"); | ||
| process.stdout.write(fs.readFileSync("/proc/self/maps","utf8")); | ||
| ' 2>/dev/null) | ||
| if [[ -n "$maps" ]]; then | ||
| rwx_count=$(grep -cE '\brwx' <<<"$maps") | ||
| rwx_detail=$(grep -E '\brwx' <<<"$maps" | head -1 | awk '{print $1, $NF}') | ||
| image_base=$(head -1 <<<"$maps" | cut -d- -f1) | ||
| fi | ||
| fi | ||
|
|
||
| row() { printf ' %-10s %-6s %s\n' "$1" "$2" "$3"; } | ||
| off_on() { [[ "$1" -gt 0 ]] && echo ON || echo OFF; } | ||
|
|
||
| echo "hardening: $BIN" | ||
| echo | ||
| row CONTROL STATE EVIDENCE | ||
| row PIE "$([[ "$elf_type" == DYN ]] && echo ON || echo OFF)" "ELF type=$elf_type, image base=$image_base" | ||
| row RELRO "$([[ "$has_relro" -gt 0 && "$has_bindnow" -gt 0 ]] && echo full || { [[ "$has_relro" -gt 0 ]] && echo part || echo OFF; })" "GNU_RELRO=$has_relro BIND_NOW=$has_bindnow PLT=$n_plt" | ||
| row CANARY "$(off_on "$n_canary")" "__stack_chk_fail imports=$n_canary" | ||
| row FORTIFY "$(off_on "$n_fortify")" "*_chk imports=$n_fortify" | ||
| row CET "$(off_on "$has_cet")" "$([[ "$has_cet" -gt 0 ]] && grep -E 'IBT|SHSTK' <<<"$notes" | head -1 | xargs || echo 'no .note.gnu.property x86 feature')" | ||
| row NX "$([[ "$stack_perm" == RW ]] && echo ON || echo OFF)" "GNU_STACK=$stack_perm" | ||
| row JIT-W^X "$([[ "$rwx_count" == 0 ]] && echo ON || echo OFF)" "rwx maps=$rwx_count${rwx_detail:+ ($rwx_detail)}" | ||
| echo | ||
|
|
||
| fails=() | ||
| [[ "$elf_type" == DYN ]] || fails+=(PIE) | ||
| [[ "$has_relro" -gt 0 && "$has_bindnow" -gt 0 ]] || fails+=(RELRO) | ||
| [[ "$n_canary" -gt 0 ]] || fails+=(CANARY) | ||
| [[ "$n_fortify" -gt 0 ]] || fails+=(FORTIFY) | ||
| [[ "$has_cet" -gt 0 ]] || fails+=(CET) | ||
| [[ "$stack_perm" == RW ]] || fails+=(NX) | ||
| [[ "$rwx_count" == 0 || "$rwx_count" == "n/a" ]] || fails+=(JIT-W^X) | ||
|
|
||
| if [[ ${#fails[@]} -eq 0 ]]; then | ||
| echo "PASS: all controls" | ||
| else | ||
| echo "FAIL: ${fails[*]}" | ||
| fi | ||
| exit ${#fails[@]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunExe, isLinux } from "harness"; | ||
| import { realpathSync } from "node:fs"; | ||
|
|
||
| // Linux-only: asserts the ELF hardening properties that scripts/build/flags.ts | ||
| // is expected to produce. Kept in the test suite so a flag regression (e.g. | ||
| // reintroducing -z norelro, or a strip tool that drops PT_GNU_RELRO) fails CI | ||
| // rather than shipping silently. See scripts/verify-hardening.sh for the full | ||
| // audit including the controls this change does not flip (PIE/CET/JIT W^X). | ||
|
|
||
| async function readelf(args: string[]): Promise<string> { | ||
| const bin = realpathSync(bunExe()); | ||
| await using proc = Bun.spawn({ | ||
| cmd: ["readelf", ...args, bin], | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [out, exited] = await Promise.all([proc.stdout.text(), proc.exited]); | ||
| expect(exited).toBe(0); | ||
| return out; | ||
| } | ||
|
|
||
| describe.skipIf(!isLinux)("binary hardening (linux ELF)", () => { | ||
| test("link: full RELRO", async () => { | ||
| const seg = await readelf(["-lW"]); | ||
| // -Wl,-z,relro: PT_GNU_RELRO segment must survive strip. | ||
| expect(seg).toContain("GNU_RELRO"); | ||
|
|
||
| // -Wl,-z,now: DT_BIND_NOW or DF_1_NOW so ld.so eagerly binds and then | ||
| // mprotects the RELRO segment read-only. | ||
| const dyn = await readelf(["-dW"]); | ||
| expect(dyn).toMatch(/BIND_NOW|\bNOW\b/); | ||
| }); | ||
|
|
||
| test("compile: stack canaries", async () => { | ||
| // -fstack-protector-strong on the C/C++ side pulls in __stack_chk_fail. | ||
| const syms = await readelf(["--dyn-syms", "-W"]); | ||
| expect(syms).toContain("__stack_chk_fail"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.