readelf.c 41 KB

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