Skip to content

[AUTOMATED] fix(analysis): clamp a symbol's declared extent before narrowing it, so one hostile st_size stops costing the whole analysis commit (DIV-91) - #342

Merged
mahaloz merged 1 commit into
mainfrom
fix/symbolsizeclamp
Aug 27, 2026
Merged

mahaloz merged 1 commit into
mainfrom
fix/symbolsizeclamp

Conversation

@mahaloz

@mahaloz mahaloz commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Closes #339 (engine half).

A single data symbol whose declared st_size is not representable as the int4 the
type factory sizes types in cost the binary its entire analysis commit — or the
process. st_size is a 64-bit ELF field that no header check validates, so it is
ordinary attacker-controlled data.

The defect

commit_analysis_output arm 4a narrowed the loader's data-symbol extent with
(*size).max(1) as int4 — it clamps the number after truncating it, so it
inspects the wrong one. Two classes fell straight through:

st_size Before After
0x1_0000_0000 (low 32 bits zero) truncates to 0 → error: read symbols (analysis commit) failed: g_a symbol created with zero size type, exit 1, no output loads, g_a named
0xffff_fff0 (low 32 bits sign-bit set) truncates to -16 → index out of bounds: the len is 9 but the index is 18446744073709551600, exit 101 loads, g_a named
8 (control) loads byte-identical

The first case is not "one symbol is dropped". commit_analysis_output mutates the
architecture arm by arm and propagates the first error, so arms 0–4 stay applied
and every later arm is abandoned: library prototypes, cppsig and DWARF prototypes,
ARM/MIPS context paints, tracked register values, call-fixups, DWARF stack locals,
DWARF line comments. It is also unretryable — pending_analysis and
loader_data_objects are both mem::taken before the failure, so a second
read symbols commits nothing.

The second case is not in the issue. The filing claims 0xfffffff0 "loads clean";
it does not — it is a process abort, and it is the more severe half. Verified verbatim
on the parent commit before the fix.

The fix (strict bug fix — no option, no catalog counts, no stage XML)

  1. kuna-console/src/engine.rs arm 4a — clamp into 1 ..= int4::MAX before the
    cast. One edit closes both classes at the one live site.
  2. Same shape applied to the DWARF twin at arm 1a. Not a live defect — d.size comes
    from a Datatype::get_size() (itself an int4) filtered to >= 1, so it already
    lives in 1..=int4::MAX — but the two arms must not differ in shape, and the arm
    comment says why that one is already bounded upstream.
  3. kuna-decomp/src/substrate/dtype.rs — a lower bound at both type-factory cache
    lookups (get_base, get_type_char_sized), so a negative size is an ordinary
    KunaError rather than a process abort if one ever arrives by another route.
    get_base refuses a negative rather than falling through to Datatype::new:
    find_add then computes an alignment from size as uint4, which overflows for
    s == -1 — a second, debug-only panic on the same input. (This differs from the
    scouted (0..9).contains(&s) shape for exactly that reason.)

The judgment call, measured rather than designed away

The clamp saturates, so a 0x1_0000_0004 symbol goes from a silently-wrong 4-byte
extent to a ~2 GiB one, and a ~2 GiB SymbolEntry wins find_container for every
address above it. That is a real cost and it is stated, not hidden:

# 8 named neighbours above a huge symbol, plus g_huge itself
st_size = 8           →  g_huge + n_1 + n_2 + n_3 + n_4 + n_5 + n_6 + n_7 + n_8
st_size = 0x7fffffff  →  g_huge[0] + g_huge[8] + g_huge[0xc] + ... + n_6 + g_huge[0x20] + n_8

Two things bound it, and both are measurements:

  • No new hazard class. A legal st_size = 0x7fffffff already produces byte-for-byte
    that output today, on the parent commit. Only symbols committed after the huge one
    are absorbed (n_6/n_8 come earlier in .symtab and survive); the rest lose their
    own name to a g_huge[offset] index.
  • Not an OOM and not a hang — the checked-for-worse outcome. The 2 GiB-extent
    fixture decompiles in 0.47 s / 53 MB peak RSS, the same as the sane-size control;
    an oversize extent becomes a TypeArray count, never an allocation.

