Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fakeclient: Fix TOCTOU races #2980

Merged
merged 1 commit into from
Oct 19, 2024

Conversation

alvaroaleman
Copy link
Member

The fake client currently has a number of time of check time of use races, where it fetches an object to determine what to do in a mutating operation. The problem is that the object might change in between fetching it and doing the mutating operation. Most notably, this happens when:

  • Patching is done in parallel. Only one of the patches will succeed, the other ones will fail with a conflict
  • Updates of objects that allow unconditional updates: All updates will succeed, but not all of them will increment the resource version (i.E dirty writes for the RV)
  • An update for an object that allows createOnUpdate races with a create or delete
  • A DeleteAllOf call races with Delete calls
  • A scale update races with a normal update

This change:

  • Adds tests for all of these cases
  • Fixes them by adding a lock around the write operations, including their read part, if any

Note: This patch is a more complete alternate to #2979. Initially I thought that this change might be too breaking to backport, because tests tend to converge extremely closely to any behavior of the fakeclient regardless of it being correct or not, but I think it should be fine. The fact that it took years for someone to notice a very basic issue where concurrent patching doesn't work correctly is also indicative of the fact that the fake client seems to be rarely used concurrently and only then will this change result in any change of behavior.

Fixes #2977
Fixes #2960

/cc @akutz

The fake client currently has a number of time of check time of use
races, where it fetches an object to determine what to do in a mutating
operation. The problem is that the object might change in between
fetching it and doing the mutating operation. Most notably, this
happens when:
* Patching is done in parallel. Only one of the patches will succeed,
  the other ones will fail with a conflict
* Updates of objects that allow unconditional updates: All updates will
  succeed, but not all of them will increment the resource version (i.E
  dirty writes for the RV)
* An update for an object that allows createOnUpdate races with a create
  or delete
* A DeleteAllOf call races with Delete calls
* A scale update races with a normal update

This change:
* Adds tests for all of these cases
* Fixes them by adding a lock around the write operations, including
  their read part, if any
@k8s-ci-robot k8s-ci-robot requested a review from akutz October 12, 2024 22:22
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 12, 2024
@alvaroaleman
Copy link
Member Author

/cc @vincepri @sbueringer

@sbueringer
Copy link
Member

Thank you very much!!

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 19, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 5ff547602e5e17cac4e046740f836e186406b5c2

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, sbueringer

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

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [alvaroaleman,sbueringer]

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

@k8s-ci-robot k8s-ci-robot merged commit 0074265 into kubernetes-sigs:main Oct 19, 2024
11 checks passed
@alvaroaleman
Copy link
Member Author

/cherrypick release-0.19

@alvaroaleman alvaroaleman deleted the fictoctou branch October 19, 2024 15:15
@k8s-infra-cherrypick-robot

@alvaroaleman: new pull request created: #2992

In response to this:

/cherrypick release-0.19

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-sigs/prow repository.

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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
4 participants