toolkit/crashreporter/google-breakpad/src/common/linux/elfutils.cc

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 // Copyright (c) 2012, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29
michael@0 30 #include "common/linux/elfutils.h"
michael@0 31
michael@0 32 #include <assert.h>
michael@0 33 #include <string.h>
michael@0 34
michael@0 35 #include "common/linux/linux_libc_support.h"
michael@0 36 #include "common/linux/elfutils-inl.h"
michael@0 37
michael@0 38 namespace google_breakpad {
michael@0 39
michael@0 40 namespace {
michael@0 41
michael@0 42 template<typename ElfClass>
michael@0 43 void FindElfClassSection(const char *elf_base,
michael@0 44 const char *section_name,
michael@0 45 typename ElfClass::Word section_type,
michael@0 46 const void **section_start,
michael@0 47 int *section_size) {
michael@0 48 typedef typename ElfClass::Ehdr Ehdr;
michael@0 49 typedef typename ElfClass::Shdr Shdr;
michael@0 50
michael@0 51 assert(elf_base);
michael@0 52 assert(section_start);
michael@0 53 assert(section_size);
michael@0 54
michael@0 55 assert(my_strncmp(elf_base, ELFMAG, SELFMAG) == 0);
michael@0 56
michael@0 57 const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base);
michael@0 58 assert(elf_header->e_ident[EI_CLASS] == ElfClass::kClass);
michael@0 59
michael@0 60 const Shdr* sections =
michael@0 61 GetOffset<ElfClass,Shdr>(elf_header, elf_header->e_shoff);
michael@0 62 const Shdr* section_names = sections + elf_header->e_shstrndx;
michael@0 63 const char* names =
michael@0 64 GetOffset<ElfClass,char>(elf_header, section_names->sh_offset);
michael@0 65 const char *names_end = names + section_names->sh_size;
michael@0 66
michael@0 67 const Shdr* section =
michael@0 68 FindElfSectionByName<ElfClass>(section_name, section_type,
michael@0 69 sections, names, names_end,
michael@0 70 elf_header->e_shnum);
michael@0 71
michael@0 72 if (section != NULL && section->sh_size > 0) {
michael@0 73 *section_start = elf_base + section->sh_offset;
michael@0 74 *section_size = section->sh_size;
michael@0 75 }
michael@0 76 }
michael@0 77
michael@0 78 template<typename ElfClass>
michael@0 79 void FindElfClassSegment(const char *elf_base,
michael@0 80 typename ElfClass::Word segment_type,
michael@0 81 const void **segment_start,
michael@0 82 int *segment_size) {
michael@0 83 typedef typename ElfClass::Ehdr Ehdr;
michael@0 84 typedef typename ElfClass::Phdr Phdr;
michael@0 85
michael@0 86 assert(elf_base);
michael@0 87 assert(segment_start);
michael@0 88 assert(segment_size);
michael@0 89
michael@0 90 assert(my_strncmp(elf_base, ELFMAG, SELFMAG) == 0);
michael@0 91
michael@0 92 const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base);
michael@0 93 assert(elf_header->e_ident[EI_CLASS] == ElfClass::kClass);
michael@0 94
michael@0 95 const Phdr* phdrs =
michael@0 96 GetOffset<ElfClass,Phdr>(elf_header, elf_header->e_phoff);
michael@0 97
michael@0 98 for (int i = 0; i < elf_header->e_phnum; ++i) {
michael@0 99 if (phdrs[i].p_type == segment_type) {
michael@0 100 *segment_start = elf_base + phdrs[i].p_offset;
michael@0 101 *segment_size = phdrs[i].p_filesz;
michael@0 102 return;
michael@0 103 }
michael@0 104 }
michael@0 105 }
michael@0 106
michael@0 107 } // namespace
michael@0 108
michael@0 109 bool IsValidElf(const void* elf_base) {
michael@0 110 return my_strncmp(reinterpret_cast<const char*>(elf_base),
michael@0 111 ELFMAG, SELFMAG) == 0;
michael@0 112 }
michael@0 113
michael@0 114 int ElfClass(const void* elf_base) {
michael@0 115 const ElfW(Ehdr)* elf_header =
michael@0 116 reinterpret_cast<const ElfW(Ehdr)*>(elf_base);
michael@0 117
michael@0 118 return elf_header->e_ident[EI_CLASS];
michael@0 119 }
michael@0 120
michael@0 121 bool FindElfSection(const void *elf_mapped_base,
michael@0 122 const char *section_name,
michael@0 123 uint32_t section_type,
michael@0 124 const void **section_start,
michael@0 125 int *section_size,
michael@0 126 int *elfclass) {
michael@0 127 assert(elf_mapped_base);
michael@0 128 assert(section_start);
michael@0 129 assert(section_size);
michael@0 130
michael@0 131 *section_start = NULL;
michael@0 132 *section_size = 0;
michael@0 133
michael@0 134 if (!IsValidElf(elf_mapped_base))
michael@0 135 return false;
michael@0 136
michael@0 137 int cls = ElfClass(elf_mapped_base);
michael@0 138 if (elfclass) {
michael@0 139 *elfclass = cls;
michael@0 140 }
michael@0 141
michael@0 142 const char* elf_base =
michael@0 143 static_cast<const char*>(elf_mapped_base);
michael@0 144
michael@0 145 if (cls == ELFCLASS32) {
michael@0 146 FindElfClassSection<ElfClass32>(elf_base, section_name, section_type,
michael@0 147 section_start, section_size);
michael@0 148 return *section_start != NULL;
michael@0 149 } else if (cls == ELFCLASS64) {
michael@0 150 FindElfClassSection<ElfClass64>(elf_base, section_name, section_type,
michael@0 151 section_start, section_size);
michael@0 152 return *section_start != NULL;
michael@0 153 }
michael@0 154
michael@0 155 return false;
michael@0 156 }
michael@0 157
michael@0 158 bool FindElfSegment(const void *elf_mapped_base,
michael@0 159 uint32_t segment_type,
michael@0 160 const void **segment_start,
michael@0 161 int *segment_size,
michael@0 162 int *elfclass) {
michael@0 163 assert(elf_mapped_base);
michael@0 164 assert(segment_start);
michael@0 165 assert(segment_size);
michael@0 166
michael@0 167 *segment_start = NULL;
michael@0 168 *segment_size = 0;
michael@0 169
michael@0 170 if (!IsValidElf(elf_mapped_base))
michael@0 171 return false;
michael@0 172
michael@0 173 int cls = ElfClass(elf_mapped_base);
michael@0 174 if (elfclass) {
michael@0 175 *elfclass = cls;
michael@0 176 }
michael@0 177
michael@0 178 const char* elf_base =
michael@0 179 static_cast<const char*>(elf_mapped_base);
michael@0 180
michael@0 181 if (cls == ELFCLASS32) {
michael@0 182 FindElfClassSegment<ElfClass32>(elf_base, segment_type,
michael@0 183 segment_start, segment_size);
michael@0 184 return *segment_start != NULL;
michael@0 185 } else if (cls == ELFCLASS64) {
michael@0 186 FindElfClassSegment<ElfClass64>(elf_base, segment_type,
michael@0 187 segment_start, segment_size);
michael@0 188 return *segment_start != NULL;
michael@0 189 }
michael@0 190
michael@0 191 return false;
michael@0 192 }
michael@0 193
michael@0 194 } // namespace google_breakpad

mercurial