Function HubPoolLogic::updateWithWithdraw() doesn't round up in favour of protocol if isFAmount == false. This implies that users will receive more underlying tokens than they should due to rounding errors.
Vulnerability Details
Function HubPoolLogic::updateWithWithdraw() is called by LoanManager::executeWithdraw() to calculate underlying and fAmount to be withdrawn.
This is the function snippet
function updateWithWithdraw(
HubPoolState.PoolData storage pool,
uint256 amount,
bool isFAmount
) external returns (DataTypes.WithdrawPoolParams memory withdrawPoolParams) {
// can withdraw even if pool is depreciated
// update interest indexes before the interest rates change
pool.updateInterestIndexes();
if (isFAmount) {
withdrawPoolParams.fAmount = amount;
withdrawPoolParams.underlingAmount = amount.toUnderlingAmount(pool.depositData.interestIndex);
} else {
withdrawPoolParams.underlingAmount = amount;
withdrawPoolParams.fAmount = amount.toFAmount(pool.depositData.interestIndex);
}
pool.depositData.totalAmount -= withdrawPoolParams.underlingAmount;
pool.updateInterestRates();
}
In case isFAmount == false the fAmount is calculated as follows:
function toFAmount(uint256 underlyingAmount, uint256 depositInterestIndexAtT) internal pure returns (uint256) {
return underlyingAmount.mulDiv(ONE_18_DP, depositInterestIndexAtT);
}
Since depositInterestIndexAtT is always grater than ONE_18_DP the fAmount will be rounded down.
That means pool.depositData.totalAmount -= withdrawPoolParams.underlingAmount; will be decreased by the amount required by the user while loan.collaterals[poolId].balance -= fAmount; will be decreased by a smaller amount.
Impact Details
This bug can be exploited by a malicious user by withdrawing 1 wei in a for loop, or even in the long run by all users which carry out multiple withdrawals of deposited funds.
Proof of concept
POC
The following POC shall be run in Forge. The aim is to check the user and pool balances after withdrawal of 1 wei.
This test forks mainnet, and starts from a withdrawal onchain transaction. This is the transaction https://testnet.snowtrace.io/tx/0xbc7c3f5d5447d7a40c741f92a8e789c8ad588b618d699cffea29e091955e81a4?chainid=43113
The POC shall be run with the following command: forge test --match-test test_withdraw_1_wei --fork-url https://api.avax-test.network/ext/bc/C/rpc --fork-block-number 35147412 -vv --via-ir --optimizer-runs 10000
This is the console output. The amount of underlying token received back by user increases, the pool amount decreases, while the user fToken amount doesn't change.
[PASS] test_withdraw_1_wei() (gas: 845444793)
Logs:
User Initial Balance 0
User initial fAmount balance 9.9948666910183675e16
Pool initial deposit total amount 2.029690866072611417037e21
User final Balance 4000
User final fAmount balance 9.9948666910183675e16
Pool final deposit total amount 2.029690866072611413037e21