Repository navigation
fix(G602): track append() results for slice bounds checking - #1754
Merged
ccojocar merged 2 commits intoOct 5, 2026
Merged
Conversation
cipherprofessor
temporarily deployed
to
security-review
September 20, 2026 05:19 — with
GitHub Actions
Inactive
cipherprofessor
force-pushed
the
fix/g602-append-bounds-checking
branch
from
September 29, 2026 18:30
cea1f49 to
60f7009
Compare
cipherprofessor
temporarily deployed
to
security-review
September 29, 2026 18:31 — with
GitHub Actions
Inactive
Contributor
Author
|
Rebased onto current master (conflicts were in |
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
force-pushed
the
fix/g602-append-bounds-checking
branch
from
October 3, 2026 06:22
60f7009 to
c0f8cb2
Compare
cipherprofessor
temporarily deployed
to
security-review
October 3, 2026 06:22 — with
GitHub Actions
Inactive
…InsideBounds The new append() path adds a fourth call site, and unparam now reports that l always receives 0. Behaviour is unchanged.
cipherprofessor
temporarily deployed
to
security-review
October 5, 2026 05:13 — with
GitHub Actions
Inactive
ccojocar
approved these changes
Oct 5, 2026
This branch was previously deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes part of #1753
Problem
G602performed zero bounds-checking — guarded or unguarded — when the slice being indexed was the direct result of anappend()call.analyzers/slice_bounds.go'strackSliceBoundsfollows referrers of an*ssa.Alloc-backed slice to findIndexAddrviolations and correlatelen()guards, but itscase *ssa.Call:only recognizedlen()-guard calls and user-function calls — a call to theappendbuiltin 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 countappend(slice, ...)adds when that count is statically determinable, and wires the result intotrackSliceBounds's existingcase *ssa.Call:branch so theappend()result is tracked exactly likemake()/composite-literal slices already are — reusing the existingIndexAddrandlen()-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.Constplaceholder, recognized explicitly as zero added elements.append(s, x, y, ...)with one or more literal elements — the SSA builder synthesizes anAlloc-backed array to hold them;staticSliceLen's existingAlloc-based length computation applies directly.append(s, other...)spreading an existing slice with statically-known bounds —staticSliceLencomputes its guaranteed length (using the slice's actual length, not a 3-index slice's capacity, sinceappend()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
testutils/g602_samples.gocovering: unguarded out-of-bounds append growth (flags), a satisfiedlen()guard (doesn't flag), the existing cross-variable guard-mismatch case (still flags — no regression), a spread of unknown length (doesn't flag), zero-argappend()(flags), append inside a loop (doesn't flag), chainedappend(append(s,1),2)(flags), and multi-elementappend(s,1,2,3)(flags).go build ./...,go vet ./...,gofmt -l, and the fullgo test ./...are all clean.golangci-lintcannot 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 onmasterbut not after this fix. Onceappend()'s result is tracked,len(s_int) >= 3registers as a length guard, which reaches a separate, pre-existing bug inrunSliceBounds's guard-correlation loop: thecase 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 thebounded/upperBoundedarms just below it, which check both). I reproduced this identically on unmodifiedmasterusing plainmake()-backed slices with noappend()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) >= nguards if done carelessly) — filing it as its own issue rather than folding it into this PR.