BondNFT.extendLock force a user to extend the bond at least for current bond.period
The BondNFT.extendLock function incorrectly adds the requested extension period to the bond's original period rather than to the remaining time, forcing users to extend for at least the full original period. This means bonds with a 365-day lock cannot be extended at all, and users extending shorter bonds are locked in longer than intended.
Originally disclosed on Code4rena
Submitted by carlitox477
The current implementation forces a user to extend their bonds for at least they current bond period. This means that, for instance, a bond which was initially locked for 365 can never be extended, even after a week of being created.
If we consider that a bond should have at least a 7 days lock and at the most 365 days, then the current BondNFT.extendLock function should be refactored.
Impact
- Current
BondNFT.extendLockfunction does not work as expected, forcing user who wants to extend their bond to extend them at least for their current bond.period. - For bonds which were set with a lock period of 365 days, they can not be extended, even after days of their creation.
Proof of Concept
// In 09.Bond.js, describe "Extending lock"
it("POC: Extending the lock does not work as expected", async function () {
await stabletoken.connect(owner).mintFor(user.address, ethers.utils.parseEther("100"));
// user lock bond funds for 10 days
await lock.connect(user).lock(StableToken.address, ethers.utils.parseEther("100"), 10);
const fiveDaysTime = 5 * 24 * 60 * 60
const eightDaysTime = 8 * 24 * 60 * 60
// owner distribute rewards
console.log("User created a lock for 10 days")
await stabletoken.connect(owner).mintFor(owner.address, ethers.utils.parseEther("10"));
await bond.connect(owner).distribute(stabletoken.address, ethers.utils.parseEther("10"));
// Five days pass
await network.provider.send("evm_increaseTime", [fiveDaysTime]); // Skip 10 days
await network.provider.send("evm_mine");
console.log("\n5 days pass")
// User decide to extend their lock three days, given the current implementation the user is forced to extended 13 days
const bondInfoBeforeExtension = await bond.idToBond(1)
console.log(`Bond info before extension: {period: ${bondInfoBeforeExtension.period}, expireEpoch: ${bondInfoBeforeExtension.expireEpoch}}`)
await lock.connect(user).extendLock(1, 0, 3)
console.log("Bond was extended for 3 days")
const bondInfoAfterExtension = await bond.idToBond(1)
console.log(`Bond info after extension: {period: ${bondInfoAfterExtension.period}, expireEpoch: ${bondInfoAfterExtension.expireEpoch}}`)
// 8 days pass, user should be able to release the bond given the extension of 3 days (8 days should be enough)
await network.provider.send("evm_increaseTime", [eightDaysTime]);
await network.provider.send("evm_mine");
console.log("\n8 days later")
console.log("After 13 days (10 original days + 3 days from extension) the user can not release the bond")
// The user decide to claim their part and get their bond amount
// The user should recieve all the current funds in the contract
await expect(lock.connect(user).release(1)).to.be.revertedWith('!expire')
});
Recommended Mitigation Steps
In order to extendLock to work properly, the current implementation should be changed to:
function extendLock(
uint _id,
address _asset,
uint _amount,
uint _period,
address _sender
) external onlyManager() {
Bond memory bond = idToBond(_id);
Bond storage _bond = _idToBond[_id];
require(bond.owner == _sender, "!owner");
require(!bond.expired, "Expired");
require(bond.asset == _asset, "!BondAsset");
require(bond.pending == 0); //Cannot extend a lock with pending rewards
+ uint currentEpoch = block.timestamp/DAY;
- require(epoch[bond.asset] == block.timestamp/DAY, "Bad epoch");
require(epoch[bond.asset] == currentEpoch, "Bad epoch");
+ uint pendingEpochs = bond.expireEpoch - currentEpoch;
+ uint newBondPeriod = pendingEpochs + _period;
+ //In order to respect min bond period when we extend a bon
+ // Next line can be omitted at discretion of the protocol and devs
+ // If it is omitted any created bond would be able to be extended always (except from those with period = 365)
+ require(newBondPeriod >= 7, "MIN PERIOD");
- require(bond.period+_period <= 365, "MAX PERIOD");
+ require(newBondPeriod <= 365, "MAX PERIOD");
unchecked {
- uint shares = (bond.amount + _amount) * (bond.period + _period) / 365;
+ uint shares = (bond.amount + _amount) * newBondPeriod / 365;
- uint expireEpoch = block.timestamp/DAY + bond.period + _period;
+ uint expireEpoch = currentEpoch + newBondPeriod;
totalShares[bond.asset] += shares-bond.shares;
_bond.shares = shares;
_bond.amount += _amount;
_bond.expireEpoch = expireEpoch;
_bond.period += _period;
_bond.mintTime = block.timestamp;
- _bond.mintEpoch = epoch[bond.asset];
+ _bond.mintEpoch = currentEpoch;
- bondPaid[_id][bond.asset] = accRewardsPerShare[bond.asset][epoch[bond.asset]] * _bond.shares / 1e18;
+ bondPaid[_id][bond.asset] = accRewardsPerShare[bond.asset][currentEpoch] * _bond.shares / 1e18;
}
emit ExtendLock(_period, _amount, _sender, _id);
}
Alex the Entreprenerd (judge) commented:
The warden has shown that the mechanic for extending locks can cause lock duration to be longer than intended, while rewards math will behave as inputted by the user.
While an argument for this being a user mistake could be made, I believe that in this case the demonstrated logic flaw takes precedence, that’s because a user interacting with the system as intended will still be locked for longer than intended and receive less rewards for that mistake.
For this reason (conditionality, logic flaw, no loss of principal) I believe Medium Severity to be appropriate.
GainsGoblin (Tigris Trade) confirmed and commented:
Mitigation: https://github.com/code-423n4/2022-12-tigris/pull/2#issuecomment-1419175579
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 extendLock function calculates the new period by adding the extension to the original bond.period instead of the remaining epochs, and uses the original period for share and expiry calculations.
Impact
Users who extend a bond are locked for longer than intended and receive fewer rewards for that period; bonds with a 365-day lock cannot be extended at all.
What the attacker needed
A bond with a lock period; the bond has not expired; the bond has no pending rewards
The fix
The extendLock function should calculate the new period based on the remaining epochs plus the extension, and use that for share and expiry calculations.
How it was found
- User locks funds for 10 days.
- Owner distributes rewards.
- Five days pass.
- User calls extendLock with a 3-day extension.
- The bond's period becomes 13 days instead of 3, and the user cannot release until 13 days after the extension.
- Eight days later (13 days total), the user still cannot release because the bond has not expired.
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 bond is a commitment to lock funds for a set time. The extendLock function is meant to let you add more time, but it mistakenly adds the new time to the original lock length instead of the time left. So if you locked for 10 days and after 5 days ask to add 3 days, you end up locked for 13 more days, not 3.
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.