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

DepositTradeHelper.depositAndTrade will not work normally with ERC20 that need to clear allowance #198

Open
code423n4 opened this issue Dec 12, 2022 · 4 comments
Labels
bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue grade-a Q-20 QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/prepo-io/prepo-monorepo/blob/feat/2022-12-prepo/apps/smart-contracts/core/contracts/DepositTradeHelper.sol#L24

Vulnerability details

Impact

DepositTradeHelper.depositAndTrade will not work normally with ERC20 that need to clear allowance as it sets allowance to uint256.max and do not set it to 0 after. As result next time when user calls depositAndTrade function it will fail as allowance should be set to 0 before.

Proof of Concept

DepositTradeHelper.depositAndTrade function allows user to deposit base tokens into Collateral token and then swap it using Uniswap.
https://github.com/prepo-io/prepo-monorepo/blob/feat/2022-12-prepo/apps/smart-contracts/core/contracts/DepositTradeHelper.sol#L22-L34

  function depositAndTrade(uint256 baseTokenAmount, Permit calldata baseTokenPermit, Permit calldata collateralPermit, OffChainTradeParams calldata tradeParams) external override {
    if (baseTokenPermit.deadline != 0) {
      IERC20Permit(address(_baseToken)).permit(msg.sender, address(this), type(uint256).max, baseTokenPermit.deadline, baseTokenPermit.v, baseTokenPermit.r, baseTokenPermit.s);
    }
    _baseToken.transferFrom(msg.sender, address(this), baseTokenAmount);
    if (collateralPermit.deadline != 0) {
      _collateral.permit(msg.sender, address(this), type(uint256).max, collateralPermit.deadline, collateralPermit.v, collateralPermit.r, collateralPermit.s);
    }
    uint256 _collateralAmountMinted = _collateral.deposit(msg.sender, baseTokenAmount);
    _collateral.transferFrom(msg.sender, address(this), _collateralAmountMinted);
    ISwapRouter.ExactInputSingleParams memory exactInputSingleParams = ISwapRouter.ExactInputSingleParams(address(_collateral), tradeParams.tokenOut, POOL_FEE_TIER, msg.sender, tradeParams.deadline, _collateralAmountMinted, tradeParams.amountOutMinimum, tradeParams.sqrtPriceLimitX96);
    _swapRouter.exactInputSingle(exactInputSingleParams);
  }

It uses permit function to provide allowance to himself and it sets allowance to type(uint256).max. After the transfer it doesn't set allowance to 0.
And next time when user will call this function it will revert if _baseToken doesn't support providing allowance when current allowance is not set to 0(like USDT).

Tools Used

VsCode

Recommended Mitigation Steps

Do not set allowance to type(uint256).max, set it to baseTokenAmount value.

@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 Dec 12, 2022
code423n4 added a commit that referenced this issue Dec 12, 2022
@c4-judge c4-judge added QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax downgraded by judge Judge downgraded the risk level of this issue and removed 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value labels Dec 17, 2022
@c4-judge
Copy link
Contributor

Picodes changed the severity to QA (Quality Assurance)

@c4-judge
Copy link
Contributor

Picodes marked the issue as grade-c

@c4-judge c4-judge reopened this Dec 19, 2022
@c4-judge c4-judge added grade-b and removed grade-c unsatisfactory does not satisfy C4 submission criteria; not eligible for awards labels Dec 19, 2022
@c4-judge
Copy link
Contributor

Picodes marked the issue as grade-b

@c4-judge c4-judge added grade-a and removed grade-b labels Dec 19, 2022
@c4-judge
Copy link
Contributor

Picodes marked the issue as grade-a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue grade-a Q-20 QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax
Projects
None yet
Development

No branches or pull requests

3 participants