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

feat: test for reentrancy in invariant tests #1578

Open
mds1 opened this issue May 11, 2022 · 5 comments
Open

feat: test for reentrancy in invariant tests #1578

mds1 opened this issue May 11, 2022 · 5 comments
Labels
A-testing Area: testing C-forge Command: forge Cmd-forge-test Command: forge test T-feature Type: feature

Comments

@mds1
Copy link
Collaborator

mds1 commented May 11, 2022

Component

Forge

Describe the feature you would like

In #1572 invariant testing will be added to forge. This basically executes a sequence of random calls in attempt to break some invariant.

This is really valuable but it can't test reentrancy, which is common class of vulnerabilities. @brockelmore had an idea for testing this, which is:

  • For each state mutating function, generate a contract that has a fallback method which calls that method.
  • Add each of these contracts to the list of potential callers.
  • If one of your contracts ever transfers control back to the caller, this contract will reenter when it's the caller and hopefully break your invariant.

One downside is that each of these contracts would have no state in the system until its first call, which isn't always ideal. One way to resolve this is to generate a single contract that can call all methods with any calldata, and randomly choose which it will reenter. That contract will more frequently be the caller and likely to have state in the system than if we generated many contracts.

In general, there's many variants of reentrancy vulns and the above won't cover all of them. This is very much a work-in-progress idea that we'd need to spec out more before implementing, but wanted to open this issue so we don't lose track of it. Ideas welcome 🙂

Additional context

No response

@mds1 mds1 added the T-feature Type: feature label May 11, 2022
@onbjerg onbjerg moved this to Todo in Foundry May 11, 2022
@onbjerg onbjerg added this to Foundry May 11, 2022
@onbjerg onbjerg added A-testing Area: testing Cmd-forge-test Command: forge test C-forge Command: forge labels May 12, 2022
@onbjerg
Copy link
Member

onbjerg commented Aug 11, 2022

Was this solved?

@onbjerg onbjerg moved this from Todo to May be solved in Foundry Aug 11, 2022
@mds1
Copy link
Collaborator Author

mds1 commented Aug 11, 2022

There's a simple version of reentrancy invariant testing that @joshieDo added and is currently off by default, but the approach isn't perfect (not sure any approach will be though). IMO worth keeping this open just to track the idea / as a place to drop any ideas on the best way(s) to do this

@grandizzy
Copy link
Collaborator

Here's a sample on how reentrancy can be tested in invariants (adapted from https://github.com/rappie/echidna-rari-hack): https://github.com/grandizzy/foundry-rari-hack

@mds1 do we need more of these or OK to close ticket for now and improve as we go? (would probably make sense to follow up with more testing with call_override = true setting)

@zerosnacks zerosnacks added this to the v1.0.0 milestone Jul 26, 2024
@mds1
Copy link
Collaborator Author

mds1 commented Jul 28, 2024

From a quick read of that repo it seems it manually sets up a handler for reentrancy testing. If that's the approach we want to recommend (since I don't think call_override was very useful) then we can close this and just update docs accordingly (if not already done). But if we want to improve the built-in call_override way of reentrancy testing then we should either keep this open or make sure that's tracked in another issue

@grandizzy
Copy link
Collaborator

Yeah, had some testing for same scenario with call_override without any good results, there's more effort to put in to make it work.

Agree, let's keep this one to make call_override robust

@zerosnacks zerosnacks removed this from the v1.0.0 milestone Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testing Area: testing C-forge Command: forge Cmd-forge-test Command: forge test T-feature Type: feature
Projects
Status: Blocked
Development

No branches or pull requests

4 participants