Skip to content
DoS Audit finding Intermediate

An attacker can leave the protocol in a “drawing” state for extended period of time

The protocol's random number request function uses a try/catch block that, if the external call fails, leaves the system in a state where draws cannot proceed until a delay passes. An attacker can deliberately cause the external call to fail by supplying insufficient gas, repeatedly triggering the delay and keeping the protocol in a 'drawing' state for extended periods.

alexxander Follow

Originally disclosed on Code4rena

Submitted by alexxander

https://github.com/code-423n4/2023-03-wenwin/blob/91b89482aaedf8b8feb73c771d11c257eed997e8/src/RNSourceController.sol#L106-L120

https://github.com/code-423n4/2023-03-wenwin/blob/91b89482aaedf8b8feb73c771d11c257eed997e8/src/RNSourceController.sol#L60-L75

https://github.com/code-423n4/2023-03-wenwin/blob/91b89482aaedf8b8feb73c771d11c257eed997e8/src/RNSourceController.sol#L89-L104

The current implementation of Random Number Generation uses Chainlink’s V2 Direct Funding Method (https://docs.chain.link/vrf/v2/direct-funding).

VRFv2RNSource.sol (inherits Chainlink’s VRFV2WrapperConsumerBase.sol) is responsible for handling requests and responses for the Lottery system. The communicator between VRFv2RNSource.sol contract and Lottery.sol is RNSourceController.sol. The ideal flow of control is the following:

  1. Any user can call executeDraw() in Lottery.sol assuming that the current draw is past the scheduled time for registering tickets.
  2. executeDraw() puts the system in the state of drawExecutionInProgress = true and calls requestRandomNumber().
  3. in RNSourceController.sol - requestRandomNumber() checks if the previous draw was completed and calls requestRandomNumberFromSource().
  4. requestRandomNumberFromSource() records the timestamp of the request in lastRequestTimestamp = block.timestamp and sets lastRequestFulfilled = false i.e executeDraw() cannot be called until the draw is finished. Lastly source.requestRandomNumber() is invoked.
  5. Now source.requestRandomNumber() calls requestRandomnessFromUnderlyingSource() and that subsequently calls requestRandomness() to generate a RN from Chainlink VRF.
  6. Several blocks later Chainlink VRF has verified a RN and sends a callback call to fulfillRandomWords() that calls fulfill(), which calls onRandomNumberFulfilled() in the RNSourceController.sol that sets lastRequestFulfilled = true and lastly receiveRandomNumber(randomNumber) is invoked in Lottery.sol that sets drawExecutionInProgress = false and starts a new draw (increments currentDraw state variable).

The culprit for this issue is the implementation of requestRandomNumberFromSource() in RNSourceController.sol. After lastRequestFulfilled = false the invocation to VRFv2RNSource.sol is done in a try{} catch{} block -

        lastRequestTimestamp = block.timestamp;
        lastRequestFulfilled = false;

        try source.requestRandomNumber() {
            emit SuccessfulRNRequest(source);
        } catch Error(string memory reason) {
            emit FailedRNRequest(source, bytes(reason));
        } catch (bytes memory reason) {
            emit FailedRNRequest(source, reason);
        }
    }

This is very problematic due to how try{} catch{} works - OpenZeppelin article. If the request to Chainlink VRF fails at any point then execution of the above block will not revert but will continue in the catch{} statements only emitting an event and leaving RNSourceController in the state lastRequestFulfilled = false and triggering the maxRequestDelay (currently 5 hours) until retry() becomes available to call to retry sending a RN request. This turns out to be dangerous since there is a trivial way of making Chainlink VRF revert - simply not supplying enough gas for the transaction either initially in calling executeDraw() or subsequently in retry() invocations with the attacker front-running the malicious transaction.

