Skip to content
Smart contracts Audit finding Advanced

Processes refinance operations may call malicious code by re-created refinancer contract

A borrower could propose a refinancer contract that appears benign but can be destroyed and re-created at the same address with malicious code before the lender accepts the new terms. The lender's call to acceptNewTerms then delegatecalls into the malicious code, potentially causing devastating consequences for the loan.

rayn Follow

Originally disclosed on Code4rena

Submitted by rayn

When an attacker (borrower) proposes a new term, the attacker can let a lender accept the malicious term which the lender doesn’t expect.

It uses delegatecall in _acceptNewTerms of MapleLoanInternals.sol. Though a lender can manually check refinancer contract before calling acceptNewTerms, the attacker (borrower) can still re-create a malicious contract on same address before the lender is calling acceptNewTerms, and trigger malicious code by delegatecall in _acceptNewTerms.

Proof of Concept

In summary, an attacker can use CREATE2 to re-create a new malicious contract on same address. Here is CREATE2 exploit example: https://x9453.github.io/2020/01/04/Balsn-CTF-2019-Creativity/

  1. An attacker (borrower) first deploy a refinancer contract with normal refinance actions to cheat lenders. The refinancer have malicious constructor which can be hidden in inherited contracts.

  2. The attacker call proposeNewTerms, specifying a refinancer contract, and monitor acceptNewTerms in Mempool.

  3. When the attacker monitored a lender calls acceptNewTerms, then quickly pack these transactions:

    1. Destroy refinancer contract by calling selfdestruct
    2. Use CREATE2 to re-deploy a new refinancer contract with malicious code on same address
    3. The lender calls acceptNewTerms
  4. Then a lender will execute malicious code of new refinancer contract.

Tools Used

ethers.js

Recommended Mitigation Steps

Also check refinancer contract bytecodes in _getRefinanceCommitment:

function _getRefinanceCommitment(address refinancer_, uint256 deadline_, bytes[] calldata calls_) internal pure returns (bytes32 refinanceCommitment_) {
    return keccak256(abi.encode(refinancer_, deadline_, calls_, at(refinancer)));
}

function at(address _addr) public view returns (bytes memory o_code) {
    assembly {
        // retrieve the size of the code, this needs assembly
        let size := extcodesize(_addr)
        // allocate output byte array - this could also be done without assembly
        // by using o_code = new bytes(size)
        o_code := mload(0x40)
        // new "memory end" including padding
        mstore(0x40, add(o_code, and(add(add(size, 0x20), 0x1f), not(0x1f))))
        // store length in memory
        mstore(o_code, size)
        // actually retrieve the code, this needs assembly
        extcodecopy(_addr, add(o_code, 0x20), 0, size)
    }
}

lucas-manuel (Maple Finance) disputed and commented:

Refinancer contracts are vetted by the smart contracts team. Any custom refinancer that is used will be able to have devastating consequences on the Loan as it performs custom delegatecalls. For this reason the assumption is made that Borrowers and Lenders will only use audited immutable Refinancer contracts that are deployed by the Maple Labs smart contracts team, and if not, they must be diligent enough to audit themselves or must accept any consequences.

This is not a valid issue.

LSDan (judge) commented:

The report is valid. Given the potential danger involved, you should whitelist the allowable Refinancer contracts to protect your lenders. Issue stands.

lucas-manuel (Maple Finance) disagreed with High severity and commented:

We’re providing smart contracts team validated refinancers to the application for the lenders and borrowers to use. If they choose to use externally developed refinancers (something we want to support as it will allow for highly customizable logic between lenders and borrowers which is a feature not a bug), they must audit themselves.

If there is a selfdestruct contained in the contract, it can be immediately assumed to be malicious because of the exploit outlined above. Same goes for proxied refinancers and stateful refinancers.

I do not agree with this being a High Risk issue due to its very high degree of difficulty. The borrower would have to develop a smart contract with this exploit, submit it outside of the application, and convince the Pool Delegate to accept the refinancer address also outside of the application without an audit.

I think that this is an interesting finding and am not discounting it, but I highly disagree with severity.

LSDan (judge) decreased severity to Medium and commented:

I see your point. This exploit has external requirements and so should be medium, but I do think this represents a significant attack vector should a lender act accidentally against their own best interests.


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

Likely the use of delegatecall on a refinancer contract address that can be re-created via CREATE2 after selfdestruct, without verifying the contract's code or whitelisting trusted refinancers.

Impact

A lender could execute malicious code, which may have devastating consequences on the loan, but the exploit requires the lender to accept an externally developed refinancer without auditing it.

What the attacker needed

Borrower can propose a refinancer contract; lender accepts the proposed terms; refinancer contract contains selfdestruct; attacker can monitor mempool and front-run

The fix

The project should whitelist allowable refinancer contracts or check the refinancer contract bytecode in _getRefinanceCommitment to prevent address re-creation attacks.

How it was found

  1. Attacker deploys a refinancer contract with normal actions but a hidden malicious constructor.
  2. Attacker calls proposeNewTerms specifying that refinancer contract and monitors acceptNewTerms in the mempool.
  3. When a lender calls acceptNewTerms, attacker quickly destroys the refinancer contract via selfdestruct.
  4. Attacker uses CREATE2 to re-deploy a malicious refinancer contract at the same address.
  5. The lender's acceptNewTerms call then executes the malicious code via delegatecall.

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 borrower can propose a contract that the lender must approve. The borrower can secretly replace that contract with a malicious one just before the lender approves it, so the lender ends up running the malicious code. This is possible because the system uses a low-level call (delegatecall) that executes code from the proposed contract.

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 rayn

View all

12 more disclosed reports, best first.

More from Maple Finance 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.