+2014-12-30 Mark Wielaard <mjw@redhat.com>
+
+ * elf_getphdrnum.c (__elf_getphdrnum_chk_rdlock): New function.
+ (elf_getphdrnum): Call __elf_getphdrnum_chk_rdlock.
+ * gelf_getphdr (gelf_getphdr): Call __elf_getphdrnum_chk_rdlock
+ and always check ndx against phnum.
+ * libelfP.h (__elf_getphdrnum_chk_rdlock): New internal function.
+
2014-12-25 Mark Wielaard <mjw@redhat.com>
* elf_begin.c (__libelf_next_arhdr_wrlock): ar_size cannot be
}
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
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. */
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;
/* Test whether the index is ok. */
size_t phnum;
- if (ndx >= elf->state.elf32.ehdr->e_phnum
- && (elf->state.elf32.ehdr->e_phnum != PN_XNUM
- || __elf_getphdrnum_rdlock (elf, &phnum) != 0
- || (size_t) ndx >= phnum))
+ if (__elf_getphdrnum_chk_rdlock (elf, &phnum) != 0
+ || (size_t) ndx >= phnum)
{
__libelf_seterrno (ELF_E_INVALID_INDEX);
goto out;
/* Test whether the index is ok. */
size_t phnum;
- if (ndx >= elf->state.elf64.ehdr->e_phnum
- && (elf->state.elf64.ehdr->e_phnum != PN_XNUM
- || __elf_getphdrnum_rdlock (elf, &phnum) != 0
- || (size_t) ndx >= phnum))
+ if (__elf_getphdrnum_chk_rdlock (elf, &phnum) != 0
+ || (size_t) ndx >= phnum)
{
__libelf_seterrno (ELF_E_INVALID_INDEX);
goto out;
attribute_hidden;
extern int __elf_getphdrnum_rdlock (Elf *__elf, size_t *__dst)
internal_function;
+extern int __elf_getphdrnum_chk_rdlock (Elf *__elf, size_t *__dst)
+ internal_function;
extern int __elf_getshdrnum_rdlock (Elf *__elf, size_t *__dst)
internal_function;
extern int __elf_getshdrstrndx_internal (Elf *__elf, size_t *__dst)