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.

     1 // Copyright (c) 2007, Google Inc.
     2 // All rights reserved.
     3 //
     4 // Redistribution and use in source and binary forms, with or without
     5 // modification, are permitted provided that the following conditions are
     6 // met:
     7 //
     8 //     * Redistributions of source code must retain the above copyright
     9 // notice, this list of conditions and the following disclaimer.
    10 //     * Redistributions in binary form must reproduce the above
    11 // copyright notice, this list of conditions and the following disclaimer
    12 // in the documentation and/or other materials provided with the
    13 // distribution.
    14 //     * Neither the name of Google Inc. nor the names of its
    15 // contributors may be used to endorse or promote products derived from
    16 // this software without specific prior written permission.
    17 //
    18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    30 // file_id.cc: Return a unique identifier for a file
    31 //
    32 // See file_id.h for documentation
    33 //
    34 // Author: Alfred Peng
    36 #include <elf.h>
    37 #include <fcntl.h>
    38 #include <gelf.h>
    39 #include <sys/mman.h>
    40 #include <sys/ksyms.h>
    41 #include <stdio.h>
    42 #include <string.h>
    43 #include <unistd.h>
    45 #include <cassert>
    46 #include <cstdio>
    48 #include "common/md5.h"
    49 #include "common/solaris/file_id.h"
    50 #include "common/solaris/message_output.h"
    51 #include "google_breakpad/common/minidump_format.h"
    53 namespace google_breakpad {
    55 class AutoElfEnder {
    56  public:
    57   AutoElfEnder(Elf *elf) : elf_(elf) {}
    58   ~AutoElfEnder() { if (elf_) elf_end(elf_); }
    59  private:
    60   Elf *elf_;
    61 };
    63 // Find the text section in elf object file.
    64 // Return the section start address and the size.
    65 static bool FindElfTextSection(int fd, const void *elf_base,
    66                                const void **text_start,
    67                                int *text_size) {
    68   assert(text_start);
    69   assert(text_size);
    71   *text_start = NULL;
    72   *text_size = 0;
    74   if (elf_version(EV_CURRENT) == EV_NONE) {
    75     print_message2(2, "elf_version() failed: %s\n", elf_errmsg(0));
    76     return false;
    77   }
    79   GElf_Ehdr elf_header;
    80   lseek(fd, 0L, 0);
    81   Elf *elf = elf_begin(fd, ELF_C_READ, NULL);
    82   AutoElfEnder elfEnder(elf);
    84   if (gelf_getehdr(elf, &elf_header) == (GElf_Ehdr *)NULL) {
    85     print_message2(2, "failed to read elf header: %s\n", elf_errmsg(-1));
    86     return false;
    87   }
    89   if (elf_header.e_ident[EI_MAG0] != ELFMAG0 ||
    90       elf_header.e_ident[EI_MAG1] != ELFMAG1 ||
    91       elf_header.e_ident[EI_MAG2] != ELFMAG2 ||
    92       elf_header.e_ident[EI_MAG3] != ELFMAG3) {
    93     print_message1(2, "header magic doesn't match\n");
    94     return false;
    95   }
    97   static const char kTextSectionName[] = ".text";
    98   const GElf_Shdr *text_section = NULL;
    99   Elf_Scn *scn = NULL;
   100   GElf_Shdr shdr;
   102   while ((scn = elf_nextscn(elf, scn)) != NULL) {
   103     if (gelf_getshdr(scn, &shdr) == (GElf_Shdr *)0) {
   104       print_message2(2, "failed to read section header: %s\n", elf_errmsg(0));
   105       return false;
   106     }
   108     if (shdr.sh_type == SHT_PROGBITS) {
   109       const char *section_name = elf_strptr(elf, elf_header.e_shstrndx,
   110                                             shdr.sh_name);
   111       if (!section_name) {
   112         print_message2(2, "Section name error: %s\n", elf_errmsg(-1));
   113         continue;
   114       }
   116       if (strcmp(section_name, kTextSectionName) == 0) {
   117         text_section = &shdr;
   118         break;
   119       }
   120     }
   121   }
   122   if (text_section != NULL && text_section->sh_size > 0) {
   123     *text_start = (char *)elf_base + text_section->sh_offset;
   124     *text_size = text_section->sh_size;
   125     return true;
   126   }
   128   return false;
   129 }
   131 FileID::FileID(const char *path) {
   132   strcpy(path_, path);
   133 }
   135 class AutoCloser {
   136  public:
   137   AutoCloser(int fd) : fd_(fd) {}
   138   ~AutoCloser() { if (fd_) close(fd_); }
   139  private:
   140   int fd_;
   141 };
   143 bool FileID::ElfFileIdentifier(unsigned char identifier[16]) {
   144   int fd = 0;
   145   if ((fd = open(path_, O_RDONLY)) < 0)
   146     return false;
   148   AutoCloser autocloser(fd);
   149   struct stat st;
   150   if (fstat(fd, &st) != 0 || st.st_size <= 0)
   151     return false;
   153   void *base = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
   154   if (base == MAP_FAILED)
   155     return false;
   157   bool success = false;
   158   const void *text_section = NULL;
   159   int text_size = 0;
   161   if (FindElfTextSection(fd, base, &text_section, &text_size)) {
   162     MD5Context md5;
   163     MD5Init(&md5);
   164     MD5Update(&md5, (const unsigned char *)text_section, text_size);
   165     MD5Final(identifier, &md5);
   166     success = true;
   167   }
   169   munmap((char *)base, st.st_size);
   170   return success;
   171 }
   173 // static
   174 bool FileID::ConvertIdentifierToString(const unsigned char identifier[16],
   175                                        char *buffer, int buffer_length) {
   176   if (buffer_length < 34)
   177     return false;
   179   int buffer_idx = 0;
   180   for (int idx = 0; idx < 16; ++idx) {
   181     int hi = (identifier[idx] >> 4) & 0x0F;
   182     int lo = (identifier[idx]) & 0x0F;
   184     buffer[buffer_idx++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi;
   185     buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo;
   186   }
   188   // Add an extra "0" by the end.
   189   buffer[buffer_idx++] = '0';
   191   // NULL terminate
   192   buffer[buffer_idx] = 0;
   194   return true;
   195 }
   197 }  // namespace google_breakpad

mercurial