From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by sourceware.org (Postfix) with ESMTPS id 14D063858C1F for ; Tue, 22 Nov 2022 21:57:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 14D063858C1F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-x52f.google.com with SMTP id q71so15167000pgq.8 for ; Tue, 22 Nov 2022 13:57:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=3V+73MkwYbDQUonBhguoB595gtaggiTPzY6I6cFEyDk=; b=RzxpPADysGHPrNbmpm42Ns0qsRZUgV37Fb/aqnJvT2VoWxzONraQR8qN2N0o7QcIjM Rp9OPBdQxWKptKihEDBmpuhEOlQGBkfpk/dnT6r26q3cc46RWijLIymNnUc0/70R9vxt fmIQlBawpNMSAmkcO3ECRWNJZ7CwrboZpW3wDIvPCDZcdxHjciQBROq4hBuV58CoZHp4 J93tnTL5xe3Bg6a36Ph7o54ZeDKDTsh7GCkajh7uTAHv0BA5ElESlocTFN/X2p/dzZsB X7e2YEIUsOwkC9+xpkr/BVfqgFoAuWDNDGoQx7TTd8MdmIGM3f3JZNTWrPCmAd5XE8QW FG9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=3V+73MkwYbDQUonBhguoB595gtaggiTPzY6I6cFEyDk=; b=YjEngCognk3X9J8fLTD/9LUCesmnh6Yi+N0nRWkOyHjXqB1weiLTjKMTh4RCTrjt1M wTMkuD7Idw4I/TjFp9Vvs3aN4HdJ4XJ3DoG/wPZxFrBTcpooEK0NAvYTAimJyy0Rf2iN /gvp6cr0SSfyvIm1qR290mA1e13XyILoCJGk2A1+VfIzRj1erpsIQaYvijpKAVJOkkf4 HsDKTRfY3yDSuDDADUstcW1kvwP/FdD79u//MiHmpiyBz9Ho/avW+/jMtjbMIRqrvxcf q9VOTW3kKG96rPZwSn6A1HLQ9WLuKjRhzuDkGxMbId1fzYIr4pIS3hmfCu8jFAej7Y8L TNhQ== X-Gm-Message-State: ANoB5plHNolxsDAywiHt0lM247lbhe0faoOAbO4sPD+vfYs6Liq12sjS DWILsMDcqYCJNr8gABFNf+fo7WKL+Tw= X-Google-Smtp-Source: AA0mqf7BSOUZ1lEwvbwHf2KnulKkIe8KtqS5R8k31/+gGJsx9A7Iy/s75DGslAMow1tWDh8g36SO/A== X-Received: by 2002:aa7:8c19:0:b0:573:620a:3b1c with SMTP id c25-20020aa78c19000000b00573620a3b1cmr6451977pfd.50.1669154230081; Tue, 22 Nov 2022 13:57:10 -0800 (PST) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id l7-20020a170902f68700b00177fb862a87sm12738752plg.20.2022.11.22.13.57.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Nov 2022 13:57:09 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 17EAD1140DFC; Wed, 23 Nov 2022 08:27:07 +1030 (ACDT) Date: Wed, 23 Nov 2022 08:27:07 +1030 From: Alan Modra To: Brett Werling Cc: binutils@sourceware.org Subject: Re: [PATCH] readelf: use fseeko64 or fseeko if possible Message-ID: References: <20221114150348.112815-1-bwerl.dev@gmail.com> <20221117143419.19571-1-bwerl.dev@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221117143419.19571-1-bwerl.dev@gmail.com> X-Spam-Status: No, score=-3036.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Replace the macros with a small wrapper function that verifies the fseek offset arg isn't overlarge. * readelf.c (FSEEK_FUNC): Delete, replace uses with.. (fseek64): ..this new function. (process_program_headers): Don't cast p_offset to long. 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 -#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 }; +static int +fseek64 (FILE *stream, int64_t offset, int whence) +{ +#if defined (HAVE_FSEEKO64) + off64_t o = offset; + if (o != offset) + { + errno = EINVAL; + return -1; + } + return fseeko64 (stream, o, whence); +#elif defined (HAVE_FSEEKO) + off_t o = offset; + if (o != offset) + { + errno = EINVAL; + return -1; + } + return fseeko (stream, o, whence); +#else + long o = offset; + if (o != offset) + { + errno = 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; } - 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 segment\n")); if (segment->p_offset >= filedata->file_size || segment->p_filesz > filedata->file_size - segment->p_offset || segment->p_filesz - 1 >= (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] == ELFCLASS64) hash_ent_size = 8; - 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; - 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 += bitmaskwords * 8; - 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) maxchain -= filedata->gnusymidx; - 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) == 0); - 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) 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 = false; } - if (FSEEK_FUNC (filedata->handle, current_pos, SEEK_SET) != 0) + if (fseek64 (filedata->handle, current_pos, SEEK_SET) != 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; /* Read the next archive header. */ - if (FSEEK_FUNC (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0) + if (fseek64 (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 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) /* 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) != 0) + if (fseek64 (nested_arch.file, filedata->archive_file_offset, + SEEK_SET) != 0) { error (_("%s: failed to seek to archive member.\n"), nested_arch.file_name); -- Alan Modra Australia Development Lab, IBM