* [PATCH v2] elf: Earlier missing dynamic segment check in _dl_map_object_from_fd
@ 2021-11-05 18:26 Florian Weimer
2021-11-05 18:30 ` H.J. Lu
0 siblings, 1 reply; 2+ messages in thread
From: Florian Weimer @ 2021-11-05 18:26 UTC (permalink / raw)
To: libc-alpha
Separated debuginfo files have PT_DYNAMIC with p_filesz == 0. We
need to check for that before the _dl_map_segments call because
that could attempt to write to mappings that extend beyond the end
of the file, resulting in SIGBUS.
---
v2: Mark the result of the || expression as unlikely, and not just the
first branch.
elf/dl-load.c | 22 ++++++++++++----------
1 file changed, 12 insertions(+), 10 deletions(-)
diff --git a/elf/dl-load.c b/elf/dl-load.c
index a1f1682188..9f4fa9617d 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -1135,6 +1135,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
struct loadcmd loadcmds[l->l_phnum];
size_t nloadcmds = 0;
bool has_holes = false;
+ bool empty_dynamic = false;
/* The struct is initialized to zero so this is not necessary:
l->l_ld = 0;
@@ -1147,7 +1148,9 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
segments are mapped in. We record the addresses it says
verbatim, and later correct for the run-time load address. */
case PT_DYNAMIC:
- if (ph->p_filesz)
+ if (ph->p_filesz == 0)
+ empty_dynamic = true; /* Usually separate debuginfo. */
+ else
{
/* Debuginfo only files from "objcopy --only-keep-debug"
contain a PT_DYNAMIC segment with p_filesz == 0. Skip
@@ -1270,6 +1273,13 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
goto lose;
}
+ /* This check recognizes most separate debuginfo files. */
+ if (__glibc_unlikely ((l->l_ld == 0 && type == ET_DYN) || empty_dynamic))
+ {
+ errstring = N_("object file has no dynamic section");
+ goto lose;
+ }
+
/* Length of the sections to be loaded. */
maplength = loadcmds[nloadcmds - 1].allocend - loadcmds[0].mapstart;
@@ -1287,15 +1297,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
}
}
- if (l->l_ld == 0)
- {
- if (__glibc_unlikely (type == ET_DYN))
- {
- errstring = N_("object file has no dynamic section");
- goto lose;
- }
- }
- else
+ if (l->l_ld != 0)
l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
elf_get_dynamic_info (l, false, false);
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v2] elf: Earlier missing dynamic segment check in _dl_map_object_from_fd
2021-11-05 18:26 [PATCH v2] elf: Earlier missing dynamic segment check in _dl_map_object_from_fd Florian Weimer
@ 2021-11-05 18:30 ` H.J. Lu
0 siblings, 0 replies; 2+ messages in thread
From: H.J. Lu @ 2021-11-05 18:30 UTC (permalink / raw)
To: Florian Weimer; +Cc: GNU C Library
On Fri, Nov 5, 2021 at 11:27 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> Separated debuginfo files have PT_DYNAMIC with p_filesz == 0. We
> need to check for that before the _dl_map_segments call because
> that could attempt to write to mappings that extend beyond the end
> of the file, resulting in SIGBUS.
>
> ---
> v2: Mark the result of the || expression as unlikely, and not just the
> first branch.
> elf/dl-load.c | 22 ++++++++++++----------
> 1 file changed, 12 insertions(+), 10 deletions(-)
>
> diff --git a/elf/dl-load.c b/elf/dl-load.c
> index a1f1682188..9f4fa9617d 100644
> --- a/elf/dl-load.c
> +++ b/elf/dl-load.c
> @@ -1135,6 +1135,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
> struct loadcmd loadcmds[l->l_phnum];
> size_t nloadcmds = 0;
> bool has_holes = false;
> + bool empty_dynamic = false;
>
> /* The struct is initialized to zero so this is not necessary:
> l->l_ld = 0;
> @@ -1147,7 +1148,9 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
> segments are mapped in. We record the addresses it says
> verbatim, and later correct for the run-time load address. */
> case PT_DYNAMIC:
> - if (ph->p_filesz)
> + if (ph->p_filesz == 0)
> + empty_dynamic = true; /* Usually separate debuginfo. */
> + else
> {
> /* Debuginfo only files from "objcopy --only-keep-debug"
> contain a PT_DYNAMIC segment with p_filesz == 0. Skip
> @@ -1270,6 +1273,13 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
> goto lose;
> }
>
> + /* This check recognizes most separate debuginfo files. */
> + if (__glibc_unlikely ((l->l_ld == 0 && type == ET_DYN) || empty_dynamic))
> + {
> + errstring = N_("object file has no dynamic section");
> + goto lose;
> + }
> +
> /* Length of the sections to be loaded. */
> maplength = loadcmds[nloadcmds - 1].allocend - loadcmds[0].mapstart;
>
> @@ -1287,15 +1297,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
> }
> }
>
> - if (l->l_ld == 0)
> - {
> - if (__glibc_unlikely (type == ET_DYN))
> - {
> - errstring = N_("object file has no dynamic section");
> - goto lose;
> - }
> - }
> - else
> + if (l->l_ld != 0)
> l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
>
> elf_get_dynamic_info (l, false, false);
>
LGTM.
Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
Thanks.
--
H.J.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-11-05 18:31 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-05 18:26 [PATCH v2] elf: Earlier missing dynamic segment check in _dl_map_object_from_fd Florian Weimer
2021-11-05 18:30 ` 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).