This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
ether_sky - When bad debts are cleared, there will be some untracked funds #130
Labels
Has Duplicates
A valid issue with 1+ other issues describing the same vulnerability
Medium
A valid Medium severity issue
Reward
A payout will be made for this issue
Sponsor Confirmed
The sponsor acknowledged this issue is valid
Will Fix
The sponsor confirmed this issue will be fixed
ether_sky
medium
When bad debts are cleared, there will be some untracked funds
Summary
In the
market
, allfunds
should be tracked accurately, whether they are currently held,borrowed
byborrowers
, or repaid in the future.To ensure this, the
market
has a sophisticated tracking system that functions effectively.However, when bad debts are cleared, there will be some untracked funds in the market.
Vulnerability Detail
Users have the option to deposit into the
market
directly or into specificfixed rate pools
.When
borrowers
borrow
funds from thefixed rate pool
, they are backed by thefixed deposits
first.If there is a shortfall in funds, the remaining
debt
is supported byfloating assets
.The movement of funds between
fixed borrowers
andfixed depositors
is straightforward outside of thetracking system
.The
tracking system
within themarket
primarily monitors funds within thevariable pool
itself.To simplify the scenario, let's assume there are no
fixed depositors
involved.First, there are
extraordinary earnings
, includingvariable backup fees
,late fixed repayment penalties
, etc.The
earnings accumulator
is responsible for collecting these earnings fromextraordinary
sources and subsequently distributing them gradually and smoothly.For this purpose, there is a
earningsAccumulator
variable.When users deposit funds into the
variable pool
, thefloatingAssets
increase by the deposited amounts as well as any additional earnings from theearnings accumulator
.Funds borrowed by
variable rate borrowers
are tracked using thefloatingDebt
variable, while funds borrowed byfixed rate borrowers
are tracked using thefloatingBackupBorrowed
variable.Additionally, there is an
unassignedEarnings
variable for eachmaturity pool
, which represents upcomingfees
fromborrowers
.These earnings are added to the
floatingAssets
whenever there are changes in themarket
, such asborrowers
repaying theirdebt
, depositors withdrawing their funds etc.While this variable is important, it is not directly involved in the
tracking system
.Let's describe the vulnerability.
A user deposits
5 DAI
into theDAI market
.When clearing the
bad debt
, the amount is deducted from theearnings accumulator
.For testing purpose,
ALICE
borrows funds at afixed rate
and repays them after maturity, and thepenalty fee
from this is added to theearnings accumulator
.Consequently, the
DAI market
has enoughearningsAccumulator
for clearing upcomingbad debt
in the test. (see below log)Now this user
borrows
1 DAI
from theDAI market
at a specificmaturity
.At this point, there is no
bad debt
in themarket
and the currenttracking values
are as follows:The current
DAI balance
is equal tofloatingAssets - floatingDebt - floatingBackupBorrowed + earningsAccumulator
.Everything is correct.
Now, consider
1 DAI
equals to5000 WETH
.Given sufficient
collateral
, this user canborrow
5000 WEHT
from theWETH market
.If the price of
DAI
drops to1000 WETH
, this user can beliquidated
.When
borrowers
borrow
fixed rate funds
, theprincipal
is backed byfloating assets
(assuming nofixed rate depositors
), and thefee
is added to theunassignedEarnings
of thatmaturity pool
.These
unassignedEarnings
are later added to thefloatingAssets
whenever changes occur in thepool
.However, when clearing
bad debt
, the sum ofprincipal
andfee
is deducted from theearningsAccumulator
if it's enough to cover thebad debt
.The
floatingBackupBorrowed
is reduced asprincipal
(means that these funds returns to thevariable pool
), but there is no provision for thefee
.In reality, the
fee
is reflected in theunassignedEarnings
of thatmaturity pool
, requiring an appropriate mechanism to update theseunassignedEarnings
.If this user is the last user of this
maturity pool
, there is no way to convert theseunassignedEarnings
to thetracking system
.Consequently, funds equal to the
unassignedEarnings
remain untracked and unused.Or if this user is not the last user of this
maturity pool
, these untrackedunassignedEarnings
can be allocated to latefixed depositors
.Below are tracking states in the
DAI market
afterliquidation
:The difference between the actual
DAI balance
andtracked balance
is equal to theunassignedEarnings
.Please add below test to the
Market.t.sol
.Impact
This vulnerability can happen under normal situation and there should be no untracked funds in the
market
.Nobody will detect these untracked funds and they won't be used.
Code Snippet
https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/8f6ef1b0868d3ea3a98a5ab7e8b3a164857681d7/protocol/contracts/Market.sol#L253
https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/8f6ef1b0868d3ea3a98a5ab7e8b3a164857681d7/protocol/contracts/Market.sol#L714
https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/8f6ef1b0868d3ea3a98a5ab7e8b3a164857681d7/protocol/contracts/Market.sol#L244-L245
https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/8f6ef1b0868d3ea3a98a5ab7e8b3a164857681d7/protocol/contracts/Market.sol#L652-L655
https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/8f6ef1b0868d3ea3a98a5ab7e8b3a164857681d7/protocol/contracts/Market.sol#L514
https://github.com/sherlock-audit/2024-04-interest-rate-model/blob/8f6ef1b0868d3ea3a98a5ab7e8b3a164857681d7/protocol/contracts/Market.sol#L299-L306
Tool used
Manual Review
Recommendation
Or we need more sophisticated solution.
The text was updated successfully, but these errors were encountered: