File libelf-always-set-elf-maxsize-when-reading.patch of Package elfutils.13244
From: Mark Wielaard <mark at klomp dot org>
Subject: [PATCH] libelf: Always set ELF maxsize when reading an ELF file for
sanity checks
Date: Wed, 26 Oct 2016 06:17:00
Reference: bnc#1030472
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
elfutils packaging edits:
- changelog hunks removed from original patch to avoid conflict
- extracted from mailing list web interface, thus missing header information
- backported to 0.158
Signed-off-by: Mark Wielaard <mjw(a)redhat.com>
---
libelf/elf_begin.c | 37 +++++++++++++++++++++----------------
2 files changed, 27 insertions(+), 18 deletions(-)
diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index 8fdb376..5e9099c 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -596,22 +596,30 @@
use_mmap = 0;
#endif
- if (use_mmap)
+ if (parent == NULL)
{
- if (parent == NULL)
+ if (maxsize == ~((size_t) 0))
{
- if (maxsize == ~((size_t) 0))
- {
- /* We don't know in the moment how large the file is.
- Determine it now. */
- struct stat st;
+ /* We don't know in the moment how large the file is.
+ Determine it now. */
+ struct stat st;
- if (fstat (fildes, &st) == 0
- && (sizeof (size_t) >= sizeof (st.st_size)
- || st.st_size <= ~((size_t) 0)))
- maxsize = (size_t) st.st_size;
- }
+ if (fstat (fildes, &st) == 0
+ && (sizeof (size_t) >= sizeof (st.st_size)
+ || st.st_size <= ~((size_t) 0)))
+ maxsize = (size_t) st.st_size;
+ }
+ }
+ else
+ {
+ /* The parent is already loaded. Use it. */
+ assert (maxsize != ~((size_t) 0));
+ }
+ if (use_mmap)
+ {
+ if (parent == NULL)
+ {
/* We try to map the file ourself. */
map_address = mmap (NULL, maxsize, (cmd == ELF_C_READ_MMAP
? PROT_READ
@@ -626,9 +634,6 @@
}
else
{
- /* The parent is already loaded. Use it. */
- assert (maxsize != ~((size_t) 0));
-
map_address = parent->map_address;
}
}
--
1.8.3.1