60 Commits

Author SHA1 Message Date
lizhenlin efba194008 update version to 0.193
Signed-off-by: lizhenlin <lizhenlin2@h-partners.com>
2025-11-12 14:48:40 +08:00
zhanghaibo e660ab2124 upgrade to v0.188
Signed-off-by: zhanghaibo <zhanghaibo0@huawei.com>
Change-Id: I83c8756f5ce0a464b037bc963cfdce43df82921b
2023-04-25 18:45:37 +08:00
Mark Wielaard d21692c7eb libelf: Make sure we have at least a full ELF header available.
When elf_memory is called we could get a slightly too small image
that doesn't contain a full ELF header (but does contain at least
the e_ident values). Require the full header before even validating
the rest of the ELF header fields.

https://sourceware.org/bugzilla/show_bug.cgi?id=27076

Signed-off-by: Mark Wielaard <mark@klomp.org>
2020-12-16 11:01:52 +01:00
Dmitry V. Levin d178e3765d libelf: fix spelling typos in comments
Auxialiary -> Auxiliary
constucted -> constructed
desriptor -> descriptor
endianess -> endianness
responsibilty -> responsibility
sufficent -> sufficient

elf.h comes from glibc and therefore is excluded.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
2020-12-12 18:06:28 +01:00
Mark Wielaard a46200f630 libelf: Fix some 32bit offset/size issues that break updating 4G+ files.
Some years ago, in commit b1d0b0fc "libelf: Use int64_t for offsets in
libelf.h", we changed the public interface to use 64bit offsets/sizes.
This wasn't really a API change, before we relied on loff_t always
being 64bits on all platforms.

We didn't change the implementation to use the int64_t type though.
That was a little confusing, since the function definitions used a
different type, int64_t, from the function implementations, off_t.
Since we always build with _FILE_OFFSET_BITS=64 this should be fine.
But it was a bit sloppy and confusing.

Worse is that we got the translation of offset/sizes wrong in a
couple of places when translating to ELF types. In various places
we would use Elf32_Word or Elf64_Word. But both are 32bit (unsigned)
types! As is GElf_Word. Elf32_Off is 32bits and Elf64_Off is 64bits.
But we were not using those consistently.

This patch introduces comments for the usage of [G]Elf(32|64)Word in
libelf that are correct. And introduces Elf(32|64)_SizeWord in
elf32_updatenull.c where we want to make a difference between sizes
and offsets (the ELF variants are both unsigned, while int64_t/loff_t
is signed).

It also includes a new run-large-elf-file.sh test that creates a
large ELF files (one 64bit, little endian, rel and another big endian,
non-rel) and runs eu-strip, eu-elflint, eu-unstrip and eu-elfcmp.
Before this patch, that test case fails and creates corrupt ELF files.

The test is guarded by some checks that try to make sure there is
enough disk space and memory available on the machine. The test is
skipped otherwise.

Signed-off-by: Mark Wielaard <mark@klomp.org>
2019-06-28 23:14:27 +02:00
Mark Wielaard b9d1678951 libelf: There is just one ELF version.
Remove (partially defined out) code and data structures dealing with
multiple ELF versions. There hasn't been a new ELF version in the
last 20 years. Simplify the code a bit by just assuming there will
only be one version (EV_CURRENT == 1).

Simplifies elf_version, gets rid of __libelf_version_initialized.
Removes one (or more) array (version) dimension from various tables
and accessor functions (__elf_xfctstom, shtype_map, __libelf_data_type,
__libelf_type_aligns and __libelf_type_sizes).

