toolkit/crashreporter/google-breakpad/src/common/solaris/file_id.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) 2007, 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 // file_id.cc: Return a unique identifier for a file
michael@0 31 //
michael@0 32 // See file_id.h for documentation
michael@0 33 //
michael@0 34 // Author: Alfred Peng
michael@0 35
michael@0 36 #include <elf.h>
michael@0 37 #include <fcntl.h>
michael@0 38 #include <gelf.h>
michael@0 39 #include <sys/mman.h>
michael@0 40 #include <sys/ksyms.h>
michael@0 41 #include <stdio.h>
michael@0 42 #include <string.h>
michael@0 43 #include <unistd.h>
michael@0 44
michael@0 45 #include <cassert>
michael@0 46 #include <cstdio>
michael@0 47
michael@0 48 #include "common/md5.h"
michael@0 49 #include "common/solaris/file_id.h"
michael@0 50 #include "common/solaris/message_output.h"
michael@0 51 #include "google_breakpad/common/minidump_format.h"
michael@0 52
michael@0 53 namespace google_breakpad {
michael@0 54
michael@0 55 class AutoElfEnder {
michael@0 56 public:
michael@0 57 AutoElfEnder(Elf *elf) : elf_(elf) {}
michael@0 58 ~AutoElfEnder() { if (elf_) elf_end(elf_); }
michael@0 59 private:
michael@0 60 Elf *elf_;
michael@0 61 };
michael@0 62
michael@0 63 // Find the text section in elf object file.
michael@0 64 // Return the section start address and the size.
michael@0 65 static bool FindElfTextSection(int fd, const void *elf_base,
michael@0 66 const void **text_start,
michael@0 67 int *text_size) {
michael@0 68 assert(text_start);
michael@0 69 assert(text_size);
michael@0 70
michael@0 71 *text_start = NULL;
michael@0 72 *text_size = 0;
michael@0 73
michael@0 74 if (elf_version(EV_CURRENT) == EV_NONE) {
michael@0 75 print_message2(2, "elf_version() failed: %s\n", elf_errmsg(0));
michael@0 76 return false;
michael@0 77 }
michael@0 78
michael@0 79 GElf_Ehdr elf_header;
michael@0 80 lseek(fd, 0L, 0);
michael@0 81 Elf *elf = elf_begin(fd, ELF_C_READ, NULL);
michael@0 82 AutoElfEnder elfEnder(elf);
michael@0 83
michael@0 84 if (gelf_getehdr(elf, &elf_header) == (GElf_Ehdr *)NULL) {
michael@0 85 print_message2(2, "failed to read elf header: %s\n", elf_errmsg(-1));
michael@0 86 return false;
michael@0 87 }
michael@0 88
michael@0 89 if (elf_header.e_ident[EI_MAG0] != ELFMAG0 ||
michael@0 90 elf_header.e_ident[EI_MAG1] != ELFMAG1 ||
michael@0 91 elf_header.e_ident[EI_MAG2] != ELFMAG2 ||
michael@0 92 elf_header.e_ident[EI_MAG3] != ELFMAG3) {
michael@0 93 print_message1(2, "header magic doesn't match\n");
michael@0 94 return false;
michael@0 95 }
michael@0 96
michael@0 97 static const char kTextSectionName[] = ".text";
michael@0 98 const GElf_Shdr *text_section = NULL;
michael@0 99 Elf_Scn *scn = NULL;
michael@0 100 GElf_Shdr shdr;
michael@0 101
michael@0 102 while ((scn = elf_nextscn(elf, scn)) != NULL) {
michael@0 103 if (gelf_getshdr(scn, &shdr) == (GElf_Shdr *)0) {
michael@0 104 print_message2(2, "failed to read section header: %s\n", elf_errmsg(0));
michael@0 105 return false;
michael@0 106 }
michael@0 107
michael@0 108 if (shdr.sh_type == SHT_PROGBITS) {
michael@0 109 const char *section_name = elf_strptr(elf, elf_header.e_shstrndx,
michael@0 110 shdr.sh_name);
michael@0 111 if (!section_name) {
michael@0 112 print_message2(2, "Section name error: %s\n", elf_errmsg(-1));
michael@0 113 continue;
michael@0 114 }
michael@0 115
michael@0 116 if (strcmp(section_name, kTextSectionName) == 0) {
michael@0 117 text_section = &shdr;
michael@0 118 break;
michael@0 119 }
michael@0 120 }
michael@0 121 }
michael@0 122 if (text_section != NULL && text_section->sh_size > 0) {
michael@0 123 *text_start = (char *)elf_base + text_section->sh_offset;
michael@0 124 *text_size = text_section->sh_size;
michael@0 125 return true;
michael@0 126 }
michael@0 127
michael@0 128 return false;
michael@0 129 }
michael@0 130
michael@0 131 FileID::FileID(const char *path) {
michael@0 132 strcpy(path_, path);
michael@0 133 }
michael@0 134
michael@0 135 class AutoCloser {
michael@0 136 public:
michael@0 137 AutoCloser(int fd) : fd_(fd) {}
michael@0 138 ~AutoCloser() { if (fd_) close(fd_); }
michael@0 139 private:
michael@0 140 int fd_;
michael@0 141 };
michael@0 142
michael@0 143 bool FileID::ElfFileIdentifier(unsigned char identifier[16]) {
michael@0 144 int fd = 0;
michael@0 145 if ((fd = open(path_, O_RDONLY)) < 0)
michael@0 146 return false;
michael@0 147
michael@0 148 AutoCloser autocloser(fd);
michael@0 149 struct stat st;
michael@0 150 if (fstat(fd, &st) != 0 || st.st_size <= 0)
michael@0 151 return false;
michael@0 152
michael@0 153 void *base = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
michael@0 154 if (base == MAP_FAILED)
michael@0 155 return false;
michael@0 156
michael@0 157 bool success = false;
michael@0 158 const void *text_section = NULL;
michael@0 159 int text_size = 0;
michael@0 160
michael@0 161 if (FindElfTextSection(fd, base, &text_section, &text_size)) {
michael@0 162 MD5Context md5;
michael@0 163 MD5Init(&md5);
michael@0 164 MD5Update(&md5, (const unsigned char *)text_section, text_size);
michael@0 165 MD5Final(identifier, &md5);
michael@0 166 success = true;
michael@0 167 }
michael@0 168
michael@0 169 munmap((char *)base, st.st_size);
michael@0 170 return success;
michael@0 171 }
michael@0 172
michael@0 173 // static
michael@0 174 bool FileID::ConvertIdentifierToString(const unsigned char identifier[16],
michael@0 175 char *buffer, int buffer_length) {
michael@0 176 if (buffer_length < 34)
michael@0 177 return false;
michael@0 178
michael@0 179 int buffer_idx = 0;
michael@0 180 for (int idx = 0; idx < 16; ++idx) {
michael@0 181 int hi = (identifier[idx] >> 4) & 0x0F;
michael@0 182 int lo = (identifier[idx]) & 0x0F;
michael@0 183
michael@0 184 buffer[buffer_idx++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi;
michael@0 185 buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo;
michael@0 186 }
michael@0 187
michael@0 188 // Add an extra "0" by the end.
michael@0 189 buffer[buffer_idx++] = '0';
michael@0 190
michael@0 191 // NULL terminate
michael@0 192 buffer[buffer_idx] = 0;
michael@0 193
michael@0 194 return true;
michael@0 195 }
michael@0 196
michael@0 197 } // namespace google_breakpad

mercurial