Utilization rates are 0 when average assets are 0, which may be used to game maturity borrows / deposits / withdrawals
When the average floating assets are zero, the protocol calculates a utilization rate of zero, which results in the lowest possible interest rate for a borrow. An attacker could exploit this at market launch to take a large loan at a very low interest rate, potentially harming lenders.
At protocol launch, when previewFloatingAssetsAverage() is 0, a borrow may be taken with the lowest interest rate possibel due to the utilization being 0 when the average assets are 0.
Vulnerability Detail
In Market::initialize(), lastAverageUpdate is set to block.timestamp. When calculating the average assets, the formula used is:
As can be seen, if block.timestamp == lastAverageUpdate, averageFactor will be 1 - e^0 = 0, resulting in average assets equal to memFloatingAssetsAverage, which is 0 when no deposits were made. Thus, even if a user deposits and borrows, its deposit will still lead to previewFloatingAssetsAverage() == 0, allowing him to get a borrow, even if almost as big as its deposit (minus the adjust factor), using the lowest interest rate possible.
The following test confirms this behaviour, add it to Market.t.sol:
Solidity
function test_POC_BorrowAtMaturity_LowestRate() external {
uint256 assets = 10_000 ether;
ERC20 asset = market.asset();
deal(address(asset), ALICE, assets + 1);
uint256 maturity = FixedLib.INTERVAL * 2;
//skip(1 days); // Uncomment to confirm that if some time passes assetsOwed will increase
vm.startPrank(ALICE);
market.deposit(assets, ALICE);
uint256 assetsOwed = market.borrowAtMaturity(maturity, assets / 2, type(uint256).max, ALICE, ALICE);
assertEq(assetsOwed, 5076712328767123285000);
}
Impact
User borrows and places the protocol at a significant risk with a very small interest rate.
When assets are 0, the interest rate should be the maximum.
Additionally, when initializing, it's better to set the factor to some time in the past so the first depositor increases the utilization ratio accordingly.
Discussion
santipu03
I believe this issue may be invalid due to describing the intended design of the protocol. If I'm not mistaken, when the utilization is 0 (assets are 0), the interest rates should also be at the lowest point.
Waiting for the sponsor tags to confirm it.
C.C. @itofarina @cruzdanilo
0x73696d616f
Escalate
Utilization ratio is borrows / assets. When assets == 0, this is borrows / 0. It goes to infinity (1) if borrows != 0. If borrows == 0, it's 0/0, which should also go to 1 to protect the protocol (using 0 would favor borrowers).
Run the following POC to verify how the utilization ratios are 0 but there are 10_000 deposits and 6_400 borrows:
Utilization ratio is borrows / assets. When assets == 0, this is borrows / 0. It goes to infinity (1) if borrows != 0. If borrows == 0, it's 0/0, which should also go to 1 to protect the protocol (using 0 would favor borrowers).
Run the following POC to verify how the utilization ratios are 0 but there are 10_000 deposits and 6_400 borrows:
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
santipu03
The utilization rate doesn't get updated instantly because it's using the average of deposited assets instead of the current assets, and that is the intended design of the protocol. This report describes a byproduct of that mechanism that won't have much impact because in the worst case, it will allow a user in a new market to take a loan with a lower interest rate than it should.
This issue can be classified as a design improvement suggestion but I don't think it should be more than low severity because the current design may be suboptimal but it doesn't imply a loss of funds.
0x73696d616f
Firstly, I would like to mention that I agree with this statement
The utilization rate doesn't get updated instantly because it's using the average of deposited assets instead of the current assets, and that is the intended design of the protocol.
The intended design of the protocol is indeed using average assets, and I am not disputing this. Now, on to the following statement
This report describes a byproduct of that mechanism that won't have much impact because in the worst case, it will allow a user in a new market to take a loan with a lower interest rate than it should.
This issue is not a byproduct of the mechanism at all. Just makes it more likely, but is not the point of it. Please note that this issue would still exist WITHOUT the mechanism. Initially or at any time really, as long as assets are 0, when there are no deposits (there may be debt), the utilization rate will always be 0, so users may take any loan, as big as they want, for the lowest fee, while the protocol is close to insolvency.
Now, onto the fact that @santipu03 is trying to diminish the impact of this issue, which is issuing loans with the lowest interest rate possible when the protocol is close to insolvency. There will be times where the assets are 0, being one of these times the initial period (a fix for this was also mentioned in the report, in the recommendation).
During these times, it's important for the protocol to protect itself from borrowers and insolvency risk by increasing the utilization rate and corresponding fee when borrowing.
In a market with an adjust factor of 0.9, the protocol could have 1000 collateral and 900 debt, and still have an utilization rate of 0 and thus issue loans with the lowest interest rates possible!
I don't think it is acceptable at all for the protocol to be close to insolvency and issue loans with the lowest interest rate possible, it goes against the whole design of the interest rate model.
When the borrowed amounts are closed to the deposits, the protocol should protect itself by increasing interest rates and disincentivize borrows, which is clearly broken here.
Leave this up to the person deciding the escalations, I don't think any further clarification is needed.
santipu03
When the assets are zero, it's not possible to have debt on the market, this is enforced in borrow:
After this is clear, I only see this issue exploitable when a user is the first depositor on the market and it executes a deposit and a borrow in the same block. Because the utilization uses the average of deposited assets, it will result in zero, and this will cause interest rates to be lower than they should.
Now, because this issue will only be triggered on a new market, I don't think it's going to cause the claimed risk of insolvency.
0x73696d616f
After this is clear, I only see this issue exploitable when a user is the first depositor on the market and it executes a deposit and a borrow in the same block
Does not have to be the first depositor, the market may have 0 assets at any point.
Because the utilization uses the average of deposited assets, it will result in zero, and this will cause interest rates to be lower than they should.
It's not because they use the average, but due to incorrectly assuming assets == 0 means 0 utilization rate, which is obviously wrong and heavily favors borrowers, increasing the protocol's risk of insolvency.
Now, because this issue will only be triggered on a new market, I don't think it's going to cause the claimed risk of insolvency.
It does not matter if it is triggered on a new market or old market or whatever. As long as it allows you reaching a state with possibly 1000 collateral and 900 debt, and the interest rate is 0, the market is close to insolvency. This state is dangerous and can not be disregarded.
santipu03
Does not have to be the first depositor, the market may have 0 assets at any point.
Realistically, the market only has strictly 0 assets at the start, and never again, even if the pool is abandoned, there are usually some dust assets due to rounding.
It's not because they use the average, but due to incorrectly assuming assets == 0 means 0 utilization rate, which is obviously wrong and heavily favors borrowers, increasing the protocol's risk of insolvency.
Well, when assets are 0, the utilization rate should be also 0 because there cannot be any borrowed funds due to the check I showed you before. The root cause of this issue is that the protocol assumes that when the average of assets is 0, the utilization is 0, which cannot be true as demonstrated in your report.
Still, I admit that this issue can be exploitable because when a new market is deployed, a user can take a fixed loan with a minimum interest rate. Given the impact of this issue is partly similar to 67 but it can only be triggered once when the market is deployed, I see this issue as a borderline low/medium.
0x73696d616f
Realistically, the market only has strictly 0 assets at the start, and never again, even if the pool is abandoned, there are usually some dust assets due to rounding.
This may not always be the case, but the market may be left in the incorrect state regardless of it.
Well, when assets are 0, the utilization rate should be also 0 because there cannot be any borrowed funds due to the check I showed you before.
I can not agree with this part. 0/0 is not 0 and should never favour borrowers. 0/0 is an indetermination and should be resolved by favouring the protocol, hence not returning 0. As it currently returns 0, it will lead to an exploitable state where the protocol is at the highest utilization ratio (1000 collateral and 900 debt), but is giving out loans with the lowest interest possible.
The root cause of this issue is that the protocol assumes that when the average of assets is 0, the utilization is 0, which cannot be true as demonstrated in your report.
Not sure whay you mean but I proved that my claims are true, at least when the market is deployed.
Given the impact of this issue is partly similar to 67 but it can only be triggered once when the market is deployed, I see this issue as a borderline low/medium.
I think the impact is bigger. If this is exploited to take a big loan with very low interest at the beginning, the protocol will reach the state where it is closest to insolvency. I say this because #67 means the attacker may take a loan with some interest rate that is too low for its loan, but here it takes the loan with the lowest interest rate possible, guaranteed.
The likelihood / times of ocurrence may be lower but keep in mind that this exploit is very profitable for a borrower.
0x73696d616f
If I'm not mistaken, when the utilization is 0 (assets are 0), the interest rates should also be at the lowest point.
Just pointing out that your reason to close this issue is false as proven by the comments above. 0/0 is not 0 and should be the maximum in this case to protect the protocol, as you agree its important. Not the lowest. This is the core of this issue.
santipu03
I disagree with your argument that taking a loan with the lowest interest rate possible leads to insolvency. The trusted admins are in charge of setting the correct adjust factors for each token so that if the adjust factor is set at 0.9, it's assumed that it is safe to have 1000 collateral and 900 debt because the market is stable. Having a market with the highest utilization ratio doesn't mean that the market is close to insolvency, it just means that most of the funds are borrowed.
However, being able to take a loan with a lower interest rate than it should be I think it warrants a medium severity because lenders are being paid less than expected.
Then we have the likelihood of this issue occurring. As I pointed out before, the only possible scenario where this issue can be exploited is when the market is newly deployed and the first depositor makes a big deposit and a fixed borrow in the same block. Because of this issue, that user will take a fixed loan with the lowest interest rate possible because the average of floating assets will be 0 at that moment.
Taking all of this into account, I consider the impact to be medium and the likelihood to be extremely low, and that's why I think the overall severity of this issue is a borderline low/medium. The head of judging will have the final say.
0x73696d616f
I disagree with your argument that taking a loan with the lowest interest rate possible leads to insolvency. The trusted admins are in charge of setting the correct adjust factors for each token so that if the adjust factor is set at 0.9, it's assumed that it is safe to have 1000 collateral and 900 debt because the market is stable. Having a market with the highest utilization ratio doesn't mean that the market is close to insolvency, it just means that most of the funds are borrowed.
This is not the core part of this issue, but it is closer to insolvency anyway.
the likelihood to be extremely low
I completely disagree with this statement. The market always goes through deployment and the incorrect state, so the likelihood can not be low.
cvetanovv
Even if the assets are only zero when the market is deployed, I think this issue can be Medium severity.
I disagree that the likelihood is small. The likelihood is the same as a First depositor Inflation attack.
Here, the impact is lower, but it still allows a user to take advantage of a loan with a lower interest rate than it should be. This will lead to potential losses for lenders.
Planning to accept the escalation and make this issue a Medium severity.
santipu03
@cvetanovv
I agree that the attacker is able to take a fixed loan for a lower interest rate when the market is deployed. However, the only lender on the market at that point is the attacker so the liquidity taken for the loan is from the same attacker, meaning the only affected user of this exploit would be the attacker.
0x73696d616f
@santipu03 there can be several attackers. And the protocol would be in a high borrowing / utilization state while having the lowest interest rates on loans.
santipu03
This attack is only possible at the market deployment because after a few blocks have passed, the average of floating assets has gotten much closer to the deposited floating assets, so the utilization rate is updated and interest rates are correct. The only impact will be that a fixed loan will have a lower interest rate, but the liquidity used for that loan will be the attacker's liquidity.
From what I can see, the attacker will pay fewer fees for a loan but those fees should go directly to him, so, in the end, it's like self-griefing.
0x73696d616f
The only impact will be that a fixed loan will have a lower interest rate, but the liquidity used for that loan will be the attacker's liquidity.
There may be more and honestly it does not matter. What I am saying is, the high utilization ratio of the market is not justified by the current interest the loans are earning. The market is heavily utilized, but the current loans are at the lowest interest.
From what I can see, the attacker will pay fewer fees for a loan but those fees should go directly to him, so, in the end, it's like self-griefing.
The point of a loan in the perspective of a borrower is not earning fees. The loan he's got has the lowest interest rate, which is exactly what he wants.
etherSky111
Even if the assets are only zero when the market is deployed, I think this issue can be Medium severity.
I disagree that the likelihood is small. The likelihood is the same as a First depositor Inflation attack.
Here, the impact is lower, but it still allows a user to take advantage of a loan with a lower interest rate than it should be. This will lead to potential losses for lenders.
Planning to accept the escalation and make this issue a Medium severity.
Even if the assets are only zero when the market is deployed, I think this issue can be Medium severity.
I disagree that the likelihood is small. The likelihood is the same as a First depositor Inflation attack.
Here, the impact is lower, but it still allows a user to take advantage of a loan with a lower interest rate than it should be. This will lead to potential losses for lenders.
Planning to accept the escalation and make this issue a Medium severity.
@cvetanovv , what about this comment from sponsor? #176 (comment) Exactly team will be first depositor on every market.
I can't consider this comment a "Source of truth" because it was written after the audit ended.
Is it written somewhere in the documentation? If there is, this issue is invalid.
0x73696d616f
@etherSky111 the average assets will still be 0, the issue is valid regardless.
And the information from the sponsor was given after the end of the contest.
cvetanovv
I did not find any information before the audit that the protocol would mint and burn shares at every market deployment.
Remains my initial decision to accept the escalation
santipu03
If I'm not missing something, I believe this issue doesn't have any real impact.
As we've agreed before, the only impact this issue has is that when a market is deployed, an attacker can be its first depositor and take a fixed loan with a lower interest rate than usual. Usually, an impact like this (manipulating interest rates) is categorized as medium severity because it implies a loss of fees for lenders. However, at the time of the exploit, the only lender is the attacker so this issue impacts no other users.
In other words, the attacker is paying less fees for a loan, but those fees should go to him, so the potential damage only affects the attacker.
What do you think about this issue @santichez?
0x73696d616f
Your comment is completely false as the fees become unassigned and will later be coverted to floating assets, over time (or until someone deposits at maturity). Thus he is effectively stealing fees.
santichez
Even though the impact might seem low, the issue looks valid to me.
Exactly will be minting shares as the first deposit every time a new market is enabled, but I understand the fact that this wasn't explicitly commented on our docs.
santipu03
Your comment is completely false as the fees become unassigned and will later be coverted to floating assets, over time (or until someone deposits at maturity). Thus he is effectively stealing fees.
I agree with this statement, I'm sorry I missed that part.
However, I've found that the interest rate is actually lower when the average assets are higher than zero, thus confronting the root cause of this issue, which is that the first depositor will get a fixed loan with the lowest interest rate.
Solidity
function test_POC_BorrowAtMaturity_LowestRate() external {
uint256 assets = 10_000 ether;
uint256 borrow = 10_000 ether / 2;
ERC20 asset = market.asset();
deal(address(asset), ALICE, assets + 1);
uint256 maturity = FixedLib.INTERVAL * 2;
vm.startPrank(ALICE);
uint256 snapshot = vm.snapshot();
// First depositor gets a fixed loan with average assets being 0
market.deposit(assets, ALICE);
assertEq(market.previewFloatingAssetsAverage(), 0);
uint256 assetsOwed = market.borrowAtMaturity(maturity, borrow, type(uint256).max, ALICE, ALICE);
assertEq(assetsOwed - borrow, 76.712328767123285000e18); // Fixed interest is ~76e18
vm.revertTo(snapshot);
// First depositor gets a fixed loan with average assets being equal to the deposited assets
market.deposit(assets, ALICE);
skip(1 days);
assertEq(market.previewFloatingAssetsAverage(), assets);
assetsOwed = market.borrowAtMaturity(maturity, borrow, type(uint256).max, ALICE, ALICE);
assertEq(assetsOwed - borrow, 75.342465753424655000e18); // Fixed interest is ~75e18
}
@0x73696d616f Do you know how is this possible? I believe this may be a behavior of the IRM but I'm not sure.
0x73696d616f
I tested this in the POC of the issue, it leads to less debt.
The diff is in your poc, you're depositing, waiting and then borrowing. My poc above is waiting, depositing and borrowing.
0x73696d616f
It's because of block.timestamp being 1 days bigger.
assets.mulWadDown(fixedRate.mulDivDown(maturity - block.timestamp, 365 days));
It's a smaller loan duration.
santipu03
But in your original PoC, if you uncomment the line skip(1 days), the assets owed actually decrease, not increase. And the fee amounts are the same as in my POC, so the difference is also based on the shorter loan?
I tested this in the POC of the issue, it leads to less debt.
Sir, I'd like to believe this has been a mistake on your part because your PoC directly contradicts the claims you've been making for the last week on this issue.
0x73696d616f
Ok so the irm is mocked, so the rate is always the same.
Can confirm my original poc decreases, instead of increasing due to the loan duration. My mistake, the claims are still valid.
Change the irm from mocked to a real one and the fee calculation to maturity only and it works.
Solidity
function test_POC_BorrowAtMaturity_LowestRate_Test() external {
market.setInterestRateModel(
new InterestRateModel(
Parameters({
minRate: 3.5e16,
naturalRate: 8e16,
maxUtilization: 1.1e18,
naturalUtilization: 0.75e18,
growthSpeed: 1.1e18,
sigmoidSpeed: 2.5e18,
spreadFactor: 0.2e18,
maturitySpeed: 0.5e18,
timePreference: 0.01e18,
fixedAllocation: 0.6e18,
maxRate: 15_000e16
}),
Market(address(0))
)
);
uint256 assets = 10_000 ether;
uint256 borrow = 10_000 ether / 2;
ERC20 asset = market.asset();
deal(address(asset), ALICE, assets + 1);
uint256 maturity = FixedLib.INTERVAL * 2;
vm.startPrank(ALICE);
uint256 snapshot = vm.snapshot();
// First depositor gets a fixed loan with average assets being 0
market.deposit(assets, ALICE);
assertEq(market.previewFloatingAssetsAverage(), 0);
uint256 assetsOwed = market.borrowAtMaturity(maturity, borrow, type(uint256).max, ALICE, ALICE);
assertEq(assetsOwed - borrow, 26849315068493150000); // Fixed interest is ~76e18
vm.revertTo(snapshot);
// First depositor gets a fixed loan with average assets being equal to the deposited assets
market.deposit(assets, ALICE);
skip(1 days);
assertEq(market.previewFloatingAssetsAverage(), assets);
assetsOwed = market.borrowAtMaturity(maturity, borrow, type(uint256).max, ALICE, ALICE);
assertEq(assetsOwed - borrow, 32487218504212110000); // Fixed interest is ~75e18
}
Did you change the line in the market? fee = assets.mulWadDown(fixedRate.mulDivDown(maturity, 365 days));
0x73696d616f
I just ran it and it works.
santipu03
Do I have to change a line in the Market for the PoC to work?
0x73696d616f
yes, to simulate the duration of the loan being the same.
0x73696d616f
If it is bigger without changing the line, it is valid anyway.
0x73696d616f
Sir, I'd like to believe this has been a mistake on your part because your PoC directly contradicts the claims you've been making for the last week on this issue.
Btw this is false because even if the interest rate was the same/lower, it would be a bug of the irm. It makes no sense for the interest rate to be lower given a much higher utilization ratio, something would be off.
santipu03
After checking this last PoC, I agree that the bug is triggered and the attack is feasible, this issue warrants medium severity.
cvetanovv
After the discussion and the Lead Judge agreement, my decision remains to accept the escalation and make this issue a Medium severity.
The original report as published on Sherlock, by 0x73696d616f. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the protocol assumes that when average assets are zero, utilization is zero, but in reality it should be treated as maximum to protect the protocol.
Impact
A user could borrow a large amount at the lowest interest rate, potentially causing losses for lenders and putting the protocol at risk.
What the attacker needed
New market with zero average assets; attacker is first depositor; deposit and borrow in same block
The fix
The interest rate should be set to maximum when assets are zero, and initialization should set the factor to a past time so the first depositor increases utilization correctly.
How it was found
Deploy a new market where average floating assets are zero.
As the first depositor, deposit a large amount of assets.
In the same block, borrow a large amount at maturity.
Observe that the interest rate is the lowest possible due to zero utilization.
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.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
This is about a lending protocol where interest rates depend on how much of the pool is borrowed (utilization). If the pool's average assets are zero, the code treats utilization as zero, giving the lowest interest rate. A borrower could then take a big loan cheaply, which is unfair to lenders.
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.