readelf.c 44 KB

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