Missing check for equal length arrays in transferByOwnerBatch and mintByOwnerBatch
The batch transfer and batch mint functions in an ERC1155 contract iterate over the ids array without checking that the other arrays have the same length. If the ids array is shorter, the extra entries in the other arrays are silently ignored, so a caller could get a transfer or mint that does not match what they intended.
Source: https://github.com/sherlock-audit/2022-10-nftport-judging/issues/33
Found by
obront
Summary
The transferByOwnerBatch() and mintByOwnerBatch() functions in the ERC1155 implementation does not check whether the lengths of the arrays submitted are equal. This can lead to unexpected results.
Vulnerability Detail
In the transferByOwnerBatch() function, the user submits three arrays (addresses, ids, and amounts), while in the mintByOwnerBatch() function, the user submits four arrays (addresses, ids, amounts, uris).
The expectation is that the user submitting the function will ensure that the indexes of the arrays correspond to the correct values in the other arrays, and thus that the lengths will be the same.
Common practice in such a situation is to verify that the lengths are equal to ensure the user hasn't made an error. In other functions like burnBatch(), this verification is done by the underlying ERC1155 function.
However, in these two functions, we simply iterate through the ids array without performing this check, and then call out to the underlying ERC1155 _safeTransferFrom() or _mint() functions for each id separately.
Impact
If the ids array is a shorter length than the other arrays, the additional values in the other arrays will be ignored. This could lead to transfers with unexpected results, which would be better served by reverting.
Code Snippet
Tool used
Manual Review
Recommendation
Add a check to the transferByOwnerBatch() function that confirms that to, ids, and amounts are all equal length.
require(ids.length == to.length, "mismatched array lengths");
require(ids.length == amounts.length, "mismatched array lengths");
Add a check to the mintByOwnerBatch() function that confirms that to, ids, amounts, and uris are all equal length.
require(ids.length == to.length, "mismatched array lengths");
require(ids.length == amounts.length, "mismatched array lengths");
require(ids.length == uris.length, "mismatched array lengths");
Discussion
hyperspacebunny
Fixed in https://github.com/nftport/evm-minting-sherlock-fixes/pull/13
rayn731
Fixed, checks the arrays' length should be equal.
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 batch functions loop over the ids array without validating that the to, amounts and uris arrays have matching lengths, unlike the underlying ERC1155 batch functions.
Impact
A caller passing a shorter ids array could have transfers or mints executed with unexpected results rather than the call reverting; the report does not claim any theft or loss beyond this mismatch.
What the attacker needed
Ability to call the batch functions
The fix
The functions were fixed by adding checks that the to, ids, amounts and uris arrays are all equal in length.
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 batch function lets you act on many tokens at once by passing several lists side by side, where the first item of each list belongs together. The code only loops over the list of token ids, so if that list is shorter than the others, the leftover entries are quietly dropped instead of causing an error. The fix is to reject the call when the lists are not all the same length.
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.