Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing CVE-2021-42279: Chakra Scripting Engine Memory Corruption Vulnerability #6996

Closed
wants to merge 13 commits into from

Commits on Jul 1, 2024

  1. Changes For:

    =========
    CVE-2021-42279: Chakra Scripting Engine Memory Corruption Vulnerability
    
    CVE-2021-42279 is a high-severity memory corruption vulnerability affecting the Chakra Scripting Engine and ChakraCore.
    
    The vulnerability stems from an out-of-bounds write, which can potentially be exploited to achieve remote code execution (RCE). An attacker could exploit this by convincing a user to perform certain actions, leading to memory corruption and potentially allowing the attacker to execute arbitrary code on the affected system.
    
    Vulnerability Type:
    =============
    Memory Violation, resulting in Remote Code Execution (RCE)
    
    Affected Versions:
    ============
    All versions of ChakraCore up to and including 1.11.24.
    
    Severity:
    ======
    CVSS Score: 7.5 (HIGH)
    
    Root Cause:
    =========
    # An out-of-bounds array write occurs when a program writes data to a memory location outside the bounds of the allocated memory for an array.
    # In the context of ChakraCore, an out-of-bounds write can lead to memory corruption. When data is written outside the bounds of an array, it can overwrite adjacent memory regions that may be used for other variables, objects, or control structures. This unintended overwrite can corrupt data, causing unpredictable behavior or program crashes.
    # Consider a simplified scenario where ChakraCore mishandles the bounds of an array:
    
    // JavaScript code that might trigger out-of-bounds write
    let arr = new Array(1);
    arr[100] = 42; // This could lead to out-of-bounds write if not properly handled
    
    # If ChakraCore does not correctly check the bounds before writing to the ‘arr[100]’ position, it could write the value ‘42’ to a memory location outside the allocated space for ‘arr’. This could overwrite important data or control structures, leading to memory corruption.
    
    Fix:
    ===
    # To address this issue, fix is added to allow setting elements only if the index falls within the bounds of the array.
    # This fix is added such that, it takes effect only if additional command line switch “--ValidateArrayBounds” is passed to ChakraCore. With this, we ensure that the existing functionalities of ChakraCore continues to work as before.
    # Users of ChakraCore can take advantage of the newly added switch “--ValidateArrayBounds", which helps in making sure, any injection of elements to an array is restricted only within the bounds of the array. With this, we ensure that the out-of-bound write issue is not seen, there by preventing write operation to sensitive memory locations.
    # By default, “--ValidateArrayBounds" will be false.
    
    Unit Testing:
    ==========
    All unit test cases are executed and there is no failure due to these new changes. In fact, this fix takes effect only when the switch “--ValidateArrayBounds" is passed to the ChakraCore engine, so there is no impact to existing testcases/functionalities.
    
    Sample Output:
    ===========
    Consider Script "test.js" below:
    
    let arr = new Array(2);
    console.log("arr.length is " + arr.length)
    arr[0] = 0;
    console.log("arr[0] is " + arr[0])
    arr[1] = 1;
    console.log("arr[1] is " + arr[1])
    arr[100] = 100;
    console.log("arr[100] is " + arr[100])
    
    Output without the newly added switch,
    C:\temp>ch.exe test.js
    arr.length is 2
    arr[0] is 0
    arr[1] is 1
    arr[100] is 100
    
    Output with the newly added switch,
    C:\temp>ch.exe --ValidateArrayBounds test.js
    arr.length is 2
    arr[0] is 0
    arr[1] is 1
    RangeError: Memory index is out of range
       at Global code (C:\temp\test.js:11:1)
    
    References:
    ========
    https://nvd.nist.gov/vuln/detail/CVE-2021-42279
    https://www.cve.org/CVERecord?id=CVE-2021-42279
    https://github.com/chakra-core/ChakraCore
    bhmohanr-techie committed Jul 1, 2024
    Configuration menu
    Copy the full SHA
    20df1ea View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    dfa5a16 View commit details
    Browse the repository at this point in the history

