public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [PATCH] libelf: Also copy/convert partial datastructures in xlate functions
Date: Wed, 30 Mar 2022 00:26:42 +0200	[thread overview]
Message-ID: <20220329222642.436873-1-mark@klomp.org> (raw)

The generated xlate functions can only convert full datastructures,
dropping any trailing partial data on the floor. That means some of
the data might be undefined. Just copy over the trailing bytes as
is. That data isn't really usable. But at least it is defined data.

https://sourceware.org/bugzilla/show_bug.cgi?id=29000

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libelf/ChangeLog    |  5 +++++
 libelf/gelf_xlate.c | 10 ++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index 7fd6202b..299179cb 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,3 +1,8 @@
+2022-03-29  Mark Wielaard  <mark@klomp.org>
+
+	* gelf_xlate.c (START): Define and use sz variable.
+	(END): Use sz variable to decide whether to do a memmove.
+
 2022-03-24  Mark Wielaard  <mark@klomp.org>
 
 	* elf.h: Update from glibc.
diff --git a/libelf/gelf_xlate.c b/libelf/gelf_xlate.c
index b9e7fd65..6f8c57b7 100644
--- a/libelf/gelf_xlate.c
+++ b/libelf/gelf_xlate.c
@@ -1,5 +1,6 @@
 /* Transformation functions for ELF data types.
    Copyright (C) 1998,1999,2000,2002,2004,2005,2006,2007,2015 Red Hat, Inc.
+   Copyright (C) 2022 Mark J. Wielaard <mark@klomp.org>
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 1998.
 
@@ -138,9 +139,14 @@ union unaligned
 			    int encode __attribute__ ((unused)))	      \
   { ElfW2(Bits, Name) *tdest = (ElfW2(Bits, Name) *) dest;		      \
     ElfW2(Bits, Name) *tsrc = (ElfW2(Bits, Name) *) src;		      \
+    size_t sz = sizeof (ElfW2(Bits, Name));				      \
     size_t n;								      \
-    for (n = len / sizeof (ElfW2(Bits, Name)); n > 0; ++tdest, ++tsrc, --n) {
-#define END(Bits, Name) } }
+    for (n = len / sz; n > 0; ++tdest, ++tsrc, --n) {
+#define END(Bits, Name)							      \
+    }									      \
+    if (len % sz > 0) /* Cannot convert partial structures, just copy. */     \
+      memmove (dest, src, len % sz);					      \
+  }
 #define TYPE_EXTRA(Code)
 #define TYPE_XLATE(Code) Code
 #define TYPE_NAME(Type, Name) TYPE_NAME2 (Type, Name)
-- 
2.30.2


                 reply	other threads:[~2022-03-29 22:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220329222642.436873-1-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    /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).