From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [PATCH] readelf: Fix bounds check in print_form_data.
Date: Mon, 11 Jun 2018 00:18:00 -0000 [thread overview]
Message-ID: <1528676283-13515-1-git-send-email-mark@klomp.org> (raw)
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
next reply other threads:[~2018-06-11 0:18 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-11 0:18 Mark Wielaard [this message]
2018-06-11 15:52 ` Mark Wielaard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1528676283-13515-1-git-send-email-mark@klomp.org \
--to=mark@klomp.org \
--cc=elfutils-devel@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).