readelf.c 40 KB

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