2013-01-11 01:50:16 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 ci et: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winternl.h> // NTSTATUS
|
2013-03-06 19:11:57 +04:00
|
|
|
#include <io.h>
|
2013-01-11 01:50:16 +04:00
|
|
|
#include "nsWindowsDllInterceptor.h"
|
|
|
|
#include "mozilla/mozPoisonWrite.h"
|
|
|
|
#include "mozPoisonWriteBase.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2013-03-06 19:11:57 +04:00
|
|
|
intptr_t FileDescriptorToID(int aFd) {
|
|
|
|
return _get_osfhandle(aFd);
|
|
|
|
}
|
|
|
|
|
2013-01-11 01:50:16 +04:00
|
|
|
static WindowsDllInterceptor sNtDllInterceptor;
|
|
|
|
|
2013-03-06 19:11:57 +04:00
|
|
|
void AbortOnBadWrite(HANDLE);
|
2013-01-11 01:50:16 +04:00
|
|
|
bool ValidWriteAssert(bool ok);
|
|
|
|
|
2013-03-06 19:11:57 +04:00
|
|
|
typedef NTSTATUS (WINAPI* WriteFile_fn)(HANDLE, HANDLE, PIO_APC_ROUTINE,
|
|
|
|
void*, PIO_STATUS_BLOCK,
|
|
|
|
const void*, ULONG, PLARGE_INTEGER,
|
|
|
|
PULONG);
|
|
|
|
WriteFile_fn gOriginalWriteFile;
|
|
|
|
|
|
|
|
static NTSTATUS WINAPI
|
|
|
|
patched_WriteFile(HANDLE aFile, HANDLE aEvent, PIO_APC_ROUTINE aApc,
|
|
|
|
void* aApcUser, PIO_STATUS_BLOCK aIoStatus,
|
|
|
|
const void* aBuffer, ULONG aLength,
|
|
|
|
PLARGE_INTEGER aOffset, PULONG aKey)
|
|
|
|
{
|
|
|
|
AbortOnBadWrite(aFile);
|
|
|
|
return gOriginalWriteFile(aFile, aEvent, aApc, aApcUser, aIoStatus,
|
|
|
|
aBuffer, aLength, aOffset, aKey);
|
|
|
|
}
|
|
|
|
|
2013-03-07 19:02:33 +04:00
|
|
|
|
|
|
|
typedef NTSTATUS (WINAPI* WriteFileGather_fn)(HANDLE, HANDLE, PIO_APC_ROUTINE,
|
|
|
|
void*, PIO_STATUS_BLOCK,
|
|
|
|
FILE_SEGMENT_ELEMENT*,
|
|
|
|
ULONG, PLARGE_INTEGER, PULONG);
|
|
|
|
WriteFileGather_fn gOriginalWriteFileGather;
|
|
|
|
|
|
|
|
static NTSTATUS WINAPI
|
|
|
|
patched_WriteFileGather(HANDLE aFile, HANDLE aEvent, PIO_APC_ROUTINE aApc,
|
|
|
|
void* aApcUser, PIO_STATUS_BLOCK aIoStatus,
|
|
|
|
FILE_SEGMENT_ELEMENT* aSegments, ULONG aLength,
|
|
|
|
PLARGE_INTEGER aOffset, PULONG aKey)
|
|
|
|
{
|
|
|
|
AbortOnBadWrite(aFile);
|
|
|
|
return gOriginalWriteFileGather(aFile, aEvent, aApc, aApcUser, aIoStatus,
|
|
|
|
aSegments, aLength, aOffset, aKey);
|
|
|
|
}
|
|
|
|
|
2013-03-06 19:11:57 +04:00
|
|
|
void AbortOnBadWrite(HANDLE aFile)
|
2013-01-11 01:50:16 +04:00
|
|
|
{
|
|
|
|
if (!PoisonWriteEnabled())
|
|
|
|
return;
|
|
|
|
|
2013-03-06 19:11:57 +04:00
|
|
|
// Debugging files are OK.
|
|
|
|
if (IsDebugFile(reinterpret_cast<intptr_t>(aFile)))
|
|
|
|
return;
|
|
|
|
|
2013-01-11 01:50:16 +04:00
|
|
|
ValidWriteAssert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PoisonWrite() {
|
|
|
|
// Quick sanity check that we don't poison twice.
|
|
|
|
static bool WritesArePoisoned = false;
|
|
|
|
MOZ_ASSERT(!WritesArePoisoned);
|
|
|
|
if (WritesArePoisoned)
|
|
|
|
return;
|
|
|
|
WritesArePoisoned = true;
|
|
|
|
|
|
|
|
if (!PoisonWriteEnabled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
sNtDllInterceptor.Init("ntdll.dll");
|
2013-03-06 19:11:57 +04:00
|
|
|
sNtDllInterceptor.AddHook("NtWriteFile", reinterpret_cast<intptr_t>(patched_WriteFile), reinterpret_cast<void**>(&gOriginalWriteFile));
|
2013-03-07 19:02:33 +04:00
|
|
|
sNtDllInterceptor.AddHook("NtWriteFileGather", reinterpret_cast<intptr_t>(patched_WriteFileGather), reinterpret_cast<void**>(&gOriginalWriteFileGather));
|
2013-01-11 01:50:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|