Skip to content

fix(G602): track append() results for slice bounds checking - #1754

Merged
ccojocar merged 2 commits into
securego:masterfrom
cipherprofessor:fix/g602-append-bounds-checking
Oct 5, 2026
Merged

ccojocar merged 2 commits into
securego:masterfrom
cipherprofessor:fix/g602-append-bounds-checking

Conversation

@cipherprofessor

Copy link
Copy Markdown
Contributor

Fixes part of #1753

Problem

G602 performed zero bounds-checking — guarded or unguarded — when the slice being indexed was the direct result of an append() call. analyzers/slice_bounds.go's trackSliceBounds follows referrers of an *ssa.Alloc-backed slice to find IndexAddr violations and correlate len() guards, but its case *ssa.Call: only recognized len()-guard calls and user-function calls — a call to the append builtin matched neither, so the grown slice's value was dropped and never tracked further, regardless of whether the subsequent index access was actually guarded.

Fix

Adds staticAppendGrowth/staticSliceLen (additive only, in the same file) to compute the guaranteed element count append(slice, ...) adds when that count is statically determinable, and wires the result into trackSliceBounds's existing case *ssa.Call: branch so the append() result is tracked exactly like make()/composite-literal slices already are — reusing the existing IndexAddr and len()-guard correlation machinery rather than building a parallel one.

