The getWhiteListedCollections function creates a mismatch between the reported length of a returned array and its actual memory allocation. This discrepancy is caused by improper use of inline assembly to modify the array's length without reallocating memory.
The function allocates a fixed-size memory array based on the total number of collections, but then uses inline assembly to modify only the length field of this array to match the number of whitelisted collections. This changes the reported length of the array without adjusting the actual memory allocation.
The function creates a memory array with a size equal to the total number of collections. Then populates this array only with whitelisted collections, tracking the count with an 'offset' variable. Using inline assembly, the function overwrites the array's length field with the 'offset' value. And it returns this modified array.
The result is an array that reports a length potentially smaller than its actual memory allocation.
Here's a practical example:
Suppose we have this simplified version of the function:
Now, let's see what's happening in memory:
Initial memory allocation:
After populating with 3 addresses:
After assembly code executes:
Now, when this function returns ret
, it's returning an array that:
Reports a length of 3
Actually has memory allocated for 5 elements
While the direct impact is mitigated because the function is not used elsewhere in the code, potential risks include:
Gas inefficiency: The function always allocates memory for the maximum possible size, which is wasteful if the actual number of whitelisted collections is smaller.
Maintenance risks: If the function is used in future updates or by external contracts, it could lead to: a) Exposure of uninitialized memory if accessed beyond the reported length. b) Conflicts with memory management if other operations assume the unused memory is free.
Manual review
The function can be refactored to allocate only the needed memory:
Please, do not suppose impacts, think about the real impact of the bug and check the CodeHawks documentation to confirm: https://docs.codehawks.com/hawks-auditors/how-to-determine-a-finding-validity A PoC always helps to understand the real impact possible.
Please, do not suppose impacts, think about the real impact of the bug and check the CodeHawks documentation to confirm: https://docs.codehawks.com/hawks-auditors/how-to-determine-a-finding-validity A PoC always helps to understand the real impact possible.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.