about summary refs log tree commit diff
path: root/elf/readelflib.c
diff options
context:
space:
mode:
authorH.J. Lu <hjl.tools@gmail.com>2017-11-28 09:56:47 -0800
committerH.J. Lu <hjl.tools@gmail.com>2017-11-28 09:57:00 -0800
commit8d81ce0c6d6ca923571e8b2bac132929f9a02973 (patch)
tree084029fbe3af20f86d408a1eb07c424357d48d51 /elf/readelflib.c
parent313ba4630f5f891af22bea9bdf9d9f3c88e49aee (diff)
downloadglibc-8d81ce0c6d6ca923571e8b2bac132929f9a02973.tar.gz
glibc-8d81ce0c6d6ca923571e8b2bac132929f9a02973.tar.xz
glibc-8d81ce0c6d6ca923571e8b2bac132929f9a02973.zip
Properly compute offsets of note descriptor and next note [BZ #22370]
A note header has 3 4-bytes fields, followed by note name and note
descriptor.  According to gABI, in a note entry, the note name field,
not note name size, is padded for the note descriptor.  And the note
descriptor field, not note descriptor size, is padded for the next
note entry.  Notes are aligned to 4 bytes in 32-bit objects and 8 bytes
in 64-bit objects.

For all GNU notes, the name is "GNU" which is 4 bytes.  They have the
same format in the first 16 bytes in both 32-bit and 64-bit objects.
They differ by note descriptor size and note type.  So far, .note.ABI-tag
and .note.gnu.build-id notes are always aligned to 4 bytes.  The exsting
codes compute the note size by aligning the note name size and note
descriptor size to 4 bytes.  It happens to produce the same value as
the actual note size by luck since the name size is 4 and offset of the
note descriptor is 16.  But it will produce the wrong size when note
alignment is 8 bytes in 64-bit objects.

This patch defines ELF_NOTE_DESC_OFFSET and ELF_NOTE_NEXT_OFFSET to
properly compute offsets of note descriptor and next note.  It uses
alignment of PT_NOTE segment to support both 4-byte and 8-byte note
alignments in 64-bit objects.  To handle PT_NOTE segments with
incorrect alignment, which may lead to an infinite loop, if segment
alignment is less than 4, we treate alignment as 4 bytes since some
note segments have 0 or 1 byte alignment.

	[BZ #22370]
	* elf/dl-hwcaps.c (ROUND): Removed.
	(_dl_important_hwcaps): Replace ROUND with ELF_NOTE_DESC_OFFSET
	and ELF_NOTE_NEXT_OFFSET.
	* elf/dl-load.c (ROUND): Removed.
	(open_verify): Replace ROUND with ELF_NOTE_NEXT_OFFSET.
	* elf/readelflib.c (ROUND): Removed.
	(process_elf_file): Replace ROUND with ELF_NOTE_NEXT_OFFSET.
	* include/elf.h [!_ISOMAC]: Include <libc-pointer-arith.h>.
	[!_ISOMAC] (ELF_NOTE_DESC_OFFSET): New.
	[!_ISOMAC] (ELF_NOTE_NEXT_OFFSET): Likewise.
Diffstat (limited to 'elf/readelflib.c')
-rw-r--r--elf/readelflib.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/elf/readelflib.c b/elf/readelflib.c
index 9ad56dcc34..3a303fff5f 100644
--- a/elf/readelflib.c
+++ b/elf/readelflib.c
@@ -131,15 +131,26 @@ process_elf_file (const char *file_name, const char *lib, int *flag,
 	      ElfW(Word) *abi_note = (ElfW(Word) *) (file_contents
 						     + segment->p_offset);
 	      ElfW(Addr) size = segment->p_filesz;
+	      /* NB: Some PT_NOTE segment may have alignment value of 0
+		 or 1.  gABI specifies that PT_NOTE segments should be
+		 aligned to 4 bytes in 32-bit objects and to 8 bytes in
+		 64-bit objects.  As a Linux extension, we also support
+		 4 byte alignment in 64-bit objects.  If p_align is less
+		 than 4, we treate alignment as 4 bytes since some note
+		 segments have 0 or 1 byte alignment.   */
+	      ElfW(Addr) align = segment->p_align;
+	      if (align < 4)
+		align = 4;
+	      else if (align != 4 && align != 8)
+		continue;
 
 	      while (abi_note [0] != 4 || abi_note [1] != 16
 		     || abi_note [2] != 1
 		     || memcmp (abi_note + 3, "GNU", 4) != 0)
 		{
-#define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word)))
-		  ElfW(Addr) note_size = 3 * sizeof (ElfW(Word))
-					 + ROUND (abi_note[0])
-					 + ROUND (abi_note[1]);
+		  ElfW(Addr) note_size
+		    = ELF_NOTE_NEXT_OFFSET (abi_note[0], abi_note[1],
+					    align);
 
 		  if (size - 32 < note_size || note_size == 0)
 		    {