Withdrawers can get more value returned than expected with reentrant call
A reentrancy vulnerability in the vault's withdraw function allows an attacker to deposit during the callback and receive more UST than they should. The attacker can withdraw significantly more than their fair share.
Submitted by camden, also found by cmichel and harleythedog
The impact of this is that users can get significantly more UST withdrawn than they would be alotted if they had done non-reentrant withdraw calls.
Proof of Concept
Here’s an outline of the attack:
Assume the vault has 100 UST in it. The attacker makes two deposits of 100UST and waits for them to be withdrawable. The attacker triggers a withdraw one of their deposit positions. The vault code executes until it reaches this point: https://github.com/code-423n4/2022-01-sandclock/blob/a90ad3824955327597be00bb0bd183a9c228a4fb/sandclock/contracts/Vault.sol#L565 Since the attacker is the claimer, the vault will call back to the attacker. Inside onDepositBurned, trigger another 100 UST deposit. Since claimers.onWithdraw has already been called, reducing the amount of shares, but the UST hasn’t been transferred yet, the vault will compute the amount of UST to be withdrawn based on an unexpected value for _totalUnderlyingMinusSponsored (300). https://github.com/code-423n4/2022-01-sandclock/blob/a90ad3824955327597be00bb0bd183a9c228a4fb/sandclock/contracts/Vault.sol#L618
After the attack, the attacker will have significantly more than if they had withdrawn without reentrancy.
Here’s my proof of concept showing a very similar exploit with deposit, but I think it’s enough to illustrate the point. I have a forge repo if you want to see it, just ping me on discord. https://gist.github.com/CamdenClark/abc67bc1b387c15600549f6dfd5cb27a
Tools Used
Forge
Recommended Mitigation Steps
Reentrancy guards.
Also, consider simplifying some of the shares logic.
ryuheimat (Sandclock) confirmed
Fixed in https://github.com/sandclock-org/solidity-contracts/pull/75
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
Likely the contract updates share balances before transferring funds, and allows reentrant calls that manipulate the total underlying value used for withdrawal calculations.
What the attacker needed
Attacker has funds to deposit; deposits must be withdrawable
The fix
The vulnerability was fixed by adding reentrancy guards and simplifying the shares logic.
How it was found
- Attacker makes two deposits of 100 UST each and waits until they are withdrawable.
- Attacker triggers a withdraw on one deposit position.
- During the withdrawal callback (onDepositBurned), attacker triggers another 100 UST deposit.
- The vault computes the withdrawal amount using an inflated total underlying value, resulting in more UST returned.
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.
This is a reentrancy bug in a smart contract. When the contract sends funds or calls an external contract, that external contract can call back into the original function before it finishes. Here, during a withdrawal, the attacker re-enters to make a deposit, which changes the internal accounting and lets them withdraw more money than they are owed.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.