Skip to content

Commit

Permalink
[CVE-2017-0208] Fix integer overflow in string.repeat
Browse files Browse the repository at this point in the history
When using repeat API on javascript strings, we aren't checking for the upper cap of the length property.
Fix:
Instead of directly setting the length property in the constructor - We are now calling SetLength() - which also checks for the upper cap and throws OOM.
	       i
  • Loading branch information
satheeshravi authored and rajatd committed Apr 13, 2017
1 parent a8582a3 commit 54d6d08
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/Runtime/Library/JavascriptString.cpp
Expand Up @@ -199,10 +199,10 @@ namespace Js
}

JavascriptString::JavascriptString(StaticType * type, charcount_t charLength, const char16* szValue)
: RecyclableObject(type), m_charLength(charLength), m_pszValue(szValue)
: RecyclableObject(type), m_pszValue(szValue)
{
Assert(type->GetTypeId() == TypeIds_String);
AssertMsg(IsValidCharCount(charLength), "String length is out of range");
SetLength(charLength);
}

_Ret_range_(m_charLength, m_charLength)
Expand Down Expand Up @@ -3353,7 +3353,7 @@ namespace Js
return builder.ToString();
}

int JavascriptString::IndexOfUsingJmpTable(JmpTable jmpTable, const char16* inputStr, int len, const char16* searchStr, int searchLen, int position)
int JavascriptString::IndexOfUsingJmpTable(JmpTable jmpTable, const char16* inputStr, charcount_t len, const char16* searchStr, int searchLen, int position)
{
int result = -1;

Expand Down Expand Up @@ -3400,7 +3400,7 @@ namespace Js
return result;
}

int JavascriptString::LastIndexOfUsingJmpTable(JmpTable jmpTable, const char16* inputStr, int len, const char16* searchStr, int searchLen, int position)
int JavascriptString::LastIndexOfUsingJmpTable(JmpTable jmpTable, const char16* inputStr, charcount_t len, const char16* searchStr, charcount_t searchLen, charcount_t position)
{
const char16 searchFirst = searchStr[0];
uint32 lMatchedJump = searchLen;
Expand Down
4 changes: 2 additions & 2 deletions lib/Runtime/Library/JavascriptString.h
Expand Up @@ -157,8 +157,8 @@ namespace Js
char16* GetSzCopy(); // get a copy of the inner string without compacting the chunks

static Var ToCaseCore(JavascriptString* pThis, ToCase toCase);
static int IndexOfUsingJmpTable(JmpTable jmpTable, const char16* inputStr, int len, const char16* searchStr, int searchLen, int position);
static int LastIndexOfUsingJmpTable(JmpTable jmpTable, const char16* inputStr, int len, const char16* searchStr, int searchLen, int position);
static int IndexOfUsingJmpTable(JmpTable jmpTable, const char16* inputStr, charcount_t len, const char16* searchStr, int searchLen, int position);
static int LastIndexOfUsingJmpTable(JmpTable jmpTable, const char16* inputStr, charcount_t len, const char16* searchStr, charcount_t searchLen, charcount_t position);
static bool BuildLastCharForwardBoyerMooreTable(JmpTable jmpTable, const char16* searchStr, int searchLen);
static bool BuildFirstCharBackwardBoyerMooreTable(JmpTable jmpTable, const char16* searchStr, int searchLen);
static charcount_t ConvertToIndex(Var varIndex, ScriptContext *scriptContext);
Expand Down
21 changes: 21 additions & 0 deletions test/Strings/repeatBug.js
@@ -0,0 +1,21 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------

try
{
var str = "+".repeat(0x80000000);
str = str.replace(str, "+");

WScript.Echo("FAIL: Was expecting Out of Memory exception.");
}
catch (e)
{
if(e.number == -2146828281) //Out of Memory
WScript.Echo("PASS");
else
WScript.Echo("FAIL: Got the wrong exception code.");
}


6 changes: 6 additions & 0 deletions test/Strings/rlexe.xml
Expand Up @@ -242,4 +242,10 @@
<tags>exclude_win7</tags>
</default>
</test>
<test>
<default>
<files>repeatBug.js</files>
<tags>exclude_chk, Slow</tags>
</default>
</test>
</regress-exe>

0 comments on commit 54d6d08

Please sign in to comment.