Lock.sol: assets deposited with Lock.extendLock function are lost
The Lock contract's extendLock function increases a bond's locked amount without increasing the totalLocked accounting value for that asset. When the user later releases the bond, the release function subtracts the full amount from totalLocked, which underflows and reverts, so assets added via extendLock can never be withdrawn.
HollaDieWaldfee Follow with sha256yan, kaliberpoziomka8552 and 7 others
Originally disclosed on Code4rena
Submitted by HollaDieWaldfee, also found by sha256yan, kaliberpoziomka8552, 0xsomeone, cccz, 0xbepresent, ali_shehab, Ruhum, rvierdiiev, and csanuragjain
Impact
The Lock contract (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Lock.sol#L10) allows end-users to interact with bonds.
There are two functions that allow to lock some amount of assets. The first function is Lock.lock (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Lock.sol#L61-L76) which creates a new bond. The second function is Lock.extendLock (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Lock.sol#L84-L92). This function extends the lock for some _period and / or increases the locked amount by some _amount.
The issue is that the Lock.extendLock function does not increase the value in totalLocked[_asset]. This however is necessary because totalLocked[_asset] is reduced when Lock.release (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Lock.sol#L98-L105) is called.
Therefore only the amount of assets deposited via Lock.lock can be released again. The amount of assets deposited using Lock.extendLock can never be released again because reducing totalLocked[_asset] will cause a revert due to underflow.
So the amount of assets deposited using Lock.extendLock is lost.
Proof of Concept
- User A calls
Lock.lockto lock a certain_amount(amount1) of_assetfor a certain_period. - User A calls then
Lock.extendLockand increases the locked amount of the bond by some amount2 - User A waits until the bond has expired
- User A calls
Lock.release. This function calculatestotalLocked[asset] -= lockAmount;. Which will cause a revert because the value oftotalLocked[asset]is only amount1
You can add the following test to the Bonds test in Bonds.js:
describe("ReleaseUnderflow", function () {
it("release can cause underflow", async function () {
await stabletoken.connect(owner).mintFor(user.address, ethers.utils.parseEther("110"));
// Lock 100 for 9 days
await lock.connect(user).lock(StableToken.address, ethers.utils.parseEther("100"), 9);
await bond.connect(owner).setManager(lock.address);
await stabletoken.connect(user).approve(lock.address, ethers.utils.parseEther("10"));
// Lock another 10
await lock.connect(user).extendLock(1, ethers.utils.parseEther("10"), 0);
await network.provider.send("evm_increaseTime", [864000]); // Skip 10 days
await network.provider.send("evm_mine");
// Try to release 110 after bond has expired -> Underflow
await lock.connect(user).release(1);
});
});
Run it with npx hardhat test --grep "release can cause underflow".
You can see that it fails because it causes an underflow.
Tools Used
VS Code
Recommended Mitigation Steps
Add totalLocked[_asset] += amount to the Lock.extendLock function.
TriHaz (Tigris Trade) confirmed
Alex the Entreprenerd (judge) commented:
The warden has shown an issue with accounting that will cause principal deposits added via
extendLockto be lost, for this reason I agree with High Severity.
GainsGoblin (Tigris Trade) resolved:
Mitigation: https://github.com/code-423n4/2022-12-tigris/pull/2#issuecomment-1419172200
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 updates the individual bond amount but omits the matching increment of the totalLocked[_asset] accounting variable that release relies on.
Impact
Assets deposited through extendLock are permanently lost because release always reverts, so the user cannot withdraw the extra principal.
What the attacker needed
A bond already created via lock; user adds funds via extendLock
The fix
The extendLock function should add the deposited amount to totalLocked[_asset]; the project marked the issue resolved with a mitigation pull request.
How it was found
- User A calls Lock.lock to lock an amount of an asset for a period.
- User A calls Lock.extendLock to increase the locked amount of the bond.
- User A waits until the bond has expired.
- User A calls Lock.release, which subtracts the full lock amount from totalLocked and reverts due to underflow.
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 keeps a running tally of how much of each token is locked in total. When someone adds more tokens to an existing lock, the contract forgets to add that amount to the tally. Later, when the person tries to take their tokens back, the contract subtracts more than the tally holds, the maths goes negative, and the transaction fails, leaving the extra tokens stuck.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.