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

TokenController interactions after balance updates #24

Merged
merged 1 commit into from
Sep 21, 2023

Conversation

3esmit
Copy link

@3esmit 3esmit commented Sep 14, 2023

WIP for #17 in top of #29

Description

moved the interaction !TokenController(controller).onTransfer(_from, _to, _amount) to be called after the update balances happened, so in case of it having a reentrancy, the checks will fail because the balance already changed.

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?

@0x-r4bbit
Copy link
Collaborator

Glad to see we're eliminating this one.
This is supposed to fix a security vulnerability, so we should add a test for this as well.

If you need help with writing the test let me know.

@3esmit
Copy link
Author

3esmit commented Sep 15, 2023

If we need to test it, we need to do a reentrancy call using a crafted TokenController for that purpose.

@0x-r4bbit
Copy link
Collaborator

0x-r4bbit commented Sep 15, 2023

Yes exactly. That's what I'm looking for. I've done this a couple of times, so let me know if you need help!

Here's a concrete example of it: https://github.com/0x-r4bbit/simple-reentrancy-attack

@0x-r4bbit
Copy link
Collaborator

Added a test case in #26
If this commit is rebased on top of it, the test will fail.

0x-r4bbit added a commit that referenced this pull request Sep 20, 2023
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 added a commit that referenced this pull request Sep 20, 2023
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
@3esmit 3esmit changed the base branch from master to test/transfer-reentrancy-2 September 21, 2023 23:34
@3esmit 3esmit merged commit e3289ce into test/transfer-reentrancy-2 Sep 21, 2023
6 checks passed
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.

Reentrancy danger on controller’s onTransfer() callback
2 participants