Skip to content
Business logic Audit finding Intermediate

Undesired behavior

The store function in NestedRecords.sol pushes a token address into an array without checking for duplicates, so if a token is first added with amount 0 and later with a larger amount, it appears twice and all processing uses the first (zero) occurrence. An attacker could potentially cause loss of funds by exploiting this duplicate entry, but the sponsor argues the current operators prevent zero-amount transfers, so the issue is not currently exploitable.

robee Follow

Originally disclosed on Code4rena

Submitted by robee, also found by 0x1f8b, csanuragjain, and Dravee

NestedRecords.sol#L117-L131

You push a parameter into an array of tokens without checking if it already exists. And, if at first it’s added with amount 0, it can later on be pushed with a greater amount and be twice in the array. Then in all processing it will consider the first occurrence and therefore the occurrence with amount 0.

     NestedRecords.store pushed the parameter _token

maximebrugel (Nested Finance) disagreed with High severity and commented:

Indeed, _amount is not checked and may result in the loss of funds for the user… If we only look at the store function.

However, this situation can’t happen because of the `NestedFactory’ (the only one able to call).

The Factory is calling with this private function :

/// @dev Transfer tokens to the reserve, and compute the amount received to store
/// in the records. We need to know the amount received in case of deflationary tokens.
/// @param _token The token to transfer (IERC20)
/// @param _amount The amount to send to the reserve
/// @param _nftId The Token ID to store the assets
function _transferToReserveAndStore(
    IERC20 _token,
    uint256 _amount,
    uint256 _nftId
) private {
    address reserveAddr = address(reserve);
    uint256 balanceReserveBefore = _token.balanceOf(reserveAddr);
    // Send output to reserve
    _token.safeTransfer(reserveAddr, _amount);
    uint256 balanceReserveAfter = _token.balanceOf(reserveAddr);
    nestedRecords.store(_nftId, address(_token), balanceReserveAfter - balanceReserveBefore, reserveAddr);
}

Here, the store amount parameter can be 0 if :

  • _amount is equal to 0. Then balanceReserveAfter - balanceReserveBefore = 0.
  • _amount is not equal to 0 but the safeTransfer function is transferring 0 tokens (100% fees, malicious contract,…).

We can’t consider the second option, It is an external cause and we are not able to manage the exotic behaviors of ERC20s. So, when the _amount parameter of this function can be equal to 0 ?

=> In submitOutOrders :

amountBought = _batchedOrders.outputToken.balanceOf(address(this));

(...)

amountBought = _batchedOrders.outputToken.balanceOf(address(this)) - amountBought;
feesAmount = amountBought / 100; // 1% Fee
if (_toReserve) {
    _transferToReserveAndStore(_batchedOrders.outputToken, amountBought - feesAmount, _nftId);
}

But the ZeroExOperator or FlatOperator will revert if the amount bought is 0.

=> In _submitOrder

(bool success, uint256[] memory amounts) = callOperator(_order, _inputToken, _outputToken);
require(success, "NF: OPERATOR_CALL_FAILED");
if (_toReserve) {
    _transferToReserveAndStore(IERC20(_outputToken), amounts[0], _nftId);
}

Same, the ZeroExOperator or FlatOperator will revert if the amount bought is 0.

In conclusion, we should check this parameter, but in the actual code state it can’t happen (without taking into account the exotic ERC20s that we do not manage). If we add an operator that does not check the amount bought it can happen, so, maybe reducing the severity ?

PR: Med/High Risk Fixes

harleythedog (judge) decreased severity to Medium and commented:

Agree with sponsor, this issue doesn’t exist with the current operators, so it is not currently a threat. I am going to downgrade this to medium.


The original report as published on Code4rena, by robee. 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 store function lacks a check to prevent duplicate token entries in the records array.

Impact

Could lead to loss of funds for the user if a token is stored twice with different amounts, but the sponsor states this cannot happen with the current operators because they revert on zero amounts, so it is not currently a threat.

What the attacker needed

A zero-amount token transfer must occur; an operator that allows zero-amount transfers

The fix

The code should check for duplicate token entries before pushing to the array, and a fix PR was referenced.

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.

This is about a smart contract that keeps a list of tokens for each user. The code adds a token to the list without first checking if it is already there. If a token is added once with zero amount and later with a real amount, the list has two entries for the same token, and the contract only looks at the first one (the zero). That could make the contract think the user has no tokens when they actually do, potentially losing funds.

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 robee

View all

6 more disclosed reports, best first.

More from Nested 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.