Skip to content

Fix gosec G601: Implicit memory aliasing of items from a range statement - #6551

Merged
jetstack-bot merged 4 commits into
cert-manager:masterfrom
wallrj:gosec-601
Dec 20, 2023
Merged

jetstack-bot merged 4 commits into
cert-manager:masterfrom
wallrj:gosec-601

Conversation

@wallrj

@wallrj wallrj commented Dec 12, 2023 •

Copy link
Copy Markdown
Member
  • Enables the Gosec G601 check in golangci-lint
  • Reconfigures golangci-lint to show all issues in all files (rather than limiting the results)
  • Reconfigures golangci-lint to also check _test.go files
  • Makes a local copy of the loop var where necessary.
  • Annotates the failed code where the failure is a false positive.

G601 is described in https://github.com/securego/gosec#available-rules as follows:

G601: Implicit memory aliasing of items from a range statement

This gosec check was proposed in securego/gosec#438 and implemented in securego/gosec#443.

It is intended to identify instances of the problem described in The Go Common Mistakes section: Using reference to loop iterator variable, but the check suffers from the problems described by RSC in golang/go#56010:

Static analysis is not a viable alternative
Whether a particular loop is “buggy” due to the current behavior depends on whether the address of an iteration value is taken and then that pointer is used after the next iteration begins. It is impossible in general for analyzers to see where the pointer lands and what will happen to it.

Most of these are false positives and I've annotated these using the gosec # nosec annotation,
because it allows me to specify exactly which issue I want to ignore.

The alternative is // nolint:gosec which is less specific:

I wasn't aware when I started this PR, that the check (G601), these changes and annotations will become obsolete upon the release of Go 1.22 which changes the scope of the loop var. Once cert-manager upgrades to Go 1.22 we can probably revert all these changes:

Testing

Example failures:

You can also run the checks locally as follows:

make verify-golangci-lint
Fix gosec G601: Implicit memory aliasing of items from a range statement

/kind cleanup

@jetstack-bot jetstack-bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. release-note Denotes a PR that will be considered when it comes time to generate release notes. area/acme Indicates a PR directly modifies the ACME Issuer code area/testing Issues relating to testing size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed release-note-none Denotes a PR that doesn't merit a release note. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Dec 12, 2023
@wallrj wallrj changed the title WIP: Enable gosec G601 and fix the warnings Fix gosec G601: Implicit memory aliasing of items from a range statement Dec 13, 2023
@jetstack-bot jetstack-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 13, 2023
Comment thread cmd/ctl/pkg/renew/renew.go Resolved
Comment thread pkg/controller/acmeorders/util.go Resolved
@wallrj
wallrj requested a review from inteon December 13, 2023 09:51
Comment thread .golangci.ci.yaml Resolved
Comment thread cmd/ctl/pkg/renew/renew.go Resolved
@inteon

ghost commented Dec 13, 2023 •

Copy link
Copy Markdown
Member

@wallrj general remark:
I think there are 2 ways to fix these errors:

  1. copy the value
for _, crt := range crts {
    crt := crt

or equivalent (but less clear)

for i := range crts {
    crt := crts[i]
  1. use a reference to the value in the array
for i := range crts {
    crt := &crts[i]

I think we should use 1 whenever possible and use 2 in case we are working with large objects and want to avoid a lot of memory being allocated (eg. in case of the list.Items loops).
However, if we use 1, the function that receives the value as an argument should probably also just accept a copy of the value instead of a pointer.

@jetstack-bot jetstack-bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Dec 13, 2023
var owned []*cmacme.Order
for _, o := range l.Items {
for i := range l.Items {
o := &l.Items[i]

ghost Dec 13, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We might want to copy the values, so the underlying array can be gc'ed.
This is ok since it's just test code.

ghost Dec 13, 2023

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't even considered it.
I don't know how Go garbage collection works.

If there's a block of memory somewhere containing this Order{} object and o is a pointer to it which lives beyond the scope of this loop, why should that prevent the garbage collector freeing up the memory used by the other Orders in that list?

ghost Dec 13, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ghost Dec 14, 2023

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very interesting, thanks!
In that case I'll alter the commit and ping you when done.

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve
/lgtm
/hold I left a comment, please unhold after reading.

@jetstack-bot jetstack-bot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm Indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Dec 13, 2023
@wallrj

ghost commented Dec 13, 2023

Copy link
Copy Markdown
Member Author

The golangci-lint config file currently ignores these issues in _test.go files, but I'm going to change that and see how many issues there are.

@jetstack-bot

ghost commented Dec 13, 2023

Copy link
Copy Markdown
Contributor

@SgtCoDFish: Overrode contexts on behalf of SgtCoDFish: pull-cert-manager-master-license

Details

In response to this:

/override pull-cert-manager-master-license

Yeah it's an upstream change - I'll raise a PR for it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@wallrj

ghost commented Dec 13, 2023

Copy link
Copy Markdown
Member Author

/retest

1 similar comment
@wallrj

ghost commented Dec 13, 2023

Copy link
Copy Markdown
Member Author

/retest

@jetstack-bot jetstack-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Dec 13, 2023
@wallrj
wallrj requested a review from inteon December 13, 2023 17:35
@inteon

ghost commented Dec 13, 2023

Copy link
Copy Markdown
Member

/lgtm

@jetstack-bot jetstack-bot added the lgtm Indicates that a PR is ready to be merged. label Dec 13, 2023
@jetstack-bot jetstack-bot removed the lgtm Indicates that a PR is ready to be merged. label Dec 14, 2023
@wallrj

ghost commented Dec 20, 2023

Copy link
Copy Markdown
Member Author

@inteon I've revisited each of the warnings and decided that most of them are false positives and annotated them as such.
Where a pointer to the loop var was living beyond the end of the loop, I have used the simple local copy method (1 in your earlier comment #6551 (comment)) for readability.

I've also updated the PR description with my latest understanding of all this.

PTAL

@wallrj
wallrj requested a review from inteon December 20, 2023 17:52

ghost left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for making this change.
I agree that this is a false-positive in case the lifetime of the variable does not exceed the for loop body scope.
I think golangci-lint needs a smarter check, using some kind of borrow-checker (it should be more like rust 😄 ).
/approve
/lgtm

@jetstack-bot jetstack-bot added the lgtm Indicates that a PR is ready to be merged. label Dec 20, 2023
@jetstack-bot

ghost commented Dec 20, 2023

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: inteon

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@inteon

ghost commented Dec 20, 2023

Copy link
Copy Markdown
Member

/unhold

@jetstack-bot jetstack-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 20, 2023
@jetstack-bot
jetstack-bot merged commit c7714e6 into cert-manager:master Dec 20, 2023
@wallrj
wallrj deleted the gosec-601 branch December 20, 2023 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. area/acme Indicates a PR directly modifies the ACME Issuer code area/testing Issues relating to testing area/vault Indicates a PR directly modifies the Vault Issuer code dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants