1 | /* Copyright (C) 1999-2021 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Andreas Jaeger <aj@suse.de>, 1999 and |
4 | Jakub Jelinek <jakub@redhat.com>, 1999. |
5 | |
6 | The GNU C Library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either |
9 | version 2.1 of the License, or (at your option) any later version. |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | Lesser General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, see |
18 | <https://www.gnu.org/licenses/>. */ |
19 | |
20 | #include <elf-read-prop.h> |
21 | |
22 | /* This code is a heavily simplified version of the readelf program |
23 | that's part of the current binutils development version. For architectures |
24 | which need to handle both 32bit and 64bit ELF libraries, this file is |
25 | included twice for each arch size. */ |
26 | |
27 | /* check_ptr checks that a pointer is in the mmaped file and doesn't |
28 | point outside it. */ |
29 | #undef check_ptr |
30 | #define check_ptr(ptr) \ |
31 | do \ |
32 | { \ |
33 | if ((void *)(ptr) < file_contents \ |
34 | || (void *)(ptr) > (file_contents+file_length)) \ |
35 | { \ |
36 | error (0, 0, _("file %s is truncated\n"), file_name); \ |
37 | return 1; \ |
38 | } \ |
39 | } \ |
40 | while (0); |
41 | |
42 | /* Returns 0 if everything is ok, != 0 in case of error. */ |
43 | int |
44 | process_elf_file (const char *file_name, const char *lib, int *flag, |
45 | unsigned int *osversion, unsigned int *isa_level, |
46 | char **soname, void *file_contents, size_t file_length) |
47 | { |
48 | int i; |
49 | unsigned int j; |
50 | unsigned int dynamic_addr; |
51 | size_t dynamic_size; |
52 | char *program_interpreter; |
53 | |
54 | ElfW(Ehdr) *; |
55 | ElfW(Phdr) *, *segment; |
56 | ElfW(Dyn) *dynamic_segment, *dyn_entry; |
57 | char *dynamic_strings; |
58 | |
59 | elf_header = (ElfW(Ehdr) *) file_contents; |
60 | *osversion = 0; |
61 | |
62 | if (elf_header->e_ident [EI_CLASS] != ElfW (CLASS)) |
63 | { |
64 | if (opt_verbose) |
65 | { |
66 | if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) |
67 | error (0, 0, _("%s is a 32 bit ELF file.\n" ), file_name); |
68 | else if (elf_header->e_ident [EI_CLASS] == ELFCLASS64) |
69 | error (0, 0, _("%s is a 64 bit ELF file.\n" ), file_name); |
70 | else |
71 | error (0, 0, _("Unknown ELFCLASS in file %s.\n" ), file_name); |
72 | } |
73 | return 1; |
74 | } |
75 | |
76 | if (elf_header->e_type != ET_DYN) |
77 | { |
78 | error (0, 0, _("%s is not a shared object file (Type: %d).\n" ), file_name, |
79 | elf_header->e_type); |
80 | return 1; |
81 | } |
82 | |
83 | /* Get information from elf program header. */ |
84 | elf_pheader = (ElfW(Phdr) *) (elf_header->e_phoff + file_contents); |
85 | check_ptr (elf_pheader); |
86 | |
87 | /* The library is an elf library, now search for soname and |
88 | libc5/libc6. */ |
89 | *flag = FLAG_ELF; |
90 | |
91 | /* The default ISA level is 0. */ |
92 | *isa_level = 0; |
93 | |
94 | dynamic_addr = 0; |
95 | dynamic_size = 0; |
96 | program_interpreter = NULL; |
97 | for (i = 0, segment = elf_pheader; |
98 | i < elf_header->e_phnum; i++, segment++) |
99 | { |
100 | check_ptr (segment); |
101 | |
102 | switch (segment->p_type) |
103 | { |
104 | case PT_DYNAMIC: |
105 | if (dynamic_addr) |
106 | error (0, 0, _("more than one dynamic segment\n" )); |
107 | |
108 | dynamic_addr = segment->p_offset; |
109 | dynamic_size = segment->p_filesz; |
110 | break; |
111 | |
112 | case PT_INTERP: |
113 | program_interpreter = (char *) (file_contents + segment->p_offset); |
114 | check_ptr (program_interpreter); |
115 | |
116 | /* Check if this is enough to classify the binary. */ |
117 | for (j = 0; j < sizeof (interpreters) / sizeof (interpreters [0]); |
118 | ++j) |
119 | if (strcmp (program_interpreter, interpreters[j].soname) == 0) |
120 | { |
121 | *flag = interpreters[j].flag; |
122 | break; |
123 | } |
124 | break; |
125 | |
126 | case PT_NOTE: |
127 | if (!*osversion && segment->p_filesz >= 32 && segment->p_align >= 4) |
128 | { |
129 | ElfW(Word) *abi_note = (ElfW(Word) *) (file_contents |
130 | + segment->p_offset); |
131 | ElfW(Addr) size = segment->p_filesz; |
132 | /* NB: Some PT_NOTE segment may have alignment value of 0 |
133 | or 1. gABI specifies that PT_NOTE segments should be |
134 | aligned to 4 bytes in 32-bit objects and to 8 bytes in |
135 | 64-bit objects. As a Linux extension, we also support |
136 | 4 byte alignment in 64-bit objects. If p_align is less |
137 | than 4, we treate alignment as 4 bytes since some note |
138 | segments have 0 or 1 byte alignment. */ |
139 | ElfW(Addr) align = segment->p_align; |
140 | if (align < 4) |
141 | align = 4; |
142 | else if (align != 4 && align != 8) |
143 | continue; |
144 | |
145 | while (abi_note [0] != 4 || abi_note [1] != 16 |
146 | || abi_note [2] != 1 |
147 | || memcmp (abi_note + 3, "GNU" , 4) != 0) |
148 | { |
149 | ElfW(Addr) note_size |
150 | = ELF_NOTE_NEXT_OFFSET (abi_note[0], abi_note[1], |
151 | align); |
152 | |
153 | if (size - 32 < note_size || note_size == 0) |
154 | { |
155 | size = 0; |
156 | break; |
157 | } |
158 | size -= note_size; |
159 | abi_note = (void *) abi_note + note_size; |
160 | } |
161 | |
162 | if (size == 0) |
163 | break; |
164 | |
165 | *osversion = ((abi_note [4] << 24) |
166 | | ((abi_note [5] & 0xff) << 16) |
167 | | ((abi_note [6] & 0xff) << 8) |
168 | | (abi_note [7] & 0xff)); |
169 | } |
170 | break; |
171 | |
172 | case PT_GNU_PROPERTY: |
173 | /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned to 4 bytes |
174 | in 32-bit objects and to 8 bytes in 64-bit objects. Skip |
175 | notes with incorrect alignment. */ |
176 | if (segment->p_align == (__ELF_NATIVE_CLASS / 8)) |
177 | { |
178 | const ElfW(Nhdr) *note = (const void *) (file_contents |
179 | + segment->p_offset); |
180 | const ElfW(Addr) size = segment->p_filesz; |
181 | const ElfW(Addr) align = segment->p_align; |
182 | |
183 | const ElfW(Addr) start = (ElfW(Addr)) (uintptr_t) note; |
184 | unsigned int last_type = 0; |
185 | |
186 | while ((ElfW(Addr)) (uintptr_t) (note + 1) - start < size) |
187 | { |
188 | /* Find the NT_GNU_PROPERTY_TYPE_0 note. */ |
189 | if (note->n_namesz == 4 |
190 | && note->n_type == NT_GNU_PROPERTY_TYPE_0 |
191 | && memcmp (note + 1, "GNU" , 4) == 0) |
192 | { |
193 | /* Check for invalid property. */ |
194 | if (note->n_descsz < 8 |
195 | || (note->n_descsz % sizeof (ElfW(Addr))) != 0) |
196 | goto done; |
197 | |
198 | /* Start and end of property array. */ |
199 | unsigned char *ptr = (unsigned char *) (note + 1) + 4; |
200 | unsigned char *ptr_end = ptr + note->n_descsz; |
201 | |
202 | do |
203 | { |
204 | unsigned int type = *(unsigned int *) ptr; |
205 | unsigned int datasz = *(unsigned int *) (ptr + 4); |
206 | |
207 | /* Property type must be in ascending order. */ |
208 | if (type < last_type) |
209 | goto done; |
210 | |
211 | ptr += 8; |
212 | if ((ptr + datasz) > ptr_end) |
213 | goto done; |
214 | |
215 | last_type = type; |
216 | |
217 | /* Target specific property processing. |
218 | Return value: |
219 | false: Continue processing the properties. |
220 | true : Stop processing the properties. |
221 | */ |
222 | if (read_gnu_property (isa_level, type, |
223 | datasz, ptr)) |
224 | goto done; |
225 | |
226 | /* Check the next property item. */ |
227 | ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); |
228 | } |
229 | while ((ptr_end - ptr) >= 8); |
230 | |
231 | /* Only handle one NT_GNU_PROPERTY_TYPE_0. */ |
232 | goto done; |
233 | } |
234 | |
235 | note = ((const void *) note |
236 | + ELF_NOTE_NEXT_OFFSET (note->n_namesz, |
237 | note->n_descsz, |
238 | align)); |
239 | } |
240 | } |
241 | done: |
242 | break; |
243 | |
244 | default: |
245 | break; |
246 | } |
247 | |
248 | } |
249 | |
250 | /* Now we can read the dynamic sections. */ |
251 | if (dynamic_size == 0) |
252 | return 1; |
253 | |
254 | dynamic_segment = (ElfW(Dyn) *) (file_contents + dynamic_addr); |
255 | check_ptr (dynamic_segment); |
256 | |
257 | /* Find the string table. */ |
258 | dynamic_strings = NULL; |
259 | for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL; |
260 | ++dyn_entry) |
261 | { |
262 | check_ptr (dyn_entry); |
263 | if (dyn_entry->d_tag == DT_STRTAB) |
264 | { |
265 | /* Find the file offset of the segment containing the dynamic |
266 | string table. */ |
267 | ElfW(Off) loadoff = -1; |
268 | for (i = 0, segment = elf_pheader; |
269 | i < elf_header->e_phnum; i++, segment++) |
270 | { |
271 | if (segment->p_type == PT_LOAD |
272 | && dyn_entry->d_un.d_val >= segment->p_vaddr |
273 | && (dyn_entry->d_un.d_val - segment->p_vaddr |
274 | < segment->p_filesz)) |
275 | { |
276 | loadoff = segment->p_vaddr - segment->p_offset; |
277 | break; |
278 | } |
279 | } |
280 | if (loadoff == (ElfW(Off)) -1) |
281 | { |
282 | /* Very strange. */ |
283 | loadoff = 0; |
284 | } |
285 | |
286 | dynamic_strings = (char *) (file_contents + dyn_entry->d_un.d_val |
287 | - loadoff); |
288 | check_ptr (dynamic_strings); |
289 | break; |
290 | } |
291 | } |
292 | |
293 | if (dynamic_strings == NULL) |
294 | return 1; |
295 | |
296 | /* Now read the DT_NEEDED and DT_SONAME entries. */ |
297 | for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL; |
298 | ++dyn_entry) |
299 | { |
300 | if (dyn_entry->d_tag == DT_NEEDED || dyn_entry->d_tag == DT_SONAME) |
301 | { |
302 | char *name = dynamic_strings + dyn_entry->d_un.d_val; |
303 | check_ptr (name); |
304 | |
305 | if (dyn_entry->d_tag == DT_NEEDED) |
306 | { |
307 | |
308 | if (*flag == FLAG_ELF) |
309 | { |
310 | /* Check if this is enough to classify the binary. */ |
311 | for (j = 0; |
312 | j < sizeof (known_libs) / sizeof (known_libs [0]); |
313 | ++j) |
314 | if (strcmp (name, known_libs [j].soname) == 0) |
315 | { |
316 | *flag = known_libs [j].flag; |
317 | break; |
318 | } |
319 | } |
320 | } |
321 | |
322 | else if (dyn_entry->d_tag == DT_SONAME) |
323 | *soname = xstrdup (name); |
324 | |
325 | /* Do we have everything we need? */ |
326 | if (*soname && *flag != FLAG_ELF) |
327 | return 0; |
328 | } |
329 | } |
330 | |
331 | return 0; |
332 | } |
333 | |