Skip to content

Commit

Permalink
Flash fix:
Browse files Browse the repository at this point in the history
When flash was first used on Windows then console windows with "echo NOT SANDBOXED" flickered.
  • Loading branch information
janRucka committed Mar 24, 2017
1 parent 319d6c0 commit 4632f04
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions base/native_library_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,88 @@
#include <windows.h>

#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "base/win/iat_patch_function.h"
#include "chrome/common/chrome_paths.h"

namespace base {

typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name);

namespace {

base::win::IATPatchFunction* FlashCreateProcessProxy = nullptr;

BOOL WINAPI CreateProcessAForFlash(
_In_opt_ LPCSTR lpApplicationName,
_Inout_opt_ LPSTR lpCommandLine,
_In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes,
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ BOOL bInheritHandles,
_In_ DWORD dwCreationFlags,
_In_opt_ LPVOID lpEnvironment,
_In_opt_ LPCSTR lpCurrentDirectory,
_In_ LPSTARTUPINFOA lpStartupInfo,
_Out_ LPPROCESS_INFORMATION lpProcessInformation) {
bool unhook = false;
if (FlashCreateProcessProxy != nullptr &&
strstr(lpCommandLine, "cmd.exe /c echo NOT SANDBOXED") != NULL) {
unhook = true;
dwCreationFlags |= CREATE_NO_WINDOW;
}

typedef BOOL(WINAPI *CREATE_PROC) (
LPCSTR,
LPSTR,
LPSECURITY_ATTRIBUTES,
LPSECURITY_ATTRIBUTES,
BOOL,
DWORD,
LPVOID,
LPCSTR,
LPSTARTUPINFOA,
LPPROCESS_INFORMATION
);

if (FlashCreateProcessProxy != nullptr) {
CREATE_PROC createProc = (CREATE_PROC)FlashCreateProcessProxy->original_function();
BOOL retVal = createProc(
lpApplicationName,
lpCommandLine,
lpProcessAttributes,
lpThreadAttributes,
bInheritHandles,
dwCreationFlags,
lpEnvironment,
lpCurrentDirectory,
lpStartupInfo,
lpProcessInformation
);

if (unhook) {
DWORD lastError = GetLastError();
delete FlashCreateProcessProxy;
FlashCreateProcessProxy = nullptr;
SetLastError(lastError);
}
return retVal;
}
return FALSE;
}

bool IsFlash(const FilePath& library_path) {
base::FilePath flash_filename;
if (!PathService::Get(chrome::FILE_PEPPER_FLASH_SYSTEM_PLUGIN,
&flash_filename))
return false;

return flash_filename == library_path;
}

NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path,
LoadLibraryFunction load_library_api,
NativeLibraryLoadError* error) {
Expand All @@ -42,6 +113,20 @@ NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path,
error->code = GetLastError();
}

if (module) {
if (IsFlash(library_path)) {
FlashCreateProcessProxy = new base::win::IATPatchFunction;
if (NO_ERROR !=
FlashCreateProcessProxy->Patch(library_path.value().c_str(),
"kernel32.dll",
"CreateProcessA",
CreateProcessAForFlash)) {
delete FlashCreateProcessProxy;
FlashCreateProcessProxy = nullptr;
}
}
}

if (restore_directory)
SetCurrentDirectory(current_directory);

Expand Down

0 comments on commit 4632f04

Please sign in to comment.