In the provided smart contract, a malicious recipient can prevent the cancellation of a stream by the sender through a frontrunning attack. This is possible due to the sequence of checks and state updates in the cancel
and _cancel
functions. Specifically, the vulnerability arises from the fact that an attacker can deplete the stream by withdrawing the remaining funds just before the sender's cancellation transaction is processed. If the stream is found to be depleted, the cancel
function will revert, thus preventing the sender from canceling the stream and reclaiming the remaining funds.
Scenario
The stream has remaining funds that can be withdrawn by the recipient.
The stream is cancelable by the sender.
The attacker can monitor and detect pending transactions in the memepool
The attacker submits a withdrawal transaction with a higher gas fee to ensure it is mined before the sender's cancellation transaction.
if (_streams[streamId].isDepleted) { revert Errors.SablierV2Lockup_StreamDepleted(streamId); }
if (streamedAmount >= amounts.deposited) { revert Errors.SablierV2Lockup_StreamSettled(streamId); }
Setup: Assume there is a stream with ID streamId
and remaining funds that the recipient has not yet withdrawn.
Monitor: The attacker monitors the memepool for pending transactions involving the cancel
function call for streamId
.
Frontrunning: When a cancel
transaction is detected, the attacker submits a withdraw
transaction to claim the remaining funds. This transaction is sent with a higher gas price to ensure it is mined before the cancel
transaction.
Execution: The withdraw
transaction is processed first, depleting the stream.
Reversion: When the cancel
transaction is subsequently mined, it reverts due to the stream being depleted.
MoreClaiming: Sender wanted to cancel the stream and withdraw remaining funds but malicious recipient always attempts withdraw before cancelling to withdraw all the remaining funds that sender doesn't want to stream
Atomic Operations
Combine the withdrawal and cancellation processes into a single atomic operation to ensure that no funds can be withdrawn once the cancellation process starts.
Time Locks and Delay Mechanisms
Introduce time locks or delay mechanisms to allow a buffer period before executing critical operations like cancellation. This would give legitimate users time to react to potential attacks.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.