public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] libdwfl: Call xlatetom on aligned buffers in dwfl_link_map_report
@ 2021-12-24  0:49 Mark Wielaard
  2022-01-03 23:39 ` Mark Wielaard
  0 siblings, 1 reply; 2+ messages in thread
From: Mark Wielaard @ 2021-12-24  0:49 UTC (permalink / raw)
  To: elfutils-devel; +Cc: Mark Wielaard

Make sure that when calling xlatetom for Phdrs and Dyns in
dwfl_link_map_report the input buffer is correctly aligned by calling
memcpy and setting in.d_buf to out.d_buf.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libdwfl/ChangeLog  |  5 +++++
 libdwfl/link_map.c | 19 ++++++++++++++++++-
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 49a35e41..73d8613c 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,8 @@
+2021-12-23  Mark Wielaard  <mark@klomp.org>
+
+	* link_map.c (dwfl_link_map_report): Call memcpy and set in.d_buf to
+	out.d_buf before calling xlatetom for unaligned buffers.
+
 2021-12-23  Mark Wielaard  <mark@klomp.org>
 
 	* core-file.c (dwfl_elf_phdr_memory_callback): Check start <
diff --git a/libdwfl/link_map.c b/libdwfl/link_map.c
index c4f79f11..f57c5585 100644
--- a/libdwfl/link_map.c
+++ b/libdwfl/link_map.c
@@ -922,11 +922,20 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
 		      return false;
 		    }
 		}
+	      bool is32 = (elfclass == ELFCLASS32);
+	      size_t phdr_align = (is32
+				   ? __alignof__ (Elf32_Phdr)
+				   : __alignof__ (Elf64_Phdr));
+	      if (!in_from_exec
+		  && ((uintptr_t) in.d_buf & (phdr_align - 1)) != 0)
+		{
+		  memcpy (out.d_buf, in.d_buf, in.d_size);
+		  in.d_buf = out.d_buf;
+		}
 	      if (likely ((elfclass == ELFCLASS32
 			   ? elf32_xlatetom : elf64_xlatetom)
 			  (&out, &in, elfdata) != NULL))
 		{
-		  bool is32 = (elfclass == ELFCLASS32);
 		  for (size_t i = 0; i < phnum; ++i)
 		    {
 		      GElf_Word type = (is32
@@ -1044,6 +1053,14 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
 		};
 	      if (in.d_size > out.d_size)
 		in.d_size = out.d_size;
+	      size_t dyn_align = (elfclass == ELFCLASS32
+			          ? __alignof__ (Elf32_Dyn)
+				  : __alignof__ (Elf64_Dyn));
+	      if (((uintptr_t) in.d_buf & (dyn_align - 1)) != 0)
+		{
+		  memcpy (out.d_buf, in.d_buf, in.d_size);
+		  in.d_buf = out.d_buf;
+		}
 	      if (likely ((elfclass == ELFCLASS32
 			   ? elf32_xlatetom : elf64_xlatetom)
 			  (&out, &in, elfdata) != NULL))
-- 
2.30.2


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

* Re: [PATCH] libdwfl: Call xlatetom on aligned buffers in dwfl_link_map_report
  2021-12-24  0:49 [PATCH] libdwfl: Call xlatetom on aligned buffers in dwfl_link_map_report Mark Wielaard
@ 2022-01-03 23:39 ` Mark Wielaard
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Wielaard @ 2022-01-03 23:39 UTC (permalink / raw)
  To: elfutils-devel

On Fri, Dec 24, 2021 at 01:49:54AM +0100, Mark Wielaard wrote:
> Make sure that when calling xlatetom for Phdrs and Dyns in
> dwfl_link_map_report the input buffer is correctly aligned by calling
> memcpy and setting in.d_buf to out.d_buf.
> 
> https://sourceware.org/bugzilla/show_bug.cgi?id=28720

Pushed


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

end of thread, other threads:[~2022-01-03 23:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-24  0:49 [PATCH] libdwfl: Call xlatetom on aligned buffers in dwfl_link_map_report Mark Wielaard
2022-01-03 23:39 ` 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).