Signed-off-by: Mark Wielaard <mark@klomp.org>`
2019-02-28 22:41:14 +01:00
Mark Wielaard e32380ecef libelf: Make sure ar_size is terminated when reading ar long names.
The ar_size is given as a fixed size decimal string, right padded with
spaces.  Make sure we read it properly even if there is no terminating
space. Also sanity check len early if we can.

https://sourceware.org/bugzilla/show_bug.cgi?id=24085

Signed-off-by: Mark Wielaard <mark@klomp.org>
2019-02-14 11:47:59 +01:00
Joshua Watt 555e15ebe8 Use fallthrough attribute.
Use __attribute__ ((fallthrough)) to indicate switch case fall through
instead of a comment. This ensures that the fallthrough warning is not
triggered even if the file is pre-processed (hence stripping the
comments) before it is compiled.

The actual fallback implementation is hidden behind a FALLBACK macro in
case the compiler doesn't support it.

Finally, the -Wimplict-fallthrough warning was upgraded to only allow
the attribute to satisfy it; a comment alone is no longer sufficient.

Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
2018-02-10 03:19:26 +01:00
Mark Wielaard 4f977868ea libelf: Don't error out when sanity checking e_shoff if scncnt is zero.
We won't use the e_shoff value in that case because we will set
elf->state.elf[64|32].scns.cnt to zero to indicate not to read
any section header data from the file.

Signed-off-by: Mark Wielaard <mark@klomp.org>
2017-10-13 16:22:06 +02:00
Mark Wielaard fef9e11b30 libelf: Add ELF_E_INVALID_ELF error value.
Add ELF_E_INVALID_ELF which is set when the ELF file data is bad.
This is different from ELF_E_INVALID_FILE which is set when the file
could not be read.

Signed-off-by: Mark Wielaard <mark@klomp.org>
2017-10-13 16:22:06 +02:00
Ulf Hermann fd9e7345b4 Use F_GETFD rather than F_GETFL to check validity of file descriptor
F_GETFD is both cheaper and easier to port, and otherwise has the same
effect here.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
2017-05-02 13:18:40 +02:00
Mark Wielaard 191000fded libelf: Always set ELF maxsize when reading an ELF file for sanity checks.
There are various sanity checks that depend on knowing the file size
of the underlying ELF file which we only used when mmapping the ELF file.
Although we probably won't crash if we use pread to try to read from
the file, we still might return completely bogus data structures. This
could cause us to malloc insane amounts of memory.

Always try to get the maxsize when unknown in elf_begin.c (read_file).

https://bugzilla.redhat.com/show_bug.cgi?id=1388057

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-11-10 12:11:00 +01:00
Akihiko Odaki 60b2bf1b08 lib: Provide MAX and MIN in system.h
This change also creates a new header file libeu.h to provide the
prototypes for the function of libeu. That hides the definition of function
crc32, which can conflict with zlib, from libelf. It also prevents mistakes
to refer those functions from a component which doesn't link with libeu,
such as libelf.

Signed-off-by: Akihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
2016-10-12 15:43:14 +02:00
Mark Wielaard 272018bba1 libelf: Add elf_compress and elf_compress_gnu.
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-01-06 14:27:10 +01:00
Jose E. Marchesi 1ed7195ea8 libelf: use the right size when preading in[0].sh_size
Signed-off-by: Jose E. Marchesi <jose.marchesi@oracle.com>
2015-10-22 23:09:56 +02:00
Chih-Hung Hsieh 239d859169 Move nested functions in libelf.
* Move nested functions to file scope
  in libelf/elf_begin.c and elf32_updatefile.c
  to compile with clang.

Signed-off-by: Chih-Hung Hsieh <chh@google.com>
2015-10-22 22:43:42 +02:00
Mark Wielaard 1ccdfb683a Remove old-style function definitions.
We already require -std=gnu99 and old-style function definitions might
hide some compiler warnings.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2015-09-23 15:50:55 +02:00
Mark Wielaard 560aa98b9a libelf: Check e_shoff alignment before direct access of Elf(32|64)_Shdr.
In get_shnum the check was whether the Elf(32|64)_Ehdr was correctly
aligned, but to access the Shdr directly we need to check whether the
address that points to the Elf(32|64)_Shdr structure is correctly aligned.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2015-06-08 11:12:38 +02:00
Mark Wielaard b9fd0eee50 libelf: Split checks for ehdr and shdr, drop phdr check in file_read_elf.
There are various places in the code that check whether mmapped structures
are correctly aligned (or ALLOW_UNALIGNED is set). Some of these checks
are asserts. Like the one in elf(32|64)_getshdr. We should not get into
that part of the code if the shdr scn structure was cached in elf_begin
because it was mmapped in and properly aligned.

These asserts could trigger because in elf_begin.c file_read_elf ()
all alignment checks were combined. So even though only one of the ehdr,
shdr or phdr structures were not properly aligned all structures would be
copied. Also the phdr structure was not even read in elf_begin, so the
alignment check was unnecessary.

This patch splits the alignment checks and reading of ehdr and shdr
structures into separate code paths. It also drops the phdr alignment
checks in elf_begin. Those phdr checks are done in elf(32|64)_getphdr
already.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2015-06-08 11:12:38 +02:00
Mark Wielaard 68588d628a libelf: ar_size cannot be negative. Fix max ar size.
Elf_Arhdr ar_size is loff_t, which is signed. Make sure it isn't negative.
When the parent start_offset is non-zero maxsize should include it to
compensate for ar offset.

Found with afl-fuzz.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2015-01-12 22:02:46 +01:00
Alexander Cherepanov 147018e729 libelf: Fix dir traversal vuln in ar extraction.
read_long_names terminates names at the first '/' found but then skips
one character without checking (it's supposed to be '\n'). Hence the
next name could start with any character including '/'. This leads to
a directory traversal vulnerability at the time the contents of the
archive is extracted.

The danger is mitigated by the fact that only one '/' is possible in a
resulting filename and only in the leading position. Hence only files
in the root directory can be written via this vuln and only when ar is
executed as root.

The fix for the vuln is to not skip any characters while looking
for '/'.

Signed-off-by: Alexander Cherepanov <cherepan@mccme.ru>
2014-12-29 00:57:17 +01:00
Mark Wielaard 55f847d63a libelf: Make sure ar archive long_names len fits in mapped ELF file.
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2014-12-17 16:49:03 +01:00
Mark Wielaard 9520242b2a libelf: Clear any garbage left in the ar name table.
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2014-12-17 16:43:27 +01:00
Mark Wielaard 65a556fc86 libelf: Correct ELF64 section offset check in file_read_elf.
The ELF64 case didn't check for overflow and accidentially used the 32bit
Shdr size.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2014-12-15 10:04:23 +01:00
Mark Wielaard 7d63628b8a libelf: Add some ar header sanity checking.
Don't allow entries or size to overflow the parent file size.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2014-12-15 10:03:03 +01:00
Mark Wielaard babd5b6d9e libelf: Use mempcpy not __mempcpy.
We were using mempcpy everywhere else, only __libelf_next_arhdr_wrlock used
__mempcpy.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2014-11-13 15:37:40 +01:00
Mark Wielaard b635b8dcb0 libelf: Correct shdr size check for (raw) getdata.
Reported-by: Hanno Böck <hanno@hboeck.de>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2014-11-08 14:47:16 +01:00
Petr Machata 237188e4d7 elf_begin sets elf_errno for cases that the file is completely unreadable
- The problem this is trying to solve can be seen for example thus:
	$ eu-readelf -a /
	eu-readelf: failed reading '/': (null)

  With the fix we see a proper error number and get a reasonable error
  message:

	$ eu-readelf -a /
	eu-readelf: failed reading '/': invalid file descriptor

Signed-off-by: Petr Machata <pmachata@redhat.com>
2014-09-11 19:45:11 +02:00
Mark Wielaard 3232479483 Remove mudflap build option.
The --enable-mudflap configure build has been broken for 2 years without
anybody apparently noticing. GCC 4.9 removed mudflap support. Before
release we now run make distcheck with valgrind support. Removal of the
mudflap configure option simplifies the build a little.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2014-04-18 11:46:51 +02:00
Jakub Jelinek 720383c53b robustify: libelf.
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2014-01-23 11:31:53 +01:00
Petr Machata e1873141a5 Implement support for archives with 64-bit symbol table
Signed-off-by: Petr Machata <pmachata@redhat.com>
2012-08-07 14:28:57 +02:00
Mark Wielaard de2ed97f33 Update name, license and contributor policy.
* Change name from "Red Hat elfutils" to "elfutils".
* Update license of standalone tools and test from GPLv2 to GPLv3+.
* Change license of libraries from GPLv2+exception to GPLv2/LGPLv3+.
* Add Developer Certificate of Origin based contributor policy.

top-level:

- COPYING: Upgraded from GPLv2 to GPLv3.
- CONTRIBUTING, COPYING-GPLv2, COPYING-LGPLv3: New files.
- NEWS: Added note about new contribution and license policy.
- Makefile.am: Updated to GPLv3, added new files to EXTRA_DIST.
- configure.ac: Update to GPLv3, changed AC_INIT name to 'elfutils'.

backends, lib, libasm, libcpu, libdw, libdwfl, libebl, libelf:

- All files updated to GPLv2/LGPLv3+. Except some very small files
  (<5 lines) which didn't have any headers at all before, the linker
  .maps files and the libcpu/defs files which only contain data and
  libelf/elf.h which comes from glibc and is under LGPLv2+.

config:

- elfutils.spec.in: Add new License: headers and new %doc files.
- Update all license headers to GPLv2/LGPLv3+ for files used by libs.

src, tests:

- All files updated to GPLv3+. Except for the test bz2 data files, the
  linker maps and script files and some very small files (<5 lines)
  that don't have any headers.

Signed-off-by: Richard Fontana <rfontana@redhat.com>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2012-06-05 23:12:05 +02:00
Roland McGrath 957072c18a Handle truncated phdrs without crash. 2010-04-14 11:24:15 -07:00
Roland McGrath 429502fd32 Avoid wild section data pointers from bogus sh_offset in mapped files. 2010-02-17 01:45:32 -08:00
Roland McGrath 6fd3cd104a Add elf_getphdrnum, support >65536 phdrs. 2010-01-07 19:43:42 -08:00
Ulrich Drepper 02f6645263 propagate from branch 'com.redhat.elfutils.roland.pending.libelf-threads' (head fc97c9c202b5d7d7699a3b1d5c53007a2ef37bb1)
to branch 'com.redhat.elfutils' (head 67cccb9bfffc1a7fe3d8d355a2d9b6d0e489ff81)
2008-12-04 05:58:16 +00:00
Roland McGrath 9cf28e4f89 libdwfl/
2008-09-29  Roland McGrath  <roland@redhat.com>

	* segment.c (insert): Must realloc DWFL->lookup_module here too.
	(dwfl_report_segment): Clear DWFL->lookup_module before insert calls.
2008-09-30 06:35:35 +00:00
Roland McGrath b4d6f0f806 Fix up bogon and missing log entries from .pmachata.threads branch. 2008-08-25 22:55:17 +00:00
Ulrich Drepper d56e232fb8 propagate from branch 'com.redhat.elfutils.pmachata.threads' (head 8bd3bc10eb015c96f7bafcc6a22c973620b57dd8)
to branch 'com.redhat.elfutils' (head c5a11b6b3329382f1b5ffd0020f0d93c64176f20)
2008-08-16 03:09:13 +00:00
Roland McGrath 059c83e5db Finish elflint .gnu.attributes checking 2008-02-21 06:19:39 +00:00
Ulrich Drepper 3cbdd387c7 propagate from branch 'com.redhat.elfutils.disasm' (head d15b4eb794e81e477f9896fe82a74cb5ecf4514c)
to branch 'com.redhat.elfutils' (head eaacbf01f8cc89d043ec6eca9b5e35cb5c4cde06)
2008-01-02 17:44:39 +00:00
Ulrich Drepper b597dfad92 merge of '92c36bfdbc6468d1711c043b530e0dfe5abb6dec'
and 'c22c8c43f8f68b0bffd4d5ccdb2282c958268742'
2007-10-16 05:21:27 +00:00
Ulrich Drepper c07fbb3ff7 propagate from branch 'com.redhat.elfutils.roland.pending' (head b584b7056d679db0fc272b47667047d07737ca55)
to branch 'com.redhat.elfutils' (head 5f150a0b2f07e8c60913d4e6ad833ef026ccd26e)
2007-03-30 19:14:59 +00:00
Ulrich Drepper ce0bdb6ee5 Implement ar program.
Relax elflint in GNU ld mode for latest idiosyncracies.
2007-02-05 07:13:52 +00:00
Ulrich Drepper 077c65f3be merge of 67f3844b544f03a64b3ac896c19f94744923d76a
and ba017f6b59b4ad649cf32f4eefdfecca8b31332c
2006-07-12 19:54:51 +00:00
Ulrich Drepper cc41562f94 propagate from branch 'com.redhat.elfutils.roland.pending' (head e5cfdd13aa39dfae16b905fd57ff56ad8a131bb5)
to branch 'com.redhat.elfutils' (head 60222fc1981540f15e674b6705fd7a6f628f95a4)
2006-07-12 19:42:23 +00:00
Ulrich Drepper 7f7f1bc0e2 Adjust for internal_function_def removal. 2006-07-12 19:53:08 +00:00
Ulrich Drepper 1e9ef50681 Fix FSF address. No exception for libdwarf. 2006-04-04 22:29:06 +00:00
Ulrich Drepper 361df7da6d propagate from branch 'com.redhat.elfutils.roland.pending' (head 4f8fc821345feef58624f0aa5b470d4827577d8c)
to branch 'com.redhat.elfutils' (head 76e26cb54695fd3b21ee8fb5be3036bd68200633)
2006-04-04 21:38:57 +00:00
Ulrich Drepper fbe998a0b1 merge of 333c187506c852455e9f7be44fa9adc360416217
and 79955b942e3f0ddc71117feea5754df61edcc42a
2005-08-29 16:27:10 +00:00