Variable balance ERC20 support
The protocol stores the spot balance of deposited ERC20 tokens, which is inaccurate for tokens whose balances change over time (fee-on-transfer, rebasing, or variable-balance tokens). This can lead to incorrect collateral valuation and loss or locking of funds during deposit, repayment, or withdrawal.
__141345__ Follow with minhquanym, everyanykey and 14 others
Originally disclosed on Code4rena
Submitted by 141345, also found by minhquanym, everyanykey, pashov, rbserver, Ch_301, hansfriese, cccz, ladboy233, Jeiwan, codexploder, Ruhum, Bnke0x0, ayeslick, Lambda, aphak5010, and rvierdiiev
Impact
Some ERC20 may be tricky for the balance. Such as:
- fee on transfer (STA, USDT also has this mode)
- rebasing (aToken from AAVE)
- variable balance (stETH, balance could go up and down)
For these tokens, the balance can change over time, even without transfer()/transferFrom(). But current accounting stores the spot balance of the asset.
The impacts include:
-
the calculation of collateral value could be inaccurate
-
protocol could lose funds due to the deposit/repay amount being less than the actual transferred amount after fee
-
the amount user withdraw collateral when
_close()will be inaccurate- some users could lose funds due to under value
- some funds could be locked due to the balance inflation
- some funds might be locked due to the balance deflation
Proof of Concept
The spot new deposit amount is stored in the mapping self.deposited[token].amount and credit.deposit, and later used to calculate the collateral value and withdraw amount.
// Line-of-Credit/contracts/utils/EscrowLib.sol
function addCollateral(EscrowState storage self, address oracle, uint256 amount, address token) {
// ...
LineLib.receiveTokenOrETH(token, msg.sender, amount);
self.deposited[token].amount += amount;
// ...
}
function _getCollateralValue(EscrowState storage self, address oracle) public returns (uint256) {
// ...
d = self.deposited[token];
// ...
collateralValue += CreditLib.calculateValue(
o.getLatestAnswer(d.asset),
deposit,
d.assetDecimals
);
// ...
}
// Line-of-Credit/contracts/modules/credit/LineOfCredit.sol
function increaseCredit(bytes32 id, uint256 amount) {
// ...
Credit memory credit = credits[id];
credit = _accrue(credit, id);
credit.deposit += amount;
credits[id] = credit;
LineLib.receiveTokenOrETH(credit.token, credit.lender, amount);
// ...
}
function _close(Credit memory credit, bytes32 id) internal virtual returns (bool) {
// ...
if (credit.deposit + credit.interestRepaid > 0) {
LineLib.sendOutTokenOrETH(
credit.token,
credit.lender,
credit.deposit + credit.interestRepaid
);
}
However, if the balance changed later, the returned collateral value will be inaccurate. And the amount used when withdraw collateral in _close() is also wrong.
Recommended Mitigation Steps
- checking the before and after balance of token transfer
- recording the relative shares of each user instead of specific amount
- if necessary, call
ERC20(token).balanceOf()to confirm the balance - disallow such kind of tokens
This issue encompasses all ‘non-standard’ ERC20 tokens and their potential side effects within the system. Special mention for report #350, which adds a case this report fails to capture.
kibagateaux (Debt DAO) disputed
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
The protocol records the nominal transfer amount as the deposited balance instead of measuring the actual balance change or using shares, so tokens with variable or fee-on-transfer balances are misaccounted.
Impact
Collateral values may be inaccurate, and users could lose funds or have funds locked due to balance inflation or deflation; the report notes the sponsor disputed the issue.
What the attacker needed
Use of fee-on-transfer, rebasing, or variable-balance tokens
The fix
The protocol should check the before and after balance of token transfers, record relative shares instead of specific amounts, confirm balances with balanceOf when necessary, or disallow such tokens.
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.
Some digital tokens do not keep a fixed balance: the amount in an account can go up or down on its own, or a transfer may deduct a fee so the receiver gets less than sent. This protocol records the exact number it sees at deposit time and later assumes that number is still correct, which can make collateral values and withdrawal amounts wrong.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.