* [PATCH] bfd_mmap_local: Check offset and size
@ 2024-04-04 1:34 H.J. Lu
2024-04-04 7:20 ` Alan Modra
0 siblings, 1 reply; 3+ messages in thread
From: H.J. Lu @ 2024-04-04 1:34 UTC (permalink / raw)
To: binutils; +Cc: amodra
Update bfd_mmap_local to return NULL if offset + size > the file size.
* libbfd.c (bfd_mmap_local): Validate offset and size against
the file size.
---
bfd/libbfd.c | 13 +++++--------
1 file changed, 5 insertions(+), 8 deletions(-)
diff --git a/bfd/libbfd.c b/bfd/libbfd.c
index 34197b75b5e..400a5a47d2a 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 ((offset + rsize) > filesize)
{
- 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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] bfd_mmap_local: Check offset and size
2024-04-04 1:34 [PATCH] bfd_mmap_local: Check offset and size H.J. Lu
@ 2024-04-04 7:20 ` Alan Modra
2024-04-04 13:38 ` H.J. Lu
0 siblings, 1 reply; 3+ messages in thread
From: Alan Modra @ 2024-04-04 7:20 UTC (permalink / raw)
To: H.J. Lu; +Cc: binutils
On Wed, Apr 03, 2024 at 06:34:33PM -0700, H.J. Lu wrote:
> Update bfd_mmap_local to return NULL if offset + size > the file size.
>
> * libbfd.c (bfd_mmap_local): Validate offset and size against
> the file size.
> ---
> bfd/libbfd.c | 13 +++++--------
> 1 file changed, 5 insertions(+), 8 deletions(-)
>
> diff --git a/bfd/libbfd.c b/bfd/libbfd.c
> index 34197b75b5e..400a5a47d2a 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 ((offset + rsize) > filesize)
Doesn't need parens around "offset + rsize" here. Also, can this
expression ever overflow? If so it would be better written as
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] bfd_mmap_local: Check offset and size
2024-04-04 7:20 ` Alan Modra
@ 2024-04-04 13:38 ` H.J. Lu
0 siblings, 0 replies; 3+ messages in thread
From: H.J. Lu @ 2024-04-04 13:38 UTC (permalink / raw)
To: Alan Modra; +Cc: binutils
On Thu, Apr 4, 2024 at 12:20 AM Alan Modra <amodra@gmail.com> wrote:
>
> On Wed, Apr 03, 2024 at 06:34:33PM -0700, H.J. Lu wrote:
> > Update bfd_mmap_local to return NULL if offset + size > the file size.
> >
> > * libbfd.c (bfd_mmap_local): Validate offset and size against
> > the file size.
> > ---
> > bfd/libbfd.c | 13 +++++--------
> > 1 file changed, 5 insertions(+), 8 deletions(-)
> >
> > diff --git a/bfd/libbfd.c b/bfd/libbfd.c
> > index 34197b75b5e..400a5a47d2a 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 ((offset + rsize) > filesize)
>
> Doesn't need parens around "offset + rsize" here. Also, can this
> expression ever overflow? If so it would be better written as
>
> if (filesize < offset
> || filesize - offset < rsize)
The v2 patch:
https://sourceware.org/pipermail/binutils/2024-April/133372.html
--
H.J.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-04-04 13:39 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-04 1:34 [PATCH] bfd_mmap_local: Check offset and size H.J. Lu
2024-04-04 7:20 ` Alan Modra
2024-04-04 13:38 ` H.J. Lu
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).