This is the mail archive of the elfutils-devel@sourceware.org mailing list for the elfutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] libdwfl: dwfl_linux_proc_find_elf should only return regular files.


The original fix still opened the files and then checked whether they were
actually regular files with fstat. But some special device files hang the
open call because they can only be opened once (and the process we are
inspecting already have it open). So swap the fstat with a stat check that
is done before even trying to open the file.

<-- snip -->

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
open or read from the file). Make sure we only try to open and return
regular files.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
---
 libdwfl/ChangeLog         | 5 +++++
 libdwfl/linux-proc-maps.c | 9 +++++++++
 2 files changed, 14 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  <mjw@redhat.com>
+
+	* linux-proc-maps.c (dwfl_linux_proc_find_elf): Don't return special
+	character device files, only regular files.
+
 2013-12-24  Mark Wielaard  <mjw@redhat.com>
 
 	* 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..b1f8b33 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>
@@ -345,6 +346,14 @@ dwfl_linux_proc_find_elf (Dwfl_Module *mod __attribute__ ((unused)),
 {
   if (module_name[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.  Special devices
+	 might hang on open or read.  */
+      struct stat sb;
+      if (stat (module_name, &sb) == -1 || (sb.st_mode & S_IFMT) != S_IFREG)
+	return -1;
+
       int fd = open64 (module_name, O_RDONLY);
       if (fd >= 0)
 	{
-- 
1.8.4.2


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]