Commit dda0385c authored by Arthur Edelstein's avatar Arthur Edelstein
Browse files

Bug 1344034 - Auto-enforce W^X for WindowsDllInterceptor hook pages. r=dmajor

parent 345e19c0
Loading
Loading
Loading
Loading
+10 −25
Original line number Diff line number Diff line
@@ -93,6 +93,9 @@ public:
  {
    mSuccess = !!VirtualProtectEx(GetCurrentProcess(), mFunc, mSize,
                                  mNewProtect, &mOldProtect);
    if (!mSuccess) {
      // printf("VirtualProtectEx failed! %d\n", GetLastError());
    }
    return mSuccess;
  }

@@ -132,7 +135,6 @@ public:
      // Ensure we can write to the code.
      AutoVirtualProtect protect(fn, 2, PAGE_EXECUTE_READWRITE);
      if (!protect.Protect()) {
        // printf("VirtualProtectEx failed! %d\n", GetLastError());
        continue;
      }

@@ -268,7 +270,6 @@ public:
    AutoVirtualProtect protectBefore(fn - 5, 5, PAGE_EXECUTE_READWRITE);
    AutoVirtualProtect protectAfter(fn, 2, PAGE_EXECUTE_READWRITE);
    if (!protectBefore.Protect() || !protectAfter.Protect()) {
      //printf ("VirtualProtectEx failed! %d\n", GetLastError());
      return false;
    }

@@ -377,7 +378,6 @@ public:
      // ensure we can modify the original code
      AutoVirtualProtect protect(origBytes, nBytes, PAGE_EXECUTE_READWRITE);
      if (!protect.Protect()) {
        //printf ("VirtualProtectEx failed! %d\n", GetLastError());
        continue;
      }

@@ -417,7 +417,7 @@ public:
    mHookPage = (byteptr_t)VirtualAllocEx(GetCurrentProcess(), nullptr,
                                          mMaxHooks * kHookSize,
                                          MEM_COMMIT | MEM_RESERVE,
                                          PAGE_EXECUTE_READWRITE);
                                          PAGE_EXECUTE_READ);
    if (!mHookPage) {
      mModule = 0;
      return;
@@ -426,19 +426,6 @@ public:

  bool Initialized() { return !!mModule; }

  void LockHooks()
  {
    if (!mModule) {
      return;
    }

    DWORD op;
    VirtualProtectEx(GetCurrentProcess(), mHookPage, mMaxHooks * kHookSize,
                     PAGE_EXECUTE_READ, &op);

    mModule = 0;
  }

  bool AddHook(const char* aName, intptr_t aHookDest, void** aOrigFunc)
  {
    if (!mModule) {
@@ -670,6 +657,12 @@ protected:
  {
    *aOutTramp = nullptr;

    AutoVirtualProtect protectHookPage(mHookPage, mMaxHooks * kHookSize,
                                       PAGE_EXECUTE_READWRITE);
    if (!protectHookPage.Protect()) {
      return;
    }

    byteptr_t tramp = FindTrampolineSpace();
    if (!tramp) {
      return;
@@ -1005,7 +998,6 @@ protected:
    // ensure we can modify the original code
    AutoVirtualProtect protect(aOrigFunction, nBytes, PAGE_EXECUTE_READWRITE);
    if (!protect.Protect()) {
      //printf ("VirtualProtectEx failed! %d\n", GetLastError());
      return;
    }

@@ -1091,13 +1083,6 @@ public:
    // not need it.
  }

  void LockHooks()
  {
    if (mDetourPatcher.Initialized()) {
      mDetourPatcher.LockHooks();
    }
  }

  bool AddHook(const char* aName, intptr_t aHookDest, void** aOrigFunc)
  {
    // Use a nop space patch if possible, otherwise fall back to a detour.