diff options
author | Jonathan Gray <jsg@cvs.openbsd.org> | 2020-05-18 06:46:24 +0000 |
---|---|---|
committer | Jonathan Gray <jsg@cvs.openbsd.org> | 2020-05-18 06:46:24 +0000 |
commit | 9090e45334c51817d4a0fa281c9d76c6e90f8d07 (patch) | |
tree | b031656cb3332146fd6486dd2f885f3a22897f69 | |
parent | b6287cef9243e06e73ff157fa843467499f51489 (diff) |
update libelf from elftoolchain r3717 to r3833
ok deraadt@
37 files changed, 220 insertions, 119 deletions
diff --git a/lib/libelf/_libelf.h b/lib/libelf/_libelf.h index 8531ba3cb59..4d1e7461700 100644 --- a/lib/libelf/_libelf.h +++ b/lib/libelf/_libelf.h @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: _libelf.h,v 1.2 2019/03/19 02:31:35 jsg Exp $ + * $Id: _libelf.h,v 1.3 2020/05/18 06:46:23 jsg Exp $ */ #ifndef __LIBELF_H_ @@ -226,7 +226,7 @@ size_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version); void *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count); Elf *_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror); struct _Libelf_Data *_libelf_release_data(struct _Libelf_Data *_d); -Elf *_libelf_release_elf(Elf *_e); +void _libelf_release_elf(Elf *_e); Elf_Scn *_libelf_release_scn(Elf_Scn *_s); int _libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum); int _libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum); diff --git a/lib/libelf/_libelf_config.h b/lib/libelf/_libelf_config.h index 93876419a73..e89bf3b806d 100644 --- a/lib/libelf/_libelf_config.h +++ b/lib/libelf/_libelf_config.h @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: _libelf_config.h,v 1.1 2019/02/01 05:27:37 jsg Exp $ + * $Id: _libelf_config.h,v 1.2 2020/05/18 06:46:23 jsg Exp $ */ #if defined(__APPLE__) || defined(__DragonFly__) @@ -103,6 +103,12 @@ #define LIBELF_BYTEORDER ELFDATA2LSB #define LIBELF_CLASS ELFCLASS64 +#elif defined(__riscv64) + +#define LIBELF_ARCH EM_RISCV +#define LIBELF_BYTEORDER ELFDATA2LSB +#define LIBELF_CLASS ELFCLASS64 + #elif defined(__sparc__) #define LIBELF_ARCH EM_SPARCV9 diff --git a/lib/libelf/elf.3 b/lib/libelf/elf.3 index 3ace373a800..3c238f2ea4f 100644 --- a/lib/libelf/elf.3 +++ b/lib/libelf/elf.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf.3,v 1.4 2019/06/11 18:38:46 schwarze Exp $ +.\" $Id: elf.3,v 1.5 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd February 5, 2019 +.Dd June 12, 2019 .Dt ELF 3 .Os .Sh NAME diff --git a/lib/libelf/elf_data.c b/lib/libelf/elf_data.c index 4164f67763d..03d3212f8e0 100644 --- a/lib/libelf/elf_data.c +++ b/lib/libelf/elf_data.c @@ -32,7 +32,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: elf_data.c,v 1.2 2019/03/19 02:31:35 jsg Exp $"); +ELFTC_VCSID("$Id: elf_data.c,v 1.3 2020/05/18 06:46:23 jsg Exp $"); Elf_Data * elf_getdata(Elf_Scn *s, Elf_Data *ed) @@ -118,7 +118,8 @@ elf_getdata(Elf_Scn *s, Elf_Data *ed) count = (size_t) (sh_size / fsz); - msz = _libelf_msize(elftype, elfclass, e->e_version); + if ((msz = _libelf_msize(elftype, elfclass, e->e_version)) == 0) + return (NULL); if (count > 0 && msz > SIZE_MAX / count) { LIBELF_SET_ERROR(RANGE, 0); diff --git a/lib/libelf/elf_end.c b/lib/libelf/elf_end.c index 84820feb302..e116e194cef 100644 --- a/lib/libelf/elf_end.c +++ b/lib/libelf/elf_end.c @@ -34,7 +34,7 @@ #include <sys/mman.h> #endif -ELFTC_VCSID("$Id: elf_end.c,v 1.2 2019/03/19 02:31:35 jsg Exp $"); +ELFTC_VCSID("$Id: elf_end.c,v 1.3 2020/05/18 06:46:23 jsg Exp $"); int elf_end(Elf *e) @@ -88,7 +88,7 @@ elf_end(Elf *e) sv = e; if ((e = e->e_parent) != NULL) e->e_u.e_ar.e_nchildren--; - sv = _libelf_release_elf(sv); + _libelf_release_elf(sv); } return (0); diff --git a/lib/libelf/elf_flagdata.3 b/lib/libelf/elf_flagdata.3 index c099c43fde2..f4ab6ed1faf 100644 --- a/lib/libelf/elf_flagdata.3 +++ b/lib/libelf/elf_flagdata.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_flagdata.3,v 1.2 2019/06/11 18:30:31 schwarze Exp $ +.\" $Id: elf_flagdata.3,v 1.3 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd December 3, 2011 +.Dd June 12, 2019 .Dt ELF_FLAGDATA 3 .Os .Sh NAME diff --git a/lib/libelf/elf_getdata.3 b/lib/libelf/elf_getdata.3 index 5bb1c2d2810..aa14deb5706 100644 --- a/lib/libelf/elf_getdata.3 +++ b/lib/libelf/elf_getdata.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getdata.3,v 1.1 2019/02/01 05:27:37 jsg Exp $ +.\" $Id: elf_getdata.3,v 1.2 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd January 26, 2011 +.Dd April 22, 2019 .Dt ELF_GETDATA 3 .Os .Sh NAME @@ -214,7 +214,12 @@ is incorrect. .It Bq Er ELF_E_UNIMPL The section type associated with section .Ar scn -is currently unsupported by the library. +is not supported. +.It Bq Er ELF_E_VERSION +Section +.Ar scn +was associated with an ELF object with an unsupported +version. .El .Sh SEE ALSO .Xr elf 3 , diff --git a/lib/libelf/elf_open.3 b/lib/libelf/elf_open.3 index ec01f438228..3c71a54e31c 100644 --- a/lib/libelf/elf_open.3 +++ b/lib/libelf/elf_open.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_open.3,v 1.2 2019/06/11 18:30:31 schwarze Exp $ +.\" $Id: elf_open.3,v 1.3 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd May 31, 2012 +.Dd June 12, 2019 .Dt ELF_OPEN 3 .Os .Sh NAME diff --git a/lib/libelf/elf_update.3 b/lib/libelf/elf_update.3 index 863561df1ae..840e3e9b225 100644 --- a/lib/libelf/elf_update.3 +++ b/lib/libelf/elf_update.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_update.3,v 1.1 2019/02/01 05:27:37 jsg Exp $ +.\" $Id: elf_update.3,v 1.2 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd August 14, 2011 +.Dd April 22, 2019 .Dt ELF_UPDATE 3 .Os .Sh NAME @@ -345,6 +345,10 @@ operation was requested after a prior call to disassociated the ELF descriptor .Ar elf from its underlying file. +.It Bq Er ELF_E_UNIMPL +Argument +.Ar elf +contained a section with an unsupported ELF type. .It Bq Er ELF_E_VERSION Argument .Ar elf diff --git a/lib/libelf/elf_update.c b/lib/libelf/elf_update.c index 880fd1a0e62..e71109b6a4b 100644 --- a/lib/libelf/elf_update.c +++ b/lib/libelf/elf_update.c @@ -41,7 +41,7 @@ #include <sys/mman.h> #endif -ELFTC_VCSID("$Id: elf_update.c,v 1.2 2019/03/19 02:31:35 jsg Exp $"); +ELFTC_VCSID("$Id: elf_update.c,v 1.3 2020/05/18 06:46:23 jsg Exp $"); /* * Layout strategy: @@ -222,10 +222,16 @@ _libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t rc) } /* + * The data buffer's ELF type, ELF class and ELF version + * should be supported. + */ + if ((msz = _libelf_msize(d->d_type, ec, e->e_version)) == 0) + return (0); + + /* * The buffer's size should be a multiple of the * memory size of the underlying type. */ - msz = _libelf_msize(d->d_type, ec, e->e_version); if (d->d_size % msz) { LIBELF_SET_ERROR(DATA, 0); return (0); @@ -800,7 +806,8 @@ _libelf_write_scn(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) d = &ld->d_data; - msz = _libelf_msize(d->d_type, ec, e->e_version); + if ((msz = _libelf_msize(d->d_type, ec, e->e_version)) == 0) + return ((off_t) -1); if ((uint64_t) rc < sh_off + d->d_off) (void) memset(nf + rc, @@ -812,6 +819,7 @@ _libelf_write_scn(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) assert(d->d_buf != NULL); assert(d->d_version == e->e_version); assert(d->d_size % msz == 0); + assert(msz != 0); nobjects = (size_t) (d->d_size / msz); @@ -851,7 +859,8 @@ _libelf_write_ehdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) assert(ehdr != NULL); fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1); - msz = _libelf_msize(ELF_T_EHDR, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_EHDR, ec, e->e_version)) == 0) + return ((off_t) -1); em = _libelf_elfmachine(e); @@ -885,15 +894,17 @@ _libelf_write_phdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) Elf32_Ehdr *eh32; Elf64_Ehdr *eh64; Elf_Data dst, src; - size_t fsz, phnum; + size_t fsz, msz, phnum; uint64_t phoff; assert(ex->ex_type == ELF_EXTENT_PHDR); ec = e->e_class; + ehdr = _libelf_ehdr(e, ec, 0); - phnum = e->e_u.e_elf.e_nphdr; + assert(ehdr != NULL); + phnum = e->e_u.e_elf.e_nphdr; assert(phnum > 0); if (ec == ELFCLASS32) { @@ -913,14 +924,15 @@ _libelf_write_phdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) (void) memset(&dst, 0, sizeof(dst)); (void) memset(&src, 0, sizeof(src)); + if ((msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version)) == 0) + return ((off_t) -1); fsz = _libelf_fsize(ELF_T_PHDR, ec, e->e_version, phnum); assert(fsz > 0); src.d_buf = _libelf_getphdr(e, ec); src.d_version = dst.d_version = e->e_version; src.d_type = ELF_T_PHDR; - src.d_size = phnum * _libelf_msize(ELF_T_PHDR, ec, - e->e_version); + src.d_size = phnum * msz; dst.d_size = fsz; dst.d_buf = nf + ex->ex_start; @@ -945,13 +957,16 @@ _libelf_write_shdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) uint64_t shoff; Elf32_Ehdr *eh32; Elf64_Ehdr *eh64; - size_t fsz, nscn; + size_t fsz, msz, nscn; Elf_Data dst, src; assert(ex->ex_type == ELF_EXTENT_SHDR); ec = e->e_class; + ehdr = _libelf_ehdr(e, ec, 0); + assert(ehdr != NULL); + nscn = e->e_u.e_elf.e_nscn; if (ec == ELFCLASS32) { @@ -971,8 +986,11 @@ _libelf_write_shdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) (void) memset(&dst, 0, sizeof(dst)); (void) memset(&src, 0, sizeof(src)); + if ((msz = _libelf_msize(ELF_T_SHDR, ec, e->e_version)) == 0) + return ((off_t) -1); + src.d_type = ELF_T_SHDR; - src.d_size = _libelf_msize(ELF_T_SHDR, ec, e->e_version); + src.d_size = msz; src.d_version = dst.d_version = e->e_version; fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1); @@ -1109,12 +1127,14 @@ _libelf_write_elf(Elf *e, off_t newsize, struct _Elf_Extent_List *extents) assert((e->e_flags & LIBELF_F_RAWFILE_MALLOC) || (e->e_flags & LIBELF_F_RAWFILE_MMAP)); if (e->e_flags & LIBELF_F_RAWFILE_MALLOC) { + assert((e->e_flags & LIBELF_F_RAWFILE_MMAP) == 0); free(e->e_rawfile); e->e_rawfile = newfile; newfile = NULL; } #if ELFTC_HAVE_MMAP else if (e->e_flags & LIBELF_F_RAWFILE_MMAP) { + assert((e->e_flags & LIBELF_F_RAWFILE_MALLOC) == 0); if ((e->e_rawfile = mmap(NULL, (size_t) newsize, PROT_READ, MAP_PRIVATE, e->e_fd, (off_t) 0)) == MAP_FAILED) { diff --git a/lib/libelf/gelf.3 b/lib/libelf/gelf.3 index 3937a15588b..c43bc41bd6d 100644 --- a/lib/libelf/gelf.3 +++ b/lib/libelf/gelf.3 @@ -21,13 +21,13 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf.3,v 1.2 2019/06/11 18:30:31 schwarze Exp $ +.\" $Id: gelf.3,v 1.3 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd September 1, 2006 +.Dd June 12, 2019 .Dt GELF 3 .Os .Sh NAME -.Nm GElf +.Nm gelf .Nd class-independent API for ELF manipulation .Sh LIBRARY .Lb libelf diff --git a/lib/libelf/gelf_cap.c b/lib/libelf/gelf_cap.c index caf372abfb6..e65cddc4f59 100644 --- a/lib/libelf/gelf_cap.c +++ b/lib/libelf/gelf_cap.c @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_cap.c,v 1.1 2019/02/01 05:27:37 jsg Exp $"); +ELFTC_VCSID("$Id: gelf_cap.c,v 1.2 2020/05/18 06:46:23 jsg Exp $"); GElf_Cap * gelf_getcap(Elf_Data *ed, int ndx, GElf_Cap *dst) @@ -67,9 +67,8 @@ gelf_getcap(Elf_Data *ed, int ndx, GElf_Cap *dst) return (NULL); } - msz = _libelf_msize(ELF_T_CAP, ec, e->e_version); - - assert(msz > 0); + if ((msz = _libelf_msize(ELF_T_CAP, ec, e->e_version)) == 0) + return (NULL); if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); @@ -127,8 +126,8 @@ gelf_update_cap(Elf_Data *ed, int ndx, GElf_Cap *gc) return (0); } - msz = _libelf_msize(ELF_T_CAP, ec, e->e_version); - assert(msz > 0); + if ((msz = _libelf_msize(ELF_T_CAP, ec, e->e_version)) == 0) + return (0); if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); diff --git a/lib/libelf/gelf_dyn.c b/lib/libelf/gelf_dyn.c index a7ac12940e0..304228a4cef 100644 --- a/lib/libelf/gelf_dyn.c +++ b/lib/libelf/gelf_dyn.c @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_dyn.c,v 1.1 2019/02/01 05:27:37 jsg Exp $"); +ELFTC_VCSID("$Id: gelf_dyn.c,v 1.2 2020/05/18 06:46:23 jsg Exp $"); GElf_Dyn * gelf_getdyn(Elf_Data *ed, int ndx, GElf_Dyn *dst) @@ -67,9 +67,9 @@ gelf_getdyn(Elf_Data *ed, int ndx, GElf_Dyn *dst) return (NULL); } - msz = _libelf_msize(ELF_T_DYN, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_DYN, ec, e->e_version)) == 0) + return (NULL); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= d->d_data.d_size) { @@ -127,9 +127,9 @@ gelf_update_dyn(Elf_Data *ed, int ndx, GElf_Dyn *ds) return (0); } - msz = _libelf_msize(ELF_T_DYN, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_DYN, ec, e->e_version)) == 0) + return (0); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= d->d_data.d_size) { diff --git a/lib/libelf/gelf_getcap.3 b/lib/libelf/gelf_getcap.3 index 27ac036a60c..dcfbd9fa265 100644 --- a/lib/libelf/gelf_getcap.3 +++ b/lib/libelf/gelf_getcap.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getcap.3,v 1.1 2019/02/01 05:27:37 jsg Exp $ +.\" $Id: gelf_getcap.3,v 1.2 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd August 29, 2006 +.Dd April 22, 2019 .Dt GELF_GETCAP 3 .Os .Sh NAME @@ -113,6 +113,12 @@ was not associated with a section of type .Dv SHT_SUNW_cap . .It Bq Er ELF_E_RANGE A value was not representable in the target type. +.It Bq Er ELF_E_VERSION +The +.Vt Elf_Data +descriptor denoted by argument +.Ar data +is associated with an ELF object with an unsupported version. .El .Sh SEE ALSO .Xr elf 3 , diff --git a/lib/libelf/gelf_getdyn.3 b/lib/libelf/gelf_getdyn.3 index 5a12e4bf49f..5079f3379ef 100644 --- a/lib/libelf/gelf_getdyn.3 +++ b/lib/libelf/gelf_getdyn.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getdyn.3,v 1.1 2019/02/01 05:27:37 jsg Exp $ +.\" $Id: gelf_getdyn.3,v 1.2 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd August 29, 2006 +.Dd April 22, 2019 .Dt GELF_GETDYN 3 .Os .Sh NAME @@ -115,6 +115,13 @@ was not associated with a section of type .Dv SHT_DYNAMIC . .It Bq Er ELF_E_RANGE A value was not representable in the target type. +.It Bq Er ELF_E_VERSION +The +.Vt Elf_Data +descriptor denoted by argument +.Ar data +is associated with an ELF object with an unsupported version. +.El .El .Sh SEE ALSO .Xr elf 3 , diff --git a/lib/libelf/gelf_getmove.3 b/lib/libelf/gelf_getmove.3 index 3bfe905efcc..c75b0467979 100644 --- a/lib/libelf/gelf_getmove.3 +++ b/lib/libelf/gelf_getmove.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getmove.3,v 1.1 2019/02/01 05:27:37 jsg Exp $ +.\" $Id: gelf_getmove.3,v 1.2 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd August 29, 2006 +.Dd April 22, 2019 .Dt GELF_GETMOVE 3 .Os .Sh NAME @@ -112,6 +112,12 @@ Data descriptor was not associated with a section containing move information. .It Bq Er ELF_E_RANGE A value was not representable in the target type. +.It Bq Er ELF_E_VERSION +The +.Vt Elf_Data +descriptor denoted by argument +.Ar data +is associated with an ELF object with an unsupported version. .El .Sh SEE ALSO .Xr elf 3 , diff --git a/lib/libelf/gelf_getrel.3 b/lib/libelf/gelf_getrel.3 index 427446fe137..407e63688d1 100644 --- a/lib/libelf/gelf_getrel.3 +++ b/lib/libelf/gelf_getrel.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getrel.3,v 1.1 2019/02/01 05:27:38 jsg Exp $ +.\" $Id: gelf_getrel.3,v 1.2 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd August 29, 2006 +.Dd April 22, 2019 .Dt GELF_GETREL 3 .Os .Sh NAME @@ -113,6 +113,12 @@ was not associated with a section of type .Dv SHT_REL . .It Bq Er ELF_E_RANGE A value was not representable in the target type. +.It Bq Er ELF_E_VERSION +The +.Vt Elf_Data +descriptor denoted by argument +.Ar data +is associated with an ELF object with an unsupported version. .El .Sh SEE ALSO .Xr elf 3 , diff --git a/lib/libelf/gelf_getrela.3 b/lib/libelf/gelf_getrela.3 index 9c0dfaa9005..0f3cbe78192 100644 --- a/lib/libelf/gelf_getrela.3 +++ b/lib/libelf/gelf_getrela.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getrela.3,v 1.1 2019/02/01 05:27:38 jsg Exp $ +.\" $Id: gelf_getrela.3,v 1.2 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd August 29, 2006 +.Dd April 22, 2019 .Dt GELF_GETRELA 3 .Os .Sh NAME @@ -113,6 +113,12 @@ was not associated with a section of type .Dv SHT_RELA . .It Bq Er ELF_E_RANGE A value was not representable in the target type. +.It Bq Er ELF_E_VERSION +The +.Vt Elf_Data +descriptor denoted by argument +.Ar data +is associated with an ELF object with an unsupported version. .El .Sh SEE ALSO .Xr elf 3 , diff --git a/lib/libelf/gelf_getsym.3 b/lib/libelf/gelf_getsym.3 index b47ba348951..e16f27777e3 100644 --- a/lib/libelf/gelf_getsym.3 +++ b/lib/libelf/gelf_getsym.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getsym.3,v 1.1 2019/02/01 05:27:38 jsg Exp $ +.\" $Id: gelf_getsym.3,v 1.2 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd August 29, 2006 +.Dd April 22, 2019 .Dt GELF_GETSYM 3 .Os .Sh NAME @@ -115,6 +115,12 @@ Data descriptor was not associated with a section containing symbol information. .It Bq Er ELF_E_RANGE A value was not representable in the target type. +.It Bq Er ELF_E_VERSION +The +.Vt Elf_Data +descriptor denoted by argument +.Ar data +is associated with an ELF object with an unsupported version. .El .Sh SEE ALSO .Xr elf 3 , diff --git a/lib/libelf/gelf_getsyminfo.3 b/lib/libelf/gelf_getsyminfo.3 index 356459cfb05..a056dd0aa49 100644 --- a/lib/libelf/gelf_getsyminfo.3 +++ b/lib/libelf/gelf_getsyminfo.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getsyminfo.3,v 1.1 2019/02/01 05:27:38 jsg Exp $ +.\" $Id: gelf_getsyminfo.3,v 1.2 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd August 29, 2006 +.Dd April 22, 2019 .Dt GELF_GETSYMINFO 3 .Os .Sh NAME @@ -105,6 +105,12 @@ descriptor. Data descriptor .Ar data was not associated with a section containing symbol information. +.It Bq Er ELF_E_VERSION +The +.Vt Elf_Data +descriptor denoted by argument +.Ar data +is associated with an ELF object with an unsupported version. .El .Sh SEE ALSO .Xr elf 3 , diff --git a/lib/libelf/gelf_getsymshndx.3 b/lib/libelf/gelf_getsymshndx.3 index e66edff456f..17461a3e854 100644 --- a/lib/libelf/gelf_getsymshndx.3 +++ b/lib/libelf/gelf_getsymshndx.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getsymshndx.3,v 1.1 2019/02/01 05:27:38 jsg Exp $ +.\" $Id: gelf_getsymshndx.3,v 1.2 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd November 5, 2006 +.Dd April 22, 2019 .Dt GELF_GETSYMSHNDX 3 .Os .Sh NAME @@ -152,6 +152,14 @@ Data descriptor and .Ar xndxdata were associated with different ELF objects. +.It Bq Er ELF_E_VERSION +The +.Vt Elf_Data +descriptors denoted by arguments +.Ar symdata +and +.Ar xndxdata +are associated with an ELF object with an unsupported version. .El .Sh SEE ALSO .Xr elf 3 , diff --git a/lib/libelf/gelf_move.c b/lib/libelf/gelf_move.c index 7d6dbfe1233..ce0780d5f39 100644 --- a/lib/libelf/gelf_move.c +++ b/lib/libelf/gelf_move.c @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_move.c,v 1.1 2019/02/01 05:27:38 jsg Exp $"); +ELFTC_VCSID("$Id: gelf_move.c,v 1.2 2020/05/18 06:46:23 jsg Exp $"); GElf_Move * gelf_getmove(Elf_Data *ed, int ndx, GElf_Move *dst) @@ -67,9 +67,9 @@ gelf_getmove(Elf_Data *ed, int ndx, GElf_Move *dst) return (NULL); } - msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version)) == 0) + return (NULL); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= d->d_data.d_size) { @@ -130,9 +130,9 @@ gelf_update_move(Elf_Data *ed, int ndx, GElf_Move *gm) return (0); } - msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version)) == 0) + return (0); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= d->d_data.d_size) { diff --git a/lib/libelf/gelf_newehdr.3 b/lib/libelf/gelf_newehdr.3 index 7bf742a88a6..9babb2d5aca 100644 --- a/lib/libelf/gelf_newehdr.3 +++ b/lib/libelf/gelf_newehdr.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_newehdr.3,v 1.2 2019/06/11 18:30:31 schwarze Exp $ +.\" $Id: gelf_newehdr.3,v 1.3 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd October 22, 2007 +.Dd June 12, 2019 .Dt GELF_NEWEHDR 3 .Os .Sh NAME diff --git a/lib/libelf/gelf_newphdr.3 b/lib/libelf/gelf_newphdr.3 index 61e07066f11..e8cacfccc07 100644 --- a/lib/libelf/gelf_newphdr.3 +++ b/lib/libelf/gelf_newphdr.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_newphdr.3,v 1.2 2019/06/11 18:30:31 schwarze Exp $ +.\" $Id: gelf_newphdr.3,v 1.3 2020/05/18 06:46:23 jsg Exp $ .\" -.Dd October 22, 2007 +.Dd June 12, 2019 .Dt GELF_NEWPHDR 3 .Os .Sh NAME diff --git a/lib/libelf/gelf_rel.c b/lib/libelf/gelf_rel.c index 9f56e5c148a..8058630165c 100644 --- a/lib/libelf/gelf_rel.c +++ b/lib/libelf/gelf_rel.c @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_rel.c,v 1.1 2019/02/01 05:27:38 jsg Exp $"); +ELFTC_VCSID("$Id: gelf_rel.c,v 1.2 2020/05/18 06:46:23 jsg Exp $"); GElf_Rel * gelf_getrel(Elf_Data *ed, int ndx, GElf_Rel *dst) @@ -67,9 +67,9 @@ gelf_getrel(Elf_Data *ed, int ndx, GElf_Rel *dst) return (NULL); } - msz = _libelf_msize(ELF_T_REL, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_REL, ec, e->e_version)) == 0) + return (NULL); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= d->d_data.d_size) { @@ -129,9 +129,9 @@ gelf_update_rel(Elf_Data *ed, int ndx, GElf_Rel *dr) return (0); } - msz = _libelf_msize(ELF_T_REL, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_REL, ec, e->e_version)) == 0) + return (0); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= d->d_data.d_size) { @@ -144,7 +144,7 @@ gelf_update_rel(Elf_Data *ed, int ndx, GElf_Rel *dr) LIBELF_COPY_U32(rel32, dr, r_offset); - if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0UL) || + if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0U) || ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) { LIBELF_SET_ERROR(RANGE, 0); return (0); diff --git a/lib/libelf/gelf_rela.c b/lib/libelf/gelf_rela.c index 59f8a66e541..90f066e7d3c 100644 --- a/lib/libelf/gelf_rela.c +++ b/lib/libelf/gelf_rela.c @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_rela.c,v 1.1 2019/02/01 05:27:38 jsg Exp $"); +ELFTC_VCSID("$Id: gelf_rela.c,v 1.2 2020/05/18 06:46:23 jsg Exp $"); GElf_Rela * gelf_getrela(Elf_Data *ed, int ndx, GElf_Rela *dst) @@ -67,9 +67,9 @@ gelf_getrela(Elf_Data *ed, int ndx, GElf_Rela *dst) return (NULL); } - msz = _libelf_msize(ELF_T_RELA, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_RELA, ec, e->e_version)) == 0) + return (NULL); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= d->d_data.d_size) { @@ -130,9 +130,9 @@ gelf_update_rela(Elf_Data *ed, int ndx, GElf_Rela *dr) return (0); } - msz = _libelf_msize(ELF_T_RELA, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_RELA, ec, e->e_version)) == 0) + return (0); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= d->d_data.d_size) { @@ -145,7 +145,7 @@ gelf_update_rela(Elf_Data *ed, int ndx, GElf_Rela *dr) LIBELF_COPY_U32(rela32, dr, r_offset); - if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0UL) || + if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0U) || ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) { LIBELF_SET_ERROR(RANGE, 0); return (0); diff --git a/lib/libelf/gelf_sym.c b/lib/libelf/gelf_sym.c index a8bc1a8ab65..1134e9320c7 100644 --- a/lib/libelf/gelf_sym.c +++ b/lib/libelf/gelf_sym.c @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_sym.c,v 1.1 2019/02/01 05:27:38 jsg Exp $"); +ELFTC_VCSID("$Id: gelf_sym.c,v 1.2 2020/05/18 06:46:23 jsg Exp $"); GElf_Sym * gelf_getsym(Elf_Data *ed, int ndx, GElf_Sym *dst) @@ -67,9 +67,9 @@ gelf_getsym(Elf_Data *ed, int ndx, GElf_Sym *dst) return (NULL); } - msz = _libelf_msize(ELF_T_SYM, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_SYM, ec, e->e_version)) == 0) + return (NULL); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= d->d_data.d_size) { @@ -129,9 +129,9 @@ gelf_update_sym(Elf_Data *ed, int ndx, GElf_Sym *gs) return (0); } - msz = _libelf_msize(ELF_T_SYM, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_SYM, ec, e->e_version)) == 0) + return (0); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= d->d_data.d_size) { diff --git a/lib/libelf/gelf_syminfo.c b/lib/libelf/gelf_syminfo.c index 529e861f66b..e8fa68871e2 100644 --- a/lib/libelf/gelf_syminfo.c +++ b/lib/libelf/gelf_syminfo.c @@ -29,7 +29,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_syminfo.c,v 1.1 2019/02/01 05:27:38 jsg Exp $"); +ELFTC_VCSID("$Id: gelf_syminfo.c,v 1.2 2020/05/18 06:46:23 jsg Exp $"); GElf_Syminfo * gelf_getsyminfo(Elf_Data *ed, int ndx, GElf_Syminfo *dst) @@ -65,9 +65,9 @@ gelf_getsyminfo(Elf_Data *ed, int ndx, GElf_Syminfo *dst) return (NULL); } - msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version)) == 0) + return (NULL); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= d->d_data.d_size) { @@ -126,9 +126,9 @@ gelf_update_syminfo(Elf_Data *ed, int ndx, GElf_Syminfo *gs) return (0); } - msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version)) == 0) + return (0); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= d->d_data.d_size) { diff --git a/lib/libelf/gelf_symshndx.c b/lib/libelf/gelf_symshndx.c index 3bf8386c70f..cde15dfb693 100644 --- a/lib/libelf/gelf_symshndx.c +++ b/lib/libelf/gelf_symshndx.c @@ -29,7 +29,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_symshndx.c,v 1.1 2019/02/01 05:27:38 jsg Exp $"); +ELFTC_VCSID("$Id: gelf_symshndx.c,v 1.2 2020/05/18 06:46:23 jsg Exp $"); GElf_Sym * gelf_getsymshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *dst, @@ -69,9 +69,9 @@ gelf_getsymshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *dst, return (NULL); } - msz = _libelf_msize(ELF_T_WORD, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_WORD, ec, e->e_version)) == 0) + return (NULL); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= id->d_size) { @@ -121,9 +121,9 @@ gelf_update_symshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *gs, return (0); } - msz = _libelf_msize(ELF_T_WORD, ec, e->e_version); + if ((msz = _libelf_msize(ELF_T_WORD, ec, e->e_version)) == 0) + return (0); - assert(msz > 0); assert(ndx >= 0); if (msz * (size_t) ndx >= id->d_size) { diff --git a/lib/libelf/libelf_allocate.c b/lib/libelf/libelf_allocate.c index 441dbcbab55..3585caaf8b4 100644 --- a/lib/libelf/libelf_allocate.c +++ b/lib/libelf/libelf_allocate.c @@ -36,7 +36,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_allocate.c,v 1.2 2019/03/19 02:31:35 jsg Exp $"); +ELFTC_VCSID("$Id: libelf_allocate.c,v 1.3 2020/05/18 06:46:23 jsg Exp $"); Elf * _libelf_allocate_elf(void) @@ -76,7 +76,7 @@ _libelf_init_elf(Elf *e, Elf_Kind kind) } } -Elf * +void _libelf_release_elf(Elf *e) { Elf_Arhdr *arh; @@ -114,8 +114,6 @@ _libelf_release_elf(Elf *e) } free(e); - - return (NULL); } struct _Libelf_Data * diff --git a/lib/libelf/libelf_convert.m4 b/lib/libelf/libelf_convert.m4 index af3282a2378..2d631caeadf 100644 --- a/lib/libelf/libelf_convert.m4 +++ b/lib/libelf/libelf_convert.m4 @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_convert.m4,v 1.2 2019/03/19 02:31:35 jsg Exp $"); +ELFTC_VCSID("$Id: libelf_convert.m4,v 1.3 2020/05/18 06:46:23 jsg Exp $"); /* WARNING: GENERATED FROM __file__. */ @@ -971,6 +971,7 @@ _libelf_cvt_NOTE_tom(unsigned char *dst, size_t dsz, unsigned char *src, if (count < sz || dsz < sz) /* Buffers are too small. */ return (0); + /* Copy the remainder of the note as-is. */ (void) memcpy(dst, src, sz); src += sz; @@ -1025,6 +1026,7 @@ _libelf_cvt_NOTE_tof(unsigned char *dst, size_t dsz, unsigned char *src, if (count < sz) sz = count; + /* Copy the remainder of the note as-is. */ (void) memcpy(dst, src, sz); src += sz; diff --git a/lib/libelf/libelf_data.c b/lib/libelf/libelf_data.c index cdc6a8566d5..d53f8749e0e 100644 --- a/lib/libelf/libelf_data.c +++ b/lib/libelf/libelf_data.c @@ -28,7 +28,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_data.c,v 1.1 2019/02/01 05:27:38 jsg Exp $"); +ELFTC_VCSID("$Id: libelf_data.c,v 1.2 2020/05/18 06:46:23 jsg Exp $"); int _libelf_xlate_shtype(uint32_t sht) @@ -89,8 +89,14 @@ _libelf_xlate_shtype(uint32_t sht) * OS, processor and user-defined section types) are * legal, but since we do not know anything more about * their semantics, we return a type of ELF_T_BYTE. + * + * The ELF specification uses 32 bit unsigned values for + * denoting section types, and defines SHT_HIUSER to be + * 0xFFFFFFFFUL (i.e., UINT32_MAX). Consequently, we only + * need to check that 'sht' is greater than or equal to + * SHT_LOOS. */ - if (sht >= SHT_LOOS && sht <= SHT_HIUSER) + if (sht >= SHT_LOOS) return (ELF_T_BYTE); /* diff --git a/lib/libelf/libelf_ehdr.c b/lib/libelf/libelf_ehdr.c index ef82463c852..7897abc055e 100644 --- a/lib/libelf/libelf_ehdr.c +++ b/lib/libelf/libelf_ehdr.c @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_ehdr.c,v 1.2 2019/03/19 02:31:35 jsg Exp $"); +ELFTC_VCSID("$Id: libelf_ehdr.c,v 1.3 2020/05/18 06:46:23 jsg Exp $"); /* * Retrieve counts for sections, phdrs and the section string table index @@ -148,9 +148,8 @@ _libelf_ehdr(Elf *e, int ec, int allocate) return (NULL); } - msz = _libelf_msize(ELF_T_EHDR, ec, EV_CURRENT); - - assert(msz > 0); + if ((msz = _libelf_msize(ELF_T_EHDR, ec, EV_CURRENT)) == 0) + return (NULL); if ((ehdr = calloc((size_t) 1, msz)) == NULL) { LIBELF_SET_ERROR(RESOURCE, 0); diff --git a/lib/libelf/libelf_memory.c b/lib/libelf/libelf_memory.c index 705bebe17b6..2ba2e9c79cd 100644 --- a/lib/libelf/libelf_memory.c +++ b/lib/libelf/libelf_memory.c @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_memory.c,v 1.2 2019/03/19 02:31:35 jsg Exp $"); +ELFTC_VCSID("$Id: libelf_memory.c,v 1.3 2020/05/18 06:46:23 jsg Exp $"); /* * Create an ELF descriptor for a memory image, optionally reporting @@ -78,7 +78,7 @@ _libelf_memory(unsigned char *image, size_t sz, int reporterror) if (error != ELF_E_NONE) { if (reporterror) { LIBELF_PRIVATE(error) = LIBELF_ERROR(error, 0); - (void) _libelf_release_elf(e); + _libelf_release_elf(e); return (NULL); } } else { diff --git a/lib/libelf/libelf_msize.m4 b/lib/libelf/libelf_msize.m4 index 1662ffd2e12..457eadf489a 100644 --- a/lib/libelf/libelf_msize.m4 +++ b/lib/libelf/libelf_msize.m4 @@ -30,7 +30,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_msize.m4,v 1.1 2019/02/01 05:27:38 jsg Exp $"); +ELFTC_VCSID("$Id: libelf_msize.m4,v 1.2 2020/05/18 06:46:23 jsg Exp $"); /* WARNING: GENERATED FROM __file__. */ @@ -87,6 +87,14 @@ static struct msize msize[ELF_T_NUM] = { MSIZES(ELF_TYPE_LIST) }; +/* + * Returns the memory size of the specified ELF type 't' of ELF + * class 'ec' and ELF version 'version'. + * + * If the specified combination of ELF type, class, and version is + * unsupported then a value of 0 will be returned and the appropriate + * library error code set. + */ size_t _libelf_msize(Elf_Type t, int elfclass, unsigned int version) { @@ -102,5 +110,10 @@ _libelf_msize(Elf_Type t, int elfclass, unsigned int version) sz = (elfclass == ELFCLASS32) ? msize[t].msz32 : msize[t].msz64; + if (sz == 0) { + LIBELF_SET_ERROR(UNIMPL, 0); + return (0); + } + return (sz); } diff --git a/lib/libelf/libelf_phdr.c b/lib/libelf/libelf_phdr.c index 797a66977f0..45b42bdc90d 100644 --- a/lib/libelf/libelf_phdr.c +++ b/lib/libelf/libelf_phdr.c @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_phdr.c,v 1.2 2019/03/19 02:31:35 jsg Exp $"); +ELFTC_VCSID("$Id: libelf_phdr.c,v 1.3 2020/05/18 06:46:23 jsg Exp $"); void * _libelf_getphdr(Elf *e, int ec) @@ -87,9 +87,8 @@ _libelf_getphdr(Elf *e, int ec) return (NULL); } - msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT); - - assert(msz > 0); + if ((msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT)) == 0) + return (NULL); if ((phdr = calloc(phnum, msz)) == NULL) { LIBELF_SET_ERROR(RESOURCE, 0); @@ -130,9 +129,8 @@ _libelf_newphdr(Elf *e, int ec, size_t count) assert(ec == ELFCLASS32 || ec == ELFCLASS64); assert(e->e_version == EV_CURRENT); - msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version); - - assert(msz > 0); + if ((msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version)) == 0) + return (NULL); newphdr = NULL; if (count > 0 && (newphdr = calloc(count, msz)) == NULL) { diff --git a/lib/libelf/libelf_xlate.c b/lib/libelf/libelf_xlate.c index 73291599554..4f22a1d1bae 100644 --- a/lib/libelf/libelf_xlate.c +++ b/lib/libelf/libelf_xlate.c @@ -30,7 +30,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_xlate.c,v 1.1 2019/02/01 05:27:38 jsg Exp $"); +ELFTC_VCSID("$Id: libelf_xlate.c,v 1.2 2020/05/18 06:46:23 jsg Exp $"); /* * Translate to/from the file representation of ELF objects. @@ -84,9 +84,8 @@ _libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding, (src->d_type, (size_t) 1, src->d_version)) == 0) return (NULL); - msz = _libelf_msize(src->d_type, elfclass, src->d_version); - - assert(msz > 0); + if ((msz = _libelf_msize(src->d_type, elfclass, src->d_version)) == 0) + return (NULL); if (src->d_size % (direction == ELF_TOMEMORY ? fsz : msz)) { LIBELF_SET_ERROR(DATA, 0); |