Commits on Jul 2, 2024

  1. Merging from chakra-core#6970

    Avoid using MSVC-internal _STRINGIZE chakra-core#6970
    
    Below is the comment from
    Stephan T. Lavavej in the above mentioned pull request:
    
    "I work on Microsoft's C++ Standard Library implementation, where we recently merged microsoft/STL#4405 to remove our internal _STRINGIZE macro. Our "Real World Code" test suite, which builds popular open-source projects like yours, found that you were using this MSVC-internal macro and therefore our change broke your code.
    
    The C++ Standard's rule is that _Leading_underscore_capital identifiers (including _LEADING_UNDERSCORE_ALL_CAPS) are reserved for the compiler and Standard Library, so other libraries and applications should avoid using such reserved identifiers. This is N4971 5.10 [lex.name]/3:
    
    In addition, some identifiers appearing as a token or preprocessing-token are reserved for use by C++ implementations and shall not be used otherwise; no diagnostic is required.
    — Each identifier that contains a double underscore __ or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.
    
    This PR introduces non-reserved names that will work on all platforms."
    bhmohanr-techie committed Jul 2, 2024
    Configuration menu
    Copy the full SHA
    d7c3d57 View commit details
    Browse the repository at this point in the history
  2. Updating the copyright info to follow the format of pal_copyright_lin…

    …es as defined in tools/StyleChecks/check_copyright.py.
    bhmohanr-techie committed Jul 2, 2024
    Configuration menu
    Copy the full SHA
    1da48f1 View commit details
    Browse the repository at this point in the history

Commits on Jul 8, 2024

  1. Added couple of changes here,

    1. I have added two new tests as part of the unit test framework within ChakraCore (one test to verify out-of-bounds access behavior without my  fix, and other test with my fix). In both the cases, I have verified that the tests are PASSED.
    2. In addition to my earlier commits for handling out-of-bounds write with javascript arrays, I have added a change here to address out-of-bounds read scenario.
    bhmohanr-techie committed Jul 8, 2024
    Configuration menu
    Copy the full SHA
    4053e07 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    402d98b View commit details
    Browse the repository at this point in the history
  3. This change is in continuation to my earlier commit for adding unit t…

    …ests... Added a change here to summarize the unit test results, so that the unit test framewokr can capture and report the test results.
    bhmohanr-techie committed Jul 8, 2024
    Configuration menu
    Copy the full SHA
    8a67147 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    a72f9f9 View commit details
    Browse the repository at this point in the history

Commits on Jul 9, 2024

  1. Deleting one of the unit test code, that was written without followin…

    …g UnitTestFramework... This will be replaced with new unit test soon, that adheres to ChakraCore's unit test framework.
    bhmohanr-techie committed Jul 9, 2024
    Configuration menu
    Copy the full SHA
    ea32270 View commit details
    Browse the repository at this point in the history
  2. Updating rlexe.xml

    bhmohanr-techie committed Jul 9, 2024
    Configuration menu
    Copy the full SHA
    db3294f View commit details
    Browse the repository at this point in the history

Commits on Aug 12, 2024

  1. Configuration menu
    Copy the full SHA
    dfe539a View commit details
    Browse the repository at this point in the history
  2. Merging PR: chakra-core#6531.

    Merging PR: chakra-core#6531. 
    This is the change that was added by Microsoft as part of their December 2020 Security Update, which addresses the CVE-2020-17131. This CVE is to address Out-of-bounds Write in ChakraCore, and there by fixing memory corruption vulnerability. This change is unfortunately missed in the latest ChakraCore code base, and hence adding this for review.
    bhmohanr-techie committed Aug 12, 2024
    Configuration menu
    Copy the full SHA
    4800753 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    4726126 View commit details
    Browse the repository at this point in the history