public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: Evgeny Vereshchagin <evverx@gmail.com>,
	Aleksei Vetrov <vvvvvv@google.com>,
	Mark Wielaard <mark@klomp.org>
Subject: [PATCH 1/2] libelf: memmove any extra bytes left by elf_cvt_gnuhash conversion
Date: Mon, 20 Feb 2023 16:55:17 +0100	[thread overview]
Message-ID: <20230220155518.86598-2-mark@klomp.org> (raw)
In-Reply-To: <20230220155518.86598-1-mark@klomp.org>

Otherwise some undefined bytes might be left in the buffer. Now they
might still be not useful, but at least they are as defined in the
file.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 ChangeLog              |  4 ++++
 libelf/gnuhash_xlate.h | 12 ++++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index d99d837d..55787f64 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2023-02-20  Mark Wielaard  <mark@klomp.org>
+
+	* gnuhash_xlate.h (elf_cvt_gnuhash): memmove any left over bytes.
+
 2023-02-15  Mark Wielaard  <mark@klomp.org>
 
 	* configure.ac: Error out when demangler is enabled, but
diff --git a/libelf/gnuhash_xlate.h b/libelf/gnuhash_xlate.h
index 6faf1136..3a00ae0a 100644
--- a/libelf/gnuhash_xlate.h
+++ b/libelf/gnuhash_xlate.h
@@ -1,5 +1,6 @@
 /* Conversion functions for versioning information.
    Copyright (C) 2006, 2007 Red Hat, Inc.
+   Copyright (C) 2023, Mark J. Wielaard <mark@klomp.org>
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2006.
 
@@ -36,6 +37,7 @@
 static void
 elf_cvt_gnuhash (void *dest, const void *src, size_t len, int encode)
 {
+  size_t size = len;
   /* The GNU hash table format on 64 bit machines mixes 32 bit and 64 bit
      words.  We must detangle them here.   */
   Elf32_Word *dest32 = dest;
@@ -45,7 +47,7 @@ elf_cvt_gnuhash (void *dest, const void *src, size_t len, int encode)
   for (unsigned int cnt = 0; cnt < 4; ++cnt)
     {
       if (len < 4)
-	return;
+	goto done;
       dest32[cnt] = bswap_32 (src32[cnt]);
       len -= 4;
     }
@@ -58,7 +60,7 @@ elf_cvt_gnuhash (void *dest, const void *src, size_t len, int encode)
   for (unsigned int cnt = 0; cnt < bitmask_words; ++cnt)
     {
       if (len < 8)
-	return;
+	goto done;
       dest64[cnt] = bswap_64 (src64[cnt]);
       len -= 8;
     }
@@ -71,4 +73,10 @@ elf_cvt_gnuhash (void *dest, const void *src, size_t len, int encode)
       *dest32++ = bswap_32 (*src32++);
       len -= 4;
     }
+
+ done:
+  /* If there are any bytes left, we weren't able to convert the
+     partial structures, just copy them over. */
+  if (len > 0)
+    memmove (dest + size - len, src + size - len, len);
 }
-- 
2.39.2


  reply	other threads:[~2023-02-20 15:55 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-20 15:55 Fix some .debug checking and gnu hash xlate logic Mark Wielaard
2023-02-20 15:55 ` Mark Wielaard [this message]
2023-02-20 15:55 ` [PATCH 2/2] libdw: Use elf_rawdata when checking .debug section Mark Wielaard
2023-02-20 16:02   ` Aleksei Vetrov
2023-02-21  2:28     ` Evgeny Vereshchagin
2023-02-21 11:24       ` Mark Wielaard
2023-02-23 10:20         ` Mark Wielaard
2023-02-21 16:40       ` Aleksei Vetrov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230220155518.86598-2-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    --cc=evverx@gmail.com \
    --cc=vvvvvv@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).