Method refundPlayers doesn't update _lockedETH in WinnableTicketManager
The refundPlayers function does not subtract refunded amounts from _lockedETH, so after a raffle is cancelled and refunds are issued, the locked ETH remains inflated. This causes the owner to be unable to withdraw the full protocol income from later completed raffles, leaving funds stuck in the contract.
0x0bserver Follow with 0x6a70, 0x73696d616f and 47 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-08-winnables-raffles-judging/issues/138
Found by
0x0bserver, 0x6a70, 0x73696d616f, 0xShahilHussain, 0xbrivan, 0xjarix, 4gontuk, Afriaudit, AllTooWell, AuditorPraise, BlocSoc_Audits, CatchEmAll, Galturok, GenevaRoc, IvanFitro, MSK, Offensive021, Paradox, Penaldo, PeterSR, S3v3ru5, Silvermist, TessKimy, Waydou, almurhasan, anonymousjoe, araj, charles__cheerful, dany.armstrong90, dimi6oni, dimulski, dobrevaleri, gajiknownnothing, gerd, gkrastenov, iamnmt, ihtishamsudo, ironside, irresponsible, matejdb, neko_nyaa, ni8mare, oxelmiguel, oxwhite, p0wd3r, pandasec, pashap9990, phoenixv110, sakshamguruji, shikhar, shui, utsav, vinica_boy, y4y
Summary
The variable _lockedETH keeps track of the ETH(AVAX) collected by the raffles that are underway. The owner can't withdraw this amount. If a raffle is cancelled then users get to withdraw their ETH(AVAX) paid to buy tickets. But the _lockedETH is not updated. So in the future raffle which do gets completed the owner is supposed to get the ticket amount. But since the _lockedETH from previously wasn't set to 0 it having some value leads to that much amount getting stuck in the contract forever.
Root Cause
In https://github.com/sherlock-audit/2024-08-winnables-raffles/blob/main/public-contracts/contracts/WinnablesTicketManager.sol#L215-L228 the refunded amount should've been subtracted from _lockedETH amount. Since it's not updated the owner will not be able to withdraw this much amount ever https://github.com/sherlock-audit/2024-08-winnables-raffles/blob/main/public-contracts/contracts/WinnablesTicketManager.sol#L300-L306
Internal pre-conditions
No response
External pre-conditions
No response
Attack Path
- A new raffle starts. Alice buys tickets worth of 2 ETH(AVAX). The
_lockedETHis updated from 0 to 2. - Bob buys tickets worth of 1 ETH(AVAX). The
_lockedETHis updated from 2 to 3. - Now the Raffle gets cancelled because of some reason. Both Alice and Bob withdraws their money
refundPlayers() - In Future another raffle starts. Both Alice and Bob stakes 2 ETH each. The
_lockedETHgets updated to 4 + 3 = 7. - The Raffle finishes and winner is chosen. The
propagateRaffleWinner()updated_lockedETHto 7-4 = 3. - Now when the Owner tries to withdraw the payment which was 4 ETH it reverts since the
_lockedETHis 3. So the owner is only allowed to withdraw 1ETH. Rest of the 3ETH will be stuck in the contract.
Impact
It leads to locking of ETH(AVAX) in the contract forever that was protocol income.
PoC
Add the following snippet in /test/TicketManager.js
it('Should be able to refund tickets purchased', async () => {
const contractBalanceBefore = await ethers.provider.getBalance(manager.address);
const userBalanceBefore = await ethers.provider.getBalance(buyer2.address);
let lockedETH = await manager.getLockedEth()
console.log("Locked ETH is: ", lockedETH)
const tx = await manager.refundPlayers(1, [buyer2.address]);
lockedETH = await manager.getLockedEth()
console.log("Locked ETH after player unlock is: ", lockedETH)
const { events } = await tx.wait();
expect(events).to.have.lengthOf(1);
const [ event ] = events;
expect(event.event).to.equal('PlayerRefund');
const contractBalanceAfter = await ethers.provider.getBalance(manager.address);
const userBalanceAfter = await ethers.provider.getBalance(buyer2.address);
expect(contractBalanceAfter).to.eq(contractBalanceBefore.sub(100));
expect(userBalanceAfter).to.eq(userBalanceBefore.add(100));
const { withdrawn } = await manager.getParticipation(1, buyer2.address);
expect(withdrawn).to.eq(true);
});
Output:
Locked ETH is: BigNumber { value: "100" }
Locked ETH after player unlock is: BigNumber { value: "100" }
Mitigation
Update the _lockedETH variable in refundPlayers() as below:
function refundPlayers(uint256 raffleId, address[] calldata players) external {
Raffle storage raffle = _raffles[raffleId];
if (raffle.status != RaffleStatus.CANCELED) revert InvalidRaffle();
for (uint256 i = 0; i < players.length; ) {
address player = players[i];
uint256 participation = uint256(raffle.participations[player]);
if (((participation >> 160) & 1) == 1) revert PlayerAlreadyRefunded(player);
raffle.participations[player] = bytes32(participation | (1 << 160));
uint256 amountToSend = (participation & type(uint128).max);
_lockedETH -= amountToSend;
_sendETH(amountToSend, player);
emit PlayerRefund(raffleId, player, bytes32(participation));
unchecked { ++i; }
}
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/Winnables/public-contracts/pull/5
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 refundPlayers function fails to decrease _lockedETH by the amount refunded to players.
Impact
ETH (AVAX) representing protocol income becomes permanently locked in the contract, as the owner cannot withdraw the full amount.
What the attacker needed
A raffle is cancelled; players request refunds; a later raffle completes
The fix
The refundPlayers function should subtract the refunded amount from _lockedETH before sending the refund.
How it was found
- A new raffle starts and Alice buys tickets worth 2 ETH, updating _lockedETH from 0 to 2.
- Bob buys tickets worth 1 ETH, updating _lockedETH from 2 to 3.
- The raffle is cancelled and both Alice and Bob call refundPlayers to get their money back.
- A future raffle starts and both Alice and Bob each stake 2 ETH, so _lockedETH becomes 4 + 3 = 7.
- The raffle finishes and propagateRaffleWinner updates _lockedETH to 7 - 4 = 3.
- The owner tries to withdraw the 4 ETH payment but the transaction reverts because _lockedETH is only 3, so only 1 ETH can be withdrawn and 3 ETH remain stuck.
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 people buy raffle tickets, their money is held in the contract and tracked by a variable called _lockedETH. If a raffle is cancelled, players get their money back, but the tracking variable is not reduced. Later, when a raffle finishes successfully, the owner tries to withdraw the profits, but the contract thinks more money is still locked than actually is, so the withdrawal fails or is limited.
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.