[PATCH v2] bfd_mmap_local: Check offset and size

Alan Modra amodra@gmail.com
Thu Apr 4 22:25:04 GMT 2024


On Thu, Apr 04, 2024 at 06:37:18AM -0700, H.J. Lu wrote:
> 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.

OK.

>  bfd/libbfd.c | 13 +++++--------
>  1 file changed, 5 insertions(+), 8 deletions(-)
> 
> diff --git a/bfd/libbfd.c b/bfd/libbfd.c
> index 34197b75b5e..41f89d5fc78 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;
> -- 
> 2.44.0

-- 
Alan Modra
Australia Development Lab, IBM


More information about the Binutils mailing list