readelf.c 42 KB

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