From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id B63AE3858422; Tue, 22 Nov 2022 21:59:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B63AE3858422 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Alan Modra To: bfd-cvs@sourceware.org Subject: [binutils-gdb] Re: readelf: use fseeko64 or fseeko if possible X-Act-Checkin: binutils-gdb X-Git-Author: Alan Modra X-Git-Refname: refs/heads/master X-Git-Oldrev: f3f7ecc942f3844559142b933aa40b5ef75e3d5e X-Git-Newrev: 63cf857e24be8e657dd2d3197da5c01a0f590d27 Message-Id: <20221122215930.B63AE3858422@sourceware.org> Date: Tue, 22 Nov 2022 21:59:30 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Nov 2022 21:59:30 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D63cf857e24be= 8e657dd2d3197da5c01a0f590d27 commit 63cf857e24be8e657dd2d3197da5c01a0f590d27 Author: Alan Modra Date: Wed Nov 23 07:45:49 2022 +1030 Re: readelf: use fseeko64 or fseeko if possible =20 Replace the macros with a small wrapper function that verifies the fseek offset arg isn't overlarge. =20 * readelf.c (FSEEK_FUNC): Delete, replace uses with.. (fseek64): ..this new function. (process_program_headers): Don't cast p_offset to long. Diff: --- binutils/readelf.c | 125 +++++++++++++++++++++++++++++++------------------= ---- 1 file changed, 74 insertions(+), 51 deletions(-) diff --git a/binutils/readelf.c b/binutils/readelf.c index 044022ec334..291bc13e0d0 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -178,14 +178,6 @@ #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER)) #endif =20 -#if defined (HAVE_FSEEKO64) -#define FSEEK_FUNC fseeko64 -#elif defined (HAVE_FSEEKO) -#define FSEEK_FUNC fseeko -#else -#define FSEEK_FUNC fseek -#endif - typedef struct elf_section_list { Elf_Internal_Shdr * hdr; @@ -373,6 +365,36 @@ enum versioned_symbol_info symbol_public }; =20 +static int +fseek64 (FILE *stream, int64_t offset, int whence) +{ +#if defined (HAVE_FSEEKO64) + off64_t o =3D offset; + if (o !=3D offset) + { + errno =3D EINVAL; + return -1; + } + return fseeko64 (stream, o, whence); +#elif defined (HAVE_FSEEKO) + off_t o =3D offset; + if (o !=3D offset) + { + errno =3D EINVAL; + return -1; + } + return fseeko (stream, o, whence); +#else + long o =3D offset; + if (o !=3D offset) + { + errno =3D EINVAL; + return -1; + } + return fseek (stream, o, whence); +#endif +} + static const char * get_symbol_version_string (Filedata *, bool, const char *, unsigned long, unsigned, Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *); @@ -490,8 +512,8 @@ get_data (void *var, return NULL; } =20 - if (FSEEK_FUNC (filedata->handle, filedata->archive_file_offset + offset, - SEEK_SET)) + if (fseek64 (filedata->handle, filedata->archive_file_offset + offset, + SEEK_SET)) { if (reason) error (_("Unable to seek to 0x%lx for %s\n"), @@ -6291,9 +6313,9 @@ the .dynamic section is not the same as the dynamic s= egment\n")); if (segment->p_offset >=3D filedata->file_size || segment->p_filesz > filedata->file_size - segment->p_offset || segment->p_filesz - 1 >=3D (size_t) -2 - || FSEEK_FUNC (filedata->handle, - filedata->archive_file_offset + (long) segment->p_offset, - SEEK_SET)) + || fseek64 (filedata->handle, + filedata->archive_file_offset + segment->p_offset, + SEEK_SET)) error (_("Unable to find program interpreter name\n")); else { @@ -11064,11 +11086,12 @@ get_num_dynamic_syms (Filedata * filedata) && filedata->file_header.e_ident[EI_CLASS] =3D=3D ELFCLASS64) hash_ent_size =3D 8; =20 - if (FSEEK_FUNC (filedata->handle, - (filedata->archive_file_offset - + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH], - sizeof nb + sizeof nc)), - SEEK_SET)) + if (fseek64 (filedata->handle, + (filedata->archive_file_offset + + offset_from_vma (filedata, + filedata->dynamic_info[DT_HASH], + sizeof nb + sizeof nc)), + SEEK_SET)) { error (_("Unable to seek to start of dynamic information\n")); goto no_hash; @@ -11117,12 +11140,12 @@ get_num_dynamic_syms (Filedata * filedata) uint64_t buckets_vma; unsigned long hn; =20 - if (FSEEK_FUNC (filedata->handle, - (filedata->archive_file_offset - + offset_from_vma (filedata, - filedata->dynamic_info_DT_GNU_HASH, - sizeof nb)), - SEEK_SET)) + if (fseek64 (filedata->handle, + (filedata->archive_file_offset + + offset_from_vma (filedata, + filedata->dynamic_info_DT_GNU_HASH, + sizeof nb)), + SEEK_SET)) { error (_("Unable to seek to start of dynamic information\n")); goto no_gnu_hash; @@ -11143,10 +11166,10 @@ get_num_dynamic_syms (Filedata * filedata) else buckets_vma +=3D bitmaskwords * 8; =20 - if (FSEEK_FUNC (filedata->handle, - (filedata->archive_file_offset - + offset_from_vma (filedata, buckets_vma, 4)), - SEEK_SET)) + if (fseek64 (filedata->handle, + (filedata->archive_file_offset + + offset_from_vma (filedata, buckets_vma, 4)), + SEEK_SET)) { error (_("Unable to seek to start of dynamic information\n")); goto no_gnu_hash; @@ -11173,13 +11196,13 @@ get_num_dynamic_syms (Filedata * filedata) =20 maxchain -=3D filedata->gnusymidx; =20 - if (FSEEK_FUNC (filedata->handle, - (filedata->archive_file_offset - + offset_from_vma (filedata, - buckets_vma + 4 * (filedata->ngnubuckets - + maxchain), - 4)), - SEEK_SET)) + if (fseek64 (filedata->handle, + (filedata->archive_file_offset + + offset_from_vma (filedata, + buckets_vma + 4 * (filedata->ngnubuckets + + maxchain), + 4)), + SEEK_SET)) { error (_("Unable to seek to start of dynamic information\n")); goto no_gnu_hash; @@ -11200,12 +11223,12 @@ get_num_dynamic_syms (Filedata * filedata) } while ((byte_get (nb, 4) & 1) =3D=3D 0); =20 - if (FSEEK_FUNC (filedata->handle, - (filedata->archive_file_offset - + offset_from_vma (filedata, (buckets_vma - + 4 * filedata->ngnubuckets), - 4)), - SEEK_SET)) + if (fseek64 (filedata->handle, + (filedata->archive_file_offset + + offset_from_vma (filedata, (buckets_vma + + 4 * filedata->ngnubuckets), + 4)), + SEEK_SET)) { error (_("Unable to seek to start of dynamic information\n")); goto no_gnu_hash; @@ -11219,12 +11242,12 @@ get_num_dynamic_syms (Filedata * filedata) =20 if (filedata->dynamic_info_DT_MIPS_XHASH) { - if (FSEEK_FUNC (filedata->handle, - (filedata->archive_file_offset - + offset_from_vma (filedata, (buckets_vma - + 4 * (filedata->ngnubuckets - + maxchain)), 4)), - SEEK_SET)) + if (fseek64 (filedata->handle, + (filedata->archive_file_offset + + offset_from_vma (filedata, (buckets_vma + + 4 * (filedata->ngnubuckets + + maxchain)), 4)), + SEEK_SET)) { error (_("Unable to seek to start of dynamic information\n")); goto no_gnu_hash; @@ -22618,7 +22641,7 @@ process_archive (Filedata * filedata, bool is_thin_= archive) ret =3D false; } =20 - if (FSEEK_FUNC (filedata->handle, current_pos, SEEK_SET) !=3D 0) + if (fseek64 (filedata->handle, current_pos, SEEK_SET) !=3D 0) { error (_("%s: failed to seek back to start of object files " "in the archive\n"), @@ -22645,7 +22668,7 @@ process_archive (Filedata * filedata, bool is_thin_= archive) char * qualified_name; =20 /* Read the next archive header. */ - if (FSEEK_FUNC (filedata->handle, arch.next_arhdr_offset, SEEK_SET) = !=3D 0) + if (fseek64 (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != =3D 0) { error (_("%s: failed to seek to next archive header\n"), arch.file_name); @@ -22755,8 +22778,8 @@ process_archive (Filedata * filedata, bool is_thin_= archive) =20 /* The nested archive file will have been opened and setup by get_archive_member_name. */ - if (FSEEK_FUNC (nested_arch.file, filedata->archive_file_offset, - SEEK_SET) !=3D 0) + if (fseek64 (nested_arch.file, filedata->archive_file_offset, + SEEK_SET) !=3D 0) { error (_("%s: failed to seek to archive member.\n"), nested_arch.file_name);