From 146456c537de5ac7c80608f88babbba026cca03b Mon Sep 17 00:00:00 2001
From: Mark Wielaard <mark@klomp.org>
Date: Sat, 18 Aug 2018 19:51:27 +0200
Subject: [PATCH 1/2] libdw: Check end of attributes list consistently.

dwarf_child (__libdw_find_attr), dwarf_getabbrevattr[_data] and
dwarf_getattrs all assume the end of the attribute list is when
both the name (code) and form of the attribute are zero.

dwarf_getabbrev (__libdw_getabbrev) and dwarf_hasattr assume the
end of the attribute list is when either the name (code) or the
form of the attribute is zero.

The DWARF spec says: "The series of attribute specifications ends
with an entry containing 0 for the name and 0 for the form." So
the first check is correct.

Make sure dwarf_getabbrev and dwarf_hasattr use the same check.
This is important since all other functions expect dwarf_getabbrev
(__libdw_getabbrev) to have done a data sanity check of the attribute.
So if the ending condition is different it could cause a crash.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>

Upstream-Status: Backport [https://sourceware.org/git/?p=elfutils.git;a=commit;h=6983e59b727458a6c64d9659c85f08218bc4fcda]
CVE: CVE-2018-16403

Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
---
 libdw/ChangeLog         | 7 +++++++
 libdw/dwarf_getabbrev.c | 2 +-
 libdw/dwarf_hasattr.c   | 4 ++--
 3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index 9e43ea9..f3cf5d3 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,5 +1,12 @@
 2018-08-18  Mark Wielaard  <mark@klomp.org>
 
+	* dwarf_getabbrev.c (__libdw_getabbrev): Continue until both name
+	and form are zero.
+	* dwarf_hasattr.c (dwarf_hasattr): Stop when both name and form
+	are zero.
+
+2018-08-18  Mark Wielaard  <mark@klomp.org>
+
 	* dwarf_getaranges.c (dwarf_getaranges.c): Make sure there is enough
 	data to read the address and segment size.
 
diff --git a/libdw/dwarf_getabbrev.c b/libdw/dwarf_getabbrev.c
index 988d12c..6a7e981 100644
--- a/libdw/dwarf_getabbrev.c
+++ b/libdw/dwarf_getabbrev.c
@@ -140,7 +140,7 @@ __libdw_getabbrev (Dwarf *dbg, struct Dwarf_CU *cu, Dwarf_Off offset,
 	  get_sleb128 (formval, abbrevp, end);
 	}
     }
-  while (attrname != 0 && attrform != 0);
+  while (attrname != 0 || attrform != 0);
 
   /* Return the length to the caller if she asked for it.  */
   if (lengthp != NULL)
diff --git a/libdw/dwarf_hasattr.c b/libdw/dwarf_hasattr.c
index 90053b1..eca0839 100644
--- a/libdw/dwarf_hasattr.c
+++ b/libdw/dwarf_hasattr.c
@@ -60,8 +60,8 @@ dwarf_hasattr (Dwarf_Die *die, unsigned int search_name)
       unsigned int attr_form;
       get_uleb128_unchecked (attr_form, attrp);
 
-      /* We can stop if we found the attribute with value zero.  */
-      if (attr_name == 0 || attr_form == 0)
+      /* We can stop if we found the end of the attribute list.  */
+      if (attr_name == 0 && attr_form == 0)
 	return 0;
 
       if (attr_name == search_name)
-- 
2.7.4

