Skip to content
Smart contracts Audit finding Advanced

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.

camden Follow

Originally disclosed on Code4rena

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

naps62 (Sandclock) resolved:

Fixed in https://github.com/sandclock-org/solidity-contracts/pull/75

The original report as published on Code4rena, by camden. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

  1. Attacker makes two deposits of 100 UST each and waits until they are withdrawable.
  2. Attacker triggers a withdraw on one deposit position.
  3. During the withdrawal callback (onDepositBurned), attacker triggers another 100 UST deposit.
  4. 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.

More by camden

View all

5 more disclosed reports, best first.

More from Sandclock contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.