Narrowing the extent to "distance to the next symbol" would fix the shadowing, but it
is a heuristic — a judgment call — and by the standing options contract it would need
its own named option. It is deliberately not smuggled in here.

Collateral: zero on real software, and structurally so

No STT_OBJECT symbol with st_size > int4::MAX exists in any ELF scanned: every
/usr/bin binary (5675 files), every /usr/lib/x86_64-linux-gnu shared object,
tests/bug-repro/*, and all committed fixtures. No previously-loading real binary
reaches the changed path. decompile-all --json base vs branch is byte-identical on
grep, sort, faillog, libselinux.so.1, fmt_x86_64 and cpp_mangled_x86_64.

What this PR deliberately does NOT change

Whether one bad symbol voids the whole analysis commit. There are 20 in-loop ? sites
in commit_analysis_output, each the same hazard; converting them to skip-and-collect
trades a loud failure for a quiet one and needs its own PR, a warning channel, and
probably a flag. The reported symptom — that kuna decompile swallowed this
entirely and exited 0 with degraded C while kuna functions exited 1 — is the
CLI-diagnostic half of GH-339 and ships in the sibling fix/clidiag PR, which
makes the failure loud on all surfaces. This PR does not touch
kuna-cli/src/decompile.rs, so the two do not conflict.

Tests

kuna-console/tests/verify_hostile_symbol_sizes.rs — 3 e2e cases over 3 new
checked-in fixtures built reproducibly from checked-in assembly (no byte-patching),
differing only in the st_size of g_a. The assertion is that the recovered
symbol name comes back, not merely that the load returns Ok — a silently dropped
symbol would satisfy the weaker one. Plus a TypeFactory unit test for the negative-size
guards.

A/B'd against the reverted source: the two hostile cases fail with exactly the
historical error and the historical panic, and the sane-size control passes both ways.

Neither parity corpus can cover this and a green parity run is not evidence the fix
works: every fixture in tests/datatests/ and tests/stages/ is a symbol-less
<bytechunk> that never constructs an ObjectLoadImage, so a .symtab-gated change
is unreachable from them in either direction. Correspondingly there is no new stage
XML
, so no kuna-base/src/xml.rs corpus-count bump and no baseline-stages.json
re-record — this PR cannot collide with the in-flight stage-test PRs.

Speed

No measurable change, and this host could not resolve one anyway. Interleaved,
alternating-arm, minimum-of-4
decompile-all --json, base vs branch:

Target base min branch min delta
libselinux.so.1 (621 fns) 8.694 s 8.680 s 0%
grep (449 fns) 15.837 s 14.914 s -5.00% (branch faster — noise)
null control: base vs itself, grep 15.502 s 14.820 s -4.00%

The null control establishes the noise floor at ~4%, which is already near the 5%
budget — the box was running two concurrent workspace suites (load average 15–23 on
80 cores). Structurally the change is one extra comparison per data symbol plus one
sign test per get_base.

Gates

Gate Result
make test datatests: 675/675 assertions passed / PARITY OK (no re-pin)
make test-stages datatests: 538/538 assertions passed / PARITY OK
make rust-test CARGO_EXIT=0 — 325 suites, 4838 passed, 0 failed, 38 ignored, 0 failure blocks
make check-spec check-spec OK (lenient mode) + check-spec OK (strict mode)
kuna catalog --check catalog OK: documents exactly the registered kuna options

No option added, so no catalog counts move. No stage XML added, so no
kuna-base/src/xml.rs corpus-count bump and no baseline-stages.json re-record
.

DIV

DIV-90 (docs/history.md). Warranted because the 0x1_0000_0004 class previously
loaded
and its shipped output changes (g_a → g_a[0], unnamed neighbours absorbed).
DIV-89 is the highest on main as of this branch; a sibling PR may claim 90 first, in
which case renumber the row and its references at merge.

🤖 Generated with Claude Code

https://claude.ai/code/session_011KpG7qK6BDFZyPnyo4r1c1

…so one hostile st_size stops costing the whole analysis commit (DIV-91)

[AUTOMATED]

`st_size` is a 64-bit ELF field that no header check validates, so it is
attacker-controlled data. Arm 4a of `commit_analysis_output` narrowed it with
`(*size).max(1) as int4` -- clamping AFTER the truncating cast, which inspects
the wrong number. Two classes fell through:

  * low 32 bits zero (0x1_0000_0000) truncates to 0, which `add_symbol_internal`
    rejects. Because the commit mutates the architecture arm by arm and
    propagates the first error, that one symbol abandoned every LATER arm
    (prototypes, context paints, tracked registers, call-fixups, DWARF locals
    and line comments) with arms 0-4 already applied in place -- and both stashes
    are `mem::take`n by then, so a second `read symbols` commits nothing.
    `kuna functions` exited 1 with no output.
  * low 32 bits with the sign bit set (0xffff_fff0) truncates to a NEGATIVE
    int4, which `TypeFactory::get_base` handed to `typecache[s as usize]` -- an
    out-of-bounds index, so this was a process abort at exit 101, not an error.
    The issue as filed claims this case loads clean; it does not.

Clamp into `1 ..= int4::MAX` before the cast at the one live site, apply the
same shape to the DWARF twin at arm 1a (already bounded upstream, noted in the
arm comment), and add a lower bound at both type-factory cache lookups so a
negative size is an ordinary KunaError rather than an abort. `get_base` refuses
a negative rather than falling through to `Datatype::new`, because `find_add`
then computes an alignment from `size as uint4` and overflows for s == -1.

The clamp saturates, so a 0x1_0000_0004 symbol goes from a silently-wrong
4-byte extent to a ~2 GiB one that shadows unnamed addresses above it. Measured
and stated rather than designed away: a legal st_size of 0x7fffffff already
reaches that exact state today, and it is neither an OOM nor a hang (0.47s /
53 MB, same as the sane-size control). Narrowing the extent to "distance to the
next symbol" is a heuristic that would need its own named option.

Whether one bad symbol voids the whole commit is deliberately unchanged; the
CLI-diagnostic half of GH-339 ships in the sibling fix/clidiag PR.

Closes #339 (engine half).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011KpG7qK6BDFZyPnyo4r1c1

Interleaved min-of-4 with a null control (the box was running two concurrent
workspace suites) is recorded on the DIV row.
@mahaloz
mahaloz force-pushed the fix/symbolsizeclamp branch from 34802c8 to 56196b2 Compare August 27, 2026 19:19
@mahaloz mahaloz changed the title [AUTOMATED] fix(analysis): clamp a symbol's declared extent before narrowing it — one hostile st_size stops costing the whole analysis commit (DIV-90) [AUTOMATED] fix(analysis): clamp a symbol's declared extent before narrowing it, so one hostile st_size stops costing the whole analysis commit (DIV-91) Aug 27, 2026
@mahaloz
mahaloz merged commit b61835b into main Aug 27, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/symbolsizeclamp branch August 27, 2026 19:24
mahaloz added a commit that referenced this pull request Aug 27, 2026
[AUTOMATED] DIV-90/91/92 were claimed by #341/#342/#344 while this branch was
in flight; the row and every reference move to DIV-93.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011KpG7qK6BDFZyPnyo4r1c1
mahaloz added a commit that referenced this pull request Aug 27, 2026
…e aggressive preset (DIV-93) (#345)

* feat(modes): the four ARM entry-discovery options join the aggressive preset (DIV-90)

cortexmvectors (#248), ptrentry (#255), tailcallentry (#259) and poolentry (#278)
all shipped default-OFF and sat on the UNEVALUATED list in modes.rs, so they were
absent from AGGRESSIVE_OVERRIDES. `auto` resolves to `aggressive` under 500 KiB,
and the preset is what the whole-binary surfaces actually run, so all four were
INERT on every default path -- the DIV-64 itaniumrtti trap, four times over. The
default path's stripped-ARM entry recall had not moved since they merged.

They are flipped jointly because they compose, which is exactly what the
UNEVALUATED comment demanded before any flip.

Measured over the 110 stripped non-x86-64 decbench twins against their unstripped
twins' symbol tables (50,724 ground-truth function starts), --mode aggressive:

  arm       entries   exact   midbody   recall     added  added_exact  lost_exact
  base        55006   44957      8333   88.63%         -            -           -
  cortexm     55142   45093      8333   88.90%       136          136           0
  ptr         56594   46522      8333   91.72%      1588         1565           0
  tail        55655   45602      8337   89.90%       649          645           0
  pool        53956   45122      7116   88.96%       167          165           0
  all4        56188   47330      7117   93.31%      2402         2373           0

Recall and precision improve together: +2,373 real entries (98.8% of everything
added is a real function start) while mid-body false entries FALL 8,333 -> 7,117.
No ground-truth entry is lost by any arm -- poolentry's 1,217 removals contain
zero ground-truth addresses. 0 of 110 binaries lose recall.

Restricted to the 105 binaries `auto` actually sends to `aggressive` (< 500 KiB),
the default-path number is 90.92% -> 96.06%. The 5 images at/over the threshold
resolve to `reliable` and are disclosed in the DIV row rather than papered over.

Non-ARM inertness is measured: identical entry sets over 90 x86-64 twins and the
12 i386 PE images in the ARM corpus, byte-identical emitted C over 8 x86-64
binaries. Speed, interleaved min-of-4: decompile-all cf2.elf -5.8% (a speedup),
nuttx +3.5%, x86-64 controls at noise; discovery-only `kuna functions` +6.2%,
which is more work for more output on the one surface that only does discovery.

The 0/675 ablation is proven, not asserted: a throwaway build with all four forced
ON as engine defaults -- strictly stronger than preset membership, which the XML
harness never applies -- still reports 675/675 and 538/538 PARITY OK.

Also lands PR #239's two doc corrections, repointed at the shipped feature docs
rather than at its unshipped-proposal directory: the recall-measurement.md
mechanism/noise correction and the missing-ghidra-analyses.md
OperandReferenceAnalyzer row (its "function creation is subsumed by entry-disc"
claim is what ptrentry disproved).

[AUTOMATED] Generated by Claude Code.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011KpG7qK6BDFZyPnyo4r1c1

* docs(decbench): retire the two round-ledger claims DIV-90 falsifies

features-round4.md Rank 6 was a filed, still-open recommendation to flip
`tailcallentry` into the preset ("aggressive does not carry it"); DIV-90 does
exactly that, so it is marked SHIPPED with the sweep it asked for. features.md
still called step 3 "in flight" and did not know about step 5 (`poolentry`, #278).

[AUTOMATED] Generated by Claude Code.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011KpG7qK6BDFZyPnyo4r1c1

* docs: state the ARM restriction precisely -- poolentry has no arch gate

cortexmvectors/ptrentry/tailcallentry each early-return on
`object::Architecture::Arm`; poolentry does not, and is ARM-only in EFFECT
because it keys on PC-relative literal pools. The measured inertness is the same
either way (identical entry sets on 90 x86-64 twins and 12 i386 PE images), but
"all four are ARM-gated" overstated the mechanism for one of them.

[AUTOMATED] Generated by Claude Code.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011KpG7qK6BDFZyPnyo4r1c1

* docs(history): add the leave-one-out direction to the DIV-90 evidence

Dropping each option from the combination loses 27 / 1427 / 645 / 136 ground-truth
entries respectively, and dropping poolentry is the only one that also raises
mid-body false entries (7117 -> 8337). No option is redundant given the others.

[AUTOMATED] Generated by Claude Code.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011KpG7qK6BDFZyPnyo4r1c1

* docs: renumber the ARM-preset divergence to DIV-93

[AUTOMATED] DIV-90/91/92 were claimed by #341/#342/#344 while this branch was
in flight; the row and every reference move to DIV-93.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011KpG7qK6BDFZyPnyo4r1c1

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[AUTOMATED] A symbol whose st_size has zero low 32 bits kills the analysis commit — loudly on three surfaces, silently on kuna decompile

1 participant