Chrome: Integer overflow in NewFixedDoubleArray VULNERABILITY DETAILS https://cs.chromium.org/chromium/src/v8/src/heap/factory.cc?rcl=dd689541d3815d64b4b39f6a41603248c71aa00e&l=496 Handle Factory::NewFixedDoubleArray(int length, PretenureFlag pretenure) { DCHECK_LE(0, length); if (length == 0) return empty_fixed_array(); if (length > FixedDoubleArray::kMaxLength) { // ***1*** isolate()->heap()->FatalProcessOutOfMemory(\"invalid array length\"); } int size = FixedDoubleArray::SizeFor(length); // ***2*** Map map = *fixed_double_array_map(); HeapObject result = AllocateRawWithImmortalMap(size, pretenure, map, kDoubleAligned); Handle array(FixedDoubleArray::cast(result), isolate()); array->set_length(length); return array; } https://cs.chromium.org/chromium/src/v8/src/builtins/builtins-array.cc?rcl=933508f981a984b7868d70c3adb781783e5aa32d&l=1183 Object Slow_ArrayConcat(BuiltinArguments* args, Handle species, Isolate* isolate) { [...] uint32_t estimate_result_length = 0; uint32_t estimate_nof = 0; FOR_WITH_HANDLE_SCOPE(isolate, int, i = 0, i, i < argument_count, i++, { Handle obj = args->at(i); uint32_t length_estimate; uint32_t element_estimate; if (obj->IsJSArray()) { Handle array(Handle::cast(obj)); length_estimate = static_cast(array->length()->Number()); if (length_estimate != 0) { ElementsKind array_kind = GetPackedElementsKind(array->GetElementsKind()); kind = GetMoreGeneralElementsKind(kind, array_kind); } element_estimate = EstimateElementCount(isolate, array); } else { [...] } // Avoid overflows by capping at kMaxElementCount. if (JSObject::kMaxElementCount - estimate_result_length < length_estimate) { // ***3*** estimate_result_length = JSObject::kMaxElementCount; } else { estimate_result_length += length_estimate; } if (JSObject::kMaxElementCount - estimate_nof < element_estimate) { estimate_nof = JSObject::kMaxElementCount; } else { estimate_nof += element_estimate; } }); // If estimated number of elements is more than half of length, a // fixed array (fast case) is more time and space-efficient than a // dictionary. bool fast_case = is_array_species && (estimate_nof * 2) >= estimate_result_length && isolate->IsIsConcatSpreadableLookupChainIntact(); // ***4*** if (fast_case && kind == PACKED_DOUBLE_ELEMENTS) { Handle storage = isolate->factory()->NewFixedDoubleArray(estimate_result_length); // ***5*** [...] https://cs.chromium.org/chromium/src/v8/src/builtins/builtins-array.cc?rcl=9ea32aab5b494eaaf27ced51a6608e8400a3c4e5&l=1378 MaybeHandle Fast_ArrayConcat(Isolate* isolate, BuiltinArguments* args) { [...] int result_len = 0; { DisallowHeapAllocation no_gc; // Iterate through all the arguments performing checks // and calculating total length. for (int i = 0; i < n_arguments; i++) { Object arg = (*args)[i]; if (!arg->IsJSArray()) return MaybeHandle(); if (!HasOnlySimpleReceiverElements(isolate, JSObject::cast(arg))) { return MaybeHandle(); } // TODO(cbruni): support fast concatenation of DICTIONARY_ELEMENTS. if (!JSObject::cast(arg)->HasFastElements()) { return MaybeHandle(); } Handle array(JSArray::cast(arg), isolate); if (!IsSimpleArray(isolate, array)) { // ***6*** return MaybeHandle(); } // The Array length is guaranted to be <= kHalfOfMaxInt thus we won't // overflow. result_len += Smi::ToInt(array->length()); DCHECK_GE(result_len, 0); // Throw an Error if we overflow the FixedArray limits if (FixedDoubleArray::kMaxLength < result_len || /// ***7*** FixedArray::kMaxLength < result_len) { AllowHeapAllocation gc; THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kInvalidArrayLength), JSArray); } } } return ElementsAccessor::Concat(isolate, args, n_arguments, result_len); } https://cs.chromium.org/chromium/src/v8/src/builtins/builtins-array.cc?rcl=9ea32aab5b494eaaf27ced51a6608e8400a3c4e5&l=244 BUILTIN(ArrayPrototypeFill) { [...] // 2. Let len be ? ToLength(? Get(O, \"length\")). double length; MAYBE_ASSIGN_RETURN_FAILURE_ON_EXCEPTION( isolate, length, GetLengthProperty(isolate, receiver)); // ***8*** // 3. Let relativeStart be ? ToInteger(start). // 4. If relativeStart < 0, let k be max((len + relativeStart), 0); // else let k be min(relativeStart, len). Handle start = args.atOrUndefined(isolate, 2); double start_index; MAYBE_ASSIGN_RETURN_FAILURE_ON_EXCEPTION( isolate, start_index, GetRelativeIndex(isolate, length, start, 0)); // ***9*** // 5. If end is undefined, let relativeEnd be len; // else let relativeEnd be ? ToInteger(end). // 6. If relativeEnd < 0, let final be max((len + relativeEnd), 0); // else let final be min(relativeEnd, len). Handle end = args.atOrUndefined(isolate, 3); double end_index; MAYBE_ASSIGN_RETURN_FAILURE_ON_EXCEPTION( isolate, end_index, GetRelativeIndex(isolate, length, end, length)); [...] if (TryFastArrayFill(isolate, &args, receiver, value, start_index, end_index)) { https://cs.chromium.org/chromium/src/v8/src/elements.cc?rcl=d8b0d88de4b7d73ea02abb8511c146944d6ccf67&l=2244 static Object FillImpl(Handle receiver, Handle obj_value, uint32_t start, uint32_t end) { // Ensure indexes are within array bounds DCHECK_LE(0, start); DCHECK_LE(start, end); // Make sure COW arrays are copied. if (IsSmiOrObjectElementsKind(Subclass::kind())) { JSObject::EnsureWritableFastElements(receiver); } // Make sure we have enough space. uint32_t capacity = Subclass::GetCapacityImpl(*receiver, receiver->elements()); if (end > capacity) { Subclass::GrowCapacityAndConvertImpl(receiver, end); // ***10*** CHECK_EQ(Subclass::kind(), receiver->GetElementsKind()); } |NewFixedDoubleArray| doesn't expect the |length| argument to be negative (there's even a DCHECK for that), as it would pass the maximum length check[1] and cause an integer overflow when computing the size of the backing store[2]. The undersized backing store then might be used for out-of-bounds access. It turns out there are at least two methods that allow passing negative values to |NewFixedDoubleArray|. 1. Concat The implementation of |Array.prototype.concat| in V8 has quite a few fast code paths that deal with different kinds of arguments. The structure roughly looks like: +------------------+ | | +--------------> Fast_ArrayConcat | | | | | +------------------+ *********************** +-------------+ * * | | +----------------> packed double array * | ArrayConcat | | * * | | | *********************** +-------------+ | | +------------------+ +---------------------+ | | | | | +--------------> Slow_ArrayConcat | +------------------> fixed array storage | | | | | | +------------------+ | +---------------------+ | | | +---------------------+ +---------------------+ | | | | | +----------------> ArrayConcatVisitor +-------> dictionary storage | | | | | +---------------------+ +---------------------+ | | +---------------------+ | | | +------------------> JSReceiver storage | | | +---------------------+ The relevant code path for this issue is the packed double array case inside |Slow_ArrayConcat|. The method uses an unsigned variable for computing the result array length and caps it at |kMaxElementCount|[3], i.e., at 0xffffffff. Then the value of the variable gets converted to a *signed* type and passed to |NewFixedDoubleArray|[5] provided that the |fast_case| condition is satisfied[4], and the estimated array type is PACKED_DOUBLE. Thus, any value in the range [0x80000000, 0xffffffff] could pass the length check and trigger the overflow. That still means an attacker has to make the method iterate through more than two billion array elements, which might seem implausible; actually, the whole process takes just a couple of seconds on a modern machine and has moderate memory requirements because multiple arguments can refer to the same array. Also, |ArrayConcat| calls |Fast_ArrayConcat| in the beginning, and the fast method has a more strict length check, which might throw an error when the result length is more than |FixedDoubleArray:: kMaxLength|[7]. So, the attacker has to make |Fast_ArrayConcat| return early without triggering the error. The easiest way to achieve that is to define an additional property on the array. REPRODUCTION CASE: 2. Fill The bug in |concat| allows writing data beyond the bounds of an array, but it's difficult to limit the size of the OOB data to a sane value, which makes the exploitation primitive less useful. So, I've spent some time looking for variants of the issue, and found one in |Array.prototype.fill|. |ArrayPrototypeFill| initially obtains the length of an array[8] and uses that value to limit the |start| and |end| arguments. However, a later call to |GetRelativeIndex|[9] might trigger a user-defined JS function, which could modify the length. Usually, that's enough to cause OOB access, so |FastElementsAccessor::FillImpl| double-checks that the capacity of the array is not less than |end| and might call |GrowCapacityAndConvertImpl|[10], which in turn might call |NewFixedDoubleArray|. The issue here is that there's no check that |end| is small enough to fit in a signed type; therefore the same overflow leading to the allocation of an undersized backing store could occur. REPRODUCTION CASE: Exploitation: Unlike |concat|, |fill| conveniently allows limiting the size of the OOB block by modifying the |start| argument. The exploit forces the method to return an array whose length value is bigger than the actual size of the backing store, which is essentially a ready-to-use OOB read/write exploitation primitive. The rest is just copied from https://crbug.com/931640. VERSION Google Chrome 72.0.3626.121 (Official Build) (64-bit) Google Chrome 74.0.3725.0 (Official Build) canary I'd recommend changing |NewFixedDoubleArray| so it throws an OOM error on negative values, the same way as the similar |AllocateRawFixedArray| function currently does. This bug is subject to a 90 day disclosure deadline. After 90 days elapse or a patch has been made broadly available (whichever is earlier), the bug report will become visible to the public. Found by: glazunov@google.com