NestedFactory.sol#_submitInOrders() Wrong implementation cause users to be overcharged
The order-submission function in the NestedFactory contract calculated fees and handled leftover input tokens incorrectly, so any unspent amount was treated as part of the fee rather than returned to the user. An attacker or ordinary user could therefore be overcharged whenever orders cost less than the supplied input amount.
Submitted by WatchPug
When executing orders, the actual amountSpent + feesAmount can be lower than _inputTokenAmount, the unspent amount should be returned to the user.
However, in the current implementation, the unspent amount will be taken as part of the fee. NestedFactory.sol L285-L309
function _submitInOrders(
uint256 _nftId,
IERC20 _inputToken,
uint256 _inputTokenAmount,
Order[] calldata _orders,
bool _reserved,
bool _fromReserve
) private returns (uint256 feesAmount, IERC20 tokenSold) {
_inputToken = _transferInputTokens(_nftId, _inputToken, _inputTokenAmount, _fromReserve);
uint256 amountSpent;
for (uint256 i = 0; i < _orders.length; i++) {
amountSpent += _submitOrder(address(_inputToken), _orders[i].token, _nftId, _orders[i], _reserved);
}
feesAmount = _calculateFees(_msgSender(), amountSpent);
assert(amountSpent <= _inputTokenAmount - feesAmount); // overspent
// If input is from the reserve, update the records
if (_fromReserve) {
_decreaseHoldingAmount(_nftId, address(_inputToken), _inputTokenAmount);
}
_handleUnderSpending(_inputTokenAmount - feesAmount, amountSpent, _inputToken);
tokenSold = _inputToken;
}
Recommendation
Change to:
function _submitInOrders(
uint256 _nftId,
IERC20 _inputToken,
uint256 _inputTokenAmount,
Order[] calldata _orders,
bool _reserved,
bool _fromReserve
) private returns (uint256 feesAmount, IERC20 tokenSold) {
_inputToken = _transferInputTokens(_nftId, _inputToken, _inputTokenAmount, _fromReserve);
uint256 amountSpent;
for (uint256 i = 0; i < _orders.length; i++) {
amountSpent += _submitOrder(address(_inputToken), _orders[i].token, _nftId, _orders[i], _reserved);
}
feesAmount = _calculateFees(_msgSender(), amountSpent);
assert(amountSpent <= _inputTokenAmount - feesAmount); // overspent
// If input is from the reserve, update the records
if (_fromReserve) {
_decreaseHoldingAmount(_nftId, address(_inputToken), amountSpent+feesAmount);
}
ExchangeHelpers.setMaxAllowance(_token, address(feeSplitter));
feeSplitter.sendFees(_token, feesAmount);
if (_inputTokenAmount > amountSpent + feesAmount) {
_inputToken.transfer(_fromReserve ? address(reserve) : _msgSender(), _inputTokenAmount - amountSpent - feesAmount);
}
tokenSold = _inputToken;
}
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 under-spend handling passed the full input amount minus fees rather than the actual amount spent plus fees, so the leftover was absorbed into the fee calculation instead of being refunded.
Impact
Users could be overcharged because unspent input tokens were not returned to them.
What the attacker needed
User submits orders that cost less than input
The fix
The recommendation is to decrease the reserve holding by the amount actually spent plus fees and to transfer any remaining input tokens back to the user or reserve.
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 you send tokens to a contract to buy something, the contract may not spend all of them. Any leftover should go back to you. Here the contract kept the leftover as an extra fee instead of returning it, so users paid more than they should.
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.