BytesM to Bytes conversion does not match the reference implementation
The Vyper compiler rejects a conversion from a fixed-size bytes type to a dynamic bytes type that the documented reference implementation says should be allowed. An attacker cannot exploit this; it is a compiler correctness issue that causes valid code to fail at compile time.
Originally disclosed on Immunefi
- Disclosed after under an hour
#38693 [SC-Insight] BytesM to Bytes conversion does not match the reference implementation
Submitted on Jan 10th 2025 at 08:24:12 UTC by @impermanentW for Attackathon | Ethereum Protocol
- Report ID: #38693
- Report Type: Smart Contract
- Report severity: Insight
- Target: https://github.com/vyperlang/vyper
- Impacts:
- (Compiler) Unexpected behavior
Description
Brief/Intro
According to the Vyper doc (see ref 1) a small Python reference implementation for conversion logic is maintained as part of Vyper’s test suite (see ref 2)
However behavior of the compiler does not match the reference when it comes to BytesM to Bytes[] conversion
Vulnerability Details
The reference specifies the BytesM_T -> Bytes_T conversion should be possible if the size of the output type is at least as big as the in type (see ref 3)
But the compiler does not implement that behavior correctly, this is done in vyper/builtins/_convert.py(see ref 4) which does not allow bytesM as an input type for to_bytes()
Impact Details
Conversions that should work according to the reference are throwing an error at compile time.
References
- https://docs.vyperlang.org/en/stable/types.html#type-conversions
- https://github.com/vyperlang/vyper/blob/v0.4.0/tests/functional/builtins/codegen/test\\_convert.py
- https://github.com/vyperlang/vyper/blob/e9db8d9f7486eae38f5b86531629019ad28f514e/tests/functional/builtins/codegen/test\\_convert.py#L83-L86
- https://github.com/vyperlang/vyper/blob/e9db8d9f7486eae38f5b86531629019ad28f514e/vyper/builtins/\\_convert.py#L447-L449
Proof of Concept
Proof of Concept
Compile the following code using Vyper v0.4.0
#pragma version ^0.4.0
def test(test: bytes1) -> Bytes[1]:
x: Bytes[1] = convert(test, Bytes[1])
return x
Fails with vyper.exceptions.TypeMismatch: Can't convert bytes1 to Bytes[1] even though both _bits_of_type() are equal so the conversion should be doable according to the reference
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the conversion builtin does not include fixed-size bytes types as allowed inputs for the dynamic bytes conversion, so the type check rejects them.
What the attacker needed
Use Vyper v0.4.0; write a conversion from bytesM to Bytes[N]
The fix
The compiler's conversion logic should be changed to accept fixed-size bytes types as input for the dynamic bytes conversion when the output size is large enough.
How it was found
- Write a Vyper function that converts a bytes1 value to Bytes[1] using convert.
- Compile the code with Vyper v0.4.0.
- Observe the compiler error saying the conversion is not allowed.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Vyper is a programming language for smart contracts. It has a documented rule that you can convert a fixed-size byte value into a variable-size byte value when the target is large enough. The compiler does not follow its own rule and stops with an error, so developers cannot compile code that should be valid.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.