When the dwfl_linux_proc_find_elf callback is used together with the
dwfl_linux_proc_report callback that reads /proc/PID/maps files we might
see and try to open special character device files that cannot be normally
read and processed by libelf (and might hang the library on the initial
read from the file). Make sure we only open and return regular files.

Signed-off-by: Mark Wielaard <[email protected]>
---
 libdwfl/ChangeLog         |  5 +++++
 libdwfl/linux-proc-maps.c | 11 +++++++++++
 2 files changed, 16 insertions(+)

diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 6e8fa31..5ddaec8 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,8 @@
+2013-12-28  Mark Wielaard  <[email protected]>
+
+       * linux-proc-maps.c (dwfl_linux_proc_find_elf): Don't return special
+       character device files, only regular files.
+
 2013-12-24  Mark Wielaard  <[email protected]>
 
        * linux-core-attach.c (core_next_thread): Check whether thread_argp
diff --git a/libdwfl/linux-proc-maps.c b/libdwfl/linux-proc-maps.c
index 8863cc8..cb081b1 100644
--- a/libdwfl/linux-proc-maps.c
+++ b/libdwfl/linux-proc-maps.c
@@ -29,6 +29,7 @@
 #include "libdwflP.h"
 #include <inttypes.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdio_ext.h>
@@ -348,6 +349,16 @@ dwfl_linux_proc_find_elf (Dwfl_Module *mod __attribute__ 
((unused)),
       int fd = open64 (module_name, O_RDONLY);
       if (fd >= 0)
        {
+         /* When this callback is used together with dwfl_linux_proc_report
+            then we might see mappings of special character devices.  Make
+            sure we only open and return regular files.  */
+         struct stat sb;
+         if (fstat(fd, &sb) == -1 || (sb.st_mode & S_IFMT) != S_IFREG)
+           {
+             close (fd);
+             return -1;
+           }
+
          *file_name = strdup (module_name);
          if (*file_name == NULL)
            {
-- 
1.8.4.2

Reply via email to