readelf.c 44 KB

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