Make the argument checking of the gelf_get and gelf_update accessor
functions consistent.
Like the elf functions, make sure all gelf accessor functions return
failure if the first argument is NULL without setting the libelf
errno. That assumes a NULL first argument means creation of that
handle failed and the user is interested in the error that was set for
that failure. But other errors should explicitly set the libelf
errno. Most functions already worked that way. Update gelf_getclass
and gelf_update_shdr.
For other NULL arguments, which are documented to not be NULL return
an error with ELF_E_INVALID_OPERAND, instead of crashing.
Make sure that functions that deal with Elf_Data check the d_type and
always set the same, ELF_E_DATA_MISMATCH, error. This was inconsistent
between functions. And make sure that d_buf is not NULL (unless d_size
is zero). Some already did, most didn't.
All functions that take an index should use the INVALID_NDX macro. All
did, except gelf_update_symshndx. The functions that take an offset,
gelf_{get,update_}{verneed,vernaux,verdef,verdaux} should make sure
the offset is currectly aligned and the structure fits the data buffer
(the getters already did, the updates were missing the checks).
* doc/gelf_getclass.3: Document ELFCLASSNONE is zero. And if
Elf is NULL elf_errno is not set.
* doc/gelf_update_syminfo.3: Remove duplicate sentence.
* libelf/gelf_getclass.c: Check elf is not NULL separate from
elf->kind.
* libelf/gelf_getehdr.c (gelf_ehdr): Check dest is not NULL.
* libelf/gelf_getnote.c (gelf_getnote): Set
ELF_E_DATA_MISMATCH if d_type is incorrect. Check d_buf and
d_size. Check result, offset and desc_offset are not NULL.
* libelf/gelf_getauxv.c (gelf_getauxv): Check dst is not
NULL. Set ELF_E_DATA_MISMATCH if d_type is incorrect. Check
d_buf and d_size.
* libelf/gelf_getdyn.c (gelf_getdyn): Likewise.
* libelf/gelf_getlib.c (gelf_getlib): Likewise.
* libelf/gelf_getmove.c (gelf_move): Likewise.
* libelf/gelf_getrel.c (gelf_getrel): Likewise.
* libelf/gelf_getrela.c (gelf_getrela): Likewise.
* libelf/gelf_getsym.c (gelf_getsym): Likewise.
* libelf/gelf_getsyminfo.c (gelf_getsyminfo): Likewise.
* libelf/gelf_getsymshndx.c (gelf_getsymshndx): Likewise.
* libelf/gelf_getverdaux.c (gelf_getverdaux): Likewise.
* libelf/gelf_getverdef.c (gelf_getverdef): Likewise.
* libelf/gelf_getvernaux.c (gelf_getvernaux): Likewise.
* libelf/gelf_getverneed.c (gelf_getverneed): Likewise.
* libelf/gelf_getversym.c (getversym): Likewise.
* libelf/gelf_update_ehdr.c (gelf_update_ehdr): Check src is
not NULL.
* libelf/gelf_update_phdr.c (gelf_update_phdr): Likewise.
* libelf/gelf_update_shdr.c (gelf_update_shdr): Separate scn
and src checks.
* libelf/gelf_update_auxv.c (gelf_update_auxv): Check src is
not NULL. Check d_buf and d_size.
* libelf/gelf_update_dyn.c (gelf_update_dyn): Likewise.
* libelf/gelf_update_lib.c (gelf_update_lib): Likewise.
* libelf/gelf_update_move.c (gelf_upate_move): Likewise.
* libelf/gelf_update_rel.c (gelf_update_rel): Likewise.
* libelf/gelf_update_rela.c (gelf_update_rela): Likewise.
* libelf/gelf_update_sym.c (gelf_update_sym): Likewise.
* libelf/gelf_update_syminfo.c (gelf_update_syminfo): Likewise.
* libelf/gelf_update_symshndx.c (gelf_update_symshndx): Check
src is not NULL. Also check d_type from shndxdata_scn if not
NULL. Check symdata and shndxsdata d_buf and d_size. Use
INVALID_NDX macro for ndx check.
* libelf/gelf_update_verdaux.c (gelf_update_verdaux): Check
src is not NULL. Make sure offset is OK. Check d_buf and
d_size.
* libelf/gelf_update_verdef.c (gelf_update_verdef): Likewise.
* libelf/gelf_update_vernaux.c (gelf_update_vernaux): Likewise.
* libelf/gelf_update_verneed.c (gelf_update_verneed): Likewise.
* libelf/gelf_update_versym.c (gelf_update_versym): Check src
is not NULL. Check d_buf and d_size.
Signed-off-by: Mark Wielaard <[email protected]>
---
doc/gelf_getclass.3 | 10 ++++++----
doc/gelf_update_syminfo.3 | 1 -
libelf/gelf_getauxv.c | 14 +++++++++++++-
libelf/gelf_getclass.c | 11 ++++++++++-
libelf/gelf_getdyn.c | 14 +++++++++++++-
libelf/gelf_getehdr.c | 6 ++++++
libelf/gelf_getlib.c | 14 +++++++++++++-
libelf/gelf_getmove.c | 14 +++++++++++++-
libelf/gelf_getnote.c | 14 +++++++++++++-
libelf/gelf_getrel.c | 14 +++++++++++++-
libelf/gelf_getrela.c | 14 +++++++++++++-
libelf/gelf_getsym.c | 14 +++++++++++++-
libelf/gelf_getsyminfo.c | 14 +++++++++++++-
libelf/gelf_getsymshndx.c | 16 +++++++++++++++-
libelf/gelf_getverdaux.c | 14 +++++++++++++-
libelf/gelf_getverdef.c | 14 +++++++++++++-
libelf/gelf_getvernaux.c | 14 +++++++++++++-
libelf/gelf_getverneed.c | 14 +++++++++++++-
libelf/gelf_getversym.c | 14 +++++++++++++-
libelf/gelf_update_auxv.c | 12 ++++++++++++
libelf/gelf_update_dyn.c | 12 ++++++++++++
libelf/gelf_update_ehdr.c | 6 ++++++
libelf/gelf_update_lib.c | 12 ++++++++++++
libelf/gelf_update_move.c | 12 ++++++++++++
libelf/gelf_update_phdr.c | 6 ++++++
libelf/gelf_update_rel.c | 12 ++++++++++++
libelf/gelf_update_rela.c | 12 ++++++++++++
libelf/gelf_update_shdr.c | 8 +++++++-
libelf/gelf_update_sym.c | 12 ++++++++++++
libelf/gelf_update_syminfo.c | 12 ++++++++++++
libelf/gelf_update_symshndx.c | 21 +++++++++++++++++++--
libelf/gelf_update_verdaux.c | 19 ++++++++++++++++---
libelf/gelf_update_verdef.c | 19 ++++++++++++++++---
libelf/gelf_update_vernaux.c | 19 ++++++++++++++++---
libelf/gelf_update_verneed.c | 19 ++++++++++++++++---
libelf/gelf_update_versym.c | 12 ++++++++++++
36 files changed, 429 insertions(+), 36 deletions(-)
diff --git a/doc/gelf_getclass.3 b/doc/gelf_getclass.3
index 25d517db9f12..708fe3d9ad26 100644
--- a/doc/gelf_getclass.3
+++ b/doc/gelf_getclass.3
@@ -24,7 +24,7 @@ The object is a 64\-bit ELF class.
If
.I elf
does not denote a valid ELF object, or the class cannot be determined,
-.B ELFCLASSNONE
+.B ELFCLASSNONE (0)
is returned.
.SH RETURN VALUE
@@ -35,11 +35,13 @@ or
on success.
If
.I elf
-is NULL or does not refer to a valid ELF binary with kind
+does not refer to a valid ELF binary with kind
.BR ELF_K_ELF ,
then
-.B ELFCLASSNONE
-is returned.
+.B ELFCLASSNONE (0)
+is returned. If
+.I elf
+is NULL, then zero is returned without setting elf_errno.
.SH SEE ALSO
.BR elf_kind (3),
diff --git a/doc/gelf_update_syminfo.3 b/doc/gelf_update_syminfo.3
index c02dcd586e3c..6950b60e2152 100644
--- a/doc/gelf_update_syminfo.3
+++ b/doc/gelf_update_syminfo.3
@@ -32,7 +32,6 @@ ELF files.
.I data
Pointer to an
.B Elf_Data
-associated with an ELF section with type
for a section with type
.B SHT_SUNW_syminfo
and data type
diff --git a/libelf/gelf_getauxv.c b/libelf/gelf_getauxv.c
index d3a178b5fc46..4891a24dc46d 100644
--- a/libelf/gelf_getauxv.c
+++ b/libelf/gelf_getauxv.c
@@ -46,9 +46,21 @@ gelf_getauxv (Elf_Data *data, int ndx, GElf_auxv_t *dst)
if (data_scn == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (data_scn->d.d_type != ELF_T_AUXV))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_getclass.c b/libelf/gelf_getclass.c
index 7d0924bdc860..d344c171281a 100644
--- a/libelf/gelf_getclass.c
+++ b/libelf/gelf_getclass.c
@@ -40,5 +40,14 @@
int
gelf_getclass (Elf *elf)
{
- return elf == NULL || elf->kind != ELF_K_ELF ? ELFCLASSNONE : elf->class;
+ if (elf == NULL)
+ return ELFCLASSNONE;
+
+ if (elf->kind != ELF_K_ELF)
+ {
+ __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ return ELFCLASSNONE;
+ }
+
+ return elf->class;
}
diff --git a/libelf/gelf_getdyn.c b/libelf/gelf_getdyn.c
index 32e75f9a888e..e8af333b606a 100644
--- a/libelf/gelf_getdyn.c
+++ b/libelf/gelf_getdyn.c
@@ -47,9 +47,21 @@ gelf_getdyn (Elf_Data *data, int ndx, GElf_Dyn *dst)
if (data_scn == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (data_scn->d.d_type != ELF_T_DYN))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_getehdr.c b/libelf/gelf_getehdr.c
index abeb70c4a59b..c610a26d45bd 100644
--- a/libelf/gelf_getehdr.c
+++ b/libelf/gelf_getehdr.c
@@ -100,6 +100,12 @@ gelf_getehdr (Elf *elf, GElf_Ehdr *dest)
if (elf == NULL)
return NULL;
+ if (dest == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
rwlock_rdlock (elf->lock);
result = __gelf_getehdr_rdlock (elf, dest);
rwlock_unlock (elf->lock);
diff --git a/libelf/gelf_getlib.c b/libelf/gelf_getlib.c
index 9a6c24165ece..d32afff5712c 100644
--- a/libelf/gelf_getlib.c
+++ b/libelf/gelf_getlib.c
@@ -43,9 +43,21 @@ gelf_getlib (Elf_Data *data, int ndx, GElf_Lib *dst)
if (data == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (data->d_type != ELF_T_LIB))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (data->d_buf == NULL && data->d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_getmove.c b/libelf/gelf_getmove.c
index 20fb54d5cd0e..88b973c4ce6c 100644
--- a/libelf/gelf_getmove.c
+++ b/libelf/gelf_getmove.c
@@ -47,9 +47,21 @@ gelf_getmove (Elf_Data *data, int ndx, GElf_Move *dst)
if (data == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (data->d_type != ELF_T_MOVE))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (data->d_buf == NULL && data->d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_getnote.c b/libelf/gelf_getnote.c
index 2cf28565e0cd..24d9ddf7c3c4 100644
--- a/libelf/gelf_getnote.c
+++ b/libelf/gelf_getnote.c
@@ -45,7 +45,19 @@ gelf_getnote (Elf_Data *data, size_t offset, GElf_Nhdr
*result,
if (unlikely (data->d_type != ELF_T_NHDR && data->d_type != ELF_T_NHDR8))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return 0;
+ }
+
+ if (unlikely (data->d_buf == NULL && data->d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
+ if (result == NULL || name_offset == NULL || desc_offset == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
return 0;
}
diff --git a/libelf/gelf_getrel.c b/libelf/gelf_getrel.c
index 309e3d37457b..8369f18cb4d2 100644
--- a/libelf/gelf_getrel.c
+++ b/libelf/gelf_getrel.c
@@ -47,9 +47,21 @@ gelf_getrel (Elf_Data *data, int ndx, GElf_Rel *dst)
if (data_scn == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (data_scn->d.d_type != ELF_T_REL))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_getrela.c b/libelf/gelf_getrela.c
index d695f6593dc4..c4d281b0d1f2 100644
--- a/libelf/gelf_getrela.c
+++ b/libelf/gelf_getrela.c
@@ -47,9 +47,21 @@ gelf_getrela (Elf_Data *data, int ndx, GElf_Rela *dst)
if (data_scn == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (data_scn->d.d_type != ELF_T_RELA))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_getsym.c b/libelf/gelf_getsym.c
index bc1e7b56bdc5..1a1c670d1cb6 100644
--- a/libelf/gelf_getsym.c
+++ b/libelf/gelf_getsym.c
@@ -46,9 +46,21 @@ gelf_getsym (Elf_Data *data, int ndx, GElf_Sym *dst)
if (data == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (data->d_type != ELF_T_SYM))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (data->d_buf == NULL && data->d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_getsyminfo.c b/libelf/gelf_getsyminfo.c
index d1ec9e7942c9..1ed5f90771ca 100644
--- a/libelf/gelf_getsyminfo.c
+++ b/libelf/gelf_getsyminfo.c
@@ -45,9 +45,21 @@ gelf_getsyminfo (Elf_Data *data, int ndx, GElf_Syminfo *dst)
if (data == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (data->d_type != ELF_T_SYMINFO))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (data->d_buf == NULL && data->d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_getsymshndx.c b/libelf/gelf_getsymshndx.c
index 1be35360eeda..67e3ce1c9d6e 100644
--- a/libelf/gelf_getsymshndx.c
+++ b/libelf/gelf_getsymshndx.c
@@ -50,11 +50,25 @@ gelf_getsymshndx (Elf_Data *symdata, Elf_Data *shndxdata,
int ndx,
if (symdata == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (symdata->d_type != ELF_T_SYM)
|| (likely (shndxdata_scn != NULL)
&& unlikely (shndxdata->d_type != ELF_T_WORD)))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (symdata->d_buf == NULL && symdata->d_size != 0)
+ || (likely (shndxdata_scn != NULL)
+ && unlikely (shndxdata->d_buf == NULL && shndxdata->d_size != 0)))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_getverdaux.c b/libelf/gelf_getverdaux.c
index 099c6612f5ad..f5f8600fc862 100644
--- a/libelf/gelf_getverdaux.c
+++ b/libelf/gelf_getverdaux.c
@@ -45,9 +45,21 @@ gelf_getverdaux (Elf_Data *data, int offset, GElf_Verdaux
*dst)
if (data == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (data->d_type != ELF_T_VDEF))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (data->d_buf == NULL && data->d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_getverdef.c b/libelf/gelf_getverdef.c
index 85be7caf4fa3..5dde3ff20762 100644
--- a/libelf/gelf_getverdef.c
+++ b/libelf/gelf_getverdef.c
@@ -45,9 +45,21 @@ gelf_getverdef (Elf_Data *data, int offset, GElf_Verdef *dst)
if (data == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (data->d_type != ELF_T_VDEF))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (data->d_buf == NULL && data->d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_getvernaux.c b/libelf/gelf_getvernaux.c
index 234543367606..3da875b41e75 100644
--- a/libelf/gelf_getvernaux.c
+++ b/libelf/gelf_getvernaux.c
@@ -45,9 +45,21 @@ gelf_getvernaux (Elf_Data *data, int offset, GElf_Vernaux
*dst)
if (data == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (data->d_type != ELF_T_VNEED))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (data->d_buf == NULL && data->d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_getverneed.c b/libelf/gelf_getverneed.c
index ff3743ea3451..8c8cf0a19378 100644
--- a/libelf/gelf_getverneed.c
+++ b/libelf/gelf_getverneed.c
@@ -45,9 +45,21 @@ gelf_getverneed (Elf_Data *data, int offset, GElf_Verneed
*dst)
if (data == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (data->d_type != ELF_T_VNEED))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (data->d_buf == NULL && data->d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_getversym.c b/libelf/gelf_getversym.c
index aa2c0a0904e1..43d354b0d5ee 100644
--- a/libelf/gelf_getversym.c
+++ b/libelf/gelf_getversym.c
@@ -47,9 +47,21 @@ gelf_getversym (Elf_Data *data, int ndx, GElf_Versym *dst)
if (data == NULL)
return NULL;
+ if (dst == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return NULL;
+ }
+
if (unlikely (data->d_type != ELF_T_HALF))
{
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
+ __libelf_seterrno (ELF_E_DATA_MISMATCH);
+ return NULL;
+ }
+
+ if (unlikely (data->d_buf == NULL && data->d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
return NULL;
}
diff --git a/libelf/gelf_update_auxv.c b/libelf/gelf_update_auxv.c
index 1254ef466b36..ac8e26ff6e0c 100644
--- a/libelf/gelf_update_auxv.c
+++ b/libelf/gelf_update_auxv.c
@@ -46,6 +46,12 @@ gelf_update_auxv (Elf_Data *data, int ndx, GElf_auxv_t *src)
if (data == NULL)
return 0;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
if (unlikely (ndx < 0))
{
__libelf_seterrno (ELF_E_INVALID_INDEX);
@@ -59,6 +65,12 @@ gelf_update_auxv (Elf_Data *data, int ndx, GElf_auxv_t *src)
return 0;
}
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
scn = data_scn->s;
rwlock_wrlock (scn->elf->lock);
diff --git a/libelf/gelf_update_dyn.c b/libelf/gelf_update_dyn.c
index 5c515d262660..c2eb84c0db99 100644
--- a/libelf/gelf_update_dyn.c
+++ b/libelf/gelf_update_dyn.c
@@ -47,6 +47,12 @@ gelf_update_dyn (Elf_Data *data, int ndx, GElf_Dyn *src)
if (data == NULL)
return 0;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
if (unlikely (data_scn->d.d_type != ELF_T_DYN))
{
/* The type of the data better should match. */
@@ -54,6 +60,12 @@ gelf_update_dyn (Elf_Data *data, int ndx, GElf_Dyn *src)
return 0;
}
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
scn = data_scn->s;
rwlock_wrlock (scn->elf->lock);
diff --git a/libelf/gelf_update_ehdr.c b/libelf/gelf_update_ehdr.c
index 73d5af7ab221..44ef15ebbf56 100644
--- a/libelf/gelf_update_ehdr.c
+++ b/libelf/gelf_update_ehdr.c
@@ -51,6 +51,12 @@ gelf_update_ehdr (Elf *elf, GElf_Ehdr *src)
return 0;
}
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
rwlock_wrlock (elf->lock);
if (elf->class == ELFCLASS32)
diff --git a/libelf/gelf_update_lib.c b/libelf/gelf_update_lib.c
index d0f235e8ea53..5bfd6a4bd7d7 100644
--- a/libelf/gelf_update_lib.c
+++ b/libelf/gelf_update_lib.c
@@ -44,6 +44,12 @@ gelf_update_lib (Elf_Data *data, int ndx, GElf_Lib *src)
if (data == NULL)
return 0;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
if (unlikely (data_scn->d.d_type != ELF_T_LIB))
{
@@ -52,6 +58,12 @@ gelf_update_lib (Elf_Data *data, int ndx, GElf_Lib *src)
return 0;
}
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
Elf_Scn *scn = data_scn->s;
rwlock_wrlock (scn->elf->lock);
diff --git a/libelf/gelf_update_move.c b/libelf/gelf_update_move.c
index 2b12c7eb5efa..c165cd505ae3 100644
--- a/libelf/gelf_update_move.c
+++ b/libelf/gelf_update_move.c
@@ -48,6 +48,12 @@ gelf_update_move (Elf_Data *data, int ndx, GElf_Move *src)
if (data == NULL)
return result;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return result;
+ }
+
if (unlikely (data->d_type != ELF_T_MOVE))
{
/* The type of the data better should match. */
@@ -55,6 +61,12 @@ gelf_update_move (Elf_Data *data, int ndx, GElf_Move *src)
return result;
}
+ if (unlikely (data->d_buf == NULL && data->d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return result;
+ }
+
elf = data_scn->s->elf;
rwlock_wrlock (elf->lock);
diff --git a/libelf/gelf_update_phdr.c b/libelf/gelf_update_phdr.c
index 4566328f5aa7..9cab8e6ec466 100644
--- a/libelf/gelf_update_phdr.c
+++ b/libelf/gelf_update_phdr.c
@@ -51,6 +51,12 @@ gelf_update_phdr (Elf *elf, int ndx, GElf_Phdr *src)
return 0;
}
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
rwlock_wrlock (elf->lock);
/* Test whether the index is ok. */
diff --git a/libelf/gelf_update_rel.c b/libelf/gelf_update_rel.c
index 14f62e973a5e..7d961bac3297 100644
--- a/libelf/gelf_update_rel.c
+++ b/libelf/gelf_update_rel.c
@@ -47,6 +47,12 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src)
if (dst == NULL)
return 0;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
if (unlikely (data_scn->d.d_type != ELF_T_REL))
{
/* The type of the data better should match. */
@@ -54,6 +60,12 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src)
return 0;
}
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
scn = data_scn->s;
rwlock_wrlock (scn->elf->lock);
diff --git a/libelf/gelf_update_rela.c b/libelf/gelf_update_rela.c
index 88252703976f..b3d4e140dfb0 100644
--- a/libelf/gelf_update_rela.c
+++ b/libelf/gelf_update_rela.c
@@ -47,6 +47,12 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src)
if (dst == NULL)
return 0;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
if (unlikely (data_scn->d.d_type != ELF_T_RELA))
{
/* The type of the data better should match. */
@@ -54,6 +60,12 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src)
return 0;
}
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
scn = data_scn->s;
rwlock_wrlock (scn->elf->lock);
diff --git a/libelf/gelf_update_shdr.c b/libelf/gelf_update_shdr.c
index c93c6ec0a316..2b172cc8ed51 100644
--- a/libelf/gelf_update_shdr.c
+++ b/libelf/gelf_update_shdr.c
@@ -43,9 +43,15 @@ gelf_update_shdr (Elf_Scn *scn, GElf_Shdr *src)
int result = 0;
Elf *elf;
- if (scn == NULL || src == NULL)
+ if (scn == NULL)
return 0;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
elf = scn->elf;
rwlock_wrlock (elf->lock);
diff --git a/libelf/gelf_update_sym.c b/libelf/gelf_update_sym.c
index 0f47885776b8..cdf60a1493fa 100644
--- a/libelf/gelf_update_sym.c
+++ b/libelf/gelf_update_sym.c
@@ -48,6 +48,12 @@ gelf_update_sym (Elf_Data *data, int ndx, GElf_Sym *src)
if (data == NULL)
return 0;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
if (unlikely (data_scn->d.d_type != ELF_T_SYM))
{
/* The type of the data better should match. */
@@ -55,6 +61,12 @@ gelf_update_sym (Elf_Data *data, int ndx, GElf_Sym *src)
return 0;
}
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
scn = data_scn->s;
rwlock_wrlock (scn->elf->lock);
diff --git a/libelf/gelf_update_syminfo.c b/libelf/gelf_update_syminfo.c
index a471e42983a9..6873d6759add 100644
--- a/libelf/gelf_update_syminfo.c
+++ b/libelf/gelf_update_syminfo.c
@@ -47,6 +47,12 @@ gelf_update_syminfo (Elf_Data *data, int ndx, GElf_Syminfo
*src)
if (data == NULL)
return 0;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
{
/* The type of the data better should match. */
@@ -54,6 +60,12 @@ gelf_update_syminfo (Elf_Data *data, int ndx, GElf_Syminfo
*src)
return 0;
}
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
/* The types for 32 and 64 bit are the same. Lucky us. */
eu_static_assert (sizeof (GElf_Syminfo) == sizeof (Elf32_Syminfo));
eu_static_assert (sizeof (GElf_Syminfo) == sizeof (Elf64_Syminfo));
diff --git a/libelf/gelf_update_symshndx.c b/libelf/gelf_update_symshndx.c
index 14f424184383..373a33b575ef 100644
--- a/libelf/gelf_update_symshndx.c
+++ b/libelf/gelf_update_symshndx.c
@@ -52,13 +52,30 @@ gelf_update_symshndx (Elf_Data *symdata, Elf_Data
*shndxdata, int ndx,
if (symdata == NULL)
return 0;
- if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
+ if (unlikely (symdata_scn->d.d_type != ELF_T_SYM)
+ || (shndxdata_scn != NULL
+ && unlikely (shndxdata_scn->d.d_type != ELF_T_WORD)))
{
/* The type of the data better should match. */
__libelf_seterrno (ELF_E_DATA_MISMATCH);
return 0;
}
+ if (unlikely (symdata_scn->d.d_buf == NULL && symdata_scn->d.d_size != 0)
+ || (shndxdata_scn != NULL
+ && unlikely (shndxdata_scn->d.d_buf == NULL
+ && shndxdata_scn->d.d_size != 0)))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
scn = symdata_scn->s;
/* We simply have to believe the user that the two sections belong to
the same ELF file. */
@@ -68,7 +85,7 @@ gelf_update_symshndx (Elf_Data *symdata, Elf_Data *shndxdata,
int ndx,
section index table. */
if (shndxdata_scn != NULL)
{
- if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
+ if (INVALID_NDX (ndx, Elf32_Word, &shndxdata_scn->d))
{
__libelf_seterrno (ELF_E_INVALID_INDEX);
goto out;
diff --git a/libelf/gelf_update_verdaux.c b/libelf/gelf_update_verdaux.c
index 9f4a43fad275..1579104218de 100644
--- a/libelf/gelf_update_verdaux.c
+++ b/libelf/gelf_update_verdaux.c
@@ -45,15 +45,22 @@ gelf_update_verdaux (Elf_Data *data, int offset,
GElf_Verdaux *src)
if (data == NULL)
return 0;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
/* The types for 32 and 64 bit are the same. Lucky us. */
eu_static_assert (sizeof (GElf_Verdaux) == sizeof (Elf32_Verdaux));
eu_static_assert (sizeof (GElf_Verdaux) == sizeof (Elf64_Verdaux));
- /* Check whether we have to resize the data buffer. */
+ /* Make sure the offset is OK. */
if (unlikely (offset < 0)
- || unlikely ((offset + sizeof (GElf_Verdaux)) > data_scn->d.d_size))
+ || unlikely ((offset + sizeof (GElf_Verdaux)) > data_scn->d.d_size)
+ || unlikely (offset % __alignof__ (GElf_Verdaux) != 0))
{
- __libelf_seterrno (ELF_E_INVALID_INDEX);
+ __libelf_seterrno (ELF_E_OFFSET_RANGE);
return 0;
}
@@ -64,6 +71,12 @@ gelf_update_verdaux (Elf_Data *data, int offset,
GElf_Verdaux *src)
return 0;
}
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
rwlock_wrlock (data_scn->s->elf->lock);
memcpy ((char *) data_scn->d.d_buf + offset, src, sizeof (GElf_Verdaux));
diff --git a/libelf/gelf_update_verdef.c b/libelf/gelf_update_verdef.c
index bd224bdba6f4..5b9c25966c52 100644
--- a/libelf/gelf_update_verdef.c
+++ b/libelf/gelf_update_verdef.c
@@ -45,15 +45,22 @@ gelf_update_verdef (Elf_Data *data, int offset, GElf_Verdef
*src)
if (data == NULL)
return 0;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
/* The types for 32 and 64 bit are the same. Lucky us. */
eu_static_assert (sizeof (GElf_Verdef) == sizeof (Elf32_Verdef));
eu_static_assert (sizeof (GElf_Verdef) == sizeof (Elf64_Verdef));
- /* Check whether we have to resize the data buffer. */
+ /* Make sure the offset is OK. */
if (unlikely (offset < 0)
- || unlikely ((offset + sizeof (GElf_Verdef)) > data_scn->d.d_size))
+ || unlikely ((offset + sizeof (GElf_Verdef)) > data_scn->d.d_size)
+ || unlikely (offset % __alignof__ (GElf_Verdef) != 0))
{
- __libelf_seterrno (ELF_E_INVALID_INDEX);
+ __libelf_seterrno (ELF_E_OFFSET_RANGE);
return 0;
}
@@ -64,6 +71,12 @@ gelf_update_verdef (Elf_Data *data, int offset, GElf_Verdef
*src)
return 0;
}
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
rwlock_wrlock (data_scn->s->elf->lock);
memcpy ((char *) data_scn->d.d_buf + offset, src, sizeof (GElf_Verdef));
diff --git a/libelf/gelf_update_vernaux.c b/libelf/gelf_update_vernaux.c
index f60429a65b88..713d23dc6c75 100644
--- a/libelf/gelf_update_vernaux.c
+++ b/libelf/gelf_update_vernaux.c
@@ -45,15 +45,22 @@ gelf_update_vernaux (Elf_Data *data, int offset,
GElf_Vernaux *src)
if (data == NULL)
return 0;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
/* The types for 32 and 64 bit are the same. Lucky us. */
eu_static_assert (sizeof (GElf_Vernaux) == sizeof (Elf32_Vernaux));
eu_static_assert (sizeof (GElf_Vernaux) == sizeof (Elf64_Vernaux));
- /* Check whether we have to resize the data buffer. */
+ /* Make sure the offset is OK. */
if (unlikely (offset < 0)
- || unlikely ((offset + sizeof (GElf_Vernaux)) > data_scn->d.d_size))
+ || unlikely ((offset + sizeof (GElf_Vernaux)) > data_scn->d.d_size)
+ || unlikely (offset % sizeof (GElf_Vernaux) != 0))
{
- __libelf_seterrno (ELF_E_INVALID_INDEX);
+ __libelf_seterrno (ELF_E_OFFSET_RANGE);
return 0;
}
@@ -64,6 +71,12 @@ gelf_update_vernaux (Elf_Data *data, int offset,
GElf_Vernaux *src)
return 0;
}
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
rwlock_wrlock (data_scn->s->elf->lock);
memcpy ((char *) data_scn->d.d_buf + offset, src, sizeof (GElf_Vernaux));
diff --git a/libelf/gelf_update_verneed.c b/libelf/gelf_update_verneed.c
index 43338a8f76d8..ce53f5f7a995 100644
--- a/libelf/gelf_update_verneed.c
+++ b/libelf/gelf_update_verneed.c
@@ -45,15 +45,22 @@ gelf_update_verneed (Elf_Data *data, int offset,
GElf_Verneed *src)
if (data == NULL)
return 0;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
/* The types for 32 and 64 bit are the same. Lucky us. */
eu_static_assert (sizeof (GElf_Verneed) == sizeof (Elf32_Verneed));
eu_static_assert (sizeof (GElf_Verneed) == sizeof (Elf64_Verneed));
- /* Check whether we have to resize the data buffer. */
+ /* Make sure the offset is OK. */
if (unlikely (offset < 0)
- || unlikely ((offset + sizeof (GElf_Verneed)) > data_scn->d.d_size))
+ || unlikely ((offset + sizeof (GElf_Verneed)) > data_scn->d.d_size)
+ || unlikely (offset % sizeof (GElf_Verneed) != 0))
{
- __libelf_seterrno (ELF_E_INVALID_INDEX);
+ __libelf_seterrno (ELF_E_OFFSET_RANGE);
return 0;
}
@@ -64,6 +71,12 @@ gelf_update_verneed (Elf_Data *data, int offset,
GElf_Verneed *src)
return 0;
}
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
rwlock_wrlock (data_scn->s->elf->lock);
memcpy ((char *) data_scn->d.d_buf + offset, src, sizeof (GElf_Verneed));
diff --git a/libelf/gelf_update_versym.c b/libelf/gelf_update_versym.c
index 5dc46222e930..438fb0e757cb 100644
--- a/libelf/gelf_update_versym.c
+++ b/libelf/gelf_update_versym.c
@@ -45,6 +45,12 @@ gelf_update_versym (Elf_Data *data, int ndx, GElf_Versym
*src)
if (data == NULL)
return 0;
+ if (src == NULL)
+ {
+ __libelf_seterrno (ELF_E_INVALID_OPERAND);
+ return 0;
+ }
+
/* The types for 32 and 64 bit are the same. Lucky us. */
eu_static_assert (sizeof (GElf_Versym) == sizeof (Elf32_Versym));
eu_static_assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
@@ -63,6 +69,12 @@ gelf_update_versym (Elf_Data *data, int ndx, GElf_Versym
*src)
return 0;
}
+ if (unlikely (data_scn->d.d_buf == NULL && data_scn->d.d_size != 0))
+ {
+ __libelf_seterrno (ELF_E_INVALID_DATA);
+ return 0;
+ }
+
rwlock_wrlock (data_scn->s->elf->lock);
((GElf_Versym *) data_scn->d.d_buf)[ndx] = *src;
--
2.56.1