CVE-2024-24560: Vyper external calls can overflow return data to return input buffer

Published Feb 2, 2024
·
Updated

Summary

When calls to external contracts are made, we write the input buffer starting at byte 28, and allocate the return buffer to start at byte 0 (overlapping with the input buffer). When checking RETURNDATASIZE for dynamic types, the size is compared only to the minimum allowed size for that type, and not to the returned value's length. As a result, malformed return data can cause the contract to mistake data from the input buffer for returndata.

This advisory is given a severity of "Low" because when the called contract returns invalid ABIv2 encoded data, the calling contract can read different invalid data (from the dirty buffer) than the called contract returned.

Details

When arguments are packed for an external call, we create a buffer of size max(args, returndata) + 32. The input buffer is placed in this buffer (starting at byte 28), and the return buffer is allocated to start at byte 0. The assumption is that we can reuse the memory becase we will not be able to read past RETURNDATASIZE.

python if fntype.returntype is not None: returnabit = calculatetypeforexternalreturn(fntype.returntype).abitype

# we use the same buffer for args and returndata, # so allocate enough space here for the returndata too. buflen = max(argsabit.sizebound(), returnabit.sizebound()) else: buflen = argsabit.sizebound()

buflen += 32 # padding for the method id

When data is returned, we unpack the return data by starting at byte 0. We check that RETURNDATASIZE is greater than the minimum allowed for the returned type: python if not callkwargs.skipcontractcheck: assertion = IRnode.fromlist( ["assert", ["ge", "returndatasize", minreturnsize]], errormsg="returndatasize too small", ) unpacker.append(assertion)

This check ensures that any dynamic types returned will have a size of at least 64. However, it does not verify that RETURNDATASIZE is as large as the length word of the dynamic type.

As a result, if a contract expects a dynamic type to be returned, and the part of the return data that is read as length includes a size that is larger than the actual RETURNDATASIZE, the return data read from the buffer will overrun the actual return data size and read from the input buffer.

Proof of Concept

This contract calls an external contract with two arguments. As the call is made, the buffer includes: - byte 28: methodid - byte 32: first argument (0) - byte 64: second argument (hash)

The return data buffer begins at byte 0, and will return the returned bytestring, up to a maximum length of 96 bytes.

python interface Zero: def sneaky(a: uint256, b: bytes32) -> Bytes[96]: view

@external def testsneaky(z: address) -> Bytes[96]: return Zero(z).sneaky(0, keccak256("oops")) On the other side, imagine a simple contract that does not, in fact, return a bytestring, but instead returns two uint256s. I've implemented it in Solidity for ease of use with Foundry: solidity function sneaky(uint a, bytes32 b) external pure returns (uint, uint) { return (32, 32); }

The return data will be parsed as a bytestring. The first 32 will point us to byte 32 to read the length. The second 32 will be perceived as the length. It will then read the next 32 bytes from the return data buffer, even though those weren't a part of the return data.

Since these bytes will come from byte 64, we can see above that the hash was placed there in the input buffer.

If we run the following Foundry test, we can see that this does in fact happen: solidity function testsneakyZeroReturn() public { ZeroReturn z = new ZeroReturn(); c = SuperContract(deployer.deploy("src/loose/", "retoverflow", "")); console.logBytes(c.testsneaky(address(z))); }

md Logs: 0xd54c03ccbc84dd6002c98c6df5a828e42272fc54b512ca20694392ca89c4d2c6

Patches Patched in https://github.com/vyperlang/vyper/pull/3925, https://github.com/vyperlang/vyper/pull/4091, https://github.com/vyperlang/vyper/pull/4144, https://github.com/vyperlang/vyper/pull/4060.

Impact

Malicious or mistaken contracts returning the malformed data can result in overrunning the returned data and reading return data from the input buffer.

Other sources

Vyper is a Pythonic Smart Contract Language for the Ethereum Virtual Machine. When calls to external contracts are made, we write the input buffer starting at byte 28, and allocate the return buffer to start at byte 0 (overlapping with the input buffer). When checking RETURNDATASIZE for dynamic types, the size is compared only to the minimum allowed size for that type, and not to the returned value's length. As a result, malformed return data can cause the contract to mistake data from the input buffer for returndata. When the called contract returns invalid ABIv2 encoded data, the calling contract can read different invalid data (from the dirty buffer) than the called contract returned.

— NVD

Affected Software

2 affected componentsFixes available
pip/vyper<0.4.0
0.4.0
vyperlang Vyper Python<=0.3.10

Remediation

Recommended actions to resolve this vulnerability, in priority order.

  1. Upgrade

    Upgrade pip/vyper to a version that resolves this vulnerability.

    Fixed in 0.4.0
  2. Upgrade

    Upgrade vyper to a version that resolves this vulnerability.

    Patch https://github.com/vyperlang/vyper/pull/3925
  3. Upgrade

    Upgrade vyper to a version that resolves this vulnerability.

    Patch https://github.com/vyperlang/vyper/pull/4060
  4. Upgrade

    Upgrade vyper to a version that resolves this vulnerability.

    Patch https://github.com/vyperlang/vyper/pull/4091
  5. Upgrade

    Upgrade vyper to a version that resolves this vulnerability.

    Patch https://github.com/vyperlang/vyper/pull/4144
  6. Configuration

    Apply the fix described for Vyper external calls: when handling dynamic return types, check `RETURNDATASIZE` is as large as required for the returned type’s `length`/size (the fix added an assertion like `['assert', ['ge', 'returndatasize', min_return_size]]`) so decoding does not read beyond the actual `RETURNDATASIZE` and therefore cannot overlap with dirty input-buffer memory.

    Vyper external call return-data decoding RETURNDATASIZE check for dynamic types (size comparison) = Compare RETURNDATASIZE against the minimum allowed size for the returned dynamic type, and ensure the returned dynamic type has sufficient size (not only minimum-allowed)

Event History

Feb 2, 2024
CVE Published
via MITRE·04:19 PM
Data Sourced
via MITRE·04:19 PM
DescriptionSeverityWeakness
Advisory Published
via GitHub·06:10 PM

Frequently Asked Questions

1

What is the severity of CVE-2024-24560?

The severity of CVE-2024-24560 is categorized as a high-risk vulnerability due to potential buffer overflow issues.

2

How do I fix CVE-2024-24560?

To fix CVE-2024-24560, upgrade to Vyper version 0.4.0 or later, which resolves the vulnerability.

3

What components are affected by CVE-2024-24560?

CVE-2024-24560 affects Vyper versions up to 0.3.10, specifically impacting function calls to external contracts.

4

What type of vulnerability is CVE-2024-24560?

CVE-2024-24560 is classified as a buffer overflow vulnerability that can lead to security breaches.

5

Is CVE-2024-24560 a remote exploit?

Yes, CVE-2024-24560 can be exploited remotely through vulnerable contract interactions.

Contact

SecAlerts Pty Ltd.
132 Wickham Terrace
Fortitude Valley,
QLD 4006, Australia
info@secalerts.co
By using SecAlerts services, you agree to our services end-user license agreement. This website is safeguarded by reCAPTCHA and governed by the Google Privacy Policy and Terms of Service. All names, logos, and brands of products are owned by their respective owners, and any usage of these names, logos, and brands for identification purposes only does not imply endorsement. If you possess any content that requires removal, please get in touch with us.
© 2026 SecAlerts Pty Ltd.
ABN: 70 645 966 203, ACN: 645 966 203