1 // Copyright (c) 2006, 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.
29 //
30 // file_id.cc: Return a unique identifier for a file
31 //
32 // See file_id.h for documentation
33 //
34
35 #include "common/linux/file_id.h"
36
37 #include <arpa/inet.h>
38 #include <assert.h>
39 #include <elf.h>
40 #include <fcntl.h>
41 #if defined(__ANDROID__)
42 #include "client/linux/android_link.h"
43 #else
44 #include <link.h>
45 #endif
46 #include <stdio.h>
47 #include <string.h>
48 #include <sys/mman.h>
49 #include <sys/stat.h>
50 #include <unistd.h>
51
52 #include <algorithm>
53
54 #include "common/linux/linux_libc_support.h"
55 #include "third_party/lss/linux_syscall_support.h"
56
57 namespace google_breakpad {
58
FileID(const char * path)59 FileID::FileID(const char* path) {
60 strncpy(path_, path, sizeof(path_));
61 }
62
63 struct ElfClass32 {
64 typedef Elf32_Ehdr Ehdr;
65 typedef Elf32_Shdr Shdr;
66 static const int kClass = ELFCLASS32;
67 };
68
69 struct ElfClass64 {
70 typedef Elf64_Ehdr Ehdr;
71 typedef Elf64_Shdr Shdr;
72 static const int kClass = ELFCLASS64;
73 };
74
75 // These three functions are also used inside the crashed process, so be safe
76 // and use the syscall/libc wrappers instead of direct syscalls or libc.
77 template<typename ElfClass>
FindElfClassTextSection(const char * elf_base,const void ** text_start,int * text_size)78 static void FindElfClassTextSection(const char *elf_base,
79 const void **text_start,
80 int *text_size) {
81 typedef typename ElfClass::Ehdr Ehdr;
82 typedef typename ElfClass::Shdr Shdr;
83
84 assert(elf_base);
85 assert(text_start);
86 assert(text_size);
87
88 assert(my_strncmp(elf_base, ELFMAG, SELFMAG) == 0);
89
90 const char* text_section_name = ".text";
91 int name_len = my_strlen(text_section_name);
92
93 const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base);
94 assert(elf_header->e_ident[EI_CLASS] == ElfClass::kClass);
95
96 const Shdr* sections =
97 reinterpret_cast<const Shdr*>(elf_base + elf_header->e_shoff);
98 const Shdr* string_section = sections + elf_header->e_shstrndx;
99
100 const Shdr* text_section = NULL;
101 for (int i = 0; i < elf_header->e_shnum; ++i) {
102 if (sections[i].sh_type == SHT_PROGBITS) {
103 const char* section_name = (char*)(elf_base +
104 string_section->sh_offset +
105 sections[i].sh_name);
106 if (!my_strncmp(section_name, text_section_name, name_len)) {
107 text_section = §ions[i];
108 break;
109 }
110 }
111 }
112 if (text_section != NULL && text_section->sh_size > 0) {
113 *text_start = elf_base + text_section->sh_offset;
114 *text_size = text_section->sh_size;
115 }
116 }
117
FindElfTextSection(const void * elf_mapped_base,const void ** text_start,int * text_size)118 static bool FindElfTextSection(const void *elf_mapped_base,
119 const void **text_start,
120 int *text_size) {
121 assert(elf_mapped_base);
122 assert(text_start);
123 assert(text_size);
124
125 const char* elf_base =
126 static_cast<const char*>(elf_mapped_base);
127 const ElfW(Ehdr)* elf_header =
128 reinterpret_cast<const ElfW(Ehdr)*>(elf_base);
129 if (my_strncmp(elf_base, ELFMAG, SELFMAG) != 0)
130 return false;
131
132 if (elf_header->e_ident[EI_CLASS] == ELFCLASS32) {
133 FindElfClassTextSection<ElfClass32>(elf_base, text_start, text_size);
134 } else if (elf_header->e_ident[EI_CLASS] == ELFCLASS64) {
135 FindElfClassTextSection<ElfClass64>(elf_base, text_start, text_size);
136 } else {
137 return false;
138 }
139
140 return true;
141 }
142
143 // static
ElfFileIdentifierFromMappedFile(void * base,uint8_t identifier[kMDGUIDSize])144 bool FileID::ElfFileIdentifierFromMappedFile(void* base,
145 uint8_t identifier[kMDGUIDSize])
146 {
147 const void* text_section = NULL;
148 int text_size = 0;
149 bool success = false;
150 if (FindElfTextSection(base, &text_section, &text_size) && (text_size > 0)) {
151 my_memset(identifier, 0, kMDGUIDSize);
152 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(text_section);
153 const uint8_t* ptr_end = ptr + std::min(text_size, 4096);
154 while (ptr < ptr_end) {
155 for (unsigned i = 0; i < kMDGUIDSize; i++)
156 identifier[i] ^= ptr[i];
157 ptr += kMDGUIDSize;
158 }
159 success = true;
160 }
161 return success;
162 }
163
ElfFileIdentifier(uint8_t identifier[kMDGUIDSize])164 bool FileID::ElfFileIdentifier(uint8_t identifier[kMDGUIDSize]) {
165 int fd = open(path_, O_RDONLY);
166 if (fd < 0)
167 return false;
168 struct stat st;
169 if (fstat(fd, &st) != 0) {
170 close(fd);
171 return false;
172 }
173 void* base = mmap(NULL, st.st_size,
174 PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
175 close(fd);
176 if (base == MAP_FAILED)
177 return false;
178
179 bool success = ElfFileIdentifierFromMappedFile(base, identifier);
180 munmap(base, st.st_size);
181 return success;
182 }
183
184 // static
ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize],char * buffer,int buffer_length)185 void FileID::ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize],
186 char* buffer, int buffer_length) {
187 uint8_t identifier_swapped[kMDGUIDSize];
188
189 // Endian-ness swap to match dump processor expectation.
190 memcpy(identifier_swapped, identifier, kMDGUIDSize);
191 uint32_t* data1 = reinterpret_cast<uint32_t*>(identifier_swapped);
192 *data1 = htonl(*data1);
193 uint16_t* data2 = reinterpret_cast<uint16_t*>(identifier_swapped + 4);
194 *data2 = htons(*data2);
195 uint16_t* data3 = reinterpret_cast<uint16_t*>(identifier_swapped + 6);
196 *data3 = htons(*data3);
197
198 int buffer_idx = 0;
199 for (unsigned int idx = 0;
200 (buffer_idx < buffer_length) && (idx < kMDGUIDSize);
201 ++idx) {
202 int hi = (identifier_swapped[idx] >> 4) & 0x0F;
203 int lo = (identifier_swapped[idx]) & 0x0F;
204
205 if (idx == 4 || idx == 6 || idx == 8 || idx == 10)
206 buffer[buffer_idx++] = '-';
207
208 buffer[buffer_idx++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi;
209 buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo;
210 }
211
212 // NULL terminate
213 buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0;
214 }
215
216 } // namespace google_breakpad
217