public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] strip: Add check for elf_begin() result
@ 2024-04-05 18:53 Maks Mishin
  2024-04-05 19:04 ` Mark Wielaard
  0 siblings, 1 reply; 2+ messages in thread
From: Maks Mishin @ 2024-04-05 18:53 UTC (permalink / raw)
  To: elfutils-devel; +Cc: Maks Mishin

Return value of a function 'elf_begin' is dereferenced at strip.c:1166
without checking for NULL, but it is usually checked for this function.

Found by RASU JSC.

Signed-off-by: Maks Mishin <maks.mishinFZ@gmail.com>
---
 src/strip.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/strip.c b/src/strip.c
index 6436443d..ebab4866 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -1153,7 +1153,9 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
     {
       /* Also create an ELF descriptor for the debug file */
       debugelf = elf_begin (debug_fd, ELF_C_WRITE, NULL);
-      if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0))
+      ELF_CHECK (debugelf != NULL, _("cannot create ELF descriptor: %s"));
+
+	  if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0))
 	{
 	  error (0, 0, _("cannot create new ehdr for file '%s': %s"),
 		 debug_fname, elf_errmsg (-1));
-- 
2.30.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] strip: Add check for elf_begin() result
  2024-04-05 18:53 [PATCH] strip: Add check for elf_begin() result Maks Mishin
@ 2024-04-05 19:04 ` Mark Wielaard
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Wielaard @ 2024-04-05 19:04 UTC (permalink / raw)
  To: Maks Mishin; +Cc: elfutils-devel

Hi Maks,

On Fri, Apr 05, 2024 at 09:53:03PM +0300, Maks Mishin wrote:
> Return value of a function 'elf_begin' is dereferenced at strip.c:1166
> without checking for NULL, but it is usually checked for this function.

Please stop sending these "patches" till you actually understand the
code.

In this case if debugelf == NULL then gelf_newehdr will also return
NULL to indicate the earlier error.

This is a common pattern in the libelf code.

Thanks,

Mark

> Found by RASU JSC.
> 
> Signed-off-by: Maks Mishin <maks.mishinFZ@gmail.com>
> ---
>  src/strip.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/src/strip.c b/src/strip.c
> index 6436443d..ebab4866 100644
> --- a/src/strip.c
> +++ b/src/strip.c
> @@ -1153,7 +1153,9 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
>      {
>        /* Also create an ELF descriptor for the debug file */
>        debugelf = elf_begin (debug_fd, ELF_C_WRITE, NULL);
> -      if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0))
> +      ELF_CHECK (debugelf != NULL, _("cannot create ELF descriptor: %s"));
> +
> +	  if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0))
>  	{
>  	  error (0, 0, _("cannot create new ehdr for file '%s': %s"),
>  		 debug_fname, elf_errmsg (-1));
> -- 
> 2.30.2
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-04-05 19:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-05 18:53 [PATCH] strip: Add check for elf_begin() result Maks Mishin
2024-04-05 19:04 ` Mark Wielaard

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).