From: Roland McGrath Date: Sun, 9 Aug 2009 21:43:42 +0000 (-0700) Subject: Roll limit checks into INVALID_NDX macro, avoid overflow. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=74a5c7da8c846ce82e17683d63937d6535367678;p=thirdparty%2Felfutils.git Roll limit checks into INVALID_NDX macro, avoid overflow. --- diff --git a/libelf/gelf_getdyn.c b/libelf/gelf_getdyn.c index bdf647626..195d2e0a5 100644 --- a/libelf/gelf_getdyn.c +++ b/libelf/gelf_getdyn.c @@ -1,5 +1,5 @@ /* Get information from dynamic table at the given index. - Copyright (C) 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 2000-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2000. @@ -93,8 +93,7 @@ gelf_getdyn (data, ndx, dst) table entries has to be adopted. The user better has provided a buffer where we can store the information. While copying the data we are converting the format. */ - if (INVALID_NDX (ndx, Elf32_Dyn) - || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf32_Dyn, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; @@ -115,8 +114,7 @@ gelf_getdyn (data, ndx, dst) /* The data is already in the correct form. Just make sure the index is OK. */ - if (INVALID_NDX (ndx, GElf_Dyn) - || unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, GElf_Dyn, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; diff --git a/libelf/gelf_getlib.c b/libelf/gelf_getlib.c index eba1a3e9d..3a110f8f9 100644 --- a/libelf/gelf_getlib.c +++ b/libelf/gelf_getlib.c @@ -1,5 +1,5 @@ /* Get library from table at the given index. - Copyright (C) 2004 Red Hat, Inc. + Copyright (C) 2004-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2004. @@ -86,8 +86,7 @@ gelf_getlib (data, ndx, dst) /* The data is already in the correct form. Just make sure the index is OK. */ GElf_Lib *result = NULL; - if (INVALID_NDX (ndx, GElf_Lib) - || unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size)) + if (INVALID_NDX (ndx, GElf_Lib, data)) __libelf_seterrno (ELF_E_INVALID_INDEX); else { diff --git a/libelf/gelf_getmove.c b/libelf/gelf_getmove.c index 05f61b95c..db36e9d5e 100644 --- a/libelf/gelf_getmove.c +++ b/libelf/gelf_getmove.c @@ -1,5 +1,5 @@ /* Get move structure at the given index. - Copyright (C) 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 2000-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2000. @@ -83,8 +83,7 @@ gelf_getmove (data, ndx, dst) /* The data is already in the correct form. Just make sure the index is OK. */ - if (INVALID_NDX (ndx, GElf_Move) - || unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size)) + if (INVALID_NDX (ndx, GElf_Move, data)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; diff --git a/libelf/gelf_getrel.c b/libelf/gelf_getrel.c index c80933790..bc90e60b1 100644 --- a/libelf/gelf_getrel.c +++ b/libelf/gelf_getrel.c @@ -1,5 +1,5 @@ /* Get REL relocation information at given index. - Copyright (C) 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 2000-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2000. @@ -87,8 +87,7 @@ gelf_getrel (data, ndx, dst) if (scn->elf->class == ELFCLASS32) { /* We have to convert the data. */ - if (INVALID_NDX (ndx, Elf32_Rel) - || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf32_Rel, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); result = NULL; @@ -108,8 +107,7 @@ gelf_getrel (data, ndx, dst) { /* Simply copy the data after we made sure we are actually getting correct data. */ - if (INVALID_NDX (ndx, Elf64_Rel) - || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf64_Rel, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); result = NULL; diff --git a/libelf/gelf_getrela.c b/libelf/gelf_getrela.c index 5d945bc67..40da4e66c 100644 --- a/libelf/gelf_getrela.c +++ b/libelf/gelf_getrela.c @@ -1,5 +1,5 @@ /* Get RELA relocation information at given index. - Copyright (C) 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 2000-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2000. @@ -87,8 +87,7 @@ gelf_getrela (data, ndx, dst) if (scn->elf->class == ELFCLASS32) { /* We have to convert the data. */ - if (INVALID_NDX (ndx, Elf32_Rela) - || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf32_Rela, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); result = NULL; @@ -109,8 +108,7 @@ gelf_getrela (data, ndx, dst) { /* Simply copy the data after we made sure we are actually getting correct data. */ - if (INVALID_NDX (ndx, Elf64_Rela) - || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf64_Rela, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); result = NULL; diff --git a/libelf/gelf_getsym.c b/libelf/gelf_getsym.c index 1ec6321d6..77df2dc57 100644 --- a/libelf/gelf_getsym.c +++ b/libelf/gelf_getsym.c @@ -1,5 +1,5 @@ /* Get symbol information from symbol table at the given index. - Copyright (C) 1999, 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 1999-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 1999. @@ -90,8 +90,7 @@ gelf_getsym (data, ndx, dst) table entries has to be adopted. The user better has provided a buffer where we can store the information. While copying the data we are converting the format. */ - if (INVALID_NDX (ndx, Elf32_Sym) - || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size)) + if (INVALID_NDX (ndx, Elf32_Sym, data)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; @@ -120,8 +119,7 @@ gelf_getsym (data, ndx, dst) /* The data is already in the correct form. Just make sure the index is OK. */ - if (INVALID_NDX (ndx, GElf_Sym) - || unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size)) + if (INVALID_NDX (ndx, GElf_Sym, data)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; diff --git a/libelf/gelf_getsyminfo.c b/libelf/gelf_getsyminfo.c index f561cd451..03e32a175 100644 --- a/libelf/gelf_getsyminfo.c +++ b/libelf/gelf_getsyminfo.c @@ -1,5 +1,5 @@ /* Get additional symbol information from symbol table at the given index. - Copyright (C) 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 2000-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2000. @@ -84,8 +84,7 @@ gelf_getsyminfo (data, ndx, dst) /* The data is already in the correct form. Just make sure the index is OK. */ - if (INVALID_NDX (ndx, GElf_Syminfo) - || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size)) + if (INVALID_NDX (ndx, GElf_Syminfo, data)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; diff --git a/libelf/gelf_getsymshndx.c b/libelf/gelf_getsymshndx.c index 33481c992..c574a2a61 100644 --- a/libelf/gelf_getsymshndx.c +++ b/libelf/gelf_getsymshndx.c @@ -1,6 +1,6 @@ /* Get symbol information and separate section index from symbol table at the given index. - Copyright (C) 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 2000-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2000. @@ -90,9 +90,7 @@ gelf_getsymshndx (symdata, shndxdata, ndx, dst, dstshndx) section index table. */ if (likely (shndxdata_scn != NULL)) { - if (INVALID_NDX (ndx, Elf32_Word) - || 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; @@ -112,8 +110,7 @@ gelf_getsymshndx (symdata, shndxdata, ndx, dst, dstshndx) table entries has to be adopted. The user better has provided a buffer where we can store the information. While copying the data we are converting the format. */ - if (INVALID_NDX (ndx, Elf32_Sym) - || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size)) + if (INVALID_NDX (ndx, Elf32_Sym, symdata)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; @@ -142,8 +139,7 @@ gelf_getsymshndx (symdata, shndxdata, ndx, dst, dstshndx) /* The data is already in the correct form. Just make sure the index is OK. */ - if (INVALID_NDX (ndx, GElf_Sym) - || unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size)) + if (INVALID_NDX (ndx, GElf_Sym, symdata)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; diff --git a/libelf/gelf_getversym.c b/libelf/gelf_getversym.c index 7097ca52c..1abfce0b0 100644 --- a/libelf/gelf_getversym.c +++ b/libelf/gelf_getversym.c @@ -1,5 +1,5 @@ /* Get symbol version information at the given index. - Copyright (C) 1999, 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 1999-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 1999. @@ -92,8 +92,7 @@ gelf_getversym (data, ndx, dst) /* The data is already in the correct form. Just make sure the index is OK. */ - if (INVALID_NDX (ndx, GElf_Versym) - || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size)) + if (INVALID_NDX (ndx, GElf_Versym, data)) { __libelf_seterrno (ELF_E_INVALID_INDEX); result = NULL; diff --git a/libelf/gelf_update_dyn.c b/libelf/gelf_update_dyn.c index 38d107686..08bd9bb3f 100644 --- a/libelf/gelf_update_dyn.c +++ b/libelf/gelf_update_dyn.c @@ -1,5 +1,5 @@ /* Update information in dynamic table at the given index. - Copyright (C) 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 2000-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2000. @@ -96,8 +96,7 @@ gelf_update_dyn (data, ndx, src) } /* Check whether we have to resize the data buffer. */ - if (INVALID_NDX (ndx, Elf32_Dyn) - || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf32_Dyn, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; @@ -111,8 +110,7 @@ gelf_update_dyn (data, ndx, src) else { /* Check whether we have to resize the data buffer. */ - if (INVALID_NDX (ndx, Elf64_Dyn) - || unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf64_Dyn, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; diff --git a/libelf/gelf_update_lib.c b/libelf/gelf_update_lib.c index 375d23812..efbb9b6d3 100644 --- a/libelf/gelf_update_lib.c +++ b/libelf/gelf_update_lib.c @@ -1,5 +1,5 @@ /* Update library in table at the given index. - Copyright (C) 2004 Red Hat, Inc. + Copyright (C) 2004-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2004. @@ -81,8 +81,7 @@ gelf_update_lib (data, ndx, src) /* Check whether we have to resize the data buffer. */ int result = 0; - if (INVALID_NDX (ndx, Elf64_Lib) - || unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf64_Lib, &data_scn->d)) __libelf_seterrno (ELF_E_INVALID_INDEX); else { diff --git a/libelf/gelf_update_move.c b/libelf/gelf_update_move.c index faadb0ba9..cb3d4ad3c 100644 --- a/libelf/gelf_update_move.c +++ b/libelf/gelf_update_move.c @@ -1,5 +1,5 @@ /* Update move structure at the given index. - Copyright (C) 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 2000-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2000. @@ -75,8 +75,7 @@ gelf_update_move (data, ndx, src) assert (sizeof (GElf_Move) == sizeof (Elf64_Move)); /* Check whether we have to resize the data buffer. */ - if (INVALID_NDX (ndx, GElf_Move) - || unlikely ((ndx + 1) * sizeof (GElf_Move) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, GElf_Move, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); return 0; diff --git a/libelf/gelf_update_rel.c b/libelf/gelf_update_rel.c index 1129185a0..f966fc742 100644 --- a/libelf/gelf_update_rel.c +++ b/libelf/gelf_update_rel.c @@ -1,5 +1,5 @@ /* Update REL relocation information at given index. - Copyright (C) 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 2000-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2000. @@ -93,8 +93,7 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src) } /* Check whether we have to resize the data buffer. */ - if (INVALID_NDX (ndx, Elf32_Rel) - || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf32_Rel, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; @@ -109,8 +108,7 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src) else { /* Check whether we have to resize the data buffer. */ - if (INVALID_NDX (ndx, Elf64_Rel) - || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf64_Rel, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; diff --git a/libelf/gelf_update_rela.c b/libelf/gelf_update_rela.c index b5c496569..31234c170 100644 --- a/libelf/gelf_update_rela.c +++ b/libelf/gelf_update_rela.c @@ -1,5 +1,5 @@ /* Update RELA relocation information at given index. - Copyright (C) 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 2000-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2000. @@ -95,8 +95,7 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src) } /* Check whether we have to resize the data buffer. */ - if (INVALID_NDX (ndx, Elf32_Rela) - || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf32_Rela, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; @@ -112,8 +111,7 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src) else { /* Check whether we have to resize the data buffer. */ - if (INVALID_NDX (ndx, Elf64_Rela) - || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf64_Rela, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; diff --git a/libelf/gelf_update_sym.c b/libelf/gelf_update_sym.c index 16a673626..960df2ec8 100644 --- a/libelf/gelf_update_sym.c +++ b/libelf/gelf_update_sym.c @@ -1,5 +1,5 @@ /* Update symbol information in symbol table at the given index. - Copyright (C) 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 2000-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2000. @@ -96,8 +96,7 @@ gelf_update_sym (data, ndx, src) } /* Check whether we have to resize the data buffer. */ - if (INVALID_NDX (ndx, Elf32_Sym) - || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf32_Sym, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; @@ -120,8 +119,7 @@ gelf_update_sym (data, ndx, src) else { /* Check whether we have to resize the data buffer. */ - if (INVALID_NDX (ndx, Elf64_Sym) - || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf64_Sym, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; diff --git a/libelf/gelf_update_syminfo.c b/libelf/gelf_update_syminfo.c index 659fa4f04..758d35bd7 100644 --- a/libelf/gelf_update_syminfo.c +++ b/libelf/gelf_update_syminfo.c @@ -1,5 +1,5 @@ /* Update additional symbol information in symbol table at the given index. - Copyright (C) 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 2000-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2000. @@ -87,8 +87,7 @@ gelf_update_syminfo (data, ndx, src) rwlock_wrlock (scn->elf->lock); /* Check whether we have to resize the data buffer. */ - if (INVALID_NDX (ndx, GElf_Syminfo) - || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, GElf_Syminfo, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; diff --git a/libelf/gelf_update_symshndx.c b/libelf/gelf_update_symshndx.c index e7d6fad71..0b8f7beaf 100644 --- a/libelf/gelf_update_symshndx.c +++ b/libelf/gelf_update_symshndx.c @@ -1,6 +1,6 @@ /* Update symbol information and section index in symbol table at the given index. - Copyright (C) 2000, 2001, 2002 Red Hat, Inc. + Copyright (C) 2000-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2000. @@ -122,8 +122,7 @@ gelf_update_symshndx (symdata, shndxdata, ndx, src, srcshndx) } /* Check whether we have to resize the data buffer. */ - if (INVALID_NDX (ndx, Elf32_Sym) - || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf32_Sym, &symdata_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; @@ -146,8 +145,7 @@ gelf_update_symshndx (symdata, shndxdata, ndx, src, srcshndx) else { /* Check whether we have to resize the data buffer. */ - if (INVALID_NDX (ndx, Elf64_Sym) - || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size)) + if (INVALID_NDX (ndx, Elf64_Sym, &symdata_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); goto out; diff --git a/libelf/gelf_update_versym.c b/libelf/gelf_update_versym.c index b69ec75b6..b4e9c17d2 100644 --- a/libelf/gelf_update_versym.c +++ b/libelf/gelf_update_versym.c @@ -1,5 +1,5 @@ /* Update symbol version information. - Copyright (C) 2001, 2002 Red Hat, Inc. + Copyright (C) 2001-2009 Red Hat, Inc. This file is part of Red Hat elfutils. Written by Ulrich Drepper , 2001. @@ -75,8 +75,7 @@ gelf_update_versym (data, ndx, src) assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym)); /* Check whether we have to resize the data buffer. */ - if (INVALID_NDX (ndx, GElf_Versym) - || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data_scn->d.d_size)) + if (INVALID_NDX (ndx, GElf_Versym, &data_scn->d)) { __libelf_seterrno (ELF_E_INVALID_INDEX); return 0; diff --git a/libelf/libelfP.h b/libelf/libelfP.h index 2abf58cd1..ac90f39c0 100644 --- a/libelf/libelfP.h +++ b/libelf/libelfP.h @@ -606,13 +606,8 @@ extern uint32_t __libelf_crc32 (uint32_t crc, unsigned char *buf, size_t len) /* Align offset to 4 bytes as needed for note name and descriptor data. */ #define NOTE_ALIGN(n) (((n) + 3) & -4U) -/* Convenience macro. Assumes int NDX and TYPE with size at least - 2 bytes. */ -#if SIZE_MAX > 4294967295U -# define INVALID_NDX(ndx, type) unlikely (ndx < 0) -#else -# define INVALID_NDX(ndx, type) \ - unlikely ((unsigned int) (ndx) >= SIZE_MAX / sizeof (type)) -#endif +/* Convenience macro. */ +#define INVALID_NDX(ndx, type, data) \ + unlikely ((data)->d_size / sizeof (type) <= (unsigned int) (ndx)) #endif /* libelfP.h */