Skip to content

Commit 4c697df

Browse files
committed
[1.3>1.4] [1.2>1.3] [MERGE #2230 @boingoing] Change to address CVE-2016-7287,CVE-2016-7286,CVE-2016-7288,CVE-2016-7296
Merge pull request #2230 from boingoing:1612_1.2_stage Fix for SpreadArgs overrun The array's length gets changed when we do the get-item this will overrun already allocated buffer. Fixed that by creating a local initialized that length. Use-after-free in TypedArray.sort %TypedArray%.prototype.sort has a helper method which performs the compare between two elements of the array. This helper takes an optional compare function callback, which is user code, and executes it (if present) to compute the sort order in a user-defined way. We call ToNumber on the return value from the compare function callback. The issue here is that this compare function can return an object which executes user code in the ToNumber conversion (via valueOf). This valueOf function is user code which can detach the buffer in the TypedArray. We check to see if the buffer has been detached after calling the compare function but we don't check again after calling ToNumber. If the valueOf call detaches the buffer, our sort function will re-order elements in the buffer's memory after it was detached and potentially free'd. Fix is to detect the buffer detach after calling ToNumber and throw out of the helper to make sure we don't shuffle elements in the detached memory. Uninitialized Memory in SIMD.toLocaleString JavascriptSIMDObject::ToLocaleString explicitly handles the number of arguments it copies into a temporary array before passing this array to a toLocaleString helper. We have a check for 1, 2, or 3 arguments but calling the function with more than 3 arguments causes it to skip copying any of the incoming arguments into the temporary array. This leads to the toLocaleString helper using uninitialized values in the temporary array since we access this array using the original argument count. There's also another bug here which is that the toLocaleString helper can throw an exception which leaks the temporary array memory since it was allocated on the heap and is not free'd when this helper throws. Fix both issues by allocating the temporary array on the stack with a fixed size of 3, clamping the number of incoming arguments to 3, and removing the explicit checks for the number of incoming arguments. Now we insert into the temporary array the two optional arguments if our original set of args includes those optional arguments. Uninitialized Memory in SIMD.Load All of the EntryLoad variants in the SIMD Javascript library directly access elements in the arguments array regardless of how many arguments are actually in the array. If user code calls this API with no arguments we will end up loading values which are unknown and potentially uninitiailized. Simple fix is to check the count of arguments passed-in to the function and use undefined for missing arguments. Type Confusion in Internationalization Initialization IntlEngineInterfaceExtensionObject::deletePrototypePropertyHelper loads a property from an object and uses it as if it is a DynamicObject even if the property isn't an object. We can run into two different problems here. First, we can end up using an uninitialized stack var if the object we're loading the property from doesn't have this property. Second, we can treat a non-object as an object leading to reading from arbitrary memory addresses.
2 parents 0067111 + cb0b058 commit 4c697df

12 files changed

+414
-55
lines changed

lib/Runtime/Library/IntlEngineInterfaceExtensionObject.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -297,35 +297,46 @@ namespace Js
297297

298298
void IntlEngineInterfaceExtensionObject::deletePrototypePropertyHelper(ScriptContext* scriptContext, DynamicObject* intlObject, Js::PropertyId objectPropertyId, Js::PropertyId getterFunctionId)
299299
{
300-
DynamicObject *prototypeVal = nullptr;
300+
DynamicObject *prototypeObject = nullptr;
301301
DynamicObject *functionObj = nullptr;
302-
Var propertyValue;
303-
Var getter;
304-
Var setter;
302+
Var propertyValue = nullptr;
303+
Var prototypeValue = nullptr;
304+
Var resolvedOptionsValue = nullptr;
305+
Var getter = nullptr;
306+
Var setter = nullptr;
305307

306-
if (!Js::JavascriptOperators::GetProperty(intlObject, objectPropertyId, &propertyValue, scriptContext))
308+
if (!JavascriptOperators::GetProperty(intlObject, objectPropertyId, &propertyValue, scriptContext) ||
309+
!JavascriptOperators::IsObject(propertyValue))
307310
{
308-
AssertMsg(false, "Error.");
309311
return;
310312
}
311313

312-
if (!Js::JavascriptOperators::GetProperty(DynamicObject::FromVar(propertyValue), Js::PropertyIds::prototype, &propertyValue, scriptContext))
314+
if (!JavascriptOperators::GetProperty(DynamicObject::FromVar(propertyValue), Js::PropertyIds::prototype, &prototypeValue, scriptContext) ||
315+
!JavascriptOperators::IsObject(prototypeValue))
313316
{
314-
AssertMsg(false, "Can't be null, otherwise Intl library wasn't initialized correctly");
315317
return;
316318
}
317319

318-
if (!Js::JavascriptOperators::GetProperty(prototypeVal = DynamicObject::FromVar(propertyValue), Js::PropertyIds::resolvedOptions, &propertyValue, scriptContext))
320+
prototypeObject = DynamicObject::FromVar(prototypeValue);
321+
322+
if (!JavascriptOperators::GetProperty(prototypeObject, Js::PropertyIds::resolvedOptions, &resolvedOptionsValue, scriptContext) ||
323+
!JavascriptOperators::IsObject(resolvedOptionsValue))
319324
{
320-
AssertMsg(false, "If these operations result in false, Intl tests will detect them");
321325
return;
322326
}
323327

324-
(functionObj = DynamicObject::FromVar(propertyValue))->SetConfigurable(Js::PropertyIds::prototype, true);
328+
functionObj = DynamicObject::FromVar(resolvedOptionsValue);
329+
functionObj->SetConfigurable(Js::PropertyIds::prototype, true);
325330
functionObj->DeleteProperty(Js::PropertyIds::prototype, Js::PropertyOperationFlags::PropertyOperation_None);
326331

327-
JavascriptOperators::GetOwnAccessors(prototypeVal, getterFunctionId, &getter, &setter, scriptContext);
328-
(functionObj = DynamicObject::FromVar(getter))->SetConfigurable(Js::PropertyIds::prototype, true);
332+
if (!JavascriptOperators::GetOwnAccessors(prototypeObject, getterFunctionId, &getter, &setter, scriptContext) ||
333+
!JavascriptOperators::IsObject(getter))
334+
{
335+
return;
336+
}
337+
338+
functionObj = DynamicObject::FromVar(getter);
339+
functionObj->SetConfigurable(Js::PropertyIds::prototype, true);
329340
functionObj->DeleteProperty(Js::PropertyIds::prototype, Js::PropertyOperationFlags::PropertyOperation_None);
330341
}
331342

lib/Runtime/Library/JavascriptFunction.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,15 +1027,16 @@ namespace Js
10271027

10281028
if (arr != nullptr && !arr->IsCrossSiteObject())
10291029
{
1030+
uint32 length = arr->GetLength();
10301031
// CONSIDER: Optimize by creating a JavascriptArray routine which allows
10311032
// memcpy-like semantics in optimal situations (no gaps, etc.)
1032-
if (argsIndex + arr->GetLength() > destArgs.Info.Count)
1033+
if (argsIndex + length > destArgs.Info.Count)
10331034
{
10341035
AssertMsg(false, "The array length has changed since we allocated the destArgs buffer?");
10351036
Throw::FatalInternalError();
10361037
}
10371038

1038-
for (uint32 j = 0; j < arr->GetLength(); j++)
1039+
for (uint32 j = 0; j < length; j++)
10391040
{
10401041
Var element;
10411042
if (!arr->DirectGetItemAtFull(j, &element))

lib/Runtime/Library/JavascriptSimdObject.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ namespace Js
147147
}
148148

149149
template <typename T, size_t N>
150-
Var JavascriptSIMDObject::ToLocaleString(const Var* args, uint numArgs, const char16 *typeString, const T (&laneValues)[N],
150+
Var JavascriptSIMDObject::ToLocaleString(const Var* args, uint numArgs, const char16 *typeString, const T(&laneValues)[N],
151151
CallInfo* callInfo, ScriptContext* scriptContext) const
152152
{
153153
Assert(args);
@@ -159,23 +159,26 @@ namespace Js
159159
return ToString(scriptContext); //Boolean types does not have toLocaleString.
160160
}
161161

162+
// Clamp to the first 3 arguments - we'll ignore more.
163+
if (numArgs > 3)
164+
{
165+
numArgs = 3;
166+
}
167+
162168
// Creating a new arguments list for the JavascriptNumber generated from each lane.The optional SIMDToLocaleString Args are
163169
//added to this argument list.
164-
Var* newArgs = HeapNewArray(Var, numArgs);
165-
switch (numArgs)
170+
Var newArgs[3] = { nullptr, nullptr, nullptr };
171+
CallInfo newCallInfo((ushort)numArgs);
172+
173+
if (numArgs > 1)
166174
{
167-
case 1:
168-
break;
169-
case 2:
170-
newArgs[1] = args[1];
171-
break;
172-
case 3:
173175
newArgs[1] = args[1];
176+
}
177+
if (numArgs > 2)
178+
{
174179
newArgs[2] = args[2];
175-
break;
176-
default:
177-
Assert(UNREACHED);
178180
}
181+
179182
//Locale specifc seperator??
180183
JavascriptString *seperator = JavascriptString::NewWithSz(_u(", "), scriptContext);
181184
uint idx = 0;
@@ -184,7 +187,7 @@ namespace Js
184187
char16* stringBuffer = AnewArray(tempAllocator, char16, SIMD_STRING_BUFFER_MAX);
185188
JavascriptString *result = nullptr;
186189

187-
swprintf_s(stringBuffer, 1024, typeString);
190+
swprintf_s(stringBuffer, SIMD_STRING_BUFFER_MAX, typeString);
188191
result = JavascriptString::NewCopySzFromArena(stringBuffer, scriptContext, scriptContext->GeneralAllocator());
189192

190193
if (typeDescriptor == TypeIds_SIMDFloat32x4)
@@ -193,44 +196,43 @@ namespace Js
193196
{
194197
laneVar = JavascriptNumber::ToVarWithCheck(laneValues[idx], scriptContext);
195198
newArgs[0] = laneVar;
196-
JavascriptString *laneValue = JavascriptNumber::ToLocaleStringIntl(newArgs, *callInfo, scriptContext);
199+
JavascriptString *laneValue = JavascriptNumber::ToLocaleStringIntl(newArgs, newCallInfo, scriptContext);
197200
result = JavascriptString::Concat(result, laneValue);
198201
result = JavascriptString::Concat(result, seperator);
199202
}
200203
laneVar = JavascriptNumber::ToVarWithCheck(laneValues[idx], scriptContext);
201204
newArgs[0] = laneVar;
202-
result = JavascriptString::Concat(result, JavascriptNumber::ToLocaleStringIntl(newArgs, *callInfo, scriptContext));
205+
result = JavascriptString::Concat(result, JavascriptNumber::ToLocaleStringIntl(newArgs, newCallInfo, scriptContext));
203206
}
204207
else if (typeDescriptor == TypeIds_SIMDInt8x16 || typeDescriptor == TypeIds_SIMDInt16x8 || typeDescriptor == TypeIds_SIMDInt32x4)
205208
{
206209
for (; idx < numLanes - 1; ++idx)
207210
{
208-
laneVar = JavascriptNumber::ToVar(static_cast<int>(laneValues[idx]), scriptContext);
211+
laneVar = JavascriptNumber::ToVar(static_cast<int>(laneValues[idx]), scriptContext);
209212
newArgs[0] = laneVar;
210-
JavascriptString *laneValue = JavascriptNumber::ToLocaleStringIntl(newArgs, *callInfo, scriptContext);
213+
JavascriptString *laneValue = JavascriptNumber::ToLocaleStringIntl(newArgs, newCallInfo, scriptContext);
211214
result = JavascriptString::Concat(result, laneValue);
212215
result = JavascriptString::Concat(result, seperator);
213216
}
214217
laneVar = JavascriptNumber::ToVar(static_cast<int>(laneValues[idx]), scriptContext);
215218
newArgs[0] = laneVar;
216-
result = JavascriptString::Concat(result, JavascriptNumber::ToLocaleStringIntl(newArgs, *callInfo, scriptContext));
219+
result = JavascriptString::Concat(result, JavascriptNumber::ToLocaleStringIntl(newArgs, newCallInfo, scriptContext));
217220
}
218221
else
219222
{
220223
Assert((typeDescriptor == TypeIds_SIMDUint8x16 || typeDescriptor == TypeIds_SIMDUint16x8 || typeDescriptor == TypeIds_SIMDUint32x4));
221224
for (; idx < numLanes - 1; ++idx)
222225
{
223-
laneVar = JavascriptNumber::ToVar(static_cast<uint>(laneValues[idx]), scriptContext);
226+
laneVar = JavascriptNumber::ToVar(static_cast<uint>(laneValues[idx]), scriptContext);
224227
newArgs[0] = laneVar;
225-
JavascriptString *laneValue = JavascriptNumber::ToLocaleStringIntl(newArgs, *callInfo, scriptContext);
228+
JavascriptString *laneValue = JavascriptNumber::ToLocaleStringIntl(newArgs, newCallInfo, scriptContext);
226229
result = JavascriptString::Concat(result, laneValue);
227230
result = JavascriptString::Concat(result, seperator);
228231
}
229232
laneVar = JavascriptNumber::ToVar(static_cast<uint>(laneValues[idx]), scriptContext);
230233
newArgs[0] = laneVar;
231-
result = JavascriptString::Concat(result, JavascriptNumber::ToLocaleStringIntl(newArgs, *callInfo, scriptContext));
234+
result = JavascriptString::Concat(result, JavascriptNumber::ToLocaleStringIntl(newArgs, newCallInfo, scriptContext));
232235
}
233-
HeapDeleteArray(numArgs, newArgs);
234236
END_TEMP_ALLOCATOR(tempAllocator, scriptContext);
235237
return JavascriptString::Concat(result, JavascriptString::NewWithSz(_u(")"), scriptContext));
236238
}

lib/Runtime/Library/SimdFloat32x4Lib.cpp

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,8 +1035,26 @@ namespace Js
10351035
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
10361036
Assert(!(callInfo.Flags & CallFlags_New));
10371037

1038+
Var tarray;
1039+
Var index;
1040+
if (args.Info.Count > 1)
1041+
{
1042+
tarray = args[1];
1043+
}
1044+
else
1045+
{
1046+
tarray = scriptContext->GetLibrary()->GetUndefined();
1047+
}
1048+
if (args.Info.Count > 2)
1049+
{
1050+
index = args[2];
1051+
}
1052+
else
1053+
{
1054+
index = scriptContext->GetLibrary()->GetUndefined();
1055+
}
10381056

1039-
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(args[1], args[2], 4 * FLOAT32_SIZE, scriptContext);
1057+
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(tarray, index, 4 * FLOAT32_SIZE, scriptContext);
10401058
}
10411059

10421060
Var SIMDFloat32x4Lib::EntryLoad1(RecyclableObject* function, CallInfo callInfo, ...)
@@ -1049,7 +1067,26 @@ namespace Js
10491067
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
10501068
Assert(!(callInfo.Flags & CallFlags_New));
10511069

1052-
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(args[1], args[2], 1 * FLOAT32_SIZE, scriptContext);
1070+
Var tarray;
1071+
Var index;
1072+
if (args.Info.Count > 1)
1073+
{
1074+
tarray = args[1];
1075+
}
1076+
else
1077+
{
1078+
tarray = scriptContext->GetLibrary()->GetUndefined();
1079+
}
1080+
if (args.Info.Count > 2)
1081+
{
1082+
index = args[2];
1083+
}
1084+
else
1085+
{
1086+
index = scriptContext->GetLibrary()->GetUndefined();
1087+
}
1088+
1089+
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(tarray, index, 1 * FLOAT32_SIZE, scriptContext);
10531090
}
10541091

10551092
Var SIMDFloat32x4Lib::EntryLoad2(RecyclableObject* function, CallInfo callInfo, ...)
@@ -1062,7 +1099,26 @@ namespace Js
10621099
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
10631100
Assert(!(callInfo.Flags & CallFlags_New));
10641101

1065-
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(args[1], args[2], 2 * FLOAT32_SIZE, scriptContext);
1102+
Var tarray;
1103+
Var index;
1104+
if (args.Info.Count > 1)
1105+
{
1106+
tarray = args[1];
1107+
}
1108+
else
1109+
{
1110+
tarray = scriptContext->GetLibrary()->GetUndefined();
1111+
}
1112+
if (args.Info.Count > 2)
1113+
{
1114+
index = args[2];
1115+
}
1116+
else
1117+
{
1118+
index = scriptContext->GetLibrary()->GetUndefined();
1119+
}
1120+
1121+
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(tarray, index, 2 * FLOAT32_SIZE, scriptContext);
10661122
}
10671123

10681124
Var SIMDFloat32x4Lib::EntryLoad3(RecyclableObject* function, CallInfo callInfo, ...)
@@ -1075,7 +1131,26 @@ namespace Js
10751131
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
10761132
Assert(!(callInfo.Flags & CallFlags_New));
10771133

1078-
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(args[1], args[2], 3 * FLOAT32_SIZE, scriptContext);
1134+
Var tarray;
1135+
Var index;
1136+
if (args.Info.Count > 1)
1137+
{
1138+
tarray = args[1];
1139+
}
1140+
else
1141+
{
1142+
tarray = scriptContext->GetLibrary()->GetUndefined();
1143+
}
1144+
if (args.Info.Count > 2)
1145+
{
1146+
index = args[2];
1147+
}
1148+
else
1149+
{
1150+
index = scriptContext->GetLibrary()->GetUndefined();
1151+
}
1152+
1153+
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(tarray, index, 3 * FLOAT32_SIZE, scriptContext);
10791154
}
10801155

