]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libelf: Make sure conversion functions work on aligned data for type.
authorMark Wielaard <mjw@redhat.com>
Wed, 3 Jun 2015 16:50:40 +0000 (18:50 +0200)
committerMark Wielaard <mjw@redhat.com>
Tue, 9 Jun 2015 20:47:54 +0000 (22:47 +0200)
The gelf_xlate conversion functions work on properly aligned ELF data
types.  If elf_get data needs to do conversion and ! ALLOW_UNALIGNED
and the rawdata_base isn't aligned properly for the section type, then
provide an aligned copy of the data.

Found with --enable-sanitize-undefined  in run-test-archive64.sh on x86_64.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
libelf/ChangeLog
libelf/elf_getdata.c

index 772eb52d64b91f00d466416ab2c787b4a1071d1d..25f673db32f37cb0cf0b759416f120bb0b23178d 100644 (file)
@@ -1,3 +1,8 @@
+2015-06-02  Mark Wielaard  <mjw@redhat.com>
+
+       * elf_getdata.c (convert_data): Make sure source data is properly
+       aligned for type before calling actual conversion function.
+
 2015-06-04  Mark Wielaard  <mjw@redhat.com>
 
        * elf_begin.c (get_shnum): Check alignment of Shdr, not Ehdr before
index 8567da1e8300300ba806710cc30b90b170ff716c..1a4981efb0a28aca7d8fde8bd84b5d16ccd52a4b 100644 (file)
@@ -1,5 +1,5 @@
 /* Return the next data element from the section after possibly converting it.
-   Copyright (C) 1998-2005, 2006, 2007 Red Hat, Inc.
+   Copyright (C) 1998-2005, 2006, 2007, 2015 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 1998.
 
@@ -144,6 +144,25 @@ convert_data (Elf_Scn *scn, int version __attribute__ ((unused)), int eclass,
          return;
        }
 
+      /* Make sure the source is correctly aligned for the conversion
+        function to directly access the data elements.  */
+      char *rawdata_source;
+      if (ALLOW_UNALIGNED ||
+         ((((size_t) (char *) scn->rawdata_base)) & (align - 1)) == 0)
+       rawdata_source = scn->rawdata_base;
+      else
+       {
+         rawdata_source = (char *) malloc (size);
+         if (rawdata_source == NULL)
+           {
+             __libelf_seterrno (ELF_E_NOMEM);
+             return;
+           }
+
+         /* The copy will be appropriately aligned for direct access.  */
+         memcpy (rawdata_source, scn->rawdata_base, size);
+       }
+
       /* Get the conversion function.  */
 #if EV_NUM != 2
       fp = __elf_xfctstom[version - 1][__libelf_version - 1][eclass - 1][type];
@@ -151,7 +170,10 @@ convert_data (Elf_Scn *scn, int version __attribute__ ((unused)), int eclass,
       fp = __elf_xfctstom[0][0][eclass - 1][type];
 #endif
 
-      fp (scn->data_base, scn->rawdata_base, size, 0);
+      fp (scn->data_base, rawdata_source, size, 0);
+
+      if (rawdata_source != scn->rawdata_base)
+       free (rawdata_source);
     }
 
   scn->data_list.data.d.d_buf = scn->data_base;