Skip to content
Business logic Audit finding Intermediate

setLenderManager may cause some Lenders to lose their assets

The contract stores the lender manager's address in the bid's lender field when a loan NFT is claimed. If the owner later changes the lender manager, repayments for those loans are sent to the old lender manager address instead of the actual lender, causing loss of funds.

MiloTruck Follow with T1MOH, cccz and 5 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-03-teller-judging/issues/339

Found by

MiloTruck, T1MOH, cccz, dingo, duc, saidam017, shaka, yixxas

Summary

If the contract's lenderManager changes, repaid assets will be sent to the old lenderManager

Vulnerability Detail

setLenderManager is used to change the lenderManager address of the contract

Solidity
    function setLenderManager(address _lenderManager)
        external
        reinitializer(8)
        onlyOwner
    {
        _setLenderManager(_lenderManager);
    }

    function _setLenderManager(address _lenderManager)
        internal
        onlyInitializing
    {
        require(
            _lenderManager.isContract(),
            "LenderManager must be a contract"
        );
        lenderManager = ILenderManager(_lenderManager);
    }

claimLoanNFT will change the bid.lender to the current lenderManager

Solidity
    function claimLoanNFT(uint256 _bidId)
        external
        acceptedLoan(_bidId, "claimLoanNFT")
        whenNotPaused
    {
        // Retrieve bid
        Bid storage bid = bids[_bidId];

        address sender = _msgSenderForMarket(bid.marketplaceId);
        require(sender == bid.lender, "only lender can claim NFT");
        // mint an NFT with the lender manager
        lenderManager.registerLoan(_bidId, sender);
        // set lender address to the lender manager so we know to check the owner of the NFT for the true lender
        bid.lender = address(lenderManager);
    }

In getLoanLender, if the bid.lender is the current lenderManager, the owner of the NFT will be returned as the lender, and the repaid assets will be sent to the lender.

Solidity
    function getLoanLender(uint256 _bidId)
        public
        view
        returns (address lender_)
    {
        lender_ = bids[_bidId].lender;

        if (lender_ == address(lenderManager)) {
            return lenderManager.ownerOf(_bidId);
        }
    }
...
        address lender = getLoanLender(_bidId);

        // Send payment to the lender
        bid.loanDetails.lendingToken.safeTransferFrom(
            _msgSenderForMarket(bid.marketplaceId),
            lender,
            paymentAmount
        );

If setLenderManager is called to change the lenderManager, in getLoanLender, since the bid.lender is not the current lenderManager, the old lenderManager address will be returned as the lender, and the repaid assets will be sent to the old lenderManager, resulting in the loss of the lender's assets

Impact

It may cause some Lenders to lose their assets

Code Snippet

https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/TellerV2.sol#L212-L229 https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/TellerV2.sol#L560-L574 https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/TellerV2.sol#L1037-L1047 https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/TellerV2.sol#L744-L752

Tool used

Manual Review

Recommendation

Consider using MAGIC_NUMBER as bid.lender in claimLoanNFT and using that MAGIC_NUMBER in getLoanLender to do the comparison.

Diff
+  address MAGIC_NUMBER = 0x...;
    function claimLoanNFT(uint256 _bidId)
        external
        acceptedLoan(_bidId, "claimLoanNFT")
        whenNotPaused
    {
        // Retrieve bid
        Bid storage bid = bids[_bidId];

        address sender = _msgSenderForMarket(bid.marketplaceId);
        require(sender == bid.lender, "only lender can claim NFT");
        // mint an NFT with the lender manager
        lenderManager.registerLoan(_bidId, sender);
        // set lender address to the lender manager so we know to check the owner of the NFT for the true lender
-       bid.lender = address(lenderManager);
+       bid.lender = MAGIC_NUMBER;
    }
...
    function getLoanLender(uint256 _bidId)
        public
        view
        returns (address lender_)
    {
        lender_ = bids[_bidId].lender;

-       if (lender_ == address(lenderManager)) {
+       if (lender_ == MAGIC_NUMBER) {
            return lenderManager.ownerOf(_bidId);
        }
    }

Discussion

ethereumdegen

Github PR Issue 339 - Using magic number for lender manager control

IAm0x52

Fix looks good. Changed as recommended and is now using a magic number rather than the address of the lenderManager.

The original report as published on Sherlock, by MiloTruck. 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.

How it worked

Root cause

The contract uses the current lenderManager address as a marker in bid.lender, so changing the lenderManager breaks the link between the bid and the NFT owner.

Impact

Lenders whose loans have been converted to NFTs may lose their repaid assets if the lenderManager is changed.

What the attacker needed

A loan that has been claimed as an NFT; the contract owner changes the lenderManager

The fix

The issue was fixed by using a fixed magic number instead of the lenderManager address to mark NFT-backed loans.

How it was found

  1. A lender claims the loan NFT, which sets bid.lender to the current lenderManager address.
  2. The contract owner calls setLenderManager to change the lenderManager to a new address.
  3. When the borrower repays, getLoanLender sees that bid.lender is not the current lenderManager and returns the old lenderManager address as the lender.
  4. The repayment is sent to the old lenderManager instead of the actual lender, causing loss of assets.

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 lets lenders turn their loan into a transferable token (an NFT). To know who owns the loan, the contract checks if the recorded lender is a special manager contract. If the owner changes that manager contract, the check fails and repayments go to the old manager instead of the real lender. This means the lender can lose the money they are owed.

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.

More by MiloTruck

View all

35 more disclosed reports, best first.

More from Teller

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.