Files
breakpad/src/processor/exploitability_linux.cc
Liu.andrew.x@gmail.com cca153368a Checking location of the instruction pointer to see if it is
in valid code for Linux exploitability rating.

This CL adds to the Linux exploitability checker by verifying that the
instruction pointer is in valid code. Verification is done by obtaining a
memory mapping of the crash and checking if the instruction pointer lies in
an executable region. If there is no memory mapping, the instruction pointer
is checked to determine if it lies within a known module.

R=ivanpe@chromium.org

Review URL: https://codereview.chromium.org/1210493003

git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@1464 4c0a9323-5329-0410-9bdc-e9ce6186880e
2015-06-25 23:05:16 +00:00

146 lines
5.6 KiB
C++

// Copyright (c) 2013 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// exploitability_linux.cc: Linux specific exploitability engine.
//
// Provides a guess at the exploitability of the crash for the Linux
// platform given a minidump and process_state.
//
// Author: Matthew Riley
#include "processor/exploitability_linux.h"
#include "google_breakpad/processor/process_state.h"
#include "google_breakpad/processor/call_stack.h"
#include "google_breakpad/processor/stack_frame.h"
#include "processor/logging.h"
namespace {
// This function in libc is called if the program was compiled with
// -fstack-protector and a function's stack canary changes.
const char kStackCheckFailureFunction[] = "__stack_chk_fail";
// This function in libc is called if the program was compiled with
// -D_FORTIFY_SOURCE=2, a function like strcpy() is called, and the runtime
// can determine that the call would overflow the target buffer.
const char kBoundsCheckFailureFunction[] = "__chk_fail";
} // namespace
namespace google_breakpad {
ExploitabilityLinux::ExploitabilityLinux(Minidump *dump,
ProcessState *process_state)
: Exploitability(dump, process_state) { }
ExploitabilityRating ExploitabilityLinux::CheckPlatformExploitability() {
// Check the crashing thread for functions suggesting a buffer overflow or
// stack smash.
if (process_state_->requesting_thread() != -1) {
CallStack* crashing_thread =
process_state_->threads()->at(process_state_->requesting_thread());
const vector<StackFrame*>& crashing_thread_frames =
*crashing_thread->frames();
for (size_t i = 0; i < crashing_thread_frames.size(); ++i) {
if (crashing_thread_frames[i]->function_name ==
kStackCheckFailureFunction) {
return EXPLOITABILITY_HIGH;
}
if (crashing_thread_frames[i]->function_name ==
kBoundsCheckFailureFunction) {
return EXPLOITABILITY_HIGH;
}
}
}
// Check if the instruction pointer is in a valid instruction region
// by finding if it maps to an executable part of memory.
uint64_t instruction_ptr = 0;
// Getting exception data. (It should exist for all minidumps.)
MinidumpException *exception = dump_->GetException();
if (exception == NULL) {
BPLOG(INFO) << "No exception record.";
return EXPLOITABILITY_ERR_PROCESSING;
}
const MinidumpContext *context = exception->GetContext();
if (context == NULL) {
BPLOG(INFO) << "No exception context.";
return EXPLOITABILITY_ERR_PROCESSING;
}
// Getting instruction pointer based off architecture.
uint32_t architecture = context->GetContextCPU();
switch (architecture) {
case MD_CONTEXT_X86:
instruction_ptr = context->GetContextX86()->eip;
break;
case MD_CONTEXT_AMD64:
instruction_ptr = context->GetContextAMD64()->rip;
break;
default:
// TODO(liuandrew): Add support ARM and arm64 architectures.
BPLOG(INFO) << "Unsupported architecture.";
return EXPLOITABILITY_ERR_PROCESSING;
}
if (!this->InstructionPointerInCode(instruction_ptr)) {
return EXPLOITABILITY_HIGH;
}
return EXPLOITABILITY_NONE;
}
bool ExploitabilityLinux::InstructionPointerInCode(uint64_t instruction_ptr) {
// Here we get memory mapping. Most minidumps will not contain a memory
// mapping, so we will commonly resort to checking modules.
MinidumpMemoryInfoList *mem_info_list = dump_->GetMemoryInfoList();
const MinidumpMemoryInfo *mem_info =
mem_info_list ?
mem_info_list->GetMemoryInfoForAddress(instruction_ptr) : NULL;
// Checking if the memory mapping at the instruction pointer is executable.
// If there is no memory mapping, we will use the modules as reference.
if (mem_info != NULL) {
return mem_info->IsExecutable();
}
// If the memory mapping retrieval fails, we will check the modules
// to see if the instruction pointer is inside a module.
// TODO(liuandrew): Check if the instruction pointer lies in an executable
// region within the module.
MinidumpModuleList *minidump_module_list = dump_->GetModuleList();
return !minidump_module_list ||
minidump_module_list->GetModuleForAddress(instruction_ptr);
}
} // namespace google_breakpad