1 /*
2 * Copyright (c) Christos Zoulas 2003.
3 * All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27 #include "file.h"
28
29 #ifndef lint
30 FILE_RCSID("@(#)$File: readelf.c,v 1.175 2020/12/17 20:43:37 christos Exp $")
31 #endif
32
33 #ifdef BUILTIN_ELF
34 #include <string.h>
35 #include <ctype.h>
36 #include <stdlib.h>
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #include "readelf.h"
42 #include "magic.h"
43
44 #ifdef ELFCORE
45 private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
46 off_t, int *, uint16_t *);
47 #endif
48 private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
49 off_t, int, int *, uint16_t *);
50 private int doshn(struct magic_set *, int, int, int, off_t, int, size_t,
51 off_t, int, int, int *, uint16_t *);
52 private size_t donote(struct magic_set *, void *, size_t, size_t, int,
53 int, size_t, int *, uint16_t *, int, off_t, int, off_t);
54
55 #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
56
57 #define isquote(c) (strchr("'\"`", (c)) != NULL)
58
59 private uint16_t getu16(int, uint16_t);
60 private uint32_t getu32(int, uint32_t);
61 private uint64_t getu64(int, uint64_t);
62
63 #define MAX_PHNUM 128
64 #define MAX_SHNUM 32768
65 #define SIZE_UNKNOWN CAST(off_t, -1)
66
67 private int
toomany(struct magic_set * ms,const char * name,uint16_t num)68 toomany(struct magic_set *ms, const char *name, uint16_t num)
69 {
70 if (ms->flags & MAGIC_MIME)
71 return 1;
72 if (file_printf(ms, ", too many %s (%u)", name, num) == -1)
73 return -1;
74 return 1;
75 }
76
77 private uint16_t
getu16(int swap,uint16_t value)78 getu16(int swap, uint16_t value)
79 {
80 union {
81 uint16_t ui;
82 char c[2];
83 } retval, tmpval;
84
85 if (swap) {
86 tmpval.ui = value;
87
88 retval.c[0] = tmpval.c[1];
89 retval.c[1] = tmpval.c[0];
90
91 return retval.ui;
92 } else
93 return value;
94 }
95
96 private uint32_t
getu32(int swap,uint32_t value)97 getu32(int swap, uint32_t value)
98 {
99 union {
100 uint32_t ui;
101 char c[4];
102 } retval, tmpval;
103
104 if (swap) {
105 tmpval.ui = value;
106
107 retval.c[0] = tmpval.c[3];
108 retval.c[1] = tmpval.c[2];
109 retval.c[2] = tmpval.c[1];
110 retval.c[3] = tmpval.c[0];
111
112 return retval.ui;
113 } else
114 return value;
115 }
116
117 private uint64_t
getu64(int swap,uint64_t value)118 getu64(int swap, uint64_t value)
119 {
120 union {
121 uint64_t ui;
122 char c[8];
123 } retval, tmpval;
124
125 if (swap) {
126 tmpval.ui = value;
127
128 retval.c[0] = tmpval.c[7];
129 retval.c[1] = tmpval.c[6];
130 retval.c[2] = tmpval.c[5];
131 retval.c[3] = tmpval.c[4];
132 retval.c[4] = tmpval.c[3];
133 retval.c[5] = tmpval.c[2];
134 retval.c[6] = tmpval.c[1];
135 retval.c[7] = tmpval.c[0];
136
137 return retval.ui;
138 } else
139 return value;
140 }
141
142 #define elf_getu16(swap, value) getu16(swap, value)
143 #define elf_getu32(swap, value) getu32(swap, value)
144 #define elf_getu64(swap, value) getu64(swap, value)
145
146 #define xsh_addr (clazz == ELFCLASS32 \
147 ? CAST(void *, &sh32) \
148 : CAST(void *, &sh64))
149 #define xsh_sizeof (clazz == ELFCLASS32 \
150 ? sizeof(sh32) \
151 : sizeof(sh64))
152 #define xsh_size CAST(size_t, (clazz == ELFCLASS32 \
153 ? elf_getu32(swap, sh32.sh_size) \
154 : elf_getu64(swap, sh64.sh_size)))
155 #define xsh_offset CAST(off_t, (clazz == ELFCLASS32 \
156 ? elf_getu32(swap, sh32.sh_offset) \
157 : elf_getu64(swap, sh64.sh_offset)))
158 #define xsh_type (clazz == ELFCLASS32 \
159 ? elf_getu32(swap, sh32.sh_type) \
160 : elf_getu32(swap, sh64.sh_type))
161 #define xsh_name (clazz == ELFCLASS32 \
162 ? elf_getu32(swap, sh32.sh_name) \
163 : elf_getu32(swap, sh64.sh_name))
164
165 #define xph_addr (clazz == ELFCLASS32 \
166 ? CAST(void *, &ph32) \
167 : CAST(void *, &ph64))
168 #define xph_sizeof (clazz == ELFCLASS32 \
169 ? sizeof(ph32) \
170 : sizeof(ph64))
171 #define xph_type (clazz == ELFCLASS32 \
172 ? elf_getu32(swap, ph32.p_type) \
173 : elf_getu32(swap, ph64.p_type))
174 #define xph_offset CAST(off_t, (clazz == ELFCLASS32 \
175 ? elf_getu32(swap, ph32.p_offset) \
176 : elf_getu64(swap, ph64.p_offset)))
177 #define xph_align CAST(size_t, (clazz == ELFCLASS32 \
178 ? CAST(off_t, (ph32.p_align ? \
179 elf_getu32(swap, ph32.p_align) : 4))\
180 : CAST(off_t, (ph64.p_align ? \
181 elf_getu64(swap, ph64.p_align) : 4))))
182 #define xph_vaddr CAST(size_t, (clazz == ELFCLASS32 \
183 ? CAST(off_t, (ph32.p_vaddr ? \
184 elf_getu32(swap, ph32.p_vaddr) : 4))\
185 : CAST(off_t, (ph64.p_vaddr ? \
186 elf_getu64(swap, ph64.p_vaddr) : 4))))
187 #define xph_filesz CAST(size_t, (clazz == ELFCLASS32 \
188 ? elf_getu32(swap, ph32.p_filesz) \
189 : elf_getu64(swap, ph64.p_filesz)))
190 #define xph_memsz CAST(size_t, ((clazz == ELFCLASS32 \
191 ? elf_getu32(swap, ph32.p_memsz) \
192 : elf_getu64(swap, ph64.p_memsz))))
193 #define xnh_addr (clazz == ELFCLASS32 \
194 ? CAST(void *, &nh32) \
195 : CAST(void *, &nh64))
196 #define xnh_sizeof (clazz == ELFCLASS32 \
197 ? sizeof(nh32) \
198 : sizeof(nh64))
199 #define xnh_type (clazz == ELFCLASS32 \
200 ? elf_getu32(swap, nh32.n_type) \
201 : elf_getu32(swap, nh64.n_type))
202 #define xnh_namesz (clazz == ELFCLASS32 \
203 ? elf_getu32(swap, nh32.n_namesz) \
204 : elf_getu32(swap, nh64.n_namesz))
205 #define xnh_descsz (clazz == ELFCLASS32 \
206 ? elf_getu32(swap, nh32.n_descsz) \
207 : elf_getu32(swap, nh64.n_descsz))
208
209 #define xdh_addr (clazz == ELFCLASS32 \
210 ? CAST(void *, &dh32) \
211 : CAST(void *, &dh64))
212 #define xdh_sizeof (clazz == ELFCLASS32 \
213 ? sizeof(dh32) \
214 : sizeof(dh64))
215 #define xdh_tag (clazz == ELFCLASS32 \
216 ? elf_getu32(swap, dh32.d_tag) \
217 : elf_getu64(swap, dh64.d_tag))
218 #define xdh_val (clazz == ELFCLASS32 \
219 ? elf_getu32(swap, dh32.d_un.d_val) \
220 : elf_getu64(swap, dh64.d_un.d_val))
221
222 #define xcap_addr (clazz == ELFCLASS32 \
223 ? CAST(void *, &cap32) \
224 : CAST(void *, &cap64))
225 #define xcap_sizeof (clazz == ELFCLASS32 \
226 ? sizeof(cap32) \
227 : sizeof(cap64))
228 #define xcap_tag (clazz == ELFCLASS32 \
229 ? elf_getu32(swap, cap32.c_tag) \
230 : elf_getu64(swap, cap64.c_tag))
231 #define xcap_val (clazz == ELFCLASS32 \
232 ? elf_getu32(swap, cap32.c_un.c_val) \
233 : elf_getu64(swap, cap64.c_un.c_val))
234
235 #define xauxv_addr (clazz == ELFCLASS32 \
236 ? CAST(void *, &auxv32) \
237 : CAST(void *, &auxv64))
238 #define xauxv_sizeof (clazz == ELFCLASS32 \
239 ? sizeof(auxv32) \
240 : sizeof(auxv64))
241 #define xauxv_type (clazz == ELFCLASS32 \
242 ? elf_getu32(swap, auxv32.a_type) \
243 : elf_getu64(swap, auxv64.a_type))
244 #define xauxv_val (clazz == ELFCLASS32 \
245 ? elf_getu32(swap, auxv32.a_v) \
246 : elf_getu64(swap, auxv64.a_v))
247
248 #define prpsoffsets(i) (clazz == ELFCLASS32 \
249 ? prpsoffsets32[i] \
250 : prpsoffsets64[i])
251
252 #ifdef ELFCORE
253 /*
254 * Try larger offsets first to avoid false matches
255 * from earlier data that happen to look like strings.
256 */
257 static const size_t prpsoffsets32[] = {
258 #ifdef USE_NT_PSINFO
259 104, /* SunOS 5.x (command line) */
260 88, /* SunOS 5.x (short name) */
261 #endif /* USE_NT_PSINFO */
262
263 100, /* SunOS 5.x (command line) */
264 84, /* SunOS 5.x (short name) */
265
266 44, /* Linux (command line) */
267 28, /* Linux (short name) */
268
269 48, /* Linux PowerPC (command line) */
270 32, /* Linux PowerPC (short name) */
271
272 8, /* FreeBSD */
273 };
274
275 static const size_t prpsoffsets64[] = {
276 #ifdef USE_NT_PSINFO
277 152, /* SunOS 5.x (command line) */
278 136, /* SunOS 5.x (short name) */
279 #endif /* USE_NT_PSINFO */
280
281 136, /* SunOS 5.x, 64-bit (command line) */
282 120, /* SunOS 5.x, 64-bit (short name) */
283
284 56, /* Linux (command line) */
285 40, /* Linux (tested on core from 2.4.x, short name) */
286
287 16, /* FreeBSD, 64-bit */
288 };
289
290 #define NOFFSETS32 __arraycount(prpsoffsets32)
291 #define NOFFSETS64 __arraycount(prpsoffsets64)
292
293 #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
294
295 /*
296 * Look through the program headers of an executable image, searching
297 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
298 * "FreeBSD"; if one is found, try looking in various places in its
299 * contents for a 16-character string containing only printable
300 * characters - if found, that string should be the name of the program
301 * that dropped core. Note: right after that 16-character string is,
302 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
303 * Linux, a longer string (80 characters, in 5.x, probably other
304 * SVR4-flavored systems, and Linux) containing the start of the
305 * command line for that program.
306 *
307 * SunOS 5.x core files contain two PT_NOTE sections, with the types
308 * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
309 * same info about the command name and command line, so it probably
310 * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
311 * above (see USE_NT_PSINFO), in case we ever decide to do so. The
312 * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
313 * the SunOS 5.x file command relies on this (and prefers the latter).
314 *
315 * The signal number probably appears in a section of type NT_PRSTATUS,
316 * but that's also rather OS-dependent, in ways that are harder to
317 * dissect with heuristics, so I'm not bothering with the signal number.
318 * (I suppose the signal number could be of interest in situations where
319 * you don't have the binary of the program that dropped core; if you
320 * *do* have that binary, the debugger will probably tell you what
321 * signal it was.)
322 */
323
324 #define OS_STYLE_SVR4 0
325 #define OS_STYLE_FREEBSD 1
326 #define OS_STYLE_NETBSD 2
327
328 private const char os_style_names[][8] = {
329 "SVR4",
330 "FreeBSD",
331 "NetBSD",
332 };
333
334 #define FLAGS_CORE_STYLE 0x0003
335
336 #define FLAGS_DID_CORE 0x0004
337 #define FLAGS_DID_OS_NOTE 0x0008
338 #define FLAGS_DID_BUILD_ID 0x0010
339 #define FLAGS_DID_CORE_STYLE 0x0020
340 #define FLAGS_DID_NETBSD_PAX 0x0040
341 #define FLAGS_DID_NETBSD_MARCH 0x0080
342 #define FLAGS_DID_NETBSD_CMODEL 0x0100
343 #define FLAGS_DID_NETBSD_EMULATION 0x0200
344 #define FLAGS_DID_NETBSD_UNKNOWN 0x0400
345 #define FLAGS_IS_CORE 0x0800
346 #define FLAGS_DID_AUXV 0x1000
347
348 private int
dophn_core(struct magic_set * ms,int clazz,int swap,int fd,off_t off,int num,size_t size,off_t fsize,int * flags,uint16_t * notecount)349 dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
350 int num, size_t size, off_t fsize, int *flags, uint16_t *notecount)
351 {
352 Elf32_Phdr ph32;
353 Elf64_Phdr ph64;
354 size_t offset, len;
355 unsigned char nbuf[BUFSIZ];
356 ssize_t bufsize;
357 off_t ph_off = off, offs;
358 int ph_num = num;
359
360 if (ms->flags & MAGIC_MIME)
361 return 0;
362
363 if (num == 0) {
364 if (file_printf(ms, ", no program header") == -1)
365 return -1;
366 return 0;
367 }
368 if (size != xph_sizeof) {
369 if (file_printf(ms, ", corrupted program header size") == -1)
370 return -1;
371 return 0;
372 }
373
374 /*
375 * Loop through all the program headers.
376 */
377 for ( ; num; num--) {
378 if (pread(fd, xph_addr, xph_sizeof, off) <
379 CAST(ssize_t, xph_sizeof)) {
380 if (file_printf(ms,
381 ", can't read elf program headers at %jd",
382 (intmax_t)off) == -1)
383 return -1;
384 return 0;
385 }
386 off += size;
387
388 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
389 /* Perhaps warn here */
390 continue;
391 }
392
393 if (xph_type != PT_NOTE)
394 continue;
395
396 /*
397 * This is a PT_NOTE section; loop through all the notes
398 * in the section.
399 */
400 len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
401 offs = xph_offset;
402 if ((bufsize = pread(fd, nbuf, len, offs)) == -1) {
403 if (file_printf(ms, " can't read note section at %jd",
404 (intmax_t)offs) == -1)
405 return -1;
406 return 0;
407 }
408 offset = 0;
409 for (;;) {
410 if (offset >= CAST(size_t, bufsize))
411 break;
412 offset = donote(ms, nbuf, offset, CAST(size_t, bufsize),
413 clazz, swap, 4, flags, notecount, fd, ph_off,
414 ph_num, fsize);
415 if (offset == 0)
416 break;
417
418 }
419 }
420 return 0;
421 }
422 #endif
423
424 static int
do_note_netbsd_version(struct magic_set * ms,int swap,void * v)425 do_note_netbsd_version(struct magic_set *ms, int swap, void *v)
426 {
427 uint32_t desc;
428 memcpy(&desc, v, sizeof(desc));
429 desc = elf_getu32(swap, desc);
430
431 if (file_printf(ms, ", for NetBSD") == -1)
432 return -1;
433 /*
434 * The version number used to be stuck as 199905, and was thus
435 * basically content-free. Newer versions of NetBSD have fixed
436 * this and now use the encoding of __NetBSD_Version__:
437 *
438 * MMmmrrpp00
439 *
440 * M = major version
441 * m = minor version
442 * r = release ["",A-Z,Z[A-Z] but numeric]
443 * p = patchlevel
444 */
445 if (desc > 100000000U) {
446 uint32_t ver_patch = (desc / 100) % 100;
447 uint32_t ver_rel = (desc / 10000) % 100;
448 uint32_t ver_min = (desc / 1000000) % 100;
449 uint32_t ver_maj = desc / 100000000;
450
451 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
452 return -1;
453 if (ver_rel == 0 && ver_patch != 0) {
454 if (file_printf(ms, ".%u", ver_patch) == -1)
455 return -1;
456 } else if (ver_rel != 0) {
457 while (ver_rel > 26) {
458 if (file_printf(ms, "Z") == -1)
459 return -1;
460 ver_rel -= 26;
461 }
462 if (file_printf(ms, "%c", 'A' + ver_rel - 1)
463 == -1)
464 return -1;
465 }
466 }
467 return 0;
468 }
469
470 static int
do_note_freebsd_version(struct magic_set * ms,int swap,void * v)471 do_note_freebsd_version(struct magic_set *ms, int swap, void *v)
472 {
473 uint32_t desc;
474
475 memcpy(&desc, v, sizeof(desc));
476 desc = elf_getu32(swap, desc);
477 if (file_printf(ms, ", for FreeBSD") == -1)
478 return -1;
479
480 /*
481 * Contents is __FreeBSD_version, whose relation to OS
482 * versions is defined by a huge table in the Porter's
483 * Handbook. This is the general scheme:
484 *
485 * Releases:
486 * Mmp000 (before 4.10)
487 * Mmi0p0 (before 5.0)
488 * Mmm0p0
489 *
490 * Development branches:
491 * Mmpxxx (before 4.6)
492 * Mmp1xx (before 4.10)
493 * Mmi1xx (before 5.0)
494 * M000xx (pre-M.0)
495 * Mmm1xx
496 *
497 * M = major version
498 * m = minor version
499 * i = minor version increment (491000 -> 4.10)
500 * p = patchlevel
501 * x = revision
502 *
503 * The first release of FreeBSD to use ELF by default
504 * was version 3.0.
505 */
506 if (desc == 460002) {
507 if (file_printf(ms, " 4.6.2") == -1)
508 return -1;
509 } else if (desc < 460100) {
510 if (file_printf(ms, " %d.%d", desc / 100000,
511 desc / 10000 % 10) == -1)
512 return -1;
513 if (desc / 1000 % 10 > 0)
514 if (file_printf(ms, ".%d", desc / 1000 % 10) == -1)
515 return -1;
516 if ((desc % 1000 > 0) || (desc % 100000 == 0))
517 if (file_printf(ms, " (%d)", desc) == -1)
518 return -1;
519 } else if (desc < 500000) {
520 if (file_printf(ms, " %d.%d", desc / 100000,
521 desc / 10000 % 10 + desc / 1000 % 10) == -1)
522 return -1;
523 if (desc / 100 % 10 > 0) {
524 if (file_printf(ms, " (%d)", desc) == -1)
525 return -1;
526 } else if (desc / 10 % 10 > 0) {
527 if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
528 return -1;
529 }
530 } else {
531 if (file_printf(ms, " %d.%d", desc / 100000,
532 desc / 1000 % 100) == -1)
533 return -1;
534 if ((desc / 100 % 10 > 0) ||
535 (desc % 100000 / 100 == 0)) {
536 if (file_printf(ms, " (%d)", desc) == -1)
537 return -1;
538 } else if (desc / 10 % 10 > 0) {
539 if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
540 return -1;
541 }
542 }
543 return 0;
544 }
545
546 private int
547 /*ARGSUSED*/
do_bid_note(struct magic_set * ms,unsigned char * nbuf,uint32_t type,int swap,uint32_t namesz,uint32_t descsz,size_t noff,size_t doff,int * flags)548 do_bid_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
549 int swap __attribute__((__unused__)), uint32_t namesz, uint32_t descsz,
550 size_t noff, size_t doff, int *flags)
551 {
552 if (namesz == 4 && strcmp(RCAST(char *, &nbuf[noff]), "GNU") == 0 &&
553 type == NT_GNU_BUILD_ID && (descsz >= 4 && descsz <= 20)) {
554 uint8_t desc[20];
555 const char *btype;
556 uint32_t i;
557 *flags |= FLAGS_DID_BUILD_ID;
558 switch (descsz) {
559 case 8:
560 btype = "xxHash";
561 break;
562 case 16:
563 btype = "md5/uuid";
564 break;
565 case 20:
566 btype = "sha1";
567 break;
568 default:
569 btype = "unknown";
570 break;
571 }
572 if (file_printf(ms, ", BuildID[%s]=", btype) == -1)
573 return -1;
574 memcpy(desc, &nbuf[doff], descsz);
575 for (i = 0; i < descsz; i++)
576 if (file_printf(ms, "%02x", desc[i]) == -1)
577 return -1;
578 return 1;
579 }
580 if (namesz == 4 && strcmp(RCAST(char *, &nbuf[noff]), "Go") == 0 &&
581 type == NT_GO_BUILD_ID && descsz < 128) {
582 char buf[256];
583 if (file_printf(ms, ", Go BuildID=%s",
584 file_copystr(buf, sizeof(buf), descsz,
585 RCAST(const char *, &nbuf[doff]))) == -1)
586 return -1;
587 return 1;
588 }
589 return 0;
590 }
591
592 private int
do_os_note(struct magic_set * ms,unsigned char * nbuf,uint32_t type,int swap,uint32_t namesz,uint32_t descsz,size_t noff,size_t doff,int * flags)593 do_os_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
594 int swap, uint32_t namesz, uint32_t descsz,
595 size_t noff, size_t doff, int *flags)
596 {
597 const char *name = RCAST(const char *, &nbuf[noff]);
598
599 if (namesz == 5 && strcmp(name, "SuSE") == 0 &&
600 type == NT_GNU_VERSION && descsz == 2) {
601 *flags |= FLAGS_DID_OS_NOTE;
602 if (file_printf(ms, ", for SuSE %d.%d", nbuf[doff],
603 nbuf[doff + 1]) == -1)
604 return -1;
605 return 1;
606 }
607
608 if (namesz == 4 && strcmp(name, "GNU") == 0 &&
609 type == NT_GNU_VERSION && descsz == 16) {
610 uint32_t desc[4];
611 memcpy(desc, &nbuf[doff], sizeof(desc));
612
613 *flags |= FLAGS_DID_OS_NOTE;
614 if (file_printf(ms, ", for GNU/") == -1)
615 return -1;
616 switch (elf_getu32(swap, desc[0])) {
617 case GNU_OS_LINUX:
618 if (file_printf(ms, "Linux") == -1)
619 return -1;
620 break;
621 case GNU_OS_HURD:
622 if (file_printf(ms, "Hurd") == -1)
623 return -1;
624 break;
625 case GNU_OS_SOLARIS:
626 if (file_printf(ms, "Solaris") == -1)
627 return -1;
628 break;
629 case GNU_OS_KFREEBSD:
630 if (file_printf(ms, "kFreeBSD") == -1)
631 return -1;
632 break;
633 case GNU_OS_KNETBSD:
634 if (file_printf(ms, "kNetBSD") == -1)
635 return -1;
636 break;
637 default:
638 if (file_printf(ms, "<unknown>") == -1)
639 return -1;
640 }
641 if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
642 elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
643 return -1;
644 return 1;
645 }
646
647 if (namesz == 7 && strcmp(name, "NetBSD") == 0) {
648 if (type == NT_NETBSD_VERSION && descsz == 4) {
649 *flags |= FLAGS_DID_OS_NOTE;
650 if (do_note_netbsd_version(ms, swap, &nbuf[doff]) == -1)
651 return -1;
652 return 1;
653 }
654 }
655
656 if (namesz == 8 && strcmp(name, "FreeBSD") == 0) {
657 if (type == NT_FREEBSD_VERSION && descsz == 4) {
658 *flags |= FLAGS_DID_OS_NOTE;
659 if (do_note_freebsd_version(ms, swap, &nbuf[doff])
660 == -1)
661 return -1;
662 return 1;
663 }
664 }
665
666 if (namesz == 8 && strcmp(name, "OpenBSD") == 0 &&
667 type == NT_OPENBSD_VERSION && descsz == 4) {
668 *flags |= FLAGS_DID_OS_NOTE;
669 if (file_printf(ms, ", for OpenBSD") == -1)
670 return -1;
671 /* Content of note is always 0 */
672 return 1;
673 }
674
675 if (namesz == 10 && strcmp(name, "DragonFly") == 0 &&
676 type == NT_DRAGONFLY_VERSION && descsz == 4) {
677 uint32_t desc;
678 *flags |= FLAGS_DID_OS_NOTE;
679 if (file_printf(ms, ", for DragonFly") == -1)
680 return -1;
681 memcpy(&desc, &nbuf[doff], sizeof(desc));
682 desc = elf_getu32(swap, desc);
683 if (file_printf(ms, " %d.%d.%d", desc / 100000,
684 desc / 10000 % 10, desc % 10000) == -1)
685 return -1;
686 return 1;
687 }
688 return 0;
689 }
690
691 private int
do_pax_note(struct magic_set * ms,unsigned char * nbuf,uint32_t type,int swap,uint32_t namesz,uint32_t descsz,size_t noff,size_t doff,int * flags)692 do_pax_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
693 int swap, uint32_t namesz, uint32_t descsz,
694 size_t noff, size_t doff, int *flags)
695 {
696 const char *name = RCAST(const char *, &nbuf[noff]);
697
698 if (namesz == 4 && strcmp(name, "PaX") == 0 &&
699 type == NT_NETBSD_PAX && descsz == 4) {
700 static const char *pax[] = {
701 "+mprotect",
702 "-mprotect",
703 "+segvguard",
704 "-segvguard",
705 "+ASLR",
706 "-ASLR",
707 };
708 uint32_t desc;
709 size_t i;
710 int did = 0;
711
712 *flags |= FLAGS_DID_NETBSD_PAX;
713 memcpy(&desc, &nbuf[doff], sizeof(desc));
714 desc = elf_getu32(swap, desc);
715
716 if (desc && file_printf(ms, ", PaX: ") == -1)
717 return -1;
718
719 for (i = 0; i < __arraycount(pax); i++) {
720 if (((1 << CAST(int, i)) & desc) == 0)
721 continue;
722 if (file_printf(ms, "%s%s", did++ ? "," : "",
723 pax[i]) == -1)
724 return -1;
725 }
726 return 1;
727 }
728 return 0;
729 }
730
731 private int
do_core_note(struct magic_set * ms,unsigned char * nbuf,uint32_t type,int swap,uint32_t namesz,uint32_t descsz,size_t noff,size_t doff,int * flags,size_t size,int clazz)732 do_core_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
733 int swap, uint32_t namesz, uint32_t descsz,
734 size_t noff, size_t doff, int *flags, size_t size, int clazz)
735 {
736 #ifdef ELFCORE
737 char buf[256];
738 const char *name = RCAST(const char *, &nbuf[noff]);
739
740 int os_style = -1;
741 /*
742 * Sigh. The 2.0.36 kernel in Debian 2.1, at
743 * least, doesn't correctly implement name
744 * sections, in core dumps, as specified by
745 * the "Program Linking" section of "UNIX(R) System
746 * V Release 4 Programmer's Guide: ANSI C and
747 * Programming Support Tools", because my copy
748 * clearly says "The first 'namesz' bytes in 'name'
749 * contain a *null-terminated* [emphasis mine]
750 * character representation of the entry's owner
751 * or originator", but the 2.0.36 kernel code
752 * doesn't include the terminating null in the
753 * name....
754 */
755 if ((namesz == 4 && strncmp(name, "CORE", 4) == 0) ||
756 (namesz == 5 && strcmp(name, "CORE") == 0)) {
757 os_style = OS_STYLE_SVR4;
758 }
759
760 if ((namesz == 8 && strcmp(name, "FreeBSD") == 0)) {
761 os_style = OS_STYLE_FREEBSD;
762 }
763
764 if ((namesz >= 11 && strncmp(name, "NetBSD-CORE", 11)
765 == 0)) {
766 os_style = OS_STYLE_NETBSD;
767 }
768
769 if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
770 if (file_printf(ms, ", %s-style", os_style_names[os_style])
771 == -1)
772 return -1;
773 *flags |= FLAGS_DID_CORE_STYLE;
774 *flags |= os_style;
775 }
776
777 switch (os_style) {
778 case OS_STYLE_NETBSD:
779 if (type == NT_NETBSD_CORE_PROCINFO) {
780 char sbuf[512];
781 struct NetBSD_elfcore_procinfo pi;
782 memset(&pi, 0, sizeof(pi));
783 memcpy(&pi, nbuf + doff, MIN(descsz, sizeof(pi)));
784
785 if (file_printf(ms, ", from '%.31s', pid=%u, uid=%u, "
786 "gid=%u, nlwps=%u, lwp=%u (signal %u/code %u)",
787 file_printable(sbuf, sizeof(sbuf),
788 RCAST(char *, pi.cpi_name), sizeof(pi.cpi_name)),
789 elf_getu32(swap, CAST(uint32_t, pi.cpi_pid)),
790 elf_getu32(swap, pi.cpi_euid),
791 elf_getu32(swap, pi.cpi_egid),
792 elf_getu32(swap, pi.cpi_nlwps),
793 elf_getu32(swap, CAST(uint32_t, pi.cpi_siglwp)),
794 elf_getu32(swap, pi.cpi_signo),
795 elf_getu32(swap, pi.cpi_sigcode)) == -1)
796 return -1;
797
798 *flags |= FLAGS_DID_CORE;
799 return 1;
800 }
801 break;
802
803 case OS_STYLE_FREEBSD:
804 if (type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
805 size_t argoff, pidoff;
806
807 if (clazz == ELFCLASS32)
808 argoff = 4 + 4 + 17;
809 else
810 argoff = 4 + 4 + 8 + 17;
811 if (file_printf(ms, ", from '%.80s'", nbuf + doff +
812 argoff) == -1)
813 return -1;
814 pidoff = argoff + 81 + 2;
815 if (doff + pidoff + 4 <= size) {
816 if (file_printf(ms, ", pid=%u",
817 elf_getu32(swap, *RCAST(uint32_t *, (nbuf +
818 doff + pidoff)))) == -1)
819 return -1;
820 }
821 *flags |= FLAGS_DID_CORE;
822 }
823 break;
824
825 default:
826 if (type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
827 size_t i, j;
828 unsigned char c;
829 /*
830 * Extract the program name. We assume
831 * it to be 16 characters (that's what it
832 * is in SunOS 5.x and Linux).
833 *
834 * Unfortunately, it's at a different offset
835 * in various OSes, so try multiple offsets.
836 * If the characters aren't all printable,
837 * reject it.
838 */
839 for (i = 0; i < NOFFSETS; i++) {
840 unsigned char *cname, *cp;
841 size_t reloffset = prpsoffsets(i);
842 size_t noffset = doff + reloffset;
843 size_t k;
844 for (j = 0; j < 16; j++, noffset++,
845 reloffset++) {
846 /*
847 * Make sure we're not past
848 * the end of the buffer; if
849 * we are, just give up.
850 */
851 if (noffset >= size)
852 goto tryanother;
853
854 /*
855 * Make sure we're not past
856 * the end of the contents;
857 * if we are, this obviously
858 * isn't the right offset.
859 */
860 if (reloffset >= descsz)
861 goto tryanother;
862
863 c = nbuf[noffset];
864 if (c == '\0') {
865 /*
866 * A '\0' at the
867 * beginning is
868 * obviously wrong.
869 * Any other '\0'
870 * means we're done.
871 */
872 if (j == 0)
873 goto tryanother;
874 else
875 break;
876 } else {
877 /*
878 * A nonprintable
879 * character is also
880 * wrong.
881 */
882 if (!isprint(c) || isquote(c))
883 goto tryanother;
884 }
885 }
886 /*
887 * Well, that worked.
888 */
889
890 /*
891 * Try next offsets, in case this match is
892 * in the middle of a string.
893 */
894 for (k = i + 1 ; k < NOFFSETS; k++) {
895 size_t no;
896 int adjust = 1;
897 if (prpsoffsets(k) >= prpsoffsets(i))
898 continue;
899 for (no = doff + prpsoffsets(k);
900 no < doff + prpsoffsets(i); no++)
901 adjust = adjust
902 && isprint(nbuf[no]);
903 if (adjust)
904 i = k;
905 }
906
907 cname = CAST(unsigned char *,
908 &nbuf[doff + prpsoffsets(i)]);
909 for (cp = cname; cp < nbuf + size && *cp
910 && isprint(*cp); cp++)
911 continue;
912 /*
913 * Linux apparently appends a space at the end
914 * of the command line: remove it.
915 */
916 while (cp > cname && isspace(cp[-1]))
917 cp--;
918 if (file_printf(ms, ", from '%s'",
919 file_copystr(buf, sizeof(buf),
920 CAST(size_t, cp - cname),
921 CAST(const char *, cname))) == -1)
922 return -1;
923 *flags |= FLAGS_DID_CORE;
924 return 1;
925
926 tryanother:
927 ;
928 }
929 }
930 break;
931 }
932 #endif
933 return 0;
934 }
935
936 private off_t
get_offset_from_virtaddr(struct magic_set * ms,int swap,int clazz,int fd,off_t off,int num,off_t fsize,uint64_t virtaddr)937 get_offset_from_virtaddr(struct magic_set *ms, int swap, int clazz, int fd,
938 off_t off, int num, off_t fsize, uint64_t virtaddr)
939 {
940 Elf32_Phdr ph32;
941 Elf64_Phdr ph64;
942
943 /*
944 * Loop through all the program headers and find the header with
945 * virtual address in which the "virtaddr" belongs to.
946 */
947 for ( ; num; num--) {
948 if (pread(fd, xph_addr, xph_sizeof, off) <
949 CAST(ssize_t, xph_sizeof)) {
950 if (file_printf(ms,
951 ", can't read elf program header at %jd",
952 (intmax_t)off) == -1)
953 return -1;
954 return 0;
955
956 }
957 off += xph_sizeof;
958
959 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
960 /* Perhaps warn here */
961 continue;
962 }
963
964 if (virtaddr >= xph_vaddr && virtaddr < xph_vaddr + xph_filesz)
965 return xph_offset + (virtaddr - xph_vaddr);
966 }
967 return 0;
968 }
969
970 private size_t
get_string_on_virtaddr(struct magic_set * ms,int swap,int clazz,int fd,off_t ph_off,int ph_num,off_t fsize,uint64_t virtaddr,char * buf,ssize_t buflen)971 get_string_on_virtaddr(struct magic_set *ms,
972 int swap, int clazz, int fd, off_t ph_off, int ph_num,
973 off_t fsize, uint64_t virtaddr, char *buf, ssize_t buflen)
974 {
975 char *bptr;
976 off_t offset;
977
978 if (buflen == 0)
979 return 0;
980
981 offset = get_offset_from_virtaddr(ms, swap, clazz, fd, ph_off, ph_num,
982 fsize, virtaddr);
983 if (offset < 0 ||
984 (buflen = pread(fd, buf, CAST(size_t, buflen), offset)) <= 0) {
985 if (file_printf(ms, ", can't read elf string at %jd",
986 (intmax_t)offset) == -1)
987 return -1;
988 return 0;
989 }
990
991 buf[buflen - 1] = '\0';
992
993 /* We expect only printable characters, so return if buffer contains
994 * non-printable character before the '\0' or just '\0'. */
995 for (bptr = buf; *bptr && isprint(CAST(unsigned char, *bptr)); bptr++)
996 continue;
997 if (*bptr != '\0')
998 return 0;
999
1000 return bptr - buf;
1001 }
1002
1003
1004 /*ARGSUSED*/
1005 private int
do_auxv_note(struct magic_set * ms,unsigned char * nbuf,uint32_t type,int swap,uint32_t namesz,uint32_t descsz,size_t noff,size_t doff,int * flags,size_t size,int clazz,int fd,off_t ph_off,int ph_num,off_t fsize)1006 do_auxv_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
1007 int swap, uint32_t namesz __attribute__((__unused__)),
1008 uint32_t descsz __attribute__((__unused__)),
1009 size_t noff __attribute__((__unused__)), size_t doff,
1010 int *flags, size_t size __attribute__((__unused__)), int clazz,
1011 int fd, off_t ph_off, int ph_num, off_t fsize)
1012 {
1013 #ifdef ELFCORE
1014 Aux32Info auxv32;
1015 Aux64Info auxv64;
1016 size_t elsize = xauxv_sizeof;
1017 const char *tag;
1018 int is_string;
1019 size_t nval;
1020
1021 if ((*flags & (FLAGS_IS_CORE|FLAGS_DID_CORE_STYLE)) !=
1022 (FLAGS_IS_CORE|FLAGS_DID_CORE_STYLE))
1023 return 0;
1024
1025 switch (*flags & FLAGS_CORE_STYLE) {
1026 case OS_STYLE_SVR4:
1027 if (type != NT_AUXV)
1028 return 0;
1029 break;
1030 #ifdef notyet
1031 case OS_STYLE_NETBSD:
1032 if (type != NT_NETBSD_CORE_AUXV)
1033 return 0;
1034 break;
1035 case OS_STYLE_FREEBSD:
1036 if (type != NT_FREEBSD_PROCSTAT_AUXV)
1037 return 0;
1038 break;
1039 #endif
1040 default:
1041 return 0;
1042 }
1043
1044 *flags |= FLAGS_DID_AUXV;
1045
1046 nval = 0;
1047 for (size_t off = 0; off + elsize <= descsz; off += elsize) {
1048 memcpy(xauxv_addr, &nbuf[doff + off], xauxv_sizeof);
1049 /* Limit processing to 50 vector entries to prevent DoS */
1050 if (nval++ >= 50) {
1051 file_error(ms, 0, "Too many ELF Auxv elements");
1052 return 1;
1053 }
1054
1055 switch(xauxv_type) {
1056 case AT_LINUX_EXECFN:
1057 is_string = 1;
1058 tag = "execfn";
1059 break;
1060 case AT_LINUX_PLATFORM:
1061 is_string = 1;
1062 tag = "platform";
1063 break;
1064 case AT_LINUX_UID:
1065 is_string = 0;
1066 tag = "real uid";
1067 break;
1068 case AT_LINUX_GID:
1069 is_string = 0;
1070 tag = "real gid";
1071 break;
1072 case AT_LINUX_EUID:
1073 is_string = 0;
1074 tag = "effective uid";
1075 break;
1076 case AT_LINUX_EGID:
1077 is_string = 0;
1078 tag = "effective gid";
1079 break;
1080 default:
1081 is_string = 0;
1082 tag = NULL;
1083 break;
1084 }
1085
1086 if (tag == NULL)
1087 continue;
1088
1089 if (is_string) {
1090 char buf[256];
1091 ssize_t buflen;
1092 buflen = get_string_on_virtaddr(ms, swap, clazz, fd,
1093 ph_off, ph_num, fsize, xauxv_val, buf, sizeof(buf));
1094
1095 if (buflen == 0)
1096 continue;
1097
1098 if (file_printf(ms, ", %s: '%s'", tag, buf) == -1)
1099 return -1;
1100 } else {
1101 if (file_printf(ms, ", %s: %d", tag,
1102 CAST(int, xauxv_val)) == -1)
1103 return -1;
1104 }
1105 }
1106 return 1;
1107 #else
1108 return 0;
1109 #endif
1110 }
1111
1112 private size_t
dodynamic(struct magic_set * ms,void * vbuf,size_t offset,size_t size,int clazz,int swap,int * pie,size_t * need)1113 dodynamic(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
1114 int clazz, int swap, int *pie, size_t *need)
1115 {
1116 Elf32_Dyn dh32;
1117 Elf64_Dyn dh64;
1118 unsigned char *dbuf = CAST(unsigned char *, vbuf);
1119
1120 if (xdh_sizeof + offset > size) {
1121 /*
1122 * We're out of note headers.
1123 */
1124 return xdh_sizeof + offset;
1125 }
1126
1127 memcpy(xdh_addr, &dbuf[offset], xdh_sizeof);
1128 offset += xdh_sizeof;
1129
1130 switch (xdh_tag) {
1131 case DT_FLAGS_1:
1132 *pie = 1;
1133 if (xdh_val & DF_1_PIE)
1134 ms->mode |= 0111;
1135 else
1136 ms->mode &= ~0111;
1137 break;
1138 case DT_NEEDED:
1139 (*need)++;
1140 break;
1141 default:
1142 break;
1143 }
1144 return offset;
1145 }
1146
1147
1148 private size_t
donote(struct magic_set * ms,void * vbuf,size_t offset,size_t size,int clazz,int swap,size_t align,int * flags,uint16_t * notecount,int fd,off_t ph_off,int ph_num,off_t fsize)1149 donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
1150 int clazz, int swap, size_t align, int *flags, uint16_t *notecount,
1151 int fd, off_t ph_off, int ph_num, off_t fsize)
1152 {
1153 Elf32_Nhdr nh32;
1154 Elf64_Nhdr nh64;
1155 size_t noff, doff;
1156 uint32_t namesz, descsz;
1157 char buf[256];
1158 unsigned char *nbuf = CAST(unsigned char *, vbuf);
1159
1160 if (*notecount == 0)
1161 return 0;
1162 --*notecount;
1163
1164 if (xnh_sizeof + offset > size) {
1165 /*
1166 * We're out of note headers.
1167 */
1168 return xnh_sizeof + offset;
1169 }
1170 /*XXX: GCC */
1171 memset(&nh32, 0, sizeof(nh32));
1172 memset(&nh64, 0, sizeof(nh64));
1173
1174 memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
1175 offset += xnh_sizeof;
1176
1177 namesz = xnh_namesz;
1178 descsz = xnh_descsz;
1179
1180 if ((namesz == 0) && (descsz == 0)) {
1181 /*
1182 * We're out of note headers.
1183 */
1184 return (offset >= size) ? offset : size;
1185 }
1186
1187 if (namesz & 0x80000000) {
1188 if (file_printf(ms, ", bad note name size %#lx",
1189 CAST(unsigned long, namesz)) == -1)
1190 return -1;
1191 return 0;
1192 }
1193
1194 if (descsz & 0x80000000) {
1195 if (file_printf(ms, ", bad note description size %#lx",
1196 CAST(unsigned long, descsz)) == -1)
1197 return -1;
1198 return 0;
1199 }
1200
1201 noff = offset;
1202 doff = ELF_ALIGN(offset + namesz);
1203
1204 if (offset + namesz > size) {
1205 /*
1206 * We're past the end of the buffer.
1207 */
1208 return doff;
1209 }
1210
1211 offset = ELF_ALIGN(doff + descsz);
1212 if (doff + descsz > size) {
1213 /*
1214 * We're past the end of the buffer.
1215 */
1216 return (offset >= size) ? offset : size;
1217 }
1218
1219
1220 if ((*flags & FLAGS_DID_OS_NOTE) == 0) {
1221 if (do_os_note(ms, nbuf, xnh_type, swap,
1222 namesz, descsz, noff, doff, flags))
1223 return offset;
1224 }
1225
1226 if ((*flags & FLAGS_DID_BUILD_ID) == 0) {
1227 if (do_bid_note(ms, nbuf, xnh_type, swap,
1228 namesz, descsz, noff, doff, flags))
1229 return offset;
1230 }
1231
1232 if ((*flags & FLAGS_DID_NETBSD_PAX) == 0) {
1233 if (do_pax_note(ms, nbuf, xnh_type, swap,
1234 namesz, descsz, noff, doff, flags))
1235 return offset;
1236 }
1237
1238 if ((*flags & FLAGS_DID_CORE) == 0) {
1239 if (do_core_note(ms, nbuf, xnh_type, swap,
1240 namesz, descsz, noff, doff, flags, size, clazz))
1241 return offset;
1242 }
1243
1244 if ((*flags & FLAGS_DID_AUXV) == 0) {
1245 if (do_auxv_note(ms, nbuf, xnh_type, swap,
1246 namesz, descsz, noff, doff, flags, size, clazz,
1247 fd, ph_off, ph_num, fsize))
1248 return offset;
1249 }
1250
1251 if (namesz == 7 && strcmp(RCAST(char *, &nbuf[noff]), "NetBSD") == 0) {
1252 int descw, flag;
1253 const char *str, *tag;
1254 if (descsz > 100)
1255 descsz = 100;
1256 switch (xnh_type) {
1257 case NT_NETBSD_VERSION:
1258 return offset;
1259 case NT_NETBSD_MARCH:
1260 flag = FLAGS_DID_NETBSD_MARCH;
1261 tag = "compiled for";
1262 break;
1263 case NT_NETBSD_CMODEL:
1264 flag = FLAGS_DID_NETBSD_CMODEL;
1265 tag = "compiler model";
1266 break;
1267 case NT_NETBSD_EMULATION:
1268 flag = FLAGS_DID_NETBSD_EMULATION;
1269 tag = "emulation:";
1270 break;
1271 default:
1272 if (*flags & FLAGS_DID_NETBSD_UNKNOWN)
1273 return offset;
1274 *flags |= FLAGS_DID_NETBSD_UNKNOWN;
1275 if (file_printf(ms, ", note=%u", xnh_type) == -1)
1276 return offset;
1277 return offset;
1278 }
1279
1280 if (*flags & flag)
1281 return offset;
1282 str = RCAST(const char *, &nbuf[doff]);
1283 descw = CAST(int, descsz);
1284 *flags |= flag;
1285 file_printf(ms, ", %s: %s", tag,
1286 file_copystr(buf, sizeof(buf), descw, str));
1287 return offset;
1288 }
1289
1290 return offset;
1291 }
1292
1293 /* SunOS 5.x hardware capability descriptions */
1294 typedef struct cap_desc {
1295 uint64_t cd_mask;
1296 const char *cd_name;
1297 } cap_desc_t;
1298
1299 static const cap_desc_t cap_desc_sparc[] = {
1300 { AV_SPARC_MUL32, "MUL32" },
1301 { AV_SPARC_DIV32, "DIV32" },
1302 { AV_SPARC_FSMULD, "FSMULD" },
1303 { AV_SPARC_V8PLUS, "V8PLUS" },
1304 { AV_SPARC_POPC, "POPC" },
1305 { AV_SPARC_VIS, "VIS" },
1306 { AV_SPARC_VIS2, "VIS2" },
1307 { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
1308 { AV_SPARC_FMAF, "FMAF" },
1309 { AV_SPARC_FJFMAU, "FJFMAU" },
1310 { AV_SPARC_IMA, "IMA" },
1311 { 0, NULL }
1312 };
1313
1314 static const cap_desc_t cap_desc_386[] = {
1315 { AV_386_FPU, "FPU" },
1316 { AV_386_TSC, "TSC" },
1317 { AV_386_CX8, "CX8" },
1318 { AV_386_SEP, "SEP" },
1319 { AV_386_AMD_SYSC, "AMD_SYSC" },
1320 { AV_386_CMOV, "CMOV" },
1321 { AV_386_MMX, "MMX" },
1322 { AV_386_AMD_MMX, "AMD_MMX" },
1323 { AV_386_AMD_3DNow, "AMD_3DNow" },
1324 { AV_386_AMD_3DNowx, "AMD_3DNowx" },
1325 { AV_386_FXSR, "FXSR" },
1326 { AV_386_SSE, "SSE" },
1327 { AV_386_SSE2, "SSE2" },
1328 { AV_386_PAUSE, "PAUSE" },
1329 { AV_386_SSE3, "SSE3" },
1330 { AV_386_MON, "MON" },
1331 { AV_386_CX16, "CX16" },
1332 { AV_386_AHF, "AHF" },
1333 { AV_386_TSCP, "TSCP" },
1334 { AV_386_AMD_SSE4A, "AMD_SSE4A" },
1335 { AV_386_POPCNT, "POPCNT" },
1336 { AV_386_AMD_LZCNT, "AMD_LZCNT" },
1337 { AV_386_SSSE3, "SSSE3" },
1338 { AV_386_SSE4_1, "SSE4.1" },
1339 { AV_386_SSE4_2, "SSE4.2" },
1340 { 0, NULL }
1341 };
1342
1343 private int
doshn(struct magic_set * ms,int clazz,int swap,int fd,off_t off,int num,size_t size,off_t fsize,int mach,int strtab,int * flags,uint16_t * notecount)1344 doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
1345 size_t size, off_t fsize, int mach, int strtab, int *flags,
1346 uint16_t *notecount)
1347 {
1348 Elf32_Shdr sh32;
1349 Elf64_Shdr sh64;
1350 int stripped = 1, has_debug_info = 0;
1351 size_t nbadcap = 0;
1352 void *nbuf;
1353 off_t noff, coff, name_off, offs;
1354 uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilities */
1355 uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilities */
1356 char name[50];
1357 ssize_t namesize;
1358
1359 if (ms->flags & MAGIC_MIME)
1360 return 0;
1361
1362 if (num == 0) {
1363 if (file_printf(ms, ", no section header") == -1)
1364 return -1;
1365 return 0;
1366 }
1367 if (size != xsh_sizeof) {
1368 if (file_printf(ms, ", corrupted section header size") == -1)
1369 return -1;
1370 return 0;
1371 }
1372
1373 /* Read offset of name section to be able to read section names later */
1374 offs = CAST(off_t, (off + size * strtab));
1375 if (pread(fd, xsh_addr, xsh_sizeof, offs) < CAST(ssize_t, xsh_sizeof)) {
1376 if (file_printf(ms, ", missing section headers at %jd",
1377 (intmax_t)offs) == -1)
1378 return -1;
1379 return 0;
1380 }
1381 name_off = xsh_offset;
1382
1383 if (fsize != SIZE_UNKNOWN && fsize < name_off) {
1384 if (file_printf(ms, ", too large section header offset %jd",
1385 (intmax_t)name_off) == -1)
1386 return -1;
1387 return 0;
1388 }
1389
1390 for ( ; num; num--) {
1391 /* Read the name of this section. */
1392 offs = name_off + xsh_name;
1393 if ((namesize = pread(fd, name, sizeof(name) - 1, offs))
1394 == -1) {
1395 if (file_printf(ms,
1396 ", can't read name of elf section at %jd",
1397 (intmax_t)offs) == -1)
1398 return -1;
1399 return 0;
1400 }
1401 name[namesize] = '\0';
1402 if (strcmp(name, ".debug_info") == 0) {
1403 has_debug_info = 1;
1404 stripped = 0;
1405 }
1406
1407 if (pread(fd, xsh_addr, xsh_sizeof, off) <
1408 CAST(ssize_t, xsh_sizeof)) {
1409 if (file_printf(ms, ", can't read elf section at %jd",
1410 (intmax_t)off) == -1)
1411 return -1;
1412 return 0;
1413 }
1414 off += size;
1415
1416 /* Things we can determine before we seek */
1417 switch (xsh_type) {
1418 case SHT_SYMTAB:
1419 #if 0
1420 case SHT_DYNSYM:
1421 #endif
1422 stripped = 0;
1423 break;
1424 default:
1425 if (fsize != SIZE_UNKNOWN && xsh_offset > fsize) {
1426 /* Perhaps warn here */
1427 continue;
1428 }
1429 break;
1430 }
1431
1432
1433 /* Things we can determine when we seek */
1434 switch (xsh_type) {
1435 case SHT_NOTE:
1436 if (CAST(uintmax_t, (xsh_size + xsh_offset)) >
1437 CAST(uintmax_t, fsize)) {
1438 if (file_printf(ms,
1439 ", note offset/size %#" INTMAX_T_FORMAT
1440 "x+%#" INTMAX_T_FORMAT "x exceeds"
1441 " file size %#" INTMAX_T_FORMAT "x",
1442 CAST(uintmax_t, xsh_offset),
1443 CAST(uintmax_t, xsh_size),
1444 CAST(uintmax_t, fsize)) == -1)
1445 return -1;
1446 return 0;
1447 }
1448 if ((nbuf = malloc(xsh_size)) == NULL) {
1449 file_error(ms, errno, "Cannot allocate memory"
1450 " for note");
1451 return -1;
1452 }
1453 offs = xsh_offset;
1454 if (pread(fd, nbuf, xsh_size, offs) <
1455 CAST(ssize_t, xsh_size)) {
1456 free(nbuf);
1457 if (file_printf(ms,
1458 ", can't read elf note at %jd",
1459 (intmax_t)offs) == -1)
1460 return -1;
1461 return 0;
1462 }
1463
1464 noff = 0;
1465 for (;;) {
1466 if (noff >= CAST(off_t, xsh_size))
1467 break;
1468 noff = donote(ms, nbuf, CAST(size_t, noff),
1469 xsh_size, clazz, swap, 4, flags, notecount,
1470 fd, 0, 0, 0);
1471 if (noff == 0)
1472 break;
1473 }
1474 free(nbuf);
1475 break;
1476 case SHT_SUNW_cap:
1477 switch (mach) {
1478 case EM_SPARC:
1479 case EM_SPARCV9:
1480 case EM_IA_64:
1481 case EM_386:
1482 case EM_AMD64:
1483 break;
1484 default:
1485 goto skip;
1486 }
1487
1488 if (nbadcap > 5)
1489 break;
1490 if (lseek(fd, xsh_offset, SEEK_SET)
1491 == CAST(off_t, -1)) {
1492 file_badseek(ms);
1493 return -1;
1494 }
1495 coff = 0;
1496 for (;;) {
1497 Elf32_Cap cap32;
1498 Elf64_Cap cap64;
1499 char cbuf[/*CONSTCOND*/
1500 MAX(sizeof(cap32), sizeof(cap64))];
1501 if ((coff += xcap_sizeof) >
1502 CAST(off_t, xsh_size))
1503 break;
1504 if (read(fd, cbuf, CAST(size_t, xcap_sizeof)) !=
1505 CAST(ssize_t, xcap_sizeof)) {
1506 file_badread(ms);
1507 return -1;
1508 }
1509 if (cbuf[0] == 'A') {
1510 #ifdef notyet
1511 char *p = cbuf + 1;
1512 uint32_t len, tag;
1513 memcpy(&len, p, sizeof(len));
1514 p += 4;
1515 len = getu32(swap, len);
1516 if (memcmp("gnu", p, 3) != 0) {
1517 if (file_printf(ms,
1518 ", unknown capability %.3s", p)
1519 == -1)
1520 return -1;
1521 break;
1522 }
1523 p += strlen(p) + 1;
1524 tag = *p++;
1525 memcpy(&len, p, sizeof(len));
1526 p += 4;
1527 len = getu32(swap, len);
1528 if (tag != 1) {
1529 if (file_printf(ms, ", unknown gnu"
1530 " capability tag %d", tag)
1531 == -1)
1532 return -1;
1533 break;
1534 }
1535 // gnu attributes
1536 #endif
1537 break;
1538 }
1539 memcpy(xcap_addr, cbuf, xcap_sizeof);
1540 switch (xcap_tag) {
1541 case CA_SUNW_NULL:
1542 break;
1543 case CA_SUNW_HW_1:
1544 cap_hw1 |= xcap_val;
1545 break;
1546 case CA_SUNW_SF_1:
1547 cap_sf1 |= xcap_val;
1548 break;
1549 default:
1550 if (file_printf(ms,
1551 ", with unknown capability "
1552 "%#" INT64_T_FORMAT "x = %#"
1553 INT64_T_FORMAT "x",
1554 CAST(unsigned long long, xcap_tag),
1555 CAST(unsigned long long, xcap_val))
1556 == -1)
1557 return -1;
1558 if (nbadcap++ > 2)
1559 coff = xsh_size;
1560 break;
1561 }
1562 }
1563 /*FALLTHROUGH*/
1564 skip:
1565 default:
1566 break;
1567 }
1568 }
1569
1570 if (has_debug_info) {
1571 if (file_printf(ms, ", with debug_info") == -1)
1572 return -1;
1573 }
1574 if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
1575 return -1;
1576 if (cap_hw1) {
1577 const cap_desc_t *cdp;
1578 switch (mach) {
1579 case EM_SPARC:
1580 case EM_SPARC32PLUS:
1581 case EM_SPARCV9:
1582 cdp = cap_desc_sparc;
1583 break;
1584 case EM_386:
1585 case EM_IA_64:
1586 case EM_AMD64:
1587 cdp = cap_desc_386;
1588 break;
1589 default:
1590 cdp = NULL;
1591 break;
1592 }
1593 if (file_printf(ms, ", uses") == -1)
1594 return -1;
1595 if (cdp) {
1596 while (cdp->cd_name) {
1597 if (cap_hw1 & cdp->cd_mask) {
1598 if (file_printf(ms,
1599 " %s", cdp->cd_name) == -1)
1600 return -1;
1601 cap_hw1 &= ~cdp->cd_mask;
1602 }
1603 ++cdp;
1604 }
1605 if (cap_hw1)
1606 if (file_printf(ms,
1607 " unknown hardware capability %#"
1608 INT64_T_FORMAT "x",
1609 CAST(unsigned long long, cap_hw1)) == -1)
1610 return -1;
1611 } else {
1612 if (file_printf(ms,
1613 " hardware capability %#" INT64_T_FORMAT "x",
1614 CAST(unsigned long long, cap_hw1)) == -1)
1615 return -1;
1616 }
1617 }
1618 if (cap_sf1) {
1619 if (cap_sf1 & SF1_SUNW_FPUSED) {
1620 if (file_printf(ms,
1621 (cap_sf1 & SF1_SUNW_FPKNWN)
1622 ? ", uses frame pointer"
1623 : ", not known to use frame pointer") == -1)
1624 return -1;
1625 }
1626 cap_sf1 &= ~SF1_SUNW_MASK;
1627 if (cap_sf1)
1628 if (file_printf(ms,
1629 ", with unknown software capability %#"
1630 INT64_T_FORMAT "x",
1631 CAST(unsigned long long, cap_sf1)) == -1)
1632 return -1;
1633 }
1634 return 0;
1635 }
1636
1637 /*
1638 * Look through the program headers of an executable image, to determine
1639 * if it is statically or dynamically linked. If it has a dynamic section,
1640 * it is pie, and does not have an interpreter or needed libraries, we
1641 * call it static pie.
1642 */
1643 private int
dophn_exec(struct magic_set * ms,int clazz,int swap,int fd,off_t off,int num,size_t size,off_t fsize,int sh_num,int * flags,uint16_t * notecount)1644 dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
1645 int num, size_t size, off_t fsize, int sh_num, int *flags,
1646 uint16_t *notecount)
1647 {
1648 Elf32_Phdr ph32;
1649 Elf64_Phdr ph64;
1650 const char *linking_style;
1651 unsigned char nbuf[BUFSIZ];
1652 char ibuf[BUFSIZ];
1653 char interp[BUFSIZ];
1654 ssize_t bufsize;
1655 size_t offset, align, len, need = 0;
1656 int pie = 0, dynamic = 0;
1657
1658 if (num == 0) {
1659 if (file_printf(ms, ", no program header") == -1)
1660 return -1;
1661 return 0;
1662 }
1663 if (size != xph_sizeof) {
1664 if (file_printf(ms, ", corrupted program header size") == -1)
1665 return -1;
1666 return 0;
1667 }
1668
1669 interp[0] = '\0';
1670 for ( ; num; num--) {
1671 int doread;
1672 if (pread(fd, xph_addr, xph_sizeof, off) <
1673 CAST(ssize_t, xph_sizeof)) {
1674 if (file_printf(ms,
1675 ", can't read elf program headers at %jd",
1676 (intmax_t)off) == -1)
1677 return -1;
1678 return 0;
1679 }
1680
1681 off += size;
1682 bufsize = 0;
1683 align = 4;
1684
1685 /* Things we can determine before we seek */
1686 switch (xph_type) {
1687 case PT_DYNAMIC:
1688 doread = 1;
1689 break;
1690 case PT_NOTE:
1691 if (sh_num) /* Did this through section headers */
1692 continue;
1693 if (((align = xph_align) & 0x80000000UL) != 0 ||
1694 align < 4) {
1695 if (file_printf(ms,
1696 ", invalid note alignment %#lx",
1697 CAST(unsigned long, align)) == -1)
1698 return -1;
1699 align = 4;
1700 }
1701 /*FALLTHROUGH*/
1702 case PT_INTERP:
1703 doread = 1;
1704 break;
1705 default:
1706 doread = 0;
1707 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
1708 /* Maybe warn here? */
1709 continue;
1710 }
1711 break;
1712 }
1713
1714 if (doread) {
1715 len = xph_filesz < sizeof(nbuf) ? xph_filesz
1716 : sizeof(nbuf);
1717 off_t offs = xph_offset;
1718 bufsize = pread(fd, nbuf, len, offs);
1719 if (bufsize == -1) {
1720 if (file_printf(ms,
1721 ", can't read section at %jd",
1722 (intmax_t)offs) == -1)
1723 return -1;
1724 return 0;
1725 }
1726 } else
1727 len = 0;
1728
1729 /* Things we can determine when we seek */
1730 switch (xph_type) {
1731 case PT_DYNAMIC:
1732 dynamic = 1;
1733 offset = 0;
1734 // Let DF_1 determine if we are PIE or not.
1735 ms->mode &= ~0111;
1736 for (;;) {
1737 if (offset >= CAST(size_t, bufsize))
1738 break;
1739 offset = dodynamic(ms, nbuf, offset,
1740 CAST(size_t, bufsize), clazz, swap,
1741 &pie, &need);
1742 if (offset == 0)
1743 break;
1744 }
1745 if (ms->flags & MAGIC_MIME)
1746 continue;
1747 break;
1748
1749 case PT_INTERP:
1750 need++;
1751 if (ms->flags & MAGIC_MIME)
1752 continue;
1753 if (bufsize && nbuf[0]) {
1754 nbuf[bufsize - 1] = '\0';
1755 memcpy(interp, nbuf, CAST(size_t, bufsize));
1756 } else
1757 strlcpy(interp, "*empty*", sizeof(interp));
1758 break;
1759 case PT_NOTE:
1760 if (ms->flags & MAGIC_MIME)
1761 return 0;
1762 /*
1763 * This is a PT_NOTE section; loop through all the notes
1764 * in the section.
1765 */
1766 offset = 0;
1767 for (;;) {
1768 if (offset >= CAST(size_t, bufsize))
1769 break;
1770 offset = donote(ms, nbuf, offset,
1771 CAST(size_t, bufsize), clazz, swap, align,
1772 flags, notecount, fd, 0, 0, 0);
1773 if (offset == 0)
1774 break;
1775 }
1776 break;
1777 default:
1778 if (ms->flags & MAGIC_MIME)
1779 continue;
1780 break;
1781 }
1782 }
1783 if (ms->flags & MAGIC_MIME)
1784 return 0;
1785 if (dynamic) {
1786 if (pie && need == 0)
1787 linking_style = "static-pie";
1788 else
1789 linking_style = "dynamically";
1790 } else {
1791 linking_style = "statically";
1792 }
1793 if (file_printf(ms, ", %s linked", linking_style) == -1)
1794 return -1;
1795 if (interp[0])
1796 if (file_printf(ms, ", interpreter %s",
1797 file_printable(ibuf, sizeof(ibuf), interp, sizeof(interp)))
1798 == -1)
1799 return -1;
1800 return 0;
1801 }
1802
1803
1804 protected int
file_tryelf(struct magic_set * ms,const struct buffer * b)1805 file_tryelf(struct magic_set *ms, const struct buffer *b)
1806 {
1807 int fd = b->fd;
1808 const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
1809 size_t nbytes = b->flen;
1810 union {
1811 int32_t l;
1812 char c[sizeof(int32_t)];
1813 } u;
1814 int clazz;
1815 int swap;
1816 struct stat st;
1817 const struct stat *stp;
1818 off_t fsize;
1819 int flags = 0;
1820 Elf32_Ehdr elf32hdr;
1821 Elf64_Ehdr elf64hdr;
1822 uint16_t type, phnum, shnum, notecount;
1823
1824 if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
1825 return 0;
1826 /*
1827 * ELF executables have multiple section headers in arbitrary
1828 * file locations and thus file(1) cannot determine it from easily.
1829 * Instead we traverse thru all section headers until a symbol table
1830 * one is found or else the binary is stripped.
1831 * Return immediately if it's not ELF (so we avoid pipe2file unless
1832 * needed).
1833 */
1834 if (buf[EI_MAG0] != ELFMAG0
1835 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
1836 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
1837 return 0;
1838
1839 /*
1840 * If we cannot seek, it must be a pipe, socket or fifo.
1841 */
1842 if((lseek(fd, CAST(off_t, 0), SEEK_SET) == CAST(off_t, -1))
1843 && (errno == ESPIPE))
1844 fd = file_pipe2file(ms, fd, buf, nbytes);
1845
1846 if (fd == -1) {
1847 file_badread(ms);
1848 return -1;
1849 }
1850
1851 stp = &b->st;
1852 /*
1853 * b->st.st_size != 0 if previous fstat() succeeded,
1854 * which is likely, we can avoid extra stat() call.
1855 */
1856 if (b->st.st_size == 0) {
1857 stp = &st;
1858 if (fstat(fd, &st) == -1) {
1859 file_badread(ms);
1860 return -1;
1861 }
1862 }
1863 if (S_ISREG(stp->st_mode) || stp->st_size != 0)
1864 fsize = stp->st_size;
1865 else
1866 fsize = SIZE_UNKNOWN;
1867
1868 clazz = buf[EI_CLASS];
1869
1870 switch (clazz) {
1871 case ELFCLASS32:
1872 #undef elf_getu
1873 #define elf_getu(a, b) elf_getu32(a, b)
1874 #undef elfhdr
1875 #define elfhdr elf32hdr
1876 #include "elfclass.h"
1877 case ELFCLASS64:
1878 #undef elf_getu
1879 #define elf_getu(a, b) elf_getu64(a, b)
1880 #undef elfhdr
1881 #define elfhdr elf64hdr
1882 #include "elfclass.h"
1883 default:
1884 if (file_printf(ms, ", unknown class %d", clazz) == -1)
1885 return -1;
1886 break;
1887 }
1888 return 0;
1889 }
1890 #endif
1891