Repeated Calls to Shelter.withdraw Can Drain All Funds in Shelter
The withdraw function in the Shelter contract does not check if a user has already withdrawn their tokens, allowing a user who has deposited funds to call withdraw repeatedly and drain the shelter's entire balance, including other users' funds.
Submitted by mtz, also found by 0x1f8b, 0xliumin, bitbopper, cccz, cmichel, csanuragjain, Czar102, danb, Alex the Entreprenerd, GeekyLumberjack, gzeon, hickuphh3, hyh, leastwood, Randyyy, Rhynorater, Ruhum, and ShadowyNoobDev
tl;dr Anyone who can call withdraw to withdraw their own funds can call it repeatedly to withdraw the funds of others. withdraw should only succeed if the user hasn’t withdrawn the token already.
The shelter can be used for users to withdraw funds in the event of an emergency. The withdraw function allows callers to withdraw tokens based on the tokens they have deposited into the shelter client: ConvexStakingWrapper. However, withdraw does not check if a user has already withdrawn their tokens. Thus a user that can withdraw tokens, can call withdraw repeatedly to steal the tokens of others.
Proof of Concept
tl;dr an attacker that can successfully call withdraw once on a shelter, can call it repeatedly to steal the funds of others. Below is a detailed scenario where this situation can be exploited.
- Mallory deposits 1
wETHintoConvexStakingWrapperusingdeposit. Let’s also assume that other users have deposited 2wETHinto the same contract. - An emergency happens and the owner of
ConvexStakingWrappercallssetShelter(shelter)andenterShelter([pidOfWETHToken, ...]). Nowshelterhas 3wETHand is activated forwETH. - Mallory calls
shelter.withdraw(wETHAddr, MalloryAddr), Mallory will rightfully receive 1 wETH because her share of wETH in the shelter is 1/3. - Mallory calls
shelter.withdraw(wETHAddr, MalloryAddr)again, receiving 1/3*2 = 2/3 wETH.withdrawdoes not check that she has already withdrawn. This time, the wETH does not belong to her, she has stolen the wETH of the other users. She can continue callingwithdrawto steal the rest of the funds
Recommended Mitigation Steps
To mitigate this, withdraw must first check that msg.sender has not withdrawn this token before and withdraw must also record that msg.sender has withdrawn the token. The exact steps for this are below:
- Add the following line to the beginning of
withdraw(line 53):
require(!claimed[_token][msg.sender], "already claimed")
- Replace line 55 with the following:
claimed[_token][msg.sender] = true;
This replacement is necessary because we want to record who is withdrawing, not where they are sending the token which isn’t really useful info.
Alex the Entreprenerd (judge) commented:
The warden has identified a logical fallacy in the
Sheltercontract.This would allow a caller to claim their tokens multiple times, as long as they send them to a new address.
Mitigation is as simple as checking claims against
msg.sender, however because all funds can be drained, this finding is of High Severity.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The withdraw function lacks a check to prevent a user from withdrawing the same token multiple times, and it does not record that a withdrawal has occurred.
Impact
An attacker who can call withdraw once can repeatedly call it to drain all funds from the shelter, stealing other users' tokens.
What the attacker needed
Attacker has deposited tokens into the shelter; shelter is activated for the token; attacker can call withdraw
The fix
The withdraw function should check that the caller has not already withdrawn the token and record the withdrawal against the caller's address.
How it was found
- Mallory deposits 1 wETH into ConvexStakingWrapper, and other users deposit 2 wETH.
- An emergency occurs and the owner calls setShelter and enterShelter, moving 3 wETH into the shelter.
- Mallory calls shelter.withdraw(wETHAddr, MalloryAddr) and receives 1 wETH (her share).
- Mallory calls shelter.withdraw(wETHAddr, MalloryAddr) again and receives 2/3 wETH that belongs to others.
- Mallory can continue calling withdraw to steal the remaining funds.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A smart contract called Shelter lets people take back tokens they deposited during an emergency. But the contract forgot to remember who had already taken their tokens, so anyone who took theirs once could just ask again and again, each time getting more tokens that actually belonged to other people.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.