aboutsummaryrefslogtreecommitdiff
path: root/src/processor/exploitability_linux.cc
blob: c1c291ce3103d6f9e536b8539d1be0b8952e8612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// 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 <elf.h>

#include "google_breakpad/common/minidump_exception_linux.h"
#include "google_breakpad/processor/call_stack.h"
#include "google_breakpad/processor/process_state.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;
      }
    }
  }

  // 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 MDRawExceptionStream *raw_exception_stream = exception->exception();
  if (raw_exception_stream == NULL) {
    BPLOG(INFO) << "No raw exception stream.";
    return EXPLOITABILITY_ERR_PROCESSING;
  }

  // Checking for benign exceptions that caused the crash.
  if (this->BenignCrashTrigger(raw_exception_stream)) {
    return EXPLOITABILITY_NONE;
  }

  // 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;

  const MinidumpContext *context = exception->GetContext();
  if (context == NULL) {
    BPLOG(INFO) << "No exception context.";
    return EXPLOITABILITY_ERR_PROCESSING;
  }

  if (this->ArchitectureType() == UNSUPPORTED_ARCHITECTURE) {
    BPLOG(INFO) << "Unsupported architecture.";
    return EXPLOITABILITY_ERR_PROCESSING;
  }
  // Getting the instruction pointer.
  if (!context->GetInstructionPointer(&instruction_ptr)) {
    BPLOG(INFO) << "Failed to retrieve instruction pointer.";
    return EXPLOITABILITY_ERR_PROCESSING;
  }

  // Checking for the instruction pointer in a valid instruction region.
  if (!this->InstructionPointerInCode(instruction_ptr)) {
    return EXPLOITABILITY_HIGH;
  }

  // There was no strong evidence suggesting exploitability, but the minidump
  // does not appear totally benign either.
  return EXPLOITABILITY_INTERESTING;
}

LinuxArchitectureType ExploitabilityLinux::ArchitectureType() {
  // GetContextCPU() should have already been successfully called before
  // calling this method. Thus there should be a raw exception stream for
  // the minidump.
  MinidumpException *exception = dump_->GetException();
  const DumpContext *dump_context =
      exception ?
      exception->GetContext() : NULL;
  if (dump_context == NULL) {
    BPLOG(INFO) << "No raw dump context.";
    return UNSUPPORTED_ARCHITECTURE;
  }

  // Check the architecture type.
  switch (dump_context->GetContextCPU()) {
    case MD_CONTEXT_ARM:
    case MD_CONTEXT_X86:
      return LINUX_32_BIT;
    case MD_CONTEXT_ARM64:
    case MD_CONTEXT_AMD64:
      return LINUX_64_BIT;
    default:
      // This should not happen. The four architectures above should be
      // the only Linux architectures.
      BPLOG(INFO) << "Unsupported architecture.";
      return UNSUPPORTED_ARCHITECTURE;
  }
}

