Skip to content

Commit

Permalink
Fix CVE-2020-1914 by using NEXTINST for SaveGeneratorLong
Browse files Browse the repository at this point in the history
Summary:
If `SaveGeneratorLong` was emitted, it would accidentally jump to the
wrong next instruction, based on how long SaveGenerator was.

Make a callout function to handle the common case, and handle the dispatch
within each case of the interpreter loop.

Fixes CVE-2020-1914

Reviewed By: neildhar

Differential Revision: D24024242

fbshipit-source-id: 3bcb88daa740f0d50e91771a49eb212551ce8bd8
  • Loading branch information
dulinriley authored and facebook-github-bot committed Oct 1, 2020
1 parent fe0e3dd commit b2021df
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
8 changes: 8 additions & 0 deletions include/hermes/VM/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class Interpreter {
Handle<Environment> envHandle,
NativeArgs args);

/// Suspend the generator function and yield to the caller.
/// \param resumeIP Is the IP where the generator should resume from when it
/// is resumed.
static void saveGenerator(
Runtime *runtime,
PinnedHermesValue *frameRegs,
const Inst *resumeIP);

/// Slow path for ReifyArguments resReg, lazyReg
/// It assumes that he fast path has handled the case when 'lazyReg' is
/// already initialized. It creates a new 'arguments' object and populates it
Expand Down
11 changes: 11 additions & 0 deletions lib/VM/Interpreter-slowpaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "JSLib/JSLibInternal.h"
#include "hermes/VM/Casting.h"
#include "hermes/VM/Interpreter.h"
#include "hermes/VM/StackFrame-inline.h"
#include "hermes/VM/StringPrimitive.h"

#include "Interpreter-internal.h"
Expand All @@ -18,6 +19,16 @@ using namespace hermes::inst;
namespace hermes {
namespace vm {

void Interpreter::saveGenerator(
Runtime *runtime,
PinnedHermesValue *frameRegs,
const Inst *resumeIP) {
auto *innerFn = vmcast<GeneratorInnerFunction>(FRAME.getCalleeClosure());
innerFn->saveStack(runtime);
innerFn->setNextIP(resumeIP);
innerFn->setState(GeneratorInnerFunction::State::SuspendedYield);
}

ExecutionStatus Interpreter::caseDirectEval(
Runtime *runtime,
PinnedHermesValue *frameRegs,
Expand Down
33 changes: 18 additions & 15 deletions lib/VM/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,16 @@ CallResult<HermesValue> Interpreter::interpretFunction(

#endif // NDEBUG

/// \def DONT_CAPTURE_IP(expr)
/// \param expr A call expression to a function external to the interpreter. The
/// expression should not make any allocations and the IP should be set
/// immediately following this macro.
#define DONT_CAPTURE_IP(expr) \
do { \
NoAllocScope noAlloc(runtime); \
(void)expr; \
} while (false)

LLVM_DEBUG(dbgs() << "interpretFunction() called\n");

ScopedNativeDepthTracker depthTracker{runtime};
Expand Down Expand Up @@ -1798,25 +1808,18 @@ CallResult<HermesValue> Interpreter::interpretFunction(
}

CASE(SaveGenerator) {
nextIP = IPADD(ip->iSaveGenerator.op1);
goto doSaveGen;
DONT_CAPTURE_IP(
saveGenerator(runtime, frameRegs, IPADD(ip->iSaveGenerator.op1)));
ip = NEXTINST(SaveGenerator);
DISPATCH;
}
CASE(SaveGeneratorLong) {
nextIP = IPADD(ip->iSaveGeneratorLong.op1);
goto doSaveGen;
DONT_CAPTURE_IP(saveGenerator(
runtime, frameRegs, IPADD(ip->iSaveGeneratorLong.op1)));
ip = NEXTINST(SaveGeneratorLong);
DISPATCH;
}

doSaveGen : {
auto *innerFn = vmcast<GeneratorInnerFunction>(
runtime->getCurrentFrame().getCalleeClosure());

innerFn->saveStack(runtime);
innerFn->setNextIP(nextIP);
innerFn->setState(GeneratorInnerFunction::State::SuspendedYield);
ip = NEXTINST(SaveGenerator);
DISPATCH;
}

CASE(StartGenerator) {
auto *innerFn = vmcast<GeneratorInnerFunction>(
runtime->getCurrentFrame().getCalleeClosure());
Expand Down
15 changes: 15 additions & 0 deletions test/hermes/es6/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,18 @@ print(iterator.next().value);
iterator.return(123);
// CHECK-NEXT: 1
// CHECK-NEXT: get return

// Make sure using SaveGeneratorLong works.
function* saveGeneratorLong() {
yield* [1];
// Waste some registers, to change SaveGenerator to SaveGeneratorLong.
[].push(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
}
print(saveGeneratorLong().next().value);
// CHECK-NEXT: 1

0 comments on commit b2021df

Please sign in to comment.