Proof of Concept

  1. Ticket registration closes, attacker calls executeDraw() with insufficient gas and Lottery is put in the Executing Draw State (drawExecutionInProgress = true).
  2. Attacker front-runs the transaction if there are other executeDraw() transactions.
  3. RNSourceController.sol calls VRFv2RNSource.so in a try{} catch{} block, VRF transaction reverts and lastRequestFulfilled remains equal to false.
  4. After “maxRequestDelay” time has past retry() becomes available that relies on the same try{} catch{} block in requestRandomNumberFromSource().
  5. Attacker calls retry() with insufficient gas and front-runs the transaction if there are other retry() transactions.
  6. Attacker repeats steps 4 and 5 leaving the system in a Drawing state for extended period of time (5 hours for every retry() in the example implementation).

Moreover, the attacker doesn’t have any incentive to deposit LINK himself since VRF will also revert on insufficient LINK tokens.

This Proof of Concept was also implemented and confirmed in a Remix environment, tested on the Ethereum Sepolia test network. A video walk-through can be provided on my behalf if requested by judge or sponsors.

Impact

System is left for extended period of time in “Drawing” state without the possibility to execute further draws, user experience is damaged significantly.

Comment

Building upon this issue, an obvious observation arises - there is the swapSource() method that becomes available ( only to the owner ) after a predefined number of failed retry() invocations - maxFailedAttempts. Therefore, potentially, admins of the protocol could replace the VRF solution with a better one that is resistant to the try catch exploit? It turns out that the current implementation of swapSource() introduces a new exploit that breaks the fairness of the protocol and an edge case could even be constructed that leads to an attacker stealing a jackpot.

rand0c0des (Wenwin) confirmed and commented:

I communicated with warden. This is confirmed as an issue.

cccz (judge) commented:

From alexxander (warden):

Hello, I looked deeper into Chainlink VRF implementation and it turns my reasoning behind why the vulnerability happens in the poc video is not correct. Chainlink does custom gas checks (in the callback) but not in computing the requests so indeed it reverts with out-of-gas, however there is a small detail that the caller always retains 1/64th of the gas as per the solidity documentation note on try{} catch{} - https://docs.soliditylang.org/en/v0.8.19/control-structures.html#try-catch - quoting:

“The reason behind a failed call can be manifold. Do not assume that the error message is coming directly from the called contract: The error might have happened deeper down in the call chain and the called contract just forwarded it. Also, it could be due to an out-of-gas situation and not a deliberate error condition: The caller always retains at least 1/64th of the gas in a call and thus even if the called contract goes out of gas, the caller still has some gas left.”

In short, depending on how much the gasLimit was set at, in some situations there will be enough gas left to finish the catch{} clause although the try{} threw a out-of-gas error.

I also found a Consensys auditor that has documented in principle the exact same issue as in the contest implementation and confirms it happens due to 1/64 gas retention - https://twitter.com/cleanunicorn/status/1574808522130194432?lang=en

Finally, I notified Rando about what I further found and also sent him Foundry traces of a successful malicious tx that reverts with out-of-gas but finishes execution of the catch{} clause, emits an event and leaves the protocol with cooldown.


The original report as published on Code4rena, by alexxander. 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 use of a try/catch block around an external call that, on failure, does not revert the state change (setting lastRequestFulfilled to false), allowing an attacker to force the failure and trigger a cooldown.

Impact

The system is left in a drawing state for extended periods, preventing further draws and damaging user experience.

What the attacker needed

Attacker can call executeDraw(); attacker can front-run transactions; attacker can supply insufficient gas

How it was found

  1. Ticket registration closes, attacker calls executeDraw() with insufficient gas.
  2. Attacker front-runs other executeDraw() transactions.
  3. The external call to VRFv2RNSource reverts, but the catch block runs and lastRequestFulfilled remains false.
  4. After maxRequestDelay, retry() becomes available.
  5. Attacker calls retry() with insufficient gas and front-runs other retry() transactions.
  6. Attacker repeats steps 4 and 5 to keep the system in a drawing state.

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 smart contract lottery. When it needs a random number, it asks an outside service. If that request fails, the contract does not undo everything; it just notes the failure and waits before trying again. An attacker can make the request fail on purpose by not providing enough gas, so the lottery gets stuck waiting and cannot start new draws.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by alexxander

View all

10 more disclosed reports, best first.

More from Wenwin contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.