[PATCH] readelf: Fix bounds check in print_form_data.

Mark Wielaard mark@klomp.org
Mon Jun 11 00:18:00 GMT 2018


The afl fuzzer found that we did a wrong check in print_form_data when
comparing the remaining bytes in the buffer to an (unsigned) value read.
We were casting the value to ptrdiff_t which is a signed value and so
might turn a really big unsigned value into a negative number. Since we
know the difference between readendp and readp is zero or greater, we
should cast the pointer difference to size_t (and unsigned type) instead
before comparing with the unsigned value.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/ChangeLog |  5 +++++
 src/readelf.c | 14 +++++++-------
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 8ebb5fb..6484b9a 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2018-06-10  Mark Wielaard  <mark@klomp.org>
+
+	* readelf.c (print_form_data): Don't cast value to ptrdiff_t, cast
+	ptrdiff_t to size_t.
+
 2018-06-08  Mark Wielaard  <mark@klomp.org>
 
 	* readelf.c (print_debug_rnglists_section): Calculate max_entries
diff --git a/src/readelf.c b/src/readelf.c
index bbaaf96..fbda6c1 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -7880,7 +7880,7 @@ print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
       if (readendp - readp < 1)
 	goto invalid_data;
       get_uleb128 (val, readp, readendp);
-      if (readendp - readp < (ptrdiff_t) val)
+      if ((size_t) (readendp - readp) < val)
 	goto invalid_data;
       print_bytes (val, readp);
       readp += val;
@@ -7890,7 +7890,7 @@ print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
       if (readendp - readp < 1)
 	goto invalid_data;
       val = *readp++;
-      if (readendp - readp < (ptrdiff_t) val)
+      if ((size_t) (readendp - readp) < val)
 	goto invalid_data;
       print_bytes (val, readp);
       readp += val;
@@ -7900,7 +7900,7 @@ print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
       if (readendp - readp < 2)
 	goto invalid_data;
       val = read_2ubyte_unaligned_inc (dbg, readp);
-      if (readendp - readp < (ptrdiff_t) val)
+      if ((size_t) (readendp - readp) < val)
 	goto invalid_data;
       print_bytes (val, readp);
       readp += val;
@@ -7910,7 +7910,7 @@ print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
       if (readendp - readp < 2)
 	goto invalid_data;
       val = read_4ubyte_unaligned_inc (dbg, readp);
-      if (readendp - readp < (ptrdiff_t) val)
+      if ((size_t) (readendp - readp) < val)
 	goto invalid_data;
       print_bytes (val, readp);
       readp += val;
@@ -7941,7 +7941,7 @@ print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
     case DW_FORM_strp:
     case DW_FORM_line_strp:
     case DW_FORM_strp_sup:
-      if (readendp - readp < (ptrdiff_t) offset_len)
+      if ((size_t) (readendp - readp) < offset_len)
 	goto invalid_data;
       if (offset_len == 8)
 	val = read_8ubyte_unaligned_inc (dbg, readp);
@@ -7965,7 +7965,7 @@ print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
       break;
 
     case DW_FORM_sec_offset:
-      if (readendp - readp < (ptrdiff_t) offset_len)
+      if ((size_t) (readendp - readp) < offset_len)
 	goto invalid_data;
       if (offset_len == 8)
 	val = read_8ubyte_unaligned_inc (dbg, readp);
@@ -7988,7 +7988,7 @@ print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
 	{
 	  readp = data->d_buf + str_offsets_base + val;
 	  readendp = data->d_buf + data->d_size;
-	  if (readendp - readp < (ptrdiff_t) offset_len)
+	  if ((size_t) (readendp - readp) < offset_len)
 	    str = "???";
 	  else
 	    {
-- 
1.8.3.1



More information about the Elfutils-devel mailing list