public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [PATCH] libdw: Fix overflow warning on 32bit systems with GCC8 in dwarf_getsrclines.
Date: Wed, 30 May 2018 13:55:00 -0000	[thread overview]
Message-ID: <1527688492-10324-1-git-send-email-mark@klomp.org> (raw)

ndirs is read from the debug data and should be size checked before use.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libdw/ChangeLog           | 5 +++++
 libdw/dwarf_getsrclines.c | 6 ++++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index 5a33d9c..c304a3b 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,8 @@
+2018-05-30  Mark Wielaard  <mark@klomp.org>
+
+	* libdw/dwarf_getsrclines.c (read_srclines): Change ndir and
+	ndirlist to size_t. Add check to see ndirlist doesn't overflow.
+
 2018-05-29  Mark Wielaard  <mark@klomp.org>
 
 	* dwarf_cuoffset.c (dwarf_cuoffset): Check die->cu is not NULL.
diff --git a/libdw/dwarf_getsrclines.c b/libdw/dwarf_getsrclines.c
index 2bf3098..790d4e4 100644
--- a/libdw/dwarf_getsrclines.c
+++ b/libdw/dwarf_getsrclines.c
@@ -154,7 +154,7 @@ read_srclines (Dwarf *dbg,
   int res = -1;
 
   size_t nfilelist = 0;
-  unsigned int ndirlist = 0;
+  size_t ndirlist = 0;
 
   /* If there are a large number of lines, files or dirs don't blow up
      the stack.  Stack allocate some entries, only dynamically malloc
@@ -299,7 +299,7 @@ read_srclines (Dwarf *dbg,
   };
 
   /* First count the entries.  */
-  unsigned int ndirs = 0;
+  size_t ndirs = 0;
   if (version < 5)
     {
       const unsigned char *dirp = linep;
@@ -359,6 +359,8 @@ read_srclines (Dwarf *dbg,
   ndirlist = ndirs;
   if (ndirlist >= MAX_STACK_DIRS)
     {
+      if (ndirlist > SIZE_MAX / sizeof (*dirarray))
+	goto no_mem;
       dirarray = (struct dirlist *) malloc (ndirlist * sizeof (*dirarray));
       if (unlikely (dirarray == NULL))
 	{
-- 
1.8.3.1

             reply	other threads:[~2018-05-30 13:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-30 13:55 Mark Wielaard [this message]
2018-05-31 19:54 ` 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=1527688492-10324-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).