readelf.c 40 KB

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