Repository navigation
fix: four defects behind twelve G115 alerts, and the gate that could not see them (#525) - #529
Merged
Merged
Conversation
hdr.Mode is an int64 read from an archive this process did not write; the tar mode field is 12 bytes of octal, or GNU base-256 which can carry a negative. ArchiveEntry.Mode is a uint32, and the conversion between them was unchecked. Truncation is the defect, not the G115 warning: 0x1_0000_0644 narrows to 0644 and -1 narrows to 0777 with every special bit set. A crafted archive got to state one mode and have a different, entirely plausible one recorded, with nothing downstream able to tell the difference. Go's own archive/tar writes and reads back both values in its default format — measured, and only the PAX writer refuses them — so this is reachable with a file. The bound is representability rather than the permission mask, and that is a choice against the stricter rule. internal/vfs rejects a mode carrying bits outside 0o7777 and copying that here would look more careful, but Apache Commons Compress writes 0100644 for a regular file, so the strict rule rejects archives from a mainstream producer. Inside [0, MaxUint32] the value is recorded exactly as stated: this is a metadata surface reporting what an archive says, and no production path reads the field to make an access decision.
…ides (#525) `"feature_" + string(rune(index))` builds the Unicode codepoint numbered index, not its decimal text, so index 6 produced "feature_\x06" — a control character — where "feature_6" was meant. gosec reported the int→rune overflow, which is the least of it: the name was garbage for every index the branch could reach. The same expression was written twice, and that was the worse half. getFeatureName named the key a weight is written under and predict named the key it is read by, each from its own literal list. Two lists that drift leave training updating a weight prediction never consults, and a map lookup that misses is not an error, so nothing would report it. There is now one list and one function, so the two sides agree by construction rather than by review. Unreachable today — createTrainingExamples emits three features and the list holds six — which is the argument for fixing it rather than suppressing it. The trap springs on whoever adds a seventh.
…zero (#525) GetConnection's round-robin is `nextIndex.Add(1) % uint32(len(cp.managers))`. gosec reported the int→uint32 conversion, which needs a pool of more than four billion managers before it can misrepresent anything. It said nothing about the `% 0` in the same expression, which needs none: NewConnectionPool accepts size 0 without complaint, a pool size read from configuration is how that happens, and the first GetConnection then panicked with an integer divide by zero — in the package whose entire subject is surviving failures. Guarded at the point of use rather than in the constructor: NewConnectionPool returns no error and is published surface, so giving it one is a breaking change for a defect that is answerable where it bites. The answer names the actual condition. Falling through to the retry loop instead would produce "no healthy connections available" — the loop has nothing to iterate — which sends an operator to look at connection health for a pool that was never built. The conversion itself keeps a `#nosec G115` recording the bound, because it is real: one ConnectionManager per unit of size, so narrowing needs more live connections than the process could hold. Verified with gosec -include=G115: the package now reports nothing. The test also sweeps HealthyCount, GetStats and ConnectAll on an empty pool, and asserts which error GetConnection returns rather than that it returns one. Refs #525
… let lint see G115 (#525) Two things, because the second is why the first was invisible. ## The part ceiling CalculateOptimalChunkSize's size ladder tops out at eight times the configured base chunk. With the 16 MiB default that is 128 MiB, so 10,000 parts cover 1.25 TiB — but the base is configuration, and at a 5 MiB base the ceiling binds at 400 GiB. Past it, S3 rejects every part number above 10,000 individually, after the first 10,000 have been transferred, billed, and left in the bucket for an abort to clean up. The chunk size now grows to keep the count inside the ceiling, which is what S3 wants for an object that size anyway, and is capped at the 5 GiB maximum part. Fixed in the pure function and not at its one call site: today's path holds the whole object in memory and so cannot reach 1.25 TiB, but the invariant then holds for anything that computes a part count from it, including a streaming path for which the ceiling is immediately live. TestCalculateOptimalChunkSizeRespectsThePartCeiling asserts the property — the chunk divides the file into no more than 10,000 parts, and stays between S3's 5 MB minimum and 5 GiB maximum — over three base sizes, because the table above it can only restate the formula it checks. Six mutations, all caught; the first two runs left the min and max clamps asserted but never exercised, which the sweep's sizes now reach. ## The gate G115 was excluded globally in .golangci.yml, and the only other run that had it is security.yml's standalone gosec, which runs with -no-fail and reports into code scanning where nothing blocks a merge. So no gate anywhere could fail on a G115, which is how twelve alerts accumulated. Lifting the exclusion produced 40 findings: 33 in _test.go, where the conversion is the test building its own input, and 7 in production, of which 4 were defects — the three fixed in the preceding commits plus this one. Three of the four were adjacent to what gosec described rather than the conversion itself, which is the argument for reading a check that points near a defect, not for turning it off. The 33 test findings are excluded by a _test.go path rule modelled on the existing G404 one. Five production sites carry `#nosec G115 -- <why it is bounded>`, honored by both runs: the two multipart part numbers (now bounded by construction), ListObjects' MaxKeys (bounded to 1..1000 by the min above and the limit return below), difftest's FillBytes (where truncation is the operation), and health's heap-delta log line. sdks/c gets a plain comment, since no directive of either form works in a cgo package; that is #200's finding and its header already records it. Both halves verified by measurement rather than by reading the config: an unbounded int64→uint16 added to internal/health is reported by lint, and removing the _test.go rule brings 7 findings back in internal/compression. gosec -include=G115 over internal/, pkg/ and cmd/ now reports nothing outside _test.go. Refs #525
…525) `Security Scan` failed on this branch with seven '-' lines and no '+' lines: every `#nosec G115 -- <why>` added to a bounded conversion took its finding out of the standalone gosec report as well as out of golangci-lint's, and the two conversions that were defects went away with the fixes. The gate's own message says what to do — remove the slot in the same commit — because a slot kept after its finding is fixed is permanent headroom for the next conversion in that file. Which means the ratchet worked in the direction that usually goes unbuilt. I did not predict this failure; the gate found it. What is left is `sdks/c/main.go`'s three, which are in the file because no directive can reach them: cgo collapses the call and any nearby comment onto one synthetic position, so a `#nosec` there lands on a line gosec is not reading (#200). `TestGosecBaselineNamesFilesThatExist` asserted a floor of five entries on the reasoning that twelve findings across eight files could not honestly shrink below it. That was a count copied into a test, and it went stale exactly the way a version number in prose does — it failed this edit. Replaced with the two structural claims that cannot go stale: the file parses (an unparsable baseline reads as "all findings fixed", which is the one way it can lie), and it still accounts for the cgo file.
# Conflicts: # CHANGELOG.md
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.
Closes #525.
The gate first, because it is why the rest accumulated
G115 was excluded globally in
.golangci.yml. The only other run that had it issecurity.yml'sstandalone
gosec -fmt sarif -no-fail, which reports into code scanning, where nothing blocks a merge.So no gate anywhere could fail on a G115 — the twelve alerts in the issue were not a backlog of
things a gate had flagged and someone had deferred; they were things nothing was able to flag.
The config's own comment recorded, as a caveat, that the exclusion had hidden a real
uint32narrowingin
s3/config.go:202for as long as that site carried a//nolint. That was not a caveat. It was thefinding.
Lifting it produces 40 findings: 33 in
_test.go, 7 in production, 4 of them defects._test.gopath rule with the rationale attached, modelled onthe existing G404 rule. In a test the conversion is the test building its own input, and a test that
passed a value it did not mean would fail.
#nosec G115 -- <why it is bounded>, which both runs honour.sdks/cgets a plain comment, because no directive of either form works in a cgo package — sdks/c: audit the G115 integer conversions that SARIF now drops — nothing else tracks them #200'sfinding, already recorded in that file's header.
Both halves are verified by measurement, not by reading the config, since "correctly suppressed" and
"still globally excluded" look identical from every angle except the outcome:
int64→uint16added tointernal/healthlintreportsG115: integer overflow conversion int64 -> uint16_test.gorule removed,./internal/compression/gosec -include=G115 ./internal/... ./pkg/... ./cmd/..._test.goThe four defects
Three of the four are adjacent to what gosec described rather than the conversion itself, which is
the argument for reading a check that points near a defect.
1. A tar entry could state one mode and have a different, plausible one recorded
BuildIndexFromBytesnarrowedtar.Header.Mode(int64, from a 12-byte field of an archive thisprocess did not write) to the
uint32anArchiveEntryrecords.0x1_0000_0644narrowed to0644;-1to0777with every special bit. Nothing downstream could tell.Go's own
archive/tarwrites and reads back both values in its default and GNU formats — measuredwith a scratch program, not assumed — so producing such an archive takes no special tooling.
An entry whose mode cannot be recorded as stated now stops the index with
ErrMalformedEntry. Thebound is representability and deliberately not the permission mask: a tar mode field legitimately
carries file-type bits, and Apache Commons Compress writes
0100644for a regular file, so maskingwould reject archives from a mainstream producer. That row is in the table and is load-bearing — a
mutation tightening the bound to
0o7777is caught by it.2. Four of six model weights were named after control characters, and training wrote keys prediction never read
The fallback was
"feature_" + string(rune(index)), which builds the codepoint numbered index — sofeature 6 was
"feature_\x06". Worse, the named-feature list existed twice, ingetFeatureNameand inline in
PredictionModel.predict, and the two disagreed past index 5.updateModelwroteweights[featureName(i)]andpredictlooked up a different key; a map lookup that misses is not anerror, so the feature contributed nothing and training on it had no effect at all.
One
featureNamefunction now serves both sides, so they agree by construction.TestTrainingAndPredictionAgreeOnWeightKeysasserts the coupling rather than the spelling.3. An empty connection pool panicked
recovery.ConnectionPool.GetConnectioncomputednextIndex.Add(1) % uint32(len(cp.managers)).NewConnectionPoolaccepts size 0 without complaint — a pool size read from configuration is how thathappens — and the first call died with an integer divide by zero, in the package whose entire subject is
surviving failures. gosec reported the
int→uint32in that expression, which needs more than fourbillion managers; it said nothing about the
% 0beside it, which needs none.The error names the actual condition. Falling through to the retry loop would produce
no healthy connections available— it has nothing to iterate — which sends an operator to look atconnection health for a pool that was never built. The test asserts which error comes back.
4. A multipart upload past ~1.25 TiB asked S3 for part numbers it rejects
CalculateOptimalChunkSize's ladder tops out at 8× the configured base, so the 10,000-part ceilingbinds at 1.25 TiB with the 16 MiB default — and at 400 GiB with a 5 MiB base, which is
configuration. Past it every part above 10,000 is rejected individually, after the first 10,000 have
been transferred, billed, and left behind for an abort.
The chunk size now grows to keep the count inside the ceiling — which is what S3 wants for an object
that size anyway — capped at the 5 GiB maximum part. Fixed in the pure function, not at its one call
site: today's path holds the whole object in memory and cannot reach 1.25 TiB, but the invariant then
holds for a streaming path, for which the ceiling is immediately live. It is also what makes the
#nosecon the two part-number conversions honest rather than a promise.Verification
the feature-name fix, five on the empty pool, six on the part ceiling. Two of the part-ceiling
mutations survived the first run — the min and max part-size clamps were asserted but never
exercised, because the sweep's sizes did not reach where they bind. The sweep was widened rather than
the assertions dropped.
go test -racegreen oninternal/storage/s3,internal/health,internal/difftest,pkg/recovery,internal/archive,internal/cache.CGO_ENABLED=0 go build ./...,go vet,gofmtclean.golangci-lintclean apart from 16modernize/errorsastypefindings that only the newer local v2.13.2 has; CI pins v2.12.2.