readelf.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. #ifdef BUILTIN_ELF
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <stdlib.h>
  32. #ifdef HAVE_UNISTD_H
  33. #include <unistd.h>
  34. #endif
  35. #include "readelf.h"
  36. #include "magic.h"
  37. #ifndef lint
  38. FILE_RCSID("@(#)$File: readelf.c,v 1.73 2008/03/27 22:00:28 christos Exp $")
  39. #endif
  40. #ifdef ELFCORE
  41. private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
  42. off_t, int *);
  43. #endif
  44. private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
  45. off_t, int *);
  46. private int doshn(struct magic_set *, int, int, int, off_t, int, size_t, int *);
  47. private size_t donote(struct magic_set *, unsigned char *, size_t, size_t, int,
  48. int, size_t, int *);
  49. #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
  50. #define isquote(c) (strchr("'\"`", (c)) != NULL)
  51. private uint16_t getu16(int, uint16_t);
  52. private uint32_t getu32(int, uint32_t);
  53. private uint64_t getu64(int, uint64_t);
  54. private uint16_t
  55. getu16(int swap, uint16_t value)
  56. {
  57. union {
  58. uint16_t ui;
  59. char c[2];
  60. } retval, tmpval;
  61. if (swap) {
  62. tmpval.ui = value;
  63. retval.c[0] = tmpval.c[1];
  64. retval.c[1] = tmpval.c[0];
  65. return retval.ui;
  66. } else
  67. return value;
  68. }
  69. private uint32_t
  70. getu32(int swap, uint32_t value)
  71. {
  72. union {
  73. uint32_t ui;
  74. char c[4];
  75. } retval, tmpval;
  76. if (swap) {
  77. tmpval.ui = value;
  78. retval.c[0] = tmpval.c[3];
  79. retval.c[1] = tmpval.c[2];
  80. retval.c[2] = tmpval.c[1];
  81. retval.c[3] = tmpval.c[0];
  82. return retval.ui;
  83. } else
  84. return value;
  85. }
  86. private uint64_t
  87. getu64(int swap, uint64_t value)
  88. {
  89. union {
  90. uint64_t ui;
  91. char c[8];
  92. } retval, tmpval;
  93. if (swap) {
  94. tmpval.ui = value;
  95. retval.c[0] = tmpval.c[7];
  96. retval.c[1] = tmpval.c[6];
  97. retval.c[2] = tmpval.c[5];
  98. retval.c[3] = tmpval.c[4];
  99. retval.c[4] = tmpval.c[3];
  100. retval.c[5] = tmpval.c[2];
  101. retval.c[6] = tmpval.c[1];
  102. retval.c[7] = tmpval.c[0];
  103. return retval.ui;
  104. } else
  105. return value;
  106. }
  107. #define elf_getu16(swap, value) getu16(swap, value)
  108. #define elf_getu32(swap, value) getu32(swap, value)
  109. #ifdef USE_ARRAY_FOR_64BIT_TYPES
  110. # define elf_getu64(swap, array) \
  111. ((swap ? ((uint64_t)elf_getu32(swap, array[0])) << 32 : elf_getu32(swap, array[0])) + \
  112. (swap ? elf_getu32(swap, array[1]) : ((uint64_t)elf_getu32(swap, array[1]) << 32)))
  113. #else
  114. # define elf_getu64(swap, value) getu64(swap, value)
  115. #endif
  116. #define xsh_addr (class == ELFCLASS32 \
  117. ? (void *) &sh32 \
  118. : (void *) &sh64)
  119. #define xsh_sizeof (class == ELFCLASS32 \
  120. ? sizeof sh32 \
  121. : sizeof sh64)
  122. #define xsh_size (class == ELFCLASS32 \
  123. ? elf_getu32(swap, sh32.sh_size) \
  124. : elf_getu64(swap, sh64.sh_size))
  125. #define xsh_offset (class == ELFCLASS32 \
  126. ? elf_getu32(swap, sh32.sh_offset) \
  127. : elf_getu64(swap, sh64.sh_offset))
  128. #define xsh_type (class == ELFCLASS32 \
  129. ? elf_getu32(swap, sh32.sh_type) \
  130. : elf_getu32(swap, sh64.sh_type))
  131. #define xph_addr (class == ELFCLASS32 \
  132. ? (void *) &ph32 \
  133. : (void *) &ph64)
  134. #define xph_sizeof (class == ELFCLASS32 \
  135. ? sizeof ph32 \
  136. : sizeof ph64)
  137. #define xph_type (class == ELFCLASS32 \
  138. ? elf_getu32(swap, ph32.p_type) \
  139. : elf_getu32(swap, ph64.p_type))
  140. #define xph_offset (off_t)(class == ELFCLASS32 \
  141. ? elf_getu32(swap, ph32.p_offset) \
  142. : elf_getu64(swap, ph64.p_offset))
  143. #define xph_align (size_t)((class == ELFCLASS32 \
  144. ? (off_t) (ph32.p_align ? \
  145. elf_getu32(swap, ph32.p_align) : 4) \
  146. : (off_t) (ph64.p_align ? \
  147. elf_getu64(swap, ph64.p_align) : 4)))
  148. #define xph_filesz (size_t)((class == ELFCLASS32 \
  149. ? elf_getu32(swap, ph32.p_filesz) \
  150. : elf_getu64(swap, ph64.p_filesz)))
  151. #define xnh_addr (class == ELFCLASS32 \
  152. ? (void *) &nh32 \
  153. : (void *) &nh64)
  154. #define xph_memsz (size_t)((class == ELFCLASS32 \
  155. ? elf_getu32(swap, ph32.p_memsz) \
  156. : elf_getu64(swap, ph64.p_memsz)))
  157. #define xnh_sizeof (class == ELFCLASS32 \
  158. ? sizeof nh32 \
  159. : sizeof nh64)
  160. #define xnh_type (class == ELFCLASS32 \
  161. ? elf_getu32(swap, nh32.n_type) \
  162. : elf_getu32(swap, nh64.n_type))
  163. #define xnh_namesz (class == ELFCLASS32 \
  164. ? elf_getu32(swap, nh32.n_namesz) \
  165. : elf_getu32(swap, nh64.n_namesz))
  166. #define xnh_descsz (class == ELFCLASS32 \
  167. ? elf_getu32(swap, nh32.n_descsz) \
  168. : elf_getu32(swap, nh64.n_descsz))
  169. #define prpsoffsets(i) (class == ELFCLASS32 \
  170. ? prpsoffsets32[i] \
  171. : prpsoffsets64[i])
  172. #ifdef ELFCORE
  173. /*
  174. * Try larger offsets first to avoid false matches
  175. * from earlier data that happen to look like strings.
  176. */
  177. static const size_t prpsoffsets32[] = {
  178. #ifdef USE_NT_PSINFO
  179. 104, /* SunOS 5.x (command line) */
  180. 88, /* SunOS 5.x (short name) */
  181. #endif /* USE_NT_PSINFO */
  182. 100, /* SunOS 5.x (command line) */
  183. 84, /* SunOS 5.x (short name) */
  184. 44, /* Linux (command line) */
  185. 28, /* Linux 2.0.36 (short name) */
  186. 8, /* FreeBSD */
  187. };
  188. static const size_t prpsoffsets64[] = {
  189. #ifdef USE_NT_PSINFO
  190. 152, /* SunOS 5.x (command line) */
  191. 136, /* SunOS 5.x (short name) */
  192. #endif /* USE_NT_PSINFO */
  193. 136, /* SunOS 5.x, 64-bit (command line) */
  194. 120, /* SunOS 5.x, 64-bit (short name) */
  195. 56, /* Linux (command line) */
  196. 40, /* Linux (tested on core from 2.4.x, short name) */
  197. 16, /* FreeBSD, 64-bit */
  198. };
  199. #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
  200. #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
  201. #define NOFFSETS (class == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
  202. /*
  203. * Look through the program headers of an executable image, searching
  204. * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
  205. * "FreeBSD"; if one is found, try looking in various places in its
  206. * contents for a 16-character string containing only printable
  207. * characters - if found, that string should be the name of the program
  208. * that dropped core. Note: right after that 16-character string is,
  209. * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
  210. * Linux, a longer string (80 characters, in 5.x, probably other
  211. * SVR4-flavored systems, and Linux) containing the start of the
  212. * command line for that program.
  213. *
  214. * SunOS 5.x core files contain two PT_NOTE sections, with the types
  215. * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
  216. * same info about the command name and command line, so it probably
  217. * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
  218. * above (see USE_NT_PSINFO), in case we ever decide to do so. The
  219. * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
  220. * the SunOS 5.x file command relies on this (and prefers the latter).
  221. *
  222. * The signal number probably appears in a section of type NT_PRSTATUS,
  223. * but that's also rather OS-dependent, in ways that are harder to
  224. * dissect with heuristics, so I'm not bothering with the signal number.
  225. * (I suppose the signal number could be of interest in situations where
  226. * you don't have the binary of the program that dropped core; if you
  227. * *do* have that binary, the debugger will probably tell you what
  228. * signal it was.)
  229. */
  230. #define OS_STYLE_SVR4 0
  231. #define OS_STYLE_FREEBSD 1
  232. #define OS_STYLE_NETBSD 2
  233. private const char os_style_names[][8] = {
  234. "SVR4",
  235. "FreeBSD",
  236. "NetBSD",
  237. };
  238. #define FLAGS_DID_CORE 1
  239. #define FLAGS_DID_NOTE 2
  240. #define FLAGS_DID_CORE_STYLE 4
  241. private int
  242. dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
  243. int num, size_t size, off_t fsize, int *flags)
  244. {
  245. Elf32_Phdr ph32;
  246. Elf64_Phdr ph64;
  247. size_t offset;
  248. unsigned char nbuf[BUFSIZ];
  249. ssize_t bufsize;
  250. off_t savedoffset;
  251. struct stat st;
  252. if (fstat(fd, &st) < 0) {
  253. file_badread(ms);
  254. return -1;
  255. }
  256. if (size != xph_sizeof) {
  257. if (file_printf(ms, ", corrupted program header size") == -1)
  258. return -1;
  259. return 0;
  260. }
  261. /*
  262. * Loop through all the program headers.
  263. */
  264. for ( ; num; num--) {
  265. if ((savedoffset = lseek(fd, off, SEEK_SET)) == (off_t)-1) {
  266. file_badseek(ms);
  267. return -1;
  268. }
  269. if (read(fd, xph_addr, xph_sizeof) == -1) {
  270. file_badread(ms);
  271. return -1;
  272. }
  273. if (xph_offset > fsize) {
  274. if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
  275. file_badseek(ms);
  276. return -1;
  277. }
  278. continue;
  279. }
  280. off += size;
  281. if (xph_type != PT_NOTE)
  282. continue;
  283. /*
  284. * This is a PT_NOTE section; loop through all the notes
  285. * in the section.
  286. */
  287. if (lseek(fd, xph_offset, SEEK_SET) == (off_t)-1) {
  288. file_badseek(ms);
  289. return -1;
  290. }
  291. bufsize = read(fd, nbuf,
  292. ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf)));
  293. if (bufsize == -1) {
  294. file_badread(ms);
  295. return -1;
  296. }
  297. offset = 0;
  298. for (;;) {
  299. if (offset >= (size_t)bufsize)
  300. break;
  301. offset = donote(ms, nbuf, offset, (size_t)bufsize,
  302. class, swap, 4, flags);
  303. if (offset == 0)
  304. break;
  305. }
  306. }
  307. return 0;
  308. }
  309. #endif
  310. private size_t
  311. donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
  312. int class, int swap, size_t align, int *flags)
  313. {
  314. Elf32_Nhdr nh32;
  315. Elf64_Nhdr nh64;
  316. size_t noff, doff;
  317. #ifdef ELFCORE
  318. int os_style = -1;
  319. #endif
  320. uint32_t namesz, descsz;
  321. (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
  322. offset += xnh_sizeof;
  323. namesz = xnh_namesz;
  324. descsz = xnh_descsz;
  325. if ((namesz == 0) && (descsz == 0)) {
  326. /*
  327. * We're out of note headers.
  328. */
  329. return (offset >= size) ? offset : size;
  330. }
  331. if (namesz & 0x80000000) {
  332. (void)file_printf(ms, ", bad note name size 0x%lx",
  333. (unsigned long)namesz);
  334. return offset;
  335. }
  336. if (descsz & 0x80000000) {
  337. (void)file_printf(ms, ", bad note description size 0x%lx",
  338. (unsigned long)descsz);
  339. return offset;
  340. }
  341. noff = offset;
  342. doff = ELF_ALIGN(offset + namesz);
  343. if (offset + namesz > size) {
  344. /*
  345. * We're past the end of the buffer.
  346. */
  347. return doff;
  348. }
  349. offset = ELF_ALIGN(doff + descsz);
  350. if (doff + descsz > size) {
  351. /*
  352. * We're past the end of the buffer.
  353. */
  354. return (offset >= size) ? offset : size;
  355. }
  356. if (*flags & FLAGS_DID_NOTE)
  357. goto core;
  358. if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  359. xnh_type == NT_GNU_VERSION && descsz == 16) {
  360. uint32_t desc[4];
  361. (void)memcpy(desc, &nbuf[doff], sizeof(desc));
  362. if (file_printf(ms, ", for GNU/") == -1)
  363. return size;
  364. switch (elf_getu32(swap, desc[0])) {
  365. case GNU_OS_LINUX:
  366. if (file_printf(ms, "Linux") == -1)
  367. return size;
  368. break;
  369. case GNU_OS_HURD:
  370. if (file_printf(ms, "Hurd") == -1)
  371. return size;
  372. break;
  373. case GNU_OS_SOLARIS:
  374. if (file_printf(ms, "Solaris") == -1)
  375. return size;
  376. break;
  377. case GNU_OS_KFREEBSD:
  378. if (file_printf(ms, "kFreeBSD") == -1)
  379. return size;
  380. break;
  381. case GNU_OS_KNETBSD:
  382. if (file_printf(ms, "kNetBSD") == -1)
  383. return size;
  384. break;
  385. default:
  386. if (file_printf(ms, "<unknown>") == -1)
  387. return size;
  388. }
  389. if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
  390. elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
  391. return size;
  392. *flags |= FLAGS_DID_NOTE;
  393. return size;
  394. }
  395. if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
  396. xnh_type == NT_NETBSD_VERSION && descsz == 4) {
  397. uint32_t desc;
  398. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  399. desc = elf_getu32(swap, desc);
  400. if (file_printf(ms, ", for NetBSD") == -1)
  401. return size;
  402. /*
  403. * The version number used to be stuck as 199905, and was thus
  404. * basically content-free. Newer versions of NetBSD have fixed
  405. * this and now use the encoding of __NetBSD_Version__:
  406. *
  407. * MMmmrrpp00
  408. *
  409. * M = major version
  410. * m = minor version
  411. * r = release ["",A-Z,Z[A-Z] but numeric]
  412. * p = patchlevel
  413. */
  414. if (desc > 100000000U) {
  415. uint32_t ver_patch = (desc / 100) % 100;
  416. uint32_t ver_rel = (desc / 10000) % 100;
  417. uint32_t ver_min = (desc / 1000000) % 100;
  418. uint32_t ver_maj = desc / 100000000;
  419. if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
  420. return size;
  421. if (ver_rel == 0 && ver_patch != 0) {
  422. if (file_printf(ms, ".%u", ver_patch) == -1)
  423. return size;
  424. } else if (ver_rel != 0) {
  425. while (ver_rel > 26) {
  426. if (file_printf(ms, "Z") == -1)
  427. return size;
  428. ver_rel -= 26;
  429. }
  430. if (file_printf(ms, "%c", 'A' + ver_rel - 1)
  431. == -1)
  432. return size;
  433. }
  434. }
  435. *flags |= FLAGS_DID_NOTE;
  436. return size;
  437. }
  438. if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
  439. xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
  440. uint32_t desc;
  441. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  442. desc = elf_getu32(swap, desc);
  443. if (file_printf(ms, ", for FreeBSD") == -1)
  444. return size;
  445. /*
  446. * Contents is __FreeBSD_version, whose relation to OS
  447. * versions is defined by a huge table in the Porter's
  448. * Handbook. This is the general scheme:
  449. *
  450. * Releases:
  451. * Mmp000 (before 4.10)
  452. * Mmi0p0 (before 5.0)
  453. * Mmm0p0
  454. *
  455. * Development branches:
  456. * Mmpxxx (before 4.6)
  457. * Mmp1xx (before 4.10)
  458. * Mmi1xx (before 5.0)
  459. * M000xx (pre-M.0)
  460. * Mmm1xx
  461. *
  462. * M = major version
  463. * m = minor version
  464. * i = minor version increment (491000 -> 4.10)
  465. * p = patchlevel
  466. * x = revision
  467. *
  468. * The first release of FreeBSD to use ELF by default
  469. * was version 3.0.
  470. */
  471. if (desc == 460002) {
  472. if (file_printf(ms, " 4.6.2") == -1)
  473. return size;
  474. } else if (desc < 460100) {
  475. if (file_printf(ms, " %d.%d", desc / 100000,
  476. desc / 10000 % 10) == -1)
  477. return size;
  478. if (desc / 1000 % 10 > 0)
  479. if (file_printf(ms, ".%d", desc / 1000 % 10)
  480. == -1)
  481. return size;
  482. if ((desc % 1000 > 0) || (desc % 100000 == 0))
  483. if (file_printf(ms, " (%d)", desc) == -1)
  484. return size;
  485. } else if (desc < 500000) {
  486. if (file_printf(ms, " %d.%d", desc / 100000,
  487. desc / 10000 % 10 + desc / 1000 % 10) == -1)
  488. return size;
  489. if (desc / 100 % 10 > 0) {
  490. if (file_printf(ms, " (%d)", desc) == -1)
  491. return size;
  492. } else if (desc / 10 % 10 > 0) {
  493. if (file_printf(ms, ".%d", desc / 10 % 10)
  494. == -1)
  495. return size;
  496. }
  497. } else {
  498. if (file_printf(ms, " %d.%d", desc / 100000,
  499. desc / 1000 % 100) == -1)
  500. return size;
  501. if ((desc / 100 % 10 > 0) ||
  502. (desc % 100000 / 100 == 0)) {
  503. if (file_printf(ms, " (%d)", desc) == -1)
  504. return size;
  505. } else if (desc / 10 % 10 > 0) {
  506. if (file_printf(ms, ".%d", desc / 10 % 10)
  507. == -1)
  508. return size;
  509. }
  510. }
  511. *flags |= FLAGS_DID_NOTE;
  512. return size;
  513. }
  514. if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
  515. xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
  516. if (file_printf(ms, ", for OpenBSD") == -1)
  517. return size;
  518. /* Content of note is always 0 */
  519. *flags |= FLAGS_DID_NOTE;
  520. return size;
  521. }
  522. if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
  523. xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
  524. uint32_t desc;
  525. if (file_printf(ms, ", for DragonFly") == -1)
  526. return size;
  527. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  528. desc = elf_getu32(swap, desc);
  529. if (file_printf(ms, " %d.%d.%d", desc / 100000,
  530. desc / 10000 % 10, desc % 10000) == -1)
  531. return size;
  532. *flags |= FLAGS_DID_NOTE;
  533. return size;
  534. }
  535. core:
  536. /*
  537. * Sigh. The 2.0.36 kernel in Debian 2.1, at
  538. * least, doesn't correctly implement name
  539. * sections, in core dumps, as specified by
  540. * the "Program Linking" section of "UNIX(R) System
  541. * V Release 4 Programmer's Guide: ANSI C and
  542. * Programming Support Tools", because my copy
  543. * clearly says "The first 'namesz' bytes in 'name'
  544. * contain a *null-terminated* [emphasis mine]
  545. * character representation of the entry's owner
  546. * or originator", but the 2.0.36 kernel code
  547. * doesn't include the terminating null in the
  548. * name....
  549. */
  550. if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
  551. (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
  552. os_style = OS_STYLE_SVR4;
  553. }
  554. if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
  555. os_style = OS_STYLE_FREEBSD;
  556. }
  557. if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
  558. == 0)) {
  559. os_style = OS_STYLE_NETBSD;
  560. }
  561. #ifdef ELFCORE
  562. if ((*flags & FLAGS_DID_CORE) != 0)
  563. return size;
  564. if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
  565. if (file_printf(ms, ", %s-style", os_style_names[os_style])
  566. == -1)
  567. return size;
  568. *flags |= FLAGS_DID_CORE_STYLE;
  569. }
  570. switch (os_style) {
  571. case OS_STYLE_NETBSD:
  572. if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
  573. uint32_t signo;
  574. /*
  575. * Extract the program name. It is at
  576. * offset 0x7c, and is up to 32-bytes,
  577. * including the terminating NUL.
  578. */
  579. if (file_printf(ms, ", from '%.31s'",
  580. &nbuf[doff + 0x7c]) == -1)
  581. return size;
  582. /*
  583. * Extract the signal number. It is at
  584. * offset 0x08.
  585. */
  586. (void)memcpy(&signo, &nbuf[doff + 0x08],
  587. sizeof(signo));
  588. if (file_printf(ms, " (signal %u)",
  589. elf_getu32(swap, signo)) == -1)
  590. return size;
  591. *flags |= FLAGS_DID_CORE;
  592. return size;
  593. }
  594. break;
  595. default:
  596. if (xnh_type == NT_PRPSINFO) {
  597. size_t i, j;
  598. unsigned char c;
  599. /*
  600. * Extract the program name. We assume
  601. * it to be 16 characters (that's what it
  602. * is in SunOS 5.x and Linux).
  603. *
  604. * Unfortunately, it's at a different offset
  605. * in various OSes, so try multiple offsets.
  606. * If the characters aren't all printable,
  607. * reject it.
  608. */
  609. for (i = 0; i < NOFFSETS; i++) {
  610. unsigned char *cname, *cp;
  611. size_t reloffset = prpsoffsets(i);
  612. size_t noffset = doff + reloffset;
  613. for (j = 0; j < 16; j++, noffset++,
  614. reloffset++) {
  615. /*
  616. * Make sure we're not past
  617. * the end of the buffer; if
  618. * we are, just give up.
  619. */
  620. if (noffset >= size)
  621. goto tryanother;
  622. /*
  623. * Make sure we're not past
  624. * the end of the contents;
  625. * if we are, this obviously
  626. * isn't the right offset.
  627. */
  628. if (reloffset >= descsz)
  629. goto tryanother;
  630. c = nbuf[noffset];
  631. if (c == '\0') {
  632. /*
  633. * A '\0' at the
  634. * beginning is
  635. * obviously wrong.
  636. * Any other '\0'
  637. * means we're done.
  638. */
  639. if (j == 0)
  640. goto tryanother;
  641. else
  642. break;
  643. } else {
  644. /*
  645. * A nonprintable
  646. * character is also
  647. * wrong.
  648. */
  649. if (!isprint(c) || isquote(c))
  650. goto tryanother;
  651. }
  652. }
  653. /*
  654. * Well, that worked.
  655. */
  656. cname = (unsigned char *)
  657. &nbuf[doff + prpsoffsets(i)];
  658. for (cp = cname; *cp && isprint(*cp); cp++)
  659. continue;
  660. /*
  661. * Linux apparently appends a space at the end
  662. * of the command line: remove it.
  663. */
  664. while (cp > cname && isspace(cp[-1]))
  665. cp--;
  666. if (file_printf(ms, ", from '%.*s'",
  667. (int)(cp - cname), cname) == -1)
  668. return size;
  669. *flags |= FLAGS_DID_CORE;
  670. return size;
  671. tryanother:
  672. ;
  673. }
  674. }
  675. break;
  676. }
  677. #endif
  678. return offset;
  679. }
  680. private int
  681. doshn(struct magic_set *ms, int class, int swap, int fd, off_t off, int num,
  682. size_t size, int *flags)
  683. {
  684. Elf32_Shdr sh32;
  685. Elf64_Shdr sh64;
  686. int stripped = 1;
  687. void *nbuf;
  688. off_t noff;
  689. if (size != xsh_sizeof) {
  690. if (file_printf(ms, ", corrupted section header size") == -1)
  691. return -1;
  692. return 0;
  693. }
  694. if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
  695. file_badseek(ms);
  696. return -1;
  697. }
  698. for ( ; num; num--) {
  699. if (read(fd, xsh_addr, xsh_sizeof) == -1) {
  700. file_badread(ms);
  701. return -1;
  702. }
  703. switch (xsh_type) {
  704. case SHT_SYMTAB:
  705. #if 0
  706. case SHT_DYNSYM:
  707. #endif
  708. stripped = 0;
  709. break;
  710. case SHT_NOTE:
  711. if ((off = lseek(fd, (off_t)0, SEEK_CUR)) ==
  712. (off_t)-1) {
  713. file_badread(ms);
  714. return -1;
  715. }
  716. if ((nbuf = malloc((size_t)xsh_size)) == NULL) {
  717. file_error(ms, errno, "Cannot allocate memory"
  718. " for note");
  719. return -1;
  720. }
  721. if ((noff = lseek(fd, (off_t)xsh_offset, SEEK_SET)) ==
  722. (off_t)-1) {
  723. file_badread(ms);
  724. free(nbuf);
  725. return -1;
  726. }
  727. if (read(fd, nbuf, (size_t)xsh_size) !=
  728. (ssize_t)xsh_size) {
  729. free(nbuf);
  730. file_badread(ms);
  731. return -1;
  732. }
  733. noff = 0;
  734. for (;;) {
  735. if (noff >= (size_t)xsh_size)
  736. break;
  737. noff = donote(ms, nbuf, (size_t)noff,
  738. (size_t)xsh_size, class, swap, 4,
  739. flags);
  740. if (noff == 0)
  741. break;
  742. }
  743. if ((lseek(fd, off, SEEK_SET)) == (off_t)-1) {
  744. free(nbuf);
  745. file_badread(ms);
  746. return -1;
  747. }
  748. free(nbuf);
  749. break;
  750. }
  751. }
  752. if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
  753. return -1;
  754. return 0;
  755. }
  756. /*
  757. * Look through the program headers of an executable image, searching
  758. * for a PT_INTERP section; if one is found, it's dynamically linked,
  759. * otherwise it's statically linked.
  760. */
  761. private int
  762. dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
  763. int num, size_t size, off_t fsize, int *flags)
  764. {
  765. Elf32_Phdr ph32;
  766. Elf64_Phdr ph64;
  767. const char *linking_style = "statically";
  768. const char *shared_libraries = "";
  769. unsigned char nbuf[BUFSIZ];
  770. int bufsize;
  771. size_t offset, align;
  772. off_t savedoffset = (off_t)-1;
  773. struct stat st;
  774. if (fstat(fd, &st) < 0) {
  775. file_badread(ms);
  776. return -1;
  777. }
  778. if (size != xph_sizeof) {
  779. if (file_printf(ms, ", corrupted program header size") == -1)
  780. return -1;
  781. return 0;
  782. }
  783. if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
  784. file_badseek(ms);
  785. return -1;
  786. }
  787. for ( ; num; num--) {
  788. if (read(fd, xph_addr, xph_sizeof) == -1) {
  789. file_badread(ms);
  790. return -1;
  791. }
  792. if (xph_offset > st.st_size && savedoffset != (off_t)-1) {
  793. if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
  794. file_badseek(ms);
  795. return -1;
  796. }
  797. continue;
  798. }
  799. if ((savedoffset = lseek(fd, (off_t)0, SEEK_CUR)) == (off_t)-1) {
  800. file_badseek(ms);
  801. return -1;
  802. }
  803. if (xph_offset > fsize) {
  804. if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
  805. file_badseek(ms);
  806. return -1;
  807. }
  808. continue;
  809. }
  810. switch (xph_type) {
  811. case PT_DYNAMIC:
  812. linking_style = "dynamically";
  813. break;
  814. case PT_INTERP:
  815. shared_libraries = " (uses shared libs)";
  816. break;
  817. case PT_NOTE:
  818. if ((align = xph_align) & 0x80000000) {
  819. if (file_printf(ms,
  820. ", invalid note alignment 0x%lx",
  821. (unsigned long)align) == -1)
  822. return -1;
  823. align = 4;
  824. }
  825. /*
  826. * This is a PT_NOTE section; loop through all the notes
  827. * in the section.
  828. */
  829. if (lseek(fd, xph_offset, SEEK_SET)
  830. == (off_t)-1) {
  831. file_badseek(ms);
  832. return -1;
  833. }
  834. bufsize = read(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ?
  835. xph_filesz : sizeof(nbuf)));
  836. if (bufsize == -1) {
  837. file_badread(ms);
  838. return -1;
  839. }
  840. offset = 0;
  841. for (;;) {
  842. if (offset >= (size_t)bufsize)
  843. break;
  844. offset = donote(ms, nbuf, offset,
  845. (size_t)bufsize, class, swap, align,
  846. flags);
  847. if (offset == 0)
  848. break;
  849. }
  850. if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
  851. file_badseek(ms);
  852. return -1;
  853. }
  854. break;
  855. default:
  856. break;
  857. }
  858. }
  859. if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
  860. == -1)
  861. return -1;
  862. return 0;
  863. }
  864. protected int
  865. file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
  866. size_t nbytes)
  867. {
  868. union {
  869. int32_t l;
  870. char c[sizeof (int32_t)];
  871. } u;
  872. int class;
  873. int swap;
  874. struct stat st;
  875. off_t fsize;
  876. int flags = 0;
  877. Elf32_Ehdr elf32hdr;
  878. Elf64_Ehdr elf64hdr;
  879. uint16_t type;
  880. if (ms->flags & MAGIC_MIME)
  881. return 0;
  882. /*
  883. * ELF executables have multiple section headers in arbitrary
  884. * file locations and thus file(1) cannot determine it from easily.
  885. * Instead we traverse thru all section headers until a symbol table
  886. * one is found or else the binary is stripped.
  887. * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
  888. */
  889. if (buf[EI_MAG0] != ELFMAG0
  890. || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
  891. || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
  892. return 0;
  893. /*
  894. * If we cannot seek, it must be a pipe, socket or fifo.
  895. */
  896. if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
  897. fd = file_pipe2file(ms, fd, buf, nbytes);
  898. if (fstat(fd, &st) == -1) {
  899. file_badread(ms);
  900. return -1;
  901. }
  902. fsize = st.st_size;
  903. class = buf[EI_CLASS];
  904. switch (class) {
  905. case ELFCLASS32:
  906. #undef elf_getu
  907. #define elf_getu(a, b) elf_getu32(a, b)
  908. #undef elfhdr
  909. #define elfhdr elf32hdr
  910. #include "elfclass.h"
  911. case ELFCLASS64:
  912. #undef elf_getu
  913. #define elf_getu(a, b) elf_getu64(a, b)
  914. #undef elfhdr
  915. #define elfhdr elf64hdr
  916. #include "elfclass.h"
  917. default:
  918. if (file_printf(ms, ", unknown class %d", class) == -1)
  919. return -1;
  920. break;
  921. }
  922. return 0;
  923. }
  924. #endif