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

MToken May be Inflation Attack #92

Closed
code423n4 opened this issue Jul 29, 2023 · 9 comments
Closed

MToken May be Inflation Attack #92

code423n4 opened this issue Jul 29, 2023 · 9 comments
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working primary issue Highest quality submission among a set of duplicates sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue unsatisfactory does not satisfy C4 submission criteria; not eligible for awards

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2023-07-moonwell/blob/fced18035107a345c31c9a9497d0da09105df4df/src/core/MToken.sol#L340-L370
https://github.com/code-423n4/2023-07-moonwell/blob/fced18035107a345c31c9a9497d0da09105df4df/src/core/MErc20.sol#L171-L174

Vulnerability details

Impact

MToken May be Inflation Attack like ERC4626 Inflation Attack

Proof of Concept

Currently mToken still has the common ERC4626 Inflation Attack
The number of assets can be increased by donating
MToken.sol#exchangeRateStoredInternal()

    function exchangeRateStoredInternal() virtual internal view returns (MathError, uint) {
        uint _totalSupply = totalSupply;
        if (_totalSupply == 0) {
---SKIP---
            uint totalCash = getCashPrior(); // @audit <---------------asset from getCashPrior()
            uint cashPlusBorrowsMinusReserves;
            Exp memory exchangeRate;
            MathError mathErr;
---SKIP---
            return (MathError.NO_ERROR, exchangeRate.mantissa);
        }
    }

MErc20.sol#getCashPrior()

    function getCashPrior() virtual override internal view returns (uint) {
        EIP20Interface token = EIP20Interface(underlying);
        return token.balanceOf(address(this)); // @audit //<---------------from balaceOf()
    }

So it is still possible to mint first and then redeem, and finally 1 shares left, and then donate by direct transfer assert,Amplify exchangeRate, thus carrying out Inflation Attack
Note: Although the initial mint initialSupply can be executed when creating a MToken, there is no restriction on initialSupply==0
It is recommended to refer to the new 4.9.0 release of OpenZeppelin, which has a special version of ERC4626 Inflation Attack for this
https://twitter.com/OpenZeppelin/status/1656066698410328064?s=20

Tools Used

Manual Review

Recommended Mitigation Steps

  1. Keeping track of the total assets internally
  2. Creating "dead shares"
    Reference: Implement or recommend mitigations for ERC4626 inflation attacks OpenZeppelin/openzeppelin-contracts#3706 (comment)

Assessed type

ERC4626

@code423n4 code423n4 added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working labels Jul 29, 2023
code423n4 added a commit that referenced this issue Jul 29, 2023
@c4-pre-sort
Copy link

0xSorryNotSorry marked the issue as primary issue

@ElliotFriedman
Copy link

without a PoC, this is an invalid finding because yes, you could donate to the protocol and increase the share price, but you have to show a way to do this in a way that's profitable to an attacker.

@c4-sponsor
Copy link

ElliotFriedman marked the issue as sponsor disputed

@c4-sponsor c4-sponsor added the sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue label Aug 3, 2023
@alcueca
Copy link

alcueca commented Aug 13, 2023

@ElliotFriedman, I think that #264 does a good job in explaining how this would be profitable for an attacker:

The attacker can then redeem their 1 wei cToken for all the underlying assets in that cToken contract.

I also like that #126 links to existing literature on this specific bug.

#264 also goes to explain how you are protecting yourself against this vulnerability in mip00, but how the protection seems to be not enough given the cross-chain context.

To mitigate the potential for front-running attacks, it is advised that the initial mToken minting process is finalized prior to transferring administrative rights to the governance or bundle unpausing and mToken minting into the same VAA to be executed atomically.

@c4-judge
Copy link
Contributor

alcueca marked the issue as selected for report

@c4-judge c4-judge added the selected for report This submission will be included/highlighted in the audit report label Aug 13, 2023
@ElliotFriedman
Copy link

This isn't a valid finding because known issues here explained in the readme: https://github.com/code-423n4/2023-07-moonwell/blob/fced18035107a345c31c9a9497d0da09105df4df/README.md#known-compound-v2-issues

"New markets must be added with no collateral factor, and some small amount of the collateral token supply must be burned in order to avoid market manipulation. This is a known issue."

@ElliotFriedman
Copy link

So yes, it's a valid issue, but it is out of scope as we recognize this is an issue and don't want to award a payout for something that is clearly stated to be out of scope.

@alcueca
Copy link

alcueca commented Aug 15, 2023

Agree with the sponsor, except for #264. I'll continue the conversation there.

@c4-judge
Copy link
Contributor

alcueca marked the issue as unsatisfactory:
Out of scope

@c4-judge c4-judge added unsatisfactory does not satisfy C4 submission criteria; not eligible for awards and removed selected for report This submission will be included/highlighted in the audit report labels Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working primary issue Highest quality submission among a set of duplicates sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue unsatisfactory does not satisfy C4 submission criteria; not eligible for awards
Projects
None yet
Development

No branches or pull requests

6 participants