Skip to content
Business logic Audit finding Advanced

Incorrect implementation of Lender can result in lost tokens

The _sendFee function in MapleLoanInternals decodes the return data from an external call as an address without checking its length. If the called function returns bytes data, abi.decode interprets the length prefix (0x20) as the address, causing tokens to be sent to the wrong destination and lost.

cccz Follow

Originally disclosed on Code4rena

Submitted by cccz

MapleLoanInternals._sendFee should check returnData.length == 32 before decoding, otherwise if it returns bytes data, the abi.decode will return 0x20, result in lost tokens.

Proof of Concept

MapleLoanInternals.sol#L332-L344 This contract can test that when the function returns bytes data, abi.encode will decode the return value as 0x20.

pragma solidity 0.8.7;
contract A{
    address public destination;
    uint256 public number;
    function convertA() external{
        (bool su,bytes memory ret )= address(this).call(abi.encodeWithSelector(this.ret.selector));
        number = ret.length;
        destination = abi.decode(ret, (address));
    }
    function ret() public returns(bytes memory){
        return "0x74d754378a59Ab45d3E6CaC83f0b87E8E8719270";
    }
}

Recommended Mitigation Steps

function _sendFee(address lookup_, bytes4 selector_, uint256 amount_) internal returns (bool success_) {
    if (amount_ == uint256(0)) return true;

    ( bool success , bytes memory data ) = lookup_.call(abi.encodeWithSelector(selector_));

+       if (!success || data.length != uint256(32)) return false;

    address destination = abi.decode(data, (address));

    if (destination == address(0)) return false;

    return ERC20Helper.transfer(_fundsAsset, destination, amount_);
}

lucas-manuel (Maple Finance) confirmed, but disagreed with Medium severity and commented:

This technically is true, but I consider this informational as this is an extreme edge case. The poolDelegate and mapleTreasury values are almost guaranteed to never change, and if they did it would be to an audited Maple implementation which would never use bytes memory to return the address.

This is a cool finding and we will address, but it is informational.

JGcarv (Maple Finance) resolved:

Fix: Check data length (maple-labs/loan#154)

LSDan (judge) commented:

Disagree with sponsor on the rating. This has external requirements and assets are not at direct risk, but it could result in a loss of funds.


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

Impact

If the external contract returns bytes data instead of an address, tokens could be sent to an unintended address and lost; the sponsor considers this an extreme edge case and informational, but the judge notes it could result in a loss of funds.

What the attacker needed

The called contract returns bytes data; the called contract is not an audited Maple implementation

The fix

The fix was to add a check that the call succeeded and that the returned data length is exactly 32 bytes before decoding.

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.

When a smart contract calls another contract, it expects a certain type of answer. Here, the code assumed the answer would always be an address (a 20-byte value), but if the other contract returns a different type (like a bytes array), the code misreads the first part of that answer as an address. This could send tokens to the wrong place, and they would be lost.

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 cccz

View all

104 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.

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.