public inbox for binutils-cvs@sourceware.org
 help / color / mirror / Atom feed
From: H.J. Lu <hjl@sourceware.org>
To: binutils-cvs@sourceware.org
Subject: [binutils-gdb] bfd_mmap_local: Check offset and size
Date: Thu,  4 Apr 2024 22:36:23 +0000 (GMT)	[thread overview]
Message-ID: <20240404223623.8F6003858C33@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=0ffdc37b98964477e70bfd63ca28f374193dcdb2

commit 0ffdc37b98964477e70bfd63ca28f374193dcdb2
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Apr 4 06:37:18 2024 -0700

    bfd_mmap_local: Check offset and size
    
    Update bfd_mmap_local to return NULL if filesize < offset or filesize -
    offset < rsize.
    
            * libbfd.c (bfd_mmap_local): Validate offset and size against
            the file size.

Diff:
---
 bfd/libbfd.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/bfd/libbfd.c b/bfd/libbfd.c
index 5126ee207a8..86366e496c5 100644
--- a/bfd/libbfd.c
+++ b/bfd/libbfd.c
@@ -1072,18 +1072,15 @@ static void *
 bfd_mmap_local (bfd *abfd, size_t rsize, int prot, void **map_addr,
 		size_t *map_size)
 {
-  if (!_bfd_constant_p (rsize))
+  ufile_ptr filesize = bfd_get_file_size (abfd);
+  ufile_ptr offset = bfd_tell (abfd);
+  if (filesize < offset || filesize - offset < rsize)
     {
-      ufile_ptr filesize = bfd_get_file_size (abfd);
-      if (filesize != 0 && rsize > filesize)
-	{
-	  bfd_set_error (bfd_error_file_truncated);
-	  return NULL;
-	}
+      bfd_set_error (bfd_error_file_truncated);
+      return NULL;
     }
 
   void *mem;
-  ufile_ptr offset = bfd_tell (abfd);
   mem = bfd_mmap (abfd, NULL, rsize, prot, MAP_PRIVATE, offset,
 		  map_addr, map_size);
   return mem;

                 reply	other threads:[~2024-04-04 22:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240404223623.8F6003858C33@sourceware.org \
    --to=hjl@sourceware.org \
    --cc=binutils-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).