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

make MiniMeToken inherit ReentrancyGuard #26

Closed
wants to merge 2 commits into from

Conversation

0x-r4bbit
Copy link
Collaborator

@0x-r4bbit 0x-r4bbit commented Sep 19, 2023

Description

If a MiniMeToken has a TokenController configured, it can intercept
every transfer using the onTransfer callback and reenter the
MiniMeToken contract.

This is a reentrancy vulnerablity as a malicious TokenController has
access to the MiniMeToken balances and it privileged to perform
transfers.

Unfortunately, simply using the CEI-pattern as done in
#24 isn't sufficient, because the
reentrancy can be done non-recursively, resulting in no error and a
possible double spent issue.

To prevent this vulnerablity, this commit introduces OZ's
ReentrancyGuard and makes MiniMeToken inherit it. This gives us
access to the nonReentrant modifer that is attached to every transfer
function.

The commit also introduces a test that proves that the contract reverts
in case of a reentrancy attempt.

Closes: #17

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • Added natspec comments?
  • Ran forge snapshot?
  • Ran pnpm lint?
  • Ran forge test?

assertEq(minimeToken.balanceOf(attackerController.attackerEOA()), fundsAmount);
assertEq(minimeToken.balanceOf(sender), fundsAmount - sendAmount);
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, there's not particular reason to have this test live in this file.
I just wanted an environment without noise.

But I can move this test to MiniMeToken.t.sol.

Lemme know what you think @3esmit

Copy link

Choose a reason for hiding this comment

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

It should error.

Copy link

@3esmit 3esmit left a comment

Choose a reason for hiding this comment

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

Make it error, and merge it into branch issue17

This was not causing any compilation issues, but the solidity language
server gets confused by this and complains about incorrect import
statements otherwise.
@0x-r4bbit 0x-r4bbit changed the base branch from master to 3esmit/issue17 September 19, 2023 15:02
@0x-r4bbit
Copy link
Collaborator Author

This is now rebased on top of #24. The test fails but not as expected. There seems to still be a double spend happening.
Need to investigate why that is.

I think we need to be more careful with the history updates on balances.

@0x-r4bbit
Copy link
Collaborator Author

0x-r4bbit commented Sep 20, 2023

@3esmit As discussed offline, unfortunately simply applying CEI-Pattern is not going to solve the issue because the reentrancy can be done non-recursively in which case a single reentrancy with the exact balance of the victim can still be performed and wouldn't cause an error.

To account for this, I've now introduced the ReentrancyGuard and its nonReentrant modifier, which will revert in any attempt of reentrancy.

We can now reconsider if #24 is still needed

If a `MiniMeToken` has a `TokenController` configured, it can intercept
every transfer using the `onTransfer` callback and reenter the
`MiniMeToken` contract.

This is a reentrancy vulnerablity as a malicious `TokenController` has
access to the `MiniMeToken` `balances` and it privileged to perform
transfers.

Unfortunately, simply using the CEI-pattern as done in
#24 isn't sufficient, because the
reentrancy can be done non-recursively, resulting in no error and a
possible double spent issue.

To prevent this vulnerablity, this commit introduces OZ's
`ReentrancyGuard` and makes `MiniMeToken` inherit it. This gives us
access to the `nonReentrant` modifer that is attached to every transfer
function.

The commit also introduces a test that proves that the contract reverts
in case of a reentrancy attempt.

Closes: #17
@0x-r4bbit 0x-r4bbit changed the title Add reentrancy attack test make MiniMeToken inherit ReentrancyGuard Sep 20, 2023
@0x-r4bbit
Copy link
Collaborator Author

Closing this in favour of e715502
Which landed via (#30 )

@0x-r4bbit 0x-r4bbit closed this Sep 22, 2023
@3esmit 3esmit deleted the test/transfer-reentrancy branch September 26, 2023 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants