The Cairo
library in the provided smart contract includes several functions for serializing and deserializing data types used in Cairo. However, the cairoStringUnpack
function is vulnerable to incorrect string length handling, which can lead to buffer overflow issues and incorrect deserialization of strings.
The cairoStringUnpack
function in the Cairo library is vulnerable to insufficient buffer validation, leading to potential buffer overflow issues and incorrect deserialization of strings. This vulnerability could compromise the integrity of the NFT transfer process and potentially cause NFT loss and bridge instability.
The vulnerability exists in the cairoStringUnpack
function, which extracts a string from a buffer by concatenating parts of the buffer into a string. The issues are:
The function does not properly handle cases where the buffer contains incomplete or malformed data.
The function assumes that buffer length information is accurate, lacking sufficient bounds checking.
If the length information is incorrect or if the buffer is truncated, it may lead to buffer overflows or incorrect data handling.
Buffer Overflow: The function may read beyond allocated memory if the buffer contains fewer bytes than expected.
Incorrect Data Handling: The function may produce incorrect strings if the buffer is not properly formatted or if the data length is misreported.
Initial Setup: Bob initiates an NFT transfer from Ethereum (L1) to Starknet (L2).
Execution: The cairoStringUnpack
function is called to deserialize the message on Starknet (L2).
Vulnerability Exploitation: If the message is malformed or shorter than expected, the function may read beyond the allocated memory, leading to memory corruption or unexpected behavior.
Bob: May experience failed or incorrect NFT transfers, leading to a negative user experience.
Alice: Might receive incorrect NFT data, potentially affecting asset ownership and trust in the NFT Bridge service.
The test case fails with errors when handling malformed buffers, confirming that the function does not adequately handle buffer overflows and incorrect data.
Solidity: Language for implementing the Cairo library.
Forge: Testing framework used to create the test cases.
Manual Analysis: Additional examination of code and behavior.
Add Length Checks: Implement checks to ensure buffer length is sufficient to prevent reading beyond its bounds.
Improved Error Handling: Include additional checks to catch cases where the buffer index might go out of bounds.
Use Assertions for Validation: Utilize assert
for internal logic checks to ensure correctness during development.
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.