readelf.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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. #ifndef lint
  29. FILE_RCSID("@(#)$File: readelf.c,v 1.127 2015/11/18 12:29:29 christos Exp $")
  30. #endif
  31. #ifdef BUILTIN_ELF
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <stdlib.h>
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif
  38. #include "readelf.h"
  39. #include "magic.h"
  40. #ifdef ELFCORE
  41. private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
  42. off_t, int *, uint16_t *);
  43. #endif
  44. private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
  45. off_t, int, int *, uint16_t *);
  46. private int doshn(struct magic_set *, int, int, int, off_t, int, size_t,
  47. off_t, int, int, int *, uint16_t *);
  48. private size_t donote(struct magic_set *, void *, size_t, size_t, int,
  49. int, size_t, int *, uint16_t *, int, off_t, int, off_t);
  50. #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
  51. #define isquote(c) (strchr("'\"`", (c)) != NULL)
  52. private uint16_t getu16(int, uint16_t);
  53. private uint32_t getu32(int, uint32_t);
  54. private uint64_t getu64(int, uint64_t);
  55. #define MAX_PHNUM 128
  56. #define MAX_SHNUM 32768
  57. #define SIZE_UNKNOWN ((off_t)-1)
  58. private int
  59. toomany(struct magic_set *ms, const char *name, uint16_t num)
  60. {
  61. if (file_printf(ms, ", too many %s (%u)", name, num
  62. ) == -1)
  63. return -1;
  64. return 0;
  65. }
  66. private uint16_t
  67. getu16(int swap, uint16_t value)
  68. {
  69. union {
  70. uint16_t ui;
  71. char c[2];
  72. } retval, tmpval;
  73. if (swap) {
  74. tmpval.ui = value;
  75. retval.c[0] = tmpval.c[1];
  76. retval.c[1] = tmpval.c[0];
  77. return retval.ui;
  78. } else
  79. return value;
  80. }
  81. private uint32_t
  82. getu32(int swap, uint32_t value)
  83. {
  84. union {
  85. uint32_t ui;
  86. char c[4];
  87. } retval, tmpval;
  88. if (swap) {
  89. tmpval.ui = value;
  90. retval.c[0] = tmpval.c[3];
  91. retval.c[1] = tmpval.c[2];
  92. retval.c[2] = tmpval.c[1];
  93. retval.c[3] = tmpval.c[0];
  94. return retval.ui;
  95. } else
  96. return value;
  97. }
  98. private uint64_t
  99. getu64(int swap, uint64_t value)
  100. {
  101. union {
  102. uint64_t ui;
  103. char c[8];
  104. } retval, tmpval;
  105. if (swap) {
  106. tmpval.ui = value;
  107. retval.c[0] = tmpval.c[7];
  108. retval.c[1] = tmpval.c[6];
  109. retval.c[2] = tmpval.c[5];
  110. retval.c[3] = tmpval.c[4];
  111. retval.c[4] = tmpval.c[3];
  112. retval.c[5] = tmpval.c[2];
  113. retval.c[6] = tmpval.c[1];
  114. retval.c[7] = tmpval.c[0];
  115. return retval.ui;
  116. } else
  117. return value;
  118. }
  119. #define elf_getu16(swap, value) getu16(swap, value)
  120. #define elf_getu32(swap, value) getu32(swap, value)
  121. #define elf_getu64(swap, value) getu64(swap, value)
  122. #define xsh_addr (clazz == ELFCLASS32 \
  123. ? (void *)&sh32 \
  124. : (void *)&sh64)
  125. #define xsh_sizeof (clazz == ELFCLASS32 \
  126. ? sizeof(sh32) \
  127. : sizeof(sh64))
  128. #define xsh_size (size_t)(clazz == ELFCLASS32 \
  129. ? elf_getu32(swap, sh32.sh_size) \
  130. : elf_getu64(swap, sh64.sh_size))
  131. #define xsh_offset (off_t)(clazz == ELFCLASS32 \
  132. ? elf_getu32(swap, sh32.sh_offset) \
  133. : elf_getu64(swap, sh64.sh_offset))
  134. #define xsh_type (clazz == ELFCLASS32 \
  135. ? elf_getu32(swap, sh32.sh_type) \
  136. : elf_getu32(swap, sh64.sh_type))
  137. #define xsh_name (clazz == ELFCLASS32 \
  138. ? elf_getu32(swap, sh32.sh_name) \
  139. : elf_getu32(swap, sh64.sh_name))
  140. #define xph_addr (clazz == ELFCLASS32 \
  141. ? (void *) &ph32 \
  142. : (void *) &ph64)
  143. #define xph_sizeof (clazz == ELFCLASS32 \
  144. ? sizeof(ph32) \
  145. : sizeof(ph64))
  146. #define xph_type (clazz == ELFCLASS32 \
  147. ? elf_getu32(swap, ph32.p_type) \
  148. : elf_getu32(swap, ph64.p_type))
  149. #define xph_offset (off_t)(clazz == ELFCLASS32 \
  150. ? elf_getu32(swap, ph32.p_offset) \
  151. : elf_getu64(swap, ph64.p_offset))
  152. #define xph_align (size_t)((clazz == ELFCLASS32 \
  153. ? (off_t) (ph32.p_align ? \
  154. elf_getu32(swap, ph32.p_align) : 4) \
  155. : (off_t) (ph64.p_align ? \
  156. elf_getu64(swap, ph64.p_align) : 4)))
  157. #define xph_vaddr (size_t)((clazz == ELFCLASS32 \
  158. ? (off_t) (ph32.p_vaddr ? \
  159. elf_getu32(swap, ph32.p_vaddr) : 4) \
  160. : (off_t) (ph64.p_vaddr ? \
  161. elf_getu64(swap, ph64.p_vaddr) : 4)))
  162. #define xph_filesz (size_t)((clazz == ELFCLASS32 \
  163. ? elf_getu32(swap, ph32.p_filesz) \
  164. : elf_getu64(swap, ph64.p_filesz)))
  165. #define xnh_addr (clazz == ELFCLASS32 \
  166. ? (void *)&nh32 \
  167. : (void *)&nh64)
  168. #define xph_memsz (size_t)((clazz == ELFCLASS32 \
  169. ? elf_getu32(swap, ph32.p_memsz) \
  170. : elf_getu64(swap, ph64.p_memsz)))
  171. #define xnh_sizeof (clazz == ELFCLASS32 \
  172. ? sizeof(nh32) \
  173. : sizeof(nh64))
  174. #define xnh_type (clazz == ELFCLASS32 \
  175. ? elf_getu32(swap, nh32.n_type) \
  176. : elf_getu32(swap, nh64.n_type))
  177. #define xnh_namesz (clazz == ELFCLASS32 \
  178. ? elf_getu32(swap, nh32.n_namesz) \
  179. : elf_getu32(swap, nh64.n_namesz))
  180. #define xnh_descsz (clazz == ELFCLASS32 \
  181. ? elf_getu32(swap, nh32.n_descsz) \
  182. : elf_getu32(swap, nh64.n_descsz))
  183. #define prpsoffsets(i) (clazz == ELFCLASS32 \
  184. ? prpsoffsets32[i] \
  185. : prpsoffsets64[i])
  186. #define xcap_addr (clazz == ELFCLASS32 \
  187. ? (void *)&cap32 \
  188. : (void *)&cap64)
  189. #define xcap_sizeof (clazz == ELFCLASS32 \
  190. ? sizeof cap32 \
  191. : sizeof cap64)
  192. #define xcap_tag (clazz == ELFCLASS32 \
  193. ? elf_getu32(swap, cap32.c_tag) \
  194. : elf_getu64(swap, cap64.c_tag))
  195. #define xcap_val (clazz == ELFCLASS32 \
  196. ? elf_getu32(swap, cap32.c_un.c_val) \
  197. : elf_getu64(swap, cap64.c_un.c_val))
  198. #define xauxv_addr (clazz == ELFCLASS32 \
  199. ? (void *)&auxv32 \
  200. : (void *)&auxv64)
  201. #define xauxv_sizeof (clazz == ELFCLASS32 \
  202. ? sizeof(auxv32) \
  203. : sizeof(auxv64))
  204. #define xauxv_type (clazz == ELFCLASS32 \
  205. ? elf_getu32(swap, auxv32.a_type) \
  206. : elf_getu64(swap, auxv64.a_type))
  207. #define xauxv_val (clazz == ELFCLASS32 \
  208. ? elf_getu32(swap, auxv32.a_v) \
  209. : elf_getu64(swap, auxv64.a_v))
  210. #ifdef ELFCORE
  211. /*
  212. * Try larger offsets first to avoid false matches
  213. * from earlier data that happen to look like strings.
  214. */
  215. static const size_t prpsoffsets32[] = {
  216. #ifdef USE_NT_PSINFO
  217. 104, /* SunOS 5.x (command line) */
  218. 88, /* SunOS 5.x (short name) */
  219. #endif /* USE_NT_PSINFO */
  220. 100, /* SunOS 5.x (command line) */
  221. 84, /* SunOS 5.x (short name) */
  222. 44, /* Linux (command line) */
  223. 28, /* Linux 2.0.36 (short name) */
  224. 8, /* FreeBSD */
  225. };
  226. static const size_t prpsoffsets64[] = {
  227. #ifdef USE_NT_PSINFO
  228. 152, /* SunOS 5.x (command line) */
  229. 136, /* SunOS 5.x (short name) */
  230. #endif /* USE_NT_PSINFO */
  231. 136, /* SunOS 5.x, 64-bit (command line) */
  232. 120, /* SunOS 5.x, 64-bit (short name) */
  233. 56, /* Linux (command line) */
  234. 40, /* Linux (tested on core from 2.4.x, short name) */
  235. 16, /* FreeBSD, 64-bit */
  236. };
  237. #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
  238. #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
  239. #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
  240. /*
  241. * Look through the program headers of an executable image, searching
  242. * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
  243. * "FreeBSD"; if one is found, try looking in various places in its
  244. * contents for a 16-character string containing only printable
  245. * characters - if found, that string should be the name of the program
  246. * that dropped core. Note: right after that 16-character string is,
  247. * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
  248. * Linux, a longer string (80 characters, in 5.x, probably other
  249. * SVR4-flavored systems, and Linux) containing the start of the
  250. * command line for that program.
  251. *
  252. * SunOS 5.x core files contain two PT_NOTE sections, with the types
  253. * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
  254. * same info about the command name and command line, so it probably
  255. * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
  256. * above (see USE_NT_PSINFO), in case we ever decide to do so. The
  257. * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
  258. * the SunOS 5.x file command relies on this (and prefers the latter).
  259. *
  260. * The signal number probably appears in a section of type NT_PRSTATUS,
  261. * but that's also rather OS-dependent, in ways that are harder to
  262. * dissect with heuristics, so I'm not bothering with the signal number.
  263. * (I suppose the signal number could be of interest in situations where
  264. * you don't have the binary of the program that dropped core; if you
  265. * *do* have that binary, the debugger will probably tell you what
  266. * signal it was.)
  267. */
  268. #define OS_STYLE_SVR4 0
  269. #define OS_STYLE_FREEBSD 1
  270. #define OS_STYLE_NETBSD 2
  271. private const char os_style_names[][8] = {
  272. "SVR4",
  273. "FreeBSD",
  274. "NetBSD",
  275. };
  276. #define FLAGS_DID_CORE 0x001
  277. #define FLAGS_DID_OS_NOTE 0x002
  278. #define FLAGS_DID_BUILD_ID 0x004
  279. #define FLAGS_DID_CORE_STYLE 0x008
  280. #define FLAGS_DID_NETBSD_PAX 0x010
  281. #define FLAGS_DID_NETBSD_MARCH 0x020
  282. #define FLAGS_DID_NETBSD_CMODEL 0x040
  283. #define FLAGS_DID_NETBSD_UNKNOWN 0x080
  284. #define FLAGS_IS_CORE 0x100
  285. #define FLAGS_DID_AUXV 0x200
  286. private int
  287. dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
  288. int num, size_t size, off_t fsize, int *flags, uint16_t *notecount)
  289. {
  290. Elf32_Phdr ph32;
  291. Elf64_Phdr ph64;
  292. size_t offset, len;
  293. unsigned char nbuf[BUFSIZ];
  294. ssize_t bufsize;
  295. off_t ph_off = off;
  296. int ph_num = num;
  297. if (size != xph_sizeof) {
  298. if (file_printf(ms, ", corrupted program header size") == -1)
  299. return -1;
  300. return 0;
  301. }
  302. /*
  303. * Loop through all the program headers.
  304. */
  305. for ( ; num; num--) {
  306. if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) {
  307. file_badread(ms);
  308. return -1;
  309. }
  310. off += size;
  311. if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
  312. /* Perhaps warn here */
  313. continue;
  314. }
  315. if (xph_type != PT_NOTE)
  316. continue;
  317. /*
  318. * This is a PT_NOTE section; loop through all the notes
  319. * in the section.
  320. */
  321. len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
  322. if ((bufsize = pread(fd, nbuf, len, xph_offset)) == -1) {
  323. file_badread(ms);
  324. return -1;
  325. }
  326. offset = 0;
  327. for (;;) {
  328. if (offset >= (size_t)bufsize)
  329. break;
  330. offset = donote(ms, nbuf, offset, (size_t)bufsize,
  331. clazz, swap, 4, flags, notecount, fd, ph_off,
  332. ph_num, fsize);
  333. if (offset == 0)
  334. break;
  335. }
  336. }
  337. return 0;
  338. }
  339. #endif
  340. static void
  341. do_note_netbsd_version(struct magic_set *ms, int swap, void *v)
  342. {
  343. uint32_t desc;
  344. (void)memcpy(&desc, v, sizeof(desc));
  345. desc = elf_getu32(swap, desc);
  346. if (file_printf(ms, ", for NetBSD") == -1)
  347. return;
  348. /*
  349. * The version number used to be stuck as 199905, and was thus
  350. * basically content-free. Newer versions of NetBSD have fixed
  351. * this and now use the encoding of __NetBSD_Version__:
  352. *
  353. * MMmmrrpp00
  354. *
  355. * M = major version
  356. * m = minor version
  357. * r = release ["",A-Z,Z[A-Z] but numeric]
  358. * p = patchlevel
  359. */
  360. if (desc > 100000000U) {
  361. uint32_t ver_patch = (desc / 100) % 100;
  362. uint32_t ver_rel = (desc / 10000) % 100;
  363. uint32_t ver_min = (desc / 1000000) % 100;
  364. uint32_t ver_maj = desc / 100000000;
  365. if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
  366. return;
  367. if (ver_rel == 0 && ver_patch != 0) {
  368. if (file_printf(ms, ".%u", ver_patch) == -1)
  369. return;
  370. } else if (ver_rel != 0) {
  371. while (ver_rel > 26) {
  372. if (file_printf(ms, "Z") == -1)
  373. return;
  374. ver_rel -= 26;
  375. }
  376. if (file_printf(ms, "%c", 'A' + ver_rel - 1)
  377. == -1)
  378. return;
  379. }
  380. }
  381. }
  382. static void
  383. do_note_freebsd_version(struct magic_set *ms, int swap, void *v)
  384. {
  385. uint32_t desc;
  386. (void)memcpy(&desc, v, sizeof(desc));
  387. desc = elf_getu32(swap, desc);
  388. if (file_printf(ms, ", for FreeBSD") == -1)
  389. return;
  390. /*
  391. * Contents is __FreeBSD_version, whose relation to OS
  392. * versions is defined by a huge table in the Porter's
  393. * Handbook. This is the general scheme:
  394. *
  395. * Releases:
  396. * Mmp000 (before 4.10)
  397. * Mmi0p0 (before 5.0)
  398. * Mmm0p0
  399. *
  400. * Development branches:
  401. * Mmpxxx (before 4.6)
  402. * Mmp1xx (before 4.10)
  403. * Mmi1xx (before 5.0)
  404. * M000xx (pre-M.0)
  405. * Mmm1xx
  406. *
  407. * M = major version
  408. * m = minor version
  409. * i = minor version increment (491000 -> 4.10)
  410. * p = patchlevel
  411. * x = revision
  412. *
  413. * The first release of FreeBSD to use ELF by default
  414. * was version 3.0.
  415. */
  416. if (desc == 460002) {
  417. if (file_printf(ms, " 4.6.2") == -1)
  418. return;
  419. } else if (desc < 460100) {
  420. if (file_printf(ms, " %d.%d", desc / 100000,
  421. desc / 10000 % 10) == -1)
  422. return;
  423. if (desc / 1000 % 10 > 0)
  424. if (file_printf(ms, ".%d", desc / 1000 % 10) == -1)
  425. return;
  426. if ((desc % 1000 > 0) || (desc % 100000 == 0))
  427. if (file_printf(ms, " (%d)", desc) == -1)
  428. return;
  429. } else if (desc < 500000) {
  430. if (file_printf(ms, " %d.%d", desc / 100000,
  431. desc / 10000 % 10 + desc / 1000 % 10) == -1)
  432. return;
  433. if (desc / 100 % 10 > 0) {
  434. if (file_printf(ms, " (%d)", desc) == -1)
  435. return;
  436. } else if (desc / 10 % 10 > 0) {
  437. if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
  438. return;
  439. }
  440. } else {
  441. if (file_printf(ms, " %d.%d", desc / 100000,
  442. desc / 1000 % 100) == -1)
  443. return;
  444. if ((desc / 100 % 10 > 0) ||
  445. (desc % 100000 / 100 == 0)) {
  446. if (file_printf(ms, " (%d)", desc) == -1)
  447. return;
  448. } else if (desc / 10 % 10 > 0) {
  449. if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
  450. return;
  451. }
  452. }
  453. }
  454. private int
  455. /*ARGSUSED*/
  456. do_bid_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  457. int swap __attribute__((__unused__)), uint32_t namesz, uint32_t descsz,
  458. size_t noff, size_t doff, int *flags)
  459. {
  460. if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  461. type == NT_GNU_BUILD_ID && (descsz == 16 || descsz == 20)) {
  462. uint8_t desc[20];
  463. uint32_t i;
  464. *flags |= FLAGS_DID_BUILD_ID;
  465. if (file_printf(ms, ", BuildID[%s]=", descsz == 16 ? "md5/uuid" :
  466. "sha1") == -1)
  467. return 1;
  468. (void)memcpy(desc, &nbuf[doff], descsz);
  469. for (i = 0; i < descsz; i++)
  470. if (file_printf(ms, "%02x", desc[i]) == -1)
  471. return 1;
  472. return 1;
  473. }
  474. return 0;
  475. }
  476. private int
  477. do_os_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  478. int swap, uint32_t namesz, uint32_t descsz,
  479. size_t noff, size_t doff, int *flags)
  480. {
  481. if (namesz == 5 && strcmp((char *)&nbuf[noff], "SuSE") == 0 &&
  482. type == NT_GNU_VERSION && descsz == 2) {
  483. *flags |= FLAGS_DID_OS_NOTE;
  484. file_printf(ms, ", for SuSE %d.%d", nbuf[doff], nbuf[doff + 1]);
  485. return 1;
  486. }
  487. if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  488. type == NT_GNU_VERSION && descsz == 16) {
  489. uint32_t desc[4];
  490. (void)memcpy(desc, &nbuf[doff], sizeof(desc));
  491. *flags |= FLAGS_DID_OS_NOTE;
  492. if (file_printf(ms, ", for GNU/") == -1)
  493. return 1;
  494. switch (elf_getu32(swap, desc[0])) {
  495. case GNU_OS_LINUX:
  496. if (file_printf(ms, "Linux") == -1)
  497. return 1;
  498. break;
  499. case GNU_OS_HURD:
  500. if (file_printf(ms, "Hurd") == -1)
  501. return 1;
  502. break;
  503. case GNU_OS_SOLARIS:
  504. if (file_printf(ms, "Solaris") == -1)
  505. return 1;
  506. break;
  507. case GNU_OS_KFREEBSD:
  508. if (file_printf(ms, "kFreeBSD") == -1)
  509. return 1;
  510. break;
  511. case GNU_OS_KNETBSD:
  512. if (file_printf(ms, "kNetBSD") == -1)
  513. return 1;
  514. break;
  515. default:
  516. if (file_printf(ms, "<unknown>") == -1)
  517. return 1;
  518. }
  519. if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
  520. elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
  521. return 1;
  522. return 1;
  523. }
  524. if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
  525. if (type == NT_NETBSD_VERSION && descsz == 4) {
  526. *flags |= FLAGS_DID_OS_NOTE;
  527. do_note_netbsd_version(ms, swap, &nbuf[doff]);
  528. return 1;
  529. }
  530. }
  531. if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0) {
  532. if (type == NT_FREEBSD_VERSION && descsz == 4) {
  533. *flags |= FLAGS_DID_OS_NOTE;
  534. do_note_freebsd_version(ms, swap, &nbuf[doff]);
  535. return 1;
  536. }
  537. }
  538. if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
  539. type == NT_OPENBSD_VERSION && descsz == 4) {
  540. *flags |= FLAGS_DID_OS_NOTE;
  541. if (file_printf(ms, ", for OpenBSD") == -1)
  542. return 1;
  543. /* Content of note is always 0 */
  544. return 1;
  545. }
  546. if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
  547. type == NT_DRAGONFLY_VERSION && descsz == 4) {
  548. uint32_t desc;
  549. *flags |= FLAGS_DID_OS_NOTE;
  550. if (file_printf(ms, ", for DragonFly") == -1)
  551. return 1;
  552. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  553. desc = elf_getu32(swap, desc);
  554. if (file_printf(ms, " %d.%d.%d", desc / 100000,
  555. desc / 10000 % 10, desc % 10000) == -1)
  556. return 1;
  557. return 1;
  558. }
  559. return 0;
  560. }
  561. private int
  562. do_pax_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  563. int swap, uint32_t namesz, uint32_t descsz,
  564. size_t noff, size_t doff, int *flags)
  565. {
  566. if (namesz == 4 && strcmp((char *)&nbuf[noff], "PaX") == 0 &&
  567. type == NT_NETBSD_PAX && descsz == 4) {
  568. static const char *pax[] = {
  569. "+mprotect",
  570. "-mprotect",
  571. "+segvguard",
  572. "-segvguard",
  573. "+ASLR",
  574. "-ASLR",
  575. };
  576. uint32_t desc;
  577. size_t i;
  578. int did = 0;
  579. *flags |= FLAGS_DID_NETBSD_PAX;
  580. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  581. desc = elf_getu32(swap, desc);
  582. if (desc && file_printf(ms, ", PaX: ") == -1)
  583. return 1;
  584. for (i = 0; i < __arraycount(pax); i++) {
  585. if (((1 << (int)i) & desc) == 0)
  586. continue;
  587. if (file_printf(ms, "%s%s", did++ ? "," : "",
  588. pax[i]) == -1)
  589. return 1;
  590. }
  591. return 1;
  592. }
  593. return 0;
  594. }
  595. private int
  596. do_core_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  597. int swap, uint32_t namesz, uint32_t descsz,
  598. size_t noff, size_t doff, int *flags, size_t size, int clazz)
  599. {
  600. #ifdef ELFCORE
  601. int os_style = -1;
  602. /*
  603. * Sigh. The 2.0.36 kernel in Debian 2.1, at
  604. * least, doesn't correctly implement name
  605. * sections, in core dumps, as specified by
  606. * the "Program Linking" section of "UNIX(R) System
  607. * V Release 4 Programmer's Guide: ANSI C and
  608. * Programming Support Tools", because my copy
  609. * clearly says "The first 'namesz' bytes in 'name'
  610. * contain a *null-terminated* [emphasis mine]
  611. * character representation of the entry's owner
  612. * or originator", but the 2.0.36 kernel code
  613. * doesn't include the terminating null in the
  614. * name....
  615. */
  616. if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
  617. (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
  618. os_style = OS_STYLE_SVR4;
  619. }
  620. if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
  621. os_style = OS_STYLE_FREEBSD;
  622. }
  623. if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
  624. == 0)) {
  625. os_style = OS_STYLE_NETBSD;
  626. }
  627. if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
  628. if (file_printf(ms, ", %s-style", os_style_names[os_style])
  629. == -1)
  630. return 1;
  631. *flags |= FLAGS_DID_CORE_STYLE;
  632. }
  633. switch (os_style) {
  634. case OS_STYLE_NETBSD:
  635. if (type == NT_NETBSD_CORE_PROCINFO) {
  636. char sbuf[512];
  637. uint32_t signo;
  638. /*
  639. * Extract the program name. It is at
  640. * offset 0x7c, and is up to 32-bytes,
  641. * including the terminating NUL.
  642. */
  643. if (file_printf(ms, ", from '%.31s'",
  644. file_printable(sbuf, sizeof(sbuf),
  645. (const char *)&nbuf[doff + 0x7c])) == -1)
  646. return 1;
  647. /*
  648. * Extract the signal number. It is at
  649. * offset 0x08.
  650. */
  651. (void)memcpy(&signo, &nbuf[doff + 0x08],
  652. sizeof(signo));
  653. if (file_printf(ms, " (signal %u)",
  654. elf_getu32(swap, signo)) == -1)
  655. return 1;
  656. *flags |= FLAGS_DID_CORE;
  657. return 1;
  658. }
  659. break;
  660. default:
  661. if (type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
  662. size_t i, j;
  663. unsigned char c;
  664. /*
  665. * Extract the program name. We assume
  666. * it to be 16 characters (that's what it
  667. * is in SunOS 5.x and Linux).
  668. *
  669. * Unfortunately, it's at a different offset
  670. * in various OSes, so try multiple offsets.
  671. * If the characters aren't all printable,
  672. * reject it.
  673. */
  674. for (i = 0; i < NOFFSETS; i++) {
  675. unsigned char *cname, *cp;
  676. size_t reloffset = prpsoffsets(i);
  677. size_t noffset = doff + reloffset;
  678. size_t k;
  679. for (j = 0; j < 16; j++, noffset++,
  680. reloffset++) {
  681. /*
  682. * Make sure we're not past
  683. * the end of the buffer; if
  684. * we are, just give up.
  685. */
  686. if (noffset >= size)
  687. goto tryanother;
  688. /*
  689. * Make sure we're not past
  690. * the end of the contents;
  691. * if we are, this obviously
  692. * isn't the right offset.
  693. */
  694. if (reloffset >= descsz)
  695. goto tryanother;
  696. c = nbuf[noffset];
  697. if (c == '\0') {
  698. /*
  699. * A '\0' at the
  700. * beginning is
  701. * obviously wrong.
  702. * Any other '\0'
  703. * means we're done.
  704. */
  705. if (j == 0)
  706. goto tryanother;
  707. else
  708. break;
  709. } else {
  710. /*
  711. * A nonprintable
  712. * character is also
  713. * wrong.
  714. */
  715. if (!isprint(c) || isquote(c))
  716. goto tryanother;
  717. }
  718. }
  719. /*
  720. * Well, that worked.
  721. */
  722. /*
  723. * Try next offsets, in case this match is
  724. * in the middle of a string.
  725. */
  726. for (k = i + 1 ; k < NOFFSETS; k++) {
  727. size_t no;
  728. int adjust = 1;
  729. if (prpsoffsets(k) >= prpsoffsets(i))
  730. continue;
  731. for (no = doff + prpsoffsets(k);
  732. no < doff + prpsoffsets(i); no++)
  733. adjust = adjust
  734. && isprint(nbuf[no]);
  735. if (adjust)
  736. i = k;
  737. }
  738. cname = (unsigned char *)
  739. &nbuf[doff + prpsoffsets(i)];
  740. for (cp = cname; *cp && isprint(*cp); cp++)
  741. continue;
  742. /*
  743. * Linux apparently appends a space at the end
  744. * of the command line: remove it.
  745. */
  746. while (cp > cname && isspace(cp[-1]))
  747. cp--;
  748. if (file_printf(ms, ", from '%.*s'",
  749. (int)(cp - cname), cname) == -1)
  750. return 1;
  751. *flags |= FLAGS_DID_CORE;
  752. return 1;
  753. tryanother:
  754. ;
  755. }
  756. }
  757. break;
  758. }
  759. #endif
  760. return 0;
  761. }
  762. private off_t
  763. get_offset_from_virtaddr(struct magic_set *ms, int swap, int clazz, int fd,
  764. off_t off, int num, off_t fsize, uint64_t virtaddr)
  765. {
  766. Elf32_Phdr ph32;
  767. Elf64_Phdr ph64;
  768. /*
  769. * Loop through all the program headers and find the header with
  770. * virtual address in which the "virtaddr" belongs to.
  771. */
  772. for ( ; num; num--) {
  773. if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) {
  774. file_badread(ms);
  775. return -1;
  776. }
  777. off += xph_sizeof;
  778. if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
  779. /* Perhaps warn here */
  780. continue;
  781. }
  782. if (virtaddr >= xph_vaddr && virtaddr < xph_vaddr + xph_filesz)
  783. return xph_offset + (virtaddr - xph_vaddr);
  784. }
  785. return 0;
  786. }
  787. private size_t
  788. get_string_on_virtaddr(struct magic_set *ms,
  789. int swap, int clazz, int fd, off_t ph_off, int ph_num,
  790. off_t fsize, uint64_t virtaddr, char *buf, ssize_t buflen)
  791. {
  792. char *bptr;
  793. off_t offset;
  794. if (buflen == 0)
  795. return 0;
  796. offset = get_offset_from_virtaddr(ms, swap, clazz, fd, ph_off, ph_num,
  797. fsize, virtaddr);
  798. if ((buflen = pread(fd, buf, buflen, offset)) <= 0) {
  799. file_badread(ms);
  800. return 0;
  801. }
  802. buf[buflen - 1] = '\0';
  803. /* We expect only printable characters, so return if buffer contains
  804. * non-printable character before the '\0' or just '\0'. */
  805. for (bptr = buf; *bptr && isprint((unsigned char)*bptr); bptr++)
  806. continue;
  807. if (*bptr != '\0')
  808. return 0;
  809. return bptr - buf;
  810. }
  811. private int
  812. do_auxv_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  813. int swap, uint32_t namesz __attribute__((__unused__)),
  814. uint32_t descsz __attribute__((__unused__)),
  815. size_t noff __attribute__((__unused__)), size_t doff,
  816. int *flags, size_t size __attribute__((__unused__)), int clazz,
  817. int fd, off_t ph_off, int ph_num, off_t fsize)
  818. {
  819. #ifdef ELFCORE
  820. Aux32Info auxv32;
  821. Aux64Info auxv64;
  822. size_t elsize = xauxv_sizeof;
  823. const char *tag;
  824. int is_string;
  825. size_t nval;
  826. if (type != NT_AUXV || (*flags & FLAGS_IS_CORE) == 0)
  827. return 0;
  828. *flags |= FLAGS_DID_AUXV;
  829. nval = 0;
  830. for (size_t off = 0; off + elsize <= descsz; off += elsize) {
  831. (void)memcpy(xauxv_addr, &nbuf[doff + off], xauxv_sizeof);
  832. /* Limit processing to 50 vector entries to prevent DoS */
  833. if (nval++ >= 50) {
  834. file_error(ms, 0, "Too many ELF Auxv elements");
  835. return 1;
  836. }
  837. switch(xauxv_type) {
  838. case AT_LINUX_EXECFN:
  839. is_string = 1;
  840. tag = "execfn";
  841. break;
  842. case AT_LINUX_PLATFORM:
  843. is_string = 1;
  844. tag = "platform";
  845. break;
  846. case AT_LINUX_UID:
  847. is_string = 0;
  848. tag = "real uid";
  849. break;
  850. case AT_LINUX_GID:
  851. is_string = 0;
  852. tag = "real gid";
  853. break;
  854. case AT_LINUX_EUID:
  855. is_string = 0;
  856. tag = "effective uid";
  857. break;
  858. case AT_LINUX_EGID:
  859. is_string = 0;
  860. tag = "effective gid";
  861. break;
  862. default:
  863. is_string = 0;
  864. tag = NULL;
  865. break;
  866. }
  867. if (tag == NULL)
  868. continue;
  869. if (is_string) {
  870. char buf[256];
  871. ssize_t buflen;
  872. buflen = get_string_on_virtaddr(ms, swap, clazz, fd,
  873. ph_off, ph_num, fsize, xauxv_val, buf, sizeof(buf));
  874. if (buflen == 0)
  875. continue;
  876. if (file_printf(ms, ", %s: '%s'", tag, buf) == -1)
  877. return 0;
  878. } else {
  879. if (file_printf(ms, ", %s: %d", tag, (int) xauxv_val)
  880. == -1)
  881. return 0;
  882. }
  883. }
  884. return 1;
  885. #else
  886. return 0;
  887. #endif
  888. }
  889. private size_t
  890. donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
  891. int clazz, int swap, size_t align, int *flags, uint16_t *notecount,
  892. int fd, off_t ph_off, int ph_num, off_t fsize)
  893. {
  894. Elf32_Nhdr nh32;
  895. Elf64_Nhdr nh64;
  896. size_t noff, doff;
  897. uint32_t namesz, descsz;
  898. unsigned char *nbuf = CAST(unsigned char *, vbuf);
  899. if (*notecount == 0)
  900. return 0;
  901. --*notecount;
  902. if (xnh_sizeof + offset > size) {
  903. /*
  904. * We're out of note headers.
  905. */
  906. return xnh_sizeof + offset;
  907. }
  908. (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
  909. offset += xnh_sizeof;
  910. namesz = xnh_namesz;
  911. descsz = xnh_descsz;
  912. if ((namesz == 0) && (descsz == 0)) {
  913. /*
  914. * We're out of note headers.
  915. */
  916. return (offset >= size) ? offset : size;
  917. }
  918. if (namesz & 0x80000000) {
  919. (void)file_printf(ms, ", bad note name size 0x%lx",
  920. (unsigned long)namesz);
  921. return 0;
  922. }
  923. if (descsz & 0x80000000) {
  924. (void)file_printf(ms, ", bad note description size 0x%lx",
  925. (unsigned long)descsz);
  926. return 0;
  927. }
  928. noff = offset;
  929. doff = ELF_ALIGN(offset + namesz);
  930. if (offset + namesz > size) {
  931. /*
  932. * We're past the end of the buffer.
  933. */
  934. return doff;
  935. }
  936. offset = ELF_ALIGN(doff + descsz);
  937. if (doff + descsz > size) {
  938. /*
  939. * We're past the end of the buffer.
  940. */
  941. return (offset >= size) ? offset : size;
  942. }
  943. if ((*flags & FLAGS_DID_OS_NOTE) == 0) {
  944. if (do_os_note(ms, nbuf, xnh_type, swap,
  945. namesz, descsz, noff, doff, flags))
  946. return offset;
  947. }
  948. if ((*flags & FLAGS_DID_BUILD_ID) == 0) {
  949. if (do_bid_note(ms, nbuf, xnh_type, swap,
  950. namesz, descsz, noff, doff, flags))
  951. return offset;
  952. }
  953. if ((*flags & FLAGS_DID_NETBSD_PAX) == 0) {
  954. if (do_pax_note(ms, nbuf, xnh_type, swap,
  955. namesz, descsz, noff, doff, flags))
  956. return offset;
  957. }
  958. if ((*flags & FLAGS_DID_CORE) == 0) {
  959. if (do_core_note(ms, nbuf, xnh_type, swap,
  960. namesz, descsz, noff, doff, flags, size, clazz))
  961. return offset;
  962. }
  963. if ((*flags & FLAGS_DID_AUXV) == 0) {
  964. if (do_auxv_note(ms, nbuf, xnh_type, swap,
  965. namesz, descsz, noff, doff, flags, size, clazz,
  966. fd, ph_off, ph_num, fsize))
  967. return offset;
  968. }
  969. if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
  970. if (descsz > 100)
  971. descsz = 100;
  972. switch (xnh_type) {
  973. case NT_NETBSD_VERSION:
  974. return offset;
  975. case NT_NETBSD_MARCH:
  976. if (*flags & FLAGS_DID_NETBSD_MARCH)
  977. return offset;
  978. *flags |= FLAGS_DID_NETBSD_MARCH;
  979. if (file_printf(ms, ", compiled for: %.*s",
  980. (int)descsz, (const char *)&nbuf[doff]) == -1)
  981. return offset;
  982. break;
  983. case NT_NETBSD_CMODEL:
  984. if (*flags & FLAGS_DID_NETBSD_CMODEL)
  985. return offset;
  986. *flags |= FLAGS_DID_NETBSD_CMODEL;
  987. if (file_printf(ms, ", compiler model: %.*s",
  988. (int)descsz, (const char *)&nbuf[doff]) == -1)
  989. return offset;
  990. break;
  991. default:
  992. if (*flags & FLAGS_DID_NETBSD_UNKNOWN)
  993. return offset;
  994. *flags |= FLAGS_DID_NETBSD_UNKNOWN;
  995. if (file_printf(ms, ", note=%u", xnh_type) == -1)
  996. return offset;
  997. break;
  998. }
  999. return offset;
  1000. }
  1001. return offset;
  1002. }
  1003. /* SunOS 5.x hardware capability descriptions */
  1004. typedef struct cap_desc {
  1005. uint64_t cd_mask;
  1006. const char *cd_name;
  1007. } cap_desc_t;
  1008. static const cap_desc_t cap_desc_sparc[] = {
  1009. { AV_SPARC_MUL32, "MUL32" },
  1010. { AV_SPARC_DIV32, "DIV32" },
  1011. { AV_SPARC_FSMULD, "FSMULD" },
  1012. { AV_SPARC_V8PLUS, "V8PLUS" },
  1013. { AV_SPARC_POPC, "POPC" },
  1014. { AV_SPARC_VIS, "VIS" },
  1015. { AV_SPARC_VIS2, "VIS2" },
  1016. { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
  1017. { AV_SPARC_FMAF, "FMAF" },
  1018. { AV_SPARC_FJFMAU, "FJFMAU" },
  1019. { AV_SPARC_IMA, "IMA" },
  1020. { 0, NULL }
  1021. };
  1022. static const cap_desc_t cap_desc_386[] = {
  1023. { AV_386_FPU, "FPU" },
  1024. { AV_386_TSC, "TSC" },
  1025. { AV_386_CX8, "CX8" },
  1026. { AV_386_SEP, "SEP" },
  1027. { AV_386_AMD_SYSC, "AMD_SYSC" },
  1028. { AV_386_CMOV, "CMOV" },
  1029. { AV_386_MMX, "MMX" },
  1030. { AV_386_AMD_MMX, "AMD_MMX" },
  1031. { AV_386_AMD_3DNow, "AMD_3DNow" },
  1032. { AV_386_AMD_3DNowx, "AMD_3DNowx" },
  1033. { AV_386_FXSR, "FXSR" },
  1034. { AV_386_SSE, "SSE" },
  1035. { AV_386_SSE2, "SSE2" },
  1036. { AV_386_PAUSE, "PAUSE" },
  1037. { AV_386_SSE3, "SSE3" },
  1038. { AV_386_MON, "MON" },
  1039. { AV_386_CX16, "CX16" },
  1040. { AV_386_AHF, "AHF" },
  1041. { AV_386_TSCP, "TSCP" },
  1042. { AV_386_AMD_SSE4A, "AMD_SSE4A" },
  1043. { AV_386_POPCNT, "POPCNT" },
  1044. { AV_386_AMD_LZCNT, "AMD_LZCNT" },
  1045. { AV_386_SSSE3, "SSSE3" },
  1046. { AV_386_SSE4_1, "SSE4.1" },
  1047. { AV_386_SSE4_2, "SSE4.2" },
  1048. { 0, NULL }
  1049. };
  1050. private int
  1051. doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
  1052. size_t size, off_t fsize, int mach, int strtab, int *flags,
  1053. uint16_t *notecount)
  1054. {
  1055. Elf32_Shdr sh32;
  1056. Elf64_Shdr sh64;
  1057. int stripped = 1;
  1058. size_t nbadcap = 0;
  1059. void *nbuf;
  1060. off_t noff, coff, name_off;
  1061. uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */
  1062. uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */
  1063. char name[50];
  1064. ssize_t namesize;
  1065. if (size != xsh_sizeof) {
  1066. if (file_printf(ms, ", corrupted section header size") == -1)
  1067. return -1;
  1068. return 0;
  1069. }
  1070. /* Read offset of name section to be able to read section names later */
  1071. if (pread(fd, xsh_addr, xsh_sizeof, CAST(off_t, (off + size * strtab)))
  1072. < (ssize_t)xsh_sizeof) {
  1073. file_badread(ms);
  1074. return -1;
  1075. }
  1076. name_off = xsh_offset;
  1077. for ( ; num; num--) {
  1078. /* Read the name of this section. */
  1079. if ((namesize = pread(fd, name, sizeof(name) - 1, name_off + xsh_name)) == -1) {
  1080. file_badread(ms);
  1081. return -1;
  1082. }
  1083. name[namesize] = '\0';
  1084. if (strcmp(name, ".debug_info") == 0)
  1085. stripped = 0;
  1086. if (pread(fd, xsh_addr, xsh_sizeof, off) < (ssize_t)xsh_sizeof) {
  1087. file_badread(ms);
  1088. return -1;
  1089. }
  1090. off += size;
  1091. /* Things we can determine before we seek */
  1092. switch (xsh_type) {
  1093. case SHT_SYMTAB:
  1094. #if 0
  1095. case SHT_DYNSYM:
  1096. #endif
  1097. stripped = 0;
  1098. break;
  1099. default:
  1100. if (fsize != SIZE_UNKNOWN && xsh_offset > fsize) {
  1101. /* Perhaps warn here */
  1102. continue;
  1103. }
  1104. break;
  1105. }
  1106. /* Things we can determine when we seek */
  1107. switch (xsh_type) {
  1108. case SHT_NOTE:
  1109. if ((uintmax_t)(xsh_size + xsh_offset) >
  1110. (uintmax_t)fsize) {
  1111. if (file_printf(ms,
  1112. ", note offset/size 0x%" INTMAX_T_FORMAT
  1113. "x+0x%" INTMAX_T_FORMAT "x exceeds"
  1114. " file size 0x%" INTMAX_T_FORMAT "x",
  1115. (uintmax_t)xsh_offset, (uintmax_t)xsh_size,
  1116. (uintmax_t)fsize) == -1)
  1117. return -1;
  1118. return 0;
  1119. }
  1120. if ((nbuf = malloc(xsh_size)) == NULL) {
  1121. file_error(ms, errno, "Cannot allocate memory"
  1122. " for note");
  1123. return -1;
  1124. }
  1125. if (pread(fd, nbuf, xsh_size, xsh_offset) <
  1126. (ssize_t)xsh_size) {
  1127. file_badread(ms);
  1128. free(nbuf);
  1129. return -1;
  1130. }
  1131. noff = 0;
  1132. for (;;) {
  1133. if (noff >= (off_t)xsh_size)
  1134. break;
  1135. noff = donote(ms, nbuf, (size_t)noff,
  1136. xsh_size, clazz, swap, 4, flags, notecount,
  1137. fd, 0, 0, 0);
  1138. if (noff == 0)
  1139. break;
  1140. }
  1141. free(nbuf);
  1142. break;
  1143. case SHT_SUNW_cap:
  1144. switch (mach) {
  1145. case EM_SPARC:
  1146. case EM_SPARCV9:
  1147. case EM_IA_64:
  1148. case EM_386:
  1149. case EM_AMD64:
  1150. break;
  1151. default:
  1152. goto skip;
  1153. }
  1154. if (nbadcap > 5)
  1155. break;
  1156. if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) {
  1157. file_badseek(ms);
  1158. return -1;
  1159. }
  1160. coff = 0;
  1161. for (;;) {
  1162. Elf32_Cap cap32;
  1163. Elf64_Cap cap64;
  1164. char cbuf[/*CONSTCOND*/
  1165. MAX(sizeof cap32, sizeof cap64)];
  1166. if ((coff += xcap_sizeof) > (off_t)xsh_size)
  1167. break;
  1168. if (read(fd, cbuf, (size_t)xcap_sizeof) !=
  1169. (ssize_t)xcap_sizeof) {
  1170. file_badread(ms);
  1171. return -1;
  1172. }
  1173. if (cbuf[0] == 'A') {
  1174. #ifdef notyet
  1175. char *p = cbuf + 1;
  1176. uint32_t len, tag;
  1177. memcpy(&len, p, sizeof(len));
  1178. p += 4;
  1179. len = getu32(swap, len);
  1180. if (memcmp("gnu", p, 3) != 0) {
  1181. if (file_printf(ms,
  1182. ", unknown capability %.3s", p)
  1183. == -1)
  1184. return -1;
  1185. break;
  1186. }
  1187. p += strlen(p) + 1;
  1188. tag = *p++;
  1189. memcpy(&len, p, sizeof(len));
  1190. p += 4;
  1191. len = getu32(swap, len);
  1192. if (tag != 1) {
  1193. if (file_printf(ms, ", unknown gnu"
  1194. " capability tag %d", tag)
  1195. == -1)
  1196. return -1;
  1197. break;
  1198. }
  1199. // gnu attributes
  1200. #endif
  1201. break;
  1202. }
  1203. (void)memcpy(xcap_addr, cbuf, xcap_sizeof);
  1204. switch (xcap_tag) {
  1205. case CA_SUNW_NULL:
  1206. break;
  1207. case CA_SUNW_HW_1:
  1208. cap_hw1 |= xcap_val;
  1209. break;
  1210. case CA_SUNW_SF_1:
  1211. cap_sf1 |= xcap_val;
  1212. break;
  1213. default:
  1214. if (file_printf(ms,
  1215. ", with unknown capability "
  1216. "0x%" INT64_T_FORMAT "x = 0x%"
  1217. INT64_T_FORMAT "x",
  1218. (unsigned long long)xcap_tag,
  1219. (unsigned long long)xcap_val) == -1)
  1220. return -1;
  1221. if (nbadcap++ > 2)
  1222. coff = xsh_size;
  1223. break;
  1224. }
  1225. }
  1226. /*FALLTHROUGH*/
  1227. skip:
  1228. default:
  1229. break;
  1230. }
  1231. }
  1232. if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
  1233. return -1;
  1234. if (cap_hw1) {
  1235. const cap_desc_t *cdp;
  1236. switch (mach) {
  1237. case EM_SPARC:
  1238. case EM_SPARC32PLUS:
  1239. case EM_SPARCV9:
  1240. cdp = cap_desc_sparc;
  1241. break;
  1242. case EM_386:
  1243. case EM_IA_64:
  1244. case EM_AMD64:
  1245. cdp = cap_desc_386;
  1246. break;
  1247. default:
  1248. cdp = NULL;
  1249. break;
  1250. }
  1251. if (file_printf(ms, ", uses") == -1)
  1252. return -1;
  1253. if (cdp) {
  1254. while (cdp->cd_name) {
  1255. if (cap_hw1 & cdp->cd_mask) {
  1256. if (file_printf(ms,
  1257. " %s", cdp->cd_name) == -1)
  1258. return -1;
  1259. cap_hw1 &= ~cdp->cd_mask;
  1260. }
  1261. ++cdp;
  1262. }
  1263. if (cap_hw1)
  1264. if (file_printf(ms,
  1265. " unknown hardware capability 0x%"
  1266. INT64_T_FORMAT "x",
  1267. (unsigned long long)cap_hw1) == -1)
  1268. return -1;
  1269. } else {
  1270. if (file_printf(ms,
  1271. " hardware capability 0x%" INT64_T_FORMAT "x",
  1272. (unsigned long long)cap_hw1) == -1)
  1273. return -1;
  1274. }
  1275. }
  1276. if (cap_sf1) {
  1277. if (cap_sf1 & SF1_SUNW_FPUSED) {
  1278. if (file_printf(ms,
  1279. (cap_sf1 & SF1_SUNW_FPKNWN)
  1280. ? ", uses frame pointer"
  1281. : ", not known to use frame pointer") == -1)
  1282. return -1;
  1283. }
  1284. cap_sf1 &= ~SF1_SUNW_MASK;
  1285. if (cap_sf1)
  1286. if (file_printf(ms,
  1287. ", with unknown software capability 0x%"
  1288. INT64_T_FORMAT "x",
  1289. (unsigned long long)cap_sf1) == -1)
  1290. return -1;
  1291. }
  1292. return 0;
  1293. }
  1294. /*
  1295. * Look through the program headers of an executable image, searching
  1296. * for a PT_INTERP section; if one is found, it's dynamically linked,
  1297. * otherwise it's statically linked.
  1298. */
  1299. private int
  1300. dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
  1301. int num, size_t size, off_t fsize, int sh_num, int *flags,
  1302. uint16_t *notecount)
  1303. {
  1304. Elf32_Phdr ph32;
  1305. Elf64_Phdr ph64;
  1306. const char *linking_style = "statically";
  1307. const char *interp = "";
  1308. unsigned char nbuf[BUFSIZ];
  1309. char ibuf[BUFSIZ];
  1310. ssize_t bufsize;
  1311. size_t offset, align, len;
  1312. if (size != xph_sizeof) {
  1313. if (file_printf(ms, ", corrupted program header size") == -1)
  1314. return -1;
  1315. return 0;
  1316. }
  1317. for ( ; num; num--) {
  1318. if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) {
  1319. file_badread(ms);
  1320. return -1;
  1321. }
  1322. off += size;
  1323. bufsize = 0;
  1324. align = 4;
  1325. /* Things we can determine before we seek */
  1326. switch (xph_type) {
  1327. case PT_DYNAMIC:
  1328. linking_style = "dynamically";
  1329. break;
  1330. case PT_NOTE:
  1331. if (sh_num) /* Did this through section headers */
  1332. continue;
  1333. if (((align = xph_align) & 0x80000000UL) != 0 ||
  1334. align < 4) {
  1335. if (file_printf(ms,
  1336. ", invalid note alignment 0x%lx",
  1337. (unsigned long)align) == -1)
  1338. return -1;
  1339. align = 4;
  1340. }
  1341. /*FALLTHROUGH*/
  1342. case PT_INTERP:
  1343. len = xph_filesz < sizeof(nbuf) ? xph_filesz
  1344. : sizeof(nbuf);
  1345. bufsize = pread(fd, nbuf, len, xph_offset);
  1346. if (bufsize == -1) {
  1347. file_badread(ms);
  1348. return -1;
  1349. }
  1350. break;
  1351. default:
  1352. if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
  1353. /* Maybe warn here? */
  1354. continue;
  1355. }
  1356. break;
  1357. }
  1358. /* Things we can determine when we seek */
  1359. switch (xph_type) {
  1360. case PT_INTERP:
  1361. if (bufsize && nbuf[0]) {
  1362. nbuf[bufsize - 1] = '\0';
  1363. interp = (const char *)nbuf;
  1364. } else
  1365. interp = "*empty*";
  1366. break;
  1367. case PT_NOTE:
  1368. /*
  1369. * This is a PT_NOTE section; loop through all the notes
  1370. * in the section.
  1371. */
  1372. offset = 0;
  1373. for (;;) {
  1374. if (offset >= (size_t)bufsize)
  1375. break;
  1376. offset = donote(ms, nbuf, offset,
  1377. (size_t)bufsize, clazz, swap, align,
  1378. flags, notecount, fd, 0, 0, 0);
  1379. if (offset == 0)
  1380. break;
  1381. }
  1382. break;
  1383. default:
  1384. break;
  1385. }
  1386. }
  1387. if (file_printf(ms, ", %s linked", linking_style)
  1388. == -1)
  1389. return -1;
  1390. if (interp[0])
  1391. if (file_printf(ms, ", interpreter %s",
  1392. file_printable(ibuf, sizeof(ibuf), interp)) == -1)
  1393. return -1;
  1394. return 0;
  1395. }
  1396. protected int
  1397. file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
  1398. size_t nbytes)
  1399. {
  1400. union {
  1401. int32_t l;
  1402. char c[sizeof (int32_t)];
  1403. } u;
  1404. int clazz;
  1405. int swap;
  1406. struct stat st;
  1407. off_t fsize;
  1408. int flags = 0;
  1409. Elf32_Ehdr elf32hdr;
  1410. Elf64_Ehdr elf64hdr;
  1411. uint16_t type, phnum, shnum, notecount;
  1412. if (ms->flags & (MAGIC_MIME|MAGIC_APPLE|MAGIC_EXTENSION))
  1413. return 0;
  1414. /*
  1415. * ELF executables have multiple section headers in arbitrary
  1416. * file locations and thus file(1) cannot determine it from easily.
  1417. * Instead we traverse thru all section headers until a symbol table
  1418. * one is found or else the binary is stripped.
  1419. * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
  1420. */
  1421. if (buf[EI_MAG0] != ELFMAG0
  1422. || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
  1423. || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
  1424. return 0;
  1425. /*
  1426. * If we cannot seek, it must be a pipe, socket or fifo.
  1427. */
  1428. if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
  1429. fd = file_pipe2file(ms, fd, buf, nbytes);
  1430. if (fstat(fd, &st) == -1) {
  1431. file_badread(ms);
  1432. return -1;
  1433. }
  1434. if (S_ISREG(st.st_mode) || st.st_size != 0)
  1435. fsize = st.st_size;
  1436. else
  1437. fsize = SIZE_UNKNOWN;
  1438. clazz = buf[EI_CLASS];
  1439. switch (clazz) {
  1440. case ELFCLASS32:
  1441. #undef elf_getu
  1442. #define elf_getu(a, b) elf_getu32(a, b)
  1443. #undef elfhdr
  1444. #define elfhdr elf32hdr
  1445. #include "elfclass.h"
  1446. case ELFCLASS64:
  1447. #undef elf_getu
  1448. #define elf_getu(a, b) elf_getu64(a, b)
  1449. #undef elfhdr
  1450. #define elfhdr elf64hdr
  1451. #include "elfclass.h"
  1452. default:
  1453. if (file_printf(ms, ", unknown class %d", clazz) == -1)
  1454. return -1;
  1455. break;
  1456. }
  1457. return 0;
  1458. }
  1459. #endif