bool ExploitabilityLinux::InstructionPointerInCode(uint64_t instruction_ptr) {
  // Get memory mapping. Most minidumps will not contain a memory
  // mapping, so processing 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;

  // Check if the memory mapping at the instruction pointer is executable.
  // If there is no memory mapping, processing will use modules as reference.
  if (mem_info != NULL) {
    return mem_info->IsExecutable();
  }

  // If the memory mapping retrieval fails, check the modules
  // to see if the instruction pointer is inside a module.
  MinidumpModuleList *minidump_module_list = dump_->GetModuleList();
  const MinidumpModule *minidump_module =
      minidump_module_list ?
      minidump_module_list->GetModuleForAddress(instruction_ptr) : NULL;

  // If the instruction pointer isn't in a module, return false.
  if (minidump_module == NULL) {
    return false;
  }

  // Get ELF header data from the instruction pointer's module.
  const uint64_t base_address = minidump_module->base_address();
  MinidumpMemoryList *memory_list = dump_->GetMemoryList();
  MinidumpMemoryRegion *memory_region =
      memory_list ?
      memory_list->GetMemoryRegionForAddress(base_address) : NULL;

  // The minidump does not have the correct memory region.
  // This returns true because even though there is no memory data available,
  // the evidence so far suggests that the instruction pointer is not at a
  // bad location.
  if (memory_region == NULL) {
    return true;
  }

  // Examine ELF headers. Depending on the architecture, the size of the
  // ELF headers can differ.
  LinuxArchitectureType architecture =  this->ArchitectureType();
  if (architecture == LINUX_32_BIT) {
    // Check if the ELF header is within the memory region and if the
    // instruction pointer lies within the ELF header.
    if (memory_region->GetSize() < sizeof(Elf32_Ehdr) ||
        instruction_ptr < base_address + sizeof(Elf32_Ehdr)) {
      return false;
    }
    // Load 32-bit ELF header.
    Elf32_Ehdr header;
    this->LoadElfHeader(memory_region, base_address, &header);
    // Check if the program header table is within the memory region, and
    // validate that the program header entry size is correct.
    if (header.e_phentsize != sizeof(Elf32_Phdr) ||
        memory_region->GetSize() <
        header.e_phoff +
        ((uint64_t) header.e_phentsize * (uint64_t) header.e_phnum)) {
      return false;
    }
    // Load 32-bit Program Header Table.
    scoped_array<Elf32_Phdr> program_headers(new Elf32_Phdr[header.e_phnum]);
    this->LoadElfHeaderTable(memory_region,
                             base_address + header.e_phoff,
                             header.e_phnum,
                             program_headers.get());
    // Find correct program header that corresponds to the instruction pointer.
    for (int i = 0; i < header.e_phnum; i++) {
      const Elf32_Phdr& program_header = program_headers[i];
      // Check if instruction pointer lies within this program header's region.
      if (instruction_ptr >= program_header.p_vaddr &&
          instruction_ptr < program_header.p_vaddr + program_header.p_memsz) {
        // Return whether this program header region is executable.
        return program_header.p_flags & PF_X;
      }
    }
  } else if (architecture == LINUX_64_BIT) {
    // Check if the ELF header is within the memory region and if the
    // instruction pointer lies within the ELF header.
    if (memory_region->GetSize() < sizeof(Elf64_Ehdr) ||
        instruction_ptr < base_address + sizeof(Elf64_Ehdr)) {
      return false;
    }
    // Load 64-bit ELF header.
    Elf64_Ehdr header;
    this->LoadElfHeader(memory_region, base_address, &header);
    // Check if the program header table is within the memory region, and
    // validate that the program header entry size is correct.
    if (header.e_phentsize != sizeof(Elf64_Phdr) ||
        memory_region->GetSize() <
        header.e_phoff +
        ((uint64_t) header.e_phentsize * (uint64_t) header.e_phnum)) {
      return false;
    }
    // Load 64-bit Program Header Table.
    scoped_array<Elf64_Phdr> program_headers(new Elf64_Phdr[header.e_phnum]);
    this->LoadElfHeaderTable(memory_region,
                             base_address + header.e_phoff,
                             header.e_phnum,
                             program_headers.get());
    // Find correct program header that corresponds to the instruction pointer.
    for (int i = 0; i < header.e_phnum; i++) {
      const Elf64_Phdr& program_header = program_headers[i];
      // Check if instruction pointer lies within this program header's region.
      if (instruction_ptr >= program_header.p_vaddr &&
          instruction_ptr < program_header.p_vaddr + program_header.p_memsz) {
        // Return whether this program header region is executable.
        return program_header.p_flags & PF_X;
      }
    }
  }

  // The instruction pointer was not in an area identified by the ELF headers.
  return false;
}

bool ExploitabilityLinux::BenignCrashTrigger(const MDRawExceptionStream
                                                  *raw_exception_stream) {
  // Check the cause of crash.
  // If the exception of the crash is a benign exception,
  // it is probably not exploitable.
  switch (raw_exception_stream->exception_record.exception_code) {
    case MD_EXCEPTION_CODE_LIN_SIGHUP:
    case MD_EXCEPTION_CODE_LIN_SIGINT:
    case MD_EXCEPTION_CODE_LIN_SIGQUIT:
    case MD_EXCEPTION_CODE_LIN_SIGTRAP:
    case MD_EXCEPTION_CODE_LIN_SIGABRT:
    case MD_EXCEPTION_CODE_LIN_SIGFPE:
    case MD_EXCEPTION_CODE_LIN_SIGKILL:
    case MD_EXCEPTION_CODE_LIN_SIGUSR1:
    case MD_EXCEPTION_CODE_LIN_SIGUSR2:
    case MD_EXCEPTION_CODE_LIN_SIGPIPE:
    case MD_EXCEPTION_CODE_LIN_SIGALRM:
    case MD_EXCEPTION_CODE_LIN_SIGTERM:
    case MD_EXCEPTION_CODE_LIN_SIGCHLD:
    case MD_EXCEPTION_CODE_LIN_SIGCONT:
    case MD_EXCEPTION_CODE_LIN_SIGSTOP:
    case MD_EXCEPTION_CODE_LIN_SIGTSTP:
    case MD_EXCEPTION_CODE_LIN_SIGTTIN:
    case MD_EXCEPTION_CODE_LIN_SIGTTOU:
    case MD_EXCEPTION_CODE_LIN_SIGURG:
    case MD_EXCEPTION_CODE_LIN_SIGXCPU:
    case MD_EXCEPTION_CODE_LIN_SIGXFSZ:
    case MD_EXCEPTION_CODE_LIN_SIGVTALRM:
    case MD_EXCEPTION_CODE_LIN_SIGPROF:
    case MD_EXCEPTION_CODE_LIN_SIGWINCH:
    case MD_EXCEPTION_CODE_LIN_SIGIO:
    case MD_EXCEPTION_CODE_LIN_SIGPWR:
    case MD_EXCEPTION_CODE_LIN_SIGSYS:
    case MD_EXCEPTION_CODE_LIN_DUMP_REQUESTED:
      return true;
      break;
    default:
      return false;
      break;
  }
}

}  // namespace google_breakpad