The TokenManager
contract contains two key functions: addTokenBalance()
and withdraw()
. The addTokenBalance()
function allows certain privileged contracts to increase a user's token balance in the userTokenBalanceMap
, while the withdraw()
function allows users to withdraw their recorded balance. However, there's a critical flaw in how these functions interact, potentially allowing users to withdraw more tokens than they should be able to.
The addTokenBalance()
function updates the user's balance in the contract's state:
The withdraw()
function allows users to withdraw their recorded balance:
The issue is that the withdraw()
function does not update the user's balance after a successful withdrawal. This means that a user can potentially withdraw their entire balance multiple times, as long as the addTokenBalance()
function has been called at least once to set a non-zero balance.
This vulnerability allows malicious users to drain the contract of funds far beyond their actual entitlement. A user could potentially withdraw the same balance multiple times, leading to significant financial losses for the protocol.
Alice has a legitimate balance of 100 tokens recorded in userTokenBalanceMap
.
Alice calls withdraw()
and successfully withdraws 100 tokens.
Alice's balance in userTokenBalanceMap
remains unchanged at 100 tokens.
Alice calls withdraw()
again and successfully withdraws another 100 tokens.
This process can be repeated until the contract is drained of funds.
Manual review
The withdraw()
function should update the user's balance after a successful withdrawal. Here's a suggested fix:
This change ensures that a user's balance is set to zero immediately before the transfer is initiated, preventing multiple withdrawals of the same balance. Additionally, consider implementing a reentrancy guard to prevent potential reentrancy attacks during the withdrawal process.
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)
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.