Описание
Vyper's bounds check on built-in slice() function can be overflowed
Summary
The bounds check for slices does not account for the ability for start + length to overflow when the values aren't literals.
If a slice() function uses a non-literal argument for the start or length variable, this creates the ability for an attacker to overflow the bounds check.
This issue can be used to do OOB access to storage, memory or calldata addresses. It can also be used to corrupt the length slot of the respective array.
A contract search was performed and no vulnerable contracts were found in production.
tracking in issue https://github.com/vyperlang/vyper/issues/3756. patched in https://github.com/vyperlang/vyper/pull/3818.
Details
Here the flow for storage is supposed, but it is generalizable also for the other locations.
When calling slice() on a storage value, there are compile time bounds checks if the start and length values are literals, but of course this cannot happen if they are passed values:
At runtime, we perform the following equivalent check, but the runtime check does not account for overflows:
The storage slice() function copies bytes directly from storage into memory and returns the memory value of the resulting slice. This means that, if a user is able to input the start or length value, they can force an overflow and access an unrelated storage slot.
In most cases, this will mean they have the ability to forcibly return 0 for the slice, even if this shouldn't be possible. In extreme cases, it will mean they can return another unrelated value from storage.
POC: OOB access
For simplicity, take the following Vyper contract, which takes an argument to determine where in a Bytes[64] bytestring should be sliced. It should only accept a value of zero, and should revert in all other cases.
We can use the following manual storage to demonstrate the vulnerability:
If we run the following test, passing max - 63 as the start value, we will overflow the bounds check, but access the storage slot at 1 + (2**256 - 63) / 32, which is what was set in the above storage layout:
The result is that we return the secret value from storage:
POC: length corruption
OOG exception doesn't have to be raised - because of the overflow, only a few bytes can be copied, but the length slot is set with the original input value.
The corruption of length can be then used to read dirty memory:
Impact
The built-in slice() method can be used for OOB accesses or the corruption of the length slot.
Ссылки
- https://github.com/vyperlang/vyper/security/advisories/GHSA-9x7f-gwxq-6f2c
- https://nvd.nist.gov/vuln/detail/CVE-2024-24561
- https://github.com/vyperlang/vyper/issues/3756
- https://github.com/pypa/advisory-database/tree/main/vulns/vyper/PYSEC-2024-149.yaml
- https://github.com/vyperlang/vyper/blob/b01cd686aa567b32498fefd76bd96b0597c6f099/vyper/builtins/functions.py#L404-L457
Пакеты
vyper
<= 0.3.10
0.4.0
Связанные уязвимости
Vyper is a pythonic Smart Contract Language for the ethereum virtual machine. In versions 0.3.10 and earlier, the bounds check for slices does not account for the ability for start + length to overflow when the values aren't literals. If a slice() function uses a non-literal argument for the start or length variable, this creates the ability for an attacker to overflow the bounds check. This issue can be used to do OOB access to storage, memory or calldata addresses. It can also be used to corrupt the length slot of the respective array.