Skip to content

[HIGH] BytesMemory.unwrap misaligns the free-memory pointer, enabling overlapping allocations and tampering with previously returned bytes #258

@cygent-dev

Description

@cygent-dev

Security Finding

Field Value
Severity HIGH
Category logic_error
Repository 1inch/solidity-utils
Confidence 72%

Description

This logic_error stems from BytesMemory’s unwrap allocator violating Solidity’s 32-byte alignment convention, leaving previously returned bytes buffers vulnerable to overlap whenever slice lengths are not multiples of 32. The flaw is triggered when the function uses the global free-memory pointer, copies attacker-influenced slices, and immediately exposes the resulting bytes to downstream security-sensitive workflows such as calldata construction, signature parsing, or delegatecall preparation.

[Truncated - see PR for full details]

Location

  • BytesMemory.sol:L54-L69 in unwrap (BytesMemory)
  • BytesMemory.sol:L58-L66 in unwrap (BytesMemory)

Impact

Memory corruption lets adversaries forge calldata or corrupt verification data after it has been validated, enabling bypass of signature checks, rerouting of token/ETH transfers, arbitrary logic execution, and complete compromise of downstream contracts that rely on BytesMemory.unwrap in security-sensitive flows.

Recommendation

Advance the free-memory pointer by a 32-byte–padded payload length before storing it in slot 0x40, ensuring that every allocation is word-aligned and preventing overlapping writes. Adopt the following change (any equivalent computation of (length + 31) & ~31 is acceptable) and add targeted unit and fuzz tests that unwrap lengths from 0 to 64 bytes followed by subsequent allocations to confirm no overlap occurs.

[Truncated - see PR for full details]


Created by CARA Security Audit via Cygent

Metadata

Metadata

Assignees

No one assigned

    Labels

    cygent:highHigh severity security findingcygent:openSecurity finding - Open

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions