Skip to content

Commit b6e09c2

Browse files
committed
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.
1 parent 8df95e1 commit b6e09c2

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
@@ -295,35 +295,46 @@ namespace Js
295295

296296
void IntlEngineInterfaceExtensionObject::deletePrototypePropertyHelper(ScriptContext* scriptContext, DynamicObject* intlObject, Js::PropertyId objectPropertyId, Js::PropertyId getterFunctionId)
297297
{
298-
DynamicObject *prototypeVal = nullptr;
298+
DynamicObject *prototypeObject = nullptr;
299299
DynamicObject *functionObj = nullptr;
300-
Var propertyValue;
301-
Var getter;
302-
Var setter;
300+
Var propertyValue = nullptr;
301+
Var prototypeValue = nullptr;
302+
Var resolvedOptionsValue = nullptr;
303+
Var getter = nullptr;
304+
Var setter = nullptr;
303305

304-
if (!Js::JavascriptOperators::GetProperty(intlObject, objectPropertyId, &propertyValue, scriptContext))
306+
if (!JavascriptOperators::GetProperty(intlObject, objectPropertyId, &propertyValue, scriptContext) ||
307+
!JavascriptOperators::IsObject(propertyValue))
305308
{
306-
AssertMsg(false, "Error.");
307309
return;
308310
}
309311

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

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

322-
(functionObj = DynamicObject::FromVar(propertyValue))->SetConfigurable(Js::PropertyIds::prototype, true);
326+
functionObj = DynamicObject::FromVar(resolvedOptionsValue);
327+
functionObj->SetConfigurable(Js::PropertyIds::prototype, true);
323328
functionObj->DeleteProperty(Js::PropertyIds::prototype, Js::PropertyOperationFlags::PropertyOperation_None);
324329

325-
JavascriptOperators::GetOwnAccessors(prototypeVal, getterFunctionId, &getter, &setter, scriptContext);
326-
(functionObj = DynamicObject::FromVar(getter))->SetConfigurable(Js::PropertyIds::prototype, true);
330+
if (!JavascriptOperators::GetOwnAccessors(prototypeObject, getterFunctionId, &getter, &setter, scriptContext) ||
331+
!JavascriptOperators::IsObject(getter))
332+
{
333+
return;
334+
}
335+
336+
functionObj = DynamicObject::FromVar(getter);
337+
functionObj->SetConfigurable(Js::PropertyIds::prototype, true);
327338
functionObj->DeleteProperty(Js::PropertyIds::prototype, Js::PropertyOperationFlags::PropertyOperation_None);
328339
}
329340

lib/Runtime/Library/JavascriptFunction.cpp

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

990990
if (arr != nullptr && !arr->IsCrossSiteObject())
991991
{
992+
uint32 length = arr->GetLength();
992993
// CONSIDER: Optimize by creating a JavascriptArray routine which allows
993994
// memcpy-like semantics in optimal situations (no gaps, etc.)
994-
if (argsIndex + arr->GetLength() > destArgs.Info.Count)
995+
if (argsIndex + length > destArgs.Info.Count)
995996
{
996997
AssertMsg(false, "The array length has changed since we allocated the destArgs buffer?");
997998
Throw::FatalInternalError();
998999
}
9991000

1000-
for (uint32 j = 0; j < arr->GetLength(); j++)
1001+
for (uint32 j = 0; j < length; j++)
10011002
{
10021003
Var element;
10031004
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
@@ -1092,8 +1092,26 @@ namespace Js
10921092
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
10931093
Assert(!(callInfo.Flags & CallFlags_New));
10941094

1095+
Var tarray;
1096+
Var index;
1097+
if (args.Info.Count > 1)
1098+
{
1099+
tarray = args[1];
1100+
}
1101+
else
1102+
{
1103+
tarray = scriptContext->GetLibrary()->GetUndefined();
1104+
}
1105+
if (args.Info.Count > 2)
1106+
{
1107+
index = args[2];
1108+
}
1109+
else
1110+
{
1111+
index = scriptContext->GetLibrary()->GetUndefined();
1112+
}
10951113

1096-
return SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(args[1], args[2], 4 * FLOAT32_SIZE, scriptContext);
1114+
return SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(tarray, index, 4 * FLOAT32_SIZE, scriptContext);
10971115
}
10981116

10991117
Var SIMDFloat32x4Lib::EntryLoad1(RecyclableObject* function, CallInfo callInfo, ...)
@@ -1106,7 +1124,26 @@ namespace Js
11061124
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
11071125
Assert(!(callInfo.Flags & CallFlags_New));
11081126

1109-
return SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(args[1], args[2], 1 * FLOAT32_SIZE, scriptContext);
1127+
Var tarray;
1128+
Var index;
1129+
if (args.Info.Count > 1)
1130+
{
1131+
tarray = args[1];
1132+
}
1133+
else
1134+
{
1135+
tarray = scriptContext->GetLibrary()->GetUndefined();
1136+
}
1137+
if (args.Info.Count > 2)
1138+
{
1139+
index = args[2];
1140+
}
1141+
else
1142+
{
1143+
index = scriptContext->GetLibrary()->GetUndefined();
1144+
}
1145+
1146+
return SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(tarray, index, 1 * FLOAT32_SIZE, scriptContext);
11101147
}
11111148

11121149
Var SIMDFloat32x4Lib::EntryLoad2(RecyclableObject* function, CallInfo callInfo, ...)
@@ -1119,7 +1156,26 @@ namespace Js
11191156
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
11201157
Assert(!(callInfo.Flags & CallFlags_New));
11211158

1122-
return SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(args[1], args[2], 2 * FLOAT32_SIZE, scriptContext);
1159+
Var tarray;
1160+
Var index;
1161+
if (args.Info.Count > 1)
1162+
{
1163+
tarray = args[1];
1164+
}
1165+
else
1166+
{
1167+
tarray = scriptContext->GetLibrary()->GetUndefined();
1168+
}
1169+
if (args.Info.Count > 2)
1170+
{
1171+
index = args[2];
1172+
}
1173+
else
1174+
{
1175+
index = scriptContext->GetLibrary()->GetUndefined();
1176+
}
1177+
1178+
return SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(tarray, index, 2 * FLOAT32_SIZE, scriptContext);
11231179
}
11241180

11251181
Var SIMDFloat32x4Lib::EntryLoad3(RecyclableObject* function, CallInfo callInfo, ...)
@@ -1132,7 +1188,26 @@ namespace Js
11321188
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
11331189
Assert(!(callInfo.Flags & CallFlags_New));
11341190

1135-
return SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(args[1], args[2], 3 * FLOAT32_SIZE, scriptContext);
1191+
Var tarray;
1192+
Var index;
1193+
if (args.Info.Count > 1)
1194+
{
1195+
tarray = args[1];
1196+
}
1197+
else
1198+
{
1199+
tarray = scriptContext->GetLibrary()->GetUndefined();
1200+
}
1201+
if (args.Info.Count > 2)
1202+
{
1203+
index = args[2];
1204+
}
1205+
else
1206+
{
1207+
index = scriptContext->GetLibrary()->GetUndefined();
1208+
}
1209+
1210+
return SIMD128TypedArrayLoad<JavascriptSIMDFloat32x4>(tarray, index, 3 * FLOAT32_SIZE, scriptContext);
11361211
}
11371212

11381213
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
@@ -873,7 +873,26 @@ namespace Js
873873
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
874874
Assert(!(callInfo.Flags & CallFlags_New));
875875

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

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

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

892930
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 SIMD128TypedArrayLoad<JavascriptSIMDInt16x8>(args[1], args[2], 8 * INT16_SIZE, scriptContext);
725+
return SIMD128TypedArrayLoad<JavascriptSIMDInt16x8>(tarray, index, 8 * INT16_SIZE, scriptContext);
708726
}
709727

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

0 commit comments

Comments
 (0)