gosec: G115 is reporting false positives (a summary)
Summary
G115 is still reporting false positives even after #1189 and #1194.
Known false positives
I’m writing these down without much investigation whether it is possible to implement fixes for them.
- any kind of arithmetic done on the checked variable after the check:
func foo(a int) uint {
if a != 3 || a != 4 {
panic("not supported")
}
// the value being passed here is different than the one checked
// so it is tricky to determine if the check is valid
//
// this will be difficult to implement without introducing false negatives
return uint(a-1) // false positive
}
- explicit value checks are lacking:
func foo(a int) uint {
if a == 3 || a != 4 {
// see that sneaky NEQ there?
return uint(a) // false negative
}
panic("not supported")
}
- binary truncation (reported by @stephenc):
func foo(a int) uint16 {
return uint16(a &0xffff) // false positive
}
- builtin min and max functions (reported by @ben-krieger):
func foo() uint16 {
a, b := 1234, 2345
result := min(a,b)
return uint(result) // false positive
}
- loop indices (reported by @PlasmaPower):
func foo(myArr []string) {
// these seem to have a similar implementation difficulty to the arithmetic example
for i, _ := range myArr {
_ = uint64(i) // false positive
}
for i := 0; i < 10; i++ {
_ = uint64(i) // false positive
}
for i := randIntMightBeNegativeEven(); i >= 0; i-- {
_ = uint64(i) // false positive
}
}
Notes:
I recommend opening multiple small PRs that fix one issue at a time.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 19
- Comments: 26 (13 by maintainers)
Commits related to this issue
- chore: Exclude G115 from gosec linter Exclude G115 from the gosec linter to address the issue with https://github.com/securego/gosec/issues/1212. — committed to NethermindEth/juno by AnkushinDaniil 2 years ago
- chore: Update golangci-lint to version 1.61.0 (#2154) * chore: Update golangci-lint to version 1.61.0 * chore: Exclude G115 from gosec linter Exclude G115 from the gosec linter to address the i... — committed to NethermindEth/juno by AnkushinDaniil 2 years ago
- Disable G115 check. https://github.com/securego/gosec/issues/1212 — committed to yakuninmax/imgpreviewer by deleted user 2 years ago
- Squashed commit of the following: commit 8ca930797e6d76acb92c7ca99e8283733c1cff67 Author: wojciechos <wojciech.zieba@nethermind.io> Date: Thu Oct 24 14:19:52 2024 +0200 Fix spec version for 0_... — committed to NethermindEth/juno by weiihann 2 years ago
- Squashed commit of the following: commit 8ca930797e6d76acb92c7ca99e8283733c1cff67 Author: wojciechos <wojciech.zieba@nethermind.io> Date: Thu Oct 24 14:19:52 2024 +0200 Fix spec version for 0_... — committed to NethermindEth/juno by weiihann 2 years ago
- Squashed commit of the following: commit 8ca930797e6d76acb92c7ca99e8283733c1cff67 Author: wojciechos <wojciech.zieba@nethermind.io> Date: Thu Oct 24 14:19:52 2024 +0200 Fix spec version for 0_... — committed to NethermindEth/juno by weiihann 2 years ago
- golangci-lint: enable `gosec` Also exclude G115 until the following issue is resolved. https://github.com/securego/gosec/issues/1212 — committed to dnaeon/logging by dnaeon 2 years ago
- golangci-lint: enable `gosec` Also exclude G115 until the following issue is resolved. https://github.com/securego/gosec/issues/1212 — committed to dnaeon/logging by dnaeon 2 years ago
- golangci-lint: enable `gosec` Also exclude G115 until the following issue is resolved. https://github.com/securego/gosec/issues/1212 — committed to gardener/logging by dnaeon 2 years ago
- Golang 1.24.1 / Golangci-lint 1.64.7 This patch updates VM Operator to use Golang 1.24.1 and Golangci-lint 1.64.7 (as it supports Golang 1.24.1). This patch also addresses several new linter errors:... — committed to akutz/vm-operator by akutz 2 years ago
- Golang 1.24.1 / Golangci-lint 1.64.7 This patch updates VM Operator to use Golang 1.24.1 and Golangci-lint 1.64.7 (as it supports Golang 1.24.1). This patch also addresses several new linter errors:... — committed to akutz/vm-operator by akutz 2 years ago
- Golang 1.24.1 / Golangci-lint 1.64.7 This patch updates VM Operator to use Golang 1.24.1 and Golangci-lint 1.64.7 (as it supports Golang 1.24.1). This patch also addresses several new linter errors:... — committed to akutz/vm-operator by akutz 2 years ago
- Golang 1.24.1 / Golangci-lint 1.64.7 This patch updates VM Operator to use Golang 1.24.1 and Golangci-lint 1.64.7 (as it supports Golang 1.24.1). This patch also addresses several new linter errors:... — committed to akutz/vm-operator by akutz 2 years ago
- Golang 1.24.1 / Golangci-lint 1.64.7 This patch updates VM Operator to use Golang 1.24.1 and Golangci-lint 1.64.7 (as it supports Golang 1.24.1). This patch also addresses several new linter errors:... — committed to akutz/vm-operator by akutz 2 years ago
- Golang 1.24.1 / Golangci-lint 1.64.7 This patch updates VM Operator to use Golang 1.24.1 and Golangci-lint 1.64.7 (as it supports Golang 1.24.1). This patch also addresses several new linter errors:... — committed to akutz/vm-operator by akutz 2 years ago
On this, I think we need to start the Obfuscated Go Integer Overflow Contest. 😄
I have no precise release date for now but soon, mainly because of G115 false positives.
In order to keep this thread a useful source of truth, let me fill in some details.
@Victoremepunto’s issue was that accessing a struct field is considered a different value every time in SSA. This is because without deeper analysis (than SSA), you can’t rule out a concurrent routine modifying that value between accesses.
Therefore, the issue could have been resolved without any changes to
gosecby simply changingto
However, since struct field access is a common use case and modifying the value at the same time as reading it is a race condition (which is usually considered a bug), #1221 treats field accesses as not being modified between time-of-check and time-of-use (TOCTOU).
And yes, TOCTOU is its own type of potential security vulnerability. Maybe a future linter can better look for this.
Maybe it’s because the fix is inside golangci-lint v1.61.0 and not v1.60.1 😉
Quick follow up, golangci-lint 1.61.0 was shipped with gosec 2.21.2 💪
So everything that was made in gosec to improve G115 detection are now released 🎉
@Victoremepunto you’re getting that warning because golangci-lint has not released a new version with the changes from #1194 yet. The code you have there looks sound so once we have a new golangci-lint release, it should work.
@ccoVeille do you know and can share at least approximately when we can expect it?
I seem to be getting problems with nested structs, here is a contrived example:
This gives me:
So it seems happy with the the bounds check on
*i.u32, but not with*n.i.u32Here’s another one:
another false postive.
I’ve issued https://github.com/securego/gosec/pull/1254 to fix the
ParseUintissue I described above.I ran into a false G115 positive just now, using
strconv.ParseUint:This fails with
G115: integer overflow conversion uint64 -> intThere’s actually a unit test similar to this issue, except it casts to a
uint8rather than anint.Thanks @ben-krieger for the explanation. I purposefully decided to ignore TOCTOU in that PR.
The proposal of a separate lint rule for it is a very good idea since a different lint message would explain the issue better.
Hi! unfortunately, using golangci-lint 1.60.1 the problem is still there - might worth trying to get a simpler reproducer and add it to this issue?
check this: https://github.com/RedHatInsights/frontend-operator/actions/runs/10835444044/job/30066989278?pr=188#step:5:26
I’m not sure if I should open a separate issue for this, but another false positive is casting the index produced by iterating over a range to an unsigned int. We do this a lot in our codebase. E.g.:
Ideally it’d be able to look at bounds in for loops as well, but I get that’s more complicated:
I’m just a random gopher. I’m pretty active, but I’m not part of golangci-lint release.
@ldez might be a better candidate to reply that question