]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
bfd_mmap_local: Check offset and size
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 4 Apr 2024 13:37:18 +0000 (06:37 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 4 Apr 2024 22:35:48 +0000 (15:35 -0700)
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.

bfd/libbfd.c

index 5126ee207a8ea7d3a61df7e29db0d116e41fb9b1..86366e496c5c399397c17a781152ea046ff144cb 100644 (file)
@@ -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;