10811156
Var SIMDFloat32x4Lib::EntryStore(RecyclableObject* function, CallInfo callInfo, ...)

lib/Runtime/Library/SimdFloat64x2Lib.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,26 @@ namespace Js
875875
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
876876
Assert(!(callInfo.Flags & CallFlags_New));
877877

878-
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDFloat64x2>(args[1], args[2], 2 * FLOAT64_SIZE, scriptContext);
878+
Var tarray;
879+
Var index;
880+
if (args.Info.Count > 1)
881+
{
882+
tarray = args[1];
883+
}
884+
else
885+
{
886+
tarray = scriptContext->GetLibrary()->GetUndefined();
887+
}
888+
if (args.Info.Count > 2)
889+
{
890+
index = args[2];
891+
}
892+
else
893+
{
894+
index = scriptContext->GetLibrary()->GetUndefined();
895+
}
896+
897+
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDFloat64x2>(tarray, index, 2 * FLOAT64_SIZE, scriptContext);
879898
}
880899

881900
Var SIMDFloat64x2Lib::EntryLoad1(RecyclableObject* function, CallInfo callInfo, ...)
@@ -888,7 +907,26 @@ namespace Js
888907
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
889908
Assert(!(callInfo.Flags & CallFlags_New));
890909

891-
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDFloat64x2>(args[1], args[2], 1 * FLOAT64_SIZE, scriptContext);
910+
Var tarray;
911+
Var index;
912+
if (args.Info.Count > 1)
913+
{
914+
tarray = args[1];
915+
}
916+
else
917+
{
918+
tarray = scriptContext->GetLibrary()->GetUndefined();
919+
}
920+
if (args.Info.Count > 2)
921+
{
922+
index = args[2];
923+
}
924+
else
925+
{
926+
index = scriptContext->GetLibrary()->GetUndefined();
927+
}
928+
929+
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDFloat64x2>(tarray, index, 1 * FLOAT64_SIZE, scriptContext);
892930
}
893931

894932
Var SIMDFloat64x2Lib::EntryStore(RecyclableObject* function, CallInfo callInfo, ...)

lib/Runtime/Library/SimdInt16x8Lib.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,26 @@ namespace Js
703703
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
704704
Assert(!(callInfo.Flags & CallFlags_New));
705705

706+
Var tarray;
707+
Var index;
708+
if (args.Info.Count > 1)
709+
{
710+
tarray = args[1];
711+
}
712+
else
713+
{
714+
tarray = scriptContext->GetLibrary()->GetUndefined();
715+
}
716+
if (args.Info.Count > 2)
717+
{
718+
index = args[2];
719+
}
720+
else
721+
{
722+
index = scriptContext->GetLibrary()->GetUndefined();
723+
}
706724

707-
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDInt16x8>(args[1], args[2], 8 * INT16_SIZE, scriptContext);
725+
return SIMDUtils::SIMD128TypedArrayLoad<JavascriptSIMDInt16x8>(tarray, index, 8 * INT16_SIZE, scriptContext);
708726
}
709727

710728
Var SIMDInt16x8Lib::EntryStore(RecyclableObject* function, CallInfo callInfo, ...)

0 commit comments

Comments
 (0)