Skip to content
Smart contracts Audit finding Intermediate

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

https://github.com/debtdao/Line-of-Credit/blob/e8aa08b44f6132a5ed901f8daa231700c5afeb3a/contracts/utils/EscrowLib.sol#L94-L96

https://github.com/debtdao/Line-of-Credit/blob/e8aa08b44f6132a5ed901f8daa231700c5afeb3a/contracts/utils/EscrowLib.sol#L75-L79

https://github.com/debtdao/Line-of-Credit/blob/e8aa08b44f6132a5ed901f8daa231700c5afeb3a/contracts/modules/credit/LineOfCredit.sol#L273-L280

https://github.com/debtdao/Line-of-Credit/blob/e8aa08b44f6132a5ed901f8daa231700c5afeb3a/contracts/modules/credit/LineOfCredit.sol#L487-L493

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

dmvt (judge) commented:

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


The original report as published on Code4rena, by __141345__. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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.

More by __141345__

View all

18 more disclosed reports, best first.

More from Debt DAO contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.