2014-07-23 07:54:41 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/.
|
2009-11-17 22:17:20 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Code in this file needs to be kept in sync with code in nsPresArena.cpp.
|
|
|
|
*
|
|
|
|
* We want to use a fixed address for frame poisoning so that it is readily
|
|
|
|
* identifiable in crash dumps. Whether such an address is available
|
|
|
|
* without any special setup depends on the system configuration.
|
|
|
|
*
|
|
|
|
* All current 64-bit CPUs (with the possible exception of PowerPC64)
|
|
|
|
* reserve the vast majority of the virtual address space for future
|
|
|
|
* hardware extensions; valid addresses must be below some break point
|
|
|
|
* between 2**48 and 2**54, depending on exactly which chip you have. Some
|
|
|
|
* chips (notably amd64) also allow the use of the *highest* 2**48 -- 2**54
|
|
|
|
* addresses. Thus, if user space pointers are 64 bits wide, we can just
|
|
|
|
* use an address outside this range, and no more is required. To
|
|
|
|
* accommodate the chips that allow very high addresses to be valid, the
|
|
|
|
* value chosen is close to 2**63 (that is, in the middle of the space).
|
|
|
|
*
|
|
|
|
* In most cases, a purely 32-bit operating system must reserve some
|
|
|
|
* fraction of the address space for its own use. Contemporary 32-bit OSes
|
|
|
|
* tend to take the high gigabyte or so (0xC000_0000 on up). If we can
|
|
|
|
* prove that high addresses are reserved to the kernel, we can use an
|
|
|
|
* address in that region. Unfortunately, not all 32-bit OSes do this;
|
|
|
|
* OSX 10.4 might not, and it is unclear what mobile OSes are like
|
|
|
|
* (some 32-bit CPUs make it very easy for the kernel to exist in its own
|
|
|
|
* private address space).
|
|
|
|
*
|
|
|
|
* Furthermore, when a 32-bit user space process is running on a 64-bit
|
|
|
|
* kernel, the operating system has no need to reserve any of the space that
|
|
|
|
* the process can see, and generally does not do so. This is the scenario
|
|
|
|
* of greatest concern, since it covers all contemporary OSX iterations
|
|
|
|
* (10.5+) as well as Windows Vista and 7 on newer amd64 hardware. Linux on
|
|
|
|
* amd64 is generally run as a pure 64-bit environment, but its 32-bit
|
|
|
|
* compatibility mode also has this property.
|
|
|
|
*
|
|
|
|
* Thus, when user space pointers are 32 bits wide, we need to validate
|
|
|
|
* our chosen address, and possibly *make* it a good poison address by
|
|
|
|
* allocating a page around it and marking it inaccessible. The algorithm
|
|
|
|
* for this is:
|
|
|
|
*
|
|
|
|
* 1. Attempt to make the page surrounding the poison address a reserved,
|
|
|
|
* inaccessible memory region using OS primitives. On Windows, this is
|
|
|
|
* done with VirtualAlloc(MEM_RESERVE); on Unix, mmap(PROT_NONE).
|
|
|
|
*
|
|
|
|
* 2. If mmap/VirtualAlloc failed, there are two possible reasons: either
|
|
|
|
* the region is reserved to the kernel and no further action is
|
|
|
|
* required, or there is already usable memory in this area and we have
|
|
|
|
* to pick a different address. The tricky part is knowing which case
|
|
|
|
* we have, without attempting to access the region. On Windows, we
|
|
|
|
* rely on GetSystemInfo()'s reported upper and lower bounds of the
|
|
|
|
* application memory area. On Unix, there is nothing devoted to the
|
|
|
|
* purpose, but seeing if madvise() fails is close enough (it *might*
|
|
|
|
* disrupt someone else's use of the memory region, but not by as much
|
|
|
|
* as anything else available).
|
|
|
|
*
|
|
|
|
* Be aware of these gotchas:
|
|
|
|
*
|
|
|
|
* 1. We cannot use mmap() with MAP_FIXED. MAP_FIXED is defined to
|
|
|
|
* _replace_ any existing mapping in the region, if necessary to satisfy
|
|
|
|
* the request. Obviously, as we are blindly attempting to acquire a
|
|
|
|
* page at a constant address, we must not do this, lest we overwrite
|
|
|
|
* someone else's allocation.
|
|
|
|
*
|
|
|
|
* 2. For the same reason, we cannot blindly use mprotect() if mmap() fails.
|
|
|
|
*
|
|
|
|
* 3. madvise() may fail when applied to a 'magic' memory region provided as
|
|
|
|
* a kernel/user interface. Fortunately, the only such case I know about
|
|
|
|
* is the "vsyscall" area (not to be confused with the "vdso" area) for
|
|
|
|
* *64*-bit processes on Linux - and we don't even run this code for
|
|
|
|
* 64-bit processes.
|
|
|
|
*
|
|
|
|
* 4. VirtualQuery() does not produce any useful information if
|
|
|
|
* applied to kernel memory - in fact, it doesn't write its output
|
|
|
|
* at all. Thus, it is not used here.
|
|
|
|
*/
|
|
|
|
|
2011-12-15 09:27:42 +04:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2013-07-26 03:31:48 +04:00
|
|
|
|
2011-12-15 09:27:42 +04:00
|
|
|
// MAP_ANON(YMOUS) is not in any standard. Add defines as necessary.
|
2009-11-17 22:17:20 +03:00
|
|
|
#define _GNU_SOURCE 1
|
|
|
|
#define _DARWIN_C_SOURCE 1
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#ifndef MAP_ANON
|
|
|
|
#ifdef MAP_ANONYMOUS
|
|
|
|
#define MAP_ANON MAP_ANONYMOUS
|
|
|
|
#else
|
|
|
|
#error "Don't know how to get anonymous memory"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define SIZxPTR ((int)(sizeof(uintptr_t)*2))
|
|
|
|
|
|
|
|
/* This program assumes that a whole number of return instructions fit into
|
|
|
|
* 32 bits, and that 32-bit alignment is sufficient for a branch destination.
|
2010-11-11 11:19:11 +03:00
|
|
|
* For architectures where this is not true, fiddling with RETURN_INSTR_TYPE
|
|
|
|
* can be enough.
|
2009-11-17 22:17:20 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined __i386__ || defined __x86_64__ || \
|
2009-11-20 09:11:42 +03:00
|
|
|
defined __i386 || defined __x86_64 || \
|
2009-11-17 22:17:20 +03:00
|
|
|
defined _M_IX86 || defined _M_AMD64
|
|
|
|
#define RETURN_INSTR 0xC3C3C3C3 /* ret; ret; ret; ret */
|
|
|
|
|
|
|
|
#elif defined __arm__ || defined _M_ARM
|
|
|
|
#define RETURN_INSTR 0xE12FFF1E /* bx lr */
|
|
|
|
|
|
|
|
// PPC has its own style of CPU-id #defines. There is no Windows for
|
|
|
|
// PPC as far as I know, so no _M_ variant.
|
|
|
|
#elif defined _ARCH_PPC || defined _ARCH_PWR || defined _ARCH_PWR2
|
|
|
|
#define RETURN_INSTR 0x4E800020 /* blr */
|
|
|
|
|
2009-11-20 09:11:42 +03:00
|
|
|
#elif defined __sparc || defined __sparcv9
|
|
|
|
#define RETURN_INSTR 0x81c3e008 /* retl */
|
|
|
|
|
2010-11-11 11:19:11 +03:00
|
|
|
#elif defined __alpha
|
|
|
|
#define RETURN_INSTR 0x6bfa8001 /* ret */
|
|
|
|
|
|
|
|
#elif defined __hppa
|
|
|
|
#define RETURN_INSTR 0xe840c002 /* bv,n r0(rp) */
|
|
|
|
|
|
|
|
#elif defined __mips
|
|
|
|
#define RETURN_INSTR 0x03e00008 /* jr ra */
|
|
|
|
|
|
|
|
#ifdef __MIPSEL
|
|
|
|
/* On mipsel, jr ra needs to be followed by a nop.
|
|
|
|
0x03e00008 as a 64 bits integer just does that */
|
|
|
|
#define RETURN_INSTR_TYPE uint64_t
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#elif defined __s390__
|
|
|
|
#define RETURN_INSTR 0x07fe0000 /* br %r14 */
|
|
|
|
|
2014-01-24 01:34:31 +04:00
|
|
|
#elif defined __aarch64__
|
|
|
|
#define RETURN_INSTR 0xd65f03c0 /* ret */
|
|
|
|
|
2010-11-11 11:19:11 +03:00
|
|
|
#elif defined __ia64
|
2014-07-23 07:54:41 +04:00
|
|
|
struct ia64_instr { uint32_t mI[4]; };
|
2010-11-11 11:19:11 +03:00
|
|
|
static const ia64_instr _return_instr =
|
|
|
|
{{ 0x00000011, 0x00000001, 0x80000200, 0x00840008 }}; /* br.ret.sptk.many b0 */
|
|
|
|
|
|
|
|
#define RETURN_INSTR _return_instr
|
|
|
|
#define RETURN_INSTR_TYPE ia64_instr
|
|
|
|
|
2009-11-17 22:17:20 +03:00
|
|
|
#else
|
|
|
|
#error "Need return instruction for this architecture"
|
|
|
|
#endif
|
|
|
|
|
2010-11-11 11:19:11 +03:00
|
|
|
#ifndef RETURN_INSTR_TYPE
|
|
|
|
#define RETURN_INSTR_TYPE uint32_t
|
|
|
|
#endif
|
|
|
|
|
2009-11-17 22:17:20 +03:00
|
|
|
// Miscellaneous Windows/Unix portability gumph
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
// Uses of this function deliberately leak the string.
|
|
|
|
static LPSTR
|
2014-07-23 07:54:41 +04:00
|
|
|
StrW32Error(DWORD aErrcode)
|
2009-11-17 22:17:20 +03:00
|
|
|
{
|
|
|
|
LPSTR errmsg;
|
|
|
|
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
2014-07-23 07:54:41 +04:00
|
|
|
nullptr, aErrcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
2013-07-26 03:31:48 +04:00
|
|
|
(LPSTR)&errmsg, 0, nullptr);
|
2009-11-17 22:17:20 +03:00
|
|
|
|
|
|
|
// FormatMessage puts an unwanted newline at the end of the string
|
|
|
|
size_t n = strlen(errmsg)-1;
|
2014-07-23 07:54:41 +04:00
|
|
|
while (errmsg[n] == '\r' || errmsg[n] == '\n') {
|
|
|
|
n--;
|
|
|
|
}
|
2009-11-17 22:17:20 +03:00
|
|
|
errmsg[n+1] = '\0';
|
|
|
|
return errmsg;
|
|
|
|
}
|
|
|
|
#define LastErrMsg() (StrW32Error(GetLastError()))
|
|
|
|
|
|
|
|
// Because we use VirtualAlloc in MEM_RESERVE mode, the "page size" we want
|
|
|
|
// is the allocation granularity.
|
2014-06-14 23:47:01 +04:00
|
|
|
static SYSTEM_INFO sInfo_;
|
2009-11-17 22:17:20 +03:00
|
|
|
|
2014-06-14 23:47:01 +04:00
|
|
|
static inline uint32_t
|
|
|
|
PageSize()
|
|
|
|
{
|
|
|
|
return sInfo_.dwAllocationGranularity;
|
|
|
|
}
|
2009-11-17 22:17:20 +03:00
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
static void*
|
|
|
|
ReserveRegion(uintptr_t aRequest, bool aAccessible)
|
2009-11-17 22:17:20 +03:00
|
|
|
{
|
2014-07-23 07:54:41 +04:00
|
|
|
return VirtualAlloc((void*)aRequest, PageSize(),
|
|
|
|
aAccessible ? MEM_RESERVE|MEM_COMMIT : MEM_RESERVE,
|
|
|
|
aAccessible ? PAGE_EXECUTE_READWRITE : PAGE_NOACCESS);
|
2009-11-17 22:17:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-07-23 07:54:41 +04:00
|
|
|
ReleaseRegion(void* aPage)
|
2009-11-17 22:17:20 +03:00
|
|
|
{
|
2014-07-23 07:54:41 +04:00
|
|
|
VirtualFree(aPage, PageSize(), MEM_RELEASE);
|
2009-11-17 22:17:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-07-23 07:54:41 +04:00
|
|
|
ProbeRegion(uintptr_t aPage)
|
2009-11-17 22:17:20 +03:00
|
|
|
{
|
2014-07-23 07:54:41 +04:00
|
|
|
return aPage >= (uintptr_t)sInfo_.lpMaximumApplicationAddress &&
|
|
|
|
aPage + PageSize() >= (uintptr_t)sInfo_.lpMaximumApplicationAddress;
|
2009-11-17 22:17:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-07-23 07:54:41 +04:00
|
|
|
MakeRegionExecutable(void*)
|
2009-11-17 22:17:20 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef MAP_FAILED
|
|
|
|
#define MAP_FAILED 0
|
|
|
|
|
2011-02-16 01:10:16 +03:00
|
|
|
#else // Unix
|
2009-11-17 22:17:20 +03:00
|
|
|
|
|
|
|
#define LastErrMsg() (strerror(errno))
|
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
static unsigned long gUnixPageSize;
|
2014-06-14 23:47:01 +04:00
|
|
|
|
|
|
|
static inline unsigned long
|
|
|
|
PageSize()
|
|
|
|
{
|
2014-07-23 07:54:41 +04:00
|
|
|
return gUnixPageSize;
|
2014-06-14 23:47:01 +04:00
|
|
|
}
|
2009-11-17 22:17:20 +03:00
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
static void*
|
|
|
|
ReserveRegion(uintptr_t aRequest, bool aAccessible)
|
2009-11-17 22:17:20 +03:00
|
|
|
{
|
2014-07-23 07:54:41 +04:00
|
|
|
return mmap(reinterpret_cast<void*>(aRequest), PageSize(),
|
|
|
|
aAccessible ? PROT_READ|PROT_WRITE : PROT_NONE,
|
2009-11-17 22:17:20 +03:00
|
|
|
MAP_PRIVATE|MAP_ANON, -1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-07-23 07:54:41 +04:00
|
|
|
ReleaseRegion(void* aPage)
|
2009-11-17 22:17:20 +03:00
|
|
|
{
|
2014-07-23 07:54:41 +04:00
|
|
|
munmap(aPage, PageSize());
|
2009-11-17 22:17:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-07-23 07:54:41 +04:00
|
|
|
ProbeRegion(uintptr_t aPage)
|
2009-11-17 22:17:20 +03:00
|
|
|
{
|
2014-07-23 07:54:41 +04:00
|
|
|
return !!madvise(reinterpret_cast<void*>(aPage), PageSize(), MADV_NORMAL);
|
2009-11-17 22:17:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-07-23 07:54:41 +04:00
|
|
|
MakeRegionExecutable(void* aPage)
|
2009-11-17 22:17:20 +03:00
|
|
|
{
|
2014-07-23 07:54:41 +04:00
|
|
|
return mprotect((caddr_t)aPage, PageSize(), PROT_READ|PROT_WRITE|PROT_EXEC);
|
2009-11-17 22:17:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static uintptr_t
|
|
|
|
ReservePoisonArea()
|
|
|
|
{
|
|
|
|
if (sizeof(uintptr_t) == 8) {
|
|
|
|
// Use the hardware-inaccessible region.
|
|
|
|
// We have to avoid 64-bit constants and shifts by 32 bits, since this
|
|
|
|
// code is compiled in 32-bit mode, although it is never executed there.
|
|
|
|
uintptr_t result = (((uintptr_t(0x7FFFFFFFu) << 31) << 1 |
|
|
|
|
uintptr_t(0xF0DEAFFFu)) &
|
2014-06-14 23:47:01 +04:00
|
|
|
~uintptr_t(PageSize()-1));
|
2012-03-15 22:01:08 +04:00
|
|
|
printf("INFO | poison area assumed at 0x%.*" PRIxPTR "\n", SIZxPTR, result);
|
2009-11-17 22:17:20 +03:00
|
|
|
return result;
|
2014-07-23 07:54:41 +04:00
|
|
|
}
|
2009-11-30 19:51:07 +03:00
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
// First see if we can allocate the preferred poison address from the OS.
|
|
|
|
uintptr_t candidate = (0xF0DEAFFF & ~(PageSize() - 1));
|
|
|
|
void* result = ReserveRegion(candidate, false);
|
|
|
|
if (result == reinterpret_cast<void*>(candidate)) {
|
|
|
|
// success - inaccessible page allocated
|
|
|
|
printf("INFO | poison area allocated at 0x%.*" PRIxPTR
|
|
|
|
" (preferred addr)\n", SIZxPTR, reinterpret_cast<uintptr_t>(result));
|
|
|
|
return candidate;
|
|
|
|
}
|
2009-11-30 19:51:07 +03:00
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
// That didn't work, so see if the preferred address is within a range
|
|
|
|
// of permanently inacessible memory.
|
|
|
|
if (ProbeRegion(candidate)) {
|
|
|
|
// success - selected page cannot be usable memory
|
2009-11-30 19:51:07 +03:00
|
|
|
if (result != MAP_FAILED) {
|
2014-07-23 07:54:41 +04:00
|
|
|
ReleaseRegion(result);
|
2009-11-30 19:51:07 +03:00
|
|
|
}
|
2014-07-23 07:54:41 +04:00
|
|
|
printf("INFO | poison area assumed at 0x%.*" PRIxPTR
|
|
|
|
" (preferred addr)\n", SIZxPTR, candidate);
|
|
|
|
return candidate;
|
|
|
|
}
|
2009-11-30 19:51:07 +03:00
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
// The preferred address is already in use. Did the OS give us a
|
|
|
|
// consolation prize?
|
|
|
|
if (result != MAP_FAILED) {
|
|
|
|
uintptr_t ures = reinterpret_cast<uintptr_t>(result);
|
|
|
|
printf("INFO | poison area allocated at 0x%.*" PRIxPTR
|
|
|
|
" (consolation prize)\n", SIZxPTR, ures);
|
|
|
|
return ures;
|
|
|
|
}
|
2009-11-17 22:17:20 +03:00
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
// It didn't, so try to allocate again, without any constraint on
|
|
|
|
// the address.
|
|
|
|
result = ReserveRegion(0, false);
|
|
|
|
if (result != MAP_FAILED) {
|
|
|
|
uintptr_t ures = reinterpret_cast<uintptr_t>(result);
|
|
|
|
printf("INFO | poison area allocated at 0x%.*" PRIxPTR
|
|
|
|
" (fallback)\n", SIZxPTR, ures);
|
|
|
|
return ures;
|
2009-11-17 22:17:20 +03:00
|
|
|
}
|
2014-07-23 07:54:41 +04:00
|
|
|
|
|
|
|
printf("ERROR | no usable poison area found\n");
|
|
|
|
return 0;
|
2009-11-17 22:17:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The "positive control" area confirms that we can allocate a page with the
|
|
|
|
* proper characteristics.
|
|
|
|
*/
|
|
|
|
static uintptr_t
|
|
|
|
ReservePositiveControl()
|
|
|
|
{
|
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
void* result = ReserveRegion(0, false);
|
2009-11-17 22:17:20 +03:00
|
|
|
if (result == MAP_FAILED) {
|
|
|
|
printf("ERROR | allocating positive control | %s\n", LastErrMsg());
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-15 22:01:08 +04:00
|
|
|
printf("INFO | positive control allocated at 0x%.*" PRIxPTR "\n",
|
2009-11-17 22:17:20 +03:00
|
|
|
SIZxPTR, (uintptr_t)result);
|
|
|
|
return (uintptr_t)result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The "negative control" area confirms that our probe logic does detect a
|
|
|
|
* page that is readable, writable, or executable.
|
|
|
|
*/
|
|
|
|
static uintptr_t
|
|
|
|
ReserveNegativeControl()
|
|
|
|
{
|
2014-07-23 07:54:41 +04:00
|
|
|
void* result = ReserveRegion(0, true);
|
2009-11-17 22:17:20 +03:00
|
|
|
if (result == MAP_FAILED) {
|
|
|
|
printf("ERROR | allocating negative control | %s\n", LastErrMsg());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill the page with return instructions.
|
2014-07-23 07:54:41 +04:00
|
|
|
RETURN_INSTR_TYPE* p = reinterpret_cast<RETURN_INSTR_TYPE*>(result);
|
|
|
|
RETURN_INSTR_TYPE* limit =
|
|
|
|
reinterpret_cast<RETURN_INSTR_TYPE*>(
|
|
|
|
reinterpret_cast<char*>(result) + PageSize());
|
|
|
|
while (p < limit) {
|
2009-11-17 22:17:20 +03:00
|
|
|
*p++ = RETURN_INSTR;
|
2014-07-23 07:54:41 +04:00
|
|
|
}
|
2009-11-17 22:17:20 +03:00
|
|
|
|
|
|
|
// Now mark it executable as well as readable and writable.
|
|
|
|
// (mmap(PROT_EXEC) may fail when applied to anonymous memory.)
|
|
|
|
|
|
|
|
if (MakeRegionExecutable(result)) {
|
|
|
|
printf("ERROR | making negative control executable | %s\n", LastErrMsg());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-15 22:01:08 +04:00
|
|
|
printf("INFO | negative control allocated at 0x%.*" PRIxPTR "\n",
|
2009-11-17 22:17:20 +03:00
|
|
|
SIZxPTR, (uintptr_t)result);
|
|
|
|
return (uintptr_t)result;
|
|
|
|
}
|
|
|
|
|
2010-11-11 11:19:11 +03:00
|
|
|
static void
|
2014-07-23 07:54:41 +04:00
|
|
|
JumpTo(uintptr_t aOpaddr)
|
2010-11-11 11:19:11 +03:00
|
|
|
{
|
|
|
|
#ifdef __ia64
|
2014-07-23 07:54:41 +04:00
|
|
|
struct func_call
|
|
|
|
{
|
|
|
|
uintptr_t mFunc;
|
|
|
|
uintptr_t mGp;
|
|
|
|
} call = { aOpaddr, };
|
2010-11-11 11:19:11 +03:00
|
|
|
((void (*)())&call)();
|
|
|
|
#else
|
2014-07-23 07:54:41 +04:00
|
|
|
((void (*)())aOpaddr)();
|
2010-11-11 11:19:11 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-01-06 02:29:08 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
static BOOL
|
2014-07-23 07:54:41 +04:00
|
|
|
IsBadExecPtr(uintptr_t aPtr)
|
2010-01-06 02:29:08 +03:00
|
|
|
{
|
|
|
|
BOOL ret = false;
|
|
|
|
|
2016-02-07 22:50:50 +03:00
|
|
|
#ifdef _MSC_VER
|
2010-01-06 02:29:08 +03:00
|
|
|
__try {
|
2014-07-23 07:54:41 +04:00
|
|
|
JumpTo(aPtr);
|
2010-01-06 02:29:08 +03:00
|
|
|
} __except (EXCEPTION_EXECUTE_HANDLER) {
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
#else
|
2016-02-07 22:50:50 +03:00
|
|
|
printf("INFO | exec test not supported on MinGW build\n");
|
2010-01-06 02:29:08 +03:00
|
|
|
// We do our best
|
2014-07-23 07:54:41 +04:00
|
|
|
ret = IsBadReadPtr((const void*)aPtr, 1);
|
2010-01-06 02:29:08 +03:00
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-11-17 22:17:20 +03:00
|
|
|
/* Test each page. */
|
|
|
|
static bool
|
2014-07-23 07:54:41 +04:00
|
|
|
TestPage(const char* aPageLabel, uintptr_t aPageAddr, int aShouldSucceed)
|
2009-11-17 22:17:20 +03:00
|
|
|
{
|
2014-07-23 07:54:41 +04:00
|
|
|
const char* oplabel;
|
2009-11-17 22:17:20 +03:00
|
|
|
uintptr_t opaddr;
|
|
|
|
|
|
|
|
bool failed = false;
|
|
|
|
for (unsigned int test = 0; test < 3; test++) {
|
|
|
|
switch (test) {
|
|
|
|
// The execute test must be done before the write test, because the
|
|
|
|
// write test will clobber memory at the target address.
|
2014-07-23 07:54:41 +04:00
|
|
|
case 0: oplabel = "reading"; opaddr = aPageAddr + PageSize()/2 - 1; break;
|
|
|
|
case 1: oplabel = "executing"; opaddr = aPageAddr + PageSize()/2; break;
|
|
|
|
case 2: oplabel = "writing"; opaddr = aPageAddr + PageSize()/2 - 1; break;
|
2009-11-17 22:17:20 +03:00
|
|
|
default: abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2010-01-06 02:29:08 +03:00
|
|
|
BOOL badptr;
|
2009-12-30 01:33:09 +03:00
|
|
|
|
2010-01-06 02:29:08 +03:00
|
|
|
switch (test) {
|
|
|
|
case 0: badptr = IsBadReadPtr((const void*)opaddr, 1); break;
|
|
|
|
case 1: badptr = IsBadExecPtr(opaddr); break;
|
|
|
|
case 2: badptr = IsBadWritePtr((void*)opaddr, 1); break;
|
|
|
|
default: abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (badptr) {
|
2014-07-23 07:54:41 +04:00
|
|
|
if (aShouldSucceed) {
|
|
|
|
printf("TEST-UNEXPECTED-FAIL | %s %s\n", oplabel, aPageLabel);
|
2009-12-29 22:24:43 +03:00
|
|
|
failed = true;
|
2010-01-06 02:29:08 +03:00
|
|
|
} else {
|
2014-07-23 07:54:41 +04:00
|
|
|
printf("TEST-PASS | %s %s\n", oplabel, aPageLabel);
|
2009-11-17 22:17:20 +03:00
|
|
|
}
|
2010-01-06 02:29:08 +03:00
|
|
|
} else {
|
|
|
|
// if control reaches this point the probe succeeded
|
2014-07-23 07:54:41 +04:00
|
|
|
if (aShouldSucceed) {
|
|
|
|
printf("TEST-PASS | %s %s\n", oplabel, aPageLabel);
|
2009-12-30 01:33:09 +03:00
|
|
|
} else {
|
2014-07-23 07:54:41 +04:00
|
|
|
printf("TEST-UNEXPECTED-FAIL | %s %s\n", oplabel, aPageLabel);
|
2010-01-06 02:29:08 +03:00
|
|
|
failed = true;
|
2009-12-30 01:33:09 +03:00
|
|
|
}
|
2009-11-17 22:17:20 +03:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == -1) {
|
2014-07-23 07:54:41 +04:00
|
|
|
printf("ERROR | %s %s | fork=%s\n", oplabel, aPageLabel,
|
2009-11-17 22:17:20 +03:00
|
|
|
LastErrMsg());
|
|
|
|
exit(2);
|
|
|
|
} else if (pid == 0) {
|
2009-11-20 09:11:42 +03:00
|
|
|
volatile unsigned char scratch;
|
2009-11-17 22:17:20 +03:00
|
|
|
switch (test) {
|
2014-07-23 07:54:41 +04:00
|
|
|
case 0: scratch = *(volatile unsigned char*)opaddr; break;
|
2010-11-11 11:19:11 +03:00
|
|
|
case 1: JumpTo(opaddr); break;
|
2014-07-23 07:54:41 +04:00
|
|
|
case 2: *(volatile unsigned char*)opaddr = 0; break;
|
2009-11-17 22:17:20 +03:00
|
|
|
default: abort();
|
|
|
|
}
|
2011-12-20 07:46:39 +04:00
|
|
|
(void)scratch;
|
2009-11-17 22:17:20 +03:00
|
|
|
_exit(0);
|
|
|
|
} else {
|
|
|
|
int status;
|
|
|
|
if (waitpid(pid, &status, 0) != pid) {
|
2014-07-23 07:54:41 +04:00
|
|
|
printf("ERROR | %s %s | wait=%s\n", oplabel, aPageLabel,
|
2009-11-17 22:17:20 +03:00
|
|
|
LastErrMsg());
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
|
2014-07-23 07:54:41 +04:00
|
|
|
if (aShouldSucceed) {
|
|
|
|
printf("TEST-PASS | %s %s\n", oplabel, aPageLabel);
|
2009-11-17 22:17:20 +03:00
|
|
|
} else {
|
2010-03-11 21:04:55 +03:00
|
|
|
printf("TEST-UNEXPECTED-FAIL | %s %s | unexpected successful exit\n",
|
2014-07-23 07:54:41 +04:00
|
|
|
oplabel, aPageLabel);
|
2009-11-17 22:17:20 +03:00
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
} else if (WIFEXITED(status)) {
|
|
|
|
printf("ERROR | %s %s | unexpected exit code %d\n",
|
2014-07-23 07:54:41 +04:00
|
|
|
oplabel, aPageLabel, WEXITSTATUS(status));
|
2009-11-17 22:17:20 +03:00
|
|
|
exit(2);
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
2014-07-23 07:54:41 +04:00
|
|
|
if (aShouldSucceed) {
|
2010-03-11 21:04:55 +03:00
|
|
|
printf("TEST-UNEXPECTED-FAIL | %s %s | unexpected signal %d\n",
|
2014-07-23 07:54:41 +04:00
|
|
|
oplabel, aPageLabel, WTERMSIG(status));
|
2009-11-17 22:17:20 +03:00
|
|
|
failed = true;
|
|
|
|
} else {
|
2010-03-11 21:04:55 +03:00
|
|
|
printf("TEST-PASS | %s %s | signal %d (as expected)\n",
|
2014-07-23 07:54:41 +04:00
|
|
|
oplabel, aPageLabel, WTERMSIG(status));
|
2009-11-17 22:17:20 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("ERROR | %s %s | unexpected exit status %d\n",
|
2014-07-23 07:54:41 +04:00
|
|
|
oplabel, aPageLabel, status);
|
2009-11-17 22:17:20 +03:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2014-06-14 23:47:01 +04:00
|
|
|
GetSystemInfo(&sInfo_);
|
2014-02-11 02:57:01 +04:00
|
|
|
#else
|
2014-07-23 07:54:41 +04:00
|
|
|
gUnixPageSize = sysconf(_SC_PAGESIZE);
|
2009-11-17 22:17:20 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
uintptr_t ncontrol = ReserveNegativeControl();
|
|
|
|
uintptr_t pcontrol = ReservePositiveControl();
|
|
|
|
uintptr_t poison = ReservePoisonArea();
|
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
if (!ncontrol || !pcontrol || !poison) {
|
2009-11-17 22:17:20 +03:00
|
|
|
return 2;
|
2014-07-23 07:54:41 +04:00
|
|
|
}
|
2009-11-17 22:17:20 +03:00
|
|
|
|
|
|
|
bool failed = false;
|
|
|
|
failed |= TestPage("negative control", ncontrol, 1);
|
|
|
|
failed |= TestPage("positive control", pcontrol, 0);
|
|
|
|
failed |= TestPage("poison area", poison, 0);
|
|
|
|
|
|
|
|
return failed ? 1 : 0;
|
|
|
|
}
|