sherlock-audit / 2023-01-uxd-judging

3 stars 1 forks source link

duc - Deposit and withdraw to the vault with the wrong decimals of amount in contract `PerpDepository` #402

Open github-actions[bot] opened 1 year ago

github-actions[bot] commented 1 year ago

duc

high

Deposit and withdraw to the vault with the wrong decimals of amount in contract PerpDepository

Summary

Function vault.deposit and vault.withdraw of vault in contract PerpDepository need to be passed with the amount in raw decimal of tokens (is different from 18 in case using USDC, WBTC, ... as base and quote tokens). But some calls miss the conversion of decimals from 18 to token's decimal, and pass wrong decimals into them.

Vulnerability Detail

(uint256 baseAmount, uint256 quoteAmount) = _placePerpOrder( normalizedAmount, isShort, amountIsInput, sqrtPriceLimitX96 ); vault.withdraw(assetToken, baseAmount);

...


## Impact
Because of calling `vault.deposit` and `vault.withdraw` with the wrong decimal of the param amount, the protocol can lose a lot of funds. And some functionalities of the protocol can be broken cause it can revert by not enough allowance when calling these functions.
## Code Snippet
https://github.com/sherlock-audit/2023-01-uxd/blob/main/contracts/integrations/perp/PerpDepository.sol#L498
https://github.com/sherlock-audit/2023-01-uxd/blob/main/contracts/integrations/perp/PerpDepository.sol#L524
https://github.com/sherlock-audit/2023-01-uxd/blob/main/contracts/integrations/perp/PerpDepository.sol#L638
## Tool used

Manual Review

## Recommendation
Should convert the param `amount` from token's decimal to decimal 18 before `vault.deposit` and `vault.withdraw`.
WarTech9 commented 1 year ago

amount is always in the unit of the token being deposited so no normalization required.

hrishibhat commented 1 year ago

@WarTech9 just confirming that your comment is applicable to all the duplicates tagged here. Right?

WarTech9 commented 1 year ago

After further review this issue is valid. quoteAmount should be converted to quoteDecimals before vault.deposit() in _rebalanceNegativePnlWithSwap()

WarTech9 commented 1 year ago

Issue exists in _rebalanceNegativePnlWithSwap() before vault.deposit(). Need to convert from 18 decimals to 6 for USDC. Issue does not exist in _rebalanceNegativePnlLite where amount returned by _placePerpOrder() is already in 18 decimals. So no conversion required before vault.withdraw().

WarTech9 commented 1 year ago

Fixed here: https://github.com/UXDProtocol/uxd-evm/pull/27

IAm0x52 commented 1 year ago

Fix looks good. Decimals are now correctly adjusted for quote deposits