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