Three shapes are handled:

  • append(s) with no variadic arguments — the SSA builder lowers this to a *ssa.Const placeholder, recognized explicitly as zero added elements.
  • append(s, x, y, ...) with one or more literal elements — the SSA builder synthesizes an Alloc-backed array to hold them; staticSliceLen's existing Alloc-based length computation applies directly.
  • append(s, other...) spreading an existing slice with statically-known bounds — staticSliceLen computes its guaranteed length (using the slice's actual length, not a 3-index slice's capacity, since append() only ever adds as many elements as the spread value's length).

Spreading a slice of unknown length (e.g. a function parameter) is left untracked rather than guessed, so this doesn't introduce new false positives.

Verification

  • Regression tests added to testutils/g602_samples.go covering: unguarded out-of-bounds append growth (flags), a satisfied len() guard (doesn't flag), the existing cross-variable guard-mismatch case (still flags — no regression), a spread of unknown length (doesn't flag), zero-arg append() (flags), append inside a loop (doesn't flag), chained append(append(s,1),2) (flags), and multi-element append(s,1,2,3) (flags).
  • TDD red→green confirmed for the core regression test against pre-fix code.
  • go build ./..., go vet ./..., gofmt -l, and the full go test ./... are all clean.
  • Manually verified against the issue's own reproduction with the built CLI.
  • golangci-lint cannot run locally due to a pre-existing Go 1.26/golangci-lint toolchain version mismatch, unrelated to this change (confirmed identical failure against a clean base with zero diff present); CI covers it.

Known limitation (disclosed, not fixed here)

This fix causes a real, verified detection change on issue #1753's own reproduction: if len(s_int) >= 3 { ...; s_int2[6] } (a guard on one slice incorrectly "protecting" a different slice's out-of-bounds index) is flagged on master but not after this fix. Once append()'s result is tracked, len(s_int) >= 3 registers as a length guard, which reaches a separate, pre-existing bug in runSliceBounds's guard-correlation loop: the case upperUnbounded, unbounded: delete(issues, instr) arm clears any constant-index violation in its guarded block outright, with no check on the index value or which slice it belongs to (unlike the bounded/upperBounded arms just below it, which check both). I reproduced this identically on unmodified master using plain make()-backed slices with no append() involved at all, so it predates and is independent of this fix — append()-derived slices now simply reach the same pre-existing machinery every other tracked slice already goes through.

Fixing that arm is a separate, larger change (an index/slice-identity check consistent with the other arms, with its own test surface and real risk of new false positives on legitimate len(s) >= n guards if done carelessly) — filing it as its own issue rather than folding it into this PR.

@cipherprofessor

Copy link
Copy Markdown
Contributor Author

Rebased onto current master (conflicts were in testutils/g602_samples.go, from #1746/#1749/#1757 landing in the same sample list). One test expectation changed as a result: the append(s, 10); if len(s) >= 3 { s[6] } case now expects 1 finding instead of 0. It previously locked in the pre-#1757 behaviour where >= guards were treated as unbounded; with #1757 a >= guard is only a lower bound, so append()-derived slices now reach the same result as make()-backed ones. Analyzer suite passes.

@ccojocar

ccojocar commented Oct 2, 2026

Copy link
Copy Markdown
Member

Please could you rebase and solve the conflicts?

An IndexAddr whose base was a *ssa.Call result (append()'s return value)
never entered any bounds-checking path: trackSliceBounds's case *ssa.Call
only recognized len()-guard calls and user-function calls, silently
dropping append(). This left every access into an append()-grown slice
unchecked, guarded or not.

Add staticAppendGrowth/staticSliceLen to compute the guaranteed element
count append(slice, ...) adds when the appended count is statically
known, and wire the result into trackSliceBounds so the append() call is
tracked like make()/composite literals already are, reusing the existing
IndexAddr and len()-guard correlation machinery.

staticAppendGrowth handles three shapes of a statically-known count:
- append(s) with no variadic arguments: the SSA builder always lowers
  this to append(s, nil...), a *ssa.Const placeholder, not a *ssa.Slice --
  recognized explicitly as zero added elements rather than falling
  through to staticSliceLen, which would otherwise reject it.
- append(s, x, y, ...) with one or more literal elements: the SSA builder
  synthesizes an Alloc-backed array/slice to hold them and spreads it, so
  staticSliceLen's Alloc-based length computation applies directly.
- append(s, other...) spreading an existing *ssa.Slice over a
  *ssa.Alloc-backed array with statically-known bounds: staticSliceLen
  computes its guaranteed length, using h-l (the slice's actual length)
  rather than ComputeSliceNewCap's maxIdx-l (its capacity) for a
  3-index slice expression, since append() only ever adds as many
  elements as the spread value's length, never its capacity.
Spreading a slice of unknown length (e.g. a function parameter) is left
untracked (no assumed growth) rather than guessed, avoiding new false
positives.

Verified against master via SSA dump and confirmed empirically with the
built CLI: a "len(s) == N" guard on an append()-grown slice now reaches
the same trust model already established for make()-backed slices by
fires correctly.

Disclosure -- a real detection regression on issue securego#1753's own
reproduction, verified and deliberately left as-is: base flags
`if len(s_int) >= 3 { ...; s_int2[6] }` (a guard on one slice incorrectly
"protecting" a different slice's out-of-bounds index), but this fix does
not. Once append()'s result is tracked, `len(s_int) >= 3` now registers
as a length guard in `ifs`, which reaches a separate, pre-existing bug in
runSliceBounds's ifs correlation loop: the
`case upperUnbounded, unbounded: delete(issues, instr)` arm clears any
constant-index violation found in its guarded block outright, with no
check on the index value or on which slice it belongs to (unlike the
`bounded`/`upperBounded` arms just below it, which do check both).
Reproduces identically on unmodified master with plain make()-backed
slices and no append() involved at all, so it predates and is independent
of this fix -- append()-derived slices now simply reach the same
pre-existing machinery every other tracked slice already goes through,
inheriting both its correct equality-guard behavior and this looseness
for inequality guards. Fixing that arm is a separate, larger change (an
index/slice-identity check consistent with the other arms) left for a
follow-up issue rather than folded in here.

golangci-lint cannot run locally due to a Go 1.26/golangci-lint toolchain
version mismatch unrelated to this change; CI covers it.

Fixes part of securego#1753
@cipherprofessor
cipherprofessor force-pushed the fix/g602-append-bounds-checking branch from 60f7009 to c0f8cb2 Compare October 3, 2026 06:22
…InsideBounds

The new append() path adds a fourth call site, and unparam now reports
that l always receives 0. Behaviour is unchanged.
@ccojocar
ccojocar merged commit 1f52e64 into securego:master Oct 5, 2026
11 of 12 checks passed

This branch was previously deployed

1 inactive deployment
security-review — 32b43f93 Deployed Oct 5, 2026 by cipherprofessor via barry-ai-security-review #2634
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.

2 participants