NFTBridge
60,000 USDC
View results
Submission Details
Severity: low
Invalid

Potential out-of-bounds memory access in `Cairo::cairoStringPack()` function

Summary:

The cairoStringPack() function in the Cairo library may access memory beyond the bounds of the input string when processing strings with lengths not divisible by CAIRO_STR_LEN.

Vulnerability details:

The function calculates dataLen as strBytes.length / CAIRO_STR_LEN, which truncates any remainder. It then uses this dataLen to process full 31-byte chunks, followed by a "pending word" for remaining bytes. However, for strings with lengths not divisible by 31, the offset used in the final assembly block could exceed the string's length:

uint256 dataLen = strBytes.length / CAIRO_STR_LEN;
// ... processing full chunks ...
assembly {
v := mload(add(strBytes, offset))
v := shr(mul(sub(32, pendingLen), 8),v)
}

Impact

This could lead to reading uninitialized memory, potentially causing incorrect packing of the string data.

Tools Used

Manual Review

Recommendation:

Adjust the logic to properly handle strings of any length. Calculate dataLen with rounding up, process one fewer full chunk in the main loop, and handle the last (potentially partial) chunk separately. For example:

uint256 dataLen = (strBytes.length + CAIRO_STR_LEN - 1) / CAIRO_STR_LEN;
// ... process (dataLen - 1) full chunks ...
// Handle last chunk separately, accounting for potential partial data

Also, ensure pendingLen is correctly calculated for all cases, including when the string length is a multiple of CAIRO_STR_LEN.

Updates

Lead Judging Commences

n0kto Lead Judge about 1 year ago
Submission Judgement Published
Invalidated
Reason: Too generic

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.