CVE-2024-22419: concat built-in can corrupt memory in vyper

Published Jan 18, 2024
·
Updated

Summary concat built-in can write over the bounds of the memory buffer that was allocated for it and thus overwrite existing valid data. The root cause is that the buildIR for concat doesn't properly adhere to the API of copy functions (for >=0.3.2 the copybytes function).

A contract search was performed and no vulnerable contracts were found in production.

Tracked in issue https://github.com/vyperlang/vyper/issues/3737

Details The buildIR allocates a new internal variable for the concatenation: https://github.com/vyperlang/vyper/blob/3b310d5292c4d1448e673d7b3adb223f9353260e/vyper/builtins/functions.py#L534-L550

Notice that the buffer is allocated for the maxlen + 1 word to actually hold the length of the array.

Later the copybytes function is used to copy the actual source arguments to the destination: https://github.com/vyperlang/vyper/blob/3b310d5292c4d1448e673d7b3adb223f9353260e/vyper/builtins/functions.py#L569-L572

The dstdata is defined via: - data ptr - to skip the 1 word that holds the length - offset - to skip the source arguments that were already written to the buffer - the offset is increased via: ["set", ofst, ["add", ofst, arglen]], ie it is increased by the length of the source argument

Now, the copybytes function has multiple control flow paths, the following ones are of interest: 1) https://github.com/vyperlang/vyper/blob/3b310d5292c4d1448e673d7b3adb223f9353260e/vyper/codegen/core.py#L270-L273 2) https://github.com/vyperlang/vyper/blob/3b310d5292c4d1448e673d7b3adb223f9353260e/vyper/codegen/core.py#L301-L320

Note that the function itself contains the following note: https://github.com/vyperlang/vyper/blob/3b310d5292c4d1448e673d7b3adb223f9353260e/vyper/codegen/core.py#L245-L247

That is we can ask for a copy of 1B yet a whole word is copied.

Consider the first interesting path - if the dstdata's distance to the end of the concat data buffer is < 32B, the copyop = STORE(dst, LOAD(src)) from copybytes will result in buffer overflow as it essentially will mstore to dstdata the mload of the source (mload will load whole word and the distance of the dstdata to the word boundary is <32B).

From the two mentioned paths in copybytes it can be seen that both sources from memory and storage can cause the corruption.

PoC The main attack vector that was found was when the concat is inside an internal function. Suppose we have an external function that calls internal one. In such case the address space is divided such that the memory for the internal function is in lower portion of the adr space. As such the buffer overflow can overwrite valid data of the caller.

Here is a simple example: python #@version ^0.3.9

@internal def bar() -> uint256: sss: String[2] = concat("a", "b") return 1

@external def foo() -> int256: a: int256 = -1 b: uint256 = self.bar() return a

foo should clearly return -1, but it returns 452312848583266388373324160190187140051835877600158453279131187530910662655

-1 was used intentionally due to its bit structure but the value here is fairly irelevant. In this example during the second iteration of the for loop in the buildIR mload to dst+1 will be executed (because len('a') == 1), thus the function will write 1B over the bounds of the buffer. The string 'b' is stored such that its right-most byte is a zero byte. So a zero byte will be written over the bounds. So when -1 is considered it's left-most B will be overwritten to all 0. Therefore it can be seen: 452312848583266388373324160190187140051835877600158453279131187530910662655 == (2248-1) will output True.

IR If we look at the contract's IR (vyper --no optimize -f ir), we see: Line 30 / a: int256 = -1 / [mstore, 320, -1 <-1>], And for the second iteration of the loop in concat: len, [mload, arg], [seq, [with, src, [add, arg, 32], [with, dst, [add, [add, 256 <concat destination>, 32], concatofst], [mstore, dst, [mload, src]]]], [set, concatofst, [add, concatofst, len]]]]], [mstore, 256 <concat destination>, concatofst], 256 <concat destination>]], So the address of the int is 320.

The dst is defined as: [add, [add, 256 <concat destination>, 32], concatofst],. In the second iteration the concatofst will be 1 because len('a)==1 so 256+32+1 = 289. Now this address will be mstored to - so the last mstored B will have the address 289+32=320 which clearly overlaps with the address of the int a.

PoC 2 Due to how immutables are handled, they can be corrupted too: python #@version ^0.3.9

i: immutable(int256)

@external def init(): i = -1 s: String[2] = concat("a", "b")

@external def foo() -> int256: return i

Output of calling foo() = 452312848583266388373324160190187140051835877600158453279131187530910662655.

Impact The buffer overflow can result in the change of semantics of the contract. The overflow is length-dependent and thus it might go unnoticed during contract testing.

However, certainly not all usages of concat will result in overwritten valid data as we require it to be in an internal function and close to the return statement where other memory allocations don't occur.

Concluding remarks The bug based on the fast path in copybytes was likely introduced in: 548d35d720fb6fd8efbdc0ce525bed259a73f0b9. git bisect was used between v0.3.1 and v0.3.2, forge test was run and the test asserted that the function indeed returns -1.

For the general case, 0.3.0 and 0.3.1 are also affected.

Other sources

Vyper is a Pythonic Smart Contract Language for the Ethereum Virtual Machine. The concat built-in can write over the bounds of the memory buffer that was allocated for it and thus overwrite existing valid data. The root cause is that the buildIR for concat doesn't properly adhere to the API of copy functions (for >=0.3.2 the copybytes function). A contract search was performed and no vulnerable contracts were found in production. The buffer overflow can result in the change of semantics of the contract. The overflow is length-dependent and thus it might go unnoticed during contract testing. However, certainly not all usages of concat will result in overwritten valid data as we require it to be in an internal function and close to the return statement where other memory allocations don't occur. This issue has been addressed in 0.4.0.

— NVD

Affected Software

2 affected componentsFixes available
pip/vyper>=0.3.0<=0.3.10
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.

    Fixed in 0.4.0

Event History

Jan 18, 2024
CVE Published
via MITRE·06:45 PM
Data Sourced
via MITRE·06:45 PM
DescriptionSeverityWeakness
Data Sourced
via NVD·07:15 PM
RemedyDescriptionSeverityWeaknessAffected Software
Jan 19, 2024
Advisory Published
via GitHub·04:19 PM

Frequently Asked Questions

1

What is the severity of CVE-2024-22419?

CVE-2024-22419 has been classified as having a medium severity due to the potential for memory corruption.

2

How do I fix CVE-2024-22419?

To mitigate CVE-2024-22419, upgrade to Vyper version 0.4.0 or later.

3

What versions of Vyper are affected by CVE-2024-22419?

CVE-2024-22419 affects Vyper versions from 0.3.0 to 0.3.10.

4

What types of vulnerabilities does CVE-2024-22419 represent?

CVE-2024-22419 represents a memory corruption vulnerability due to improper handling of buffer boundaries.

5

Is CVE-2024-22419 a coding issue in Vyper?

Yes, CVE-2024-22419 is caused by an issue in the 'build_IR' function which does not correctly follow the API for safe memory operations.

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