Attackers can abuse the quitLock function to get a very large amount of votes
The quitLock function in a voting escrow contract allows an attacker to use a flash loan to create a large voting power and then immediately quit, potentially passing malicious proposals. The attack requires paying a penalty proportional to the lock duration, which may offset the gained votes.
Originally disclosed on Code4rena
Submitted by CertoraInc
An attacker can use a flashloan and the quitLock function to achieve a large amount of votes for one transaction. It can, depends on the implementation of the modules that will use this contract, be used to pass malicious proposals or exploit any feature that depends on the voting balance.
Proof of Concept
We assume here that there is a contract that provides a flashloan (for simplicity without fees, but can also work with fees, just requires the attacker to provide a larger amount of tokens) for the token that is used by the VotingEscrow contract.
- The attacker deploys a smart contract that implements the following logic and approves the contract for the token that is used in the
VotingEscrowcontract (of course assigning all the values of the variables). - The attacker calls the
attackfunction with the amount he wants to take as a flashloan (he will need to cover a penalty based on that amount). - The
attackfunction calculates the penalty and transfers it from the attacker. - The
flashloanfunction is called, which provides the tokens to the contract and then calls theflashloanCallbackwith the lent amount. - The
flashloanCallbackfunction creates a lock with the amount received from the flashloan for a week (the unlock time can be changed to achieve larger votes balance, but it must be considered when calculating the penalty). - The attacker can do whatever he wants with the amount of votes the contract currently has.
- The quitLock function is called to get back the funds (excluding the penalty), and the loan is payed back.
contract VotingEscrowAttack {
IERC20 constant token = IERC20(0x...); // the token used in the VotingEscrow contract
IVotingEscrow constant votingEscrow = IVotingEscrow(0x...); // the address of the VotingEscrow contract
uint constant WEEK = 1 weeks;
uint constant MAXTIME = 365 days;
uint constant MAXPENALTY = 10**18;
uint constant PENALTY_RATE = (WEEK * MAXPENALTY) / MAXTIME;
IFlashLoan constant flashloanContract = IFlashLoan(0x...); // the address of the flashloan provider
function attack(uint amount) external {
uint penalty = (amount * PENALTY_RATE) / (10 ** 18);
token.transferFrom(msg.sender, penalty); // assuming no flashloan fee
IFlashLoan.flashloan(token, amount);
}
function flashloanCallback(uint amount) {
votingEscrow.createLock(amount, block.timestamp + WEEK); // create a lock for a week with a very large of token
// do whatever you want with your large amount of votes
votingEscrow.quitLock();
token.transfer(msg.sender, amount); // pay back the flashloan
}
}
- The function names, arguments and return values might not be accurate and might change depends on the used flashloan platform, but this contract is just to give a general idea of an attack vector.
Tools Used
Manual auditing - VS Code and me :)
Recommended Mitigation Steps
Think about implementing a mechanism that prevents users from creating a lock and quitting it in the same transaction, that way attackers won’t be able to use flashloan in order to achieve large voting power. However, regular loans will still be a problem with that fix, and if this isn’t a wanted behavior, additional fix is needed to be thought of.
lacoop6tu (FIAT DAO) acknowledged
elnilz (FIAT DAO) disagreed with severity and commented:
We actually could mitigate the issue by restricting increasing voting power and quitting in the same block. This would make the implementation safer.
But even then, the severity is rather 2 as funds are not directly at risk.
Justin Goro (judge) commented:
The scope of voting power is unclear. It may be that a proposal becomes possible that enables large funds transfers. For this reason, severity is maintained.
elnilz (FIAT DAO) disputed and commented:
Did review the economics of this attack and they are the same than without quitLock:
First, note that both balanceOf (voting power) and fee paid in quitLock are proportional to
(locked.end-block.timestamp)/MAXTIMEor remaining lock duration. In other words, in order to gain voting power, user also accepts higher quitLock fee.For max voting power user locks with MAXTIME. When quitting, this also results in the loss of all her locked tokens. Because the voting power decreases linearly with remaining lock duration, the amount of tokens that need be locked in order to achieve same voting power increases with lower lock durations. This results in the same quitLock fee as if the user would chose max lock duration or if user would not be able to quit at all.
See this interactive graph and play with the t (remaining lock duration) and N (tokens locked) sliders: https://www.desmos.com/calculator/yjby9zempb
Thus, quitLock doesn’t change the economics of governance attacks and so we dispute this issue.
Justin Goro (judge) decreased severity to Medium and commented:
Upon reconsideration, it appears the dampening effect of the penalty ensures that this FlashLoan attack is only really viable at very low impact levels. The larger the attack, the more the cost of the attack will exceed any benefit. The only case where this wouldn’t be the case is when a small vote can tip the balance of an important decision which is unlikely in a gauge style vote. But even if a threshold emerges, the attacker may as well just get the votes through normal channels.
Nonetheless, the vector is only dampened, not eradicated and so I’ll be downgrading this to Medium severity.
Thanks for reconsidering. I’d also be curious about feedback from CertoraInc as it’s been reported by the user.
The implications of the above analysis are, however, that the cost of an attack is the same as with Curve’s VotingEscrow which doesn’t implement a quitLock function. So unless the attack vector on Curve itself is also a medium severity this issue should be invalidated.
IllIllI (warden) reviewed mitigation:
The sponsor disputed the issue on the basis of an economic analysis of the penalty taken vs the votes gained, the outcome of which was that the penalty always covers the votes gained. I spoke with the sponsor and the sponsor explained that for the original Curve Finance code, the number of votes one gets is not equal one-for-one to the number of tokens locked: locking for the maximum duration will get you close to one-for-one, but every second under that number, the locking user gets fewer and fewer votes. Therefore in veFTD, the penalty is always chosen such that when the penalty is subtracted from the votes gained, the number of votes a user is left with after quitting is equal to the number of votes they would have been given had they used the quit time as their lock end time instead. In other words, the votes one would get for locking for the week the warden mentions, would be equal to the penalty they gather ahead of time, so the flash loan does not help the attacker.
To verify all of this, I wrote two tests that make use of hardhat’s ability to mine specific blocks with specific times. The first test confirmed that indeed, when one subtracts the penalty from the number of votes gained, the remaining number of votes is less than the number of votes a separate user gains for locking for the shorter duration, so it’s always better to specify the correct lock time rather and unlocking early. The second test verifies that the same is true even if one quits the second after the lock is created. Finally, I wrote a test that specifically does the flash loan scenario the warden outlined, and was able to show that when the attacking contract checks its balance between locking and quitting for the previous block, the votes are zero, and for the current block, the penalty is larger than the votes gained (and one cannot query vote balances for future blocks). Once the contract’s attack call completes, checks for the votes for same blocks show zero votes for both, so I believe the warden’s finding is invalid.
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 ability to create a lock and quit it within the same transaction, combined with a penalty that may not fully offset the gained voting power.
Impact
The attacker could gain a large amount of voting power for one transaction, potentially passing malicious proposals, but the penalty may limit the viability to low-impact scenarios.
What the attacker needed
A flash loan provider for the token; attacker can pay the penalty upfront; voting system uses this contract
The fix
The project should implement a mechanism that prevents creating a lock and quitting it in the same transaction, though regular loans would still be a problem.
How it was found
- Deploy a smart contract that approves the token used in VotingEscrow.
- Call the attack function with the desired flash loan amount, covering the penalty.
- The attack function calculates and transfers the penalty from the attacker.
- The flashloan function provides tokens and calls flashloanCallback.
- The callback creates a lock with the borrowed tokens for a week.
- The attacker uses the votes, then calls quitLock to retrieve funds (minus penalty) and repays the loan.
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.
A voting system lets people lock tokens to get voting power. The quitLock function lets them unlock early but pay a penalty. An attacker could borrow a huge amount of tokens temporarily (a flash loan), lock them to get many votes, use those votes, then quit and repay the loan. The penalty is meant to make this costly, but the report argues it might still be profitable in some cases.
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.