AugAssign evaluation order causing OOB write within the object
The Vyper compiler's handling of augmented assignment statements (such as +=) caches the target location before evaluating the right-hand side, so if the right-hand side modifies the array (for example by popping an element), the final store can write to an index outside the array's current bounds. An attacker could exploit this to cause out-of-bounds writes or other incorrect behaviour in compiled contracts.
- Disclosed after under an hour
#38682 [SC-Medium] AugAssign evaluation order causing OOB write within the object
Submitted on Jan 10th 2025 at 01:15:10 UTC by @anatomist for Attackathon | Ethereum Protocol
- Report ID: #38682
- Report Type: Smart Contract
- Report severity: Medium
- Target: https://github.com/vyperlang/vyper
- Impacts:
- (Compiler) Incorrect bytecode generation leading to incorrect behavior
- (Compiler) Unexpected behavior
Description
Brief/Intro
AugAssign (e.g. +=, -=, ...) statements does not consider the side effect caused by rhs, allowing out-of-bounds index write within the array.
Vulnerability Details
Vyper parse AugAssign statements by first cache the target location to avoid double evaluation.
However, in the case when target is an access to a DynArray and the right value modify the array, the cached target will evaluate first, following the modifications like DynArray.pop(), and eventually the STORE operation could store the computed output in an index that falls outside the array’s current length bounds.
def parse_AugAssign(self):
target = self._get_target(self.stmt.target)
right = Expr.parse_value_expr(self.stmt.value, self.context)
if not target.typ._is_prim_word:
# because of this check, we do not need to check for
# make_setter references lhs<->rhs as in parse_Assign -
# single word load/stores are atomic.
raise TypeCheckFailure("unreachable")
with target.cache_when_complex("_loc") as (b, target):
left = IRnode.from_list(LOAD(target), typ=target.typ)
new_val = Expr.handle_binop(self.stmt.op, left, right, self.context)
return b.resolve(STORE(target, new_val))
This is alike this high severity issue where it discusses similar issues for Assign. However, it is not appropriate to apply the same mitigations in parse_Assign to parse_AugAssign, since the evaluation order of Assign is rhs -> lhs, while evaluation of AugAssign should be dereference lhs -> rhs -> assign to lhs, so assigning rhs to a temporary variable will actually change the evaluation order. We currently don't have a simple patch in mind, aside from reimplementing the entire AugAssign. Notably, care must be taken to not double evaluate lhs when fixing the bug.
Impact Details
The direct impact is out-of-bounds index access within the array. But wrong IR code emission could also lead to various consequences.
References
Proof of Concept
Proof of Concept
@deploy
def __init__():
a: DynArray[uint256, 2] = [1, 2]
a[1] += a.pop() # should revert here, but doesn't. rhs doesn't have to be pop, and can be other complex functions
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 compiler caches the target location before evaluating the right-hand side, so side effects from the right-hand side are not reflected in the cached location used for the final store.
Impact
The direct impact is out-of-bounds index access within the array, and wrong IR code emission could also lead to various consequences.
What the attacker needed
Compiler bug in Vyper; contract uses augmented assignment on array element; right-hand side modifies the same array
The fix
The compiler should be fixed by reimplementing AugAssign so that the left-hand side is dereferenced before the right-hand side is evaluated, without double-evaluating the left-hand side.
How it was found
- Deploy a contract with a dynamic array initialised to two elements.
- Execute an augmented assignment on the second element where the right-hand side pops an element from the same array.
- Observe that the operation does not revert as it should, and the store may target an out-of-bounds index.
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.
In a programming language, an augmented assignment like 'a[1] += x' means 'read a[1], add x, then write back to a[1]'. The compiler here reads the location of a[1] first, then runs the right-hand side, which can shrink the array. It then writes to the old location even though the array is now shorter, which is like writing past the end of a list.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.