readelf.c 43 KB

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