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

validateVariablePoolHasEnoughLiquidity() misuse liquidity #80

Closed
c4-bot-5 opened this issue Jul 1, 2024 · 3 comments
Closed

validateVariablePoolHasEnoughLiquidity() misuse liquidity #80

c4-bot-5 opened this issue Jul 1, 2024 · 3 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 downgraded by judge Judge downgraded the risk level of this issue duplicate-218 🤖_primary AI based primary recommendation 🤖_10_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality

Comments

@c4-bot-5
Copy link
Contributor

c4-bot-5 commented Jul 1, 2024

Lines of code

https://github.com/code-423n4/2024-06-size/blob/8850e25fb088898e9cf86f9be1c401ad155bea86/src/libraries/CapsLibrary.sol#L68

Vulnerability details

Vulnerability details

validateVariablePoolHasEnoughLiquidity()
Called after buyCreditMarket()/sellCreditMarket()/liquidateWithReplacement() method execution
The main purpose is a safety mechanism.

   /// @notice Validate that the Variable Pool has enough liquidity to withdraw the amount of cash
   /// @dev Reverts if the Variable Pool does not have enough liquidity
   ///      This safety mechanism prevents takers from matching orders that could not be withdrawn from the Variable Pool.
   ///        Nevertheless, the Variable Pool may still fail to withdraw the cash due to other factors (such as a pause, etc),
   ///        which is understood as an acceptable risk.
   /// @param state The state struct
   /// @param amount The amount of cash to withdraw
   function validateVariablePoolHasEnoughLiquidity(State storage state, uint256 amount) public view {
@>     uint256 liquidity = state.data.underlyingBorrowToken.balanceOf(address(state.data.variablePool));
       if (liquidity < amount) {
           revert Errors.NOT_ENOUGH_BORROW_ATOKEN_LIQUIDITY(liquidity, amount);
       }
   }

But currently using liquidity = usdc.balanceOf(aave3 pool), it's always 0
Pool doesn't have a balance, token is deposited into aToken and not in pool.
https://github.com/aave/aave-v3-core/blob/master/contracts/protocol/libraries/logic/SupplyLogic.sol
Pool.sol - > SupplyLogic.sol

library SupplyLogic {

  function executeSupply(
...

@>  IERC20(params.asset).safeTransferFrom(msg.sender, reserveCache.aTokenAddress, params.amount);

    bool isFirstSupply = IAToken(reserveCache.aTokenAddress).mint(
      msg.sender,
      params.onBehalfOf,
      params.amount,
      reserveCache.nextLiquidityIndex
    );

https://github.com/aave/aave-v3-core/blob/master/contracts/protocol/tokenization/AToken.sol#L104

contract AToken is VersionedInitializable, ScaledBalanceTokenBase, EIP712Base, IAToken {
...
  function burn(
    address from,
    address receiverOfUnderlying,
    uint256 amount,
    uint256 index
  ) external virtual override onlyPool {
    _burnScaled(from, receiverOfUnderlying, amount, index);
    if (receiverOfUnderlying != address(this)) {
@>    IERC20(_underlyingAsset).safeTransfer(receiverOfUnderlying, amount);
    }
  }

Impact

Methods at the core of the protocol don't work, e.g.:buyCreditMarket()/sellCreditMarket()/liquidateWithReplacement() always revert NOT_ENOUGH_BORROW_ATOKEN_ LIQUIDITY

Recommended Mitigation

    function validateVariablePoolHasEnoughLiquidity(State storage state, uint256 amount) public view {
-       uint256 liquidity = state.data.underlyingBorrowToken.balanceOf(address(state.data.variablePool));
+       address aTokenAddress = state.data.variablePool.getReserveData(address(state.data.underlyingBorrowToken)).aTokenAddress
+       uint256 liquidity = state.data.underlyingBorrowToken.balanceOf(aTokenAddress); 
        if (liquidity < amount) {
            revert Errors.NOT_ENOUGH_BORROW_ATOKEN_LIQUIDITY(liquidity, amount);
        }
    }

Assessed type

Context

@c4-bot-5 c4-bot-5 added 3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working labels Jul 1, 2024
c4-bot-5 added a commit that referenced this issue Jul 1, 2024
@c4-bot-12 c4-bot-12 added the 🤖_10_group AI based duplicate group recommendation label Jul 2, 2024
@c4-bot-13 c4-bot-13 added the 🤖_primary AI based primary recommendation label Jul 2, 2024
@howlbot-integration howlbot-integration bot added sufficient quality report This report is of sufficient quality duplicate-114 labels Jul 4, 2024
@C4-Staff
Copy link

C4-Staff commented Jul 8, 2024

CloudEllie marked the issue as duplicate of #218

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Jul 11, 2024
@c4-judge
Copy link
Contributor

hansfriese marked the issue as satisfactory

@c4-judge
Copy link
Contributor

hansfriese changed the severity to 2 (Med Risk)

@c4-judge c4-judge added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value downgraded by judge Judge downgraded the risk level of this issue and removed 3 (High Risk) Assets can be stolen/lost/compromised directly labels Jul 21, 2024
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 downgraded by judge Judge downgraded the risk level of this issue duplicate-218 🤖_primary AI based primary recommendation 🤖_10_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality
Projects
None yet
Development

No branches or pull requests

5 participants