summaryrefslogtreecommitdiffstats
path: root/libelf/elf_getphdrnum.c
diff options
context:
space:
mode:
authorMark Wielaard <[email protected]>2014-12-31 00:51:45 +0100
committerMark Wielaard <[email protected]>2015-01-15 14:04:28 +0100
commitcc62e378c292daaded19f1fe03681d63b7437ea0 (patch)
treeb61389196a961c575d901b7a41e5f936455ca526 /libelf/elf_getphdrnum.c
parentd973206f716d441634f3b937be9c8c5b8b6250db (diff)
libelf: gelf_getphdr should check phdr index is valid.
elf_getphdrnum does checks the phdrnum makes sense. But gelf_getphdr checked the given index against the "raw" e_phnum or internal __elf_getphdrnum_rdlock result without checking. Extract the checking code into a new internal __elf_getphdrnum_chk_rdlock function and use that. Found by afl-fuzz. Signed-off-by: Mark Wielaard <[email protected]>
Diffstat (limited to 'libelf/elf_getphdrnum.c')
-rw-r--r--libelf/elf_getphdrnum.c46
1 files changed, 26 insertions, 20 deletions
diff --git a/libelf/elf_getphdrnum.c b/libelf/elf_getphdrnum.c
index 63c27fb1..f2fad87a 100644
--- a/libelf/elf_getphdrnum.c
+++ b/libelf/elf_getphdrnum.c
@@ -80,23 +80,11 @@ __elf_getphdrnum_rdlock (elf, dst)
}
int
-elf_getphdrnum (elf, dst)
+__elf_getphdrnum_chk_rdlock (elf, dst)
Elf *elf;
size_t *dst;
{
- int result;
-
- if (elf == NULL)
- return -1;
-
- if (unlikely (elf->kind != ELF_K_ELF))
- {
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
- return -1;
- }
-
- rwlock_rdlock (elf->lock);
- result = __elf_getphdrnum_rdlock (elf, dst);
+ int result = __elf_getphdrnum_rdlock (elf, dst);
/* Do some sanity checking to make sure phnum and phoff are consistent. */
Elf64_Off off = (elf->class == ELFCLASS32
@@ -105,14 +93,13 @@ elf_getphdrnum (elf, dst)
if (unlikely (off == 0))
{
*dst = 0;
- goto out;
+ return result;
}
if (unlikely (off >= elf->maximum_size))
{
__libelf_seterrno (ELF_E_INVALID_DATA);
- result = -1;
- goto out;
+ return -1;
}
/* Check for too many sections. */
@@ -121,15 +108,34 @@ elf_getphdrnum (elf, dst)
if (unlikely (*dst > SIZE_MAX / phdr_size))
{
__libelf_seterrno (ELF_E_INVALID_DATA);
- result = -1;
- goto out;
+ return -1;
}
/* Truncated file? Don't return more than can be indexed. */
if (unlikely (elf->maximum_size - off < *dst * phdr_size))
*dst = (elf->maximum_size - off) / phdr_size;
-out:
+ return result;
+}
+
+int
+elf_getphdrnum (elf, dst)
+ Elf *elf;
+ size_t *dst;
+{
+ int result;
+
+ if (elf == NULL)
+ return -1;
+
+ if (unlikely (elf->kind != ELF_K_ELF))
+ {
+ __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ return -1;
+ }
+
+ rwlock_rdlock (elf->lock);
+ result = __elf_getphdrnum_chk_rdlock (elf, dst);
rwlock_unlock (elf->lock);
return result;