readelf.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  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.130 2017/01/29 19:34:24 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 >= 4 || descsz <= 20)) {
  462. uint8_t desc[20];
  463. const char *btype;
  464. uint32_t i;
  465. *flags |= FLAGS_DID_BUILD_ID;
  466. switch (descsz) {
  467. case 8:
  468. btype = "xxHash";
  469. break;
  470. case 16:
  471. btype = "md5/uuid";
  472. break;
  473. case 20:
  474. btype = "sha1";
  475. break;
  476. default:
  477. btype = "unknown";
  478. break;
  479. }
  480. if (file_printf(ms, ", BuildID[%s]=", btype) == -1)
  481. return 1;
  482. (void)memcpy(desc, &nbuf[doff], descsz);
  483. for (i = 0; i < descsz; i++)
  484. if (file_printf(ms, "%02x", desc[i]) == -1)
  485. return 1;
  486. return 1;
  487. }
  488. return 0;
  489. }
  490. private int
  491. do_os_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  492. int swap, uint32_t namesz, uint32_t descsz,
  493. size_t noff, size_t doff, int *flags)
  494. {
  495. if (namesz == 5 && strcmp((char *)&nbuf[noff], "SuSE") == 0 &&
  496. type == NT_GNU_VERSION && descsz == 2) {
  497. *flags |= FLAGS_DID_OS_NOTE;
  498. file_printf(ms, ", for SuSE %d.%d", nbuf[doff], nbuf[doff + 1]);
  499. return 1;
  500. }
  501. if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  502. type == NT_GNU_VERSION && descsz == 16) {
  503. uint32_t desc[4];
  504. (void)memcpy(desc, &nbuf[doff], sizeof(desc));
  505. *flags |= FLAGS_DID_OS_NOTE;
  506. if (file_printf(ms, ", for GNU/") == -1)
  507. return 1;
  508. switch (elf_getu32(swap, desc[0])) {
  509. case GNU_OS_LINUX:
  510. if (file_printf(ms, "Linux") == -1)
  511. return 1;
  512. break;
  513. case GNU_OS_HURD:
  514. if (file_printf(ms, "Hurd") == -1)
  515. return 1;
  516. break;
  517. case GNU_OS_SOLARIS:
  518. if (file_printf(ms, "Solaris") == -1)
  519. return 1;
  520. break;
  521. case GNU_OS_KFREEBSD:
  522. if (file_printf(ms, "kFreeBSD") == -1)
  523. return 1;
  524. break;
  525. case GNU_OS_KNETBSD:
  526. if (file_printf(ms, "kNetBSD") == -1)
  527. return 1;
  528. break;
  529. default:
  530. if (file_printf(ms, "<unknown>") == -1)
  531. return 1;
  532. }
  533. if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
  534. elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
  535. return 1;
  536. return 1;
  537. }
  538. if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
  539. if (type == NT_NETBSD_VERSION && descsz == 4) {
  540. *flags |= FLAGS_DID_OS_NOTE;
  541. do_note_netbsd_version(ms, swap, &nbuf[doff]);
  542. return 1;
  543. }
  544. }
  545. if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0) {
  546. if (type == NT_FREEBSD_VERSION && descsz == 4) {
  547. *flags |= FLAGS_DID_OS_NOTE;
  548. do_note_freebsd_version(ms, swap, &nbuf[doff]);
  549. return 1;
  550. }
  551. }
  552. if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
  553. type == NT_OPENBSD_VERSION && descsz == 4) {
  554. *flags |= FLAGS_DID_OS_NOTE;
  555. if (file_printf(ms, ", for OpenBSD") == -1)
  556. return 1;
  557. /* Content of note is always 0 */
  558. return 1;
  559. }
  560. if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
  561. type == NT_DRAGONFLY_VERSION && descsz == 4) {
  562. uint32_t desc;
  563. *flags |= FLAGS_DID_OS_NOTE;
  564. if (file_printf(ms, ", for DragonFly") == -1)
  565. return 1;
  566. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  567. desc = elf_getu32(swap, desc);
  568. if (file_printf(ms, " %d.%d.%d", desc / 100000,
  569. desc / 10000 % 10, desc % 10000) == -1)
  570. return 1;
  571. return 1;
  572. }
  573. return 0;
  574. }
  575. private int
  576. do_pax_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  577. int swap, uint32_t namesz, uint32_t descsz,
  578. size_t noff, size_t doff, int *flags)
  579. {
  580. if (namesz == 4 && strcmp((char *)&nbuf[noff], "PaX") == 0 &&
  581. type == NT_NETBSD_PAX && descsz == 4) {
  582. static const char *pax[] = {
  583. "+mprotect",
  584. "-mprotect",
  585. "+segvguard",
  586. "-segvguard",
  587. "+ASLR",
  588. "-ASLR",
  589. };
  590. uint32_t desc;
  591. size_t i;
  592. int did = 0;
  593. *flags |= FLAGS_DID_NETBSD_PAX;
  594. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  595. desc = elf_getu32(swap, desc);
  596. if (desc && file_printf(ms, ", PaX: ") == -1)
  597. return 1;
  598. for (i = 0; i < __arraycount(pax); i++) {
  599. if (((1 << (int)i) & desc) == 0)
  600. continue;
  601. if (file_printf(ms, "%s%s", did++ ? "," : "",
  602. pax[i]) == -1)
  603. return 1;
  604. }
  605. return 1;
  606. }
  607. return 0;
  608. }
  609. private int
  610. do_core_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  611. int swap, uint32_t namesz, uint32_t descsz,
  612. size_t noff, size_t doff, int *flags, size_t size, int clazz)
  613. {
  614. #ifdef ELFCORE
  615. int os_style = -1;
  616. /*
  617. * Sigh. The 2.0.36 kernel in Debian 2.1, at
  618. * least, doesn't correctly implement name
  619. * sections, in core dumps, as specified by
  620. * the "Program Linking" section of "UNIX(R) System
  621. * V Release 4 Programmer's Guide: ANSI C and
  622. * Programming Support Tools", because my copy
  623. * clearly says "The first 'namesz' bytes in 'name'
  624. * contain a *null-terminated* [emphasis mine]
  625. * character representation of the entry's owner
  626. * or originator", but the 2.0.36 kernel code
  627. * doesn't include the terminating null in the
  628. * name....
  629. */
  630. if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
  631. (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
  632. os_style = OS_STYLE_SVR4;
  633. }
  634. if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
  635. os_style = OS_STYLE_FREEBSD;
  636. }
  637. if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
  638. == 0)) {
  639. os_style = OS_STYLE_NETBSD;
  640. }
  641. if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
  642. if (file_printf(ms, ", %s-style", os_style_names[os_style])
  643. == -1)
  644. return 1;
  645. *flags |= FLAGS_DID_CORE_STYLE;
  646. }
  647. switch (os_style) {
  648. case OS_STYLE_NETBSD:
  649. if (type == NT_NETBSD_CORE_PROCINFO) {
  650. char sbuf[512];
  651. uint32_t signo;
  652. /*
  653. * Extract the program name. It is at
  654. * offset 0x7c, and is up to 32-bytes,
  655. * including the terminating NUL.
  656. */
  657. if (file_printf(ms, ", from '%.31s'",
  658. file_printable(sbuf, sizeof(sbuf),
  659. (const char *)&nbuf[doff + 0x7c])) == -1)
  660. return 1;
  661. /*
  662. * Extract the signal number. It is at
  663. * offset 0x08.
  664. */
  665. (void)memcpy(&signo, &nbuf[doff + 0x08],
  666. sizeof(signo));
  667. if (file_printf(ms, " (signal %u)",
  668. elf_getu32(swap, signo)) == -1)
  669. return 1;
  670. *flags |= FLAGS_DID_CORE;
  671. return 1;
  672. }
  673. break;
  674. default:
  675. if (type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
  676. size_t i, j;
  677. unsigned char c;
  678. /*
  679. * Extract the program name. We assume
  680. * it to be 16 characters (that's what it
  681. * is in SunOS 5.x and Linux).
  682. *
  683. * Unfortunately, it's at a different offset
  684. * in various OSes, so try multiple offsets.
  685. * If the characters aren't all printable,
  686. * reject it.
  687. */
  688. for (i = 0; i < NOFFSETS; i++) {
  689. unsigned char *cname, *cp;
  690. size_t reloffset = prpsoffsets(i);
  691. size_t noffset = doff + reloffset;
  692. size_t k;
  693. for (j = 0; j < 16; j++, noffset++,
  694. reloffset++) {
  695. /*
  696. * Make sure we're not past
  697. * the end of the buffer; if
  698. * we are, just give up.
  699. */
  700. if (noffset >= size)
  701. goto tryanother;
  702. /*
  703. * Make sure we're not past
  704. * the end of the contents;
  705. * if we are, this obviously
  706. * isn't the right offset.
  707. */
  708. if (reloffset >= descsz)
  709. goto tryanother;
  710. c = nbuf[noffset];
  711. if (c == '\0') {
  712. /*
  713. * A '\0' at the
  714. * beginning is
  715. * obviously wrong.
  716. * Any other '\0'
  717. * means we're done.
  718. */
  719. if (j == 0)
  720. goto tryanother;
  721. else
  722. break;
  723. } else {
  724. /*
  725. * A nonprintable
  726. * character is also
  727. * wrong.
  728. */
  729. if (!isprint(c) || isquote(c))
  730. goto tryanother;
  731. }
  732. }
  733. /*
  734. * Well, that worked.
  735. */
  736. /*
  737. * Try next offsets, in case this match is
  738. * in the middle of a string.
  739. */
  740. for (k = i + 1 ; k < NOFFSETS; k++) {
  741. size_t no;
  742. int adjust = 1;
  743. if (prpsoffsets(k) >= prpsoffsets(i))
  744. continue;
  745. for (no = doff + prpsoffsets(k);
  746. no < doff + prpsoffsets(i); no++)
  747. adjust = adjust
  748. && isprint(nbuf[no]);
  749. if (adjust)
  750. i = k;
  751. }
  752. cname = (unsigned char *)
  753. &nbuf[doff + prpsoffsets(i)];
  754. for (cp = cname; *cp && isprint(*cp); cp++)
  755. continue;
  756. /*
  757. * Linux apparently appends a space at the end
  758. * of the command line: remove it.
  759. */
  760. while (cp > cname && isspace(cp[-1]))
  761. cp--;
  762. if (file_printf(ms, ", from '%.*s'",
  763. (int)(cp - cname), cname) == -1)
  764. return 1;
  765. *flags |= FLAGS_DID_CORE;
  766. return 1;
  767. tryanother:
  768. ;
  769. }
  770. }
  771. break;
  772. }
  773. #endif
  774. return 0;
  775. }
  776. private off_t
  777. get_offset_from_virtaddr(struct magic_set *ms, int swap, int clazz, int fd,
  778. off_t off, int num, off_t fsize, uint64_t virtaddr)
  779. {
  780. Elf32_Phdr ph32;
  781. Elf64_Phdr ph64;
  782. /*
  783. * Loop through all the program headers and find the header with
  784. * virtual address in which the "virtaddr" belongs to.
  785. */
  786. for ( ; num; num--) {
  787. if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) {
  788. file_badread(ms);
  789. return -1;
  790. }
  791. off += xph_sizeof;
  792. if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
  793. /* Perhaps warn here */
  794. continue;
  795. }
  796. if (virtaddr >= xph_vaddr && virtaddr < xph_vaddr + xph_filesz)
  797. return xph_offset + (virtaddr - xph_vaddr);
  798. }
  799. return 0;
  800. }
  801. private size_t
  802. get_string_on_virtaddr(struct magic_set *ms,
  803. int swap, int clazz, int fd, off_t ph_off, int ph_num,
  804. off_t fsize, uint64_t virtaddr, char *buf, ssize_t buflen)
  805. {
  806. char *bptr;
  807. off_t offset;
  808. if (buflen == 0)
  809. return 0;
  810. offset = get_offset_from_virtaddr(ms, swap, clazz, fd, ph_off, ph_num,
  811. fsize, virtaddr);
  812. if ((buflen = pread(fd, buf, buflen, offset)) <= 0) {
  813. file_badread(ms);
  814. return 0;
  815. }
  816. buf[buflen - 1] = '\0';
  817. /* We expect only printable characters, so return if buffer contains
  818. * non-printable character before the '\0' or just '\0'. */
  819. for (bptr = buf; *bptr && isprint((unsigned char)*bptr); bptr++)
  820. continue;
  821. if (*bptr != '\0')
  822. return 0;
  823. return bptr - buf;
  824. }
  825. private int
  826. do_auxv_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  827. int swap, uint32_t namesz __attribute__((__unused__)),
  828. uint32_t descsz __attribute__((__unused__)),
  829. size_t noff __attribute__((__unused__)), size_t doff,
  830. int *flags, size_t size __attribute__((__unused__)), int clazz,
  831. int fd, off_t ph_off, int ph_num, off_t fsize)
  832. {
  833. #ifdef ELFCORE
  834. Aux32Info auxv32;
  835. Aux64Info auxv64;
  836. size_t elsize = xauxv_sizeof;
  837. const char *tag;
  838. int is_string;
  839. size_t nval;
  840. if (type != NT_AUXV || (*flags & FLAGS_IS_CORE) == 0)
  841. return 0;
  842. *flags |= FLAGS_DID_AUXV;
  843. nval = 0;
  844. for (size_t off = 0; off + elsize <= descsz; off += elsize) {
  845. (void)memcpy(xauxv_addr, &nbuf[doff + off], xauxv_sizeof);
  846. /* Limit processing to 50 vector entries to prevent DoS */
  847. if (nval++ >= 50) {
  848. file_error(ms, 0, "Too many ELF Auxv elements");
  849. return 1;
  850. }
  851. switch(xauxv_type) {
  852. case AT_LINUX_EXECFN:
  853. is_string = 1;
  854. tag = "execfn";
  855. break;
  856. case AT_LINUX_PLATFORM:
  857. is_string = 1;
  858. tag = "platform";
  859. break;
  860. case AT_LINUX_UID:
  861. is_string = 0;
  862. tag = "real uid";
  863. break;
  864. case AT_LINUX_GID:
  865. is_string = 0;
  866. tag = "real gid";
  867. break;
  868. case AT_LINUX_EUID:
  869. is_string = 0;
  870. tag = "effective uid";
  871. break;
  872. case AT_LINUX_EGID:
  873. is_string = 0;
  874. tag = "effective gid";
  875. break;
  876. default:
  877. is_string = 0;
  878. tag = NULL;
  879. break;
  880. }
  881. if (tag == NULL)
  882. continue;
  883. if (is_string) {
  884. char buf[256];
  885. ssize_t buflen;
  886. buflen = get_string_on_virtaddr(ms, swap, clazz, fd,
  887. ph_off, ph_num, fsize, xauxv_val, buf, sizeof(buf));
  888. if (buflen == 0)
  889. continue;
  890. if (file_printf(ms, ", %s: '%s'", tag, buf) == -1)
  891. return 0;
  892. } else {
  893. if (file_printf(ms, ", %s: %d", tag, (int) xauxv_val)
  894. == -1)
  895. return 0;
  896. }
  897. }
  898. return 1;
  899. #else
  900. return 0;
  901. #endif
  902. }
  903. private size_t
  904. donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
  905. int clazz, int swap, size_t align, int *flags, uint16_t *notecount,
  906. int fd, off_t ph_off, int ph_num, off_t fsize)
  907. {
  908. Elf32_Nhdr nh32;
  909. Elf64_Nhdr nh64;
  910. size_t noff, doff;
  911. uint32_t namesz, descsz;
  912. unsigned char *nbuf = CAST(unsigned char *, vbuf);
  913. if (*notecount == 0)
  914. return 0;
  915. --*notecount;
  916. if (xnh_sizeof + offset > size) {
  917. /*
  918. * We're out of note headers.
  919. */
  920. return xnh_sizeof + offset;
  921. }
  922. (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
  923. offset += xnh_sizeof;
  924. namesz = xnh_namesz;
  925. descsz = xnh_descsz;
  926. if ((namesz == 0) && (descsz == 0)) {
  927. /*
  928. * We're out of note headers.
  929. */
  930. return (offset >= size) ? offset : size;
  931. }
  932. if (namesz & 0x80000000) {
  933. (void)file_printf(ms, ", bad note name size 0x%lx",
  934. (unsigned long)namesz);
  935. return 0;
  936. }
  937. if (descsz & 0x80000000) {
  938. (void)file_printf(ms, ", bad note description size 0x%lx",
  939. (unsigned long)descsz);
  940. return 0;
  941. }
  942. noff = offset;
  943. doff = ELF_ALIGN(offset + namesz);
  944. if (offset + namesz > size) {
  945. /*
  946. * We're past the end of the buffer.
  947. */
  948. return doff;
  949. }
  950. offset = ELF_ALIGN(doff + descsz);
  951. if (doff + descsz > size) {
  952. /*
  953. * We're past the end of the buffer.
  954. */
  955. return (offset >= size) ? offset : size;
  956. }
  957. if ((*flags & FLAGS_DID_OS_NOTE) == 0) {
  958. if (do_os_note(ms, nbuf, xnh_type, swap,
  959. namesz, descsz, noff, doff, flags))
  960. return offset;
  961. }
  962. if ((*flags & FLAGS_DID_BUILD_ID) == 0) {
  963. if (do_bid_note(ms, nbuf, xnh_type, swap,
  964. namesz, descsz, noff, doff, flags))
  965. return offset;
  966. }
  967. if ((*flags & FLAGS_DID_NETBSD_PAX) == 0) {
  968. if (do_pax_note(ms, nbuf, xnh_type, swap,
  969. namesz, descsz, noff, doff, flags))
  970. return offset;
  971. }
  972. if ((*flags & FLAGS_DID_CORE) == 0) {
  973. if (do_core_note(ms, nbuf, xnh_type, swap,
  974. namesz, descsz, noff, doff, flags, size, clazz))
  975. return offset;
  976. }
  977. if ((*flags & FLAGS_DID_AUXV) == 0) {
  978. if (do_auxv_note(ms, nbuf, xnh_type, swap,
  979. namesz, descsz, noff, doff, flags, size, clazz,
  980. fd, ph_off, ph_num, fsize))
  981. return offset;
  982. }
  983. if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
  984. if (descsz > 100)
  985. descsz = 100;
  986. switch (xnh_type) {
  987. case NT_NETBSD_VERSION:
  988. return offset;
  989. case NT_NETBSD_MARCH:
  990. if (*flags & FLAGS_DID_NETBSD_MARCH)
  991. return offset;
  992. *flags |= FLAGS_DID_NETBSD_MARCH;
  993. if (file_printf(ms, ", compiled for: %.*s",
  994. (int)descsz, (const char *)&nbuf[doff]) == -1)
  995. return offset;
  996. break;
  997. case NT_NETBSD_CMODEL:
  998. if (*flags & FLAGS_DID_NETBSD_CMODEL)
  999. return offset;
  1000. *flags |= FLAGS_DID_NETBSD_CMODEL;
  1001. if (file_printf(ms, ", compiler model: %.*s",
  1002. (int)descsz, (const char *)&nbuf[doff]) == -1)
  1003. return offset;
  1004. break;
  1005. default:
  1006. if (*flags & FLAGS_DID_NETBSD_UNKNOWN)
  1007. return offset;
  1008. *flags |= FLAGS_DID_NETBSD_UNKNOWN;
  1009. if (file_printf(ms, ", note=%u", xnh_type) == -1)
  1010. return offset;
  1011. break;
  1012. }
  1013. return offset;
  1014. }
  1015. return offset;
  1016. }
  1017. /* SunOS 5.x hardware capability descriptions */
  1018. typedef struct cap_desc {
  1019. uint64_t cd_mask;
  1020. const char *cd_name;
  1021. } cap_desc_t;
  1022. static const cap_desc_t cap_desc_sparc[] = {
  1023. { AV_SPARC_MUL32, "MUL32" },
  1024. { AV_SPARC_DIV32, "DIV32" },
  1025. { AV_SPARC_FSMULD, "FSMULD" },
  1026. { AV_SPARC_V8PLUS, "V8PLUS" },
  1027. { AV_SPARC_POPC, "POPC" },
  1028. { AV_SPARC_VIS, "VIS" },
  1029. { AV_SPARC_VIS2, "VIS2" },
  1030. { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
  1031. { AV_SPARC_FMAF, "FMAF" },
  1032. { AV_SPARC_FJFMAU, "FJFMAU" },
  1033. { AV_SPARC_IMA, "IMA" },
  1034. { 0, NULL }
  1035. };
  1036. static const cap_desc_t cap_desc_386[] = {
  1037. { AV_386_FPU, "FPU" },
  1038. { AV_386_TSC, "TSC" },
  1039. { AV_386_CX8, "CX8" },
  1040. { AV_386_SEP, "SEP" },
  1041. { AV_386_AMD_SYSC, "AMD_SYSC" },
  1042. { AV_386_CMOV, "CMOV" },
  1043. { AV_386_MMX, "MMX" },
  1044. { AV_386_AMD_MMX, "AMD_MMX" },
  1045. { AV_386_AMD_3DNow, "AMD_3DNow" },
  1046. { AV_386_AMD_3DNowx, "AMD_3DNowx" },
  1047. { AV_386_FXSR, "FXSR" },
  1048. { AV_386_SSE, "SSE" },
  1049. { AV_386_SSE2, "SSE2" },
  1050. { AV_386_PAUSE, "PAUSE" },
  1051. { AV_386_SSE3, "SSE3" },
  1052. { AV_386_MON, "MON" },
  1053. { AV_386_CX16, "CX16" },
  1054. { AV_386_AHF, "AHF" },
  1055. { AV_386_TSCP, "TSCP" },
  1056. { AV_386_AMD_SSE4A, "AMD_SSE4A" },
  1057. { AV_386_POPCNT, "POPCNT" },
  1058. { AV_386_AMD_LZCNT, "AMD_LZCNT" },
  1059. { AV_386_SSSE3, "SSSE3" },
  1060. { AV_386_SSE4_1, "SSE4.1" },
  1061. { AV_386_SSE4_2, "SSE4.2" },
  1062. { 0, NULL }
  1063. };
  1064. private int
  1065. doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
  1066. size_t size, off_t fsize, int mach, int strtab, int *flags,
  1067. uint16_t *notecount)
  1068. {
  1069. Elf32_Shdr sh32;
  1070. Elf64_Shdr sh64;
  1071. int stripped = 1, has_debug_info = 1;
  1072. size_t nbadcap = 0;
  1073. void *nbuf;
  1074. off_t noff, coff, name_off;
  1075. uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */
  1076. uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */
  1077. char name[50];
  1078. ssize_t namesize;
  1079. if (size != xsh_sizeof) {
  1080. if (file_printf(ms, ", corrupted section header size") == -1)
  1081. return -1;
  1082. return 0;
  1083. }
  1084. /* Read offset of name section to be able to read section names later */
  1085. if (pread(fd, xsh_addr, xsh_sizeof, CAST(off_t, (off + size * strtab)))
  1086. < (ssize_t)xsh_sizeof) {
  1087. if (file_printf(ms, ", missing section headers") == -1)
  1088. return -1;
  1089. return 0;
  1090. }
  1091. name_off = xsh_offset;
  1092. for ( ; num; num--) {
  1093. /* Read the name of this section. */
  1094. if ((namesize = pread(fd, name, sizeof(name) - 1, name_off + xsh_name)) == -1) {
  1095. file_badread(ms);
  1096. return -1;
  1097. }
  1098. name[namesize] = '\0';
  1099. if (strcmp(name, ".debug_info") == 0) {
  1100. has_debug_info = 1;
  1101. stripped = 0;
  1102. }
  1103. if (pread(fd, xsh_addr, xsh_sizeof, off) < (ssize_t)xsh_sizeof) {
  1104. file_badread(ms);
  1105. return -1;
  1106. }
  1107. off += size;
  1108. /* Things we can determine before we seek */
  1109. switch (xsh_type) {
  1110. case SHT_SYMTAB:
  1111. #if 0
  1112. case SHT_DYNSYM:
  1113. #endif
  1114. stripped = 0;
  1115. break;
  1116. default:
  1117. if (fsize != SIZE_UNKNOWN && xsh_offset > fsize) {
  1118. /* Perhaps warn here */
  1119. continue;
  1120. }
  1121. break;
  1122. }
  1123. /* Things we can determine when we seek */
  1124. switch (xsh_type) {
  1125. case SHT_NOTE:
  1126. if ((uintmax_t)(xsh_size + xsh_offset) >
  1127. (uintmax_t)fsize) {
  1128. if (file_printf(ms,
  1129. ", note offset/size 0x%" INTMAX_T_FORMAT
  1130. "x+0x%" INTMAX_T_FORMAT "x exceeds"
  1131. " file size 0x%" INTMAX_T_FORMAT "x",
  1132. (uintmax_t)xsh_offset, (uintmax_t)xsh_size,
  1133. (uintmax_t)fsize) == -1)
  1134. return -1;
  1135. return 0;
  1136. }
  1137. if ((nbuf = malloc(xsh_size)) == NULL) {
  1138. file_error(ms, errno, "Cannot allocate memory"
  1139. " for note");
  1140. return -1;
  1141. }
  1142. if (pread(fd, nbuf, xsh_size, xsh_offset) <
  1143. (ssize_t)xsh_size) {
  1144. file_badread(ms);
  1145. free(nbuf);
  1146. return -1;
  1147. }
  1148. noff = 0;
  1149. for (;;) {
  1150. if (noff >= (off_t)xsh_size)
  1151. break;
  1152. noff = donote(ms, nbuf, (size_t)noff,
  1153. xsh_size, clazz, swap, 4, flags, notecount,
  1154. fd, 0, 0, 0);
  1155. if (noff == 0)
  1156. break;
  1157. }
  1158. free(nbuf);
  1159. break;
  1160. case SHT_SUNW_cap:
  1161. switch (mach) {
  1162. case EM_SPARC:
  1163. case EM_SPARCV9:
  1164. case EM_IA_64:
  1165. case EM_386:
  1166. case EM_AMD64:
  1167. break;
  1168. default:
  1169. goto skip;
  1170. }
  1171. if (nbadcap > 5)
  1172. break;
  1173. if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) {
  1174. file_badseek(ms);
  1175. return -1;
  1176. }
  1177. coff = 0;
  1178. for (;;) {
  1179. Elf32_Cap cap32;
  1180. Elf64_Cap cap64;
  1181. char cbuf[/*CONSTCOND*/
  1182. MAX(sizeof cap32, sizeof cap64)];
  1183. if ((coff += xcap_sizeof) > (off_t)xsh_size)
  1184. break;
  1185. if (read(fd, cbuf, (size_t)xcap_sizeof) !=
  1186. (ssize_t)xcap_sizeof) {
  1187. file_badread(ms);
  1188. return -1;
  1189. }
  1190. if (cbuf[0] == 'A') {
  1191. #ifdef notyet
  1192. char *p = cbuf + 1;
  1193. uint32_t len, tag;
  1194. memcpy(&len, p, sizeof(len));
  1195. p += 4;
  1196. len = getu32(swap, len);
  1197. if (memcmp("gnu", p, 3) != 0) {
  1198. if (file_printf(ms,
  1199. ", unknown capability %.3s", p)
  1200. == -1)
  1201. return -1;
  1202. break;
  1203. }
  1204. p += strlen(p) + 1;
  1205. tag = *p++;
  1206. memcpy(&len, p, sizeof(len));
  1207. p += 4;
  1208. len = getu32(swap, len);
  1209. if (tag != 1) {
  1210. if (file_printf(ms, ", unknown gnu"
  1211. " capability tag %d", tag)
  1212. == -1)
  1213. return -1;
  1214. break;
  1215. }
  1216. // gnu attributes
  1217. #endif
  1218. break;
  1219. }
  1220. (void)memcpy(xcap_addr, cbuf, xcap_sizeof);
  1221. switch (xcap_tag) {
  1222. case CA_SUNW_NULL:
  1223. break;
  1224. case CA_SUNW_HW_1:
  1225. cap_hw1 |= xcap_val;
  1226. break;
  1227. case CA_SUNW_SF_1:
  1228. cap_sf1 |= xcap_val;
  1229. break;
  1230. default:
  1231. if (file_printf(ms,
  1232. ", with unknown capability "
  1233. "0x%" INT64_T_FORMAT "x = 0x%"
  1234. INT64_T_FORMAT "x",
  1235. (unsigned long long)xcap_tag,
  1236. (unsigned long long)xcap_val) == -1)
  1237. return -1;
  1238. if (nbadcap++ > 2)
  1239. coff = xsh_size;
  1240. break;
  1241. }
  1242. }
  1243. /*FALLTHROUGH*/
  1244. skip:
  1245. default:
  1246. break;
  1247. }
  1248. }
  1249. if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
  1250. return -1;
  1251. if (has_debug_info) {
  1252. if (file_printf(ms, ", with debug_info") == -1)
  1253. return -1;
  1254. }
  1255. if (cap_hw1) {
  1256. const cap_desc_t *cdp;
  1257. switch (mach) {
  1258. case EM_SPARC:
  1259. case EM_SPARC32PLUS:
  1260. case EM_SPARCV9:
  1261. cdp = cap_desc_sparc;
  1262. break;
  1263. case EM_386:
  1264. case EM_IA_64:
  1265. case EM_AMD64:
  1266. cdp = cap_desc_386;
  1267. break;
  1268. default:
  1269. cdp = NULL;
  1270. break;
  1271. }
  1272. if (file_printf(ms, ", uses") == -1)
  1273. return -1;
  1274. if (cdp) {
  1275. while (cdp->cd_name) {
  1276. if (cap_hw1 & cdp->cd_mask) {
  1277. if (file_printf(ms,
  1278. " %s", cdp->cd_name) == -1)
  1279. return -1;
  1280. cap_hw1 &= ~cdp->cd_mask;
  1281. }
  1282. ++cdp;
  1283. }
  1284. if (cap_hw1)
  1285. if (file_printf(ms,
  1286. " unknown hardware capability 0x%"
  1287. INT64_T_FORMAT "x",
  1288. (unsigned long long)cap_hw1) == -1)
  1289. return -1;
  1290. } else {
  1291. if (file_printf(ms,
  1292. " hardware capability 0x%" INT64_T_FORMAT "x",
  1293. (unsigned long long)cap_hw1) == -1)
  1294. return -1;
  1295. }
  1296. }
  1297. if (cap_sf1) {
  1298. if (cap_sf1 & SF1_SUNW_FPUSED) {
  1299. if (file_printf(ms,
  1300. (cap_sf1 & SF1_SUNW_FPKNWN)
  1301. ? ", uses frame pointer"
  1302. : ", not known to use frame pointer") == -1)
  1303. return -1;
  1304. }
  1305. cap_sf1 &= ~SF1_SUNW_MASK;
  1306. if (cap_sf1)
  1307. if (file_printf(ms,
  1308. ", with unknown software capability 0x%"
  1309. INT64_T_FORMAT "x",
  1310. (unsigned long long)cap_sf1) == -1)
  1311. return -1;
  1312. }
  1313. return 0;
  1314. }
  1315. /*
  1316. * Look through the program headers of an executable image, searching
  1317. * for a PT_INTERP section; if one is found, it's dynamically linked,
  1318. * otherwise it's statically linked.
  1319. */
  1320. private int
  1321. dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
  1322. int num, size_t size, off_t fsize, int sh_num, int *flags,
  1323. uint16_t *notecount)
  1324. {
  1325. Elf32_Phdr ph32;
  1326. Elf64_Phdr ph64;
  1327. const char *linking_style = "statically";
  1328. const char *interp = "";
  1329. unsigned char nbuf[BUFSIZ];
  1330. char ibuf[BUFSIZ];
  1331. ssize_t bufsize;
  1332. size_t offset, align, len;
  1333. if (size != xph_sizeof) {
  1334. if (file_printf(ms, ", corrupted program header size") == -1)
  1335. return -1;
  1336. return 0;
  1337. }
  1338. for ( ; num; num--) {
  1339. if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) {
  1340. file_badread(ms);
  1341. return -1;
  1342. }
  1343. off += size;
  1344. bufsize = 0;
  1345. align = 4;
  1346. /* Things we can determine before we seek */
  1347. switch (xph_type) {
  1348. case PT_DYNAMIC:
  1349. linking_style = "dynamically";
  1350. break;
  1351. case PT_NOTE:
  1352. if (sh_num) /* Did this through section headers */
  1353. continue;
  1354. if (((align = xph_align) & 0x80000000UL) != 0 ||
  1355. align < 4) {
  1356. if (file_printf(ms,
  1357. ", invalid note alignment 0x%lx",
  1358. (unsigned long)align) == -1)
  1359. return -1;
  1360. align = 4;
  1361. }
  1362. /*FALLTHROUGH*/
  1363. case PT_INTERP:
  1364. len = xph_filesz < sizeof(nbuf) ? xph_filesz
  1365. : sizeof(nbuf);
  1366. bufsize = pread(fd, nbuf, len, xph_offset);
  1367. if (bufsize == -1) {
  1368. file_badread(ms);
  1369. return -1;
  1370. }
  1371. break;
  1372. default:
  1373. if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
  1374. /* Maybe warn here? */
  1375. continue;
  1376. }
  1377. break;
  1378. }
  1379. /* Things we can determine when we seek */
  1380. switch (xph_type) {
  1381. case PT_INTERP:
  1382. if (bufsize && nbuf[0]) {
  1383. nbuf[bufsize - 1] = '\0';
  1384. interp = (const char *)nbuf;
  1385. } else
  1386. interp = "*empty*";
  1387. break;
  1388. case PT_NOTE:
  1389. /*
  1390. * This is a PT_NOTE section; loop through all the notes
  1391. * in the section.
  1392. */
  1393. offset = 0;
  1394. for (;;) {
  1395. if (offset >= (size_t)bufsize)
  1396. break;
  1397. offset = donote(ms, nbuf, offset,
  1398. (size_t)bufsize, clazz, swap, align,
  1399. flags, notecount, fd, 0, 0, 0);
  1400. if (offset == 0)
  1401. break;
  1402. }
  1403. break;
  1404. default:
  1405. break;
  1406. }
  1407. }
  1408. if (file_printf(ms, ", %s linked", linking_style)
  1409. == -1)
  1410. return -1;
  1411. if (interp[0])
  1412. if (file_printf(ms, ", interpreter %s",
  1413. file_printable(ibuf, sizeof(ibuf), interp)) == -1)
  1414. return -1;
  1415. return 0;
  1416. }
  1417. protected int
  1418. file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
  1419. size_t nbytes)
  1420. {
  1421. union {
  1422. int32_t l;
  1423. char c[sizeof (int32_t)];
  1424. } u;
  1425. int clazz;
  1426. int swap;
  1427. struct stat st;
  1428. off_t fsize;
  1429. int flags = 0;
  1430. Elf32_Ehdr elf32hdr;
  1431. Elf64_Ehdr elf64hdr;
  1432. uint16_t type, phnum, shnum, notecount;
  1433. if (ms->flags & (MAGIC_MIME|MAGIC_APPLE|MAGIC_EXTENSION))
  1434. return 0;
  1435. /*
  1436. * ELF executables have multiple section headers in arbitrary
  1437. * file locations and thus file(1) cannot determine it from easily.
  1438. * Instead we traverse thru all section headers until a symbol table
  1439. * one is found or else the binary is stripped.
  1440. * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
  1441. */
  1442. if (buf[EI_MAG0] != ELFMAG0
  1443. || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
  1444. || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
  1445. return 0;
  1446. /*
  1447. * If we cannot seek, it must be a pipe, socket or fifo.
  1448. */
  1449. if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
  1450. fd = file_pipe2file(ms, fd, buf, nbytes);
  1451. if (fstat(fd, &st) == -1) {
  1452. file_badread(ms);
  1453. return -1;
  1454. }
  1455. if (S_ISREG(st.st_mode) || st.st_size != 0)
  1456. fsize = st.st_size;
  1457. else
  1458. fsize = SIZE_UNKNOWN;
  1459. clazz = buf[EI_CLASS];
  1460. switch (clazz) {
  1461. case ELFCLASS32:
  1462. #undef elf_getu
  1463. #define elf_getu(a, b) elf_getu32(a, b)
  1464. #undef elfhdr
  1465. #define elfhdr elf32hdr
  1466. #include "elfclass.h"
  1467. case ELFCLASS64:
  1468. #undef elf_getu
  1469. #define elf_getu(a, b) elf_getu64(a, b)
  1470. #undef elfhdr
  1471. #define elfhdr elf64hdr
  1472. #include "elfclass.h"
  1473. default:
  1474. if (file_printf(ms, ", unknown class %d", clazz) == -1)
  1475. return -1;
  1476. break;
  1477. }
  1478. return 0;
  1479. }
  1480. #endif